// Ikigai — Hero section
// Apple keynote-style: oversized type, kanji watermark, cinematic figure,
// scroll-driven parallax. Three tonal variants: bright, dark, editorial.

const { useEffect: useEffectH, useRef: useRefH, useState: useStateH } = React;

function Hero({ variant = "bright" }) {
  const y = useScrollY();
  const heroRef = useRefH(null);

  // parallax: kanji moves slower than scroll, figure slightly faster
  const kanjiY = -y * 0.15;
  const figureY = y * 0.08;
  const titleY = -y * 0.05;
  const fadeOut = Math.max(0, 1 - y / 600);

  if (variant === "dark") return <HeroDark y={y} />;
  if (variant === "editorial") return <HeroEditorial y={y} />;
  if (variant === "cinematic") return <HeroCinematic y={y} />;

  // ── BRIGHT (default) — full-bleed video background
  return (
    <section
      ref={heroRef}
      id="top"
      style={{
        position: "relative",
        minHeight: "100vh",
        overflow: "hidden",
        color: "#fff",
        background: "#0a0a0a",
      }}
    >
      {/* Video background */}
      <video
        src="assets/hero-kata.mp4"
        autoPlay muted loop playsInline
        aria-hidden="true"
        style={{
          position: "absolute", inset: 0,
          width: "100%", height: "100%", objectFit: "cover",
          zIndex: 0,
          transform: `scale(1.05) translateY(${y * 0.08}px)`,
        }}
      />

      {/* Dark gradient overlay for legibility */}
      <div style={{
        position: "absolute", inset: 0, zIndex: 1, pointerEvents: "none",
        background: "linear-gradient(180deg, rgba(0,0,0,0.55) 0%, rgba(0,0,0,0.35) 35%, rgba(0,0,0,0.5) 70%, rgba(0,0,0,0.85) 100%)",
      }} />

      {/* Kanji watermark — subtle, on top of overlay */}
      <div
        style={{
          position: "absolute",
          top: "30%", left: "50%",
          transform: `translate(-50%, calc(-50% + ${kanjiY}px))`,
          pointerEvents: "none",
          fontFamily: "var(--k-font-jp)",
          fontSize: "min(38vw, 540px)",
          lineHeight: 0.85,
          color: "#fff",
          opacity: 0.06,
          fontWeight: 400,
          letterSpacing: "-0.04em",
          whiteSpace: "nowrap",
          zIndex: 1,
        }}
        aria-hidden
      >
        空手
      </div>

      {/* Eyebrow + headline + CTAs */}
      <div className="k-container" style={{ paddingTop: 140, paddingBottom: 80, position: "relative", zIndex: 2, minHeight: "100vh", display: "flex", flexDirection: "column", justifyContent: "center" }}>
        <Reveal>
          <div style={{ display: "flex", justifyContent: "center", marginBottom: 18 }}>
            <img src="assets/logo-ikigai.png" alt="Ikigai" style={{ height: 110, width: "auto", filter: "drop-shadow(0 6px 24px rgba(0,0,0,0.5))" }}/>
          </div>
          <p className="k-eyebrow" style={{ textAlign: "center", marginBottom: 14, fontSize: 17, color: "#f5f5f7", letterSpacing: 0.02, fontWeight: 500 }}>
            Ikigai · Karatê Shotokan Tradicional · SKIBRASIL
          </p>
        </Reveal>
        <Reveal delay={120}>
          <h1
            className="k-h1-tight"
            style={{
              textAlign: "center",
              fontSize: "clamp(56px, 10vw, 124px)",
              color: "#fff",
              transform: `translateY(${titleY}px)`,
              opacity: fadeOut,
              marginBottom: 16,
              textShadow: "0 4px 30px rgba(0,0,0,0.45)",
            }}
          >
            O caminho não tem fim.
          </h1>
        </Reveal>
        <Reveal delay={240}>
          <p
            className="k-lead"
            style={{
              textAlign: "center",
              maxWidth: 640,
              margin: "0 auto",
              fontSize: "clamp(19px, 1.6vw, 24px)",
              color: "#e5e5ea",
              opacity: fadeOut,
              textShadow: "0 2px 16px rgba(0,0,0,0.5)",
            }}
          >
            Karatê tradicional para todas as idades e níveis.<br/>
            Disciplina, técnica e respeito desde 1987.
          </p>
        </Reveal>
        <Reveal delay={360}>
          <div style={{ display: "flex", justifyContent: "center", marginTop: 32, opacity: fadeOut }}>
            <div className="k-row" style={{ gap: 22, flexWrap: "wrap", alignItems: "center", justifyContent: "center" }}>
              <a href="#matricula" className="k-btn" style={{ background: "#fff", color: "#000" }}>Agendar aula experimental</a>
              <a href="#sobre" className="k-link" style={{ color: "#fff" }}>Conhecer o dojo</a>
            </div>
          </div>
        </Reveal>
      </div>

      {/* Scroll cue */}
      <div
        style={{
          position: "absolute",
          bottom: 28, left: "50%",
          transform: "translateX(-50%)",
          fontSize: 11,
          letterSpacing: 0.18,
          textTransform: "uppercase",
          color: "rgba(255,255,255,0.65)",
          opacity: fadeOut,
          fontWeight: 500,
          zIndex: 3,
          display: "flex", flexDirection: "column", alignItems: "center", gap: 6,
        }}
      >
        <span>Role para descobrir</span>
        <span style={{ fontSize: 14 }}>↓</span>
      </div>
    </section>
  );
}

// ── DARK variant — black, dramatic
function HeroDark({ y }) {
  const kanjiY = -y * 0.2;
  const titleY = -y * 0.05;
  const fadeOut = Math.max(0, 1 - y / 600);
  return (
    <section
      style={{
        position: "relative",
        background: "#000",
        color: "#fff",
        minHeight: "100vh",
        overflow: "hidden",
      }}
    >
      <img
        src="assets/figure-kick.svg"
        alt=""
        aria-hidden
        style={{
          position: "absolute",
          right: "-8%", top: "10%",
          height: "90%", width: "auto",
          opacity: 0.92,
          transform: `translateY(${y * 0.12}px)`,
        }}
      />
      <div
        style={{
          position: "absolute",
          top: "12%", left: "-4%",
          fontFamily: "var(--k-font-jp)",
          fontSize: "min(46vw, 680px)",
          lineHeight: 0.85,
          color: "#fff",
          opacity: 0.05,
          transform: `translateY(${kanjiY}px)`,
          letterSpacing: "-0.04em",
        }}
        aria-hidden
      >
        道
      </div>

      <div className="k-container" style={{ position: "relative", zIndex: 2, paddingTop: 180, maxWidth: 1080 }}>
        <Reveal>
          <p style={{ fontSize: 14, color: "#86868b", letterSpacing: 0.1, textTransform: "uppercase", marginBottom: 18, fontWeight: 500 }}>
            Ikigai — Edição Tradicional
          </p>
        </Reveal>
        <Reveal delay={150}>
          <h1
            className="k-h1-tight"
            style={{
              fontSize: "clamp(64px, 11vw, 168px)",
              color: "#fff",
              maxWidth: 900,
              transform: `translateY(${titleY}px)`,
              opacity: fadeOut,
              fontWeight: 700,
            }}
          >
            Comece<br/>onde você está.
          </h1>
        </Reveal>
        <Reveal delay={300}>
          <p className="k-lead" style={{ color: "#a1a1a6", maxWidth: 480, marginTop: 24, fontSize: "clamp(19px, 1.5vw, 22px)", opacity: fadeOut }}>
            Quatro décadas de prática. Três estilos. Uma escola.
            Treinamos corpo e mente — uma técnica de cada vez.
          </p>
        </Reveal>
        <Reveal delay={450}>
          <div style={{ marginTop: 32, opacity: fadeOut }}>
            <a href="#matricula" className="k-btn k-btn-light">Agendar aula experimental</a>
            <a href="#sobre" className="k-link" style={{ color: "#2997ff", marginLeft: 22 }}>Conheça o dojo</a>
          </div>
        </Reveal>
      </div>
    </section>
  );
}

// ── EDITORIAL variant — magazine, asymmetric
function HeroEditorial({ y }) {
  const fadeOut = Math.max(0, 1 - y / 600);
  return (
    <section
      style={{
        position: "relative",
        background: "var(--k-bg)",
        minHeight: "100vh",
        overflow: "hidden",
      }}
    >
      <div className="k-container-wide" style={{ paddingTop: 110, paddingBottom: 60, maxWidth: 1280 }}>
        <Reveal>
          <p className="k-eyebrow muted" style={{ marginBottom: 8, fontSize: 14, letterSpacing: 0.08, textTransform: "uppercase" }}>
            № 01 — Edição São Paulo · 1987–2026
          </p>
        </Reveal>
        <div className="k-grid" style={{ gridTemplateColumns: "1.2fr 1fr", gap: 60, alignItems: "end" }}>
          <div>
            <Reveal delay={120}>
              <h1
                style={{
                  fontFamily: "var(--k-font-display)",
                  fontSize: "clamp(56px, 9vw, 144px)",
                  fontWeight: 600,
                  letterSpacing: "-0.03em",
                  lineHeight: 0.95,
                  marginTop: 24,
                }}
              >
                Karatê,<br/>como sempre<br/>foi.
              </h1>
            </Reveal>
          </div>
          <div style={{ paddingBottom: 24 }}>
            <Reveal delay={300}>
              <div style={{ borderLeft: "1px solid var(--k-line)", paddingLeft: 22, maxWidth: 360 }}>
                <p className="k-body" style={{ fontSize: 18, lineHeight: 1.5, color: "var(--k-ink-2)" }}>
                  Sem hierarquia inflada. Sem coreografia.
                  O dojo Ikigai segue a escola tradicional
                  japonesa há quase quarenta anos —
                  formando praticantes, não consumidores.
                </p>
                <div style={{ marginTop: 22 }}>
                  <CTAPair primary="Conhecer o dojo" secondary="Ler a história" tone="blue" />
                </div>
              </div>
            </Reveal>
          </div>
        </div>

        {/* full-bleed image stripe */}
        <Reveal delay={500}>
          <div
            style={{
              marginTop: 60,
              borderRadius: 22,
              overflow: "hidden",
              aspectRatio: "21/9",
              background: "#000",
              transform: `translateY(${-y * 0.04}px)`,
            }}
          >
            <img src="assets/kata-sequence.svg" alt="Sequência de kata" style={{ width: "100%", height: "100%", objectFit: "cover" }} />
          </div>
        </Reveal>
      </div>
    </section>
  );
}

// ── CINEMATIC variant — bold editorial split, live class countdown
function HeroCinematic({ y }) {
  const fadeOut = Math.max(0, 1 - y / 700);
  const kanjiY = -y * 0.12;
  const photoY = y * 0.06;
  const [tick, setTick] = React.useState(0);

  // Próxima aula: Seg/Qua/Sex 09:00, 18:00, 19:00
  const proxAula = React.useMemo(() => {
    const horarios = [
      { dia: 1, hora: 9 }, { dia: 1, hora: 18 }, { dia: 1, hora: 19 },
      { dia: 3, hora: 9 }, { dia: 3, hora: 18 }, { dia: 3, hora: 19 },
      { dia: 5, hora: 9 }, { dia: 5, hora: 18 }, { dia: 5, hora: 19 },
    ];
    const agora = new Date();
    let melhor = null;
    for (let dOffset = 0; dOffset < 14; dOffset++) {
      const data = new Date(agora);
      data.setDate(agora.getDate() + dOffset);
      const diaSemana = data.getDay();
      for (const h of horarios) {
        if (h.dia !== diaSemana) continue;
        const ts = new Date(data);
        ts.setHours(h.hora, 0, 0, 0);
        if (ts > agora && (!melhor || ts < melhor.ts)) melhor = { ts, dia: diaSemana, hora: h.hora };
      }
      if (melhor) break;
    }
    return melhor;
  }, [tick]);

  React.useEffect(() => {
    const t = setInterval(() => setTick(x => x + 1), 60000);
    return () => clearInterval(t);
  }, []);

  const nomeDia = (d) => ["Domingo","Segunda","Terça","Quarta","Quinta","Sexta","Sábado"][d];
  const formatCountdown = (target) => {
    if (!target) return "";
    const diff = target.ts - new Date();
    if (diff < 0) return "agora";
    const horas = Math.floor(diff / 3600000);
    const mins = Math.floor((diff % 3600000) / 60000);
    if (horas >= 24) return `em ${Math.floor(horas/24)}d ${horas%24}h`;
    if (horas > 0) return `em ${horas}h ${mins}min`;
    return `em ${mins}min`;
  };

  return (
    <section id="top" style={{
      position: "relative", minHeight: "100vh", overflow: "hidden",
      background: "linear-gradient(135deg, #0a0a0a 0%, #1d1d1f 60%, #0a0a0a 100%)",
      color: "#fff",
    }}>
      {/* Glow ambient */}
      <div style={{
        position: "absolute", inset: 0, pointerEvents: "none", opacity: 0.5,
        background: `radial-gradient(circle at 20% 30%, rgba(255,255,255,0.04), transparent 50%), radial-gradient(circle at 80% 70%, rgba(230,168,0,0.06), transparent 50%)`,
      }} />

      {/* Kanji 道 gigante ao fundo */}
      <div style={{
        position: "absolute", left: "-8%", top: `calc(8% + ${kanjiY}px)`,
        fontFamily: "var(--k-font-jp)", fontSize: "min(72vw, 1100px)",
        lineHeight: 0.85, color: "#fff", opacity: 0.035, fontWeight: 400,
        pointerEvents: "none", userSelect: "none",
      }} aria-hidden>道</div>

      {/* Linha decorativa vertical à esquerda */}
      <div style={{
        position: "absolute", left: 60, top: 0, bottom: 0, width: 1,
        background: "linear-gradient(180deg, transparent, rgba(255,255,255,0.08), transparent)",
        pointerEvents: "none",
      }} aria-hidden />

      <div className="k-hero-cinematic-grid" style={{
        position: "relative", zIndex: 2,
        maxWidth: 1440, margin: "0 auto",
        padding: "120px 60px 80px",
        display: "grid", gridTemplateColumns: "1.3fr 1fr",
        gap: 60, alignItems: "center", minHeight: "100vh",
      }}>
        {/* LEFT */}
        <div style={{ opacity: fadeOut }}>
          <Reveal>
            <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 26 }}>
              <span style={{ width: 32, height: 1, background: "#e6a800" }} />
              <p className="k-eyebrow" style={{
                color: "#e6a800", fontSize: 12, letterSpacing: 0.16,
                textTransform: "uppercase", fontWeight: 600, margin: 0,
              }}>道場 · Dojô Ikigai · Nilópolis RJ</p>
            </div>
          </Reveal>

          <Reveal delay={100}>
            <h1 style={{
              fontFamily: "var(--k-font-display)",
              fontSize: "clamp(72px, 13vw, 200px)",
              fontWeight: 700, letterSpacing: "-0.04em",
              lineHeight: 0.88, margin: 0, color: "#fff",
              textShadow: "0 4px 60px rgba(230,168,0,0.06)",
            }}>IKIGAI</h1>
          </Reveal>

          <Reveal delay={200}>
            <div style={{ display: "flex", alignItems: "baseline", gap: 22, marginTop: 14, marginBottom: 32, flexWrap: "wrap" }}>
              <p style={{
                fontFamily: "var(--k-font-jp)", fontSize: 36, letterSpacing: "0.4em",
                color: "#fff", margin: 0, fontWeight: 400,
              }} aria-label="ikigai em japonês">生き甲斐</p>
              <span style={{ fontSize: 14, color: "#86868b", letterSpacing: 0.04 }}>
                <em style={{ color: "#a1a1a6" }}>"a razão pela qual você acorda de manhã"</em>
              </span>
            </div>
          </Reveal>

          <Reveal delay={300}>
            <p style={{
              fontFamily: "var(--k-font-display)",
              fontSize: "clamp(22px, 2.2vw, 30px)",
              fontWeight: 400, lineHeight: 1.35,
              color: "#f5f5f7", maxWidth: 580, margin: "0 0 18px",
              letterSpacing: "-0.01em",
            }}>
              Karatê Shotokan tradicional, no ritmo da<br/>tradição japonesa.
            </p>
          </Reveal>

          <Reveal delay={400}>
            <p style={{
              fontSize: 16, lineHeight: 1.6, color: "#a1a1a6",
              maxWidth: 460, margin: "0 0 36px",
            }}>
              Sob a direção do <strong style={{ color: "#fff", fontWeight: 500 }}>Mestre Inst. Robson</strong>,
              filiado à SKIBRASIL. Para todas as idades a partir de 14 anos.
            </p>
          </Reveal>

          <Reveal delay={500}>
            <div style={{ display: "flex", alignItems: "center", gap: 20, marginBottom: 48, flexWrap: "wrap" }}>
              <a href="#matricula" style={{
                background: "#fff", color: "#000",
                padding: "16px 32px", borderRadius: 999,
                fontSize: 16, fontWeight: 600, letterSpacing: "-0.01em",
                textDecoration: "none", display: "inline-flex", alignItems: "center", gap: 8,
                transition: "transform 0.2s var(--k-ease)",
              }}
                onMouseEnter={(e) => e.currentTarget.style.transform = "translateY(-2px)"}
                onMouseLeave={(e) => e.currentTarget.style.transform = "translateY(0)"}>
                Agendar aula experimental
                <span style={{ fontSize: 18 }}>→</span>
              </a>
              <a href="#sensei" style={{
                color: "#fff", fontSize: 15, fontWeight: 500,
                textDecoration: "none", borderBottom: "1px solid rgba(255,255,255,0.25)",
                paddingBottom: 4, letterSpacing: "-0.01em",
              }}>Conhecer o Mestre Robson</a>
            </div>
          </Reveal>

          {/* Live ticker — próxima aula */}
          {proxAula && (
            <Reveal delay={600}>
              <div style={{
                display: "inline-flex", alignItems: "center", gap: 14,
                padding: "14px 22px",
                background: "rgba(255,255,255,0.04)",
                border: "1px solid rgba(255,255,255,0.08)",
                borderRadius: 14,
                backdropFilter: "blur(10px)",
                flexWrap: "wrap",
              }}>
                <span style={{ position: "relative", display: "inline-flex", width: 10, height: 10 }}>
                  <span style={{
                    position: "absolute", inset: 0, borderRadius: "50%",
                    background: "#2da06a", animation: "k-pulse-dot 2s ease-in-out infinite",
                  }} />
                  <span style={{
                    position: "absolute", inset: 0, borderRadius: "50%",
                    background: "#2da06a", opacity: 0.3,
                    animation: "k-pulse-ring 2s ease-out infinite",
                  }} />
                </span>
                <div style={{ display: "flex", gap: 18, alignItems: "center", flexWrap: "wrap" }}>
                  <span style={{ fontSize: 11, letterSpacing: 0.12, textTransform: "uppercase", color: "#86868b", fontWeight: 600 }}>
                    Próxima aula
                  </span>
                  <span style={{ fontSize: 14, color: "#fff", fontWeight: 500 }}>
                    {nomeDia(proxAula.dia)} · {String(proxAula.hora).padStart(2, "0")}:00
                  </span>
                  <span style={{ fontSize: 13, color: "#e6a800", fontWeight: 600 }}>
                    {formatCountdown(proxAula)}
                  </span>
                </div>
              </div>
            </Reveal>
          )}
        </div>

        {/* RIGHT — Mestre Robson photo */}
        <Reveal delay={350}>
          <div style={{ position: "relative", transform: `translateY(${photoY}px)` }}>
            {/* Vertical kanji caption */}
            <div style={{
              position: "absolute", top: 0, left: -56,
              writingMode: "vertical-rl", textOrientation: "upright",
              fontFamily: "var(--k-font-jp)", fontSize: 18,
              letterSpacing: "0.4em", color: "#86868b",
              opacity: 0.7, lineHeight: 1.2, fontWeight: 400,
            }} aria-hidden>師 範 · MESTRE FUNDADOR</div>

            <div style={{
              position: "relative", aspectRatio: "4/5",
              borderRadius: 20, overflow: "hidden",
              background: "linear-gradient(180deg, #1d1d1f 0%, #0a0a0a 100%)",
              boxShadow: "0 30px 80px -20px rgba(0,0,0,0.6), inset 0 0 100px rgba(0,0,0,0.4)",
            }}>
              <img src="assets/sensei-robson.png?v=2" alt="Mestre Inst. Robson" style={{
                position: "absolute", bottom: 0, left: "50%",
                transform: "translateX(-50%)",
                height: "94%", width: "auto",
                filter: "drop-shadow(0 20px 40px rgba(0,0,0,0.5))",
              }} />
              <img src="assets/skib.png" alt="SKIBRASIL" style={{
                position: "absolute", top: 22, right: 22,
                width: 88, height: "auto", opacity: 0.92,
              }} />
              <div style={{
                position: "absolute", inset: 0,
                background: "linear-gradient(180deg, rgba(0,0,0,0.5) 0%, transparent 30%, transparent 70%, rgba(0,0,0,0.75) 100%)",
                pointerEvents: "none",
              }} />
              <div style={{
                position: "absolute", left: 24, bottom: 22, right: 24,
              }}>
                <p style={{ fontSize: 10.5, letterSpacing: 0.12, textTransform: "uppercase", color: "#a1a1a6", marginBottom: 4, fontWeight: 600 }}>
                  Faixa preta · SKIBRASIL
                </p>
                <p style={{ fontSize: 18, fontWeight: 600, letterSpacing: "-0.01em", color: "#fff", margin: 0 }}>
                  Mestre Inst. Robson
                </p>
              </div>
            </div>
          </div>
        </Reveal>
      </div>

      <div style={{
        position: "absolute", bottom: 28, left: "50%", transform: "translateX(-50%)",
        display: "flex", flexDirection: "column", alignItems: "center", gap: 6,
        color: "#86868b", fontSize: 10, letterSpacing: 0.18, textTransform: "uppercase",
        opacity: fadeOut, pointerEvents: "none",
      }}>
        <span>Role para descobrir</span>
        <span style={{ animation: "k-bounce 1.8s ease-in-out infinite" }}>↓</span>
      </div>

      <style>{`
        @keyframes k-pulse-dot { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.15); } }
        @keyframes k-pulse-ring { 0% { transform: scale(1); opacity: 0.4; } 100% { transform: scale(2.5); opacity: 0; } }
        @keyframes k-bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(6px); } }
        @media (max-width: 880px) {
          .k-hero-cinematic-grid { grid-template-columns: 1fr !important; gap: 40px !important; padding: 90px 24px 60px !important; min-height: auto !important; }
        }
      `}</style>
    </section>
  );
}

Object.assign(window, { Hero });
