:root {
  --bg: #f1f4f8;
  --card: #ffffff;
  --ink: #10293a;
  --ink-soft: #2c4557;
  --muted: #64798b;
  --muted-2: #8b9dab;
  --line: #e2e8ef;
  --line-strong: #d3dde7;
  --navy: #0a2c40;
  --navy-2: #0c3c53;
  --teal: #0e7490;
  --teal-soft: #e3f2f6;
  --amber: #c96a12;
  --amber-strong: #a9540a;
  --amber-soft: #fdf1e3;
  --male: #2f7ea8;
  --female: #b25c7a;
  --radius-lg: 18px;
  --radius-md: 12px;
  --shadow-sm: 0 1px 2px rgba(12, 40, 58, 0.04), 0 4px 14px rgba(12, 40, 58, 0.05);
  --shadow-md: 0 8px 28px rgba(12, 40, 58, 0.1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans Thai", "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--teal); }

.container {
  width: min(1280px, calc(100% - 40px));
  margin: 0 auto;
}

/* ---------- Hero ---------- */
.hero {
  position: relative;
  overflow: hidden;
  color: #f4fbfd;
  background:
    radial-gradient(900px 380px at 88% -10%, rgba(28, 160, 184, 0.38), transparent 60%),
    radial-gradient(600px 320px at 8% 110%, rgba(217, 119, 6, 0.22), transparent 65%),
    linear-gradient(118deg, #082536 0%, #0a3045 46%, #0b4356 100%);
}

.hero::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.045) 1px, transparent 1px);
  background-size: 46px 46px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.55), transparent 90%);
  pointer-events: none;
}

.hero-inner {
  position: relative;
  z-index: 1;
  display: flex;
  gap: 28px;
  align-items: flex-end;
  justify-content: space-between;
  padding: 30px 0 26px;
}

.kicker {
  font-size: 12.5px;
  letter-spacing: 0.06em;
  color: #9fd4e2;
  margin-bottom: 8px;
}

.hero h1 {
  font-size: clamp(26px, 3.4vw, 38px);
  line-height: 1.25;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.subtitle {
  margin-top: 6px;
  color: #c2dce5;
  font-size: 15px;
}

.hero-badges {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.09);
  border: 1px solid rgba(255, 255, 255, 0.16);
  font-size: 12.5px;
  color: #e7f5f8;
  backdrop-filter: blur(4px);
}

.dot { width: 7px; height: 7px; border-radius: 50%; }
.dot.teal { background: #45d3e8; box-shadow: 0 0 0 3px rgba(69, 211, 232, 0.18); }
.dot.amber { background: #f6b45c; box-shadow: 0 0 0 3px rgba(246, 180, 92, 0.18); }

.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; padding-bottom: 4px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 13.5px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

.btn:active { transform: translateY(1px); }

.btn-solid {
  background: #f0b35e;
  color: #2b1b06;
  border-color: #f0b35e;
}
.btn-solid:hover { background: #f7c377; }

.btn-ghost {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.24);
  color: #eaf7fa;
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.16); }

/* ---------- Layout ---------- */
main.container { padding: 20px 0 40px; display: grid; gap: 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px 0;
}

.card-head h2 { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.card-head h3 { font-size: 15.5px; font-weight: 700; }
.card-sub { margin-top: 2px; color: var(--muted); font-size: 12.5px; }

/* ---------- KPI ---------- */
.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
}

.kpi {
  position: relative;
  padding: 16px 18px 15px;
  overflow: hidden;
}

.kpi::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: linear-gradient(180deg, var(--teal), #52b7c9);
}

.kpi:nth-child(2)::before { background: linear-gradient(180deg, var(--amber), #efb160); }
.kpi:nth-child(3)::before { background: linear-gradient(180deg, #4a6f8f, #8fa9bf); }
.kpi:nth-child(4)::before { background: linear-gradient(180deg, var(--amber-strong), #d99246); }

.kpi-top { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.kpi-label { font-size: 13px; color: var(--muted); font-weight: 600; }

.kpi-year {
  font-size: 11px;
  color: var(--muted);
  background: #f1f5f8;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 6px;
  font-variant-numeric: tabular-nums;
}

.kpi-value {
  margin-top: 8px;
  font-size: clamp(26px, 2.7vw, 34px);
  font-weight: 800;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.amber-text { color: var(--amber-strong); }

.kpi-sub { margin-top: 3px; font-size: 12px; color: var(--muted); }

.kpi-chip {
  display: inline-block;
  margin-top: 10px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kpi-chip.teal { color: #075a6e; background: var(--teal-soft); }
.kpi-chip.amber { color: #8a4306; background: var(--amber-soft); }

.mini-split {
  margin-top: 10px;
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--muted);
  flex-wrap: wrap;
}
.mini-split b { color: var(--ink); font-weight: 700; font-variant-numeric: tabular-nums; }
.swatch { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }
.swatch.male { background: var(--male); }
.swatch.female { background: var(--female); }

.meter {
  margin-top: 12px;
  height: 8px;
  border-radius: 99px;
  background: #eef2f6;
  overflow: hidden;
}
.meter span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #0e7490, #31a6bd);
  transition: width 0.7s ease;
}

/* ---------- Map card ---------- */
.map-card { overflow: hidden; }
.map-head { padding-bottom: 4px; align-items: center; }

.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: #eef3f7;
  border: 1px solid var(--line);
  border-radius: 11px;
  flex-shrink: 0;
}

.segmented button {
  border: 0;
  background: transparent;
  padding: 7px 13px;
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
  white-space: nowrap;
}

.segmented button:hover { color: var(--ink); }
.segmented button.active {
  background: #fff;
  color: var(--navy);
  box-shadow: 0 1px 3px rgba(16, 41, 58, 0.14);
}

.map-toolbar {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 20px 14px;
  border-bottom: 1px solid var(--line);
}

.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 11.5px; color: var(--muted); font-weight: 600; }

.field select,
.field input {
  min-width: 150px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: #fff;
  font-size: 13px;
  outline: none;
}
.field input { min-width: 180px; }

.field select:focus,
.field input:focus {
  border-color: #7ab8c9;
  box-shadow: 0 0 0 3px rgba(14, 116, 144, 0.12);
}

.toolbar-spacer { flex: 1 1 20px; }

.coverage-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  color: #6b4a1d;
  background: var(--amber-soft);
  border: 1px solid #f1d9bb;
  padding: 6px 10px;
  border-radius: 999px;
  margin-bottom: 2px;
}
.coverage-chip::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #d08a2e;
}

.map-wrap {
  position: relative;
  height: 620px;
  background:
    radial-gradient(1200px 500px at 50% 0%, #eaf3f8 0%, #dce9f1 55%, #d3e2ec 100%);
}

#map { position: absolute; inset: 0; z-index: 1; background: transparent; }

.leaflet-container { font-family: inherit; background: transparent; }

.leaflet-tooltip {
  background: rgba(9, 38, 54, 0.94);
  color: #f4fbfd;
  border: 0;
  border-radius: 8px;
  padding: 7px 10px;
  box-shadow: 0 6px 18px rgba(5, 28, 40, 0.28);
  font-size: 12px;
  line-height: 1.45;
}
.leaflet-tooltip::before { display: none; }
.leaflet-tooltip b { font-size: 12.5px; }
.leaflet-tooltip .tt-count { color: #ffd08f; font-weight: 700; }

.map-loading {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(241, 246, 249, 0.85);
  color: var(--muted);
  font-size: 13px;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}
.map-loading.hidden { opacity: 0; visibility: hidden; }

.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #b9cbd7;
  border-top-color: var(--teal);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.legend {
  position: absolute;
  left: 16px;
  bottom: 16px;
  z-index: 4;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: var(--shadow-sm);
  max-width: 280px;
}
.legend-title { font-size: 11px; font-weight: 700; color: var(--ink-soft); margin-bottom: 6px; }
.legend-items { display: flex; flex-direction: column; gap: 4px; }
.legend-item { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--muted); }
.legend-swatch { width: 22px; height: 10px; border-radius: 3px; border: 1px solid rgba(16, 41, 58, 0.08); flex-shrink: 0; }

.map-detail {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 4;
  width: 300px;
  max-height: calc(100% - 32px);
  overflow: auto;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  padding: 15px 16px;
}

.map-detail .detail-kicker { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: 0.02em; }
.map-detail h3 { margin-top: 2px; font-size: 18px; line-height: 1.3; letter-spacing: -0.01em; }
.map-detail .detail-count {
  margin-top: 8px;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.map-detail .detail-count small { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 5px; letter-spacing: 0; }
.map-detail .detail-sub { font-size: 12px; color: var(--muted); margin-top: 1px; }

.detail-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 12px;
}
.detail-split > div {
  background: #f5f8fa;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
}
.detail-split span { display: block; font-size: 10.5px; color: var(--muted); }
.detail-split b { font-size: 15px; font-variant-numeric: tabular-nums; }

.detail-list { margin-top: 12px; display: grid; gap: 6px; }
.detail-row { display: grid; grid-template-columns: 108px 1fr 38px; align-items: center; gap: 7px; font-size: 11.5px; }
.detail-row .lbl { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail-row .bar { height: 6px; background: #edf2f6; border-radius: 99px; overflow: hidden; }
.detail-row .bar span { display: block; height: 100%; border-radius: inherit; background: var(--teal); }
.detail-row .val { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-soft); font-weight: 600; }

.detail-note { margin-top: 11px; font-size: 11px; color: var(--muted); border-top: 1px dashed var(--line-strong); padding-top: 9px; }

.detail-action {
  margin-top: 12px;
  width: 100%;
  border: 1px solid #f0cfa7;
  background: var(--amber-soft);
  color: #7c4008;
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.detail-action:hover { background: #fbe6cd; }

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

/* ---------- Charts ---------- */
.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.chart-card { padding-bottom: 14px; }
.chart-tag {
  font-size: 10.5px;
  color: var(--muted);
  background: #f3f6f9;
  border: 1px solid var(--line);
  padding: 2px 7px;
  border-radius: 6px;
  flex-shrink: 0;
}
.chart-box { position: relative; height: 300px; padding: 12px 14px 4px; }

/* ---------- Table ---------- */
.table-card { overflow: hidden; }
.table-badge {
  font-size: 11.5px;
  color: #075a6e;
  background: var(--teal-soft);
  border: 1px solid #c9e4ea;
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
}
.table-scroll { max-height: 460px; overflow: auto; margin-top: 12px; border-top: 1px solid var(--line); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f7fafc;
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 700;
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

thead th.num { text-align: right; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th.sortable .arrow { font-size: 9px; opacity: 0.5; }
th.sortable.sorted .arrow { opacity: 1; color: var(--teal); }

tbody td {
  padding: 9px 14px;
  border-bottom: 1px solid #eef2f6;
  vertical-align: middle;
}
tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; transition: background 0.12s ease; }
tbody tr:hover { background: #f6fafb; }
tbody tr.selected { background: #fff7ec; box-shadow: inset 3px 0 0 var(--amber); }
tbody tr:last-child td { border-bottom: 0; }

.t-name { font-weight: 600; }
.t-type {
  display: inline-block;
  font-size: 11px;
  color: var(--ink-soft);
  background: #f1f5f8;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 7px;
}

.table-foot { padding: 10px 20px 14px; border-top: 1px solid var(--line); font-size: 11.5px; color: var(--muted); }
.empty-row td { text-align: center; color: var(--muted); padding: 28px 14px; cursor: default; }

/* ---------- Sources ---------- */
.sources { padding-bottom: 20px; }
.source-badge {
  font-size: 11px;
  font-weight: 700;
  color: #0b5b4e;
  background: #e2f5ee;
  border: 1px solid #c3e8dc;
  padding: 3px 9px;
  border-radius: 999px;
}

.source-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 14px 20px 0;
}

.source-item {
  display: block;
  text-decoration: none;
  color: inherit;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 13px;
  background: #fbfcfd;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}
.source-item:hover { border-color: #b7d6df; background: #f7fbfc; transform: translateY(-1px); }
.source-item .src-org { font-size: 11px; color: var(--teal); font-weight: 700; }
.source-item .src-name { display: block; margin-top: 2px; font-size: 12.5px; font-weight: 600; line-height: 1.45; }
.source-item .src-via { display: block; margin-top: 4px; font-size: 11px; color: var(--muted); }

.method {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  padding: 18px 20px 4px;
}
.method h3 { font-size: 12.5px; margin-bottom: 5px; color: var(--ink-soft); }
.method p { font-size: 12px; color: var(--muted); line-height: 1.65; }
.method b { color: var(--ink-soft); }

.note-list {
  margin: 14px 20px 0;
  padding: 13px 16px 13px 32px;
  background: #f8fafb;
  border: 1px dashed var(--line-strong);
  border-radius: 11px;
  color: var(--muted);
  font-size: 11.5px;
  display: grid;
  gap: 5px;
}
.note-list li::marker { color: #b7864a; }

/* ---------- Footer ---------- */
.footer { border-top: 1px solid var(--line); background: #ecf1f5; }
.footer-inner {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 18px 0 22px;
  font-size: 11.5px;
  color: var(--muted);
}

/* ---------- Responsive ---------- */
@media (max-width: 1020px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .map-detail { width: 270px; }
}

@media (max-width: 820px) {
  .container { width: min(100% - 24px, 1280px); }
  .hero-inner { flex-direction: column; align-items: flex-start; gap: 16px; padding: 24px 0 20px; }
  .hero-actions { padding-bottom: 0; }
  .chart-grid { grid-template-columns: 1fr; }
  .source-grid, .method { grid-template-columns: 1fr; }
  .map-head { flex-direction: column; align-items: flex-start; }
  .segmented { align-self: stretch; }
  .segmented button { flex: 1; }
  .map-wrap { height: auto; }
  #map { position: relative; height: 430px; }
  .map-detail {
    position: relative;
    inset: auto;
    width: auto;
    max-height: none;
    margin: 12px;
    box-shadow: var(--shadow-sm);
  }
  .legend {
    position: static;
    margin: 0 12px 12px;
    max-width: none;
    box-shadow: none;
  }
  .map-loading { height: 430px; border-radius: 0; }
}

@media (max-width: 560px) {
  .kpis { grid-template-columns: 1fr; }
  .chart-box { height: 320px; padding: 8px 8px 0; }
  .card-head { padding: 16px 16px 0; }
  .map-toolbar { padding: 12px 16px; }
  .field, .field select, .field input { width: 100%; min-width: 0; }
  .field { flex: 1 1 100%; }
  .table-scroll { max-height: 380px; }
  thead th, tbody td { padding-left: 10px; padding-right: 10px; }
  .footer-inner { flex-direction: column; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
  .meter span { transition: none; }
}

/* ---------- Print ---------- */
@media print {
  body { background: #fff; font-size: 11px; }
  .hero { background: #0b3045 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .hero-actions, .map-toolbar, .segmented, .map-detail, .map-loading, .coverage-chip { display: none !important; }
  .container { width: 100%; }
  main.container { gap: 10px; padding: 10px 0; }
  .card { box-shadow: none; break-inside: avoid; }
  .map-wrap { height: 420px; }
  .chart-grid { grid-template-columns: 1fr 1fr; }
  .chart-box { height: 220px; }
  .table-scroll { max-height: none; overflow: visible; }
  .footer { background: #fff; }
}
