/* La Tramoya · paleta sacada del logo y de los azulejos de la barra */
:root {
  --crema: #F4EEDF;
  --azul-logo: #3B44C4;
  --celeste: #6FB0C4;
  --rojo: #B5150F;
  --naranja: #E8431A;
  --amarillo: #F2B705;
  --verde: #2F4A2E;
  --marino: #1E3A9E;
  --negro: #141414;

  --bg: var(--crema);
  --surface: #FFFDF8;
  --surface-2: #EDE5D2;
  --text: #1A1A1A;
  --muted: #5E5A52;
  --line: #DDD3BD;
  --primary: var(--azul-logo);
  --on-primary: #FFFFFF;
  --ingresos: var(--azul-logo);
  --ingresos-suave: #C5C8F0;
  --beneficio: #2E7039;
  --perdida: var(--rojo);
  --efectivo: #2E7039;
  --tarjeta: var(--marino);
  --ok: #2E7039;
  --aviso: #8A6500;
  --sombra: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 5%);

  --radio: 16px;
  --nav-alto: 72px;
  color-scheme: light;
}

/* Oscuro: negro encimera + texto crema. Para leer con poca luz. */
@media (prefers-color-scheme: dark) {
  :root:not([data-tema="claro"]) {
    --bg: #121212; --surface: #1E1D1B; --surface-2: #2A2825; --text: #F4EEDF; --muted: #B8B0A0;
    --line: #3A3733; --primary: #8E95F7; --on-primary: #0E1030; --ingresos: #8E95F7; --ingresos-suave: #545AB8;
    --beneficio: #6FC57D; --perdida: #FF6F5C; --efectivo: #6FC57D; --tarjeta: var(--celeste);
    --ok: #6FC57D; --aviso: var(--amarillo); --sombra: none; color-scheme: dark;
  }
}
:root[data-tema="oscuro"] {
  --bg: #121212; --surface: #1E1D1B; --surface-2: #2A2825; --text: #F4EEDF; --muted: #B8B0A0;
  --line: #3A3733; --primary: #8E95F7; --on-primary: #0E1030; --ingresos: #8E95F7; --ingresos-suave: #545AB8;
  --beneficio: #6FC57D; --perdida: #FF6F5C; --efectivo: #6FC57D; --tarjeta: var(--celeste);
  --ok: #6FC57D; --aviso: var(--amarillo); --sombra: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  font: 18px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
button, input, a { font: inherit; color: inherit; }
button { cursor: pointer; }
h2 { font-size: 1rem; margin: 0 0 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

/* Franja de azulejos, como la barra del bar */
.azulejos {
  height: 10px;
  background: linear-gradient(90deg,
    var(--naranja) 0 11.1%, var(--crema) 0 22.2%, var(--celeste) 0 33.3%, var(--amarillo) 0 44.4%,
    var(--negro) 0 55.5%, var(--rojo) 0 66.6%, #ffffff 0 77.7%, var(--verde) 0 88.8%, var(--marino) 0);
  background-size: 180px 10px;
}

/* Carga */
.cargando { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); }
.cargando img { border-radius: 50%; animation: latido 1.2s ease-in-out infinite; }
@keyframes latido { 50% { transform: scale(.94); opacity: .8; } }

/* Login */
.login { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; padding: max(48px, env(safe-area-inset-top)) 24px 24px; gap: 24px; }
.login-logo { border-radius: 50%; box-shadow: var(--sombra); }
.login .azulejos { width: 100%; max-width: 420px; border-radius: 3px; }
.login-form { width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 16px; }
.campo { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.campo input {
  height: 60px; padding: 0 16px; border-radius: 12px; border: 2px solid var(--line);
  background: var(--surface); font-size: 20px;
}
.campo input:focus { border-color: var(--primary); outline: none; }
.error { color: var(--perdida); min-height: 1.4em; margin: 0; font-weight: 600; }

/* Botones */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 56px; padding: 0 20px; border-radius: 14px; border: 0;
  font-weight: 700; text-decoration: none; text-align: center;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
  transition: transform .1s ease, opacity .15s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; }
.btn-primario { background: var(--primary); color: var(--on-primary); }
.btn-grande { min-height: 64px; font-size: 22px; width: 100%; }
.btn-secundario { background: var(--surface-2); color: var(--text); width: 100%; }
.btn-texto { background: none; border: 0; padding: 16px; color: var(--muted); text-decoration: underline; min-height: 56px; touch-action: manipulation; }
.btn-peligro { color: var(--perdida); }
.btn-icono {
  width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%;
  border: 0; background: transparent; color: var(--text); touch-action: manipulation;
}

/* Cabecera */
#app { padding-bottom: calc(var(--nav-alto) + env(safe-area-inset-bottom)); }
.cabecera {
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
}
.cabecera-logo { border-radius: 50%; }
.cabecera h1 { flex: 1; margin: 0; font-size: 24px; }

.vista { padding: 16px; max-width: 560px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }

/* Selector de día / mes */
.selector { display: flex; align-items: stretch; gap: 8px; }
.btn-flecha {
  width: 64px; min-height: 72px; border-radius: 14px; border: 0; background: var(--surface);
  box-shadow: var(--sombra); font-size: 40px; line-height: 1; color: var(--primary); touch-action: manipulation;
}
.btn-flecha:disabled { opacity: .3; }
.selector-centro {
  position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--surface); border-radius: 14px; box-shadow: var(--sombra); padding: 8px; text-align: center;
}
.selector-sup { font-size: 14px; font-weight: 700; color: var(--primary); text-transform: uppercase; letter-spacing: .06em; }
.selector-principal { font-size: 20px; font-weight: 700; }
.selector-principal::first-letter { text-transform: uppercase; }
/* El input de fecha cubre toda la caja: al tocar se abre el calendario del móvil */
.input-fecha-oculto { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; border: 0; }
.input-fecha-oculto::-webkit-calendar-picker-indicator { position: absolute; inset: 0; width: 100%; height: 100%; cursor: pointer; }

.estado { margin: -4px 0 0; text-align: center; font-weight: 600; color: var(--muted); min-height: 1.4em; }
.estado.ok { color: var(--ok); }
.estado.pendiente { color: var(--aviso); }

/* Tarjetas */
.tarjeta { background: var(--surface); border-radius: var(--radio); padding: 16px; box-shadow: var(--sombra); }
#form-cierre { display: flex; flex-direction: column; gap: 16px; }

.fila-importe { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.etiqueta { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 600; }
.punto { display: inline-block; width: 14px; height: 14px; border-radius: 3px; flex: none; }
.punto-efectivo { background: var(--efectivo); }
.punto-tarjeta { background: var(--tarjeta); }
.punto-ingresos { background: var(--ingresos-suave); }
.punto-beneficio { background: var(--beneficio); }

.input-euros {
  display: flex; align-items: center; gap: 6px; height: 60px; padding: 0 14px;
  border: 2px solid var(--line); border-radius: 12px; background: var(--bg);
  width: 180px; flex: none;
}
.input-euros:focus-within { border-color: var(--primary); }
.input-euros input {
  width: 100%; min-width: 0; border: 0; background: transparent; outline: none;
  font-size: 24px; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums;
}
.input-euros span { color: var(--muted); font-weight: 700; }

.fila-total { display: flex; justify-content: space-between; align-items: baseline; border-top: 2px dashed var(--line); margin-top: 12px; padding-top: 12px; font-weight: 600; }
.fila-total strong { font-size: 24px; font-variant-numeric: tabular-nums; }

/* Gastos */
.lista-gastos { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.gasto { display: grid; grid-template-columns: 1fr 120px 48px; gap: 8px; align-items: center; }
.gasto input {
  height: 56px; border: 2px solid var(--line); border-radius: 12px; background: var(--bg);
  padding: 0 12px; font-size: 18px; min-width: 0; width: 100%;
}
.gasto input:focus { border-color: var(--primary); outline: none; }
.gasto .importe { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.gasto .quitar {
  width: 48px; height: 48px; border: 0; border-radius: 50%; background: var(--surface-2);
  font-size: 26px; line-height: 1; color: var(--muted); touch-action: manipulation;
}
.sin-gastos { color: var(--muted); margin: 0 0 12px; }

.tarjeta-beneficio { display: flex; justify-content: space-between; align-items: center; font-weight: 700; font-size: 20px; }
.tarjeta-beneficio strong { font-size: 30px; color: var(--beneficio); font-variant-numeric: tabular-nums; }
.tarjeta-beneficio.negativo strong { color: var(--perdida); }

.barra-guardar {
  position: sticky; bottom: calc(var(--nav-alto) + env(safe-area-inset-bottom) + 8px); z-index: 2;
}
.barra-guardar .btn { box-shadow: 0 6px 20px rgb(0 0 0 / 22%); }

/* Últimos días */
.ultimos { margin-top: 8px; }
.lista-ultimos { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.lista-ultimos button {
  width: 100%; min-height: 60px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  border: 0; border-radius: 12px; background: var(--surface); padding: 10px 16px; box-shadow: var(--sombra);
  text-align: left; touch-action: manipulation;
}
.lista-ultimos button[aria-current="true"] { outline: 3px solid var(--primary); }
.lista-ultimos .ult-fecha { font-weight: 600; }
.lista-ultimos .ult-fecha::first-letter { text-transform: uppercase; }
.lista-ultimos .ult-num { text-align: right; font-variant-numeric: tabular-nums; }
.lista-ultimos .ult-num strong { display: block; }
.lista-ultimos .ult-num small { color: var(--beneficio); font-weight: 600; }
.lista-ultimos .ult-num small.negativo { color: var(--perdida); }
.vacio { color: var(--muted); }

/* Dashboard */
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.kpi { background: var(--surface); border-radius: var(--radio); padding: 14px; box-shadow: var(--sombra); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kpi span { font-size: 14px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kpi strong { font-size: 24px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi small { color: var(--muted); font-size: 15px; }
.kpi:first-child strong { color: var(--ingresos); }
.kpi:nth-child(2) strong { color: var(--beneficio); }
.kpi.negativo strong { color: var(--perdida); }

.leyenda { display: flex; gap: 16px; font-size: 15px; color: var(--muted); margin: -4px 0 8px; }
.leyenda span { display: flex; align-items: center; gap: 6px; }
.grafico svg { display: block; width: 100%; height: auto; touch-action: manipulation; }
.grafico .eje { stroke: var(--line); stroke-width: 1; }
.grafico .eje-cero { stroke: var(--muted); stroke-width: 1.5; }
.grafico text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.grafico .b-ing { fill: var(--ingresos-suave); }
.grafico .b-ben { fill: var(--beneficio); }
.grafico .b-per { fill: var(--perdida); }
.grafico .activo .b-ing { fill: var(--ingresos); }
.grafico .hoy text, .grafico .activo text { fill: var(--text); font-weight: 700; }
.grafico .zona { fill: transparent; cursor: pointer; }
.detalle { margin: 8px 0 0; min-height: 2.8em; color: var(--muted); font-size: 16px; }
.detalle strong { color: var(--text); }
.acciones { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-top: 8px; }

/* Navegación inferior */
.nav-inferior {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: 1fr 1fr;
  height: calc(var(--nav-alto) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 15px; font-weight: 700; touch-action: manipulation;
}
.nav-item[aria-current="page"] { color: var(--primary); }
.nav-item[aria-current="page"] svg { background: color-mix(in srgb, var(--primary) 16%, transparent); border-radius: 14px; padding: 2px 14px; width: 60px; box-sizing: content-box; }

/* Avisos flotantes */
.aviso {
  position: fixed; left: 50%; bottom: calc(var(--nav-alto) + env(safe-area-inset-bottom) + 92px); z-index: 10;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 14px 22px; border-radius: 999px; font-weight: 700;
  transition: opacity .2s ease, transform .2s ease; max-width: calc(100% - 32px); text-align: center;
}
.aviso.visible { opacity: 1; transform: translate(-50%, 0); }
.aviso.error { background: var(--perdida); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
@media (hover: hover) {
  .btn:hover, .btn-flecha:hover, .lista-ultimos button:hover { opacity: .9; }
}

/* Importe mal escrito */
.input-euros:has(input[aria-invalid="true"]),
.gasto input[aria-invalid="true"] { border-color: var(--perdida); background: color-mix(in srgb, var(--perdida) 8%, var(--bg)); }
