/* ═══════════════════════════════════════════════
   Power Grid Fragility — Blackout Vulnerability Index
   Style Sheet
═══════════════════════════════════════════════ */

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

:root {
  --bg:       #0d1117;
  --surface:  #161b22;
  --surface2: #21262d;
  --border:   #30363d;
  --text:     #e6edf3;
  --text-muted: #8b949e;
  --accent:   #f0883e;
  --accent2:  #58a6ff;

  --critical: #67000d;
  --high:     #d73027;
  --moderate: #fc8d59;
  --low:      #fee090;

  --panel-w: 300px;
  --header-h: 56px;
}

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

/* ── Header ── */
header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  display: flex;
  align-items: center;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0 20px;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  line-height: 1;
}

.header-title h1 {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.header-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.header-nav {
  display: flex;
  gap: 4px;
}

.nav-btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  transition: all .15s;
}

.nav-btn:hover { background: var(--surface2); color: var(--text); }
.nav-btn.active { background: var(--accent); border-color: var(--accent); color: #000; font-weight: 600; }

/* ── Main layout ── */
main {
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0; bottom: 0;
}

.view { display: none; height: 100%; }
.view.active { display: flex; flex-direction: column; }

.map-layout {
  display: flex;
  height: 100%;
}

/* ── Side panel ── */
.side-panel {
  width: var(--panel-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.side-panel::-webkit-scrollbar { width: 4px; }
.side-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.panel-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.panel-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--accent);
}

.panel-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ── Search ── */
.search-wrap { position: relative; }

#county-search {
  width: 100%;
  padding: 7px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}

#county-search:focus { border-color: var(--accent2); }

.search-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  z-index: 200;
  max-height: 200px;
  overflow-y: auto;
  display: none;
}

.search-dropdown.visible { display: block; }

.search-result {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}

.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--surface); color: var(--accent2); }

.search-result .res-state {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
}

/* ── Layer buttons ── */
.layer-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.layer-btn {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 12px;
  transition: all .15s;
}

.layer-btn:hover { background: var(--surface2); color: var(--text); }
.layer-btn.active { background: var(--accent2); border-color: var(--accent2); color: #000; font-weight: 600; }

/* ── Legend ── */
.legend { display: flex; flex-direction: column; gap: 5px; }

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

.swatch {
  width: 16px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* ── County card ── */
.county-card {
  margin: 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  transition: opacity .2s;
}

.county-card.hidden { display: none; }

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 8px;
}

.card-header h3, .card-county-name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
}

.tier-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

.tier-Critical { background: var(--critical); color: #fff; }
.tier-High     { background: var(--high); color: #fff; }
.tier-Moderate { background: var(--moderate); color: #000; }
.tier-Low      { background: var(--low); color: #000; }

.bvi-display {
  text-align: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}

.bvi-number {
  font-size: 36px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.bvi-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.bvi-rank {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ── Component bars ── */
.card-components { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }

.component-bar {
  display: grid;
  grid-template-columns: 100px 1fr 40px;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

.bar-track {
  height: 6px;
  background: var(--surface);
  border-radius: 3px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .4s ease;
}

.grid-bar    { background: #58a6ff; }
.hazard-bar  { background: #f0883e; }
.fema-bar    { background: #bc8cff; }
.elderly-bar { background: #3fb950; }
.poverty-bar { background: #ffa657; }

.bar-val { font-size: 11px; color: var(--text); text-align: right; }

/* ── Card stats ── */
.card-stats {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.stat { flex: 1; text-align: center; }
.stat-label { display: block; font-size: 10px; color: var(--text-muted); }
.stat-val   { display: block; font-size: 13px; font-weight: 600; color: var(--text); }

/* ── Summary stats ── */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.summary-stat {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  text-align: center;
}

.ss-num { font-size: 20px; font-weight: 700; color: var(--accent); }
.ss-lab { font-size: 11px; color: var(--text-muted); }

/* ── Map container ── */
.map-container {
  flex: 1;
  position: relative;
}

/* ── Map tooltip ── */
.map-tooltip {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  pointer-events: none;
  font-size: 12px;
  z-index: 500;
  max-width: 200px;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
}

.map-tooltip.hidden { display: none; }

.tooltip-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.tooltip-bvi  { color: var(--accent); font-size: 18px; font-weight: 700; }
.tooltip-tier { font-size: 11px; color: var(--text-muted); }

/* ── Texas toggles ── */
.toggle-list { display: flex; flex-direction: column; gap: 6px; }

.toggle-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
}

.toggle-item input { accent-color: var(--accent); cursor: pointer; }

/* ── Plant legend ── */
.plant-legend { display: flex; flex-direction: column; gap: 5px; }
.plant-legend-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-muted); }
.plant-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }

/* ── Rankings ── */
.rankings-layout {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 20px 24px;
  overflow: hidden;
}

.rankings-header h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 6px;
}

.rankings-header p {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 14px;
  max-width: 700px;
}

.filter-row {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.filter-row input,
.filter-row select {
  padding: 7px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}

.filter-row input { flex: 1; min-width: 200px; }
.filter-row input:focus,
.filter-row select:focus { border-color: var(--accent2); }

.table-wrap {
  flex: 1;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.table-wrap::-webkit-scrollbar { width: 6px; height: 6px; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

#rankings-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

#rankings-table thead {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
}

#rankings-table th {
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

#rankings-table th:hover { color: var(--text); }

#rankings-table td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

#rankings-table tbody tr:hover { background: var(--surface2); cursor: pointer; }

.rank-num { font-weight: 700; color: var(--text-muted); }
.bvi-score { font-weight: 700; color: var(--accent); font-size: 14px; }

/* ── Footer ── */
footer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 20px;
  z-index: 100;
}

.footer-inner {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
}

/* ── Mapbox overrides ── */
.maplibregl-popup-content {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
  padding: 10px 14px !important;
  box-shadow: 0 4px 16px rgba(0,0,0,.4) !important;
}

.maplibregl-popup-tip { display: none; }

.maplibregl-ctrl-group {
  background: var(--surface) !important;
  border: 1px solid var(--border) !important;
}

.maplibregl-ctrl button { background-color: var(--surface2) !important; }
.maplibregl-ctrl button span { filter: invert(1); }

/* ── Loading overlay ── */
#loading {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 1000;
  font-size: 15px;
  color: var(--text-muted);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ── */
@media (max-width: 700px) {
  :root { --panel-w: 100%; }
  .map-layout { flex-direction: column; }
  .side-panel { width: 100%; max-height: 240px; border-right: none; border-bottom: 1px solid var(--border); }
  .header-title h1 { font-size: 14px; }
  .header-sub { display: none; }
  .rankings-layout { padding: 12px; }
}
