/* FaceScan — ธีม ม่วง / ขาว / น้ำเงิน
   โหมดสี: สว่าง / มืด / อัตโนมัติ (ตามเครื่อง) — สลับได้ที่ปุ่มโหมด (assets/theme.js ตั้ง data-theme บน <html>) */
:root {
  --concrete: #f6f5fc; --concrete-2: #ecebf7; --panel: #ffffff; --panel-2: #f3f2fb;
  --ink: #1e1b4b; --ink-2: #5b5883; --rule: #dcd9ef;
  --hivis: #7c3aed; --hivis-ink: #ffffff; --hivis-text: #6d28d9;   /* ม่วง = สีหลัก */
  --blue: #2563eb; --blue-text: #1d4ed8;                             /* น้ำเงิน = สีรอง */
  --silver: #ffffff;
  --safe: #15803d; --caution: #f59e0b; --caution-text: #92400e; --stop: #dc2626;
  --in: #15803d; --out: #2563eb;
  --focus: #2563eb;
  --bar-bg: linear-gradient(100deg, #4c1d95 0%, #3730a3 55%, #1e40af 100%); --bar-ink: #ffffff;
  --r-sm: 6px; --r: 10px; --r-lg: 16px;
  --f-display: "Chakra Petch", "Bai Jamjuree", system-ui, sans-serif;
  --f-body: "Bai Jamjuree", system-ui, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --tape: linear-gradient(180deg, var(--hivis) 0 34%, var(--silver) 34% 66%, var(--blue) 66% 100%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --concrete: #0e0c1d; --concrete-2: #151229; --panel: #17142c; --panel-2: #201c3b;
    --ink: #ece9ff; --ink-2: #a9a4cf; --rule: #2f2a52;
    --hivis: #a78bfa; --hivis-ink: #160d33; --hivis-text: #c4b5fd;
    --blue: #60a5fa; --blue-text: #93c5fd; --silver: #e9e5ff;
    --safe: #4ade80; --caution-text: #fcd34d; --stop: #f87171;
    --in: #4ade80; --out: #60a5fa; --focus: #60a5fa;
    --bar-bg: linear-gradient(100deg, #1f1147 0%, #1a1a4e 55%, #0f2350 100%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --concrete: #0e0c1d; --concrete-2: #151229; --panel: #17142c; --panel-2: #201c3b;
  --ink: #ece9ff; --ink-2: #a9a4cf; --rule: #2f2a52;
  --hivis: #a78bfa; --hivis-ink: #160d33; --hivis-text: #c4b5fd;
  --blue: #60a5fa; --blue-text: #93c5fd; --silver: #e9e5ff;
  --safe: #4ade80; --caution-text: #fcd34d; --stop: #f87171;
  --in: #4ade80; --out: #60a5fa; --focus: #60a5fa;
  --bar-bg: linear-gradient(100deg, #1f1147 0%, #1a1a4e 55%, #0f2350 100%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--concrete); color: var(--ink); font: 16px/1.55 var(--f-body); -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
h1, h2 { font-family: var(--f-display); font-weight: 700; letter-spacing: -.01em; margin: 0; }
h1 { font-size: 26px; line-height: 1.2; }
h2 { font-size: 17px; margin-bottom: 12px; }
.muted { color: var(--ink-2); }
small.muted { font-size: 13px; }
.mono, code { font-family: var(--f-mono); font-size: .92em; }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
.ico { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* reflective tape — signature */
.tape { height: 12px; background: var(--tape); position: relative; overflow: hidden; }
.tape::after { content: ""; position: absolute; inset: 0; background: linear-gradient(100deg, transparent 40%, rgba(255,255,255,.75) 50%, transparent 60%);
  transform: translateX(-100%); }

/* ───── controls ───── */
input, select { font: inherit; font-size: 15px; color: var(--ink); background: var(--panel); border: 1.5px solid var(--rule);
  border-radius: var(--r-sm); padding: 9px 11px; min-width: 0; min-height: 42px; }
input:focus-visible, select:focus-visible { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--ink); font-size: 15px; font-weight: 500; min-height: 32px; }
label.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--hivis); }
.btn { font: inherit; font-weight: 600; font-size: 15px; border: 1.5px solid var(--rule); background: var(--panel); color: var(--ink);
  border-radius: var(--r-sm); padding: 8px 16px; min-height: 42px; cursor: pointer; white-space: nowrap; text-decoration: none;
  display: inline-flex; align-items: center; gap: 8px; justify-content: center; transition: background-color .15s, border-color .15s; }
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--hivis); border-color: var(--hivis); color: var(--hivis-ink); }
.btn.primary:hover { background: color-mix(in srgb, var(--hivis) 85%, var(--blue)); }
.btn.hivis { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.danger { color: var(--stop); }
.btn.danger:hover { border-color: var(--stop); }
.btn.sm { padding: 4px 12px; min-height: 34px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.err { color: var(--stop); min-height: 1.3em; margin: 4px 0 0; font-size: 14px; font-weight: 500; }

/* ───── app shell ───── */
.topbar { position: sticky; top: 0; z-index: 10; background: var(--bar-bg); color: var(--bar-ink); }
.topbar-in { display: flex; align-items: center; gap: 20px; padding: 0 20px; min-height: 60px; flex-wrap: wrap; }
.topbar .tape { height: 8px; }
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--f-display); }
.brand b { font-size: 19px; font-weight: 700; letter-spacing: .02em; }
.brand small { display: block; font-family: var(--f-body); font-size: 13px; font-weight: 500; opacity: .75; }
.logo { width: 34px; height: 34px; border-radius: var(--r-sm); background: var(--hivis); color: var(--hivis-ink);
  display: grid; place-items: center; }
.logo svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.brand.big .logo { width: 48px; height: 48px; } .brand.big .logo svg { width: 30px; height: 30px; }
nav { display: flex; gap: 2px; flex-wrap: nowrap; flex: 1; min-width: 0; align-self: stretch; overflow-x: auto; scrollbar-width: none; }
nav a { display: inline-flex; align-items: center; padding: 0 12px; white-space: nowrap; color: rgba(255,255,255,.72);
  cursor: pointer; text-decoration: none; font-weight: 600; font-size: 15px; border-bottom: 3px solid transparent; min-height: 52px; }
nav a:hover { color: #fff; }
nav a.active { color: #fff; border-bottom-color: #fff; }
nav a.kiosk-link { color: #c7d2fe; }
.who { display: flex; align-items: center; gap: 8px; font-size: 14px; opacity: .9; }
.who .btn { background: transparent; color: #fff; border-color: rgba(255,255,255,.3); }
.who .btn:hover { border-color: #fff; }

main { max-width: 1240px; margin: 0 auto; padding: 28px 20px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.eyebrow { font-family: var(--f-mono); font-size: 12px; font-weight: 500; letter-spacing: .08em; text-transform: uppercase; color: var(--hivis-text); margin-bottom: 4px; }
.card { background: var(--panel); border: 1px solid var(--rule); border-radius: var(--r); padding: 20px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.row > label { flex: 1 1 160px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* gate board (สถิติวันนี้) */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--panel); border: 1px solid var(--rule);
  border-radius: var(--r); margin-bottom: 16px; overflow: hidden; }
.stat { padding: 16px 20px; border-left: 1px solid var(--rule); }
.stat:first-child { border-left: 0; }
.stat span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.stat b { display: block; font-family: var(--f-display); font-size: 34px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.stat.alert b { color: var(--hivis-text); }
@media (max-width: 720px) { .stats { grid-template-columns: 1fr 1fr; } .stat:nth-child(3) { border-left: 0; } .stat:nth-child(n+3) { border-top: 1px solid var(--rule); } }

/* tables */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--rule); vertical-align: middle; }
th { color: var(--ink-2); font-weight: 600; font-size: 13px; position: sticky; top: 0; background: var(--panel); border-bottom: 2px solid var(--ink); }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.code { font-family: var(--f-mono); font-size: 14px; white-space: nowrap; }
td.time { font-family: var(--f-mono); font-size: 14px; white-space: nowrap; }
tr.inactive td { opacity: .45; }
.avatar { width: 42px; height: 42px; border-radius: var(--r-sm); object-fit: cover; background: var(--panel-2); display: block; border: 1px solid var(--rule); }
.tag { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 4px; font-size: 13px; font-weight: 600; white-space: nowrap;
  background: var(--panel-2); color: var(--ink-2); }
.tag::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.tag.in { color: var(--in); } .tag.out { color: var(--out); }
.tag.ok { color: var(--safe); } .tag.warn { color: var(--caution-text); } .tag.bad { color: var(--stop); }
.empty { text-align: center; color: var(--ink-2); padding: 36px 16px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(400px, 100%); display: flex; flex-direction: column; gap: 0; padding: 0; overflow: hidden; }
.login-card .tape { height: 10px; }
.login-card .inner { padding: 24px 24px 20px; display: flex; flex-direction: column; gap: 16px; }

/* modal */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(15,18,20,.6); display: grid; place-items: center; padding: 16px; }
.modal > .card { width: min(900px, 100%); max-height: calc(100vh - 32px); overflow: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-head h2 { margin: 0; }

/* enroll camera */
.enroll { display: grid; grid-template-columns: 1.3fr 1fr; gap: 20px; }
@media (max-width: 720px) { .enroll { grid-template-columns: 1fr; } }
.cam { position: relative; aspect-ratio: 4 / 3; background: #101315; border-radius: var(--r); overflow: hidden; }
.cam video, .cam canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cam video.mirror { transform: scaleX(-1); }
.cam .hint { position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 14px; color: #fff; font-size: 14px; font-weight: 500;
  background: linear-gradient(transparent, rgba(0,0,0,.75)); }
.shots { display: flex; gap: 8px; flex-wrap: wrap; min-height: 64px; align-items: center; }
.shots img { width: 64px; height: 64px; border-radius: var(--r-sm); object-fit: cover; border: 2px solid var(--safe); cursor: pointer; }
.shots img:hover { border-color: var(--stop); opacity: .8; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 99; background: var(--ink); color: var(--concrete);
  padding: 12px 20px; border-radius: var(--r-sm); font-size: 15px; font-weight: 500; border-left: 6px solid var(--hivis);
  box-shadow: 0 10px 30px rgba(0,0,0,.25); max-width: calc(100% - 32px); }

@media (max-width: 640px) {
  main { padding: 18px 16px 40px; }
  .topbar-in { padding: 8px 16px; gap: 10px; }
  nav { order: 3; width: 100%; overflow-x: auto; flex-wrap: nowrap; }
  nav a { min-height: 44px; padding: 0 10px; white-space: nowrap; }
  .hide-sm { display: none; }
  .btn { min-height: 44px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }
#syncBox p { margin: 0; }

/* ───── พื้นที่สแกน (แผนที่) ───── */
.sites-grid { display: grid; grid-template-columns: 1fr 400px; gap: 16px; align-items: start; }
@media (max-width: 960px) { .sites-grid { grid-template-columns: 1fr; } }
.map-card { padding: 0; overflow: hidden; position: sticky; top: 84px; }
#map { height: min(72vh, 640px); min-height: 360px; background: var(--panel-2); }
@media (max-width: 960px) { .map-card { position: static; } #map { height: 52vh; } }
.leaflet-container { font-family: var(--f-body); }
.site-pin { width: 22px; height: 22px; border-radius: 50%; background: var(--hivis); border: 4px solid #fff; box-shadow: 0 0 0 2px var(--ink), 0 4px 10px rgba(0,0,0,.35); }
.site-pin.off { background: var(--silver); }
tr.sel td { background: color-mix(in srgb, var(--hivis) 10%, transparent); }

/* ───── สลับโหมดสี (สว่าง / มืด / อัตโนมัติ) ───── */
.theme-seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 999px; background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.22); }
.theme-seg button { font: inherit; font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 0 12px;
  border: 0; border-radius: 999px; background: transparent; color: rgba(255,255,255,.8); cursor: pointer; }
.theme-seg button svg { width: 17px; height: 17px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.theme-seg button:hover { color: #fff; }
.theme-seg button[aria-pressed="true"] { background: #fff; color: #3b0764; }
.theme-seg.on-light { background: var(--panel-2); border-color: var(--rule); }
.theme-seg.on-light button { color: var(--ink-2); }
.theme-seg.on-light button:hover { color: var(--ink); }
.theme-seg.on-light button[aria-pressed="true"] { background: var(--hivis); color: var(--hivis-ink); }
@media (max-width: 900px) { .theme-seg button span { display: none; } .theme-seg button { padding: 0 9px; } }
