:root {
  --bg: #f4f5f8;
  --card: #ffffff;
  --border: #e3e6ec;
  --text: #1a1d24;
  --muted: #667085;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-light: #eef0fe;
  --accent: #d97706;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-row: 0 2px 6px rgba(16, 24, 40, 0.10), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161c;
    --card: #1c1f27;
    --border: #2c2f3a;
    --text: #e7e9ee;
    --muted: #8b93a3;
    --primary: #6366f1;
    --primary-dark: #818cf8;
    --primary-light: #24263a;
    --accent: #f59e0b;
    --danger: #f87171;
    --danger-light: #2a1a1c;
    --success: #4ade80;
    --success-light: #16241c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 6px rgba(0, 0, 0, 0.25);
    --shadow-row: 0 3px 8px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14161c;
  --card: #1c1f27;
  --border: #2c2f3a;
  --text: #e7e9ee;
  --muted: #8b93a3;
  --primary: #6366f1;
  --primary-dark: #818cf8;
  --primary-light: #24263a;
  --accent: #f59e0b;
  --danger: #f87171;
  --danger-light: #2a1a1c;
  --success: #4ade80;
  --success-light: #16241c;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 6px rgba(0, 0, 0, 0.25);
  --shadow-row: 0 3px 8px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Nav (sidebar) ---------- */

.nav {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 30;
  width: 250px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 18px 14px;
  overflow-y: auto;
}

.nav .brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 800;
  font-size: 1.2rem;
  letter-spacing: -0.01em;
  color: var(--text);
  padding: 4px 8px 22px;
}
.nav .brand:hover { text-decoration: none; }
.nav .brand svg { flex: none; color: var(--accent); }

.nav-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  cursor: pointer;
  color: var(--text);
}

.nav .links {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  font-size: 0.92rem;
  flex: 1;
  min-height: 0;
}

.nav .links a.nav-link {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
}
.nav .links a.nav-link:hover { background: var(--primary-light); color: var(--text); text-decoration: none; }
.nav .links a.nav-link.active { background: var(--primary-light); color: var(--primary-dark); }
.nav-icon { flex: none; opacity: 0.8; }
.nav .links a.nav-link.active .nav-icon { opacity: 1; }

.nav-group-label {
  margin: 16px 10px 6px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  opacity: 0.75;
}

.nav-more { margin-top: 4px; }
.nav-more-summary {
  cursor: pointer;
  list-style: none;
}
.nav-more-summary::-webkit-details-marker { display: none; }
.nav-more-links {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 6px 0 2px 10px;
  padding-left: 10px;
  border-left: 3px solid var(--border);
}

.nav-footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.nav-footer .signed-in { color: var(--muted); font-size: 0.85rem; padding: 0 4px; }
.nav-footer form { margin: 0; }
.nav-footer form button { width: 100%; }

/* ---------- Layout ---------- */

.container {
  max-width: 1100px;
  margin: 28px auto 28px 290px;
  padding: 0 24px;
}

.container.no-sidebar {
  margin: 24px auto;
}

.card {
  background: var(--card);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 18px;
}

h1 { font-size: 1.4rem; margin: 0 0 12px; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; margin: 0 0 10px; letter-spacing: -0.01em; }

.muted { color: var(--muted); font-size: 0.88rem; }

/* ---------- Forms ---------- */

input, select, textarea {
  padding: 7px 9px;
  border: 3px solid var(--border);
  border-radius: 7px;
  font-size: 0.95rem;
  width: 100%;
  font-family: inherit;
  transition: border-color 0.15s ease;
}
input, textarea {
  background: var(--card);
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

/* Deliberately NOT setting background/color on select: Chromium draws the
   open dropdown list (and its hovered/selected row) as native OS chrome,
   and once a <select> has an author background/color, Chrome stops
   applying its own dark-aware palette to that popup — so the list falls
   back to plain light colors while still inheriting our light *text*
   color, making the highlighted row unreadable. Leaving background/color
   off select and relying on color-scheme (set per-theme on :root) lets
   Chrome render the closed box AND the open list with one consistent,
   correctly-contrasted native palette instead of a partial override that
   only styles the box and breaks the list. */

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 10px 0 4px; font-weight: 500; }

.btn {
  display: inline-block;
  padding: 9px 16px;
  border-radius: 7px;
  border: none;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: background 0.15s ease, transform 0.05s ease;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: var(--primary-light); color: var(--primary-dark); }
.btn.secondary:hover { background: var(--border); }
.btn.danger { background: var(--danger); }

/* ---------- Tables ---------- */

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 2px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
tr:hover td { background: var(--bg); }

.qty-low { color: var(--danger); font-weight: 700; }
.qty-ok { color: var(--success); font-weight: 600; }

.search-bar { margin-bottom: 14px; }

.error {
  background: var(--danger-light);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: 7px;
  margin-bottom: 14px;
  font-size: 0.9rem;
  font-weight: 500;
}

.success {
  background: var(--success-light);
  border: 1px solid var(--success);
  color: var(--success);
  padding: 10px 14px;
  border-radius: 7px;
  margin-bottom: 14px;
  font-size: 0.9rem;
  font-weight: 500;
}

.inline-form { display: inline-flex; gap: 6px; align-items: center; }
.inline-form input[type=number] { width: 70px; }

.pill {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  background: var(--primary-light);
  color: var(--muted);
}
.pill.take { background: var(--danger-light); color: var(--danger); }
.pill.restock { background: var(--success-light); color: var(--success); }
.pill.create { background: var(--primary-light); color: var(--primary-dark); }
.pill.transfer { background: var(--primary-light); color: var(--accent); }

.actions-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 14px; }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}
.empty-state svg { color: var(--border); margin-bottom: 10px; }

.skeleton-row td {
  padding: 14px 10px;
}
.skeleton-bar {
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.3s ease-in-out infinite;
}
@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.btn.scan {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--primary-light);
  color: var(--primary-dark);
}
.btn.scan:hover { background: var(--border); }

.scanner-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.scanner-modal[hidden] { display: none; }
.scanner-modal-inner {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  max-width: 420px;
  width: 100%;
  box-shadow: var(--shadow-row);
}
.scanner-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-weight: 700;
}
#scanner-reader { width: 100%; border-radius: 8px; overflow: hidden; background: #000; min-height: 200px; }

/* ---------- Mobile ---------- */

@media (max-width: 860px) {
  .nav-toggle { display: inline-flex; align-items: center; justify-content: center; }

  .nav {
    position: sticky;
    top: 0; left: auto; right: auto; bottom: auto;
    width: auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-right: none;
    border-bottom: 2px solid var(--border);
    overflow-y: visible;
  }

  .nav .brand { padding: 0; }

  .nav .links {
    display: none;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    order: 3;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 2px solid var(--border);
    gap: 2px;
    flex: none;
  }
  .nav .links.open {
    display: flex;
    max-height: calc(100vh - 64px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .nav .links a.nav-link { padding: 10px 8px; }
  .nav-group-label { margin: 10px 8px 4px; }
  .nav-footer { margin-top: 10px; padding-top: 10px; }

  .container { margin-left: auto; }
}

@media (max-width: 720px) {
  .container { margin: 14px auto; padding: 0 10px; }
  .card { padding: 14px; }

  h1 { font-size: 1.25rem; }

  input, select, textarea, .btn, button {
    font-size: 1rem;
    min-height: 42px;
  }

  .inline-form { flex-wrap: wrap; width: 100%; gap: 8px; }
  .inline-form input { flex: 1 1 auto; width: auto; min-width: 70px; }
  .inline-form input[name="note"] { flex-basis: 100%; width: 100%; }
  .inline-form button { flex: 1 1 100%; }

  .actions-row { flex-direction: column; align-items: stretch; }
  .actions-row form { width: 100%; }
  .actions-row .btn, .actions-row button { width: 100%; }

  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td {
    display: block;
    width: 100%;
  }
  table.responsive tr[hidden] { display: none; }
  table.responsive tr {
    border: 3px solid var(--border);
    border-left: 4px solid var(--primary);
    border-radius: 10px;
    margin-bottom: 12px;
    padding: 6px 14px;
    background: var(--card);
    box-shadow: var(--shadow-row);
  }
  table.responsive tr:active { background: var(--primary-light); }
  table.responsive tr:hover td { background: transparent; }
  table.responsive td {
    border: none;
    padding: 8px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    text-align: right;
  }
  table.responsive td:not(:last-child) { border-bottom: 2px solid var(--border); }
  table.responsive td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    text-align: left;
  }

  /* Compact/expandable rows — a two-line summary by default (identifier +
     qty/status together on line 1, description on line 2), tap to reveal
     the rest of the row. The row itself becomes the flex container so
     .rc-primary/.rc-primary-end cells can share line 1 via `order`
     regardless of their actual column position in the markup (which has
     to stay in <thead>-matching order for the desktop table). */
  table.responsive.compact-rows tr[data-expandable] {
    position: relative;
    cursor: pointer;
    padding-right: 28px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 10px;
  }
  table.responsive.compact-rows tr[data-expandable] td.rc-summary {
    display: flex;
    align-items: center;
    text-align: left;
    border-bottom: none;
    width: auto;
    order: 2;
    flex: 0 1 100%;
  }
  table.responsive.compact-rows tr[data-expandable] td.rc-primary {
    order: 1;
    flex: 0 0 auto;
  }
  table.responsive.compact-rows tr[data-expandable] td.rc-primary-end {
    order: 1;
    flex: 0 0 auto;
    margin-left: auto;
  }
  table.responsive.compact-rows tr[data-expandable] td.rc-summary::before { content: none; }
  /* Collapsed: one clipped line, so the row stays a clean 2-line summary
     (item/qty + a single description line) no matter how long the real
     description is. Tap to expand for the full text — see the .expanded
     override below. */
  table.responsive.compact-rows tr[data-expandable] td.rc-summary .rc-desc {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
    color: var(--muted);
    font-size: 0.85rem;
  }
  /* Expanded: show the full description, wrapping onto as many lines as it
     needs. overflow-wrap handles a long unbroken word (common in distributor
     SKU-style descriptions) so it wraps instead of spilling past the card. */
  table.responsive.compact-rows tr[data-expandable].expanded td.rc-summary .rc-desc {
    overflow: visible;
    text-overflow: clip;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  table.responsive.compact-rows tr[data-expandable] td:not(.rc-summary) {
    display: none;
    width: auto;
    order: 3;
    flex: 0 0 100%;
  }
  table.responsive.compact-rows tr[data-expandable].expanded td:not(.rc-summary) { display: flex; }
  table.responsive.compact-rows tr[data-expandable]::after {
    content: '▸';
    position: absolute;
    right: 14px;
    top: 14px;
    color: var(--muted);
    transition: transform 0.15s;
  }
  table.responsive.compact-rows tr[data-expandable].expanded::after { transform: rotate(90deg); }
}

/* ---------- Print ---------- */

@media print {
  .nav, .search-bar, .actions-row, form, .btn, button, .scanner-modal, .empty-state svg { display: none !important; }
  body { background: #fff; color: #000; }
  .container { margin: 0; padding: 0; max-width: 100%; }
  .card { border: none; box-shadow: none; padding: 0; margin: 0 0 16px; }
  .print-header { display: block !important; margin-bottom: 14px; }
  table.responsive thead { display: table-header-group !important; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td {
    display: revert !important;
    width: auto !important;
  }
  table.responsive tr { border: none !important; box-shadow: none !important; margin: 0 !important; padding: 0 !important; }
  table.responsive td {
    border-bottom: 1px solid #ccc !important;
    padding: 6px 8px !important;
    display: table-cell !important;
    text-align: left !important;
  }
  table.responsive td::before { content: none !important; }
  th, td { color: #000 !important; }
  a { color: #000 !important; text-decoration: none !important; }
  .qty-low { color: #b91c1c !important; font-weight: 700; }
  .qty-ok { color: #15803d !important; }
  .pill { border: 1px solid #999; background: none !important; color: #000 !important; }
  .no-print { display: none !important; }
}

.print-header { display: none; }
