/* ---------------------------------------------------------------------------
   Painel do mercado de habitação em Portugal
   Tokens de cor: paleta validada para daltonismo (deuteranopia/protanopia/
   tritanopia) — pior par adjacente ΔE 9,1 em claro e 8,4 em escuro.
   Modo escuro é escolhido, não invertido: cada tom tem o seu passo próprio.

   Três tons (aqua, amarelo, magenta) ficam abaixo de 3:1 contra a superfície
   clara. É deliberado e está compensado: nenhum gráfico depende só da cor —
   há sempre legenda, rótulos diretos nas séries selecionadas, e a tabela no
   fim da página com todos os valores em texto.

   A rampa sequencial do mapa (--seq-1 .. --seq-6, do valor mais baixo para o
   mais alto) percorre tons **vizinhos e todos frios** — turquesa → ciano →
   azul → índigo — em vez de um único azul. Um azul só distingue as classes
   apenas por lightness, e no extremo claro as classes colavam umas às outras;
   ao deixar o tom variar também, cada classe passa a ter nome próprio. Foi
   gerada em OKLCH com L estritamente monótona e medida: pior par vizinho
   ΔE 11,3 em claro e 11,4 em escuro (antes 9,1 e 9,5), e a classe mais fraca
   passou a distar ΔE 9,3 do cinzento de "sem dados" (antes 5,0 — quase
   indistinguíveis).

   A rampa inverte-se no modo escuro (--seq-1 passa a ser o tom mais escuro):
   o princípio é que o valor mais baixo recua para a superfície, e num fundo
   escuro quem recua é o escuro.
   --------------------------------------------------------------------------- */
:root {
  color-scheme: light;
  --surface-1:      #fcfcfb;
  --surface-2:      #f3f3f0;
  --page:           #f9f9f7;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --grid:           #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11,11,11,0.10);
  --good:           #006300;
  --warning:        #fab219;
  --critical:       #d03b3b;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-ctx: #898781;

  --div-pos: #2a78d6;
  --div-neg: #e34948;
  --div-mid: #f0efec;

  /* Rampa sequencial do mapa: turquesa -> ciano -> azul -> índigo.
     Multi-tom por tons vizinhos (todos frios), não arco-íris. Gerada em OKLCH
     com lightness estritamente monótona; ver o cabeçalho deste ficheiro. */
  --seq-1: #95f4f9; --seq-2: #4acfee; --seq-3: #16a4df;
  --seq-4: #2a78c2; --seq-5: #384e96; --seq-6: #322c61;
  --seq-vazio: #e6e5e0;

  /* Rampa divergente para o mapa de variação (azul <-> vermelho, cinzento ao centro) */
  --dv-neg-3: #a02726; --dv-neg-2: #d0403f; --dv-neg-1: #eb9695;
  --dv-zero:  #f0efec;
  --dv-pos-1: #a8caf4; --dv-pos-2: #4a8fdd; --dv-pos-3: #17518f;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --page:           #0d0d0d;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255,255,255,0.10);
    --good:           #0ca30c;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;

    --div-pos: #3987e5;
    --div-neg: #e66767;
    --div-mid: #383835;

    --seq-1: #2f2961; --seq-2: #324b98; --seq-3: #1675c4;
    --seq-4: #11a1d9; --seq-5: #30cded; --seq-6: #8bf2f5;
    --seq-vazio: #2c2c2a;

    --dv-neg-3: #f4b4b3; --dv-neg-2: #e66767; --dv-neg-1: #8e3433;
    --dv-zero:  #383835;
    --dv-pos-1: #244a75; --dv-pos-2: #3987e5; --dv-pos-3: #9ec5f4;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --page:           #0d0d0d;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255,255,255,0.10);
  --good:           #0ca30c;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;

  --div-pos: #3987e5;
  --div-neg: #e66767;
  --div-mid: #383835;

  --seq-1: #2f2961; --seq-2: #324b98; --seq-3: #1675c4;
  --seq-4: #11a1d9; --seq-5: #30cded; --seq-6: #8bf2f5;
  --seq-vazio: #2c2c2a;

  --dv-neg-3: #f4b4b3; --dv-neg-2: #e66767; --dv-neg-1: #8e3433;
  --dv-zero:  #383835;
  --dv-pos-1: #244a75; --dv-pos-2: #3987e5; --dv-pos-3: #9ec5f4;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}
a { color: inherit; }
.wrap { max-width: 1240px; margin: 0 auto; padding: 20px 20px 80px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

/* Estas classes existem porque a Content-Security-Policy do site não permite
   atributos `style=` no HTML (ver web/_headers). Estilo inline em marcação é
   exatamente o que uma CSP restritiva bloqueia — e é o que impede que um
   futuro bug de injeção consiga desenhar o que quiser na página. */
.header-bar {
  display: flex; justify-content: space-between;
  align-items: flex-start; gap: 16px; flex-wrap: wrap;
}
.btn-row { display: flex; gap: 8px; }
.seg.spaced { margin: 8px 0 12px; }
.stat-tile.hero { margin-bottom: 6px; }
.count-note { color: var(--text-muted); font-size: 0.82rem; }
/* especificidade acima de `.field select`, que fixa min-width: 150px */
.field select.narrow { min-width: 96px; }

/* ---------------- cabeçalho ---------------- */
header.top { padding: 6px 0 16px; }
header.top h1 { font-size: 1.55rem; margin: 0 0 6px; letter-spacing: -0.01em; }
header.top p.lede { color: var(--text-secondary); margin: 4px 0 0; max-width: 80ch; }
.vintage-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.pill {
  display: inline-block; font-size: 0.72rem; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-secondary);
  border: 1px solid var(--border);
}
.pill b { color: var(--text-primary); font-weight: 600; }

/* ---------------- barra de filtros ---------------- */
.filters {
  position: sticky; top: 0; z-index: 30;
  background: var(--page);
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
  margin-bottom: 8px;
}
.filters-row {
  display: flex; flex-wrap: wrap; gap: 10px 12px; align-items: flex-end;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label {
  font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--text-muted); font-weight: 600;
}
.field select, .field input[type="search"], .field input[type="text"] {
  font: inherit; font-size: 0.86rem;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 7px;
  padding: 6px 9px; min-width: 150px; max-width: 230px;
}
.field select:focus-visible, .field input:focus-visible, button:focus-visible {
  outline: 2px solid var(--series-1); outline-offset: 1px;
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.seg button {
  font: inherit; font-size: 0.82rem; padding: 6px 11px; cursor: pointer;
  background: var(--surface-1); color: var(--text-secondary); border: 0;
  border-right: 1px solid var(--border);
}
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--series-1); color: #fff; }
.btn {
  font: inherit; font-size: 0.82rem; padding: 6px 12px; cursor: pointer;
  background: var(--surface-1); color: var(--text-secondary);
  border: 1px solid var(--border); border-radius: 7px;
}
.btn:hover { color: var(--text-primary); border-color: var(--text-secondary); }
.btn.primary { background: var(--series-1); color: #fff; border-color: transparent; }
.filters-meta {
  margin-top: 9px; font-size: 0.8rem; color: var(--text-secondary);
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.78rem; padding: 3px 6px 3px 9px; border-radius: 999px;
  background: var(--surface-1); border: 1px solid var(--border);
}
.chip .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.chip button {
  border: 0; background: transparent; cursor: pointer; color: var(--text-muted);
  font-size: 0.95rem; line-height: 1; padding: 0 3px;
}
.chip button:hover { color: var(--critical); }

/* ---------------- secções e cartões ---------------- */
section { margin: 40px 0; scroll-margin-top: 120px; }
section h2 { font-size: 1.15rem; margin: 0 0 4px; }
section .section-sub {
  color: var(--text-secondary); font-size: 0.87rem; margin: 0 0 16px; max-width: 92ch;
}
.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 10px; padding: 16px 18px;
}
.card h3 { margin: 0 0 2px; font-size: 0.95rem; }
.card .sub { font-size: 0.78rem; color: var(--text-muted); margin: 0 0 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.map-layout { display: grid; grid-template-columns: minmax(0,1fr) 260px; gap: 16px; }
@media (max-width: 960px) {
  .grid-2, .grid-4, .map-layout { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: 1fr 1fr; }
}

/* ---------------- stat tiles ---------------- */
.stat-tile .label { font-size: 0.78rem; color: var(--text-secondary); }
.stat-tile .value { font-size: 1.75rem; font-weight: 600; margin: 3px 0 2px; }
.stat-tile .delta { font-size: 0.83rem; }
.delta.up { color: var(--good); }
.delta.down { color: var(--div-neg); }
.stat-tile .foot { font-size: 0.76rem; color: var(--text-muted); margin-top: 5px; }

/* ---------------- legendas ---------------- */
.legend {
  display: flex; flex-wrap: wrap; gap: 12px; margin: 2px 0 12px;
  font-size: 0.81rem; color: var(--text-secondary);
}
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend .swatch.line { width: 15px; height: 3px; border-radius: 2px; }
.scale-legend { display: flex; flex-direction: column; gap: 4px; font-size: 0.76rem; }
.scale-legend .row { display: flex; align-items: center; gap: 8px; color: var(--text-secondary); }
.scale-legend .box { width: 22px; height: 12px; border-radius: 3px; flex: none; }

/* ---------------- tooltip ---------------- */
#tooltip {
  position: fixed; pointer-events: none; z-index: 60;
  background: var(--text-primary); color: var(--page);
  font-size: 0.78rem; padding: 7px 10px; border-radius: 6px;
  max-width: 280px; line-height: 1.4;
  opacity: 0; transform: translate(-9999px, -9999px);
  transition: opacity 0.08s ease;
  box-shadow: 0 4px 14px rgba(0,0,0,0.28);
}
#tooltip.show { opacity: 1; }
#tooltip .t-title { font-weight: 600; margin-bottom: 3px; }
#tooltip .t-row { display: flex; justify-content: space-between; gap: 12px; }
#tooltip .t-row .k { opacity: 0.75; }
#tooltip .t-row .v { font-weight: 600; font-variant-numeric: tabular-nums; }
#tooltip .t-key { display: inline-block; width: 12px; height: 2px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

/* ---------------- SVG ---------------- */
svg { display: block; }
svg text {
  fill: var(--text-secondary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
svg .axis-label { fill: var(--text-muted); font-size: 10.5px; font-variant-numeric: tabular-nums; }
svg .axis-title { fill: var(--text-muted); font-size: 10.5px; }
svg .grid-line { stroke: var(--grid); stroke-width: 1; }
svg .baseline { stroke: var(--baseline); stroke-width: 1; }
svg .zero-line { stroke: var(--baseline); stroke-width: 1; }
svg .mark-label { fill: var(--text-primary); font-size: 11px; font-weight: 600; }
svg .mark-sublabel { fill: var(--text-secondary); font-size: 10.5px; }
svg .crosshair { stroke: var(--baseline); stroke-width: 1; pointer-events: none; }
.hover-target { cursor: pointer; }
.chart-scroll { overflow-x: auto; }
.chart-note { font-size: 0.76rem; color: var(--text-muted); margin-top: 8px; }

/* mapa */
.map-shape > path, .map-shape > circle {
  stroke: var(--surface-1); stroke-width: 0.5;
}
.map-shape { cursor: pointer; }
.map-shape.selected > path, .map-shape.selected > circle {
  stroke: var(--text-primary); stroke-width: 1.6;
}
.map-shape:hover > path, .map-shape:hover > circle,
.map-shape:focus-visible > path, .map-shape:focus-visible > circle {
  stroke: var(--text-primary); stroke-width: 1.2;
}
.map-shape > circle[fill="transparent"] { stroke: none; }
.map-shape:focus { outline: none; }
.map-shape.dim { opacity: 0.18; }
.map-inset-label { fill: var(--text-muted); font-size: 10px; }

/* ---------------- tabela ---------------- */
.table-tools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 10px; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
table.data-table th, table.data-table td {
  padding: 7px 10px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data-table th.left, table.data-table td.left { text-align: left; }
table.data-table th {
  font-size: 0.72rem; color: var(--text-muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.02em; cursor: pointer; user-select: none;
  position: sticky; top: 0; background: var(--surface-1); z-index: 1;
}
table.data-table th:hover { color: var(--text-primary); }
table.data-table th[aria-sort] { color: var(--text-primary); }
table.data-table tbody tr:hover { background: rgba(128,128,128,0.07); }
table.data-table tbody tr.selected { background: rgba(42,120,214,0.10); }
table.data-table td.num { font-variant-numeric: tabular-nums; }
td.pos { color: var(--good); }
td.neg { color: var(--div-neg); }
.table-scroll { max-height: 620px; overflow: auto; }

/* ---------------- rodapé / metodologia ---------------- */
details.methodology summary { cursor: pointer; font-weight: 600; padding: 5px 0; }
details.methodology ul { color: var(--text-secondary); font-size: 0.87rem; }
details.methodology li { margin-bottom: 6px; }
.source-item { margin-bottom: 10px; }
.source-item b { color: var(--text-primary); }
footer.site-footer {
  margin-top: 56px; padding-top: 18px; border-top: 1px solid var(--border);
  color: var(--text-muted); font-size: 0.78rem;
}
.theme-toggle {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 12px; font-size: 0.78rem; color: var(--text-secondary); cursor: pointer;
}
.empty-state {
  color: var(--text-muted); font-size: 0.87rem; padding: 26px; text-align: center;
}
.loading { opacity: 0.45; transition: opacity 0.15s ease; }

@media print {
  .filters, .theme-toggle, .btn { display: none; }
}
