:root {
  --bg: #EEEBE5;
  --card: #FFFFFF;
  --ink: #1D1E1B;
  --muted: #676A63;
  --line: #DEDAD1;
  --field: #F4F2EE;
  --accent: #C4501C;
  --accent-ink: #FFFFFF;
  --accent-soft: #F6E3D8;
  --tape: #F2E3A9;
  --tape-ink: #3A3214;
  --danger: #B3372B;
  --ok: #2F6B4F;
  --shadow: 0 1px 2px rgba(29, 30, 27, .06), 0 4px 14px rgba(29, 30, 27, .05);
  --radius: 14px;
  --tab-h: 60px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615; --card: #20201E; --ink: #EDEBE6; --muted: #A09D95; --line: #32312D; --field: #2A2926;
    --accent: #F07A3F; --accent-ink: #1A0D05; --accent-soft: #3A2418; --tape: #4A4224; --tape-ink: #F3E6B5;
    --danger: #EF7B6B; --ok: #6DBE94; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161615; --card: #20201E; --ink: #EDEBE6; --muted: #A09D95; --line: #32312D; --field: #2A2926;
  --accent: #F07A3F; --accent-ink: #1A0D05; --accent-soft: #3A2418; --tape: #4A4224; --tape-ink: #F3E6B5;
  --danger: #EF7B6B; --ok: #6DBE94; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
/* -apple-system-body follows the iPhone's text-size setting (Dynamic Type). */
html { font: -apple-system-body; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  font-size: max(1rem, 16px); line-height: 1.4;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
ul { margin: 0; padding: 0; list-style: none; }
.icon { flex: none; display: block; }
a { color: inherit; }

.main { padding: calc(env(safe-area-inset-top) + 10px) 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 28px); max-width: 720px; margin: 0 auto; }
.view { display: flex; flex-direction: column; gap: 14px; }

/* ---------- Type ---------- */
.title { font-size: 1.75em; font-weight: 750; letter-spacing: -.02em; line-height: 1.15; }
.h2 { font-size: 1.15em; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: .875em; }
.tiny { font-size: .78em; }
.strong { font-weight: 650; }
.center { text-align: center; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.section-label { font-size: .85em; font-weight: 650; color: var(--muted); margin: 6px 2px -4px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* ---------- Layout helpers ---------- */
.row { display: flex; align-items: center; min-width: 0; }
.row.gap { gap: 10px; }
.row.gap-s { gap: 6px; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.tight { gap: 6px; }
.grow { flex: 1; min-width: 0; }
.hidden { display: none !important; }

/* ---------- Cards and lists ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list > * + * { border-top: 1px solid var(--line); }
.li {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; min-height: 60px;
  text-decoration: none; color: inherit; background: none; border: 0; width: 100%; text-align: left;
}
.li:active { background: var(--field); }
.li .li-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.li .li-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li .li-sub { font-size: .85em; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li .chev { color: var(--muted); }
.li.selected { background: var(--accent-soft); }
.li .check {
  width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); flex: none;
  display: grid; place-items: center; color: var(--accent-ink);
}
.li.selected .check { background: var(--accent); border-color: var(--accent); }
.li.dragging { position: relative; z-index: 5; background: var(--card); box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.drag-handle { touch-action: none; color: var(--muted); padding: 6px; margin: -6px; cursor: grab; }

.thumb {
  width: 46px; height: 46px; border-radius: 10px; flex: none; object-fit: cover;
  background: var(--field); display: grid; place-items: center; color: var(--muted); overflow: hidden;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Grid view */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
  text-decoration: none; color: inherit; display: flex; flex-direction: column; border: 0; padding: 0; text-align: left; position: relative;
}
.tile .tile-img { aspect-ratio: 1 / 1; background: var(--field); display: grid; place-items: center; color: var(--muted); }
.tile .tile-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .tile-body { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tile .tile-title { font-weight: 600; font-size: .95em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .tile-sub { font-size: .8em; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile.selected { outline: 3px solid var(--accent); }
.tile .tile-check { position: absolute; top: 8px; right: 8px; }

/* Label-tape chip for box numbers */
.code {
  font-family: var(--mono); font-weight: 700; font-size: .8em; letter-spacing: .04em;
  background: var(--tape); color: var(--tape-ink); padding: 2px 7px; border-radius: 4px;
  white-space: nowrap; flex: none; display: inline-block; line-height: 1.35;
}
.code.big { font-size: 1em; padding: 4px 10px; }
.qty { font-variant-numeric: tabular-nums; font-weight: 650; color: var(--muted); flex: none; }
.pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px;
  background: var(--field); color: var(--ink); font-size: .82em; font-weight: 550; white-space: nowrap;
}

/* Breadcrumb path */
.path { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; font-size: .88em; color: var(--muted); }
.path a { text-decoration: none; color: var(--muted); padding: 2px 0; }
.path a:active { color: var(--ink); }
.path .sep { opacity: .5; }

/* ---------- Header bits ---------- */
.topbar { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.topbar .grow { min-width: 0; }
.hero { display: flex; gap: 14px; align-items: flex-start; }
.hero-photo { width: 84px; height: 84px; border-radius: 14px; overflow: hidden; background: var(--field); flex: none; display: grid; place-items: center; color: var(--muted); border: 0; padding: 0; }
.hero-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.stats { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 0;
  background: var(--field); color: var(--ink); font-weight: 650; text-decoration: none; white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9em; border-radius: 10px; }
.btn:disabled { opacity: .45; }
.btn:active { filter: brightness(.94); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent; color: var(--ink);
  display: grid; place-items: center; flex: none; text-decoration: none;
}
.icon-btn:active { background: var(--field); }
.icon-btn.filled { background: var(--card); box-shadow: var(--shadow); }
.icon-btn.light { color: #fff; background: rgba(0,0,0,.35); }
.icon-btn.on { color: var(--accent); }

.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 8px; }
.action {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 12px 6px; border-radius: 12px;
  background: var(--card); box-shadow: var(--shadow); border: 0; font-size: .82em; font-weight: 600; text-decoration: none; color: inherit;
}
.action .icon { color: var(--accent); }

.seg { display: inline-flex; background: var(--field); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-weight: 600; font-size: .88em; color: var(--muted); min-height: 34px; display: inline-flex; align-items: center; gap: 4px; }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 2px 0; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 0; background: var(--card); box-shadow: inset 0 0 0 1.5px var(--line); padding: 7px 13px; border-radius: 999px; font-weight: 600; font-size: .88em; white-space: nowrap; color: var(--ink); }
.chip.on { background: var(--ink); color: var(--bg); box-shadow: none; }

/* ---------- Fields ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: .85em; font-weight: 650; color: var(--muted); }
.field-hint { font-size: .8em; color: var(--muted); }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 11px 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--field); font-size: max(1rem, 16px); outline: none; -webkit-appearance: none; appearance: none;
}
textarea.input { min-height: 90px; resize: vertical; }
.input:focus { border-color: var(--accent); background: var(--card); }
select.input { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 20px) 50%, calc(100% - 14px) 50%; background-size: 6px 6px; background-repeat: no-repeat; padding-right: 36px; }

.searchbox { position: relative; }
.searchbox .icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.searchbox .input { padding-left: 44px; min-height: 52px; font-size: max(1.05rem, 17px); background: var(--card); box-shadow: var(--shadow); border-color: transparent; }
.searchbox .input:focus { border-color: var(--accent); }
.searchbox .clear { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }

.stepper { display: inline-flex; align-items: center; background: var(--field); border-radius: 12px; }
.stepper button { width: 44px; height: 44px; border: 0; background: transparent; display: grid; place-items: center; }
.stepper input { width: 64px; text-align: center; border: 0; background: transparent; font-weight: 700; font-size: 1.1em; min-height: 44px; outline: none; }

/* Rapid add bar */
.addbar {
  position: sticky; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 10px); z-index: 4;
  display: flex; gap: 8px; align-items: center; background: var(--card); padding: 8px; border-radius: 16px;
  box-shadow: 0 6px 24px rgba(0,0,0,.12); border: 1px solid var(--line);
}
.addbar .input { min-height: 44px; background: var(--field); }

/* Selection bar */
.selbar {
  position: fixed; left: 50%; transform: translateX(-50%); width: min(688px, calc(100% - 24px));
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 10px); z-index: 30;
  background: var(--ink); color: var(--bg); border-radius: 16px; padding: 8px 8px 8px 16px;
  display: flex; align-items: center; gap: 6px; box-shadow: 0 8px 28px rgba(0,0,0,.25);
}
.selbar .btn { background: rgba(255,255,255,.12); color: inherit; min-height: 40px; padding: 0 12px; }
:root[data-theme="dark"] .selbar .btn { background: rgba(0,0,0,.12); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .selbar .btn { background: rgba(0,0,0,.12); } }

/* ---------- Banners and empty states ---------- */
.banner { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-radius: var(--radius); background: var(--accent-soft); }
.banner .icon { color: var(--accent); }
.empty { text-align: center; padding: 28px 16px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty .icon { color: var(--line); }
.tip { display: flex; gap: 12px; align-items: flex-start; }
.tip .num { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; flex: none; font-size: .9em; }

/* ---------- Photos ---------- */
.photos { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.photos::-webkit-scrollbar { display: none; }
.photo { position: relative; flex: none; width: 140px; height: 140px; border-radius: 12px; overflow: hidden; background: var(--field); border: 0; padding: 0; }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo.add { display: grid; place-items: center; color: var(--muted); box-shadow: inset 0 0 0 2px var(--line); background: transparent; }
.viewer { position: fixed; inset: 0; background: #000; z-index: 90; display: flex; flex-direction: column; }
.viewer img { flex: 1; min-height: 0; object-fit: contain; width: 100%; }
.viewer .viewer-bar { display: flex; justify-content: space-between; padding: calc(env(safe-area-inset-top) + 8px) 12px 8px; }
.viewer .viewer-foot { display: flex; justify-content: center; gap: 12px; padding: 12px 12px calc(env(safe-area-inset-bottom) + 16px); }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: saturate(1.4) blur(16px); -webkit-backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--line); display: flex; justify-content: center;
}
.tabbar .tabs { display: flex; width: 100%; max-width: 720px; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: .7em; font-weight: 600; border: 0; background: none; }
.tab.on { color: var(--accent); }
.tab.scan .scan-dot { width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; margin-top: -6px; box-shadow: 0 4px 14px rgba(196, 80, 28, .35); }

/* ---------- Sheets ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; transition: opacity .25s; z-index: 50; }
.sheet-backdrop.open { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; margin: 0 auto; max-width: 640px;
  background: var(--bg); border-radius: 20px 20px 0 0; max-height: 88vh; max-height: 88dvh; display: flex; flex-direction: column;
  transform: translateY(105%); transition: transform .26s cubic-bezier(.2,.8,.2,1);
}
.sheet.open { transform: none; }
.sheet.tall { height: 88vh; height: 88dvh; }
.sheet-head { padding: 6px 16px 4px; flex: none; }
.sheet-grab { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 6px; }
.sheet-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-title { font-size: 1.15em; font-weight: 700; }
.sheet-body { padding: 8px 16px 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; min-height: 0; }
.sheet-foot { padding: 10px 16px calc(env(safe-area-inset-bottom) + 12px); border-top: 1px solid var(--line); flex: none; }
@media (min-width: 700px) {
  .sheet { bottom: auto; top: 50%; border-radius: 20px; transform: translateY(-40%) scale(.98); opacity: 0; transition: transform .2s, opacity .2s; max-height: 82vh; }
  .sheet.open { transform: translateY(-50%); opacity: 1; }
  .sheet.tall { height: 82vh; }
  .sheet-grab { display: none; }
}

/* ---------- Toast ---------- */
.toast {
  /* At the top: the keyboard and the add bar own the bottom of the screen. */
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 10px); transform: translate(-50%, -20px);
  background: var(--ink); color: var(--bg); padding: 6px 6px 6px 16px; border-radius: 12px; font-weight: 600; font-size: .92em;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 80; display: flex; align-items: center; gap: 10px;
  max-width: calc(100% - 32px); min-height: 44px;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast .toast-msg { padding: 6px 10px 6px 0; }
.toast button { border: 0; background: rgba(255,255,255,.14); color: inherit; font-weight: 700; padding: 8px 14px; border-radius: 9px; }

/* ---------- Scanner ---------- */
.scanner { position: fixed; inset: 0; z-index: 70; background: #000; }
.scan-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-overlay { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between; color: #fff; }
.scan-top { display: flex; justify-content: space-between; padding: calc(env(safe-area-inset-top) + 10px) 12px 0; }
.scan-frame { align-self: center; width: min(72vw, 300px); aspect-ratio: 1; border-radius: 22px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.45); border: 3px solid rgba(255,255,255,.9); }
.scan-bottom { padding: 16px 16px calc(env(safe-area-inset-bottom) + 18px); display: flex; flex-direction: column; gap: 10px; background: linear-gradient(transparent, rgba(0,0,0,.6) 30%); }
.scan-status { text-align: center; font-weight: 650; }
.scan-bottom .input { background: rgba(255,255,255,.95); color: #111; border-color: transparent; }

/* ---------- Move picker ---------- */
.picker-indent { display: inline-block; flex: none; }

/* ---------- Labels (screen preview) ---------- */
.label-preview { display: flex; justify-content: center; padding: 8px 0; }
.label-preview .lbl { box-shadow: var(--shadow); background: #fff; }
.print-root { display: none; }

/* A printed label. Sizes come from inline styles per layout. */
.lbl { color: #000; background: #fff; display: flex; align-items: center; gap: 3mm; padding: 2.5mm 3mm; overflow: hidden; break-inside: avoid; font-family: -apple-system, "Segoe UI", system-ui, sans-serif; }
.lbl .lbl-qr { flex: none; display: block; }
.lbl .lbl-qr svg { display: block; width: 100%; height: 100%; }
.lbl .lbl-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1mm; }
.lbl .lbl-code { font-family: var(--mono); font-weight: 800; letter-spacing: .03em; line-height: 1; }
.lbl .lbl-name { font-weight: 700; line-height: 1.1; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.lbl .lbl-where { color: #444; line-height: 1.1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.lbl .lbl-line { border-bottom: .3mm solid #888; height: 5mm; }
.lbl .lbl-list { color: #333; line-height: 1.15; overflow: hidden; }

/* ---------- Inventory table (print + screen) ---------- */
.inv-table { width: 100%; border-collapse: collapse; font-size: .9em; }
.inv-table th, .inv-table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.inv-table th { font-size: .8em; color: var(--muted); font-weight: 650; }
.inv-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

@media (min-width: 900px) {
  .main { max-width: 900px; padding-left: 24px; padding-right: 24px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
}

@media print {
  @page { margin: 0; }
  body { background: #fff !important; }
  .main, .tabbar, .toast, .sheet, .sheet-backdrop, .addbar, .selbar { display: none !important; }
  .print-root { display: block !important; }
  .print-page { break-after: page; }
  .print-page:last-child { break-after: auto; }
  .print-doc { color: #000; font-family: -apple-system, "Segoe UI", system-ui, sans-serif; }
  .print-doc .inv-table th, .print-doc .inv-table td { border-color: #ccc; color: #000; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-backdrop, .toast { transition: none; }
}
