// Motion layer — restrained blur-fade section reveals + subtle card/button polish.
// Reference feel: lemonsqueezy.com — content softly resolves into focus as it scrolls in.
// No new colors, no new visual language. Polish only.

(function () {
  const INK      = '#0B1220';
  const HAIR     = 'rgba(11,18,32,0.08)';
  const HAIR_2   = 'rgba(11,18,32,0.14)';

  // ====== Global styles ======
  function GlobalStyles() {
    return (
      <style>{`
        /* Blur-fade section reveal — matches lemonsqueezy feel */
        .mo-reveal {
          opacity: 0;
          transform: translateY(40px);
          filter: blur(14px);
          transition:
            opacity 1200ms cubic-bezier(.22,.61,.36,1),
            transform 1200ms cubic-bezier(.22,.61,.36,1),
            filter 1000ms cubic-bezier(.22,.61,.36,1);
          will-change: opacity, transform, filter;
        }
        .mo-reveal.is-in {
          opacity: 1;
          transform: translateY(0);
          filter: blur(0);
        }

        /* Child stagger inside a revealed section — headings, paragraphs,
           links, and card grid items softly blur in as the section resolves. */
        .mo-reveal.is-in :is(h1, h2, h3, h4) {
          animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both;
          animation-delay: 80ms;
        }
        .mo-reveal.is-in :is(p, ul, ol) {
          animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both;
          animation-delay: 180ms;
        }
        .mo-reveal.is-in a {
          animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both;
          animation-delay: 260ms;
        }
        /* Feature cards stagger across columns */
        .mo-reveal.is-in .mo-feat:nth-child(1) { animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both; animation-delay: 180ms; }
        .mo-reveal.is-in .mo-feat:nth-child(2) { animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both; animation-delay: 260ms; }
        .mo-reveal.is-in .mo-feat:nth-child(3) { animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both; animation-delay: 340ms; }
        .mo-reveal.is-in .mo-feat:nth-child(4) { animation: moChildIn 900ms cubic-bezier(.22,.61,.36,1) both; animation-delay: 420ms; }

        @keyframes moChildIn {
          from { opacity: 0; transform: translateY(14px); filter: blur(6px); }
          to   { opacity: 1; transform: translateY(0);    filter: blur(0); }
        }

        /* Buttons: subtle lift only — no color change */
        a { transition: transform 220ms cubic-bezier(.2,.7,.2,1), box-shadow 220ms cubic-bezier(.2,.7,.2,1), opacity 180ms ease; }
        a:hover { transform: translateY(-1px); }
        a:active { transform: translateY(0); }

        /* Primary CTA polish — shared by Start Free + matching dark/ink CTAs.
           Gentle lift + soft shadow. No gloss sweep. */
        .mo-cta {
          transition:
            transform 240ms cubic-bezier(.2,.7,.2,1),
            box-shadow 260ms cubic-bezier(.2,.7,.2,1);
        }
        .mo-cta:hover {
          transform: translateY(-1px);
          box-shadow:
            0 6px 14px rgba(11,18,32,0.10),
            0 1px 3px rgba(11,18,32,0.06) !important;
        }
        .mo-cta:active {
          transform: translateY(0);
          box-shadow: 0 1px 2px rgba(11,18,32,0.1) !important;
        }

        /* Feature cards in "The fastest way to host" — polish only */
        .mo-feat {
          transition:
            transform 340ms cubic-bezier(.2,.7,.2,1),
            box-shadow 340ms cubic-bezier(.2,.7,.2,1),
            border-color 340ms ease;
          cursor: default;
        }
        .mo-feat:hover {
          transform: translateY(-3px);
          border-color: ${HAIR_2} !important;
          box-shadow:
            0 16px 28px -14px rgba(11,18,32,0.12),
            0 2px 6px rgba(11,18,32,0.03) !important;
        }
        .mo-feat .mo-feat-icon {
          transition: transform 340ms cubic-bezier(.2,.7,.2,1);
        }
        .mo-feat:hover .mo-feat-icon {
          transform: translateY(-1px) scale(1.04);
        }
        .mo-feat .mo-feat-title {
          position: relative;
          display: inline-block;
        }
        .mo-feat .mo-feat-title::after {
          content: '';
          position: absolute;
          left: 0; right: 0; bottom: -3px;
          height: 1px;
          background: ${INK};
          transform: scaleX(0);
          transform-origin: left;
          transition: transform 420ms cubic-bezier(.2,.7,.2,1);
        }
        .mo-feat:hover .mo-feat-title::after {
          transform: scaleX(1);
        }

        html { scroll-behavior: smooth; }

        /* Pricing card — gentle pop on reveal, lift on hover */
        .mo-reveal .mo-pricing-card,
        section .mo-pricing-card {
          transition:
            transform 600ms cubic-bezier(.2,.7,.2,1),
            box-shadow 600ms cubic-bezier(.2,.7,.2,1),
            border-color 400ms ease;
        }
        .mo-reveal .mo-pricing-card {
          opacity: 0;
          transform: translateY(30px) scale(0.97);
          filter: blur(10px);
          transition:
            opacity 1100ms cubic-bezier(.22,.61,.36,1) 220ms,
            transform 1100ms cubic-bezier(.22,.61,.36,1) 220ms,
            filter 900ms cubic-bezier(.22,.61,.36,1) 220ms,
            box-shadow 600ms cubic-bezier(.2,.7,.2,1);
        }
        .mo-reveal.is-in .mo-pricing-card {
          opacity: 1;
          transform: translateY(0) scale(1);
          filter: blur(0);
        }
        .mo-pricing-card:hover {
          transform: translateY(-4px) !important;
          box-shadow:
            0 32px 64px rgba(11,18,32,0.12),
            0 8px 20px rgba(11,18,32,0.06),
            inset 0 1px 0 rgba(255,255,255,0.8) !important;
        }
        .mo-pricing-card .mo-price-num {
          display: inline-block;
          transition: transform 600ms cubic-bezier(.2,.7,.2,1);
        }
        .mo-reveal.is-in .mo-pricing-card .mo-price-num {
          animation: moPriceIn 900ms cubic-bezier(.22,.61,.36,1) 400ms both;
        }
        @keyframes moPriceIn {
          from { opacity: 0; transform: translateY(20px) scale(0.9); }
          to { opacity: 1; transform: translateY(0) scale(1); }
        }

        @media (prefers-reduced-motion: reduce) {
          .mo-reveal { opacity: 1 !important; transform: none !important; filter: none !important; }
          a:hover, .mo-feat:hover, .mo-feat:hover .mo-feat-icon { transform: none !important; }
        }
      `}</style>
    );
  }

  // ====== Reveal wrapper — IntersectionObserver-driven blur fade ======
  function Reveal({children, delay = 0}) {
    const ref = React.useRef(null);
    const [seen, setSeen] = React.useState(false);
    React.useEffect(() => {
      if (!ref.current) return;
      const io = new IntersectionObserver((entries) => {
        entries.forEach(e => {
          if (e.isIntersecting) {
            setSeen(true);
            io.unobserve(e.target);
          }
        });
      }, { threshold: 0.15, rootMargin: '0px 0px -15% 0px' });
      io.observe(ref.current);
      return () => io.disconnect();
    }, []);
    return (
      <div ref={ref}
        className={`mo-reveal${seen ? ' is-in' : ''}`}
        style={{ transitionDelay: `${delay}ms` }}>
        {children}
      </div>
    );
  }

  // ====== WhatIs override — same layout, just adds the polish classes ======
  // We reimplement this one component (tiny) so the feature cards pick up hover.
  function WhatIsMotion() {
    // Match originals exactly
    const BG       = '#FAFBFC';
    const BG_2     = '#F3F5F8';
    const SURFACE  = '#FFFFFF';
    const INK_2    = 'rgba(11,18,32,0.68)';
    const CORAL    = '#FF6A5B';
    const FONT     = '"Inter","SF Pro Display",-apple-system,BlinkMacSystemFont,system-ui,sans-serif';

    const items = [
      {t:'Push to Deploy',  d:'Every GitHub commit auto-updates your live site in about 30 seconds.', k:'↗'},
      {t:'Global Edge',     d:'300+ Cloudflare edge locations serve from the node nearest your visitor.', k:'◉'},
      {t:'Custom Domains',  d:'Any domain you own. Free SSL. Zero configuration required.', k:'✦'},
      {t:'Human Support',   d:'Real humans who know your setup. Not a forum. Not a chatbot.', k:'★'},
    ];
    return (
      <section style={{background: BG, padding:'120px 28px', fontFamily: FONT}}>
        <div style={{maxWidth: 1200, margin:'0 auto'}}>
          <div style={{textAlign:'center', maxWidth: 820, margin:'0 auto'}}>
            <div style={{
              display:'inline-flex', alignItems:'center', gap: 8,
              fontSize: 12, fontWeight: 700, letterSpacing:'.08em',
              textTransform:'uppercase', color: CORAL, fontFamily: FONT,
            }}>
              <span style={{width: 16, height: 1.5, background: CORAL}}/>
              The product
            </div>
            <h2 style={{
              fontSize:'clamp(40px, 4.6vw, 64px)', fontWeight: 700, lineHeight: 1.04,
              letterSpacing:'-0.03em', color: INK, margin:'18px 0 0', textWrap:'balance',
            }}>The fastest way to host a static website.</h2>
            <p style={{
              fontSize: 18, lineHeight: 1.55, color: INK_2, maxWidth: 620,
              margin:'26px auto 0',
            }}>
              Whether you built it with Lovable, Replit, or hand-coded every line — connect your GitHub
              repo and we deploy to Cloudflare's global edge in seconds. Custom domains, SSL,
              analytics, and expert support included. <strong style={{color: INK}}>Starting at $5/site.</strong>
            </p>
          </div>
          <div style={{
            marginTop: 80, display:'grid', gridTemplateColumns:'repeat(4, 1fr)', gap: 20,
          }}>
            {items.map((x,i)=>(
              <div key={i} className="mo-feat" style={{
                background: SURFACE, borderRadius: 14, border:`1px solid ${HAIR}`,
                padding:'28px 24px', boxShadow:'0 1px 2px rgba(11,18,32,0.03)',
              }}>
                <div className="mo-feat-icon" style={{
                  width: 36, height: 36, borderRadius: 10,
                  background: BG_2, color: CORAL,
                  display:'flex', alignItems:'center', justifyContent:'center',
                  fontSize: 16, fontWeight: 700, marginBottom: 20,
                }}>{x.k}</div>
                <h4 style={{fontSize: 17, fontWeight: 600, margin:'0 0 8px',
                  letterSpacing:'-0.01em'}}>
                  <span className="mo-feat-title">{x.t}</span>
                </h4>
                <p style={{fontSize: 14, lineHeight: 1.55, color: INK_2, margin: 0}}>{x.d}</p>
              </div>
            ))}
          </div>
        </div>
      </section>
    );
  }

  window.MotionGlobalStyles = GlobalStyles;
  window.MotionReveal = Reveal;
  window.MotionWhatIs = WhatIsMotion;

  // ====== Support override — same polish on the three "Real support" cards ======
  function SupportMotion() {
    const BG_2     = '#F3F5F8';
    const SURFACE  = '#FFFFFF';
    const INK_2    = 'rgba(11,18,32,0.68)';
    const INK_3    = 'rgba(11,18,32,0.50)';
    const CORAL    = '#FF6A5B';
    const FONT     = '"Inter","SF Pro Display",-apple-system,BlinkMacSystemFont,system-ui,sans-serif';

    const pillars = [
      {t:'Simple Onboarding', d:"We'll walk you through your first deploy — no terminal, no stress.", k:'✦'},
      {t:'24/7 Live Chat',          d:'Real engineers in about 90 seconds. No ticket queues.',             k:'●'},
      {t:'Expert Troubleshooting',  d:'Talk to a human who knows your setup — and cares about fixing it.', k:'★'},
    ];
    return (
      <section style={{background: BG_2, padding:'120px 28px', fontFamily: FONT}}>
        <div style={{maxWidth: 1200, margin:'0 auto'}}>
          <div style={{textAlign:'center', maxWidth: 720, margin:'0 auto 56px'}}>
            <div style={{
              display:'inline-flex', alignItems:'center', gap: 8,
              fontSize: 12, fontWeight: 700, letterSpacing:'.08em',
              textTransform:'uppercase', color: CORAL, fontFamily: FONT,
            }}>
              <span style={{width: 16, height: 1.5, background: CORAL}}/>
              Support
            </div>
            <h2 style={{
              fontSize:'clamp(30px, 3.4vw, 48px)', fontWeight: 700, lineHeight: 1.04,
              letterSpacing:'-0.03em', color: INK, margin:'18px 0 0', textWrap:'balance',
            }}>Real support. From real humans.</h2>
          </div>
          <div style={{display:'grid', gridTemplateColumns:'repeat(3, 1fr)', gap: 20}}>
            {pillars.map((p,i)=>(
              <div key={i} className="mo-feat" style={{
                background: SURFACE, borderRadius: 14, border:`1px solid ${HAIR}`,
                padding:'32px 28px', boxShadow:'0 1px 2px rgba(11,18,32,0.03)',
              }}>
                <div className="mo-feat-icon" style={{
                  width: 40, height: 40, borderRadius: 10,
                  background: CORAL, color:'#fff',
                  display:'flex', alignItems:'center', justifyContent:'center',
                  fontSize: 18, fontWeight: 700, marginBottom: 22,
                }}>{p.k}</div>
                <h3 style={{fontSize: 19, fontWeight: 600, margin:'0 0 10px',
                  letterSpacing:'-0.015em'}}>
                  <span className="mo-feat-title">{p.t}</span>
                </h3>
                <p style={{fontSize: 14.5, lineHeight: 1.6, color: INK_2, margin: 0}}>{p.d}</p>
              </div>
            ))}
          </div>
        </div>
      </section>
    );
  }
  window.MotionSupport = SupportMotion;

  // ====== Tag the pricing card after render ======
  function PricingCardTagger() {
    React.useEffect(() => {
      const tag = () => {
        const sections = document.querySelectorAll('section');
        sections.forEach(s => {
          if (s.textContent.includes('Simple pricing. No surprises.')) {
            const card = [...s.querySelectorAll('div')].find(d =>
              d.style && d.style.borderRadius &&
              parseInt(d.style.borderRadius) >= 16 &&
              d.textContent.includes('/ site'));
            if (card && !card.classList.contains('mo-pricing-card')) {
              card.classList.add('mo-pricing-card');
            }
          }
        });
      };
      tag();
      const id = setInterval(tag, 400);
      const stop = setTimeout(() => clearInterval(id), 4000);
      return () => { clearInterval(id); clearTimeout(stop); };
    }, []);
    return null;
  }
  window.MotionPricingTagger = PricingCardTagger;

  // ====== Tag primary CTAs (Start Free + matching dark buttons) ======
  function PrimaryCTATagger() {
    React.useEffect(() => {
      const tag = () => {
        const anchors = document.querySelectorAll('a');
        anchors.forEach(a => {
          if (a.classList.contains('mo-cta')) return;
          // Skip anything inside the pricing card — user request.
          if (a.closest('.mo-pricing-card')) return;
          const txt = (a.textContent || '').trim();
          const bg = (a.style && a.style.background) || '';
          // Match: dark (INK) backgrounds — that's what Start Free and PrimaryBtn use.
          const isDarkBg = /#0B1220|rgb\(11,\s*18,\s*32\)/i.test(bg);
          const matchesLabel = /Start free|Deploy your first site|Watch the demo|Start free trial/i.test(txt);
          if (isDarkBg || matchesLabel) {
            a.classList.add('mo-cta');
          }
        });
      };
      tag();
      const id = setInterval(tag, 400);
      const stop = setTimeout(() => clearInterval(id), 4000);
      return () => { clearInterval(id); clearTimeout(stop); };
    }, []);
    return null;
  }
  window.MotionPrimaryCTATagger = PrimaryCTATagger;
})();
