:root {
  color-scheme: light;
  --page: #f7f5f2;
  --surface: #ffffff;
  --ink: #2b2b2d;
  --ink-2: #57575a;
  --muted: #76767a;
  --hair: #e8e4de;
  --axis: #cfcac2;
  --graphite: #414143;
  --copper: #ab7136;
  --copper-ink: #875621;
  --copper-wash: #f6eee5;
  --good: #006300;
  --bad: #b02a2a;
  --bad-wash: #fbeeee;
  --warn-wash: #fdf6e7;
  --warn-line: #ecd9ac;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(43, 43, 45, .04);
  --font: "Manrope", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 500 15px/1.45 var(--font);
}
button, input, select { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--copper-ink); outline-offset: 2px; }

/* ---- Estrutura ---- */
.shell { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 24px;
  padding: 24px 16px;
  background: var(--surface);
  border-right: 1px solid var(--hair);
}
.sidebar .logo { width: 132px; margin: 0 8px; display: block; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav button {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border: 0; background: none; border-radius: var(--radius-sm);
  cursor: pointer; color: var(--ink-2); font-weight: 600; text-align: left;
}
.nav button:hover { background: var(--page); }
.nav button[aria-current="page"] { background: var(--copper-wash); color: var(--copper-ink); }
.nav svg, .tabbar svg { width: 20px; height: 20px; flex: none; }
.sidebar .btn-primary { width: 100%; }
.sidebar-foot { margin-top: auto; padding: 0 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
.sidebar-foot span { min-width: 0; overflow-wrap: anywhere; }
.topbar-user { display: flex; align-items: center; gap: 10px; min-width: 0; font-size: 13px; color: var(--ink-2); }
.topbar-user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; }
.chips { position: relative; }

.main { min-width: 0; padding: 28px 32px 48px; }
.content { max-width: 1240px; margin: 0 auto; display: flex; flex-direction: column; gap: 20px; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 12px; }
.page-head h1 { font-size: 24px; font-weight: 700; letter-spacing: -.01em; }
.page-head p { color: var(--ink-2); font-size: 14px; }

.topbar, .tabbar { display: none; }

/* ---- Componentes ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--hair);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}
.card h2 { font-size: 15px; font-weight: 700; }
.card-sub { color: var(--muted); font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 8px 16px;
  border: 1px solid var(--axis); border-radius: var(--radius-sm);
  background: var(--surface); font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--page); }
.btn-primary { background: var(--copper-ink); border-color: var(--copper-ink); color: #fff; }
.btn-primary:hover { background: #734819; }
.btn-danger { color: var(--bad); border-color: #e3bcbc; }
.btn-danger:hover { background: var(--bad-wash); }
.btn-ghost { border-color: transparent; background: none; color: var(--ink-2); }
.btn-sm { min-height: 34px; padding: 4px 12px; font-size: 14px; }
.btn[disabled] { opacity: .6; cursor: progress; }

label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
input[type=text], input[type=password], input[type=date], input[type=search], select {
  width: 100%; min-height: 42px; padding: 8px 12px;
  background: var(--surface); border: 1px solid var(--axis); border-radius: var(--radius-sm);
  font-size: 16px; font-weight: 500; color: var(--ink);
}
select { padding-right: 28px; }

.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; padding: 3px; background: #efece7; border-radius: 10px; }
.chips button {
  min-height: 34px; padding: 4px 12px; border: 0; border-radius: var(--radius-sm);
  background: none; color: var(--ink-2); font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chips button[aria-pressed="true"], .chips button[aria-checked="true"] {
  background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(43, 43, 45, .14);
}
.filters select, .filters input[type=date], .filters input[type=search] { width: auto; min-height: 40px; font-size: 14px; }
.filters input[type=search] { flex: 1 1 200px; min-width: 160px; }

.banner {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px;
  padding: 12px 16px; background: var(--warn-wash); border: 1px solid var(--warn-line);
  border-radius: var(--radius); font-size: 14px;
}

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  max-width: 260px; padding: 8px 12px;
  background: var(--ink); color: #fff; border-radius: var(--radius-sm); font-size: 13px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .18);
}
.tooltip .t-title { color: #cfcfd2; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; }
.tooltip .t-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.tooltip .t-value { font-weight: 700; font-variant-numeric: tabular-nums; }
.tooltip .t-name { color: #cfcfd2; }

.toast {
  position: fixed; z-index: 60; left: 50%; bottom: 24px; transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 12px 16px;
  background: var(--ink); color: #fff; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
.toast.error { background: var(--bad); }

/* ---- Painel ---- */
.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 16px; }
.span-3 { grid-column: span 3; }
.span-4 { grid-column: span 4; }
.span-5 { grid-column: span 5; }
.span-6 { grid-column: span 6; }
.span-7 { grid-column: span 7; }
.span-8 { grid-column: span 8; }
.span-12 { grid-column: span 12; }

.hero-label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.hero-value { font-size: clamp(38px, 4.4vw, 54px); font-weight: 700; line-height: 1.1; letter-spacing: -.02em; margin: 4px 0 16px; overflow-wrap: anywhere; }
.neg { color: var(--bad); }

.hero-foot { display: flex; flex-wrap: wrap; gap: 4px 16px; }
.formula { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--hair); font-size: 13px; color: var(--muted); }
.cash-flow { display: flex; align-items: center; justify-content: space-between; gap: 8px 16px; }
.cash-flow > div { flex: 1 1 0; min-width: 0; }
.cash-flow span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.cash-flow strong { font-size: clamp(16px, 1.45vw, 20px); font-weight: 700; white-space: nowrap; }
.cash-flow > i { font-style: normal; font-size: 20px; font-weight: 600; color: var(--muted); }
.cash-total { padding: 8px 12px; background: var(--copper-wash); border-radius: var(--radius-sm); }
.link-btn { padding: 0 2px; border: 0; background: none; color: var(--copper-ink); font-size: 13px; font-weight: 600; text-decoration: underline; cursor: pointer; }
.tile-wrap { display: flex; min-width: 0; }
.month-tile { flex: 1; }
.month-row { cursor: pointer; }
.month-row:hover td { background: #fbfaf8; }
.result { font-size: 13px; font-weight: 600; white-space: nowrap; }
.result.gain { color: var(--good); }
.result.loss { color: var(--bad); }
.result.muted { color: var(--muted); }
.side-actions { gap: 8px; }
.side-actions .btn { width: 100%; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat .label { font-size: 13px; font-weight: 600; color: var(--ink-2); display: flex; align-items: center; gap: 8px; }
.stat .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.stat .value { font-size: clamp(20px, 1.8vw, 26px); font-weight: 700; letter-spacing: -.01em; white-space: nowrap; }
.delta { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }

.chart-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 16px; margin-bottom: 12px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.chart { width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; }
.chart text { font: 500 12px var(--font); fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .peak { fill: var(--ink); font-weight: 700; }
.chart .hit { fill: transparent; cursor: default; outline: none; }
.chart .hit:hover, .chart .hit:focus { fill: rgba(43, 43, 45, .05); }

.ranked { display: flex; flex-direction: column; gap: 12px; }
.ranked-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; font-size: 14px; border-radius: 4px; }
.ranked-row:focus-visible { outline-offset: 4px; }
.ranked-name { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }
.ranked-val { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ranked-val small { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 6px; }
.ranked-bar { grid-column: 1 / -1; height: 10px; }
.ranked-bar i { display: block; height: 100%; min-width: 2px; border-radius: 0 4px 4px 0; }

.table-wrap { overflow-x: auto; margin: 0 -20px -8px; padding: 0 20px 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; font-weight: 700; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--axis); white-space: nowrap; }
td { padding: 10px; border-bottom: 1px solid var(--hair); }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.strong { font-weight: 700; }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); }

/* ---- Lançamentos ---- */
.summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.summary div span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.summary div strong { font-size: 18px; font-weight: 700; white-space: nowrap; }
.month-nav { display: inline-flex; align-items: center; gap: 4px; }
.month-nav strong { min-width: 138px; text-align: center; font-size: 14px; }
.filter-toggle { display: none; }
.filter-extra { display: contents; }
.nowrap { white-space: nowrap; }
.mobile-only { display: none; }
.month-nav .btn { min-height: 38px; padding: 4px 12px; }

.ledger td { vertical-align: middle; }
.ledger .tx { cursor: pointer; }
.ledger .tx:hover td { background: #fbfaf8; }
.desc-main { font-weight: 600; background: none; border: 0; padding: 0; text-align: left; cursor: pointer; }
.history .desc-main { text-decoration: underline; text-decoration-color: var(--axis); text-underline-offset: 3px; }
.ledger .desc-meta { display: block; font-size: 13px; color: var(--muted); }
.ledger .in-val { color: #14704b; font-weight: 700; }
.ledger .out-val { color: var(--ink); font-weight: 700; }
.ledger .opening td { color: var(--ink-2); background: #fbfaf8; font-weight: 600; }
.tag { display: inline-block; margin-left: 6px; padding: 0 6px; border: 1px solid var(--warn-line); border-radius: 4px; font-size: 11px; font-weight: 600; color: #7b5f1b; background: var(--warn-wash); }

/* ---- Perfil ---- */
.profile-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.section-note { font-size: 13px; color: var(--muted); margin: 4px 0 12px; }
.item-list { display: flex; flex-direction: column; margin-bottom: 12px; }
.item-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--hair); }
.item-row:last-child { border-bottom: 0; }
.item-row .grow { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.item-row .sub { display: block; font-size: 13px; color: var(--muted); font-weight: 500; }
.item-row.archived .grow { color: var(--muted); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex: none; border: 0; border-radius: var(--radius-sm); background: none; color: var(--ink-2); cursor: pointer;
}
.icon-btn:hover { background: var(--page); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }
.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; min-width: 0; }
.group-title { font-size: 13px; font-weight: 700; color: var(--ink-2); margin: 16px 0 4px; }
.group-title:first-of-type { margin-top: 0; }

/* ---- Diálogos ---- */
dialog {
  width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  padding: 0; border: 0; border-radius: 16px; color: var(--ink); background: var(--surface);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(30, 28, 25, .45); }
.dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-head h2 { font-size: 18px; font-weight: 700; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dialog-actions .spacer { flex: 1; }
.two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.amount-input { font-size: 22px !important; font-weight: 700 !important; min-height: 52px !important; }
.date-input { min-height: 52px !important; }
.form-error { color: var(--bad); font-size: 14px; font-weight: 600; }
.segment { width: 100%; }
.segment button { flex: 1; }

/* ---- Entrada ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(400px, 100%); display: flex; flex-direction: column; gap: 14px; }
.auth-card .logo { width: 170px; margin: 8px auto 12px; display: block; }
.auth-card h1 { font-size: 20px; font-weight: 700; }
.auth-card p { color: var(--ink-2); font-size: 14px; }
.auth-error { padding: 24px; text-align: center; }

/* ---- Responsivo ---- */
@media (max-width: 1100px) {
  .span-4, .span-5, .span-6, .span-7, .span-8 { grid-column: span 12; }
  .span-3 { grid-column: span 6; }
  .hero-value { font-size: clamp(38px, 8vw, 54px); }
  .stat .value { font-size: 22px; }
  .history { font-size: 13px; }
  .history th, .history td { padding-left: 5px; padding-right: 5px; }
  .history .tag { display: block; width: max-content; margin: 2px 0 0; }
}

@media (max-width: 900px) {
  .shell { display: block; padding-bottom: calc(68px + env(safe-area-inset-bottom)); }
  .sidebar { display: none; }
  .topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--hair);
  }
  .topbar .logo { height: 34px; display: block; }
  .main { padding: 16px 16px 24px; }
  .content { gap: 14px; }
  .page-head h1 { font-size: 21px; }
  .hero-value { font-size: clamp(30px, 10vw, 54px); }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px -8px; padding: 0 16px 8px; }

  .tabbar {
    position: fixed; z-index: 20; left: 0; right: 0; bottom: 0;
    display: grid; grid-template-columns: repeat(5, 1fr);
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--hair);
  }
  .tabbar button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: 60px; padding: 4px; border: 0; background: none; color: var(--ink-2);
    font-size: 11px; font-weight: 600; cursor: pointer;
  }
  .tabbar button[aria-current="page"] { color: var(--copper-ink); }
  .tabbar .tab-add svg { width: 34px; height: 34px; padding: 7px; border-radius: 50%; background: var(--copper-ink); color: #fff; }

  .filters { align-items: stretch; }
  .filters > .chips { width: 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .filters select, .filters input[type=date] { flex: 1 1 140px; }
  .filters select, .filters input[type=date], .filters input[type=search] { font-size: 16px; }
  .desktop-only { display: none; }
  .mobile-only { display: inline; }
  th.mobile-only, td.mobile-only { display: table-cell; }
  .history .tag { display: block; width: max-content; margin: 2px 0 0; }
  table { font-size: 13px; }
  th, td { padding-left: 4px; padding-right: 4px; }
  .filters input[type=search] { flex: 1 1 0; min-width: 0; }
  .filter-toggle { display: inline-flex; min-height: 40px; }
  .filter-extra { display: none; width: 100%; }
  .filter-extra.open { display: grid; gap: 8px; }
  .filter-extra select { width: 100%; }
  .stats { grid-template-columns: minmax(0, 1fr); gap: 0; padding: 4px 16px; }
  .stat { flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 2px 12px; padding: 12px 0; border-bottom: 1px solid var(--hair); }
  .stat:last-child { border-bottom: 0; }
  .stat .delta { width: 100%; text-align: right; }
  .stat.month-tile { flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; align-items: flex-start; padding: 14px; border: 1px solid var(--hair); }
  .stat.month-tile:last-child { border-bottom: 1px solid var(--hair); }
  .month-tile .value { font-size: 18px; }
  .month-tile .delta { text-align: left; }
  .summary { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .cash-flow { flex-direction: column; align-items: stretch; gap: 2px; }
  .cash-flow > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex: none; }
  .cash-flow > i { display: none; }
  .cash-flow > .op-plus span::before { content: "+ "; }
  .cash-flow > .op-minus span::before { content: "− "; }
  .cash-flow strong { font-size: 17px; white-space: nowrap; }
  .cash-total { margin-top: 4px; }
  .summary div strong { font-size: 16px; }
  .profile-grid { grid-template-columns: minmax(0, 1fr); }
  .month-nav { width: 100%; justify-content: space-between; }

  /* Livro em formato de cartão no celular */
  .ledger thead { display: none; }
  .ledger, .ledger tbody { display: block; }
  .ledger tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 12px 0; border-bottom: 1px solid var(--hair); }
  .ledger tr:last-child { border-bottom: 0; }
  .ledger td { display: block; padding: 0; border: 0; background: none !important; }
  .ledger .c-date { grid-column: 1 / -1; font-size: 12px; font-weight: 600; color: var(--muted); }
  .ledger .c-desc { grid-column: 1; }
  .ledger .c-in, .ledger .c-out { grid-column: 2; grid-row: 2; }
  .ledger .c-in:empty, .ledger .c-out:empty { display: none; }
  .ledger .c-bal { grid-column: 1 / -1; text-align: right; font-size: 12px; color: var(--muted); }
  .ledger .c-bal::before { content: "Caixa "; }
  .ledger .opening .c-label { grid-column: 1; }
  .ledger .opening .c-bal { grid-column: 2; grid-row: 1; font-size: 14px; color: var(--ink); }
  .ledger .opening .c-bal::before { content: ""; }

  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 16px 16px 0 0; max-height: 92dvh; }
  .dialog-body { padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
  .dialog-actions .btn { flex: 1 1 auto; }
  .dialog-actions .spacer { display: none; }
  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); width: max-content; }
}

@media (prefers-reduced-motion: no-preference) {
  .ranked-bar i { transition: width .25s ease; }
}

@media print {
  .sidebar, .tabbar, .topbar, .filters, .banner, .btn { display: none !important; }
  .shell { display: block; }
  .main { padding: 0; }
}
