:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --text: #1d2321;
  --muted: #6b7470;
  --line: #e3e1da;
  --brand: #1f7a5c;
  --brand-ink: #ffffff;
  --brand-soft: #e3f1eb;
  --red: #c2352b;     --red-soft: #fbe7e5;
  --amber: #a86200;   --amber-soft: #fdf0d9;
  --blue: #2457a6;    --blue-soft: #e5edf9;
  --gray-soft: #eeede8;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514;
    --card: #1c201e;
    --text: #e9ecea;
    --muted: #9aa39f;
    --line: #2c322f;
    --brand: #3fae86;
    --brand-ink: #0b1511;
    --brand-soft: #1b3229;
    --red: #ff7a6e;   --red-soft: #3a1f1c;
    --amber: #f0b453; --amber-soft: #3a2d15;
    --blue: #7fb0ff;  --blue-soft: #1a2638;
    --gray-soft: #262b29;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
a { color: var(--brand); }
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); font-weight: 400; }
.pad { padding: 8px 16px; }
.pad-x { padding: 0 16px; margin: 4px 0 8px; }
.hint { color: var(--muted); font-size: .85rem; margin: 6px 0 0; }
code { font-size: .85em; word-break: break-all; }

.app {
  max-width: 600px;
  margin: 0 auto;
  padding: 0 0 calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.topbar h1 { flex: 1; font-size: 1.25rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mark { font-size: 1.5rem; width: 40px; text-align: center; }
.icon-btn, .icon-btn-spacer {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center;
  border-radius: 50%; border: 0; background: transparent;
  font-size: 1.25rem; color: var(--text); text-decoration: none; cursor: pointer;
}
a.icon-btn[data-back] { font-size: 2rem; line-height: 1; color: var(--brand); }
.icon-btn:active { background: var(--gray-soft); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, transparent); background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: translateY(1px); }
button.link { border: 0; background: none; color: var(--brand); font: inherit; padding: 0; cursor: pointer; text-decoration: underline; }

/* ---------- search ---------- */
.search { padding: 4px 16px 8px; }
.search input {
  width: 100%; height: 48px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 24px;
  background: var(--card); color: var(--text); font: inherit;
  box-shadow: var(--shadow);
}
.search input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

/* ---------- alerts ---------- */
.alerts { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 16px 4px; }
.alert {
  flex: 1 1 140px; padding: 12px 14px; border-radius: var(--radius);
  text-decoration: none; font-size: .95rem;
}
.alert b { font-size: 1.3rem; display: block; }
.alert-red { background: var(--red-soft); color: var(--red); }
.alert-amber { background: var(--amber-soft); color: var(--amber); }
.alert-blue { background: var(--blue-soft); color: var(--blue); }

.section-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 16px 8px; }

/* ---------- box grid ---------- */
.box-grid {
  list-style: none; margin: 0; padding: 0 16px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px;
}
.box-card {
  display: flex; flex-direction: column; gap: 2px; height: 100%;
  padding: 14px; border-radius: var(--radius);
  background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow);
  color: var(--text); text-decoration: none;
}
.box-card:active { transform: scale(.98); }
.box-emoji { font-size: 1.6rem; line-height: 1; }
.box-emoji.big { font-size: 2.6rem; }
.box-code { font-weight: 800; letter-spacing: .02em; margin-top: 6px; color: var(--brand); display: flex; align-items: center; gap: 6px; }
.box-name { font-weight: 600; }
.box-loc { color: var(--muted); font-size: .82rem; flex: 1; }
.box-count { color: var(--muted); font-size: .8rem; margin-top: 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--red); display: inline-block; }

.box-hero { display: flex; gap: 14px; align-items: center; padding: 4px 16px 0; }
.box-hero h2 { font-size: 1.4rem; }
.box-hero p { margin: 4px 0 0; }
.box-actions { padding: 20px 16px; display: grid; gap: 8px; }

/* ---------- lists ---------- */
.list-title { padding: 18px 16px 6px; }
.exp-title-expired { color: var(--red); }
.exp-title-soon { color: var(--amber); }
.list {
  list-style: none; margin: 0 16px; padding: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow);
}
.list > li + li { border-top: 1px solid var(--line); }
.row-link { display: block; padding: 14px 16px; color: var(--text); text-decoration: none; }

.item-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; padding: 12px 12px 12px 16px; }
.item-main { flex: 1 1 180px; min-width: 0; color: var(--text); text-decoration: none; display: flex; flex-direction: column; gap: 3px; }
.item-name { font-weight: 600; }
.item-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: .82rem; color: var(--muted); }
.where { color: var(--muted); }

.exp { font-weight: 600; }
.exp-expired { color: var(--red); }
.exp-soon { color: var(--amber); }
.exp-month { color: var(--text); }
.exp-ok { color: var(--muted); font-weight: 400; }

/* ---------- level picker ---------- */
.levels { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--bg); }
.level {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: .85rem; font-weight: 600;
  min-height: 38px; padding: 0 12px; cursor: pointer;
}
.levels.compact .level { min-height: 34px; padding: 0 9px; font-size: .78rem; }
.level + .level { border-left: 1px solid var(--line); }
.level.on.lv-full  { background: var(--brand); color: var(--brand-ink); }
.level.on.lv-half  { background: var(--brand-soft); color: var(--brand); }
.level.on.lv-low   { background: var(--amber-soft); color: var(--amber); }
.level.on.lv-empty { background: var(--red-soft); color: var(--red); }

/* ---------- buy list ---------- */
.buy-row .bought-form { flex-basis: 100%; display: grid; gap: 8px; padding-top: 4px; }
.buy-row .bought-form[hidden] { display: none; }

/* ---------- forms ---------- */
.form { display: grid; gap: 16px; padding: 8px 16px; }
.form label, .form .field, .form fieldset { display: grid; gap: 6px; font-weight: 600; font-size: .92rem; }
.form fieldset { border: 0; padding: 0; margin: 0; }
.form legend { padding: 0; margin-bottom: 6px; }
.form input:not([type=radio]):not([type=checkbox]), .form select, .form textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text);
  font: inherit; font-weight: 400;
}
.form input.narrow { width: 90px; display: inline-block; }
.form input:focus, .form select:focus, .form textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.form-actions .btn { flex: 1 1 140px; }
.error { color: var(--red); background: var(--red-soft); padding: 10px 12px; border-radius: 10px; margin: 0; }
.date-row { display: flex; gap: 8px; }
.date-row input { flex: 1; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 20px; padding: 6px 12px; font: inherit; font-size: .85rem; cursor: pointer;
}
.emoji-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.emoji-pick label { display: block; }
.emoji-pick input { position: absolute; opacity: 0; pointer-events: none; }
.emoji-pick span {
  display: grid; place-items: center; height: 44px; font-size: 1.4rem;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); cursor: pointer;
}
.emoji-pick input:checked + span { border: 2px solid var(--brand); background: var(--brand-soft); }
.emoji-pick input:focus-visible + span { outline: 2px solid var(--brand); }
.checks { display: grid; gap: 4px; max-height: 260px; overflow: auto; padding: 4px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); }
.check { display: flex !important; align-items: center; gap: 8px; font-weight: 400 !important; padding: 6px 4px; }
.check input { width: 20px; height: 20px; accent-color: var(--brand); }
.stack { display: grid; gap: 8px; }

.notice { margin: 4px 16px 8px; padding: 12px 14px; border-radius: 12px; background: var(--amber-soft); color: var(--text); font-size: .9rem; }

.empty-state { text-align: center; padding: 48px 24px; display: grid; gap: 12px; justify-items: center; }
.empty-state p { color: var(--muted); max-width: 34ch; margin: 0; }
.big-emoji { font-size: 3.5rem; }

/* ---------- floating add + tab bar ---------- */
.fab {
  position: fixed; right: max(16px, calc(50vw - 300px + 16px)); bottom: calc(84px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--brand); color: var(--brand-ink);
  font-size: 1.8rem; text-decoration: none; box-shadow: 0 4px 14px rgba(0,0,0,.2);
}
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a {
  position: relative; flex: 1; max-width: 150px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 10px; font-size: .75rem; font-weight: 600;
  color: var(--muted); text-decoration: none;
}
.tabbar a span { font-size: 1.3rem; filter: grayscale(1); opacity: .7; }
.tabbar a.active { color: var(--brand); }
.tabbar a.active span { filter: none; opacity: 1; }
.badge {
  position: absolute; top: 4px; left: calc(50% + 8px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--red); color: #fff; font-size: .7rem; line-height: 18px; text-align: center;
}

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 20; max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 22px;
  background: var(--text); color: var(--bg); font-size: .9rem; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,.2);
}

/* ---------- QR sticker sheets ---------- */
.preview-wrap { margin: 0 16px; overflow: hidden; border-radius: 8px; }
.preview { transform-origin: top left; }
.preview .sheet { background: #fff; margin-bottom: 12px; box-shadow: 0 1px 6px rgba(0,0,0,.15); }
.sheet {
  width: 210mm; height: 297mm;
  display: grid; align-content: start; justify-content: start;
  color: #000; background: #fff; overflow: hidden;
  page-break-after: always; break-after: page;
}
.label {
  display: flex; align-items: center; overflow: hidden;
  border: 0.2mm dashed transparent; border-radius: 2mm;
}
.preview .label:not(.empty) { border-color: #ccc; }
.label.plain { border-color: #999; }
.label-qr svg { width: 100%; height: 100%; display: block; }
.label-qr svg path, .label-qr svg rect { shape-rendering: crispEdges; }
.label-text { display: flex; flex-direction: column; gap: 0.8mm; min-width: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.label-code { font-weight: 800; line-height: 1; letter-spacing: 0.02em; }
.label-name { font-weight: 600; line-height: 1.15; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.label-loc { color: #444; line-height: 1.15; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

.print-area { display: none; }

@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .app, .tabbar, .toast, .fab { display: none !important; }
  .print-area { display: block; }
  .print-area .sheet:last-child { page-break-after: auto; break-after: auto; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

.form label.inline-field { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.fab { font-weight: 300; line-height: 1; }
/* keep "(optional)" hints on the same line as their label */
.form label:not(.inline-field):not(.check), .form .field { display: block; }
.form label > input:not([type=radio]):not([type=checkbox]), .form label > select, .form label > textarea,
.form .field > .date-row, .form .field > .chips, .form label > .hint { margin-top: 6px; display: flex; }
.form label > input.narrow { display: inline-block; }
.form .field > label { display: block; }
.form label > .hint { display: block; }
.form input.narrow { width: 90px !important; }
.hint { font-weight: 400; }

/* ---------- scanner ---------- */
.search { display: flex; gap: 8px; }
.search input { flex: 1; min-width: 0; }
.scan-btn { border-radius: 24px; min-height: 48px; white-space: nowrap; }
.scanner { padding: 0 16px; display: grid; gap: 12px; }
.scan-view { position: relative; aspect-ratio: 3 / 4; max-height: 60vh; width: 100%; border-radius: var(--radius); overflow: hidden; background: #000; }
.scan-view[hidden] { display: none; }
.scan-view video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame {
  position: absolute; left: 50%; top: 50%; width: 62%; aspect-ratio: 1; transform: translate(-50%, -50%);
  border: 3px solid rgba(255,255,255,.9); border-radius: 18px; box-shadow: 0 0 0 9999px rgba(0,0,0,.35);
}
.scan-status {
  position: absolute; left: 12px; right: 12px; bottom: 12px; margin: 0;
  padding: 8px 12px; border-radius: 10px; text-align: center;
  background: rgba(0,0,0,.6); color: #fff; font-size: .9rem;
}
.manual { padding: 0; }
.tabbar .tab-scan span { filter: none; opacity: 1; }

/* ---------- % left ---------- */
.pct { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; color: var(--text); }
.pct-track { width: 56px; height: 6px; border-radius: 3px; background: var(--gray-soft); overflow: hidden; }
.pct-fill { display: block; height: 100%; background: var(--brand); border-radius: 3px; }
.pct-half .pct-fill { background: color-mix(in srgb, var(--brand) 65%, var(--amber)); }
.pct-low .pct-fill { background: var(--amber); }
.pct-low .pct-num { color: var(--amber); }
.pct-empty .pct-num { color: var(--red); }
.pct-range { width: 100%; accent-color: var(--brand); min-height: 32px; margin: 0 0 8px; }
.pct-out { font-weight: 800; color: var(--brand); margin-left: 6px; }

/* ---------- search results ---------- */
mark { background: color-mix(in srgb, var(--amber) 30%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }
.result-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.result-top .item-name { font-size: 1.05rem; }
.box-chip { flex: none; font-size: .8rem; font-weight: 700; color: var(--brand); background: var(--brand-soft); padding: 2px 8px; border-radius: 10px; }
.result-sub { font-size: .82rem; color: var(--muted); }
.result-sub.via { color: var(--blue); }
.result-facts { display: grid; gap: 3px; margin-top: 4px; font-size: .88rem; color: var(--text); }
.fact-amount { display: flex; align-items: center; gap: 6px; }
.small { font-size: .78rem; }
.form input.pct-range { min-height: 32px; padding: 0; border: 0; background: transparent; }
.form input.pct-range:focus { outline: none; }
.form input.pct-range:focus-visible { outline: 2px solid var(--brand); outline-offset: 4px; }
.tagline { margin: -4px 0 8px !important; font-style: italic; color: var(--brand) !important; font-weight: 600; }
