:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --text: #16202a;
  --muted: #5b6875;
  --line: #e3e8ed;
  --accent: #0f6fb8;
  --accent-soft: #e6f1fa;
  --sev4: #9b1c1c;
  --sev3: #e0362c;
  --sev2: #f08c00;
  --sev1: #2f9e44;
  --off: #9aa5b1;
  --report: #6741d9;
  --shadow: 0 6px 24px rgba(16, 32, 48, .14);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1319; --panel: #151c24; --text: #e6ebf0; --muted: #9aa7b4; --line: #26313d;
    --accent: #4da3e8; --accent-soft: #16304a; --sev4: #ff6b6b; --sev3: #ff8a5b; --sev2: #ffc34d;
    --sev1: #51cf66; --off: #6b7784; --report: #9775fa; --shadow: 0 6px 24px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1319; --panel: #151c24; --text: #e6ebf0; --muted: #9aa7b4; --line: #26313d;
  --accent: #4da3e8; --accent-soft: #16304a; --sev4: #ff6b6b; --sev3: #ff8a5b; --sev2: #ffc34d;
  --sev1: #51cf66; --off: #6b7784; --report: #9775fa; --shadow: 0 6px 24px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 "IBM Plex Sans Thai", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; cursor: pointer; }

#app { position: fixed; inset: 0; display: flex; }
#map { flex: 1; height: 100%; background: var(--bg); }

/* ---------- side panel ---------- */
#panel {
  width: 380px; max-width: 100%; height: 100%;
  display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--line);
  z-index: 500;
}
.grab { display: none; }
.brand { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; padding: 16px 16px 8px; }
.brand h1 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -.01em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.sub .warn { color: var(--sev3); }
.icon-btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  width: 36px; height: 36px; display: grid; place-items: center; flex: none;
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.search { display: flex; margin: 4px 16px 10px; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--bg); }
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; color: var(--text); font: inherit; padding: 10px 12px; outline: none; }
.search button { border: 0; background: transparent; padding: 0 12px; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 0 16px 10px; }
.stat {
  border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; text-align: left; background: var(--panel);
}
.stat b { display: block; font-size: 20px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11.5px; color: var(--muted); }
.stat.on { border-color: var(--accent); background: var(--accent-soft); }
.stat .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 1px; }

.toggles { display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 0 16px 8px; font-size: 13.5px; color: var(--muted); }
.toggles label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.toggles input { accent-color: var(--accent); }

.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 12px; }
.tab { flex: 1; border: 0; background: none; padding: 9px 4px; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 500; }
.tab.on { color: var(--text); border-bottom-color: var(--accent); }
.tab span { font-weight: 400; color: var(--muted); font-size: 13px; }

.list { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; }
.item { display: flex; gap: 10px; align-items: center; padding: 9px 16px; cursor: pointer; border-bottom: 1px solid var(--line); }
.item:hover, .item:focus-visible { background: var(--accent-soft); outline: none; }
.badge {
  flex: none; min-width: 52px; text-align: center; border-radius: 8px; padding: 4px 6px;
  font-weight: 700; font-variant-numeric: tabular-nums; color: #fff; font-size: 14px;
}
.badge small { font-weight: 500; font-size: 10.5px; opacity: .9; }
.item .t { flex: 1; min-width: 0; }
.item .n { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .m { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { padding: 24px 16px; color: var(--muted); text-align: center; }
.more { display: block; width: calc(100% - 32px); margin: 10px 16px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: none; }

.foot { padding: 10px 16px 14px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--line); }

/* ---------- map overlays ---------- */
.map-ctl { position: fixed; right: 12px; top: 84px; z-index: 600; display: flex; flex-direction: column; gap: 8px; }
.fab {
  width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); box-shadow: var(--shadow); display: grid; place-items: center;
}
.fab:hover { color: var(--accent); }
.legend {
  position: fixed; right: 12px; bottom: 24px; z-index: 600; background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; font-size: 12.5px; box-shadow: var(--shadow);
}
.legend div { display: flex; align-items: center; gap: 7px; line-height: 1.7; }
.legend i { width: 11px; height: 11px; border-radius: 50%; display: inline-block; }
.legend i.sq { border-radius: 3px; background: var(--report); transform: rotate(45deg) scale(.85); }

.toast {
  position: fixed; left: 50%; top: 14px; transform: translate(-50%, -80px); z-index: 900;
  background: var(--text); color: var(--panel); padding: 8px 14px; border-radius: 10px; font-size: 14px;
  transition: transform .25s; pointer-events: none; max-width: calc(100% - 32px);
}
.toast.show { transform: translate(-50%, 0); }

/* markers */
.pin {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 11.5px; border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.35); font-variant-numeric: tabular-nums;
}
.pin.small { width: 14px; height: 14px; border-width: 2px; font-size: 0; }
.pin.pulse { animation: pulse 1.8s ease-out infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(224,54,44,.55); } 100% { box-shadow: 0 0 0 14px rgba(224,54,44,0); } }
.rpt { width: 14px; height: 14px; background: var(--report); border: 2px solid #fff; border-radius: 3px; transform: rotate(45deg); box-shadow: 0 1px 3px rgba(0,0,0,.35); }
.rpt.deep { background: var(--sev3); }
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large { background: rgba(103,65,217,.25) !important; }
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div { background: rgba(103,65,217,.85) !important; color: #fff; font-weight: 600; font-family: inherit; }
.me { width: 16px; height: 16px; border-radius: 50%; background: var(--accent); border: 3px solid #fff; box-shadow: 0 0 0 6px rgba(15,111,184,.25); }

/* popups */
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel); color: var(--text); }
.leaflet-popup-content { margin: 12px 14px; font: 14px/1.45 "IBM Plex Sans Thai", system-ui, sans-serif; min-width: 230px; }
.pp h3 { margin: 0 0 2px; font-size: 15.5px; }
.pp .road { color: var(--muted); font-size: 12.5px; }
.pp .big { display: flex; align-items: baseline; gap: 6px; margin: 8px 0 4px; }
.pp .big b { font-size: 28px; line-height: 1; font-variant-numeric: tabular-nums; }
.pp .chip { display: inline-block; padding: 1px 8px; border-radius: 99px; color: #fff; font-size: 12px; font-weight: 600; }
.pp dl { display: grid; grid-template-columns: auto 1fr; gap: 1px 10px; margin: 6px 0; font-size: 12.5px; }
.pp dt { color: var(--muted); }
.pp dd { margin: 0; }
.pp .spark { margin: 6px 0 2px; }
.pp .spark svg { display: block; }
.pp .spark small { color: var(--muted); font-size: 11px; }
.pp .links { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; margin-top: 6px; }
.pp img { width: 100%; max-height: 160px; object-fit: cover; border-radius: 8px; margin: 6px 0; background: var(--line); }
.pp .txt { font-size: 13px; max-height: 110px; overflow: auto; white-space: pre-line; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-control-attribution { font-size: 10px; }
.leaflet-bar a { background: var(--panel); color: var(--text); border-color: var(--line); }

/* ---------- mobile: panel becomes a bottom sheet ---------- */
@media (max-width: 760px) {
  #app { display: block; }
  #map { position: fixed; inset: 0; }
  #panel {
    position: fixed; left: 0; right: 0; bottom: 0; width: 100%;
    height: 46vh; border-right: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
    transition: height .25s ease;
  }
  #panel.min { height: 168px; }
  #panel.max { height: 88vh; }
  .grab { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; cursor: grab; flex: none; }
  .brand { padding: 6px 16px 6px; }
  .brand h1 { font-size: 18px; }
  .stats { gap: 4px; }
  .stat b { font-size: 17px; }
  .legend { top: 12px; bottom: auto; left: 12px; right: auto; font-size: 11.5px; padding: 6px 10px; cursor: pointer; }
  .leaflet-control-zoom { display: none; }
  .foot { display: none; }
  #panel.max .foot { display: block; }
  .legend.collapsed div:not(:first-child) { display: none; }
  .map-ctl { top: 12px; bottom: auto; }
}
