:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eef0ec;
  --text: #1b1f1d;
  --muted: #6b726e;
  --line: #e2e5e0;
  --on-accent: #ffffff;
  --pos: #15803d;
  --neg: #c2410c;
  --inc: #2f9e6e;
  --exp: #e0884a;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111413;
    --surface: #1b1f1d;
    --surface-2: #252a27;
    --text: #e9ece9;
    --muted: #9aa29d;
    --line: #2e3430;
    --on-accent: #0b1412;
    --pos: #4ade80;
    --neg: #fb923c;
    --inc: #3fbf87;
    --exp: #f0a06a;
    --shadow: none;
    color-scheme: dark;
  }
}

/* Paletas de color (se eligen en Ajustes) */
:root, :root[data-accent="verde"] { --accent: #0f766e; --accent-soft: #d5ede9; }
:root[data-accent="azul"] { --accent: #1d4ed8; --accent-soft: #dde6fb; }
:root[data-accent="violeta"] { --accent: #6d28d9; --accent-soft: #ebe2fb; }
:root[data-accent="rosa"] { --accent: #be185d; --accent-soft: #fbe1ec; }
:root[data-accent="grafito"] { --accent: #334155; --accent-soft: #e2e8f0; }
@media (prefers-color-scheme: dark) {
  :root, :root[data-accent="verde"] { --accent: #2dd4bf; --accent-soft: #163b37; }
  :root[data-accent="azul"] { --accent: #60a5fa; --accent-soft: #172a4a; }
  :root[data-accent="violeta"] { --accent: #a78bfa; --accent-soft: #2a2048; }
  :root[data-accent="rosa"] { --accent: #f472b6; --accent-soft: #45182f; }
  :root[data-accent="grafito"] { --accent: #cbd5e1; --accent-soft: #2a313b; }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
h1, h3, h4 { margin: 0 0 10px; }
h3 { font-size: 15px; }
h4 { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

#shell { max-width: 560px; margin: 0 auto; padding-bottom: calc(90px + env(safe-area-inset-bottom)); }
/* minmax(0, 1fr): la columna nunca se estira por un texto largo; el ancho lo pone la pantalla */
#view { padding: 0 16px 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }

/* Encabezado y selector de mes */
.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
}
.demo {
  font-size: 12px; background: var(--accent-soft); color: var(--text);
  padding: 6px 10px; border-radius: 10px; margin-bottom: 8px; text-align: center;
}
.demo button { background: none; border: 0; color: var(--accent); font-weight: 600; text-decoration: underline; padding: 0; }
.month-bar { display: flex; align-items: center; justify-content: space-between; }
.month-bar .period-btn {
  flex: 1; min-width: 0; margin: 0 8px; border: 0; background: none; padding: 6px;
  display: flex; align-items: center; justify-content: center; gap: 4px;
  font-weight: 700; font-size: 17px;
}
.period-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.period-btn svg { width: 18px; height: 18px; flex: none; fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; }
.list-summary { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); padding: 0 4px; font-variant-numeric: tabular-nums; }
.list-summary span:not(:first-child) { font-weight: 600; color: var(--text); }
.list-summary span.pos { color: var(--pos); }
.month-bar > button {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: var(--surface); font-size: 22px; box-shadow: var(--shadow);
}
.month-bar > button:disabled { opacity: 0.3; cursor: default; }

/* Tarjetas */
.card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card.empty { text-align: center; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.stat.wide { grid-column: 1 / -1; background: var(--accent); color: var(--on-accent); }
.stat.wide strong { font-size: 28px; }
.stat.wide .label, .stat.wide .sub { color: inherit; opacity: 0.85; }
.stat.alert { background: var(--neg); }
.label { font-size: 13px; color: var(--muted); }
.sub { font-size: 13px; }

.row {
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
  padding: 8px 0; border-top: 1px solid var(--line);
}
.row:first-of-type { border-top: 0; }
.row > span:last-child { font-variant-numeric: tabular-nums; white-space: nowrap; }
.row.total { font-weight: 700; }

/* Barras por categoría */
.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 6px 0; }
.bar-row .amt { font-variant-numeric: tabular-nums; }
.bar { grid-column: 1 / -1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

/* Tendencia 6 meses */
.trend { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; height: 140px; align-items: end; }
.tcol { display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; }
.tcol span { font-size: 12px; color: var(--muted); }
.tcol.current span { color: var(--text); font-weight: 700; }
.tbars { flex: 1; width: 100%; display: flex; gap: 3px; align-items: end; justify-content: center; }
.tbars i { width: 40%; max-width: 16px; border-radius: 4px 4px 0 0; min-height: 2px; }
i.inc { background: var(--inc); }
i.exp { background: var(--exp); }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* Lista de movimientos */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; white-space: nowrap;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.day { margin: 6px 4px -4px; }
.txlist { list-style: none; margin: 0; padding: 4px 16px; }
.txlist .tx:first-child { border-top: 0; }
section .txlist { padding: 0; }
.tx { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.tx.readonly { cursor: default; }
.tx .ico {
  width: 38px; height: 38px; flex: none; border-radius: 12px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 18px;
}
.tx .txt { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.tx .txt b, .tx .txt small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .txt b { font-weight: 600; }
.tx .txt small { color: var(--muted); font-size: 12px; }
.tx .amt { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }

/* Convivencia */
.verdict { text-align: center; }
.verdict p { font-size: 20px; margin: 6px 0 12px; }
.verdict.pos p b { color: var(--pos); }
.verdict.neg p b { color: var(--neg); }
.verdict small { display: block; margin-top: 8px; }
table.split { width: 100%; border-collapse: collapse; margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.split th { font-size: 13px; color: var(--muted); font-weight: 600; text-align: right; padding: 4px 0; }
.split td { padding: 8px 0 8px 8px; border-top: 1px solid var(--line); text-align: right; white-space: nowrap; }
.split td:first-child { white-space: normal; padding-left: 0; }
.split td:first-child { text-align: left; color: var(--muted); }
.split td small { display: block; font-size: 12px; color: var(--muted); }
.split tr.diff td { font-weight: 600; }
.code {
  font: 700 28px/1 ui-monospace, Consolas, monospace; letter-spacing: 4px;
  background: var(--accent-soft); border-radius: 12px; padding: 14px; margin: 12px 0;
}
.code.small { font-size: 15px; letter-spacing: 2px; padding: 4px 8px; margin: 0; }

/* Ajustes */
.plain { list-style: none; padding: 0; margin: 0; }
.plain li { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.plain li span:first-child { color: var(--muted); }
.plain li span:nth-child(2) { margin-left: auto; }

/* Colores */
.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.swatch { border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); padding: 0; }
.swatch i { width: 40px; height: 40px; border-radius: 50%; border: 3px solid var(--surface); box-shadow: 0 0 0 2px var(--line); }
.swatch.on i { box-shadow: 0 0 0 2px var(--text); }
.swatch.on { color: var(--text); font-weight: 600; }

/* Tipos de gasto e ingreso */
.catlist { list-style: none; padding: 0; margin: 0 0 12px; }
.catlist li { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); }
.catlist li:first-child { border-top: 0; }
.catlist .ico { width: 32px; height: 32px; border-radius: 10px; background: var(--surface-2); display: grid; place-items: center; flex: none; }
.catlist .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.small { padding: 4px 10px; font-size: 14px; opacity: 0.45; }
.chip.small.on { opacity: 1; background: var(--accent-soft); color: var(--text); border-color: var(--accent); }
.cat-add { display: flex; gap: 8px; }
.cat-add select { flex: none; width: 64px; font-size: 18px; text-align: center; }

/* Formularios */
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input, select, textarea {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px;
  padding: 11px 12px; font-size: 16px; width: 100%; min-width: 0;
}
textarea { resize: vertical; }
.priv-text { min-height: 45vh; font-family: ui-monospace, Consolas, monospace; font-size: 12px; line-height: 1.45; white-space: pre; overflow: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.stack { display: grid; gap: 12px; }
.inline { display: flex; gap: 8px; }
.two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.btn {
  border: 0; border-radius: 12px; padding: 11px 16px; font-weight: 600;
  background: var(--accent); color: var(--on-accent);
}
.btn.big { padding: 14px; font-size: 16px; width: 100%; }
.btn.small { padding: 8px 12px; font-size: 14px; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { color: var(--neg); }
.btn:disabled { opacity: 0.5; }
.icon-btn { border: 0; background: var(--surface-2); width: 32px; height: 32px; border-radius: 50%; flex: none; }

/* Hoja inferior */
dialog#sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 560px;
  background: var(--surface); color: var(--text); border-radius: 20px 20px 0 0;
  max-height: 92vh; overflow-y: auto; overflow-x: hidden;
}
dialog#sheet[open] { animation: up 0.18s ease-out; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-body { padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); display: grid; gap: 14px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head h3 { margin: 0; font-size: 17px; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button, .seg label span {
  border: 0; background: none; padding: 9px; border-radius: 9px; font-weight: 600; color: var(--muted);
  display: block; text-align: center; font-size: 14px;
}
.seg button.on, .seg input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.amount { flex-direction: row; align-items: center; justify-content: center; font-size: 34px; font-weight: 700; color: var(--text); }
.amount input {
  border: 0; background: none; font-size: 40px; font-weight: 700; text-align: center;
  width: 8ch; padding: 4px; font-variant-numeric: tabular-nums;
}
.amount input:focus { outline: 0; }
.cats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cat {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 8px 2px; font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  overflow: hidden; text-overflow: ellipsis;
}
.cat span { font-size: 20px; }
.cat.on { border-color: var(--accent); background: var(--accent-soft); font-weight: 600; }

/* Navegación inferior */
.nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  max-width: 560px; margin: 0 auto;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.nav button {
  border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; color: var(--muted); padding: 4px 0;
}
.nav svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.nav button.active { color: var(--accent); }
.nav .fab {
  width: 56px; height: 56px; border-radius: 50%; justify-self: center; margin-top: -26px;
  background: var(--accent); color: var(--on-accent);
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 40%, transparent);
}
.nav .fab svg { width: 28px; height: 28px; stroke-width: 2.5; }

/* Pantallas de ingreso */
#screen { max-width: 440px; margin: 0 auto; padding: calc(32px + env(safe-area-inset-top)) 16px 32px; }
.auth { display: grid; gap: 14px; text-align: center; }
.auth form { text-align: left; }
.auth h1 { margin: 0; }
.logo {
  width: 64px; height: 64px; margin: 0 auto; border-radius: 18px; background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center; font-size: 34px; font-weight: 800;
}

/* Avisos */
#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: 0.2s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--neg); color: #fff; }

/* Importar resumen */
.import-btn { width: 100%; }
.card-order { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.card-order li { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 4px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 4px 6px 4px 12px; }
.card-order label { flex-direction: row; align-items: center; gap: 10px; min-width: 0; font-size: 15px; color: var(--text); padding: 8px 0; }
.card-order input { width: 20px; height: 20px; padding: 0; flex: none; accent-color: var(--accent); }
.card-order .icon-btn:disabled { opacity: 0.25; }
#home-edit { justify-self: center; }
.import-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.import-row .btn { padding-left: 8px; padding-right: 8px; }
.mic { display: block; margin: 0 auto; align-self: center; width: 84px; height: 84px; border-radius: 50%; border: 0; font-size: 36px; background: var(--accent-soft); cursor: pointer; }
.mic.on { background: var(--accent); animation: mic-pulse 1.2s ease-in-out infinite; }
@keyframes mic-pulse { 50% { box-shadow: 0 0 0 12px var(--accent-soft); } }
.mic-state { text-align: center; margin: 0; min-height: 1.2em; }
.plan-card .btn { width: 100%; margin-top: 10px; }
.plan .card h4 { margin: 14px 0 6px; }
.plan .card h4:first-child { margin-top: 0; }
.plan .row.big { font-size: 17px; margin-top: 8px; }
.plan-list { list-style: none; margin: 0; padding: 0; }
.plan-list li { display: grid; grid-template-columns: 32px 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line, rgba(0,0,0,.06)); }
.plan-list li:last-child { border-bottom: 0; }
.plan-list .txt { display: flex; flex-direction: column; min-width: 0; }
.plan-list .txt b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plan-list .txt small { color: var(--muted, #6b7280); font-size: 12px; }
.plan-list .ico { font-size: 20px; text-align: center; }
.notice { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; font-size: 14px; margin: 0; }
.check { display: flex; flex-direction: row; gap: 10px; align-items: flex-start; font-size: 14px; }
.check input { width: auto; margin-top: 3px; }
.check.warn { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; }
.imp-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.imp-row { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); border-radius: 12px; padding: 10px; background: var(--surface); }
.imp-row > input[type=checkbox] { width: 20px; height: 20px; margin-top: 2px; flex: none; accent-color: var(--accent); }
.imp-row.off { opacity: 0.5; }
.imp-main { flex: 1; min-width: 0; display: grid; gap: 4px; }
.imp-top { display: flex; justify-content: space-between; gap: 8px; }
.imp-top b { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.imp-top .amt { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.imp-main small { font-size: 12px; }
.imp-main .dup { color: var(--neg); }
.imp-edit { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 6px; align-items: center; }
.imp-edit input, .imp-edit select { padding: 6px 8px; font-size: 14px; min-width: 0; }
.imp-total { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; justify-content: space-between; font-variant-numeric: tabular-nums; }
.imp-total span { color: var(--muted); font-size: 14px; }
.imp-total small { width: 100%; text-align: right; }
.imp-check { display: grid; gap: 2px; border-radius: 12px; padding: 10px 12px; font-size: 14px; font-variant-numeric: tabular-nums; }
.imp-check.ok { background: color-mix(in srgb, var(--pos) 12%, transparent); }
.imp-check.bad { background: color-mix(in srgb, var(--neg) 14%, transparent); }
.imp-check small { color: var(--muted); font-size: 12px; }
.unit-note { margin: -4px 4px 0; }
.bar-row.warn .bar i { background: #d99a0b; }
.bar-row.over .bar i { background: var(--neg); }
.bar-row .over-note { grid-column: 1 / -1; color: var(--neg); font-size: 12px; }
.bar-row .amt small { font-size: 12px; }
.fixed-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.fixed-list li { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 6px 10px; align-items: center; }
.fixed-list .txt { display: grid; min-width: 0; }
.fixed-list .txt b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fixed-list .txt small { color: var(--muted); font-size: 12px; }
.fixed-actions { grid-column: 2 / -1; display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 6px; align-items: center; }
.fixed-actions input { padding: 7px 10px; font-size: 15px; }
.budget-grid { display: grid; grid-template-columns: minmax(0, 1fr) 130px; gap: 6px 10px; align-items: center; }
.budget-grid span { font-size: 15px; }
.budget-grid input { padding: 7px 10px; font-size: 15px; text-align: right; }
