:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6b6a66;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series: #2a78d6;
  --track: #cde2fb;
  --ok: #0ca30c;
  --warn: #fab219;
  --crit: #d03b3b;
  --good-text: #006300;
  --bad-text: #d03b3b;
  --on-accent: #ffffff;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #a3a29b;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series: #3987e5;
    --track: #0d366b;
    --good-text: #0ca30c;
    --bad-text: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #a3a29b;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series: #3987e5;
  --track: #0d366b;
  --good-text: #0ca30c;
  --bad-text: #e66767;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
h1, h2, p, ul, ol { margin: 0; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 650; margin-bottom: 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--series); outline-offset: 2px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
  max-width: 640px; margin: 0 auto;
}
.wrap {
  max-width: 640px; margin: 0 auto;
  padding: 4px 16px calc(150px + env(safe-area-inset-bottom));
  display: grid; gap: 12px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
input, select {
  background: var(--page);
  border: 1px solid var(--baseline);
  border-radius: 10px;
  padding: 11px 12px;
  min-height: 44px;
  min-width: 0;
}
button.primary, button.secondary {
  min-height: 44px; padding: 0 18px; border-radius: 10px; font-weight: 600; border: 1px solid transparent;
}
button.primary { background: var(--series); color: var(--on-accent); }
button.secondary { background: transparent; border-color: var(--baseline); }
button.link { background: none; border: 0; color: var(--ink-2); padding: 8px; min-height: 44px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-size: 24px; line-height: 1;
}
.icon-btn:disabled { opacity: 0.35; cursor: default; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: 12px; text-align: center; }
.login-icon { margin: 0 auto; border-radius: 16px; }
.error { color: var(--bad-text); min-height: 1.4em; font-size: 14px; }

/* Semana */
.weeknav { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.weeknav-label { display: grid; text-align: center; }
.weeknav-label span { font-size: 13px; }

/* Cifra principal: una sola por vista */
.label { font-size: 14px; color: var(--ink-2); }
.hero-figure { font-size: clamp(48px, 15vw, 64px); font-weight: 700; line-height: 1.05; letter-spacing: -0.02em; margin: 4px 0; }
.delta { font-size: 15px; font-weight: 600; min-height: 1.4em; }
.delta.up { color: var(--bad-text); }
.delta.down { color: var(--good-text); }
.delta .vs { color: var(--muted); font-weight: 400; }

/* Medidores de presupuesto */
.meter-row + .meter-row { margin-top: 14px; }
.meter-head { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; margin-bottom: 6px; }
.meter-status { font-weight: 600; white-space: nowrap; }
.meter { height: 8px; border-radius: 999px; background: var(--track); overflow: hidden; }
.meter > span { display: block; height: 100%; width: var(--w, 0%); border-radius: 999px; background: var(--series); }
.meter.warn > span { background: var(--warn); }
.meter.crit > span { background: var(--crit); }

/* Columnas por día */
.days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; align-items: end; height: 150px; border-bottom: 1px solid var(--baseline); }
.day { display: block; height: 100%; background: none; border: 0; padding: 0; color: inherit; min-width: 0; cursor: pointer; }
.day-track { display: flex; flex-direction: column; justify-content: flex-end; align-items: center; width: 100%; height: 100%; padding-top: 18px; position: relative; }
.day-bar { width: min(24px, 70%); height: var(--h, 0%); min-height: 0; background: var(--series); border-radius: 4px 4px 0 0; }
.day-value { position: absolute; top: 0; font-size: 12px; color: var(--ink-2); white-space: nowrap; }
.day.sel .day-bar { outline: 2px solid var(--ink); outline-offset: 1px; }
.day.future .day-bar { opacity: 0.4; }
.day-names { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; margin-top: 6px; text-align: center; font-size: 12px; color: var(--ink-2); text-transform: capitalize; }
.readout { margin-top: 10px; font-size: 14px; min-height: 1.4em; }

/* Barras por categoría */
.cat + .cat { margin-top: 12px; }
.cat-head { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; margin-bottom: 5px; }
.cat-value { color: var(--ink-2); white-space: nowrap; }
.cat-bar { height: 10px; border-radius: 0 4px 4px 0; background: var(--series); width: var(--w, 0%); min-width: 2px; }

/* Recomendaciones */
.tips { padding-left: 20px; display: grid; gap: 10px; }
.tips li { padding-left: 4px; }

/* Lista de gastos (también sirve como vista de tabla) */
.expenses { list-style: none; padding: 0; }
.expense { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 10px 0; border-top: 1px solid var(--grid); }
.expense:first-child { border-top: 0; padding-top: 0; }
.expense-desc { font-weight: 550; overflow-wrap: anywhere; }
.expense-amount { font-weight: 650; text-align: right; }
.expense-meta { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.expense-meta select { min-height: 32px; padding: 2px 8px; font-size: 13px; border-radius: 8px; }
.expense-del { margin-left: auto; background: none; border: 0; color: var(--muted); min-height: 32px; padding: 0 8px; border-radius: 8px; font-size: 13px; }
.empty { color: var(--muted); padding: 4px 0; }

/* Formulario de presupuesto */
.budget-form { display: grid; grid-template-columns: 1fr 110px auto; gap: 8px; margin-bottom: 8px; }

/* Barra para escribir, fija abajo */
.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; gap: 8px; align-items: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer input { flex: 1; font-size: 18px; min-height: 48px; }
.composer button { min-height: 48px; }
@media (min-width: 680px) {
  .composer { left: 50%; right: auto; width: 640px; transform: translateX(-50%); border: 1px solid var(--border); border-bottom: 0; border-radius: 14px 14px 0 0; }
}

.toast {
  position: fixed; left: 16px; right: 16px; z-index: 11;
  bottom: calc(86px + env(safe-area-inset-bottom));
  max-width: 608px; margin: 0 auto;
  background: var(--ink); color: var(--page);
  border-radius: 12px; padding: 12px 14px; font-size: 15px;
  display: grid; gap: 8px;
}
.toast .row { display: flex; align-items: center; gap: 10px; justify-content: space-between; }
.toast button { background: none; border: 0; color: inherit; text-decoration: underline; min-height: 32px; padding: 0 4px; }
.toast .chips { display: flex; flex-wrap: wrap; gap: 6px; }
.toast .chip { border: 1px solid currentColor; border-radius: 999px; text-decoration: none; padding: 4px 10px; min-height: 32px; }
.toast .alert { font-weight: 600; }
.toast.error-toast { background: var(--crit); color: #fff; }

@media (prefers-reduced-motion: no-preference) {
  .meter > span, .day-bar, .cat-bar { transition: width 0.3s ease, height 0.3s ease; }
}
