const INTRO_PIXELS = Array.from({ length: 18 }, (_, i) => {
  const a = (i / 18) * Math.PI * 2 + (i % 3) * 0.4;
  const r = 260 + (i % 5) * 70;
  return { x: Math.cos(a) * r, y: Math.sin(a) * r * 0.7, z: 300 + (i % 4) * 160, s: 8 + (i % 4) * 5, d: (i % 6) * 0.05, c: ['#7C3AED', '#2563EB', '#06B6D4'][i % 3] };
});

function Intro({ onDone }) {
  const reduce = useReducedMotion();
  const [leaving, setLeaving] = React.useState(false);
  const total = reduce ? 700 : 3300;
  React.useEffect(() => {
    document.documentElement.style.overflow = 'hidden';
    const t1 = setTimeout(() => setLeaving(true), total - 650);
    const t2 = setTimeout(() => { document.documentElement.style.overflow = ''; onDone(); }, total);
    return () => { clearTimeout(t1); clearTimeout(t2); document.documentElement.style.overflow = ''; };
  }, []);
  const skip = () => { document.documentElement.style.overflow = ''; onDone(); };
  const word = 'Yantrrk'.split('');
  return (
    <div onClick={skip} role="presentation" style={{ position: 'fixed', inset: 0, zIndex: 1000, display: 'grid', placeItems: 'center', background: 'var(--mesh-bg)', overflow: 'hidden', cursor: 'pointer', opacity: leaving ? 0 : 1, filter: leaving ? 'blur(8px)' : 'none', transition: 'opacity .6s cubic-bezier(.22,1,.36,1), filter .6s' }}>
      <style>{`
        @keyframes yk-in-spin { 0% { transform: rotateY(-120deg) rotateX(28deg) scale(.55); opacity: 0 } 60% { opacity: 1 } 100% { transform: rotateY(0) rotateX(0) scale(1); opacity: 1 } }
        @keyframes yk-in-float { 0%,100% { transform: rotateY(-8deg) rotateX(6deg) } 50% { transform: rotateY(8deg) rotateX(-4deg) } }
        @keyframes yk-in-pixel { 0% { opacity: 0; transform: translate3d(var(--x), var(--y), var(--z)) rotate(120deg) } 30% { opacity: 1 } 100% { opacity: 0; transform: translate3d(0,0,0) rotate(0) scale(.3) } }
        @keyframes yk-in-letter { from { opacity: 0; transform: translateY(26px) rotateX(-70deg) } to { opacity: 1; transform: none } }
        @keyframes yk-in-fade { from { opacity: 0; transform: translateY(8px) } to { opacity: 1; transform: none } }
        @keyframes yk-in-ring { from { transform: scale(.4); opacity: .7 } to { transform: scale(2.2); opacity: 0 } }
        @keyframes yk-in-blob { 0%,100% { transform: translate(0,0) } 50% { transform: translate(30px,-24px) } }
        @keyframes yk-in-bar { from { transform: scaleX(0) } to { transform: scaleX(1) } }
      `}</style>
      <div aria-hidden style={{ position: 'absolute', width: 520, height: 520, borderRadius: '50%', right: '-8%', top: '-12%', background: 'radial-gradient(circle, rgba(124,58,237,.28), transparent 65%)', animation: reduce ? 'none' : 'yk-in-blob 6s ease-in-out infinite' }} />
      <div aria-hidden style={{ position: 'absolute', width: 460, height: 460, borderRadius: '50%', left: '-6%', bottom: '-14%', background: 'radial-gradient(circle, rgba(6,182,212,.24), transparent 65%)', animation: reduce ? 'none' : 'yk-in-blob 7s ease-in-out infinite reverse' }} />
      <div style={{ position: 'relative', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 22, padding: '44px 56px 36px', borderRadius: 28, background: 'var(--glass-bg)', border: '1px solid var(--glass-border)', backdropFilter: 'var(--glass-blur)', WebkitBackdropFilter: 'var(--glass-blur)', boxShadow: 'var(--glass-shadow)', transform: leaving ? 'scale(1.06)' : 'none', transition: 'transform .6s cubic-bezier(.22,1,.36,1)', animation: reduce ? 'none' : 'yk-in-fade .5s cubic-bezier(.22,1,.36,1) both' }}>
        <div style={{ position: 'relative', width: 150, height: 130, perspective: 900 }}>
          {!reduce && <span aria-hidden style={{ position: 'absolute', inset: -10, borderRadius: '50%', border: '2px solid rgba(124,58,237,.35)', animation: 'yk-in-ring 1.1s 1.05s cubic-bezier(.22,1,.36,1) both' }} />}
          {!reduce && INTRO_PIXELS.map((p, i) => <span key={i} aria-hidden style={{ position: 'absolute', left: '50%', top: '50%', width: p.s, height: p.s, marginLeft: -p.s / 2, marginTop: -p.s / 2, borderRadius: 3, background: p.c, '--x': p.x + 'px', '--y': p.y + 'px', '--z': p.z + 'px', animation: `yk-in-pixel 1s ${0.15 + p.d}s cubic-bezier(.55,0,.25,1) both` }} />)}
          <div style={{ position: 'absolute', inset: 0, transformStyle: 'preserve-3d', animation: reduce ? 'none' : 'yk-in-spin 1.2s .1s cubic-bezier(.22,1,.36,1) both' }}>
            <div style={{ position: 'absolute', inset: 0, transformStyle: 'preserve-3d', animation: reduce ? 'none' : 'yk-in-float 3s 1.3s ease-in-out infinite' }}>
              {Array.from({ length: 9 }, (_, i) => <img key={i} src={(window.__resources && window.__resources.logoSymbol) || '../../assets/logo-symbol.png'} alt={i === 8 ? 'Yantrrk' : ''} aria-hidden={i !== 8} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'contain', transform: `translateZ(${(i - 8) * 1.6}px)`, filter: i < 8 ? 'brightness(.55) saturate(1.2)' : 'none' }} />)}
            </div>
          </div>
        </div>
        <div style={{ display: 'flex', perspective: 600 }}>
          {word.map((ch, i) => <span key={i} style={{ font: '800 46px/1 var(--font-display)', letterSpacing: '-0.03em', color: 'var(--yk-indigo)', display: 'inline-block', animation: reduce ? 'none' : `yk-in-letter .55s ${1.15 + i * 0.06}s cubic-bezier(.22,1,.36,1) both` }}>{ch}</span>)}
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12, animation: reduce ? 'none' : 'yk-in-fade .5s 1.7s cubic-bezier(.22,1,.36,1) both' }}>
          <span style={{ font: '500 12px var(--font-body)', letterSpacing: '.22em', textTransform: 'uppercase', color: 'var(--yk-charcoal)' }}>Automate • Connect • Grow</span>
          <span style={{ width: 160, height: 2, borderRadius: 2, background: 'rgba(124,58,237,.12)', overflow: 'hidden' }}><span style={{ display: 'block', height: '100%', background: 'linear-gradient(90deg,#7C3AED,#2563EB,#06B6D4)', transformOrigin: 'left', animation: reduce ? 'none' : 'yk-in-bar 1.2s 1.75s cubic-bezier(.65,0,.35,1) both' }} /></span>
        </div>
      </div>
      <span style={{ position: 'absolute', bottom: 24, font: '500 11px var(--font-mono)', color: 'var(--yk-charcoal)', opacity: 0.7 }}>Click to skip</span>
    </div>
  );
}

Object.assign(window, { Intro });
