:root {
  --bg: #0f141b;
  --card: #1a2230;
  --card-2: #222d42;
  --line: #2e3a52;
  --text: #e8edf5;
  --muted: #93a0b8;
  --accent: #e0a92e;
  --green: #34c77b;
  --red: #e5484d;
  --blue: #4f9cf0;
  --grey: #8a94a6;
  --radius: 12px;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 12px 16px;
  background: #131a26; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 50;
}
.brand { font-weight: 700; font-size: 1.05rem; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.outlet-badge {
  background: var(--card-2); border: 1px solid var(--accent); color: var(--accent);
  border-radius: 999px; padding: 4px 12px; font-size: 0.85rem; font-weight: 600;
  white-space: nowrap;
}
.user-chip {
  background: var(--card-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 4px 12px; font-size: 0.85rem; color: var(--muted);
  max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: 4px; overflow-x: auto;
  background: #131a26; border-bottom: 1px solid var(--line);
  padding: 0 12px; position: sticky; top: 57px; z-index: 40;
}
.tabs a {
  color: var(--muted); text-decoration: none; font-weight: 600;
  padding: 12px 16px; white-space: nowrap; border-bottom: 3px solid transparent;
  min-height: 48px; display: inline-flex; align-items: center;
}
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- layout ---------- */
.container { max-width: 1100px; margin: 0 auto; padding: 16px 16px 96px; }
.page-title { margin: 4px 0 12px; font-size: 1.3rem; }
.grid { display: grid; gap: 12px; }
.grid.stat { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 720px) { .grid.stat { grid-template-columns: repeat(4, 1fr); } }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px;
}
.stat-card .num { font-size: 1.9rem; font-weight: 700; line-height: 1.2; }
.stat-card .lbl { color: var(--muted); font-size: 0.9rem; }
.stat-card.warn .num { color: var(--red); }
.stat-card.ok .num { color: var(--green); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; background: var(--card); font-size: 0.92rem; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
thead th { background: var(--card-2); color: var(--muted); font-weight: 600; position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- badges ---------- */
.badge {
  display: inline-block; border-radius: 999px; padding: 2px 10px;
  font-size: 0.8rem; font-weight: 600; white-space: nowrap;
}
.b-ok { background: rgba(52,199,123,.15); color: var(--green); }
.b-over { background: rgba(229,72,77,.15); color: var(--red); }
.b-na { background: rgba(138,148,166,.15); color: var(--grey); }
.b-info { background: rgba(79,156,240,.15); color: var(--blue); }
.b-warn { background: rgba(224,169,46,.15); color: var(--accent); }

/* ---------- forms & buttons ---------- */
.field { margin-bottom: 12px; }
.field label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 0.9rem; }
.input, select.input {
  width: 100%; min-height: 48px; font-size: 1rem; color: var(--text);
  background: var(--card-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 14px; font-family: inherit;
}
.input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 10px 22px; border-radius: 10px; border: none;
  font-size: 1rem; font-weight: 700; font-family: inherit; cursor: pointer;
}
.btn-primary { background: var(--accent); color: #1a1405; width: 100%; }
.btn-primary:disabled { opacity: .5; cursor: wait; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); min-height: 36px; padding: 4px 14px; font-size: 0.85rem; }
.form-error { color: var(--red); margin-top: 10px; min-height: 1.4em; }
.login-card { max-width: 420px; margin: 8vh auto 0; }
.demo-hint { margin-top: 14px; font-size: 0.82rem; color: var(--muted); }
.demo-hint code { color: var(--text); }

/* ---------- misc ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar select.input { width: auto; min-width: 180px; }
.muted { color: var(--muted); }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.skeleton { animation: pulse 1.2s infinite; color: var(--muted); }
@keyframes pulse { 50% { opacity: .4; } }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--card-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 12px 20px; z-index: 100; max-width: 90vw;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
}
.toast.error { border-color: var(--red); }
.toast.ok { border-color: var(--green); }

/* ---------- count sheet ---------- */
.cat {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); margin-bottom: 12px;
}
.cat summary {
  padding: 14px 16px; font-weight: 700; cursor: pointer; min-height: 52px;
  list-style: none; display: flex; gap: 8px; align-items: center;
}
.cat summary::-webkit-details-marker { display: none; }
.cat summary::before { content: "▸"; color: var(--accent); }
.cat[open] summary::before { content: "▾"; }
.cat-rows { padding: 0 12px 12px; display: grid; gap: 10px; }
.count-row {
  background: var(--card-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 12px;
}
.cr-code { font-weight: 700; font-size: 0.9rem; display: flex; gap: 6px; align-items: center; }
.cr-name { color: var(--muted); font-size: 0.9rem; margin: 2px 0 10px; }
.cr-controls { display: grid; gap: 8px; }
.stepper { display: flex; gap: 8px; align-items: stretch; }
.btn.step {
  flex: 1; min-height: 56px; font-size: 1.4rem; padding: 6px;
  background: var(--card); border: 1px solid var(--accent); color: var(--accent);
}
.btn.step:disabled { opacity: .35; }
.step-val {
  flex: 1; display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem; font-weight: 700; background: #0c1119;
  border-radius: 10px; min-height: 56px; min-width: 64px;
}
.tenth-row { display: flex; gap: 10px; align-items: center; }
.tenth-row input[type="range"] { flex: 1; min-height: 48px; accent-color: var(--accent); }
.tenth-val { min-width: 44px; text-align: center; font-weight: 700; }
.ltr-input { text-align: center; font-size: 1.3rem; font-weight: 700; flex: 1.4; min-width: 0; }
.serve-prev { display: flex; flex-wrap: wrap; gap: 6px; }
.cr-phys { margin-top: 8px; text-align: right; color: var(--muted); font-size: 0.9rem; }
.cr-phys b { font-size: 1.2rem; color: var(--accent); }
#countSearch { position: sticky; top: 112px; z-index: 30; }

/* ---------- recon ---------- */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip.active { border-color: var(--accent); color: var(--accent); }
select.need, input.need { border-color: var(--red) !important; }
tr.over { background: rgba(229,72,77,.06); }
.appr-row { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 0.9rem; }
.appr-row:last-child { border-bottom: none; }
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.65); z-index: 200;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  max-width: 680px; width: 100%; max-height: 85vh;
  display: flex; flex-direction: column;
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.modal-body { padding: 14px 16px; overflow: auto; }
table a { color: var(--accent); }

/* ---------- variance bars ---------- */
.bar-row { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; font-size: 0.9rem; }
.bar-lbl { min-width: 150px; }
.bar-track { flex: 1; background: #0c1119; border-radius: 6px; height: 18px; overflow: hidden; }
.bar-fill { background: var(--accent); height: 100%; border-radius: 6px; }
.bar-fill.blue { background: var(--blue); }
.bar-val { min-width: 90px; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- print (ใช้ปุ่ม "พิมพ์" บน topbar -> PDF/กระดาษ) ---------- */
@media print {
  .topbar-right, .tabs, #toast, .btn, .toolbar select, .toolbar input { display: none !important; }
  body { background: #fff; color: #000; font-size: 12px; }
  .container { max-width: none; padding: 0 0 16px; }
  .card, .table-wrap, table { background: #fff !important; color: #000; border-color: #999; }
  thead th { background: #eee !important; color: #000; }
  th, td { border-bottom: 1px solid #999; }
  .badge { border: 1px solid #999; }
  .muted, .cr-name, .cr-phys { color: #333; }
  .cr-phys b, .stat-card .num { color: #000; }
  .cat, .card { break-inside: avoid; }
  details.cat { display: block; }
}
