// web-shell.jsx — Desktop web frame for ilovejp.
// Goal: maximally preserve the original mobile screen rendering — wrap it in a
// web-style chrome (top nav, side context panels) instead of an iPhone bezel.

const SHELL_WIDTH = 440;   // mobile viewport width preserved (≈ 402+padding)
const SHELL_HEIGHT = 860;

// ─────────────────────────────────────────────────────────────
// Top global header — logo, primary nav, search, theme/user
// ─────────────────────────────────────────────────────────────
const WebHeader = ({ active, onNav, dark, onToggleDark }) => {
  const T = useTheme(dark);
  const items = [
    { id: 'home',    label: '首頁',  jp: 'ホーム' },
    { id: 'learn',   label: '學習',  jp: 'まなぶ' },
    { id: 'teacher', label: '老師',  jp: '先生' },
    { id: 'about',   label: '關於',  jp: 'アバウト' },
  ];
  return (
    <header style={{
      position: 'sticky', top: 0, zIndex: 50,
      borderBottom: `1px solid ${T.rule}`,
      background: dark ? 'rgba(26,23,20,0.92)' : 'rgba(250,246,238,0.92)',
      backdropFilter: 'blur(14px)',
    }}>
      <div style={{
        maxWidth: 1440, margin: '0 auto', padding: '14px 28px',
        display: 'flex', alignItems: 'center', gap: 28,
      }}>
        {/* Logo */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{
            width: 40, height: 40, borderRadius: 10, background: TOKENS.red,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: FONTS.jpHand, color: '#fff', fontWeight: 700, fontSize: 20,
            transform: 'rotate(-3deg)', boxShadow: '0 2px 6px rgba(200,85,61,0.3)',
          }}>井</div>
          <div>
            <div style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, letterSpacing: 1.6 }}>ILOVEJP · WEB</div>
            <div style={{ fontFamily: FONTS.zhHand, fontSize: 15, fontWeight: 700, color: T.ink, letterSpacing: 0.4, whiteSpace: 'nowrap' }}>井上老師の日本語教室</div>
          </div>
        </div>

        {/* Nav */}
        <nav style={{ display: 'flex', gap: 4, marginLeft: 12 }}>
          {items.map(it => {
            const on = active === it.id;
            return (
              <button key={it.id} onClick={() => onNav(it.id)} style={{
                background: on ? (dark ? 'rgba(200,85,61,0.18)' : '#FBEFEA') : 'transparent',
                border: 'none', cursor: 'pointer', whiteSpace: 'nowrap', flexShrink: 0,
                padding: '8px 14px', borderRadius: 999,
                display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 1,
              }}>
                <span style={{ fontFamily: FONTS.zh, fontSize: 13, fontWeight: 700, color: on ? TOKENS.red : T.ink, whiteSpace: 'nowrap' }}>{it.label}</span>
                <span style={{ fontFamily: FONTS.mono, fontSize: 9, color: on ? TOKENS.red : T.inkMute, letterSpacing: 1, opacity: 0.85, whiteSpace: 'nowrap' }}>{it.jp}</span>
              </button>
            );
          })}
        </nav>

        {/* Search */}
        <div style={{ flex: 1, minWidth: 120, maxWidth: 380, marginLeft: 'auto' }}>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 8,
            background: T.ivory, border: `1px solid ${T.rule}`,
            borderRadius: 999, padding: '8px 14px',
          }}>
            <svg width="14" height="14" viewBox="0 0 14 14"><circle cx="6" cy="6" r="4.5" stroke={T.inkMute} strokeWidth="1.5" fill="none"/><path d="M9.5 9.5L13 13" stroke={T.inkMute} strokeWidth="1.5" strokeLinecap="round"/></svg>
            <input placeholder="搜尋課程・單字・文法…" style={{
              flex: 1, border: 'none', background: 'transparent', outline: 'none',
              fontFamily: FONTS.zh, fontSize: 13, color: T.ink,
            }}/>
            <span style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, padding: '2px 6px', border: `1px solid ${T.rule}`, borderRadius: 4 }}>⌘ K</span>
          </div>
        </div>

        {/* Right cluster */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <button onClick={onToggleDark} style={{
            width: 38, height: 38, borderRadius: 19, border: `1px solid ${T.rule}`,
            background: T.ivory, cursor: 'pointer',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
          }}>
            {dark ? (
              <svg width="18" height="18" viewBox="0 0 20 20"><path d="M16 12a6 6 0 01-8-8 7 7 0 108 8z" fill={T.ink}/></svg>
            ) : (
              <svg width="18" height="18" viewBox="0 0 20 20" fill="none">
                <circle cx="10" cy="10" r="3.2" stroke={T.inkSoft} strokeWidth="1.6"/>
                <path d="M10 2v2M10 16v2M3 10h2M15 10h2M4.9 4.9l1.4 1.4M13.7 13.7l1.4 1.4M4.9 15.1l1.4-1.4M13.7 6.3l1.4-1.4" stroke={T.inkSoft} strokeWidth="1.6" strokeLinecap="round"/>
              </svg>
            )}
          </button>
          <div style={{
            width: 38, height: 38, borderRadius: 19, cursor: 'pointer',
            background: TOKENS.book2Soft, border: `2px dashed ${TOKENS.book2Ink}`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: FONTS.jpHand, fontSize: 16, fontWeight: 700, color: TOKENS.book2Ink,
          }}>あ</div>
        </div>
      </div>
    </header>
  );
};

// ─────────────────────────────────────────────────────────────
// Mobile-fidelity viewport — renders a screen at exactly 402×?
// with the original paper background, in a soft "card" frame
// (no phone bezel — clearly web-native).
// ─────────────────────────────────────────────────────────────
const MobileViewport = ({ children, dark, height = SHELL_HEIGHT, label }) => {
  const T = useTheme(dark);
  return (
    <div style={{
      width: SHELL_WIDTH, flexShrink: 0, position: 'relative',
    }}>
      {label && (
        <div style={{
          fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute,
          letterSpacing: 1.5, marginBottom: 10, paddingLeft: 4,
          display: 'flex', alignItems: 'center', gap: 8,
        }}>
          <span style={{ width: 8, height: 8, borderRadius: 4, background: TOKENS.red }}/>
          {label}
        </div>
      )}
      <div style={{
        width: '100%', height,
        borderRadius: 24, overflow: 'hidden', position: 'relative',
        background: T.paper,
        border: `1px solid ${T.rule}`,
        boxShadow: dark
          ? '0 1px 0 rgba(255,255,255,0.04), 0 24px 60px rgba(0,0,0,0.4)'
          : '0 1px 0 rgba(255,255,255,0.6) inset, 0 24px 60px rgba(60,40,20,0.10), 0 4px 12px rgba(60,40,20,0.06)',
      }}>
        <div style={{ height: '100%', overflow: 'auto' }}>{children}</div>
      </div>
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// Left rail — book quick switcher (always visible on web)
// ─────────────────────────────────────────────────────────────
const LeftRail = ({ dark, currentBook, onPickBook, onOpenLesson }) => {
  const T = useTheme(dark);
  return (
    <aside style={{ width: 240, flexShrink: 0, position: 'sticky', top: 84, alignSelf: 'flex-start' }}>
      <div style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, letterSpacing: 1.6, marginBottom: 10, paddingLeft: 4 }}>
        ▍ 本棚 · BOOKSHELF
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
        {BOOKS.map(b => {
          const on = b.id === currentBook;
          return (
            <button key={b.id} onClick={() => onPickBook(b.id)} style={{
              border: 'none', cursor: 'pointer', textAlign: 'left',
              background: on ? b.color : T.ivory,
              borderLeft: `4px solid ${b.color}`,
              borderRadius: 12,
              padding: '10px 12px',
              boxShadow: on ? `0 4px 14px ${b.color}55` : 'none',
              transition: 'transform 0.15s',
              display: 'flex', alignItems: 'center', gap: 10,
            }}>
              <div style={{
                width: 30, height: 38, borderRadius: 4,
                background: b.color, position: 'relative', flexShrink: 0,
                border: `1px solid ${b.ink}40`, overflow: 'hidden',
              }}>
                <div style={{ position: 'absolute', inset: 0, opacity: 0.25,
                  backgroundImage: `repeating-linear-gradient(135deg, ${b.ink} 0 1px, transparent 1px 6px)` }}/>
                <div style={{ position: 'relative', textAlign: 'center', fontFamily: FONTS.jpHand, fontSize: 9, fontWeight: 700, color: b.ink, paddingTop: 6 }}>
                  從零<br/>{b.id > 1 ? b.id : ''}
                </div>
              </div>
              <div style={{ minWidth: 0, flex: 1 }}>
                <div style={{ fontFamily: FONTS.mono, fontSize: 9, color: on ? b.ink : T.inkMute, letterSpacing: 1, fontWeight: 700 }}>{b.tag}</div>
                <div style={{ fontFamily: FONTS.zhHand, fontSize: 13, fontWeight: 700, color: on ? b.ink : T.ink, marginTop: 1 }}>{b.short}</div>
                <div style={{ fontFamily: FONTS.mono, fontSize: 9, color: on ? b.ink : T.inkMute, opacity: 0.8, marginTop: 1 }}>{b.lessons} 課 · {b.date}</div>
              </div>
            </button>
          );
        })}
      </div>

      <div style={{ marginTop: 22 }}>
        <div style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, letterSpacing: 1.6, marginBottom: 10, paddingLeft: 4 }}>
          ▍ 快速跳轉
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 4, fontFamily: FONTS.zh, fontSize: 13 }}>
          {[
            ['五十音', 'kana'],
            ['單字卡', 'flash'],
            ['會話練習', 'conv'],
            ['影片頁', 'video'],
            ['測驗', 'quiz'],
          ].map(([l, id]) => (
            <button key={id} onClick={() => onOpenLesson(id)} style={{
              background: 'transparent', border: 'none', cursor: 'pointer',
              padding: '6px 10px', borderRadius: 8, textAlign: 'left',
              color: T.inkSoft, fontFamily: FONTS.zh, fontSize: 13,
              display: 'flex', alignItems: 'center', justifyContent: 'space-between',
            }}>
              <span>{l}</span>
              <span style={{ color: T.inkMute }}>→</span>
            </button>
          ))}
        </div>
      </div>
    </aside>
  );
};

// ─────────────────────────────────────────────────────────────
// Right rail — context panel: streak, today's plan, etc.
// ─────────────────────────────────────────────────────────────
const RightRail = ({ dark }) => {
  const T = useTheme(dark);
  return (
    <aside style={{ width: 280, flexShrink: 0, position: 'sticky', top: 84, alignSelf: 'flex-start' }}>
      {/* Streak */}
      <PaperCard dark={dark} padding={16}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <Stamp char="続" color={TOKENS.ochre} size={36}/>
          <div style={{ flex: 1 }}>
            <div style={{ fontFamily: FONTS.mono, fontSize: 9, color: T.inkMute, letterSpacing: 1.2 }}>STREAK</div>
            <div style={{ fontFamily: FONTS.zh, fontSize: 13, fontWeight: 700, color: T.ink, marginTop: 2 }}>連續學習 <span style={{ color: TOKENS.red, fontSize: 18, fontFamily: FONTS.jpHand }}>12</span> 天</div>
          </div>
        </div>
        <div style={{ marginTop: 12, display: 'flex', gap: 4 }}>
          {['月','火','水','木','金','土','日'].map((d, i) => (
            <div key={d} style={{
              flex: 1, aspectRatio: '0.7', borderRadius: 5,
              background: i < 5 ? TOKENS.book1 : T.rule,
              display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
              fontFamily: FONTS.jp, fontSize: 9, color: i < 5 ? TOKENS.book1Ink : T.inkMute,
              paddingBottom: 3,
            }}>{d}</div>
          ))}
        </div>
      </PaperCard>

      {/* Today's plan */}
      <div style={{ marginTop: 14 }}>
        <div style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, letterSpacing: 1.6, marginBottom: 10, paddingLeft: 4 }}>
          ▍ 今日計畫
        </div>
        <PaperCard dark={dark} padding={14}>
          {[
            { d: true, t: '單字 · 食べる、飲む、行く', s: '5 分鐘' },
            { d: true, t: '影片 · て形變化', s: '32 分鐘' },
            { d: false, t: '本課測驗 10 題', s: '10 分鐘' },
            { d: false, t: '會話跟讀 4 句', s: '5 分鐘' },
          ].map((p, i) => (
            <div key={i} style={{
              display: 'flex', alignItems: 'center', gap: 10,
              padding: '8px 0', borderTop: i ? `1px dashed ${T.rule}` : 'none',
            }}>
              <div style={{
                width: 20, height: 20, borderRadius: 5, flexShrink: 0,
                background: p.d ? TOKENS.book4 : 'transparent',
                border: `1.5px solid ${p.d ? TOKENS.book4Ink : T.rule}`,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
              }}>
                {p.d && <svg width="12" height="12" viewBox="0 0 12 12"><path d="M2 6l3 3 5-6" stroke={TOKENS.book4Ink} strokeWidth="2" fill="none" strokeLinecap="round"/></svg>}
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontFamily: FONTS.zh, fontSize: 12, color: p.d ? T.inkMute : T.ink, fontWeight: 600, textDecoration: p.d ? 'line-through' : 'none' }}>{p.t}</div>
                <div style={{ fontFamily: FONTS.mono, fontSize: 9, color: T.inkMute, marginTop: 1 }}>{p.s}</div>
              </div>
            </div>
          ))}
        </PaperCard>
      </div>

      {/* Keyboard shortcuts */}
      <div style={{ marginTop: 14 }}>
        <div style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.inkMute, letterSpacing: 1.6, marginBottom: 10, paddingLeft: 4 }}>
          ▍ 快捷鍵 · SHORTCUTS
        </div>
        <PaperCard dark={dark} padding={14}>
          {[
            ['搜尋',     '⌘ K'],
            ['上一頁',   'Esc'],
            ['切換振假名','F'],
            ['暗色模式',  'D'],
          ].map(([l, k], i) => (
            <div key={l} style={{
              display: 'flex', justifyContent: 'space-between', alignItems: 'center',
              padding: '6px 0', borderTop: i ? `1px dashed ${T.rule}` : 'none',
              fontFamily: FONTS.zh, fontSize: 12, color: T.inkSoft,
            }}>
              <span>{l}</span>
              <span style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.ink, padding: '2px 8px', border: `1px solid ${T.rule}`, borderRadius: 4, background: T.paperDeep }}>{k}</span>
            </div>
          ))}
        </PaperCard>
      </div>
    </aside>
  );
};

// ─────────────────────────────────────────────────────────────
// Footer
// ─────────────────────────────────────────────────────────────
const WebFooter = ({ dark }) => {
  const T = useTheme(dark);
  return (
    <footer style={{
      borderTop: `1px solid ${T.rule}`, marginTop: 60,
      padding: '32px 28px', fontFamily: FONTS.zh, fontSize: 12, color: T.inkMute,
    }}>
      <div style={{ maxWidth: 1440, margin: '0 auto', display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap', gap: 16 }}>
        <div>
          <div style={{ fontFamily: FONTS.jpHand, fontSize: 16, color: T.ink, fontWeight: 700 }}>井上老師の日本語教室</div>
          <div style={{ marginTop: 4, fontFamily: FONTS.mono, fontSize: 10, letterSpacing: 1.2 }}>ILOVEJP · WEB v 1.0.0 · build 2026.05</div>
        </div>
        <div style={{ display: 'flex', gap: 24 }}>
          {['關於這個 App', '老師專欄', 'YouTube', 'PDF 下載', '使用條款'].map(l => (
            <a key={l} style={{ color: T.inkSoft, textDecoration: 'none', cursor: 'pointer' }}>{l}</a>
          ))}
        </div>
        <div style={{ fontFamily: FONTS.mono, fontSize: 10 }}>made with ☕ + ✏️</div>
      </div>
    </footer>
  );
};

Object.assign(window, {
  WebHeader, MobileViewport, LeftRail, RightRail, WebFooter,
  SHELL_WIDTH, SHELL_HEIGHT,
});
