:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --in: #15803d;
  --out: #b91c1c;
  --tr: #6b7280;
  --soft: #eef2f7;
  --warn: #b45309;
  --bar: #c7d2fe;
  --radius: 14px;
  /* Colores de los gráficos: distintos entre sí y legibles en claro y en oscuro */
  --c1: #2563eb; --c2: #f59e0b; --c3: #db2777; --c4: #0d9488; --c5: #7c3aed; --c6: #ea580c; --c7: #65a30d;
  --c-gris: #9ca3af; --c-resto: #d1d5db;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --card: #131a22;
    --ink: #e5e7eb;
    --muted: #9ca3af;
    --line: #243041;
    --accent: #60a5fa;
    --accent-ink: #0b0f14;
    --in: #4ade80;
    --out: #f87171;
    --tr: #9ca3af;
    --soft: #1b2430;
    --warn: #fbbf24;
    --bar: #334a74;
    --c1: #60a5fa; --c2: #fbbf24; --c3: #f472b6; --c4: #2dd4bf; --c5: #a78bfa; --c6: #fb923c; --c7: #a3e635;
    --c-gris: #6b7280; --c-resto: #374151;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

#app { max-width: 560px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
.boot { padding: 40vh 16px 0; text-align: center; color: var(--muted); }

/* Cabecera */
.top {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  padding: calc(10px + env(safe-area-inset-top)) 16px 8px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.brand { font-weight: 700; font-size: 17px; }
.demo-aviso { margin: 0; padding: 8px 12px; border-radius: 10px; background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); font-size: 13px; font-weight: 600; }
.demo { font-size: 11px; font-weight: 600; background: var(--soft); color: var(--muted); padding: 2px 6px; border-radius: 6px; vertical-align: middle; }
.month { display: flex; align-items: center; gap: 2px; font-weight: 600; }
.month span { min-width: 128px; text-align: center; }
.icon { background: none; border: 0; width: 36px; height: 36px; border-radius: 10px; font-size: 22px; line-height: 1; color: var(--ink); }
.icon:disabled { opacity: .25; cursor: default; }
.icon:not(:disabled):hover { background: var(--soft); }

main { padding: 4px 16px 16px; display: grid; gap: 12px; }
.updated { color: var(--muted); font-size: 12.5px; margin: -2px 2px 0; }

/* Tarjetas */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.card h3 { font-size: 13px; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
.big { font-size: 30px; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.pair div { background: var(--soft); border-radius: 10px; padding: 8px 10px; }
.pair small { display: block; color: var(--muted); font-size: 12px; }
.pair strong { font-size: 16px; }
.in { color: var(--in); }
.out { color: var(--out); }
.tr { color: var(--tr); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Barras */
.bars { display: grid; gap: 7px; }
.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; font-size: 14px; }
.bar-row .track { grid-column: 1 / -1; height: 6px; background: var(--soft); border-radius: 4px; overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--bar); border-radius: 4px; }
.bar-row .fill.in { background: color-mix(in srgb, var(--in) 55%, transparent); }
.bar-row .fill.out { background: color-mix(in srgb, var(--out) 45%, transparent); }

/* Gráfico */
.chart svg { width: 100%; height: auto; display: block; }
.chart .lbl { fill: var(--muted); font-size: 9px; text-anchor: middle; }
.chart .zero { stroke: var(--line); }
.chart .pos { fill: color-mix(in srgb, var(--in) 60%, transparent); }
.chart .neg { fill: color-mix(in srgb, var(--out) 55%, transparent); }
.chart .sel { stroke: var(--ink); stroke-width: 1.2; }

.chart .bi { fill: color-mix(in srgb, var(--in) 70%, transparent); }
.chart .bg { fill: color-mix(in srgb, var(--out) 62%, transparent); }

/* Círculo con leyenda */
.donut { display: grid; grid-template-columns: 132px 1fr; gap: 14px; align-items: center; margin-top: 4px; }
.donut svg { width: 132px; height: 132px; display: block; }
.donut .d-total { text-anchor: middle; font-size: 15px; font-weight: 700; fill: var(--ink); }
.donut .d-sub { text-anchor: middle; font-size: 10px; fill: var(--muted); }
.leyenda { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; font-size: 13.5px; }
.leyenda li { display: grid; grid-template-columns: 12px 1fr auto 38px; gap: 8px; align-items: center; }
.leyenda .ln { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leyenda .lv { font-weight: 600; white-space: nowrap; }
.leyenda .lp { color: var(--muted); text-align: right; font-size: 12.5px; }
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.sw-in { background: color-mix(in srgb, var(--in) 70%, transparent); }
.sw-out { background: color-mix(in srgb, var(--out) 62%, transparent); }
.ley-mes { display: flex; gap: 16px; font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.ley-mes span { display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 380px) { .donut { grid-template-columns: 1fr; justify-items: center; } .leyenda { width: 100%; } }

/* Necesario o capricho */
.n-nec { background: var(--c4); }
.n-cap { background: var(--c3); }
.n-sin { background: var(--c-resto); }
.natbar { display: flex; height: 16px; border-radius: 8px; overflow: hidden; background: var(--soft); margin: 4px 0 8px; }
.natbar > span { display: block; height: 100%; }
.natley { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 14px; }
.natley > span { display: inline-flex; align-items: center; gap: 6px; }
.nat-cuentas { display: grid; gap: 12px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.nat-cta .natbar { height: 10px; margin: 4px 0 4px; }
.nat-cols { display: grid; gap: 4px 24px; margin-top: 6px; }
.nat-cols h3 { display: flex; align-items: center; gap: 6px; }
.nat-row { gap: 12px; }
.seg.seg3s { grid-template-columns: repeat(3, auto); flex-shrink: 0; }
.seg.seg3s button { font-size: 12.5px; padding: 6px 8px; }
@media (min-width: 700px) { .nat-cols { grid-template-columns: 1fr 1fr; } .nat-cuentas { grid-template-columns: 1fr 1fr; } }

/* Un círculo por cuenta */
.cuentas-donuts { display: grid; gap: 18px; }
.cd { display: grid; gap: 6px; }
.cd + .cd { border-top: 1px solid var(--line); padding-top: 14px; }
.cd-head { display: flex; justify-content: space-between; gap: 10px; font-size: 15px; }
.cd-head span { font-weight: 700; }
.cd-head span .small { font-weight: 400; }

/* Previsión de gastos fijos */
.fijos-cifras { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 4px 0 6px; }
.fijos-cifras div { background: var(--soft); border-radius: 10px; padding: 8px 10px; }
.fijos-cifras small { display: block; color: var(--muted); font-size: 12px; }
.fijos-cifras strong { font-size: 16px; }
.fijos-grid { display: grid; gap: 0; }
.fijo-cta { padding-top: 14px; }
.fijo-cta + .fijo-cta { border-top: 1px solid var(--line); margin-top: 6px; }
.fijo-cta .cd-head { margin-bottom: 4px; align-items: baseline; }
.fijo-cta .cd-head > span { white-space: nowrap; }
.fijo-cta .row .amt { display: flex; align-items: center; gap: 6px; }

/* Barras por cuenta */
.hb { display: grid; gap: 14px; }
.hb-name { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; margin-bottom: 4px; }
.hb-name span { font-weight: 600; white-space: nowrap; }
.hb-bar { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; margin-top: 3px; }
.hb-bar > span { height: 10px; border-radius: 5px; display: block; max-width: 100%; }
.hb-bar em { font-style: normal; font-size: 12.5px; color: var(--muted); white-space: nowrap; min-width: 92px; text-align: right; }
.hb-in { background: color-mix(in srgb, var(--in) 70%, transparent); }
.hb-out { background: color-mix(in srgb, var(--out) 62%, transparent); }

/* Desplegables «ver más» */
details.mas { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
details.mas > summary { color: var(--accent); font-size: 13.5px; font-weight: 600; padding: 4px 0; }

/* Listas */
.rows { display: grid; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
.row:first-child { border-top: 0; }
.row .name { min-width: 0; }
.row .name b { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .name span { color: var(--muted); font-size: 12.5px; }
.row .amt { font-weight: 600; white-space: nowrap; }
button.row { width: 100%; background: none; border: 0; border-top: 1px solid var(--line); color: inherit; text-align: left; cursor: pointer; }
button.row .name > span { display: block; }
button.row:first-child { border-top: 0; }
button.row:hover .amt { color: var(--accent); }
.seg.seg2 { grid-template-columns: repeat(2, 1fr); max-width: 320px; }
.rep-chips { margin-top: -2px; }
.rep-list { display: grid; }
.rep-row { border-top: 1px solid var(--line); }
.rep-row:first-child { border-top: 0; }
.rep-row summary { list-style: none; display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; padding: 9px 0; font-weight: 400; font-size: 14px; }
.rep-row summary::-webkit-details-marker { display: none; }
.rep-row summary .track { grid-column: 1 / -1; height: 6px; background: var(--soft); border-radius: 4px; overflow: hidden; display: block; }
.rep-row summary .fill { display: block; height: 100%; border-radius: 4px; background: var(--bar); }
.rep-row summary .fill.in { background: color-mix(in srgb, var(--in) 55%, transparent); }
.rep-row summary .fill.out { background: color-mix(in srgb, var(--out) 45%, transparent); }
.rep-name { font-weight: 600; }
.rep-amt { font-weight: 600; white-space: nowrap; }
.rep-det { padding: 0 0 10px; color: var(--muted); font-size: 13px; display: grid; gap: 3px; }
.rep-top { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; margin-top: 4px; padding: 6px 10px; background: var(--soft); border-radius: 8px; color: var(--ink); }
.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 8px 2px -2px; font-weight: 600; }
.total-line { display: flex; justify-content: space-between; padding-top: 10px; margin-top: 4px; border-top: 1px solid var(--line); font-weight: 700; }

/* Movimientos */
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--card); padding: 6px 12px; border-radius: 999px; font-size: 13.5px; }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.search { width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); }
.day { font-size: 12.5px; color: var(--muted); font-weight: 600; margin: 10px 2px 4px; }
.txs { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.tx { display: grid; grid-template-columns: 10px 1fr auto; gap: 10px; align-items: center; width: 100%; text-align: left; background: none; border: 0; border-top: 1px solid var(--line); padding: 11px 14px; }
.tx:first-child { border-top: 0; }
.tx:hover { background: var(--soft); }
.tx.ro { cursor: default; }
.tx.ro:hover { background: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--tr); }
.dot.ingreso { background: var(--in); }
.dot.gasto { background: var(--out); }
.tx-main { min-width: 0; }
.tx-name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-sub { display: block; color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { font-weight: 600; white-space: nowrap; }
.tx-amt.tr { font-weight: 500; }
.tx-amt.ex, .big.ex { text-decoration: line-through; opacity: .7; }
.dot.excluido { background: transparent; border: 1.5px solid var(--tr); }
.seg.seg4 { grid-template-columns: repeat(4, 1fr); }
.seg.seg4 button { font-size: 13px; padding: 8px 2px; }

/* Botones y formularios */
.btn { border: 0; background: var(--accent); color: var(--accent-ink); padding: 11px 16px; border-radius: 12px; font-weight: 600; width: 100%; }
.btn.secondary { background: var(--soft); color: var(--ink); }
.btn.small { width: auto; padding: 7px 12px; font-size: 13.5px; border-radius: 10px; }
.btn.danger { background: none; color: var(--out); padding: 7px 4px; }
.btn:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; color: var(--accent); padding: 8px 0; font-weight: 600; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
input[type="text"], input[type="email"], input[type="password"], input[type="search"], select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--ink);
}
.form { display: grid; gap: 12px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--soft); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: none; padding: 8px; border-radius: 8px; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--card); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .12); }
.check { display: flex; gap: 8px; align-items: flex-start; color: var(--ink); font-size: 14px; }
.check input { margin-top: 3px; }
.acc-edit { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--soft); color: var(--muted); font-weight: 600; white-space: nowrap; }
.pill.ok { color: var(--in); }
.pill.warn { color: var(--warn); }
.pill.bad { color: var(--out); }
.err { color: var(--out); font-size: 12.5px; word-break: break-word; }
details summary { cursor: pointer; font-weight: 600; }
.rule { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 13.5px; }
.rule code { font-size: 12px; background: var(--soft); padding: 1px 5px; border-radius: 5px; word-break: break-all; }
.bank-list { max-height: 50vh; overflow: auto; display: grid; gap: 4px; }
.bank { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); text-align: left; width: 100%; }
.bank img { width: 28px; height: 28px; object-fit: contain; border-radius: 6px; background: #fff; }
.empty { text-align: center; padding: 26px 10px; color: var(--muted); }
.empty b { color: var(--ink); display: block; margin-bottom: 6px; font-size: 16px; }

/* Login */
.login { padding: 18vh 24px 24px; display: grid; gap: 14px; max-width: 400px; margin: 0 auto; }
.login h1 { margin: 0; font-size: 28px; }
.login-app { border-top: 1px solid var(--line); padding-top: 14px; margin-top: 4px; }
.login-app:empty { display: none; }

/* Navegación inferior */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabs.two { grid-template-columns: repeat(2, 1fr); }
.tabs.five { grid-template-columns: repeat(5, 1fr); padding-left: 2px; padding-right: 2px; }
.tabs.five button { font-size: 11px; padding: 4px 0; }
.tabs button { background: none; border: 0; display: grid; justify-items: center; gap: 2px; font-size: 11.5px; color: var(--muted); padding: 4px; }
.tabs button svg { width: 22px; height: 22px; }
.tabs button[aria-current="page"] { color: var(--accent); font-weight: 600; }
@media (min-width: 600px) and (max-width: 959px) { .tabs { max-width: 560px; margin: 0 auto; border: 1px solid var(--line); border-bottom: 0; border-radius: 14px 14px 0 0; } }

/* Ordenador: menú lateral y tarjetas en columnas para aprovechar el ancho */
@media (min-width: 960px) {
  #app { max-width: none; padding-bottom: 24px; }
  body.con-tabs #app { padding-left: 92px; }
  /* Ancho cómodo de leer y tarjetas encajadas en dos columnas, sin huecos (como un tablero) */
  .top { padding-left: 24px; padding-right: 24px; max-width: 1180px; margin: 0 auto; }
  main { display: block; max-width: 1180px; margin: 0 auto; padding: 8px 24px 40px; columns: 2; column-gap: 18px; }
  main > * { break-inside: avoid; }
  main > .card { display: block; width: 100%; margin: 0 0 18px; }
  main > :not(.card) { column-span: all; margin: 0 0 14px; }
  main > .card.empty, main > .card.ancha { column-span: all; }
  .ri { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); gap: 28px; align-items: start; }
  .ri-graf h3 { margin-top: 0; }
  .cuentas-donuts { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0; }
  .cd { padding: 0 18px; }
  .cd:first-child { padding-left: 0; }
  .cd + .cd { border-top: 0; padding-top: 0; border-left: 1px solid var(--line); }
  .fijos-cifras { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .fijos-grid { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); column-gap: 28px; }
  .fijo-cta + .fijo-cta { border-top: 0; margin-top: 0; }
  .seg.seg2 { margin-bottom: 14px; }
  .donut { grid-template-columns: 150px 1fr; }
  .donut svg { width: 150px; height: 150px; }
  .tabs {
    top: 0; bottom: 0; left: 0; right: auto; width: 92px; max-width: none; margin: 0;
    border: 0; border-right: 1px solid var(--line); border-radius: 0;
    grid-template-columns: 1fr !important; grid-auto-rows: min-content; align-content: start; gap: 4px;
    padding: 72px 8px 16px;
  }
  .tabs.five button, .tabs button { padding: 10px 2px; border-radius: 12px; font-size: 12px; }
  .tabs button[aria-current="page"] { background: var(--soft); }
  dialog#sheet { margin: auto; border-radius: 18px; max-height: 86dvh; }
  .sheet-in { max-height: 86dvh; padding-bottom: 18px; }
  #toast { bottom: 24px; }
  .txs .tx { padding: 10px 16px; }
}
/* Imprimir o guardar en PDF: solo el contenido */
@media print {
  .tabs, .top .month button, .no-print, .demo-aviso, #toast, .rep-chips { display: none !important; }
  body, #app { background: #fff; color: #000; padding: 0 !important; }
  main { display: block; max-width: none; padding: 0; columns: 1; }
  .card { break-inside: avoid; margin-bottom: 10px; border-color: #ccc; }
  details.rep-row > .rep-det { display: grid; }
  .top { position: static; backdrop-filter: none; }
}


/* Hoja (diálogo) */
dialog#sheet {
  border: 0; padding: 0; width: 100%; max-width: 560px; margin: auto auto 0; border-radius: 18px 18px 0 0;
  background: var(--card); color: var(--ink); max-height: 92dvh;
}
dialog#sheet::backdrop { background: rgb(0 0 0 / .4); }
.sheet-in { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); display: grid; gap: 12px; overflow: auto; max-height: 92dvh; }
.sheet-in h2 { margin: 0; font-size: 18px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 20;
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgb(0 0 0 / .2);
}
