/* global React */
const { useState, useEffect, useRef } = React;

// ─────────────────────────────────────────────────────────────────────────────
// Shared section primitives
// ─────────────────────────────────────────────────────────────────────────────

function Section({ id, label, title, kicker, children, dark = true, className = "" }) {
  return (
    <section id={id} className={`relative px-6 md:px-12 py-24 md:py-32 ${className}`}>
      <div className="max-w-6xl mx-auto">
        {(label || kicker) &&
        <div className="flex items-center gap-3 mb-6">
            <span className="brand-mono text-[11px] tracking-[0.2em] uppercase" style={{ color: "oklch(75% 0.18 var(--hue))" }}>
              {label}
            </span>
            {kicker && <span className="text-xs ink-fg-dim opacity-60">{kicker}</span>}
          </div>
        }
        {title &&
        <h2 className="text-3xl md:text-5xl font-bold mb-10 md:mb-14 leading-[1.25]" style={{ textWrap: "balance" }}>
            {title}
          </h2>
        }
        {children}
      </div>
    </section>);

}

// ─────────────────────────────────────────────────────────────────────────────
// Hero
// ─────────────────────────────────────────────────────────────────────────────

function Hero({ showRepMessage }) {
  const ref = useRef(null);
  const [scrollY, setScrollY] = useState(0);
  useEffect(() => {
    const onScroll = () => setScrollY(window.scrollY);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const py = Math.min(scrollY * 0.25, 200);

  return (
    <section ref={ref} className="relative min-h-[100vh] overflow-hidden grid-bg">
      {/* Hero banner image with parallax */}
      <div
        className="absolute inset-0"
        style={{
          backgroundImage: "url(assets/hero-banner.png)",
          backgroundSize: "auto 95%",
          backgroundPosition: `20% ${30 - py * 0.05}%`,
          backgroundRepeat: "no-repeat",
          backgroundColor: "#07091a",
          transform: `translateY(${py * 0.1}px)`,
          filter: "saturate(1.1) contrast(1.05)"
        }} />
      
      {/* dark overlay */}
      <div className="absolute inset-0" style={{
        background: "linear-gradient(180deg, rgba(7,9,26,0.55) 0%, rgba(7,9,26,0.35) 30%, rgba(7,9,26,0.85) 75%, rgba(7,9,26,1) 100%)"
      }} />
      {/* neon vignette */}
      <div className="absolute inset-0 pointer-events-none" style={{
        background: "radial-gradient(60% 50% at 50% 100%, oklch(60% 0.25 var(--hue) / 0.25), transparent 70%)"
      }} />

      {/* Top bar */}
      <div className="relative z-10 flex items-center justify-between px-6 md:px-12 py-6">
        <div className="flex items-center gap-3">
          <img src="assets/logo.png" alt="Codexラボ" className="h-12 md:h-14 drop-shadow-[0_0_16px_rgba(120,160,255,0.6)]" />
        </div>
        <nav className="hidden md:flex items-center gap-7 brand-mono text-[11px] tracking-[0.18em] uppercase">
          <a href="#message" className="opacity-70 hover:opacity-100 transition">Message</a>
          <a href="#challenge" className="opacity-70 hover:opacity-100 transition">Challenge</a>
          <a href="#value" className="opacity-70 hover:opacity-100 transition">Value</a>
          <a href="#about" className="opacity-70 hover:opacity-100 transition">About</a>
          <a href="#contact" className="px-4 py-2 rounded-full neon-border opacity-90 hover:opacity-100 transition">Follow</a>
        </nav>
      </div>

      {/* Hero content */}
      <div className="relative z-10 px-6 md:px-12 pt-12 md:pt-20 pb-32 max-w-6xl mx-auto">
        <div className="brand-mono text-[11px] tracking-[0.3em] uppercase opacity-60 mb-6 flex items-center gap-3">
          <span className="inline-block w-8 h-px bg-current opacity-60" />
          AI Education Project · Est. 2026
        </div>

        <h1 className="brand-serif text-[15vw] md:text-[10rem] leading-[0.9] font-medium mb-2 neon-text glow-pulse">
          Codex<span style={{ fontStyle: "italic", opacity: 0.85 }}>ラボ</span>
        </h1>

        <div className="flex items-baseline gap-4 mb-10 flex-wrap">
          <p className="text-2xl md:text-4xl font-bold tracking-tight">AI時代。全く新しいIT素養をゼロから身に付ける。</p>
        </div>

        <p className="brand-mono text-[11px] tracking-[0.25em] uppercase opacity-50 mb-12">
          Make it possible — in the lab. For the AI era.
        </p>

        {showRepMessage &&
        <div className="max-w-2xl glass p-7 md:p-9 rounded-2xl mb-12">
            <p className="text-[15px] md:text-[17px] leading-[1.95] opacity-95" style={{ textWrap: "pretty" }}>
              世界がAIによって置き換えられていく今、<br />
              我々がAIに追い付くためには、<br />
              幼少期から社会人まで磨き上げてきた素養を<br />
              質的にも量的にも遥かに超えるような<br /><strong style={{ color: "oklch(80% 0.18 var(--hue))" }}>「全く新しいレベルの素養」</strong> が必要となっています。
            </p>
            <p className="text-[13px] md:text-[14px] opacity-80 mt-5 leading-[1.6]">
              <span className="brand-mono tracking-[0.2em] uppercase text-[10px] opacity-60 mr-2">— Codexラボ代表</span>
              <span className="brand-serif text-xl md:text-2xl ml-1" style={{ color: "oklch(85% 0.18 var(--hue))" }}>ラボとみ</span>
              <span className="brand-mono tracking-[0.15em] uppercase text-[9px] opacity-50 ml-2">/ Excelラボ代表</span>
            </p>
          </div>
        }

        <div className="flex flex-wrap items-center gap-4">
          <a href="https://x.com/codex_labo" target="_blank" rel="noopener"
          className="group inline-flex items-center gap-3 px-6 py-3.5 rounded-full neon-border lift"
          style={{ background: "oklch(70% 0.22 var(--hue) / 0.12)" }}>
            <svg viewBox="0 0 24 24" className="w-4 h-4 fill-current"><path d="M18.244 2H21l-6.49 7.41L22 22h-6.18l-4.84-6.34L5.4 22H2.64l6.95-7.94L2 2h6.34l4.38 5.79L18.244 2zm-1.08 18.5h1.7L7.94 3.4H6.16l11.004 17.1z" /></svg>
            <span className="font-bold tracking-wide">@codex_labo をフォロー</span>
            <span className="brand-mono text-[10px] opacity-60 tracking-widest uppercase group-hover:translate-x-1 transition">→</span>
          </a>
          <a href="#message" className="brand-mono text-[11px] tracking-[0.25em] uppercase opacity-60 hover:opacity-100 transition px-4 py-3">
            Scroll · 代表メッセージへ ↓
          </a>
        </div>
      </div>

      {/* status pill, bottom-right */}
      <div className="absolute bottom-8 right-6 md:right-12 z-10 brand-mono text-[10px] tracking-[0.25em] uppercase">
        <div className="flex items-center gap-2 opacity-70">
          <span className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse" />
          2026.05 — content in progress
        </div>
      </div>
    </section>);

}

// ─────────────────────────────────────────────────────────────────────────────
// English marquee strip
// ─────────────────────────────────────────────────────────────────────────────

function Marquee() {
  const items = [
  "Make it possible — in the lab",
  "Talk to AI, talk for yourself",
  "From operator to explainer",
  "Pinpoint clarity, not feature parade",
  "AI literacy for the rest of us"];

  const all = [...items, ...items, ...items];
  return (
    <div className="relative py-6 border-y overflow-hidden" style={{ borderColor: "oklch(70% 0.22 var(--hue) / 0.18)" }}>
      <div className="flex gap-12 marquee-track whitespace-nowrap brand-serif text-2xl md:text-3xl italic opacity-70" style={{ width: "max-content" }}>
        {all.map((t, i) =>
        <span key={i} className="flex items-center gap-12">
            <span>{t}</span>
            <span className="opacity-40" style={{ color: "oklch(75% 0.18 var(--hue))" }}>✦</span>
          </span>
        )}
      </div>
    </div>);

}

// ─────────────────────────────────────────────────────────────────────────────
// Classroom bridge — establishes "education institution" tone right after Hero
// ─────────────────────────────────────────────────────────────────────────────

function ClassroomBridge() {
  return (
    <section className="relative px-6 md:px-12 py-20 md:py-28">
      <div className="max-w-6xl mx-auto">
        <div className="flex items-center gap-3 mb-8">
          <span className="brand-mono text-[11px] tracking-[0.2em] uppercase" style={{ color: "oklch(75% 0.18 var(--hue))" }}>
            ✦ Codex Labo Institute
          </span>
          <span className="text-xs opacity-50">For the Study of Logic, Mechanics, and Purpose</span>
        </div>

        <div className="relative rounded-2xl overflow-hidden lift" style={{ boxShadow: "0 30px 80px rgba(0,0,0,0.5), 0 0 60px oklch(70% 0.22 var(--hue) / 0.15)" }}>
          {/* sepia banner image - full bleed */}
          <img src="assets/classroom-banner.png" alt="Codex Labo Institute — Lesson I: Principles of Logic" className="w-full h-auto block" />
          {/* subtle vignette to blend with dark theme */}
          <div className="absolute inset-0 pointer-events-none" style={{
            background: "radial-gradient(120% 80% at 50% 50%, transparent 50%, rgba(7,9,26,0.35) 100%)"
          }} />
        </div>

        {/* caption row beneath banner */}
        <div className="mt-8 grid md:grid-cols-[1.4fr_1fr] gap-8 md:gap-12 items-end">
          <div>
            <h2 className="brand-serif text-3xl md:text-5xl leading-[1.25] mb-4" style={{ textWrap: "balance" }}>
              <span className="neon-text">AI時代の新しい素養を学べる講座
              </span>です。
            </h2>
            <p className="text-base md:text-lg leading-[1.95] opacity-80 max-w-2xl" style={{ textWrap: "pretty" }}>
              我々は一人一人が、「AIと対話」しながら業務を行っていくことになります。 その時、スーパーエンジニアであるAIと対話するには、 我々側も相当な知識のレベルアップをしておかなければ、 何をAIが言っているのか理解できず、会話が成立しないのです。<br className="hidden md:block" />
              Codexラボで、AIと対話するため、<br />
              必要な素養を見直し、皆様が自信を持って働けるよう共に学習していきましょう。
            </p>
          </div>
          <div className="brand-mono text-[10px] tracking-[0.25em] uppercase opacity-55 leading-[2] md:text-right">
            <div>Lesson I — Principles of Logic</div>
            <div></div>
            <div className="mt-2 opacity-70" style={{ color: "oklch(80% 0.18 var(--hue))" }}>
              Knowledge + Discipline = Progress
            </div>
          </div>
        </div>
      </div>
    </section>);
}

Object.assign(window, { Section, Hero, Marquee, ClassroomBridge });
