:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #0f766e;
  --accent-soft: #ccfbf1;
  --gasto: #b91c1c;
  --ingreso: #15803d;
  --neutral: #1d4ed8;
  --chip: #eef0f3;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
  /* Series de datos: paleta validada (daltonismo y contraste) contra estas superficies */
  --s1: #2a78d6;
  --s2: #eb6834;
  --s0: #a3a19b;
  --grid: #ecebe7;
  --axis: #c3c2b7;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d10;
    --card: #16191e;
    --text: #f3f4f6;
    --muted: #9ca3af;
    --line: #262a31;
    --accent: #2dd4bf;
    --accent-soft: #123b37;
    --gasto: #f87171;
    --ingreso: #4ade80;
    --neutral: #93c5fd;
    --chip: #22262d;
    --shadow: none;
    --s1: #3987e5;
    --s2: #d95926;
    --s0: #6f6e69;
    --grid: #262a31;
    --axis: #3a3f47;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
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;
  padding: calc(env(safe-area-inset-top) + var(--alert-h, 0px)) 0 calc(72px + env(safe-area-inset-bottom));
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
/* iOS amplía la página al enfocar un campo de menos de 16px */
input, select, textarea { font-size: max(16px, 1em); }
html.sheet-open, html.sheet-open body { overflow: hidden; }
#app { overflow-x: clip; }
button { cursor: pointer; }

#app { max-width: 560px; margin: 0 auto; padding: 12px 16px 24px; }
h1 { font-size: 28px; margin: 8px 0 12px; letter-spacing: -.02em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 24px 4px 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.gasto { color: var(--gasto); }
.ingreso { color: var(--ingreso); }
.neutral { color: var(--neutral); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

.card { background: var(--card); border-radius: 16px; padding: 14px 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 10px; }
.grid2 > .card + .card { margin-top: 0; }

/* Captura rápida */
.composer textarea {
  width: 100%; border: 0; outline: 0; resize: none; background: transparent;
  font-size: 18px; min-height: 28px; padding: 0;
}
.composer .row { display: flex; gap: 8px; margin-top: 10px; }
.composer .hint { font-size: 13px; color: var(--muted); margin-top: 6px; }
.preview { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 12px; }
.preview .amount-line { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.preview .amount-line input {
  width: 100%; border: 0; outline: 0; background: transparent; font-size: 28px; font-weight: 700;
  font-variant-numeric: tabular-nums; padding: 0;
}
.preview .note { width: 100%; border: 0; border-bottom: 1px solid var(--line); outline: 0; background: transparent; padding: 6px 0; margin-bottom: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 0; background: var(--chip); border-radius: 999px; padding: 6px 12px; font-size: 14px;
  display: inline-flex; align-items: center; gap: 4px; position: relative;
}
.chip.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chip input[type=date] { position: absolute; inset: 0; opacity: 0; width: 100%; }

.btn {
  border: 0; border-radius: 12px; padding: 12px 16px; background: var(--chip); font-weight: 600;
}
.btn.primary { background: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #042f2e; } }
.btn.danger { color: var(--gasto); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.linkbtn { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 4px 0; }

/* Resumen */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin: 20px 0 8px; }
.month-nav button { border: 0; background: var(--card); border-radius: 10px; width: 40px; height: 36px; font-size: 18px; box-shadow: var(--shadow); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 20px; font-weight: 700; margin-top: 2px; }

.bar { height: 8px; background: var(--chip); border-radius: 999px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.bar.warn > i { background: var(--gasto); }

.catrow { display: grid; grid-template-columns: 28px 1fr auto; gap: 4px 10px; align-items: center; padding: 8px 0; }
.catrow + .catrow { border-top: 1px solid var(--line); }
.catrow .bar { grid-column: 2 / 4; margin-top: 0; }

/* Listas */
.list { background: var(--card); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; padding: 12px 16px; width: 100%; border: 0; background: none; text-align: left; }
.item + .item { border-top: 1px solid var(--line); }
.item .ico { width: 36px; height: 36px; border-radius: 10px; background: var(--chip); display: grid; place-items: center; font-size: 18px; flex: none; }
.item .main { flex: 1; min-width: 0; }
.item .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.day { font-size: 13px; color: var(--muted); margin: 16px 4px 6px; }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; }

.search { width: 100%; border: 0; border-radius: 12px; padding: 10px 14px; background: var(--card); box-shadow: var(--shadow); outline: 0; }

.banner { background: var(--accent-soft); color: var(--text); border-radius: 14px; padding: 12px 14px; margin-bottom: 10px; font-size: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }

/* Formularios */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; background: var(--bg); outline: 0;
  font-size: 16px;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.seg { display: flex; background: var(--chip); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 10px; padding: 8px 4px; font-size: 14px; }
.seg button.on { background: var(--card); font-weight: 600; box-shadow: var(--shadow); }
.actions { display: flex; gap: 8px; margin-top: 8px; }
.actions .btn { flex: 1; }

/* Tabs */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  background: color-mix(in srgb, var(--card) 88%, transparent); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 10;
}
#tabs button { flex: 1; max-width: 140px; border: 0; background: none; padding: 8px 0 6px; font-size: 11px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; }
#tabs button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
#tabs button.on { color: var(--accent); font-weight: 600; }
#tabs button.on span { filter: none; opacity: 1; }

/* Hoja inferior */
#backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; cursor: pointer; touch-action: none; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--card);
  border-radius: 20px 20px 0 0; max-height: min(88vh, calc(100vh - var(--alert-h, 0px) - env(safe-area-inset-top) - 16px));
  display: flex; flex-direction: column; overflow: hidden;
  padding-top: 8px; max-width: 560px; margin: 0 auto;
  animation: up .18s ease-out;
}
#sheet .sheet-body {
  overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}
#sheet .sheet-head { position: relative; flex: none; height: 44px; }
#sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 0; }
#sheet .sheet-x {
  position: absolute; top: 4px; right: 12px; width: 32px; height: 32px; border: 0; border-radius: 50%;
  background: var(--chip); color: var(--muted); font-size: 16px; line-height: 32px; text-align: center; padding: 0; z-index: 1;
}

#sheet.dragging { transition: none; }
#sheet h3 { margin: 0 0 14px; font-size: 18px; }
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.opt { display: flex; width: 100%; border: 0; background: none; padding: 12px 4px; gap: 10px; align-items: center; text-align: left; border-bottom: 1px solid var(--line); }
.opt.on { color: var(--accent); font-weight: 600; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 30;
  background: #111827; color: #fff; border-radius: 12px; padding: 10px 14px; font-size: 14px; display: flex; gap: 12px; align-items: center;
  width: max-content; max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
#toast button { border: 0; background: none; color: #5eead4; font-weight: 700; padding: 10px 12px; margin: -10px -12px -10px 0; }
#toast.top { top: calc(env(safe-area-inset-top) + 12px); bottom: auto; }
#alert {
  position: fixed; left: 12px; right: 12px; top: calc(env(safe-area-inset-top) + 8px); z-index: 40; max-width: 536px; margin: 0 auto;
  background: #7f1d1d; color: #fff; border-radius: 12px; padding: 10px 12px; font-size: 14px;
  display: flex; gap: 10px; align-items: center; justify-content: space-between; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
#alert button { border: 0; background: #fff; color: #7f1d1d; border-radius: 8px; padding: 6px 10px; font-weight: 700; flex: none; }
#alert button[data-alert="close"] { background: none; color: #fff; padding: 0; min-width: 44px; min-height: 44px; margin: -8px -8px -8px 6px; font-size: 18px; }

/* Gráficos */
#tabs button { max-width: 120px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; outline: none; -webkit-user-select: none; user-select: none; }
.chart svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart text.lbl { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .g { stroke: var(--grid); stroke-width: 1; }
.chart .g.base { stroke: var(--axis); }
.chart .ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { stroke: var(--card); stroke-width: 2; }
.chart .xh { stroke: var(--axis); stroke-width: 1; }
.chart .grp { transition: opacity .12s; }
.chart .grp.dim { opacity: .45; }
.s-s1 { stroke: var(--s1); } .s-s2 { stroke: var(--s2); } .s-s0 { stroke: var(--s0); }
.f-s1 { fill: var(--s1); } .f-s2 { fill: var(--s2); } .f-s0 { fill: var(--s0); }
.k-s1 { background: var(--s1); } .k-s2 { background: var(--s2); } .k-s0 { background: var(--s0); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin: 0 0 6px; }
.legend .key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.legend .key.line { width: 14px; height: 2px; border-radius: 1px; vertical-align: 3px; }
.chart .tip {
  position: absolute; top: 28px; z-index: 2; pointer-events: none; background: var(--card); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.12);
}
.chart .tip .t { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.chart .tip .r { display: flex; align-items: center; gap: 8px; white-space: nowrap; line-height: 1.5; }
.chart .tip .r i { display: inline-block; width: 12px; height: 2px; border-radius: 1px; flex: none; }
.chart .tip .r b { font-variant-numeric: tabular-nums; }
.chart .tip { max-width: 100%; }
.chart .tip .r span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
details.tbl summary { font-size: 13px; color: var(--accent); cursor: pointer; margin-top: 8px; }
details.tbl table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 6px; }
details.tbl th { text-align: right; color: var(--muted); font-weight: 600; padding: 4px 0; border-bottom: 1px solid var(--line); }
details.tbl td { text-align: right; padding: 4px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
details.tbl th:first-child, details.tbl td:first-child { text-align: left; }

/* Indicador de sincronización */
.sync-chip { float: right; border: 0; background: var(--chip); border-radius: 999px; width: 40px; height: 40px; font-size: 18px; margin-top: -2px; }
.sync-chip.error { background: color-mix(in srgb, var(--gasto) 22%, transparent); }
