/* ---- base ---- */
:root {
  --bg: #0f1116;
  --panel: #171a21;
  --panel-2: #1f242d;
  --border: #2a313d;
  --text: #e6e9ef;
  --muted: #8a93a3;
  --accent: #4f8df9;
  --pos: #4ec07b;
  --neg: #e96363;
  --shadow: 0 4px 18px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 28px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); margin: 4px 0 0; font-size: 14px; }
.small { font-size: 12px; }

/* ---- header ---- */
.page-header {
  padding: 36px 0 28px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #131722, var(--bg));
}
.title-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.date-pill {
  background: var(--panel-2);
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--muted);
  border: 1px solid var(--border);
}
.subtitle { color: var(--muted); margin: 6px 0 22px; }

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}
.kpi {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.kpi-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.kpi-value { font-size: 22px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ---- cards ---- */
main { padding: 24px 0 64px; }
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
/* Title block (always the first child) absorbs any text-length variation so the
   right-side widget (tabs, dropdowns) stays pinned in the same position across modes. */
.card-head > :first-child {
  flex: 1 1 320px;
  min-width: 0;
}
.card-head > :last-child:not(:first-child) {
  flex-shrink: 0;
}
.controls label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.controls select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  cursor: pointer;
}
.controls select:focus { border-color: var(--accent); }

/* ---- mode tabs ---- */
.mode-tabs {
  display: inline-flex;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}
.mode-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
  min-width: 110px;
  text-align: center;
  white-space: nowrap;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.is-active {
  background: var(--bg);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border);
}

.map-controls {
  margin-bottom: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  min-height: 44px;  /* keeps map from shifting when modes have different controls */
}
.controls-group {
  display: none;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
}
.controls-group.is-active { display: inline-flex; }
.controls-group[hidden] { display: none !important; }
.controls-group label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.controls-group select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  cursor: pointer;
  min-width: 200px;
}
.controls-group select:focus { border-color: var(--accent); }
.checkbox-label {
  cursor: pointer;
  user-select: none;
}
.checkbox-label input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* turnout-mode legend gradient */
.legend.turnout {
  align-items: center;
  gap: 8px;
}
.scale-bar-svg { vertical-align: middle; }

/* ---- maps ---- */
.map-wrap {
  position: relative;
}
.map {
  width: 100%;
  aspect-ratio: 16 / 10;
  background: #0c0e13;
  border-radius: 8px;
  overflow: hidden;
}
.map svg { width: 100%; height: 100%; display: block; }
.municipality {
  cursor: pointer;
  stroke: rgba(255,255,255,.18);
  stroke-width: 0.5;
  transition: filter .15s ease;
}
.municipality:hover { filter: brightness(1.25); stroke-width: 1.2; }
.donut-bg { fill: rgba(0,0,0,.55); }
.donut-segment { stroke: rgba(0,0,0,.4); stroke-width: 0.4; }

.map-tooltip {
  position: fixed;
  pointer-events: none;
  background: rgba(20,24,32,.97);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  min-width: 240px;
  max-width: 360px;
  box-shadow: var(--shadow);
  z-index: 10;
}
.tooltip-grid {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-top: 6px;
}
.tooltip-stats { flex: 1; min-width: 0; }
.tooltip-donut-wrap {
  flex-shrink: 0;
  width: 96px;
  height: 96px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tooltip-donut { display: block; }
.tooltip-donut-bg { fill: rgba(0,0,0,.55); }
.tooltip-donut-segment { stroke: rgba(0,0,0,.45); stroke-width: 0.6; }
.tooltip-donut-center {
  font-size: 11px;
  fill: var(--text);
  text-anchor: middle;
  dominant-baseline: central;
  font-weight: 600;
}
.tooltip-donut-center-sub {
  font-size: 9px;
  fill: var(--muted);
  text-anchor: middle;
  dominant-baseline: central;
}
.map-tooltip h4 {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 600;
}
.map-tooltip .row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-variant-numeric: tabular-nums;
}
.map-tooltip .row.muted { color: var(--muted); font-size: 12px; }
.map-tooltip .party-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  margin-top: 3px;
}
.party-swatch {
  width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0;
}
.party-row .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.party-row .pct { font-variant-numeric: tabular-nums; color: var(--muted); }
.section-divider {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 8px 0;
}

/* ---- legend ---- */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin-top: 12px;
  font-size: 12px;
  color: var(--muted);
}
.legend-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; }

.legend-inline {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--muted);
}
.scale-bar {
  width: 160px; height: 8px; border-radius: 4px;
}

/* ---- correlation chart ---- */
#correlation-chart {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #0c0e13;
  border-radius: 8px;
  overflow: hidden;
}
#correlation-chart svg { width: 100%; height: 100%; display: block; }
.axis path, .axis line { stroke: rgba(255,255,255,.18); shape-rendering: crispEdges; }
.axis text { fill: var(--muted); font-size: 11px; }
.axis-label { fill: var(--muted); font-size: 12px; }
.diag-line { stroke: rgba(255,255,255,.25); stroke-dasharray: 4 4; }
.scatter-dot { fill-opacity: .65; stroke: rgba(0,0,0,.45); stroke-width: .4; cursor: pointer; }
.scatter-dot:hover { fill-opacity: 1; stroke: white; stroke-width: 1; }

/* ---- strongholds small-multiples ---- */
#strongholds-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}
@media (max-width: 900px) {
  #strongholds-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  #strongholds-grid { grid-template-columns: 1fr; }
}
.stronghold-tile {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stronghold-tile .head {
  display: flex;
  align-items: center;
  gap: 8px;
}
.stronghold-tile .head .swatch {
  width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0;
}
.stronghold-tile h3 {
  font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.stronghold-tile .stat {
  font-size: 11px; color: var(--muted);
  display: flex; justify-content: space-between;
  font-variant-numeric: tabular-nums;
}
.stronghold-map {
  width: 100%;
  aspect-ratio: 16 / 10;
  border-radius: 4px;
  overflow: hidden;
  background: #0c0e13;
}
.stronghold-map svg { width: 100%; height: 100%; display: block; }
.stronghold-map path {
  stroke: rgba(255,255,255,.08);
  stroke-width: .3;
}

/* ---- Sofia: map + chart side-by-side ---- */
.sofia-layout {
  display: grid;
  grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
  gap: 18px;
  align-items: start;
}
@media (max-width: 900px) {
  .sofia-layout { grid-template-columns: 1fr; gap: 14px; }
}
#sofia-map {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #0c0e13;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}
#sofia-map svg { width: 100%; height: 100%; display: block; }
.sofia-map-cell {
  stroke: rgba(0,0,0,.06);
  stroke-width: 0.4;
  cursor: pointer;
}
.sofia-map-boundary {
  fill: none;
  stroke: rgba(0,0,0,.6);
  stroke-width: 1.2;
  stroke-linecap: round;
  pointer-events: none;
}
.sofia-map-label {
  fill: white;
  font-size: 9.5px;
  font-weight: 600;
  text-anchor: middle;
  dominant-baseline: middle;
  paint-order: stroke;
  stroke: rgba(0,0,0,.7);
  stroke-width: 2.4;
  pointer-events: none;
  user-select: none;
}
@media (max-width: 700px) {
  .sofia-map-label { font-size: 9px; stroke-width: 2; }
}

/* ---- Sofia rayoni diverging chart ---- */
#sofia-chart {
  width: 100%;
}
#sofia-chart svg { width: 100%; height: auto; display: block; }
.sofia-chart .row-label {
  fill: var(--text);
  font-size: 12.5px;
  dominant-baseline: middle;
  font-weight: 500;
}
.sofia-chart .row-diff {
  fill: var(--muted);
  font-size: 11px;
  dominant-baseline: middle;
  font-variant-numeric: tabular-nums;
}
.sofia-chart .row-turnout-label {
  fill: var(--muted);
  font-size: 11px;
  dominant-baseline: middle;
  font-variant-numeric: tabular-nums;
}
.sofia-chart .header {
  fill: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.sofia-chart .center-line { stroke: rgba(255,255,255,.35); stroke-width: 1; }
.sofia-chart .axis path,
.sofia-chart .axis line { stroke: rgba(255,255,255,.18); shape-rendering: crispEdges; }
.sofia-chart .axis text { fill: var(--muted); font-size: 11px; }
.sofia-chart .row-bar {
  cursor: pointer;
  transition: opacity .12s ease;
}
.sofia-chart .row-bar:hover { opacity: 1 !important; }
.sofia-chart .row-touch {
  fill: transparent;
  cursor: pointer;
}
.sofia-chart .row-touch:hover ~ * .row-label { fill: white; }
.sofia-chart .turnout-cell {
  fill: rgba(255,255,255,.06);
}

/* (legacy tile styles kept for reference, no longer used) */
#sofia-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}
@media (max-width: 900px) { #sofia-grid { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 600px) { #sofia-grid { grid-template-columns: repeat(2, 1fr); } }
.rayon-tile {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  cursor: default;
}
.rayon-tile .name-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.rayon-tile .name {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rayon-tile .turnout {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.rayon-tile .turnout strong { color: var(--text); font-weight: 600; }
.rayon-bar {
  display: flex;
  height: 10px;
  border-radius: 3px;
  overflow: hidden;
  background: rgba(255,255,255,.04);
}
.rayon-bar .seg { height: 100%; }
.rayon-tile .legend-rows {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.rayon-tile .legend-rows .row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
}
.rayon-tile .legend-rows .swatch {
  width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0;
}
.rayon-tile .legend-rows .name-cell {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}
.rayon-tile .legend-rows .pct {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.rayon-tile .footnote {
  font-size: 10px;
  color: var(--muted);
  margin-top: 2px;
  display: flex;
  justify-content: space-between;
}

/* ---- diaspora section ---- */
.diaspora-h3 {
  margin: 16px 0 4px;
  font-size: 14px;
  color: var(--text);
  font-weight: 600;
}
#diaspora-map {
  width: 100%;
  aspect-ratio: 21 / 9;
  background: #0c0e13;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 6px;
}
#diaspora-map svg { width: 100%; height: 100%; display: block; }
.diaspora-land { fill: #1c2029; stroke: rgba(255,255,255,.08); stroke-width: 0.4; }
.diaspora-graticule { fill: none; stroke: rgba(255,255,255,.06); stroke-width: 0.4; }
.diaspora-dot {
  cursor: pointer;
  stroke: rgba(0,0,0,.5);
  stroke-width: 0.6;
  transition: opacity .15s ease;
}
.diaspora-dot:hover { stroke: white; stroke-width: 1.2; }
.diaspora-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 18px;
  margin-top: 6px;
}
@media (max-width: 900px) {
  .diaspora-grid { grid-template-columns: 1fr; gap: 14px; }
}
#diaspora-bars { width: 100%; }
#diaspora-bars svg { width: 100%; height: auto; display: block; }
.diaspora-bars .country-label {
  fill: var(--text); font-size: 12px; dominant-baseline: middle;
}
.diaspora-bars .vote-count {
  fill: var(--muted); font-size: 11px; dominant-baseline: middle;
  font-variant-numeric: tabular-nums;
}
.diaspora-bars .seg { stroke: rgba(0,0,0,.35); stroke-width: 0.4; }
#diaspora-vs-home { width: 100%; }
#diaspora-vs-home svg { width: 100%; height: auto; display: block; }
.vshome-row { cursor: default; }
.vshome-row .label {
  fill: var(--text); font-size: 12.5px; font-weight: 500; dominant-baseline: middle;
}
.vshome-row .pct {
  fill: var(--muted); font-size: 11px; dominant-baseline: middle;
  font-variant-numeric: tabular-nums;
}
.vshome-bar-home { opacity: .55; }
.vshome-bar-abroad { opacity: 1; }
.vshome-arrow { fill: var(--muted); }

@media (max-width: 700px) {
  .diaspora-bars .country-label { font-size: 10.5px; }
  .diaspora-bars .vote-count { font-size: 10px; }
  .vshome-row .label { font-size: 11px; }
  .vshome-row .pct { font-size: 10px; }
  #diaspora-map { aspect-ratio: 16 / 10; }
}

/* ---- butterfly chart ---- */
#butterfly-chart {
  width: 100%;
  min-height: 460px;
}
#butterfly-chart svg { width: 100%; height: 100%; display: block; }
.butterfly .grid line { stroke: rgba(255,255,255,.06); shape-rendering: crispEdges; }
.butterfly .axis path, .butterfly .axis line { stroke: rgba(255,255,255,.18); }
.butterfly .axis text { fill: var(--muted); font-size: 11px; }
.butterfly .row-label {
  fill: var(--text);
  font-size: 12px;
  font-weight: 500;
  dominant-baseline: middle;
}
.butterfly .row-delta {
  fill: var(--muted);
  font-size: 11px;
  dominant-baseline: middle;
  font-variant-numeric: tabular-nums;
}
.butterfly .bar-paper { opacity: .7; }
.butterfly .bar-machine { opacity: 1; }
.butterfly .header {
  fill: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
}

/* ---- footer ---- */
.page-footer {
  border-top: 1px solid var(--border);
  padding: 28px 0 40px;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
}
.page-footer h3 { color: var(--text); margin-bottom: 10px; }
.page-footer ul { padding-left: 18px; margin: 0 0 10px; }
.page-footer li { margin-bottom: 4px; }

/* ============================================================ */
/* Responsive — mobile / narrow viewports                       */
/* ============================================================ */
@media (max-width: 720px) {
  .container { padding: 0 12px; }
  .page-header { padding: 22px 0 16px; }
  h1 { font-size: 22px; }
  h2 { font-size: 17px; }
  .subtitle { font-size: 13px; margin-bottom: 16px; }
  .date-pill { font-size: 12px; padding: 3px 10px; }

  .kpis { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .kpi { padding: 10px 12px; }
  .kpi-value { font-size: 18px; }
  .kpi-label { font-size: 11px; }
  .kpi-sub { font-size: 11px; }

  main { padding: 16px 0 32px; }
  .card { padding: 14px; margin-bottom: 14px; border-radius: 10px; }
  .card-head { gap: 10px; margin-bottom: 12px; }
  .card-head > :last-child:not(:first-child) {
    width: 100%;
    flex-shrink: 0;
  }

  /* tabs become full-width strip */
  .mode-tabs { width: 100%; display: flex; }
  .mode-btn {
    flex: 1;
    min-width: 0;
    padding: 8px 4px;
    font-size: 12px;
  }

  /* sub-controls stack vertically and use full width */
  .map-controls {
    min-height: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 12px;
  }
  .controls-group { width: 100%; flex-wrap: wrap; gap: 12px; }
  .controls-group label { width: 100%; }
  .controls-group select { width: 100%; min-width: 0; flex: 1; }

  /* maps get a more square aspect on phones */
  .map { aspect-ratio: 4 / 3; }

  /* legend smaller + wraps better */
  .legend { font-size: 11px; gap: 4px 10px; margin-top: 8px; }
  .legend-swatch { width: 10px; height: 10px; }

  /* tooltip fits the viewport */
  .map-tooltip {
    max-width: calc(100vw - 24px);
    min-width: 0;
    font-size: 12px;
    padding: 8px 10px;
  }
  .tooltip-grid { flex-direction: column-reverse; gap: 8px; }
  .tooltip-donut-wrap {
    width: 84px;
    height: 84px;
    align-self: center;
  }

  /* strongholds: single column on phones */
  #strongholds-grid { grid-template-columns: 1fr !important; gap: 12px; }
  .stronghold-tile { padding: 8px 10px; }
  .stronghold-tile h3 { font-size: 12px; }

  /* correlation chart: more square so dots aren't crushed */
  #correlation-chart { aspect-ratio: 1 / 1; }

  /* footer */
  .page-footer { padding: 20px 0 32px; font-size: 12px; }
}

@media (max-width: 480px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  h1 { font-size: 20px; }
  .title-row { gap: 8px; }
  .card { padding: 12px; }
  .mode-btn { font-size: 11px; padding: 7px 2px; }
}
