/* ============================================================
   BARRA · motion
   Un solo sistema de movimiento: entrada orquestada al cargar,
   revelados ligados al scroll, y transiciones de vista al
   cambiar de herramienta. Todo se apaga con reduced-motion.
   ============================================================ */

/* ---------------------------------------------------------------
   1 · REVELADO POR SCROLL
   Preferimos scroll-driven animations nativas (animation-timeline).
   Donde no existan, un IntersectionObserver pone .in y usamos
   transiciones. Los dos caminos comparten el mismo aspecto final.
   --------------------------------------------------------------- */
[data-reveal] {
  --rv-y: 26px;
  --rv-blur: 6px;
  --rv-scale: 1;
  opacity: 0;
  transform: translate3d(0, var(--rv-y), 0) scale(var(--rv-scale));
  filter: blur(var(--rv-blur));
  transition:
    opacity var(--d-lg) var(--ease-out),
    transform var(--d-lg) var(--ease-out),
    filter var(--d-md) var(--ease-out);
  transition-delay: var(--rv-delay, 0ms);
  will-change: opacity, transform;
}
[data-reveal].in {
  opacity: 1;
  transform: none;
  filter: blur(0);
}
[data-reveal="left"] { --rv-y: 0; transform: translate3d(-34px, 0, 0); }
[data-reveal="right"] { --rv-y: 0; transform: translate3d(34px, 0, 0); }
[data-reveal="rise"] { --rv-y: 54px; --rv-scale: 0.97; }
[data-reveal="pop"] { --rv-y: 12px; --rv-scale: 0.9; --rv-blur: 2px; }
[data-reveal="flat"] { --rv-y: 0; --rv-blur: 0; }

/* Escalonado declarativo: el padre reparte el retardo a sus hijos. */
[data-stagger] > * { --rv-delay: calc(var(--i, 0) * var(--stagger-step, 70ms)); }

/* ---------------------------------------------------------------
   2 · ENTRADA DEL HÉROE
   Secuencia única al cargar. Cada línea entra desde abajo con
   máscara, como un título de keynote.
   --------------------------------------------------------------- */
@keyframes lineUp {
  from { transform: translate3d(0, 108%, 0) rotate(1.6deg); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes fadeUp {
  from { transform: translate3d(0, 22px, 0); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes scaleIn {
  from { transform: scale(0.86) translateY(30px); opacity: 0; filter: blur(10px); }
  to { transform: none; opacity: 1; filter: blur(0); }
}
@keyframes haloBreathe {
  0%, 100% { opacity: 0.85; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.07); }
}
@keyframes drift {
  0%, 100% { transform: translate3d(0, 0, 0); }
  50% { transform: translate3d(0, -14px, 0); }
}
@keyframes shimmer {
  from { background-position: -140% 0; }
  to { background-position: 240% 0; }
}
@keyframes steamRise {
  0% { transform: translateY(6px) scaleX(0.85); opacity: 0; }
  28% { opacity: 0.5; }
  100% { transform: translateY(-40px) scaleX(1.5); opacity: 0; }
}
@keyframes pourFill {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}
@keyframes spinSlow { to { transform: rotate(360deg); } }

.mask-line { display: block; overflow: hidden; padding-bottom: 0.06em; }
.mask-line > span { display: block; }

.hero-seq .mask-line > span {
  animation: lineUp 1150ms var(--ease-out) both;
  animation-delay: calc(160ms + var(--i, 0) * 110ms);
}
.hero-seq [data-seq] {
  animation: fadeUp 900ms var(--ease-out) both;
  animation-delay: calc(420ms + var(--seq, 0) * 110ms);
}
.hero-seq [data-seq="stage"] {
  animation: scaleIn 1500ms var(--ease-out) both;
  animation-delay: 520ms;
}

/* ---------------------------------------------------------------
   3 · SECCIÓN FIJADA (el "scrollytelling" de Apple)
   El bloque se queda pegado y las capas cambian con el scroll.
   --------------------------------------------------------------- */
.pin {
  position: relative;
  /* La altura extra es lo que dura el pin. */
  height: 320vh;
}
.pin__stage {
  position: sticky;
  top: 0;
  height: 100dvh;
  display: grid;
  place-items: center;
  overflow: clip;
}
.pin__layer {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
  transform: scale(0.94) translateY(20px);
  transition: opacity var(--d-md) var(--ease-out), transform var(--d-lg) var(--ease-out);
  pointer-events: none;
  padding: var(--sp-5);
}
.pin__layer.on {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}
.pin__rail {
  position: absolute;
  left: 50%;
  bottom: clamp(18px, 4vh, 42px);
  transform: translateX(-50%);
  display: flex;
  gap: 7px;
  z-index: 3;
}
.pin__rail i {
  width: 30px; height: 3px; border-radius: 99px;
  background: var(--line-2);
  transition: background-color var(--d-sm) var(--ease-out), width var(--d-sm) var(--ease-out);
}
.pin__rail i.on { background: var(--acc); width: 46px; }

/* ---------------------------------------------------------------
   4 · BRILLO QUE RECORRE (bordes y textos de énfasis)
   --------------------------------------------------------------- */
.sweep {
  position: relative;
  overflow: hidden;
  isolation: isolate;
}
.sweep::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 34%, rgba(255, 255, 255, 0.16) 50%, transparent 66%);
  background-size: 240% 100%;
  animation: shimmer 3.6s var(--ease-in-out) infinite;
  pointer-events: none;
  z-index: 2;
}
:root[data-theme="light"] .sweep::after {
  background: linear-gradient(100deg, transparent 34%, rgba(255, 255, 255, 0.7) 50%, transparent 66%);
  background-size: 240% 100%;
}

/* ---------------------------------------------------------------
   5 · TRANSICIÓN ENTRE VISTAS DE HERRAMIENTA
   --------------------------------------------------------------- */
@keyframes viewIn {
  from { opacity: 0; transform: translate3d(0, 16px, 0) scale(0.985); }
  to { opacity: 1; transform: none; }
}
.view-enter { animation: viewIn var(--d-md) var(--ease-out) both; }

/* View Transitions nativas donde existan */
@media (prefers-reduced-motion: no-preference) {
  ::view-transition-old(tool) { animation: fadeIn var(--d-sm) var(--ease-in-out) reverse both; }
  ::view-transition-new(tool) { animation: viewIn var(--d-md) var(--ease-out) both; }
}

/* Salida y entrada del escenario cuando se abre la app */
@keyframes stageOut {
  to { opacity: 0; transform: scale(1.06) translateY(-30px); filter: blur(14px); }
}
@keyframes appIn {
  from { opacity: 0; transform: scale(0.97) translateY(24px); }
  to { opacity: 1; transform: none; }
}
.leaving { animation: stageOut var(--d-md) var(--ease-in-out) both; pointer-events: none; }
.arriving { animation: appIn var(--d-lg) var(--ease-out) both; }

/* ---------------------------------------------------------------
   6 · MICRO-INTERACCIONES
   --------------------------------------------------------------- */
.lift {
  transition: transform var(--d-sm) var(--ease-out), box-shadow var(--d-sm) var(--ease-out),
              border-color var(--d-sm) var(--ease-out);
}
.lift:hover { transform: translateY(-4px); box-shadow: var(--shadow-3); }
.lift:active { transform: translateY(-1px) scale(0.995); }

/* Reflejo que sigue al cursor: la posición la pone JS en --mx/--my */
.glow { position: relative; isolation: isolate; }
.glow::before {
  content: "";
  position: absolute; inset: -1px;
  border-radius: inherit;
  background: radial-gradient(220px circle at var(--mx, 50%) var(--my, 0%),
    var(--acc-soft), transparent 68%);
  opacity: 0;
  transition: opacity var(--d-sm) var(--ease-out);
  pointer-events: none;
  z-index: -1;
}
.glow:hover::before { opacity: 1; }

.breathe { animation: haloBreathe 7s var(--ease-in-out) infinite; }
.drift { animation: drift 6.5s var(--ease-in-out) infinite; }

/* Contador: JS interpola el número, el CSS sólo evita el salto de ancho */
.count { font-variant-numeric: tabular-nums; }

/* Barra de progreso animada por --p (0..1) */
.meter { height: 6px; border-radius: 99px; background: var(--surface-3); overflow: hidden; }
.meter > i {
  display: block; height: 100%; border-radius: 99px;
  background: linear-gradient(90deg, var(--acc-lo), var(--acc));
  transform-origin: left center;
  transform: scaleX(var(--p, 0));
  transition: transform var(--d-md) var(--ease-out);
}
.meter.data > i { background: linear-gradient(90deg, var(--acero-500), var(--data)); }
.meter.ok > i { background: linear-gradient(90deg, #0F6B42, var(--ok)); }
.meter.grave > i { background: linear-gradient(90deg, #8A2A22, var(--grave)); }

/* Anillo de progreso (temporizadores) */
.ring { transform: rotate(-90deg); }
.ring circle { fill: none; stroke-linecap: round; }
.ring .track { stroke: var(--surface-3); }
.ring .bar {
  stroke: var(--acc);
  transition: stroke-dashoffset var(--d-sm) linear, stroke var(--d-sm) var(--ease-out);
}

/* Vapor decorativo sobre la taza del héroe */
.steam span {
  animation: steamRise 3.4s var(--ease-in-out) infinite;
}
.steam span:nth-child(2) { animation-delay: 0.9s; }
.steam span:nth-child(3) { animation-delay: 1.8s; }

/* ---------------------------------------------------------------
   6b · CAPA EXTRA DE MOVIMIENTO
   Detalles que hacen que la interfaz se sienta viva sin distraer.
   --------------------------------------------------------------- */

/* Inclinación 3D de las tarjetas al pasar el cursor. Las variables
   --tx / --ty las escribe JS a partir de la posición del puntero. */
.tilt {
  transform: perspective(900px)
             rotateX(calc(var(--ty, 0) * -4deg))
             rotateY(calc(var(--tx, 0) * 4deg))
             translateZ(0);
  transition: transform 260ms var(--ease-out), box-shadow 260ms var(--ease-out);
  transform-style: preserve-3d;
}
.tilt:hover { box-shadow: var(--shadow-3); }

/* Botón que se acerca ligeramente al cursor. */
.magnet {
  transform: translate3d(calc(var(--mgx, 0) * 1px), calc(var(--mgy, 0) * 1px), 0);
  transition: transform 220ms var(--ease-out);
}

/* Entrada de una fila nueva en una lista (comanda, registro, aviso). */
@keyframes filaEntra {
  from { opacity: 0; transform: translate3d(-10px, 0, 0); }
  to { opacity: 1; transform: none; }
}
.list__row { animation: filaEntra 260ms var(--ease-out) both; }
.list__row:nth-child(n + 14) { animation: none; }

/* Latido para lo que exige atención ya: temporizador acabado,
   cámara fuera de rango, mesa esperando la cuenta. */
@keyframes latido {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 107, 94, 0.42); }
  60% { box-shadow: 0 0 0 12px rgba(255, 107, 94, 0); }
}
.urgente { animation: latido 1.9s var(--ease-out) infinite; }

/* Anillo de sincronización girando en el avatar. */
@keyframes gira { to { transform: rotate(360deg); } }
.icon-btn.girando::after {
  content: "";
  position: absolute; inset: -3px;
  border-radius: 50%;
  border: 2px solid transparent;
  border-top-color: var(--acc);
  animation: gira 900ms linear infinite;
}

/* Barra de progreso del scroll de la portada. */
.progreso {
  position: fixed; top: 0; left: 0; right: 0; height: 2px;
  z-index: calc(var(--z-nav) + 1);
  background: linear-gradient(90deg, var(--cobre-300), var(--acero-300));
  transform-origin: left center;
  transform: scaleX(var(--p, 0));
  pointer-events: none;
}
body.is-app .progreso { display: none; }

/* Aparición en cascada de las tarjetas del hub de la app. */
@keyframes hubEntra {
  from { opacity: 0; transform: translate3d(0, 14px, 0) scale(0.97); }
  to { opacity: 1; transform: none; }
}
.hub > * {
  animation: hubEntra 460ms var(--ease-out) both;
  animation-delay: calc(var(--i, 0) * 28ms);
}

/* Cifras que cambian: un destello corto para que se note. */
@keyframes destello {
  0% { background-color: var(--acc-soft); }
  100% { background-color: transparent; }
}
.destella { animation: destello 700ms var(--ease-out); border-radius: var(--r-sm); }

/* Marca de agua animada del héroe: un degradado que respira. */
@keyframes mallaGira {
  0% { transform: rotate(0deg) scale(1); }
  50% { transform: rotate(180deg) scale(1.16); }
  100% { transform: rotate(360deg) scale(1); }
}
.malla {
  position: absolute;
  inset: -40%;
  background:
    radial-gradient(closest-side at 32% 38%, var(--cobre-500), transparent 68%),
    radial-gradient(closest-side at 68% 56%, var(--acero-500), transparent 66%),
    radial-gradient(closest-side at 52% 76%, var(--cobre-300), transparent 60%);
  opacity: 0.20;
  filter: blur(58px);
  animation: mallaGira 34s linear infinite;
}
:root[data-theme="light"] .malla { opacity: 0.16; }

/* ---------------------------------------------------------------
   7 · RESPETO POR REDUCED MOTION
   Se conserva el contraste y la jerarquía; se quita el movimiento.
   --------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  [data-reveal] { opacity: 1 !important; transform: none !important; filter: none !important; }
  .pin { height: auto !important; }
  .pin__stage { position: static; height: auto; display: block; }
  .pin__layer { position: static; opacity: 1; transform: none; pointer-events: auto; }
  .pin__layer + .pin__layer { margin-top: var(--sp-7); border-top: 1px solid var(--line); padding-top: var(--sp-7); }
  .pin__rail { display: none; }
  .sweep::after { display: none; }
  .tilt, .magnet { transform: none !important; }
  .malla { animation: none; }
  .urgente { animation: none; outline: 2px solid var(--grave); }
  .list__row, .hub > * { animation: none; }
}
