/* Paleta: instância de referência validada (categorical + diverging + status), claro e escuro. */
.viz-root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f3f2ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.05);
  --good: #006300;
  --bad: #d03b3b;
  --warn: #8a5a00;
  --accent: #2a78d6;
  --ref-1: #52514e; /* Ibovespa */
  --ref-2: #898781; /* CDI */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --div-neg: 227, 73, 72;
  --div-pos: 42, 120, 214;
  --div-mid: #f0efec;
  --bar-pos: #2a78d6;
  --bar-neg: #e34948;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-root {
    color-scheme: dark;
    --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #222220; --text-primary: #f0efec; --text-secondary: #c3c2b7;
    --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.06); --good: #0ca30c; --bad: #e66767; --warn: #fab219; --accent: #3987e5;
    --ref-1: #c3c2b7; --ref-2: #898781;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --div-neg: 230, 103, 103; --div-pos: 57, 135, 229; --div-mid: #383835;
    --bar-pos: #3987e5; --bar-neg: #e66767;
    --shadow: none;
  }
}
:root[data-theme="dark"] .viz-root {
  color-scheme: dark;
  --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #222220; --text-primary: #f0efec; --text-secondary: #c3c2b7;
  --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.06); --good: #0ca30c; --bad: #e66767; --warn: #fab219; --accent: #3987e5;
  --ref-1: #c3c2b7; --ref-2: #898781;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --div-neg: 230, 103, 103; --div-pos: 57, 135, 229; --div-mid: #383835;
  --bar-pos: #3987e5; --bar-neg: #e66767;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 14px; line-height: 1.45; -webkit-text-size-adjust: 100%; }
.viz-root { background: var(--page); color: var(--text-primary); min-height: 100vh; padding: 24px clamp(12px, 3vw, 36px) 40px; }
.muted { color: var(--muted); font-weight: 400; }
a { color: inherit; }
button, input { font: inherit; color: inherit; }
b { font-weight: 650; }
.up { color: var(--good); }
.down { color: var(--bad); }
.warn { color: var(--warn); }

/* ---------- topo ---------- */
.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 14px; }
.eyebrow { margin: 0 0 2px; font-size: 12px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
h1 { font-size: clamp(22px, 3vw, 28px); margin: 0; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0; font-weight: 650; letter-spacing: -.005em; }
.sub { margin: 3px 0 0; color: var(--text-secondary); font-size: 13px; }
.status { margin: 8px 0 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }
.pill { border: 1px solid var(--border); border-radius: 999px; padding: 2px 9px; color: var(--text-secondary); background: var(--surface-1); }
.top-actions { display: flex; gap: 6px; }
button.ghost { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
button.ghost:hover:not(:disabled) { background: var(--wash); }
button.ghost:disabled { opacity: .4; cursor: default; }
button.icon { width: 36px; height: 36px; padding: 0; font-size: 16px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- barra de período (gruda no topo) ---------- */
.filters {
  position: sticky; top: 0; z-index: 15; display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center;
  margin: 0 calc(-1 * clamp(12px, 3vw, 36px)) 16px; padding: 10px clamp(12px, 3vw, 36px);
  background: color-mix(in srgb, var(--page) 88%, transparent); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.presets { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-1); }
.presets button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; border-right: 1px solid var(--border); font-size: 13px; font-variant-numeric: tabular-nums; }
.presets button:last-child { border-right: 0; }
.presets button:hover { background: var(--wash); }
.presets button[aria-checked="true"] { font-weight: 700; background: var(--text-primary); color: var(--page); }
.custom { display: flex; gap: 10px; align-items: center; color: var(--text-secondary); font-size: 13px; }
.custom label { display: flex; align-items: center; gap: 6px; }
.custom input { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; }
.dates-toggle { display: none; }
.presets button.dates-toggle[aria-checked="true"] { font-weight: 700; background: var(--text-primary); color: var(--page); }

/* ---------- indicadores ---------- */
.tiles { display: grid; grid-template-columns: 1.3fr repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.tile .label { color: var(--text-secondary); font-size: 12px; font-weight: 600; }
.tile .value { font-size: 28px; font-weight: 700; margin: 4px 0 2px; letter-spacing: -.01em; }
.tile.hero .value { font-size: 34px; }
.tile .sub { color: var(--muted); font-size: 12px; }
.tile .sub b { color: var(--text-secondary); }

/* ---------- cartões ---------- */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 10px 20px; margin-bottom: 12px; }

/* resumo em texto */
.story ul { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.story li { position: relative; padding-left: 18px; color: var(--text-secondary); font-size: 14.5px; line-height: 1.55; max-width: 92ch; }
.story li::before { content: ""; position: absolute; left: 2px; top: .62em; width: 6px; height: 6px; border-radius: 50%; background: var(--axis); }
.story li b { color: var(--text-primary); }
.story li b.up { color: var(--good); }
.story li b.down { color: var(--bad); }

/* gráfico */
.toggles { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 8px; padding: 5px 10px; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.chip input { margin: 0; }
.readout { font-size: 13px; margin: 0 0 8px; font-variant-numeric: tabular-nums; min-height: 19px; }
.readout .ev, th .ev { display: inline-block; font-size: 11px; font-weight: 600; color: var(--text-secondary); border: 1px solid var(--border); border-radius: 999px; padding: 0 7px; margin-left: 4px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.legend button { display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--border); background: var(--surface-1); border-radius: 8px; padding: 5px 10px; cursor: pointer; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.legend button .ln { color: var(--text-secondary); }
.legend button .lv { color: var(--text-primary); font-weight: 650; }
.legend button .lv.up { color: var(--good); }
.legend button .lv.down { color: var(--bad); }
.legend button .lv.stale { opacity: .5; }
.legend button:hover { background: var(--wash); }
.legend button[aria-pressed="true"] { border-color: currentColor; background: var(--wash); }
.legend button[aria-pressed="true"] .ln { color: var(--text-primary); font-weight: 650; }
.legend button.dim { opacity: .45; }
.sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; flex: 0 0 auto; }
.sw.dash { background: none !important; border-top: 2px dashed currentColor; height: 0; }
.chart { position: relative; height: 420px; }
.hint { color: var(--muted); font-size: 12px; margin: 10px 0 0; }

/* ranking */
.rank-key { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
.ref-key { display: inline-block; width: 0; height: 14px; border-left: 2px solid var(--ref-1); margin-left: 6px; }
.ref-key.cdi { border-left: 2px dashed var(--ref-2); }
.rank { display: grid; gap: 2px; }
.rrow { display: grid; grid-template-columns: minmax(130px, 210px) 1fr 72px; align-items: center; gap: 12px; padding: 4px 6px; border: 0; background: none; border-radius: 6px; cursor: pointer; text-align: left; }
.rrow:hover { background: var(--wash); }
.rname { display: flex; align-items: center; gap: 8px; font-size: 13px; min-width: 0; color: var(--text-secondary); }
.rtxt { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; min-width: 0; }
.ref-edge { position: absolute; top: 4px; opacity: .55; width: 0; height: 0; border-top: 6px solid transparent; border-bottom: 6px solid transparent; }
.ref-edge.l { left: -2px; border-right: 7px solid var(--ref-1); }
.ref-edge.r { right: -2px; border-left: 7px solid var(--ref-1); }
.rrow.on .rname { color: var(--text-primary); font-weight: 600; }
.rtrack { position: relative; height: 20px; }
.rbar { position: absolute; top: 4px; height: 12px; border-radius: 3px; }
.rbar.pos { background: var(--bar-pos); }
.rbar.neg { background: var(--bar-neg); }
.zero { position: absolute; top: 0; bottom: 0; border-left: 1px solid var(--axis); }
.ref-line { position: absolute; top: -2px; bottom: -2px; z-index: 1; }
.ref-line.ibov { border-left: 2px solid var(--ref-1); }
.ref-line.cdi { border-left: 2px dashed var(--ref-2); }
.rval { text-align: right; font-size: 13px; font-weight: 650; font-variant-numeric: tabular-nums; }

/* tabelas */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--grid); }
th { font-size: 12px; font-weight: 600; color: var(--text-secondary); cursor: pointer; user-select: none; background: var(--surface-1); }
th:hover { color: var(--text-primary); }
th.sorted { color: var(--text-primary); }
th.sorted::after { content: " ▾"; }
th.sorted.asc::after { content: " ▴"; }
td.l, th.l { text-align: left; }
tbody tr:hover { background: var(--wash); }
tr.ref td { color: var(--text-secondary); font-style: italic; background: var(--surface-2); }
#table td:first-child, #table th:first-child, .heat td.l, .heat th.l { position: sticky; left: 0; z-index: 1; background: var(--surface-1); }
#table tr.ref td:first-child { background: var(--surface-2); }
.fname { font-weight: 600; }
.fsub { color: var(--muted); font-size: 11px; }
.pick { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.pick input { margin: 0; width: 16px; height: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; border: 1px solid var(--border); flex: 0 0 auto; }
.rm { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 4px; }
.rm:hover { color: var(--bad); }
.spark { vertical-align: middle; }
.pending td { color: var(--muted); }

.heat td { text-align: center; font-size: 12px; padding: 6px; border: 2px solid var(--surface-1); border-radius: 4px; min-width: 52px; }
.heat td.l { text-align: left; border: 0; border-bottom: 1px solid var(--grid); }
.heat th { text-align: center; cursor: default; vertical-align: bottom; }
.heat th.l { text-align: left; }
.heat th.evcol { color: var(--text-primary); }
.scale { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
.scale .bar { width: 140px; height: 8px; border-radius: 4px; }

/* busca */
.search { position: relative; }
.search input { width: min(380px, 80vw); background: var(--page); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.results { position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; width: min(560px, 92vw); max-height: 360px; overflow: auto; background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.results button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--grid); }
.results button:hover { background: var(--wash); }
.results .rn { font-size: 13px; }
.results .rs { font-size: 11px; color: var(--muted); }

/* glossário */
.glossary summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; }
.glossary summary::-webkit-details-marker { display: none; }
.glossary summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
.glossary details[open] summary::before { transform: rotate(90deg); }
.glossary dl { display: grid; grid-template-columns: max-content 1fr; gap: 10px 20px; margin: 16px 0 0; }
.glossary dt { font-weight: 650; }
.glossary dd { margin: 0; color: var(--text-secondary); max-width: 80ch; }

.tooltip { display: none; }
footer { color: var(--muted); font-size: 12px; line-height: 1.6; padding: 4px 2px; }

/* ---------- telas menores ---------- */
@media (max-width: 900px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .viz-root { padding: 14px 10px 28px; }
  .filters { margin: 0 -10px 12px; padding: 8px 10px; gap: 8px; }
  .presets { flex-wrap: nowrap; overflow-x: auto; width: 100%; scrollbar-width: none; }
  .presets::-webkit-scrollbar { display: none; }
  .presets button { flex: 1 0 auto; padding: 9px 11px; }
  .dates-toggle { display: block; }
  .custom { width: 100%; display: none; }
  .filters.show-dates .custom { display: flex; }
  .eyebrow { font-size: 11px; }
  .custom label { flex: 1; }
  .custom input { flex: 1; min-width: 0; padding: 6px; }
  .tiles { gap: 8px; margin-bottom: 12px; }
  .tile { padding: 11px 12px; border-radius: 12px; }
  .tile .value, .tile.hero .value { font-size: 22px; }
  .tile .sub { font-size: 11px; }
  .card { padding: 14px 12px; border-radius: 12px; margin-bottom: 12px; }
  h2 { font-size: 15px; }
  .story li { font-size: 14px; padding-left: 14px; }
  .chart { height: 340px; margin: 0 -4px; }
  .legend { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
  .legend button { padding: 7px 8px; min-height: 36px; min-width: 0; }
  .legend button .ln { flex: 1; min-width: 0; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .toggles { width: 100%; }
  .toggles #fit { margin-left: auto; }
  .rrow { grid-template-columns: 122px 1fr 58px; gap: 8px; padding: 6px 4px; }
  .rname { font-size: 12px; }
  .rval { font-size: 12px; }
  .search, .search input { width: 100%; }
  th, td { padding: 7px 7px; font-size: 12px; }
  #table td:first-child { min-width: 150px; max-width: 175px; white-space: normal; }
  .cnpj { display: none; }
  .heat td { min-width: 46px; font-size: 11px; padding: 5px 4px; }
  .heat td.l { max-width: 120px; overflow: hidden; text-overflow: ellipsis; }
  .glossary dl { grid-template-columns: 1fr; gap: 4px; }
  .glossary dd { margin-bottom: 8px; }
}
@media (forced-colors: active) { .sw, .dot, .rbar { forced-color-adjust: none; } }
