// バリエーションB: マガジン風グリッド (大判ヒーロー + 4カラムグリッド + サイドバー細め)

function VariationB({ activeCategory, setActiveCategory }) {
  const data = useData();
  const articles = activeCategory === "all"
    ? data.articles
    : data.articles.filter((a) => a.cat === activeCategory);

  const hero = data.articles.find((a) => a.id === 11) || data.articles[0];
  const subFeatures = data.articles.filter((a) => a.featured && a.id !== hero.id).slice(0, 2);
  const grid = articles.slice(0, 12);

  return (
    <div style={{ background: "#fff", fontFamily: "var(--sans)", color: "var(--ink)" }}>
      {/* Minimal magazine header */}
      <header style={{ borderBottom: "1px solid var(--rule)" }}>
        <div style={{ maxWidth: 1240, margin: "0 auto", padding: "26px 32px 14px", display: "flex", alignItems: "center" }}>
          <div style={{ flex: 1, fontSize: 11, color: "var(--slate-600)", letterSpacing: ".25em" }}>
            VOL.{data.established.slice(-2)} · {new Date().toLocaleDateString("ja-JP")}
          </div>
          <div style={{ textAlign: "center", flex: 2 }}>
            <div style={{ fontFamily: "var(--serif)", fontSize: 44, fontWeight: 700, letterSpacing: ".25em", color: "var(--navy-900)", lineHeight: 1 }}>
              {data.siteName}
            </div>
            <div style={{ fontSize: 10, letterSpacing: ".4em", color: "var(--slate-600)", marginTop: 6 }}>
              — {data.tagline} —
            </div>
          </div>
          <div style={{ flex: 1, textAlign: "right", fontSize: 11, color: "var(--slate-600)", letterSpacing: ".15em" }}>
            EST. {data.established} · TOKYO
          </div>
        </div>
        <nav style={{ borderTop: "2px solid var(--navy-900)", borderBottom: "1px solid var(--navy-900)" }}>
          <div style={{ maxWidth: 1240, margin: "0 auto", padding: "0 32px", display: "flex", justifyContent: "center", gap: 0 }}>
            {[
              { id: "all", label: "ALL" },
              ...data.categories.slice(0, 7).map((c) => ({ id: c.slug, label: c.name.toUpperCase() })),
            ].map((n) => (
              <button key={n.id} onClick={() => setActiveCategory(n.id)} style={{
                padding: "12px 22px", background: "transparent", border: "none", cursor: "pointer",
                fontSize: 12, fontWeight: 600, letterSpacing: ".15em", fontFamily: "var(--sans)",
                color: activeCategory === n.id ? "var(--accent-red)" : "var(--ink)",
                position: "relative",
              }}>
                {n.label}
                {activeCategory === n.id && <span style={{ position: "absolute", left: 12, right: 12, bottom: -1, height: 3, background: "var(--accent-red)" }}></span>}
              </button>
            ))}
          </div>
        </nav>
      </header>

      <div style={{ maxWidth: 1240, margin: "0 auto", padding: "32px" }}>
        {/* Hero */}
        <section style={{ display: "grid", gridTemplateColumns: "1.7fr 1fr", gap: 32, marginBottom: 36, paddingBottom: 36, borderBottom: "1px solid var(--rule)" }}>
          <article>
            <Thumb article={hero} w="100%" h={420} label="COVER STORY" />
            <div style={{ marginTop: 18 }}>
              <div style={{ display: "flex", gap: 12, alignItems: "center", marginBottom: 12 }}>
                <span style={{ fontSize: 11, letterSpacing: ".3em", color: "var(--accent-red)", fontWeight: 700 }}>COVER STORY</span>
                <span style={{ width: 30, height: 1, background: "var(--slate-400)" }}></span>
                <CatChip slug={hero.cat} />
              </div>
              <h1 style={{ margin: 0, fontFamily: "var(--serif)", fontSize: 36, fontWeight: 700, lineHeight: 1.35, color: "var(--navy-900)" }}>
                {hero.title}
              </h1>
              <p style={{ fontSize: 15, lineHeight: 1.85, color: "var(--ink-soft)", marginTop: 14 }}>
                {hero.excerpt}
              </p>
              <div style={{ marginTop: 14, display: "flex", gap: 16, fontSize: 12, color: "var(--slate-600)" }}>
                <span>文・{hero.author}</span>
                <span>{hero.date}</span>
                <span>👁 {fmt(hero.views)}</span>
              </div>
            </div>
          </article>
          <aside>
            <div style={{ fontSize: 11, letterSpacing: ".3em", color: "var(--accent-red)", fontWeight: 700, marginBottom: 14 }}>FEATURED</div>
            {subFeatures.map((a, i) => (
              <article key={a.id} style={{ paddingBottom: 18, marginBottom: 18, borderBottom: i < subFeatures.length - 1 ? "1px solid var(--rule)" : "none" }}>
                <a href={`article.html?id=${a.id}`} style={{ textDecoration: "none", color: "inherit" }}>
                <Thumb article={a} w="100%" h={140} label={a.cat.toUpperCase()} />
                <div style={{ marginTop: 12 }}>
                  <CatChip slug={a.cat} />
                  <h3 style={{ margin: "8px 0 6px", fontFamily: "var(--serif)", fontSize: 18, lineHeight: 1.4, color: "var(--navy-900)" }}>
                    {a.title}
                  </h3>
                  <div style={{ fontSize: 11, color: "var(--slate-600)", fontFamily: "var(--mono)" }}>
                    {a.date} · {a.author}
                  </div>
                </div>
                </a>
              </article>
            ))}
            <div style={{ background: "var(--navy-900)", color: "#fff", padding: "20px 18px", marginTop: 8 }}>
              <div style={{ fontSize: 11, letterSpacing: ".25em", marginBottom: 8, opacity: .7 }}>NEWSLETTER</div>
              <h4 style={{ margin: 0, fontFamily: "var(--serif)", fontSize: 17, fontWeight: 700, lineHeight: 1.4 }}>
                毎週月曜、編集部による週報をお届け
              </h4>
              <input placeholder="メールアドレス" style={{
                marginTop: 12, width: "100%", padding: "9px 12px", fontSize: 12,
                border: "none", background: "rgba(255,255,255,.1)", color: "#fff",
              }} />
              <button style={{ marginTop: 8, width: "100%", padding: "9px", background: "var(--accent-gold)", color: "var(--navy-900)", border: "none", fontWeight: 700, fontSize: 12, letterSpacing: ".1em", cursor: "pointer" }}>
                登録する
              </button>
            </div>
          </aside>
        </section>

        {/* Section heading */}
        <div style={{ display: "flex", alignItems: "baseline", gap: 16, marginBottom: 24 }}>
          <h2 style={{ margin: 0, fontFamily: "var(--serif)", fontSize: 26, fontWeight: 700, color: "var(--navy-900)" }}>
            {activeCategory === "all" ? "LATEST" : data.categories.find((c) => c.slug === activeCategory)?.name}
          </h2>
          <div style={{ flex: 1, height: 1, background: "var(--slate-300)" }}></div>
          <span style={{ fontSize: 12, color: "var(--slate-600)", letterSpacing: ".15em", fontFamily: "var(--mono)" }}>
            {grid.length} ARTICLES
          </span>
        </div>

        {/* 4-column grid */}
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 24, marginBottom: 36 }}>
          {grid.map((a) => (
            <a key={a.id} href={`article.html?id=${a.id}`} style={{ textDecoration: "none", color: "inherit" }}>
            <article style={{ cursor: "pointer" }}>
              <Thumb article={a} w="100%" h={160} label={a.cat.toUpperCase()} />
              <div style={{ paddingTop: 12 }}>
                <CatChip slug={a.cat} />
                <h3 style={{
                  margin: "8px 0 8px", fontFamily: "var(--serif)", fontSize: 15.5, fontWeight: 700,
                  lineHeight: 1.45, color: "var(--navy-900)",
                }}>{a.title}</h3>
                <div style={{ fontSize: 11, color: "var(--slate-600)", fontFamily: "var(--mono)", display: "flex", justifyContent: "space-between" }}>
                  <span>{a.date}</span>
                  <span>👁 {fmt(a.views)}</span>
                </div>
              </div>
            </article>
            </a>
          ))}
        </div>

        {/* Two-up callout: ranking + opinion */}
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 32, paddingTop: 24, borderTop: "1px solid var(--rule)" }}>
          <section>
            <h3 style={{ margin: "0 0 14px", fontFamily: "var(--serif)", fontSize: 20, fontWeight: 700, color: "var(--navy-900)" }}>
              週間ランキング
            </h3>
            {data.ranking.slice(0, 5).map((r) => {
              const a = data.articles.find((x) => x.id === r.id);
              return (
                <div key={r.rank} style={{ display: "grid", gridTemplateColumns: "40px 1fr auto", gap: 14, padding: "12px 0", borderTop: "1px solid var(--rule)", alignItems: "center" }}>
                  <span style={{ fontFamily: "var(--serif)", fontSize: 28, fontWeight: 700, color: r.rank <= 3 ? "var(--accent-red)" : "var(--slate-400)", lineHeight: 1 }}>
                    {String(r.rank).padStart(2, "0")}
                  </span>
                  <div>
                    <div style={{ fontSize: 14, fontWeight: 600, lineHeight: 1.45, color: "var(--navy-900)" }}>{a.title}</div>
                    <div style={{ fontSize: 11, color: "var(--slate-600)", marginTop: 4, fontFamily: "var(--mono)" }}>{a.date}</div>
                  </div>
                  <span style={{ fontSize: 11, color: "var(--slate-600)", fontFamily: "var(--mono)" }}>👁{fmt(a.views)}</span>
                </div>
              );
            })}
          </section>
          <section style={{ background: "var(--navy-50)", padding: 24 }}>
            <div style={{ fontSize: 11, letterSpacing: ".3em", color: "var(--accent-red)", fontWeight: 700, marginBottom: 10 }}>OPINION</div>
            <h3 style={{ margin: "0 0 12px", fontFamily: "var(--serif)", fontSize: 22, fontWeight: 700, color: "var(--navy-900)", lineHeight: 1.4 }}>
              GCAPの意義は「機体」ではなく「枠組み」にある
            </h3>
            <p style={{ fontSize: 13.5, lineHeight: 1.8, color: "var(--ink-soft)" }}>
              次期戦闘機GCAPを単なるプラットフォーム更新と見るのは誤りである。
              日英伊の三国共同という枠組みそのものが、戦後日本の防衛装備調達における…
            </p>
            <div style={{ display: "flex", gap: 10, alignItems: "center", marginTop: 14 }}>
              <div style={{ width: 36, height: 36, borderRadius: 18, background: "var(--navy-700)", color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--serif)", fontWeight: 700 }}>山</div>
              <div>
                <div style={{ fontSize: 13, fontWeight: 700 }}>山岡 健一</div>
                <div style={{ fontSize: 11, color: "var(--slate-600)" }}>主筆</div>
              </div>
            </div>
          </section>
        </div>
      </div>

      <ClassicFooter />
    </div>
  );
}

Object.assign(window, { VariationB });
