// Three hero variations:
// A — Centered, type-driven, big restrained statement
// B — Asymmetric editorial, name on left, statement on right, headshot column
// C — Mark-as-art: animated geometric remix of the RLC monogram filling the right half

const { useEffect: heUE, useState: heUS, useRef: heUR } = React;

// BudgetHero — feathered photo of a hand-annotated budget on a wooden desk.
// Subtle mouse-parallax tilt on rich/medium motion; static on subtle.
function BudgetHero({ motion = "medium" }) {
  const [mx, setMx] = heUS(0);
  const [my, setMy] = heUS(0);
  heUE(() => {
    if (motion === "subtle") return;
    const onMove = (e) => {
      const x = (e.clientX / window.innerWidth - 0.5) * 2;
      const y = (e.clientY / window.innerHeight - 0.5) * 2;
      setMx(x); setMy(y);
    };
    window.addEventListener("mousemove", onMove);
    return () => window.removeEventListener("mousemove", onMove);
  }, [motion]);
  const tiltX = my * (motion === "rich" ? 3 : 1.5);
  const tiltY = mx * (motion === "rich" ? -3 : -1.5);
  const dx = mx * (motion === "rich" ? 8 : 4);
  const dy = my * (motion === "rich" ? 8 : 4);
  return (
    <div style={{
      position: "relative",
      width: "100%",
      maxWidth: 760,
      aspectRatio: "1060 / 768",
      transform: `perspective(1400px) rotateX(${tiltX}deg) rotateY(${tiltY}deg) translate3d(${dx}px, ${dy}px, 0)`,
      transition: "transform 600ms cubic-bezier(.2,.7,.2,1)",
      transformStyle: "preserve-3d",
    }}>
      <img
        src="assets/budget-hero.png"
        alt="A hand-annotated nonprofit annual operating budget on a wooden desk, with notes and checkmarks"
        style={{
          width: "100%",
          height: "100%",
          display: "block",
          objectFit: "cover",
          // Image has baked-in alpha falloff (transparent corners + soft edge fade),
          // so it blends into the page background regardless of color.
          filter: "drop-shadow(0 30px 50px rgba(0,0,0,0.55))",
        }}
      />
    </div>
  );
}

function NavBar({ variant, motion, transparent, scrolled }) {
  const links = [
    { label: "About", href: "#about" },
    { label: "Services", href: "#services" },
    { label: "Experience", href: "#experience" },
    { label: "Expertise", href: "#expertise" },
    { label: "Impact", href: "#impact" },
  ];
  const solid = !transparent || scrolled;
  const [menuOpen, setMenuOpen] = React.useState(false);
  React.useEffect(() => {
    if (menuOpen) {
      const orig = document.body.style.overflow;
      document.body.style.overflow = "hidden";
      return () => { document.body.style.overflow = orig; };
    }
  }, [menuOpen]);
  return (
    <nav className="rlc-nav" style={{
      position: "absolute",
      top: 0, left: 0, right: 0,
      zIndex: 10,
      padding: "28px 48px",
      display: "flex",
      alignItems: "center",
      justifyContent: "space-between",
      background: solid ? "rgba(12,22,36,0.85)" : "transparent",
      backdropFilter: solid ? "blur(18px) saturate(160%)" : "none",
      WebkitBackdropFilter: solid ? "blur(18px) saturate(160%)" : "none",
      borderBottom: solid ? "1px solid #1a3050" : "1px solid transparent",
      transition: "all 280ms ease",
    }}>
      <a href="#" style={{ display: "inline-flex", alignItems: "center", gap: 14, textDecoration: "none" }}>
        <RLCMark size={44} variant="dark" />
        <div style={{ lineHeight: 1, display: "flex", flexDirection: "column", gap: 5 }}>
          <span style={{ fontFamily: "Outfit, sans-serif", fontWeight: 700, fontSize: 18, letterSpacing: "0.32em", color: "#0099ff" }}>RLC</span>
          <span style={{ fontFamily: "Outfit, sans-serif", fontWeight: 400, fontSize: 10, letterSpacing: "0.22em", color: "#7a9bb5", textTransform: "uppercase" }}>Development Solutions</span>
        </div>
      </a>
      <div className="rlc-nav-desktop" style={{ display: "flex", alignItems: "center", gap: 36 }}>
        {links.map((l) => (
          <a key={l.href} href={l.href} style={{
            fontFamily: "Outfit, sans-serif",
            fontSize: 12,
            fontWeight: 600,
            letterSpacing: "0.18em",
            textTransform: "uppercase",
            color: "#ffffff",
            textDecoration: "none",
            opacity: 0.8,
            transition: "opacity 200ms ease",
          }} onMouseEnter={(e) => e.currentTarget.style.opacity = "1"} onMouseLeave={(e) => e.currentTarget.style.opacity = "0.8"}>{l.label}</a>
        ))}
        <a href="#contact" style={{
          fontFamily: "Outfit, sans-serif",
          fontSize: 12,
          fontWeight: 600,
          letterSpacing: "0.18em",
          textTransform: "uppercase",
          color: "#ffffff",
          background: "#0F8C6E",
          padding: "12px 22px",
          textDecoration: "none",
          whiteSpace: "nowrap",
        }}>Let's Talk</a>
      </div>
      {/* Hamburger button — visible on mobile only via CSS */}
      <button
        className="rlc-nav-burger"
        aria-label={menuOpen ? "Close menu" : "Open menu"}
        onClick={() => setMenuOpen(!menuOpen)}
        style={{
          display: "none",
          background: "transparent",
          border: 0,
          cursor: "pointer",
          padding: 8,
          width: 44,
          height: 44,
          flexDirection: "column",
          justifyContent: "center",
          alignItems: "center",
          gap: 5,
        }}
      >
        <span style={{ display: "block", width: 24, height: 2, background: "#fff", transition: "transform 220ms ease, opacity 220ms ease", transform: menuOpen ? "translateY(7px) rotate(45deg)" : "none" }} />
        <span style={{ display: "block", width: 24, height: 2, background: "#fff", transition: "opacity 180ms ease", opacity: menuOpen ? 0 : 1 }} />
        <span style={{ display: "block", width: 24, height: 2, background: "#fff", transition: "transform 220ms ease", transform: menuOpen ? "translateY(-7px) rotate(-45deg)" : "none" }} />
      </button>
      {/* Mobile overlay */}
      {menuOpen && (
        <div
          onClick={() => setMenuOpen(false)}
          style={{
            position: "fixed",
            inset: 0,
            background: "rgba(12,22,36,0.97)",
            backdropFilter: "blur(20px)",
            zIndex: 9,
            display: "flex",
            flexDirection: "column",
            justifyContent: "center",
            alignItems: "center",
            gap: 28,
            padding: 32,
            animation: "rlcFadeIn 220ms ease",
          }}
        >
          {links.map((l) => (
            <a key={l.href} href={l.href} onClick={() => setMenuOpen(false)} style={{
              fontFamily: "Outfit, sans-serif",
              fontSize: 24,
              fontWeight: 600,
              letterSpacing: "0.04em",
              color: "#ffffff",
              textDecoration: "none",
            }}>{l.label}</a>
          ))}
          <a href="#contact" onClick={() => setMenuOpen(false)} style={{
            marginTop: 16,
            fontFamily: "Outfit, sans-serif",
            fontSize: 14,
            fontWeight: 600,
            letterSpacing: "0.18em",
            textTransform: "uppercase",
            color: "#ffffff",
            background: "#0F8C6E",
            padding: "16px 40px",
            textDecoration: "none",
          }}>Let's Talk</a>
        </div>
      )}
      <style>{`
        @keyframes rlcFadeIn { from { opacity: 0 } to { opacity: 1 } }
        @media (max-width: 760px) {
          .rlc-nav { padding: 16px 20px !important; }
          .rlc-nav-desktop { display: none !important; }
          .rlc-nav-burger { display: flex !important; }
        }
      `}</style>
    </nav>
  );
}

// ───────────────────────────────────────────── Hero A — Centered
// budgetTreatment: "card" (default — image card on the right)
//                  "rightBleed" (image fills right half, navy wash overlay)
//                  "fullBleed" (image covers entire hero as a watermark)
function HeroCentered({ motion, transparent, scrolled, budgetTreatment = "card" }) {
  const isRightBleed = budgetTreatment === "rightBleed";
  const isFullBleed = budgetTreatment === "fullBleed";
  const isCard = budgetTreatment === "card";

  return (
    <section id="hero" style={{
      background: "#0c1624",
      minHeight: "100vh",
      position: "relative",
      overflow: "hidden",
      display: "flex",
      flexDirection: "column",
      justifyContent: "center",
      paddingTop: 100,
    }}>
      <NavBar motion={motion} transparent={transparent} scrolled={scrolled} />

      {/* Watermark image background — full-bleed or right-half */}
      {(isRightBleed || isFullBleed) && (
        <div style={{
          position: "absolute",
          top: 0, bottom: 0,
          left: isFullBleed ? 0 : "42%",
          right: 0,
          zIndex: 1,
          overflow: "hidden",
        }}>
          {/* Watermark image background — full-bleed or right-half.
              objectPosition shifts slightly RIGHT so the budget paper stays centered
              when the image is constrained to the right ~58% of the hero, while still
              showing a sliver of the "Strategic Planning" notepad on the left. */}
          <img
            src="assets/budget-hero.png"
            alt=""
            aria-hidden="true"
            style={{
              position: "absolute",
              inset: 0,
              width: "100%",
              height: "100%",
              objectFit: "cover",
              objectPosition: isRightBleed ? "20% center" : "center",
              filter: "saturate(0.6) brightness(0.95)",
            }}
          />
          {/* Navy color wash — multiplies over the image so it becomes a tonal watermark */}
          <div style={{
            position: "absolute",
            inset: 0,
            background: isFullBleed
              ? "linear-gradient(90deg, rgba(12,22,36,0.96) 0%, rgba(12,22,36,0.86) 35%, rgba(12,22,36,0.72) 100%)"
              : "linear-gradient(90deg, rgba(12,22,36,1) 0%, rgba(12,22,36,0.85) 22%, rgba(12,22,36,0.55) 100%)",
          }} />
          {/* Subtle blue tint blend */}
          <div style={{
            position: "absolute",
            inset: 0,
            background: "linear-gradient(120deg, rgba(0,153,255,0.10) 0%, rgba(15,140,110,0.06) 100%)",
            mixBlendMode: "overlay",
          }} />
        </div>
      )}

      {/* Animated grid backdrop — only visible when not under heavy image wash */}
      {isCard && <BackgroundGrid motion={motion} />}
      {isFullBleed && <BackgroundGrid motion={motion} subtle />}

      <div style={{
        maxWidth: 1280,
        margin: "0 auto",
        padding: "0 48px",
        display: "grid",
        gridTemplateColumns: isCard ? "1.15fr 1fr" : "1fr",
        gap: 64,
        alignItems: "center",
        position: "relative",
        zIndex: 2,
        width: "100%",
      }}>
        <div style={{ textAlign: "left", maxWidth: isCard ? "none" : 720 }}>
        <Reveal motion={motion}>
          <Eyebrow color="#0F8C6E"><span className="rlc-eyebrow-stack"><span>Nonprofit fundraising</span><span className="rlc-eyebrow-sep">&nbsp;·&nbsp;</span><span>strategy&nbsp;·&nbsp;capacity</span></span></Eyebrow>
        </Reveal>
        <Reveal motion={motion} delay={140}>
          <h1 style={{
            fontFamily: "Outfit, sans-serif",
            fontWeight: 700,
            fontSize: "clamp(40px, 4.8vw, 72px)",
            lineHeight: 1.1,
            letterSpacing: "-1.5px",
            color: "#ffffff",
            margin: "0 0 32px",
            textWrap: "balance",
          }}>
            <span style={{ display: "block" }}>Grow the budget.</span>
            <span style={{ display: "block", color: "#0099ff" }}>Advance the mission.</span>
          </h1>
        </Reveal>
        <Reveal motion={motion} delay={280}>
          <p style={{
            fontFamily: "Outfit, sans-serif",
            fontWeight: 300,
            fontSize: 20,
            lineHeight: 1.55,
            color: "#a8c2d8",
            margin: "0 0 40px",
            maxWidth: 540,
            textWrap: "pretty",
          }}>
            25+ years building nonprofits, reversing deficits, and growing revenue. RLC Development Solutions partners with civic, charitable, and advocacy organizations across the United States.
          </p>
        </Reveal>
        <Reveal motion={motion} delay={420}>
          <div style={{ display: "flex", gap: 16, justifyContent: "flex-start", flexWrap: "wrap" }}>
            <CTAButton href="#contact" variant="primary">Start a conversation</CTAButton>
            <CTAButton href="#services" variant="ghost">See the work →</CTAButton>
          </div>
        </Reveal>
        </div>
        {isCard && (
          <Reveal motion={motion} delay={300}>
            <div style={{ display: "grid", placeItems: "center" }}>
              <BudgetHero motion={motion} />
            </div>
          </Reveal>
        )}
      </div>
      <ScrollHint motion={motion} />
    </section>
  );
}

// ───────────────────────────────────────────── Hero B — Asymmetric editorial
function HeroEditorial({ motion, transparent, scrolled }) {
  return (
    <section id="hero" style={{
      background: "#0c1624",
      minHeight: "100vh",
      position: "relative",
      overflow: "hidden",
      display: "flex",
      flexDirection: "column",
      justifyContent: "flex-end",
      paddingTop: 120,
      paddingBottom: 80,
    }}>
      <NavBar motion={motion} transparent={transparent} scrolled={scrolled} />
      <BackgroundGrid motion={motion} subtle />
      <div style={{ maxWidth: 1180, margin: "0 auto 0", padding: "0 48px", width: "100%", position: "relative", zIndex: 2 }}>
        <div style={{ display: "grid", gridTemplateColumns: "1.2fr 1fr", gap: 80, alignItems: "end" }}>
          <div>
            <Reveal motion={motion}>
              <Eyebrow color="#0F8C6E">RLC Development Solutions · Est. 2020</Eyebrow>
            </Reveal>
            <Reveal motion={motion} delay={120}>
              <h1 style={{
                fontFamily: "Outfit, sans-serif",
                fontWeight: 700,
                fontSize: "clamp(40px, 6vw, 92px)",
                lineHeight: 1.04,
                letterSpacing: "-3px",
                color: "#ffffff",
                margin: "0 0 0",
                textWrap: "balance",
              }}>
                <span style={{ display: "block" }}>We build it</span>
                <span style={{ display: "block" }}>with you —</span>
                <span style={{ display: "block", color: "#0099ff" }}>and stay until</span>
                <span style={{ display: "block", color: "#0099ff" }}>it works.</span>
              </h1>
            </Reveal>
          </div>
          <div style={{ paddingBottom: 12 }}>
            <Reveal motion={motion} delay={260}>
              <div style={{ borderLeft: "2px solid #0F8C6E", paddingLeft: 28, marginBottom: 32 }}>
                <p style={{
                  fontFamily: "Outfit, sans-serif",
                  fontWeight: 300,
                  fontSize: 19,
                  lineHeight: 1.6,
                  color: "#c9d6e3",
                  margin: 0,
                  textWrap: "pretty",
                }}>
                  Nonprofit fundraising and strategy support from <strong style={{ color: "#ffffff", fontWeight: 500 }}>Rey López-Calderón, J.D.</strong> — 25+ years specializing in nonpartisan civic and democracy organizations.
                </p>
              </div>
            </Reveal>
            <Reveal motion={motion} delay={380}>
              <div style={{ display: "flex", gap: 16, flexWrap: "wrap" }}>
                <CTAButton href="#contact" variant="primary">Start a conversation</CTAButton>
                <CTAButton href="#experience" variant="ghost">See the work →</CTAButton>
              </div>
            </Reveal>
          </div>
        </div>
      </div>
    </section>
  );
}

// ───────────────────────────────────────────── Hero C — Mark as art
function HeroMarkArt({ motion, transparent, scrolled }) {
  const [t, setT] = heUS(0);
  heUE(() => {
    if (motion === "subtle") return;
    let raf;
    const start = performance.now();
    const loop = (now) => {
      setT((now - start) / 1000);
      raf = requestAnimationFrame(loop);
    };
    raf = requestAnimationFrame(loop);
    return () => cancelAnimationFrame(raf);
  }, [motion]);

  // Mouse parallax
  const [mx, setMx] = heUS(0);
  const [my, setMy] = heUS(0);
  heUE(() => {
    if (motion !== "rich") return;
    const onMove = (e) => {
      const x = (e.clientX / window.innerWidth - 0.5) * 2;
      const y = (e.clientY / window.innerHeight - 0.5) * 2;
      setMx(x); setMy(y);
    };
    window.addEventListener("mousemove", onMove);
    return () => window.removeEventListener("mousemove", onMove);
  }, [motion]);

  return (
    <section id="hero" style={{
      background: "#0c1624",
      minHeight: "100vh",
      position: "relative",
      overflow: "hidden",
      display: "flex",
      alignItems: "center",
      paddingTop: 100,
    }}>
      <NavBar motion={motion} transparent={transparent} scrolled={scrolled} />
      <div style={{ maxWidth: 1180, margin: "0 auto", padding: "0 48px", display: "grid", gridTemplateColumns: "1fr 1fr", gap: 80, alignItems: "center", width: "100%", position: "relative", zIndex: 2 }}>
        <div>
          <Reveal motion={motion}>
            <Eyebrow color="#0F8C6E">Fundraising · Strategy · Capacity</Eyebrow>
          </Reveal>
          <Reveal motion={motion} delay={120}>
            <h1 style={{
              fontFamily: "Outfit, sans-serif",
              fontWeight: 700,
              fontSize: "clamp(48px, 6.5vw, 104px)",
              lineHeight: 1.02,
              letterSpacing: "-2.5px",
              color: "#ffffff",
              margin: "0 0 32px",
              textWrap: "balance",
            }}>
              <span style={{ display: "block" }}>25 years</span>
              <span style={{ display: "block", color: "#0099ff" }}>in the room.</span>
            </h1>
          </Reveal>
          <Reveal motion={motion} delay={260}>
            <p style={{
              fontFamily: "Outfit, sans-serif",
              fontWeight: 300,
              fontSize: 21,
              lineHeight: 1.6,
              color: "#7a9bb5",
              margin: "0 0 40px",
              maxWidth: 480,
              textWrap: "pretty",
            }}>
              RLC Development Solutions is the consultancy of Rey López-Calderón, J.D. — a senior fundraising advisor for nonprofits that change the world.
            </p>
          </Reveal>
          <Reveal motion={motion} delay={400}>
            <div style={{ display: "flex", gap: 16, flexWrap: "wrap" }}>
              <CTAButton href="#contact" variant="primary">Let's talk</CTAButton>
              <CTAButton href="#experience" variant="ghost">Selected work →</CTAButton>
            </div>
          </Reveal>
        </div>
        <div style={{ position: "relative", height: 560, transform: `translate(${mx * 12}px, ${my * 12}px)`, transition: "transform 400ms ease-out" }}>
          <AnimatedMark motion={motion} t={t} />
        </div>
      </div>
    </section>
  );
}

// Animated remix of the RLC monogram — three deconstructed "marks" floating
function AnimatedMark({ motion, t }) {
  const drift = motion === "subtle" ? 0 : Math.sin(t * 0.6) * 8;
  const drift2 = motion === "subtle" ? 0 : Math.cos(t * 0.5) * 6;
  return (
    <div style={{ position: "absolute", inset: 0 }}>
      {/* Big primary mark */}
      <div style={{ position: "absolute", inset: 0, display: "grid", placeItems: "center", transform: `translateY(${drift}px)`, transition: "transform 200ms linear" }}>
        <RLCMark size={420} variant="dark" />
      </div>
      {/* Echo 1 — large mark, low opacity */}
      <div style={{ position: "absolute", top: -40, right: -80, opacity: 0.18, transform: `translateY(${-drift2}px)` }}>
        <RLCMark size={300} variant="white" />
      </div>
      {/* Echo 2 */}
      <div style={{ position: "absolute", bottom: -60, left: -80, opacity: 0.1, transform: `translateY(${drift2}px)` }}>
        <RLCMark size={240} variant="white" />
      </div>
      {/* Glow accent */}
      <div style={{
        position: "absolute",
        inset: "20% 20% 20% 20%",
        background: "radial-gradient(circle, rgba(0,153,255,0.25), transparent 70%)",
        pointerEvents: "none",
        filter: "blur(40px)",
      }} />
    </div>
  );
}

// ───────────────────────────────────────────── Shared subcomponents
function CTAButton({ href, children, variant = "primary" }) {
  const isPrimary = variant === "primary";
  return (
    <a href={href} style={{
      fontFamily: "Outfit, sans-serif",
      fontSize: 14,
      fontWeight: 600,
      letterSpacing: "0.15em",
      textTransform: "uppercase",
      color: "#ffffff",
      background: isPrimary ? "#0099ff" : "transparent",
      padding: "20px 32px",
      textDecoration: "none",
      border: isPrimary ? "1px solid #0099ff" : "1px solid #1e3a5a",
      transition: "all 220ms ease",
      display: "inline-block",
    }} onMouseEnter={(e) => {
      if (isPrimary) { e.currentTarget.style.background = "#33adff"; e.currentTarget.style.borderColor = "#33adff"; }
      else { e.currentTarget.style.borderColor = "#0F8C6E"; e.currentTarget.style.color = "#0F8C6E"; }
      e.currentTarget.style.transform = "translateY(-2px)";
    }} onMouseLeave={(e) => {
      if (isPrimary) { e.currentTarget.style.background = "#0099ff"; e.currentTarget.style.borderColor = "#0099ff"; }
      else { e.currentTarget.style.borderColor = "#1e3a5a"; e.currentTarget.style.color = "#ffffff"; }
      e.currentTarget.style.transform = "translateY(0)";
    }}>
      {children}
    </a>
  );
}

function BackgroundGrid({ motion, subtle }) {
  if (motion === "subtle") return null;
  return (
    <div style={{
      position: "absolute",
      inset: 0,
      backgroundImage: "linear-gradient(rgba(122,155,181,0.06) 1px, transparent 1px), linear-gradient(90deg, rgba(122,155,181,0.06) 1px, transparent 1px)",
      backgroundSize: "80px 80px",
      maskImage: "radial-gradient(ellipse at center, black 30%, transparent 70%)",
      WebkitMaskImage: "radial-gradient(ellipse at center, black 30%, transparent 70%)",
      pointerEvents: "none",
      opacity: subtle ? 0.6 : 1,
    }} />
  );
}

function ScrollHint({ motion }) {
  if (motion === "subtle") return null;
  return (
    <div style={{
      position: "absolute",
      bottom: 32,
      left: "50%",
      transform: "translateX(-50%)",
      fontFamily: "Outfit, sans-serif",
      fontSize: 11,
      fontWeight: 600,
      letterSpacing: "0.25em",
      textTransform: "uppercase",
      color: "#5A6E82",
      display: "flex",
      flexDirection: "column",
      alignItems: "center",
      gap: 12,
      animation: "rlcBob 2.4s ease-in-out infinite",
    }}>
      Scroll
      <span style={{ width: 1, height: 36, background: "linear-gradient(to bottom, #0099ff, transparent)" }} />
      <style>{`@keyframes rlcBob{0%,100%{transform:translateX(-50%) translateY(0)}50%{transform:translateX(-50%) translateY(8px)}}`}</style>
    </div>
  );
}

Object.assign(window, { HeroCentered, HeroEditorial, HeroMarkArt, NavBar });
