(()=>{
const {useState,useEffect,useRef}=React;
const clamp=(v,a,b)=>Math.max(a,Math.min(b,v));
const reduced=matchMedia('(prefers-reduced-motion: reduce)').matches;
function usePin(ref){
  const [p,setP]=useState(0);
  useEffect(()=>{let raf=0;const f=()=>{const el=ref.current;if(!el)return;const r=el.getBoundingClientRect();const t=r.height-innerHeight;setP(t>0?clamp(-r.top/t,0,1):0)};
    const on=()=>{cancelAnimationFrame(raf);raf=requestAnimationFrame(f)};f();addEventListener('scroll',on,{passive:true});addEventListener('resize',on);
    return()=>{removeEventListener('scroll',on);removeEventListener('resize',on);cancelAnimationFrame(raf)}},[]);
  return p;
}
function useInView(ref,th=.3){
  const [v,setV]=useState(false);
  useEffect(()=>{const io=new IntersectionObserver(([e])=>setV(e.isIntersecting),{threshold:th});ref.current&&io.observe(ref.current);return()=>io.disconnect()},[]);
  return v;
}
function useTick(ms,on=true){
  const [t,setT]=useState(0);
  useEffect(()=>{if(!on)return;const id=setInterval(()=>setT(x=>x+1),ms);return()=>clearInterval(id)},[ms,on]);
  return t;
}
function Particles(){
  const ref=useRef();
  useEffect(()=>{const c=ref.current,ctx=c.getContext('2d');const dpr=Math.min(2,devicePixelRatio||1);let w,h,raf;
    const N=matchMedia('(pointer: coarse)').matches?18:46;
    const size=()=>{w=innerWidth;h=innerHeight;c.width=w*dpr;c.height=h*dpr;ctx.setTransform(dpr,0,0,dpr,0,0)};size();
    const ps=Array.from({length:N},()=>({x:Math.random()*w,y:Math.random()*h,z:Math.random(),vx:(Math.random()-.5)*.08,vy:-(.03+Math.random()*.12),t:Math.random()*6}));
    const draw=()=>{ctx.clearRect(0,0,w,h);for(const p of ps){p.x+=p.vx;p.y+=p.vy;p.t+=.012;if(p.y<-5){p.y=h+5;p.x=Math.random()*w}
      ctx.beginPath();ctx.arc(p.x,p.y,.5+p.z*1.3,0,7);ctx.fillStyle='rgba(120,190,255,'+(.12+.3*p.z)*(.6+.4*Math.sin(p.t))+')';ctx.fill()}
      if(!reduced&&!document.hidden)raf=requestAnimationFrame(draw)};draw();document.addEventListener('visibilitychange',()=>{cancelAnimationFrame(raf);if(!document.hidden)draw()});addEventListener('resize',size);
    return()=>{cancelAnimationFrame(raf);removeEventListener('resize',size)}},[]);
  return <canvas ref={ref} className="nx-bgfield" aria-hidden="true"></canvas>;
}
function Cursor(){
  const b=useRef(),eyes=useRef();
  useEffect(()=>{if(!matchMedia('(any-pointer: fine)').matches)return;
    let x=-200,y=-200,bx=-200,by=-200,raf;b.current.style.opacity=0;
    const mv=e=>{x=e.clientX;y=e.clientY;b.current.style.opacity=1;const t=e.target.closest&&e.target.closest('a,button,[role=button],.cap,.wk-item,input,textarea,select');b.current.classList.toggle('big',!!t)};
    const out=()=>{b.current.style.opacity=0};
    const loop=()=>{const dx=x-bx,dy=y-by;bx+=dx*.14;by+=dy*.14;const tilt=Math.max(-14,Math.min(14,dx*.25));
      b.current.style.transform='translate('+bx+'px,'+by+'px) rotate('+tilt+'deg)';
      eyes.current.setAttribute('transform','translate('+Math.max(-2,Math.min(2,dx*.05))+','+Math.max(-1.5,Math.min(1.5,dy*.05))+')');raf=requestAnimationFrame(loop)};loop();
    addEventListener('mousemove',mv);document.addEventListener('mouseleave',out);return()=>{removeEventListener('mousemove',mv);document.removeEventListener('mouseleave',out);cancelAnimationFrame(raf)}},[]);
  return <div ref={b} className="nx-bot" aria-hidden="true"><svg viewBox="0 0 34 38">
    <line x1="17" y1="3" x2="17" y2="9" stroke="#4DB8FF" strokeWidth="1.2"/><circle className="ant" cx="17" cy="3" r="2" fill="#8FD3FF"/>
    <polygon points="4,9 27,9 31,13 31,30 8,30 4,26" fill="#0A1421" stroke="#4DB8FF" strokeWidth="1.2"/>
    <rect x="1" y="16" width="3" height="7" fill="#16273B" stroke="#4DB8FF" strokeWidth=".8"/><rect x="31" y="16" width="3" height="7" fill="#16273B" stroke="#4DB8FF" strokeWidth=".8"/>
    <g ref={eyes}><rect className="eye" x="10" y="16" width="4" height="5"/><rect className="eye" x="20" y="16" width="4" height="5"/></g>
    <line x1="13" y1="25" x2="21" y2="25" stroke="#94A8BC" strokeWidth="1"/>
    <polygon points="11,32 23,32 21,37 13,37" fill="#16273B" stroke="rgba(77,184,255,.6)" strokeWidth=".8"/></svg></div>;
}
function Stage({children,height=540,rx=10,ry=-20}){
  const [m,setM]=useState({x:0,y:0});
  const mv=e=>{const b=e.currentTarget.getBoundingClientRect();setM({x:(e.clientX-b.left)/b.width-.5,y:(e.clientY-b.top)/b.height-.5})};
  return <div className="stg" style={{height}} onMouseMove={mv} onMouseLeave={()=>setM({x:0,y:0})}>
    <div className="stg-floor"></div><div className="stg-glow"></div>
    <div className="stg-scale"><div className="stg-sway"><div className="stg-world" style={{transform:'rotateX('+(rx-m.y*10)+'deg) rotateY('+(ry+m.x*14)+'deg)'}}>{children}</div></div></div></div>;
}
function Plane({x=0,y=0,z=0,w,h,className='',style,children}){
  return <div className={'pl '+className} style={{width:w,height:h,transform:'translate(-50%,-50%) translate3d('+x+'px,'+y+'px,'+z+'px)',...style}}>{children}</div>;
}
function Sk({w='100%',h=6,b,style}){return <i className={'sk'+(b?' b':'')} style={{width:w,height:h,...style}}></i>}
function scrollToId(id){const el=document.getElementById(id);if(el)window.scrollTo({top:el.getBoundingClientRect().top+scrollY-(id==='top'?0:56),behavior:'smooth'})}
const openForm=()=>dispatchEvent(new CustomEvent('nx-form'));
window.NXH={openForm,usePin,useInView,useTick,Particles,Cursor,Stage,Plane,Sk,scrollToId,clamp,reduced};
})();
