/* Evolutio · movimiento
   El cuaderno se pasa, se escribe y se subraya. Todo el movimiento vive aquí. */

:root {
  --ease-salida: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-hoja: cubic-bezier(0.77, 0, 0.175, 1);
  --grano-papel: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='g' x='0' y='0'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.24  0 0 0 0 0.22  0 0 0 0 0.16  0.32 0 0 0 -0.115'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url%28%23g%29'/%3E%3C/svg%3E");
  --grano-claro: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='g' x='0' y='0'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.95  0 0 0 0 0.92  0 0 0 0 0.86  0.26 0 0 0 -0.095'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url%28%23g%29'/%3E%3C/svg%3E");
}

/* ---------- Pasar de página ---------- */

@view-transition { navigation: auto; }

::view-transition-group(root) { animation-duration: 520ms; }
::view-transition-old(root) {
  animation: 520ms var(--ease-hoja) both hoja-se-va;
}
::view-transition-new(root) {
  animation: 520ms var(--ease-hoja) both hoja-llega;
  box-shadow: -28px 0 48px -24px rgba(18, 20, 18, 0.45);
}
@keyframes hoja-se-va { to { transform: translateX(-12%); filter: brightness(0.9); } }
@keyframes hoja-llega { from { transform: translateX(100%); } }

/* Volver atrás: la hoja se devuelve por el otro lado */
html:active-view-transition-type(atras)::view-transition-old(root) {
  animation-name: hoja-se-devuelve;
  z-index: 2;
  box-shadow: -28px 0 48px -24px rgba(18, 20, 18, 0.45);
}
html:active-view-transition-type(atras)::view-transition-new(root) {
  animation-name: hoja-reaparece;
  box-shadow: none;
}
@keyframes hoja-se-devuelve { to { transform: translateX(100%); } }
@keyframes hoja-reaparece { from { transform: translateX(-12%); filter: brightness(0.9); } }


/* ---------- Papel: grano y cuadrícula ---------- */

.hoja {
  background-image:
    var(--grano-papel),
    linear-gradient(var(--cuadricula) 1px, transparent 1px),
    linear-gradient(90deg, var(--cuadricula) 1px, transparent 1px);
  background-size: 220px 220px, 24px 24px, 24px 24px;
  background-position: 0 0, -1px -1px, -1px -1px;
}
.escribeme { background-image: var(--grano-papel); background-size: 220px 220px; }
.separador, .tapa { background-image: var(--grano-claro); background-size: 220px 220px; }
.tapa { background-blend-mode: normal; }

/* La cuadrícula va un poco más despacio que la tinta al hacer scroll */
@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {
    .hoja {
      isolation: isolate;
      overflow: clip;
      background-image: var(--grano-papel);
      background-size: 220px 220px;
    }
    .hoja::after {
      content: "";
      position: absolute;
      inset: -72px 0;
      z-index: -1;
      pointer-events: none;
      background-image:
        linear-gradient(var(--cuadricula) 1px, transparent 1px),
        linear-gradient(90deg, var(--cuadricula) 1px, transparent 1px);
      background-size: 24px 24px;
      background-position: -1px -1px;
      animation: rejilla-lenta linear both;
      animation-timeline: view();
      animation-range: cover;
      will-change: transform;
    }
  }
}
@keyframes rejilla-lenta {
  from { transform: translate3d(0, -72px, 0); }
  to { transform: translate3d(0, 72px, 0); }
}

/* ---------- Portada: entra una vez al cargar ---------- */

.anim .portada .wordmark span {
  clip-path: inset(-0.1em 100% -0.1em -0.02em);
  animation: destapar 900ms var(--ease-salida) 80ms forwards;
}
.anim .portada .promesa,
.anim .portada .firma-dato,
.anim .portada .acciones {
  opacity: 0;
  translate: 0 10px;
  animation: subir 560ms var(--ease-salida) forwards;
}
.anim .portada .promesa { animation-delay: 260ms; }
.anim .portada .firma-dato { animation-delay: 420ms; }
.anim .portada .acciones { animation-delay: 560ms; }
.anim .firma-nombre { animation-delay: 700ms; }

@keyframes destapar { to { clip-path: inset(-0.1em -0.02em -0.1em -0.02em); } }
@keyframes subir { to { opacity: 1; translate: 0 0; } }

/* ---------- Entradas al hacer scroll (una sola vez) ---------- */

.anim .entra-escrito {
  clip-path: inset(-0.2em 100% -0.25em -0.1em);
  transition: clip-path 760ms var(--ease-salida);
}
.anim .entra-escrito.visto { clip-path: inset(-0.2em -0.2em -0.25em -0.1em); }

.anim .entra-sube {
  opacity: 0;
  translate: 0 12px;
  transition: opacity 480ms var(--ease-salida), translate 560ms var(--ease-salida);
  transition-delay: calc(var(--i, 0) * 55ms);
}
.anim .entra-sube.visto { opacity: 1; translate: 0 0; }

/* La página verde se abre desde el lomo, como un separador */
.anim .separador.entra-hoja {
  background-color: var(--papel);
  background-image: var(--grano-claro), linear-gradient(var(--verde), var(--verde));
  background-size: 220px 220px, 0% 100%;
  background-repeat: repeat, no-repeat;
  transition: background-size 820ms var(--ease-hoja);
}
.anim .separador.entra-hoja.visto { background-size: 220px 220px, 100% 100%; }
.anim .separador.entra-hoja > * {
  opacity: 0;
  translate: 0 14px;
  transition: opacity 520ms var(--ease-salida) 380ms, translate 620ms var(--ease-salida) 380ms;
}
.anim .separador.entra-hoja > * + * { transition-delay: 520ms; }
.anim .separador.entra-hoja.visto > * { opacity: 1; translate: 0 0; }

/* El fluorescente del artículo se pasa una vez, cuando la frase ya está a la vista */
.anim .subrayado-verde.entra-trazo {
  background-size: 0% 1.05em;
  transition: background-size 900ms var(--ease-salida) 250ms;
}
.anim .subrayado-verde.entra-trazo.visto { background-size: 100% 1.05em; }

/* El rotulador subraya «pocas» cuando la página ya está abierta */
.anim .plazas .subrayado {
  background-size: 0% 0.2em;
  transition: background-size 700ms var(--ease-salida) 1000ms;
}
.anim .plazas.visto .subrayado { background-size: 100% 0.2em; }

/* ---------- Botones ---------- */

.boton {
  transition: background-color 180ms ease, transform 160ms var(--ease-salida);
}
.boton:active { transform: scale(0.97); }
.boton::after { transition: transform 220ms var(--ease-salida); }
.boton:hover::after { transform: none; }
@media (hover: hover) and (pointer: fine) {
  .boton:hover::after { transform: translateX(4px); }
}
.boton-texto { display: inline-block; }

/* ---------- Enlaces: el subrayado se dibuja ---------- */

.enlace,
.ref,
.pie-datos a,
.campo-check label a {
  text-decoration: none;
  background-image:
    linear-gradient(var(--verde), var(--verde)),
    linear-gradient(currentColor, currentColor);
  background-size: 0% 2px, 100% 1px;
  background-position: 0 100%, 0 100%;
  background-repeat: no-repeat;
  padding-bottom: 0.12em;
  transition: background-size 280ms var(--ease-salida), color 180ms ease;
}
.pie-datos a { background-image: linear-gradient(var(--verde-tiza), var(--verde-tiza)), linear-gradient(currentColor, currentColor); }
.cabecera nav a:not(.nav-accion) {
  background: linear-gradient(currentColor, currentColor) 0 100% / 0% 2px no-repeat;
  transition: background-size 280ms var(--ease-salida), color 180ms ease;
}
@media (hover: hover) and (pointer: fine) {
  .enlace:hover, .ref:hover, .pie-datos a:hover, .campo-check label a:hover { background-size: 100% 2px, 100% 1px; }
  .cabecera nav a:not(.nav-accion):hover { background-size: 100% 2px; }
}

.indice .titulo {
  text-decoration: none;
  background: linear-gradient(var(--verde), var(--verde)) 0 100% / 0% 2px no-repeat;
  transition: background-size 320ms var(--ease-salida);
}
@media (hover: hover) and (pointer: fine) {
  .indice a:hover .titulo { background-size: 100% 2px; }
}
.indice a:focus-visible .titulo { background-size: 100% 2px; }

/* Foco de teclado: aparece al instante, nunca se anima */
:focus-visible { transition: none; }

/* ---------- Pestañas: el verde barre de una a otra ---------- */

.pestana,
.pestana[aria-pressed="true"] {
  background-color: transparent;
  background-image: linear-gradient(var(--verde), var(--verde));
  background-repeat: no-repeat;
  background-position: var(--lado, left) center;
  background-size: 0% 100%;
  transition: background-size 260ms var(--ease-salida), color 200ms ease, border-color 200ms ease, transform 160ms var(--ease-salida);
}
.pestana[aria-pressed="true"] { background-size: 100% 100%; }
.pestana:active { transform: scale(0.97); }
@media (hover: hover) and (pointer: fine) {
  .pestana:not([aria-pressed="true"]):hover { background-color: rgba(14, 107, 71, 0.08); }
}

/* ---------- Formulario ---------- */

.campo { --linea: var(--verde); }
.campo:has([aria-invalid="true"]) { --linea: var(--rojo); }
.campo input[type="text"],
.campo input[type="email"] {
  background-image: linear-gradient(var(--linea), var(--linea));
  background-size: 0% 2px;
  background-position: 0 100%;
  background-repeat: no-repeat;
  transition: background-size 340ms var(--ease-salida), border-color 200ms ease;
}
.campo textarea {
  background-image:
    linear-gradient(var(--linea), var(--linea)),
    linear-gradient(transparent calc(2.25rem - 1px), var(--renglon) calc(2.25rem - 1px));
  background-size: 0% 2px, 100% 2.25rem;
  background-position: 0 100%, 0 0;
  background-repeat: no-repeat, repeat;
  background-attachment: scroll, local;
  transition: background-size 340ms var(--ease-salida), border-color 200ms ease;
}
.campo input:focus,
.campo textarea:focus,
.campo [aria-invalid="true"]:focus { box-shadow: none; }
.campo input:focus { background-size: 100% 2px; }
.campo textarea:focus { background-size: 100% 2px, 100% 2.25rem; }

.error {
  transition: opacity 220ms ease, translate 260ms var(--ease-salida);
  @starting-style { opacity: 0; translate: 0 -4px; }
}

.boton-texto.cambia { animation: difuminar 280ms ease; }
@keyframes difuminar { 50% { filter: blur(2px); opacity: 0.4; } }

.recibido-cabeza { display: flex; align-items: center; gap: 0.75rem; }
.visto-trazo { width: 2.6rem; height: auto; flex: none; overflow: visible; }
.visto-trazo path {
  fill: none;
  stroke: var(--verde);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.anim .recibido .recibido-mano {
  clip-path: inset(-0.3em 100% -0.3em -0.3em);
  animation: escribir 800ms var(--ease-salida) 80ms forwards;
}
.anim .recibido .visto-trazo path {
  stroke-dasharray: 60;
  stroke-dashoffset: 60;
  animation: trazar 460ms var(--ease-salida) 760ms forwards;
}
.anim .recibido > p:last-child {
  opacity: 0;
  animation: subir 480ms var(--ease-salida) 900ms forwards;
}
@keyframes trazar { to { stroke-dashoffset: 0; } }

/* ---------- Movimiento reducido: fundidos breves, sin desplazamientos ---------- */

@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(root),
  ::view-transition-new(root),
  html:active-view-transition-type(atras)::view-transition-old(root),
  html:active-view-transition-type(atras)::view-transition-new(root) {
    animation: 160ms ease both fundido;
    box-shadow: none;
  }
  ::view-transition-old(root) { animation-direction: reverse; }
  .boton:active, .pestana:active { transform: none; }
  .boton:hover::after { transform: none; }
}
@keyframes fundido { from { opacity: 0; } }

/* ---------- Calculadora: el resultado se escribe ---------- */

.anim .resultado.recien .cifra-numero {
  animation: numero-escrito 760ms var(--ease-salida) both;
}
.anim .resultado.recien [data-bloque="deficit"] .cifra-numero { animation-delay: 240ms; }
.anim .resultado.recien .resultado-mano {
  animation: nota-escrita 900ms var(--ease-salida) 620ms both;
}
@keyframes numero-escrito {
  from { clip-path: inset(-0.1em 100% -0.15em -0.05em); }
  to { clip-path: inset(-0.1em -0.05em -0.15em -0.05em); }
}
@keyframes nota-escrita {
  from { clip-path: inset(-0.4em 100% -0.4em -0.3em); }
  to { clip-path: inset(-0.4em -0.3em -0.4em -0.3em); }
}
.atras {
  text-decoration: none;
  background-image: linear-gradient(var(--verde), var(--verde)), linear-gradient(currentColor, currentColor);
  background-size: 0% 2px, 100% 1px;
  background-position: 0 85%, 0 85%;
  background-repeat: no-repeat;
  transition: background-size 280ms var(--ease-salida), color 180ms ease;
}
@media (hover: hover) and (pointer: fine) {
  .atras:hover { background-size: 100% 2px, 100% 1px; }
}
.ficha-calc { background-image: var(--grano-papel); background-size: 220px 220px; }

/* ---------- Sesión: los ejercicios se apuntan uno a uno ---------- */

.anim .sesion.recien .ejercicio {
  animation: apuntar 460ms var(--ease-salida) both;
  animation-delay: calc(300ms + var(--i, 0) * 70ms);
}
.anim .sesion.recien .ejercicio-clave {
  animation: nota-escrita 700ms var(--ease-salida) both;
  animation-delay: calc(520ms + var(--i, 0) * 70ms);
}
@keyframes apuntar { from { opacity: 0; translate: 0 8px; } }
