:root {
  color-scheme: light dark;
  --bg: #f6f4ef; --surface: #ffffff; --surface-2: #eeeae1; --ink: #1d1b17; --ink-dim: #6a655a;
  --line: rgba(29, 27, 23, 0.13);
  --accent: #2f6b4f; --accent-ink: #ffffff; --accent-soft: rgba(47, 107, 79, 0.12);
  --good: #217346; --warn: #a86a12; --bad: #b3372b;
  --good-soft: rgba(33, 115, 70, 0.12); --warn-soft: rgba(168, 106, 18, 0.14); --bad-soft: rgba(179, 55, 43, 0.12);
  --shadow: 0 1px 2px rgba(29, 27, 23, 0.06);
  --heat: 47, 107, 79;
  --tabbar-h: 4.6rem;
  /* chart series (validated categorical order: blue, orange, aqua, yellow) + diverging negative */
  --s1: #257de0; --s2: #f47230; --s3: #00b894; --s4: #f2a600; --neg: #ec4647;
  --grid: rgba(29, 27, 23, 0.09); --axis: rgba(29, 27, 23, 0.28);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --surface: #1c2027; --surface-2: #252b34; --ink: #ebe8e1; --ink-dim: #9d998f;
    --line: rgba(235, 232, 225, 0.11);
    --accent: #5fb98c; --accent-ink: #0d1a13; --accent-soft: rgba(95, 185, 140, 0.15);
    --good: #5fc48a; --warn: #e2a64a; --bad: #ec6a5c;
    --good-soft: rgba(95, 196, 138, 0.14); --warn-soft: rgba(226, 166, 74, 0.15); --bad-soft: rgba(236, 106, 92, 0.15);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --heat: 95, 185, 140;
    --s1: #338ced; --s2: #e76624; --s3: #00a888; --s4: #d28e05; --neg: #eb6263;
    --grid: rgba(235, 232, 225, 0.08); --axis: rgba(235, 232, 225, 0.3);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
a { color: var(--accent); }
h1 { font-size: 1.6rem; font-weight: 800; letter-spacing: -0.02em; margin: 0; }
h2 { font-size: 0.78rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-dim); font-weight: 700; margin: 1.8rem 0 0.7rem; }
h3 { font-size: 1.1rem; margin: 0 0 0.3rem; }
.muted { color: var(--ink-dim); font-size: 0.92rem; margin: 0.15rem 0 0; }
.num { font-variant-numeric: tabular-nums; }
.good { color: var(--good); } .bad { color: var(--bad); } .warn { color: var(--warn); }

/* shell */
.app-shell { display: flex; min-height: 100vh; }
.main-col { flex: 1; min-width: 0; }
.page-wrap { width: 100%; max-width: 1280px; margin: 0 auto; padding: 1rem 1rem calc(var(--tabbar-h) + 1.2rem + env(safe-area-inset-bottom, 0px)); }
.brand-row { display: flex; align-items: center; gap: 0.6rem; text-decoration: none; color: var(--ink); }
.brand-row .word { font-size: 1.2rem; font-weight: 800; letter-spacing: -0.01em; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0.6rem 1rem; border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; z-index: 5; }
.top-settings { color: var(--ink-dim); padding: 0.4rem; display: flex; }
.sidebar { display: none; width: 240px; flex: none; border-right: 1px solid var(--line); background: var(--surface); padding: 1.2rem 0.8rem; flex-direction: column; gap: 1.6rem; position: sticky; top: 0; height: 100vh; }
.side-nav { display: flex; flex-direction: column; gap: 0.2rem; }
.side-nav a, .side-settings { display: flex; align-items: center; gap: 0.75rem; padding: 0.7rem 0.8rem; border-radius: 10px; font-weight: 600; color: var(--ink-dim); text-decoration: none; }
.side-settings { margin-top: auto; }
.side-nav a.active, .side-settings.active { background: var(--accent-soft); color: var(--accent); }
.tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; background: var(--surface); border-top: 1px solid var(--line); padding: 0.35rem 0.2rem calc(0.35rem + env(safe-area-inset-bottom, 0px)); }
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 0.1rem; padding: 0.3rem 0; color: var(--ink-dim); font-size: 0.72rem; font-weight: 650; text-decoration: none; }
.tabbar a.active { color: var(--accent); }
.tabbar a.tab-scan .ic { background: var(--accent); color: var(--accent-ink); border-radius: 14px; padding: 6px; width: 44px; height: 38px; margin-top: -0.2rem; }
@media (min-width: 860px) {
  .topbar, .tabbar { display: none; }
  .sidebar { display: flex; }
  .page-wrap { padding: 1.6rem 2rem 2rem; }
}

/* store switcher */
.store-switch { display: grid; gap: 0.25rem; padding: 0 0.3rem; }
.store-switch label { display: grid; gap: 0.25rem; }
.store-label { font-size: 0.72rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-dim); }
.store-name { font-weight: 700; }
.store-switch select { min-height: 42px; padding: 0.4rem 0.6rem; font-weight: 650; }
.store-new { font-size: 0.88rem; text-decoration: none; font-weight: 600; }
.store-switch.compact { flex: 1; min-width: 0; margin: 0 0.6rem; padding: 0; }
.store-switch.compact .store-label { display: none; }
.store-switch.compact .store-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.store-switch.compact select { min-height: 40px; }

/* page pieces */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
.head-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.flash { background: var(--good-soft); color: var(--good); border-radius: 10px; padding: 0.75rem 1rem; font-weight: 600; margin-bottom: 1rem; }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.card + .card { margin-top: 0.9rem; }
.grid { display: grid; gap: 0.8rem; }
.tiles { display: grid; gap: 0.7rem; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 700px) { .tiles { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); } }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 0.85rem 1rem; box-shadow: var(--shadow); min-width: 0; }
.tile-label { font-size: 0.82rem; color: var(--ink-dim); font-weight: 600; }
.tile-value { font-size: 1.6rem; font-weight: 800; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 0.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-sub { font-size: 0.82rem; color: var(--ink-dim); margin-top: 0.1rem; }
.two-col { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 1000px) { .two-col { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }

/* buttons & forms */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.45rem; min-height: 46px; padding: 0.55rem 1.1rem; border-radius: 11px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; font-weight: 650; text-decoration: none; cursor: pointer; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 36px; padding: 0.3rem 0.75rem; font-size: 0.9rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
form.stack { display: grid; gap: 0.9rem; }
.field { display: grid; gap: 0.3rem; min-width: 0; }
.field > span { font-weight: 650; font-size: 0.92rem; }
.field small { color: var(--ink-dim); }
.row { display: grid; gap: 0.8rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: start; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 0.65rem 0.75rem; min-height: 46px; width: 100%; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.seg label { flex: 1 1 auto; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: block; text-align: center; padding: 0.6rem 0.7rem; border: 1px solid var(--line); border-radius: 10px; font-weight: 600; cursor: pointer; background: var(--bg); }
.seg input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

/* tables & lists */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
th, td { padding: 0.65rem 0.8rem; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-dim); font-weight: 700; background: var(--surface-2); position: sticky; top: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; min-width: 14rem; }
tr:last-child td { border-bottom: 0; }
tr.link-row { cursor: pointer; }
tr.link-row:hover td { background: var(--accent-soft); }
.list { display: grid; gap: 0.6rem; grid-template-columns: minmax(0, 1fr); }
.item { display: flex; gap: 0.8rem; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 0.75rem 0.9rem; text-decoration: none; color: var(--ink); box-shadow: var(--shadow); }
.item:hover { border-color: var(--accent); }
.item .thumb { width: 52px; height: 52px; border-radius: 10px; background: var(--surface-2); flex: none; object-fit: contain; display: flex; align-items: center; justify-content: center; color: var(--ink-dim); overflow: hidden; }
.item .thumb img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.item .main { flex: 1; min-width: 0; }
.item .title { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 0.85rem; color: var(--ink-dim); display: flex; flex-wrap: wrap; gap: 0.2rem 0.7rem; }
.item .side { text-align: right; flex: none; }
.item .side .big { font-weight: 800; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.pill { display: inline-block; padding: 0.12rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 700; background: var(--surface-2); color: var(--ink-dim); white-space: nowrap; }
.pill.in_stock { background: var(--accent-soft); color: var(--accent); }
.pill.listed { background: var(--warn-soft); color: var(--warn); }
.pill.sold { background: var(--good-soft); color: var(--good); }
.pill.trashed, .pill.donated, .pill.kept, .pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.star { background: var(--good-soft); color: var(--good); }
.pill.avoid { background: var(--bad-soft); color: var(--bad); }
.toolbar { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.toolbar input[type=search] { flex: 1 1 220px; }
.toolbar select { flex: 1 1 140px; width: auto; min-width: 0; }
.toolbar .btn { flex: 1 1 100px; }
.empty { text-align: center; padding: 2.4rem 1rem; background: var(--surface); border: 1px dashed var(--line); border-radius: 14px; }
.empty .btn { margin-top: 0.9rem; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 0.35rem 1rem; margin: 0; }
dl.facts dt { color: var(--ink-dim); font-size: 0.9rem; }
dl.facts dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }

/* charts */
.bars { display: grid; gap: 0.45rem; }
.bar-row { display: grid; grid-template-columns: 7.5rem 1fr 5.5rem; gap: 0.6rem; align-items: center; font-size: 0.9rem; }
.bar-row .label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { position: relative; height: 22px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.bar-fill { position: absolute; top: 0; bottom: 0; background: var(--accent); border-radius: 6px; }
.bar-fill.neg { background: var(--bad); }
.bar-row .val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 650; }
.trend { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding-top: 0.5rem; }
.trend .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.trend .stick { width: 100%; max-width: 36px; background: var(--accent); border-radius: 5px 5px 2px 2px; min-height: 2px; }
.trend .stick.neg { background: var(--bad); }
.trend .m { font-size: 0.7rem; color: var(--ink-dim); }
.heat td { text-align: center; font-variant-numeric: tabular-nums; font-size: 0.85rem; padding: 0.45rem 0.35rem; }
.heat td.cat { text-align: left; font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; font-size: 0.8rem; color: var(--ink-dim); margin-top: 0.6rem; }
.legend i { display: inline-block; width: 18px; height: 12px; border-radius: 3px; }

/* scanner */
.scanner { position: relative; background: #000; border-radius: 14px; overflow: hidden; aspect-ratio: 4 / 3; max-height: 55vh; width: 100%; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .reticle { position: absolute; left: 10%; right: 10%; top: 35%; bottom: 35%; border: 3px solid rgba(255, 255, 255, 0.85); border-radius: 12px; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.35); }
.scanner .scan-msg { position: absolute; left: 0; right: 0; bottom: 0.6rem; text-align: center; color: #fff; font-weight: 600; text-shadow: 0 1px 3px #000; }
.hidden { display: none !important; }
.preview { display: flex; gap: 0.9rem; align-items: flex-start; }
.preview img { width: 96px; height: 96px; object-fit: contain; background: #fff; border-radius: 10px; flex: none; }
.qty { display: flex; align-items: center; gap: 0.4rem; }
.qty input { text-align: center; max-width: 5rem; }
.qty .btn { min-width: 46px; padding: 0; font-size: 1.3rem; }
.recent-added { font-size: 0.9rem; }

/* charts (public/charts.js) */
.chart-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 1rem 1.1rem 0.9rem; box-shadow: var(--shadow); min-width: 0; }
.chart-card h3 { font-size: 1.02rem; margin: 0; }
.chart-card .sub { color: var(--ink-dim); font-size: 0.85rem; margin: 0.15rem 0 0.7rem; }
.chart { width: 100%; min-height: 40px; position: relative; }
.chart-svg { display: block; touch-action: pan-y; user-select: none; -webkit-user-select: none; overflow: visible; }
.chart-svg .grid { stroke: var(--grid); stroke-width: 1; }
.chart-svg .axis { stroke: var(--axis); stroke-width: 1; }
.chart-svg .hair { stroke: var(--ink-dim); stroke-width: 1; }
.chart-svg .tick { fill: var(--ink-dim); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-svg .row-label { fill: var(--ink); font-size: 13px; dominant-baseline: middle; }
.chart-svg .bar-value, .chart-svg .end-label { fill: var(--ink); font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; dominant-baseline: middle; }
.chart-svg .end-label { dominant-baseline: auto; }
.chart-svg .seg-label { font-size: 12px; font-weight: 700; }
.chart-svg .mark, .chart-svg g { transition: opacity 0.12s; }
.chart-svg .dim { opacity: 0.35; }
.chart-svg .hit { cursor: crosshair; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 0.3rem 1rem; margin-top: 0.6rem; font-size: 0.82rem; color: var(--ink-dim); }
.chart-legend span { display: inline-flex; align-items: center; gap: 0.4rem; }
.chart-legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.chart-legend i.line { height: 3px; border-radius: 2px; width: 16px; }
.chart-tip { position: absolute; z-index: 50; pointer-events: none; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; padding: 0.5rem 0.7rem; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); font-size: 0.85rem; min-width: 150px; max-width: 260px; }
.chart-tip .tip-title { font-weight: 700; margin-bottom: 0.25rem; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 0.45rem; line-height: 1.5; }
.chart-tip .tip-row i { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.chart-tip .tip-row strong { font-variant-numeric: tabular-nums; }
.chart-tip .tip-row span { color: var(--ink-dim); }
.chart-grid { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 1000px) { .chart-grid.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.heat td[data-tip] { cursor: pointer; }

/* pallet cards (phones) vs table (wide screens) */
.only-wide { display: none; }
@media (min-width: 860px) { .only-wide { display: block; } .only-narrow { display: none !important; } }
.pallet-card { align-items: stretch; }
.pc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem 1rem; margin-top: 0.7rem; }
.pc-grid span { display: block; font-size: 0.78rem; color: var(--ink-dim); font-weight: 600; }
.pc-grid strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.pc-grid small { color: var(--ink-dim); font-size: 0.8rem; }
.payback { margin-top: 0.75rem; display: grid; gap: 0.3rem; font-size: 0.85rem; color: var(--ink-dim); }
.payback-track { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.payback-fill { height: 100%; border-radius: 99px; background: var(--accent); opacity: 0.55; }
.payback-fill.done { opacity: 1; }
