:root {
  --bg-primary: #0f1419;
  --bg-secondary: #1a2332;
  --bg-card: #1e2a3a;
  --bg-card-hover: #243447;
  --border: #2a3a4a;
  --border-light: #3a4a5a;
  --text-primary: #e8edf5;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent-blue: #3b82f6;
  --accent-blue-dim: rgba(59,130,246,0.15);
  --accent-red: #ef4444;
  --accent-red-dim: rgba(239,68,68,0.15);
  --accent-green: #10b981;
  --accent-green-dim: rgba(16,185,129,0.15);
  --accent-amber: #f59e0b;
  --accent-amber-dim: rgba(245,158,11,0.15);
  --accent-purple: #8b5cf6;
  --accent-purple-dim: rgba(139,92,246,0.15);
  --accent-neutral: #a1a1aa;
  --font-main: system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", Consolas, monospace;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.3);
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.2);
  --header-h: 56px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: var(--font-main); background: var(--bg-primary); color: var(--text-primary); height: 100vh; overflow: hidden; display: flex; flex-direction: column; }

/* --- Header --- */
.header {
  height: var(--header-h);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 1000;
}
.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.header-brand .logo {
  width: 42px;
  height: 32px;
  background: linear-gradient(135deg, var(--accent-blue) 0%, var(--accent-purple) 100%);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.5px;
  color: #fff;
}
.header-brand h1 {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.3px;
}
.header-brand .subtitle {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
}
.header-stats {
  display: flex;
  gap: 20px;
  margin-left: auto;
  align-items: center;
}
.header-stat {
  text-align: center;
}
.header-stat .value {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
}
.header-stat .label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.header-help-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.header-help-btn:hover { background: var(--accent-blue); color: #fff; border-color: var(--accent-blue); }

/* --- Layout --- */
.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* --- Sidebar --- */
.sidebar {
  width: 320px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}
.sidebar-section {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-section:last-child { border-bottom: none; }
.sidebar-section h3 {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.sidebar-group-label {
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--accent-blue);
  padding: 12px 16px 4px;
  border-bottom: none;
}
.sidebar-divider {
  height: 3px;
  background: linear-gradient(to right, var(--accent-blue), var(--accent-purple));
  margin: 0 16px;
  border-radius: 2px;
  opacity: 0.5;
}

/* Filter buttons */
.filter-buttons {
  display: flex;
  gap: 6px;
}
.filter-btn {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-family: var(--font-main);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
}
.filter-btn:hover { background: var(--bg-card-hover); color: var(--text-primary); }
.filter-btn.active {
  background: var(--accent-blue);
  color: #fff;
  border-color: var(--accent-blue);
  box-shadow: 0 0 12px rgba(59,130,246,0.3);
}

/* Dropdowns */
.select-wrapper {
  position: relative;
  margin-bottom: 8px;
}
.select-wrapper select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text-primary);
  font-family: var(--font-main);
  font-size: 13px;
  appearance: none;
  cursor: pointer;
  transition: border-color 0.15s;
}
.select-wrapper select:hover { border-color: var(--border-light); }
.select-wrapper select:focus { outline: none; border-color: var(--accent-blue); box-shadow: 0 0 0 2px var(--accent-blue-dim); }
.select-wrapper::after {
  content: "\25BC";
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  color: var(--text-muted);
  pointer-events: none;
}

/* Stat cards */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.15s ease;
}
.stat-card:hover { background: var(--bg-card-hover); }
.stat-card .indicator {
  width: 4px;
  height: 32px;
  border-radius: 2px;
  flex-shrink: 0;
}
.stat-card .indicator.la-nina { background: var(--accent-red); box-shadow: 0 0 8px var(--accent-red-dim); }
.stat-card .indicator.neutral { background: var(--accent-neutral); }
.stat-card .indicator.el-nino { background: var(--accent-blue); box-shadow: 0 0 8px var(--accent-blue-dim); }
.stat-card .content { flex: 1; min-width: 0; }
.stat-card .phase-name {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.stat-card .stat-value {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  margin-top: 2px;
}
.stat-card .stat-detail {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
}
.stat-positive { color: var(--accent-green); }
.stat-negative { color: var(--accent-red); }
.stat-neutral-val { color: var(--text-secondary); }

/* Info badge */
.info-text {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.5;
  padding: 8px 0;
}
.info-text a { color: var(--accent-blue); text-decoration: none; }
.info-text a:hover { text-decoration: underline; }

/* --- Main content (map + legend + table) --- */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.map-container {
  flex: 1;
  position: relative;
  min-height: 0;
}
#map {
  width: 100%;
  height: 100%;
  z-index: 1;
}
.leaflet-container { background: var(--bg-primary); }

/* Below-map area */
.below-map {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding: 12px 20px;
  overflow-y: auto;
  max-height: 280px;
}
.legend-bar-section {
  margin-bottom: 12px;
}
.legend-title { font-size: 11px; font-weight: 500; color: var(--text-secondary); margin-bottom: 6px; }
.legend-gradient {
  display: flex;
  height: 18px;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 4px;
}
.legend-gradient div { flex: 1; }
.legend-labels {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
}
.detail-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 8px 0 6px;
}
.data-table-wrap {
  max-height: 180px;
  overflow-y: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}
.data-table th {
  background: var(--bg-card);
  padding: 7px 10px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  position: sticky;
  top: 0;
  z-index: 1;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.data-table td {
  padding: 6px 10px;
  border-top: 1px solid var(--border);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 11px;
}
.data-table tr:hover td { background: var(--bg-card); }
.phase-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 9px;
  font-weight: 600;
  font-family: var(--font-main);
}
.phase-badge.la-nina { background: var(--accent-red-dim); color: var(--accent-red); }
.phase-badge.neutral { background: rgba(161,161,170,0.15); color: var(--accent-neutral); }
.phase-badge.el-nino { background: var(--accent-blue-dim); color: var(--accent-blue); }

/* Map overlay controls */
.map-overlay {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.map-control-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.15s;
  box-shadow: var(--shadow-sm);
}
.map-control-btn:hover { background: var(--bg-card-hover); color: var(--text-primary); }

/* Layer switcher */
.layer-panel {
  position: absolute;
  top: 12px;
  right: 56px;
  z-index: 500;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: none;
  box-shadow: var(--shadow);
  min-width: 140px;
}
.layer-panel.show { display: block; }
.layer-panel h4 {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.layer-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-secondary);
}
.layer-option:hover { color: var(--text-primary); }
.layer-option input { accent-color: var(--accent-blue); }

/* Map info card (hover) */
.map-info-card {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 500;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow);
  max-width: 280px;
}
.map-info-card .title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.map-info-card .detail {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.5;
}
.map-info-card .detail .highlight {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text-primary);
}

/* --- Quick Guide Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.2s;
}
.modal-overlay.hidden { display: none; opacity: 0; }
.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 580px;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  padding: 28px 32px;
}
.modal-close {
  float: right;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.modal-close:hover { background: var(--accent-red); color: #fff; border-color: var(--accent-red); }
.modal h2 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 4px;
  color: var(--text-primary);
}
.modal .modal-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 20px;
}
.modal-features {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}
.modal-feature {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  transition: all 0.15s;
}
.modal-feature:hover { background: var(--bg-card-hover); border-color: var(--border-light); }
.modal-feature .icon {
  font-size: 20px;
  margin-bottom: 6px;
}
.modal-feature .feat-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.modal-feature .feat-desc {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* Leaflet overrides */
.leaflet-control-zoom a {
  background: var(--bg-card) !important;
  color: var(--text-primary) !important;
  border-color: var(--border) !important;
}
.leaflet-control-attribution {
  background: rgba(15,20,25,0.8) !important;
  color: var(--text-muted) !important;
  font-size: 9px !important;
}
.leaflet-control-attribution a { color: var(--text-secondary) !important; }
.leaflet-tooltip {
  background: var(--bg-card) !important;
  border: 1px solid var(--border) !important;
  color: var(--text-primary) !important;
  font-family: var(--font-main) !important;
  font-size: 12px !important;
  border-radius: var(--radius) !important;
  box-shadow: var(--shadow) !important;
  padding: 8px 12px !important;
}
.leaflet-tooltip::before { border-top-color: var(--border) !important; }

.leaflet-container { background: #0d1b2a; }
.layer-note { color: var(--text-muted); font-size: 11px; margin-top: 10px; }
.mode-buttons { flex-wrap: wrap; }
.mode-buttons .filter-btn { flex: 1 1 45%; }
#map-hover-card, #detail-section { display: none; }
.empty-state { font-size: 12px; color: var(--text-muted); padding: 8px; }
.phase-mean {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 8px;
  font-family: var(--font-mono);
}

@media (max-width: 800px) {
  body {
    display: block;
    width: 100%;
    height: auto;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .header {
    height: auto;
    min-height: var(--header-h);
    flex-wrap: wrap;
    padding: 10px 14px;
    gap: 8px;
  }
  .header-brand { width: 100%; min-width: 0; }
  .header-brand h1 { font-size: 15px; }
  .header-stats {
    width: 100%;
    margin-left: 0;
    justify-content: space-between;
    gap: 4px;
  }
  .header-stat { min-width: 0; flex: 1 1 0; }
  .header-stat .value { font-size: 12px; }
  .header-stat .label { font-size: 8px; letter-spacing: 0; }
  .header-help-btn { flex-shrink: 0; }
  .app-body {
    display: grid;
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    overflow: visible;
  }
  .sidebar, .main-content { display: contents; }
  .sidebar-group-label {
    grid-column: 1 / -1;
    background: var(--bg-secondary);
    padding: 10px 12px 4px;
  }
  .sidebar-section {
    min-width: 0;
    background: var(--bg-secondary);
    padding: 10px 12px;
  }
  .sidebar-divider { display: none; }
  .filters-label { order: 1; }
  .view-section { order: 2; }
  .crop-section { order: 3; }
  .mode-section { order: 4; grid-column: 1 / -1; }
  .selection-label { order: 5; }
  .location-section {
    order: 6;
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0 8px;
  }
  .location-section h3 { grid-column: 1 / -1; }
  .location-section .select-wrapper { min-width: 0; margin-bottom: 0; }
  .map-container {
    order: 7;
    grid-column: 1 / -1;
    height: min(60vh, 460px);
    min-height: 340px;
    flex: none;
  }
  .below-map {
    order: 8;
    grid-column: 1 / -1;
    max-height: none;
    overflow: visible;
    padding: 14px;
  }
  .phase-section { order: 9; grid-column: 1 / -1; }
  .about-section { order: 10; grid-column: 1 / -1; }
  .data-table-wrap { max-height: none; overflow-x: auto; overflow-y: visible; }
  .data-table { min-width: 520px; }
  .leaflet-control-attribution {
    max-width: calc(100vw - 8px);
    white-space: normal;
    font-size: 8px;
  }
  .layer-panel { max-width: calc(100vw - 80px); }
  .modal {
    width: calc(100% - 24px);
    max-width: none;
    max-height: calc(100vh - 24px);
    padding: 18px;
  }
  .modal-features { grid-template-columns: 1fr; }
}
