/* ---- Tema ---- */

:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #f0f3f2;
  --text: #14211f;
  --muted: #64736f;
  --line: #e2e8e6;
  --primary: #0f766e;
  --primary-strong: #0b5c56;
  --primary-soft: #dcf2ee;
  --primary-text: #ffffff;
  --ok: #0f9f76;
  --warn: #d97706;
  --over: #dc2626;
  --track: #e6ecea;
  --hero: linear-gradient(140deg, #0b3f3c 0%, #0f766e 62%, #18a394 100%);
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 36, 0.05), 0 8px 24px -12px rgba(16, 40, 36, 0.14);
  --shadow-pop: 0 18px 48px -12px rgba(8, 30, 27, 0.35);
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0c1413;
  --surface: #131d1c;
  --surface-2: #1a2725;
  --text: #e7efed;
  --muted: #8fa29e;
  --line: #233230;
  --primary: #2dd4bf;
  --primary-strong: #5eead4;
  --primary-soft: #12332f;
  --primary-text: #05211e;
  --ok: #34d399;
  --warn: #fbbf24;
  --over: #f87171;
  --track: #223130;
  --hero: linear-gradient(140deg, #0a2f2c 0%, #0d5a54 62%, #127a70 100%);
  --shadow: 0 0 0 1px var(--line);
  --shadow-pop: 0 18px 48px -12px rgba(0, 0, 0, 0.7);
  color-scheme: dark;
}

/* ---- Base ---- */

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 500 16px/1.45 'Manrope', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.2s, color 0.2s;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.6rem; font-weight: 800; letter-spacing: -0.02em; }
h2 { font-size: 1.02rem; font-weight: 700; letter-spacing: -0.01em; }
strong { font-weight: 700; }
small { display: block; color: var(--muted); font-size: 0.8rem; }
.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 14px 0; text-align: center; font-size: 0.92rem; }
.error { color: var(--over); min-height: 1.2em; font-size: 0.9rem; }
.link { color: inherit; font-weight: 600; text-underline-offset: 3px; }

.icon {
  width: 22px; height: 22px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

/* ---- Estructura ---- */

.center-screen {
  min-height: 100dvh; display: grid; place-items: center; padding: 16px;
  background:
    radial-gradient(60rem 30rem at 85% -10%, color-mix(in srgb, var(--primary) 16%, transparent), transparent 60%),
    var(--bg);
}
.login-card { width: 100%; max-width: 380px; padding: 28px; gap: 18px; box-shadow: var(--shadow-pop); }
.login-head { display: flex; align-items: center; justify-content: space-between; }

.topbar {
  /* Por encima del botón "+" para que el panel de notificaciones no quede tapado. */
  position: sticky; top: 0; z-index: 7;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 720px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px;
}
.brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: -0.02em; margin-right: auto; }
/* Isotipo de ResWebApp: se recorta la "R" del logo completo (1536x1024; la R ocupa 902x616 desde 282,54).
   Va sobre una placa blanca porque el logo tiene partes azul oscuro que se pierden en el tema oscuro. */
.logo {
  display: grid; place-items: center;
  padding: 4px 6px; border-radius: 12px;
  background: #fff; border: 1px solid var(--line);
}
.logo i {
  --h: 30px;
  --s: calc(var(--h) / 616);
  display: block; width: calc(902 * var(--s)); height: var(--h);
  background: url('../img/logo.webp') no-repeat;
  background-size: calc(1536 * var(--s)) auto;
  background-position: calc(-282 * var(--s)) calc(-54 * var(--s));
}

.login-card { position: relative; }
.login-theme { position: absolute; top: 14px; right: 14px; }
.login-logo { display: grid; place-items: center; padding: 6px 0 2px; }
.login-logo img { display: block; width: 210px; max-width: 70%; height: auto; }
[data-theme='dark'] .login-logo { background: #fff; border-radius: var(--radius-sm); padding: 14px 0 10px; margin-top: 34px; }
.login-title { text-align: center; }

.month-picker {
  display: flex; align-items: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 3px;
}
#month-label { font-weight: 700; font-size: 0.92rem; min-width: 128px; text-align: center; }

.icon-btn {
  width: 38px; height: 38px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: 50%; background: none; color: var(--text); cursor: pointer;
  transition: background-color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn .icon { width: 20px; height: 20px; }
.theme-toggle { background: var(--surface); border: 1px solid var(--line); }
.theme-toggle .sun { display: none; }
[data-theme='dark'] .theme-toggle .sun { display: block; }
[data-theme='dark'] .theme-toggle .moon { display: none; }

/* ---- Campanita de notificaciones ---- */

.topbar-inner { position: relative; }
.bell { position: relative; background: var(--surface); border: 1px solid var(--line); }
.badge {
  position: absolute; top: -4px; right: -4px;
  min-width: 18px; height: 18px; padding: 0 5px;
  display: grid; place-items: center;
  border-radius: 99px; border: 2px solid var(--bg);
  background: var(--over); color: #fff;
  font-size: 0.66rem; font-weight: 800; line-height: 1;
}
.badge.dot { min-width: 12px; width: 12px; height: 12px; padding: 0; top: -1px; right: -1px; background: var(--warn); }

.bell-panel {
  position: absolute; top: calc(100% + 6px); right: 16px; z-index: 10;
  width: min(380px, calc(100vw - 32px));
  max-height: min(70vh, 560px); overflow-y: auto;
  display: grid; gap: 10px;
  padding: 18px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
  animation: pop 0.16s ease-out;
}
.bell-panel h3 { margin-top: 6px; }
.bell-panel .empty { padding: 6px 0; text-align: left; }
.bell-panel .list li { padding: 10px 0; font-size: 0.92rem; }
.list li.is-new { position: relative; padding-left: 16px; }
.list li.is-new::before {
  content: ''; position: absolute; left: 0; top: 50%;
  width: 8px; height: 8px; margin-top: -4px; border-radius: 50%;
  background: var(--primary);
}

.pill-btn {
  height: 32px; padding: 0 14px; margin-left: 2px;
  border: 0; border-radius: 999px;
  background: var(--primary); color: var(--primary-text);
  font: inherit; font-size: 0.82rem; font-weight: 700; cursor: pointer;
}
.pill-btn:disabled { background: var(--surface-2); color: var(--muted); cursor: default; }

/* En pantallas angostas el selector de mes baja a una segunda fila. */
@media (max-width: 560px) {
  .month-picker { order: 3; width: 100%; justify-content: space-between; }
  #month-label { flex: 1; }
}

#view { max-width: 720px; margin: 0 auto; padding: 16px 16px 160px; }
/* minmax(0, 1fr): la columna nunca se ensancha por un contenido largo (p. ej. importes grandes).
   Sin esto la página queda más ancha que la pantalla y el celular la muestra con zoom o achicada. */
.view { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; animation: rise 0.25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; justify-content: center; gap: 2px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabs a {
  flex: 1; max-width: 132px;
  display: grid; justify-items: center; gap: 3px;
  padding: 7px 2px; border-radius: 14px;
  color: var(--muted); text-decoration: none; font-size: 0.7rem; font-weight: 600;
  transition: color 0.15s, background-color 0.15s;
}
.tabs a.active { color: var(--primary); background: var(--primary-soft); }

/* ---- Componentes ---- */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px;
}

.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row > * { min-width: 0; }
/* El importe no se achica ni se corta: cede el texto de la izquierda. */
.row > strong, .row > .amount { flex: none; }
.row.split { border-top: 1px solid var(--line); padding-top: 12px; font-size: 0.92rem; }

/* Tarjeta destacada: redefine los colores para todo lo que contiene. */
.hero {
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.74);
  --line: rgba(255, 255, 255, 0.16);
  --track: rgba(255, 255, 255, 0.2);
  --ok: #ffffff;
  --warn: #fcd34d;
  --over: #fca5a5;
  background: var(--hero);
  color: var(--text);
  padding: 22px;
  box-shadow: 0 14px 34px -16px rgba(11, 92, 86, 0.75);
}
.hero > small:first-child { font-size: 0.78rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.hero .big { font-size: 2.35rem; font-weight: 800; letter-spacing: -0.03em; line-height: 1.1; }
.hero .bar { height: 10px; }
.summary-line { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; }
.summary-line strong { font-size: 1.1rem; }

.bar { height: 8px; border-radius: 99px; background: var(--track); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 99px; background: var(--ok); transition: width 0.4s ease-out; }
.bar.warn i { background: var(--warn); }
.bar.over i { background: var(--over); }
.bar.none i { background: var(--muted); opacity: 0.5; }

.alert {
  --tone: var(--warn);
  border-radius: var(--radius-sm); padding: 12px 14px 12px 16px;
  border-left: 4px solid var(--tone);
  background: color-mix(in srgb, var(--tone) 13%, var(--surface));
  font-size: 0.94rem;
}
.alert.over { --tone: var(--over); }

.donut {
  width: 172px; aspect-ratio: 1; border-radius: 50%;
  margin: 6px auto 10px;
  display: grid; place-items: center;
}
.donut div {
  width: 70%; aspect-ratio: 1; border-radius: 50%;
  background: var(--surface);
  display: grid; place-items: center;
  font-weight: 800; font-size: 0.98rem; letter-spacing: -0.01em;
}

.cats { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; }
.cats li { display: grid; gap: 7px; font-size: 0.95rem; }
.cats .row > span:last-child { color: var(--muted); white-space: nowrap; }
.cats .row strong { color: var(--text); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 4px; margin-right: 10px; }

.day {
  font-size: 0.74rem; color: var(--muted); font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em;
  margin: 8px 6px -6px;
}

.list { list-style: none; margin: 0; padding: 4px 18px; gap: 0; }
.list.plain { padding: 0; }
.list li {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 13px 0;
  border-bottom: 1px solid var(--line);
}
.list li:last-child { border-bottom: 0; }
.list li[role='button'] { cursor: pointer; }
.list li[role='button']:hover strong { color: var(--primary); }
.list li > div { min-width: 0; }
.list.stacked li { display: grid; justify-content: stretch; gap: 7px; padding: 15px 0; }
.list li.archived strong { color: var(--muted); text-decoration: line-through; }
.amount { font-weight: 700; white-space: nowrap; }
.row-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* Cada mes tiene ancho fijo para que el importe entre completo; lo que no entra se desplaza de costado. */
.chart {
  display: flex; align-items: flex-end; gap: 8px; height: 172px;
  overflow-x: auto; padding-bottom: 10px;
  scroll-snap-type: x proximity; scrollbar-width: thin; scrollbar-color: var(--muted) var(--track);
  -webkit-overflow-scrolling: touch;
}
.chart::-webkit-scrollbar { height: 6px; }
.chart::-webkit-scrollbar-track { background: var(--track); border-radius: 99px; }
.chart::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 99px; }
.chart > div { flex: 0 0 72px; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 5px; scroll-snap-align: start; }
.chart i { width: 100%; max-width: 46px; min-height: 3px; border-radius: 8px 8px 3px 3px; background: linear-gradient(180deg, var(--primary), color-mix(in srgb, var(--primary) 55%, var(--surface))); }
.chart span { font-size: 0.66rem; color: var(--muted); white-space: nowrap; font-weight: 600; }
.chart small { font-size: 0.72rem; }

details summary { cursor: pointer; color: var(--muted); padding: 8px 6px; font-weight: 600; font-size: 0.9rem; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }

.notice h3, h3.sub { font-size: 0.74rem; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }
.notice .list li { font-size: 0.95rem; }

/* ---- Formularios ---- */

.form { display: grid; gap: 14px; }
.form label, .label { display: grid; gap: 6px; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.form .two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form.inline { grid-template-columns: 1fr auto; align-items: end; }
.hint { font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }

input, select {
  width: 100%; min-height: 46px;
  padding: 8px 14px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  /* 16px fijo: con menos, Safari en iPhone hace zoom al enfocar el campo y no lo deshace. */
  font: inherit; font-size: 16px; font-weight: 600;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input::placeholder { color: var(--muted); font-weight: 500; opacity: 0.7; }
input:focus, select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent);
}
button:focus-visible, a:focus-visible, li:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 2px;
}

.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: var(--radius-sm); padding: 4px; }
.segmented label { display: block; }
.segmented input { position: absolute; opacity: 0; width: 0; min-height: 0; }
.segmented span { display: block; text-align: center; padding: 10px; border-radius: 9px; cursor: pointer; color: var(--muted); font-size: 0.95rem; }
.segmented input:checked + span { background: var(--surface); color: var(--text); font-weight: 700; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.segmented input:focus-visible + span { outline: 2px solid var(--primary); }

.budget-row { grid-template-columns: 1fr 150px; align-items: center; }
.budget-row > span { color: var(--text); font-size: 0.98rem; font-weight: 600; }
.budget-row small { font-weight: 500; }
.budget-row small.warn { color: var(--warn); }
.budget-row small.over { color: var(--over); }
.budget-row input { text-align: right; }

.suffix { display: flex; align-items: center; gap: 8px; color: var(--text); }
.suffix input { width: 90px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 2px; padding: 3px 3px 3px 12px; border-radius: 99px; background: var(--primary-soft); color: var(--text); font-size: 0.88rem; font-weight: 600; }
.chip button { border: 0; background: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; width: 28px; height: 28px; border-radius: 50%; }
.add-sub { display: grid; grid-template-columns: 1fr auto; gap: 8px; }

.actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }

.btn {
  min-height: 46px; padding: 0 18px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  font: inherit; font-weight: 700; cursor: pointer;
  transition: background-color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 0.82rem; border-radius: 10px; }
.btn.primary { background: var(--primary); color: var(--primary-text); }
.btn.primary:hover { background: var(--primary-strong); }
.btn.ghost { background: var(--surface); border-color: var(--line); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--over); color: #fff; }
.btn.danger-ghost { background: none; color: var(--over); margin-right: auto; }
.btn:disabled { opacity: 0.6; }
/* Arriba y a la derecha: abajo quedaría tapado por el botón "+". */
.btn.wipe { margin: -6px 0 -6px auto; }

.fab {
  /* Centrado sobre la barra inferior para no tapar los importes de la derecha. */
  position: fixed; left: calc(50% - 29px); bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 6;
  width: 58px; height: 58px; border-radius: 20px; border: 0;
  background: var(--hero); color: #fff;
  font-size: 2rem; font-weight: 400; line-height: 1; cursor: pointer;
  box-shadow: 0 12px 26px -8px rgba(11, 92, 86, 0.8);
  transition: transform 0.15s;
}
.fab:hover { transform: translateY(-2px); }

dialog {
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  border: 1px solid var(--line); border-radius: 22px;
  background: var(--surface); color: var(--text);
  padding: 22px;
  box-shadow: var(--shadow-pop);
}
dialog[open] { animation: pop 0.18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(0.98); } }
dialog::backdrop { background: rgba(6, 18, 16, 0.55); backdrop-filter: blur(3px); }
dialog h2 { font-size: 1.2rem; }
#confirm { width: min(360px, calc(100vw - 24px)); }
#confirm p { margin-bottom: 18px; font-weight: 600; }

#toasts {
  position: fixed; left: 0; right: 0; bottom: calc(92px + env(safe-area-inset-bottom)); z-index: 2147483647;
  display: grid; justify-items: center; gap: 8px; padding: 0 16px;
  pointer-events: none;
}
.toast {
  max-width: 480px; padding: 11px 18px; border-radius: 14px;
  background: #14211f; color: #fff; font-size: 0.9rem; font-weight: 600;
  box-shadow: var(--shadow-pop);
  animation: pop 0.2s ease-out;
}
.toast.warn { background: #f59e0b; color: #1c1300; }
.toast.over { background: #dc2626; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
