:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --line: #e3dfd5;
  --ink: #1b1e22;
  --ink-2: #565b63;
  --ink-3: #868b93;
  --accent: #1d6b4b;
  --accent-ink: #ffffff;
  --accent-soft: #e2f1e9;
  --good: #1b7f4b;
  --good-soft: #e3f3ea;
  --warn: #9a5800;
  --warn-soft: #fbf0dc;
  --bad: #b42318;
  --bad-soft: #fbe6e3;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 20, 30, .06), 0 4px 16px rgba(20, 20, 30, .05);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  /* Gráficos (paleta categórica validada, orden fijo) */
  --s1: #257de0; --s2: #f47230; --s3: #00b894; --s4: #f2a600; --s5: #ee7fac; --s6: #008a17; --s7: #6a4ce2; --s8: #ec4647;
  --s-other: #a3a3a3;
  --chart-grid: #ebe8e1;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181b; --surface: #1e2226; --surface-2: #272c31; --line: #323840;
    --ink: #eceef1; --ink-2: #a8aeb6; --ink-3: #7b828b;
    --accent: #5cc796; --accent-ink: #0c2219; --accent-soft: #183a2b;
    --good: #5fd39a; --good-soft: #17352a; --warn: #f0b456; --warn-soft: #3a2e17;
    --bad: #ff8a7a; --bad-soft: #3d1f1c;
    --shadow: 0 1px 2px rgba(0,0,0,.35);
    --s1: #338ced; --s2: #e76624; --s3: #00a888; --s4: #d28e05; --s5: #e24d8b; --s6: #0aac23; --s7: #9887ef; --s8: #eb6263;
    --s-other: #6b6b6b;
    --chart-grid: #2c3137;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { overflow-x: hidden; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 24px; letter-spacing: -.01em; }
h2 { font-size: 17px; }
h3 { font-size: 12.5px; color: var(--ink-2); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--good); }
.neg { color: var(--bad); }
.pad { padding: 24px; }
[hidden] { display: none !important; }
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Auth ---------- */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 10px; }
.auth-card form { display: grid; gap: 12px; margin-top: 8px; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 230px minmax(0, 1fr); min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; padding: 18px 12px; border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.side-brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.side-brand > div { display: grid; min-width: 0; }
.space-switch { border: 0; background: none; padding: 0; text-align: left; color: var(--ink-2); font-size: 12.5px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.space-switch::after { content: " ▾"; }
.side a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; color: var(--ink-2); text-decoration: none; font-weight: 500; }
.side a .ico svg { width: 19px; height: 19px; display: block; }
@media (hover: hover) { .side a:hover { background: var(--surface-2); color: var(--ink); } }
.side a.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.side .nav-settings { margin-top: auto; }
.side .nav-more { display: none; }
.main { min-width: 0; }
#view { padding: 28px 32px 120px; max-width: 1180px; outline: none; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card > h3 { margin-bottom: 12px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.stack { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
.grid > *, .stack > * { min-width: 0; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }

/* ---------- Stat tiles ---------- */
.tile .label { font-size: 13px; color: var(--ink-2); }
.tile .value { font-size: 26px; font-weight: 650; letter-spacing: -.02em; margin-top: 4px; }
.tile .sub { font-size: 13px; color: var(--ink-2); margin-top: 4px; }
.hero .value { font-size: 34px; }

/* ---------- Buttons & forms ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 550; text-decoration: none; color: var(--ink); min-height: 38px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
@media (hover: hover) { .btn:hover { filter: brightness(.97); } }
label { display: grid; gap: 5px; font-size: 13px; color: var(--ink-2); font-weight: 500; }
input, select, textarea { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 11px; font-size: 16px; color: var(--ink); min-height: 42px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=checkbox] { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
label.check { display: flex; align-items: flex-start; gap: 10px; color: var(--ink); font-size: 14px; font-weight: 400; }
label.check input { flex: none; margin-top: 1px; }
.fields { display: grid; gap: 12px; }
.fields.two { grid-template-columns: 1fr 1fr; }
.help { font-size: 12.5px; color: var(--ink-3); font-weight: 400; }
.notice { padding: 10px 12px; border-radius: 10px; font-size: 14px; margin: 0; }
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.good { background: var(--good-soft); color: var(--good); }
.notice.info { background: var(--accent-soft); color: var(--ink); }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 11px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 7px 14px; border-radius: 9px; cursor: pointer; font-weight: 550; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg.full { display: flex; }
.seg.full button { flex: 1; }
.seg button.on[data-v=expense] { color: var(--bad); }
.seg button.on[data-v=income] { color: var(--good); }

.month-nav { display: inline-flex; align-items: center; gap: 4px; }
.month-nav button { border: 0; background: var(--surface); box-shadow: var(--shadow); width: 34px; height: 34px; border-radius: 10px; cursor: pointer; font-size: 17px; }
.month-nav span { min-width: 150px; text-align: center; font-weight: 600; text-transform: capitalize; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.filters select, .filters input { width: auto; min-width: 150px; }

/* ---------- Lists ---------- */
.list { display: grid; grid-template-columns: minmax(0, 1fr); }
.item { display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.item:last-child { border-bottom: 0; }
@media (hover: hover) { .item:hover { background: var(--surface-2); border-radius: 10px; } }
.item .ic { width: 38px; height: 38px; border-radius: 11px; background: var(--surface-2); display: grid; place-items: center; font-size: 18px; flex: none; }
.item .body { flex: 1; min-width: 0; }
.item .t1 { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .t2 { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .amt { text-align: right; font-weight: 600; }
.item .amt .t2 { font-weight: 400; }
.day-head { font-size: 12.5px; font-weight: 600; color: var(--ink-3); padding: 14px 4px 4px; text-transform: capitalize; display: flex; justify-content: space-between; }
.tag { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); vertical-align: 1px; }
.tag.good { background: var(--good-soft); color: var(--good); }
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.info { background: var(--accent-soft); color: var(--accent); }
.empty { text-align: center; color: var(--ink-2); padding: 28px 12px; }
.empty .btn { margin-top: 10px; }

/* ---------- Accounts ---------- */
.acc-group { margin-bottom: 18px; }
.acc-group .card-head h3 { margin: 0; }
.acc-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar.warn > i { background: var(--warn); }
.bar.bad > i { background: var(--bad); }

/* ---------- Charts ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--chart-grid); stroke-width: 1; }
.chart .axis-label { fill: var(--ink-3); stroke: none; font-size: 11px; }
.chart .hit { fill: transparent; stroke: none; cursor: pointer; }
.chart-tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--bg); font-size: 12.5px; padding: 7px 10px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, calc(-100% - 10px)); z-index: 5; box-shadow: var(--shadow); }
.chart-tip b { font-weight: 650; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; color: var(--ink-2); margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.donut-wrap { display: grid; grid-template-columns: minmax(140px, 200px) minmax(0, 1fr); gap: 20px; align-items: center; }
.donut-center { font-size: 12px; fill: var(--ink-2); stroke: none; }
.donut-center-v { font-size: 17px; font-weight: 650; fill: var(--ink); stroke: none; }
.cat-rows { display: grid; gap: 2px; }
.cat-row { display: grid; grid-template-columns: 12px minmax(0, 1fr) auto auto; gap: 10px; align-items: center; padding: 6px 4px; border-radius: 8px; font-size: 14px; cursor: pointer; }
@media (hover: hover) { .cat-row:hover { background: var(--surface-2); } }
.cat-row i { width: 10px; height: 10px; border-radius: 3px; }
.cat-row .pct { color: var(--ink-3); font-size: 12.5px; min-width: 42px; text-align: right; }

table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; font-size: 12px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .04em; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 9px 6px; border-bottom: 1px solid var(--line); }
.tbl tr:last-child td { border-bottom: 0; }
.tbl .r { text-align: right; }
.table-scroll { overflow-x: auto; }

/* ---------- Debts ---------- */
.debt-flag { font-size: 12.5px; }
.sim-result { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.sim-col { border: 1px solid var(--line); border-radius: 12px; padding: 14px; }
.sim-col.best { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.sim-col .big { font-size: 22px; font-weight: 650; margin: 2px 0 6px; }
.order-list { margin: 8px 0 0; padding-left: 20px; font-size: 13.5px; }
.order-list li { margin: 3px 0; }

/* ---------- FAB, modal, toast ---------- */
.fab { position: fixed; right: 24px; bottom: 24px; width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 20px rgba(0,0,0,.18); cursor: pointer; display: grid; place-items: center; z-index: 20; }
.fab svg { width: 26px; height: 26px; stroke-width: 2.4; }
dialog { border: 0; border-radius: 18px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10, 12, 16, .45); }
.modal-body { padding: 22px; display: grid; gap: 14px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head .x { border: 0; background: var(--surface-2); width: 32px; height: 32px; border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--ink-2); }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.modal-foot .left { margin-right: auto; }
.amount-input { font-size: 30px; font-weight: 650; text-align: center; min-height: 60px; letter-spacing: -.01em; }
#toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 100; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }
.picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(98px, 1fr)); gap: 6px; max-height: 230px; overflow-y: auto; padding: 2px; }
.picker button { border: 1px solid var(--line); background: var(--surface); border-radius: 11px; padding: 8px 4px; cursor: pointer; display: grid; gap: 2px; justify-items: center; font-size: 12px; line-height: 1.2; color: var(--ink-2); text-align: center; }
.picker button .e { font-size: 20px; }
.picker button.on { border-color: var(--accent); background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.more-menu { display: grid; gap: 8px; }
.more-menu a { display: flex; align-items: center; gap: 12px; padding: 14px; border-radius: 12px; background: var(--surface-2); color: var(--ink); text-decoration: none; font-weight: 550; }
.more-menu a svg { width: 20px; height: 20px; }

/* ---------- Mobile ---------- */
@media (max-width: 1100px) {
  .donut-wrap { grid-template-columns: 1fr; }
  .donut-wrap .chart { max-width: 210px; width: 100%; margin: 0 auto; }
}
@media (max-width: 860px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: minmax(0, 1fr); }
  .g2 { grid-template-columns: minmax(0, 1fr); }
  .sim-result { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; flex-direction: row; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--line); background: var(--bg); z-index: 30; overflow: visible; }
  .side-brand, .side a[data-view=presupuestos], .side a[data-view=deudas], .side a[data-view=objetivos], .side a[data-view=recurrentes], .side .nav-settings { display: none; }
  .side .nav-more { display: flex; }
  .side a { flex-direction: column; gap: 2px; padding: 6px 4px; font-size: 10.5px; flex: 1; min-width: 0; text-align: center; }
  .side a.active { box-shadow: none; background: transparent; color: var(--accent); }
  #view { padding: 16px 14px calc(150px + env(safe-area-inset-bottom)); }
  .fab { bottom: calc(76px + env(safe-area-inset-bottom)); right: 16px; }
  #toast { bottom: calc(150px + env(safe-area-inset-bottom)); }
  .donut-wrap { grid-template-columns: 1fr; }
  .donut-wrap .chart { max-width: 230px; margin: 0 auto; }
  .fields.two { grid-template-columns: 1fr; }
  .tile .value { font-size: 21px; }
  .hero .value { font-size: 28px; }
  .month-nav span { min-width: 120px; }
  .filters select, .filters input { flex: 1 1 140px; }
  dialog { width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 18px 18px 0 0; }
  .modal-body { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .mobile-space { display: block !important; }
}
.mobile-space { display: none; }
