* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

:root, [data-theme="light"] {
  --max-w: min(1200px, 96vw);

  /* Cores neutras (modo claro por defeito) */
  --bg: #f4f6f9;
  --fg: #222;
  --muted: #555;
  --muted-2: #999;
  --muted-3: #aaa;
  --muted-4: #ccc;
  --card-bg: #ffffff;
  --card-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  --card-shadow-sm: 0 1px 6px rgba(0, 0, 0, 0.06);
  --header-bg: #1a1a2e;
  --header-fg: #ffffff;
  --border: #eee;
  --row-hover: #f0f4ff;
  --grid-line: rgba(0, 0, 0, 0.06);
  --grid-line-soft: rgba(0, 0, 0, 0.05);
  --grid-line-dashed: rgba(0, 0, 0, 0.15);
  --nb-fill: rgba(60, 60, 60, 0.30);
  --nb-stroke: rgba(60, 60, 60, 0.55);
  --noscript-bg: #fff3cd;
  --noscript-border: #ffe69c;
  --noscript-fg: #664d03;
  --error-bg: #fff3cd;
  --error-border: #ffc107;
  --error-fg: #856404;
  --tooltip-bg: rgba(40, 40, 40, 0.92);
  --tooltip-fg: #ffffff;
  /* Texto das legendas/eixos do Chart.js (segue o tema) */
  --chart-text: #1a1a2e;
  --chart-text-muted: #555;

  /* Cores partidárias (mantidas em ambos os temas) */
  --cor-ps: #e84393;
  --cor-ps-text: #c0187a;
  --cor-psd: #f47216;
  --cor-psd-text: #c05a00;
  --cor-ad: #1565c0;
  --cor-sempre: #2e7d32;
  --cor-cdu: #c62828;
  --cor-cdu-text: #b71c1c;
  --cor-prd: #795548;
  --cor-il: #00acc1;
  --cor-il-text: #00838f;
  --cor-outros: #7878b4;
}

[data-theme="dark"] {
  --bg: #121316;
  --fg: #e8e8ec;
  --muted: #c4c4cc;
  --muted-2: #9a9aa2;
  --muted-3: #7e7e88;
  --muted-4: #4a4a52;
  --card-bg: #1c1d22;
  --card-shadow: 0 2px 14px rgba(0, 0, 0, 0.55);
  --card-shadow-sm: 0 1px 6px rgba(0, 0, 0, 0.40);
  --header-bg: #2a2a3e;
  --header-fg: #f0f0f5;
  --border: #2c2d33;
  --row-hover: #232430;
  --grid-line: rgba(255, 255, 255, 0.10);
  --grid-line-soft: rgba(255, 255, 255, 0.07);
  --grid-line-dashed: rgba(255, 255, 255, 0.22);
  --nb-fill: rgba(220, 220, 220, 0.32);
  --nb-stroke: rgba(220, 220, 220, 0.60);
  --noscript-bg: #3a2f10;
  --noscript-border: #6b5520;
  --noscript-fg: #f5e2a8;
  --error-bg: #3a2f10;
  --error-border: #6b5520;
  --error-fg: #f5e2a8;
  --tooltip-bg: rgba(245, 245, 245, 0.95);
  --tooltip-fg: #1a1a1a;
  /* Texto das legendas/eixos do Chart.js no modo escuro */
  --chart-text: #ececf1;
  --chart-text-muted: #c4c4cc;
}

body {
  font-family: 'Segoe UI', Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  padding: clamp(10px, 2.2vw, 28px);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  transition: background-color 0.18s ease, color 0.18s ease;
}

button, a, .wb-cell { touch-action: manipulation; }

/* Tap targets mínimos 44x44px (iOS HIG / WCAG 2.5.5) */
button {
  min-height: 44px;
}

header.page-header {
  max-width: var(--max-w);
  margin: 0 auto;
}

h1 {
  text-align: center;
  font-size: clamp(1rem, 2.4vw, 1.6rem);
  margin-bottom: 6px;
  color: var(--header-bg);
  line-height: 1.25;
}

[data-theme="dark"] h1 { color: var(--fg); }

p.subtitle {
  text-align: center;
  font-size: clamp(0.72rem, 1.4vw, 0.95rem);
  color: var(--muted);
  margin-bottom: clamp(14px, 2.5vw, 30px);
}

.intro {
  max-width: var(--max-w);
  margin: 0 auto clamp(18px, 2.6vw, 30px);
  font-size: clamp(0.85rem, 1.4vw, 1rem);
  line-height: 1.55;
  color: var(--fg);
}

.intro p { margin-bottom: 0.7em; }
.intro p:last-child { margin-bottom: 0; }

.chart-container {
  max-width: var(--max-w);
  margin: 0 auto;
  background: var(--card-bg);
  border-radius: 12px;
  padding: clamp(12px, 2vw, 30px) clamp(10px, 2vw, 26px) clamp(10px, 1.5vw, 22px);
  box-shadow: var(--card-shadow);
}

.chart-box {
  position: relative;
  width: 100%;
  height: clamp(280px, 44vw, 480px);
}

.chart-box.line { height: clamp(260px, 36vw, 420px); }

.chart-box canvas {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  width: 100% !important;
  height: 100% !important;
}

@media (max-width: 700px) {
  .chart-box      { height: clamp(300px, 75vw, 420px); }
  .chart-box.line { height: clamp(280px, 70vw, 400px); }
}

.chart-hint {
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 4px;
}

.winner-bar {
  max-width: var(--max-w);
  margin: 20px auto 0;
  display: grid;
  /* grid-template-columns set by JS */
  border-radius: 8px;
  overflow: hidden;
}

.wb-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #fff;
  padding: 2px 2px;
  border-right: 1px solid rgba(255, 255, 255, 0.25);
  min-width: 0;
  overflow: hidden;
}

.wb-cell:last-child,
.wb-cell.last-col { border-right: none; }

.wb-ano {
  font-size: clamp(0.5rem, 0.95vw, 0.75rem);
  font-weight: 700;
  padding: clamp(4px, 0.6vw, 8px) 2px clamp(2px, 0.3vw, 4px);
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.wb-nome {
  font-size: clamp(0.45rem, 0.85vw, 0.7rem);
  font-weight: 700;
  white-space: nowrap;
  writing-mode: vertical-lr;
  transform: rotate(180deg);
  padding: clamp(4px, 0.6vw, 8px) 0;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.wb-partido {
  font-size: clamp(0.48rem, 0.9vw, 0.72rem);
  font-weight: 600;
  padding: clamp(2px, 0.3vw, 4px) 2px clamp(4px, 0.6vw, 8px);
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.table-wrap {
  max-width: var(--max-w);
  margin: clamp(14px, 2.5vw, 28px) auto 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 10px;
  box-shadow: var(--card-shadow-sm);
  background: var(--card-bg);
}

table.results {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
  font-size: clamp(0.7rem, 1.2vw, 0.88rem);
  background: var(--card-bg);
}

table.results caption {
  caption-side: top;
  text-align: left;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
}

table.results th {
  background: var(--header-bg);
  color: var(--header-fg);
  padding: 8px 10px;
  text-align: center;
}

/* Coluna nulos/brancos — só o cabeçalho destacado; células iguais às restantes */
table.results th.col-nb {
  background: #444;
  color: #ffffff;
}

[data-theme="dark"] table.results th.col-nb {
  background: #38383f;
  color: #f0f0f5;
}

table.results td {
  padding: 6px 10px;
  text-align: center;
  border-bottom: 1px solid var(--border);
  color: var(--fg);
  white-space: nowrap;
}

table.results tr:last-child td { border-bottom: none; }

@media (hover: hover) {
  table.results tr:hover td { background: var(--row-hover); }
}

/* Todos os números da tabela herdam color: var(--fg) (preto/branco conforme tema). */
.cell-empty { color: var(--muted-4); }

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  color: #fff;
}

.badge.ps     { background: var(--cor-ps); }
.badge.psd    { background: var(--cor-psd); }
.badge.ad     { background: var(--cor-ad); }
.badge.sempre { background: var(--cor-sempre); }
.badge.psd-sempre {
  background: repeating-linear-gradient(-45deg,
    var(--cor-psd) 0, var(--cor-psd) 5px,
    var(--cor-sempre) 5px, var(--cor-sempre) 10px);
}

.nota-sempre {
  font-size: 0.7rem;
  color: var(--muted);
  font-style: italic;
  display: block;
  margin-top: 2px;
  font-weight: normal;
}

h2.section-title {
  text-align: center;
  font-size: clamp(0.95rem, 1.8vw, 1.25rem);
  color: var(--header-bg);
  margin: clamp(20px, 3vw, 36px) 0 8px;
}

[data-theme="dark"] h2.section-title { color: var(--fg); }

.toolbar {
  max-width: var(--max-w);
  margin: 0 auto clamp(10px, 1.6vw, 18px);
  display: flex;
  justify-content: flex-start;
  gap: 10px;
  align-items: center;
}

.toolbar.toolbar-top {
  margin-top: 0;
  margin-bottom: clamp(14px, 2vw, 22px);
}

/* Botões da toolbar — partilham TODOS os estilos para garantir consistência visual */
.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: clamp(8px, 1.2vw, 11px) clamp(12px, 1.8vw, 16px);
  background: var(--card-bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: clamp(0.85rem, 1.1vw, 0.95rem);
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--card-shadow-sm);
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

@media (hover: hover) {
  .toolbar-btn:hover { background: var(--row-hover); }
}

.toolbar-btn:focus-visible {
  outline: 3px solid var(--cor-il);
  outline-offset: 2px;
}

/* Ícones sol/lua: trocados conforme o tema activo */
.theme-btn .ic-sun  { display: none; }
.theme-btn .ic-moon { display: inline-block; }
[data-theme="dark"] .theme-btn .ic-sun  { display: inline-block; }
[data-theme="dark"] .theme-btn .ic-moon { display: none; }

@media (max-width: 480px) {
  .theme-btn .theme-label { display: none; }
}

.loading {
  max-width: var(--max-w);
  margin: 30px auto;
  text-align: center;
  font-size: 0.95rem;
  color: var(--muted);
  padding: 20px;
}

.error-banner {
  max-width: var(--max-w);
  margin: 20px auto;
  padding: 14px 18px;
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: 10px;
  color: var(--error-fg);
  font-size: 0.9rem;
  line-height: 1.45;
}

footer.page-footer {
  max-width: var(--max-w);
  margin: clamp(20px, 3vw, 32px) auto 0;
  padding: 14px 0;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

footer.page-footer a {
  color: var(--cor-ps-text);
  text-decoration: none;
  display: inline-block;
  padding: 4px 2px;
}

@media (hover: hover) {
  footer.page-footer a:hover { text-decoration: underline; }
}

[data-theme="dark"] footer.page-footer a { color: var(--cor-il); }

footer.page-footer .validacao {
  font-size: 0.74rem;
  line-height: 1.5;
  max-width: 800px;
  margin: 8px auto;
  color: var(--muted);
}

footer.page-footer .validacao code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--row-hover);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.92em;
}

footer.page-footer .fontes {
  margin-bottom: 6px;
}

.noscript-msg {
  max-width: 600px;
  margin: 30px auto;
  padding: 18px 20px;
  background: var(--noscript-bg);
  border: 1px solid var(--noscript-border);
  border-radius: 10px;
  color: var(--noscript-fg);
  font-size: 0.95rem;
  line-height: 1.45;
  text-align: center;
}

.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;
}

/* Em telemóveis muito estreitos, só mostra o ano na faixa de vencedores */
@media (max-width: 480px) {
  .wb-nome    { display: none; }
  .wb-partido { font-size: 0.5rem; }
}

/* Ecrãs muito largos: aumenta um pouco o limite */
@media (min-width: 1600px) {
  :root { --max-w: 1400px; }
}
