:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --green: #16a34a;
  --red: #dc2626;
  --blue: #2563eb;
  --amber: #d97706;
  --border: #e2e8f0;
  --radius: 12px;
  --topbar-h: 56px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 16px; line-height: 1.5;
  overflow-x: hidden;
}
img, canvas, svg { max-width: 100%; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Topbar (mobile: empilhada + nav com rolagem) ---------- */
.topbar {
  background: #0f172a; color: #fff; padding: 10px 12px;
  display: flex; flex-direction: column; align-items: stretch; gap: 10px;
  position: sticky; top: 0; z-index: 10;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
}
.topbar .brand { font-weight: 800; font-size: 18px; letter-spacing: .2px; }
.topbar .brand span { color: #22c55e; }
.topbar nav {
  display: flex; gap: 8px; align-items: center;
  overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  padding-bottom: 2px;
}
.topbar nav::-webkit-scrollbar { display: none; }
.topbar nav a {
  color: #e2e8f0; background: #1e293b; padding: 8px 12px; border-radius: 8px; font-size: 14px;
  white-space: nowrap; flex: 0 0 auto; min-height: 44px;
  display: inline-flex; align-items: center;
}
.topbar nav a:hover, .topbar nav a.active { background: #22c55e; color: #052e16; text-decoration: none; }
.topbar nav a.btn-green { background: #22c55e; color: #052e16; font-weight: 700; }
.topbar nav a.btn-red { background: #fecaca; color: #7f1d1d; font-weight: 700; }

/* ---------- Layout base (mobile) ---------- */
.container { max-width: 1100px; margin: 0 auto; padding: 12px 12px 96px; }
.page-head {
  display: flex; flex-direction: column; align-items: stretch;
  gap: 12px; margin-bottom: 12px;
}
.page-head h1 { margin: 0; font-size: 20px; line-height: 1.25; overflow-wrap: anywhere; }
.actions-row { display: flex; gap: 8px; width: 100%; }
.actions-row .btn, .actions-row .button_to { flex: 1 1 0; }
.actions-row .button_to .btn { width: 100%; }
.muted { color: var(--muted); }

/* ---------- Cards / painéis (mobile: 1 coluna) ---------- */
.cards { display: grid; grid-template-columns: 1fr; gap: 12px; margin-bottom: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 12px; margin-bottom: 12px; }
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px;
}
.card h3 { margin: 0 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.card .value { font-size: 22px; font-weight: 800; overflow-wrap: anywhere; }
.card .value.green { color: var(--green); }
.card .value.red { color: var(--red); }
.card .value.blue { color: var(--blue); }
.card small { color: var(--muted); }
.panel {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px;
  min-width: 0;
}
.panel h2 { margin: 0 0 12px; font-size: 16px; }

/* ---------- Tabelas (mobile: rolagem horizontal edge-to-edge) ---------- */
.table-wrap {
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  margin: 0 -12px; padding: 0 12px;
}
table { width: 100%; min-width: 620px; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 12px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
td .btn, td .button_to .btn { min-height: 44px; display: inline-flex; align-items: center; }

/* ---------- Lista em cards (só mobile, substitui tabelas grandes) ---------- */
.tx-cards { display: grid; gap: 10px; }
.tx-card {
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; display: flex; flex-direction: column; gap: 8px; min-width: 0;
}
.tx-card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.tx-card-top strong { font-size: 15px; overflow-wrap: anywhere; }
.tx-card-top .amount { white-space: nowrap; font-size: 15px; }
.tx-card-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 13px; }
.tx-card-actions { display: flex; gap: 8px; }
.tx-card-actions .btn, .tx-card-actions .button_to { flex: 1 1 0; }
.tx-card-actions .button_to .btn { width: 100%; justify-content: center; }
.only-desktop { display: none !important; }

.badge {
  display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700;
  white-space: nowrap;
}
.badge.receita { background: #dcfce7; color: #166534; }
.badge.despesa { background: #fee2e2; color: #991b1b; }
.badge.pago { background: #dbeafe; color: #1e40af; }
.badge.pendente { background: #fef3c7; color: #92400e; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

/* ---------- Botões (alvos de toque 44px) ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: #fff; color: var(--text); font-weight: 600; font-size: 15px; cursor: pointer;
  font-family: inherit; text-align: center;
}
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn-primary { background: var(--blue); color: #fff; border-color: var(--blue); }
.btn-green { background: var(--green); color: #fff; border-color: var(--green); }
.btn-red { background: var(--red); color: #fff; border-color: var(--red); }
.btn-sm { padding: 6px 12px; font-size: 14px; }
.button_to { display: inline; margin: 0; }
.button_to .btn { font-family: inherit; }

/* ---------- Filtros e formulários (mobile: grade compacta, fonte 16px) ---------- */
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.filters .field:last-child { grid-column: 1 / -1; }
.filters .btn { width: 100%; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.field input, .field select, .field textarea {
  width: 100%; max-width: 100%; min-width: 0;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 16px; min-height: 44px; font-family: inherit; background: #fff; color: var(--text);
}
.field textarea { min-height: 88px; }
.month-nav { display: flex; gap: 8px; width: 100%; }
.month-nav input { flex: 1 1 auto; min-width: 0; font-size: 16px; min-height: 44px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-family: inherit; }
.month-nav .btn { flex: 0 0 auto; }
.form-card { max-width: 640px; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.form-actions .btn { width: 100%; min-height: 48px; }

/* ---------- Gráficos ---------- */
.chart-box { position: relative; height: 230px; }
.chart-box canvas { width: 100% !important; height: 100% !important; }
canvas { max-width: 100%; }

.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; font-size: 14px; }
.alert-notice { background: #dcfce7; color: #14532d; border: 1px solid #86efac; }
.alert-alert { background: #fee2e2; color: #7f1d1d; border: 1px solid #fca5a5; }
.errors { background: #fef2f2; border: 1px solid #fecaca; padding: 10px; border-radius: 8px; margin-bottom: 12px; }
.positive { color: var(--green); font-weight: 700; }
.negative { color: var(--red); font-weight: 700; }
.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 20px 12px; }

/* ---------- Barra de ações mobile (fixa no rodapé) ---------- */
.mobile-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: rgba(15, 23, 42, .97); border-top: 1px solid #1e293b;
}
.mobile-bar a {
  flex: 1 1 0; display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; border-radius: 10px; font-weight: 700; font-size: 14px;
  background: #1e293b; color: #e2e8f0;
}
.mobile-bar a:hover { text-decoration: none; }
.mobile-bar a.go-receita { background: #22c55e; color: #052e16; }
.mobile-bar a.go-despesa { background: #fecaca; color: #7f1d1d; }

/* ---------- Breakpoint tablet (≥640px) ---------- */
@media (min-width: 640px) {
  .container { padding: 16px 16px 40px; }
  .page-head { flex-direction: row; justify-content: space-between; align-items: center; }
  .page-head h1 { font-size: 22px; }
  .actions-row { width: auto; }
  .actions-row .btn, .actions-row .button_to { flex: 0 0 auto; }
  .cards { grid-template-columns: repeat(2, 1fr); }
  .filters { grid-template-columns: repeat(3, 1fr); }
  .filters .field:last-child { grid-column: auto; align-self: end; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-actions { flex-direction: row; }
  .form-actions .btn { width: auto; }
  .topbar { flex-direction: row; align-items: center; justify-content: space-between; padding: 12px 20px; }
  .topbar nav { overflow: visible; flex-wrap: wrap; }
  .topbar nav a { min-height: 0; }
  .btn { min-height: 0; padding: 9px 14px; font-size: 14px; }
  td .btn, td .button_to .btn { min-height: 0; display: inline-block; }
}

/* ---------- Breakpoint desktop (≥900px) ---------- */
@media (min-width: 900px) {
  .container { padding: 20px 20px 40px; }
  .page-head h1 { font-size: 24px; }
  .cards { grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
  .grid-2 { grid-template-columns: 1fr 1fr; margin-bottom: 16px; }
  .panel { padding: 16px; }
  .table-wrap { margin: 0; padding: 0; }
  table { min-width: 0; }
  .month-nav { width: auto; }
  .chart-box { height: 260px; }
  .mobile-bar { display: none; }
  .only-mobile { display: none !important; }
  .only-desktop { display: revert !important; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

/* ---------- Auth (login / cadastro) — 100% nativo, sem libs ---------- */
.auth-wrap {
  display: flex; justify-content: center; align-items: flex-start;
  padding: 24px 0;
}
.auth-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  padding: 24px; width: 100%; max-width: 460px;
  box-shadow: 0 8px 30px rgba(15, 23, 42, .08);
}
.auth-brand { font-weight: 800; font-size: 22px; margin-bottom: 4px; }
.auth-brand span { color: #22c55e; }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-link { margin-top: 12px; text-align: center; font-size: 14px; }
.field small { font-size: 12px; }

/* ---------- Usuário logado no topo ---------- */
.userbox { display: flex; align-items: center; gap: 10px; }
.userbox-name { color: #e2e8f0; font-size: 14px; font-weight: 600; }
.userbox-name small { color: #94a3b8; font-weight: 400; }
.btn-logout {
  background: #1e293b; color: #e2e8f0; border: 1px solid #334155;
  border-radius: 8px; padding: 8px 12px; font-size: 13px; font-weight: 700;
  cursor: pointer; font-family: inherit;
}
.btn-logout:hover { background: #ef4444; border-color: #ef4444; color: #fff; }
@media (min-width: 640px) {
  .auth-wrap { padding: 40px 0; }
}
