/* Layout: single centered column, top bar, tables that collapse columns on narrow screens */
:root {
  --bg: #f4f4ef;
  --surface: #ffffff;
  --surface-2: #eceee4;
  --fg: #1e2419;
  --muted: #66705d;
  --line: #dcdfd2;
  --accent: #4f6b26;
  --accent-strong: #3d5419;
  --accent-fg: #ffffff;
  --accent-soft: #e7eed8;
  --warn: #8a5300;
  --warn-bg: #fbedd3;
  --bad: #a8231b;
  --bad-bg: #f9e1de;
  --info-bg: #e3ecf3;
  --info: #1f4a6b;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12150f;
    --surface: #1b1f17;
    --surface-2: #242a1e;
    --fg: #e7eadf;
    --muted: #a0a993;
    --line: #313828;
    --accent: #9cc25b;
    --accent-strong: #b3d47a;
    --accent-fg: #142007;
    --accent-soft: #28351a;
    --warn: #f0b85b;
    --warn-bg: #3a2c12;
    --bad: #f3968e;
    --bad-bg: #3d1c19;
    --info-bg: #182a38;
    --info: #9cc6e8;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 var(--font);
}
a { color: var(--accent); }
a:hover { color: var(--accent-strong); }
h1 { font-size: 1.6rem; line-height: 1.2; margin: 0 0 4px; text-wrap: balance; }
h2 { font-size: 1.05rem; margin: 0 0 10px; }
code, .mono { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.cn { color: var(--muted); }
.cn.big { font-size: 1.15rem; margin: 0; }
.lead { font-size: 1.1rem; margin: 0 0 16px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top, 0px);
  position: sticky; top: 0; z-index: 20;
}
.topbar-inner {
  max-width: 1100px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
}
.brand { font-weight: 700; color: var(--fg); text-decoration: none; letter-spacing: .01em; }
.topbar nav { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.topbar nav a, .linklike {
  color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 8px;
  font: inherit; font-size: .95rem; background: none; border: 0; cursor: pointer;
}
.topbar nav a:hover, .linklike:hover { color: var(--fg); background: var(--surface-2); }
.topbar nav a.on { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
form.inline { display: inline; margin: 0; }

/* ---------- Page ---------- */
.page {
  max-width: 1100px; margin: 0 auto;
  padding: 20px max(16px, env(safe-area-inset-right, 0px)) calc(48px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  display: grid; gap: 20px;
}
.page > *, section > * { min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px 20px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumbs { margin: 0 0 -12px; font-size: .9rem; color: var(--muted); }
.crumbs a { text-decoration: none; }
.meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 6px 0 0; }
section { display: grid; gap: 4px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: 600 .95rem var(--font); padding: 10px 16px; min-height: 44px;
  border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--surface); color: var(--fg); text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--surface-2); color: var(--fg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.danger:hover { background: var(--bad-bg); }
.btn.small { padding: 6px 10px; min-height: 36px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Flash messages ---------- */
.flash { padding: 12px 14px; border-radius: var(--radius); background: var(--accent-soft); color: var(--fg); }
.flash.bad { background: var(--bad-bg); color: var(--bad); }
.flash.info { background: var(--info-bg); color: var(--info); }
.flash a { color: inherit; font-weight: 600; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .8rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }

/* ---------- Search and chips ---------- */
.search { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.search input[type=search] { flex: 1 1 260px; min-width: 0; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
.chip {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--fg); text-decoration: none; font-size: .9rem; white-space: nowrap;
}
.chip:hover { color: var(--fg); background: var(--surface-2); }
.chip .cn { font-size: .85rem; }
.chip .n { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.chip.on .cn, .chip.on .n { color: var(--accent-fg); opacity: .85; }

/* ---------- Alerts ---------- */
.alerts { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.alert-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.alert-box h2 { display: flex; align-items: center; gap: 8px; font-size: .95rem; }
.alert-box .count { font-size: .8rem; background: var(--surface-2); color: var(--muted); padding: 1px 8px; border-radius: 999px; }
.alert-box ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.alert-box li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; }
.alert-box li a { font-weight: 600; text-decoration: none; }

/* ---------- Tables ---------- */
.table-wrap { position: relative; overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table.list { width: 100%; border-collapse: collapse; }
.list th {
  text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.list td { padding: 12px 14px; border-top: 1px solid var(--line); vertical-align: top; }
.list tbody tr:first-child td { border-top: 0; }
.list.compact td { padding: 8px 14px; }
.list .num { text-align: right; font-variant-numeric: tabular-nums; }
.list tr.dim td { opacity: .55; }
.rowlink { font-weight: 600; color: var(--fg); text-decoration: none; }
.rowlink:hover { color: var(--accent); text-decoration: underline; }
.qty { font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.qty.warn, .stats .warn { color: var(--warn); }
.qty.bad, .stats .bad { color: var(--bad); }
.delta { font-weight: 700; font-variant-numeric: tabular-nums; }
.delta.in { color: var(--accent); }
.delta.out { color: var(--bad); }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn + .btn, .row-actions .btn + form { margin-left: 6px; }

@media (max-width: 760px) { .hide-sm { display: none; } }
@media (max-width: 480px) { .hide-xs { display: none; } .row-actions { white-space: normal; } .row-actions .btn { margin: 2px 0; } }

/* ---------- Stats strip ---------- */
.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.stats > div { padding: 14px 16px; border-left: 1px solid var(--line); min-width: 0; }
.stats > div:first-child { border-left: 0; }
.stats .label { display: block; margin-bottom: 2px; }
.stats strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.stats strong.smaller { font-size: 1rem; font-weight: 600; }

/* ---------- Details ---------- */
.details { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0; margin: 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.details > div { padding: 12px 16px; border-bottom: 1px solid var(--line); min-width: 0; }
.details dt { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.details dd { margin: 2px 0 0; overflow-wrap: anywhere; }

.empty { background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--muted); }
.empty p { margin: 4px 0; }
.danger-zone { border-top: 1px solid var(--line); padding-top: 16px; }
.danger-zone form { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* ---------- Forms ---------- */
.form { display: grid; gap: 16px; max-width: 760px; }
.form.narrow { max-width: 320px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px 20px; }
.field { display: grid; gap: 6px; align-content: start; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.label, legend.label { font-size: .85rem; font-weight: 600; letter-spacing: .01em; }
.optional { font-weight: 400; color: var(--muted); }
.help { margin: 0; font-size: .82rem; color: var(--muted); }
.error { margin: 0; font-size: .88rem; color: var(--bad); font-weight: 600; }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=password], select, textarea {
  width: 100%; min-width: 0; font: inherit; font-size: 16px; padding: 10px 12px; min-height: 44px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--fg);
}
textarea { resize: vertical; }
[aria-invalid=true] { border-color: var(--bad); }
.static { padding: 10px 0; font-weight: 600; }
.prefix { display: flex; align-items: stretch; }
.prefix span { display: grid; place-items: center; padding: 0 10px; border: 1px solid var(--line); border-right: 0;
  border-radius: 8px 0 0 8px; background: var(--surface-2); color: var(--muted); font-size: .9rem; }
.prefix input { border-radius: 0 8px 8px 0; }
.with-button { display: flex; gap: 8px; }
.with-button input { flex: 1; }
.check { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; cursor: pointer; }
.check.small { font-weight: 400; font-size: .9rem; color: var(--muted); }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.segmented { border: 0; padding: 0; margin: 0; display: flex; gap: 0; flex-wrap: wrap; }
.segmented legend { margin-bottom: 6px; padding: 0; }
.segmented label {
  flex: 1 1 140px; display: flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer;
  padding: 10px 14px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; min-height: 44px;
}
.segmented label:first-of-type { border-radius: 8px 0 0 8px; }
.segmented label:last-of-type { border-radius: 0 8px 8px 0; border-left: 0; }
.segmented label:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); }
.segmented input { accent-color: var(--accent); }

/* ---------- Login ---------- */
.login {
  width: 100%; max-width: 340px; margin: 6vh auto 0; display: grid; gap: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px;
}
.login h1 { font-size: 1.4rem; }
.eyebrow { margin: 0; font-size: .78rem; text-transform: uppercase; letter-spacing: .1em; color: var(--accent); font-weight: 700; }
input.pin-input { text-align: center; font-size: 2rem; letter-spacing: .6em; padding-left: .6em; font-family: var(--mono); min-height: 60px; }
input.pin-input.small { font-size: 1.4rem; min-height: 50px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.keypad button {
  font: 600 1.5rem var(--font); min-height: 64px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--fg); touch-action: manipulation;
}
.keypad button:active { background: var(--accent-soft); }
.keypad button.soft { font-size: .95rem; color: var(--muted); background: transparent; }
.keypad button:disabled { opacity: .4; }

/* ---------- Scanner pop-up ---------- */
.scan-modal {
  position: fixed; inset: 0; z-index: 100; background: rgba(10, 12, 8, .6);
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}
.scan-modal[hidden] { display: none; }
.scan-sheet { width: 100%; max-width: 520px; background: var(--surface); border-radius: 16px; padding: 16px; display: grid; gap: 12px; max-height: 100%; overflow-y: auto; }
.scan-head { display: flex; justify-content: space-between; align-items: center; }
.scan-head h2 { margin: 0; }
.scan-reader { background: #000; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 3; max-width: 100%; }
.scan-reader video { width: 100% !important; height: 100% !important; object-fit: cover; }
.scan-status { margin: 0; min-height: 1.5em; color: var(--muted); font-size: .92rem; }
.scan-status.error { color: var(--bad); }
.scan-actions { display: flex; gap: 8px; }
.scan-actions .btn { flex: 1; }
.scan-type { display: grid; gap: 6px; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
