:root {
  --bg: #f4f7f5;
  --card: #ffffff;
  --text: #1b2a22;
  --muted: #66766d;
  --line: #e1e9e4;
  --brand: #2e9e6a;
  --brand-dark: #237a52;
  --warn: #e08a00;
  --warn-bg: #fff3dc;
  --bad: #d23b3b;
  --bad-bg: #fde6e6;
  --ok-bg: #e3f5eb;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101713;
    --card: #19231d;
    --text: #e8f0ea;
    --muted: #93a69a;
    --line: #26352c;
    --warn-bg: #3a2c0c;
    --bad-bg: #3d1a1a;
    --ok-bg: #173325;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; }

.shell { max-width: 640px; margin: 0 auto; padding: 0 16px calc(96px + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 0 10px;
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
}
.topbar h1 { font-size: 20px; }
.topbar small { color: var(--muted); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--card); border-top: 1px solid var(--line);
  display: flex; justify-content: center; padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; max-width: 200px; background: none; border: 0; padding: 10px 4px 12px;
  color: var(--muted); font-size: 12px; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tabbar button .ico { font-size: 22px; line-height: 1; }
.tabbar button.active { color: var(--brand); font-weight: 600; }
.tabbar .badge { position: absolute; transform: translate(14px, -4px); }

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 6;
  background: var(--brand); color: #fff; border: 0; border-radius: 999px;
  padding: 14px 20px; font-weight: 600; box-shadow: 0 6px 16px rgba(0, 0, 0, .25);
}
.fab:active { background: var(--brand-dark); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 12px; }
.chip { border-radius: 999px; padding: 4px 12px; font-size: 14px; font-weight: 600; }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.ok { background: var(--ok-bg); color: var(--brand); }

.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  display: flex; gap: 12px; align-items: center; text-align: left; width: 100%;
  background: var(--card); border: 1px solid var(--line); border-left: 5px solid var(--line);
  border-radius: var(--radius); padding: 10px 12px;
}
.item.expired { border-left-color: var(--bad); }
.item.soon { border-left-color: var(--warn); }
.item.ok { border-left-color: var(--brand); }
.thumb {
  width: 52px; height: 52px; flex: none; border-radius: 10px; background: var(--bg) center / contain no-repeat;
  display: grid; place-items: center; font-size: 26px;
}
.item .main { flex: 1; min-width: 0; }
.item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .side { text-align: right; flex: none; display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }
.tag { font-size: 12px; font-weight: 700; border-radius: 6px; padding: 2px 8px; background: var(--ok-bg); color: var(--brand); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.low { background: var(--warn-bg); color: var(--warn); }
.qty { font-size: 13px; color: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .big { font-size: 48px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card h2 { font-size: 17px; margin-bottom: 10px; }
.muted { color: var(--muted); font-size: 14px; }

label.field { display: block; margin: 10px 0; font-size: 14px; color: var(--muted); }
label.field > input, label.field > select {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

.btn {
  background: var(--brand); color: #fff; border: 0; border-radius: 10px;
  padding: 11px 16px; font-weight: 600;
}
.btn:active { background: var(--brand-dark); }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--brand); border: 1px solid var(--brand); }
.btn.danger { background: transparent; color: var(--bad); border: 1px solid var(--bad); }
.btn:disabled { opacity: .5; }

.levels { display: flex; flex-wrap: wrap; gap: 8px; }
.level {
  min-width: 52px; padding: 10px 14px; border-radius: 999px; font-weight: 700;
  border: 1px solid var(--line); background: var(--card);
}
.level.active { background: var(--brand); border-color: var(--brand); color: #fff; }

.rule-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.rule-row:first-child { border-top: 0; }
.rule-row label { flex: 1; min-width: 0; }
.rule-row input { width: 76px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); text-align: right; }

.stepper { display: flex; align-items: center; gap: 12px; }
.stepper button {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--card); font-size: 22px; line-height: 1;
}
.stepper .val { min-width: 54px; text-align: center; font-size: 22px; font-weight: 700; }

.big-code {
  font: 700 28px/1 ui-monospace, Menlo, Consolas, monospace; letter-spacing: 4px;
  background: var(--bg); border: 1px dashed var(--brand); border-radius: 10px; padding: 14px; text-align: center;
  user-select: all;
}

dialog#sheet {
  width: min(560px, 100%); max-height: 92dvh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0; background: var(--card); color: var(--text);
}
dialog#sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet-body { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: 92dvh; }
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.sheet-head h2 { font-size: 19px; }
.close { background: none; border: 0; font-size: 26px; line-height: 1; color: var(--muted); padding: 0 4px; }
.actions { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }

#reader { width: 100%; border-radius: 12px; overflow: hidden; background: #000; min-height: 0; }
#reader video { border-radius: 12px; }

.status-line { padding: 10px 12px; border-radius: 10px; margin: 10px 0; font-weight: 600; background: var(--ok-bg); color: var(--brand); }
.status-line.bad { background: var(--bad-bg); color: var(--bad); }
.status-line.warn { background: var(--warn-bg); color: var(--warn); }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: .25s; z-index: 50; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
