// HERO SECTION
const { useEffect, useState, useRef, useMemo } = React;
const I = window.Icon;

function MaskReveal({ text, delay = 0, stagger = 0.05, className = '', wordClassName = '' }) {
  // Splits into words, each reveals with mask + translateY.
  // If the user prefers reduced motion (or animations are disabled for any
  // reason), we skip the inline animation styles so the text stays visible.
  const [reduced, setReduced] = useState(false);
  useEffect(() => {
    if (typeof window === 'undefined' || !window.matchMedia) return;
    const mq = window.matchMedia('(prefers-reduced-motion: reduce)');
    setReduced(mq.matches);
    const onChange = (e) => setReduced(e.matches);
    mq.addEventListener ? mq.addEventListener('change', onChange) : mq.addListener(onChange);
    return () => {
      mq.removeEventListener ? mq.removeEventListener('change', onChange) : mq.removeListener(onChange);
    };
  }, []);

  const words = text.split(' ');
  return (
    <span className={className} aria-label={text}>
      {words.map((w, i) => (
        <span key={i} className="inline-block overflow-hidden align-bottom pr-[0.18em]">
          <span
            className={`inline-block will-change-transform ${wordClassName}`}
            style={reduced ? undefined : {
              transform: 'translateY(110%)',
              opacity: 0,
              animation: `mr-up .9s cubic-bezier(.2,.7,.2,1) forwards`,
              animationDelay: `${delay + i * stagger}s`,
            }}
          >{w}</span>
        </span>
      ))}
      <style>{`@keyframes mr-up { to { transform: translateY(0); opacity: 1; } }`}</style>
    </span>
  );
}

// Live counter — "sua loja em tempo real" framing
function LiveROICounter() {
  const [val, setVal] = useState(2847.0);
  const [pulse, setPulse] = useState(false);
  useEffect(() => {
    let timer;
    const tick = () => {
      const add = 12 + Math.random() * 68;
      setVal(v => v + add);
      setPulse(true);
      setTimeout(() => setPulse(false), 700);
      timer = setTimeout(tick, 3000 + Math.random() * 2200);
    };
    timer = setTimeout(tick, 1800);
    return () => clearTimeout(timer);
  }, []);
  return (
    <div className="relative rounded-2xl border border-cyan-300/35 bg-[#0f0a1f]/85 backdrop-blur-xl p-4 shadow-glow-violet-sm w-[230px]">
      <div className="flex items-center gap-2 text-[10px] uppercase tracking-[0.18em] text-cyan-300/80 font-mono">
        <span className={`h-1.5 w-1.5 rounded-full bg-cyan-400 ${pulse?'animate-ping':''}`}></span>
        Sua loja hoje
      </div>
      <div className={`mt-1.5 text-[26px] font-semibold tracking-[-0.02em] tabular-nums text-white transition-transform ${pulse?'scale-[1.04]':''}`}>
        R$ {val.toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
      </div>
      <div className="mt-1 flex items-center gap-1.5 text-[11px] text-violet-200/60">
        <I.Sparkles size={11} className="text-cyan-300"/> 23 pedidos · fluxo PF + PJ
      </div>
      <div className="mt-3 h-1 rounded-full bg-white/8 overflow-hidden">
        <div className="h-full bg-gradient-to-r from-cyan-400 to-violet-400 animate-[mr-up_2.8s_ease-in-out_infinite_alternate]" style={{width:'68%'}}></div>
      </div>
    </div>
  );
}
window.LiveROICounter = LiveROICounter;

function HeroMockup() {
  return (
    <div className="relative perspective w-full max-w-[520px] mx-auto">
      {/* Glow under */}
      <div className="absolute -inset-10 rounded-[40px] bg-gradient-to-tr from-violet-700/40 via-violet-500/20 to-cyan-400/30 blur-3xl"></div>

      <div className="relative animate-floaty" style={{ transformStyle: 'preserve-3d' }}>
        {/* Browser chrome */}
        <div className="rounded-2xl overflow-hidden border border-white/10 bg-[#15102a]/95 backdrop-blur-xl shadow-2xl">
          <div className="flex items-center gap-2 px-4 py-3 border-b border-white/5 bg-[#100a22]">
            <span className="h-2.5 w-2.5 rounded-full bg-rose-400/80"></span>
            <span className="h-2.5 w-2.5 rounded-full bg-amber-300/80"></span>
            <span className="h-2.5 w-2.5 rounded-full bg-emerald-400/80"></span>
            <div className="ml-3 text-[11px] font-mono text-violet-200/60 truncate">
              checkout.minhaloja.com.br/pedido/A-9821
            </div>
            <div className="ml-auto text-[10px] text-violet-200/40 flex items-center gap-1">
              <I.Lock size={11} sw={2} /> SSL
            </div>
          </div>

          {/* Body */}
          <div className="p-5 grid gap-3.5 text-[13px]">
            {/* Stepper */}
            <div className="flex items-center gap-2 text-[11px]">
              <span className="flex items-center gap-1.5 text-violet-100">
                <span className="h-4 w-4 rounded-full bg-violet-500 grid place-items-center text-white"><I.Check size={10} sw={3}/></span>
                Identificação
              </span>
              <span className="flex-1 h-px bg-violet-300/30"></span>
              <span className="flex items-center gap-1.5 text-violet-100">
                <span className="h-4 w-4 rounded-full bg-violet-500 grid place-items-center text-white"><I.Check size={10} sw={3}/></span>
                Endereço
              </span>
              <span className="flex-1 h-px bg-violet-400/50"></span>
              <span className="flex items-center gap-1.5 text-white font-medium">
                <span className="h-4 w-4 rounded-full bg-violet-500 grid place-items-center text-white text-[9px] font-bold leading-none ring-2 ring-violet-400/30">3</span>
                Pagamento
              </span>
            </div>

            {/* Trust badges */}
            <div className="flex flex-wrap gap-1.5 text-[10px]">
              <span className="flex items-center gap-1 px-2 py-1 rounded-full bg-white/[0.04] border border-white/8 text-violet-100/70"><I.Lock size={10} sw={2.2} className="text-emerald-300"/> SSL 256-bit</span>
              <span className="flex items-center gap-1 px-2 py-1 rounded-full bg-white/[0.04] border border-white/8 text-violet-100/70"><I.ShieldCheck size={11} sw={2} className="text-cyan-300"/> Antifraude Clearsale</span>
              <span className="flex items-center gap-1 px-2 py-1 rounded-full bg-white/[0.04] border border-white/8 text-violet-100/70"><I.CreditCard size={11} sw={2} className="text-violet-300"/> Mercado Pago</span>
            </div>

            <div className="text-[15px] font-semibold text-white tracking-tight mt-0.5">Como você quer pagar?</div>

            {/* PIX — selected */}
            <div className="rounded-xl border border-violet-400/60 bg-violet-500/[0.12] p-3 flex items-center gap-3 shadow-glow-violet-sm">
              <span className="h-9 w-9 shrink-0 rounded-lg bg-gradient-to-br from-violet-500 to-violet-700 grid place-items-center">
                <img src="/lp/assets/pix.png" alt="Pix" className="h-5 w-5" style={{ filter: 'brightness(0) invert(1)' }} />
              </span>
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-1.5 flex-wrap">
                  <span className="text-violet-50 font-semibold text-[13px]">PIX</span>
                  <span className="text-[9px] font-bold uppercase tracking-wide px-1.5 py-0.5 rounded bg-emerald-400/15 border border-emerald-300/40 text-emerald-300">5% OFF</span>
                  <span className="text-[10px] text-violet-200/55">· aprovação imediata</span>
                </div>
                <div className="mt-0.5 flex items-baseline gap-2">
                  <span className="text-[17px] font-semibold text-white tabular-nums">R$ 1.153,97</span>
                  <span className="text-[11px] text-violet-300/50 line-through tabular-nums">R$ 1.214,70</span>
                </div>
                <div className="text-[10px] text-emerald-300/90">Você economiza R$ 60,74</div>
              </div>
              <span className="h-4 w-4 shrink-0 rounded-full border-2 border-violet-400 grid place-items-center">
                <span className="h-2 w-2 rounded-full bg-violet-400"></span>
              </span>
            </div>

            {/* Cartão */}
            <div className="rounded-xl border border-white/10 bg-white/[0.03] p-3 flex items-center gap-3">
              <span className="h-9 w-9 shrink-0 rounded-lg bg-white/[0.05] border border-white/10 grid place-items-center text-violet-200/70"><I.CreditCard size={16} sw={1.8}/></span>
              <div className="flex-1 min-w-0">
                <div className="text-violet-100 font-medium text-[13px]">Cartão de crédito</div>
                <div className="text-[10px] text-violet-200/50">Em até 18x · R$ 1.214,70</div>
              </div>
              <span className="h-4 w-4 shrink-0 rounded-full border-2 border-white/20"></span>
            </div>

            {/* Boleto */}
            <div className="rounded-xl border border-white/10 bg-white/[0.03] p-3 flex items-center gap-3">
              <span className="h-9 w-9 shrink-0 rounded-lg bg-white/[0.05] border border-white/10 grid place-items-center text-violet-200/70">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
              </span>
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-1.5">
                  <span className="text-violet-100 font-medium text-[13px]">Boleto faturado</span>
                  <span className="text-[9px] font-medium px-1.5 py-0.5 rounded bg-violet-500/15 border border-violet-300/30 text-violet-200">Exclusivo PJ</span>
                </div>
                <div className="text-[10px] text-violet-200/50">Pague em 30, 60 ou 90 dias · R$ 1.214,70</div>
              </div>
              <span className="h-4 w-4 shrink-0 rounded-full border-2 border-white/20"></span>
            </div>

            {/* Voltar */}
            <button className="mt-0.5 inline-flex items-center gap-1.5 self-start px-3.5 py-2 rounded-lg border border-white/10 bg-white/[0.03] text-[12px] text-violet-100/80 hover:border-violet-300/40 transition-colors">
              <I.ArrowRight size={13} className="rotate-180"/> Voltar
            </button>
          </div>
        </div>

        {/* Floating chip — B2B */}
        <div className="absolute -left-10 top-24 hidden md:flex items-center gap-2 px-3 py-2 rounded-xl bg-[#1a1135]/90 border border-violet-400/40 backdrop-blur-xl shadow-glow-violet-sm">
          <I.Building2 size={14} className="text-violet-300"/>
          <span className="text-[11px]">CNPJ detectado · ativando PJ</span>
        </div>

        {/* Floating chip — approved */}
        <div className="absolute -right-8 bottom-24 hidden md:flex items-center gap-2 px-3 py-2 rounded-xl bg-emerald-500/15 border border-emerald-300/40 backdrop-blur-xl">
          <span className="h-2 w-2 rounded-full bg-emerald-400 animate-pulse"></span>
          <span className="text-[11px] text-emerald-200">Pagamento aprovado em 1.2s</span>
        </div>

        {/* Live counter chip — reframed for personalization */}
        <div className="absolute -right-10 -top-10 hidden md:block">
          <LiveROICounter />
        </div>
      </div>
    </div>
  );
}

function Hero() {
  return (
    <section className="relative min-h-screen overflow-hidden">
      {/* Blobs */}
      <div className="absolute inset-0 -z-10">
        <div className="mesh-blob bg-violet-700 w-[520px] h-[520px] -top-32 -left-24 animate-drift1"></div>
        <div className="mesh-blob bg-cyan-400/60 w-[420px] h-[420px] top-1/3 -right-24 animate-drift2"></div>
        <div className="mesh-blob bg-violet-500 w-[600px] h-[600px] bottom-[-200px] left-[15%] animate-drift3 opacity-40"></div>
        <div className="mesh-blob bg-fuchsia-500/40 w-[360px] h-[360px] top-10 right-1/3 animate-drift4"></div>
        <div className="absolute inset-0 dot-grid opacity-[0.35]"></div>
        <div className="absolute inset-0 bg-gradient-to-b from-transparent via-transparent to-ink"></div>
      </div>

      <Nav />

      <div className="max-w-7xl mx-auto px-6 lg:px-10 pt-10 lg:pt-16 pb-24">
        <div className="grid lg:grid-cols-[1.15fr_1fr] gap-10 lg:gap-16 items-center">
          {/* Left */}
          <div>
            <div className="inline-flex flex-wrap items-center gap-2 px-3 py-1.5 rounded-full border border-violet-300/25 bg-violet-500/5 text-[12px] text-violet-200 mb-7 max-w-full">
              <span className="h-1.5 w-1.5 rounded-full bg-cyan-400 animate-pulse"></span>
              <img src="/brand/logo-dark.png" alt="CartROI" className="h-3.5 w-auto" />
              <span className="text-violet-300/40">·</span>
              <span className="text-violet-200/70">checkout que paga por si</span>
            </div>

            <h1 className="font-semibold tracking-[-0.035em] leading-[0.92] text-[44px] sm:text-6xl lg:text-[78px]">
              <MaskReveal text="Cada loja" />{' '}
              <MaskReveal text="é única." delay={0.18} wordClassName="grad-cyan" />
              <br />
              <span className="text-violet-100"><MaskReveal text="Seu checkout" delay={0.45} /></span>
              <br />
              <MaskReveal text="deveria ser também." delay={0.72} />
            </h1>

            <p className="mt-7 text-lg lg:text-xl text-violet-100/75 max-w-xl leading-relaxed">
              O <span className="text-white font-semibold">cartROI</span> é o primeiro checkout brasileiro 100% personalizável — cor, copy, fluxo, blocos, regras PF/PJ. Você desenha o seu, não herda o de ninguém.
            </p>

            <div className="mt-9 flex flex-col sm:flex-row gap-3">
              <a href="https://app.cartroi.com/login" className="shine-btn group inline-flex items-center justify-center gap-2 px-6 py-3.5 rounded-xl bg-gradient-to-b from-violet-500 to-violet-700 text-white font-medium shadow-glow-violet hover:shadow-[0_0_80px_-5px_rgba(167,139,250,0.6)] transition-shadow">
                Personalizar meu checkout
                <I.ArrowRight size={18} className="group-hover:translate-x-0.5 transition-transform"/>
              </a>
              <a href="https://app.cartroi.com/login" className="inline-flex items-center justify-center gap-2 px-6 py-3.5 rounded-xl border border-white/10 hover:border-violet-300/40 hover:bg-white/[0.03] text-violet-100 font-medium transition-colors">
                Ver demo ao vivo
              </a>
            </div>

            <div className="mt-7 flex flex-wrap items-center gap-x-4 gap-y-2 text-[13px] text-violet-200/70">
              <span className="flex items-center gap-1.5"><I.Check size={14} sw={2.5} className="text-cyan-300"/> 24 blocos arrastáveis</span>
              <span className="text-violet-200/20">·</span>
              <span className="flex items-center gap-1.5"><I.Check size={14} sw={2.5} className="text-cyan-300"/> Regras por público</span>
              <span className="text-violet-200/20">·</span>
              <span className="flex items-center gap-1.5"><I.Check size={14} sw={2.5} className="text-cyan-300"/> HTML/CSS inline</span>
            </div>

            <div className="mt-5 inline-flex items-center gap-2 px-3 py-1.5 rounded-full bg-white/[0.03] border border-white/10 text-[12px] text-violet-100/80">
              <span className="h-2 w-2 rounded-full bg-emerald-400 animate-pulse"></span>
              +1.247 lojistas já personalizaram o checkout deles esta semana
            </div>
          </div>

          {/* Right mockup */}
          <div className="relative">
            <HeroMockup />
          </div>
        </div>
      </div>

      {/* Marquee strip */}
      <Marquee />
    </section>
  );
}

function Nav() {
  return (
    <header className="max-w-7xl mx-auto px-6 lg:px-10 pt-6 flex items-center">
      <a href="#" className="flex items-center gap-3 group">
        <img src="/brand/logo-dark.png" alt="CartROI" className="h-7 w-auto" />
      </a>
      <nav className="hidden md:flex items-center gap-7 mx-auto text-[14px] text-violet-100/70">
        <a href="#editor" className="hover:text-white transition-colors">Produto</a>
        <a href="#pricing" className="hover:text-white transition-colors">Preços</a>
        <a href="#compare" className="hover:text-white transition-colors">Comparativo</a>
        <a href="#faq" className="hover:text-white transition-colors">FAQ</a>
        <a href="#" className="hover:text-white transition-colors">Docs</a>
      </nav>
      <div className="ml-auto md:ml-0 flex items-center gap-2.5">
        <a href="https://app.cartroi.com/login" className="hidden sm:inline text-[14px] text-violet-100/80 hover:text-white px-3 py-2">Entrar</a>
        <a href="https://app.cartroi.com/login" className="shine-btn inline-flex items-center gap-1.5 text-[14px] font-medium px-4 py-2 rounded-lg bg-white text-ink hover:bg-violet-50">
          Personalizar grátis
        </a>
      </div>
    </header>
  );
}

const PLATFORMS = [
  { name: 'Nuvemshop',    src: '/logos/nuvemshop.png' },
  { name: 'WooCommerce',  src: '/logos/woocommerce.png' },
  { name: 'Shopify',      src: '/logos/shopify.png' },
  { name: 'VTEX',         src: '/logos/vtex.png' },
  { name: 'Mercado Pago', src: '/logos/mercado-pago.png' },
  { name: 'Asaas',        src: '/logos/asaas.png' },
  { name: 'ZentriPay',    src: '/logos/zentry.png' },
  { name: 'Stripe',       src: '/logos/stripe.png' },
  { name: 'PIX',          src: '/logos/pix.svg' },
  { name: 'Melhor Envio', src: '/logos/melhor-envio.png' },
  { name: 'Correios',     src: '/logos/correios.png' },
];
function Marquee() {
  const list = [...PLATFORMS, ...PLATFORMS, ...PLATFORMS];
  return (
    <div className="relative mt-2 border-y border-white/5 bg-gradient-to-r from-transparent via-violet-500/[0.04] to-transparent">
      <div className="overflow-hidden no-scrollbar py-6 [mask-image:linear-gradient(90deg,transparent,#000_10%,#000_90%,transparent)]">
        <div className="flex w-max gap-12 animate-marquee">
          {list.map((p, i) => (
            <div key={i} className="flex items-center gap-2.5 text-violet-100/70 hover:text-white transition-colors whitespace-nowrap">
              <span className="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-white/90 p-1.5">
                <img src={p.src} alt={p.name} className="h-full w-full object-contain" />
              </span>
              <span className="text-[15px] tracking-tight font-medium">{p.name}</span>
            </div>
          ))}
        </div>
      </div>
      <div className="text-center pb-5 text-[11px] uppercase tracking-[0.18em] text-violet-200/40">Funciona nativamente com</div>
    </div>
  );
}

Object.assign(window, { Hero, MaskReveal });
