// Hero — two variations toggled from Tweaks

// --- Shared device mockup rendered with SVG + HTML ---
function DeviceMockup({ rotate = 0, parallax = { x: 0, y: 0 } }) {
  return (
    <div
      style={{
        position: "relative",
        width: "100%",
        aspectRatio: "5 / 4",
        transform: `rotate(${rotate}deg)`,
      }}
    >
      {/* Cyan ambient glow */}
      <div
        style={{
          position: "absolute",
          inset: "-10%",
          background:
            "radial-gradient(circle at 60% 55%, oklch(0.82 0.14 215 / 0.45), transparent 55%)",
          filter: "blur(30px)",
          zIndex: 0,
        }}
      />

      {/* Laptop */}
      <div
        style={{
          position: "absolute",
          left: "4%",
          top: "14%",
          width: "80%",
          zIndex: 2,
          transform: `translate(${parallax.x * 0.4}px, ${parallax.y * 0.4}px)`,
          transition: "transform .15s ease-out",
        }}
      >
        <LaptopFrame />
      </div>

      {/* Phone */}
      <div
        style={{
          position: "absolute",
          left: "2%",
          bottom: "2%",
          width: "17%",
          zIndex: 3,
          transform: `translate(${parallax.x * -0.8}px, ${parallax.y * -0.8}px)`,
          transition: "transform .15s ease-out",
        }}
      >
        <PhoneFrame />
      </div>

      {/* Tablet */}
      <div
        style={{
          position: "absolute",
          right: "0%",
          top: "28%",
          width: "26%",
          zIndex: 3,
          transform: `translate(${parallax.x * 0.9}px, ${parallax.y * 0.9}px) rotate(4deg)`,
          transition: "transform .15s ease-out",
        }}
      >
        <TabletFrame />
      </div>
    </div>
  );
}

function LaptopFrame() {
  return (
    <div>
      {/* screen */}
      <div
        style={{
          position: "relative",
          background: "oklch(0.18 0.03 240)",
          borderRadius: "10px 10px 4px 4px",
          padding: "10px 10px 12px",
          boxShadow:
            "0 30px 60px -20px oklch(0.3 0.1 240 / 0.35), inset 0 0 0 1.5px oklch(0.35 0.04 240)",
        }}
      >
        <div
          style={{
            background:
              "radial-gradient(circle at 50% 50%, oklch(0.28 0.06 230) 0%, oklch(0.14 0.04 240) 65%)",
            borderRadius: 5,
            aspectRatio: "16 / 10",
            padding: "24px",
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            justifyContent: "center",
            gap: 14,
            position: "relative",
            overflow: "hidden",
          }}
        >
          {/* faint grid */}
          <div
            style={{
              position: "absolute",
              inset: 0,
              backgroundImage:
                "linear-gradient(oklch(0.5 0.1 220 / 0.08) 1px, transparent 1px), linear-gradient(90deg, oklch(0.5 0.1 220 / 0.08) 1px, transparent 1px)",
              backgroundSize: "24px 24px",
              maskImage: "radial-gradient(circle at center, black 30%, transparent 70%)",
            }}
          />
          <img src="assets/infinitdev-logo.png" alt="InfinitDev" style={{ height: 88, width: "auto", filter: "brightness(0) invert(1)" }} />
          <div
            style={{
              fontSize: 10,
              color: "oklch(0.78 0.04 220)",
              fontFamily: "var(--font-mono)",
              marginTop: 4,
            }}
          >
            Construisons ensemble l'avenir digital.
          </div>
          <div
            style={{
              width: 28,
              height: 2,
              background: "oklch(0.78 0.14 215)",
              marginTop: 6,
            }}
          />
        </div>
      </div>
      {/* base */}
      <div
        style={{
          margin: "0 -6%",
          height: 10,
          background:
            "linear-gradient(180deg, oklch(0.7 0.02 240) 0%, oklch(0.55 0.03 240) 50%, oklch(0.38 0.03 240) 100%)",
          borderRadius: "0 0 12px 12px",
          boxShadow: "0 6px 14px -4px oklch(0.3 0.05 240 / 0.25)",
        }}
      />
    </div>
  );
}

function PhoneFrame() {
  return (
    <div
      style={{
        background: "oklch(0.18 0.03 240)",
        borderRadius: 18,
        padding: 4,
        boxShadow: "0 20px 40px -14px oklch(0.3 0.1 240 / 0.35), inset 0 0 0 1.5px oklch(0.4 0.04 240)",
        aspectRatio: "9 / 19",
      }}
    >
      <div
        style={{
          background: "radial-gradient(circle at 50% 30%, oklch(0.3 0.07 225), oklch(0.14 0.04 240) 70%)",
          borderRadius: 14,
          height: "100%",
          padding: 10,
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          justifyContent: "center",
          gap: 8,
        }}
      >
        <img src="assets/infinitdev-logo.png" alt="InfinitDev" style={{ height: 34, width: "auto", filter: "brightness(0) invert(1)" }} />
        <div
          style={{
            fontSize: 5,
            color: "oklch(0.78 0.04 220)",
            textAlign: "center",
            lineHeight: 1.4,
            padding: "0 4px",
            marginTop: 4,
          }}
        >
          Solutions innovantes pour un monde digital.
        </div>
      </div>
    </div>
  );
}

function TabletFrame() {
  return (
    <div
      style={{
        background: "oklch(0.18 0.03 240)",
        borderRadius: 14,
        padding: 6,
        boxShadow: "0 20px 40px -14px oklch(0.3 0.1 240 / 0.3), inset 0 0 0 1.5px oklch(0.4 0.04 240)",
        aspectRatio: "3 / 4",
      }}
    >
      <div
        style={{
          background: "radial-gradient(circle at 50% 50%, oklch(0.28 0.06 225), oklch(0.14 0.04 240) 70%)",
          borderRadius: 10,
          height: "100%",
          padding: 14,
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          justifyContent: "center",
          gap: 10,
        }}
      >
        <img src="assets/infinitdev-logo.png" alt="InfinitDev" style={{ height: 48, width: "auto", filter: "brightness(0) invert(1)" }} />
        <div style={{ width: 18, height: 1.5, background: "oklch(0.78 0.14 215)" }} />
        <div
          style={{
            fontSize: 7,
            color: "oklch(0.78 0.04 220)",
            textAlign: "center",
            lineHeight: 1.6,
          }}
        >
          Créatif.<br />Moderne.<br />Performant.
        </div>
      </div>
    </div>
  );
}

// --- Hero variations ---
function HeroSplit() {
  const [parallax, setParallax] = React.useState({ x: 0, y: 0 });
  const [scroll, setScroll] = React.useState(0);
  const ref = React.useRef(null);
  const { isMobile, isTablet } = useViewport();

  React.useEffect(() => {
    const onMove = (e) => {
      if (!ref.current) return;
      const r = ref.current.getBoundingClientRect();
      const cx = r.left + r.width / 2;
      const cy = r.top + r.height / 2;
      setParallax({ x: (e.clientX - cx) / 40, y: (e.clientY - cy) / 40 });
    };
    const onScroll = () => {
      if (!ref.current) return;
      const r = ref.current.getBoundingClientRect();
      setScroll(Math.max(0, Math.min(1, -r.top / r.height)));
    };
    window.addEventListener("mousemove", onMove);
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => {
      window.removeEventListener("mousemove", onMove);
      window.removeEventListener("scroll", onScroll);
    };
  }, []);

  return (
    <section
      id="accueil"
      ref={ref}
      style={{
        position: "relative",
        paddingTop: isMobile ? 108 : isTablet ? 124 : 140,
        paddingBottom: isMobile ? 56 : 80,
        overflow: "hidden",
      }}
    >
      {/* Ambient cyan arc behind */}
      <div
        aria-hidden
        style={{
          position: "absolute",
          top: "-10%",
          right: "-20%",
          width: "70%",
          aspectRatio: "1 / 1",
          borderRadius: "50%",
          background:
            "radial-gradient(circle at 50% 50%, oklch(0.9 0.08 215 / 0.55), oklch(0.98 0.02 220 / 0) 65%)",
          filter: "blur(20px)",
          transform: `translateY(${scroll * -60}px)`,
        }}
      />
      {/* Decorative backgrounds */}
      <div className="bg-grid" aria-hidden />
      <div className="bg-blob bg-blob-b float-slow" style={{ left: isMobile ? "-28%" : "-12%", top: "18%", width: isMobile ? 220 : 340, height: isMobile ? 220 : 340 }} aria-hidden />
      <div className="bg-blob bg-blob-c float-med" style={{ right: isMobile ? "-10%" : "10%", bottom: "-10%", width: isMobile ? 180 : 280, height: isMobile ? 180 : 280 }} aria-hidden />
      {/* Floating infinity SVG motifs */}
      <svg aria-hidden className="float-slow" style={{ position: "absolute", top: isMobile ? 96 : 120, left: isMobile ? 12 : 40, width: isMobile ? 60 : 90, opacity: 0.15, color: "var(--cyan-deep)" }} viewBox="0 0 100 55">
        <path className="infinity-flow-path" d="M20 27.5 C 20 12, 38 12, 48 27.5 C 58 43, 76 43, 80 27.5 C 84 12, 66 12, 56 27.5 C 46 43, 28 43, 24 27.5 Z" fill="none" stroke="currentColor" strokeWidth="3" />
      </svg>
      <svg aria-hidden className="float-fast" style={{ position: "absolute", bottom: isMobile ? 16 : 80, left: "45%", width: isMobile ? 24 : 36, opacity: 0.25, color: "var(--cyan)" }} viewBox="0 0 24 24">
        <path d="M12 2 l2 7 l7 2 l-7 2 l-2 7 l-2 -7 l-7 -2 l7 -2 z" fill="currentColor" />
      </svg>
      <svg aria-hidden className="float-med" style={{ position: "absolute", top: 200, right: isMobile ? 20 : 80, width: isMobile ? 20 : 28, opacity: 0.3, color: "var(--cyan-ink)" }} viewBox="0 0 24 24">
        <circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" strokeWidth="1.5" strokeDasharray="3 4" />
      </svg>
      {!isMobile && <div className="pulse-ring" style={{ width: 120, height: 120, left: "8%", bottom: "15%" }} aria-hidden />}
      <div className="container" style={{ position: "relative", zIndex: 1 }}>
        <div
          style={{
            display: "grid",
            gridTemplateColumns: isTablet ? "1fr" : "1.05fr 1fr",
            gap: isMobile ? 36 : isTablet ? 44 : 60,
            alignItems: "center",
          }}
        >
          <div>
            <div className="eyebrow reveal in" style={{ marginBottom: isMobile ? 18 : 24 }}>
              InfinitDev — Agence digitale
            </div>
            <h1 style={{ marginBottom: isMobile ? 20 : 28 }}>
              L'innovation<br />
              <span style={{ color: "var(--cyan-ink)" }}>au service</span><br />
              de votre réussite.
            </h1>
            <p style={{ fontSize: isMobile ? 16 : 18, maxWidth: 540, marginBottom: isMobile ? 28 : 36, color: "var(--ink-muted)" }}>
              Nous concevons et développons des solutions digitales sur mesure — web, mobile,
              interfaces et marketing — pour accompagner votre croissance et votre transformation.
            </p>
            <div style={{ display: "flex", flexDirection: isMobile ? "column" : "row", gap: 12, marginBottom: isMobile ? 36 : 48 }}>
              <a href="contact.html" className="btn btn-primary">
                Démarrer un projet <span className="arr">→</span>
              </a>
              <a href="services.html" className="btn btn-ghost">
                Découvrir nos services
              </a>
            </div>

            {/* Inline trust strip */}
            <div
              style={{
                display: "flex",
                flexDirection: isMobile ? "column" : "row",
                gap: isMobile ? 18 : 28,
                fontSize: 13,
                color: "var(--ink-muted)",
                borderTop: "1px solid var(--hairline)",
                paddingTop: isMobile ? 18 : 24,
              }}
            >
              <div>
                <div style={{ fontFamily: "var(--font-display)", fontSize: 28, color: "var(--ink)", letterSpacing: "-0.02em" }}>
                  60+
                </div>
                <div>Projets livrés</div>
              </div>
              <div>
                <div style={{ fontFamily: "var(--font-display)", fontSize: 28, color: "var(--ink)", letterSpacing: "-0.02em" }}>
                  6 ans
                </div>
                <div>D'expertise</div>
              </div>
              <div>
                <div style={{ fontFamily: "var(--font-display)", fontSize: 28, color: "var(--ink)", letterSpacing: "-0.02em" }}>
                  98<span style={{ color: "var(--cyan-ink)" }}>%</span>
                </div>
                <div>Clients satisfaits</div>
              </div>
            </div>
          </div>

          <div style={{ position: "relative", maxWidth: isTablet ? 680 : "none", width: "100%", margin: isTablet ? "0 auto" : "0" }}>
            <DeviceMockup rotate={0} parallax={parallax} />
          </div>
        </div>
      </div>
    </section>
  );
}

function HeroCentered() {
  const [parallax, setParallax] = React.useState({ x: 0, y: 0 });
  const ref = React.useRef(null);
  const { isMobile, isTablet } = useViewport();

  React.useEffect(() => {
    const onMove = (e) => {
      if (!ref.current) return;
      const r = ref.current.getBoundingClientRect();
      const cx = r.left + r.width / 2;
      const cy = r.top + r.height / 2;
      setParallax({ x: (e.clientX - cx) / 50, y: (e.clientY - cy) / 50 });
    };
    window.addEventListener("mousemove", onMove);
    return () => window.removeEventListener("mousemove", onMove);
  }, []);

  return (
    <section
      id="accueil"
      ref={ref}
      style={{
        position: "relative",
        paddingTop: isMobile ? 112 : isTablet ? 132 : 160,
        paddingBottom: isMobile ? 48 : 60,
        overflow: "hidden",
        textAlign: "center",
      }}
    >
      {/* Giant centered cyan wash */}
      <div
        aria-hidden
        style={{
          position: "absolute",
          left: "50%",
          top: isMobile ? 220 : 260,
          transform: "translateX(-50%)",
          width: "90%",
          maxWidth: 1200,
          aspectRatio: "2 / 1",
          background:
            "radial-gradient(ellipse at 50% 50%, oklch(0.88 0.11 215 / 0.5), oklch(0.98 0.02 220 / 0) 60%)",
          filter: "blur(18px)",
        }}
      />
      <div className="container" style={{ position: "relative", zIndex: 1 }}>
        <div className="eyebrow" style={{ marginBottom: isMobile ? 20 : 28 }}>Agence digitale · Fondée 2020</div>
        <h1 style={{ marginBottom: 24, maxWidth: 1040, margin: "0 auto 24px" }}>
          L'innovation <span style={{ color: "var(--cyan-ink)" }}>au service</span><br />
          de votre réussite digitale.
        </h1>
        <p
          style={{
            fontSize: isMobile ? 16 : 19,
            maxWidth: 640,
            margin: `0 auto ${isMobile ? 28 : 40}px`,
            color: "var(--ink-muted)",
          }}
        >
          Spécialistes du développement sur mesure — nous transformons vos idées en produits
          digitaux performants, pensés pour durer.
        </p>
        <div style={{ display: "flex", flexDirection: isMobile ? "column" : "row", gap: 12, justifyContent: "center", marginBottom: isMobile ? 44 : 72 }}>
          <a href="contact.html" className="btn btn-primary">
            Parlons de votre projet <span className="arr">→</span>
          </a>
          <a href="portfolio.html" className="btn btn-ghost">Voir nos réalisations</a>
        </div>

        {/* Centered device stack */}
        <div
          style={{
            maxWidth: isTablet ? 680 : 900,
            margin: "0 auto",
            position: "relative",
          }}
        >
          <DeviceMockup rotate={0} parallax={parallax} />
        </div>
      </div>
    </section>
  );
}

function Hero({ variant = "split" }) {
  return variant === "centered" ? <HeroCentered /> : <HeroSplit />;
}

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