:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #5d6b78;
  --line: #e2e7ec;
  --accent: #1565c0;
  --accent-soft: #e8f0fb;
  --l0: #2f9e61;
  --l1: #d9a400;
  --l2: #ec7418;
  --l3: #d62839;
  --water: #1e88e5;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(16, 32, 48, .10);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "IBM Plex Sans Thai", system-ui, sans-serif;
  color: var(--ink); background: var(--bg); font-size: 14px; line-height: 1.5;
  display: flex; flex-direction: column;
}
button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: #0f2b46; color: #fff; z-index: 1001; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand svg { color: #64b5f6; }
.brand small { font-weight: 400; font-size: 12px; opacity: .75; }
.search { position: relative; flex: 1; min-width: 240px; max-width: 620px; }
.search input {
  width: 100%; padding: 9px 14px; border-radius: 8px; border: 0; font: inherit; font-size: 14px;
  background: #fff; color: var(--ink);
}
.suggest {
  position: absolute; top: 42px; left: 0; right: 0; margin: 0; padding: 4px; list-style: none;
  background: #fff; color: var(--ink); border-radius: 8px; box-shadow: var(--shadow); max-height: 60vh; overflow: auto;
}
.suggest li { padding: 8px 10px; border-radius: 6px; cursor: pointer; display: flex; gap: 8px; align-items: baseline; }
.suggest li:hover, .suggest li.active { background: var(--accent-soft); }
.suggest .kind { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: #eef1f4; color: var(--muted); white-space: nowrap; }
.suggest .sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font-size: 12px; opacity: .8; margin-left: auto; }
.navlink { color: #fff; text-decoration: none; font-size: 13px; padding: 6px 12px; border: 1px solid rgba(255,255,255,.35); border-radius: 8px; white-space: nowrap; }
.navlink:hover { background: rgba(255,255,255,.12); }
.cams { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
.cam { display: block; text-decoration: none; color: inherit; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; font-size: 12px; }
.cam:hover { border-color: var(--accent); }
.cam img { width: 100%; height: 70px; object-fit: cover; display: block; background: #e6eaee; }
.btn-sm { padding: 6px 12px; border: 0; border-radius: 6px; background: var(--accent); color: #fff; font-size: 13px; }
.btn-sm.ghost { background: #eef2f6; color: var(--ink); }
.cam .noimg { height: 70px; display: grid; place-items: center; background: #eef2f6; color: #9aa8b5; }
.cam div { padding: 4px 6px; line-height: 1.35; }
.cam small { color: var(--muted); }
.cam-pin { width: 14px; height: 14px; border-radius: 3px; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.panel {
  width: 420px; max-width: 100%; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0; z-index: 500;
}
.mapwrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: #dfe6ec; }

.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab { flex: 1; padding: 10px 6px; border: 0; background: none; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 500; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabpane { display: none; overflow: auto; flex: 1; padding: 12px 14px 90px; }
.tabpane.active { display: block; }
.hint { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.loading, .empty { padding: 24px 8px; text-align: center; color: var(--muted); }

/* ---------- overview ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.kpi { border-radius: 8px; padding: 8px 6px; text-align: center; background: #f6f8fa; border-top: 3px solid; }
.kpi b { display: block; font-size: 20px; line-height: 1.2; }
.kpi span { font-size: 11.5px; color: var(--muted); }
.list .item {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 6px; border-bottom: 1px solid var(--line); cursor: pointer; border-radius: 6px;
}
.list .item:hover { background: #f6f8fa; }
.item .name { font-weight: 500; }
.item .sub { font-size: 12px; color: var(--muted); }
.spark { display: flex; gap: 2px; }
.spark i { width: 7px; height: 16px; border-radius: 2px; background: #e6eaee; }

/* ---------- levels ---------- */
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; }
.l0 { background: var(--l0); } .l1 { background: var(--l1); } .l2 { background: var(--l2); } .l3 { background: var(--l3); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; color: #fff; font-size: 12px; font-weight: 600; white-space: nowrap; }
.legend-rows { display: flex; gap: 14px; flex-wrap: wrap; }

/* ---------- analysis ---------- */
.target-head h2 { margin: 0; font-size: 19px; }
.target-head .sub { color: var(--muted); font-size: 12.5px; }
.verdict { margin: 12px 0; border-radius: var(--radius); padding: 12px 14px; color: #fff; }
.verdict .lv { font-size: 22px; font-weight: 700; }
.verdict .adv { margin-top: 4px; }
.verdict .meta { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 8px; font-size: 12.5px; opacity: .95; }
.verdict .meta b { font-weight: 600; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; }
.card h3 { margin: 0 0 8px; font-size: 14.5px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card h3 small { font-weight: 400; color: var(--muted); font-size: 12px; }
.scenario { display: flex; align-items: center; gap: 10px; }
.scenario input { flex: 1; }
.scenario output { font-weight: 700; min-width: 56px; text-align: right; }
.route { padding: 8px 0; border-bottom: 1px dashed var(--line); cursor: pointer; }
.route:last-child { border-bottom: 0; }
.route:hover .rname { color: var(--accent); }
.route .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.route .rname { font-weight: 600; }
.route .path { font-size: 12px; color: var(--muted); margin-top: 2px; }
.route .facts { font-size: 12px; margin-top: 3px; display: flex; gap: 10px; flex-wrap: wrap; }
.route.dim { opacity: .55; }
.downstream li { margin: 2px 0; }
.chartbox { position: relative; height: 210px; }
.months { display: grid; grid-template-columns: repeat(12, 1fr); gap: 3px; margin-top: 6px; }
.months div { text-align: center; font-size: 10.5px; padding: 4px 0; border-radius: 4px; background: #f0f3f6; color: var(--muted); }
.months div.hit { background: #fde2d3; color: #9a3a0b; font-weight: 600; }
.months div.now { outline: 2px solid var(--accent); }
.events li { margin-bottom: 6px; }
.events .yr { font-weight: 700; margin-right: 6px; }
table.mini { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.mini td, table.mini th { padding: 3px 4px; border-bottom: 1px solid var(--line); text-align: left; }
table.mini th { color: var(--muted); font-weight: 500; }
.note { font-size: 12px; color: var(--muted); }
.about h3 { font-size: 14.5px; margin: 14px 0 6px; }
.about ol, .about ul { padding-left: 18px; margin: 0; }
.about li { margin-bottom: 5px; }

/* ---------- timeline ---------- */
.timeline {
  position: absolute; left: 12px; right: 12px; bottom: 14px; z-index: 800;
  background: rgba(255, 255, 255, .96); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 12px; padding: 8px 14px;
}
.play { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.tl-body { flex: 1; min-width: 0; }
.tl-label { font-size: 13px; }
#slider { width: 100%; margin: 2px 0 0; accent-color: var(--accent); }
.tl-ticks { position: relative; height: 14px; font-size: 10.5px; color: var(--muted); }
.tl-ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.tl-ticks span.today { color: var(--accent); font-weight: 700; }
.maplegend {
  position: absolute; right: 12px; bottom: 92px; z-index: 800; background: rgba(255,255,255,.95);
  padding: 8px 10px; border-radius: 8px; box-shadow: var(--shadow); font-size: 12px; display: grid; gap: 3px;
}
.maplegend .sep { height: 1px; background: var(--line); margin: 3px 0; }
.pulse-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--water); box-shadow: 0 0 0 3px rgba(30,136,229,.3); vertical-align: middle; }

/* ---------- map styling ---------- */
/* ขีดบนเส้นทางน้ำไหลไปทางท้ายน้ำ — ระดับสูงไหลเร็วขึ้นเล็กน้อย แต่ไม่เร็วจนตาลาย (เดิม 0.4 วินาทีที่ระดับวิกฤต) */
.flow { stroke-dasharray: 10 14; animation: flow 3.2s linear infinite; }
.flow.f1 { animation-duration: 2.6s; } .flow.f2 { animation-duration: 2s; } .flow.f3 { animation-duration: 1.5s; }
@media (prefers-reduced-motion: reduce) { .flow, .target-ring { animation: none; } }
/* ป้ายเวลาถึงเป้าหมายบนมวลน้ำ */
.parcel-eta {
  position: absolute; left: 50%; bottom: calc(100% + 4px); transform: translateX(-50%);
  padding: 1px 7px; border-radius: 10px; border: 2px solid; background: rgba(255,255,255,.95);
  color: var(--ink); font-size: 11px; font-weight: 700; white-space: nowrap; pointer-events: none;
  box-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.parcel-eta[hidden] { display: none; }

/* ป้ายสรุปผลบนแผนที่ */
.map-summary {
  position: absolute; top: 12px; left: 56px; right: 64px; z-index: 800; max-width: 620px;
  margin: 0 auto; padding: 9px 14px; border-radius: var(--radius); box-shadow: var(--shadow);
  background: rgba(255,255,255,.97); border-left: 6px solid var(--l0); font-size: 13.5px; line-height: 1.5;
}
.map-summary.lv1 { border-left-color: var(--l1); }
.map-summary.lv2 { border-left-color: var(--l2); background: #fff6ee; }
.map-summary.lv3 { border-left-color: var(--l3); background: #fff0f1; }
.map-summary a { color: var(--accent); }
.map-summary ul { margin: 4px 0 0; padding-left: 18px; font-size: 12.5px; }
.map-summary .ms-sub { margin-top: 3px; font-size: 11.5px; color: var(--muted); }

/* ชิป "กำลังดูเฉพาะเส้นทาง" */
.focus-chip {
  position: absolute; left: 50%; bottom: 92px; transform: translateX(-50%); z-index: 800;
  display: flex; align-items: center; gap: 8px; max-width: calc(100% - 180px);
  padding: 6px 6px 6px 14px; border-radius: 999px; background: #0f2b46; color: #fff;
  font-size: 12.5px; box-shadow: var(--shadow);
}
.focus-chip[hidden] { display: none; }
.focus-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.focus-chip button { flex: none; border: 0; border-radius: 999px; padding: 4px 10px; background: #fff; color: #0f2b46; font-size: 12px; font-weight: 700; }
.route.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); padding-left: 8px; }

/* คำอธิบายครั้งแรก */
.map-help-btn {
  position: absolute; right: 12px; top: 58px; z-index: 800; width: 34px; height: 34px; border-radius: 50%;
  border: 0; background: #fff; color: var(--accent); font-weight: 800; font-size: 16px; box-shadow: var(--shadow);
}
.map-help {
  position: absolute; right: 56px; top: 58px; z-index: 900; width: min(340px, calc(100% - 80px));
  padding: 12px 14px; border-radius: var(--radius); background: #fff; box-shadow: 0 8px 28px rgba(16,32,48,.25);
  font-size: 13px;
}
.map-help[hidden] { display: none; }
.map-help ul { margin: 6px 0 10px; padding-left: 18px; }
.map-help li { margin-bottom: 6px; }
.map-help .help-line { display: inline-block; width: 26px; height: 5px; border-radius: 3px; margin-right: 4px; vertical-align: middle; background: linear-gradient(90deg, var(--l0), var(--l1), var(--l2), var(--l3)); }
.map-help button { border: 0; border-radius: 8px; padding: 6px 16px; background: var(--accent); color: #fff; font-weight: 700; }

.speed { flex: none; min-width: 48px; height: 32px; padding: 0 10px; border-radius: 16px; border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-size: 12px; font-weight: 700; cursor: pointer; }
.speed:hover { border-color: var(--accent); color: var(--accent); }
@keyframes flow { to { stroke-dashoffset: -24; } }
.target-ring { border-radius: 50%; border: 3px solid var(--accent); background: rgba(21,101,192,.18); animation: ring 1.8s ease-out infinite; }
@keyframes ring { 0% { transform: scale(.6); opacity: 1; } 100% { transform: scale(1.8); opacity: 0; } }
.parcel { border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 10px 3px rgba(30,136,229,.55); }
.leaflet-popup-content { font-family: "IBM Plex Sans Thai", sans-serif; font-size: 13px; margin: 10px 12px; }
.pop h4 { margin: 0 0 4px; font-size: 14px; }
.pop .btn { margin-top: 8px; padding: 5px 10px; border: 0; border-radius: 6px; background: var(--accent); color: #fff; }
.pop table td { padding: 1px 8px 1px 0; }
.leaflet-tooltip { font-family: "IBM Plex Sans Thai", sans-serif; }
.busy { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 900; background: #0f2b46; color: #fff; padding: 6px 14px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow); }

@media (max-width: 860px) {
  .layout { flex-direction: column-reverse; }
  .panel { width: 100%; height: 48vh; border-right: 0; border-top: 1px solid var(--line); }
  .mapwrap { height: 52vh; flex: none; }
  .brand small, .status { display: none; }
  .maplegend { display: none; }
  .timeline { left: 8px; right: 8px; bottom: 8px; padding: 6px 10px; }
  /* แผนที่เตี้ย (52vh) — ป้ายสรุปย่อให้เหลือบรรทัดหลัก, ชิป/คำอธิบายขยายเต็มความกว้าง */
  .map-summary { top: 8px; left: 50px; right: 52px; padding: 6px 10px; font-size: 12px; }
  .map-summary ul, .map-summary .ms-sub { display: none; }
  .focus-chip { bottom: 74px; max-width: calc(100% - 16px); }
  .map-help { left: 8px; right: 8px; width: auto; top: 54px; }
  .map-help-btn { top: 54px; right: 10px; }
}
