:root {
  --bg: #0b0d12;
  --panel: #141821;
  --panel2: #1b2030;
  --line: #262c3d;
  --text: #e8ecf4;
  --muted: #8d97ad;
  --accent: #7cc4ff;
  --ink: #06121f;
  --err: #ff8b8b;
  --ok: #8ee0b0;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7fb; --panel: #fff; --panel2: #f0f3fa; --line: #e1e5ef;
    --text: #141821; --muted: #5d6679; --accent: #0a66c2; --ink: #fff;
    --err: #b42318; --ok: #0e7a45;
    color-scheme: light;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
/* 16px inputs keep iOS Safari from zooming on focus. */
button, input, select, textarea { font: inherit; font-size: 16px; color: inherit; }
a { color: var(--accent); }
h1 { font-size: 1.45rem; margin: 0; line-height: 1.2; overflow-wrap: anywhere; }
h2 { font-size: 1rem; margin: 0 0 8px; }
.muted, .hint { color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel2); color: var(--text);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--ink); font-weight: 600; }
.btn.danger { color: var(--err); }
.btn.slim { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn:disabled { opacity: .5; }
.icon-btn {
  display: inline-grid; place-items: center; min-width: 44px; min-height: 44px; padding: 0 8px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel2); color: var(--text);
  text-decoration: none; cursor: pointer; font-size: 1.1rem;
}
.icon-btn.on { color: #f5c542; }
.icon-btn:disabled { opacity: .35; }

/* ---------- gate ---------- */
.gate { min-height: 100%; display: grid; place-items: center; padding: calc(24px + var(--sat)) 20px calc(24px + var(--sab)); }
.gate-card { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 24px; }
.gate-card h1 { font-size: 2rem; margin: 8px 0; }
.gate-card .back { margin: 0; }
.gate-card .back a { text-decoration: none; color: var(--muted); }
#gate-msg { color: var(--muted); min-height: 1.4em; }
.gate-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.gate-actions .btn { flex: 1; }

/* ---------- app shell ---------- */
.app {
  height: 100vh; height: 100dvh;
  display: grid; grid-template-rows: auto 1fr auto;
}
.bar {
  display: flex; gap: 8px; align-items: center;
  padding: calc(8px + var(--sat)) calc(10px + var(--sar)) 8px calc(10px + var(--sal));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.bar .home { flex: none; }
.search { flex: 1; min-width: 0; margin: 0; }
.search input {
  width: 100%; min-height: 46px; padding: 10px 14px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--bg); outline: none;
  -webkit-appearance: none; appearance: none;
}
.search input:focus { border-color: var(--accent); }
.main { min-height: 0; display: grid; grid-template-columns: 1fr; }
.pane { min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.list-pane { padding: 0 var(--sar) 16px var(--sal); }
.detail-pane { padding: 0 calc(14px + var(--sar)) 24px calc(14px + var(--sal)); }
body[data-detail="0"] .detail-pane { display: none; }
body[data-detail="1"] .list-pane { display: none; }

.tabs {
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 0 var(--sar) var(--sab) var(--sal);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 54px; color: var(--muted); text-decoration: none; font-size: .75rem;
}
.tabs a span { font-size: 1.2rem; line-height: 1; }
.tabs a[aria-current="page"] { color: var(--accent); font-weight: 600; }
body.typing .tabs { display: none; }

/* Unfolded / tablet / desktop: list and details side by side. */
@media (min-width: 760px) {
  .main { grid-template-columns: minmax(300px, 380px) 1fr; }
  body[data-detail] .list-pane, body[data-detail] .detail-pane { display: block; }
  .list-pane { border-right: 1px solid var(--line); }
  .detail-pane { padding-left: 24px; padding-right: calc(24px + var(--sar)); }
  .detail .back-link { display: none; }
  body.typing .tabs { display: grid; }
}
@media (min-width: 1100px) {
  .detail { max-width: 760px; }
}

/* ---------- lists ---------- */
.list-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 14px 6px; color: var(--muted); font-size: .85rem; min-height: 44px;
}
.rows { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  min-height: 60px; padding: 10px 14px; padding-left: calc(14px + var(--depth, 0) * 18px);
  border: 0; border-bottom: 1px solid var(--line); background: none;
  color: var(--text); text-decoration: none; text-align: left; cursor: pointer;
}
.row:active, .row.on { background: var(--panel2); }
.row.on { box-shadow: inset 3px 0 0 var(--accent); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub, .row-loc { font-size: .85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-qty { flex: none; font-variant-numeric: tabular-nums; font-weight: 600; min-width: 2.5em; text-align: right; }
.tree .row { min-height: 52px; }
.empty { color: var(--muted); text-align: center; padding: 32px 18px; }
.placeholder { color: var(--muted); display: grid; place-items: center; height: 100%; min-height: 200px; }
.badge {
  display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 99px; font-size: .72rem; font-weight: 600;
  background: var(--panel2); border: 1px solid var(--line); color: var(--muted); vertical-align: middle;
}

/* ---------- detail ---------- */
.detail { padding-top: 10px; }
.back-link { display: inline-flex; align-items: center; min-height: 44px; text-decoration: none; }
.detail-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 6px 0 12px; }
.code { margin: 4px 0 0; color: var(--muted); font: .85rem ui-monospace, SFMono-Regular, Menlo, monospace; }
.crumbs { font-size: .85rem; color: var(--muted); margin-top: 4px; }
.crumbs a { text-decoration: none; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 14px; margin: 0 0 12px; min-width: 0;
}
.card .rows { margin: 0 -14px; }
.card .rows li:last-child .row { border-bottom: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.total { color: var(--muted); font-size: .9rem; }
.kvs { margin: 0; display: grid; gap: 6px; }
.kv { display: grid; grid-template-columns: minmax(90px, 35%) 1fr; gap: 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.desc, .notes { white-space: pre-wrap; overflow-wrap: anywhere; margin: 10px 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  min-height: 34px; padding: 4px 12px; border-radius: 99px; border: 1px solid var(--line);
  background: var(--panel2); color: var(--text); cursor: pointer;
}
.links { margin: 0; padding-left: 18px; }
.links li { margin: 4px 0; overflow-wrap: anywhere; }

.stock { list-style: none; margin: 0 0 8px; padding: 0; }
.stock-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.stock-row:last-child { border-bottom: 0; }
.stock-loc { flex: 1 1 180px; min-width: 0; text-decoration: none; color: var(--text); overflow-wrap: anywhere; }
.stock-ctl { display: flex; align-items: center; gap: 6px; }
.step {
  min-width: 44px; min-height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel2); font-size: 1.3rem; cursor: pointer;
}
input.qty { width: 5.5em; min-height: 44px; text-align: center; }

input, select, textarea {
  min-height: 46px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); width: 100%; min-width: 0;
}
textarea { resize: vertical; line-height: 1.35; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
input[type="checkbox"] { width: 24px; height: 24px; min-height: 0; padding: 0; accent-color: var(--accent); flex: none; }

.add-stock { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; margin-top: 8px; }
.add-stock label { display: grid; gap: 4px; }
.add-stock label.check { display: inline-flex; }
.add-stock .grow { flex: 1 1 220px; }
.add-stock .qty { width: 5.5em; }
.lbl { font-size: .85rem; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.put-here { margin-top: 10px; }
.picks .row { min-height: 52px; }

.form .f { display: grid; gap: 4px; margin: 0 0 12px; }
.form .hint { font-size: .8rem; }
.f2 { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
@media (min-width: 520px) { .f2 { grid-template-columns: 1fr 1fr; } }
.form h1 { margin: 6px 0 14px; }
fieldset.card { margin: 6px 0 14px; }
fieldset legend { padding: 0 6px; color: var(--muted); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 6px 0 24px; }
.actions .btn.primary { flex: 1 1 200px; }

.types { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 6px; }
.type-row { display: flex; align-items: center; gap: 8px; }
.type-row .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.inline { display: flex; gap: 8px; }
.inline input { flex: 1; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(70px + var(--sab)); z-index: 10;
  max-width: min(92vw, 520px); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 24px rgba(0,0,0,.3);
}
.toast.bad { background: var(--err); color: #fff; }

/* ---------- import ---------- */
.thumb { width: 72px; height: 72px; object-fit: contain; border-radius: 12px; background: #fff; border: 1px solid var(--line); flex: none; }
.banner { padding: 10px 14px; border-radius: 12px; background: var(--panel2); border: 1px solid var(--line); }
.banner.ok { border-color: var(--ok); }
.src { margin: 0 0 8px; font-size: .9rem; overflow-wrap: anywhere; }
.warnings ul { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 6px; }
.warnings li { padding-left: 26px; position: relative; }
.warnings li::before { position: absolute; left: 0; content: "⚠️"; }
.warnings li.info { color: var(--muted); }
.warnings li.info::before { content: "ℹ️"; }
.dupe-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); padding-right: 14px; }
.dupe-row .row { flex: 1 1 200px; border-bottom: 0; }
.file-list { margin: -6px 0 12px; padding-left: 18px; color: var(--muted); font-size: .9rem; }
.import-status { min-height: 1.4em; color: var(--muted); }
.import-status.busy::before { content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 8px; border: 2px solid var(--accent); border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -1px; }
.import-status.bad { color: var(--err); }
@keyframes spin { to { transform: rotate(360deg); } }
.badge.st-review { color: var(--accent); border-color: var(--accent); }
.badge.st-accepted { color: var(--ok); border-color: var(--ok); }
.badge.st-rejected { text-decoration: line-through; }
input[type="file"] { padding: 10px; }

/* ---------- labels ---------- */
.head-btns { display: flex; gap: 8px; flex: none; }
.label-preview { background: #fff; border-radius: 10px; padding: 10px; display: flex; justify-content: center; overflow-x: auto; }
canvas.ink { image-rendering: pixelated; image-rendering: crisp-edges; max-width: 100%; height: auto; background: #fff; box-shadow: 0 0 0 1px #d7dbe3; }
.small { font-size: .85rem; }
code { font-size: .85em; overflow-wrap: anywhere; }
.art-pair { display: flex; gap: 12px; flex-wrap: wrap; margin: 4px 0 10px; }
.art-pair figure { margin: 0; background: #fff; border-radius: 10px; padding: 8px; }
.art-pair figcaption { color: #5d6679; font-size: .8rem; text-align: center; margin-top: 4px; }
.art-ctl { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.print-row { display: flex; align-items: flex-end; gap: 10px; }
.print-row .f { flex: 1; margin: 0; }
.warn-line { color: var(--err); margin: 8px 0 0; }
.status { margin: 8px 0 0; }
.status.bad { color: var(--err); }
.status.good { color: var(--ok); }
.status.warn { color: var(--accent); }
.linkish { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; font: inherit; cursor: pointer; }
.spin { display: inline-block; width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -1px; }
