/* ── Reset ─────────────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

/* ── Layout ────────────────────────────────────────────────────────────────── */
body {
  display: flex;
}

#map {
  flex: 1;
  height: 100vh;
  z-index: 0;
}

/* ── Sidebar ───────────────────────────────────────────────────────────────── */
.sidebar {
  width: 280px;
  min-width: 280px;
  height: 100vh;
  background: #1e293b;
  color: #f1f5f9;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  transition: margin-left 0.3s ease;
  z-index: 1000;
}

.sidebar.collapsed {
  margin-left: -280px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #334155;
}

.sidebar-header h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

/* ── Sidebar toggle buttons ────────────────────────────────────────────────── */
.sidebar-toggle {
  background: none;
  border: 1px solid #475569;
  color: #f1f5f9;
  font-size: 1.3rem;
  width: 30px;
  height: 30px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.sidebar-toggle:hover {
  background: #334155;
}

.sidebar-open-btn {
  display: none;            /* hidden when sidebar is open */
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1100;
  background: #1e293b;
  color: #f1f5f9;
  border: 1px solid #475569;
  font-size: 1.4rem;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.sidebar-open-btn:hover {
  background: #334155;
}

.sidebar.collapsed ~ .sidebar-open-btn {
  display: flex;
}

/* ── Sidebar sections ──────────────────────────────────────────────────────── */
.sidebar-section {
  padding: 14px 16px;
  border-bottom: 1px solid #334155;
}

.sidebar-section h2 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #94a3b8;
  margin-bottom: 10px;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 0.9rem;
  cursor: pointer;
}

.radio-label input[type="radio"] {
  accent-color: #60a5fa;
}

/* Disabled sections */
.disabled-section {
  opacity: 0.5;
}

.disabled-section .radio-label {
  cursor: not-allowed;
}

.section-note {
  font-size: 0.75rem;
  color: #64748b;
  font-style: italic;
  margin-top: 8px;
}

/* ── Raster legend ─────────────────────────────────────────────────────────── */
.legend {
  display: flex;
  gap: 12px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid #334155;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  color: #cbd5e1;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

/* ── Polygon popup ─────────────────────────────────────────────────────────── */
.polygon-popup h3 {
  margin: 0 0 6px;
  font-size: 0.95rem;
  color: #1e293b;
}

.polygon-popup table {
  border-collapse: collapse;
  font-size: 0.82rem;
  width: 100%;
}

.polygon-popup td {
  padding: 2px 6px;
  border-bottom: 1px solid #e2e8f0;
  vertical-align: top;
}

.polygon-popup td:first-child {
  white-space: nowrap;
  color: #475569;
}

/* ── Zonal-stats chart popup ───────────────────────────────────────────────── */
.chart-popup {
  text-align: center;
}

.chart-popup h3 {
  margin: 0 0 2px;
  font-size: 0.95rem;
  color: #1e293b;
}

.chart-popup .chart-subtitle {
  font-size: 0.78rem;
  color: #64748b;
  margin: 0 0 8px;
}

.chart-popup canvas {
  display: block;
  margin: 0 auto 8px;
}

.chart-loading,
.chart-error {
  padding: 16px;
  font-size: 0.85rem;
  color: #475569;
}

.chart-error {
  color: #dc2626;
}

.chart-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  text-align: left;
}

.chart-table td {
  padding: 3px 6px;
  border-bottom: 1px solid #e2e8f0;
}

.chart-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.chart-table .total-row td {
  border-top: 2px solid #94a3b8;
  border-bottom: none;
}

.chart-table .swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: middle;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

/* ── Checkbox label (district overlay) ──────────────────────────────────────── */
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 0.9rem;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  accent-color: #fb8c00;
  width: 16px;
  height: 16px;
}

/* ── District statistics bottom panel ──────────────────────────────────────── */
.district-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 55vh;
  background: #1e293b;
  color: #f1f5f9;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.4);
  z-index: 1200;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto;
  display: none;
  flex-direction: column;
}

.district-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid #334155;
  flex-shrink: 0;
}

.district-panel-header h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
}

.district-panel-close {
  background: none;
  border: 1px solid #475569;
  color: #f1f5f9;
  width: 28px;
  height: 28px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.district-panel-close:hover {
  background: #334155;
}

.district-panel-body {
  padding: 16px 20px;
  flex: 1;
  overflow-y: auto;
}

/* ── Progress bar ──────────────────────────────────────────────────────────── */
.progress-bar {
  width: 100%;
  height: 28px;
  background: #334155;
  border-radius: 6px;
  position: relative;
  overflow: hidden;
  margin-bottom: 14px;
}

.progress-bar.hidden {
  display: none;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #fb8c00, #ffb74d);
  border-radius: 6px;
  width: 0%;
  transition: width 1.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.78rem;
  color: #f1f5f9;
  white-space: nowrap;
}

/* ── District message (error / info) ───────────────────────────────────────── */
.district-message {
  text-align: center;
  padding: 20px;
  font-size: 0.9rem;
  color: #94a3b8;
}

.district-message.hidden {
  display: none;
}

.district-message strong {
  color: #f1f5f9;
}

/* ── District chart + table ────────────────────────────────────────────────── */
.district-content.hidden {
  display: none;
}

.district-chart-wrapper {
  width: 100%;
  height: 260px;
  margin-bottom: 16px;
}

.district-stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  color: #e2e8f0;
}

.district-stats-table th {
  padding: 6px 10px;
  text-align: left;
  font-weight: 600;
  color: #94a3b8;
  border-bottom: 2px solid #475569;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.district-stats-table th.num {
  text-align: right;
}

.district-stats-table td {
  padding: 5px 10px;
  border-bottom: 1px solid #334155;
}

.district-stats-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.district-stats-table .total-row td {
  border-top: 2px solid #94a3b8;
  border-bottom: none;
  color: #f1f5f9;
}

/* ── CSV download button ────────────────────────────────────────────────────── */
.district-csv-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding: 7px 16px;
  background: #334155;
  color: #f1f5f9;
  border: 1px solid #475569;
  border-radius: 5px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.2s;
}

.district-csv-btn:hover {
  background: #475569;
}

/* ── Responsive: narrow viewport ───────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    margin-left: -280px;   /* start collapsed on mobile */
  }

  .sidebar.open-mobile {
    margin-left: 0;
  }

  .sidebar-open-btn {
    display: flex;          /* always show open button on mobile */
  }

  /* When sidebar is open on mobile, dim the map */
  .sidebar.open-mobile ~ #map {
    opacity: 0.4;
    pointer-events: none;
  }

  .district-panel {
    max-height: 70vh;
  }

  .district-chart-wrapper {
    height: 200px;
  }
}