:root {
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --ink: #1b1b1a;
  --ink-2: #4a4a47;
  --muted: #76756f;
  --line: #e2e0da;
  --grid: #ecebe6;
  --neutro: #efeeea;
  --sem-dados: #cfcdc7;
  --borda-mapa: #fcfcfb;
  --foco: #1b1b1a;
  --ok: #2e7d32;
  --alerta: #b26a00;
  --erro: #b3261e;
  --raio: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121211;
    --surface: #1a1a19;
    --ink: #f1f0ec;
    --ink-2: #c9c8c2;
    --muted: #9a9993;
    --line: #2f2f2c;
    --grid: #262624;
    --neutro: #2b2b29;
    --sem-dados: #4a4946;
    --borda-mapa: #1a1a19;
    --foco: #f1f0ec;
    --ok: #66bb6a;
    --alerta: #ffb74d;
    --erro: #ef5350;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121211; --surface: #1a1a19; --ink: #f1f0ec; --ink-2: #c9c8c2; --muted: #9a9993;
  --line: #2f2f2c; --grid: #262624; --neutro: #2b2b29; --sem-dados: #4a4946; --borda-mapa: #1a1a19;
  --foco: #f1f0ec; --ok: #66bb6a; --alerta: #ffb74d; --erro: #ef5350; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
button { font: inherit; color: inherit; }

.layout {
  display: grid;
  grid-template-columns: minmax(340px, 400px) 1fr;
  min-height: 100vh;
}
.painel {
  position: sticky; top: 0;
  height: 100vh; overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 16px 20px 12px;
  display: flex; flex-direction: column; gap: 12px;
}
.mapa-area { padding: 16px 20px 20px; display: flex; flex-direction: column; min-width: 0; }

.topo { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
h1 { font-size: 19px; margin: 0; letter-spacing: -0.01em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.status { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: var(--ink-2); white-space: nowrap; padding-top: 2px; --rel-cor: var(--muted); }
.status.ok { --rel-cor: var(--ok); }
.status.alerta { --rel-cor: var(--alerta); }
.status.erro { --rel-cor: var(--erro); }
.status-txts { display: flex; flex-direction: column; line-height: 1.25; }
#status-txt { font-weight: 600; color: var(--ink); }
.verificado { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Relógio de atualização */
.relogio { width: 30px; height: 30px; flex: none; overflow: visible; }
.rel-face { fill: color-mix(in srgb, var(--rel-cor) 8%, transparent); stroke: var(--grid); stroke-width: 2.4; }
.rel-arco {
  fill: none; stroke: var(--rel-cor); stroke-width: 2.4; stroke-linecap: round;
  stroke-dasharray: 100; stroke-dashoffset: 100;
  transform: rotate(-90deg); transform-origin: 12px 12px;
}
.rel-ponteiro { stroke: var(--ink-2); stroke-width: 1.8; stroke-linecap: round; transform-origin: 12px 12px; }
.rel-centro { fill: var(--rel-cor); }
.relogio.girando .rel-arco { animation: rel-arco var(--ciclo, 15s) linear forwards; }
.relogio.girando .rel-ponteiro { animation: rel-giro var(--ciclo, 15s) linear forwards; }
.relogio.pulso { animation: rel-pulso .7s ease-out; transform-origin: center; }
.status.erro .rel-arco, .status.erro .rel-ponteiro { animation-play-state: paused; }
@keyframes rel-arco { from { stroke-dashoffset: 100; } to { stroke-dashoffset: 0; } }
@keyframes rel-giro { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
@keyframes rel-pulso {
  0% { transform: scale(1); filter: none; }
  30% { transform: scale(1.2); filter: drop-shadow(0 0 4px var(--rel-cor)); }
  100% { transform: scale(1); filter: none; }
}
@media (prefers-reduced-motion: reduce) {
  .relogio.girando .rel-ponteiro, .relogio.pulso { animation: none; }
}

.aviso-painel { display: none; }
.aviso { border: 1px solid var(--line); border-left: 3px solid var(--alerta); background: var(--bg); padding: 5px 8px; border-radius: 6px; font-size: 12px; line-height: 1.35; color: var(--ink-2); }
.aviso-mapa { margin-bottom: 10px; font-size: 13px; padding: 7px 10px; }

.rotulo { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.hero { margin: 0; font-size: 28px; font-weight: 700; line-height: 1.05; letter-spacing: -0.02em; }
.meta { margin: 2px 0 0; font-size: 12px; color: var(--ink-2); }
.barra { height: 6px; background: var(--grid); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.barra-fill { height: 100%; width: 0; background: var(--ink-2); border-radius: 3px; transition: width .6s ease; }

/* subgrid: foto/nome, percentual e votos ficam na mesma linha nos dois cards */
.cands { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto auto; column-gap: 10px; row-gap: 0; margin-top: 16px; }
.cand {
  position: relative;
  display: grid; grid-row: span 3; grid-template-rows: subgrid; align-content: start;
  border: 1px solid var(--line); border-radius: var(--raio); padding: 8px 12px 10px;
  border-top: 4px solid var(--cor); background: var(--surface); min-width: 0;
}
/* "lidera": aba presa na borda superior, fora da caixa */
.aba-lider {
  position: absolute; bottom: 100%; right: 10px;
  background: var(--cor); color: #fff;
  font-size: 11px; font-weight: 650; line-height: 1.3; letter-spacing: .02em;
  padding: 3px 10px 5px; border-radius: 6px 6px 0 0;
}
.cand.lider { box-shadow: inset 0 0 0 1px var(--cor); }
.cand-topo { display: flex; align-items: center; gap: 10px; min-width: 0; align-self: center; }
.cand-id { min-width: 0; }
.cand-foto { width: 48px; height: 48px; border-radius: 8px; object-fit: cover; object-position: center 20%; flex: none; background: var(--grid); box-shadow: 0 0 0 2px var(--cor); }
.cand-nome { margin: 0; font-weight: 650; font-size: 15px; overflow-wrap: anywhere; }
.cand-part { margin: 0; color: var(--muted); font-size: 12px; }
.cand-pct { margin: 6px 0 0; font-size: 26px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.cand-votos { margin: 2px 0 0; color: var(--ink-2); font-size: 13px; }

.dif-valor { margin: 0; font-size: 18px; font-weight: 700; }
.chip { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: 1px; }

.tl-topo { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.tl-topo .rotulo { margin: 0; }
/* a timeline ocupa o espaço que sobra no painel: visível sem rolar no desktop */
.timeline { flex: 1 1 0; min-height: 150px; display: flex; flex-direction: column; }
.tl-caixa { position: relative; flex: 1 1 0; min-height: 105px; margin-top: 6px; }
/* canvas fora do fluxo: o tamanho vem da caixa (flex), não do canvas */
.tl-caixa canvas { position: absolute; inset: 0; }
.tl-lado { position: absolute; left: 46px; font-size: 11px; color: var(--muted); pointer-events: none; z-index: 1; }
.tl-lado::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 2px; background: var(--cor); margin-right: 5px; vertical-align: 0; }
.tl-a { top: 2px; }
.tl-b { bottom: 30px; }
.tl-vazio { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; color: var(--muted); font-size: 13px; margin: 0; padding: 0 24px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--bg); }
.seg button { border: 0; background: transparent; padding: 4px 10px; border-radius: 6px; font-size: 13px; cursor: pointer; color: var(--ink-2); min-height: 30px; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
button:focus-visible, [tabindex]:focus-visible, summary:focus-visible { outline: 2px solid var(--foco); outline-offset: 2px; }

.rodape { font-size: 12px; color: var(--muted); line-height: 1.4; margin-top: 6px; }
.rodape span { display: block; margin-top: 2px; }

.mapa-barra { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.legenda { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.leg-grupo { display: flex; align-items: center; gap: 6px; }
.leg-escala { display: flex; gap: 2px; }
.leg-escala span { width: 18px; height: 10px; border-radius: 2px; display: block; }
.leg-sem { width: 18px; height: 10px; border-radius: 2px; display: inline-block; background: var(--sem-dados); background-image: repeating-linear-gradient(45deg, transparent 0 3px, rgba(255,255,255,.55) 3px 4.5px); }

.mapa-caixa { position: relative; flex: 1; min-height: 420px; margin-top: 8px; }
#mapa { width: 100%; height: 100%; display: block; position: absolute; inset: 0; }
#mapa .area { stroke: var(--borda-mapa); stroke-width: 1; cursor: pointer; transition: fill .4s ease; }
#mapa .area:hover, #mapa .area.ativa { stroke: var(--ink); stroke-width: 2; }
#mapa .contorno-reg { fill: none; stroke: var(--borda-mapa); stroke-width: 3; pointer-events: none; }
#mapa .rot { font-weight: 650; text-anchor: middle; pointer-events: none; paint-order: stroke; stroke-width: 3px; stroke-linejoin: round; }
#mapa .rot .pct { font-weight: 500; font-size: .9em; }
#mapa .guia { stroke: var(--muted); stroke-width: 1; }

.exterior {
  position: absolute; left: 8px; bottom: 8px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
  padding: 8px 10px; font-size: 12px; cursor: pointer; min-width: 150px;
}
.exterior .ext-tit { font-weight: 650; display: flex; align-items: center; gap: 6px; }
.exterior .ext-sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.exterior .ext-l { color: var(--ink-2); }

.tooltip {
  position: absolute; z-index: 5; pointer-events: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.14);
  padding: 10px 12px; font-size: 13px; min-width: 210px; max-width: 280px;
}
.tooltip.fixo { pointer-events: auto; }
.tooltip h3 { margin: 0 0 2px; font-size: 14px; }
.tooltip .tt-sub { color: var(--muted); font-size: 12px; margin: 0 0 8px; }
.tooltip .tt-linha { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; margin: 2px 0; }
.tooltip .tt-linha b { font-weight: 650; }
.tooltip .tt-votos { color: var(--ink-2); font-size: 12px; }
.tooltip .tt-dif { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--line); }
.tooltip .tt-fechar { position: absolute; top: 4px; right: 6px; border: 0; background: transparent; font-size: 18px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px; display: none; }
.tooltip.fixo .tt-fechar { display: block; }

.mapa-nota { margin: 8px 0 0; font-size: 12px; color: var(--muted); }

.tabela { margin-top: 10px; font-size: 13px; }
.tabela summary { cursor: pointer; color: var(--ink-2); padding: 4px 0; }
.tabela-rolagem { overflow-x: auto; }
.tabela table { border-collapse: collapse; width: 100%; min-width: 520px; }
.tabela th, .tabela td { padding: 5px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.tabela th:first-child, .tabela td:first-child { text-align: left; }
.tabela th { color: var(--muted); font-weight: 600; font-size: 12px; }

@media (max-width: 860px) {
  .aviso-painel { display: block; }
  .aviso-mapa { display: none; }
  .layout { grid-template-columns: 1fr; }
  .painel { position: static; height: auto; overflow: visible; border-right: 0; border-bottom: 1px solid var(--line); padding: 16px; }
  .mapa-area { padding: 12px 16px 20px; }
  .mapa-caixa { min-height: 0; flex: none; display: flex; flex-direction: column; }
  #mapa { position: relative; inset: auto; height: auto; aspect-ratio: 1 / 1; }
  .hero { font-size: 30px; }
  .timeline { flex: none; }
  .tl-caixa { flex: none; height: 220px; }
  .painel { gap: 14px; }
  .cand-pct { font-size: 24px; }
  .exterior { position: static; margin-top: 4px; align-self: flex-start; }
  .tooltip { max-width: calc(100vw - 48px); }
}
