:root {
  --navy-950: #07152c;
  --navy-900: #0b1f3a;
  --navy-800: #12345a;
  --blue: #2563eb;
  --cyan: #0ea5e9;
  --emerald: #059669;
  --amber: #d97706;
  --red: #dc2626;
  --gold: #d6a936;
  --ink: #12233f;
  --muted: #64748b;
  --line: #dce5ef;
  --surface: #ffffff;
  --canvas: #eef3f8;
  --radius: 18px;
  --shadow: 0 14px 35px rgba(15, 36, 68, 0.09);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 5% 0%, rgba(14, 165, 233, 0.12), transparent 28rem),
    radial-gradient(circle at 100% 15%, rgba(5, 150, 105, 0.1), transparent 30rem),
    var(--canvas);
  font-family: "Sarabun", sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.app-shell { width: min(1540px, calc(100% - 32px)); margin: 22px auto 36px; }
.hero-card {
  position: relative;
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 20px;
  padding: 26px 28px 18px;
  color: #fff;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 24px;
  background: linear-gradient(135deg, var(--navy-950), var(--navy-800) 70%, #0c5a63);
  box-shadow: 0 20px 50px rgba(7, 21, 44, 0.25);
}
.hero-card::after {
  content: "";
  position: absolute;
  width: 280px;
  height: 280px;
  right: -80px;
  top: -140px;
  border: 52px solid rgba(214,169,54,0.12);
  border-radius: 50%;
}
.brand, .hero-actions, .source-strip { position: relative; z-index: 1; }
.brand { display: flex; gap: 18px; align-items: center; }
.brand-icon {
  display: grid;
  place-items: center;
  flex: 0 0 70px;
  height: 70px;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 20px;
  color: #fff;
  background: linear-gradient(145deg, #0ea5e9, #2563eb);
  font-size: 30px;
  box-shadow: 0 10px 24px rgba(14,165,233,0.24);
}
.eyebrow { margin: 0 0 5px; color: var(--gold); font-size: 0.72rem; font-weight: 700; letter-spacing: 0.13em; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 6px; font-size: clamp(1.35rem, 2.4vw, 2.1rem); line-height: 1.22; }
h2 { margin-bottom: 4px; font-size: 1.2rem; }
.subtitle { margin-bottom: 0; color: #cbd8e8; }
.hero-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 10px; }
.mode-badge, .count-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 13px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 700;
}
.mode-badge.public { color: #dbeafe; background: rgba(37,99,235,0.26); border: 1px solid rgba(147,197,253,0.3); }
.mode-badge.private { color: #d1fae5; background: rgba(5,150,105,0.25); border: 1px solid rgba(110,231,183,0.3); }
.source-strip {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding-top: 14px;
  border-top: 1px solid rgba(255,255,255,0.13);
  color: #cbd8e8;
  font-size: 0.84rem;
}
.source-strip span { display: inline-flex; gap: 7px; align-items: center; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--ink);
  background: #fff;
  font-weight: 700;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.button:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(15,36,68,0.12); }
.button.primary { border-color: transparent; color: #fff; background: linear-gradient(135deg, #0ea5e9, #2563eb); }
.button.ghost { background: rgba(255,255,255,0.94); }
.button.wide { width: 100%; }

.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin: 16px 0; }
.kpi-card {
  display: flex;
  gap: 15px;
  align-items: center;
  min-height: 130px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.kpi-icon { display: grid; place-items: center; width: 48px; height: 48px; border-radius: 15px; font-size: 20px; }
.kpi-card small { display: block; color: var(--muted); font-weight: 700; }
.kpi-card strong { display: block; margin: 2px 0; font-size: 2.15rem; line-height: 1; }
.kpi-card p { margin: 0; color: var(--muted); font-size: 0.82rem; }
.kpi-card.blue .kpi-icon { color: var(--blue); background: #dbeafe; }
.kpi-card.red .kpi-icon { color: var(--red); background: #fee2e2; }
.kpi-card.amber .kpi-icon { color: var(--amber); background: #fef3c7; }
.kpi-card.emerald .kpi-icon { color: var(--emerald); background: #d1fae5; }

.filter-card, .panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.filter-card { margin-bottom: 16px; padding: 20px; }
.section-heading, .panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.filter-grid { display: grid; grid-template-columns: repeat(4, 1fr) minmax(250px, 1.25fr); gap: 12px; margin-top: 15px; }
label span { display: block; margin-bottom: 6px; color: #354966; font-size: 0.85rem; font-weight: 700; }
select, input {
  width: 100%;
  min-height: 43px;
  border: 1px solid #cad7e6;
  border-radius: 11px;
  outline: none;
  color: var(--ink);
  background: #f8fafc;
}
select { padding: 8px 36px 8px 11px; }
input { padding: 8px 11px; }
select:focus, input:focus { border-color: #38bdf8; box-shadow: 0 0 0 3px rgba(56,189,248,0.15); }
input:disabled { color: #94a3b8; cursor: not-allowed; }
.input-wrap { position: relative; }
.input-wrap i { position: absolute; left: 12px; top: 14px; color: #94a3b8; }
.input-wrap input { padding-left: 36px; }
.privacy-hint { margin: 14px 0 0; padding: 10px 12px; border-radius: 10px; color: #1d4ed8; background: #eff6ff; font-size: 0.84rem; }

.main-grid { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(330px, 0.75fr); gap: 16px; min-width: 0; }
.panel { min-width: 0; padding: 19px; }
.count-pill { color: #075985; background: #e0f2fe; }
#map { width: 100%; min-width: 0; height: 600px; margin-top: 14px; border: 1px solid #cad7e6; border-radius: 15px; background: #dbe7ef; }
.chart-wrap { position: relative; height: 540px; margin-top: 24px; }
.map-legend { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 12px; color: var(--muted); font-size: 0.82rem; }
.map-legend span { display: inline-flex; align-items: center; gap: 7px; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.red { background: var(--red); }
.dot.amber { background: var(--amber); }
.boundary-box { width: 15px; height: 10px; border: 2px solid #0f766e; border-radius: 2px; background: rgba(16,185,129,0.12); }

.table-panel { margin-top: 16px; }
.table-wrap { margin-top: 12px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 900px; }
th { padding: 11px 12px; color: #475569; background: #f1f5f9; font-size: 0.82rem; text-align: left; }
td { padding: 12px; border-bottom: 1px solid #e8eef5; font-size: 0.88rem; vertical-align: middle; }
tbody tr:hover { background: #f8fbfd; }
.name-cell { font-weight: 700; }
.tag { display: inline-flex; padding: 4px 9px; border-radius: 999px; font-size: 0.76rem; font-weight: 700; }
.tag.red { color: #991b1b; background: #fee2e2; }
.tag.gray { color: #475569; background: #e2e8f0; }
.tag.green { color: #065f46; background: #d1fae5; }
.map-link { color: #0369a1; font-weight: 700; text-decoration: none; }
.pagination { display: flex; justify-content: center; flex-wrap: wrap; gap: 7px; margin-top: 15px; }
.page-button { min-width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 9px; color: var(--ink); background: #fff; }
.page-button.active { border-color: var(--blue); color: #fff; background: var(--blue); }

.patient-marker { background: transparent; border: 0; }
.patient-dot { display: block; width: 16px; height: 16px; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 2px 10px rgba(15,23,42,0.42); }
.patient-dot.red { background: var(--red); }
.patient-dot.amber { background: var(--amber); }
.tambon-label { padding: 2px 5px !important; border: 0 !important; border-radius: 5px !important; color: #0f3d45 !important; background: rgba(255,255,255,0.82) !important; box-shadow: none !important; font-family: "Sarabun", sans-serif; font-size: 10px; font-weight: 700; }
.case-popup { min-width: 230px; font-family: "Sarabun", sans-serif; }
.case-popup h3 { margin: 0 0 8px; color: var(--navy-900); font-size: 1rem; }
.case-popup p { margin: 4px 0; font-size: 0.82rem; }
.case-popup .popup-note { margin-top: 8px; padding: 7px 8px; border-radius: 8px; color: #475569; background: #f1f5f9; }

.modal-backdrop { position: fixed; inset: 0; z-index: 3000; display: grid; place-items: center; padding: 18px; background: rgba(7,21,44,0.72); backdrop-filter: blur(6px); }
.modal-card { position: relative; width: min(430px, 100%); padding: 26px; border-radius: 20px; background: #fff; box-shadow: 0 28px 80px rgba(0,0,0,0.28); }
.modal-card label { display: block; margin-top: 14px; }
.modal-icon { display: grid; place-items: center; width: 54px; height: 54px; margin-bottom: 13px; border-radius: 16px; color: #fff; background: linear-gradient(135deg, var(--emerald), var(--cyan)); font-size: 23px; }
.modal-close { position: absolute; top: 14px; right: 14px; width: 36px; height: 36px; border: 0; border-radius: 50%; color: #64748b; background: #f1f5f9; }
.form-error { margin: 12px 0; padding: 9px; border-radius: 8px; color: #b91c1c; background: #fee2e2; font-size: 0.84rem; }
.demo-note { margin-top: 14px; padding: 10px 12px; border: 1px dashed #bfdbfe; border-radius: 9px; color: #475569; background: #eff6ff; font-size: 0.78rem; }
.toast { position: fixed; right: 18px; bottom: 18px; z-index: 4000; max-width: 380px; padding: 12px 16px; border-radius: 12px; color: #fff; background: var(--navy-900); box-shadow: 0 15px 40px rgba(0,0,0,0.22); }
.loading-overlay { position: fixed; inset: 0; z-index: 5000; display: grid; place-content: center; justify-items: center; gap: 14px; color: #fff; background: rgba(7,21,44,0.92); }
.loader { width: 44px; height: 44px; border: 4px solid rgba(255,255,255,0.24); border-top-color: #38bdf8; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
footer { padding: 24px 10px 0; color: var(--muted); font-size: 0.8rem; text-align: center; }

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .filter-grid { grid-template-columns: repeat(2, 1fr); }
  .search-field { grid-column: 1 / -1; }
  .main-grid { grid-template-columns: 1fr; }
  .chart-wrap { height: 380px; }
}

@media (max-width: 720px) {
  .app-shell { width: min(100% - 18px, 1540px); margin-top: 9px; }
  .hero-card { grid-template-columns: 1fr; padding: 20px 17px 15px; border-radius: 18px; }
  .brand { align-items: flex-start; }
  .brand-icon { flex-basis: 54px; height: 54px; border-radius: 15px; font-size: 23px; }
  .hero-actions { justify-content: flex-start; }
  .source-strip { flex-direction: column; }
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 9px; }
  .kpi-card { min-height: 105px; padding: 14px; }
  .kpi-icon { display: none; }
  .kpi-card strong { font-size: 1.75rem; }
  .filter-grid { grid-template-columns: 1fr; }
  .search-field { grid-column: auto; }
  .panel, .filter-card { padding: 15px; }
  #map { height: 520px; }
}
