/* ---------- Tokens ---------- */
:root {
  color-scheme: light dark;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --raised: #ffffff;
  --line: #e4e7eb;
  --track: #eceff2;
  --text: #11161d;
  --muted: #5d6673;
  --live: #16794c;
  --offline: #c2352b;
  --warn-bg: #fff6dc;
  --warn-line: #ecd08a;
  --warn-text: #6b4a00;
  --focus: #1f6fe0;
  --shadow: 0 1px 2px rgb(16 24 40 / .04), 0 1px 3px rgb(16 24 40 / .06);
  --radius: 14px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Party colours follow broadcast convention. Ink is the text colour used on a party fill. */
  --nat: #1f6fe0; --nat-ink: #ffffff;
  --lab: #c8102e; --lab-ink: #ffffff;
  --grn: #2ea84f; --grn-ink: #06240f;
  --act: #d81b84; --act-ink: #ffffff;
  --nzf: #1d2a5c; --nzf-ink: #ffffff;
  --opp: #0e9fbd; --opp-ink: #04232b;
  --tpm: #8c1c3c; --tpm-ink: #ffffff;
  --other: #8b94a1; --other-ink: #11161d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --raised: #2e3642;
    --line: #262d37;
    --track: #232a34;
    --text: #eef2f6;
    --muted: #9aa4b2;
    --live: #5fd39b;
    --offline: #ff7b72;
    --warn-bg: #2a2310;
    --warn-line: #5c4a1a;
    --warn-text: #f2cc6b;
    --focus: #79b0ff;
    --shadow: none;

    --nat: #4d94ff; --lab: #f0444a; --grn: #6bd88a; --act: #ff5cb4;
    --nzf: #d3d9e6; --opp: #1fb3cf; --tpm: #a8455c; --other: #7d8796;
    --nat-ink: #0d1117; --lab-ink: #0d1117; --grn-ink: #0d1117; --act-ink: #0d1117;
    --nzf-ink: #0d1117; --opp-ink: #0d1117; --tpm-ink: #ffffff; --other-ink: #0d1117;
  }
}

[data-party] { --party: var(--other); --party-ink: var(--other-ink); }
[data-party="nat"] { --party: var(--nat); --party-ink: var(--nat-ink); }
[data-party="lab"] { --party: var(--lab); --party-ink: var(--lab-ink); }
[data-party="grn"] { --party: var(--grn); --party-ink: var(--grn-ink); }
[data-party="act"] { --party: var(--act); --party-ink: var(--act-ink); }
[data-party="nzf"] { --party: var(--nzf); --party-ink: var(--nzf-ink); }
[data-party="opp"] { --party: var(--opp); --party-ink: var(--opp-ink); }
[data-party="tpm"] { --party: var(--tpm); --party-ink: var(--tpm-ink); }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}
p { margin: 0; }
a { color: inherit; text-underline-offset: 3px; }
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }

.shell {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}
@media (min-width: 640px) { .shell { padding: 0 28px; } }

/* ---------- Header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 72px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -.03em;
  line-height: 1;
  min-width: 0;
}
.brand-mark { display: flex; align-items: flex-end; gap: 3px; height: 18px; }
.brand-mark i { width: 4px; border-radius: 1px; background: var(--text); }
.brand-mark i:nth-child(1) { height: 8px; opacity: .45; }
.brand-mark i:nth-child(2) { height: 13px; opacity: .7; }
.brand-mark i:nth-child(3) { height: 18px; }
.wordmark { white-space: nowrap; }
.wordmark .dot { color: var(--muted); }
.edition {
  padding-left: 12px;
  border-left: 1px solid var(--line);
  color: var(--muted);
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0;
  white-space: nowrap;
}

.status { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.status-line { display: inline-flex; align-items: center; gap: 7px; }
.status-text { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.3; }
#connection { font-size: 14px; font-weight: 650; }
#checked-at { font-size: 13px; color: var(--muted); }
#checked-at:empty { display: none; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.status[data-state="live"] .status-dot { background: var(--live); animation: pulse 2.4s ease-out infinite; }
.status[data-state="delayed"] .status-dot { background: var(--warn-text); }
.status[data-state="offline"] .status-dot { background: var(--offline); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--live) 45%, transparent); }
  70%, 100% { box-shadow: 0 0 0 7px transparent; }
}

.refresh {
  position: relative;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
.refresh:hover { background: var(--track); }
.refresh .ring { position: absolute; inset: 0; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 2.5; }
.ring-track { stroke: var(--line); }
.ring-fill { stroke: var(--muted); stroke-linecap: round; transition: stroke-dashoffset 1s linear; }
.status:not([data-state="live"]) .ring-fill { opacity: 0; }
.refresh .icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--text);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.refresh:disabled { cursor: progress; }
.refresh:disabled .icon { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Notice ---------- */
.notice {
  margin: 4px 0 16px;
  padding: 10px 14px;
  border: 1px solid var(--warn-line);
  border-radius: 10px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
}

/* ---------- Count progress ---------- */
.count {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 4px 20px;
  padding: 12px 0 24px;
}
.count-figure { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; }
.count-figure strong { font-size: 44px; font-weight: 700; letter-spacing: -.035em; line-height: 1.05; }
.count-figure span, .count-votes span { color: var(--muted); }
.count-votes { text-align: right; }
.count-votes strong { font-size: 17px; font-weight: 650; }
.count-track { grid-column: 1 / -1; height: 6px; margin-top: 12px; }

.track { background: var(--track); border-radius: 99px; overflow: hidden; }
.track > span { display: block; width: 0; height: 100%; border-radius: 99px; background: var(--text); transition: width .6s ease; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-title, .section-title { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -.01em; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.card-meta { color: var(--muted); }
.card-meta b { color: var(--text); font-weight: 650; }
.placeholder { color: var(--muted); padding: 8px 0; }

.national {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/* ---------- Rows (party vote and electorate) ---------- */
.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 40px;
  height: 22px;
  padding: 0 6px;
  border-radius: 5px;
  background: var(--party);
  color: var(--party-ink);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: .04em;
  line-height: 1;
}

.row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 4.5rem;
  grid-template-areas: "name share" "bar votes";
  align-items: center;
  gap: 6px 14px;
  padding: 11px 0;
}
.row + .row { border-top: 1px solid var(--line); }
.row-name { grid-area: name; display: flex; align-items: center; gap: 10px; min-width: 0; font-weight: 600; line-height: 1.3; }
.row-name > span:not(.chip) { overflow-wrap: anywhere; }
.row-share { grid-area: share; text-align: right; font-size: 16px; font-weight: 700; }
.row-votes { grid-area: votes; text-align: right; font-size: 13px; color: var(--muted); }
.bar { grid-area: bar; height: 8px; border-radius: 4px; background: var(--track); overflow: hidden; }
.bar i { display: block; width: var(--w); height: 100%; border-radius: 4px; background: var(--party); transition: width .5s ease; }

.parties .card-title { margin-bottom: 4px; }
.party-rows { container: parties / inline-size; }
@container parties (min-width: 500px) {
  .party-row {
    grid-template-columns: minmax(0, 11.5rem) minmax(0, 1fr) 4rem 5.5rem;
    grid-template-areas: "name bar share votes";
    column-gap: 16px;
  }
  .party-row .row-votes { font-size: 14px; }
}

.more {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  margin-top: 4px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
}
.more::after { content: ""; width: 6px; height: 6px; margin: -3px 0 0 9px; border: solid currentColor; border-width: 0 1.6px 1.6px 0; transform: rotate(45deg); }
.more[aria-expanded="true"]::after { margin-top: 3px; transform: rotate(-135deg); }
.more:hover { color: var(--text); }

/* ---------- Seats ---------- */
.bloc-totals { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin: 18px 0 8px; color: var(--muted); font-size: 14px; }
.bloc-total { display: inline-flex; align-items: baseline; gap: 7px; }
.bloc-total b { color: var(--text); font-size: 20px; font-weight: 700; }
.majority-flag { align-self: center; padding: 1px 8px; border: 1px solid var(--line); border-radius: 99px; color: var(--text); font-size: 12px; font-weight: 650; }
.seat-figure { position: relative; margin: 22px 0 0; padding-bottom: 30px; }
.bloc-totals:not([hidden]) + .seat-figure { margin-top: 0; }
.seat-chart { display: flex; gap: 2px; height: 28px; }
.seat-chart:empty { border-radius: 4px; background: var(--track); }
.seg { min-width: 2px; border-radius: 4px; background: var(--party); }
.seg.bloc-start { margin-left: 4px; }
.majority {
  position: absolute;
  top: -5px;
  width: 2px;
  height: 38px;
  margin-left: -1px;
  border-radius: 1px;
  background: var(--text);
  box-shadow: 0 0 0 2px var(--surface);
}
.majority span {
  position: absolute;
  top: 42px;
  left: 1px;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
/* Two columns that sit under the matching ends of the bar. */
.seat-legend { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px 20px; margin: 14px 0 0; }
.bloc-list { display: flex; flex-wrap: wrap; align-content: start; gap: 10px 14px; margin: 0; padding: 0; list-style: none; }
.bloc-list.right { justify-content: flex-end; }
.bloc-list.other { grid-column: 1 / -1; }
.seat-legend li { display: flex; align-items: center; gap: 7px; }
.seat-legend b { font-size: 16px; font-weight: 700; }
.legend-name { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
/* Beside the party table there is room to name each party, which also balances the two cards. */
@media (min-width: 821px) {
  .seat-legend { margin-top: 10px; }
  .bloc-list { display: block; }
  .seat-legend li { gap: 10px; padding: 8px 0; }
  .seat-legend li + li { border-top: 1px solid var(--line); }
  .legend-name { position: static; width: auto; height: auto; overflow: visible; clip: auto; flex: 1; font-weight: 600; }
}

/* ---------- Electorates ---------- */
.races { margin-top: 36px; }
.section-title { margin-bottom: 12px; }
.race-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; align-items: start; }
.race-grid > .placeholder { grid-column: 1 / -1; }

.race-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.race-head h3 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -.015em; line-height: 1.25; }
.race-count { color: var(--muted); font-size: 14px; white-space: nowrap; }
.race-track { height: 4px; margin: 12px 0 16px; }
.race-warning { margin: -4px 0 14px; color: var(--warn-text); font-size: 14px; }

.leader { margin-bottom: 18px; }
.leader-name { font-size: 21px; font-weight: 700; letter-spacing: -.015em; line-height: 1.25; }
.leader-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-top: 8px; color: var(--muted); font-size: 14px; }
.leader-meta b { color: var(--text); font-weight: 650; }
.leader-empty { color: var(--muted); font-size: 16px; }
.surname { font-variant-caps: all-small-caps; letter-spacing: .03em; }

.race-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  padding: 3px;
  margin-bottom: 4px;
  border-radius: 10px;
  background: var(--track);
}
.race-tabs button {
  min-height: 36px;
  padding: 6px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
}
.race-tabs button:hover { color: var(--text); }
.race-tabs button[aria-pressed="true"] { background: var(--raised); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .12); }
.candidate-row .bar { height: 6px; }
.candidate-row .row-name { font-weight: 550; }
.race-foot { margin-top: 10px; padding-top: 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.race .more { margin-top: 0; }

/* ---------- Footer ---------- */
.foot {
  margin-top: 40px;
  padding: 20px 0 max(32px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}
.foot p { max-width: 68ch; }

/* ---------- Utilities ---------- */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.skip-link {
  position: fixed;
  top: 10px;
  left: 16px;
  z-index: 10;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  opacity: 0;
  pointer-events: none;
  transform: translateY(-200%);
}
.skip-link:focus { opacity: 1; pointer-events: auto; transform: none; }
.noscript { max-width: 600px; margin: 20px auto; padding: 20px; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .race-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 820px) {
  .national { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .edition { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; padding: 0; border: 0; }
  .top { min-height: 64px; }
  .count { grid-template-columns: minmax(0, 1fr); padding: 8px 0 20px; }
  .count-figure strong { font-size: 40px; }
  .count-votes { text-align: left; }
  .count-track { margin-top: 10px; }
  .card { padding: 16px; }
  .races { margin-top: 28px; }
  .foot { margin-top: 32px; }
}
@media (max-width: 380px) {
  .brand-mark { display: none; }
  .brand { font-size: 19px; }
  .chip { min-width: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
