/* ────────────────────────────────────────────────────────────
   Section 4 - The Impact on Geothermal
   "Axion's system doesn't just lower drilling costs. It changes
    the scale of what geothermal can become."
   4 stat cards in a 2x2 or 4-up grid.
   ──────────────────────────────────────────────────────────── */

function SectionImpact() {
  // Scroll-zoom: U.S. geothermal heatmap background.
  //
  // Mirrors the Section 07 (Domain expansion) capsule treatment:
  // the bg starts tight (~1.25×) when the section is entering the
  // viewport and pulls back to (~1.00×) by the time it exits the
  // top. Smoothstep easing on the progress curve so the change
  // feels deliberate, not linear; Lenis already smooths the scroll
  // input so we don't layer another time-based easing on top.
  //
  // We never go BELOW 1.0 because the bg is height-locked to the
  // wrapper (100%) - shrinking would expose the section background
  // above/below. The wrapper has overflow: hidden (via the mask)
  // so the 1.25× over-scale stays clipped to the section frame.
  const bgRef = React.useRef(null);
  const sectionRef = React.useRef(null);
  React.useEffect(() => {
    const el = bgRef.current;
    const section = sectionRef.current;
    if (!el || !section) return;
    const prm = window.matchMedia &&
      window.matchMedia("(prefers-reduced-motion: reduce)");
    if (prm && prm.matches) {
      el.style.transform = "scale(1)";
      return;
    }
    let raf = 0;
    const tick = () => {
      raf = 0;
      const sec = sectionRef.current;
      const bg = bgRef.current;
      if (!sec || !bg) return;
      const r = sec.getBoundingClientRect();
      const vh = window.innerHeight || document.documentElement.clientHeight;
      // Progress 0→1 as the section travels from "just entering
      // viewport" to "just exiting the top". Same formula as the
      // Section 07 capsule.
      let p = 1 - (r.top + r.height) / (vh + r.height);
      p = Math.max(0, Math.min(1, p));
      const eased = p * p * (3 - 2 * p); // smoothstep
      const scale = 1.25 - eased * 0.25; // 1.25 → 1.00
      bg.style.transform = `scale(${scale.toFixed(4)})`;
    };
    const onScroll = () => { if (!raf) raf = requestAnimationFrame(tick); };
    tick();
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      if (raf) cancelAnimationFrame(raf);
    };
  }, []);

  const cards = [
    { stat: "90GW",   label: "U.S. CAPACITY · BY 2050",  body: "U.S. geothermal capacity achievable by 2050 with next-generation drilling technology." },
    { stat: "1,300×", label: "RESOURCE BEYOND THE GRID", body: "How much more geothermal resource exists beyond what's currently on the U.S. grid." },
    { stat: "10×",    label: "OUTPUT MULTIPLE",          body: "Energy output of a superhot rock well versus a conventional geothermal well." },
    { stat: "96%",    label: "DRILLING COST REDUCTION",  body: "Projected reduction in drilling cost per well with Axion's system." }
  ];

  return (
    <div ref={sectionRef} style={{ position: "relative", overflow: "hidden", isolation: "isolate" }}>
      {/* Heat map background - parallax-contained.
          Wrapper is absolute-inset with overflow: hidden so the
          120% oversized inner element's 10% buffer above/below the
          frame stays clipped. The inner element owns the parallax
          translate (±40px). ImageSettle wraps the wrapper for the
          entrance choreography (opacity + scale + y settle); the
          inner ref owns the per-scroll transform - separated so
          they never fight over `transform`. */}
      <ImageSettle aria-hidden="true" style={{
        position: "absolute",
        inset: 0,
        overflow: "hidden",
        pointerEvents: "none",
        zIndex: 0,
        WebkitMaskImage: "linear-gradient(180deg, rgba(0,0,0,0.25) 0%, rgba(0,0,0,0.85) 22%, rgba(0,0,0,1) 42%, rgba(0,0,0,0.55) 65%, rgba(0,0,0,0.15) 82%, rgba(0,0,0,0) 100%)",
        maskImage: "linear-gradient(180deg, rgba(0,0,0,0.25) 0%, rgba(0,0,0,0.85) 22%, rgba(0,0,0,1) 42%, rgba(0,0,0,0.55) 65%, rgba(0,0,0,0.15) 82%, rgba(0,0,0,0) 100%)"
      }}>
        <div
          ref={bgRef}
          style={{
            position: "absolute",
            top: 0,
            left: 0,
            width: "100%",
            height: "100%",
            backgroundImage: "url(assets/imagery/section-4-geothermal-heatmap.png)",
            backgroundSize: "cover",
            backgroundPosition: "center center",
            backgroundRepeat: "no-repeat",
            opacity: 0.85,
            transformOrigin: "center center",
            willChange: "transform"
          }}
        />
      </ImageSettle>
      {/* Mild darkening scrim - ~7% so the BG reads slightly more recessed
          without losing its detail. */}
      <div aria-hidden="true" style={{
        position: "absolute", inset: 0,
        background: "rgba(0,0,0,0.07)",
        pointerEvents: "none",
        zIndex: 1
      }} />

      {/* Ambient particle field - same atmospheric treatment as
          Section 02 (Domain expansion): bumped density + speedScale
          for a slightly livelier read over imagery. Placed after the
          scrim in DOM order so it paints above the bg + scrim layers
          but stays below the SectionFrame content. */}
      {typeof PtParticles !== "undefined" &&
        <PtParticles style={{ zIndex: 1 }} density={0.00437} speedScale={1.25} />
      }

      <SectionFrame index="04" plate="IV." dispatch="Scale of opportunity · § 04" bg="transparent">
        <SectionEyebrow index="04" label="Scale of opportunity" />

      <SectionHeading size="lg">
        Axion's system doesn't just lower drilling costs. It changes the scale of what geothermal can <span style={{ color: "#D4A24C" }}>become</span>.
      </SectionHeading>

      <div className="ax-impact-grid" style={{
        marginTop: "var(--sp-fluid-xl)",
        display: "grid",
        gridTemplateColumns: "repeat(4, 1fr)",
        gap: 1,
        background: "rgba(237,234,226,0.10)",
        border: "1px solid rgba(237,234,226,0.10)",
        backdropFilter: "blur(2px)",
        WebkitBackdropFilter: "blur(2px)"
      }}>
        {cards.map((c, i) => (
          <div key={i} className="ax-impact-card" style={{
            background: "linear-gradient(180deg, rgba(15,15,16,0.55) 0%, rgba(15,15,16,0.78) 100%)",
            padding: "var(--sp-fluid-loose) var(--sp-fluid-base)",
            display: "flex",
            flexDirection: "column",
            gap: 18,
            minHeight: 320
          }}>
            <div style={{
              fontFamily: "var(--font-mono)",
              fontSize: 11,
              color: "#D4A24C",
              letterSpacing: "0.20em",
              textTransform: "uppercase",
              display: "flex",
              alignItems: "center",
              gap: 8
            }}>
              <span style={{ width: 6, height: 6, background: "#D4A24C", display: "inline-block" }} />
              <span>0{i + 1} / 04</span>
            </div>
            <div style={{
              fontFamily: "var(--font-display)",
              fontSize: "var(--fs-fluid-display)",
              fontWeight: 700,
              lineHeight: 0.92,
              letterSpacing: "-0.03em",
              color: "#EDEAE2",
              whiteSpace: "nowrap"
            }}>
              <CountUp value={c.stat} />
            </div>
            <div style={{
              fontFamily: "var(--font-mono)",
              fontSize: 11,
              color: "#A8A8A2",
              letterSpacing: "0.18em",
              textTransform: "uppercase"
            }}>
              {c.label}
            </div>
            <div style={{
              fontFamily: "var(--font-body)",
              fontSize: 14,
              lineHeight: 1.55,
              color: "#A8A8A2",
              marginTop: "auto"
            }}>
              {c.body}
            </div>
          </div>
        ))}
      </div>
      <style>{`
        @media (max-width: 1024px) {
          .ax-impact-grid { grid-template-columns: repeat(2, 1fr) !important; }
        }
        @media (max-width: 480px) {
          .ax-impact-grid { grid-template-columns: 1fr !important; }
        }
        @media (max-width: 768px) {
          .ax-impact-card { min-height: 0 !important; }
        }
      `}</style>
      </SectionFrame>
    </div>
  );
}

window.SectionImpact = SectionImpact;
