:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --text-dim: #9aa3b2;
  --text-faint: #6b7280;
  --accent: #4cc2a3;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  --mono: "SFMono-Regular", ui-monospace, Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

#map {
  position: absolute;
  inset: 0;
}

/* Generic floating panel */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------------------------------------------------------------- Sidebar */
#sidebar {
  position: absolute;
  top: 16px;
  left: 16px;
  width: 320px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 5;
}

.sidebar-head {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-head h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

.sidebar-head p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.4;
}

.sidebar-body {
  padding: 12px 16px 16px;
  overflow-y: auto;
}

.field-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  margin: 14px 0 6px;
}

select#layer-select {
  width: 100%;
  padding: 9px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
}

select#layer-select:focus,
button:focus,
.info-btn:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.layer-desc {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.5;
}

/* ---------------------------------------------------------------- Compare */
.compare-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

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

select#compare-select {
  width: 100%;
  margin-top: 8px;
  padding: 9px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
}

select#compare-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* 2D bivariate legend: a small color matrix with two axes. */
.bivar-legend {
  margin-top: 8px;
}

.bivar-matrix {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.bivar-yaxis {
  display: flex;
  align-items: center;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.2;
  max-height: 96px;
  text-align: center;
}

.bivar-grid {
  display: grid;
  gap: 2px;
}

.bivar-cell {
  width: 18px;
  height: 18px;
  border-radius: 2px;
}

.bivar-xaxis {
  margin: 6px 0 0 26px;
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.2;
  max-width: 110px;
}

.bivar-axis-arrow {
  color: var(--text-faint);
}

/* ---------------------------------------------------------------- Legend */
#legend {
  margin-top: 16px;
}

#legend .legend-title {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 8px;
}

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

.legend-swatch {
  width: 16px;
  height: 12px;
  border-radius: 3px;
  flex: none;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.source-line {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-faint);
  line-height: 1.5;
}

/* ---------------------------------------------------------------- Weight tuner */
#weights {
  margin-top: 14px;
  padding: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.weights-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.weights-head .field-label {
  margin: 0;
}

.reset-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
}

.reset-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.weight-row {
  margin: 10px 0 0;
}

.weight-top {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 2px;
}

.weight-label {
  color: var(--text-dim);
}

.weight-val {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

input.weight-slider {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.weights-sum {
  margin-top: 10px;
  font-size: 11px;
  color: var(--text-faint);
}

.weights-sum.off {
  color: var(--accent);
}

/* ---------------------------------------------------------------- Ranked list */
#ranking {
  margin-top: 16px;
}

.rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: rank;
}

.rank-item {
  counter-increment: rank;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.rank-item::before {
  content: counter(rank);
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  min-width: 16px;
}

.rank-item:hover {
  background: var(--panel-2);
}

.rank-name {
  color: var(--text-dim);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-val {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  flex: none;
}

/* ---------------------------------------------------------------- Detail panel */
#detail {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 320px;
  max-height: calc(100vh - 32px);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 5;
}

#detail.open {
  display: flex;
}

.detail-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.detail-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.detail-head .geoid {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 3px;
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

.close-btn:hover {
  color: var(--text);
}

.detail-body {
  padding: 14px 16px 16px;
  overflow-y: auto;
}

.detail-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  margin: 14px 0 8px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 5px 0;
  font-size: 13px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.stat-row .k {
  color: var(--text-dim);
}

.stat-row .v {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

#index-chart {
  margin-top: 6px;
}

#index-chart .bar-label {
  fill: var(--text-dim);
  font-size: 11px;
}

#index-chart .bar-value {
  fill: var(--text);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------- Tooltip */
.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-dim);
  font-size: 10px;
  cursor: help;
  margin-left: 6px;
  vertical-align: middle;
}

#tooltip {
  position: absolute;
  max-width: 260px;
  padding: 9px 11px;
  background: #0b0d11;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text);
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s ease;
  z-index: 20;
}

#tooltip.show {
  opacity: 1;
}

#tooltip .tt-source {
  display: block;
  margin-top: 6px;
  color: var(--text-faint);
  font-size: 11px;
}

/* ---------------------------------------------------------------- Hover popup */
.maplibregl-popup-content {
  background: #0b0d11;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  padding: 8px 10px;
  box-shadow: var(--shadow);
}

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

.hover-pop .hk {
  color: var(--text-faint);
}

.hover-pop .hv {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.hover-pop .hpop {
  display: inline-block;
  margin-top: 3px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent, #6fa3dd);
}

/* ---------------------------------------------------------------- States */
#status {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--text-dim);
  max-width: 460px;
  padding: 24px 28px;
  z-index: 4;
}

#status h2 {
  font-size: 16px;
  color: var(--text);
  margin: 0 0 8px;
}

#status code {
  font-family: var(--mono);
  background: var(--panel-2);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 12px;
}

#status.hidden {
  display: none;
}

/* ---------------------------------------------------------------- About / methodology */
.about-btn {
  margin-top: 16px;
  width: 100%;
  padding: 9px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}

.about-btn:hover {
  border-color: var(--accent);
}

#about-modal {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 30;
}

#about-modal.open {
  display: flex;
}

.about-card {
  width: min(640px, calc(100vw - 48px));
  max-height: calc(100vh - 64px);
  overflow-y: auto;
  padding: 24px 28px;
}

.about-card h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.about-card h3 {
  margin: 18px 0 6px;
  font-size: 13px;
  color: var(--accent);
}

.about-card p,
.about-card li {
  color: var(--text-dim);
  line-height: 1.6;
  font-size: 13px;
}

.about-card .caveat {
  background: var(--panel-2);
  border-left: 3px solid var(--accent);
  padding: 10px 14px;
  border-radius: 6px;
  margin: 12px 0;
  color: var(--text);
}

/* ---------------------------------------------------------------- Responsive */
@media (max-width: 820px) {
  #sidebar,
  #detail {
    width: calc(100vw - 32px);
  }
  #detail {
    top: auto;
    bottom: 16px;
    max-height: 50vh;
  }
}
