:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f6;
  --border: #e2e6ee;
  --border-strong: #cbd2df;
  --text: #0f172a;
  --text-muted: #526075;
  --text-subtle: #7b889c;
  --accent: #0f4c81;
  --accent-soft: #e7f0f9;
  --accent-2: #c95b1e;
  --success: #0d7a5f;
  --warning: #b45309;
  --danger: #b91c1c;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 6px 20px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 44px rgba(15, 23, 42, 0.16);
  --radius: 10px;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-serif: "Instrument Serif", Georgia, serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, monospace;
}

[data-theme="dark"] {
  --bg: #0b111b;
  --surface: #121a28;
  --surface-2: #192334;
  --border: #243147;
  --border-strong: #334563;
  --text: #eef2f7;
  --text-muted: #9eb0c7;
  --text-subtle: #7386a1;
  --accent: #4da3ff;
  --accent-soft: rgba(77, 163, 255, 0.14);
  --accent-2: #f98c4b;
  --success: #2dd4a8;
  --warning: #fbbf24;
  --danger: #f87171;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.num { font-variant-numeric: tabular-nums; font-family: var(--font-mono); font-size: 0.92em; }

/* Top Navigation */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: 58px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
  flex-shrink: 0;
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  background: linear-gradient(135deg, #c95b1e 0%, #0f4c81 65%, #0d7a5f 100%);
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: -0.03em;
}

.brand-title {
  font-family: var(--font-serif);
  font-size: 22px;
  line-height: 1;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-subtle);
  display: block;
  margin-top: 2px;
}

.nav-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
}

.nav-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 13px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.nav-tab:hover { background: var(--surface-2); color: var(--text); }
.nav-tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12.5px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 7px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s ease;
}
.btn:hover { border-color: var(--border-strong); background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-sm { padding: 4px 9px; font-size: 12px; }

/* KPI Strip */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.kpi-pill {
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.kpi-pill .label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-subtle);
  display: block;
}
.kpi-pill .val {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-top: 1px;
  display: block;
}

/* Main Views */
.view { display: none; }
.view.active { display: block; }

/* Atlas Map View */
.atlas-layout {
  display: grid;
  grid-template-columns: 310px 1fr;
  height: calc(100vh - 122px);
  min-height: 600px;
}

.atlas-sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-group label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  margin-bottom: 6px;
}

select, input[type="text"], input[type="search"] {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  outline: none;
}
select:focus, input:focus { border-color: var(--accent); }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  background: var(--bg);
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.segmented button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.segmented button.active {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.map-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--surface-2);
}
#atlas-map { width: 100%; height: 100%; }

.map-legend {
  position: absolute;
  bottom: 20px;
  left: 16px;
  z-index: 10;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 14px;
  box-shadow: var(--shadow-md);
  min-width: 220px;
  max-width: 290px;
}
.map-legend h4 { font-size: 12px; font-weight: 600; margin-bottom: 6px; color: var(--text); }
.legend-ramp {
  height: 10px;
  border-radius: 5px;
  margin-bottom: 5px;
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}
.legend-cats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  font-size: 11.5px;
}
.legend-cat-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0; }

.map-tooltip {
  position: absolute;
  pointer-events: none;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: var(--shadow-lg);
  max-width: 280px;
  font-size: 12.5px;
  display: none;
}
.map-tooltip .tt-title { font-weight: 700; font-size: 13.5px; margin-bottom: 2px; }
.map-tooltip .tt-sub { font-size: 11.5px; color: var(--text-muted); margin-bottom: 6px; }
.map-tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; padding: 1.5px 0; }
.map-tooltip .tt-k { color: var(--text-muted); }
.map-tooltip .tt-v { font-family: var(--font-mono); font-weight: 600; }

.map-status-badge {
  position: absolute;
  top: 14px;
  left: 16px;
  z-index: 10;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--text-muted);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  gap: 8px;
}
.pulse-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--success);
}

/* Spatial Lookup Card inside Sidebar */
.spatial-card {
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg);
  padding: 12px;
  font-size: 12.5px;
}
.spatial-card h4 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
  margin-bottom: 8px;
}
.spatial-kv {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px dashed var(--border);
}
.spatial-kv:last-child { border-bottom: none; }
.spatial-kv .k { color: var(--text-muted); flex-shrink: 0; }
.spatial-kv .v { font-weight: 600; text-align: right; }

/* Standard Page Container */
.page-wrap {
  max-width: 1480px;
  margin: 0 auto;
  padding: 20px 24px 48px;
}

.section-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.section-header h2 {
  font-family: var(--font-serif);
  font-size: 28px;
  font-weight: 400;
  line-height: 1.1;
}
.section-header p { color: var(--text-muted); font-size: 13px; margin-top: 4px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.card-sub { font-size: 11.5px; font-weight: 400; color: var(--text-subtle); }

.chart-box { width: 100%; height: 320px; }
.chart-box-lg { width: 100%; height: 380px; }
.chart-box-sm { width: 100%; height: 240px; }

/* Filter Bar */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.filter-bar > * { max-width: 240px; }
.filter-bar .grow { flex: 1; max-width: 380px; }

/* Tables */
.table-wrap {
  overflow-x: auto;
  max-height: 540px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}
thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
  padding: 9px 11px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
}
thead th.num-col, tbody td.num-col { text-align: right; font-family: var(--font-mono); font-size: 12px; }
tbody td {
  padding: 8px 11px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody tr:hover { background: var(--accent-soft); }
tbody tr.clickable { cursor: pointer; }

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
}
.badge-ingested { background: rgba(13, 122, 95, 0.14); color: var(--success); }
.badge-awaiting { background: rgba(180, 83, 9, 0.14); color: var(--warning); }
.badge-rural { background: rgba(13, 122, 95, 0.12); color: var(--success); }
.badge-urban { background: rgba(15, 76, 129, 0.14); color: var(--accent); }
.badge-party { color: #fff; padding: 2px 8px; border-radius: 5px; font-size: 11px; font-weight: 600; }

/* Slide-Over Detail Drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.42);
  z-index: 60;
  display: none;
}
.drawer-backdrop.open { display: block; }

.drawer {
  position: fixed;
  top: 0;
  right: -680px;
  width: min(680px, 96vw);
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  z-index: 70;
  transition: right 0.24s cubic-bezier(0.22, 1, 0.36, 1);
  display: flex;
  flex-direction: column;
}
.drawer.open { right: 0; }

.drawer-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.drawer-header h3 { font-family: var(--font-serif); font-size: 26px; font-weight: 400; line-height: 1.1; }
.drawer-header .sub { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.drawer-body { padding: 18px 20px 36px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 16px; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.stat-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
}
.stat-box .k { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-subtle); }
.stat-box .v { font-family: var(--font-mono); font-size: 15px; font-weight: 600; margin-top: 2px; }

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--text-muted);
}

@media (max-width: 1180px) {
  .kpi-strip { grid-template-columns: repeat(4, 1fr); }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .atlas-layout { grid-template-columns: 1fr; height: auto; }
  .map-wrap { height: 68vh; }
  .kpi-strip { grid-template-columns: repeat(2, 1fr); }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
}
