:root {
  --ink: #0f2a2e;
  --ink-2: #3c5559;
  --muted: #6b7f82;
  --line: #dde5e4;
  --bg: #f4f7f6;
  --card: #ffffff;
  --accent: #0e7c6b;
  --accent-ink: #ffffff;
  --accent-soft: #e2f3ef;
  --warn: #9a5b00;
  --warn-soft: #fff4e0;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sukhumvit Set", "Thonburi", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { overscroll-behavior: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
input { font: inherit; }

.app { min-height: 100%; display: flex; flex-direction: column; max-width: 560px; margin: 0 auto; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 12px; padding: 12px 16px; font-weight: 600; font-size: 16px; cursor: pointer;
  transition: transform .06s ease, background .15s ease, opacity .15s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-block { display: block; width: 100%; }
.btn-big { padding: 18px; font-size: 19px; border-radius: 14px; }
.small { font-size: 13px; }
.muted { color: var(--muted); }

/* ---------- lock ---------- */
.lock { flex: 1; display: grid; place-items: center; padding: 24px; padding-top: calc(24px + var(--safe-t)); }
.lock-card { width: 100%; max-width: 340px; text-align: center; }
.lock-icon { border-radius: 16px; margin-bottom: 8px; }
.lock h1 { margin: 8px 0 4px; font-size: 24px; }
.lock p { margin: 0 0 20px; }
.pin-input {
  width: 100%; text-align: center; font-size: 30px; letter-spacing: .4em; padding: 14px; margin-bottom: 12px;
  border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--ink);
}
.pin-input:focus, .plate-search input:focus, .remark input:focus { outline: 3px solid var(--accent-soft); border-color: var(--accent); }
.form-error { color: var(--danger); min-height: 1.4em; margin: 10px 0 0; font-size: 14px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + var(--safe-t)) 16px 10px; position: sticky; top: 0; z-index: 5; background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; white-space: nowrap; min-width: 0; }
.brand span { overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 430px) { .brand span { display: none; } }
.brand img { border-radius: 8px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.counters { display: flex; align-items: baseline; gap: 6px; font-size: 13px; color: var(--ink-2); background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; white-space: nowrap; }
.counters b { font-size: 15px; color: var(--ink); font-variant-numeric: tabular-nums; }
.counters .dot { color: var(--line); }
.lang { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 700; cursor: pointer; }

main { flex: 1; padding: 0 16px calc(84px + var(--safe-b)); }

/* ---------- scanner ---------- */
.viewport {
  position: relative; width: 100%; aspect-ratio: 3 / 4; max-height: 62vh; margin: 4px auto 0;
  background: #0b1416; border-radius: 22px; overflow: hidden;
}
.viewport video { width: 100%; height: 100%; object-fit: cover; display: block; }
.frame { position: absolute; inset: 0; pointer-events: none; }
.frame i { position: absolute; width: 44px; height: 44px; border: 4px solid rgba(255,255,255,.92); }
.frame i:nth-child(1) { top: 18%; left: 14%; border-right: 0; border-bottom: 0; border-top-left-radius: 14px; }
.frame i:nth-child(2) { top: 18%; right: 14%; border-left: 0; border-bottom: 0; border-top-right-radius: 14px; }
.frame i:nth-child(3) { bottom: 18%; left: 14%; border-right: 0; border-top: 0; border-bottom-left-radius: 14px; }
.frame i:nth-child(4) { bottom: 18%; right: 14%; border-left: 0; border-top: 0; border-bottom-right-radius: 14px; }
.sweep { position: absolute; left: 16%; right: 16%; top: 20%; height: 2px; background: linear-gradient(90deg, transparent, #5ef0d2, transparent); animation: sweep 2.2s ease-in-out infinite; opacity: .85; }
@keyframes sweep { 0%,100% { top: 21%; } 50% { top: 78%; } }
.viewport.paused .sweep { animation-play-state: paused; opacity: 0; }
.viewport.hit .frame i { border-color: #5ef0d2; }
.torch { position: absolute; right: 12px; bottom: 12px; width: 44px; height: 44px; border-radius: 999px; border: 0; background: rgba(0,0,0,.45); color: #fff; display: grid; place-items: center; cursor: pointer; }
.torch.on { background: #fff; color: #0b1416; }
.viewport-msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 28px; color: #e9f1f0; background: #0b1416; font-size: 15px; line-height: 1.5; }
.viewport-msg .btn { margin-top: 14px; }
.scan-status { text-align: center; color: var(--ink-2); margin: 14px 8px 18px; min-height: 2.6em; font-size: 15px; }
.scan-status.bad { color: var(--warn); font-weight: 600; }

.plate-search { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.plate-search label { display: block; font-size: 14px; color: var(--ink-2); margin-bottom: 8px; }
.plate-search .row { display: flex; gap: 8px; }
.plate-search input { flex: 1; min-width: 0; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 18px; background: var(--bg); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex; justify-content: center; gap: 8px;
  padding: 8px 16px calc(8px + var(--safe-b)); background: rgba(244,247,246,.92); backdrop-filter: blur(10px); border-top: 1px solid var(--line);
}
.tabbar button { flex: 1; max-width: 200px; border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px; color: var(--muted); font-size: 12px; font-weight: 600; cursor: pointer; border-radius: 12px; }
.tabbar button.active { color: var(--accent); background: var(--accent-soft); }

/* ---------- sheet ---------- */
.scrim { position: fixed; inset: 0; background: rgba(8,20,22,.45); z-index: 20; animation: fade .18s ease; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-width: 560px; margin: 0 auto;
  background: var(--card); border-radius: 22px 22px 0 0; padding: 10px 18px calc(18px + var(--safe-b));
  box-shadow: 0 -10px 40px rgba(0,0,0,.18); animation: up .22s cubic-bezier(.2,.8,.2,1); max-height: 92vh; overflow: auto;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 14px; }
.sheet-loading { text-align: center; padding: 26px 0 30px; color: var(--ink-2); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); margin: 0 auto 12px; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.plate-box { text-align: center; border: 2px solid var(--ink); border-radius: 12px; padding: 8px 12px 10px; margin: 0 auto 14px; max-width: 280px; background: #fff; }
.plate-box small { display: block; font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.plate-box strong { font-size: 32px; letter-spacing: .04em; }

.notice { border-radius: 12px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }

.options { border: 0; padding: 0; margin: 0 0 12px; }
.options legend { font-size: 14px; color: var(--ink-2); margin-bottom: 8px; padding: 0; }
.opt {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; margin-bottom: 8px;
  border: 2px solid var(--line); background: var(--card); border-radius: 14px; padding: 14px; font-size: 17px; font-weight: 600; cursor: pointer;
}
.opt .radio { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; display: grid; place-items: center; }
.opt[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); }
.opt[aria-checked="true"] .radio { border-color: var(--accent); }
.opt[aria-checked="true"] .radio::after { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.no-options { color: var(--danger); font-size: 14px; }

.remark { margin-bottom: 14px; }
.remark summary { cursor: pointer; color: var(--accent); font-size: 14px; font-weight: 600; padding: 4px 0; }
.remark input { width: 100%; margin-top: 8px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 16px; background: var(--bg); }

#sheetMessage { text-align: center; padding: 8px 4px 4px; }
#sheetMessage h3 { margin: 10px 0 6px; font-size: 20px; }
#sheetMessage p { margin: 0 0 18px; line-height: 1.5; }
.msg-icon { width: 56px; height: 56px; border-radius: 50%; margin: 0 auto; display: grid; place-items: center; font-size: 28px; font-weight: 800; }
.msg-icon.warn { background: var(--warn-soft); color: var(--warn); }
.msg-icon.err { background: var(--danger-soft); color: var(--danger); }

/* ---------- success flash ---------- */
.flash { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: #0e7c6b; color: #fff; animation: fade .15s ease; }
.flash-card { text-align: center; padding: 24px; }
.flash h3 { font-size: 30px; margin: 14px 0 6px; }
.flash p { font-size: 18px; margin: 0; opacity: .95; }
.flash .tick { stroke-dasharray: 40; stroke-dashoffset: 40; animation: draw .35s .1s ease forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }

/* ---------- history ---------- */
.history-head h2 { margin: 6px 0 2px; font-size: 22px; }
.history-head p { margin: 0 0 14px; }
.history-day { margin: 18px 0 6px; font-size: 13px; font-weight: 700; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; display: flex; justify-content: space-between; }
.history-item { display: grid; grid-template-columns: 52px 1fr auto; align-items: center; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; }
.history-item time { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 14px; }
.history-item .what strong { display: block; font-size: 16px; }
.history-item .what span { font-size: 13px; color: var(--muted); }
.badge { font-size: 12px; font-weight: 700; border-radius: 999px; padding: 4px 8px; }
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.unconfirmed { background: var(--warn-soft); color: var(--warn); }
.badge.failed, .badge.error { background: var(--danger-soft); color: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.diag { margin-top: 28px; border-top: 1px solid var(--line); padding-top: 12px; }
.diag summary { cursor: pointer; color: var(--ink-2); font-size: 14px; }
.diag ul { padding-left: 18px; font-size: 13px; }
.diag a { color: var(--accent); }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%); z-index: 40; background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.2); animation: fade .15s ease; }

@media (min-width: 700px) {
  .viewport { max-height: 56vh; }
  .sheet { bottom: 24px; border-radius: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
