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

:root {
  --critical: #d32f2f;
  --high:     #f57c00;
  --elevated: #fbc02d;
  --moderate: #388e3c;
  --bg:       #0d1117;
  --bg2:      #161b22;
  --bg3:      #21262d;
  --border:   #30363d;
  --text:     #e6edf3;
  --text2:    #8b949e;
  --accent:   #58a6ff;
  --water:    #1e88e5;
  --header-h: 64px;
  --banner-h: 72px;
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 14px; color: var(--text); background: var(--bg); overflow: hidden; }

/* ── Header ─────────────────────────────────────────────────────────────── */
header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000;
  height: var(--header-h); background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
}
.header-inner {
  width: 100%; max-width: 1800px; margin: 0 auto;
  padding: 0 20px;
  display: flex; align-items: center; justify-content: space-between;
}
.header-title { display: flex; align-items: center; gap: 12px; }
.header-icon  { font-size: 28px; color: var(--water); line-height: 1; }
h1 { font-size: 18px; font-weight: 700; color: var(--text); }
.subtitle { font-size: 11px; color: var(--text2); margin-top: 2px; }
.header-nav { display: flex; gap: 4px; }
.nav-btn {
  padding: 6px 16px; border-radius: 6px; border: 1px solid var(--border);
  background: transparent; color: var(--text2); cursor: pointer; font-size: 13px;
  transition: all 0.15s;
}
.nav-btn:hover  { background: var(--bg3); color: var(--text); }
.nav-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ── Stat Banner ─────────────────────────────────────────────────────────── */
.stat-banner {
  position: fixed; top: var(--header-h); left: 0; right: 0; z-index: 999;
  height: var(--banner-h); background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; padding: 0 20px; gap: 8px; overflow-x: auto;
}
.stat-card {
  flex: 1; min-width: 130px; text-align: center;
  padding: 8px 12px; border-radius: 8px;
  background: var(--bg3); border: 1px solid var(--border);
}
.stat-card.critical  { border-color: var(--critical); }
.stat-card.high      { border-color: var(--high); }
.stat-card.elevated  { border-color: var(--elevated); }
.stat-card.water     { border-color: var(--water); }
.stat-number { font-size: 22px; font-weight: 700; color: var(--text); line-height: 1.1; }
.stat-card.critical  .stat-number { color: var(--critical); }
.stat-card.high      .stat-number { color: var(--high); }
.stat-card.elevated  .stat-number { color: var(--elevated); }
.stat-card.water     .stat-number { color: var(--water); }
.stat-label  { font-size: 10px; color: var(--text2); margin-top: 3px; }

/* ── Main & Panels ───────────────────────────────────────────────────────── */
main {
  position: fixed;
  top: calc(var(--header-h) + var(--banner-h));
  left: 0; right: 0; bottom: 32px;
  overflow: hidden;
}
.panel { display: none; height: 100%; }
.panel.active { display: flex; }

/* ── MAP PANEL ───────────────────────────────────────────────────────────── */
.map-layout { display: flex; height: 100%; width: 100%; }

.map-sidebar {
  width: 270px; min-width: 270px;
  background: var(--bg2); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 12px;
  display: flex; flex-direction: column; gap: 0;
}
.sidebar-section {
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.sidebar-section:last-child { border-bottom: none; }
.sidebar-section h3 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text2); margin-bottom: 8px; }

#map { flex: 1; background: #0d1117; }

/* Legend */
.legend { display: flex; flex-direction: column; gap: 6px; }
.legend-item { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 6px; border-radius: 4px; transition: background 0.1s; }
.legend-item:hover { background: var(--bg3); }
.legend-item input { accent-color: var(--accent); cursor: pointer; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.legend-item small { color: var(--text2); margin-left: auto; font-size: 11px; }

select, input[type=text] {
  width: 100%; background: var(--bg3); border: 1px solid var(--border);
  color: var(--text); padding: 6px 8px; border-radius: 4px; font-size: 13px;
  outline: none; transition: border-color 0.15s;
}
select:focus, input[type=text]:focus { border-color: var(--accent); }

/* Load status */
.load-status { display: flex; align-items: center; gap: 8px; color: var(--text2); font-size: 12px; }
.spinner {
  width: 14px; height: 14px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.load-status.done .spinner { display: none; }
.load-status.done { color: #3fb950; }

/* Dam detail */
#detail-content { font-size: 12px; color: var(--text2); line-height: 1.7; }
#detail-content strong { color: var(--text); }
.detail-risk { font-size: 20px; font-weight: 700; }
.detail-row { display: flex; justify-content: space-between; padding: 2px 0; border-bottom: 1px solid var(--border); }
.detail-row:last-child { border-bottom: none; }

/* Leaflet overrides */
.leaflet-popup-content-wrapper {
  background: var(--bg2) !important; color: var(--text) !important;
  border: 1px solid var(--border) !important; border-radius: 8px !important;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5) !important;
}
.leaflet-popup-tip { background: var(--bg2) !important; }
.leaflet-popup-content { margin: 12px 16px !important; font-size: 13px !important; line-height: 1.6; }

/* Cluster */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background: rgba(30,136,229,0.2) !important;
}
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background: rgba(30,136,229,0.5) !important; color: #fff !important;
  font-weight: 700;
}

/* ── RANKINGS PANEL ──────────────────────────────────────────────────────── */
#panel-rankings { flex-direction: column; padding: 20px; overflow-y: auto; }
.rankings-layout { width: 100%; max-width: 960px; margin: 0 auto; }
.rankings-controls { margin-bottom: 20px; }
.rankings-controls h2 { font-size: 20px; margin-bottom: 12px; }
.control-row { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 12px; }
.control-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text2); min-width: 180px; }
.control-row select { width: auto; }
#rankings-chart { width: 100%; }
.bar-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 12px; }
.bar-label { width: 70px; text-align: right; color: var(--text2); flex-shrink: 0; font-size: 11px; }
.bar-track { flex: 1; height: 26px; background: var(--bg3); border-radius: 3px; overflow: hidden; position: relative; }
.bar-fill  { height: 100%; border-radius: 3px; transition: width 0.4s ease; }
.bar-value { width: 60px; color: var(--text); flex-shrink: 0; font-weight: 600; }
.bar-sub   { width: 100px; color: var(--text2); font-size: 11px; }

/* ── SEARCH PANEL ────────────────────────────────────────────────────────── */
#panel-search { flex-direction: column; padding: 20px; overflow-y: auto; }
.search-layout { width: 100%; max-width: 960px; margin: 0 auto; }
.search-layout h2 { font-size: 20px; margin-bottom: 16px; }
.search-controls { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.search-controls input { flex: 1; min-width: 220px; }
.search-controls select { width: 160px; }
button#search-btn, button#top-risk-btn {
  padding: 6px 20px; background: var(--accent); border: none;
  color: #fff; border-radius: 4px; cursor: pointer; font-size: 13px;
  font-weight: 600; transition: opacity 0.15s;
}
button#search-btn:hover, button#top-risk-btn:hover { opacity: 0.85; }
button#top-risk-btn { background: var(--critical); margin-left: 4px; }
#search-status { color: var(--text2); font-size: 12px; margin-bottom: 12px; min-height: 20px; }

.result-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.result-table th { background: var(--bg3); color: var(--text2); padding: 8px 10px; text-align: left; font-weight: 600; border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 1; }
.result-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.result-table tr:hover td { background: var(--bg3); cursor: pointer; }
.tier-badge {
  display: inline-block; padding: 2px 8px; border-radius: 3px;
  font-size: 11px; font-weight: 700; color: #fff;
}
.tier-Critical { background: var(--critical); }
.tier-High     { background: var(--high); }
.tier-Elevated { background: var(--elevated); color: #000; }
.tier-Moderate { background: var(--moderate); }

/* ── ABOUT PANEL ─────────────────────────────────────────────────────────── */
#panel-about { padding: 20px; overflow-y: auto; }
.about-layout { width: 100%; max-width: 800px; margin: 0 auto; }
.about-layout h2 { font-size: 22px; margin-bottom: 20px; }
.about-content h3 { font-size: 15px; color: var(--accent); margin: 20px 0 8px; }
.about-content p  { color: var(--text2); line-height: 1.7; margin-bottom: 10px; }
.about-content ul { color: var(--text2); line-height: 1.7; padding-left: 20px; }
.about-content ul li { margin-bottom: 6px; }
.about-content a { color: var(--accent); }
.method-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.method-table th { background: var(--bg3); color: var(--text2); padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 12px; }
.method-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); color: var(--text2); font-size: 13px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 3px; font-size: 11px; font-weight: 700; color: #fff; }
.badge.critical { background: var(--critical); }
.badge.high     { background: var(--high); }
.badge.elevated { background: var(--elevated); color: #000; }
.badge.moderate { background: var(--moderate); }

/* ── Condition donut chart area ─────────────────────────────────────────── */
#condition-chart { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.cond-row { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.cond-label { width: 110px; color: var(--text2); }
.cond-bar-track { flex: 1; height: 14px; background: var(--bg3); border-radius: 3px; overflow: hidden; }
.cond-bar-fill { height: 100%; border-radius: 3px; }
.cond-count { width: 55px; text-align: right; color: var(--text); font-weight: 600; font-size: 11px; }

/* ── Footer ─────────────────────────────────────────────────────────────── */
footer {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--bg2); border-top: 1px solid var(--border);
  height: 32px; z-index: 900;
}
.footer-inner {
  height: 100%; display: flex; align-items: center; gap: 10px;
  padding: 0 20px; font-size: 11px; color: var(--text2);
}
.footer-inner a { color: var(--accent); text-decoration: none; }

/* ── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
  .map-sidebar { width: 200px; min-width: 160px; }
  .stat-banner { flex-wrap: nowrap; overflow-x: scroll; }
  .stat-card   { min-width: 110px; }
  h1 { font-size: 14px; }
  .header-nav { display: none; }
}
