:root {
  --graphite: #12151b;
  --graphite-2: #161a22;
  --steel: #1b212b;
  --steel-2: #202834;
  --line: #2c3542;
  --line-soft: #232b37;
  --ice: #e8edf2;
  --slate: #8a97a6;
  --slate-dim: #5f6b7a;
  --teal: #3fbfa3;
  --teal-dim: #2b6f63;
  --amber: #f2a93b;
  --rust: #e5654b;

  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --display: "Space Grotesk", "Inter", system-ui, sans-serif;
  --body: "Inter", system-ui, -apple-system, sans-serif;

  --r: 14px;
  --pad: clamp(16px, 2.4vw, 28px);

  /* tokens que mudam com o tema */
  --glow-teal: rgba(63, 191, 163, 0.06);
  --glow-amber: rgba(242, 169, 59, 0.05);
  --scan: rgba(255, 255, 255, 0.014);
  --shadow-card: inset 0 1px 0 rgba(255, 255, 255, 0.04), 0 12px 30px -22px rgba(0, 0, 0, 0.9);
  --shadow-panel: 0 12px 30px -24px rgba(0, 0, 0, 0.9);
  --tip-bg: #0e1116;
  --toggle-bg: var(--steel);
}

/* ---------- Tema claro: aço claro, mantendo a identidade industrial ---------- */
:root[data-theme="light"] {
  --graphite: #eaeef3;
  --graphite-2: #ffffff;
  --steel: #ffffff;
  --steel-2: #f5f8fb;
  --line: #d3dbe6;
  --line-soft: #e6ebf2;
  --ice: #18212f;
  --slate: #576373;
  --slate-dim: #8793a3;
  --teal: #1f9e85;
  --teal-dim: #8fd8c7;
  --amber: #c47f14;
  --rust: #cf4a30;

  --glow-teal: rgba(31, 158, 133, 0.09);
  --glow-amber: rgba(196, 127, 20, 0.08);
  --scan: rgba(24, 33, 47, 0.022);
  --shadow-card: 0 1px 2px rgba(16, 24, 40, 0.05), 0 14px 30px -22px rgba(16, 24, 40, 0.22);
  --shadow-panel: 0 14px 34px -26px rgba(16, 24, 40, 0.28);
  --tip-bg: #ffffff;
  --toggle-bg: var(--steel-2);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background:
    radial-gradient(1200px 600px at 80% -10%, var(--glow-teal), transparent 60%),
    radial-gradient(900px 500px at 0% 0%, var(--glow-amber), transparent 55%),
    var(--graphite);
  color: var(--ice);
  font-family: var(--body);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  transition: background-color 0.25s ease, color 0.25s ease;
}

/* leve textura de varredura de instrumento */
.scanline {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image: repeating-linear-gradient(
    to bottom,
    var(--scan) 0px,
    var(--scan) 1px,
    transparent 2px,
    transparent 4px
  );
  mask-image: linear-gradient(to bottom, rgba(0,0,0,0.5), transparent 30%);
}

.topbar, .app, .footer { position: relative; z-index: 1; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px var(--pad);
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255,255,255,0.02), transparent);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 13px; }
.brand-mark {
  display: grid; place-items: center;
  width: 46px; height: 46px;
  border-radius: 11px;
  background: linear-gradient(160deg, var(--steel-2), var(--steel));
  border: 1px solid var(--line);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.05);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong {
  font-family: var(--display);
  font-weight: 700;
  letter-spacing: 0.26em;
  font-size: 15px;
}
.brand-text span { color: var(--slate); font-size: 12.5px; letter-spacing: 0.02em; }

.topbar-meta { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.source-badge {
  display: inline-flex; align-items: center; gap: 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--slate);
  padding: 6px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--steel);
}
.source-badge .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--slate-dim);
  box-shadow: 0 0 0 0 rgba(63,191,163,0.5);
}
.source-badge[data-source="live"] { color: var(--teal); border-color: var(--teal-dim); }
.source-badge[data-source="live"] .dot {
  background: var(--teal);
  animation: pulse 2.4s ease-out infinite;
}
.source-badge[data-source="snapshot"] { color: var(--amber); border-color: rgba(242,169,59,0.4); }
.source-badge[data-source="snapshot"] .dot { background: var(--amber); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(63,191,163,0.45); }
  70% { box-shadow: 0 0 0 7px rgba(63,191,163,0); }
  100% { box-shadow: 0 0 0 0 rgba(63,191,163,0); }
}
.sync { font-family: var(--mono); font-size: 11.5px; color: var(--slate-dim); }

.theme-toggle {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--toggle-bg);
  color: var(--slate);
  cursor: pointer;
  padding: 0;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}
.theme-toggle:hover { color: var(--ice); border-color: var(--slate-dim); }
.theme-toggle svg { width: 17px; height: 17px; display: block; }
.theme-toggle .icon-moon { display: none; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* ---------- Layout ---------- */
.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: clamp(20px, 3vw, 34px) var(--pad) 12px;
  display: grid;
  gap: clamp(16px, 2vw, 22px);
}
.loading {
  font-family: var(--mono);
  color: var(--slate);
  padding: 60px 0;
  text-align: center;
  letter-spacing: 0.04em;
}

/* ---------- KPIs ---------- */
.kpis {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(14px, 1.8vw, 20px);
}
.kpi {
  position: relative;
  background: linear-gradient(165deg, var(--steel-2), var(--steel));
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 22px 22px 20px;
  min-height: 168px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
  box-shadow: var(--shadow-card);
}
.kpi::before {
  content: "";
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px;
}
.kpi--receber::before { background: var(--teal); }
.kpi--atrasado::before { background: var(--rust); }
.kpi--gauge::before { background: var(--amber); }

.kpi-eyebrow {
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--slate);
}
.kpi-value {
  font-family: var(--mono);
  font-weight: 600;
  font-size: clamp(26px, 3.4vw, 38px);
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  margin-top: auto;
}
.kpi--atrasado .kpi-value { color: var(--rust); }
.kpi-foot { font-size: 12px; color: var(--slate); line-height: 1.4; }
.kpi-foot [data-slot] { color: var(--ice); }

/* gauge card */
.kpi--gauge { align-items: center; text-align: center; }
.kpi--gauge .kpi-eyebrow { align-self: flex-start; }
.gauge { width: 100%; max-width: 230px; margin: auto; }
.gauge-value {
  font-family: var(--mono);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ---------- Painéis ---------- */
.panel {
  background: linear-gradient(165deg, var(--graphite-2), var(--steel));
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: var(--pad);
  box-shadow: var(--shadow-panel);
}
.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.panel h2 {
  margin: 0;
  font-family: var(--display);
  font-weight: 600;
  font-size: 17px;
  letter-spacing: 0.01em;
}
.panel-sub { margin: 4px 0 0; color: var(--slate); font-size: 12.5px; }

.legend { display: flex; gap: 16px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 7px;
  font-family: var(--mono); font-size: 11.5px; color: var(--slate);
  text-transform: uppercase; letter-spacing: 0.06em;
}
.swatch { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }
.swatch--receber { background: var(--teal); }
.swatch--pagar { background: var(--slate-dim); }

/* chart */
.chart-scroll { overflow-x: auto; overflow-y: hidden; margin: 0 -6px; padding: 0 6px; }
.chart { min-width: 640px; }
.chart svg { display: block; width: 100%; height: auto; }
.grid-line { stroke: var(--line-soft); stroke-width: 1; }
.axis-label { fill: var(--slate-dim); font-family: var(--mono); font-size: 10.5px; }
.area-receber { fill: url(#gReceber); }
.line-receber { stroke: var(--teal); stroke-width: 2; fill: none; }
.line-pagar { stroke: var(--slate); stroke-width: 1.6; fill: none; stroke-dasharray: 4 3; }
.dot-receber { fill: var(--teal); }
.hover-band { fill: rgba(255,255,255,0.05); }
.chart-tip {
  position: absolute;
  pointer-events: none;
  background: var(--tip-bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 12px;
  min-width: 150px;
  box-shadow: 0 14px 30px -14px rgba(0,0,0,0.9);
  transform: translate(-50%, -115%);
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 5;
}
.chart-wrap { position: relative; }
.chart-tip .tip-mes { font-family: var(--mono); color: var(--slate); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 6px; }
.chart-tip .tip-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; font-variant-numeric: tabular-nums; }
.chart-tip .tip-row b { font-family: var(--mono); font-weight: 500; }
.chart-tip .tip-k { display: inline-flex; align-items: center; gap: 6px; color: var(--slate); }

/* ---------- Aging ---------- */
.aging-headline {
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--slate);
  text-align: right;
  align-self: center;
}
.aging-headline b {
  color: var(--rust);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.aging-bar {
  display: flex;
  width: 100%;
  height: 26px;
  border-radius: 7px;
  overflow: hidden;
  gap: 2px;
  background: var(--line-soft);
}
.aging-bar .seg {
  height: 100%;
  min-width: 2px;
  transform-origin: left;
  animation: grow 0.7s cubic-bezier(0.22, 1, 0.36, 1) both;
}
.aging-legend {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
}
.aging-legend li {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-left: 11px;
  border-left: 3px solid var(--seg-color, var(--line));
}
.aging-legend .ag-faixa {
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--slate);
}
.aging-legend .ag-val {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.aging-legend .ag-meta { font-size: 11.5px; color: var(--slate-dim); }

@media (max-width: 620px) {
  .aging-legend { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Ranking ---------- */
.rank { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; counter-reset: r; }
.rank li {
  counter-increment: r;
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 14px;
}
.rank-pos {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--slate-dim);
  font-variant-numeric: tabular-nums;
}
.rank-body { min-width: 0; }
.rank-name {
  font-size: 13.5px;
  margin-bottom: 6px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rank-bar { height: 7px; border-radius: 4px; background: var(--line-soft); overflow: hidden; }
.rank-bar > i {
  display: block; height: 100%;
  background: linear-gradient(90deg, var(--teal-dim), var(--teal));
  border-radius: 4px;
  transform-origin: left;
  animation: grow 0.7s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes grow { from { transform: scaleX(0); } }
.rank-val {
  font-family: var(--mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--ice);
  white-space: nowrap;
}

/* ---------- Footer ---------- */
.footer {
  max-width: 1180px;
  margin: 8px auto 0;
  padding: 20px var(--pad) 34px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--slate-dim);
  font-size: 12px;
  font-family: var(--mono);
  letter-spacing: 0.02em;
}

/* ---------- Responsivo ---------- */
@media (max-width: 760px) {
  .kpis { grid-template-columns: 1fr; }
  .kpi { min-height: 0; }
  .kpi--gauge { order: 3; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; border-radius: 4px; }
