:root {
  --bg: #0f1420;
  --panel: #161d2e;
  --border: #2a3350;
  --text: #e6e9f0;
  --muted: #9aa4c0;
  --accent: #2563eb;
  --finalist: #22c55e;
  --eliminated: #4b5568;
  --cc: #f59e0b;
  --winner: #ef4444;
  --party-left: #3b82f6;
  --party-right: #ef4444;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

header {
  padding: 20px 24px 8px;
}

h1 {
  margin: 0 0 4px;
  font-size: 1.6rem;
}

.subtitle {
  color: var(--muted);
  max-width: 900px;
  font-size: 0.92rem;
}

main {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 20px;
  padding: 0 24px 40px;
}

.controls-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  height: fit-content;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  position: sticky;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.control-group.buttons {
  flex-direction: row;
  gap: 8px;
}

.control-group.checkbox label {
  display: flex;
  align-items: center;
  gap: 6px;
}

.control-group label {
  font-size: 0.82rem;
  color: var(--muted);
}

select,
input[type="range"] {
  width: 100%;
}

select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px;
}

button {
  flex: 1;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 0.85rem;
}

button:hover {
  filter: brightness(1.1);
}

.results-toggle {
  margin-bottom: 10px;
}

.toggle-btn {
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
}

.toggle-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.status {
  min-height: 1.2em;
  font-size: 0.82rem;
  color: var(--muted);
}

.status.error {
  color: #ef4444;
}

.chart-section {
  grid-column: 2;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 20px;
  margin-bottom: 20px;
}

.chart-section h2 {
  font-size: 1.05rem;
  margin: 4px 0 8px;
}

.section-note {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 10px;
}

.chart-container {
  width: 100%;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
}

.metric-chart-cell {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  cursor: zoom-in;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.metric-chart-cell:hover {
  border-color: var(--accent, #2563eb);
  box-shadow: 0 0 0 1px var(--accent, #2563eb);
}

.chart-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}

.chart-modal-overlay[hidden] {
  display: none;
}

.chart-modal {
  position: relative;
  background: var(--bg);
  border-radius: 8px;
  padding: 24px;
  width: min(900px, 100%);
  max-height: 90vh;
  overflow: auto;
  cursor: default;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}

.chart-modal-body {
  width: 100%;
}

.chart-modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}

.chart-modal-close:hover {
  color: inherit;
}

.metrics-legend {
  display: flex;
  gap: 16px;
  margin-bottom: 10px;
  font-size: 0.82rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.headline-table-wrap {
  overflow-x: auto;
}

.headline-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

.headline-control {
  display: flex;
  align-items: center;
  gap: 6px;
}

table.headline-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

table.headline-table th,
table.headline-table td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
}

table.headline-table th {
  background: var(--bg);
}

.metric-label {
  color: var(--muted);
}

.baseline-cell {
  font-weight: 600;
}

.delta {
  color: var(--muted);
  font-size: 0.85em;
}

.delta-cell {
  color: var(--muted);
}

.best-check {
  font-size: 0.85em;
}

footer {
  padding: 16px 24px 40px;
  color: var(--muted);
  font-size: 0.78rem;
  max-width: 900px;
}

/* --- D3 chart element styling (SVG classes set by chart modules) --- */

.density-area {
  fill: var(--accent);
  opacity: 0.15;
  stroke: none;
}

.density-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
}

.median-line {
  stroke-dasharray: 4 3;
  stroke-width: 1.5;
}

.median-line.population-median {
  stroke: #a855f7;
}

.median-line.pool-median {
  stroke: #06b6d4;
}

.median-line.party-line {
  stroke: var(--muted);
  stroke-dasharray: none;
  stroke-width: 1;
}

.median-label {
  fill: var(--muted);
  font-size: 10px;
}

.party-bar {
  stroke: #0f1420;
  stroke-width: 1;
}

.party-bar-left {
  fill: var(--party-left);
}

.party-bar-right {
  fill: var(--party-right);
}

.party-bar-label {
  fill: var(--muted);
  font-size: 10px;
}

circle.candidate.finalist {
  fill: var(--finalist);
  stroke: #0f1420;
  stroke-width: 1;
}

circle.candidate.eliminated {
  fill: var(--eliminated);
  opacity: 0.7;
}

.cc-marker {
  fill: var(--cc);
  stroke: #0f1420;
  stroke-width: 1;
}

.winner-marker {
  fill: none;
  stroke: var(--winner);
  stroke-width: 2.5;
}

.draw-legend text {
  fill: var(--muted);
  font-size: 10px;
}

.legend-finalist {
  fill: var(--finalist);
}
.legend-eliminated {
  fill: var(--eliminated);
}
.legend-cc {
  fill: var(--cc);
}
.legend-winner {
  fill: none;
  stroke: var(--winner);
  stroke-width: 2;
}
.legend-population-median {
  fill: #a855f7;
}
.legend-pool-median {
  fill: #06b6d4;
}
.legend-party-line {
  fill: var(--muted);
}
.legend-party-left {
  fill: var(--party-left);
}
.legend-party-right {
  fill: var(--party-right);
}

.draw-metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 6px 16px;
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
}

.draw-metric {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.draw-metric-label {
  color: var(--muted);
}

.draw-metric-value {
  font-weight: 600;
}

.draw-metric-note {
  grid-column: 1 / -1;
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}

table.results-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

table.results-table th,
table.results-table td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

table.results-table th {
  background: var(--bg);
}

.results-row.winner-row td {
  background: rgba(239, 68, 68, 0.12);
}

.results-row.cc-row td {
  box-shadow: inset 0 0 0 1px var(--cc);
}

.results-place,
.results-votes,
.results-pct {
  white-space: nowrap;
}

.results-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 6px;
}

.results-swatch.legend-finalist {
  background: var(--finalist);
}

.results-swatch.legend-eliminated {
  background: var(--eliminated);
  opacity: 0.7;
}

.results-swatch.party-left {
  background: var(--party-left);
}

.results-swatch.party-right {
  background: var(--party-right);
}

.results-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 0.75em;
  font-weight: 600;
}

.advanced-badge {
  background: rgba(34, 197, 94, 0.18);
  color: var(--finalist);
}

.cc-badge {
  background: rgba(245, 158, 11, 0.18);
  color: var(--cc);
}

.winner-badge {
  background: rgba(239, 68, 68, 0.18);
  color: var(--winner);
}

.results-note {
  color: var(--muted);
  font-size: 0.82rem;
  margin: 8px 0 0;
}

.dynamic-footer-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 24px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.35);
}

.dynamic-footer-bar label {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

.dynamic-footer-bar input[type="range"] {
  flex: 1;
  max-width: 480px;
  width: auto;
}

/* .dynamic-footer-bar's own display:flex (author stylesheet) would
   otherwise override the [hidden] UA default, same reason
   chart-modal-overlay needs this explicit rule. */
.dynamic-footer-bar[hidden] {
  display: none;
}

/* .chart-section's own display (author stylesheet) would otherwise
   override the [hidden] UA default. */
#t-metrics-section[hidden] {
  display: none;
}

/* Reserve space at the bottom of the page so the fixed footer bar (only
   present in dynamic mode) never overlaps the last section's content. */
body.has-dynamic-footer {
  padding-bottom: 56px;
}

.equilibrium-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 0.75em;
  font-weight: 600;
}

.equilibrium-badge.duvergerian {
  background: rgba(34, 197, 94, 0.18);
  color: var(--finalist);
}

.equilibrium-badge.non-duvergerian {
  background: rgba(245, 158, 11, 0.18);
  color: var(--cc);
}

.equilibrium-badge.trivial {
  color: var(--muted);
}

.vote-share-bar.party-left {
  fill: var(--party-left);
}

.vote-share-bar.party-right {
  fill: var(--party-right);
}

.vote-share-bar.bar-finalist {
  opacity: 1;
}

.vote-share-bar.bar-eliminated {
  opacity: 0.45;
}

.vote-share-bar.bar-cc {
  stroke: var(--cc);
  stroke-width: 2;
  stroke-dasharray: 3 2;
}

.vote-share-bar.bar-winner {
  stroke: var(--winner);
  stroke-width: 3;
}

/* White halo drawn under the winner's colored stroke so it stays visible
   even when the winner's bar is party-right (same red as --winner). */
.vote-share-bar-halo {
  fill: none;
  stroke: #fff;
  stroke-width: 6;
  opacity: 0.9;
}

.hist-label {
  fill: var(--text);
  font-size: 10px;
}

.matchup-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

.matchup-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}

.matchup-heading {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.matchup-margin {
  font-weight: 400;
  color: var(--muted);
  font-size: 0.8em;
}

table.matchup-table {
  margin: 0;
}

.matchup-row.matchup-winner td {
  font-weight: 700;
}

.chart-title {
  fill: var(--text);
  font-size: 11px;
}

.chart-subtitle {
  fill: var(--muted);
  font-size: 10px;
}

.legend-label {
  fill: var(--text);
  font-size: 10px;
}

svg text {
  fill: var(--muted);
}

svg .domain,
svg .tick line {
  stroke: var(--border);
}

@media (max-width: 800px) {
  main {
    grid-template-columns: 1fr;
  }
  .chart-section {
    grid-column: 1;
  }
}
