// Variation C — Zine. Full-bleed scenes, immersive horizontal panels stacked
// vertically. Minimal chrome, tiny serif captions, big visual rhythm.

function VariationZine({ accent }) {
  const [view, setView] = React.useState({ page: 'index', id: null });
  const [query, setQuery] = React.useState('');
  const [progress, setProgress] = React.useState(0);
  const [menuOpen, setMenuOpen] = React.useState(false);
  const scrollRef = React.useRef(null);
  const filtered = ARTICLES.filter(a =>
    !query || a.title.includes(query) || a.region.includes(query) || a.titleEn.toLowerCase().includes(query.toLowerCase())
  );
  const current = ARTICLES.find(a => a.id === view.id);
  const open = (id) => { setView({ page: 'article', id }); setMenuOpen(false); if (scrollRef.current) scrollRef.current.scrollTop = 0; };
  const home = () => { setView({ page: 'index', id: null }); setMenuOpen(false); if (scrollRef.current) scrollRef.current.scrollTop = 0; };

  const onScroll = (e) => {
    const el = e.currentTarget;
    const max = el.scrollHeight - el.clientHeight;
    setProgress(max > 0 ? el.scrollTop / max : 0);
  };

  return (
    <div ref={scrollRef} onScroll={onScroll} style={{
      width: '100%', height: '100%', background: '#171513', color: '#ebe6dc',
      fontFamily: '"Noto Serif JP", "Yu Mincho", serif', overflowY: 'auto',
      position: 'relative', scrollBehavior: 'smooth',
    }}>
      {/* scroll progress */}
      <div style={{
        position: 'fixed', top: 0, left: 0, right: 0, height: 2, zIndex: 20,
        background: 'rgba(255,255,255,0.04)', pointerEvents: 'none',
      }}>
        <div style={{
          height: '100%', width: `${progress * 100}%`,
          background: accent, transition: 'width .12s linear',
        }} />
      </div>
      <header className="zine-header" style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '22px 56px', position: 'fixed', top: 0, left: 0, right: 0,
        zIndex: 10, color: '#fff',
        background: 'linear-gradient(180deg, rgba(10,9,8,0.55) 0%, rgba(10,9,8,0.25) 70%, rgba(10,9,8,0) 100%)',
        backdropFilter: 'blur(8px)', WebkitBackdropFilter: 'blur(8px)',
        textShadow: '0 1px 14px rgba(0,0,0,0.45)',
      }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 14, cursor: 'pointer' }} onClick={home}>
          <div className="zine-header-logo" style={{ fontSize: 22, fontWeight: 400, letterSpacing: '0.06em' }}>バイク日和</div>
          <div className="zine-header-vol" style={{ fontFamily: 'Inter, system-ui, sans-serif', fontSize: 12, letterSpacing: '0.28em', textTransform: 'uppercase', opacity: 0.8 }}>Zine · Vol. 04</div>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 24 }}>
          <ZineSearch value={query} onChange={setQuery} />
          <div className="zine-header-count" style={{ fontFamily: 'Inter, system-ui, sans-serif', fontSize: 14, letterSpacing: '0.22em', textTransform: 'uppercase', opacity: 0.92 }}>
            {String(filtered.length).padStart(2, '0')} / {String(ARTICLES.length).padStart(2, '0')}
          </div>
          <button
            onClick={(e) => { e.stopPropagation(); setMenuOpen(v => !v); }}
            aria-label="目次を開く"
            style={{
              background: 'transparent', border: 0, cursor: 'pointer', padding: 4,
              color: '#fff', display: 'flex', flexDirection: 'column', gap: 5,
              width: 28, alignItems: 'stretch',
            }}
          >
            <span style={{ height: 1, background: '#fff', transform: menuOpen ? 'translateY(6px) rotate(45deg)' : 'none', transition: 'transform .25s' }} />
            <span style={{ height: 1, background: '#fff', opacity: menuOpen ? 0 : 1, transition: 'opacity .15s' }} />
            <span style={{ height: 1, background: '#fff', transform: menuOpen ? 'translateY(-6px) rotate(-45deg)' : 'none', transition: 'transform .25s' }} />
          </button>
        </div>
      </header>

      <IndexDrawer
        open={menuOpen}
        onClose={() => setMenuOpen(false)}
        articles={ARTICLES}
        currentId={view.id}
        onPick={open}
        onHome={home}
        accent={accent}
      />

      {view.page === 'index' ? (
        <>
          {/* opening spread */}
          <section style={{
            height: 600, display: 'flex', alignItems: 'center',
            padding: '0 clamp(20px,7vw,56px)', position: 'relative',
          }}>
            <div style={{ position: 'absolute', inset: 0, background: 'radial-gradient(ellipse at 70% 40%, #2a221c 0%, #171513 70%)' }} />
            {/* 左：メインコピー */}
            <div style={{ position: 'relative', flex: '0 0 auto', maxWidth: '55%' }}>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif', fontSize: 11,
                letterSpacing: '0.32em', color: accent, textTransform: 'uppercase',
                marginBottom: 26,
              }}>— Spring 2026 · A Single Journey</div>
              <h1 style={{
                fontSize: 'clamp(44px, 10vw, 120px)', lineHeight: 1.02, margin: 0, fontWeight: 300,
                letterSpacing: '0.02em', textWrap: 'balance',
              }}>
                風と、<br/>路面と、<br/><span style={{ fontStyle: 'italic', color: accent }}>静けさ</span>。
              </h1>
              <div style={{
                marginTop: 40, fontFamily: 'Inter, system-ui, sans-serif',
                fontSize: 13, letterSpacing: '0.04em', opacity: 0.65,
                maxWidth: '32em', lineHeight: 1.7, fontStyle: 'italic',
              }}>
                A motorcycle journal. Routes, photographed only with words. Scroll to begin.
              </div>
            </div>
            {/* 右：巻号・記事数 */}
            <div style={{
              position: 'relative', flex: 1, display: 'flex', flexDirection: 'column',
              alignItems: 'flex-end', justifyContent: 'center',
              gap: 32, paddingRight: 'clamp(8px,3vw,24px)',
            }}>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif', textAlign: 'right',
                borderRight: `1px solid ${accent}`, paddingRight: 20,
              }}>
                <div style={{ fontSize: 10, letterSpacing: '0.4em', color: accent, textTransform: 'uppercase', marginBottom: 10 }}>Vol.</div>
                <div style={{ fontSize: 'clamp(48px,7vw,88px)', fontWeight: 300, lineHeight: 1, color: 'rgba(235,230,220,0.12)', letterSpacing: '-0.02em' }}>04</div>
              </div>
              <div style={{
                fontFamily: 'Inter, system-ui, sans-serif', textAlign: 'right',
                fontSize: 10, letterSpacing: '0.32em', textTransform: 'uppercase',
                color: 'rgba(235,230,220,0.35)', lineHeight: 2,
              }}>
                <div>{ARTICLES.length} Routes</div>
                <div>京都 · Kansai</div>
                <div style={{ color: accent }}>2026</div>
              </div>
            </div>
          </section>

          {/* article panels */}
          {filtered.map((a, i) => (
            <article key={a.id} onClick={() => open(a.id)} className="zine-panel" style={{
              cursor: 'pointer', position: 'relative', height: 560,
              overflow: 'hidden',
            }}>
              <Backdrop article={a} style={{ position: 'absolute', inset: 0, transition: 'transform .8s cubic-bezier(.2,.7,.3,1)' }} />
              {/* ベース暗幕（明るい写真でも文字が読めるよう底上げ） */}
              <div style={{ position: 'absolute', inset: 0, background: 'rgba(10,9,8,0.22)' }} />
              <div className="zine-veil" style={{
                position: 'absolute', inset: 0, transition: 'opacity .4s',
                background: i % 2
                  ? 'linear-gradient(90deg, rgba(23,21,19,0.92) 0%, rgba(23,21,19,0.55) 42%, rgba(23,21,19,0.15) 65%, transparent 80%)'
                  : 'linear-gradient(270deg, rgba(23,21,19,0.92) 0%, rgba(23,21,19,0.55) 42%, rgba(23,21,19,0.15) 65%, transparent 80%)',
              }} />
              {/* 左上ラベル */}
              <div style={{
                position: 'absolute', top: 32, left: 40,
                fontFamily: 'Inter, system-ui, sans-serif', fontSize: 12,
                letterSpacing: '0.3em', textTransform: 'uppercase',
                color: '#fff',
                textShadow: '0 1px 8px rgba(0,0,0,0.8)',
                background: 'rgba(0,0,0,0.38)',
                backdropFilter: 'blur(6px)',
                WebkitBackdropFilter: 'blur(6px)',
                padding: '6px 14px', borderRadius: 4,
                border: '0.5px solid rgba(255,255,255,0.18)',
              }}>
                <span style={{ color: accent, marginRight: 8 }}>{String(i + 1).padStart(2, '0')}</span>
                {a.region}
              </div>
              <div className="zine-panel-content" style={{
                position: 'absolute', inset: 0,
                display: 'flex', alignItems: 'center',
                padding: '0 80px',
                justifyContent: i % 2 ? 'flex-start' : 'flex-end',
              }}>
                <div style={{ maxWidth: 480, color: '#ebe6dc' }}>
                  <h2 style={{ fontSize: 'clamp(28px,4.5vw,56px)', lineHeight: 1.14, margin: '0 0 16px', fontWeight: 400, letterSpacing: '0.01em', textWrap: 'pretty' }}>
                    {a.title}
                  </h2>
                  <div style={{ fontFamily: 'Inter, system-ui, sans-serif', fontStyle: 'italic', fontSize: 13, opacity: 0.65, marginBottom: 22 }}>{a.titleEn}</div>
                  <p style={{ fontSize: 15, lineHeight: 1.85, margin: 0, opacity: 0.85 }}>{a.excerpt}</p>
                  <div style={{
                    marginTop: 28, display: 'flex', gap: 22,
                    fontFamily: 'Inter, system-ui, sans-serif', fontSize: 13,
                    letterSpacing: '0.22em', textTransform: 'uppercase', opacity: 0.78,
                  }}>
                    <span>{a.distance}</span>
                    <span>{a.duration}</span>
                    <span>{a.date}</span>
                  </div>
                </div>
              </div>
            </article>
          ))}

          <footer style={{
            padding: '60px 56px', background: '#0e0d0c',
            fontFamily: 'Inter, system-ui, sans-serif', fontSize: 11,
            letterSpacing: '0.22em', color: '#6b6457', textTransform: 'uppercase',
            display: 'flex', justifyContent: 'space-between',
          }}>
            <span>バイク日和 · Zine</span>
            <span style={{ color: accent, fontStyle: 'italic' }}>また、走りに行きます。</span>
          </footer>
        </>
      ) : current && (
        <ArticleZine article={current} onBack={home} accent={accent} />
      )}
    </div>
  );
}

function ZineSearch({ value, onChange }) {
  return (
    <input
      value={value}
      onChange={(e) => onChange(e.target.value)}
      placeholder="探す / Search"
      className="zine-header-search"
      style={{
        border: 0, borderBottom: '0.5px solid rgba(255,255,255,0.4)',
        background: 'transparent', outline: 0, color: '#fff',
        fontFamily: '"Noto Serif JP", serif', fontSize: 13,
        width: 140, padding: '4px 0',
      }}
    />
  );
}

function ArticleZine({ article, onBack, accent }) {
  return (
    <>
      <section style={{ position: 'relative', height: 760 }}>
        <Backdrop article={article} style={{ position: 'absolute', inset: 0 }} />
        {/* top scrim — protects the title against bright skies */}
        <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(180deg, rgba(10,9,8,0.65) 0%, rgba(10,9,8,0.25) 30%, transparent 55%, transparent 70%, rgba(23,21,19,0.85) 100%)' }} />
        {/* title block — anchored to the TOP, sitting in the sky area */}
        <div style={{ position: 'absolute', left: 56, right: 56, top: 110 }}>
          <button onClick={onBack} style={{
            background: 'transparent', border: 0, cursor: 'pointer',
            fontFamily: 'Inter, system-ui, sans-serif', fontSize: 14, letterSpacing: '0.22em',
            color: accent, textTransform: 'uppercase', padding: 0, marginBottom: 22,
            textShadow: '0 1px 12px rgba(0,0,0,0.5)',
          }}>← Zine</button>
          <div style={{ fontFamily: 'Inter, system-ui, sans-serif', fontSize: 15, letterSpacing: '0.28em', color: '#fff', opacity: 0.95, textTransform: 'uppercase', marginBottom: 22, textShadow: '0 1px 12px rgba(0,0,0,0.5)' }}>
            {article.issue} · {article.region} · {article.date}
          </div>
          <h1 style={{
            fontSize: 92, lineHeight: 1.08, margin: 0, fontWeight: 500,
            letterSpacing: '0.01em', color: '#fff', textWrap: 'pretty', maxWidth: '14em',
            textShadow: '0 2px 30px rgba(0,0,0,0.55), 0 1px 2px rgba(0,0,0,0.4)',
          }}>
            {article.title}
          </h1>
          <div style={{ marginTop: 18, fontFamily: 'Inter, system-ui, sans-serif', fontStyle: 'italic', fontSize: 17, color: '#fff', opacity: 0.85, textShadow: '0 1px 12px rgba(0,0,0,0.5)' }}>{article.titleEn}</div>
        </div>
      </section>
      <section style={{
        maxWidth: 680, margin: '0 auto', padding: '80px 40px 100px',
        color: '#ebe6dc',
      }}>
        <div style={{
          display: 'flex', justifyContent: 'space-between', gap: 24,
          padding: '0 0 28px', borderBottom: `0.5px solid rgba(235,230,220,0.18)`,
          marginBottom: 48, fontFamily: 'Inter, system-ui, sans-serif',
        }}>
          {[
            ['距離', article.distance], ['標高', article.elevation],
            ['所要', article.duration], ['季節', article.season],
          ].map(([k, v]) => (
            <div key={k}>
              <div style={{ fontSize: 12, letterSpacing: '0.22em', color: accent, textTransform: 'uppercase', marginBottom: 8 }}>{k}</div>
              <div style={{ fontSize: 22, color: '#ebe6dc', letterSpacing: '0.02em' }}>{v}</div>
            </div>
          ))}
        </div>
        {article.body.map((p, i) => (
          <p key={i} style={{
            fontSize: 18, lineHeight: 2.05, margin: '0 0 1.6em',
            color: i === 0 ? '#fff' : 'rgba(235,230,220,0.86)',
            textIndent: i === 0 ? 0 : '1em',
            ...(i === 0 ? { fontSize: 22, lineHeight: 1.9, fontStyle: 'normal' } : {}),
            textWrap: 'pretty',
          }}>{p}</p>
        ))}
        <div style={{ textAlign: 'center', marginTop: 64, fontFamily: 'Inter, system-ui, sans-serif', fontSize: 10, letterSpacing: '0.5em', color: accent }}>※ ※ ※</div>
      </section>
    </>
  );
}

window.VariationZine = VariationZine;

function Backdrop({ article, style, children }) {
  if (article.image) {
    return (
      <div style={{ ...style, overflow: 'hidden', background: '#0e0d0c' }}>
        <img src={article.image} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
        {children}
      </div>
    );
  }
  return <Scene preset={article.scene} style={style}>{children}</Scene>;
}

function IndexDrawer({ open, onClose, articles, currentId, onPick, onHome, accent }) {
  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => { if (e.key === 'Escape') onClose(); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [open, onClose]);

  return (
    <>
      {/* scrim */}
      <div
        onClick={onClose}
        style={{
          position: 'fixed', inset: 0, zIndex: 15,
          background: 'rgba(10,9,8,0.6)',
          backdropFilter: 'blur(6px)',
          opacity: open ? 1 : 0, pointerEvents: open ? 'auto' : 'none',
          transition: 'opacity .3s',
        }}
      />
      {/* panel */}
      <aside style={{
        position: 'fixed', top: 0, right: 0, bottom: 0, width: 520, maxWidth: '92vw',
        zIndex: 16, background: '#1a1815', color: '#ebe6dc',
        borderLeft: `0.5px solid ${accent}`,
        transform: open ? 'translateX(0)' : 'translateX(100%)',
        transition: 'transform .42s cubic-bezier(.2,.7,.2,1)',
        display: 'flex', flexDirection: 'column',
        fontFamily: '"Noto Serif JP", "Yu Mincho", serif',
      }}>
        <div style={{
          padding: '32px 48px 20px', display: 'flex', alignItems: 'baseline',
          justifyContent: 'space-between', borderBottom: '0.5px solid rgba(235,230,220,0.12)',
        }}>
          <div>
            <div style={{
              fontFamily: 'Inter, system-ui, sans-serif', fontSize: 10,
              letterSpacing: '0.32em', color: accent, textTransform: 'uppercase',
              marginBottom: 6,
            }}>Index · 目次</div>
            <div style={{ fontSize: 22, fontWeight: 400, letterSpacing: '0.04em' }}>すべての記事</div>
          </div>
          <button onClick={onClose} aria-label="閉じる" style={{
            background: 'transparent', border: 0, color: '#ebe6dc', cursor: 'pointer',
            fontSize: 22, padding: 4, opacity: 0.7,
          }}>×</button>
        </div>

        <div style={{ overflowY: 'auto', flex: 1, padding: '12px 0 32px' }}>
          <button onClick={onHome} style={{
            display: 'block', width: '100%', textAlign: 'left',
            background: 'transparent', border: 0, cursor: 'pointer',
            padding: '14px 48px', color: '#ebe6dc', opacity: 0.7,
            fontFamily: 'Inter, system-ui, sans-serif', fontSize: 10,
            letterSpacing: '0.28em', textTransform: 'uppercase',
          }}>← 表紙へ戻る</button>
          {articles.map((a, i) => {
            const isCurrent = a.id === currentId;
            return (
              <button
                key={a.id}
                onClick={() => onPick(a.id)}
                className="zine-idx-item"
                style={{
                  display: 'grid', gridTemplateColumns: '54px 1fr auto',
                  alignItems: 'baseline', gap: 18, width: '100%', textAlign: 'left',
                  background: 'transparent', border: 0, cursor: 'pointer',
                  padding: '20px 48px', color: '#ebe6dc',
                  borderBottom: '0.5px solid rgba(235,230,220,0.08)',
                  fontFamily: 'inherit',
                  position: 'relative',
                }}
              >
                <span style={{
                  fontFamily: 'Inter, system-ui, sans-serif', fontSize: 11,
                  letterSpacing: '0.18em', color: isCurrent ? accent : 'rgba(235,230,220,0.45)',
                  fontVariantNumeric: 'tabular-nums',
                }}>{String(i + 1).padStart(2, '0')}</span>
                <span>
                  <span style={{
                    display: 'block', fontSize: 22, lineHeight: 1.35,
                    color: isCurrent ? accent : '#ebe6dc', fontWeight: 400,
                    textWrap: 'pretty',
                  }}>{a.title}</span>
                  <span style={{
                    display: 'block', marginTop: 6,
                    fontFamily: 'Inter, system-ui, sans-serif', fontStyle: 'italic',
                    fontSize: 11, color: 'rgba(235,230,220,0.5)',
                  }}>{a.titleEn}</span>
                </span>
                <span style={{
                  fontFamily: 'Inter, system-ui, sans-serif', fontSize: 10,
                  letterSpacing: '0.2em', color: 'rgba(235,230,220,0.45)',
                  textTransform: 'uppercase', textAlign: 'right', lineHeight: 1.6,
                }}>
                  <span style={{ display: 'block' }}>{a.region}</span>
                  <span style={{ display: 'block' }}>{a.distance}</span>
                </span>
              </button>
            );
          })}
        </div>

        <div style={{
          padding: '20px 48px', borderTop: '0.5px solid rgba(235,230,220,0.12)',
          fontFamily: 'Inter, system-ui, sans-serif', fontSize: 10,
          letterSpacing: '0.24em', color: 'rgba(235,230,220,0.45)',
          textTransform: 'uppercase', display: 'flex', justifyContent: 'space-between',
        }}>
          <span>Esc to close</span>
          <span>{articles.length} entries</span>
        </div>
      </aside>
    </>
  );
}
