:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #14201c;
  --text-muted: #5b6b65;
  --border: #e2e8e5;
  --primary: #0f766e;
  --primary-dark: #0b5c56;
  --accent: #d97706;
  --good: #16a34a;
  --bad: #dc2626;
  --track: #e6ebe9;
  --shadow: 0 1px 3px rgba(15, 23, 20, 0.08), 0 1px 2px rgba(15, 23, 20, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121816;
    --surface: #1a2320;
    --text: #eef2f0;
    --text-muted: #9db0a8;
    --border: #2a3632;
    --primary: #2dd4bf;
    --primary-dark: #14b8a6;
    --accent: #fbbf24;
    --good: #4ade80;
    --bad: #f87171;
    --track: #263029;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html {
  /* Além do maximum-scale/user-scalable no viewport (que alguns navegadores
     ignoram), isso bloqueia especificamente o zoom por duplo toque, mantendo
     o scroll normal — tela fixa mesmo se a pessoa tocar duas vezes rápido. */
  touch-action: manipulation;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px calc(14px + env(safe-area-inset-top)) 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--primary);
  color: #fff;
}

.topbar h1 {
  font-size: 1.15rem;
  margin: 0;
  font-weight: 700;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 14px 14px 24px 14px;
}

.tab-panel.hidden { display: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.card-title {
  font-weight: 700;
  font-size: 1rem;
  margin-bottom: 10px;
}

.muted {
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.4;
  margin: 0 0 8px 0;
}

.hero {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  text-align: center;
  padding: 24px 16px;
}
.hero-label { font-size: 0.9rem; opacity: 0.9; }
.hero-value { font-size: 2.4rem; font-weight: 800; margin: 4px 0; letter-spacing: -0.02em; }
.hero-sub { font-size: 0.85rem; opacity: 0.85; }

.progress-header {
  display: flex;
  justify-content: space-between;
  font-weight: 600;
  margin-bottom: 8px;
  font-size: 0.95rem;
}
.progress-track {
  height: 16px;
  background: var(--track);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 999px;
  width: 0%;
  transition: width 0.4s ease;
}
.progress-note {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 14px;
}
.stat { margin-bottom: 0; text-align: center; }
.stat-label { font-size: 0.8rem; color: var(--text-muted); margin-bottom: 4px; }
.stat-value { font-size: 1.3rem; font-weight: 800; }

.big-highlight {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--primary);
}

.kv-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.95rem;
  gap: 12px;
}
.kv-row:last-child { border-bottom: none; }
.kv-row span { color: var(--text-muted); }
.kv-row.total { font-weight: 700; }
.kv-row.total span { color: var(--text); }

.month-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.month-row:active { transform: scale(0.99); }
.month-row.is-payoff { border-color: var(--accent); border-width: 2px; }
.month-row.is-meta { border-color: var(--good); border-width: 2px; }
.month-row.is-payoff.is-meta { border-image: linear-gradient(135deg, var(--accent), var(--good)) 1; }
.month-row.is-manual { border-left: 4px solid var(--primary); }
.month-main { display: flex; flex-direction: column; }
.month-key { font-weight: 700; font-size: 0.95rem; }
.month-detail { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }
.month-side { text-align: right; }
.month-sobra { font-weight: 700; }
.month-sobra.neg { color: var(--bad); }
.month-sobra.pos { color: var(--good); }
.month-acum { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }
.badge {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  margin-top: 4px;
  margin-right: 4px;
}
.badge-meta { background: var(--good); }

#chart-container, #sim-chart-container { width: 100%; }
#chart-container svg, #sim-chart-container svg { width: 100%; height: auto; display: block; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 0.9rem;
  font-weight: 600;
}
.field:last-child { margin-bottom: 0; }
.field input[type="number"],
.field input[type="text"],
.field select {
  font-size: 1rem;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-weight: 400;
  width: 100%;
}
.field input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  height: 32px;
}

.row-2 {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
}
.row-2 label { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); }
.row-2 input {
  font-size: 0.95rem;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}

.ff-table-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
.ff-table-row:last-child { border-bottom: none; }
.ff-table-row.recommended { color: var(--primary); font-weight: 700; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 700;
  flex: 1;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.secondary { background: var(--track); color: var(--text); }

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;
}
.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px 8px 4px;
  font-size: 0.7rem;
  font-weight: 600;
}
.tab-btn .tab-icon { font-size: 1.3rem; }
.tab-btn.active { color: var(--primary); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  z-index: 30;
}
.modal-backdrop.hidden { display: none; }
.modal-sheet {
  background: var(--surface);
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 12px 18px calc(20px + env(safe-area-inset-bottom)) 18px;
}
.modal-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  margin: 0 auto 14px auto;
  position: sticky;
  top: 0;
}
.modal-sheet h2 { margin: 0 0 14px 0; font-size: 1.1rem; }
.modal-section-title {
  font-weight: 700;
  font-size: 0.9rem;
  margin: 18px 0 8px 0;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.btn-block { width: 100%; margin-bottom: 14px; }
.extra-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.extra-item:last-of-type { border-bottom: none; }
.extra-item .extra-desc { flex: 1; font-size: 0.9rem; }
.extra-item .extra-valor { font-weight: 700; white-space: nowrap; }
.item-desc-input, .item-valor-input {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  padding: 8px 10px;
  font-size: 0.88rem;
  min-width: 0;
}
.item-desc-input { flex: 1.4; }
.item-valor-input { flex: 1; }
.item-check {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  accent-color: var(--good);
}
.extra-item.item-pago .item-desc-input,
.extra-item.item-pago .item-valor-input {
  text-decoration: line-through;
  opacity: 0.55;
}
.btn-del {
  background: none;
  border: none;
  color: var(--bad);
  font-size: 1.1rem;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

@media (min-width: 480px) {
  .hero-value { font-size: 2.8rem; }
}
