:root {
  --green: #13a66a;
  --green-dark: #0e8654;
  --green-soft: #d9f7e6;
  --navy: #102b43;
  --bg: #f4f8f7;
  --card: #fff;
  --line: #dfe8e5;
  --text: #17242d;
  --muted: #6b7b83;
  --danger: #d64545;
  --danger-soft: #fdecec;
  --warn-soft: #fff3cd;
  --warn-text: #7a5a00;
}

* { box-sizing: border-box; }
body { margin: 0; font-family: Inter, "Segoe UI", Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.invisible { visibility: hidden; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.block { display: block; }
.hint { color: var(--muted); font-size: 12px; margin-top: -6px; }

/* ---------- botões ---------- */
.primary, .secondary, .danger-btn {
  border: 0; padding: 11px 16px; border-radius: 9px; font-weight: 700; cursor: pointer;
}
.primary { background: var(--green); color: #fff; }
.primary:hover { background: var(--green-dark); }
.secondary { background: #fff; border: 1px solid var(--line); font-weight: 600; }
.secondary:hover { background: #f1f6f4; }
.danger-btn { background: var(--danger); color: #fff; }
button:disabled { opacity: .6; cursor: wait; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid #13a66a55; outline-offset: 1px; }
.link-btn { border: 0; background: none; color: var(--green-dark); font-weight: 700; cursor: pointer; padding: 6px; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 7px; padding: 6px 9px; cursor: pointer; line-height: 1; }
.icon-btn:hover { background: #f1f6f4; }
.icon-btn.ok { color: var(--green-dark); font-weight: 700; }
.icon-btn.danger { color: var(--danger); }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; place-items: center; max-width: 1100px; margin: auto; padding: 40px; gap: 60px; }
.brand-panel { text-align: center; }
.logo { width: 180px; height: 180px; object-fit: contain; }
.brand-panel h1 { font-size: 48px; margin: 0; color: var(--navy); }
.brand-panel p { font-size: 20px; color: var(--muted); }
.bubble { background: #fff; border: 1px solid var(--line); padding: 18px; border-radius: 20px; margin: 30px auto; max-width: 330px; }
.auth-card { background: #fff; border: 1px solid var(--line); box-shadow: 0 15px 45px #102b4312; padding: 28px; border-radius: 18px; width: min(420px, 100%); }
.tabs { display: flex; gap: 8px; margin-bottom: 25px; }
.tabs button { flex: 1; border: 0; padding: 10px; border-radius: 8px; background: #edf4f2; cursor: pointer; }
.tabs .active { background: var(--navy); color: #fff; }
.auth-card form { display: grid; gap: 14px; }
.auth-card input, label input, label select, .filters select, .filters input {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}

/* ---------- layout ---------- */
.app { min-height: 100vh; display: grid; grid-template-columns: 220px 1fr; }
aside { background: var(--navy); padding: 20px 14px; display: flex; flex-direction: column; gap: 7px; color: #fff; position: sticky; top: 0; height: 100vh; }
.side-brand { display: flex; align-items: center; gap: 10px; font-size: 19px; padding: 5px 8px 25px; }
.side-brand img { width: 34px; height: 34px; object-fit: contain; }
.nav { border: 0; background: transparent; color: #d9e4ea; text-align: left; padding: 12px; border-radius: 8px; cursor: pointer; display: flex; gap: 10px; align-items: center; }
.nav .ico { width: 20px; text-align: center; }
.nav.active, .nav:hover { background: #ffffff16; color: #fff; }
.spacer { flex: 1; }
main { padding: 28px; overflow: auto; min-width: 0; }
header, .view-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 22px; gap: 12px; }
header h1, header p, .view-title h2, .view-title p { margin: 3px 0; }
header p, .view-title p { color: var(--muted); }
.month-nav { display: flex; align-items: center; gap: 6px; }
#month { padding: 9px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }

/* ---------- cards ---------- */
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.cards article, .balance, .panel { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 20px; }
.cards span { display: block; color: var(--muted); font-size: 14px; margin-bottom: 10px; }
.cards strong { font-size: 22px; display: block; }
.cards small { color: var(--muted); font-size: 12px; display: block; margin-top: 4px; }
.balance { margin-bottom: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 30px; }
.balance h2 { font-size: 30px; color: var(--green); margin: 7px 0; }
.balance p { color: var(--muted); margin: 0; font-size: 13px; }
.negative { color: var(--danger) !important; }
.bar { height: 7px; background: #edf1f0; border-radius: 10px; overflow: hidden; margin-top: 10px; }
.bar i { display: block; height: 100%; width: 0; background: var(--green); border-radius: 10px; transition: width .4s; }
.bar.expense i { background: #e7894a; }

.alert { background: var(--danger-soft); color: #8a2222; border: 1px solid #f5c2c2; padding: 12px 16px; border-radius: 10px; margin-bottom: 14px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 14px; }
.panel { overflow: auto; }
.panel h2 { margin: 0 0 4px; font-size: 18px; }

.upcoming, .cat-list { list-style: none; margin: 10px 0 0; padding: 0; }
.upcoming li { display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid #edf1f0; }
.upcoming li small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.upcoming li.overdue small { color: var(--danger); font-weight: 600; }
.amount { font-weight: 700; white-space: nowrap; }
.amount.income { color: var(--green-dark); }
.amount.expense { color: var(--text); }
.cat-list li { padding: 8px 0; }
.cat-head { display: flex; justify-content: space-between; font-size: 14px; gap: 10px; }
.cat-head span:last-child { color: var(--muted); white-space: nowrap; }
.cat-list .bar { margin-top: 6px; }

/* ---------- listas ---------- */
.filters { display: grid; grid-template-columns: 180px 220px 1fr; gap: 10px; margin-bottom: 14px; }
.tx-table { width: 100%; border-collapse: collapse; }
.tx-table th, .tx-table td { padding: 12px 8px; border-bottom: 1px solid #edf1f0; text-align: left; vertical-align: middle; }
.tx-table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.tx-table td small { font-size: 12px; }
.tx-table td.num { white-space: nowrap; font-weight: 600; }
.tx-table tr.overdue td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.tx-table tfoot td { border-bottom: 0; padding-top: 16px; font-weight: 600; }
.tx-table tfoot span { margin-right: 24px; white-space: nowrap; }
.badge { padding: 4px 9px; border-radius: 20px; font-size: 12px; background: var(--warn-soft); color: var(--warn-text); white-space: nowrap; }
.badge.done { background: var(--green-soft); color: #137846; }
.badge.late { background: var(--danger-soft); color: #a12a2a; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
.empty { text-align: center; color: var(--muted); padding: 35px; }

/* ---------- conta ---------- */
.stack { display: grid; gap: 14px; margin-top: 12px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.danger-zone { border-color: #f5c2c2; }

/* ---------- diálogos ---------- */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(520px, 94vw); box-shadow: 0 25px 80px #0004; }
dialog::backdrop { background: #102b4366; }
dialog form { padding: 24px; display: grid; gap: 14px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; }
.modal-head .close { font-size: 26px; border: 0; background: none; cursor: pointer; line-height: 1; }
label { font-size: 13px; font-weight: 600; display: grid; gap: 6px; }
label small { font-weight: 400; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.check input { width: auto; margin: 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.sub { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 10px; margin: 0; background: #fafcfb; }
fieldset legend { font-size: 13px; font-weight: 600; padding: 0 4px; }
#newOnly, #editOnly { display: grid; gap: 12px; }
.form-error { background: var(--danger-soft); color: #8a2222; padding: 10px 12px; border-radius: 8px; margin: 0; font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

#toast { position: fixed; right: 20px; top: 20px; background: var(--navy); color: #fff; padding: 12px 18px; border-radius: 8px; z-index: 99; display: none; max-width: min(420px, 90vw); box-shadow: 0 8px 25px #0003; }
#toast.show { display: block; }
#toast.error { background: var(--danger); }
#toast.success { background: var(--green-dark); }

/* ---------- responsivo ---------- */
@media (max-width: 1100px) {
  .cards { grid-template-columns: 1fr 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .auth { grid-template-columns: 1fr; }
  .brand-panel { display: none; }
  .app { grid-template-columns: 72px 1fr; }
  .side-brand b, .nav span:not(.ico) { display: none; }
  .nav { justify-content: center; font-size: 20px; }
  main { padding: 16px; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters input { grid-column: 1 / -1; }
}
@media (max-width: 640px) {
  .cards, .balance, .two { grid-template-columns: 1fr; }
  .balance { gap: 16px; }
  header { align-items: flex-start; flex-direction: column; }
  .app { grid-template-columns: 1fr; }
  aside { position: fixed; z-index: 20; bottom: 0; left: 0; right: 0; top: auto; height: 64px; flex-direction: row; padding: 8px; }
  .side-brand, .spacer { display: none; }
  .nav { flex: 1; }
  main { padding-bottom: 85px; }
  .auth { padding: 20px; }
  .view-title { align-items: flex-start; flex-direction: column; }
  .filters { grid-template-columns: 1fr; }
}

/* ---------- despesas recorrentes ---------- */
.recurring-panel { margin-top: 14px; }
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.commitment { text-align: right; display: grid; gap: 2px; }
.commitment small { font-size: 12px; color: var(--muted); }
.commitment strong { font-size: 22px; color: var(--navy); }
.recurring-list { list-style: none; margin: 10px 0 0; padding: 0; }
.recurring-list li { display: grid; grid-template-columns: 1fr auto auto; gap: 14px; align-items: center; padding: 12px 0; border-bottom: 1px solid #edf1f0; }
.recurring-list li:last-child { border-bottom: 0; }
.rec-main small, .rec-side small { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }
.rec-main .bar { max-width: 260px; height: 5px; margin-top: 7px; }
.rec-side { text-align: right; }
.recurring-list li.overdue .rec-side small { color: var(--danger); font-weight: 600; }
@media (max-width: 640px) {
  .panel-head { flex-direction: column; }
  .commitment { text-align: left; }
  .recurring-list li { grid-template-columns: 1fr auto; }
  .recurring-list li .rec-side { grid-column: 1; grid-row: 2; text-align: left; display: flex; gap: 10px; align-items: baseline; }
  .recurring-list li > button, .recurring-list li > span:last-child { grid-column: 2; grid-row: 1 / span 2; }
}

/* ---------- relatórios ---------- */
.report-form { display: grid; gap: 16px; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; }
.presets button { border: 1px solid var(--line); background: #fff; border-radius: 20px; padding: 7px 13px; cursor: pointer; font-size: 13px; }
.presets button:hover { background: #f1f6f4; }
.presets button.active { background: var(--navy); border-color: var(--navy); color: #fff; }
.report-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.cat-filter { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin: 0; }
.cat-filter legend { font-size: 13px; font-weight: 600; padding: 0 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 20px; padding: 6px 12px; font-weight: 500; cursor: pointer; background: #fff; }
.chip input { width: auto; margin: 0; accent-color: var(--green); }
.chip:has(input:checked) { background: var(--green-soft); border-color: #9fdcbc; }
.chip-actions { margin-top: 6px; }
select:disabled { background: #f1f4f3; color: var(--muted); }
.report-filters-text { color: var(--muted); font-size: 13px; margin: 16px 0 10px; }
.report-cards article small { margin-top: 6px; }
.report-cards article.up small { color: var(--danger); font-weight: 600; }
.report-cards article.down small { color: var(--green-dark); font-weight: 600; }
.report-cards article.late small { color: var(--danger); font-weight: 600; }
#reportResult > .panel { margin-top: 14px; }
.legend { display: inline-block; width: 10px; height: 10px; border-radius: 3px; vertical-align: middle; }
.legend.paid { background: var(--green); }
.legend.pending { background: #e7894a; }
.month-bars { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
.month-bars li { display: grid; grid-template-columns: 64px 1fr 110px; gap: 10px; align-items: center; font-size: 13px; }
.month-bars .mlabel { color: var(--muted); }
.month-bars .mvalue { text-align: right; font-weight: 600; white-space: nowrap; }
.month-bars .stack { display: flex; height: 14px; background: #edf1f0; border-radius: 6px; overflow: hidden; }
.month-bars .stack i { display: block; height: 100%; }
.month-bars .stack i.paid { background: var(--green); }
.month-bars .stack i.pending { background: #e7894a; }
.cat-table .bar-cell { width: 28%; min-width: 80px; }
.cat-table .bar { margin-top: 0; }
.cat-table tr.clickable { cursor: pointer; }
.cat-table tr.clickable:hover td { background: #f6faf8; }
.print-only { display: none; }
@media (max-width: 900px) {
  .report-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 640px) {
  .report-grid { grid-template-columns: 1fr; }
  .month-bars li { grid-template-columns: 52px 1fr 92px; }
}
@media print {
  body { background: #fff; }
  aside, header, .no-print, #toast, dialog { display: none !important; }
  .app { display: block; }
  main { padding: 0; overflow: visible; }
  .print-only { display: block; }
  .print-title h1 { margin: 0; font-size: 20px; }
  .print-title p { margin: 2px 0 0; color: var(--muted); }
  .panel, .cards article { break-inside: avoid; box-shadow: none; }
  .cards { grid-template-columns: repeat(4, 1fr) !important; }
  .grid-2 { grid-template-columns: 1fr 1fr !important; }
  .tx-table thead { display: table-header-group !important; }
  .tx-table { display: table !important; width: 100%; }
  .tx-table tbody { display: table-row-group !important; }
  .tx-table tr { display: table-row !important; }
  .tx-table td { display: table-cell !important; }
  .tx-table td::before { content: none !important; }
  .month-bars .stack i, .bar i, .legend { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- logo no topo (mobile) ---------- */
.mobile-topbar, .auth-mobile-brand { display: none; }
@media (max-width: 900px) {
  .auth-mobile-brand { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 18px; }
  .auth-mobile-brand img { width: 72px; height: 72px; object-fit: contain; }
  .auth-mobile-brand b { font-size: 24px; color: var(--navy); }
}
@media (max-width: 640px) {
  .mobile-topbar {
    display: flex; align-items: center; gap: 10px;
    position: sticky; top: 0; z-index: 15;
    margin: -16px -16px 16px; padding: 10px 16px;
    background: var(--navy); color: #fff;
    box-shadow: 0 2px 10px #102b4326;
  }
  main { overflow: visible; } /* para o topo "grudar" ao rolar a página */
  .mobile-topbar img { width: 32px; height: 32px; object-fit: contain; }
  .mobile-topbar b { font-size: 18px; letter-spacing: .2px; }
}
@media print { .mobile-topbar { display: none !important; } }

/* ---------- ajustes de responsividade (evita estouro lateral) ---------- */
img, svg { max-width: 100%; }
main, .panel, .cards article, .balance > div, .view-title > div, header > div { min-width: 0; }
.tx-table td, .upcoming b, .rec-main b, .cat-head span:first-child { overflow-wrap: anywhere; }
.upcoming li { grid-template-columns: minmax(0, 1fr) auto auto; }
.recurring-list li { grid-template-columns: minmax(0, 1fr) auto auto; }
.tx-table tfoot td > span { display: inline-block; }

@media (max-width: 900px) {
  /* Tabelas viram cartões já no tablet / celular deitado */
  .tx-table thead { display: none; }
  .tx-table, .tx-table tbody, .tx-table tfoot, .tx-table tr, .tx-table td { display: block; width: 100%; }
  .tx-table tbody tr { border-bottom: 1px solid var(--line); padding: 10px 0; }
  .tx-table tbody tr:last-child { border-bottom: 0; }
  .tx-table td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; align-items: baseline; gap: 12px; text-align: right; }
  .tx-table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; flex: none; }
  .tx-table td:first-child { display: block; text-align: left; }
  .tx-table td:first-child::before { display: none; }
  .tx-table td.actions-cell { justify-content: flex-end; padding-top: 8px; }
  .tx-table tr.overdue td:first-child { box-shadow: none; }
  .tx-table tbody tr.overdue { box-shadow: inset 3px 0 0 var(--danger); padding-left: 10px; }
  .tx-table tfoot { border-top: 2px solid var(--line); margin-top: 4px; }
  .tx-table tfoot tr { padding: 0; }
  .tx-table tfoot td { display: flex; flex-wrap: wrap; gap: 6px 18px; text-align: left; padding-top: 12px; }
  .tx-table tfoot span { margin-right: 0; white-space: normal; }
  .cat-table .bar-cell { display: none !important; }
  .cat-table tbody tr { cursor: pointer; }
  .report-cards { grid-template-columns: 1fr 1fr; }
  .month-nav { flex-wrap: wrap; }
}

@media (max-width: 640px) {
  main { overflow-x: clip; } /* rede de segurança: nada empurra a página para o lado */
  header { margin-bottom: 16px; }
  header h1 { font-size: 26px; }
  .month-nav { width: 100%; }
  .month-nav #month { flex: 1; min-width: 0; }
  .cards strong { font-size: 20px; overflow-wrap: anywhere; }
  .balance h2 { font-size: 26px; overflow-wrap: anywhere; }
  .cards article, .balance, .panel { padding: 16px; }
  .report-cards { grid-template-columns: 1fr; }
  .view-title .row-actions { width: 100%; }
  .view-title .row-actions button { flex: 1; }
  .view-title > .primary { width: 100%; }
  .modal-actions button { flex: 1; }
  dialog form { padding: 18px; }
  .presets { gap: 6px; }
  .presets button { padding: 6px 11px; }
  #toast { left: 12px; right: 12px; top: 12px; max-width: none; }
}

@media (max-width: 380px) {
  main { padding: 12px 12px 85px; }
  .mobile-topbar { margin: -12px -12px 12px; padding: 10px 12px; }
  .cards article, .balance, .panel { padding: 14px; }
  .upcoming li { gap: 8px; }
  .upcoming .amount { font-size: 14px; }
  .month-bars li { grid-template-columns: 46px minmax(0, 1fr) 84px; gap: 6px; }
  .month-bars .mvalue { font-size: 12px; }
  .auth-card { padding: 20px; }
  .nav { padding: 10px 4px; }
}

/* ---------- app instalável (PWA) ---------- */
.install-btn {
  width: 100%; margin-top: 16px; padding: 11px 16px; border-radius: 9px; cursor: pointer;
  border: 1px dashed var(--green); background: var(--green-soft); color: #0e6b44; font-weight: 700;
}
.install-btn:hover { background: #c9f1db; }
.nav-install { color: #9fe8c3; }
.topbar-install {
  margin-left: auto; border: 1px solid #ffffff55; background: #ffffff14; color: #fff;
  border-radius: 20px; padding: 6px 12px; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.app-panel .row-actions { align-items: center; }
.install-dialog .install-hero { display: flex; gap: 14px; align-items: center; }
.install-dialog .install-hero img { width: 64px; height: 64px; border-radius: 16px; flex: none; }
.install-dialog .install-hero p { margin: 0; color: var(--muted); }
.steps { margin: 6px 0 0; padding-left: 22px; display: grid; gap: 8px; line-height: 1.45; }
.update-bar, .offline-bar {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 98;
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 10px;
  box-shadow: 0 8px 25px #0003; width: max-content; max-width: calc(100vw - 24px); font-size: 14px;
}
.update-bar { bottom: 20px; background: var(--navy); color: #fff; }
.update-bar button { border: 0; background: var(--green); color: #fff; font-weight: 700; padding: 7px 12px; border-radius: 7px; cursor: pointer; }
.offline-bar { top: 12px; background: #fff3cd; color: #6b4e00; border: 1px solid #f3dc8f; }
.update-bar.hidden, .offline-bar.hidden { display: none; }
.offline-page { min-height: 100vh; display: grid; place-content: center; justify-items: center; text-align: center; gap: 10px; padding: 24px; }
.offline-page .logo { width: 120px; height: 120px; }
.offline-page h1 { margin: 0; color: var(--navy); }
.offline-page p { color: var(--muted); max-width: 380px; }
.offline-page .primary { text-decoration: none; }

/* Áreas seguras (entalhe/barra de gestos) quando aberto como app */
@media (max-width: 640px) {
  .nav-install { display: none !important; } /* no celular o botão fica no topo */
  .mobile-topbar { padding-top: calc(10px + env(safe-area-inset-top)); }
  aside { height: calc(64px + env(safe-area-inset-bottom)); padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  main { padding-bottom: calc(85px + env(safe-area-inset-bottom)); }
  .update-bar { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .offline-bar { top: calc(62px + env(safe-area-inset-top)); }
}
@media (max-width: 380px) {
  .topbar-install { padding: 5px 9px; font-size: 12px; }
}
@media print { .update-bar, .offline-bar, .install-btn, .topbar-install, [data-install] { display: none !important; } }
