/* === DESIGN TOKENS === */
:root, [data-theme="light"] {
  --color-bg: #f5f5f0;
  --color-surface: #ffffff;
  --color-surface-2: #fafaf7;
  --color-border: #d4d1ca;
  --color-divider: #e5e3de;
  --color-text: #1a1a18;
  --color-text-muted: #6b6a66;
  --color-text-faint: #9e9d99;
  --color-primary: #1a6b5a;
  --color-primary-hover: #145549;
  --color-accent: #c45a2c;
  --color-danger: #c23b3b;
  --color-warning: #b5850a;
  --color-success: #2d7a3a;

  --text-xs: clamp(0.6875rem, 0.65rem + 0.2vw, 0.8125rem);
  --text-sm: clamp(0.8125rem, 0.78rem + 0.15vw, 0.875rem);
  --text-base: clamp(0.875rem, 0.85rem + 0.12vw, 0.9375rem);
  --text-lg: clamp(1rem, 0.95rem + 0.2vw, 1.125rem);
  --text-xl: clamp(1.125rem, 1rem + 0.5vw, 1.5rem);

  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  --radius-sm: 0.25rem;
  --radius-md: 0.375rem;
  --radius-lg: 0.5rem;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);

  --sidebar-width: 300px;
  --header-height: 48px;
}

[data-theme="dark"] {
  --color-bg: #111110;
  --color-surface: #1a1a18;
  --color-surface-2: #222220;
  --color-border: #333330;
  --color-divider: #2a2a28;
  --color-text: #d4d3cf;
  --color-text-muted: #8a8985;
  --color-text-faint: #5a5955;
  --color-primary: #4aad92;
  --color-primary-hover: #3d9880;
  --color-accent: #e07040;
  --color-danger: #e05555;
  --color-warning: #d4a620;
  --color-success: #4da65a;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.5);
}

/* === RESET & BASE === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; text-size-adjust: none; }
body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
}

/* === HEADER === */
#app-header {
  height: var(--header-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  z-index: 1000;
  position: relative;
}
.header-left {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}
.header-left h1 {
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: -0.02em;
  white-space: nowrap;
}
.logo { flex-shrink: 0; border-radius: var(--radius-sm); }
.header-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* === LAYOUT === */
#app {
  display: flex;
  height: calc(100vh - var(--header-height) - 32px);
  min-height: 500px;
}

/* === SIDEBAR === */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.sidebar-section h3 {
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

/* === BUTTONS === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--color-border);
  background: var(--color-surface-2);
  color: var(--color-text);
  transition: all 150ms ease;
}
.btn:hover { background: var(--color-divider); }
.btn-primary {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-secondary { background: transparent; }

.btn-group {
  display: flex;
  gap: 0;
}
.btn-toggle {
  border-radius: 0;
  flex: 1;
  justify-content: center;
  padding: var(--space-2) var(--space-3);
}
.btn-toggle:first-child { border-radius: var(--radius-md) 0 0 var(--radius-md); }
.btn-toggle:last-child { border-radius: 0 var(--radius-md) var(--radius-md) 0; }
.btn-toggle.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

#theme-toggle {
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-1);
  color: var(--color-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
}
#theme-toggle:hover { color: var(--color-text); background: var(--color-surface-2); }

/* === FORM CONTROLS === */
select, input[type="number"] {
  width: 100%;
  padding: var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  color: var(--color-text);
  font-size: var(--text-sm);
  font-family: var(--font-body);
}
select:focus, input:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
input[type="range"] {
  width: 100%;
  accent-color: var(--color-primary);
}

.filter-row {
  margin-bottom: var(--space-3);
}
.filter-row label {
  display: block;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-text-muted);
  margin-bottom: var(--space-1);
}
.filter-row span {
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  font-family: var(--font-mono);
}
.range-inputs {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.range-inputs input { width: 50%; padding: var(--space-1) var(--space-2); }
.range-inputs span { color: var(--color-text-faint); }

#filter-section .btn { width: 48%; margin-top: var(--space-2); }
#filter-section .btn + .btn { margin-left: 4%; }

/* === MAP === */
#map-container {
  flex: 1;
  position: relative;
  min-width: 0;
}
#map {
  width: 100%;
  height: 100%;
}
.leaflet-container { background: var(--color-bg); }

/* Fix Leaflet attribution for dark mode */
[data-theme="dark"] .leaflet-control-attribution {
  background: rgba(26,26,24,0.85) !important;
  color: var(--color-text-muted) !important;
}
[data-theme="dark"] .leaflet-control-attribution a {
  color: var(--color-primary) !important;
}
[data-theme="dark"] .leaflet-control-zoom a {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border-color: var(--color-border) !important;
}

/* === LEGEND === */
#legend {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
}
.legend-swatch {
  width: 20px;
  height: 14px;
  border-radius: 2px;
  flex-shrink: 0;
  border: 1px solid rgba(128,128,128,0.2);
}
.legend-bar {
  display: flex;
  height: 12px;
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: var(--space-1);
}
.legend-bar-segment { flex: 1; }
.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-family: var(--font-mono);
}

/* === SUMMARY STATS === */
#summary-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
}
.stat-card {
  padding: var(--space-2);
  background: var(--color-surface-2);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-divider);
}
.stat-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: var(--space-1);
}
.stat-value {
  font-size: var(--text-lg);
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--color-text);
}
.stat-sub {
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}

/* === INFO PANEL === */
#info-panel {
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  width: 360px;
  max-height: calc(100% - var(--space-8));
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  overflow-y: auto;
  padding: var(--space-4);
  transition: opacity 200ms, transform 200ms;
}
#info-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(10px);
}
#close-panel {
  position: absolute;
  top: var(--space-2);
  right: var(--space-2);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-size: var(--text-lg);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
#close-panel:hover { background: var(--color-divider); }

.panel-title {
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: var(--space-3);
  padding-right: var(--space-8);
}
.panel-subtitle {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: var(--space-4);
}
.panel-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}
.panel-metric {
  padding: var(--space-2);
  background: var(--color-surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-divider);
}
.panel-metric-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: 2px;
}
.panel-metric-value {
  font-size: var(--text-base);
  font-weight: 600;
  font-family: var(--font-mono);
}
.panel-metric.full { grid-column: 1 / -1; }

.panel-section-title {
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-divider);
}
.panel-chart-container {
  height: 180px;
  margin-bottom: var(--space-2);
}

/* === TOOLTIP === */
.map-tooltip {
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-family: var(--font-body);
  font-size: var(--text-sm);
  color: var(--color-text);
  line-height: 1.4;
}
.map-tooltip strong { font-weight: 600; }
.map-tooltip .tt-row { display: flex; justify-content: space-between; gap: var(--space-4); }
.map-tooltip .tt-label { color: var(--color-text-muted); }
.map-tooltip .tt-value { font-family: var(--font-mono); font-weight: 500; }

/* Leaflet tooltip override */
.leaflet-tooltip.map-tooltip {
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  opacity: 1;
}
.leaflet-tooltip-top.map-tooltip::before { border-top-color: var(--color-border); }
.leaflet-tooltip-bottom.map-tooltip::before { border-bottom-color: var(--color-border); }

/* === FOOTER === */
footer {
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
}
footer a { color: var(--color-text-muted); text-decoration: none; }
footer a:hover { color: var(--color-primary); }

/* === NAV LINKS === */
.header-home {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  text-decoration: none;
  color: inherit;
}
.header-right .btn-secondary.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}
a.btn {
  text-decoration: none;
}

/* === ABOUT PAGE === */
body.about-page {
  overflow-y: auto;
  height: auto;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
body.about-page #app-header {
  flex-shrink: 0;
}
.about-main {
  flex: 1;
  background: var(--color-bg);
  padding-bottom: 4rem;
}
body.about-page footer {
  flex-shrink: 0;
  margin-top: auto;
}
.about-inner {
  max-width: 1060px;
  margin: 0 auto;
  padding: 3.5rem 2.5rem;
}
.about-inner > h2 {
  font-size: clamp(1.4rem, 1.2rem + 0.8vw, 1.75rem);
  font-weight: 700;
  margin-bottom: 1.25rem;
  color: var(--color-text);
}
.about-inner > p {
  color: var(--color-text-muted);
  line-height: 1.8;
  margin-bottom: 3rem;
  font-size: var(--text-base);
  max-width: 780px;
}
.about-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem;
  margin-bottom: 3.5rem;
}
.about-card {
  padding: 1.75rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}
.about-card h3 {
  font-size: var(--text-lg);
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--color-text);
}
.about-card p {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  line-height: 1.75;
}
.about-faq {
  max-width: 100%;
  margin-top: 3rem;
  padding-top: 2.5rem;
  border-top: 1px solid var(--color-divider);
}
.about-faq + .about-faq {
  margin-top: 2.5rem;
  padding-top: 2rem;
}
.about-faq h3 {
  font-size: clamp(1.1rem, 1rem + 0.4vw, 1.3rem);
  font-weight: 600;
  margin-bottom: 1.25rem;
  color: var(--color-text);
}
.about-faq details {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  margin-bottom: 0.625rem;
  background: var(--color-surface);
}
.about-faq summary {
  padding: 1rem 1.5rem;
  font-weight: 500;
  cursor: pointer;
  font-size: var(--text-base);
  color: var(--color-text);
  line-height: 1.5;
}
.about-faq summary:hover {
  background: var(--color-surface-2);
}
.about-faq summary::marker {
  color: var(--color-text-muted);
}
.about-faq details[open] summary {
  border-bottom: 1px solid var(--color-divider);
}
.about-faq details p {
  padding: 1.25rem 1.5rem;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  line-height: 1.8;
}
@media (max-width: 640px) {
  .about-grid { grid-template-columns: 1fr; }
  .about-inner { padding: 2rem 1.25rem; }
}

/* === LOADING === */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  gap: var(--space-4);
}
.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* === RESPONSIVE === */
@media (max-width: 768px) {
  #sidebar {
    width: 100%;
    min-width: 100%;
    max-height: 40vh;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
  }
  #app { flex-direction: column; }
  #info-panel { width: calc(100% - var(--space-8)); }
  .header-left h1 { font-size: var(--text-sm); }
}
