/* ===== Tokens (paleta validada do guia de dataviz; claro e escuro selecionados) ===== */
:root {
  color-scheme: light;
  --bg: #f3f3f1;
  --surface: #fcfcfb;
  --surface-2: #f6f6f3;
  --border: #e2e1db;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #76756f;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;   /* receita */
  --series-2: #eb6834;   /* despesa */
  --series-3: #1baf7a;   /* investimento */
  --series-4: #eda100;   /* premiacao */
  --ink-line: #0b0b0b;
  --pos: #006300;        /* texto de valor positivo */
  --neg: #c0312f;        /* texto de valor negativo */
  --warn-bg: #fff4d6; --warn-text: #7a5200;
  --grid: #e9e8e3;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.04);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #222221; --border: #2f2f2c;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #9a998f;
    --accent: #3987e5; --accent-ink: #0b0b0b;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --ink-line: #f0efec; --pos: #4fcf4f; --neg: #ef7f7d;
    --warn-bg: #3a2d08; --warn-text: #f2c660; --grid: #2a2a28;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #222221; --border: #2f2f2c;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #9a998f;
  --accent: #3987e5; --accent-ink: #0b0b0b;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --ink-line: #f0efec; --pos: #4fcf4f; --neg: #ef7f7d;
  --warn-bg: #3a2d08; --warn-text: #f2c660; --grid: #2a2a28;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
h3 { font-size: 13px; margin: 0; font-weight: 600; color: var(--text-2); }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); } .neg { color: var(--neg); }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ===== Layout ===== */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 20px;
  padding: 0 16px; height: 52px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: -.01em; color: var(--text); display: flex; gap: 8px; align-items: center; }
.brand-dot { width: 10px; height: 10px; border-radius: 3px; background: var(--series-1); }
.nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; }
.nav a { padding: 7px 12px; border-radius: 8px; color: var(--text-2); white-space: nowrap; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.topbar-right { display: flex; gap: 8px; align-items: center; }
.container { max-width: 1480px; margin: 0 auto; padding: 16px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.grid { display: grid; gap: 12px; }
.g-kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.g-2 { grid-template-columns: 1.4fr 1fr; }
.g-2e { grid-template-columns: 1fr 1fr; }
.g-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 1000px) { .g-2, .g-2e, .g-3 { grid-template-columns: 1fr; } }
.stack > * + * { margin-top: 12px; }

/* ===== Controles ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; line-height: 1.2;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .chip:focus-visible, .seg button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--neg); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=search], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; min-width: 0;
}
textarea { width: 100%; min-height: 64px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
label.field input, label.field select { color: var(--text); font-size: 14px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 7px 11px; cursor: pointer; color: var(--text-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* ===== Filtros ===== */
.filters { padding: 10px 16px; background: var(--bg); border-bottom: 1px solid var(--border); z-index: 20; }
@media (min-width: 1100px) and (min-height: 820px) { .filters { position: sticky; top: 52px; } }
.filters-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; max-width: 1480px; margin: 0 auto; }
.filters .field { min-width: 120px; }
.multi { position: relative; }
.multi-btn { min-width: 150px; text-align: left; justify-content: space-between; width: 100%; }
.multi-pop {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 40; min-width: 240px; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,.18); padding: 6px;
}
.multi-pop label { display: flex; gap: 8px; align-items: center; padding: 5px 8px; border-radius: 6px; cursor: pointer; }
.multi-pop label:hover { background: var(--surface-2); }
.multi-pop .grp { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); padding: 8px 8px 2px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 2px 10px; font-size: 12px; cursor: pointer; }
.chip:hover { background: var(--surface-2); }

/* ===== KPIs ===== */
.kpi .label { color: var(--text-2); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin: 2px 0; }
.kpi .delta { font-size: 12px; color: var(--text-2); display: flex; gap: 6px; align-items: center; }
.delta .tag { font-weight: 600; color: var(--text); white-space: nowrap; }
.kpi .bar { height: 3px; border-radius: 2px; margin-bottom: 8px; }

/* ===== Tabelas ===== */
.table-wrap { overflow: auto; max-width: 100%; border-radius: 8px; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface); position: sticky; top: 0; z-index: 1; white-space: nowrap; }
tbody tr:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; }
.pivot td:first-child, .pivot th:first-child { position: sticky; left: 0; background: var(--surface); z-index: 2; min-width: 200px; }
.pivot tbody tr:hover td:first-child { background: var(--surface-2); }
.pivot tr.l0 td { font-weight: 650; background: var(--surface-2); }
.pivot tr.l0 td:first-child { background: var(--surface-2); }
.pivot tr.l1 td:first-child { padding-left: 26px; }
.pivot tr.l2 td:first-child { padding-left: 46px; color: var(--text-2); }
.pivot tr.l2 td { color: var(--text-2); }
.pivot tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
.pivot tr.memo td { color: var(--text-3); font-style: italic; }
.toggle { cursor: pointer; background: none; border: 0; padding: 0 6px 0 0; color: var(--text-3); width: 18px; }
.empty { padding: 24px; text-align: center; color: var(--text-2); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2); }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }

/* ===== Graficos ===== */
.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 360px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 160px) 1fr 100px; gap: 8px; align-items: center; padding: 3px 0; cursor: pointer; border-radius: 6px; }
.hbar:hover { background: var(--surface-2); }
.hbar[aria-pressed="true"] { background: var(--surface-2); outline: 1px solid var(--accent); }
.hbar .track { height: 10px; background: var(--surface-2); border-radius: 0 4px 4px 0; position: relative; }
.hbar .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== Modal / toast ===== */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; padding: 18px; width: min(620px, 100%); max-height: 92vh; overflow: auto; border: 1px solid var(--border); }
.modal h2 { margin-bottom: 12px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; z-index: 80; max-width: 90vw; }
.toast.error { background: var(--neg); color: #fff; }
.notice { padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--warn-text); margin-bottom: 12px; }
.skeleton { opacity: .5; }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); }
.login-card .field + .field { margin-top: 10px; }
.flash { padding: 8px 10px; border-radius: 8px; background: var(--warn-bg); color: var(--warn-text); margin-bottom: 10px; }
.dropzone { border: 2px dashed var(--border); border-radius: 12px; padding: 22px; text-align: center; color: var(--text-2); }
.row-dup { opacity: .55; }
.scroll-y { max-height: 520px; overflow: auto; }
.ctl { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
@media (max-width: 700px) {
  .topbar { gap: 8px; padding: 0 8px; }
  .kpi .value { font-size: 20px; }
  .hbar { grid-template-columns: minmax(70px, 110px) 1fr 84px; }
}
@media print {
  .topbar, .filters, .btn, .seg { display: none !important; }
  body { background: #fff; }
}
