:root {
  --green: #1d5e3a;
  --green-2: #2f7d50;
  --green-soft: #e3f0e7;
  --bg: #f3f5f2;
  --card: #ffffff;
  --ink: #17201b;
  --muted: #5d6a62;
  --line: #d9e0db;
  --red: #b42318;
  --red-soft: #fde8e6;
  --amber: #9a5b00;
  --amber-soft: #fff1d6;
  --ok: #2f6f45;
  --radius: 14px;
  --tabbar: 68px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px);
}
a { color: var(--green); }
h2 { font-size: 1.1rem; margin: 24px 0 8px; display: flex; align-items: center; gap: 8px; }
h3 { font-size: 1rem; margin: 0 0 8px; }
small, .muted { color: var(--muted); }

/* ---------- top bar & tab bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  min-height: 56px; padding: 0 8px;
  padding-top: env(safe-area-inset-top);
  background: var(--green); color: #fff;
}
.topbar h1 { flex: 1; margin: 0; font-size: 1.15rem; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbtn {
  min-width: 80px; height: 44px; border: 0; background: none; color: #fff;
  font: inherit; font-weight: 600; display: flex; align-items: center; cursor: pointer; text-decoration: none;
}
.topbtn.right { justify-content: flex-end; }
.topbtn[hidden] { visibility: hidden; display: flex; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; height: var(--tabbar); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; text-decoration: none; color: var(--muted); font-size: 0.78rem; font-weight: 600;
}
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.tabbar a.active { color: var(--green); }
.tabbar a.active svg { stroke-width: 2.4; }

main { max-width: 760px; margin: 0 auto; padding: 16px; }

/* ---------- building blocks ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
}
.card.notice { background: var(--amber-soft); border-color: #f1d49b; }
.card.good { background: var(--green-soft); border-color: #bcd9c5; }

.btn, .big-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:active, .big-btn:active { transform: translateY(1px); }
.btn.primary, .big-btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.danger { color: var(--red); border-color: #efc2bd; }
.btn.small { min-height: 40px; padding: 0 12px; font-size: 0.95rem; }
.big-btn { width: 100%; min-height: 64px; font-size: 1.15rem; margin-bottom: 12px; }
.big-btn svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.btn-row > * { flex: 1 1 auto; }
button:disabled { opacity: 0.55; cursor: default; }

.list { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; min-height: 64px;
  border-bottom: 1px solid var(--line); color: inherit; text-decoration: none;
}
.row:last-child { border-bottom: 0; }
.row .main { flex: 1; min-width: 0; }
.row .main b { display: block; overflow-wrap: anywhere; }
.row .main small { display: block; }
a.row:active { background: var(--green-soft); }
.chev { color: var(--muted); font-size: 1.4rem; }

.pill {
  display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; white-space: nowrap;
  background: var(--green-soft); color: var(--ok);
}
.pill.overdue, .pill.reorder { background: var(--red-soft); color: var(--red); }
.pill.due, .pill.soon, .pill.short { background: var(--amber-soft); color: var(--amber); }
.pill.none { background: #eef0ee; color: var(--muted); }
.count { background: var(--red); color: #fff; border-radius: 999px; padding: 0 9px; font-size: 0.85rem; }
.count.amber { background: var(--amber); }

.alert {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px; margin-bottom: 12px;
  border-radius: var(--radius); background: var(--red-soft); color: var(--red); font-weight: 600; text-decoration: none;
}
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.stat small { display: block; line-height: 1.2; }
.stat b { font-size: 1.6rem; display: block; }

/* ---------- forms ---------- */
label { display: block; font-weight: 600; margin: 14px 0 6px; }
label .opt { font-weight: 400; color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: 48px; padding: 10px 12px;
  border: 1px solid #b9c4bd; border-radius: 10px; background: #fff; color: var(--ink);
  font: inherit;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid #9cc9ab; border-color: var(--green-2); }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > input[type=number] { max-width: 110px; }
.hint { color: var(--muted); font-size: 0.92rem; margin: 6px 0 0; }
.search { margin-bottom: 12px; }
.check {
  display: flex; align-items: center; gap: 12px; padding: 12px 0; margin: 0; font-weight: 400;
  border-bottom: 1px solid #f1d49b; cursor: pointer;
}
.check:last-child { border-bottom: 0; }
.check input { width: 26px; height: 26px; min-height: 0; flex: none; accent-color: var(--green); }
.check.plain { border: 0; padding: 6px 0; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch { position: relative; width: 60px; height: 34px; flex: none; margin: 0; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; min-height: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: #b9c4bd; transition: .2s; pointer-events: none; }
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 28px; height: 28px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: .2s;
}
.switch input:checked + span { background: var(--green-2); }
.switch input:checked + span::after { transform: translateX(26px); }
.switch input:focus-visible + span { outline: 3px solid #9cc9ab; }
details summary { font-weight: 600; cursor: pointer; padding: 12px 0 4px; }
.part-line { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.part-line:last-child { border-bottom: 0; }
.part-line .main { flex: 1; min-width: 0; }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 1.3rem; cursor: pointer; }
.stepper input { width: 64px; text-align: center; min-height: 44px; padding: 4px; }
.part-pick { display: flex; gap: 8px; margin-bottom: 8px; }
.part-pick select { flex: 1; }
.part-pick input { width: 84px; }
.part-pick button { width: 48px; flex: none; }

.segmented { display: flex; background: #e4e9e5; border-radius: 12px; padding: 4px; margin-bottom: 16px; }
.segmented a { flex: 1; text-align: center; padding: 10px 4px; border-radius: 9px; text-decoration: none; color: var(--ink); font-weight: 600; font-size: 0.95rem; }
.segmented a.active { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.12); color: var(--green); }

/* ---------- scanner ---------- */
#reader { width: 100%; border-radius: var(--radius); overflow: hidden; background: #000; min-height: 240px; }
#reader video { border-radius: var(--radius); }

/* ---------- tables (reports) ---------- */
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: #f6f8f6; font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
td.num, th.num { text-align: right; }
tr.flag td { background: #fff8f7; }
.report-head { display: none; }

/* ---------- labels ---------- */
.labels { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.label-card {
  background: #fff; border: 1px dashed #9aa79f; border-radius: 10px; padding: 10px;
  display: flex; align-items: center; gap: 10px; break-inside: avoid;
}
.label-card svg { width: 96px; height: 96px; flex: none; }
.label-card b { display: block; font-size: 1.15rem; letter-spacing: 0.04em; }
.label-card span { font-size: 0.85rem; }

/* ---------- toast & dialog ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  max-width: min(92vw, 520px); padding: 14px 18px; border-radius: 12px; background: #17201b; color: #fff;
  font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; z-index: 30; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--red); }
dialog { border: 0; border-radius: 16px; padding: 20px; width: min(92vw, 420px); }
dialog::backdrop { background: rgba(10, 20, 14, .45); }
dialog h3 { font-size: 1.15rem; }

@media (min-width: 700px) {
  .tabbar { justify-content: center; }
  .tabbar a { flex: 0 0 120px; }
}

@media print {
  body { background: #fff; padding: 0; font-size: 12px; }
  .topbar, .tabbar, .no-print, #toast { display: none !important; }
  main { max-width: none; padding: 0; }
  .report-head { display: block; }
  .table-wrap, .card, .list { border: 0; overflow: visible; }
  th, td { padding: 4px 6px; }
  .labels { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .label-card { border: 1px dashed #999; }
  a { color: inherit; text-decoration: none; }
}
