:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --navy: #12355b;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --good-text: #006300;
  --critical-text: #b42323;
  --walking: #3b82f6;
  --waiting: #f59e0b;
  --serving: #16a34a;
  --series-scenario: #2a78d6;
  --series-baseline: #898781;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", Thonburi, Tahoma, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --line: #2c2c2a;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --navy: #9cc3f0;
    --good-text: #0ca30c;
    --critical-text: #f08080;
    --series-scenario: #3987e5;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h2 { font-size: 15px; }
h3 { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 16px; font-weight: 700; }
.topbar .spacer { flex: 1; }
.topbar nav { display: flex; gap: 12px; font-size: 13px; }
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--border);
}
.badge.demo { background: #fff4d6; color: #7a5200; border-color: #f5d27a; }
.badge.live { background: #e3f4e3; color: var(--good-text); border-color: #a9dca9; }
.badge.sim { background: #e3eefb; color: #1c5cab; border-color: #9ec5f4; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .badge.demo { background: #3a2e10; color: #f5d27a; border-color: #5c4a1a; }
  :root:not([data-theme="light"]) .badge.live { background: #12301a; color: #7ad67a; border-color: #1f4d2a; }
  :root:not([data-theme="light"]) .badge.sim { background: #132a45; color: #9ec5f4; border-color: #1c3f66; }
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--good); }
.dot.off { background: var(--critical); }

/* ---- buttons & forms ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 600; font-size: 13px;
  min-height: 36px; text-decoration: none; color: var(--ink);
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--critical-text); }
.btn.small { min-height: 28px; padding: 3px 10px; font-size: 12px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
input[type="text"], input[type="number"], input[type="password"], select {
  padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); min-height: 36px; width: 100%;
}
input:focus-visible, select:focus-visible, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: grid; gap: 4px; font-size: 12px; color: var(--ink-2); }
input.line-id { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; min-width: 22ch; }

/* ---- cards, tables ---- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.stack { display: grid; gap: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 12px; padding: 6px 6px; border-bottom: 1px solid var(--line); }
td { padding: 6px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
td.r, th.r { text-align: right; }

.level { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; white-space: nowrap; }
.level::before { content: ""; width: 8px; height: 8px; border-radius: 2px; background: var(--good); }
.level.warn::before { background: var(--warning); }
.level.alert::before { background: var(--critical); }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.kpi { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.kpi .label { font-size: 12px; color: var(--ink-2); }
.kpi .value { font-size: 22px; font-weight: 700; line-height: 1.2; }
.kpi .unit { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 2px; }

.alert-item {
  display: flex; gap: 8px; align-items: flex-start; padding: 8px 10px; border-radius: 8px;
  background: var(--surface-2); font-size: 13px; border-left: 3px solid var(--warning);
}
.alert-item.alert { border-left-color: var(--critical); }
.alert-item .icon { font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 10px;
  display: flex; gap: 12px; align-items: center; z-index: 50; max-width: calc(100vw - 32px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); font-size: 14px;
}
.toast .btn { background: transparent; color: inherit; border-color: rgba(255, 255, 255, 0.4); }
.toast.error { background: var(--critical); color: #fff; }

/* ---- twin page ---- */
.twin-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 400px;
  height: calc(100vh - 53px); min-height: 480px;
}
.viewport { position: relative; overflow: hidden; background: #dde3ea; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .viewport { background: #15191e; } }
.viewport canvas { display: block; }
.overlay { position: absolute; display: flex; gap: 6px; flex-wrap: wrap; }
.overlay.toolbar { top: 10px; left: 10px; right: 10px; justify-content: space-between; align-items: flex-start; pointer-events: none; }
.overlay.bottom-left { bottom: 10px; left: 10px; }
.tool-group { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; pointer-events: auto; }
.overlay .btn, .overlay select { background: rgba(255, 255, 255, 0.92); color: #0b0b0b; border-color: rgba(0, 0, 0, 0.12); }
.overlay .btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.overlay select { width: auto; min-height: 28px; padding: 2px 8px; font-size: 12px; font-weight: 600; }
.segmented { display: inline-flex; }
.segmented .btn { border-radius: 0; }
.segmented .btn:first-child { border-radius: 8px 0 0 8px; }
.segmented .btn:last-child { border-radius: 0 8px 8px 0; margin-left: -1px; }
.sim-controls {
  display: flex; gap: 6px; align-items: center; background: rgba(18, 53, 91, 0.92); color: #fff;
  padding: 4px 6px 4px 6px; border-radius: 8px;
}
.sim-controls .btn { min-width: 32px; }
.sim-clock { font-size: 12px; font-weight: 600; padding: 0 4px; white-space: nowrap; }
.legend {
  display: flex; gap: 12px; flex-wrap: wrap; align-items: center;
  background: rgba(255, 255, 255, 0.92); color: #0b0b0b; padding: 5px 10px; border-radius: 8px; font-size: 12px;
}
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.tooltip {
  position: absolute; pointer-events: none; background: rgba(11, 11, 11, 0.88); color: #fff;
  padding: 6px 9px; border-radius: 6px; font-size: 12px; white-space: nowrap; z-index: 5; transform: translate(12px, -50%);
}

.panel { background: var(--surface); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 8px; overflow-x: auto; }
.tabs button {
  border: none; background: none; padding: 10px 10px; cursor: pointer; font-weight: 600; font-size: 13px;
  color: var(--ink-2); border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tab-body { padding: 14px; overflow-y: auto; flex: 1; display: grid; gap: 14px; align-content: start; }
.source-line { font-size: 12px; color: var(--muted); padding: 8px 14px; border-top: 1px solid var(--line); }

.whatif-grid { display: grid; grid-template-columns: 1fr 72px 80px; gap: 6px 8px; align-items: center; font-size: 13px; }
.whatif-grid .head { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.insight { margin: 0; padding-left: 18px; display: grid; gap: 4px; }
.insight li:first-child { color: var(--ink-2); }

.chart-wrap { position: relative; }
.chart-wrap svg { display: block; max-width: 100%; height: auto; overflow: visible; }
.chart-legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); flex-wrap: wrap; }
.chart-legend .swatch { display: inline-block; width: 18px; height: 0; border-top: 2px solid; vertical-align: middle; margin-right: 5px; }
.chart-tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow); border-radius: 6px; padding: 6px 8px; font-size: 12px; white-space: nowrap; z-index: 3;
}

.check-pass { color: var(--good-text); font-weight: 600; white-space: nowrap; }
.check-fail { color: var(--critical-text); font-weight: 600; white-space: nowrap; }

@media (max-width: 900px) {
  .twin-layout { grid-template-columns: 1fr; grid-template-rows: 58vh auto; height: auto; }
  .panel { border-left: none; border-top: 1px solid var(--line); }
}

/* ---- desk & admin pages ---- */
.page { max-width: 760px; margin: 0 auto; padding: 16px; display: grid; gap: 14px; }
.page.wide { max-width: 1100px; }
.big-input { font-size: 22px !important; min-height: 52px !important; text-transform: uppercase; letter-spacing: 0.5px; }
.big-btn { min-height: 52px; font-size: 17px; flex: 1; }
.list-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: none; }
.list-row .id { font-weight: 700; font-size: 16px; }
.list-row .meta { flex: 1; color: var(--ink-2); font-size: 13px; }
.station-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.station-picker .btn { min-height: 64px; font-size: 16px; }
.qr-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.qr-card { text-align: center; display: grid; gap: 6px; justify-items: center; }
.qr-card svg { width: 180px; height: 180px; }
.qr-card .name { font-size: 18px; font-weight: 700; }

@media print {
  body { background: #fff; }
  .no-print, .topbar { display: none !important; }
  .card { box-shadow: none; border: none; }
  .qr-grid { grid-template-columns: repeat(2, 1fr); }
  .qr-card { break-inside: avoid; border: 1px dashed #999; padding: 16px; }
}
