:root {
  color-scheme: light;
  --bg: #fcfcfb;
  --surface: #ffffff;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6d6c67;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --hover: rgba(11, 11, 11, 0.04);
  --accent: #2a78d6;
  --banner-bg: #fff6df;
  --banner-text: #5c4300;

  /* status (fixed, never themed) — always paired with a text label */
  --st-crit: #d03b3b;
  --st-warn: #fab219;
  --st-ok: #0ca30c;
  --st-off: #8a8983;
  --st-temp: #b8b7b0;

  /* chart series (validated: light + dark, all pairs) */
  --s-in: #2a78d6;
  --s-out: #e87ba4;
  --s-river: #008300;

  --font: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", "Leelawadee UI", Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #9b9a92;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --hover: rgba(255, 255, 255, 0.05);
    --accent: #3987e5;
    --banner-bg: #2b2413;
    --banner-text: #f3dfa6;
    --s-in: #3987e5;
    --s-out: #d55181;
    --s-river: #008300;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --text: #ffffff; --text-2: #c3c2b7; --muted: #9b9a92;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10); --hover: rgba(255,255,255,0.05);
  --accent: #3987e5; --banner-bg: #2b2413; --banner-text: #f3dfa6;
  --s-in: #3987e5; --s-out: #d55181; --s-river: #008300;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.125rem; font-weight: 600; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.8125rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px; }

.disclaimer {
  background: var(--banner-bg);
  color: var(--banner-text);
  font-size: 0.8125rem;
  padding: 8px 16px;
  text-align: center;
}
.disclaimer a { color: inherit; font-weight: 500; }

.mockbanner { background: var(--st-crit); color: #fff; font-weight: 600; font-size: 0.875rem; padding: 8px 16px; text-align: center; }

.top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding-top: 20px; padding-bottom: 12px; }
.top p { margin: 2px 0 0; font-size: 0.875rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  font: inherit; font-size: 0.8125rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; text-decoration: none; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: var(--hover); }
.btn[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.btn[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }

/* status tiles double as filters */
.tiles { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 12px; }
.tile {
  font: inherit; text-align: left; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; cursor: pointer;
}
.tile:hover { background: var(--hover); }
.tile[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: -2px; }
.tile .n { font-size: 1.5rem; font-weight: 600; line-height: 1.2; font-variant-numeric: tabular-nums; }
.tile .l { font-size: 0.8125rem; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.tile.static { cursor: default; }
.tile.static:hover { background: var(--surface); }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.st-0 { background: var(--st-off); } .st-1 { background: var(--st-temp); }
.st-2 { background: var(--st-ok); } .st-3 { background: var(--st-warn); } .st-4 { background: var(--st-crit); }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8125rem; white-space: nowrap; color: var(--text); }

#map {
  height: min(62vh, 560px); min-height: 360px;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
}
.leaflet-container { font: inherit; background: var(--surface); }
.basemap { filter: grayscale(1) brightness(1.04) contrast(0.9); }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .basemap { filter: grayscale(1) invert(1) brightness(0.85) contrast(0.9); }
}
:root[data-theme="dark"] .basemap { filter: grayscale(1) invert(1) brightness(0.85) contrast(0.9); }
.leaflet-popup-content-wrapper { background: var(--surface); color: var(--text); border-radius: 10px; }
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { margin: 12px 14px; width: 320px !important; max-width: calc(100vw - 80px); }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-interactive.mk { stroke: var(--surface); stroke-width: 2; stroke-opacity: 1; fill-opacity: 1; }
.mk-4 { fill: var(--st-crit); } .mk-3 { fill: var(--st-warn); } .mk-2 { fill: var(--st-ok); }
.mk-1 { fill: var(--st-temp); } .mk-0 { fill: var(--st-off); }
.leaflet-tooltip { font: inherit; font-size: 0.75rem; }
.map-legend {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; font-size: 0.75rem; line-height: 1.7;
}
.map-legend div { display: flex; align-items: center; gap: 6px; }

.pop h3 { font-size: 0.9375rem; font-weight: 600; margin: 0 0 2px; line-height: 1.35; }
.pop .meta { font-size: 0.75rem; color: var(--muted); margin-bottom: 8px; }
.levels { display: grid; grid-template-columns: repeat(3, auto); gap: 2px 14px; font-size: 0.8125rem; margin: 8px 0; }
.levels .k { color: var(--text-2); }
.levels .v { font-variant-numeric: tabular-nums; font-weight: 500; }
.links { display: flex; gap: 12px; font-size: 0.8125rem; margin-top: 8px; flex-wrap: wrap; }

/* chart */
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; margin: 6px 0 4px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; font-size: 0.75rem; border: 0; background: transparent; color: var(--text-2); padding: 3px 10px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--hover); color: var(--text); font-weight: 600; }
.legend { display: flex; gap: 12px; font-size: 0.75rem; color: var(--text-2); flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 12px; height: 2px; border-radius: 1px; display: inline-block; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .empty { font-size: 0.8125rem; color: var(--muted); padding: 24px 0; text-align: center; }
.note { font-size: 0.75rem; color: var(--muted); }

.tip {
  position: fixed; z-index: 2000; pointer-events: none;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12); border-radius: 8px;
  padding: 6px 9px; font-size: 0.75rem; line-height: 1.5; white-space: nowrap;
}
.tip b { font-weight: 600; }
.tip .row { display: flex; align-items: center; gap: 6px; }
.tip .row i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.tip .row .val { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }

/* districts */
.districts { margin-top: 28px; }
.districts .section-head { justify-content: space-between; flex-wrap: wrap; margin-bottom: 4px; }
.districts > p { margin: 0 0 12px; max-width: 760px; }
#distPick {
  font: inherit; font-size: 0.875rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; max-width: 260px;
}
.dgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; margin-bottom: 10px; }
.dcard {
  font: inherit; color: var(--text); text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: end;
}
.dcard:hover { background: var(--hover); }
.dcard[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: -2px; }
.dcard .dn { grid-column: 1 / -1; font-weight: 500; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dcard .dv { font-size: 1.25rem; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.dcard .dv small { font-size: 0.75rem; font-weight: 400; color: var(--muted); }
.dcard .dvw { display: flex; flex-direction: column; gap: 1px; }
.dcard .dtr { font-size: 0.75rem; color: var(--text-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.dcard .dtr small, .dlegend small { color: var(--muted); }
.dlegend { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 0.75rem; color: var(--text-2); margin: 0 0 12px; }
.dlegend > span { display: inline-flex; align-items: center; gap: 6px; }
.dlegend .dot { width: 8px; height: 8px; }
.dlegend b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.dcard .dc { grid-column: 1 / -1; display: flex; gap: 10px; font-size: 0.75rem; color: var(--text-2); }
.dcard .dc span { display: inline-flex; align-items: center; gap: 4px; }
.dcard .dc .dot { width: 8px; height: 8px; }
.dpanel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; }
.dpanel h3 { margin: 0; font-size: 1rem; font-weight: 600; }
.dpanel .dsum { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.8125rem; color: var(--text-2); margin: 4px 0 6px; }
.dpanel .dsum b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.dpanel .dsum b.above { color: var(--st-crit); }

/* table */
.table-section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.controls { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.controls input[type="search"], .controls select {
  font: inherit; font-size: 0.875rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
}
.controls input[type="search"] { flex: 1 1 240px; min-width: 0; }
.controls select { max-width: 220px; }

#tblMore { margin-top: 10px; }
.table-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: middle; }
th { font-weight: 500; color: var(--text-2); font-size: 0.8125rem; white-space: nowrap; position: sticky; top: 0; background: var(--surface); }
th[data-sort] { cursor: pointer; user-select: none; }
th[data-sort]:hover { color: var(--text); }
th[aria-sort="ascending"]::after { content: " ↑"; }
th[aria-sort="descending"]::after { content: " ↓"; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr.row { cursor: pointer; }
tbody tr.row:hover { background: var(--hover); }
tbody tr.row.open { background: var(--hover); }
tbody tr:last-child td { border-bottom: 0; }
.c-spark { width: 132px; }
.c-time { white-space: nowrap; color: var(--text-2); }
.name { font-weight: 500; line-height: 1.35; }
.sub { font-size: 0.75rem; color: var(--muted); }
.above { color: var(--st-crit); font-weight: 600; }
.detail td { padding: 4px 16px 16px; background: var(--hover); }
.detail .inner { max-width: 900px; }
.spark { display: block; }

.about { margin: 40px 0 24px; max-width: 760px; }
.about { scroll-margin-top: 16px; }
.about p, .about li { color: var(--text-2); }
.about p { margin: 8px 0; }
.about ul { margin: 8px 0; padding-left: 1.25em; }
.about li { margin: 4px 0; }
footer { padding-top: 16px; padding-bottom: 32px; border-top: 1px solid var(--grid); }

@media (max-width: 760px) {
  h1 { font-size: 1.25rem; }
  .top { flex-wrap: wrap; }
  .btn-label { display: none; }
  .tiles { grid-template-columns: repeat(3, 1fr); }
  .tile .n { font-size: 1.25rem; }
  .c-time, .c-d1h { display: none; }
  #tbl .bl { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table { font-size: 0.8125rem; }
  .c-spark { width: 84px; }
  .dgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dcard { grid-template-columns: 1fr; }
  .c-spark svg { width: 80px; }
  th, td { padding: 8px 6px; }
  .controls select { max-width: none; flex: 1 1 140px; }
}
@media (max-width: 420px) {
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .tile[data-tile="rising"] { grid-column: span 2; }
}
