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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: #f5f6f8;
  color: #222;
  overflow-x: hidden;
}

/* ── Header / Controls ────────────────────────────────────────────────── */
#controls {
  background: #fff;
  border-bottom: 1px solid #ddd;
  padding: 10px 20px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

#controls h1 {
  font-size: 1.15rem;
  font-weight: 600;
}

.methodology-btn {
  padding: 5px 14px;
  border: 1px solid #3a7bd5;
  border-radius: 4px;
  background: #fff;
  color: #3a7bd5;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.methodology-btn:hover {
  background: #3a7bd5;
  color: #fff;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.metric-tabs {
  display: flex;
  gap: 4px;
}

.metric-tabs .tab {
  padding: 5px 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #f0f0f0;
  cursor: pointer;
  font-size: 0.82rem;
  transition: background 0.15s, border-color 0.15s;
}

.metric-tabs .tab:hover { background: #e0e0e0; }

.metric-tabs .tab.active {
  background: #3a7bd5;
  color: #fff;
  border-color: #3a7bd5;
}

.cumulative-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.82rem;
  cursor: pointer;
}

.cumulative-toggle input { cursor: pointer; }

/* ── Period Toggle ──────────────────────────────────────────────────── */
.period-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 2px;
}

.period-row.hidden,
.slider-row.hidden,
.strata-info.hidden {
  display: none;
}

.metric-info {
  background: #f6f7f9;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 4px 10px;
  margin-bottom: 2px;
}

.metric-info.hidden {
  display: none;
}

.metric-info-text {
  font-size: 0.75rem;
  color: #555;
  line-height: 1.5;
}

.strata-info {
  background: #f6f7f9;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 4px 10px;
  margin-bottom: 2px;
}

.strata-info-text {
  font-size: 0.75rem;
  color: #555;
  line-height: 1.5;
}

.period-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #555;
}

.period-toggle {
  display: flex;
  gap: 4px;
}

.period-btn {
  padding: 5px 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #f0f0f0;
  cursor: pointer;
  font-size: 0.82rem;
  transition: background 0.15s, border-color 0.15s;
}

.period-btn:hover { background: #e0e0e0; }
.period-btn.hidden { display: none; }

.period-btn.active {
  background: #0e8a7d;
  color: #fff;
  border-color: #0e8a7d;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

#play-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #f0f0f0;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
#play-btn:hover { background: #e0e0e0; }
#play-btn.playing { background: #3a7bd5; color: #fff; border-color: #3a7bd5; }

#year-slider {
  flex: 1;
  max-width: 500px;
  cursor: pointer;
}

.year-bound {
  font-size: 0.82rem;
  color: #888;
  min-width: 32px;
  text-align: center;
}

#year-label {
  font-weight: 700;
  font-size: 1.1rem;
  min-width: 42px;
  text-align: center;
}

/* ── Metric Description ───────────────────────────────────────────────── */
.metric-description {
  padding: 6px 20px 6px;
  font-size: 0.82rem;
  color: #555;
  line-height: 1.5;
  background: #fff;
  border-bottom: 1px solid #eee;
}

/* ── Main Layout ──────────────────────────────────────────────────────── */
#app {
  display: flex;
  height: calc(100vh - 110px);
}

#map-container {
  flex: 1;
  position: relative;
  min-width: 0;
  overflow: hidden;
}

#map svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* ── Detail Panel ─────────────────────────────────────────────────────── */
#detail-panel {
  width: 320px;
  min-width: 320px;
  background: #fff;
  border-left: 1px solid #ddd;
  overflow-y: auto;
  padding: 16px;
  position: relative;
  transition: margin-right 0.25s ease;
}

#detail-panel.collapsed {
  width: 0;
  min-width: 0;
  padding: 0;
  border: none;
  overflow: hidden;
}

#panel-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: #888;
  line-height: 1;
}
#panel-close:hover { color: #333; }

.panel-hint {
  color: #999;
  font-style: italic;
  font-size: 0.9rem;
  margin-top: 30px;
}

.panel-county-name {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 2px;
}

.panel-state {
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 10px;
}

.panel-section {
  margin-bottom: 14px;
}

.panel-section h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #888;
  margin-bottom: 4px;
  border-bottom: 1px solid #eee;
  padding-bottom: 2px;
}

.panel-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px 12px;
  font-size: 0.82rem;
}

.panel-grid dt { color: #666; }
.panel-grid dd { font-weight: 600; text-align: right; }

.mini-timeline {
  width: 100%;
  height: 86px;
}

.mini-timeline .bar { fill: #3a7bd5; cursor: pointer; }
.mini-timeline .bar:hover { fill: #ff7f0e; }
.mini-timeline .click-label { pointer-events: none; }
.mini-timeline text { font-size: 9px; fill: #888; }
.mini-timeline .axis line,
.mini-timeline .axis path { stroke: #ccc; }

.event-list {
  list-style: none;
  font-size: 0.8rem;
  max-height: 200px;
  overflow-y: auto;
}

.event-list li {
  padding: 2px 0;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
}

.event-list .evt-year { font-weight: 600; min-width: 36px; }
.event-list .evt-type { color: #555; }

/* ── Legend ────────────────────────────────────────────────────────────── */
#legend {
  position: absolute;
  bottom: 16px;
  left: 16px;
  background: rgba(255,255,255,0.93);
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 0.75rem;
  pointer-events: none;
  max-width: 220px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.legend-title {
  font-weight: 700;
  margin-bottom: 6px;
  font-size: 0.8rem;
}

.legend-gradient {
  width: 160px;
  height: 12px;
  border: 1px solid #ccc;
  border-radius: 2px;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 2px;
  font-size: 0.7rem;
  color: #666;
}

.legend-items { list-style: none; }

.legend-items li {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 2px 0;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(0,0,0,0.15);
  flex-shrink: 0;
}

/* ── Tooltip ──────────────────────────────────────────────────────────── */
#tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(255,255,255,0.96);
  border: 1px solid #bbb;
  border-radius: 5px;
  padding: 8px 10px;
  font-size: 0.78rem;
  line-height: 1.4;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  z-index: 200;
  max-width: 240px;
}

#tooltip.hidden { display: none; }

.tt-name { font-weight: 700; margin-bottom: 2px; }
.tt-state { color: #666; font-size: 0.72rem; }
.tt-metric { margin-top: 4px; }
.tt-metric span { font-weight: 600; }
.tt-pop { color: #888; font-size: 0.72rem; }

/* ── County paths ─────────────────────────────────────────────────────── */
.county {
  stroke: #fff;
  stroke-width: 0.3;
  cursor: pointer;
  transition: fill 0.2s ease;
}

.county:hover {
  stroke: #333;
  stroke-width: 1.2;
}

.county.selected {
  stroke: #000;
  stroke-width: 1.8;
}

.state-border {
  fill: none;
  stroke: #999;
  stroke-width: 0.6;
  pointer-events: none;
}

/* ── Methodology Overlay ──────────────────────────────────────────────── */
.methodology-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 20px;
}

.methodology-overlay.hidden { display: none; }

.methodology-panel {
  background: #fff;
  border-radius: 10px;
  max-width: 760px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 32px 36px;
  position: relative;
  box-shadow: 0 8px 30px rgba(0,0,0,0.18);
  line-height: 1.6;
}

.methodology-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: #888;
  line-height: 1;
}
.methodology-close:hover { color: #333; }

.methodology-panel h2 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 20px;
  padding-bottom: 8px;
  border-bottom: 2px solid #3a7bd5;
}

.methodology-panel section {
  margin-bottom: 24px;
}

.methodology-panel h3 {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: 8px;
  color: #333;
}

.methodology-panel h4 {
  font-size: 0.9rem;
  font-weight: 600;
  margin: 10px 0 4px;
  color: #555;
}

.methodology-panel p {
  font-size: 0.88rem;
  margin-bottom: 8px;
  color: #444;
}

.method-dl {
  margin: 8px 0 12px;
}

.method-dl dt {
  font-weight: 600;
  font-size: 0.88rem;
  margin-top: 8px;
  color: #333;
}

.method-dl dd {
  font-size: 0.85rem;
  color: #555;
  margin: 2px 0 0 0;
  padding-left: 16px;
}

.method-formula {
  background: #f6f7f9;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 8px 14px;
  font-size: 0.88rem;
  margin: 8px 0 12px;
  font-family: "SF Mono", "Fira Code", Menlo, monospace;
  overflow-x: auto;
}

.method-scale {
  background: #f9f9fb;
  border-left: 3px solid #3a7bd5;
  padding: 6px 12px;
  font-size: 0.84rem;
  margin-top: 8px;
  border-radius: 0 4px 4px 0;
}

.method-scale code {
  background: #eef;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.82rem;
}

.method-list {
  margin: 6px 0 10px 20px;
  font-size: 0.86rem;
  color: #444;
}

.method-list li {
  margin-bottom: 3px;
}

.method-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
  margin-top: 10px;
}

.method-table th {
  background: #f5f6f8;
  text-align: left;
  padding: 6px 10px;
  font-weight: 600;
  border-bottom: 2px solid #ddd;
  font-size: 0.8rem;
}

.method-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #eee;
  vertical-align: middle;
}

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

/* ── Responsive ───────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  #app {
    flex-direction: column;
    height: auto;
  }

  #map-container {
    height: 60vh;
  }

  #detail-panel {
    width: 100%;
    min-width: 100%;
    border-left: none;
    border-top: 1px solid #ddd;
    max-height: 40vh;
  }

  #detail-panel.collapsed {
    max-height: 0;
    padding: 0;
  }

  .metric-tabs { flex-wrap: wrap; }
  .period-toggle { flex-wrap: wrap; }
  .metric-info-text { font-size: 0.68rem; }
  .strata-info-text { font-size: 0.68rem; }
}

@media (max-width: 600px) {
  #controls h1 { font-size: 0.95rem; }
  .metric-tabs .tab { font-size: 0.72rem; padding: 4px 8px; }
}
