/*
 * Site stylesheet: tokens first, then one section per component. Warm pastel page background, white cards
 * with a 1 px border, a serif for headings and a sans for everything else. Party colours come from the run
 * catalogue and block colours from THEME (charts/base.js); nothing here overrides them. No dark mode.
 */

/* Fonts: vendored woff2 (SIL OFL, see /dist/vendor/fonts/LICENSE); bold body text is synthesised */
@font-face {
  font-family: 'Source Serif 4';
  src: url('/dist/vendor/fonts/SourceSerif4-4.005-Semibold.woff2') format('woff2');
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Source Sans 3';
  src: url('/dist/vendor/fonts/SourceSans3-3.052-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

/* Tokens. Muted text and links pass WCAG AA (4.5:1) on both the page background and the cards. */
:root {
  --color-bg: #fbf1e7;
  --color-card: #fff;
  --color-text: #1f1b18;
  --color-muted: #635a52;
  --color-line: #e2d3c3;
  --color-line-strong: #cdb9a5;
  --color-accent: #1d4f86;
  --color-soft: #f3e3d3;
  --color-error: #b00020;
  --font-head: 'Source Serif 4', Georgia, serif;
  --font-body: 'Source Sans 3', system-ui, sans-serif;
  --max-width: 1100px;
  --gutter: 16px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --radius: 4px;
  --tap: 40px;
  /* Side padding that keeps full-width bands aligned with the content column */
  --band-pad: max(var(--gutter), calc((100% - var(--max-width)) / 2 + var(--gutter)));
}

/* Base */
* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: 1.0625rem;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { font-family: var(--font-head); font-weight: 600; line-height: 1.25; }
h2 { font-size: 1.3rem; margin: 0 0 var(--space-3); }
h3 { font-size: 1.1rem; margin: var(--space-4) 0 var(--space-2); }
a { color: var(--color-accent); text-underline-offset: 2px; }
:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
code { font-size: 0.92em; }

main {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: var(--space-5) var(--gutter) 40px;
}

/* Site header: name on the left, the four-entry menu on the right */
#site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-5);
  padding: var(--space-3) var(--band-pad);
  background: var(--color-card);
  border-bottom: 1px solid var(--color-line);
}

#site-header h1 { margin: 0; font-size: 1.5rem; }
#site-header h1 a { display: inline-flex; align-items: center; min-height: var(--tap); color: var(--color-text); text-decoration: none; }
#site-header nav { display: flex; gap: var(--space-1); }

#site-header nav a,
.event-tabs a {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 var(--space-3);
  color: var(--color-text);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
}

#site-header nav a:hover,
.event-tabs a:hover { color: var(--color-accent); }

#site-header nav a[aria-current="page"],
.event-tabs a[aria-current="page"] {
  border-bottom-color: var(--color-accent);
  font-weight: 600;
}

/* Freeze banner (electoral silence), above the header */
#freeze-banner {
  padding: var(--space-3) var(--band-pad);
  background: #fff3cd;
  border-bottom: 1px solid #e0b100;
  font-weight: 600;
}
#freeze-banner[hidden] { display: none; }

/* Event bar (Promedios and Escaños): name and date, the tabs, and the pinned run and the reading of the mode on
   the right */
#event-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "title run"
    "tabs  mode";
  gap: var(--space-1) var(--space-5);
  align-items: end;
  padding: var(--space-4) var(--band-pad) 0;
  background: var(--color-soft);
  border-bottom: 1px solid var(--color-line);
}

.event-title { grid-area: title; }
.event-title h2 { margin: 0; font-size: 1.6rem; }
.event-title p { margin: var(--space-1) 0 0; }
.event-tabs { grid-area: tabs; display: flex; gap: var(--space-1); margin-top: var(--space-2); }
.event-run { grid-area: run; justify-self: end; }
.mode-text { grid-area: mode; margin: 0; max-width: 34rem; color: var(--color-muted); font-size: 0.9rem; text-align: right; align-self: center; }

.chip {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  font-size: 0.85rem;
  background: var(--color-card);
  border: 1px solid var(--color-line-strong);
  border-radius: 999px;
}

/* Forms: GET forms with a "Ver" button that only shows without JS (common.js adds `.js`) */
.controls { display: flex; flex-wrap: wrap; gap: var(--space-3) var(--space-4); align-items: center; }
form.js .nojs-only { display: none; }
fieldset { border: 0; margin: 0; padding: 0; display: flex; gap: var(--space-3); align-items: center; }
legend { float: left; padding: 0; margin-right: var(--space-2); }
label { display: inline-flex; align-items: center; gap: 6px; }

button,
select {
  min-height: var(--tap);
  padding: 0 var(--space-3);
  font: inherit;
  color: var(--color-text);
  background: var(--color-card);
  border: 1px solid var(--color-line-strong);
  border-radius: var(--radius);
}

button { cursor: pointer; }
button:hover { border-color: var(--color-accent); }

/* Scope filter of the global pages: discreet, above the cards */
#scope-filter { margin: 0 0 var(--space-4); font-size: 0.95rem; color: var(--color-muted); }
#scope-filter select { color: var(--color-text); }

/* Layout helpers */
.stack > * + * { margin-top: var(--space-4); }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.grid > * { min-width: 0; }

/* Page headline: one line outside the cards with the situation in plain words */
.headline {
  margin: 0 0 var(--space-2);
  font-family: var(--font-head);
  font-weight: 600;
  font-size: 1.4rem;
  line-height: 1.35;
}

.back { margin: 0; font-size: 0.95rem; }

/* Card pattern: title, lead, body, "Cómo leerlo" */
.card {
  min-width: 0;
  padding: var(--space-4) 20px;
  background: var(--color-card);
  border: 1px solid var(--color-line);
  border-radius: var(--radius);
}

.card-head { margin-bottom: var(--space-3); }
.card-head h2, .card-head h3 { margin: 0; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.mode-subtitle { font-family: var(--font-body); font-weight: 400; font-size: 0.9rem; color: var(--color-muted); }
.lead { margin: var(--space-1) 0 0; color: var(--color-muted); }

.how { margin-top: var(--space-3); border-top: 1px solid var(--color-line); }

.how summary {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  color: var(--color-accent);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  list-style: none;
}

.how summary::-webkit-details-marker { display: none; }
.how summary::before { content: '+'; display: inline-block; width: 1.2em; font-weight: 600; }
.how[open] summary::before { content: '−'; }
.how-body { font-size: 0.95rem; max-width: 46rem; }
.how-body p { margin: 0 0 var(--space-2); }

/* Event tiles of the Inicio; the first one (the general election) is the hero */
article.card p, .card-link p { margin: 6px 0; }
.tile { min-width: 0; }
.tile-hero .card-link { border-top: 4px solid var(--color-accent); }
.tile-hero h3 { font-size: 1.6rem; margin-top: 0; }
.card-link { display: block; color: inherit; text-decoration: none; cursor: pointer; }
.card-link:hover, .card-link:focus-visible { border-color: var(--color-accent); background: var(--color-soft); }
.card-link:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.card-link h3 { margin-top: 0; }
.card-muted { background: var(--color-soft); border-style: dashed; color: var(--color-muted); cursor: default; }
.card-muted h3 { margin-top: 0; color: inherit; }
.grid-three { grid-template-columns: repeat(3, 1fr); }
.card h3 a { color: var(--color-text); text-decoration: none; }
.card h3 a:hover { color: var(--color-accent); text-decoration: underline; }

/* Closing card: the method question, a teaser and the related links */
.card-closing { background: var(--color-soft); border-color: var(--color-line-strong); }
.card-closing h2 { margin: 0; }

.link-list { margin: var(--space-3) 0 0; padding-left: 1.2em; }
.link-list li + li { margin-top: var(--space-1); }
.section-nav { display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-4); }
.section-nav a { display: inline-flex; align-items: center; min-height: var(--tap); }
/* Subnavigation of the method pages, above the scope filter */
.metodo-nav { margin: 0 0 var(--space-4); }
.metodo-nav a[aria-current="page"] { font-weight: 600; }

/* Text helpers */
.muted { color: var(--color-muted); font-size: 0.9em; }
.notice { color: var(--color-muted); font-style: italic; margin: var(--space-2) 0; }
.status { color: var(--color-muted); }
.status.error { color: var(--color-error); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; flex: none; }

/* Tables: horizontal scroll inside `.table-wrap`; the first column sticks on narrow screens */
.table-wrap { overflow-x: auto; margin-top: var(--space-2); }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
caption { caption-side: top; text-align: left; padding: 0 0 var(--space-2); color: var(--color-muted); font-size: 0.9rem; }
th, td { padding: 6px var(--space-2); border-bottom: 1px solid var(--color-line); text-align: right; white-space: nowrap; }
th { font-weight: 600; font-size: 0.9rem; border-bottom: 2px solid var(--color-line-strong); }
th:first-child, td:first-child { text-align: left; }
dl { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-1) var(--space-4); margin: 0; }
dt { color: var(--color-muted); }
dd { margin: 0; overflow-wrap: anywhere; }

/* Charts: ECharts containers, 360 px high on desktop and 240 px on mobile */
.chart { width: 100%; height: 360px; }
.chart.chart-tall { height: 480px; }
.chart.chart-small { height: 160px; }

/* Promedios: controls of the average chart, one per row (view, window, party or block pills), and the majority bars */
.series-controls { display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-2); margin: 0 0 var(--space-2); }
.chart-window { font-size: 0.9rem; flex-wrap: wrap; gap: var(--space-1) var(--space-3); }
.chart-window legend { color: var(--color-muted); }
.chart-window[hidden], .series-controls[hidden], .pills[hidden] { display: none; }

/* Pills: a group of checkboxes drawn like a chart legend, bordered in the colour of each name and filled with it
   when checked, with the ink the server picks for that colour (`--pill-ink`). `is-on` is the fallback set by JS for
   browsers without `:has()`, in a rule of its own: a selector list with `:has()` is dropped whole by them. The
   checkbox stays focusable. */
.pills { display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-2); font-size: 0.9rem; }
.pill {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 2px 12px;
  border: 1px solid var(--pill-color, var(--color-line-strong));
  border-radius: 999px;
  background: var(--color-card);
  color: var(--color-muted);
  cursor: pointer;
  user-select: none;
}
.pill input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; pointer-events: none; }
.pill.is-on { background: var(--pill-color, var(--color-accent)); color: var(--pill-ink, #fff); }
.pill:has(input:checked) { background: var(--pill-color, var(--color-accent)); color: var(--pill-ink, #fff); }
.pill:has(input:focus-visible) { outline: 2px solid var(--color-accent); outline-offset: 2px; }
@supports not selector(:has(*)) {
  .pill:focus-within { outline: 2px solid var(--color-accent); outline-offset: 2px; }
}
#polls-more { margin-top: var(--space-2); }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.majority-row { display: grid; grid-template-columns: 6.5rem 1fr; gap: var(--space-1) var(--space-3); align-items: center; margin: var(--space-4) 0; }
.majority-track { height: 20px; background: var(--color-soft); border-radius: 3px; overflow: hidden; }
.majority-bar { height: 100%; }
.majority-text { grid-column: 2; font-size: 0.9rem; }

/* Escaños: histograms, blocks chart, coalition calculator and district form */
.grid-small { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--space-3); margin-top: var(--space-3); }
#blocks-chart { height: 120px; }
.coalition-result { font-size: 1.1rem; margin: var(--space-2) 0; }
#district-form { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-3); align-items: center; margin-top: var(--space-3); }

/* Casas and elections: bullseye and the house-effects table */
#bullseye-chart, #errors-bullseye { aspect-ratio: 1 / 1; height: auto; max-width: 480px; }
/* The history bullseye is the page's main chart: larger and centred in its card */
#errors-bullseye { max-width: 640px; margin: 0 auto; }
#effects-table td[title] { cursor: help; }

/* Footer */
#site-footer {
  padding: var(--space-4) var(--band-pad) 40px;
  border-top: 1px solid var(--color-line);
  color: var(--color-muted);
  font-size: 0.85rem;
}
#site-footer p { margin: 0 0 var(--space-1); }

/* Mobile (< 720 px): one column, the menu row scrolls sideways, the event bar stacks */
@media (max-width: 719px) {
  body { font-size: 1rem; }
  main { padding-top: var(--space-4); }
  .grid, .grid-three { grid-template-columns: 1fr; }
  .card { padding: var(--space-3) var(--space-4); }
  .headline { font-size: 1.2rem; }
  .chart { height: 240px; }
  .chart.chart-tall { height: 360px; }
  dl { grid-template-columns: 1fr; }
  dt + dd { margin-bottom: var(--space-2); }

  #site-header { display: block; padding-bottom: 0; }
  #site-header nav { overflow-x: auto; margin: var(--space-1) calc(-1 * var(--space-3)) 0; scrollbar-width: none; }
  #site-header nav::-webkit-scrollbar { display: none; }

  #event-bar {
    grid-template-columns: 1fr;
    grid-template-areas: "title" "tabs" "run" "mode";
    gap: var(--space-2);
  }
  .event-title h2 { font-size: 1.35rem; }
  .event-tabs { overflow-x: auto; margin-top: 0; }
  .event-run { justify-self: start; }
  .mode-text { text-align: left; }

  /* Larger tap targets for the chart controls and the pills */
  .chart-window label, .pill { min-height: var(--tap); }

  .table-wrap th:first-child,
  .table-wrap td:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    background: var(--color-card);
    box-shadow: 1px 0 0 var(--color-line);
    white-space: normal;
    min-width: 7rem;
  }
}

/* No motion for those who ask: nothing animates on entry; ECharts reads the same query (charts/base.js) */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* The poll name column reads left-aligned, unlike the numeric ones. */
#polls-table th:nth-child(2), #polls-table td:nth-child(2) { text-align: left; }
