/* ===== Tokens (paleta azul-ardósia) ===== */
:root {
  --bg: #f1f4f9;
  --surface: #ffffff;
  --surface-2: #e8edf5;
  --ink: #1d2b40;
  --muted: #5c6b82;
  --line: #d5dde9;
  --primary: #475f8a;
  --primary-ink: #ffffff;
  --primary-soft: #dfe7f3;
  --receita: #2b8a6e;
  --receita-soft: #d9f0e8;
  --despesa: #b5533f;
  --despesa-soft: #f6dfd9;
  --linha: #2f4f7f;
  --sombra: 0 1px 2px rgb(29 43 64 / 0.06), 0 8px 24px rgb(29 43 64 / 0.06);
  --raio: 14px;
  color-scheme: light;
}

:root:not([data-theme="light"]) {
  /* preferência do sistema, quando o usuário não escolheu manualmente */
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1826;
    --surface: #17233a;
    --surface-2: #1f2e49;
    --ink: #e6ecf6;
    --muted: #9aa9c2;
    --line: #2c3d5c;
    --primary: #7f9cd0;
    --primary-ink: #0f1826;
    --primary-soft: #24365a;
    --receita: #5fc7a4;
    --receita-soft: #173b33;
    --despesa: #ef8c77;
    --despesa-soft: #4a2a25;
    --linha: #9fb8e6;
    --sombra: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1826;
  --surface: #17233a;
  --surface-2: #1f2e49;
  --ink: #e6ecf6;
  --muted: #9aa9c2;
  --line: #2c3d5c;
  --primary: #7f9cd0;
  --primary-ink: #0f1826;
  --primary-soft: #24365a;
  --receita: #5fc7a4;
  --receita-soft: #173b33;
  --despesa: #ef8c77;
  --despesa-soft: #4a2a25;
  --linha: #9fb8e6;
  --sombra: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.3);
  color-scheme: dark;
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
}
h1 { font-size: 1.6rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ===== Layout ===== */
.shell {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  min-height: 100vh;
}
.sidebar {
  background: var(--primary);
  color: var(--primary-ink);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.marca { display: flex; align-items: center; gap: 12px; }
.marca strong, .marca small { display: block; }
.marca small { opacity: 0.75; font-size: 0.8rem; }
.marca-icone {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  background: rgb(255 255 255 / 0.18); font-size: 1.3rem; font-weight: 700;
}
.menu { display: flex; flex-direction: column; gap: 4px; }
.menu a {
  color: inherit; padding: 10px 12px; border-radius: 10px; opacity: 0.85;
}
.menu a:hover { background: rgb(255 255 255 / 0.12); text-decoration: none; opacity: 1; }
.menu a.ativo { background: rgb(255 255 255 / 0.22); opacity: 1; font-weight: 600; }
.tema {
  margin-top: auto; background: transparent; border: 1px solid rgb(255 255 255 / 0.35);
  color: inherit; padding: 8px 12px; border-radius: 10px; cursor: pointer;
}
.rodape-menu { margin-top: auto; display: grid; gap: 8px; }
.rodape-menu .tema { margin-top: 0; }
body.sem-sessao .sidebar .menu { display: none; }
.login {
  max-width: 420px; margin: 8vh auto 0;
}
.login-form { display: grid; gap: 14px; margin-top: 16px; }
.login-form .erro { margin: 0; padding: 10px 12px; }
.login code { font-size: 0.85rem; background: var(--surface-2); padding: 2px 6px; border-radius: 6px; }
.conteudo {
  padding: 28px clamp(16px, 3vw, 40px) 48px;
  min-width: 0;
  max-width: 1400px;
  width: 100%;
}

.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-end;
  justify-content: space-between; gap: 12px; margin-bottom: 20px;
}
.sub { color: var(--muted); margin: 4px 0 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--raio);
  box-shadow: var(--sombra);
  padding: 20px;
  margin-bottom: 20px;
  min-width: 0;
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
}
.grid-2 > .card { margin-bottom: 0; }

/* ===== Componentes ===== */
.btn {
  background: var(--primary); color: var(--primary-ink);
  border: 0; border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 600;
}
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.ghost {
  background: transparent; color: var(--primary); border: 1px solid var(--line);
}
.btn.perigo { background: var(--despesa); color: #fff; }
.btn.pequeno { padding: 5px 10px; font-size: 0.85rem; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; color: var(--muted); }
input, select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; min-width: 0; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.grid-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
}
.grid-form .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; }

.filtros {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 14px;
}
.filtros label { flex: 1 1 140px; }
.filtros .busca { flex: 2 1 220px; }

.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--raio);
  box-shadow: var(--sombra); padding: 18px; color: inherit; display: block;
  border-left: 4px solid var(--primary);
}
.kpi:hover { text-decoration: none; filter: brightness(1.03); }
.kpi.receita { border-left-color: var(--receita); }
.kpi.despesa { border-left-color: var(--despesa); }
.kpi span { color: var(--muted); font-size: 0.85rem; }
.kpi strong { display: block; font-size: 1.45rem; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi small { display: block; color: var(--muted); margin-top: 2px; font-size: 0.8rem; }
.kpi.receita strong { color: var(--receita); }
.linha-subconta td:first-child { padding-left: 28px; }
.grupo-conta { border-bottom: 1px solid var(--line); padding: 10px 0; }
.grupo-conta summary { cursor: pointer; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; list-style: none; padding: 6px 0; }
.grupo-conta summary::-webkit-details-marker { display: none; }
.grupo-conta summary::before { content: "▸"; color: var(--muted); }
.grupo-conta[open] summary::before { content: "▾"; }
.kpi.despesa strong { color: var(--despesa); }

.chart-box { position: relative; height: 320px; }

.tabela-wrap { overflow-x: auto; border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
td.num, th.num { text-align: right; }
tr.linha-clicavel { cursor: pointer; }
tr.linha-clicavel:hover td { background: var(--primary-soft); }
td.valor-receita { color: var(--receita); font-weight: 600; }
td.valor-despesa { color: var(--despesa); font-weight: 600; }
td.acoes { white-space: nowrap; text-align: right; }
td.acoes button + button { margin-left: 6px; }

.badge {
  display: inline-block; font-size: 0.75rem; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.badge.receita { background: var(--receita-soft); color: var(--receita); }
.badge.despesa { background: var(--despesa-soft); color: var(--despesa); }

.barra-progresso { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; min-width: 80px; }
.barra-progresso > span { display: block; height: 100%; background: var(--primary); }

.resumo-filtro { display: flex; gap: 18px; flex-wrap: wrap; margin: 4px 0 12px; color: var(--muted); }
.resumo-filtro strong { color: var(--ink); }

.carregando, .vazio { color: var(--muted); padding: 24px 0; text-align: center; }
.erro {
  background: var(--despesa-soft); color: var(--despesa);
  padding: 14px 16px; border-radius: 10px;
}
.aviso-lista { margin: 8px 0 0; padding-left: 20px; color: var(--muted); font-size: 0.9rem; }

.dropzone {
  border: 2px dashed var(--line); border-radius: var(--raio); padding: 28px;
  text-align: center; color: var(--muted); background: var(--surface-2);
}
.resultado { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; margin-top: 16px; }
.resultado div { background: var(--surface-2); padding: 14px; border-radius: 10px; }
.resultado strong { display: block; font-size: 1.2rem; font-variant-numeric: tabular-nums; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 10px; z-index: 50; }
.toast {
  background: var(--ink); color: var(--bg); padding: 12px 16px; border-radius: 10px;
  box-shadow: var(--sombra); max-width: min(360px, calc(100vw - 32px));
  animation: entrada 0.2s ease-out;
}
.toast.erro { background: var(--despesa); color: #fff; }
@keyframes entrada { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ===== Responsivo (celular: 16px de margem, sem rolagem horizontal da página) ===== */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 800px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static; height: auto; flex-direction: row; flex-wrap: wrap;
    align-items: center; gap: 12px; padding: 12px 16px;
  }
  .marca small { display: none; }
  .menu { flex-direction: row; flex-wrap: wrap; gap: 4px; order: 3; width: 100%; }
  .menu a { padding: 8px 10px; font-size: 0.9rem; }
  .tema { margin-top: 0; margin-left: auto; order: 2; }
  .conteudo { padding: 20px 16px 40px; }
  .card { padding: 16px; }
  h1 { font-size: 1.35rem; }
  .chart-box { height: 260px; }
}
@media (max-width: 480px) {
  .kpis { grid-template-columns: minmax(0, 1fr); }
  .kpi strong { font-size: 1.25rem; }
  .form-actions { flex-direction: column-reverse; }
  .form-actions .btn { width: 100%; }
}
