// Top navigation + Hero (with hero variants for tweaks)

function Nav({ onScroll }) {
  const [scrolled, setScrolled] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);

  useEffect(() => {
    const onS = () => setScrolled(window.scrollY > 24);
    onS();
    window.addEventListener("scroll", onS, { passive: true });
    return () => window.removeEventListener("scroll", onS);
  }, []);

  // Cerrar menú al hacer click en un link
  const closeMenu = () => setMenuOpen(false);

  return (
    <nav
      style={{
        position: "fixed",
        top: 0,
        left: 0,
        right: 0,
        zIndex: 100,
        backdropFilter: scrolled || menuOpen ? "saturate(140%) blur(14px)" : "none",
        WebkitBackdropFilter: scrolled || menuOpen ? "saturate(140%) blur(14px)" : "none",
        background: scrolled || menuOpen ? "rgba(239,232,218,0.95)" : "transparent",
        borderBottom: scrolled || menuOpen ? "1px solid var(--line)" : "1px solid transparent",
        transition: "all 0.3s ease",
      }}
    >
      <div
        className="container"
        style={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          padding: "16px var(--gutter)",
        }}
      >
        {/* Logo */}
        <a href="#top" style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <Logo />
          <span style={{ display: "flex", flexDirection: "column", lineHeight: 1.1 }}>
            <span style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 15, letterSpacing: "-0.01em" }}>
              Media Planning <span style={{ fontWeight: 400 }}>Canvas</span>
            </span>
            <span style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.14em", color: "var(--ink-muted)" }}>
              THE MARKETING PLAN GENERATOR
            </span>
          </span>
        </a>

        {/* Desktop nav links */}
        <div className="nav-links" style={{ display: "flex", gap: 28, alignItems: "center" }}>
          <a href="#metodo" style={navLink}>Método</a>
          <a href="#tour" style={navLink}>App</a>
          <a href="#jaime" style={navLink}>El Autor</a>
          <a href="#precios" style={navLink}>Precios</a>
          <a href="#descargar" style={navLink}>Descargar Canvas</a>
        </div>

        {/* Desktop CTAs */}
        <div className="nav-ctas" style={{ display: "flex", gap: 10 }}>
          <a href="https://app.mediaplanningcanvas.com/login" style={{ ...navLink, padding: "8px 0" }}>Entrar</a>
          <a href="#precios" className="btn btn-ink" style={{ padding: "10px 18px", fontSize: 14 }}>
            Empezar gratis
          </a>
        </div>

        {/* Hamburger — solo mobile */}
        <button
          className="hamburger"
          onClick={() => setMenuOpen(!menuOpen)}
          aria-label="Menú"
          style={{
            display: "none",
            background: "none",
            border: "none",
            padding: "8px",
            cursor: "pointer",
            flexDirection: "column",
            gap: 5,
          }}
        >
          <span style={{
            display: "block", width: 22, height: 2,
            background: "var(--ink)",
            borderRadius: 2,
            transition: "all 0.25s",
            transform: menuOpen ? "rotate(45deg) translate(5px, 5px)" : "none",
          }} />
          <span style={{
            display: "block", width: 22, height: 2,
            background: "var(--ink)",
            borderRadius: 2,
            transition: "all 0.25s",
            opacity: menuOpen ? 0 : 1,
          }} />
          <span style={{
            display: "block", width: 22, height: 2,
            background: "var(--ink)",
            borderRadius: 2,
            transition: "all 0.25s",
            transform: menuOpen ? "rotate(-45deg) translate(5px, -5px)" : "none",
          }} />
        </button>
      </div>

      {/* Mobile dropdown menu */}
      {menuOpen && (
        <div
          className="mobile-menu"
          style={{
            padding: "8px var(--gutter) 24px",
            display: "flex",
            flexDirection: "column",
            gap: 4,
            borderTop: "1px solid var(--line)",
          }}
        >
          {[
            ["Método", "#metodo"],
            ["App", "#tour"],
            ["Autor", "#jaime"],
            ["Precios", "#precios"],
            ["Descargar Canvas", "#descargar"],
          ].map(([label, href]) => (
            <a
              key={href}
              href={href}
              onClick={closeMenu}
              style={{
                fontSize: 18,
                fontWeight: 500,
                color: "var(--ink)",
                padding: "12px 0",
                borderBottom: "1px solid var(--line)",
                letterSpacing: "-0.01em",
              }}
            >
              {label}
            </a>
          ))}
          <div style={{ display: "flex", flexDirection: "column", gap: 10, marginTop: 16 }}>
            <a
              href="https://app.mediaplanningcanvas.com/login"
              onClick={closeMenu}
              className="btn btn-ghost"
              style={{ justifyContent: "center", fontSize: 15 }}
            >
              Entrar a la app
            </a>
            <a
              href="#precios"
              onClick={closeMenu}
              className="btn btn-ink"
              style={{ justifyContent: "center", fontSize: 15 }}
            >
              Empezar gratis →
            </a>
          </div>
        </div>
      )}

      <style>{`
        @media (max-width: 880px) {
          .nav-links { display: none !important; }
          .nav-ctas { display: none !important; }
          .hamburger { display: flex !important; }
        }
      `}</style>
    </nav>
  );
}

const navLink = {
  fontSize: 14,
  fontWeight: 500,
  color: "var(--ink-soft)",
  letterSpacing: "-0.005em",
};

function Logo({ size = 28, color = "var(--ink)" }) {
  // Matches the four-square mark from the app
  return (
    <svg width={size} height={size} viewBox="0 0 28 28" fill="none" aria-hidden="true">
      <rect x="0" y="0" width="12" height="12" fill={color} />
      <rect x="16" y="0" width="12" height="12" fill={color} opacity="0.35" />
      <rect x="0" y="16" width="12" height="12" fill={color} opacity="0.35" />
      <rect x="16" y="16" width="12" height="12" fill={color} />
    </svg>
  );
}

// ---------- HERO VARIANTS ----------

function Hero({ variant = "split", headline = "default", shotStyle = "browser" }) {
  const headlines = {
    default: (
      <>
        Crea tu plan de marketing<br />
        <em>profesional</em> con IA.
      </>
    ),
    method: (
      <>
        El método<br />
        Media Planning Canvas,<br />
        <em>ahora con IA.</em>
      </>
    ),
    learn: (
      <>
        Aprende a hacer planes<br />
        de marketing <em>mientras</em><br />
        creas el tuyo.
      </>
    ),
    methodology: (
      <>
        No es un chatbot.<br />
        Es <em>metodología.</em>
      </>
    ),
  };
  const headlineEl = headlines[headline] || headlines.default;

  if (variant === "centered") return <HeroCentered headline={headlineEl} shotStyle={shotStyle} />;
  if (variant === "editorial") return <HeroEditorial headline={headlineEl} shotStyle={shotStyle} />;
  return <HeroSplit headline={headlineEl} shotStyle={shotStyle} />;
}

function HeroEyebrow() {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 14, flexWrap: "wrap" }}>
      <Pill>Plan de marketing con IA</Pill>
      <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--ink-muted)", letterSpacing: "0.1em" }}>
        +10.000 PROFESIONALES YA USAN EL MEDIA PLANNING CANVAS
      </span>
    </div>
  );
}

function HeroCTAs() {
  return (
    <div style={{ display: "flex", gap: 12, flexWrap: "wrap", alignItems: "center" }}>
      <a href="#precios" className="btn btn-ink">
        Empezar gratis →
      </a>
      <a href="#metodo" className="btn btn-ghost">
        Ver los 5 pasos
      </a>
      <span style={{ fontSize: 13, color: "var(--ink-muted)", marginLeft: 4 }}>
        Sin tarjeta · Plan Free incluido
      </span>
    </div>
  );
}

function HeroSubline() {
  return (
    <p style={{ fontSize: 19, lineHeight: 1.5, color: "var(--ink-soft)", maxWidth: 520 }}>
      Cinco fases. Nuestra IA entrenada para acompañarte. Vídeo píldoras para formarte como experto en planes de marketing.
      Termina con un plan de medios real, mensualizado y exportable a PDF.
    </p>
  );
}

function HeroShot({ shotStyle }) {
  if (shotStyle === "floating") {
    return (
      <div
        style={{
          borderRadius: 16,
          overflow: "hidden",
          boxShadow: "0 24px 80px -20px rgba(14,34,54,0.28)",
          maxHeight: 480,
          border: "1px solid var(--line)",
        }}
      >
        <img
          src="Imagenes/hero-main.jpg"
          alt="Media Planning Canvas — Plan de marketing con IA"
          style={{ width: "100%", height: "100%", objectFit: "cover", objectPosition: "top", display: "block" }}
        />
      </div>
    );
  }
  if (shotStyle === "stack") {
    return (
      <div style={{ position: "relative" }}>
        <BrowserWindow
          src="assets/app-04-estrategia.png"
          alt="Pantalla de Estrategia"
          style={{ transform: "rotate(-1.2deg)" }}
        />
        <div
          style={{
            position: "absolute",
            bottom: -36,
            right: -24,
            width: "45%",
            transform: "rotate(3deg)",
          }}
        >
          <FloatingShot src="assets/app-03-target.png" alt="Target" caption="03 · Target" />
        </div>
      </div>
    );
  }
  // default: browser — imagen hero nueva
  return (
    <div
      style={{
        borderRadius: 16,
        overflow: "hidden",
        boxShadow: "0 24px 80px -20px rgba(14,34,54,0.28)",
        maxHeight: 480,
        border: "1px solid var(--line)",
      }}
    >
      <img
        src="Imagenes/hero-main.jpg"
        alt="Media Planning Canvas — Plan de marketing con IA"
        style={{
          width: "100%",
          height: "100%",
          objectFit: "cover",
          objectPosition: "top",
          display: "block",
        }}
      />
    </div>
  );
}

function HeroSplit({ headline, shotStyle }) {
  return (
    <header
      id="top"
      style={{
        paddingTop: 100,
        paddingBottom: 60,
        position: "relative",
      }}
    >
      <div className="container">
        <div
          style={{
            display: "grid",
            gridTemplateColumns: "minmax(0, 1.05fr) minmax(0, 1fr)",
            gap: 64,
            alignItems: "center",
          }}
          className="hero-grid"
        >
          <Reveal stagger>
            <div style={{ marginBottom: 28 }}>
              <HeroEyebrow />
            </div>
            <h1
              className="display"
              style={{ fontSize: "clamp(44px, 6.4vw, 88px)", marginBottom: 28 }}
            >
              {headline}
            </h1>
            <div style={{ marginBottom: 36 }}>
              <HeroSubline />
            </div>
            <HeroCTAs />
          </Reveal>

          <Reveal>
            <HeroShot shotStyle={shotStyle} />
          </Reveal>
        </div>

        <BrandStrip />
      </div>

      <style>{`
        @media (max-width: 960px) {
          .hero-grid { grid-template-columns: 1fr !important; gap: 48px !important; }
        }
      `}</style>
    </header>
  );
}

function HeroCentered({ headline, shotStyle }) {
  return (
    <header id="top" style={{ paddingTop: 160, paddingBottom: 80, position: "relative" }}>
      <div className="container" style={{ textAlign: "center" }}>
        <Reveal stagger>
          <div style={{ marginBottom: 28, display: "flex", justifyContent: "center" }}>
            <HeroEyebrow />
          </div>
          <h1
            className="display"
            style={{
              fontSize: "clamp(52px, 8vw, 120px)",
              marginBottom: 28,
              maxWidth: 1100,
              marginLeft: "auto",
              marginRight: "auto",
            }}
          >
            {headline}
          </h1>
          <div style={{ marginBottom: 32, display: "flex", justifyContent: "center" }}>
            <div style={{ maxWidth: 600 }}>
              <HeroSubline />
            </div>
          </div>
          <div style={{ display: "flex", justifyContent: "center" }}>
            <HeroCTAs />
          </div>
        </Reveal>

        <Reveal>
          <div style={{ marginTop: 72, maxWidth: 1100, marginLeft: "auto", marginRight: "auto" }}>
            <HeroShot shotStyle={shotStyle} />
          </div>
        </Reveal>

        <BrandStrip />
      </div>
    </header>
  );
}

function HeroEditorial({ headline, shotStyle }) {
  return (
    <header id="top" style={{ paddingTop: 100, paddingBottom: 80, position: "relative" }}>
      <div className="container">
        <Reveal stagger>
          <div style={{ marginBottom: 32 }}>
            <HeroEyebrow />
          </div>
          <h1
            className="display"
            style={{ fontSize: "clamp(56px, 9vw, 140px)", marginBottom: 40, lineHeight: 0.96 }}
          >
            {headline}
          </h1>

          <div
            style={{
              display: "grid",
              gridTemplateColumns: "minmax(0, 1fr) minmax(0, 1.2fr)",
              gap: 56,
              alignItems: "end",
              marginTop: 24,
            }}
            className="hero-grid"
          >
            <div>
              <div style={{ marginBottom: 32 }}>
                <HeroSubline />
              </div>
              <HeroCTAs />
            </div>

            <div>
              <HeroShot shotStyle={shotStyle === "browser" ? "floating" : shotStyle} />
            </div>
          </div>
        </Reveal>

        <BrandStrip />
      </div>
    </header>
  );
}

function BrandStrip() {
  return (
    <div style={{ marginTop: 100, paddingTop: 28, borderTop: "1px solid var(--line)" }}>
      <div
        style={{
          display: "flex",
          alignItems: "center",
          gap: 32,
          justifyContent: "space-between",
          flexWrap: "wrap",
        }}
      >
        <span className="mono" style={{ color: "var(--ink-muted)" }}>
          PROFESIONALES QUE HAN APRENDIDO CON EL MÉTODO
        </span>
        <div className="marquee" style={{ flex: 1, minWidth: 280 }}>
          {[0, 1].map((rep) => (
            <div className="marquee-track" key={rep} aria-hidden={rep === 1}>
              {[
                "EY",
                "KPMG",
                "IE BUSINESS SCHOOL",
                "COCA-COLA",
                "LEROY MERLIN",
                "P&G",
                "OMNICOM MEDIA",
                "HAVAS MEDIA",
                "SACYR",
                "NESTLÉ",
                "MERCK",
                "MASTERCARD",
                "GOOGLE",
                "SAGE",
                "ORACLE",
                "UBER",
                "NETFLIX",
                "MÁS ORANGE",
                "BT",
              ].map((b, i) => (
                <span
                  key={i}
                  style={{
                    fontFamily: "var(--font-mono)",
                    fontSize: 12,
                    letterSpacing: "0.18em",
                    color: "var(--ink-muted)",
                    whiteSpace: "nowrap",
                  }}
                >
                  {b}
                </span>
              ))}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { Nav, Hero, Logo });
