/* global React */
const { useState: useState_h, useEffect: useEffect_h, useRef: useRef_h } = React;

/* =========================================================
   Hero, cinematic, green landscape, parallax, notify form
   ========================================================= */

/* Sticky live carbon counter that stays visible while scrolling past the hero */
function StickyCarbon() {
  const [carbon, setCarbon] = useState_h(0);
  const [visible, setVisible] = useState_h(false);

  useEffect_h(() => {
    const start = performance.now();
    const seed = 128_430;
    let raf = 0;
    const tick = (t) => {
      const elapsed = (t - start) / 1000;
      setCarbon(Math.round(seed + elapsed * 38));
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, []);

  useEffect_h(() => {
    const onScroll = () => setVisible(window.scrollY > 520);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  return (
    <div className={`sticky-carbon ${visible ? 'on' : ''}`} aria-hidden={!visible}>
      <span className="sc-dot" />
      <span className="sc-lbl mono">CO₂ AVOIDED · LIVE</span>
      <span className="sc-val mono">
        {carbon.toLocaleString('en-US')}<span className="sc-unit">g</span>
      </span>
      <style>{`
        .sticky-carbon {
          position: fixed;
          bottom: 24px; left: 50%;
          transform: translate(-50%, 20px);
          z-index: 40;
          display: inline-flex; align-items: center; gap: 14px;
          padding: 10px 18px;
          background: rgba(10,10,10,0.7);
          border: 1px solid var(--line-strong);
          border-radius: 999px;
          backdrop-filter: blur(14px) saturate(140%);
          -webkit-backdrop-filter: blur(14px) saturate(140%);
          opacity: 0;
          pointer-events: none;
          transition: opacity 0.45s var(--ease-out), transform 0.45s var(--ease-out);
          box-shadow: 0 20px 50px rgba(0,0,0,0.35);
        }
        .sticky-carbon.on { opacity: 1; transform: translate(-50%, 0); }
        .sc-dot {
          width: 8px; height: 8px; border-radius: 50%;
          background: var(--mint);
          box-shadow: 0 0 12px var(--mint);
          animation: pulse 2.4s var(--ease-out) infinite;
          flex-shrink: 0;
        }
        .sc-lbl { font-size: 10px; letter-spacing: 0.16em; color: var(--mint); }
        .sc-val {
          font-size: 14px; color: var(--ink-0); font-variant-numeric: tabular-nums;
          display: inline-flex; align-items: baseline; gap: 2px;
        }
        .sc-unit { font-size: 11px; color: var(--ink-2); margin-left: 2px; }
        @media (max-width: 520px) {
          .sticky-carbon { gap: 10px; padding: 9px 14px; }
          .sc-lbl { display: none; }
        }
      `}</style>
    </div>
  );
}

function Hero() {
  const ref = useRef_h(null);
  const [offset, setOffset] = useState_h(0);
  const [carbon, setCarbon] = useState_h(0);

  useEffect_h(() => {
    let raf = 0;
    const tick = () => {
      if (ref.current) {
        const y = window.scrollY;
        setOffset(Math.min(y * 0.35, 400));
      }
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, []);

  // Live carbon counter — grams of CO2 avoided by green hosting vs avg site
  useEffect_h(() => {
    const start = performance.now();
    const seed = 128_430; // "already avoided" baseline
    let raf = 0;
    const tick = (t) => {
      const elapsed = (t - start) / 1000; // seconds
      // 2.7g per visitor avg, ~14 visitors/second across the network
      const inc = elapsed * 38;
      setCarbon(Math.round(seed + inc));
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, []);

  return (
    <section id="top" ref={ref} className="hero">
      {/* Parallax landscape layer */}
      <div className="hero-sky" style={{ transform: `translate3d(0, ${offset * 0.2}px, 0)` }} />
      <div className="hero-hills" style={{ transform: `translate3d(0, ${offset * 0.4}px, 0)` }} />
      <div className="hero-turbines" style={{ transform: `translate3d(0, ${offset * 0.6}px, 0)` }}>
        <Turbine x="12%" scale={0.9} speed={4.2} />
        <Turbine x="28%" scale={1.2} speed={3.6} />
        <Turbine x="62%" scale={1.1} speed={4.8} />
        <Turbine x="84%" scale={0.85} speed={3.2} />
      </div>
      <div className="hero-fog" />
      <div className="hero-grain" />

      {/* Top strip: pre-launch chip + carbon counter */}
      <div className="hero-top container">
        <div className="prelaunch-chip">
          <span className="prelaunch-dot" />
          <span className="mono" style={{ fontSize: 11, letterSpacing: '0.14em' }}>PRE-LAUNCH · Q3 2026</span>
        </div>
        <div className="carbon-counter">
          <div className="carbon-counter-label mono">CO₂ AVOIDED BY REE · LIVE</div>
          <div className="carbon-counter-value">
            <span className="mono">{carbon.toLocaleString('en-US')}</span>
            <span className="carbon-unit">g</span>
          </div>
        </div>
      </div>

      {/* Main content */}
      <div className="hero-content container">
        <div className="hero-eyebrow eyebrow">The green web builder · Coming soon</div>
        <h1 className="display display-xl hero-headline">
          Build beautiful,<br/>
          <span className="green-highlight">green</span> websites<br/>
          in minutes.
        </h1>
        <p className="lede hero-lede">
          Ree is the first web builder designed to be <strong style={{color:'var(--ink-0)'}}>carbon-negative by default</strong>.
          Clean design, fast performance, great hosting, on infrastructure that gives more to the planet than it takes.
        </p>

        <div className="hero-cta">
          <NotifyForm big />
        </div>

        <div className="hero-meta">
          <div className="hero-meta-item">
            <div className="hero-meta-num mono">0.09g</div>
            <div className="hero-meta-lbl">CO₂ per page view · avg.</div>
          </div>
          <div className="hero-meta-divider" />
          <div className="hero-meta-item">
            <div className="hero-meta-num mono">3 min</div>
            <div className="hero-meta-lbl">From sign-up to live site</div>
          </div>
          <div className="hero-meta-divider" />
          <div className="hero-meta-item">
            <div className="hero-meta-num mono">100%</div>
            <div className="hero-meta-lbl">Renewable hosting</div>
          </div>
        </div>
      </div>

      {/* Scroll cue */}
      <div className="scroll-cue">
        <span className="mono" style={{ fontSize: 11, letterSpacing: '0.2em' }}>SCROLL</span>
        <span className="scroll-line" />
      </div>

      <style>{`
        .hero {
          position: relative;
          min-height: 100vh;
          padding-top: 120px;
          padding-bottom: 80px;
          overflow: hidden;
          isolation: isolate;
        }
        /* Landscape: layered radial/linear gradients mimicking the figma turbine field */
        .hero-sky {
          position: absolute; inset: -40px 0 40% 0; z-index: -4;
          background:
            radial-gradient(120% 80% at 50% 100%, rgba(159,232,112,0.18) 0%, rgba(159,232,112,0.00) 55%),
            linear-gradient(180deg, #020608 0%, #0a1a0f 60%, #0e2a12 100%);
        }
        .hero-hills {
          position: absolute; left: -4%; right: -4%; bottom: 0; height: 62%; z-index: -3;
          background:
            /* far hills */
            radial-gradient(120% 60% at 20% 100%, #11371b 0%, transparent 60%),
            radial-gradient(100% 60% at 80% 100%, #0f3119 0%, transparent 55%),
            /* field */
            linear-gradient(180deg, transparent 0%, rgba(18,60,28,0.0) 30%, #123c1c 55%, #0d2a14 100%);
        }
        .hero-hills::before {
          content: ""; position: absolute; inset: 0;
          background:
            /* grass texture stripes */
            repeating-linear-gradient(180deg, transparent 0, transparent 6px, rgba(255,255,255,0.012) 6px, rgba(255,255,255,0.012) 7px),
            radial-gradient(60% 30% at 50% 90%, rgba(159,232,112,0.10) 0%, transparent 70%);
          pointer-events: none;
        }
        .hero-turbines {
          position: absolute; inset: 0; z-index: -2;
          pointer-events: none;
        }
        .hero-fog {
          position: absolute; inset: 0; z-index: -1;
          background:
            radial-gradient(80% 60% at 50% 20%, rgba(10,10,10,0.0) 0%, rgba(10,10,10,0.75) 100%),
            linear-gradient(180deg, rgba(10,10,10,0.55) 0%, rgba(10,10,10,0.0) 25%, rgba(10,10,10,0.0) 55%, rgba(10,10,10,0.45) 100%);
        }
        .hero-grain {
          position: absolute; inset: 0; z-index: 0; pointer-events: none;
          opacity: 0.08; mix-blend-mode: overlay;
          background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/></filter><rect width='100%25' height='100%25' filter='url(%23n)' opacity='0.5'/></svg>");
        }

        .hero-top {
          position: relative;
          z-index: 2;
          display: flex;
          justify-content: space-between;
          align-items: flex-start;
          margin-bottom: clamp(40px, 7vw, 80px);
        }
        .prelaunch-chip {
          display: inline-flex; align-items: center; gap: 10px;
          padding: 8px 14px;
          border-radius: 999px;
          background: rgba(255,255,255,0.04);
          border: 1px solid var(--line-strong);
          color: var(--ink-1);
          backdrop-filter: blur(8px);
          -webkit-backdrop-filter: blur(8px);
        }
        .prelaunch-dot {
          width: 6px; height: 6px; border-radius: 50%;
          background: var(--lime);
          box-shadow: 0 0 12px var(--lime);
          animation: pulse 2.4s var(--ease-out) infinite;
        }
        .carbon-counter {
          text-align: right;
          padding: 10px 16px;
          border-left: 1px solid var(--line-strong);
        }
        .carbon-counter-label { font-size: 10px; letter-spacing: 0.18em; color: var(--mint); margin-bottom: 4px; }
        .carbon-counter-value { display: flex; align-items: baseline; justify-content: flex-end; gap: 4px; color: var(--ink-0); }
        .carbon-counter-value .mono { font-size: 22px; font-weight: 500; font-variant-numeric: tabular-nums; }
        .carbon-unit { font-size: 12px; color: var(--ink-2); font-family: var(--font-mono); }

        .hero-content { position: relative; z-index: 2; }
        .hero-eyebrow { margin-bottom: 28px; }
        .hero-headline {
          color: var(--ink-0);
          margin: 0 0 32px;
          text-shadow: 0 4px 40px rgba(0,0,0,0.4);
        }
        .green-highlight {
          color: var(--mint);
          font-style: italic;
          font-weight: 500;
          letter-spacing: -0.04em;
        }
        .hero-lede { margin: 0 0 44px; font-size: clamp(17px, 1.3vw, 20px); }
        .hero-cta { margin-bottom: 64px; }

        .hero-meta {
          display: flex;
          align-items: center;
          gap: 28px;
          flex-wrap: wrap;
          padding-top: 32px;
          border-top: 1px solid var(--line);
          max-width: 720px;
        }
        .hero-meta-num { font-size: 22px; color: var(--lime); font-weight: 500; margin-bottom: 2px; }
        .hero-meta-lbl { font-size: 12px; color: var(--ink-2); letter-spacing: 0.02em; }
        .hero-meta-divider {
          width: 1px; height: 28px; background: var(--line-strong);
        }

        .scroll-cue {
          position: absolute;
          bottom: 24px;
          left: 50%;
          transform: translateX(-50%);
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 8px;
          color: var(--ink-2);
          z-index: 2;
        }
        .scroll-line {
          width: 1px; height: 40px;
          background: linear-gradient(180deg, var(--lime), transparent);
          animation: scroll-drop 2s var(--ease-out) infinite;
        }
        @keyframes scroll-drop {
          0% { transform: scaleY(0); transform-origin: top; }
          60% { transform: scaleY(1); transform-origin: top; }
          100% { transform: scaleY(1); transform-origin: bottom; transform: scaleY(0); }
        }

        @media (max-width: 720px) {
          .hero-top { flex-direction: column; gap: 18px; align-items: flex-start; }
          .carbon-counter { text-align: left; border-left: 0; padding: 0; }
          .carbon-counter-value { justify-content: flex-start; }
          .hero-meta { gap: 20px; }
          .hero-meta-divider { display: none; }
        }
      `}</style>
    </section>
  );
}

function Turbine({ x, scale = 1, speed = 4 }) {
  return (
    <div className="turbine" style={{ left: x, transform: `scale(${scale})`, transformOrigin: 'bottom center' }}>
      <svg width="120" height="220" viewBox="0 0 120 220" fill="none">
        {/* tower */}
        <path d="M57 220 L63 220 L61.5 60 L58.5 60 Z" fill="#cdd5d0" opacity="0.85"/>
        {/* nacelle */}
        <ellipse cx="60" cy="58" rx="6" ry="4" fill="#dde3de"/>
        {/* rotating hub + blades */}
        <g className="turbine-rotor" style={{ transformOrigin: '60px 58px', animation: `turbineSpin ${speed}s linear infinite` }}>
          <circle cx="60" cy="58" r="3" fill="#f5f8f4"/>
          {[0, 120, 240].map((a) => (
            <path key={a} d="M60 58 L60 4 Q62 2 64 4 L60 58 Z" fill="#e8ede7" opacity="0.92"
              transform={`rotate(${a} 60 58)`} />
          ))}
        </g>
      </svg>
      <style>{`
        .turbine { position: absolute; bottom: 18%; opacity: 0.85; filter: drop-shadow(0 10px 30px rgba(0,0,0,0.4)); }
        @keyframes turbineSpin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
      `}</style>
    </div>
  );
}

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