:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #17212b;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #a16207;
  --warn-bg: #fef9c3;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --radius: 14px;
  --tabbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1218;
    --surface: #131d26;
    --text: #e6edf3;
    --muted: #93a4b8;
    --line: #23313d;
    --brand: #14b8a6;
    --brand-ink: #04201d;
    --ok: #4ade80;
    --ok-bg: #0f2f1c;
    --warn: #facc15;
    --warn-bg: #2f2a0b;
    --bad: #f87171;
    --bad-bg: #3a1414;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Sarabun", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: rgba(255,255,255,.2); }
.pill.off { background: #f59e0b; color: #1f1300; }

main { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; }
.view { display: none; }
.view.active { display: block; }

.sub { font-size: 15px; margin: 20px 0 8px; color: var(--muted); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

.btn {
  appearance: none; border: 1px solid var(--line); background: var(--surface);
  padding: 11px 16px; border-radius: 12px; font-weight: 600; cursor: pointer;
  min-height: 44px;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.block { display: block; width: 100%; }
.input {
  width: 100%; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); min-height: 44px;
}
.row { display: flex; gap: 8px; }
.stack { display: grid; gap: 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 12px 0; }
.field { display: block; margin: 10px 0; font-size: 14px; color: var(--muted); }
.field .input { margin-top: 4px; color: var(--text); }
.check { display: flex; gap: 10px; align-items: center; padding: 8px 0; }
.check input { width: 20px; height: 20px; accent-color: var(--brand); }

/* scanner */
.progress-mini { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 14px; color: var(--muted); }
.bar { flex: 1; height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.fill { height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.reader {
  width: 100%; min-height: 220px; border-radius: var(--radius); overflow: hidden;
  background: #000 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='64' height='64' viewBox='0 0 24 24' fill='none' stroke='%23475569' stroke-width='1.5'%3E%3Cpath d='M3 7V5a2 2 0 0 1 2-2h2M17 3h2a2 2 0 0 1 2 2v2M21 17v2a2 2 0 0 1-2 2h-2M7 21H5a2 2 0 0 1-2-2v-2M7 8v8M10 8v8M13 8v8M17 8v8'/%3E%3C/svg%3E") center/64px no-repeat;
}
.reader video { display: block; width: 100% !important; height: auto !important; object-fit: cover; }
.reader.flash-ok { box-shadow: 0 0 0 4px var(--ok); }
.reader.flash-warn { box-shadow: 0 0 0 4px var(--warn); }
.reader.flash-bad { box-shadow: 0 0 0 4px var(--bad); }
.scan-controls { display: flex; gap: 8px; margin: 10px 0; }
.scan-controls .btn { flex: 1; }
.scan-controls select { flex: 1.2; }

.result {
  border-radius: var(--radius); padding: 14px 16px; border: 2px solid var(--line);
  background: var(--surface); transition: background .2s, border-color .2s;
}
.result-title { font-weight: 800; font-size: 20px; }
.result-body { font-size: 15px; word-break: break-word; }
.result .code { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 700; }
.result.ok { background: var(--ok-bg); border-color: var(--ok); }
.result.ok .result-title { color: var(--ok); }
.result.warn { background: var(--warn-bg); border-color: var(--warn); }
.result.warn .result-title { color: var(--warn); }
.result.bad { background: var(--bad-bg); border-color: var(--bad); }
.result.bad .result-title { color: var(--bad); }
.result.pop { animation: pop .25s ease-out; }
@keyframes pop { from { transform: scale(.96); } to { transform: scale(1); } }

.manual { display: flex; gap: 8px; margin-top: 10px; }
.manual .btn { flex: none; }

/* lists */
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list:empty { display: none; }
.list li { display: flex; gap: 10px; align-items: center; padding: 11px 14px; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list.compact li { padding: 8px 14px; font-size: 14px; }
.list .main { flex: 1; min-width: 0; }
.list .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .s { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; background: var(--line); border: 2px solid var(--muted); }
.dot.ok { background: var(--ok); border-color: var(--ok); }
.dot.warn { background: var(--warn); border-color: var(--warn); }
.dot.bad { background: var(--bad); border-color: var(--bad); }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 99px; flex: none; }
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.miss { background: var(--line); color: var(--muted); }
.list li.tap { cursor: pointer; }

.toolbar { display: grid; gap: 8px; position: sticky; top: 52px; z-index: 5; background: var(--bg); padding: 4px 0 8px; }
.seg { display: flex; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 9px; cursor: pointer; font-size: 14px; }
.seg button.on { background: var(--brand); color: var(--brand-ink); font-weight: 600; }

/* summary */
.donut-wrap { display: grid; place-items: center; margin: 8px 0 16px; }
.donut {
  --p: 0; width: 170px; height: 170px; border-radius: 50%;
  background: conic-gradient(var(--ok) calc(var(--p) * 1%), var(--line) 0);
  display: grid; place-items: center; position: relative;
}
.donut::before { content: ""; position: absolute; inset: 18px; border-radius: 50%; background: var(--bg); }
.donut span { position: relative; font-size: 34px; font-weight: 800; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; }
.stat b { font-size: 26px; }
.stat span { font-size: 13px; color: var(--muted); }
.stat.ok b { color: var(--ok); }
.stat.miss b { color: var(--muted); }
.stat.warn b { color: var(--warn); }
.gbar { width: 80px; height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; flex: none; }
.gbar i { display: block; height: 100%; background: var(--ok); }

/* data */
.file { display: block; }
.file input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.file span { display: block; text-align: center; padding: 18px; border: 2px dashed var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer; font-weight: 600; }
.preview { overflow-x: auto; margin: 10px 0; font-size: 12px; }
.preview table { border-collapse: collapse; white-space: nowrap; }
.preview td, .preview th { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.preview th { background: var(--bg); }

/* tabbar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; height: var(--tabbar-h); border: 0; background: none; cursor: pointer;
  display: grid; place-items: center; align-content: center; gap: 2px;
  font-size: 12px; color: var(--muted);
}
.tabbar button span { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: .7; }
.tabbar button.on { color: var(--brand); font-weight: 700; }
.tabbar button.on span { filter: none; opacity: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; max-width: calc(100% - 32px); transition: .25s; z-index: 30;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* html5-qrcode internals */
#reader__dashboard, #reader__header_message { display: none !important; }

/* topbar buttons */
.top-right { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 32px; height: 32px; border-radius: 50%; border: 0; cursor: pointer;
  background: rgba(255,255,255,.22); color: inherit; font-weight: 800; font-size: 16px;
  display: grid; place-items: center;
}
.icon-btn.dark { background: var(--line); color: var(--text); width: 30px; height: 30px; font-size: 14px; }

/* OCR */
.ocr-btn { margin-bottom: 10px; }
.ocr-panel { margin-top: 0; }
.ocr-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.ocr-panel .bar { margin: 8px 0; }
.ocr-img { display: block; width: 100%; max-height: 140px; object-fit: contain; background: #000; border-radius: 10px; margin: 8px 0; }
.ocr-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; white-space: pre-wrap; word-break: break-all; background: var(--bg); border-radius: 8px; padding: 8px; color: var(--muted); max-height: 90px; overflow: auto; }
.cands { display: grid; gap: 6px; margin: 10px 0; }
.cand {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); cursor: pointer;
}
.cand.best { border-color: var(--brand); }
.cand .main { flex: 1; min-width: 0; }
.cand .t { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 700; }
.cand .s { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conf { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--line); flex: none; }
.conf.hi { background: var(--ok-bg); color: var(--ok); }
.conf.mid { background: var(--warn-bg); color: var(--warn); }
.ocr-actions .btn { flex: 1; }
.ocr-edit { display: flex; gap: 8px; margin: 8px 0; }

/* help */
.help h3 { margin: 0 0 8px; font-size: 17px; }
.help-title { font-size: 22px; margin: 4px 0 10px; }
.help ul, .help ol { padding-left: 22px; margin: 8px 0; }
.help li { margin: 4px 0; }
.help .card { scroll-margin-top: 64px; }
.steps li::marker { font-weight: 800; color: var(--brand); }
.toc { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.toc a { font-size: 13px; padding: 5px 10px; border-radius: 99px; background: var(--surface); border: 1px solid var(--line); color: var(--text); text-decoration: none; }
.legend { display: grid; gap: 8px; }
.legend .result { padding: 10px 12px; }
.legend .result-title { font-size: 16px; }
.legend .result-body { font-size: 13px; }
.code-block { background: var(--bg); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 13px; margin: 8px 0; }
code { background: var(--bg); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.faq dt { font-weight: 700; margin-top: 10px; }
.faq dd { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
a { color: var(--brand); }
