/* Hero Section — with realistic iPhone showing live call */

/* Count-up / count-down helper — strong ease-out (decelerates heavily at the end) */
const easeOutQuint = (t) => 1 - Math.pow(1 - t, 5);

const useCount = (from, to, duration) => {
  const [v, setV] = React.useState(from);
  React.useEffect(() => {
    let raf, start;
    const tick = (ts) => {
      if (!start) start = ts;
      const p = Math.min(1, (ts - start) / duration);
      const eased = easeOutQuint(p);
      setV(from + (to - from) * eased);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [from, to, duration]);
  return v;
};

const CountUp = ({ from=0, to, duration=2000 }) => {
  const v = useCount(from, to, duration);
  return <>{Math.round(v)}</>;
};
const CountDown = ({ from, to, duration=2000 }) => {
  const v = useCount(from, to, duration);
  return <>{Math.round(v)}</>;
};
const CountDownDecimal = ({ from, to, duration=2000 }) => {
  const v = useCount(from, to, duration);
  return <>{v.toFixed(2).replace('.', ',')}</>;
};

const Hero = () => {
  // Start with the first greeting already visible — the chat shouldn't open "empty".
  const [transcriptIdx, setTranscriptIdx] = React.useState(1);
  const transcript = [
    { who: 'aivent', text: 'Willkommen beim Taxi Ruf! Wo möchten Sie abgeholt werden?' },
    { who: 'kunde', text: 'Mozartstraße 42, Vorderhaus.' },
    { who: 'aivent', text: 'Gerne! Für sofort?' },
    { who: 'kunde', text: 'Nein, für morgen früh um 6:30 Uhr.' },
    { who: 'aivent', text: 'Notiert. Und wohin darf der Wagen Sie bringen?' },
    { who: 'kunde', text: 'Zum Hauptbahnhof, bitte.' },
    { who: 'aivent', text: 'Perfekt. Morgen 6:30, Mozartstr. 42 → Hauptbahnhof. Soll ich so buchen?' },
    { who: 'kunde', text: 'Ja, gerne.' },
    { who: 'aivent', text: 'Gebucht. Sie erhalten 10 Min. vorher eine SMS mit Fahrer und Kennzeichen.' },
  ];

  React.useEffect(() => {
    const t = setInterval(() => setTranscriptIdx(i => (i + 1) % (transcript.length + 2)), 2400);
    return () => clearInterval(t);
  }, []);

  const visible = transcript.slice(0, Math.min(transcriptIdx, transcript.length));
  const typing = transcriptIdx < transcript.length;
  const nextSpeaker = transcript[transcriptIdx]?.who;

  // Time tick
  const [sec, setSec] = React.useState(8);
  React.useEffect(() => {
    const t = setInterval(() => setSec(s => s + 1), 1000);
    return () => clearInterval(t);
  }, []);
  const mm = String(Math.floor(sec/60)).padStart(2,'0');
  const ss = String(sec%60).padStart(2,'0');

  return (
    <section className="section hero-light paper" style={{paddingTop:56, paddingBottom:120, position:'relative', overflow:'hidden'}}>
      <div className="grid-bg"></div>
      <div className="wrap" style={{position:'relative'}}>
        <div style={{display:'grid',gridTemplateColumns:'1.15fr 1fr',gap:64,alignItems:'center'}} className="hero-grid">
          <div>
            <div style={{display:'inline-flex',alignItems:'center',gap:10,marginBottom:24}}>
              <span style={{position:'relative',display:'inline-flex',width:8,height:8}}>
                <span style={{position:'absolute',inset:0,borderRadius:'50%',background:'var(--lime)',animation:'heroLivePing 2s ease-out infinite',opacity:0.55}}></span>
                <span style={{position:'relative',width:8,height:8,borderRadius:'50%',background:'var(--lime)',boxShadow:'0 0 10px var(--lime-glow)'}}></span>
              </span>
              <span className="mono" style={{fontSize:12,letterSpacing:'0.14em',color:'rgba(10,11,8,0.65)',textTransform:'uppercase'}}>Live seit 2025</span>
              <style>{`@keyframes heroLivePing{0%{transform:scale(1);opacity:0.55}70%{transform:scale(2.6);opacity:0}100%{transform:scale(2.6);opacity:0}}`}</style>
            </div>
            <h1 className="h-hero hero-headline" style={{marginTop:0,fontSize:'clamp(34px,3.8vw,58px)',lineHeight:1.02}}>
              Klingeln.<br/>
              Buchen. <span style={{color:'var(--lime)',fontStyle:'italic',fontWeight:700}}>Fahren</span><span style={{color:'var(--lime)',fontWeight:700}}>.</span>
            </h1>
            <p className="lede" style={{marginTop:32,color:'rgba(10,11,8,0.7)',fontSize:'clamp(20px,1.5vw,24px)'}}>
              KI-Telefonie für Taxizentralen. Buchungen, Vorbestellungen, Sonderwünsche — vollautomatisch.
            </p>

            <div style={{display:'flex',gap:12,marginTop:40,flexWrap:'wrap'}}>
              <a href="demo.html" className="btn btn-primary">Demo buchen <span className="arrow">↗</span></a>
              <a href="kontakt.html" className="btn" style={{background:'var(--ink)',color:'#fff'}}>Kontakt <span className="arrow">↗</span></a>
            </div>
          </div>

          {/* iPhone */}
          <div style={{display:'flex',justifyContent:'center',position:'relative'}}>
            {/* Lime glow */}
            <div style={{position:'absolute',width:520,height:520,background:'radial-gradient(circle, var(--lime-glow) 0%, transparent 55%)',opacity:0.5,pointerEvents:'none',transform:'translateZ(0)'}}></div>

            <IPhone timer={`${mm}:${ss}`} visible={visible} typing={typing} nextSpeaker={nextSpeaker} />

            {/* Floating annotations — fly in on mount */}
            <div style={{position:'absolute',left:-60,top:60,background:'#fff',border:'1px solid var(--ink)',padding:'12px 16px',fontFamily:'var(--f-mono)',fontSize:12,color:'var(--ink)',letterSpacing:'0.08em',fontWeight:600,boxShadow:'4px 4px 0 var(--ink)',zIndex:2}} className="anno-left anno-fly-tl">
              ↘ ECHTZEIT-BUCHUNGEN
            </div>
            <div style={{position:'absolute',right:-60,bottom:120,background:'#fff',border:'1px solid var(--ink)',padding:'12px 16px',fontFamily:'var(--f-mono)',fontSize:12,color:'var(--ink)',letterSpacing:'0.08em',fontWeight:600,textAlign:'right',boxShadow:'4px 4px 0 var(--ink)',zIndex:2}} className="anno-right anno-fly-br">
              &lt; 800ms LATENZ
            </div>
          </div>
        </div>
      </div>

      <style>{`
        @keyframes annoFlyTL{
          0%{opacity:0;transform:translate(-24px,-24px) scale(0.92)}
          100%{opacity:1;transform:translate(0,0) scale(1)}
        }
        @keyframes annoFlyBR{
          0%{opacity:0;transform:translate(24px,24px) scale(0.92)}
          100%{opacity:1;transform:translate(0,0) scale(1)}
        }
        .anno-fly-tl{opacity:0;animation:annoFlyTL 0.9s cubic-bezier(0.22,1,0.36,1) 1.6s forwards}
        .anno-fly-br{opacity:0;animation:annoFlyBR 0.9s cubic-bezier(0.22,1,0.36,1) 2.8s forwards}
        @media(max-width:960px){
          .hero-grid{grid-template-columns:1fr !important;gap:48px !important}
          .anno-left,.anno-right{display:none}
        }
      `}</style>
    </section>
  );
};

/* ── Realistic iPhone with visible hardware buttons ── */
const IPhone = ({ timer, visible, typing, nextSpeaker }) => {
  const W = 288, H = 586;
  const btn = 'linear-gradient(90deg,#1a1a1c 0%,#3a3a3d 40%,#2a2a2c 60%,#1a1a1c 100%)';
  const btnR = 'linear-gradient(270deg,#1a1a1c 0%,#3a3a3d 40%,#2a2a2c 60%,#1a1a1c 100%)';
  return (
    <div style={{position:'relative',width:W+16,height:H+16,transform:'translateZ(0)',willChange:'transform'}}>
      {/* Side hardware buttons — sit OUTSIDE frame edge so they're visible */}
      {/* Left: Action + Volume Up + Volume Down */}
      <div style={{position:'absolute',left:-2,top:138,width:6,height:32,background:btn,borderRadius:'2px 0 0 2px',zIndex:5,boxShadow:'inset 0 1px 0 rgba(255,255,255,0.1)'}}></div>
      <div style={{position:'absolute',left:-2,top:192,width:6,height:58,background:btn,borderRadius:'2px 0 0 2px',zIndex:5,boxShadow:'inset 0 1px 0 rgba(255,255,255,0.1)'}}></div>
      <div style={{position:'absolute',left:-2,top:262,width:6,height:58,background:btn,borderRadius:'2px 0 0 2px',zIndex:5,boxShadow:'inset 0 1px 0 rgba(255,255,255,0.1)'}}></div>
      {/* Right: Power */}
      <div style={{position:'absolute',right:-2,top:212,width:6,height:96,background:btnR,borderRadius:'0 2px 2px 0',zIndex:5,boxShadow:'inset 0 1px 0 rgba(255,255,255,0.1)'}}></div>

      {/* Outer titanium frame */}
      <div style={{
        position:'absolute',inset:0,
        borderRadius:56,
        background:'linear-gradient(135deg,#3a3a3c 0%,#1c1c1e 25%,#2c2c2e 50%,#1c1c1e 75%,#3a3a3c 100%)',
        padding:3,
        zIndex:1,
        boxShadow:'0 16px 26px rgba(0,0,0,0.55), 0 3px 6px rgba(0,0,0,0.22)',
      }}>
        {/* Inner frame darker */}
        <div style={{
          width:'100%',height:'100%',
          borderRadius:53,
          background:'#3A3A3E',
          padding:5,
          position:'relative',
        }}>
          {/* Screen */}
          <div style={{
            width:'100%',height:'100%',
            borderRadius:46,
            background:'#000000',
            overflow:'hidden',
            position:'relative',
            fontFamily:'-apple-system,"SF Pro",system-ui',
          }}>
            {/* Dynamic Island */}
            <div style={{
              position:'absolute',top:8,left:'50%',transform:'translateX(-50%)',
              width:95,height:28,borderRadius:20,background:'#000',
              zIndex:50,display:'flex',alignItems:'center',justifyContent:'space-between',padding:'0 12px',
              border:'1px solid #111',
            }}>
              <span style={{display:'inline-flex',alignItems:'center',gap:4,fontSize:9,color:'var(--lime)',fontWeight:600}}>
                <span className="live-dot" style={{width:5,height:5}}></span>AIVENT
              </span>
              <span style={{fontSize:9,color:'rgba(255,255,255,0.7)',fontVariantNumeric:'tabular-nums'}}>{timer}</span>
            </div>

            {/* Status bar */}
            <div style={{display:'flex',justifyContent:'space-between',padding:'14px 22px 0',fontSize:12,color:'#F5F5F7',fontWeight:600}}>
              <span>9:41</span>
              <span style={{width:95}}></span>
              <span style={{display:'inline-flex',alignItems:'center',gap:4}}>
                <svg width="14" height="9" viewBox="0 0 17 12"><rect x="0" y="7.5" width="3" height="4.5" rx="0.7" fill="#F5F5F7"/><rect x="4.5" y="5" width="3" height="7" rx="0.7" fill="#F5F5F7"/><rect x="9" y="2.5" width="3" height="9.5" rx="0.7" fill="#F5F5F7"/><rect x="13.5" y="0" width="3" height="12" rx="0.7" fill="#F5F5F7"/></svg>
                <svg width="20" height="9" viewBox="0 0 27 13"><rect x="0.5" y="0.5" width="23" height="12" rx="3.5" stroke="#F5F5F7" strokeOpacity="0.35" fill="none"/><rect x="2" y="2" width="17" height="9" rx="2" fill="var(--lime)"/><rect x="25" y="4" width="1.5" height="5" rx="0.5" fill="#F5F5F7" opacity="0.35"/></svg>
              </span>
            </div>

            {/* Call header */}
            <div style={{padding:'34px 18px 10px',textAlign:'center',borderBottom:'0.5px solid rgba(255,255,255,0.08)'}}>
              <div style={{fontSize:9,color:'rgba(245,245,247,0.45)',fontFamily:'var(--f-mono)',letterSpacing:'0.1em',marginBottom:6}}>EINGEHENDER ANRUF · #4721</div>
              <div style={{fontSize:17,color:'#F5F5F7',fontWeight:500,letterSpacing:'-0.01em'}}>+49 351 211 XXX</div>
              <div style={{fontSize:11,color:'rgba(245,245,247,0.55)',marginTop:3}}>wird automatisch beantwortet von AIVENT</div>
            </div>

            {/* Messages */}
            <div style={{padding:'14px 12px 6px',display:'flex',flexDirection:'column',gap:8,height:330,overflow:'hidden',justifyContent:'flex-end'}}>
              {visible.slice(-5).map((m,i)=>(
                <div key={i} style={{
                  alignSelf: m.who==='aivent' ? 'flex-end' : 'flex-start',
                  maxWidth:'82%',
                  background: m.who==='aivent' ? 'var(--lime)' : '#2A2A2E',
                  color: m.who==='aivent' ? '#0a0a0a' : '#F5F5F7',
                  padding:'8px 11px',
                  borderRadius: m.who==='aivent' ? '16px 16px 4px 16px' : '16px 16px 16px 4px',
                  fontSize:11.5,lineHeight:1.4,fontWeight:450,
                  animation:'msgIn 0.35s ease-out',
                }}>
                  <div style={{fontSize:8,fontFamily:'var(--f-mono)',letterSpacing:'0.08em',textTransform:'uppercase',opacity:0.5,marginBottom:2}}>{m.who==='aivent'?'AIVENT':'ANRUFER'}</div>
                  {m.text}
                </div>
              ))}
              {typing && (
                <div style={{
                  alignSelf: nextSpeaker==='aivent' ? 'flex-end' : 'flex-start',
                  background: nextSpeaker==='aivent' ? 'var(--lime)' : '#2A2A2E',
                  padding:'10px 14px',
                  borderRadius: nextSpeaker==='aivent' ? '16px 16px 4px 16px' : '16px 16px 16px 4px',
                  display:'inline-flex',alignItems:'center',gap:4,
                  animation:'msgIn 0.3s ease-out',
                }}>
                  <span style={{width:5,height:5,borderRadius:'50%',background: nextSpeaker==='aivent' ? '#0a0a0a' : '#F5F5F7',opacity:0.45,animation:'dot 1.2s ease-in-out infinite',animationDelay:'0s'}}></span>
                  <span style={{width:5,height:5,borderRadius:'50%',background: nextSpeaker==='aivent' ? '#0a0a0a' : '#F5F5F7',opacity:0.45,animation:'dot 1.2s ease-in-out infinite',animationDelay:'0.2s'}}></span>
                  <span style={{width:5,height:5,borderRadius:'50%',background: nextSpeaker==='aivent' ? '#0a0a0a' : '#F5F5F7',opacity:0.45,animation:'dot 1.2s ease-in-out infinite',animationDelay:'0.4s'}}></span>
                </div>
              )}
            </div>

            {/* Call controls */}
            <div style={{position:'absolute',bottom:44,left:0,right:0,display:'flex',justifyContent:'center',gap:26}}>
              {[
                {lbl:'Stumm'},
                {lbl:'Tastatur'},
                {lbl:'Lautspr.'},
              ].map((b,i)=>(
                <div key={i} style={{display:'flex',flexDirection:'column',alignItems:'center',gap:4}}>
                  <div style={{width:42,height:42,borderRadius:'50%',background:'#1E1E21',border:'0.5px solid rgba(255,255,255,0.08)',display:'flex',alignItems:'center',justifyContent:'center'}}>
                    {i===0 && <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#F5F5F7" strokeWidth="1.8"><path d="M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/></svg>}
                    {i===1 && <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="5" cy="5" r="1.5" fill="#F5F5F7"/><circle cx="12" cy="5" r="1.5" fill="#F5F5F7"/><circle cx="19" cy="5" r="1.5" fill="#F5F5F7"/><circle cx="5" cy="12" r="1.5" fill="#F5F5F7"/><circle cx="12" cy="12" r="1.5" fill="#F5F5F7"/><circle cx="19" cy="12" r="1.5" fill="#F5F5F7"/><circle cx="5" cy="19" r="1.5" fill="#F5F5F7"/><circle cx="12" cy="19" r="1.5" fill="#F5F5F7"/><circle cx="19" cy="19" r="1.5" fill="#F5F5F7"/></svg>}
                    {i===2 && <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#F5F5F7" strokeWidth="1.8"><path d="M11 5L6 9H2v6h4l5 4V5z"/><path d="M15.5 8.5a5 5 0 0 1 0 7"/><path d="M19 5a9 9 0 0 1 0 14"/></svg>}
                  </div>
                  <span style={{fontSize:9,color:'rgba(245,245,247,0.55)'}}>{b.lbl}</span>
                </div>
              ))}
            </div>

            {/* Home indicator */}
            <div style={{position:'absolute',bottom:6,left:'50%',transform:'translateX(-50%)',width:100,height:4,borderRadius:2,background:'rgba(245,245,247,0.5)'}}></div>
            <style>{`
              @keyframes msgIn{from{opacity:0;transform:translateY(6px) scale(0.96)}to{opacity:1;transform:translateY(0) scale(1)}}
              @keyframes dot{0%,60%,100%{transform:translateY(0);opacity:0.35}30%{transform:translateY(-3px);opacity:0.75}}
            `}</style>
          </div>
        </div>
      </div>
    </div>
  );
};

window.Hero = Hero;
