/* =============================================================================
   SFP v2 — design system
   Tokens → layout → componentes → Tabulator → utilitários
   ============================================================================= */

:root {
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, monospace;

  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --surface-3: #eceff5;
  --border: #e2e6ee;
  --border-strong: #c9d0dc;
  --text: #17202e;
  --text-2: #4b5563;
  --muted: #7b8494;
  --brand: #1d3557;
  --brand-2: #2f5f9e;
  --brand-soft: #e5ecf7;
  --accent: #457b9d;
  --receita: #0f9d58;
  --receita-soft: #e3f5ea;
  --despesa: #d6455a;
  --despesa-soft: #fbe7ea;
  --warn: #e0a526;
  --warn-soft: #fff4d6;
  --ok: #0f9d58;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px -4px rgba(16, 24, 40, .08);
  --shadow-lg: 0 12px 40px -8px rgba(16, 24, 40, .25);
  --radius: 14px;
  --radius-sm: 9px;
  --sidebar-w: 232px;
  --topbar-h: 60px;
  --focus: 0 0 0 3px rgba(69, 123, 157, .35);
}

[data-theme="dark"] {
  --bg: #0f141c;
  --surface: #161c26;
  --surface-2: #1b2230;
  --surface-3: #222b3b;
  --border: #26303f;
  --border-strong: #36425a;
  --text: #e8edf5;
  --text-2: #b7c0cf;
  --muted: #7f8aa0;
  --brand: #9fc2ee;
  --brand-2: #6fa3e3;
  --brand-soft: #1d2a40;
  --accent: #7fb2d6;
  --receita: #38c172;
  --receita-soft: #12301f;
  --despesa: #f0657a;
  --despesa-soft: #3a1b22;
  --warn: #f3c04d;
  --warn-soft: #3a2f12;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px -4px rgba(0, 0, 0, .4);
  --shadow-lg: 0 12px 40px -8px rgba(0, 0, 0, .6);
}

/* ---------- reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 15px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: var(--font); color: var(--text); background: var(--bg);
  line-height: 1.45; -webkit-font-smoothing: antialiased;
}
a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .95rem; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
::selection { background: var(--brand-soft); }
[x-cloak] { display: none !important; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100dvh; }
.sidebar {
  position: sticky; top: 0; height: 100dvh; padding: 18px 14px; display: flex; flex-direction: column; gap: 6px;
  background: var(--surface); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; color: #fff; font-weight: 800;
  background: linear-gradient(135deg, #1d3557, #457b9d);
}
.brand-name { font-weight: 700; font-size: 1.05rem; }
.brand-sub { font-size: .72rem; color: var(--muted); margin-top: -2px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; color: var(--text-2);
  font-weight: 500; transition: background .15s, color .15s;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.ativo { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.nav svg { width: 18px; height: 18px; flex: none; }
.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 8px; font-size: .85rem; }
.user-pill { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--brand); color: #fff; font-weight: 700; font-size: .8rem; }
.user-pill .nome { font-weight: 600; line-height: 1.1; }
.user-pill .papel { color: var(--muted); font-size: .72rem; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--topbar-h); display: flex; align-items: center; gap: 14px;
  padding: 0 26px; background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar .titulo { display: flex; flex-direction: column; }
.topbar .titulo small { color: var(--muted); font-size: .78rem; }
.topbar .acoes { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.content { padding: 22px 26px 40px; display: flex; flex-direction: column; gap: 18px; }

/* ---------- grid helpers ---------- */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.span-2 { grid-column: span 2; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 8px; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px 0; }
.card-h h2 { display: flex; align-items: center; gap: 8px; }
.card-h .sub { color: var(--muted); font-size: .8rem; }
.card-b { padding: 14px 18px 18px; }
.card-b.flush { padding: 0; }

.kpi { padding: 16px 18px; display: flex; flex-direction: column; gap: 6px; position: relative; overflow: hidden; }
.kpi .rot { color: var(--muted); font-size: .78rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .val { font-size: 1.55rem; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi .delta { font-size: .78rem; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.kpi .delta b { font-weight: 600; }
.kpi.receita .val { color: var(--receita); }
.kpi.despesa .val { color: var(--despesa); }
.kpi::after { content: ""; position: absolute; inset: auto 0 0 0; height: 3px; background: var(--border); }
.kpi.receita::after { background: var(--receita); }
.kpi.despesa::after { background: var(--despesa); }
.kpi.brand::after { background: var(--brand-2); }
.pos { color: var(--receita) !important; }
.neg { color: var(--despesa) !important; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; padding: 8px 13px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface); cursor: pointer; font-weight: 600; font-size: .88rem; line-height: 1; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-2); border-color: var(--brand-2); }
[data-theme="dark"] .btn-primary { color: #0f141c; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-3); }
.btn-danger { color: var(--despesa); border-color: color-mix(in srgb, var(--despesa) 40%, transparent); }
.btn-danger:hover { background: var(--despesa-soft); }
.btn-sm { padding: 6px 10px; font-size: .8rem; border-radius: 8px; }
.btn-icon { padding: 7px; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: .78rem; font-weight: 600; color: var(--text-2); }
.input, .select, .textarea {
  width: 100%; padding: 8px 11px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface);
  outline: none; transition: border-color .15s, box-shadow .15s; font-size: .9rem;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); box-shadow: var(--focus); }
.select { appearance: none; padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%237b8494' stroke-width='2.2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; }
.input-sm, .select-sm { padding: 6px 9px; font-size: .84rem; border-radius: 8px; }
.input-icon { position: relative; }
.input-icon svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted); }
.input-icon .input { padding-left: 32px; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: .88rem; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--brand-2); }
.hint { font-size: .78rem; color: var(--muted); }
.erro { color: var(--despesa); font-size: .85rem; }

/* ---------- badges / chips ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: .74rem; font-weight: 600; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.badge-receita { background: var(--receita-soft); color: var(--receita); }
.badge-despesa { background: var(--despesa-soft); color: var(--despesa); }
.badge-warn { background: var(--warn-soft); color: #9a6b00; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface); font-size: .8rem; cursor: pointer; }
.chip.ativo { background: var(--brand-soft); border-color: var(--brand-2); color: var(--brand); font-weight: 600; }

/* ---------- toolbar / seleção em lote ---------- */
.toolbar { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; }
.toolbar .field { min-width: 140px; }
.toolbar .grow { flex: 1 1 220px; }
.bulkbar {
  position: sticky; bottom: 14px; z-index: 15; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px; border-radius: 14px; background: var(--brand); color: #fff; box-shadow: var(--shadow-lg);
}
[data-theme="dark"] .bulkbar { color: #0f141c; }
.bulkbar .btn { background: rgba(255,255,255,.14); border-color: rgba(255,255,255,.25); color: inherit; }
.bulkbar .btn:hover { background: rgba(255,255,255,.24); }
[data-theme="dark"] .bulkbar .btn { background: rgba(0,0,0,.12); border-color: rgba(0,0,0,.2); }
.bulkbar .n { font-weight: 700; font-size: 1rem; }

/* ---------- tables ---------- */
.table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.table th, .table td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; z-index: 1; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table tr:hover td { background: var(--surface-2); }
.table tfoot td { font-weight: 700; background: var(--surface-2); border-top: 2px solid var(--border-strong); }
.table-wrap { overflow: auto; max-height: 70vh; border-radius: 0 0 var(--radius) var(--radius); }
.pivot th:first-child, .pivot td:first-child { position: sticky; left: 0; background: var(--surface); z-index: 2; min-width: 190px; }
.pivot th:first-child { z-index: 3; background: var(--surface-2); }
.pivot tr:hover td:first-child { background: var(--surface-2); }
.pivot td.num { padding: 7px 10px; }
.heat { border-radius: 6px; padding: 2px 6px; display: inline-block; min-width: 74px; text-align: right; }
.pivot td.num a { color: inherit; }
.pivot td.num a:hover { text-decoration: underline; }

.bar { height: 8px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; background: var(--brand-2); }
.bar.ok > i { background: var(--ok); }
.bar.atencao > i { background: var(--warn); }
.bar.estourou > i { background: var(--despesa); }

/* ---------- modal ---------- */
.modal-bg { position: fixed; inset: 0; z-index: 50; background: rgba(10, 14, 22, .55); display: grid; place-items: center; padding: 16px; backdrop-filter: blur(2px); }
.modal { width: min(640px, 100%); max-height: 92dvh; overflow: auto; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.modal-h { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-b { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-f { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 18px 18px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 10px 14px; border-radius: 12px; background: var(--text); color: var(--bg); font-size: .88rem; box-shadow: var(--shadow-lg); display: flex; gap: 10px; align-items: center; animation: up .18s ease-out; }
.toast.ok { background: var(--receita); color: #fff; }
.toast.erro { background: var(--despesa); color: #fff; }
@keyframes up { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- misc ---------- */
.empty { padding: 30px; text-align: center; color: var(--muted); }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 8px; min-height: 18px; }
@keyframes sk { from { background-position: 200% 0; } to { background-position: -200% 0; } }
.chart { width: 100%; height: 320px; }
.chart.alto { height: 400px; }
.chart.baixo { height: 240px; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.mono { font-family: var(--mono); }
.num { font-variant-numeric: tabular-nums; }
.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface-3); border-radius: 10px; width: fit-content; }
.tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--text-2); font-size: .85rem; }
.tabs button.ativo { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
kbd { font-family: var(--mono); font-size: .72rem; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--surface-2); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .8rem; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.list { display: flex; flex-direction: column; }
.list > div { display: flex; align-items: center; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.list > div:last-child { border-bottom: 0; }
.list .desc { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .v { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- login ---------- */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 20px; background: radial-gradient(1200px 600px at 10% -10%, var(--brand-soft), transparent), var(--bg); }
.login { width: min(420px, 100%); padding: 34px 32px; }
.login .brand { padding: 0 0 22px; }

/* ---------- Tabulator theme ---------- */
.tabulator { border: 0; background: transparent; font-size: .87rem; font-family: var(--font); }
.tabulator .tabulator-header { background: var(--surface-2); border-bottom: 1px solid var(--border); color: var(--muted); }
.tabulator .tabulator-header .tabulator-col { background: transparent; border-right: 1px solid var(--border); }
.tabulator .tabulator-header .tabulator-col .tabulator-col-content { padding: 8px 10px; }
.tabulator .tabulator-header .tabulator-col .tabulator-col-title { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.tabulator .tabulator-header .tabulator-col.tabulator-sortable:hover { background: var(--surface-3); }
.tabulator .tabulator-header .tabulator-header-filter input, .tabulator .tabulator-header .tabulator-header-filter select {
  padding: 5px 7px; border-radius: 7px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font-size: .8rem; width: 100%;
}
.tabulator .tabulator-tableholder { background: var(--surface); }
.tabulator .tabulator-row { background: var(--surface); border-bottom: 1px solid var(--border); color: var(--text); min-height: 38px; }
.tabulator .tabulator-row.tabulator-row-even { background: var(--surface); }
.tabulator .tabulator-row:hover { background: var(--surface-2) !important; }
.tabulator .tabulator-row.tabulator-selected { background: var(--brand-soft) !important; }
.tabulator .tabulator-row .tabulator-cell { padding: 8px 10px; border-right: 1px solid var(--border); }
.tabulator .tabulator-row .tabulator-cell.tabulator-editing { border: 1px solid var(--accent); box-shadow: var(--focus); padding: 0 8px; }
.tabulator .tabulator-row .tabulator-cell.tabulator-editing input, .tabulator .tabulator-row .tabulator-cell.tabulator-editing select { background: var(--surface); color: var(--text); border: 0; outline: 0; font-size: .87rem; }
.tabulator .tabulator-footer { background: var(--surface-2); border-top: 1px solid var(--border); color: var(--text-2); padding: 6px 10px; }
.tabulator .tabulator-footer .tabulator-page { background: var(--surface); border-color: var(--border-strong); color: var(--text); border-radius: 7px; }
.tabulator .tabulator-footer .tabulator-page.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.tabulator .tabulator-footer .tabulator-page-size { background: var(--surface); color: var(--text); border-color: var(--border-strong); border-radius: 7px; }
.tabulator-edit-list { background: var(--surface); border: 1px solid var(--border-strong); border-radius: 10px; box-shadow: var(--shadow-lg); color: var(--text); font-family: var(--font); font-size: .86rem; }
.tabulator-edit-list .tabulator-edit-list-item { padding: 6px 10px; }
.tabulator-edit-list .tabulator-edit-list-item.active, .tabulator-edit-list .tabulator-edit-list-item:hover { background: var(--brand-soft); color: var(--brand); }
.tabulator-edit-list .tabulator-edit-list-group { background: var(--surface-2); color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; padding: 6px 10px; }
.tabulator .tabulator-cell .cat { display: inline-flex; align-items: center; gap: 6px; }
.tabulator .tabulator-cell .cat i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.tabulator .tabulator-cell input[type="checkbox"] { accent-color: var(--brand-2); width: 15px; height: 15px; }
.tabulator .tabulator-header input[type="checkbox"] { accent-color: var(--brand-2); width: 15px; height: 15px; }
.tabulator .tabulator-row .tabulator-cell.valor { font-variant-numeric: tabular-nums; font-weight: 600; }
.tabulator-tooltip { background: var(--text); color: var(--bg); border-radius: 8px; padding: 6px 10px; font-size: .8rem; }

/* ---------- responsivo ---------- */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  :root { --topbar-h: 54px; }
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: auto 0 0 0; height: auto; flex-direction: row; align-items: center; gap: 0; padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border); z-index: 30; background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(10px);
  }
  .sidebar .brand, .sidebar-footer { display: none; }
  .nav { display: flex; width: 100%; justify-content: space-around; }
  .nav a { flex-direction: column; gap: 3px; padding: 6px 4px; font-size: .64rem; font-weight: 600; border-radius: 10px; }
  .nav a.ativo { background: transparent; }
  .nav svg { width: 20px; height: 20px; }
  .topbar { padding: 0 14px; }
  .topbar .titulo small { display: none; }
  .content { padding: 14px 14px calc(90px + env(safe-area-inset-bottom)); gap: 14px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .kpi .val { font-size: 1.3rem; }
  .chart { height: 260px; }
  .bulkbar { bottom: calc(78px + env(safe-area-inset-bottom)); }
  .modal-f { flex-wrap: wrap; }
}
@media print {
  .sidebar, .topbar .acoes, .bulkbar, .toasts { display: none !important; }
  .app { grid-template-columns: 1fr; }
  .card { box-shadow: none; break-inside: avoid; }
}
