/* ==========================================================================
   AlmoxSystem — responsive chart system
   Shared app tokens. Pairs with graficos.js.

   THE CONTRACT
   Every chart renders at its ZERO state in markup, then grows when the
   `.chart` wrapper gains `.in-view`. Growth is driven entirely by CSS custom
   properties (`--v`, `--len`, `--from`, `--to`) so there is no per-frame JS
   and the animation runs on the compositor.
   ========================================================================== */

.chart {
  position: relative;
  --reveal-delay: 0ms;
  --font-system: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-family: var(--font-system);
  color: var(--label, var(--color-label));
  min-width: 0;
}

/* Staggered cascade: charts.js sets --i on each animatable child */
.chart [style*="--i"] { --reveal-delay: calc(var(--i) * var(--stagger-step)); }

/* -------------------------------------------------------------- PLOT AREA -- */
.chart-plot {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: clamp(6px, 1.4vw, 18px);
  padding: 12px 8px 30px 58px;
}
.chart-grid {
  position: absolute;
  inset: 12px 8px 30px 58px;
  pointer-events: none;
}
.chart-grid span {
  position: absolute; left: 0; right: 0;
  height: 1px;
  background: var(--viz-grid);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform var(--duration-slow) var(--ease-out) var(--reveal-delay);
}
.chart.in-view .chart-grid span { transform: scaleX(1); }
.chart-grid b {
  position: absolute; right: calc(100% + 8px);
  transform: translateY(-50%);
  font: 500 11px/1.4 var(--font-system);
  font-variant-numeric: tabular-nums;
  color: var(--label2, var(--viz-axis-label));
  white-space: nowrap;
}

/* ------------------------------------------------------------ VERTICAL BARS */
.bar-group {
  position: relative;
  flex: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 5px;
  height: 100%;
  min-width: 0;
}
/* ⚠️ A barra nasce com a ALTURA FINAL e cresce por `transform`, não por `height`.
   Animar altura obriga o navegador a refazer o layout a cada quadro, e a coluna é o
   gráfico com mais elementos da tela. O `scaleY` ancorado embaixo dá exatamente o
   mesmo movimento no compositor. A deformação do raio e do fio de luz durante o
   crescimento é transitória e some no repouso (scale 1) — diferente do esticão
   permanente do `lineArea`, que precisou de contra-escala. */
.bar {
  position: relative;
  width: 34%;
  max-width: 34px;
  min-width: 3px;
  height: calc(var(--v) * 1%);
  border-radius: 6px 6px 2px 2px;
  background: var(--c, var(--viz-1));
  transform: scaleY(0);
  transform-origin: bottom;
  transition: transform var(--duration-chart) var(--spring-subtle) var(--reveal-delay);
  will-change: transform;
}
.chart.in-view .bar { transform: scaleY(1); }
.bar:hover { filter: brightness(1.04); }

/* Stacked variant — segments grow together inside a full-height column */
.bar--stack {
  display: flex;
  flex-direction: column-reverse;
  background: none;
  box-shadow: none;
  overflow: hidden;
}
/* Aqui quem cresce é a COLUNA inteira (ela é `.bar`, herda o scaleY acima) e os
   segmentos ficam parados na altura final. Escalar segmento por segmento abriria
   vãos entre eles no meio da animação: cada um encolheria em torno do próprio pé,
   mas continuaria plantado na posição final que o flex já reservou. */
.bar--stack > i {
  display: block;
  height: calc(var(--v) * 1%);
  background: var(--c, var(--viz-1));
}

.bar-x {
  position: absolute;
  bottom: -25px; left: -4px; right: -4px;
  text-align: center;
  font: 500 11px/1.4 var(--font-system);
  color: var(--label2, var(--viz-axis-label));
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* Value printed above the bar, fading in after growth completes */
.bar-v {
  position: absolute;
  bottom: calc(100% + 7px);
  left: 50%; transform: translateX(-50%);
  font: 600 11px/1 var(--font-system);
  font-variant-numeric: tabular-nums;
  color: var(--label2, var(--color-label-secondary));
  white-space: nowrap;
  opacity: 0;
  transition: opacity var(--duration-normal) var(--ease-out)
              calc(var(--reveal-delay) + var(--duration-chart) * 0.7);
}
.chart.in-view .bar-v { opacity: 1; }

/* ---------------------------------------------------------- HORIZONTAL BARS */
.hbar-row {
  display: grid;
  grid-template-columns: minmax(120px, 1.4fr) minmax(80px, 2fr) minmax(76px, auto);
  align-items: center;
  gap: var(--space-3);
  padding: 13px 0;
  min-height: 48px;
}
.hbar-row + .hbar-row { border-top: 1px solid var(--color-separator); }
.hbar-name {
  font: 500 13px/1.5 var(--font-system);
  color: var(--label, var(--color-label));
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hbar-name small {
  display: block;
  font: 400 11px/1.5 var(--font-system);
  color: var(--label2, var(--color-label-tertiary));
}
.hbar-track {
  height: 8px;
  border-radius: 6px;
  background: var(--viz-track);
  overflow: hidden;
}
/* Mesma troca da coluna, deitada: largura final desde o começo, crescimento por
   `scaleX` ancorado à esquerda. A ponta arredondada some no meio da animação, mas
   a trilha já a recorta (`overflow:hidden`) — no repouso ela volta exata. */
.hbar-fill {
  height: 100%;
  width: calc(var(--v) * 1%);
  border-radius: inherit;
  background: var(--c, var(--viz-1));
  transform: scaleX(0);
  transform-origin: left;
  transition: transform var(--duration-chart) var(--spring-subtle) var(--reveal-delay);
  will-change: transform;
}
.chart.in-view .hbar-fill { transform: scaleX(1); }
.hbar-val {
  text-align: right;
  font: 600 13px/1.3 var(--font-system);
  font-variant-numeric: tabular-nums;
  color: var(--color-label);
}
.val-pos { color: var(--sentiment-up); }
.val-neg { color: var(--sentiment-down); }

/* ------------------------------------------------------------- LINE / AREA -- */
.linechart { display: block; width: 100%; min-height: 230px; overflow: visible; }
.linechart .grid-line { stroke: var(--viz-grid); stroke-width: 1; stroke-dasharray: 3 5; }
.linechart .series-line {
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  stroke-dasharray: var(--len);
  stroke-dashoffset: var(--len);
  transition: stroke-dashoffset var(--duration-chart) var(--ease-out) var(--reveal-delay);
}
.chart.in-view .linechart .series-line { stroke-dashoffset: 0; }

.linechart .series-area {
  opacity: 0;
  transition: opacity var(--duration-slow) var(--ease-out)
              calc(var(--reveal-delay) + var(--duration-chart) * 0.35);
}
.chart.in-view .linechart .series-area { opacity: 1; }

.linechart .dot {
  r: 3.5;
  stroke: var(--surface, var(--color-bg-primary));
  stroke-width: 1.5;
  opacity: 0;
  transition: opacity var(--duration-normal) var(--ease-out)
              calc(var(--reveal-delay) + var(--duration-chart) * 0.6),
              r var(--duration-fast) var(--spring-fast);
}
.chart.in-view .linechart .dot { opacity: 1; }
.linechart .dot:hover, .linechart .dot:focus-visible { r: 5.5; }
.linechart .axis-label {
  font: 500 11px/1.4 var(--font-system);
  fill: var(--label2, var(--viz-axis-label));
}

/* Contra-escala do esticão do viewBox — quem calcula `--kx` é `ajustarLinha()` no
   graficos.js, e o porquê está lá. A geometria FICA esticada (é ela que faz o
   gráfico ocupar cartões largos sem crescer de altura); texto e ponto voltam à
   proporção certa. Cada um gira em torno da própria âncora, senão o rótulo do eixo
   Y descola da linha e o do X sai do lugar do ponto. */
.linechart text, .linechart .dot {
  transform-box: fill-box;
  transform: scale(var(--chart-label-x, 1), var(--chart-label-y, 1));
}
.linechart .axis-x, .linechart .dot { transform-origin: center; }
.linechart .axis-y, .linechart .ref-label { transform-origin: right center; }
/* Fio fino esticado some numa direção e engrossa na outra. */
.linechart .grid-line, .linechart .ref-line { vector-effect: non-scaling-stroke; }
.linechart .ref-label { font: 600 11px/1.4 var(--font-system); fill: var(--label2, var(--viz-axis-label)); }
.linechart .ref-line { stroke: var(--label3, var(--viz-axis-label)); stroke-width: 1; stroke-dasharray: 5 5; }

/* ------------------------------------------------------------------ DONUT --- */
.donut-card {
  display: flex;
  align-items: center;
  gap: var(--space-6);
  flex-wrap: wrap;
}
.donut-wrap { position: relative; width: 172px; height: 172px; flex: none; }
.donut { width: 100%; height: 100%; transform: rotate(-90deg); }
.donut-track { fill: none; stroke: var(--viz-track); }
.donut-seg {
  fill: none;
  stroke-linecap: butt;
  stroke-dasharray: var(--len) var(--gap);
  stroke-dashoffset: var(--len);        /* start collapsed */
  transition: stroke-dashoffset var(--duration-chart) var(--spring-subtle) var(--reveal-delay);
}
.chart.in-view .donut-seg { stroke-dashoffset: var(--off); }
.donut-seg:hover { filter: brightness(1.1); }

.donut-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center;
  pointer-events: none;
}
.donut-total {
  font: 800 20px/24px var(--font-system);
  letter-spacing: -0.6px;
  font-variant-numeric: tabular-nums;
  color: var(--color-label);
}
.donut-label { max-width: 120px; font: 400 11px/1.4 var(--font-system); color: var(--label2, var(--color-label-secondary)); }

/* Legend beside the donut, never below — reclaims horizontal space */
.donut-legend { flex: 1 1 220px; min-width: 0; margin: 0; padding: 0; display: flex; flex-direction: column; }
.donut-legend li {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  align-items: center;
  gap: var(--space-2);
  padding: 11px 0;
  list-style: none;
  font: 400 12px/1.45 var(--font-system);
  opacity: 0;
  transform: translateX(-6px);
  transition: opacity var(--duration-normal) var(--ease-out) var(--reveal-delay),
              transform var(--duration-normal) var(--spring-subtle) var(--reveal-delay);
}
.chart.in-view .donut-legend li { opacity: 1; transform: none; }
.donut-legend li + li { border-top: 1px solid var(--color-separator); }
.donut-legend i { width: 12px; height: 12px; border-radius: 4px; }
.donut-legend b { font-variant-numeric: tabular-nums; font-weight: 700; }
.donut-legend span { min-width: 0; overflow-wrap: anywhere; color: var(--label2, var(--color-label-secondary)); }
.donut-legend small { display: inline-block; margin-left: 5px; font-size: 11px; font-weight: 500; }

/* --------------------------------------------------------- PROGRESS RINGS --- */
.ring-row { display: flex; gap: var(--space-6); flex-wrap: wrap; }
.ring { position: relative; flex: none; }
.ring svg { transform: rotate(-90deg); display: block; }
.ring-track { fill: none; stroke: var(--viz-track); }
.ring-fill {
  fill: none;
  stroke-linecap: round;
  stroke-dasharray: var(--len);
  stroke-dashoffset: var(--len);
  transition: stroke-dashoffset var(--duration-chart) var(--spring-subtle) var(--reveal-delay);
}
.chart.in-view .ring-fill { stroke-dashoffset: var(--off); }
.ring-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center;
}
.ring-value {
  font: 800 19px/22px var(--font-system);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.4px;
  color: var(--color-label);
}
.ring-label { font: 400 11px/1.4 var(--font-system); color: var(--label2, var(--color-label-secondary)); }

/* ------------------------------------------------------------- KPI TILES ---- */
/* Compact by design: 16–18px padding, 23px value, 15px title. */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-3);
}
.kpi {
  padding: 17px;
  border-radius: var(--radius-md);
  display: flex; flex-direction: column; gap: 6px;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity var(--duration-slow) var(--ease-out) var(--reveal-delay),
              transform var(--duration-slow) var(--spring-subtle) var(--reveal-delay),
              box-shadow var(--duration-normal) var(--ease-effects);
}
.chart.in-view .kpi { opacity: 1; transform: none; }
.kpi:hover { box-shadow: var(--shadow-4); transform: translateY(-2px); }
.kpi-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.kpi-title {
  font: var(--text-card-title);
  color: var(--color-label-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.kpi-icon {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  border-radius: var(--radius-xs);
  background: var(--c-soft, var(--viz-1-soft));
  color: var(--c, var(--viz-1));
  box-shadow: 0 4px 12px -6px var(--c, var(--viz-1));
}
.kpi-value {
  font: var(--text-kpi);
  letter-spacing: -0.8px;
  font-variant-numeric: tabular-nums;
  color: var(--color-label);
}
/* Never wraps — a delta pill dropping to a second line makes tiles uneven */
.kpi-foot {
  display: flex; align-items: center; gap: 6px;
  font: var(--text-caption1);
  flex-wrap: nowrap; min-width: 0;
}
.kpi-delta {
  display: inline-flex; align-items: center; gap: 3px;
  padding: 2px 7px;
  border-radius: var(--radius-full);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.kpi-delta.up   { color: var(--sentiment-up);   background: rgba(22, 163, 74, 0.12); }
.kpi-delta.down { color: var(--sentiment-down); background: rgba(220, 38, 38, 0.12); }
.kpi-delta.flat { color: var(--sentiment-flat); background: var(--color-fill-tertiary); }
.kpi-delta { flex: none; }
.kpi-note {
  color: var(--color-label-tertiary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}

/* --------------------------------------------------------------- SPARKLINE -- */
.sparkline { display: block; width: 100%; height: 34px; overflow: visible; }
.sparkline path.spark-line {
  fill: none; stroke-width: 2; stroke-linecap: round;
  stroke-dasharray: var(--len); stroke-dashoffset: var(--len);
  transition: stroke-dashoffset var(--duration-slower) var(--ease-out) var(--reveal-delay);
}
.chart.in-view .sparkline path.spark-line { stroke-dashoffset: 0; }
.sparkline path.spark-area {
  opacity: 0;
  transition: opacity var(--duration-slow) var(--ease-out)
              calc(var(--reveal-delay) + 250ms);
}
.chart.in-view .sparkline path.spark-area { opacity: 1; }

/* ------------------------------------------------------------------ FUNNEL -- */
.funnel { display: flex; flex-direction: column; gap: 3px; }

/* Name in a gutter, tapered shape in the plot column. The shape can never
   clip the stage name, at any card width. */
.funnel-stage {
  height: 52px;
  display: grid;
  grid-template-columns: minmax(72px, 24%) 1fr;
  align-items: center;
  gap: var(--space-3);
  transform: scaleY(0.02);
  transform-origin: top;
  opacity: 0;
  transition: transform var(--duration-slow) var(--spring-subtle) var(--reveal-delay),
              opacity var(--duration-normal) var(--ease-out) var(--reveal-delay);
}
.chart.in-view .funnel-stage { transform: none; opacity: 1; }

.funnel-name {
  font: 700 12.5px/1.3 var(--font-system);
  color: var(--color-label-secondary);
  text-align: right;
  overflow: hidden;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;
}
.funnel-plot { position: relative; height: 100%; display: grid; place-items: center; }
.funnel-shape {
  position: absolute; inset: 0;
  background: var(--c, var(--viz-1));
  clip-path: polygon(var(--l) 0, calc(100% - var(--l)) 0, calc(100% - var(--r)) 100%, var(--r) 100%);
}
.funnel-val {
  position: relative;
  display: inline-flex; align-items: center;
  color: #fff;
  font: 800 15px/1 var(--font-system);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  text-shadow: none;
}
/* Drop-off sits on a colored gradient, so it reads as translucent white
   rather than sentiment red — red on red is invisible. */
.funnel-drop {
  font: 600 11px/1.4 var(--font-system); font-style: normal;
  color: rgba(255, 255, 255, 0.85);
  margin-left: var(--space-2);
  padding: 2px 6px;
  border-radius: var(--radius-full);
  background: rgba(0, 0, 0, 0.18);
}

/* ----------------------------------------------------------------- HEATMAP -- */
.heatmap { display: grid; gap: 3px; align-items: center; }
.heat-corner { }
.heat-col, .heat-row {
  font: 500 11px/1.4 var(--font-system);
  color: var(--label2, var(--viz-axis-label));
  white-space: nowrap;
}
.heat-col { text-align: center; }
.heat-row { padding-right: var(--space-2); text-align: right; }
.heat-cell {
  aspect-ratio: 1;
  border-radius: var(--radius-xs);
  background: var(--c);
  transform: scale(0.4);
  opacity: 0;
  transition: transform var(--duration-normal) var(--spring-fast) var(--reveal-delay),
              opacity var(--duration-fast) var(--ease-out) var(--reveal-delay);
}
.chart.in-view .heat-cell { transform: none; opacity: 1; }
.heat-cell:hover { outline: 2px solid var(--color-label); outline-offset: 1px; }

/* --------------------------------------------------------------- WATERFALL -- */
.waterfall {
  position: relative;
  display: flex; align-items: flex-end;
  gap: var(--space-2);
  padding-bottom: 26px;   /* gutter for the .bar-x labels */
}
.wf-col { position: relative; flex: 1; height: 100%; }
/* Cada degrau cresce a partir do próprio piso (`--base`), que é o que faz a cascata
   ler como saldo corrente — por isso a âncora é embaixo, como na coluna. */
.wf-bar {
  position: absolute; left: 15%; right: 15%;
  bottom: calc(var(--base) * 1%);
  height: calc(var(--v) * 1%);
  border-radius: 6px 6px 2px 2px;
  background: var(--c);
  transform: scaleY(0);
  transform-origin: bottom;
  transition: transform var(--duration-chart) var(--spring-subtle) var(--reveal-delay);
}
.chart.in-view .wf-bar { transform: scaleY(1); }
/* Dashed bridge from this column's closing level into the next column */
.wf-connector {
  position: absolute;
  left: 15%; right: -15%;
  height: 0;
  border-top: 1px dashed var(--color-label-tertiary);
  opacity: 0;
  transition: opacity var(--duration-normal) var(--ease-out)
              calc(var(--reveal-delay) + var(--duration-chart) * 0.75);
}
.chart.in-view .wf-connector { opacity: 1; }

/* ------------------------------------------------------------------ LEGEND -- */
.chart-legend {
  display: flex; flex-wrap: wrap;
  gap: 8px 18px;
  margin-bottom: 18px;
  min-height: 20px;
}
.chart-legend span {
  display: inline-flex; align-items: center; gap: 7px;
  font: 500 12px/1.5 var(--font-system);
  color: var(--label2, var(--color-label-secondary));
}
.chart-legend i { width: 8px; height: 8px; border-radius: 3px; flex: none; }
/* Vertical legend for panoramic charts — sits beside, not below */
.chart-legend--side { flex-direction: column; gap: var(--space-2); margin: 0; }

/* ----------------------------------------------------------------- TOOLTIP -- */
.chart-tip {
  position: fixed;
  z-index: 400;
  box-sizing: border-box;
  max-width: min(320px, calc(100vw - 24px));
  padding: 10px 13px;
  border-radius: 10px;
  background: var(--viz-tooltip-bg);
  color: var(--viz-tooltip-fg);
  font: 500 12px/1.55 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-variant-numeric: tabular-nums;
  white-space: pre-line;
  overflow-wrap: anywhere;
  box-shadow: 0 8px 28px rgba(15, 23, 42, .16);
  pointer-events: none;
  opacity: 0;
  transform: translateY(3px);
  transition: opacity 120ms ease, transform 120ms ease;
}
.chart-tip.show { opacity: 1; transform: none; }
.chart-tip b { font-weight: 800; }

[data-tip]:focus-visible {
  outline: 2px solid var(--acento, var(--viz-1));
  outline-offset: 4px;
  border-radius: 4px;
}
.linechart .dot:focus-visible, .donut-seg:focus-visible {
  outline: none;
  stroke: var(--label, var(--color-label));
  stroke-width: 2.5;
}

/* ------------------------------------------------------------- EMPTY STATE -- */
.chart-empty {
  display: grid; place-items: center;
  min-height: 200px;
  gap: var(--space-2);
  padding: 24px;
  color: var(--label2, var(--color-label-tertiary));
  font: 400 13px/1.6 var(--font-system);
  text-align: center;
}

@media (max-width: 640px) {
  .chart-plot { min-height: 230px; gap: 5px; padding-left: 48px; }
  .chart-grid { left: 48px; }
  .bar-group { gap: 3px; }
  .bar { max-width: 24px; border-radius: 6px 6px 1px 1px; }
  .hbar-row { grid-template-columns: minmax(0, 1fr) auto; gap: 7px 12px; }
  .hbar-name { grid-column: 1; grid-row: 1; white-space: normal; }
  .hbar-val { grid-column: 2; grid-row: 1; }
  .hbar-track { grid-column: 1 / -1; grid-row: 2; }
  .donut-card { justify-content: center; gap: 18px; }
  .donut-legend { flex-basis: 100%; }
  .donut-legend li { min-height: 44px; }
  .chart-legend { gap: 8px 14px; margin-bottom: 16px; }
  .kpi-grid { grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); }
}

/* ==========================================================================
   Reduce Motion — charts appear fully drawn, instantly. The DATA is never
   what's animated away; only the transition is.
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  .chart *, .chart *::before, .chart *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
