:root {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #9aa1b2;
  --accent: #f5c542;
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  padding: 0.9rem 1.2rem 0.4rem;
}

h1 { font-size: 1.25rem; margin: 0 0 0.2rem; font-weight: 650; }
h2 { font-size: 1rem; margin: 0 0 0.4rem; }
.subtitle { margin: 0; color: var(--muted); font-size: 0.9rem; }
.header-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

button, select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
button:hover, select:hover { border-color: var(--muted); }

#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding: 0.3rem 1.2rem 0.6rem;
  font-size: 0.85rem;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.3rem 0.7rem 0.45rem;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.7rem;
}
fieldset legend { color: var(--muted); font-size: 0.75rem; padding: 0 0.3rem; }
fieldset label { display: inline-flex; align-items: center; gap: 0.3rem; cursor: pointer; }
fieldset label:has(input:disabled) { opacity: 0.4; cursor: not-allowed; }
input[type="range"] { width: 90px; accent-color: var(--accent); }
.ratio-text { min-width: 11.5rem; color: var(--muted); font-variant-numeric: tabular-nums; }
#ratio-reset { padding: 0.1rem 0.5rem; font-size: 0.75rem; }
input[type="radio"], input[type="checkbox"] { accent-color: var(--accent); }

main {
  flex: 1;
  display: flex;
  min-height: 0;
  padding: 0 1.2rem 1rem;
  gap: 1rem;
}

#chart {
  position: relative;
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

#axes { position: absolute; inset: 0; width: 100%; height: 100%; }
#plot { position: absolute; }

.axis-reset { position: absolute; padding: 0.1rem 0.45rem; font-size: 0.72rem; color: var(--muted); opacity: 0.8; }
.axis-reset:hover { color: var(--text); opacity: 1; }
#reset-y { top: 4px; left: 6px; }
#reset-x { bottom: 6px; right: 6px; }

#status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}
#status.hidden { display: none; }
#status.error { color: #ff8a8a; }

#legend {
  width: 230px;
  flex-shrink: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem;
  font-size: 0.85rem;
  overflow-y: auto;
}
.hint { color: var(--muted); font-size: 0.75rem; margin: 0 0 0.6rem; }
.map-frame { position: relative; }
#show-all { position: absolute; top: 0; right: 0; padding: 0.1rem 0.5rem; font-size: 0.72rem; }
#legend-items { list-style: none; margin: 0; padding: 0; }
#legend-items li {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  align-items: center;
  column-gap: 0.45rem;
  padding: 0.2rem 0.3rem;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}
#legend-items li:hover { background: #1f232c; }
#legend-items li.off { opacity: 0.35; }
.swatch { width: 12px; height: 12px; border-radius: 50%; }
#legend-items.neutral .swatch,
#legend-items.neutral .chip-swatch { background: transparent !important; border: 2px solid var(--muted); }

.state-chips { grid-column: 2 / 4; display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.15rem; margin-top: 0.15rem; }
.state-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.05rem 0.25rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
}
.state-chip.off { opacity: 0.35; }
.chip-swatch { width: 9px; height: 9px; border-radius: 50%; }

#map { display: block; width: 100%; height: auto; margin-bottom: 0.6rem; }
.map-state { stroke: var(--panel); stroke-width: 0.6; stroke-linejoin: round; cursor: pointer; transition: fill-opacity 0.15s; }
.map-state:hover { stroke: var(--text); }
.map-state.off { fill-opacity: 0.1; stroke: var(--border); }
.map-label { fill: var(--muted); font-size: 9px; text-anchor: middle; pointer-events: none; }

#size-legend { margin-bottom: 1rem; padding-bottom: 0.8rem; border-bottom: 1px solid var(--border); }
.size-bar { height: 12px; border-radius: 6px; }
.size-ticks { position: relative; height: 1.2rem; margin-top: 4px; color: var(--muted); font-size: 0.72rem; }
.size-ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.region-name { font-weight: 600; }
.region-stats { color: var(--muted); font-size: 0.75rem; font-variant-numeric: tabular-nums; }

#totals {
  margin-top: 0.8rem;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}
#totals strong { color: var(--text); }
#totals .expected { color: var(--muted); font-size: 0.72rem; }
.band-stats { margin-top: 0.6rem; padding-top: 0.5rem; border-top: 1px dashed var(--border); }
dialog h3 { font-size: 0.95rem; margin: 1rem 0 0.3rem; }

.tooltip-title { font-weight: 650; margin-bottom: 2px; }
.tooltip-sub { color: var(--muted); margin-bottom: 6px; }
.tooltip-grid { display: grid; grid-template-columns: auto auto; column-gap: 12px; }
.tooltip-grid span:nth-child(even) { text-align: right; font-variant-numeric: tabular-nums; }

dialog {
  max-width: 640px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.2rem 1.5rem;
  font-size: 0.9rem;
  line-height: 1.5;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog a { color: var(--accent); }
dialog li { margin-bottom: 0.35rem; }
