:root {
  --bg: #f3f5f4;
  --surface: #ffffff;
  --text: #15201d;
  --muted: #5d6b67;
  --line: #dde3e1;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --good: #15803d;
  --good-bg: #e7f6ec;
  --bad: #b91c1c;
  --bad-bg: #fdecec;
  --warn: #a16207;
  --warn-bg: #fdf6e3;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1412;
    --surface: #16201d;
    --text: #e8efec;
    --muted: #9aa9a4;
    --line: #2a3733;
    --brand: #14b8a6;
    --brand-ink: #062320;
    --good: #4ade80;
    --good-bg: #12301d;
    --bad: #f87171;
    --bad-bg: #3a1616;
    --warn: #facc15;
    --warn-bg: #352c0c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.brand svg { width: 22px; height: 22px; }
.chip {
  border: 1px solid color-mix(in srgb, var(--brand-ink) 40%, transparent);
  background: transparent; color: inherit; font: inherit; font-size: 14px;
  padding: 4px 12px; border-radius: 999px; max-width: 60%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.banner { margin: 12px 16px 0; padding: 10px 14px; border-radius: 10px; background: var(--warn-bg); color: var(--warn); font-size: 14px; }

main { max-width: 560px; margin: 0 auto; padding: 12px 16px 32px; display: grid; gap: 12px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 12px; }
.card[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.step-head { display: flex; align-items: center; gap: 10px; }
.step-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.step-no { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--brand); color: var(--brand-ink); font-weight: 700; font-size: 14px; flex: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; padding: 10px 16px; border-radius: 12px; cursor: pointer;
  min-height: 44px; text-align: center;
}
.btn svg { width: 22px; height: 22px; flex: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.big { min-height: 56px; font-size: 17px; width: 100%; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: translateY(1px); }

.scanner { display: grid; gap: 8px; }
#reader { width: 100%; border-radius: 12px; overflow: hidden; background: #000; }
#reader video { width: 100% !important; display: block; }

.search { position: relative; }
input[type="search"], input[type="text"], input[type="date"], input[type="number"] {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; min-height: 48px;
}
input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.results {
  list-style: none; margin: 6px 0 0; padding: 4px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); max-height: 280px; overflow-y: auto;
}
.results li button {
  width: 100%; text-align: left; background: none; border: 0; font: inherit; color: var(--text);
  padding: 10px; border-radius: 8px; display: grid; gap: 2px; cursor: pointer;
}
.results li button:active, .results li button:hover { background: var(--bg); }
.results small { color: var(--muted); }

.product { border-left: 4px solid var(--brand); padding: 8px 12px; background: var(--bg); border-radius: 8px; }
.product-name { font-size: 18px; font-weight: 700; }
.product-meta { display: flex; flex-wrap: wrap; gap: 4px 16px; color: var(--muted); font-size: 14px; }
.product-meta b { color: var(--text); font-weight: 600; }

.alert { padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.alert.bad { background: var(--bad-bg); color: var(--bad); }

.ocr-status { font-size: 14px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.ocr-status.error { color: var(--bad); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.preview { width: 100%; max-height: 220px; object-fit: contain; border-radius: 10px; background: var(--bg); }

.dates { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dates label:last-child { grid-column: 1 / -1; }
.dates label, .note { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.hint { margin: 0; font-size: 13px; color: var(--muted); }

.verdict { border-radius: 12px; padding: 14px; display: grid; gap: 2px; }
.verdict-title { font-size: 22px; font-weight: 700; }
.verdict-detail { font-size: 14px; }
.verdict.neutral { background: var(--bg); color: var(--muted); }
.verdict.good { background: var(--good-bg); color: var(--good); }
.verdict.bad { background: var(--bad-bg); color: var(--bad); }
.verdict.warn { background: var(--warn-bg); color: var(--warn); }

.facts { margin: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.facts div { background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.facts dt { font-size: 12px; color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }

.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.history li { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; padding: 8px 10px; background: var(--bg); border-radius: 8px; }
.history .tag { font-weight: 600; white-space: nowrap; }
.history .tag.good { color: var(--good); }
.history .tag.bad { color: var(--bad); }
.history .tag.warn { color: var(--warn); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--surface); padding: 10px 18px; border-radius: 999px;
  font-size: 15px; max-width: calc(100% - 32px); z-index: 20; box-shadow: 0 6px 20px rgb(0 0 0 / .2);
}

@media (max-width: 360px) {
  .dates { grid-template-columns: 1fr; }
  .facts { grid-template-columns: 1fr 1fr; }
}
