// Section primitives — English copy, tightened hero.
// Depends on window.{Wordmark, Pill, QBButton, SportIcon, QB_SPORTS}

const { useState, useEffect, useRef } = React;

function useOnScreen(options = { threshold: 0.15 }) {
  const ref = useRef(null);
  const [seen, setSeen] = useState(false);
  useEffect(() => {
    if (!ref.current) return;
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setSeen(true); io.disconnect(); }
    }, options);
    io.observe(ref.current);
    return () => io.disconnect();
  }, []);
  return [ref, seen];
}

function useCountUp(target, active, duration = 1400) {
  const [v, setV] = useState(0);
  useEffect(() => {
    if (!active) return;
    const start = performance.now();
    let raf;
    const tick = (t) => {
      const p = Math.min(1, (t - start) / duration);
      const e = 1 - Math.pow(1 - p, 3);
      setV(target * e);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [active, target, duration]);
  return v;
}

function Icon({ name, size = 20, stroke = 1.75 }) {
  const p = {
    zap: 'M13 2 3 14h9l-1 8 10-12h-9l1-8Z',
    check: 'm5 12 5 5 9-11',
    arrow: 'M5 12h14M13 6l6 6-6 6',
    sparkle: 'M12 4l2 5 5 2-5 2-2 5-2-5-5-2 5-2 2-5Z',
    shield: 'M12 3 4 6v6c0 5 3.5 8.5 8 9 4.5-.5 8-4 8-9V6l-8-3Z',
    card: 'M3 7h18v10a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7Zm0 4h18M7 15h4',
    users: 'M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2m8-10a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm11 10v-2a4 4 0 0 0-3-3.87M15 3.13a4 4 0 0 1 0 7.75',
    calendar: 'M3 6h18v15H3zM3 6l0-2h4M21 6l0-2h-4M8 2v4m8-4v4M3 10h18',
    phone: 'M5 4h4l2 5-3 2a11 11 0 0 0 5 5l2-3 5 2v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2Z',
    chart: 'M3 20h18M7 16V10M12 16V6M17 16v-8',
    bell: 'M6 8a6 6 0 1 1 12 0c0 7 3 7 3 9H3c0-2 3-2 3-9Zm4 13a2 2 0 0 0 4 0',
    trend: 'M3 17l6-6 4 4 8-8M21 7h-4m4 0v4',
    clock: 'M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18Zm0 4v5l3 2',
    download: 'M12 3v12m0 0-4-4m4 4 4-4M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-2',
    swap: 'M8 3 4 7l4 4M4 7h16M16 21l4-4-4-4M20 17H4',
    finger: 'M9 11V5a2 2 0 1 1 4 0v6m0-3a2 2 0 1 1 4 0v4m0-2a2 2 0 1 1 4 0v5a5 5 0 0 1-5 5h-3a5 5 0 0 1-4-2l-4-6a2 2 0 0 1 3-3l2 2',
  };
  return (
    <svg viewBox="0 0 24 24" width={size} height={size} fill="none" stroke="currentColor"
      strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round">
      <path d={p[name]}/>
    </svg>
  );
}

function Nav() {
  const [open, setOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const h = () => setScrolled(window.scrollY > 20);
    h(); window.addEventListener('scroll', h);
    return () => window.removeEventListener('scroll', h);
  }, []);
  const items = [
    { l: 'Features',   h: '#features' },
    { l: 'Pricing Edge', h: '#pricing-edge' },
    { l: 'Simple',     h: '#simple' },
    { l: 'Migrate',    h: '#migrate' },
    { l: 'Pricing',    h: '#pricing' },
  ];
  return (
    <nav style={{
      position:'sticky', top: 0, zIndex: 60,
      padding: scrolled ? '10px 16px' : '18px 16px',
      transition: 'padding .3s var(--ease-out)',
    }}>
      <div className="glass" style={{
        maxWidth: 1180, margin:'0 auto',
        display:'flex', alignItems:'center', gap: 16,
        padding:'10px 14px 10px 22px', borderRadius: 999,
        background: scrolled ? 'rgba(6,8,15,0.72)' : 'rgba(6,8,15,0.45)',
        border: '1px solid rgba(255,255,255,0.08)',
      }}>
        <a href="#top" style={{ display:'inline-flex', alignItems:'center', gap: 10 }}>
          <span style={{
            width: 26, height: 26, borderRadius: 7,
            background:'linear-gradient(135deg, #4d7dff, #7c5cff)',
            boxShadow:'0 0 20px rgba(124,92,255,0.55)',
            display:'grid', placeItems:'center', color:'#fff', fontSize: 13, fontWeight: 700,
          }}>q</span>
          <window.Wordmark style={{ fontSize: 18, color: '#fff' }}/>
        </a>
        <div className="nav-links" style={{ display:'flex', gap: 2, marginLeft: 8, flex: 1 }}>
          {items.map(i => (
            <a key={i.l} href={i.h} style={{
              padding:'8px 14px', borderRadius: 999, fontSize: 13.5, fontWeight: 500,
              color:'rgba(247,248,250,0.72)',
            }}>{i.l}</a>
          ))}
        </div>
        <a href="#" className="nav-signin" style={{ fontSize: 13.5, fontWeight: 500, color:'rgba(247,248,250,0.80)' }}>Sign in</a>
        <window.QBButton size="sm" icon={<Icon name="arrow" size={14}/>}>Get started</window.QBButton>
        <button className="nav-burger" onClick={() => setOpen(!open)} aria-label="menu" style={{
          display:'none', background:'rgba(255,255,255,0.06)', border:'1px solid rgba(255,255,255,0.12)',
          width: 36, height: 36, borderRadius: 999, color:'#fff', cursor:'pointer',
        }}>
          <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2">
            {open ? <path d="M6 6l12 12M6 18L18 6"/> : <path d="M4 7h16M4 12h16M4 17h16"/>}
          </svg>
        </button>
      </div>
      {open && (
        <div className="glass" style={{
          maxWidth: 1180, margin:'10px auto 0', borderRadius: 20,
          background:'rgba(6,8,15,0.92)', border:'1px solid rgba(255,255,255,0.08)',
          padding: 12, display:'flex', flexDirection:'column',
        }}>
          {items.map(i => (
            <a key={i.l} href={i.h} onClick={() => setOpen(false)} style={{
              padding:'12px 14px', borderRadius: 12, fontSize: 15, color:'#fff',
            }}>{i.l}</a>
          ))}
          <div style={{ display:'flex', gap: 8, padding: 8 }}>
            <a href="#" style={{ flex:1, textAlign:'center', padding:'12px', borderRadius: 999,
              border:'1px solid rgba(255,255,255,0.14)', color:'#fff', fontSize: 14, fontWeight: 600 }}>Sign in</a>
            <window.QBButton size="md" style={{ flex: 1 }}>Get started</window.QBButton>
          </div>
        </div>
      )}
      <style>{`
        @media (max-width: 900px) {
          .nav-links, .nav-signin { display: none !important; }
          .nav-burger { display: grid !important; place-items: center; }
        }
      `}</style>
    </nav>
  );
}

function Hero() {
  return (
    <section id="top" className="hero-dark hero-section" style={{
      position:'relative', overflow:'hidden',
      paddingTop: 40, paddingBottom: 180, marginTop: -80,
    }}>
      <div style={{
        position:'absolute', inset:0, pointerEvents:'none', opacity: 0.4,
        backgroundImage: 'linear-gradient(rgba(255,255,255,0.05) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.05) 1px, transparent 1px)',
        backgroundSize: '56px 56px',
        maskImage: 'radial-gradient(80% 60% at 50% 40%, black, transparent)',
        WebkitMaskImage: 'radial-gradient(80% 60% at 50% 40%, black, transparent)',
      }}/>
      <FloatingOrbs/>

      <div className="hero-grid" style={{
        position:'relative', maxWidth: 1180, margin:'0 auto', padding: '90px 24px 0',
        display:'grid', gridTemplateColumns:'1.05fr 1fr', gap: 56, alignItems:'center',
      }}>
        <div style={{ animation: 'qb-fadeUp .9s var(--ease-out) both' }}>
          <window.Pill tone="blue" style={{ marginBottom: 20 }}>
            <Icon name="sparkle" size={12}/>
            <span style={{ fontWeight: 500 }}>New</span>
            <span style={{ opacity: 0.6 }}>·</span>
            <span>2.2% payments. Setup in 3 clicks.</span>
          </window.Pill>
          <h1 style={{
            margin:'0 0 20px', color:'#fff',
            fontFamily:'var(--font-display)', fontWeight: 600,
            fontSize:'clamp(34px, 4.8vw, 60px)',
            lineHeight: 1.03, letterSpacing:'-0.035em',
          }}>
            You run the gym.<br/>
            <span style={{
              background:'linear-gradient(90deg, #8cb0ff 0%, #d070ff 50%, #4dd4ff 100%)',
              WebkitBackgroundClip:'text', backgroundClip:'text', color:'transparent',
            }}>We run the software.</span>
          </h1>
          <p style={{
            maxWidth: 540, color:'rgba(247,248,250,0.72)',
            fontSize: 17, lineHeight: 1.55, margin: 0,
          }}>
            Schedules, families, payments, attendance — all handled.
            You get back the evenings you used to spend fighting spreadsheets and clunky CRMs.
          </p>
          <div className="hero-cta-row" style={{ display:'flex', gap: 10, marginTop: 28, flexWrap:'wrap' }}>
            <window.QBButton size="lg" icon={<Icon name="arrow" size={16}/>}>Start free</window.QBButton>
            <window.QBButton size="lg" variant="ghost">
              <span style={{
                display:'inline-flex', width: 20, height: 20, borderRadius: 999,
                background:'rgba(255,255,255,0.12)', alignItems:'center', justifyContent:'center',
                marginRight: 2, fontSize: 9,
              }}>▶</span>
              Watch 90-sec demo
            </window.QBButton>
          </div>
          <div style={{
            display:'flex', gap: 20, marginTop: 32, flexWrap:'wrap',
            color:'rgba(247,248,250,0.50)', fontSize: 11.5,
            fontFamily:'var(--font-mono)', letterSpacing:'0.08em', textTransform:'uppercase',
          }}>
            <span>No credit card</span><span>·</span>
            <span>1-click migration</span><span>·</span>
            <span>Cancel anytime</span>
          </div>
        </div>
        <div className="hero-cube-wrap" style={{ display:'grid', placeItems:'center', animation: 'qb-fadeIn 1.2s var(--ease-out) both' }}>
          <window.QubeHero size={380}/>
        </div>
      </div>

      <style>{`
        @keyframes qb-fadeUp { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: none; } }
        @keyframes qb-fadeIn { from { opacity: 0; } to { opacity: 1; } }
        @media (max-width: 900px) {
          .hero-grid { grid-template-columns: 1fr !important; gap: 28px !important; padding-top: 40px !important; }
          .hero-section { padding-bottom: 120px !important; }
          .hero-cta-row > * { flex: 1 1 auto; }
          .hero-cube-wrap { transform: scale(0.78); transform-origin: center; margin: -44px 0; }
        }
        @media (max-width: 420px) {
          .hero-cta-row { flex-direction: column; }
          .hero-cta-row > * { width: 100%; }
          .hero-cube-wrap { transform: scale(0.72); margin: -54px 0; }
        }
      `}</style>
    </section>
  );
}

function FloatingOrbs() {
  return (
    <>
      <div style={{
        position:'absolute', left:'-10%', top:'20%', width: 500, height: 500, borderRadius: 999,
        background: 'radial-gradient(circle, rgba(57,104,255,0.45), transparent 60%)',
        filter: 'blur(40px)', animation: 'qb-orb1 14s ease-in-out infinite',
      }}/>
      <div style={{
        position:'absolute', right:'-5%', top:'40%', width: 420, height: 420, borderRadius: 999,
        background: 'radial-gradient(circle, rgba(124,92,255,0.40), transparent 60%)',
        filter: 'blur(40px)', animation: 'qb-orb2 18s ease-in-out infinite',
      }}/>
      <div style={{
        position:'absolute', right:'30%', bottom:'-10%', width: 380, height: 380, borderRadius: 999,
        background: 'radial-gradient(circle, rgba(77,212,255,0.28), transparent 60%)',
        filter: 'blur(40px)', animation: 'qb-orb3 20s ease-in-out infinite',
      }}/>
      <style>{`
        @keyframes qb-orb1 { 0%,100% { transform: translate(0,0); } 50% { transform: translate(40px,-30px); } }
        @keyframes qb-orb2 { 0%,100% { transform: translate(0,0); } 50% { transform: translate(-40px,40px); } }
        @keyframes qb-orb3 { 0%,100% { transform: translate(0,0); } 50% { transform: translate(60px,-40px); } }
      `}</style>
    </>
  );
}

function LogoStrip() {
  const names = ['AEROS','VITTORIA','ORBITAL','K-GYM','PROKINEX','NORDIC','APEX','FALCON','OLYMPIC','AXIS','ATLAS','RUBY'];
  const row = [...names, ...names];
  return (
    <section style={{ padding:'40px 0 80px', background:'var(--ink-950)', borderTop:'1px solid rgba(255,255,255,0.06)' }}>
      <p className="caption" style={{ textAlign:'center', color:'rgba(247,248,250,0.45)', marginBottom: 28 }}>
        Trusted by gyms across the US
      </p>
      <div style={{
        maskImage:'linear-gradient(90deg, transparent, black 10%, black 90%, transparent)',
        WebkitMaskImage:'linear-gradient(90deg, transparent, black 10%, black 90%, transparent)',
        overflow:'hidden',
      }}>
        <div style={{ display:'flex', gap: 64, animation:'qb-marquee 40s linear infinite', width:'max-content' }}>
          {row.map((n, i) => (
            <span key={i} style={{
              color:'rgba(247,248,250,0.55)', fontFamily:'var(--font-display)',
              fontWeight: 600, letterSpacing:'0.12em', fontSize: 15, whiteSpace:'nowrap',
            }}>{n}</span>
          ))}
        </div>
      </div>
      <style>{`
        @keyframes qb-marquee { from { transform: translateX(0); } to { transform: translateX(-50%); } }
      `}</style>
    </section>
  );
}

window.QBNav = Nav;
window.QBHero = Hero;
window.QBLogoStrip = LogoStrip;
window.QBIcon = Icon;
window.useOnScreen = useOnScreen;
window.useCountUp = useCountUp;
