/* Layout: one column tool, sticky header with tabs; cards only for results and setup panels. */
:root {
  --bg: #F3F5F8;
  --surface: #FFFFFF;
  --ink: #16233A;
  --muted: #5A6679;
  --line: #DCE2EA;
  --accent: #1F3A5F;
  --accent-ink: #FFFFFF;
  --accent-soft: #E6EDF6;
  --good: #1D7A4E;
  --good-soft: #E3F3EA;
  --bad: #B3392D;
  --bad-soft: #FBE9E6;
  --warn: #9A6400;
  --warn-soft: #FDF1DA;
  --font: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E141D; --surface: #162030; --ink: #E6ECF4; --muted: #9AA7BA; --line: #273447;
    --accent: #8DB2E6; --accent-ink: #0E141D; --accent-soft: #1D2B40;
    --good: #5BC58F; --good-soft: #15301F; --bad: #F08374; --bad-soft: #3A1D1A; --warn: #E2B455; --warn-soft: #36290F;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
button, input, select, textarea { font: inherit; color: inherit; }
code, .mono { font-family: var(--mono); }
[hidden] { display: none !important; }

/* header */
.top {
  position: sticky; top: 0; z-index: 5; background: var(--accent); color: var(--accent-ink);
  padding-top: env(safe-area-inset-top, 0px);
}
.top-in {
  max-width: 980px; margin: 0 auto; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px;
  padding-block: 10px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
@media (prefers-color-scheme: dark) { .top { background: var(--surface); color: var(--ink); border-bottom: 1px solid var(--line); } }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 26px; height: 26px; border: 3px solid currentColor; border-radius: 50%; position: relative; flex: none; }
.brand-mark::after { content: ""; position: absolute; inset: 4px; border-radius: 50%; background: currentColor; }
.brand-name { font-weight: 700; letter-spacing: .02em; }
.brand-exam { font-size: 12.5px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  background: transparent; border: 0; color: inherit; padding: 7px 12px; border-radius: 999px; cursor: pointer; opacity: .78; font-weight: 600;
}
.tabs button[aria-selected="true"] { background: rgba(255,255,255,.18); opacity: 1; }
@media (prefers-color-scheme: dark) { .tabs button[aria-selected="true"] { background: var(--accent-soft); } }
.tabs button:focus-visible, .btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.count { font-family: var(--mono); font-size: 12px; padding: 0 6px; border-radius: 999px; background: rgba(255,255,255,.2); margin-left: 2px; }

main {
  max-width: 980px; margin: 0 auto; padding-block: 18px 40px;
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.view { display: grid; gap: 16px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 42px; padding: 8px 16px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-weight: 600;
  cursor: pointer; text-decoration: none; text-align: center;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.big { min-height: 56px; font-size: 16.5px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); background: transparent; }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13.5px; border-radius: 8px; }
.btn.armed { background: var(--bad); color: #fff; border-color: var(--bad); }
.scan-actions { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
.scan-actions #galleryBtn { grid-column: 1 / -1; }
@media (max-width: 520px) { .scan-actions { grid-template-columns: 1fr; } }

/* live camera */
.live { display: grid; gap: 10px; }
.live-frame { position: relative; border-radius: 14px; overflow: hidden; background: #000; aspect-ratio: 3 / 4; max-height: 70vh; margin: 0 auto; width: 100%; max-width: 560px; }
.live-frame video, .live-frame canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.live-status {
  position: absolute; left: 10px; right: 10px; bottom: 10px; padding: 10px 12px; border-radius: 10px;
  background: rgba(10, 16, 26, .78); color: #fff; font-weight: 600; text-align: center;
}
.live-status.ok { background: rgba(29, 122, 78, .92); }
.live-status.warn { background: rgba(154, 100, 0, .92); }

.notice { padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); color: var(--ink); border: 1px solid color-mix(in srgb, var(--warn) 35%, var(--line)); }
.notice strong { color: var(--warn); }
.busy { padding: 14px; border-radius: 12px; background: var(--accent-soft); font-weight: 600; text-align: center; }

/* result card */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px; display: grid; gap: 12px; }
.card.fail { border-color: color-mix(in srgb, var(--bad) 45%, var(--line)); }
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 12px; align-items: flex-start; }
.who { min-width: 0; }
.who .num { font-family: var(--mono); font-size: 13px; color: var(--muted); }
.who .name { font-size: 19px; font-weight: 700; }
.score { text-align: right; }
.score .big { font-family: var(--mono); font-size: 34px; font-weight: 600; line-height: 1; }
.score .pct { color: var(--muted); font-size: 13px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 12.5px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.chip.good { background: var(--good-soft); color: var(--good); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.review { width: 100%; max-width: 520px; border-radius: 8px; border: 1px solid var(--line); justify-self: center; background: #fff; }
.fix { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px; border-radius: 10px; background: var(--warn-soft); }
.fix input { width: 7ch; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); font-family: var(--mono); }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.tips { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }
.tips summary { cursor: pointer; font-weight: 600; }
.tips ul { margin: 8px 0 2px; padding-left: 20px; color: var(--muted); }

/* results */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat .k { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.stat .v { font-family: var(--mono); font-size: 24px; font-weight: 600; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; }
.toolbar input, .toolbar select { min-height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); padding: 6px 12px; }
.toolbar input { flex: 1 1 200px; min-width: 0; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
table.results { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.results th, .results td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.results th { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; cursor: pointer; user-select: none; }
.results tr:last-child td { border-bottom: 0; }
.results td.num, .results td.mono { font-family: var(--mono); }
.results tbody tr { cursor: pointer; }
.results tbody tr:hover { background: var(--accent-soft); }
.results tr.detail td { white-space: normal; background: var(--bg); cursor: default; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }
.answers-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 6px; margin: 4px 0 10px; }
.ans { font-family: var(--mono); font-size: 13px; padding: 4px 6px; border-radius: 6px; background: var(--surface); border: 1px solid var(--line); }
.ans.good { border-color: color-mix(in srgb, var(--good) 50%, var(--line)); color: var(--good); }
.ans.bad { border-color: color-mix(in srgb, var(--bad) 50%, var(--line)); color: var(--bad); }
.sub { margin: 8px 0 0; font-size: 15px; }
.items { display: grid; gap: 6px; }
.item { display: grid; grid-template-columns: 74px 1fr 54px; gap: 10px; align-items: center; font-size: 13.5px; }
.item .bar { height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; }
.item .bar i { display: block; height: 100%; background: var(--bad); }
.item .lbl { font-family: var(--mono); }
.item .val { font-family: var(--mono); text-align: right; color: var(--muted); }
.danger-zone { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 8px; border-top: 1px solid var(--line); }

/* setup */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px; display: grid; gap: 12px; }
.panel h2 { margin: 0; font-size: 17px; }
.field { display: grid; gap: 4px; }
.field span { font-weight: 600; font-size: 13.5px; }
.field input, textarea { width: 100%; min-height: 42px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.field small, .hint { color: var(--muted); font-size: 13px; margin: 0; }
.hint.small { font-size: 12.5px; }
.row-end { display: flex; justify-content: flex-end; align-items: center; gap: 12px; flex-wrap: wrap; }
.keycheck { display: grid; gap: 8px; }
.keyrow { display: grid; grid-template-columns: 64px 1fr; gap: 8px; align-items: start; }
.keyrow b { font-size: 13px; padding-top: 4px; }
.keycells { display: flex; flex-wrap: wrap; gap: 4px; }
.kc { font-family: var(--mono); font-size: 12px; border: 1px solid var(--line); border-radius: 6px; padding: 2px 5px; background: var(--bg); }
.kc em { font-style: normal; color: var(--muted); margin-right: 3px; }
.msg-bad { color: var(--bad); font-size: 13.5px; font-weight: 600; }
.msg-good { color: var(--good); font-size: 13.5px; font-weight: 600; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; max-width: calc(100% - 32px); z-index: 20;
}
@media (prefers-reduced-motion: no-preference) { .card { animation: rise .18s ease-out; } @keyframes rise { from { transform: translateY(6px); opacity: .6; } } }

/* written name strip + handwriting reader */
.namecrop { margin: 0; display: grid; gap: 6px; }
.nc { display: flex; flex-wrap: wrap; gap: 6px; max-width: 760px; }
/* the stored strip covers x 16-190 mm; name piece = x 16-120, number piece = x 136-186 (all 14 mm tall) */
.nc-name, .nc-num { position: relative; overflow: hidden; border-radius: 8px; border: 1px solid var(--line); background: #fff; }
.nc-name { flex: 1 1 260px; aspect-ratio: 104 / 14; }
.nc-num { flex: 0 1 170px; aspect-ratio: 50 / 14; }
.nc img { position: absolute; top: 0; height: 100%; max-width: none; }
.nc-name img { left: 0; width: 167.31%; }
.nc-num img { left: -240%; width: 348%; }
.namecrop figcaption { font-size: 13.5px; line-height: 1.5; }
.namecrop figcaption .btn { margin-left: 4px; vertical-align: middle; }
.ocr-name { font-style: italic; color: var(--muted); }
.names-check { display: grid; gap: 8px; }
.names-grid { display: grid; gap: 14px; margin-top: 10px; }
.nm { display: grid; gap: 6px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.nm:last-child { border-bottom: 0; padding-bottom: 0; }
.nm-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-weight: 600; }
.field select { width: 100%; min-height: 42px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
