:root {
  --bg: #f5f6f8;
  --panel-bg: #ffffff;
  --border: #d8dce1;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-weak: #eef2ff;
  --low: #d92d20;
  --low-weak: #fef3f2;
  --error-bg: #fef3f2;
  --error-border: #f5c2c0;
  --radius: 8px;
  --gap: 16px;
  /* Minimum height of anything tapped on a phone. */
  --touch: 44px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* --- top bar -------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 8px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: 0.02em; }
.nav { display: flex; flex-wrap: wrap; gap: 4px; }
.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: var(--touch);
  padding: 0 14px;
  text-decoration: none;
  color: var(--muted);
  border-radius: var(--radius);
  font-weight: 500;
}
.nav-link.is-active { color: var(--accent); background: var(--accent-weak); }
.badge {
  display: inline-block; min-width: 1.6em; padding: 0 7px; margin-left: 6px;
  border-radius: 999px; background: var(--low); color: #fff;
  font-size: 0.85rem; font-weight: 700; text-align: center;
}
.poll-status { margin-left: auto; color: var(--muted); font-size: 0.9rem; }
.poll-status.is-stale { color: var(--low); font-weight: 600; }

/* --- page ------------------------------------------------------------------ */
.content { max-width: 1100px; margin: 0 auto; padding: 24px; }
.page-title { font-size: 1.5rem; margin: 0 0 4px; overflow-wrap: anywhere; }
.subtitle { color: var(--muted); margin: 0 0 16px; }
.crumb { margin: 0 0 12px; }
.crumb a { display: inline-flex; align-items: center; min-height: var(--touch); }
.crumb a, .sku-link { color: var(--accent); text-decoration: none; }
.sku-link:hover { text-decoration: underline; }

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: var(--gap);
  /* Wide tables scroll inside their panel instead of widening the page. */
  overflow-x: auto;
}
.panel-title { font-size: 1.05rem; margin: 0 0 12px; }
.hint { font-weight: 400; color: var(--muted); font-size: 0.85rem; margin-left: 6px; }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
}
.panel-head .panel-title { margin: 0; }

/* --- form controls: large enough to tap; 16px text stops iOS zooming in ---- */
input[type="number"], input[type="text"], input[type="search"], select {
  min-height: var(--touch);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  font-size: 16px;
  background: #fff;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.search-box { min-width: 220px; flex: 1 1 220px; max-width: 360px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  min-width: var(--touch);
  padding: 0 20px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { filter: brightness(0.92); }
.btn[aria-busy="true"] { opacity: 0.6; pointer-events: none; }
.btn-small { padding: 0 14px; }
.btn-danger { background: var(--low); color: #fff; }
.btn-secondary { background: var(--accent-weak); color: var(--accent); }

.op-form { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.op-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }
.inline-form { display: inline-block; margin: 2px 0; }
.filter-form { margin-bottom: 12px; }
.reassign-form .op-form { margin-bottom: 8px; }

/* Produce quantity: -1 [number] +1 */
.stepper { display: flex; align-items: stretch; gap: 8px; }
.stepper input { width: 6em; min-width: 0; text-align: center; font-size: 1.2rem; font-weight: 600; }
.btn-step { min-width: 56px; padding: 0 10px; font-size: 1.05rem; }

/* --- tables ------------------------------------------------------------------ */
.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td {
  text-align: left;
  padding: 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.grid th { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.grid tr:last-child td { border-bottom: none; }
.grid .sku-link { display: inline-block; padding: 6px 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); text-align: center; }
.ts { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.actions { white-space: nowrap; text-align: right; }
.row-error { background: var(--low-weak); }
.row-error td { color: var(--low); }
.row-warning { background: #fffbeb; }

/* Sortable headers: the label is a borderless button so tapping anywhere on
   the header cell sorts, with an arrow indicator via aria-sort. */
.sortable th { padding: 0; }
.th-sort {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  min-height: var(--touch);
  padding: 0 10px;
  border: none;
  background: none;
  font: inherit;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  cursor: pointer;
  text-align: left;
}
.sortable th.num .th-sort { justify-content: flex-end; }
.th-sort:hover { color: var(--text); }
.th-sort::after { content: "\2195"; font-size: 0.85em; opacity: 0.35; }
.sortable th[aria-sort="ascending"] .th-sort::after { content: "\2191"; opacity: 1; }
.sortable th[aria-sort="descending"] .th-sort::after { content: "\2193"; opacity: 1; }
.sortable th[aria-sort] .th-sort { color: var(--text); }

/* --- stock state ---------------------------------------------------------------- */
.is-low { color: var(--low); font-weight: 600; }
.low-label {
  display: inline-block;
  margin-left: 6px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--low);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1.6;
  vertical-align: middle;
}

.tag {
  display: inline-block;
  font-size: 0.75rem;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  vertical-align: middle;
}

.qty-current { font-size: 1.1rem; }
.qty-current strong { font-size: 1.6rem; }

.bom { display: flex; flex-wrap: wrap; gap: var(--gap); }
.bom-col { flex: 1 1 200px; }
.bom-list { list-style: none; margin: 0; padding: 0; }
.bom-list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.bom-list li:last-child { border-bottom: none; }
.ratio { color: var(--muted); font-variant-numeric: tabular-nums; }
.bom-col .empty { text-align: left; }

.alert { padding: 12px 14px; border-radius: var(--radius); margin-bottom: var(--gap); }
.alert-error { background: var(--error-bg); border: 1px solid var(--error-border); color: var(--low); }

.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 12px; }

/* --- phones ------------------------------------------------------------------------ */
@media (max-width: 640px) {
  .desktop-only { display: none; }

  /* Tables marked .cards become one card per row: the main cell first, then
     one "label  value" line per other cell, buttons last and visible. */
  .grid.cards thead { display: none; }
  .grid.cards, .grid.cards tbody, .grid.cards tr, .grid.cards td { display: block; width: 100%; }
  .grid.cards tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  .grid.cards tr:last-child { border-bottom: none; }
  /* Labels sit in a fixed-width column with each value right beside them, so a
     label and its value read as a pair instead of sitting at opposite edges. */
  .grid.cards td {
    display: flex; justify-content: flex-start; align-items: baseline; gap: 8px;
    padding: 3px 0; border: none; text-align: left; white-space: normal;
  }
  .grid.cards td::before {
    content: attr(data-label);
    color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em;
    text-align: left; flex: 0 0 9.5em;
  }
  .grid.cards td .hint { margin-left: 0; }
  .grid.cards td.primary { display: block; text-align: left; font-weight: 500; }
  .grid.cards td.primary::before, .grid.cards td.actions::before, .grid.cards td.empty::before { content: none; }
  .grid.cards td.actions { justify-content: flex-start; flex-wrap: wrap; padding-top: 8px; }
  .grid.cards td.empty { display: block; text-align: center; }
  .grid.cards .row-error, .grid.cards .row-warning { padding-left: 8px; padding-right: 8px; border-radius: 6px; }

  /* Inventory list: SKU on top, name below in muted text, quantity large on
     the right spanning both lines. The header stays as a one-line sort bar. */
  #sku-table, #sku-table thead, #sku-table tbody { display: block; }
  #sku-table thead tr { display: grid; grid-template-columns: auto auto 1fr; }
  #sku-table thead th { border-bottom: 1px solid var(--border); }
  #sku-table thead th.num .th-sort { justify-content: flex-end; }
  #sku-table tr.data-row {
    display: grid; grid-template-columns: 1fr auto; column-gap: 12px;
    padding: 10px 0; border-bottom: 1px solid var(--border);
  }
  #sku-table tr.data-row:last-child { border-bottom: none; }
  #sku-table tr.data-row[hidden] { display: none; }
  #sku-table tr.data-row td { display: block; padding: 0; border: none; }
  #sku-table tr.data-row td:nth-child(1) { grid-column: 1; grid-row: 1; }
  #sku-table tr.data-row td:nth-child(2) { grid-column: 1; grid-row: 2; color: var(--muted); font-size: 0.9rem; }
  #sku-table tr.data-row td:nth-child(3) {
    grid-column: 2; grid-row: 1 / span 2; align-self: center;
    font-size: 1.25rem; font-weight: 600;
  }
  #sku-table .sku-link { padding: 2px 0; font-weight: 500; }

  /* Reassign: inventory picker and quantity side by side. */
  .reassign-row { display: grid; grid-template-columns: 2fr 1fr; gap: 8px; }
  .reassign-row label { width: auto; }

  .topbar { padding: 6px 10px; }
  .brand { width: 100%; }
  .nav { width: 100%; flex-wrap: nowrap; gap: 0; justify-content: space-between; }
  .nav-link { padding: 0 6px; font-size: 0.95rem; white-space: nowrap; }
  .poll-status { margin-left: 0; width: 100%; font-size: 0.85rem; }
  .content { padding: 12px; }
  .panel { padding: 12px; }
  .page-title { font-size: 1.3rem; }
  .search-box { max-width: none; }
  .op-form { align-items: stretch; }
  .op-form label { width: 100%; }
  .op-form label input, .op-form label select { width: 100%; }
  .op-form > .btn { width: 100%; }
  .stepper { width: 100%; }
  .stepper input { flex: 1 1 0; width: 0; }
}
