/* sppoX LP — Tweaks panel (controls applied to <html> data-* + game config) */
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "theme": "neon",
  "font": "clean",
  "hero": "photo",
  "pricing": "b",
  "ai": "chat",
  "why": "story",
  "flow": "three",
  "fes": "hype",
  "catch": "この夏 \"アソビ\" つくせ！",
  "ctaLabel": "今すぐフェスを体験する"
}/*EDITMODE-END*/;

function SppoxTweaks() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const root = document.documentElement;

  React.useEffect(() => { root.setAttribute('data-theme', t.theme); }, [t.theme]);
  React.useEffect(() => { root.setAttribute('data-font', t.font); }, [t.font]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-hero', t.hero); }, [t.hero]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-pricing', t.pricing); }, [t.pricing]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-ai', t.ai); }, [t.ai]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-why', t.why); }, [t.why]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-flow', t.flow); }, [t.flow]);
  React.useEffect(() => { document.getElementById('lp').setAttribute('data-fes', t.fes); }, [t.fes]);
  React.useEffect(() => {
    const el = document.getElementById('hero-catch');
    if (!el) return;
    const raw = (t.catch || '').trim();
    // 引用符（" " 「」 “ ”）で囲んだ部分を強調
    const esc = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    let html = esc(raw).replace(/("[^"]+"|“[^”]+”|「[^」]+」)/g,
      (m) => '<span class="catch-hl">' + m + '</span>');
    el.innerHTML = html;
  }, [t.catch]);
  React.useEffect(() => {
    document.querySelectorAll('[data-cta="hero"], [data-cta="sticky"]').forEach((b) => {
      const arrow = b.querySelector('.arrow');
      b.textContent = t.ctaLabel + ' ';
      if (arrow) b.appendChild(arrow); else { const s = document.createElement('span'); s.className = 'arrow'; s.textContent = '▶'; b.appendChild(s); }
    });
  }, [t.ctaLabel]);

  return (
    <TweaksPanel>
      <TweakSection label="カラーテーマ" />
      <TweakColor
        label="テーマ"
        value={
          t.theme === 'neon' ? '#1ec8de' :
          t.theme === 'sunset' ? '#ff5aa0' :
          t.theme === 'electric' ? '#5b7bff' : '#0fb6d4'
        }
        options={['#1ec8de', '#ff5aa0', '#5b7bff', '#0fb6d4']}
        onChange={(v) => {
          const map = { '#1ec8de': 'neon', '#ff5aa0': 'sunset', '#5b7bff': 'electric', '#0fb6d4': 'arena' };
          setTweak('theme', map[v] || 'neon');
        }}
      />
      <TweakSelect
        label="テーマ名"
        value={t.theme}
        options={[
          { value: 'neon', label: 'Neon Court (ネオン)' },
          { value: 'sunset', label: 'Sunset (サンセット)' },
          { value: 'electric', label: 'Electric (パープル)' },
          { value: 'arena', label: 'Arena Light (ライト)' },
        ]}
        onChange={(v) => setTweak('theme', v)}
      />

      <TweakSection label="見出しフォント" />
      <TweakSelect
        label="フォント"
        value={t.font}
        options={[
          { value: 'clean', label: 'クリーン（Zen角ゴシック・推奨）' },
          { value: 'modern', label: 'モダン（M PLUS 1）' },
          { value: 'standard', label: 'スタンダード（Noto Sans）' },
          { value: 'pop', label: 'ぷっくりポップ（Mochiy Pop）' },
          { value: 'reggae', label: 'お祭りインパクト（Reggae One）' },
          { value: 'rocknroll', label: 'わくわくロック（RocknRoll One）' },
          { value: 'yusei', label: 'てがき風（Yusei Magic）' },
          { value: 'pixel', label: 'ゲームドット（DotGothic16）' },
          { value: 'impact', label: 'インパクト（極太・読みにくい）' },
        ]}
        onChange={(v) => setTweak('font', v)}
      />

      <TweakSection label="ヒーロー（ファーストビュー）" />
      <TweakText
        label="キャッチコピー"
        value={t.catch}
        onChange={(v) => setTweak('catch', v)}
      />
      <TweakRadio
        label="レイアウト"
        value={t.hero}
        options={[
          { value: 'photo', label: '写真+HUD' },
          { value: 'rank', label: 'ランキング' },
          { value: 'center', label: 'センター' },
        ]}
        onChange={(v) => setTweak('hero', v)}
      />
      <TweakText
        label="CTA文言"
        value={t.ctaLabel}
        onChange={(v) => setTweak('ctaLabel', v)}
      />

      <TweakSection label="フェス紹介の見せ方" />
      <TweakRadio
        label="デザイン"
        value={t.fes}
        options={[
          { value: 'info', label: '情報型' },
          { value: 'hype', label: 'ポスター型' },
        ]}
        onChange={(v) => setTweak('fes', v)}
      />

      <TweakSection label="「なぜ上達？」の見せ方" />
      <TweakRadio
        label="デザイン"
        value={t.why}
        options={[
          { value: 'loop', label: 'ループ型' },
          { value: 'quest', label: 'クエスト型' },
          { value: 'story', label: 'ストーリー型' },
        ]}
        onChange={(v) => setTweak('why', v)}
      />

      <TweakSection label="AIセクションの見せ方" />
      <TweakRadio
        label="デザイン"
        value={t.ai}
        options={[
          { value: 'flow', label: 'フロー型' },
          { value: 'chat', label: 'チャット型' },
        ]}
        onChange={(v) => setTweak('ai', v)}
      />

      <TweakSection label="利用の流れの見せ方" />
      <TweakRadio
        label="デザイン"
        value={t.flow}
        options={[
          { value: 'rich', label: 'リッチ版' },
          { value: 'simple', label: 'シンプル' },
          { value: 'three', label: '3ステップ' },
        ]}
        onChange={(v) => setTweak('flow', v)}
      />

      <TweakSection label="料金プランの見せ方" />
      <TweakRadio
        label="デザイン"
        value={t.pricing}
        options={[
          { value: 'a', label: 'A・リボン' },
          { value: 'b', label: 'B・時系列' },
          { value: 'c', label: 'C・チケット' },
        ]}
        onChange={(v) => setTweak('pricing', v)}
      />
    </TweaksPanel>
  );
}

(function mount() {
  const host = document.createElement('div');
  host.id = 'tweaks-root';
  document.body.appendChild(host);
  ReactDOM.createRoot(host).render(<SppoxTweaks />);
})();
