/* ============================================================
   Presupuesto — "Libro de cuentas calmado"
   Identidad: montos en monoespaciada tabular (libro mayor), fondo neutro cálido,
   sobres por grupo con color con significado. Acento verde pino = dinero/calma.
   ============================================================ */
:root {
  --ground: #f3f4f1;     /* neutro cálido claro */
  --surface: #ffffff;
  --surface-2: #fafbf9;
  --text: #16201b;       /* casi negro, leve verde */
  --muted: #69756d;
  --faint: #9aa49d;
  --line: #e6eae5;
  --accent: #0f6e4e;     /* verde pino */
  --accent-ink: #0a4d36;
  --accent-soft: #e4efe9;
  --neg: #bc4a3c;        /* rojo arcilla: pasado de presupuesto */
  --neg-soft: #f6e6e2;
  /* colores de sobre (significado: esencial / deseo / futuro) */
  --basal: #2c8c6a;
  --discrecional: #c0892e;
  --inversion: #3f6cb0;
  --shadow: 0 1px 2px rgba(22,32,27,.04), 0 6px 24px rgba(22,32,27,.06);
  --radius: 18px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  background: var(--ground); color: var(--text);
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
  line-height: 1.45;
}
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.eyebrow { font-size: 11px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--faint); }
.pos { color: var(--accent); } .neg { color: var(--neg); }
button { font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

/* ---- Topbar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 18px; padding-top: max(14px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--ground) 86%, transparent);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.appname { font-family: var(--mono); font-weight: 600; font-size: 15px; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.appname .mark { display: inline-grid; place-items: center; width: 24px; height: 24px; background: var(--accent); color: #fff; border-radius: 7px; font-size: 13px; }
.monthnav { display: flex; align-items: center; gap: 4px; }
.monthnav > span { min-width: 116px; text-align: center; font-weight: 600; font-size: 14px; text-transform: capitalize; }
.chev { background: transparent; border: 0; color: var(--muted); font-size: 19px; line-height: 1; padding: 6px 10px; border-radius: 999px; cursor: pointer; }
.chev:hover { background: var(--accent-soft); color: var(--accent-ink); }

/* ---- Layout ---- */
main { padding: 18px 16px 8px; max-width: 720px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; animation: rise .22s ease; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.section-title { font-size: 17px; font-weight: 700; margin: 4px 2px 12px; letter-spacing: -.01em; }
.hint { color: var(--muted); font-size: 13px; margin: 2px 4px 14px; }

/* ---- Hero (Resumen) ---- */
.hero { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px 22px 20px; margin-bottom: 16px; }
.hero .eyebrow { margin-bottom: 8px; }
.hero-amount { font-family: var(--mono); font-size: clamp(34px, 9vw, 46px); font-weight: 600; letter-spacing: -.03em; line-height: 1; }
.hero-meta { color: var(--muted); font-size: 13px; margin-top: 10px; }
.hero-meta b { color: var(--text); font-family: var(--mono); }

/* ---- Track / barras de progreso ---- */
.track { height: 8px; border-radius: 999px; background: var(--line); overflow: hidden; margin-top: 14px; }
.track > i { display: block; height: 100%; border-radius: 999px; background: var(--accent); width: 0; transition: width .55s cubic-bezier(.2,.7,.2,1); }
.track > i.over { background: var(--neg); }
.track.sm { height: 6px; margin-top: 10px; }
@media (prefers-reduced-motion: reduce) { .track > i { transition: none; } .view.active { animation: none; } }

/* ---- Sobres (grupos) ---- */
.env-grid { display: grid; gap: 12px; margin-bottom: 18px; }
.env { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 16px 18px; }
.env-top { display: flex; align-items: center; gap: 10px; }
.dot { width: 9px; height: 9px; border-radius: 999px; background: var(--gc, var(--accent)); flex: 0 0 auto; }
.env-name { font-weight: 700; font-size: 14px; }
.env-disp { margin-left: auto; font-family: var(--mono); font-weight: 600; font-size: 15px; }
.env-meta { color: var(--muted); font-size: 12.5px; margin-top: 8px; font-family: var(--mono); }
.g-basal { --gc: var(--basal); } .g-discrecional { --gc: var(--discrecional); } .g-inversion { --gc: var(--inversion); }
.env .track > i { background: var(--gc, var(--accent)); }
.env .track > i.over { background: var(--neg); }

/* ---- Feed de movimientos (estilo libro mayor) ---- */
.feed { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.feed-head { padding: 14px 18px 6px; font-weight: 700; font-size: 14px; }
.day-sep { padding: 10px 18px 4px; font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--faint); background: var(--surface-2); }
.led { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-top: 1px solid var(--line); }
.led:first-of-type { border-top: 0; }
.led-tag { width: 8px; height: 8px; border-radius: 999px; background: var(--gc, var(--faint)); flex: 0 0 auto; }
.led-main { min-width: 0; flex: 1; }
.led-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.led-sub { color: var(--muted); font-size: 12px; margin-top: 1px; }
.led-amt { font-family: var(--mono); font-weight: 600; font-size: 14px; }
.led-del { background: transparent; border: 0; color: var(--faint); cursor: pointer; font-size: 15px; padding: 4px 6px; border-radius: 8px; }
.led-del:hover { color: var(--neg); background: var(--neg-soft); }

/* ---- Stats (Presupuesto) ---- */
.stats { display: grid; grid-template-columns: 1fr; gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 14px; }
.stat .eyebrow { margin-bottom: 6px; }
.stat .num { font-family: var(--mono); font-size: 16px; font-weight: 600; letter-spacing: -.02em; }
.stat .num small { font-size: 11px; color: var(--faint); }
@media (max-width: 540px) { .stats { grid-template-columns: 1fr; } .stat { display: flex; align-items: baseline; justify-content: space-between; padding: 11px 14px; } .stat .eyebrow { margin: 0; } }

/* ---- Editor de presupuesto (lista limpia, no grilla) ---- */
.envedit { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.envedit-head { display: flex; align-items: center; gap: 10px; padding: 13px 18px; border-bottom: 1px solid var(--line); }
.envedit-head .env-name { font-size: 14px; }
.envedit-head .sub { margin-left: auto; font-family: var(--mono); color: var(--muted); font-size: 13px; }
.line { display: flex; align-items: center; gap: 12px; padding: 8px 14px 8px 18px; border-top: 1px solid var(--line); }
.line:first-of-type { border-top: 0; }
.line .lname { flex: 1; font-size: 14px; }
.amount-input { font-family: var(--mono); font-variant-numeric: tabular-nums; text-align: right; width: 130px; border: 1px solid transparent; background: var(--surface-2); border-radius: 10px; padding: 8px 10px; font-size: 14px; color: var(--text); }
.amount-input::placeholder { color: var(--faint); }
.amount-input:hover { border-color: var(--line); }
.amount-input:focus { border-color: var(--accent); background: #fff; outline: none; }

/* ---- Gráfico de barras diario (Realidad) ---- */
.chartcard { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 16px 16px 12px; margin-bottom: 16px; }
.chart-top { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 14px; }
.chart-top .total { font-family: var(--mono); font-weight: 600; font-size: 18px; }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 150px; }
.bar { flex: 1 1 0; display: flex; flex-direction: column-reverse; min-width: 0; height: 100%; justify-content: flex-start; border-radius: 3px 3px 0 0; overflow: hidden; background: transparent; }
.bar .seg { width: 100%; }
.bar .seg.basal { background: var(--basal); }
.bar .seg.discrecional { background: var(--discrecional); }
.bar .seg.inversion { background: var(--inversion); }
.bar.empty { background: var(--line); height: 2px; align-self: flex-end; border-radius: 2px; }
.axis { display: flex; justify-content: space-between; margin-top: 8px; font-size: 10.5px; color: var(--faint); font-family: var(--mono); }
.legend { display: flex; gap: 16px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }

/* ---- Categorías / Sobres (gestión) ---- */
.cat-card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.cat-head { display: flex; align-items: center; gap: 10px; padding: 13px 18px; border-bottom: 1px solid var(--line); }
.cat-head .count { margin-left: auto; font-family: var(--mono); font-size: 13px; color: var(--muted); }
.cat-row { display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 18px; border-top: 1px solid var(--line); }
.cat-row:first-of-type { border-top: 0; }
.cname { flex: 1; font: inherit; font-size: 14px; border: 1px solid transparent; background: transparent; border-radius: 8px; padding: 7px 8px; color: var(--text); }
.cname:hover { background: var(--surface-2); }
.cname:focus { border-color: var(--accent); background: #fff; outline: none; }
.add-line { padding: 10px 14px; border-top: 1px solid var(--line); }

/* ---- Botones ---- */
.btn { border: 0; border-radius: 12px; padding: 11px 16px; font: inherit; font-weight: 700; font-size: 14px; cursor: pointer; background: var(--accent); color: #fff; }
.btn:hover { background: var(--accent-ink); }
.btn:active { transform: translateY(1px); }
.btn.block { width: 100%; }
.btn.ghost { background: var(--accent-soft); color: var(--accent-ink); }
.btn.ghost:hover { background: color-mix(in srgb, var(--accent-soft) 80%, var(--accent) 20%); }
.btn.sm { padding: 7px 12px; font-size: 13px; }
.btn.subtle { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--neg); padding: 7px 10px; }
.btn.danger:hover { background: var(--neg-soft); }

/* ---- FAB ---- */
.fab {
  position: fixed; right: 18px; bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 30;
  width: 56px; height: 56px; border: 0; border-radius: 18px; background: var(--accent); color: #fff;
  display: grid; place-items: center; cursor: pointer; box-shadow: 0 8px 22px rgba(15,110,78,.38);
}
.fab svg { width: 26px; height: 26px; fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; }
.fab:hover { background: var(--accent-ink); }

/* ---- Bottom nav ---- */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  display: flex; justify-content: space-around; align-items: stretch;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.navbtn {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: transparent; border: 0; cursor: pointer; padding: 9px 4px 8px; color: var(--faint); font-size: 11px; font-weight: 600;
}
.navbtn svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.navbtn.active { color: var(--accent); }

/* ---- Bottom sheet / modal ---- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(22,32,27,.42); display: flex; align-items: flex-end; justify-content: center; z-index: 40; }
.sheet { background: var(--surface); width: 100%; max-width: 480px; border-radius: 22px 22px 0 0; box-shadow: var(--shadow); max-height: 92vh; overflow: auto; animation: sheetUp .24s cubic-bezier(.2,.7,.2,1); }
@keyframes sheetUp { from { transform: translateY(16px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 540px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 22px; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 12px; position: sticky; top: 0; background: var(--surface); }
.sheet-head h2 { margin: 0; font-size: 17px; }
.sheet-body { padding: 4px 18px 22px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; color: var(--muted); font-weight: 600; margin-bottom: 6px; }
.field input, .field select { width: 100%; font: inherit; padding: 12px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: var(--text); }
.field input.mono { font-family: var(--mono); }
.field input:focus, .field select:focus { border-color: var(--accent); background: #fff; outline: none; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.move-opts { display: grid; gap: 10px; }

/* ---- Estados vacíos / toast ---- */
.empty { text-align: center; color: var(--muted); padding: 30px 18px; font-size: 14px; }
.empty b { color: var(--text); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(96px + env(safe-area-inset-bottom)); background: var(--text); color: #fff; padding: 11px 18px; border-radius: 999px; font-size: 13.5px; font-weight: 600; z-index: 50; box-shadow: var(--shadow); }

/* ---- Login ---- */
.auth-screen { position: fixed; inset: 0; z-index: 60; background: var(--ground); display: grid; place-items: center; padding: 24px; }
.auth-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 24px; }
.auth-brand { font-family: var(--mono); font-weight: 600; display: flex; align-items: center; gap: 8px; margin-bottom: 20px; }
.auth-brand .mark { width: 24px; height: 24px; background: var(--accent); color: #fff; border-radius: 7px; display: grid; place-items: center; font-size: 13px; }
.auth-card h2 { margin: 0 0 6px; font-size: 21px; letter-spacing: -.01em; }
.auth-card #authSub { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
.auth-err { color: var(--neg); font-size: 13px; margin: 10px 0 0; }

/* ---- Banner Conectar Telegram ---- */
.tg-banner { display: flex; align-items: center; justify-content: space-between; gap: 10px; text-decoration: none; background: var(--accent-soft); color: var(--accent-ink); border: 1px solid var(--line); border-radius: 14px; padding: 13px 16px; margin-bottom: 14px; font-weight: 600; font-size: 14px; }
.tg-banner span { font-size: 18px; }
.tg-banner:hover { background: color-mix(in srgb, var(--accent-soft) 80%, var(--accent) 20%); }

/* ---- Presupuesto: membresía por período (incluir/quitar categoría del mes/año) ---- */
.muted { color: var(--muted); }
.line-rm { background: transparent; border: 0; color: var(--faint); cursor: pointer; font-size: 14px; padding: 6px 8px; border-radius: 8px; flex: 0 0 auto; }
.line-rm:hover { color: var(--neg); background: var(--neg-soft); }
.line.absent { background: var(--surface-2); }
.line.absent .lname { font-size: 13.5px; }
.line.absent .btn.subtle { padding: 6px 12px; }
.copy-banner { display: block; width: 100%; text-align: left; background: var(--accent-soft); color: var(--accent-ink); border: 1px solid var(--line); border-radius: 14px; padding: 13px 16px; margin-bottom: 14px; font-weight: 600; font-size: 13.5px; cursor: pointer; }
.copy-banner:hover { background: color-mix(in srgb, var(--accent-soft) 80%, var(--accent) 20%); }
.copy-banner b { font-weight: 800; }
/* etiqueta de período clickeable (abre el selector año+mes) */
.period-btn { background: transparent; border: 0; font: inherit; font-weight: 600; font-size: 14px; text-transform: capitalize; color: var(--text); cursor: pointer; min-width: 116px; padding: 6px 10px; border-radius: 999px; }
.period-btn:hover { background: var(--accent-soft); color: var(--accent-ink); }
/* ✎ editar sobre en el encabezado, y categoría como botón (toca para gestionar) */
.icon-btn { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--accent-soft); color: var(--accent-ink); }
.envedit-head .icon-btn { margin-left: 4px; }
.lname-btn { text-align: left; background: transparent; border: 0; font: inherit; font-size: 14px; color: var(--text); cursor: pointer; padding: 2px 0; }
.lname-btn:hover { color: var(--accent-ink); text-decoration: underline; }
.lname-btn.muted { color: var(--muted); }
.line.absent .lname-btn { font-size: 13.5px; }
/* selector rápido de período */
.ppick-year { display: flex; align-items: center; justify-content: center; gap: 16px; margin: 4px 0 14px; }
.ppick-year span { font-family: var(--mono); font-size: 20px; font-weight: 600; min-width: 64px; text-align: center; }
.ppick-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.ppick-m { padding: 12px 0; border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px; font: inherit; font-size: 13px; text-transform: capitalize; cursor: pointer; color: var(--text); }
.ppick-m:hover { border-color: var(--accent); }
.ppick-m.on { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 700; }
/* enlace discreto de conexiones en Resumen */
.conn-link { display: block; width: 100%; margin: 18px 0 4px; background: transparent; border: 0; color: var(--faint); font-size: 12.5px; font-weight: 600; cursor: pointer; padding: 9px; border-radius: 10px; }
.conn-link:hover { color: var(--muted); background: var(--surface-2); }

/* ---- Tarjeta de conexiones / onboarding ---- */
.onb-card { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 16px; box-shadow: var(--shadow); padding: 16px 18px; margin: 4px 0 14px; }
.onb-head { font-weight: 700; font-size: 14px; }
.onb-sub { color: var(--muted); font-size: 12.5px; margin: 4px 0 12px; }
.onb-row { display: flex; align-items: flex-start; gap: 11px; padding: 10px 0; border-top: 1px solid var(--line); }
.onb-ico { font-size: 18px; line-height: 1.3; flex: 0 0 auto; }
.onb-txt { flex: 1; min-width: 0; font-size: 13px; }
.onb-txt b { display: block; font-size: 13.5px; }
.onb-txt span { color: var(--muted); font-size: 12.5px; }
.onb-row .btn { flex: 0 0 auto; align-self: center; }
.onb-btns { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.onb-btns .btn { text-decoration: none; }

/* ---- Sobres dinámicos: color por grupo (--gc inline) + badge de periodicidad ---- */
.env, .envedit, .cat-card { border-left: 4px solid var(--gc, var(--line)); }
.badge { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--ground); border: 1px solid var(--line); border-radius: 999px; padding: 2px 7px; white-space: nowrap; }
.env-top .badge, .envedit-head .badge, .cat-head .badge { margin-left: 4px; }
.cat-head [data-editgroup] { margin-left: auto; }

/* ---- Cuenta e invitaciones (modal Conexiones) ---- */
.acct-sec { display: flex; flex-direction: column; gap: 6px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 14px; }
.acct-sec > span { color: var(--muted); font-size: 12.5px; }
.inv-new { display: flex; gap: 8px; margin-top: 10px; }
.inv-new input { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); color: var(--text); }
.inv-row { display: flex; align-items: center; gap: 10px; padding: 8px 2px; border-top: 1px solid var(--line); font-size: 13px; }
.inv-row:first-of-type { border-top: 0; margin-top: 6px; }
.inv-st { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: 2px 8px; border-radius: 999px; background: var(--ground); border: 1px solid var(--line); color: var(--muted); }
.inv-st.usada { background: var(--accent-soft); color: var(--accent-ink); border-color: transparent; }
.inv-st.vencida { background: var(--neg-soft); color: var(--neg); border-color: transparent; }
.inv-note { flex: 1; color: var(--faint); font-family: var(--mono); font-size: 12px; }
