:root {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --card-bg: #f7f7f7;
  --border: #e0e0e0;
  --accent: #2166ac;
  --al-alza: #c0392b;
  --a-la-baja: #2166ac;
  --estable: #8c8c8c;
  --sin-tendencia: #b8860b;
  --insuficiente: #d9d9d9;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --fg: #eaeaea;
    --muted: #9a9a9a;
    --card-bg: #1e2126;
    --border: #30343a;
    --insuficiente: #33363c;
  }
}
:root[data-theme="dark"] {
  --bg: #14161a;
  --fg: #eaeaea;
  --muted: #9a9a9a;
  --card-bg: #1e2126;
  --border: #30343a;
  --insuficiente: #33363c;
}
:root[data-theme="light"] {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --card-bg: #f7f7f7;
  --border: #e0e0e0;
  --insuficiente: #d9d9d9;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
}

a { color: var(--accent); }

.nav {
  display: flex;
  gap: 1.25rem;
  align-items: center;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.nav .brand { font-weight: 700; margin-right: auto; }
.nav a {
  text-decoration: none;
  color: var(--fg);
  font-size: 0.92rem;
  padding: 0.3rem 0;
  border-bottom: 2px solid transparent;
}
.nav a:hover, .nav a.active { border-bottom-color: var(--accent); }

main { max-width: 1180px; margin: 0 auto; padding: 1.5rem; }

h1 { font-size: 1.6rem; margin: 0.2rem 0 0.4rem; }
h2 { font-size: 1.15rem; margin: 1.6rem 0 0.6rem; }
.subtitle { color: var(--muted); margin-bottom: 1.4rem; max-width: 60ch; }

.controls {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.controls label { font-size: 0.82rem; color: var(--muted); display: flex; flex-direction: column; gap: 0.2rem; }
select, input[type="range"] { font: inherit; }
select {
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  /* Fixed width so swapping a short label for a long one (e.g. switching
     "diagnóstico específico" options) doesn't resize the control and
     reflow/shift every other control after it in the flex-wrap row. */
  width: 210px;
}
.toggle { display: flex; gap: 0.4rem; align-items: center; font-size: 0.85rem; white-space: nowrap; }
/* .controls label above sets display:flex/flex-direction:column for the
   dropdown labels (label text stacked above its <select>) - applied to a
   .toggle's <label> too, it turns the label's own text run + the inline
   <span id="r2-thresh"> into separate anonymous flex items stacked in a
   column, i.e. 3 visual lines instead of one sentence. Force plain inline
   flow here so the span sits inline with the surrounding text. */
.toggle label { display: inline; white-space: nowrap; }

.flow-toggle { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.flow-toggle button {
  border: none; background: var(--card-bg); color: var(--fg);
  padding: 0.35rem 0.9rem; font: inherit; cursor: pointer; font-size: 0.85rem;
}
.flow-toggle button.active { background: var(--accent); color: white; }

.grid { display: grid; gap: 0.9rem; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }

.tile {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem 1rem;
  background: var(--card-bg);
}
.tile .label { font-weight: 600; font-size: 0.95rem; margin-bottom: 0.3rem; }
.tile .pct { font-size: 1.5rem; font-weight: 700; }
.tile .sentence { font-size: 0.82rem; color: var(--muted); margin-top: 0.4rem; }

.chip {
  display: inline-block; font-size: 0.72rem; font-weight: 600;
  padding: 0.1rem 0.5rem; border-radius: 999px; color: white;
}

.glyph-cell { display: flex; align-items: center; gap: 0.5rem; }

.panel {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  background: var(--card-bg);
}

.map-layout { display: flex; gap: 1.2rem; flex-wrap: wrap; align-items: flex-start; }
.map-col { flex: 1 1 420px; min-width: 320px; max-width: 560px; }
.detail-col { flex: 1 1 380px; min-width: 280px; }

#map-root {
  /* Chile is tall and narrow - the container needs to be tall (not wide)
     or Leaflet's fitBounds zooms out to fit the height and mainland Chile
     shrinks to an unreadable sliver. */
  height: 85vh;
  min-height: 700px;
  max-height: 950px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.leaflet-comuna-tooltip { font: inherit; font-size: 0.82rem; }

/* Floating hover tooltip shared by the D3 charts (charts.js Charts.tooltip). */
.chart-tooltip {
  position: fixed;
  pointer-events: none;
  z-index: 2000;
  background: var(--fg);
  color: var(--bg);
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  font-size: 0.78rem;
  max-width: 240px;
  opacity: 0;
  transition: opacity 0.08s;
}

.legend { display: flex; align-items: center; gap: 0.4rem; font-size: 0.75rem; color: var(--muted); flex-wrap: wrap; margin-top: 0.5rem; }
.legend-swatch { width: 14px; height: 14px; border-radius: 3px; display: inline-block; }

.methodology-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; margin: 0.6rem 0 1.2rem; }
.methodology-table th, .methodology-table td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); }
.methodology-table th { color: var(--muted); font-weight: 600; }

.caveat {
  border-left: 3px solid var(--sin-tendencia);
  background: var(--card-bg);
  padding: 0.7rem 1rem;
  border-radius: 0 8px 8px 0;
  font-size: 0.9rem;
  margin: 1rem 0;
}

footer { color: var(--muted); font-size: 0.8rem; padding: 2rem 1.5rem 3rem; text-align: center; }

.overflow-x { overflow-x: auto; }
/* Charts set only viewBox (no width/height attrs) so their intrinsic aspect
   ratio comes from the viewBox; without this, browsers default replaced-
   element sizing to 300x150 and stretch short/wide charts vertically. */
/* Excludes Leaflet's internal SVG overlay (#map-root .leaflet-* svg) -
   Leaflet sets that SVG's width/height/pixel coordinates itself to match
   its pane; overriding them here left every path with degenerate "M0 0"
   geometry. */
main svg:not(#map-root svg) { width: 100%; max-width: 100%; height: auto; display: block; }
/* Compact charts (few categories, short viewBox) shouldn't stretch to the
   full panel width — a wide-short viewBox forced to fill ~1100px inflates
   every text label proportionally with it. */
#sex-strip svg, #fitted-line svg { max-width: 320px; }
svg text { fill: var(--fg); }
.axis-label, .tick-label { font-size: 0.7rem; fill: var(--muted); }

.era-note { font-size: 0.72rem; color: var(--muted); }

.empty-state { color: var(--muted); font-style: italic; padding: 1rem 0; }
