:root {
  --bg: #0b0d12;
  --surface: #12151c;
  --surface-2: #181c25;
  --surface-3: #1f2430;
  --border: #232836;
  --border-strong: #2f3545;
  --text: #e8eaf0;
  --muted: #8a90a0;
  --faint: #5b6170;
  --accent: #7c9bff;
  --accent-strong: #5f80ff;
  --green: #34d399;
  --red: #f87171;
  --amber: #fbbf24;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --nav-w: 232px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1, "cv11" 1;
}
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { display: block; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 26px; }
h2 { font-size: 15px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--green); }
.neg { color: var(--red); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ───────── Shell ───────── */
.shell { display: flex; min-height: 100vh; }
.nav {
  position: fixed; inset: 0 auto 0 0; width: var(--nav-w);
  padding: 22px 14px; display: flex; flex-direction: column; gap: 2px;
  background: var(--surface); border-right: 1px solid var(--border); z-index: 20;
}
.nav .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 10px 22px; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--muted); font-weight: 500; transition: background .15s, color .15s;
}
.nav a svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--surface-3); color: var(--text); }
.nav a.active svg { stroke: var(--accent); }
.view { flex: 1; margin-left: var(--nav-w); padding: 28px 32px 96px; max-width: calc(1240px + var(--nav-w)); outline: none; }

.view-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.view-head .sub { margin-top: 4px; }

/* ───────── Cards / grid ───────── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.card-head .muted { font-size: 13px; }
.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3-2 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi .label { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.kpi .value { font-size: 26px; font-weight: 680; letter-spacing: -.02em; margin: 6px 0 4px; }
.kpi .meta { font-size: 13px; color: var(--muted); }
.kpi.hero { background: linear-gradient(135deg, #1a2140 0%, #12151c 70%); border-color: #2a3360; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

.delta { font-size: 12px; padding: 2px 7px; border-radius: 99px; background: var(--surface-3); color: var(--muted); }
.delta.up-bad, .delta.down-bad { background: rgba(248, 113, 113, .12); color: var(--red); }
.delta.up-good, .delta.down-good { background: rgba(52, 211, 153, .12); color: var(--green); }

/* Flujo del mes */
.flow { display: flex; flex-direction: column; }
.flow-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 9px 0; border-bottom: 1px dashed var(--border); }
.flow-row:last-child { border-bottom: 0; }
.flow-row .k { color: var(--muted); }
.flow-row .k small { display: block; color: var(--faint); font-size: 12px; }
.flow-row.total { border-bottom: 0; border-top: 1px solid var(--border-strong); margin-top: 4px; padding-top: 12px; }
.flow-row.total .k { color: var(--text); font-weight: 600; }
.flow-row.total .v { font-size: 20px; font-weight: 700; }
.flow-sep { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); }
.carry {
  margin-top: 14px; padding: 14px 16px; border-radius: 12px;
  background: rgba(124, 155, 255, .08); border: 1px solid rgba(124, 155, 255, .25);
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.carry .v { font-size: 22px; font-weight: 720; }
.carry.neg-bg { background: rgba(248, 113, 113, .08); border-color: rgba(248, 113, 113, .3); }

/* Categorías / presupuestos */
.cat-chart { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 20px; align-items: start; }
.donut { width: 170px; height: 170px; }
.donut text { fill: var(--text); }
.cat-list { display: flex; flex-direction: column; gap: 12px; max-height: 340px; overflow: auto; padding-right: 4px; }
.cat-row { display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; gap: 10px; align-items: center; }
.cat-row .ico { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; font-size: 15px; background: var(--surface-3); }
.cat-row .name { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-row .sub { font-size: 12px; color: var(--muted); }
.bar { height: 6px; border-radius: 99px; background: var(--surface-3); overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; border-radius: inherit; }
.bar.over > i { background: var(--red) !important; }

/* Charts */
.chart { width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--faint); font-size: 11px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend i.line { height: 2px; border-radius: 0; }
.legend i.dash { height: 0; border-top: 2px dashed var(--faint); background: none !important; }
.tabs { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; }
.tabs button { border: 0; background: transparent; padding: 5px 10px; border-radius: 7px; color: var(--muted); cursor: pointer; font-size: 13px; }
.tabs button.on { background: var(--surface-3); color: var(--text); }

/* Listas */
.list { display: flex; flex-direction: column; }
.item { display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 10px 8px; border-radius: 12px; }
.item.clickable { cursor: pointer; }
.item.clickable:hover { background: var(--surface-2); }
.item .ico { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; font-size: 17px; background: var(--surface-3); position: relative; }
.item .title { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amt { text-align: right; font-weight: 600; }
.item .amt small { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.day-head { display: flex; justify-content: space-between; padding: 18px 8px 6px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.day-head:first-child { padding-top: 4px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--surface-3); color: var(--muted); font-weight: 500; }
.badge.ok { background: rgba(52, 211, 153, .12); color: var(--green); }
.badge.warn { background: rgba(251, 191, 36, .12); color: var(--amber); }
.badge.bad { background: rgba(248, 113, 113, .12); color: var(--red); }
.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty .big { font-size: 32px; margin-bottom: 8px; }

.accounts-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.acc-card { position: relative; overflow: hidden; cursor: pointer; transition: border-color .15s, transform .15s; }
.acc-card:hover { border-color: var(--border-strong); transform: translateY(-1px); }
.acc-card::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--c, var(--accent)); }
.acc-card .type { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.acc-card .name { font-size: 17px; font-weight: 600; margin: 4px 0 14px; }
.acc-card .bal { font-size: 24px; font-weight: 700; letter-spacing: -.02em; }
.acc-card.archived { opacity: .5; }

/* Filtros */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.filters .input, .filters select { width: auto; flex: 1 1 170px; min-width: 150px; }
.filters .search { flex: 2 1 240px; }
.filters .check { flex: none; }
.chips { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.chip { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; }
.chip .k { font-size: 12px; color: var(--muted); }
.chip .v { font-weight: 650; font-size: 16px; }

/* Selector de mes */
.month-picker { display: inline-flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 4px; }
.month-picker .lbl { min-width: 150px; text-align: center; font-weight: 600; text-transform: capitalize; cursor: pointer; padding: 4px 6px; border-radius: 8px; }
.month-picker .lbl:hover { background: var(--surface-2); }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border: 0; background: transparent; border-radius: 8px; cursor: pointer; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Formularios */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--text); font-weight: 600; cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { color: var(--red); border-color: rgba(248, 113, 113, .35); background: transparent; }
.btn.danger:hover { background: rgba(248, 113, 113, .1); }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .6; cursor: progress; }
.input, select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface-2); outline: none; transition: border-color .15s, box-shadow .15s;
}
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 30px; }
.input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124, 155, 255, .18); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 500; }
.field input:not([type=checkbox]):not([type=color]) { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface-2); outline: none; }
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124, 155, 255, .18); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.check { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; cursor: pointer; color: var(--muted); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
input[type=color] { width: 44px; height: 40px; padding: 2px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface-2); cursor: pointer; }
.amount-input { font-size: 32px !important; font-weight: 700; text-align: center; letter-spacing: -.02em; padding: 14px !important; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 16px; }
.seg label { text-align: center; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--muted); font-weight: 600; font-size: 14px; }
.seg input { display: none; }
.seg input:checked + span { color: var(--text); }
.seg label:has(input:checked) { background: var(--surface-3); }
.seg label:has(input[value=gasto]:checked) span { color: var(--red); }
.seg label:has(input[value=ingreso]:checked) span { color: var(--green); }
.seg label:has(input[value=transferencia]:checked) span { color: var(--accent); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.swatches button { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatches button.on { border-color: var(--text); }

/* Modal */
.backdrop { position: fixed; inset: 0; background: rgba(5, 6, 10, .65); backdrop-filter: blur(4px); display: grid; place-items: center; z-index: 60; padding: 16px; animation: fade .15s ease; }
.modal { width: min(480px, 100%); max-height: calc(100vh - 32px); overflow: auto; background: var(--surface); border: 1px solid var(--border-strong); border-radius: 20px; box-shadow: var(--shadow); animation: pop .18s ease; }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 6px; }
.modal header h2 { font-size: 18px; }
.modal .body { padding: 12px 20px 8px; }
.modal footer { display: flex; gap: 10px; justify-content: flex-end; padding: 8px 20px 20px; }
.modal footer .spacer { flex: 1; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

/* FAB y toast */
.fab {
  position: fixed; right: 28px; bottom: 28px; width: 58px; height: 58px; border-radius: 18px; border: 0;
  background: var(--accent-strong); color: #fff; display: grid; place-items: center; cursor: pointer;
  box-shadow: 0 10px 30px rgba(95, 128, 255, .4); z-index: 40; transition: transform .15s, background .15s;
}
.fab:hover { transform: translateY(-2px); background: var(--accent); }
.fab svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
#toast { position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); background: var(--surface-3); border: 1px solid var(--border-strong); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: .2s; z-index: 80; box-shadow: var(--shadow); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { border-color: rgba(248, 113, 113, .5); color: #fecaca; }

.alert { background: rgba(248, 113, 113, .1); border: 1px solid rgba(248, 113, 113, .35); color: #fecaca; padding: 10px 14px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
.loading { padding: 60px; text-align: center; color: var(--muted); }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 24px 0 10px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; }

/* Auth */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(1200px 600px at 20% -10%, #1a2140 0%, transparent 60%), var(--bg); }
.auth-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 32px 28px; box-shadow: var(--shadow); }
.auth-card .brand { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 18px; margin-bottom: 24px; }
.auth-card h1 { font-size: 24px; margin-bottom: 6px; }
.auth-card p { margin: 0 0 22px; }
.auth-card .btn { margin-top: 6px; padding: 12px; }

/* ───────── Responsive ───────── */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3-2 { grid-template-columns: minmax(0, 1fr); }
  .grid-3 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 820px) {
  .nav {
    inset: auto 0 0 0; width: 100%; height: calc(64px + env(safe-area-inset-bottom));
    flex-direction: row; padding: 6px 4px env(safe-area-inset-bottom); border-right: 0; border-top: 1px solid var(--border);
    justify-content: space-around; background: rgba(18, 21, 28, .92); backdrop-filter: blur(12px);
  }
  .nav .brand { display: none; }
  .nav a { flex-direction: column; gap: 3px; font-size: 10.5px; padding: 6px 4px; flex: 1; text-align: center; }
  .nav a.active { background: transparent; color: var(--accent); }
  .view { margin-left: 0; padding: 20px 16px calc(110px + env(safe-area-inset-bottom)); }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .fab { right: 18px; bottom: calc(80px + env(safe-area-inset-bottom)); width: 54px; height: 54px; }
  #toast { bottom: calc(150px + env(safe-area-inset-bottom)); }
  h1 { font-size: 22px; }
  .kpi .value { font-size: 21px; }
  .card { padding: 16px; }
  .cat-chart { grid-template-columns: 1fr; justify-items: center; }
  .cat-list { width: 100%; max-height: none; }
  .filters .input, .filters select { min-width: 0; flex: 1 1 45%; }
  .month-picker .lbl { min-width: 120px; }
  .backdrop { align-items: end; padding: 0; }
  .modal { width: 100%; border-radius: 20px 20px 0 0; max-height: 92vh; }
}
@media (max-width: 420px) {
  .kpis { gap: 10px; }
  .kpi { padding: 14px; }
  .kpi .value { font-size: 18px; }
  .row { grid-template-columns: 1fr; gap: 0; }
}
