(()=>{
const NX=window.NURVEXAIDesignSystem_4750bb;const {useState,useEffect,useRef}=React;
// Paste your Google Apps Script Web App URL here (see google-sheets/SETUP.md)
const SHEET_URL='https://script.google.com/macros/s/AKfycbw_FIrx24aDBMaeV-UwLufdPButAuSwD-0zEXf0Ib0LpyAQT_1g31BGcRnhTwp1luTr/exec';
const saveLead=d=>{if(!SHEET_URL)return Promise.resolve();const b=new URLSearchParams({...d,page:document.title,url:location.href,time:new Date().toISOString()});
  return fetch(SHEET_URL,{method:'POST',mode:'no-cors',body:b}).catch(()=>{try{navigator.sendBeacon&&navigator.sendBeacon(SHEET_URL,b)}catch(e){}})};
function Robot({talk}){
  return <svg className={'rb'+(talk?' talk':'')} viewBox="0 0 200 210" aria-hidden="true">
    <defs><linearGradient id="rbm" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#E6EEF7"/><stop offset=".5" stopColor="#8FA3B8"/><stop offset="1" stopColor="#3A4B5E"/></linearGradient>
      <linearGradient id="rbv" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#07111C"/><stop offset="1" stopColor="#0F1C2C"/></linearGradient></defs>
    <ellipse className="rb-shadow" cx="100" cy="202" rx="52" ry="6" fill="rgba(22,140,255,.35)"/>
    <g className="rb-body">
      <g className="rb-arm-l"><rect x="28" y="128" width="14" height="44" rx="3" fill="url(#rbm)"/><circle cx="35" cy="176" r="8" fill="#16273B" stroke="#4DB8FF" strokeWidth="1.5"/></g>
      <g className="rb-arm-r"><rect x="158" y="128" width="14" height="44" rx="3" fill="url(#rbm)"/><circle cx="165" cy="176" r="8" fill="#16273B" stroke="#4DB8FF" strokeWidth="1.5"/></g>
      <polygon points="52,120 140,120 150,130 150,186 60,186 50,176" fill="url(#rbm)"/>
      <polygon points="66,134 132,134 136,138 136,170 70,170 66,166" fill="#0A1421" stroke="#4DB8FF" strokeWidth="1"/>
      <g className="rb-core"><polygon points="101,142 108,152 101,162 94,152" fill="#4DB8FF"/></g>
      <g className="rb-head">
        <line x1="100" y1="22" x2="100" y2="38" stroke="#8FA3B8" strokeWidth="3"/><circle className="rb-ant" cx="100" cy="18" r="6" fill="#4DB8FF"/>
        <polygon points="40,38 150,38 164,52 164,104 154,114 50,114 36,100 36,42" fill="url(#rbm)"/>
        <polygon points="50,52 148,52 152,56 152,94 146,100 54,100 48,94 48,56" fill="url(#rbv)" stroke="rgba(77,184,255,.5)" strokeWidth="1"/>
        <g className="rb-eyes"><rect x="72" y="62" width="16" height="16" fill="#4DB8FF"/><rect x="112" y="62" width="16" height="16" fill="#4DB8FF"/></g>
        <g className="rb-mouth">{[0,1,2,3,4,5,6].map(i=><rect key={i} x={79+i*6.4} y="84" width="3.6" height="8" fill="#8FD3FF" style={{animationDelay:(i*.07)+'s'}}/>)}</g>
        <rect x="28" y="62" width="8" height="24" fill="#16273B" stroke="#4DB8FF" strokeWidth="1"/><rect x="164" y="62" width="8" height="24" fill="#16273B" stroke="#4DB8FF" strokeWidth="1"/>
      </g>
    </g></svg>;
}
const ok={name:v=>v.trim().length>1,phone:v=>v.replace(/\D/g,'').length>=7,email:v=>/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v.trim())};
const MSG={name:'Please enter your name',phone:'Please enter a valid phone number',email:'Please enter a valid email'};
function ContactForm({onClose}){
  const [v,setV]=useState({name:'',phone:'',email:''});const [err,setErr]=useState({});const [st,setSt]=useState('form');
  const [typed,setTyped]=useState('');const [talk,setTalk]=useState(false);const [tilt,setTilt]=useState({x:0,y:0});const ts=useRef([]);
  const first=v.name.trim().split(' ')[0];
  const line='Thank you'+(first?', '+first:'')+'! We will be calling you within 2 minutes.';
  useEffect(()=>{const k=e=>e.key==='Escape'&&close();addEventListener('keydown',k);document.body.style.overflow='hidden';
    return()=>{removeEventListener('keydown',k);document.body.style.overflow='';ts.current.forEach(clearTimeout);window.speechSynthesis&&speechSynthesis.cancel()}},[]);
  const close=()=>{setSt(s=>s==='closing'?s:'closing');ts.current.push(setTimeout(onClose,450))};
  const speak=()=>{setTyped('');setTalk(true);[...line].forEach((ch,i)=>ts.current.push(setTimeout(()=>setTyped(line.slice(0,i+1)),i*42)));
    ts.current.push(setTimeout(()=>setTalk(false),line.length*42+900));
    try{if(window.speechSynthesis){speechSynthesis.cancel();const u=new SpeechSynthesisUtterance(line);u.rate=1;u.pitch=1.15;speechSynthesis.speak(u)}}catch(e){}};
  const submit=e=>{e.preventDefault();const E={};Object.keys(ok).forEach(k=>{if(!ok[k](v[k]))E[k]=MSG[k]});setErr(E);
    if(Object.keys(E).length){setSt('shake');ts.current.push(setTimeout(()=>setSt('form'),500));return}
    setSt('sending');const t0=Date.now();
    Promise.race([saveLead({name:v.name.trim(),phone:v.phone.trim(),email:v.email.trim()}),new Promise(r=>setTimeout(r,6000))]).then(()=>{
      ts.current.push(setTimeout(()=>{setSt('done');ts.current.push(setTimeout(speak,900))},Math.max(0,1500-(Date.now()-t0))))})};
  const set=k=>val=>{setV(o=>({...o,[k]:val}));if(err[k]&&ok[k](val))setErr(o=>({...o,[k]:undefined}))};
  const mv=e=>{if(matchMedia('(pointer: coarse)').matches)return;const r=e.currentTarget.getBoundingClientRect();setTilt({x:(e.clientX-r.left)/r.width-.5,y:(e.clientY-r.top)/r.height-.5})};
  const done=st==='done';
  return <div className={'cf'+(st==='closing'?' out':'')} onMouseDown={e=>e.target===e.currentTarget&&close()}>
    <div className="cf-grid"></div>
    <div className="cf-scene" onMouseMove={mv} onMouseLeave={()=>setTilt({x:0,y:0})}>
      <div className="cf-enter"><div className="cf-tilt" style={{transform:'rotateX('+(-tilt.y*8)+'deg) rotateY('+(tilt.x*10)+'deg)'}}>
        <i className="cf-layer l2"></i><i className="cf-layer l1"></i>
        <div className={'cf-card'+(done?' flip':'')+(st==='shake'?' shake':'')}>
          <form className="cf-face cf-front" onSubmit={submit} noValidate aria-hidden={done}>
            <div className="cf-head"><NX.StatusLine>NURVEX AI — Strategy call</NX.StatusLine><button type="button" className="cf-x" aria-label="Close" onClick={close}><i></i><i></i></button></div>
            <h2 className="cf-h">Book a <span>strategy call.</span></h2>
            <p className="cf-p">Leave your details and we will call you back.</p>
            <div className="cf-fields">
              <div className="cf-f" style={{'--d':'.35s'}}><NX.Input label="Full name" required name="name" placeholder="Your name" value={v.name} onChange={set('name')} error={err.name}/></div>
              <div className="cf-f" style={{'--d':'.45s'}}><NX.Input label="Phone" required type="tel" name="phone" placeholder="+91 98765 43210" value={v.phone} onChange={set('phone')} error={err.phone}/></div>
              <div className="cf-f" style={{'--d':'.55s'}}><NX.Input label="Email" required type="email" name="email" placeholder="you@company.com" value={v.email} onChange={set('email')} error={err.email}/></div>
            </div>
            <button type="submit" className={'cf-submit'+(st==='sending'?' busy':'')} disabled={st==='sending'}>
              <span className="nx-call-beam"></span>
              {st==='sending'?<><span className="cf-dots"><i></i><i></i><i></i></span><span>Connecting to NURVEX AI</span></>:<><span>Request my call</span><span className="nx-call-a">→</span></>}
            </button>
          </form>
          <div className="cf-face cf-back" aria-hidden={!done} aria-live="polite">
            <div className="cf-head"><NX.StatusLine tone="busy">Request received</NX.StatusLine><button type="button" className="cf-x" aria-label="Close" onClick={close} tabIndex={done?0:-1}><i></i><i></i></button></div>
            <div className="cf-bot"><div className="cf-halo"></div>{done?<Robot talk={talk}/>:null}</div>
            <div className="cf-say"><span className="mono" style={{color:'var(--text-accent)'}}>NURVEX assistant</span><p>{typed}<b className={'cf-caret'+(talk?'':' off')}></b></p></div>
            <NX.Waveform active={talk} bars={40} height={36}/>
            <div className="cf-back-ft"><button type="button" className="cf-ghost mono" onClick={speak} tabIndex={done?0:-1}>Replay</button><NX.Button size="sm" variant="secondary" onClick={close}>Close</NX.Button></div>
          </div>
        </div>
      </div></div>
    </div>
  </div>;
}
window.NXContactForm=ContactForm;
})();
