:root {
  --bg: #f4f3ef;
  --card: #ffffff;
  --ink: #1d2422;
  --muted: #6b7470;
  --line: #e3e1da;
  --accent: #14735f;
  --accent-soft: #dcefe9;
  --gasto: #c2412d;
  --ingreso: #1e8a4c;
  --ahorro: #2a6fb0;
  --warn: #c98a12;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121615;
    --card: #1b2120;
    --ink: #e8ecea;
    --muted: #94a09b;
    --line: #2b3331;
    --accent: #3cb393;
    --accent-soft: #19352e;
    --gasto: #f07a63;
    --ingreso: #5bcf8a;
    --ahorro: #6aa8e6;
    --warn: #e4b146;
    --shadow: none;
    color-scheme: dark;
  }
}

[hidden] { display: none !important; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h2 { font-size: 1.05rem; margin: 22px 0 10px; }
h3 { font-size: .8rem; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 1.2rem; margin: 0; letter-spacing: -.01em; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.icon-btn { background: none; border: 0; font-size: 1.25rem; padding: 6px; border-radius: 10px; }
.sync { font-size: .78rem; color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); padding: 3px 8px; border-radius: 99px; }

.view { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; }
.loading { text-align: center; color: var(--muted); padding: 60px 0; }

/* ---------- bottom tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs button {
  background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: .72rem; color: var(--muted);
}
.tabs button span { font-size: 1.25rem; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); font-weight: 600; }
.tabs button.active span { filter: none; opacity: 1; }

/* ---------- generic ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); }
.pad { padding: 14px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.right { text-align: right; }
.hidden { display: none !important; }

.input, select.input, textarea.input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card);
  outline: none; min-height: 48px;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { min-height: 70px; resize: vertical; }
label.field { display: block; margin: 12px 0; }
label.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 5px; }

.btn {
  border: 1px solid var(--line); background: var(--card); padding: 11px 16px; border-radius: 12px;
  min-height: 46px; font-weight: 500;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { color: var(--gasto); border-color: color-mix(in srgb, var(--gasto) 40%, var(--line)); }
.btn.block { width: 100%; }
.btn.big { min-height: 56px; font-size: 1.08rem; }
.btn:disabled { opacity: .5; }
.btn.small-btn { min-height: 36px; padding: 6px 12px; font-size: .88rem; }

/* ---------- segmented ---------- */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--line); padding: 3px; border-radius: 12px; gap: 3px; }
.seg button { border: 0; background: transparent; padding: 9px 0; border-radius: 9px; font-weight: 500; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.seg button.on[data-tipo="gasto"] { color: var(--gasto); }
.seg button.on[data-tipo="ingreso"] { color: var(--ingreso); }
.seg button.on[data-tipo="ahorro"] { color: var(--ahorro); }

/* ---------- amount ---------- */
.amount {
  display: flex; align-items: baseline; justify-content: center; gap: 6px;
  margin: 18px 0 6px; padding: 8px 0 10px; border-bottom: 2px solid var(--line);
}
.amount:focus-within { border-color: var(--accent); }
.amount input {
  border: 0; background: transparent; outline: none; text-align: right;
  font-size: 2.6rem; font-weight: 700; width: 100%; max-width: 260px; letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
}
.amount span { font-size: 1.8rem; color: var(--muted); font-weight: 600; }
.amount-hint { text-align: center; font-size: .8rem; color: var(--muted); min-height: 1.2em; }

/* ---------- chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 99px;
  padding: 8px 13px; font-size: .92rem; display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; max-width: 100%;
}
.chip .ic { font-size: 1.05rem; }
.chip.on { border-color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 16%, var(--card)); font-weight: 600; }
.chip.quick { border-style: dashed; font-size: .85rem; min-height: 36px; padding: 6px 11px; }
.chips.cats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.chips.cats .chip { border-radius: 12px; justify-content: flex-start; }
.chip .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.save-bar { margin-top: 20px; display: grid; gap: 10px; }

/* ---------- month nav ---------- */
.monthnav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.monthnav button { border: 1px solid var(--line); background: var(--card); width: 42px; height: 42px; border-radius: 12px; font-size: 1.2rem; }
.monthnav .m { font-weight: 700; font-size: 1.08rem; text-transform: capitalize; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpi { padding: 12px 14px; }
.kpi .k { font-size: .78rem; color: var(--muted); }
.kpi .v { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.c-gasto { color: var(--gasto); } .c-ingreso { color: var(--ingreso); } .c-ahorro { color: var(--ahorro); }

/* ---------- list ---------- */
.totals { display: flex; justify-content: space-between; gap: 8px; font-size: .85rem; margin-bottom: 10px; flex-wrap: wrap; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.filters .input { min-height: 42px; padding: 8px 12px; font-size: .92rem; }
.day { margin-top: 14px; }
.day-h { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); padding: 0 4px 6px; text-transform: capitalize; }
.mov { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-top: 1px solid var(--line); background: none; border-left: 0; border-right: 0; border-bottom: 0; width: 100%; text-align: left; }
.mov:first-child { border-top: 0; }
.mov .ic { width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; font-size: 1.15rem; flex: none;
  background: color-mix(in srgb, var(--c) 18%, transparent); }
.mov .t { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mov .s { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mov .a { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

/* ---------- budget bars ---------- */
.bud { padding: 12px 14px; border-top: 1px solid var(--line); }
.bud:first-child { border-top: 0; }
.bud-h { display: flex; align-items: center; gap: 8px; width: 100%; background: none; border: 0; padding: 0; text-align: left; }
.bud-h .n { flex: 1; font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bud-h .v { font-variant-numeric: tabular-nums; font-size: .9rem; white-space: nowrap; }
.bar { height: 8px; border-radius: 99px; background: var(--line); margin-top: 8px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar.alerta > i { background: var(--warn); }
.bar.excedido > i { background: var(--gasto); }
.bud-sub { margin: 10px 0 0 30px; display: grid; gap: 9px; }
.bud-sub .bar { height: 5px; margin-top: 4px; }
.tag { font-size: .7rem; padding: 2px 7px; border-radius: 99px; font-weight: 600; white-space: nowrap; }
.tag.ok { background: var(--accent-soft); color: var(--accent); }
.tag.alerta { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.tag.excedido { background: color-mix(in srgb, var(--gasto) 16%, transparent); color: var(--gasto); }

.chart { width: 100%; height: auto; display: block; }
.legend { display: flex; gap: 14px; font-size: .78rem; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
table.ytd { width: 100%; border-collapse: collapse; font-size: .88rem; font-variant-numeric: tabular-nums; }
table.ytd th, table.ytd td { padding: 9px 12px; border-top: 1px solid var(--line); }
table.ytd th { font-size: .75rem; color: var(--muted); font-weight: 600; text-align: right; border-top: 0; }
table.ytd th:first-child, table.ytd td:first-child { text-align: left; }
table.ytd td { text-align: right; }
table.ytd tr.total td { font-weight: 700; }

/* ---------- categories admin ---------- */
.catcard { margin-bottom: 12px; overflow: hidden; }
.catcard-h { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-left: 4px solid var(--c); }
.catcard-h .n { flex: 1; font-weight: 600; min-width: 0; }
.subrow { display: flex; align-items: center; gap: 10px; padding: 10px 14px 10px 18px; border-top: 1px solid var(--line); width: 100%; background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left; }
.subrow .n { flex: 1; min-width: 0; }
.subrow .p { font-variant-numeric: tabular-nums; font-size: .9rem; white-space: nowrap; }
.off { opacity: .45; }
.addsub { width: 100%; border: 0; border-top: 1px dashed var(--line); background: none; padding: 10px; color: var(--accent); font-weight: 500; }

/* ---------- dialog ---------- */
dialog#modal {
  border: 0; padding: 0; border-radius: 18px 18px 0 0; width: 100%; max-width: 640px;
  margin: auto auto 0; max-height: 92dvh; background: var(--card); color: var(--ink);
}
dialog#modal::backdrop { background: rgba(0,0,0,.4); }
@media (min-width: 700px) { dialog#modal { border-radius: 18px; margin: auto; } }
.modal-body { padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); }
.modal-body h2 { margin-top: 0; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; }
.emoji-pick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.emoji-pick button { font-size: 1.3rem; width: 42px; height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); }
.check { display: flex; align-items: center; gap: 10px; margin: 14px 0; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabs-h) + 14px + env(safe-area-inset-bottom)); z-index: 50;
  background: #222; color: #fff; padding: 11px 16px; border-radius: 12px; font-size: .92rem;
  display: flex; gap: 14px; align-items: center; width: max-content; max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.toast button { background: none; border: 0; color: #7fe0c4; font-weight: 700; padding: 0; }

/* ---------- login ---------- */
.login { max-width: 380px; margin: 8vh auto 0; }
.login .logo { font-size: 3rem; text-align: center; }
.login h2 { text-align: center; margin-top: 6px; }
.err { color: var(--gasto); font-size: .9rem; min-height: 1.3em; margin-top: 8px; }
