// Main app, theme state, section order, sticky CTA
const { useState: useAppState, useEffect: useAppEffect } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accentColor": "#00D9A0",
  "accentSecondary": "#8B5CF6",
  "heroHeadline": "We build the AI systems that run your business while you sleep.",
  "showParticles": true,
  "animationsEnabled": true,
  "cardRadius": 12
}/*EDITMODE-END*/;

function App() {
  const [tweaks, setTweak] = useTweaks(TWEAK_DEFAULTS);

  // Theme state, persist in localStorage
  const [theme, setTheme] = useAppState(() => {
    try { return localStorage.getItem('q8-theme') || 'dark'; } catch { return 'dark'; }
  });

  const toggleTheme = () => {
    const next = theme === 'dark' ? 'light' : 'dark';
    setTheme(next);
    try { localStorage.setItem('q8-theme', next); } catch {}
  };

  // Apply theme to <html>
  useAppEffect(() => {
    document.documentElement.setAttribute('data-theme', theme);
  }, [theme]);

  // Apply accent color tweaks
  useAppEffect(() => {
    document.documentElement.style.setProperty('--accent-mint', tweaks.accentColor);
    document.documentElement.style.setProperty('--accent-violet', tweaks.accentSecondary);
  }, [tweaks.accentColor, tweaks.accentSecondary]);

  // Global scroll reveal animation
  useAppEffect(() => {
    const obs = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.style.opacity = 1;
          e.target.style.transform = 'translateY(0)';
          obs.unobserve(e.target);
        }
      });
    }, { threshold: 0.1, rootMargin: '0px 0px -50px 0px' });

    // Select sections (except hero) and premium cards to animate
    const els = document.querySelectorAll('section:not(#hero), .premium-card');
    els.forEach(el => {
      // Avoid overriding existing inline transition if possible, but we need to ensure it animates
      if (el.style.opacity !== '1' && !el.dataset.animated) {
        el.style.opacity = 0;
        el.style.transform = 'translateY(30px)';
        el.style.transition = 'opacity 0.8s cubic-bezier(0.16,1,0.3,1), transform 0.8s cubic-bezier(0.16,1,0.3,1)';
        el.dataset.animated = 'true';
        obs.observe(el);
      }
    });
    return () => obs.disconnect();
  }, []);

  return (
    <>
      {/* Ambient lighting orbs */}
      <div style={{ position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, zIndex: -1, pointerEvents: 'none', overflow: 'hidden' }}>
        <div style={{
          position: 'absolute', top: '-10%', right: '-5%', width: '50vw', height: '50vw', minWidth: 400, minHeight: 400,
          background: 'var(--accent-violet)', filter: 'blur(160px)', opacity: theme === 'dark' ? 0.08 : 0.03,
          borderRadius: '50%', animation: 'q8-orb-float 20s ease-in-out infinite alternate',
        }} />
        <div style={{
          position: 'absolute', bottom: '-10%', left: '-5%', width: '50vw', height: '50vw', minWidth: 400, minHeight: 400,
          background: 'var(--accent-mint)', filter: 'blur(160px)', opacity: theme === 'dark' ? 0.06 : 0.03,
          borderRadius: '50%', animation: 'q8-orb-float 25s ease-in-out infinite alternate-reverse',
        }} />
      </div>
      <style>{`@keyframes q8-orb-float { 0% { transform: translate(0, 0) scale(1); } 100% { transform: translate(-30px, 30px) scale(1.1); } }`}</style>
      
      <Nav theme={theme} toggleTheme={toggleTheme} />
      <main>
        {/* 1. Hero, gradient headline, live agent canvas */}
        <Hero theme={theme} />

        {/* 2. Social Proof, trust strip immediately after hero */}
        <SocialProof />

        {/* 3. Case Studies, show the work */}
        <CaseStudies />

        {/* 4. Industry Selector, high conversion lever */}
        <IndustrySelector />

        {/* 5. Founder, humanizes the brand */}
        <Founder />

        {/* 6. Differentiator, old way vs Q8 way */}
        <Differentiator />

        {/* 7. Services, four ways we ship */}
        <Services />

        {/* 8. Process, 6-week live build simulation */}
        <Process />

        {/* 9. Pricing, with pilot guarantee */}
        <Pricing />

        {/* 10. Tech Stack, compact credibility strip */}
        <TechStack />

        {/* 11. FAQ */}
        <FAQ />

        {/* 12. Final CTA, 2-field form, direct to calendar */}
        <FinalCTA />
      </main>
      <Footer />

      {/* Sticky book-a-call pill, appears after scroll */}
      <StickyCallButton />

      {/* AI Chatbot */}
      <Chatbot />

      <TweaksPanel>
        <TweakSection title="Brand">
          <TweakColor label="Accent (Mint)"   value={tweaks.accentColor}      onChange={v => setTweak('accentColor', v)} />
          <TweakColor label="Accent (Violet)" value={tweaks.accentSecondary}  onChange={v => setTweak('accentSecondary', v)} />
        </TweakSection>
        <TweakSection title="Hero">
          <TweakText   label="Headline"        value={tweaks.heroHeadline}     onChange={v => setTweak('heroHeadline', v)} />
          <TweakToggle label="Particle field"  value={tweaks.showParticles}    onChange={v => setTweak('showParticles', v)} />
        </TweakSection>
        <TweakSection title="Motion">
          <TweakToggle label="Animations"      value={tweaks.animationsEnabled} onChange={v => setTweak('animationsEnabled', v)} />
        </TweakSection>
        <TweakSection title="Layout">
          <TweakSlider label="Card radius" value={tweaks.cardRadius} min={0} max={24} step={2} onChange={v => setTweak('cardRadius', v)} />
        </TweakSection>
      </TweaksPanel>
    </>
  );
}

function StickyCallButton() {
  const [scrolled, setScrolled] = useAppState(false);
  useAppEffect(() => {
    const h = () => setScrolled(window.scrollY > 600);
    window.addEventListener('scroll', h, { passive: true });
    return () => window.removeEventListener('scroll', h);
  }, []);

  return (
    <a href="https://cal.com/ratan-kumar/ai-automation-startegy-call-with-ratan-quantaeight" target="_blank" rel="noopener" style={{
      position: 'fixed', bottom: 88, right: 28, zIndex: 198,
      display: 'flex', alignItems: 'center', gap: 8,
      padding: '10px 18px',
      background: 'var(--accent-mint)',
      color: '#0A0A0B', fontWeight: 700, fontSize: 13,
      fontFamily: 'Inter, sans-serif',
      borderRadius: 40, textDecoration: 'none',
      boxShadow: '0 4px 20px rgba(0,217,160,0.35)',
      transition: 'all 0.4s cubic-bezier(0.16,1,0.3,1)',
      opacity: scrolled ? 1 : 0,
      transform: scrolled ? 'translateY(0) scale(1)' : 'translateY(12px) scale(0.92)',
      pointerEvents: scrolled ? 'auto' : 'none',
      letterSpacing: '0.01em',
    }}
    onMouseEnter={e => { e.currentTarget.style.transform = 'translateY(-2px) scale(1.04)'; e.currentTarget.style.boxShadow = '0 8px 28px rgba(0,217,160,0.45)'; }}
    onMouseLeave={e => { e.currentTarget.style.transform = 'translateY(0) scale(1)'; e.currentTarget.style.boxShadow = '0 4px 20px rgba(0,217,160,0.35)'; }}
    >
      <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'rgba(10,10,11,0.5)', animation: 'sticky-blink 1.5s ease infinite' }} />
      Book a discovery call
      <style>{`@keyframes sticky-blink{0%,100%{opacity:1}50%{opacity:0.3}}`}</style>
    </a>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
