:root {
  color-scheme: dark;
  --bg: #0e1116;
  --surface: #161a21;
  --surface-2: #1d222b;
  --line: #262c36;
  --text: #f3f4f6;
  --text-2: #b4b9c3;
  --muted: #7d8491;
  --accent: #3987e5;
  --accent-ink: #ffffff;
  --pos: #3fbf8a;
  --neg: #f07474;
  --warn: #e0a534;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --s0: #6b7280;
  --radius: 16px;
  --tab-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f6f6f3; --surface: #ffffff; --surface-2: #f0efec; --line: #e4e3df;
    --text: #0b0b0b; --text-2: #3f3e3b; --muted: #6b6a66;
    --accent: #2a78d6; --pos: #0f8a5f; --neg: #c93a39; --warn: #a86b00;
    --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
    --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948; --s0: #8a8985;
  }
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f6f3; --surface: #ffffff; --surface-2: #f0efec; --line: #e4e3df;
  --text: #0b0b0b; --text-2: #3f3e3b; --muted: #6b6a66;
  --accent: #2a78d6; --pos: #0f8a5f; --neg: #c93a39; --warn: #a86b00;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948; --s0: #8a8985;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%;
  min-height: 100dvh; overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }

/* ---------- estructura ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(160%) blur(18px); -webkit-backdrop-filter: saturate(160%) blur(18px);
  display: flex; align-items: center; gap: 10px;
}
.top h1 { font-size: 28px; line-height: 1.1; margin: 0; flex: 1; letter-spacing: -0.02em; font-weight: 700; }
.top .back { background: none; border: 0; color: var(--accent); font-size: 17px; padding: 6px 4px; margin-left: -4px; display: flex; align-items: center; gap: 2px; }
main { padding: 4px 16px calc(var(--tab-h) + var(--safe-b) + 90px); max-width: 640px; margin: 0 auto; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: saturate(160%) blur(18px); -webkit-backdrop-filter: saturate(160%) blur(18px);
  border-top: 1px solid var(--line);
}
.tabs a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500; }
.tabs a svg { width: 24px; height: 24px; }
.tabs a.on { color: var(--accent); }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--tab-h) + var(--safe-b) + 16px); z-index: 31;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 8px 24px rgba(0,0,0,.35); display: grid; place-items: center;
}
.fab svg { width: 28px; height: 28px; }

/* ---------- tarjetas ---------- */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin: 12px 0; border: 1px solid var(--line); }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card h2 a, .card h2 button { text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--accent); text-decoration: none; background: none; border: 0; padding: 0; font-weight: 500; }
.hero { padding: 20px 16px; }
.hero .label { color: var(--text-2); font-size: 14px; }
.hero .big { font-size: 38px; font-weight: 700; letter-spacing: -0.03em; line-height: 1.1; margin: 4px 0 6px; overflow-wrap: anywhere; }
.hero .sub { color: var(--text-2); font-size: 14px; display: flex; gap: 14px; flex-wrap: wrap; }
.delta { font-size: 14px; font-weight: 600; }
.pos { color: var(--pos); } .neg { color: var(--neg); } .mut { color: var(--muted); } .warn { color: var(--warn); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 12px; min-width: 0; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 18px; font-weight: 650; margin-top: 2px; overflow-wrap: anywhere; }

/* ---------- listas ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); min-height: 52px; }
.row:last-child { border-bottom: 0; }
.row.tap { cursor: pointer; }
.row .main { flex: 1; min-width: 0; }
.row .t { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .s { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .r { text-align: right; flex-shrink: 0; }
.row .r .t { font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.chev { color: var(--muted); flex-shrink: 0; }
.bar-track { height: 6px; background: var(--surface-2); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 3px; }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); vertical-align: middle; }
.badge.neg { background: color-mix(in srgb, var(--neg) 18%, transparent); color: var(--neg); }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.pos { background: color-mix(in srgb, var(--pos) 18%, transparent); color: var(--pos); }
.badge.auto { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }

.btn { border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 600; background: var(--surface-2); color: var(--text); min-height: 44px; }
.btn.pri { background: var(--accent); color: var(--accent-ink); }
.btn.del { background: transparent; color: var(--neg); }
.btn.sm { padding: 6px 12px; min-height: 34px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btns { display: flex; gap: 10px; margin-top: 16px; }
.btns .btn { flex: 1; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--text-2); min-height: 32px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.seg.full { display: flex; } .seg.full button { flex: 1; }
.iconbtn { background: var(--surface-2); border: 0; border-radius: 50%; width: 36px; height: 36px; display: grid; place-items: center; color: var(--text); flex-shrink: 0; }
.iconbtn svg { width: 18px; height: 18px; }
.iconbtn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.monthnav { display: flex; align-items: center; justify-content: space-between; margin: 6px 0; }
.monthnav b { font-size: 17px; text-transform: capitalize; }

.empty { color: var(--muted); text-align: center; padding: 18px 8px; font-size: 15px; }
.foot { color: var(--muted); font-size: 12px; text-align: center; margin: 16px 0; line-height: 1.6; }

/* ---------- gráficos ---------- */
.chart { position: relative; margin-top: 6px; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 6px 10px; font-size: 13px; white-space: nowrap; transform: translate(-50%, -110%);
  box-shadow: 0 6px 18px rgba(0,0,0,.3); display: none;
}
.chart .tip b { display: block; }

/* ---------- hoja inferior (formularios) ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50; opacity: 0; transition: opacity .2s; }
.sheet-bg.on { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; max-height: 92dvh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; padding: 8px 16px calc(20px + var(--safe-b));
  transform: translateY(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1);
  max-width: 640px; margin: 0 auto; -webkit-overflow-scrolling: touch;
}
.sheet.on { transform: none; }
.sheet .grab { width: 36px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 0 0 12px; font-size: 20px; }
.f { margin-bottom: 12px; }
.f label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 500; }
.f input, .f select, .f textarea {
  width: 100%; background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; font-size: 16px; min-height: 46px; outline: none; -webkit-appearance: none; appearance: none;
}
.f select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
.f input:focus, .f select:focus, .f textarea:focus { border-color: var(--accent); }
.f .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.f.amount input { font-size: 28px; font-weight: 650; padding: 10px 12px; }
.frow { display: flex; gap: 10px; } .frow > .f { flex: 1; min-width: 0; }
.chk { display: flex; align-items: center; gap: 10px; font-size: 15px; margin: 6px 0 12px; }
.chk input { width: 22px; height: 22px; accent-color: var(--accent); }
.errbox { color: var(--neg); font-size: 14px; margin: 4px 0 8px; min-height: 1em; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login .box { width: 100%; max-width: 360px; text-align: center; }
.login img { width: 76px; height: 76px; border-radius: 18px; margin-bottom: 14px; }
.login h1 { margin: 0 0 4px; font-size: 26px; }
.login p { color: var(--muted); margin: 0 0 22px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-b) + 88px); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 550;
  opacity: 0; transition: all .2s; z-index: 60; pointer-events: none; max-width: calc(100% - 32px); text-align: center;
}
#toast.on { opacity: 1; transform: translateX(-50%); }
.offline { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); font-size: 13px; padding: 8px 12px; border-radius: 10px; margin: 8px 0; text-align: center; }
.skeleton { height: 120px; border-radius: var(--radius); background: var(--surface); margin: 12px 0; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
