:root {
  --bg: #f3f5f8;
  --card: #fff;
  --text: #1d2733;
  --muted: #677487;
  --line: #dde3ea;
  --primary: #1f4e79;
  --primary-2: #2c6aa3;
  --ok: #1e8e3e;
  --ok-bg: #e6f4ea;
  --short: #c5221f;
  --short-bg: #fce8e6;
  --over: #b06000;
  --over-bg: #fef7e0;
  --unknown: #a50e0e;
  --unknown-bg: #f9d6d5;
  --none: #5f6368;
  --none-bg: #eceff1;
  --radius: 10px;
  font-family: 'Sarabun', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { font-size: 16px; line-height: 1.45; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--short); min-height: 1.2em; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
h2 { margin: 0 0 12px; font-size: 1.25rem; }
h3 { margin: 0 0 8px; font-size: 1.05rem; }

/* ปุ่ม/ฟอร์ม */
.btn {
  appearance: none; border: 1px solid var(--line); background: #fff; color: var(--text);
  border-radius: 8px; padding: 9px 14px; font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; justify-content: center; text-decoration: none;
}
.btn:hover { border-color: #b8c3cf; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.danger { color: var(--short); border-color: #f0b8b4; }
.btn.warn { background: var(--over-bg); border-color: #f3d58a; color: #7a4300; }
.btn.ghost { background: transparent; border-color: transparent; color: inherit; }
.btn.small { padding: 5px 10px; font-size: .9rem; }
.btn.block { width: 100%; }
.btn.big { padding: 14px 18px; font-size: 1.05rem; }
label { display: block; font-size: .9rem; color: var(--muted); margin-bottom: 10px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; font: inherit; color: var(--text);
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc3e6; border-color: var(--primary-2); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; }
.row > .shrink { flex: 0 0 auto; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; background: var(--primary); color: #fff;
}
.brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.topbar select { width: auto; max-width: 52vw; margin: 0; padding: 6px 10px; }
.topbar .btn.ghost { color: #fff; }
.spacer { flex: 1; }
.tabs {
  position: sticky; top: 50px; z-index: 19; display: flex; gap: 2px; overflow-x: auto;
  background: #fff; border-bottom: 1px solid var(--line); padding: 0 8px; scrollbar-width: none;
}
.tabs button {
  border: 0; background: none; font: inherit; padding: 11px 12px; color: var(--muted);
  border-bottom: 3px solid transparent; cursor: pointer; white-space: nowrap; font-weight: 600;
}
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }
main { max-width: 1280px; margin: 0 auto; padding: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.badge { background: var(--short); color: #fff; border-radius: 999px; padding: 0 7px; font-size: .75rem; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); }
.login-card h1 { margin: 0; color: var(--primary); }

/* ตาราง */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { padding: 7px 9px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { background: #f7f9fb; position: sticky; top: 0; font-weight: 600; white-space: nowrap; }
tbody tr:hover { background: #fafcff; }
tfoot td { font-weight: 700; background: #f7f9fb; }

/* สถานะ */
.st { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .82rem; font-weight: 600; white-space: nowrap; }
.st-MATCH { background: var(--ok-bg); color: var(--ok); }
.st-SHORT { background: var(--short-bg); color: var(--short); }
.st-OVER { background: var(--over-bg); color: var(--over); }
.st-UNKNOWN { background: var(--unknown-bg); color: var(--unknown); }
.st-NOT_COUNTED { background: var(--none-bg); color: var(--none); }
.neg { color: var(--short); }
.pos { color: var(--over); }

/* หน้าสแกน */
.scan-head { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
@media (max-width: 640px) { .scan-head { grid-template-columns: 1fr; } }
.scan-input-wrap { display: flex; gap: 8px; margin-top: 6px; }
.scan-input-wrap input { font-size: 1.35rem; padding: 12px 14px; margin: 0; letter-spacing: .5px; }
.mode-toggle { display: flex; gap: 6px; align-items: center; margin-top: 10px; font-size: .9rem; color: var(--muted); }
.mode-toggle input { width: auto; margin: 0; display: inline; }
.result { border-radius: var(--radius); padding: 16px; border: 2px solid var(--line); background: #fff; margin-bottom: 14px; }
.result.ok { border-color: #8bd0a0; background: #f4fbf6; }
.result.unknown { border-color: #f09b96; background: #fff4f3; }
.result.pending { border-color: #9cc3e6; background: #f3f8fd; }
.result.undo { border-color: #f3d58a; background: #fffbef; }
.result .title { font-size: 1.15rem; font-weight: 700; }
.result .codes { color: var(--muted); font-size: .92rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin: 12px 0; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.stat .k { font-size: .78rem; color: var(--muted); }
.stat .v { font-size: 1.45rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat.big .v { font-size: 2rem; color: var(--primary); }
.history li { display: flex; gap: 8px; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.history { list-style: none; margin: 0; padding: 0; font-size: .92rem; }

/* summary cards */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.kpi .k { color: var(--muted); font-size: .85rem; }
.kpi .v { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.progress { height: 8px; background: var(--none-bg); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.progress > div { height: 100%; background: var(--ok); }

/* ภาพ */
.gallery { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.photo { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; font-size: .88rem; }
.photo img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; background: #eee; cursor: zoom-in; }
.photo .meta { padding: 8px 10px; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumbs img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 32, .55); z-index: 50; display: grid; place-items: center; padding: 12px; }
.modal { background: #fff; border-radius: 12px; width: min(560px, 100%); max-height: 92vh; overflow: auto; padding: 18px; }
.modal.wide { width: min(900px, 100%); }
.modal header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
#camera-reader { width: 100%; min-height: 240px; background: #000; border-radius: 8px; overflow: hidden; }
.lightbox img { max-width: 100%; max-height: 80vh; display: block; margin: 0 auto; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60;
  background: #263238; color: #fff; padding: 10px 16px; border-radius: 8px; max-width: 90vw; box-shadow: 0 4px 18px rgba(0,0,0,.2);
}
.toast.err { background: var(--short); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; }
.msg-list { max-height: 220px; overflow: auto; font-size: .88rem; margin: 6px 0; padding-left: 18px; }
.flash-ok { animation: flashok .5s; }
.flash-bad { animation: flashbad .6s; }
@keyframes flashok { from { background: #c8ecd3; } }
@keyframes flashbad { from { background: #f7b4af; } }

@media print {
  .topbar, .tabs, .no-print, button { display: none !important; }
  main { padding: 0; max-width: none; }
  .card { border: 0; padding: 0; }
}
