// Animación de entrada de Rodando (17/11/2026): del logo de Salvallantas al ícono escalonado.
// "Las curvas se cierran en pin" y al final la franja del medio se adelanta.
// Se muestra la primera vez del día en cada equipo; se salta con "reducir movimiento" o tocando la pantalla.
(function () {
const BANDS='<path d="M237.79,121.48c0-41.66-33.31-75.07-76.31-75.07H1.52A38.28,38.28,0,0,0,34.85,75.67H160.48c27.66,0,47.82,20.55,47.82,46.57v.26h29.5c0-.29,0-.59,0-.87Z"/><path d="M161.48,0h-130A38.32,38.32,0,0,0,1.52,28.76h153q3.44-.26,6.94-.26a93.74,93.74,0,0,1,93.74,93.74v.26h28.5v-.26A122.24,122.24,0,0,0,161.48,0Z"/><path d="M168.75,93.29H148.39a9.37,9.37,0,0,1-1.73,0H25.33c-13.54,0-23.27,12-24.84,25.36v3.85h189.9v-4.07C189,106.15,180.81,93.29,168.75,93.29Z"/>';
const ENV='M1.52,1 H161.48 A121.2,121.2 0 0 1 282.68,122.5 H0.49 Z';
const PR={r:54,cy:34};
function pinPath(cx){const cy=PR.cy,r=PR.r,T=[cx,122.5],dd=T[1]-cy,al=Math.acos(r/dd),f=n=>+n.toFixed(2);
 return {d:`M${T[0]},${T[1]} L${f(cx+r*Math.sin(al))},${f(cy+r*Math.cos(al))} A${r},${r} 0 1 0 ${f(cx-r*Math.sin(al))},${f(cy+r*Math.cos(al))} Z`,cx,cy}}
function rectaLogo(i,x,y,xr){
 if(i===0)return `M${x},${y+28.76} A38.32,38.32 0 0 1 ${x+29.96},${y} H${xr} V${y+28.76} Z`;
 if(i===1)return `M${x},${y} H${xr} V${y+29.26} H${x+33.33} A38.28,38.28 0 0 1 ${x},${y} Z`;
 return `M${x+24.84},${y} H${xr} V${y+29.21} H${x} V${y+25.36} C${x+1.57},${y+12} ${x+11.3},${y} ${x+24.84},${y} Z`}
const P=pinPath(215),DX=6,HALO=13;
const BOTTOM_END=(xl=0.49)=>`M${xl+24.84},93.29 H${168.75+DX} C${180.81+DX},93.29 ${189+DX},106.15 ${190.39+DX},118.43 V122.5 H${xl} V${93.29+25.36} C${xl+1.57},${93.29+12} ${xl+11.3},93.29 ${xl+24.84},93.29 Z`;
const STRIPES=(xs,id='')=>`<path d="${rectaLogo(0,xs[0],0,215)}" clip-path="url(#${id}e)"/><path d="${rectaLogo(1,xs[1],46.41,215)}" clip-path="url(#${id}m)"/>`;
const ENVM='M-900,46.41 H161.48 A76.3,76.3 0 0 1 237.78,122.5 H-900 Z';
// Contorno de la S (para que las franjas no asomen por encima de la curva de afuera)
const ENVC='M-900,0 H161.48 A122.24,122.24 0 0 1 283.72,122.5 H-900 Z';
const easeI={out:t=>1-Math.pow(1-t,3),inOut:t=>t<.5?4*t*t*t:1-Math.pow(-2*t+2,3)/2,
 bounce:t=>{const n=7.5625,d=2.75;if(t<1/d)return n*t*t;if(t<2/d)return n*(t-=1.5/d)*t+.75;if(t<2.5/d)return n*(t-=2.25/d)*t+.9375;return n*(t-=2.625/d)*t+.984375},
 back:t=>{const c=1.70158,c3=c+1;return 1+c3*Math.pow(t-1,3)+c*Math.pow(t-1,2)}};
const cl=(x)=>Math.max(0,Math.min(1,x)),seg=(t,a,b)=>cl((t-a)/(b-a)),lerp=(a,b,k)=>a+(b-a)*k;
let uidA=0;
function cuadro(st,conTexto=true){const id='a'+(++uidA),c=st.color||'#D9302A';
 const W=conTexto?790:286,X0=-30,Y0=-60,H=200;
 const pinG=`<g transform="${st.pinT||''}"><path d="${P.d}"/></g>`;
 const halo=`<g transform="${st.pinT||''}"><path d="${P.d}" fill="#000" stroke="#000" stroke-width="${2*HALO}" stroke-linejoin="round"/></g>`;
 const clip=(st.clipR!=null?`<clipPath id="${id}c"><circle cx="205" cy="70" r="${st.clipR}"/></clipPath>`:'')+`<clipPath id="${id}e"><path d="${ENVC}"/></clipPath><clipPath id="${id}m"><path d="${ENVM}"/></clipPath><clipPath id="${id}r"><rect x="161" y="-300" width="600" height="600"/></clipPath>`;
 const m=(n,inner,base='#fff')=>`<mask id="${id}${n}" maskUnits="userSpaceOnUse" x="${X0-400}" y="${Y0-400}" width="${W+1200}" height="${H+800}"><rect x="${X0-400}" y="${Y0-400}" width="${W+1200}" height="${H+800}" fill="${base}"/>${inner}</mask>`;
 const mHalo=m('h',st.pinOn?(st.clipR!=null?`<g clip-path="url(#${id}c)">${halo}</g>`:halo):'');
 const mPin=m('p',`<g transform="translate(${st.sdx||0},0)"><path d="${ENV}" fill="#000"/><g fill="#fff">${BANDS}</g></g><circle cx="215" cy="34" r="19.44" fill="#000" transform="${st.pinT||''}"/>`);
 const xs=st.xs||[1.52,1.52,0.49];
 const grupo=`<g transform="translate(${st.gx||0},0)">
   <g mask="url(#${id}h)">${STRIPES(xs,id)}${st.arcs>0?`<g opacity="${st.arcs}" clip-path="url(#${id}r)">${BANDS}</g>`:''}</g>
   <g mask="url(#${id}h)"><path d="${BOTTOM_END(xs[2])}"/></g>
   ${st.pinOn?`<g mask="url(#${id}p)">${st.clipR!=null?`<g clip-path="url(#${id}c)">${pinG}</g>`:pinG}</g>`:''}</g>`;
 const txt=conTexto?`<text x="${320+lerp(30,0,st.txt||0)}" y="98" font-family="Inter,system-ui,sans-serif" font-weight="800" font-size="96" letter-spacing="-3" fill="${c}" opacity="${st.txt||0}">Rodando</text>`:'';
 return `<svg viewBox="${X0} ${Y0} ${W} ${H}" width="100%" height="100%" fill="${c}" style="max-height:200px"><defs>${clip}${mHalo}${mPin}</defs>${grupo}${txt}</svg>`}

const ESC=[46,1.5,70],ALI=[1.52,1.52,0.49];
const mix=(A,B,k)=>A.map((v,i)=>lerp(v,B[i],k));
// Animación 2 (las curvas se cierran en pin), parametrizada: franjas de inicio y fin, y tiempos
function anim2(t,t0){const k=easeI.inOut(seg(t,t0+.4,t0+1.35));const a=1-seg(t,t0+.9,t0+1.45);
 return {pinOn:t>t0+.4,clipR:lerp(0,170,k),pinT:`translate(215,122.5) scale(1,${lerp(.82,1,easeI.back(seg(t,t0+1.0,t0+1.5)))}) translate(-215,-122.5)`,arcs:a,sdx:lerp(0,DX,seg(t,t0+.9,t0+1.45))}}

// Sigue al interruptor de logo.jsx: escalonado = "franjas al final"; alineado = animación 2 directa.
const ALINEADO = window.RODANDO_VARIANTE === 'alineado';
const INTRO_DUR = ALINEADO ? 2.4 : 2.8;
function estadoIntro(t) {
  const st = anim2(t, 0);
  if (ALINEADO) { st.xs = ALI; st.txt = easeI.out(seg(t, 1.5, 2.05)); return st; }
  st.xs = mix(ALI, ESC, easeI.back(seg(t, 1.4, 1.95)));
  st.txt = easeI.out(seg(t, 1.8, 2.35));
  return st;
}
const LS_INTRO = 'rodando.introDia';
function hoyIntro() { const d = new Date(); return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate(); }
function debeMostrarIntro() {
  try {
    if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) return false;
    return localStorage.getItem(LS_INTRO) !== hoyIntro();
  } catch (e) { return false; }
}
function RodandoIntro() {
  const [show, setShow] = React.useState(debeMostrarIntro);
  const [saliendo, setSaliendo] = React.useState(false);
  const ref = React.useRef(null);
  const cerrar = React.useCallback(() => {
    try { localStorage.setItem(LS_INTRO, hoyIntro()); } catch (e) {}
    setSaliendo(true);
    setTimeout(() => setShow(false), 380);
  }, []);
  React.useEffect(() => {
    if (!show) return undefined;
    let raf = 0, fin = false;
    const t0 = performance.now();
    const paso = (now) => {
      const t = Math.min((now - t0) / 1000, INTRO_DUR);
      if (ref.current) ref.current.innerHTML = cuadro({ ...estadoIntro(t), color: '#fff' }, true).replace('max-height:200px', 'max-height:none');
      if (t < INTRO_DUR) raf = requestAnimationFrame(paso);
      else if (!fin) { fin = true; setTimeout(cerrar, 450); }
    };
    raf = requestAnimationFrame(paso);
    return () => cancelAnimationFrame(raf);
  }, [show, cerrar]);
  if (!show) return null;
  return (
    <div onClick={cerrar} role="presentation" style={{ position: 'fixed', inset: 0, zIndex: 100000, background: '#D9302A', display: 'flex', alignItems: 'center', justifyContent: 'center', opacity: saliendo ? 0 : 1, transition: 'opacity .35s ease', cursor: 'pointer' }}>
      <div ref={ref} style={{ width: 'min(92vw, 820px)' }} />
    </div>);
}
window.RodandoIntro = RodandoIntro;
window.rodandoIntroCuadro = (t, color) => cuadro({ ...estadoIntro(t), color: color || '#fff' }, true);
})();
