// Clube da Luz — sections part A: Nav, Hero, Marquee, Manifesto
// Hero remixed — mixed blue ink + gold portrait, parallax, mouse tilt 3D.

const { useState, useEffect, useRef, useMemo } = React;

/* ============================================================ */
/* Mark                                                          */
/* ============================================================ */
function Mark({ size = 22 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
      <defs>
        <linearGradient id="markGrad" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#7AD7E8" />
          <stop offset="100%" stopColor="#E8C079" />
        </linearGradient>
      </defs>
      <circle cx="16" cy="16" r="15" stroke="url(#markGrad)" strokeWidth="1" opacity="0.7" />
      <circle cx="16" cy="16" r="7" stroke="url(#markGrad)" strokeWidth="1" />
      <circle cx="16" cy="16" r="2" fill="url(#markGrad)" />
      <line x1="16" y1="1" x2="16" y2="6" stroke="url(#markGrad)" strokeWidth="1" />
      <line x1="16" y1="26" x2="16" y2="31" stroke="url(#markGrad)" strokeWidth="1" />
      <line x1="1" y1="16" x2="6" y2="16" stroke="url(#markGrad)" strokeWidth="1" />
      <line x1="26" y1="16" x2="31" y2="16" stroke="url(#markGrad)" strokeWidth="1" />
    </svg>
  );
}

/* ============================================================ */
/* Nav (responsive)                                              */
/* ============================================================ */
function Nav() {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const navStyle = {
    position: "fixed", top: 0, left: 0, right: 0, zIndex: 50,
    padding: scrolled ? "12px 0" : "22px 0",
    backdropFilter: scrolled ? "blur(18px) saturate(140%)" : "none",
    WebkitBackdropFilter: scrolled ? "blur(18px) saturate(140%)" : "none",
    background: scrolled ? "rgba(6,9,18,0.7)" : "transparent",
    borderBottom: scrolled ? "1px solid var(--line)" : "1px solid transparent",
    transition: "all .35s ease",
  };

  return (
    <nav style={navStyle}>
      <div className="wrap between">
        <a href="#" style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <Mark />
          <div style={{ display: "flex", flexDirection: "column", lineHeight: 1 }}>
            <span className="display display-italic" style={{ fontSize: 22 }}>Clube da Luz</span>
            <span className="serial" style={{ marginTop: 4 }}>EST. MMXXVI · CÍRCULO PRIVADO</span>
          </div>
        </a>

        <div className="nav-links-desktop" style={{ display: "flex", alignItems: "center", gap: 28, fontSize: 13.5 }}>
          <a href="#manifesto" className="muted">Manifesto</a>
          <a href="#trilhas" className="muted">Trilhas</a>
          <a href="#mentor" className="muted">Mentor</a>
          <a href="#regua" className="muted">Régua</a>
          <a href="#aplicar" className="btn" style={{ padding: "10px 18px" }}>
            Solicitar acesso
            <svg className="btn-arrow" viewBox="0 0 24 24" fill="none">
              <path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
            </svg>
          </a>
        </div>

        {/* Mobile burger */}
        <button
          onClick={() => setOpen(o => !o)}
          aria-label="Abrir menu"
          style={{
            display: "none",
            width: 40, height: 40, borderRadius: 8,
            border: "1px solid var(--line-strong)",
            background: "rgba(255,255,255,0.04)",
            alignItems: "center", justifyContent: "center",
          }}
          className="nav-burger-mobile"
        >
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
            {open
              ? <path d="M6 6l12 12M18 6l-12 12" stroke="currentColor" strokeWidth="1.5" />
              : <path d="M4 7h16M4 12h16M4 17h16" stroke="currentColor" strokeWidth="1.5" />
            }
          </svg>
        </button>
      </div>

      {/* Mobile drawer */}
      {open && (
        <div className="mobile-drawer" style={{
          position: "absolute", top: "100%", left: 0, right: 0,
          background: "rgba(6,9,18,0.96)",
          backdropFilter: "blur(20px)",
          borderTop: "1px solid var(--line)",
          borderBottom: "1px solid var(--line)",
          padding: "18px 0",
        }}>
          <div className="wrap" style={{ display: "flex", flexDirection: "column", gap: 4 }}>
            {[
              ["Manifesto", "#manifesto"], ["Trilhas", "#trilhas"],
              ["Mentor", "#mentor"], ["Régua", "#regua"], ["Solicitar acesso", "#aplicar"]
            ].map(([t, h]) => (
              <a key={h} href={h} onClick={() => setOpen(false)}
                style={{ padding: "14px 4px", fontSize: 18, fontFamily: "var(--f-display)", borderBottom: "1px solid var(--line)" }}>
                {t}
              </a>
            ))}
          </div>
        </div>
      )}

      <style>{`
        @media (max-width: 820px) {
          .nav-burger-mobile { display: flex !important; }
        }
      `}</style>
    </nav>
  );
}

/* ============================================================ */
/* Hero — composição azul/dourado, parallax, 3D                  */
/* ============================================================ */
function Hero() {
  const y = window.useScrollY();
  const tilt = window.useMouseTilt(0.6);
  const vw = window.useViewport();
  const isMobile = vw <= 820;

  // parallax factors (subtle)
  const inkY    = y * -0.18;
  const portY   = y * -0.10;
  const fogY    = y *  0.06;
  const sparkY  = y * -0.32;
  const heroFade = Math.max(0, 1 - y / 600);

  const tiltStyle = (depth) => ({
    transform: `translate3d(${tilt.tx * depth * -20}px, ${tilt.ty * depth * -20}px, 0)`,
    transition: "transform .15s cubic-bezier(.2,.7,.2,1)",
  });

  return (
    <section
      ref={tilt.ref}
      style={{
        minHeight: isMobile ? "auto" : "100vh",
        position: "relative",
        paddingTop: isMobile ? 110 : 120,
        paddingBottom: isMobile ? 60 : 80,
        overflow: "hidden",
      }}
      className="scene-3d"
    >
      {/* layer 1 — blue ink background (LEFT) */}
      <div style={{
        position: "absolute", inset: 0,
        backgroundImage: "url('assets/hero-ink.png')",
        backgroundSize: "cover",
        backgroundPosition: "left center",
        opacity: 0.5 * heroFade,
        transform: `translate3d(${tilt.tx * -14}px, ${inkY}px, 0)`,
        WebkitMaskImage: "radial-gradient(60% 80% at 25% 50%, black 35%, transparent 85%)",
        maskImage: "radial-gradient(60% 80% at 25% 50%, black 35%, transparent 85%)",
        mixBlendMode: "screen",
        willChange: "transform",
      }} />

      {/* layer 2 — Adriano portrait (RIGHT), tinted & blended */}
      <div style={{
        position: "absolute",
        right: isMobile ? "-10%" : "-2%",
        top: isMobile ? "40%" : "0",
        bottom: 0,
        width: isMobile ? "120%" : "62%",
        backgroundImage: "url('assets/adriano.png')",
        backgroundSize: isMobile ? "auto 80%" : "auto 105%",
        backgroundPosition: isMobile ? "center top" : "center 8%",
        backgroundRepeat: "no-repeat",
        opacity: isMobile ? 0.45 : 0.85,
        filter: "saturate(1.1) contrast(1.05)",
        transform: `translate3d(${tilt.tx * -8}px, ${portY}px, 0)`,
        WebkitMaskImage: isMobile
          ? "linear-gradient(180deg, transparent 0%, black 35%, black 80%, transparent 100%)"
          : "linear-gradient(90deg, transparent 0%, black 35%, black 100%)",
        maskImage: isMobile
          ? "linear-gradient(180deg, transparent 0%, black 35%, black 80%, transparent 100%)"
          : "linear-gradient(90deg, transparent 0%, black 35%, black 100%)",
        willChange: "transform",
      }} />

      {/* layer 3 — aqua/gold fog wash to bind the two together */}
      <div style={{
        position: "absolute", inset: 0,
        background:
          "radial-gradient(900px 700px at 28% 38%, rgba(74,184,216,0.28), transparent 65%)," +
          "radial-gradient(800px 700px at 70% 55%, rgba(232,192,121,0.18), transparent 60%)," +
          "linear-gradient(180deg, rgba(6,9,18,0.45) 0%, rgba(6,9,18,0.15) 40%, rgba(6,9,18,0.92) 100%)",
        transform: `translate3d(0, ${fogY}px, 0)`,
        pointerEvents: "none",
      }} />

      {/* layer 4 — drifting sparkles (gold + aqua) */}
      <Sparkles count={isMobile ? 12 : 22} parallax={sparkY} />

      {/* layer 5 — content */}
      <div className="wrap" style={{ position: "relative", zIndex: 3, paddingTop: isMobile ? 30 : 60 }}>
        <div
          className="stack-mobile"
          style={{
            display: "grid",
            gridTemplateColumns: "1.05fr 0.95fr",
            gap: isMobile ? 40 : 80,
            alignItems: "center",
            minHeight: isMobile ? "auto" : "68vh",
          }}
        >
          <div style={tiltStyle(0.4)}>
            <div className="reveal in" style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: isMobile ? 22 : 36 }}>
              <span className="eyebrow">— Edição I · 2026</span>
              <span className="serial">∴ Acesso por convite</span>
            </div>

            <h1
              className="display"
              style={{
                fontSize: isMobile ? "clamp(48px, 13vw, 78px)" : "clamp(64px, 8.5vw, 124px)",
                marginBottom: isMobile ? 24 : 32,
                color: "var(--ivory)",
              }}
            >
              <span className="line-rise line-rise-1" style={{ display: "block" }}>Onde a</span>
              <span className="display-italic grad-dual line-rise line-rise-2" style={{ display: "block" }}>
                criação
              </span>
              <span className="line-rise line-rise-3" style={{ display: "block" }}>encontra a sua</span>
              <span className="display-italic grad-gold line-rise line-rise-4" style={{ display: "block" }}>
                luz.
              </span>
            </h1>

            <div
              className="line-rise line-rise-5"
              style={{
                display: "flex", gap: 14, alignItems: "center",
                flexWrap: "wrap",
              }}
            >
              <a href="#aplicar" className="btn btn-primary" style={{ padding: "16px 26px" }}>
                Iniciar aplicação
                <svg className="btn-arrow" viewBox="0 0 24 24" fill="none">
                  <path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
                </svg>
              </a>
              <a href="#manifesto" className="btn">Ler o manifesto</a>
            </div>
          </div>

          {/* Right column — empty visual breathing space (the portrait lives in layer 2) */}
          <div className="hide-mobile" style={{ minHeight: "60vh" }} />
        </div>

        {/* bottom strip — info compacta */}
        <div
          style={{
            marginTop: isMobile ? 60 : 80,
            paddingTop: 24,
            borderTop: "1px solid var(--line)",
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            color: "var(--muted)",
            fontSize: 11.5,
            letterSpacing: "0.1em",
            textTransform: "uppercase",
            fontFamily: "var(--f-mono)",
            flexWrap: "wrap",
            gap: 16,
          }}
          className="line-rise line-rise-6"
        >
          <span>Próxima imersão · 14.06.2026 · Rio de Janeiro</span>
          <span style={{ display: "flex", alignItems: "center", gap: 8 }}>
            <span style={{
              width: 6, height: 6, borderRadius: "50%", background: "var(--gold)",
              boxShadow: "0 0 12px var(--gold)",
              animation: "pulseDotG 1.6s ease-in-out infinite"
            }} />
            12 vagas · Inscrições abertas
          </span>
          <span className="hide-mobile">Edição I — Vol. 01</span>
        </div>
      </div>

      <style>{`
        @keyframes pulseDotG {
          0%,100% { opacity: 1; transform: scale(1); box-shadow: 0 0 12px var(--gold); }
          50%     { opacity: .55; transform: scale(.78); box-shadow: 0 0 4px var(--gold); }
        }
      `}</style>
    </section>
  );
}

/* Sparkles — pontos dourados/aqua flutuando com leve parallax */
function Sparkles({ count = 18, parallax = 0 }) {
  const dots = useMemo(() => {
    const seed = [];
    for (let i = 0; i < count; i++) {
      const gold = Math.random() > 0.4;
      seed.push({
        left:   Math.random() * 80 + 5,
        top:    Math.random() * 90 + 5,
        size:   Math.random() * 4 + 1.5,
        delay:  Math.random() * 6,
        dur:    Math.random() * 6 + 6,
        dx:     (Math.random() - 0.5) * 80,
        dy:     -Math.random() * 140 - 40,
        gold,
      });
    }
    return seed;
  }, [count]);

  return (
    <div style={{
      position: "absolute", inset: 0, pointerEvents: "none",
      transform: `translate3d(0, ${parallax}px, 0)`,
      willChange: "transform",
      zIndex: 2,
    }}>
      {dots.map((d, i) => (
        <span
          key={i}
          style={{
            position: "absolute",
            left: `${d.left}%`,
            top:  `${d.top}%`,
            width: d.size, height: d.size,
            borderRadius: "50%",
            background: d.gold ? "var(--gold)" : "var(--aqua)",
            boxShadow: d.gold
              ? "0 0 12px rgba(232,192,121,0.85), 0 0 22px rgba(232,192,121,0.4)"
              : "0 0 12px rgba(122,215,232,0.85), 0 0 22px rgba(122,215,232,0.4)",
            opacity: 0,
            animation: `sparkleDrift ${d.dur}s ${d.delay}s ease-in-out infinite`,
            ["--dx"]: `${d.dx}px`,
            ["--dy"]: `${d.dy}px`,
          }}
        />
      ))}
    </div>
  );
}

/* ============================================================ */
/* Marquee strip                                                 */
/* ============================================================ */
function Marquee() {
  const items = [
    "Criatividade aplicada",
    "Mentoria viva",
    "Régua criativa",
    "Laboratório de projetos",
    "Círculo privado",
    "Rituais semanais",
    "Biblioteca selecionada",
  ];
  return (
    <div style={{
      borderTop: "1px solid var(--line)",
      borderBottom: "1px solid var(--line)",
      padding: "22px 0",
      overflow: "hidden",
      background: "rgba(10,15,28,0.4)",
    }}>
      <div style={{
        display: "flex",
        gap: 64,
        whiteSpace: "nowrap",
        animation: "marquee 40s linear infinite",
      }}>
        {[...items, ...items, ...items].map((t, i) => (
          <span key={i} style={{
            fontFamily: "var(--f-display)",
            fontStyle: "italic",
            fontSize: 26,
            color: "var(--ivory-dim)",
            display: "inline-flex",
            alignItems: "center",
            gap: 64,
          }}>
            {t}
            <span style={{ color: i % 2 ? "var(--gold)" : "var(--aqua)", fontSize: 14 }}>✦</span>
          </span>
        ))}
      </div>
    </div>
  );
}

/* ============================================================ */
/* Manifesto                                                     */
/* ============================================================ */
function Manifesto() {
  return (
    <section id="manifesto" style={{ padding: "120px 0 140px" }}>
      <div className="wrap">
        <div
          className="stack-mobile"
          style={{ display: "grid", gridTemplateColumns: "240px 1fr", gap: 80 }}
        >
          <window.Reveal>
            <div className="eyebrow" style={{ marginBottom: 16 }}>— 01 · Manifesto</div>
            <div className="serial">∴ Lido em voz alta no início de cada ciclo.</div>
          </window.Reveal>
          <div style={{ maxWidth: 820 }}>
            <window.Reveal delay={1} slow>
              <p className="display" style={{ fontSize: "clamp(30px, 4vw, 56px)", lineHeight: 1.15, marginBottom: 40 }}>
                Criatividade não é talento. É <span className="display-italic grad-dual">um modo de estar no mundo</span> —
                uma escuta refinada, um músculo treinado, uma escolha diária de tornar visível
                o que ainda não tem forma.
              </p>
            </window.Reveal>
            <div
              className="stack-mobile"
              style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 60, marginTop: 60 }}
            >
              <window.Reveal delay={2}>
                <p style={{ fontSize: 15.5, lineHeight: 1.75, color: "var(--ivory-dim)" }}>
                  Este é um lugar para quem reconhece que <em>fazer</em> é mais importante do que esperar
                  a inspiração. Aqui, a obra acontece em comunidade — entre rituais, encontros, trilhas
                  de estudo e provocações que afiam o olhar.
                </p>
              </window.Reveal>
              <window.Reveal delay={3}>
                <p style={{ fontSize: 15.5, lineHeight: 1.75, color: "var(--ivory-dim)" }}>
                  Não vendemos cursos. Sustentamos uma prática. Quem entra, entra para ficar, para
                  contribuir, para liderar pequenos movimentos dentro do círculo. A entrada é por
                  aplicação — porque o tempo de cada mentor é finito.
                </p>
              </window.Reveal>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { Mark, Nav, Hero, Marquee, Manifesto });
