// Main app — composes the page.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accent": "#e21d24"
}/*EDITMODE-END*/;

// Big blurred brand-colored blob that hugs the side margin of the viewport and
// alternates sides each section. When switching sides it ARCS OVER THE TOP of
// the viewport (going up and off-screen) instead of crossing through the page
// — the body text never gets overlapped or tinted.
function ScrollBlob() {
  const [scrollY, setScrollY] = useState(0);
  const [vh, setVh] = useState(typeof window !== "undefined" ? window.innerHeight : 800);

  useEffect(() => {
    let raf = 0;
    let target = window.scrollY;
    const tick = () => {
      setScrollY((prev) => prev + (target - prev) * 0.1);
      raf = requestAnimationFrame(tick);
    };
    const onScroll = () => { target = window.scrollY; };
    const onResize = () => setVh(window.innerHeight);
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onResize);
    raf = requestAnimationFrame(tick);
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onResize);
      cancelAnimationFrame(raf);
    };
  }, []);

  // Slow, organic, irregular drift. Two sine waves with incommensurate periods
  // — the trajectory never exactly repeats and the blob spends different
  // amounts of time on each side. Phase offsets are tuned so it starts visible
  // in the right margin on first paint. It crosses sides every ~3500 px of
  // scroll on average (very slow), and always arcs out of the viewport via
  // the top edge while crossing.
  const phaseA = scrollY * 0.00028 + 1.2;
  const phaseB = scrollY * 0.00018 + 1.5;
  const sideRaw = Math.sin(phaseA) * 0.72 + Math.sin(phaseB) * 0.28;     // -1..+1
  // Bias toward the side extremes so it spends most time at the margin
  const sideSigned = Math.sign(sideRaw) * Math.pow(Math.abs(sideRaw), 0.5);
  const sideMix = (sideSigned + 1) / 2;                                   // 0..1
  const xVw = (sideMix - 0.5) * 118;                                      // -59..+59 vw

  // Centeredness peaks (= 1) when the blob is between sides — arc it up so it
  // exits over the top rather than crossing through the page.
  const centeredness = Math.max(0, 1 - 2 * Math.abs(sideMix - 0.5));      // 1 at center, 0 at sides
  const arcVh = -Math.pow(centeredness, 0.6) * 100;

  // Slow irregular vertical bob (two harmonics) while parked at a margin
  const yBob = Math.sin(scrollY * 0.00046 + 0.4) * 8 +
               Math.cos(scrollY * 0.00027 + 2.1) * 5;
  const yVh = arcVh + yBob * (1 - centeredness);

  return (
    <div style={{
      position: "fixed",
      top: `calc(50% + ${yVh}vh)`,
      left: "50%",
      width: "55vmin",
      height: "55vmin",
      pointerEvents: "none",
      zIndex: 0,
      transform: `translate(calc(-50% + ${xVw}vw), -50%)`,
      willChange: "transform, top"
    }}>
      <div className="cc-scroll-blob" style={{
        width: "100%",
        height: "100%",
        background:
          "radial-gradient(circle at 50% 50%, rgba(226,29,36,0.55) 0%, rgba(226,29,36,0.2) 32%, rgba(226,29,36,0.05) 58%, transparent 72%)",
        filter: "blur(60px)",
        borderRadius: "50%"
      }} />
      <style>{`
        .cc-scroll-blob { animation: cc-blob-breathe 9s ease-in-out infinite; }
        @keyframes cc-blob-breathe {
          0%, 100% { transform: scale(0.9); opacity: 0.9; }
          50%      { transform: scale(1.2); opacity: 1; }
        }
        @media (prefers-reduced-motion: reduce) {
          .cc-scroll-blob { animation: none; }
        }
      `}</style>
    </div>
  );
}

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  // Apply accent
  useEffect(() => {
    const pair = {
      "#e21d24": "#b8171c",
      "#b8171c": "#8e1115",
      "#dc2626": "#b91c1c",
      "#0d0f14": "#27272a",
    }[t.accent] || "#b8171c";
    document.documentElement.style.setProperty("--brand", t.accent);
    document.documentElement.style.setProperty("--brand-2", pair);
  }, [t.accent]);

  return (
    <LangProvider>
      <ScrollBlob />
      <Nav />
      <HeroCinematic />
      <Reality />
      <Services />
      <Certifications />
      <Fleet />
      <ColdChain />
      <Tracking />
      <Calculator />
      <Sustainability />
      <MetricsStrip />
      <About />
      <Contact />
      <Footer />

      <TweaksPanel title="Tweaks · CoolCargo">
        <TweakSection label="Marca" />
        <TweakColor
          label="Acento"
          value={t.accent}
          options={["#e21d24", "#b8171c", "#dc2626", "#0d0f14"]}
          onChange={v => setTweak("accent", v)}
        />
      </TweaksPanel>
    </LangProvider>
  );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
