const { useState, useEffect, useRef } = window.React;

/* ═══════════════════════════════════════════════════════════════════════════
   Shared site bits — nav, footer, logo, animated mindmap
   ═══════════════════════════════════════════════════════════════════════════ */

const LogoGlyph = ({ size = 28 }) => (
  <svg viewBox="-20 -30 180 180" width={size} height={size}>
    <path d="M 70 12 A 58 58 0 1 0 128 70" fill="none" stroke="currentColor" strokeWidth="4" strokeLinecap="round"/>
    <g stroke="currentColor" strokeLinecap="round">
      <g strokeWidth="3.8"><line x1="70" y1="117" x2="70" y2="124"/><line x1="16" y1="70" x2="23" y2="70"/></g>
      <g strokeWidth="2" opacity="0.7">
        <line x1="116.75" y1="97" x2="112.5" y2="95"/>
        <line x1="97" y1="116.75" x2="95" y2="112.5"/>
        <line x1="43" y1="116.75" x2="45" y2="112.5"/>
        <line x1="23.25" y1="97" x2="27.5" y2="95"/>
        <line x1="23.25" y1="43" x2="27.5" y2="45"/>
        <line x1="43" y1="23.25" x2="45" y2="27.5"/>
      </g>
    </g>
    <g fill="none" stroke="var(--accent)" strokeWidth="3" strokeLinecap="round">
      <path d="M70 70 Q 94 76 104 88"/><path d="M70 70 Q 52 92 44 104"/><path d="M70 70 Q 50 68 36 64"/>
    </g>
    <g fill="var(--accent)">
      <circle cx="104" cy="88" r="5"/><circle cx="44" cy="104" r="5"/><circle cx="36" cy="64" r="5"/>
    </g>
    <path d="M70 70 Q 92 44 115 6" fill="none" stroke="var(--accent)" strokeWidth="3.6" strokeLinecap="round"/>
    <circle cx="115" cy="6" r="7" fill="var(--accent)"/>
    <circle cx="70" cy="70" r="6" fill="currentColor"/>
  </svg>
);

const Nav = ({ lang, setLang }) => (
  <nav style={{
    position: "sticky", top: 12, zIndex: 50,
    maxWidth: 860, margin: "0 auto",
    padding: "0 8px",
    background: "rgba(255,255,255,0.35)",
    backdropFilter: "blur(24px) saturate(180%)",
    WebkitBackdropFilter: "blur(24px) saturate(180%)",
    borderRadius: 999,
    border: "1px solid rgba(255,255,255,0.45)",
    borderTop: "1px solid rgba(255,255,255,0.6)",
    boxShadow: "0 4px 24px rgba(26,24,21,0.06), 0 1px 4px rgba(26,24,21,0.04), inset 0 1px 0 rgba(255,255,255,0.5)",
  }}>
    <div style={{
      display: "flex", alignItems: "center", justifyContent: "space-between", height: 52,
      padding: "0 16px",
    }}>
      <a href="#" style={{ display: "flex", alignItems: "center", gap: 10, color: "var(--ink)", textDecoration: "none" }}>
        <LogoGlyph size={26}/>
        <span style={{ fontFamily: "'IBM Plex Sans', var(--font-sans)", fontWeight: 600, fontSize: 15, letterSpacing: "-0.02em" }}>LL<span style={{ color: "#C9583C" }}>Mind</span>Map</span>
      </a>
      <div style={{ display: "flex", alignItems: "center", gap: 28 }}>
        {[{ href: "#how", j: "仕組み", e: "How" },
          { href: "#features", j: "機能", e: "Features" },
          { href: "#pricing", j: "料金", e: "Pricing" }].map(n => (
          <a key={n.e} href={n.href} onClick={e => {
            e.preventDefault();
            const el = document.querySelector(n.href);
            if (el) {
              const y = el.getBoundingClientRect().top + window.scrollY - 80;
              window.scrollTo({ top: y, behavior: "smooth" });
            }
          }} style={{
            font: "500 12.5px/1 var(--font-sans)", color: "var(--ink-60)",
            textDecoration: "none", letterSpacing: "-0.005em", cursor: "pointer",
          }}>{lang === "ja" ? n.j : n.e}</a>
        ))}
        <div className="lang">
          <button aria-pressed={lang === "ja"} onClick={() => { if (lang !== "ja") window.location.assign("/ja/"); }}>JA</button>
          <button aria-pressed={lang === "en"} onClick={() => { if (lang !== "en") window.location.assign("/"); }}>EN</button>
        </div>
        <a className="btn btn-accent btn-sm" href="https://chromewebstore.google.com/detail/llmindmap/jlaffgbaoefnmgfkagopepnljgpcfepl" target="_blank" rel="noopener">
          {lang === "ja" ? "地図にする" : "Start mapping"}
        </a>
      </div>
    </div>
  </nav>
);

const Footer = ({ lang }) => (
  <footer style={{ borderTop: "1px solid var(--rule)", padding: "48px 0 32px", marginTop: 80 }}>
    <div className="container" style={{ display: "grid", gridTemplateColumns: "1.3fr 1fr 1fr 1fr", gap: 32 }}>
      <div>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <LogoGlyph size={22}/>
          <span style={{ fontFamily: "'IBM Plex Sans', var(--font-sans)", fontWeight: 600, fontSize: 13, letterSpacing: "-0.01em" }}>LL<span style={{ color: "#C9583C" }}>Mind</span>Map</span>
        </div>
        <div className="small" style={{ marginTop: 12, maxWidth: 280 }}>
          {lang === "ja"
            ? "あらゆるLLMチャットを、思考の地図に。"
            : "Turn any LLM conversation into a map of your thinking."}
        </div>
        <div className="mono" style={{ marginTop: 20, color: "var(--ink-40)" }}>v2.4.1 · built 2026</div>
      </div>
      {[
        { h: lang === "ja" ? "製品" : "Product", l: [
          { label: "Chrome Extension", href: "#" },
          { label: lang === "ja" ? "料金" : "Pricing", href: "/#pricing" },
        ]},
        { h: lang === "ja" ? "法務" : "Legal", l: [
          { label: lang === "ja" ? "プライバシーポリシー" : "Privacy Policy", href: "/privacy.html" },
          { label: lang === "ja" ? "特定商取引法に基づく表記" : "Commercial Transactions Act", href: "/tokusho.html" },
        ]},
        { h: lang === "ja" ? "お問い合わせ" : "Contact", l: [
          { label: "info@llmindmap.com", href: "mailto:info@llmindmap.com" },
        ]},
      ].map(col => (
        <div key={col.h}>
          <div className="mono-hi" style={{ marginBottom: 16 }}>{col.h}</div>
          {col.l.map(link => (
            <a key={link.label} href={link.href} style={{
              display: "block", padding: "6px 0",
              font: "400 13px/1.4 var(--font-sans)", color: "var(--ink-60)",
              textDecoration: "none",
            }}>{link.label}</a>
          ))}
        </div>
      ))}
    </div>
    <div className="container" style={{
      marginTop: 36, paddingTop: 20, borderTop: "1px solid var(--rule)",
      display: "flex", justifyContent: "space-between", alignItems: "center",
    }}>
      <div className="mono" style={{ color: "var(--ink-40)" }}>© 2026 LLMindMap</div>
      <div className="mono" style={{ color: "var(--ink-40)" }}>TOKYO · SAPPORO</div>
    </div>
  </footer>
);

/* ─── Animated mindmap for hero B ───────────────────────────────────────── */
const HeroMindmap = ({ width = 680, height = 520 }) => {
  const CATS = { issue: "#C9583C", question: "#B8864A", idea: "#8F6FA3", decision: "#536A8A", info: "#7A9A85", aside: "#8A7D86" };
  const cx = width / 2, cy = height / 2;
  const kids = [
    { k: "idea", a: -Math.PI/2, l: "Semantic search", g: [{ k: "question", l: "Embeddings drift" }, { k: "decision", l: "Cache layer" }] },
    { k: "issue", a: -Math.PI/2 + (2*Math.PI/5), l: "Retrieval latency", g: [{ k: "info", l: "Cold starts" }] },
    { k: "decision", a: -Math.PI/2 + (4*Math.PI/5), l: "Eval harness", g: [{ k: "idea", l: "Golden set" }, { k: "aside", l: "Regression" }] },
    { k: "info", a: -Math.PI/2 + (6*Math.PI/5), l: "Token pricing" },
    { k: "question", a: -Math.PI/2 + (8*Math.PI/5), l: "Why 8k ceiling?" },
  ];
  const R1 = 150, R2 = 230;
  const n = kids.map(k => ({ ...k, x: cx + R1*Math.cos(k.a), y: cy + R1*Math.sin(k.a) }));
  return (
    <svg viewBox={`0 0 ${width} ${height}`} width="100%" style={{ display: "block", maxHeight: "70vh" }}>
      <defs>
        <filter id="g1"><feGaussianBlur stdDeviation="6"/></filter>
      </defs>
      {n.map((k, i) => (
        <g key={i}>
          <line x1={cx} y1={cy} x2={k.x} y2={k.y} stroke="var(--ink-20)" strokeWidth="1.2">
            <animate attributeName="stroke-dashoffset" from="200" to="0" dur="0.8s" begin={`${i * 0.15}s`} fill="freeze"/>
            <animate attributeName="stroke-dasharray" from="0 200" to="200 0" dur="0.8s" begin={`${i * 0.15}s`} fill="freeze"/>
          </line>
          {(k.g || []).map((gk, j) => {
            const spread = 0.5;
            const gang = k.a + ((j - ((k.g.length - 1)/2)) * spread / Math.max(1, k.g.length));
            const gx = cx + R2 * Math.cos(gang);
            const gy = cy + R2 * Math.sin(gang);
            return (
              <g key={j}>
                <line x1={k.x} y1={k.y} x2={gx} y2={gy} stroke="var(--ink-20)" strokeWidth="0.9" opacity="0">
                  <animate attributeName="opacity" from="0" to="1" dur="0.4s" begin={`${0.8 + i * 0.15 + j * 0.1}s`} fill="freeze"/>
                </line>
                <circle cx={gx} cy={gy} r="4" fill={CATS[gk.k]} opacity="0">
                  <animate attributeName="opacity" from="0" to="1" dur="0.3s" begin={`${0.8 + i * 0.15 + j * 0.1}s`} fill="freeze"/>
                </circle>
                <text x={gx} y={gy - 12} textAnchor="middle"
                  fontFamily="var(--font-mono)" fontSize="10" fill="var(--ink-60)"
                  letterSpacing="0.02em" opacity="0">
                  {gk.l}
                  <animate attributeName="opacity" from="0" to="1" dur="0.3s" begin={`${1.0 + i * 0.15 + j * 0.1}s`} fill="freeze"/>
                </text>
              </g>
            );
          })}
          {k.k === "question" && (
            <circle cx={k.x} cy={k.y} r="10" fill="none" stroke={CATS.question}
              strokeWidth="1" strokeDasharray="2 2" opacity="0.9"/>
          )}
          <circle cx={k.x} cy={k.y} r="6" fill={CATS[k.k]}>
            <animate attributeName="r" from="0" to="6" dur="0.4s" begin={`${0.4 + i * 0.15}s`} fill="freeze"/>
          </circle>
          <text x={k.x} y={k.y - 14} textAnchor="middle"
            fontFamily="var(--font-mono)" fontSize="11.5" fill="var(--ink)"
            fontWeight="500" letterSpacing="0.01em" opacity="0">
            {k.l}
            <animate attributeName="opacity" from="0" to="1" dur="0.4s" begin={`${0.6 + i * 0.15}s`} fill="freeze"/>
          </text>
        </g>
      ))}
      <circle cx={cx} cy={cy} r="9" fill="var(--ink)"/>
      <circle cx={cx} cy={cy} r="3" fill="var(--bg)"/>
      <text x={cx} y={cy - 20} textAnchor="middle"
        fontFamily="var(--font-serif)" fontSize="14" fill="var(--ink)" fontStyle="italic">
        Context windows
      </text>
    </svg>
  );
};

Object.assign(window, { LogoGlyph, Nav, Footer, HeroMindmap });
