:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e4e7ec;
  --text: #101828;
  --text-2: #475467;
  --text-3: #98a2b3;
  /* Verde da marca W3Life (#32b86c), escurecido no tema claro para contraste AA com texto branco */
  --brand: #32b86c;
  --accent: #1a8048;
  --accent-soft: #e8f6ee;
  --on-accent: #ffffff;
  --pos: #12b76a;
  --neg: #f04438;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --sidebar: 232px;

  /* Escala tipográfica (use sempre estes tokens; nada de px solto):
     xs  11  rótulos de eixo, sobretítulos em caixa-alta
     sm  12  legendas, rótulos de campo/KPI, chips, cabeçalho de tabela, subtítulos
     md  13  tabelas, listas, botões pequenos, mensagens
     base 14 texto padrão, botões, campos
     lg  16  título de card, valores em texto
     xl  20  números de destaque secundários (mini KPIs, células da matriz)
     2xl 24  título da página e número dos KPIs */
  --fs-xs: 11px; --fs-sm: 12px; --fs-md: 13px; --fs-base: 14px;
  --fs-lg: 16px; --fs-xl: 20px; --fs-2xl: 24px;

  --seg-campeoes: #16a34a;
  --seg-crescimento: #0ea5e9;
  --seg-novos: #8b5cf6;
  --seg-risco: #ef4444;
  --seg-esfriando: #f59e0b;
  --seg-perdidos: #94a3b8;

  --c1: #1a8048; --c2: #0ea5e9; --c3: #6366f1; --c4: #f59e0b;
  --c5: #ef4444; --c6: #8b5cf6; --c7: #ec4899; --c8: #f97316; --c9: #64748b; --c10: #a16207;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d12; --surface: #13161d; --surface-2: #1b1f28; --border: #262b36;
    --text: #f2f4f7; --text-2: #c0c6d1; --text-3: #7a8394;
    --accent: #32b86c; --accent-soft: #10291b; --on-accent: #0b0d12; --c1: #32b86c;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d12; --surface: #13161d; --surface-2: #1b1f28; --border: #262b36;
  --text: #f2f4f7; --text-2: #c0c6d1; --text-3: #7a8394;
  --accent: #32b86c; --accent-soft: #10291b; --on-accent: #0b0d12; --c1: #32b86c;
  --shadow: none; color-scheme: dark;
}

/* Logo: versão preta no tema claro, branca no escuro */
.logo { display: block; height: 30px; width: auto; }
.logo-light { display: block; }
.logo-dark { display: none; }
.logo-mark { display: none; width: 32px; height: 32px; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: var(--fs-base)/1.5 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: var(--fs-2xl); line-height: 1.25; }
h2 { font-size: var(--fs-lg); line-height: 1.35; }
h3 { font-size: var(--fs-md); color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.subtle { color: var(--text-3); font-size: var(--fs-sm); }
.fs-xs { font-size: var(--fs-xs); } .fs-sm { font-size: var(--fs-sm); } .fs-md { font-size: var(--fs-md); } .fs-lg { font-size: var(--fs-lg); }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 12px; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: grid; gap: 6px; padding: 0 10px 18px; }
.brand-name { font-size: var(--fs-xs); font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); }
.nav-group { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); padding: 14px 10px 4px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-2); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }
.sidebar-foot { margin-top: auto; padding: 12px 10px 0; border-top: 1px solid var(--border); display: grid; gap: 8px; }

.brand-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.brand-row .brand { min-width: 0; }
#collapse-btn svg { transition: transform .2s; }

/* Sidebar recolhida (só desktop): faixa de ícones */
@media (min-width: 821px) {
  .shell { transition: grid-template-columns .2s ease; }
  .shell.collapsed { --sidebar: 68px; }
  .shell.collapsed .sidebar { padding: 20px 10px; overflow-x: hidden; }
  .shell.collapsed .brand-row { flex-direction: column; align-items: center; gap: 10px; padding-bottom: 8px; }
  .shell.collapsed .brand { padding: 0; }
  /* recolhida: troca a logo completa pelo símbolo (o mesmo arquivo do favicon) */
  .shell.collapsed .brand .logo { display: none; }
  .shell.collapsed .brand .logo-mark { display: block; }
  .shell.collapsed .brand-name,
  .shell.collapsed .nav-label,
  .shell.collapsed .refresh-label,
  .shell.collapsed #refresh-info,
  .shell.collapsed .user-name { display: none; }
  .shell.collapsed .nav-group { font-size: 0; padding: 0; margin: 10px 6px; border-top: 1px solid var(--border); }
  .shell.collapsed .nav a { justify-content: center; padding: 9px 0; }
  .shell.collapsed .sidebar-foot { padding: 12px 0 0; justify-items: center; }
  .shell.collapsed #refresh-btn { width: 40px; padding: 0; }
  .shell.collapsed .user-row { flex-direction: column; }
  .shell.collapsed .user-row > span { flex-direction: column; }
  .shell.collapsed #collapse-btn svg { transform: rotate(180deg); }
}
@media (max-width: 820px) { #collapse-btn { display: none; } }

.main { min-width: 0; padding: 28px 32px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 8px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }
.topbar { display: none; }

/* ---------- componentes ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head p { margin: 2px 0 0; font-size: var(--fs-sm); color: var(--text-3); }
.grid { display: grid; gap: 16px; }
/* Espaçamento vertical só entre blocos empilhados (nunca entre itens lado a lado de um grid) */
#view > * + *, .stack > * + * { margin-top: 16px; }
/* Coluna empilhada dentro de um grid: ocupa a altura da linha e o último card estica */
.stack { display: flex; flex-direction: column; min-width: 0; }
.stack > :last-child { flex: 1; }
/* Card cujo corpo cresce para preencher a altura disponível */
.card.fill { display: flex; flex-direction: column; }
.card.fill > .card-body { flex: 1; min-height: 0; }
.scroll-y { overflow-y: auto; }
/* Corpo que não "empurra" a altura da linha: acompanha o card vizinho e rola por dentro */
.card-body.follow { position: relative; min-height: 420px; }
/* se estende até a borda do card (como .table-wrap) para a rolagem não cortar tabelas */
.card-body.follow > .follow-inner { position: absolute; top: 0; bottom: 0; left: -20px; right: -20px; padding: 0 20px; overflow-y: auto; }
.scroll-box { max-height: 520px; overflow-y: auto; margin: 0 -20px; padding: 0 20px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cols-3-1 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }

.kpi .label { font-size: var(--fs-sm); color: var(--text-2); font-weight: 500; }
.kpi .value { font-size: var(--fs-2xl); font-weight: 600; letter-spacing: -.02em; margin-top: 4px; }
.kpi .foot { font-size: var(--fs-sm); color: var(--text-3); margin-top: 2px; }
.delta { font-size: var(--fs-sm); font-weight: 600; }
.delta.up { color: var(--pos); } .delta.down { color: var(--neg); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm); font: inherit; font-weight: 500;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.sm { height: 30px; padding: 0 10px; font-size: var(--fs-md); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn.icon { width: 32px; padding: 0; }
.mini-kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.mini-kpis .value { font-size: var(--fs-xl); font-weight: 600; letter-spacing: -.02em; }
.mini-kpis .label { font-size: var(--fs-sm); color: var(--text-2); }
.mini-kpis .value.text { font-size: var(--fs-lg); line-height: 1.3; margin: 2px 0; }
/* Alerta com a cor do segmento (ex.: Em risco em vermelho, não na cor primária) */
.banner[class*="seg-"] { background: color-mix(in srgb, var(--seg) 9%, var(--surface)); color: var(--text); border-left: 3px solid var(--seg); }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field, div.field { display: grid; gap: 4px; align-content: start; font-size: var(--fs-sm); color: var(--text-2); font-weight: 500; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 8px; border-radius: 999px;
  font-size: var(--fs-sm); font-weight: 500; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.chip.ok { background: color-mix(in srgb, var(--pos) 14%, transparent); color: var(--pos); }
.chip-toggle { cursor: pointer; border: 1px solid var(--border); background: var(--surface); }
.chip-toggle.on { border-color: currentColor; }
/* Botão "×" para remover um chip (tags, filtros) */
.btn.sm.chip-x { height: 18px; padding: 0 2px; }
/* Link que envolve um bloco (card, KPI) sem mudar a cor nem sublinhar */
a.plain, a.plain:hover { color: inherit; text-decoration: none; }

.seg-campeoes { --seg: var(--seg-campeoes); }
.seg-crescimento { --seg: var(--seg-crescimento); }
.seg-novos { --seg: var(--seg-novos); }
.seg-risco { --seg: var(--seg-risco); }
.seg-esfriando { --seg: var(--seg-esfriando); }
.seg-perdidos { --seg: var(--seg-perdidos); }
.chip.seg { background: color-mix(in srgb, var(--seg) 14%, transparent); color: var(--seg); }
.chip.seg .dot { background: var(--seg); }

/* ---------- tabela ---------- */
.table-wrap {
  overflow-x: auto; margin: 0 -20px;
  /* sombras laterais indicam que há mais conteúdo para rolar */
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) left / 32px 100% no-repeat local,
    linear-gradient(to left, var(--surface) 30%, transparent) right / 32px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(16, 24, 40, .14), transparent) left / 10px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(16, 24, 40, .14), transparent) right / 10px 100% no-repeat scroll;
}
.table-wrap th { background: transparent; }
td.wrap { white-space: normal; min-width: 220px; }
.mini-bar { display: inline-block; width: 64px; height: 6px; border-radius: 3px; background: var(--surface-2); vertical-align: middle; margin-right: 8px; overflow: hidden; }
.mini-bar span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { padding-left: 20px; }
th:last-child, td:last-child { padding-right: 20px; }
th { font-size: var(--fs-sm); font-weight: 500; color: var(--text-3); background: var(--surface); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sorted { color: var(--accent); }
td.r, th.r { text-align: right; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tr:last-child td { border-bottom: 0; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding-top: 14px; flex-wrap: wrap; }
.empty { padding: 40px 12px; text-align: center; color: var(--text-3); }

/* ---------- matriz RFV ---------- */
.matrix { display: grid; grid-template-columns: 74px repeat(3, minmax(0, 1fr)); gap: 8px; }
.matrix .axis { font-size: var(--fs-sm); color: var(--text-3); display: flex; align-items: center; justify-content: center; text-align: center; font-weight: 500; }
.matrix .axis.x { flex-direction: column; gap: 2px; padding-bottom: 4px; }
.matrix .axis.x strong { color: var(--text-2); font-weight: 600; }
.matrix .axis.y { justify-content: flex-start; color: var(--text-2); font-weight: 600; }
.matrix .corner { font-size: var(--fs-xs); color: var(--text-3); display: flex; align-items: flex-end; padding-bottom: 4px; line-height: 1.3; }
.cell {
  border-radius: var(--radius-sm); padding: 14px; min-height: 108px; cursor: pointer; border: 1px solid transparent;
  background: color-mix(in srgb, var(--seg) calc(var(--alpha, .15) * 100%), var(--surface));
  display: flex; flex-direction: column; justify-content: space-between; transition: transform .12s, border-color .12s;
}
.cell:hover { border-color: var(--seg); transform: translateY(-1px); }
.cell .name { font-size: var(--fs-sm); font-weight: 600; color: var(--seg); }
.cell .count { font-size: var(--fs-xl); font-weight: 600; letter-spacing: -.02em; }
.cell .meta { font-size: var(--fs-sm); color: var(--text-2); }

.seg-card { border-left: 3px solid var(--seg); }
.seg-card .top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.seg-card .acao { margin-top: 10px; font-size: var(--fs-sm); color: var(--text-2); background: var(--surface-2); border-radius: var(--radius-sm); padding: 8px 10px; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--seg, var(--accent)); border-radius: 3px; }

/* ---------- gráficos ---------- */
/* Gráficos são desenhados na largura real (charts.js): nunca escalar o SVG, senão o texto cresce junto */
.chart-canvas { width: 100%; min-width: 0; }
.chart-canvas svg { display: block; overflow: visible; }
/* Gráfico "grow": ocupa a altura que sobra no card (ao lado de um card mais alto no grid).
   O SVG fica absoluto para a altura vir do layout, e não do próprio desenho. */
.card:has(> .card-body > .chart.grow) { display: flex; flex-direction: column; }
.card-body:has(> .chart.grow) { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chart.grow { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chart.grow > .chart-canvas { flex: 1; position: relative; }
.chart.grow > .chart-canvas > svg { position: absolute; top: 0; left: 0; }
.chart text { fill: var(--text-3); font-size: var(--fs-xs); font-family: inherit; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--border); }
.chart .donut-center { fill: var(--text); font-size: var(--fs-lg); font-weight: 600; }
.chart.donut { display: flex; gap: 24px; align-items: center; flex-wrap: wrap; }
.chart.donut svg { flex: none; overflow: visible; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: var(--fs-sm); color: var(--text-2); margin-top: 12px; }
.legend.legend-stack { display: grid; gap: 6px; margin: 0; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
/* Colunas fixas: todas as barras começam e terminam no mesmo ponto */
.hbar { display: grid; grid-template-columns: 140px minmax(0, 1fr) 92px; gap: 10px; align-items: center; font-size: var(--fs-md); padding: 5px 0; }
.hbar > :last-child { text-align: right; }
.hbar .track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.hbar .track span { display: block; height: 100%; border-radius: 4px; background: var(--c, var(--accent)); }
.hbar .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.tooltip {
  position: fixed; pointer-events: none; z-index: 50; background: var(--text); color: var(--bg);
  font-size: var(--fs-sm); padding: 6px 9px; border-radius: 6px; white-space: nowrap; opacity: 0; transition: opacity .1s;
}
.heat td { text-align: center; font-size: var(--fs-sm); padding: 6px 4px; border: 0; }
.heat td.c { color: var(--text); border-radius: 4px; }
.heat th { text-align: center; padding: 6px 4px; }
.heat { border-collapse: separate; border-spacing: 3px; }

.funnel { display: grid; gap: 10px; }
.funnel .step { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.funnel .step .bar { grid-column: 1 / -1; height: 26px; border-radius: 6px; }
.funnel .step .bar span { background: var(--accent); border-radius: 6px; }

/* ---------- perfil ---------- */
.profile-head { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.avatar { width: 52px; height: 52px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 600; font-size: var(--fs-lg); flex: none; }
.dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: var(--fs-md); margin: 0; }
.dl dt { color: var(--text-3); } .dl dd { margin: 0; overflow-wrap: anywhere; }
.timeline { display: grid; gap: 10px; }
.order { border: 1px solid var(--border); border-radius: var(--radius-sm); }
.order summary { list-style: none; cursor: pointer; padding: 10px 14px; display: grid; grid-template-columns: 96px 1fr auto auto; gap: 12px; align-items: center; }
.order summary::-webkit-details-marker { display: none; }
.order[open] summary { border-bottom: 1px solid var(--border); }
.order .items { padding: 4px 0; }
.note { border-bottom: 1px solid var(--border); padding: 10px 0; }
.note:last-child { border-bottom: 0; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 32px; }
.login form { display: grid; gap: 14px; margin-top: 22px; }
.error { color: var(--neg); font-size: var(--fs-md); min-height: 18px; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 60; background: var(--text); color: var(--bg);
  padding: 10px 14px; border-radius: var(--radius-sm); font-size: var(--fs-md); box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
.banner { background: var(--accent-soft); color: var(--accent); border-radius: var(--radius-sm); padding: 10px 14px; font-size: var(--fs-md); display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 6px; min-height: 90px; }
@keyframes sk { to { background-position: -200% 0; } }
.spin { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: rot .7s linear infinite; display: inline-block; }
@keyframes rot { to { transform: rotate(360deg); } }

/* ---------- responsivo ---------- */
@media (max-width: 1100px) {
  .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cols-3-1 { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .shell.nav-open .sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
  .main { padding: 20px 16px 40px; }
  .cols-2, .cols-3 { grid-template-columns: 1fr; }
  .matrix { grid-template-columns: 52px repeat(3, minmax(0, 1fr)); gap: 6px; }
  .cell { padding: 10px 8px; min-height: 92px; }
  .cell .count { font-size: var(--fs-lg); }
  .cell .meta-rev, .matrix .corner span { display: none; }
  .order summary { grid-template-columns: 1fr auto; }
  .hbar { grid-template-columns: 96px minmax(0, 1fr) 84px; }
  .mini-kpis { grid-template-columns: 1fr 1fr; }
}
