:root {
  --bg: #0b1220;
  --bg-2: #0f172a;
  --surface: #131c2e;
  --surface-2: #1a2540;
  --line: #223052;
  --text: #e8eef7;
  --muted: #8ea0bf;
  --primary: #3b82f6;
  --primary-2: #2563eb;
  --in: #22c55e;
  --out: #f97316;
  --danger: #ef4444;
  --need: #60a5fa;
  --want: #f59e0b;
  --save: #34d399;
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #eef2f8; --bg-2: #f6f8fc; --surface: #ffffff; --surface-2: #f1f5fb;
    --line: #e2e8f2; --text: #10233f; --muted: #64748b; --shadow: 0 8px 24px rgba(20,40,80,.10);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* El atributo hidden debe ocultar SIEMPRE, incluso sobre clases con display:flex/grid */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
#app { max-width: 720px; margin: 0 auto; padding-bottom: calc(150px + var(--safe-bottom)); }

.muted { color: var(--muted); }
h1,h2,h3 { margin: 0; font-weight: 700; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 14px;
  background: color-mix(in srgb, var(--bg-2) 82%, transparent);
  backdrop-filter: blur(14px); border-bottom: 1px solid var(--line);
}
.topbar-brand { display: flex; align-items: center; gap: 12px; }
.topbar-brand h1 { font-size: 18px; }
.topbar-brand p { font-size: 12px; margin: 2px 0 0; }
.logo {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), #8b5cf6); color: #fff;
  font-size: 22px; font-weight: 800; box-shadow: var(--shadow);
}
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.month-label { font-size: 13px; font-weight: 600; min-width: 92px; text-align: center; text-transform: capitalize; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 10px; font-size: 18px; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); display: grid; place-items: center;
}
.icon-btn:active { transform: scale(.94); }

/* Views */
main { padding: 16px; display: block; }
.view { display: none; animation: fade .25s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px);} to {opacity:1;transform:none;} }

/* Cards */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-head h2 { font-size: 15px; }
.tag {
  font-size: 11px; font-weight: 600; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--line); padding: 3px 9px; border-radius: 999px;
}
.link-btn { color: var(--primary); font-size: 13px; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.5; margin: 10px 0 0; }

/* Hero */
.hero {
  background: linear-gradient(140deg, #1e293b, #0f172a);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; margin-bottom: 14px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
@media (prefers-color-scheme: light){ .hero { background: linear-gradient(140deg,#1e3a8a,#2563eb); color:#fff; } .hero .muted{color:#cbd8f5;} }
.hero::after {
  content:""; position:absolute; right:-40px; top:-40px; width:160px; height:160px;
  background: radial-gradient(circle, rgba(59,130,246,.35), transparent 70%); pointer-events:none;
}
.hero-label { font-size: 13px; color: var(--muted); margin: 0; }
.hero-value { font-size: 34px; font-weight: 800; margin: 6px 0 2px; letter-spacing: -.5px; }
.hero-sub { font-size: 13px; margin: 0; }
.hero-mini { display: flex; gap: 10px; margin-top: 16px; }
.mini { flex: 1; background: rgba(255,255,255,.04); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px; }
@media (prefers-color-scheme: light){ .mini{ background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.25);} }
.mini-label { display: block; font-size: 11px; color: var(--muted); }
.mini-value { display: block; font-size: 17px; font-weight: 700; margin-top: 2px; }
.mini.in .mini-value { color: var(--in); }
.mini.out .mini-value { color: var(--out); }
@media (prefers-color-scheme: light){ .hero .mini.in .mini-value{color:#86efac;} .hero .mini.out .mini-value{color:#fdba74;} }

/* Rule 50/30/20 */
.rule-bars { display: flex; flex-direction: column; gap: 14px; }
.rule-row { display: block; }
.rule-row-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.rule-name { font-size: 13px; font-weight: 600; }
.rule-name .dot { display:inline-block; width:9px; height:9px; border-radius:50%; margin-right:7px; vertical-align: middle; }
.rule-nums { font-size: 12px; color: var(--muted); }
.rule-track { height: 9px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.rule-prog { height: 100%; border-radius: 999px; transition: width .5s ease; }
.rule-over { color: var(--danger); font-weight: 600; }

/* Progress (emergency fund) */
.progress-wrap { }
.progress { height: 12px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress-fill { height: 100%; width: 0; border-radius: 999px; background: linear-gradient(90deg, var(--save), #10b981); transition: width .6s ease; }
.progress-legend { display: flex; justify-content: space-between; margin-top: 8px; font-size: 13px; font-weight: 600; }

/* Tips */
.tips { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.tips li {
  display: flex; gap: 12px; align-items: flex-start; font-size: 13.5px; line-height: 1.5;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px 14px;
}
.tips li .ico { font-size: 18px; flex-shrink: 0; }
.tips li.good { border-left: 3px solid var(--in); }
.tips li.warn { border-left: 3px solid var(--want); }
.tips li.bad  { border-left: 3px solid var(--danger); }

/* Debt priority */
.debt-priority { margin: 0; padding-left: 4px; list-style: none; counter-reset: d; display:flex; flex-direction:column; gap:8px; }
.debt-priority li { counter-increment: d; display: flex; align-items: center; gap: 12px; background: var(--surface-2); border:1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px; }
.debt-priority li::before { content: counter(d); display: grid; place-items: center; width: 26px; height: 26px; border-radius: 8px; background: var(--primary); color:#fff; font-weight: 700; font-size: 13px; flex-shrink:0;}
.debt-priority .dp-name { font-weight: 600; font-size: 14px; }
.debt-priority .dp-meta { font-size: 12px; color: var(--muted); }
.debt-priority .dp-amt { margin-left: auto; text-align: right; }
.debt-priority .dp-amt b { font-size: 14px; }

/* Segmented */
.seg { display: inline-flex; background: var(--surface-2); border:1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 12px; }
.seg.wide { display: flex; }
.seg button { flex: 1; padding: 9px 14px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--muted); transition: .15s; }
.seg button.active { background: var(--primary); color: #fff; }

/* Transaction list */
.tx-list, .loan-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.tx {
  display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--line); cursor: pointer;
}
.tx:active { transform: scale(.99); }
.tx-ico { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 19px; flex-shrink: 0; }
.tx-ico.ingreso { background: color-mix(in srgb, var(--in) 20%, transparent); }
.tx-ico.gasto { background: color-mix(in srgb, var(--out) 18%, transparent); }
.tx-body { flex: 1; min-width: 0; }
.tx-cat { font-size: 14px; font-weight: 600; }
.tx-note { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-right { text-align: right; }
.tx-amt { font-size: 15px; font-weight: 700; }
.tx-amt.ingreso { color: var(--in); }
.tx-amt.gasto { color: var(--out); }
.tx-date { font-size: 11px; color: var(--muted); }

/* Loans */
.loan-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.loan-stat { background: var(--surface); border:1px solid var(--line); border-radius: var(--radius-sm); padding: 12px 14px; box-shadow: var(--shadow); }
.loan-stat span { font-size: 12px; color: var(--muted); display:block; }
.loan-stat b { font-size: 19px; }
.loan {
  background: var(--surface); border:1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
}
.loan-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.loan-name { font-size: 15px; font-weight: 700; }
.loan-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.loan-tags .tag.rate { color: var(--want); border-color: color-mix(in srgb, var(--want) 40%, var(--line)); }
.loan-bal { font-size: 20px; font-weight: 800; text-align: right; }
.loan-bal small { display:block; font-size: 11px; color: var(--muted); font-weight: 500; }
.loan-actions { display: flex; gap: 8px; margin-top: 12px; }
.loan-actions .btn { flex: 1; padding: 9px; font-size: 13px; }

/* Buttons */
.btn { border-radius: 12px; padding: 12px 16px; font-size: 14px; font-weight: 600; border: 1px solid transparent; transition: .15s; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--primary); color: #fff; width: 100%; }
.btn.ghost { background: var(--surface-2); border-color: var(--line); color: var(--text); }
.btn.danger-ghost { background: transparent; border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); color: var(--danger); }
.btn-row { display: flex; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; min-width: 130px; }

/* Fields */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field input, .field select {
  width: 100%; padding: 12px 14px; border-radius: 12px; font-size: 15px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--primary); }
.field small { display: block; margin-top: 6px; font-size: 11.5px; }
.field.checkbox { display: flex; align-items: center; gap: 10px; }
.field.checkbox span { margin: 0; font-weight: 500; font-size: 13px; }
.field.checkbox input { width: auto; }

/* Empty */
.empty { text-align: center; color: var(--muted); font-size: 14px; padding: 30px 12px; }

/* FAB */
.fab {
  position: fixed; right: 18px; bottom: calc(84px + var(--safe-bottom)); z-index: 30;
  width: 60px; height: 60px; border-radius: 20px; font-size: 30px; color: #fff;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  box-shadow: 0 12px 26px rgba(59,130,246,.45); display: grid; place-items: center;
}
.fab:active { transform: scale(.92); }
#app:not(.show-fab) .fab { display: none; }

/* Bottom nav */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 25;
  display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--bg-2) 88%, transparent); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line); padding: 8px 6px calc(8px + var(--safe-bottom));
}
.nav-btn { display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 10.5px; color: var(--muted); padding: 4px 10px; border-radius: 12px; flex: 1; }
.nav-btn .ni { font-size: 19px; }
.nav-btn.active { color: var(--primary); }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; background: rgba(0,0,0,.5); animation: fade .2s ease; }
.modal-card {
  width: 100%; max-width: 560px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 22px 22px 0 0; padding: 20px 18px calc(24px + var(--safe-bottom));
  box-shadow: 0 -10px 40px rgba(0,0,0,.4); animation: slideup .28s cubic-bezier(.2,.8,.2,1); max-height: 92vh; overflow-y: auto;
}
@media (min-width: 600px){ .modal { align-items: center; } .modal-card { border-radius: 22px; } }
@keyframes slideup { from { transform: translateY(30px); opacity:.6;} to { transform: none; opacity:1;} }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-head h3 { font-size: 17px; }
.modal-actions { display: flex; gap: 10px; margin-top: 6px; }
.modal-actions .btn.primary { flex: 1; }

/* Toast */
.toast {
  position: fixed; bottom: calc(96px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: #0b1220; color: #fff; border: 1px solid var(--line); padding: 12px 20px; border-radius: 14px;
  font-size: 13px; font-weight: 600; z-index: 60; box-shadow: var(--shadow); animation: fade .2s ease;
}
.about p { margin: 4px 0; font-size: 12px; }

/* ===== Auth / PIN screens ===== */
.auth-screen {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: linear-gradient(160deg, var(--bg-2), var(--bg));
  padding-top: calc(20px + var(--safe-top)); padding-bottom: calc(20px + var(--safe-bottom));
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 22px; padding: 26px 22px; box-shadow: var(--shadow); animation: fade .3s ease;
}
.auth-brand { text-align: center; margin-bottom: 20px; }
.auth-brand h1 { font-size: 22px; margin: 12px 0 4px; }
.auth-brand p { font-size: 13px; margin: 0; }
.logo.big { width: 60px; height: 60px; border-radius: 18px; font-size: 32px; margin: 0 auto; }
.auth-error {
  background: color-mix(in srgb, var(--danger) 14%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 40%, var(--line));
  color: var(--danger); font-size: 13px; font-weight: 600; padding: 10px 12px; border-radius: 10px; margin: 0 0 12px;
}
.hint.center, .auth-error.center { text-align: center; }
.auth-card #auth-submit { width: 100%; margin-top: 4px; }

/* PIN pad */
.auth-card.pin { max-width: 340px; }
.pin-dots { display: flex; justify-content: center; gap: 16px; margin: 8px 0 18px; }
.pin-dots i { width: 15px; height: 15px; border-radius: 50%; background: var(--surface-2); border: 2px solid var(--line); transition: .15s; }
.pin-dots i.on { background: var(--primary); border-color: var(--primary); transform: scale(1.1); }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.pinpad button {
  aspect-ratio: 1.4; border-radius: 16px; font-size: 24px; font-weight: 600; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--line); transition: .12s;
}
.pinpad button:active { transform: scale(.93); background: var(--primary); color: #fff; }
.pinpad button.pin-alt { font-size: 15px; font-weight: 600; color: var(--muted); }
.pin-other-btn {
  display: block; width: 100%; margin-top: 20px; padding: 12px; border-radius: 12px;
  font-size: 14px; font-weight: 600; color: var(--primary);
  background: var(--surface-2); border: 1px solid var(--line);
}
.pin-other-btn:active { transform: scale(.97); }

/* Cuenta (Ajustes) */
.account-row { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.account-avatar {
  width: 52px; height: 52px; border-radius: 16px; display: grid; place-items: center; font-size: 24px; font-weight: 800; color: #fff;
  background: linear-gradient(135deg, var(--primary), #8b5cf6); flex-shrink: 0;
}
.account-name { font-size: 17px; font-weight: 700; }
#sync-badge { font-size: 11px; }
