/* ==========================================================================
   Finanzas Personal — app.css
   Mobile-first para iPhone. Todos los colores y espaciados son variables.
   Modo oscuro automático con prefers-color-scheme. Sin librerías.
   ========================================================================== */

/* ---------- 1. Variables ---------- */
:root {
  color-scheme: light dark;

  /* Colores (claro) — paleta del sistema iOS */
  --c-fondo: #f2f2f7;            /* fondo agrupado */
  --c-superficie: #ffffff;       /* tarjetas y listas */
  --c-superficie-2: #f2f2f7;     /* campos dentro de tarjetas */
  --c-elevada: #ffffff;          /* hojas inferiores */
  --c-texto: #000000;
  --c-texto-2: rgba(60, 60, 67, 0.6);
  --c-texto-3: rgba(60, 60, 67, 0.3);
  --c-separador: rgba(60, 60, 67, 0.29);
  --c-acento: #007aff;
  --c-acento-texto: #ffffff;
  --c-acento-suave: rgba(0, 122, 255, 0.12);
  --c-gasto: #d70015;            /* rojo legible sobre blanco (contraste AA) */
  --c-ingreso: #248a3d;          /* verde legible sobre blanco (contraste AA) */
  --c-aviso: #c93400;
  --c-error-fondo: #ffe5e7;
  --c-ok-fondo: #e3f6e8;
  --c-barra: rgba(249, 249, 249, 0.86);   /* cabecera y pestañas translúcidas */
  --c-velo: rgba(0, 0, 0, 0.4);           /* fondo tras una hoja inferior */
  --c-sombra: 0 -8px 30px rgba(0, 0, 0, 0.12);
  --c-foco: #007aff;

  /* Tipografía */
  --fuente: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  --fuente-numeros: -apple-system, BlinkMacSystemFont, "SF Pro Display", system-ui, sans-serif;
  --t-base: 1rem;          /* 16 px: iOS no hace zoom en los campos */
  --t-pequeno: 0.8125rem;  /* 13 px */
  --t-mini: 0.6875rem;     /* 11 px (etiquetas de pestañas) */
  --t-titulo: 1.0625rem;   /* 17 px (título de cabecera) */
  --t-grande: 1.75rem;     /* 28 px (cifras destacadas) */
  --t-seccion: 0.8125rem;

  /* Espaciado (múltiplos de 4) */
  --e-1: 4px;
  --e-2: 8px;
  --e-3: 12px;
  --e-4: 16px;
  --e-5: 20px;
  --e-6: 24px;
  --e-8: 32px;

  /* Formas y medidas */
  --radio: 12px;
  --radio-pequeno: 8px;
  --radio-hoja: 16px;
  --tactil: 44px;               /* zona táctil mínima de Apple */
  --alto-cabecera: 44px;
  --alto-pestanas: 50px;
  --ancho-max: 640px;           /* en iPad/escritorio, columna centrada */

  /* Zonas seguras (notch e indicador de inicio) */
  --seguro-arriba: env(safe-area-inset-top, 0px);
  --seguro-abajo: env(safe-area-inset-bottom, 0px);
  --seguro-izq: env(safe-area-inset-left, 0px);
  --seguro-der: env(safe-area-inset-right, 0px);

  --transicion: 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-fondo: #000000;
    --c-superficie: #1c1c1e;
    --c-superficie-2: #2c2c2e;
    --c-elevada: #1c1c1e;
    --c-texto: #ffffff;
    --c-texto-2: rgba(235, 235, 245, 0.6);
    --c-texto-3: rgba(235, 235, 245, 0.3);
    --c-separador: rgba(84, 84, 88, 0.65);
    --c-acento: #0a84ff;
    --c-acento-suave: rgba(10, 132, 255, 0.2);
    --c-gasto: #ff6961;
    --c-ingreso: #30d158;
    --c-aviso: #ff9f0a;
    --c-error-fondo: #3a1215;
    --c-ok-fondo: #0f2e18;
    --c-barra: rgba(22, 22, 24, 0.86);
    --c-velo: rgba(0, 0, 0, 0.6);
    --c-sombra: 0 -8px 30px rgba(0, 0, 0, 0.5);
    --c-foco: #0a84ff;
  }
}

/* ---------- 2. Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  background: var(--c-fondo);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--fuente);
  font-size: var(--t-base);
  line-height: 1.4;
  color: var(--c-texto);
  background: var(--c-fondo);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--c-acento); text-decoration: none; }
img, svg { display: block; }
button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 3px solid var(--c-foco); outline-offset: 2px; border-radius: 4px; }

.saltar {
  position: absolute; left: var(--e-2); top: -100px; z-index: 100;
  padding: var(--e-2) var(--e-3); background: var(--c-superficie); border-radius: var(--radio-pequeno);
}
.saltar:focus { top: calc(var(--seguro-arriba) + var(--e-2)); }

.visualmente-oculto {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.icono { width: 24px; height: 24px; flex: none; }

/* Importes: cifras tabulares para que alineen en columna */
.importe { font-variant-numeric: tabular-nums; white-space: nowrap; }
.importe-gasto { color: var(--c-gasto); }
.importe-ingreso { color: var(--c-ingreso); }

.texto-secundario { color: var(--c-texto-2); }
.texto-pequeno { font-size: var(--t-pequeno); }

/* ---------- 3. Cabecera compacta ---------- */
.cabecera {
  position: sticky; top: 0; z-index: 20;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  min-height: calc(var(--alto-cabecera) + var(--seguro-arriba));
  padding: var(--seguro-arriba) calc(var(--e-2) + var(--seguro-der)) 0 calc(var(--e-2) + var(--seguro-izq));
  background: var(--c-barra);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--c-separador);
}
.cabecera-titulo {
  font-size: var(--t-titulo); font-weight: 600; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cabecera-lado { display: flex; align-items: center; min-width: 0; }
.cabecera-accion { justify-content: flex-end; }
.boton-cabecera {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--e-1);
  min-width: var(--tactil); min-height: var(--tactil); padding: 0 var(--e-2);
  border: 0; background: none; color: var(--c-acento); font-size: var(--t-titulo); cursor: pointer;
}
.boton-volver { margin-left: calc(-1 * var(--e-2)); }

/* ---------- 4. Contenido ---------- */
.contenido {
  width: 100%; max-width: var(--ancho-max); margin: 0 auto;
  padding: var(--e-4) calc(var(--e-4) + var(--seguro-der)) var(--e-6) calc(var(--e-4) + var(--seguro-izq));
}
.contenido:focus { outline: none; }
body.con-pestanas .contenido {
  padding-bottom: calc(var(--alto-pestanas) + var(--seguro-abajo) + var(--e-6));
}

.seccion { margin-bottom: var(--e-6); }
.seccion-titulo {
  margin: 0 var(--e-4) var(--e-2);
  font-size: var(--t-seccion); font-weight: 400; text-transform: uppercase; letter-spacing: 0.02em;
  color: var(--c-texto-2);
}
.seccion-pie { margin: var(--e-2) var(--e-4) 0; font-size: var(--t-pequeno); color: var(--c-texto-2); }

.tarjeta {
  background: var(--c-superficie); border-radius: var(--radio); padding: var(--e-4);
}

/* Cifras destacadas de Inicio */
.resumen { display: grid; grid-template-columns: 1fr 1fr; gap: var(--e-3); }
.resumen .tarjeta { display: flex; flex-direction: column; gap: var(--e-1); }
.resumen .tarjeta-ancha { grid-column: 1 / -1; }
.cifra-etiqueta { font-size: var(--t-pequeno); color: var(--c-texto-2); }
.cifra { font-family: var(--fuente-numeros); font-size: var(--t-grande); font-weight: 700; letter-spacing: -0.01em; }
.cifra-vacia { color: var(--c-texto-3); }

/* ---------- 5. Listas al estilo iOS ---------- */
.lista {
  list-style: none; margin: 0; padding: 0;
  background: var(--c-superficie); border-radius: var(--radio); overflow: hidden;
}
.lista-item { position: relative; }
.lista-item + .lista-item::before {
  content: ""; position: absolute; top: 0; right: 0; left: 60px;
  border-top: 0.5px solid var(--c-separador);
}
.lista-item.sin-icono + .lista-item::before,
.lista-item + .lista-item.sin-icono::before { left: var(--e-4); }

.fila {
  display: flex; align-items: center; gap: var(--e-3);
  min-height: var(--tactil); padding: var(--e-2) var(--e-4);
  width: 100%; border: 0; background: none; text-align: left; color: var(--c-texto); cursor: pointer;
}
a.fila:active, button.fila:active { background: var(--c-superficie-2); }
.fila[aria-disabled="true"] { cursor: default; }
.fila[aria-disabled="true"]:active { background: none; }
.fila-icono {
  display: grid; place-items: center; flex: none;
  width: 32px; height: 32px; border-radius: var(--radio-pequeno);
  background: var(--c-acento); color: #fff; font-size: 18px;
}
.fila-icono .icono { width: 20px; height: 20px; }
.fila-texto { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.fila-principal { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fila-secundaria { font-size: var(--t-pequeno); color: var(--c-texto-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fila-valor { flex: none; color: var(--c-texto-2); }
.fila-flecha { width: 18px; height: 18px; color: var(--c-texto-3); flex: none; }
.fila-peligro { color: var(--c-gasto); }
.etiqueta-pronto {
  flex: none; font-size: var(--t-mini); padding: 2px var(--e-2); border-radius: 999px;
  background: var(--c-superficie-2); color: var(--c-texto-2);
}

/* Colores de los iconos de Más (como en Ajustes de iOS) */
.fila-icono.c-azul { background: #007aff; }
.fila-icono.c-verde { background: #34c759; }
.fila-icono.c-naranja { background: #ff9500; }
.fila-icono.c-morado { background: #af52de; }
.fila-icono.c-rojo { background: #ff3b30; }
.fila-icono.c-gris { background: #8e8e93; }
.fila-icono.c-indigo { background: #5856d6; }
.fila-icono.c-turquesa { background: #30b0c7; }

/* Grupo de movimientos por fecha (se usará en la Fase 3) */
.grupo-fecha { margin: var(--e-5) var(--e-4) var(--e-2); font-size: var(--t-pequeno); font-weight: 600; color: var(--c-texto-2); }

/* ---------- 6. Estado vacío ---------- */
.vacio {
  display: flex; flex-direction: column; align-items: center; gap: var(--e-2);
  padding: var(--e-8) var(--e-4); text-align: center; color: var(--c-texto-2);
}
.vacio .icono { width: 44px; height: 44px; color: var(--c-texto-3); margin-bottom: var(--e-2); }
.vacio-titulo { font-size: var(--t-titulo); font-weight: 600; color: var(--c-texto); }
.vacio .boton { margin-top: var(--e-3); }

/* ---------- 7. Botones ---------- */
.boton {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--e-2);
  min-height: var(--tactil); padding: var(--e-3) var(--e-5);
  border: 0; border-radius: var(--radio); cursor: pointer;
  font-size: var(--t-titulo); font-weight: 600; text-align: center;
  transition: opacity var(--transicion), transform var(--transicion);
}
.boton:active { transform: scale(0.98); opacity: 0.85; }
.boton:disabled, .boton[aria-disabled="true"] { opacity: 0.4; cursor: default; transform: none; }
.boton-principal { background: var(--c-acento); color: var(--c-acento-texto); }
.boton-secundario { background: var(--c-acento-suave); color: var(--c-acento); }
.boton-texto { background: none; color: var(--c-acento); font-weight: 400; }
.boton-ancho { width: 100%; }

/* ---------- 8. Formularios ---------- */
.formulario { display: flex; flex-direction: column; gap: var(--e-4); }
.formulario-titulo { font-size: 1.375rem; font-weight: 700; }
.campo { display: flex; flex-direction: column; gap: var(--e-1); }
.campo-etiqueta { font-size: var(--t-pequeno); color: var(--c-texto-2); padding-left: var(--e-1); }
.campo-ayuda { font-size: var(--t-pequeno); color: var(--c-texto-2); padding-left: var(--e-1); }
.campo input, .campo select, .buscador input {
  width: 100%; min-height: var(--tactil); padding: var(--e-3) var(--e-3);
  font-size: var(--t-base); /* 16 px mínimo: evita el zoom de iOS */
  background: var(--c-superficie-2); color: var(--c-texto);
  border: 1px solid transparent; border-radius: var(--radio-pequeno);
  -webkit-appearance: none; appearance: none;
}
.campo input:focus, .campo select:focus, .buscador input:focus { outline: none; border-color: var(--c-acento); }
.campo-doble { display: grid; grid-template-columns: 1fr 1fr; gap: var(--e-3); }

.interruptor { display: flex; align-items: center; gap: var(--e-3); min-height: var(--tactil); cursor: pointer; }
.interruptor input {
  -webkit-appearance: none; appearance: none; flex: none; position: relative; margin: 0;
  width: 51px; height: 31px; border-radius: 999px; background: var(--c-texto-3);
  transition: background var(--transicion); cursor: pointer;
}
.interruptor input::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); transition: transform var(--transicion);
}
.interruptor input:checked { background: #34c759; }
.interruptor input:checked::after { transform: translateX(20px); }

.buscador { position: relative; margin-bottom: var(--e-4); }
.buscador .icono { position: absolute; left: var(--e-3); top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--c-texto-2); pointer-events: none; }
.buscador input { padding-left: 36px; background: var(--c-superficie); }

.chips { display: flex; gap: var(--e-2); overflow-x: auto; margin: 0 calc(-1 * var(--e-4)) var(--e-4); padding: 0 var(--e-4); scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 32px; padding: var(--e-1) var(--e-3); border-radius: 999px;
  border: 0; background: var(--c-superficie); color: var(--c-texto); font-size: var(--t-pequeno); cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--c-acento); color: var(--c-acento-texto); }

/* ---------- 9. Avisos (flash) ---------- */
.flash { padding: var(--e-3) var(--e-4); border-radius: var(--radio); margin-bottom: var(--e-4); }
.flash-error { background: var(--c-error-fondo); color: var(--c-gasto); }
.flash-ok { background: var(--c-ok-fondo); color: var(--c-ingreso); }
.formulario .flash { margin-bottom: 0; }

/* ---------- 10. Barra de pestañas fija abajo ---------- */
.pestanas {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: space-around;
  padding: 0 var(--seguro-der) var(--seguro-abajo) var(--seguro-izq);
  background: var(--c-barra);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--c-separador);
}
.pestana {
  flex: 1; max-width: 120px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--alto-pestanas); padding-top: var(--e-1);
  color: var(--c-texto-2); font-size: var(--t-mini); font-weight: 500;
}
.pestana .icono { width: 26px; height: 26px; }
.pestana[aria-current="page"] { color: var(--c-acento); }
.pestana[aria-current="page"] .icono { stroke-width: 2.2; }

/* ---------- 11. Hojas inferiores (bottom sheets) con <dialog> ---------- */
dialog.hoja {
  position: fixed; inset: auto 0 0 0; margin: 0 auto; padding: 0;
  width: 100%; max-width: var(--ancho-max); max-height: 88vh; max-height: 88dvh;
  border: 0; border-radius: var(--radio-hoja) var(--radio-hoja) 0 0;
  background: var(--c-elevada); color: var(--c-texto); box-shadow: var(--c-sombra);
  overflow: hidden; overscroll-behavior: contain;
}
dialog.hoja[open] { display: flex; flex-direction: column; animation: hoja-entra var(--transicion); }
dialog.hoja.cerrando { animation: hoja-sale 0.2s ease-in forwards; }
dialog.hoja::backdrop { background: var(--c-velo); animation: velo-entra var(--transicion); }
dialog.hoja.cerrando::backdrop { animation: velo-sale 0.2s ease-in forwards; }
.hoja-asa {
  display: block; width: 100%; height: 22px; border: 0; background: none; cursor: grab; touch-action: none;
  position: relative; flex: none;
}
.hoja-asa::after {
  content: ""; position: absolute; left: 50%; top: 8px; width: 36px; height: 5px; margin-left: -18px;
  border-radius: 3px; background: var(--c-texto-3);
}
.hoja-cabecera {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 0 var(--e-2) var(--e-2); border-bottom: 0.5px solid var(--c-separador); flex: none;
}
.hoja-cabecera h2 { font-size: var(--t-titulo); font-weight: 600; text-align: center; }
.hoja-cabecera .boton-cabecera:last-child { justify-self: end; font-weight: 600; }
.hoja-cuerpo {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: var(--e-4) var(--e-4) calc(var(--e-6) + var(--seguro-abajo));
}
@keyframes hoja-entra { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes hoja-sale { from { transform: translateY(0); } to { transform: translateY(100%); } }
@keyframes velo-entra { from { opacity: 0; } to { opacity: 1; } }
@keyframes velo-sale { from { opacity: 1; } to { opacity: 0; } }

/* ---------- 12. Pantallas de acceso ---------- */
body.acceso { display: flex; }
.acceso-contenido {
  width: 100%; max-width: 420px; margin: auto;
  padding: calc(var(--e-8) + var(--seguro-arriba)) calc(var(--e-4) + var(--seguro-der)) calc(var(--e-8) + var(--seguro-abajo)) calc(var(--e-4) + var(--seguro-izq));
}
.acceso-marca { display: flex; flex-direction: column; align-items: center; gap: var(--e-3); margin-bottom: var(--e-6); }
.acceso-marca img { border-radius: 16px; }
.acceso-nombre { font-size: 1.375rem; font-weight: 700; }
.acceso-salir { display: flex; justify-content: center; margin-top: var(--e-4); }

/* ---------- 13. Gráficos (preparado para la Fase 4) ---------- */
.grafico { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ---------- 14. Preferencias de accesibilidad ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* Pantallas anchas (iPad, escritorio): misma columna, centrada */
@media (min-width: 700px) {
  .pestanas { justify-content: center; gap: var(--e-6); }
}
