:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #17201e;
  --muted: #5e6b68;
  --line: #d9e0de;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --dup: #a16207;
  --dup-bg: #fef3c7;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --q: #1d4ed8;
  --q-bg: #dbeafe;
  --radius: 12px;
  font-family: "Sarabun", "Noto Sans Thai", -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --surface: #182120;
    --text: #e7eeec;
    --muted: #93a29e;
    --line: #2b3735;
    --brand: #14b8a6;
    --brand-ink: #04201d;
    --ok: #4ade80;
    --ok-bg: #12301f;
    --dup: #fbbf24;
    --dup-bg: #33280c;
    --bad: #f87171;
    --bad-bg: #3a1515;
    --q: #93c5fd;
    --q-bg: #172a4a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.mono { font-family: ui-monospace, Menlo, monospace; }
.nowrap { white-space: nowrap; }
.text-ok { color: var(--ok); }
.text-bad { color: var(--bad); }
.h2 { font-size: 15px; margin: 18px 0 8px; color: var(--muted); font-weight: 600; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.title { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pills { display: flex; gap: 6px; flex-shrink: 0; }
.pill { font: inherit; font-size: 13px; border: 0; border-radius: 99px; padding: 3px 10px; background: rgba(255,255,255,.2); color: inherit; }
.pill-off { background: var(--bad); color: #fff; }
.pill-queue { background: #fff; color: var(--q); font-weight: 700; cursor: pointer; }

.tabs { display: flex; background: var(--surface); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; font: inherit; font-size: 15px; padding: 12px 6px; border: 0; background: none;
  color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; cursor: pointer;
}
.tab.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 700; }
.demo-banner { background: var(--dup-bg); color: var(--dup); padding: 8px 16px; font-size: 14px; }

.view { max-width: 1100px; margin: 0 auto; padding: 14px 16px 40px; }
.view.narrow { max-width: 480px; }

.row-2 { display: grid; grid-template-columns: 1.5fr 1fr; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); min-width: 0; }
.field-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 10px; }
.map-details { margin-top: 12px; }
.map-details > summary { cursor: pointer; font-size: 14px; color: var(--brand); font-weight: 600; }
input, select {
  font: inherit; font-size: 16px; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-width: 0; width: 100%;
}
input[type=file] { padding: 8px; }
#roundSelect { font-weight: 700; }

.btn {
  font: inherit; font-size: 15px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 10px; padding: 10px 14px; cursor: pointer; white-space: nowrap;
}
.btn:disabled { opacity: .6; cursor: wait; }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 700; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.btn-danger { color: var(--bad); border-color: var(--bad); }
.danger-zone { margin-top: 24px; }
.danger-zone > summary { cursor: pointer; font-size: 14px; color: var(--muted); }
.link { font: inherit; color: var(--brand); background: none; border: 0; padding: 0; cursor: pointer; text-align: left; text-decoration: underline; }

.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.metric {
  display: flex; flex-direction: column; text-align: left; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 12px;
}
.metric span { font-size: 13px; color: var(--muted); }
.metric b { font-size: 26px; font-weight: 800; line-height: 1.15; font-variant-numeric: tabular-nums; }
.m-ok { background: var(--ok-bg); border-color: transparent; }
.m-ok span, .m-ok b { color: var(--ok); }
.m-left { background: var(--dup-bg); border-color: transparent; }
.m-left span, .m-left b { color: var(--dup); }
button.metric { cursor: pointer; }
.metrics.wide b { font-size: 32px; }

.display {
  background: var(--surface); border: 2px solid var(--line); border-radius: var(--radius);
  height: 98px; overflow: hidden;
}
#dEntry { padding: 4px 14px 6px; }
.d-label { font-size: 13px; color: var(--muted); }
.d-num { font-size: 40px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.d-preview { font-size: 16px; color: var(--muted); min-height: 24px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.d-preview b { color: var(--text); font-size: 18px; }
.d-preview.is-bad { color: var(--bad); font-weight: 700; }
.is-dup { color: var(--dup); font-weight: 700; }

.msg { height: 100%; padding: 8px 14px; border-left: 8px solid; display: flex; flex-direction: column; justify-content: center; }
.msg.collapsed { display: none; }
.msg-title { font-weight: 800; font-size: 22px; }
.msg-note { font-size: 15px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#display:has(.msg:not(.collapsed):not(.hidden)) { border-color: transparent; }
.m-ok.msg { background: var(--ok-bg); border-color: var(--ok); }
.m-ok .msg-title { color: var(--ok); }
.m-dup.msg { background: var(--dup-bg); border-color: var(--dup); }
.m-dup .msg-title { color: var(--dup); }
.m-bad.msg { background: var(--bad-bg); border-color: var(--bad); }
.m-bad .msg-title { color: var(--bad); }

.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px; }
.key {
  font: inherit; font-size: 26px; font-weight: 600; height: 50px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer;
  touch-action: manipulation; user-select: none;
}
.key:active { background: var(--line); }
.key-fn { font-size: 16px; color: var(--muted); }
.btn-go { width: 100%; height: 56px; margin-top: 6px; font-size: 22px; touch-action: manipulation; }
.btn-undo { width: 100%; margin-top: 8px; color: var(--muted); }

.block { margin-top: 18px; }
details.block > summary { cursor: pointer; font-size: 15px; font-weight: 600; color: var(--muted); padding: 6px 0; }
.chips { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 14px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 99px; padding: 4px 10px; cursor: pointer; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip b { color: var(--dup); margin-right: 2px; }

.recent { list-style: none; margin: 6px 0 0; padding: 0; }
.recent li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.rc-q { font-weight: 800; font-size: 18px; min-width: 36px; text-align: right; font-variant-numeric: tabular-nums; }
.rc-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rc-main b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rc-main small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rc-time { font-size: 13px; color: var(--muted); }
.tag { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 99px; flex-shrink: 0; min-width: 56px; text-align: center; }
.tag-ok { background: var(--ok-bg); color: var(--ok); }
.tag-dup { background: var(--dup-bg); color: var(--dup); }
.tag-bad { background: var(--bad-bg); color: var(--bad); }
.tag-queued { background: var(--q-bg); color: var(--q); }
.tag-pending { background: var(--line); color: var(--muted); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; }
.toolbar select, .toolbar input { width: auto; flex: 1 1 200px; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.grid { border-collapse: collapse; width: 100%; font-size: 14px; }
.grid th, .grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.grid th { background: var(--bg); font-weight: 600; white-space: nowrap; }
.grid td:nth-child(2) { min-width: 160px; }
.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.yes { color: var(--ok); font-weight: 600; white-space: nowrap; }
.grid td.no { color: var(--dup); white-space: nowrap; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.panel .field { margin-bottom: 10px; }
.panel .h2 { margin-top: 0; }
#previewTable { font-size: 13px; }
#previewTable td, #previewTable th { white-space: nowrap; padding: 6px 8px; }
#previewTable td:nth-child(2) { min-width: 0; }
.warn { color: var(--dup); font-weight: 700; }

#view-checkin .field span { font-size: 12px; }
#view-checkin { padding-top: 10px; }
#view-checkin input, #view-checkin select { padding: 8px 10px; }
@media (max-width: 330px) {
  .row-2 { grid-template-columns: 1fr; }
  .metric b { font-size: 24px; }
  .key { height: 56px; }
}
