
// Prepend remote base URL for screenshots if hosted on Cloudflare R2 / S3
function getShotUrl(path) {
  if (!path) return "";
  const base = window.SCREENSHOT_BASE_URL || "";
  if (base) {
    const cleanBase = base.endsWith("/") ? base : base + "/";
    return cleanBase + path.replace(/\\/g, "/");
  }
  return path;
}
// Shared components: icons, sidebar, drawer, lightbox, toasts.

function formatSize(bytes) {
  if (!bytes) return "—";
  if (bytes < 1024) return bytes + " B";
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + " KB";
  return (bytes / (1024 * 1024)).toFixed(1) + " MB";
}

// ── Icons (line, 16px viewBox) ──────────────────────────────────────────────
const ic = {
  archive: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M2 4h12v3H2zM3 7v6h10V7M6 9.5h4"/></svg>,
  grid:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2.5" y="2.5" width="4.5" height="4.5" rx="1"/><rect x="9" y="2.5" width="4.5" height="4.5" rx="1"/><rect x="2.5" y="9" width="4.5" height="4.5" rx="1"/><rect x="9" y="9" width="4.5" height="4.5" rx="1"/></svg>,
  list:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M2.5 4h11M2.5 8h11M2.5 12h11"/><circle cx="5" cy="4" r=".7" fill="currentColor"/><circle cx="5" cy="8" r=".7" fill="currentColor"/><circle cx="5" cy="12" r=".7" fill="currentColor"/></svg>,
  health:  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M2 9h3l1.5-4 3 8L11 9h3"/></svg>,
  folder:  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M2 4.5C2 3.7 2.7 3 3.5 3h2.6c.5 0 .9.2 1.2.6L8 4.5h4.5c.8 0 1.5.7 1.5 1.5v6c0 .8-.7 1.5-1.5 1.5h-9C2.7 13.5 2 12.8 2 12V4.5z"/></svg>,
  terminal:<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2" y="3" width="12" height="10" rx="1.5"/><path d="M5 6l2 2-2 2M8.5 10.5h3"/></svg>,
  search:  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><circle cx="7" cy="7" r="4.5"/><path d="m10.5 10.5 3 3"/></svg>,
  x:       <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="m4 4 8 8M12 4l-8 8"/></svg>,
  star:    <svg viewBox="0 0 16 16" fill="currentColor"><path d="m8 1.5 2 4.4 4.8.5-3.6 3.3 1 4.8L8 12l-4.2 2.5 1-4.8L1.2 6.4 6 5.9z"/></svg>,
  flame:   <svg viewBox="0 0 16 16" fill="currentColor"><path d="M8 1.5c.4 2 2 2.8 2 5 0 1-.5 1.7-1 2 0-1-.5-1.6-1-2-2 1-3 3-3 5 0 2 1.5 3.5 3 3.5s3-1.5 3-3.5c0-3-3-5-3-10z"/></svg>,
  sun:     <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><circle cx="8" cy="8" r="2.5"/><path d="M8 1.5v1.8M8 12.7v1.8M1.5 8h1.8M12.7 8h1.8M3.5 3.5l1.3 1.3M11.2 11.2l1.3 1.3M3.5 12.5l1.3-1.3M11.2 4.8l1.3-1.3"/></svg>,
  moon:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M13 9.5A5 5 0 0 1 6.5 3a5 5 0 1 0 6.5 6.5z"/></svg>,
  download:<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M8 2v8m0 0L5 7m3 3 3-3M3 12.5h10"/></svg>,
  play:    <svg viewBox="0 0 16 16" fill="currentColor"><path d="M4 3v10l9-5z"/></svg>,
  broken:  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M7 6 5 4 3 6l2 2M9 10l2 2 2-2-2-2M6.5 6.5l3 3"/></svg>,
  check:   <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2.2"><path d="m3 8 3.5 3.5L13 5"/></svg>,
  cam:     <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2" y="4.5" width="12" height="8.5" rx="1.5"/><path d="M6 4.5 7 3h2l1 1.5"/><circle cx="8" cy="9" r="2"/></svg>,
  hdd:     <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2" y="4" width="12" height="8" rx="1.5"/><circle cx="11.5" cy="8" r=".8" fill="currentColor"/></svg>,
  trophy:  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M4 3h8v3a4 4 0 1 1-8 0V3z"/><path d="M3 3.5h1M12 3.5h1M6 11h4M5.5 13.5h5"/></svg>,
  drag:    <svg viewBox="0 0 16 16" fill="currentColor"><circle cx="6" cy="4" r="1"/><circle cx="10" cy="4" r="1"/><circle cx="6" cy="8" r="1"/><circle cx="10" cy="8" r="1"/><circle cx="6" cy="12" r="1"/><circle cx="10" cy="12" r="1"/></svg>,
  plus:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="M8 3v10M3 8h10"/></svg>,
  chevron: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="m6 4 4 4-4 4"/></svg>,
  arrow_l: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="m10 4-4 4 4 4"/></svg>,
  arrow_r: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="m6 4 4 4-4 4"/></svg>,
  ext:     <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M6 3h7v7M13 3 6 10M3 6v7h7"/></svg>,
  refresh: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M13.5 7a5.5 5.5 0 1 0-1.4 4.6"/><path d="M13.5 3.5v3.7h-3.7"/></svg>,
  menu:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M2.5 4h11M2.5 8h11M2.5 12h11"/></svg>,
  heart:   <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M8 13.5s-5-2.8-5-6.5a2.5 2.5 0 0 1 4.5-1.5L8 6.2l.5-.7A2.5 2.5 0 0 1 13 7c0 3.7-5 6.5-5 6.5z"/></svg>,
  dice:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2.5" y="2.5" width="11" height="11" rx="2"/><circle cx="5.5" cy="5.5" r="1" fill="currentColor"/><circle cx="10.5" cy="10.5" r="1" fill="currentColor"/><circle cx="8" cy="8" r="1" fill="currentColor"/><circle cx="10.5" cy="5.5" r="1" fill="currentColor"/><circle cx="5.5" cy="10.5" r="1" fill="currentColor"/></svg>,
  bulb:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M5.5 11.5h5M6.5 13h3M8 2.5a4.5 4.5 0 0 1 4.5 4.5c0 1.6-.8 3-2.1 3.8-.4.3-.4.8-.4 1.2H6c0-.4 0-.9-.4-1.2A4.5 4.5 0 0 1 8 2.5z"/></svg>,
  link:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M4.75 7.5a3.25 3.25 0 0 1 5.54-2.3l1.2 1.2a3.25 3.25 0 0 1-4.6 4.6l-.6-.6M11.25 8.5a3.25 3.25 0 0 1-5.54 2.3l-1.2-1.2a3.25 3.25 0 0 1 4.6-4.6l.6.6"/></svg>,
  warning: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><path d="M8 2l6 11H2L8 2zM8 5.5v4M8 11.5h.01"/></svg>,
  checkCircle: <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><circle cx="8" cy="8" r="6"/><path d="m5.5 8 2 2 3.5-4"/></svg>,
  mail:    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4"><rect x="2" y="3.5" width="12" height="9" rx="1.5"/><path d="m2 5 6 3.5 6-3.5"/></svg>,
};

// Brand mark — slightly animated "archive" glyph
function BrandMark() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M2.5 4.5 8 2l5.5 2.5M2.5 4.5v7L8 14l5.5-2.5v-7M2.5 4.5 8 7l5.5-2.5M8 7v7" />
    </svg>
  );
}

// ── Sidebar ─────────────────────────────────────────────────────────────────
function Sidebar({ view, onView, tweaks, setTweak, gameCount }) {
  const NAV = [
    { k: 'explorer',    label: 'Browse Games',      icon: ic.archive,  count: gameCount },
    { k: 'donation',    label: 'Donation & Support', icon: ic.heart,    count: null },
    { k: 'links',       label: 'Community Links',   icon: ic.ext,      count: null },
    { k: 'contact',     label: 'About & Contact',   icon: ic.mail,     count: null }
  ];
  return (
    <aside className="sb">
      <div className="sb-brand">
        <div className="sb-logo"><BrandMark /></div>
        <div style={{ flex: 1 }}>
          <div className="sb-brand-name">Archive</div>
          <div className="sb-brand-sub mono">fangame library</div>
        </div>
        <button className="sb-mobile-close" onClick={() => window.closeSidebar && window.closeSidebar()} title="Close menu">
          {ic.x}
        </button>
      </div>

      <div>
        <div className="sb-section-label">Library</div>
        <nav className="sb-nav">
          {NAV.map((n) => (
            <button key={n.k} className={'sb-item' + (view === n.k ? ' active' : '')} onClick={() => onView(n.k)}>
              {n.icon}
              <span>{n.label}</span>
              {n.count != null && <span className="sb-item-count tnum">{n.count}</span>}
            </button>
          ))}
        </nav>
      </div>

      <div className="sb-foot">
        <div className="sb-stat"><span><span className="sb-pulse" />Storage</span><b className="mono">618.24 GB</b></div>
        <div className="sb-stat"><span>Archived</span><b className="mono">{gameCount.toLocaleString()}</b></div>
        <div className="sb-stat"><span>Sync Status</span><b className="mono" style={{ color: 'oklch(0.72 0.15 152)' }}>Online</b></div>
        <div style={{ padding: '10px 0 0 0', borderTop: '1px solid var(--border)', marginTop: '10px', fontSize: '9.5px', color: 'var(--muted)', letterSpacing: '0.01em', lineHeight: '1.45' }}>
          Fangame Archive © Kureist 2026<br/>
          Developer & Designer
        </div>
      </div>
    </aside>
  );
}

// ── Toasts ─────────────────────────────────────────────────────────────────
function Toasts({ items }) {
  return (
    <div className="toasts">
      {items.map((t) => (
        <div key={t.id} className="toast">
          <span className="dot" />
          <b>{t.title}</b>
          <span className="sub">{t.sub}</span>
        </div>
      ))}
    </div>
  );
}

// ── Lightbox ───────────────────────────────────────────────────────────────
function Lightbox({ shots, index, onClose, onPrev, onNext }) {
  React.useEffect(() => {
    const onKey = (e) => {
      if (e.key === 'Escape') onClose();
      else if (e.key === 'ArrowLeft') onPrev();
      else if (e.key === 'ArrowRight') onNext();
    };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose, onPrev, onNext]);
  if (!shots) return null;
  const cur = shots[index];
  return (
    <div className="lbox" onClick={onClose}>
      <div className="lbox-inner" onClick={(e) => e.stopPropagation()}>
        <img src={getShotUrl(cur?.image_path)} style={{ maxWidth: '100%', maxHeight: '100%', objectFit: 'contain', zIndex: 1 }} />
        <button className="lbox-x" onClick={onClose}>{ic.x}</button>
        {shots.length > 1 && <>
          <button className="lbox-nav prev" onClick={onPrev}>{ic.arrow_l}</button>
          <button className="lbox-nav next" onClick={onNext}>{ic.arrow_r}</button>
        </>}
        <div className="lbox-cap mono">
          {index + 1} / {shots.length} · captured by {cur?.by} · esc to close · ←/→ to navigate
        </div>
      </div>
    </div>
  );
}

// ── Drawer (Game Detail) ───────────────────────────────────────────────────
function Drawer({ game, isRoll, onClose }) {
  const [openShot, setOpenShot] = React.useState(-1);
  const shots = game ? (window.DATA.SCREENSHOTS[game.id] || []) : [];
  const reviews = game ? (window.DATA.REVIEWS[game.id] || []) : [];

  if (!game) return null;


  return (
    <>
      <div className="drawer-scrim on" onClick={onClose} />
      <aside className="drawer on">
        <header className="drawer-hd">
          <div className="drawer-thumb">
            <div className="card-thumb-grid" />
            <div className="card-thumb-glyph" style={{ fontSize: 22 }}>{game.title[0]}</div>
          </div>
          <div style={{ flex: 1, minWidth: 0 }}>
            <h2 className="drawer-title">{game.title}</h2>
            <div className="drawer-meta">
              by <a href="#">{game.creator}</a>
              {' · '}<span className="mono">{game.df_id}</span>
            </div>
            <div className="drawer-meta" style={{ marginTop: 2, fontSize: 11, opacity: 0.9 }}>
              <span className="mono">{game.url}</span>
            </div>
          </div>
          <button className="drawer-close" onClick={onClose}>{ic.x}</button>
        </header>

        <div className="drawer-body">
          {/* Stats Metrics Grid */}
          <section className="drawer-sec" style={{ 
            display: 'grid', 
            gridTemplateColumns: 'repeat(3, 1fr)', 
            gap: '8px', 
            marginBottom: '12px' 
          }}>
            <div style={{
              background: 'rgba(255, 255, 255, 0.025)',
              border: '1px solid rgba(255, 255, 255, 0.05)',
              borderRadius: '8px',
              padding: '8px 6px',
              textAlign: 'center',
              display: 'flex',
              flexDirection: 'column',
              gap: '2px'
            }}>
              <span style={{ fontSize: '9px', textTransform: 'uppercase', letterSpacing: '0.05em', color: 'var(--muted)' }}>Rating</span>
              <span style={{ fontSize: '13px', fontWeight: '700', fontFamily: 'var(--font-mono)' }}>
                {game.rating > 0 ? `${game.rating.toFixed(1)}/10.0` : 'N/A'}
              </span>
              <span style={{ fontSize: '9px', color: 'var(--muted)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
                {game.reviews} {game.reviews === 1 ? 'review' : 'reviews'}
              </span>
            </div>
            
            <div style={{
              background: 'rgba(255, 255, 255, 0.025)',
              border: '1px solid rgba(255, 255, 255, 0.05)',
              borderRadius: '8px',
              padding: '8px 6px',
              textAlign: 'center',
              display: 'flex',
              flexDirection: 'column',
              gap: '2px'
            }}>
              <span style={{ fontSize: '9px', textTransform: 'uppercase', letterSpacing: '0.05em', color: 'var(--muted)' }}>Difficulty</span>
              <span style={{ fontSize: '13px', fontWeight: '700', fontFamily: 'var(--font-mono)' }}>
                {game.difficulty > 0 ? `${game.difficulty.toFixed(1)}/100.0` : 'N/A'}
              </span>
              <span style={{ fontSize: '9px', color: 'var(--muted)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
                {game.difficulty > 0 ? 'Standard' : 'N/A'}
              </span>
            </div>
            
            <div style={{
              background: 'rgba(255, 255, 255, 0.025)',
              border: '1px solid rgba(255, 255, 255, 0.05)',
              borderRadius: '8px',
              padding: '8px 6px',
              textAlign: 'center',
              display: 'flex',
              flexDirection: 'column',
              gap: '2px'
            }}>
              <span style={{ fontSize: '9px', textTransform: 'uppercase', letterSpacing: '0.05em', color: 'var(--muted)' }}>File Size</span>
              <span style={{ fontSize: '13px', fontWeight: '700', fontFamily: 'var(--font-mono)' }}>
                {game.file_size > 0 ? formatSize(game.file_size) : 'N/A'}
              </span>
              <span style={{ fontSize: '9px', color: 'var(--muted)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
                {game.file_size > 0 ? (game.flags?.local ? 'R2 CDN' : 'External') : 'N/A'}
              </span>
            </div>
          </section>

          <section className="drawer-sec" style={{ display: 'flex', gap: '8px' }}>
            <button
              className="launch-btn"
              style={{ flex: 1 }}
              disabled={!game.url || game.flags?.broken}
              onClick={() => {
                if (game.url && !game.flags?.broken) {
                  window.open(game.url, '_blank', 'noopener,noreferrer');
                }
              }}
            >
              {(!game.url || game.flags?.broken) ? (
                <>{ic.x} Not Available</>
              ) : (
                <>{ic.download} Download</>
              )}
            </button>

            {isRoll && window.rollRandomGame && (
              <button
                className="roll-again-btn"
                onClick={() => window.rollRandomGame()}
                title="Roll another random game from current filters"
                style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: '6px' }}
              >
                {ic.dice} Roll Again
              </button>
            )}
          </section>

          {game.tags && game.tags.length > 0 && (
            <section className="drawer-sec">
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: 5 }}>
                {game.tags.map((t) => <span key={t} className="tag" style={{ height: 22, cursor: 'default' }}>{t}</span>)}
              </div>
            </section>
          )}



          <section className="drawer-sec">
            <h5>Screenshots <span style={{ color: 'var(--muted)', fontWeight: 400, marginLeft: 4 }}>({shots.length})</span></h5>
            {shots.length > 0 ? (
              <div className="gallery">
                {shots.map((s, i) => (
                  <div key={i} className="shot" onClick={() => setOpenShot(i)}>
                    <img src={getShotUrl(s.image_path)} style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
                    <span className="cap">by {s.by}</span>
                  </div>
                ))}
              </div>
            ) : <p style={{ margin: 0, fontSize: 12, color: 'var(--muted)' }}>No screenshots captured.</p>}
          </section>

          <section className="drawer-sec">
            <h5>Community Reviews <span style={{ color: 'var(--muted)', fontWeight: 400, marginLeft: 4 }}>({reviews.length})</span></h5>
            {reviews.length === 0 && <p style={{ margin: 0, fontSize: 12, color: 'var(--muted)' }}>No reviews mirrored yet.</p>}
            {reviews.map((r, i) => (
              <div key={i} className="review">
                <div className="review-hd">
                  <b><a href="#">{r.user}</a></b>
                  <span className="mono">{r.rating}/10</span>
                  <span style={{ color: 'var(--muted)' }}>diff {r.diff}</span>
                  <span style={{ color: 'var(--muted)' }}>· ♡ {r.liked}</span>
                  <span className="date">{r.date}</span>
                </div>
                <p className="review-body">{r.body}</p>
                <div className="review-foot">
                  {r.tags.map((t) => <span key={t} className="tag-mini">{t}</span>)}
                </div>
              </div>
            ))}
          </section>
        </div>
      </aside>

      {openShot >= 0 && (
        <Lightbox shots={shots} index={openShot}
          onClose={() => setOpenShot(-1)}
          onPrev={() => setOpenShot((openShot - 1 + shots.length) % shots.length)}
          onNext={() => setOpenShot((openShot + 1) % shots.length)} />
      )}
    </>
  );
}

function DonationView() {
  const [copied, setCopied] = React.useState(null);
  
  const wallets = [
    { coin: 'AFD', label: 'Afdian - Support Creator', addr: 'https://ifdian.net/a/kureist', color: '#946ce6', isLink: true },
    { coin: 'BTC', label: 'Bitcoin (BTC)', addr: 'bc1qdrkrrqrtquuwrwug4ps0djws47yndsc6k4mxdj', color: '#f7931a' },
    { coin: 'ETH', label: 'Ethereum (ETH - ERC20)', addr: '0xe1F7768210Dd93F635553b2ba3F1B897ef7B795C', color: '#627eea' },
    { coin: 'USDT', label: 'Tether USD (USDT - ERC20)', addr: '0xe1F7768210Dd93F635553b2ba3F1B897ef7B795C', color: '#26a17b' },
    { coin: 'USDC', label: 'USD Coin (USDC - ERC20)', addr: '0xe1F7768210Dd93F635553b2ba3F1B897ef7B795C', color: '#2775ca' }
  ];

  const handleCopy = (addr, coin) => {
    navigator.clipboard.writeText(addr);
    setCopied(coin);
    setTimeout(() => setCopied(null), 2000);
  };

  return (
    <div style={{ width: '100%', height: '100%', overflowY: 'auto', minHeight: 0 }}>
      <div className="donation-view" style={{ padding: '28px 36px', maxWidth: '820px', margin: '0 auto', fontFamily: 'var(--font-sans)', boxSizing: 'border-box' }}>
      <div style={{ marginBottom: '32px' }}>
        <h1 style={{ fontSize: '26px', fontWeight: '700', marginBottom: '12px', display: 'flex', alignItems: 'center', gap: '10px', color: 'var(--text)' }}>
          <span style={{ color: '#ef4444', display: 'inline-flex', alignItems: 'center', filter: 'drop-shadow(0 2px 4px rgba(239, 68, 68, 0.15))' }}>{ic.heart}</span> Donation & Support
        </h1>
        <p style={{ color: 'var(--text-soft)', fontSize: '14px', lineHeight: '1.6', margin: 0 }}>
          This fangame archive is a community-driven project hosting over 17,000 games and 618GB+ of authentic crawled content. 
          Your sponsorship directly supports ongoing server hosting, high-performance bandwidth, and CDN distribution costs.
          Thank you for keeping the archive alive!
        </p>
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: '1fr', gap: '16px' }}>
        {wallets.map((w) => (
          <div key={w.coin} style={{
            background: 'var(--panel)',
            border: '1px solid var(--border)',
            borderRadius: '12px',
            padding: '20px 24px',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'space-between',
            gap: '16px',
            boxShadow: 'var(--shadow-sm)',
            transition: 'all 0.2s ease',
          }} className="donation-card">
            <div style={{ display: 'flex', alignItems: 'center', gap: '16px', flex: 1, minWidth: 0 }}>
              <div style={{
                width: '46px',
                height: '46px',
                borderRadius: '10px',
                background: `${w.color}15`,
                color: w.color,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                fontSize: '14px',
                fontWeight: '700',
                flexShrink: 0,
                border: `1px solid ${w.color}30`
              }}>
                {w.coin}
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: '12.5px', color: 'var(--muted)', fontWeight: '500', marginBottom: '4px' }}>
                  {w.label}
                </div>
                <div className="mono" style={{ 
                  fontSize: '13px', 
                  color: 'var(--text)', 
                  background: 'var(--panel-active)', 
                  padding: '8px 12px', 
                  borderRadius: '6px',
                  border: '1px solid var(--border-soft)',
                  overflowX: 'auto',
                  whiteSpace: 'nowrap',
                  scrollbarWidth: 'none',
                  WebkitOverflowScrolling: 'touch'
                }}>
                  {w.addr}
                </div>
              </div>
            </div>
            
            {w.isLink ? (
              <a 
                href={w.addr}
                target="_blank"
                rel="noopener noreferrer"
                style={{
                  background: 'var(--accent-soft)',
                  color: 'var(--accent-strong)',
                  border: '1px solid var(--accent-border)',
                  borderRadius: '8px',
                  padding: '8px 20px',
                  fontSize: '12.5px',
                  fontWeight: '600',
                  cursor: 'pointer',
                  transition: 'all 0.15s ease',
                  display: 'flex',
                  alignItems: 'center',
                  gap: '6px',
                  whiteSpace: 'nowrap',
                  textDecoration: 'none',
                  outline: 'none'
                }}
                className="sponsor-btn"
              >
                <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" style={{ width: '12px', height: '12px' }}>
                  <path d="M6 3h7v7M13 3 6 10M3 6v7h7" />
                </svg>
                Sponsor
              </a>
            ) : (
              <button 
                onClick={() => handleCopy(w.addr, w.coin)}
                style={{
                  background: copied === w.coin ? 'var(--accent-soft)' : 'var(--panel-active)',
                  color: copied === w.coin ? 'var(--accent-strong)' : 'var(--text)',
                  border: `1px solid ${copied === w.coin ? 'var(--accent-border)' : 'var(--border)'}`,
                  borderRadius: '8px',
                  padding: '8px 16px',
                  fontSize: '12.5px',
                  fontWeight: '600',
                  cursor: 'pointer',
                  transition: 'all 0.15s ease',
                  display: 'flex',
                  alignItems: 'center',
                  gap: '6px',
                  whiteSpace: 'nowrap',
                  outline: 'none'
                }}
                className="copy-btn"
              >
                {copied === w.coin ? (
                  <>
                    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" style={{ width: '12px', height: '12px' }}>
                      <path d="m3 8 3.5 3.5L13 5" />
                    </svg>
                    Copied
                  </>
                ) : (
                  <>
                    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" style={{ width: '12px', height: '12px' }}>
                      <rect x="2.5" y="4.5" width="7" height="9" rx="1.5" />
                      <path d="M6.5 2.5h5A1.5 1.5 0 0 1 13 4v7" />
                    </svg>
                    Copy
                  </>
                )}
              </button>
            )}
          </div>
        ))}
      </div>
      
      <div style={{ 
        marginTop: '32px', 
        padding: '16px 20px', 
        background: 'rgba(59, 130, 246, 0.08)', 
        border: '1px solid rgba(59, 130, 246, 0.18)', 
        borderRadius: '10px', 
        color: 'var(--text-soft)', 
        fontSize: '12.5px', 
        lineHeight: '1.55',
        display: 'flex',
        alignItems: 'flex-start',
        gap: '12px'
      }}>
        <span style={{ color: 'var(--accent)', display: 'inline-flex', alignItems: 'center', filter: 'drop-shadow(0 1px 2px rgba(59,130,246,0.2))' }}>{ic.bulb}</span>
        <div>
          <b style={{ color: 'var(--text)' }}>Security Notice:</b> Double-check the wallet address and network before making any transaction. 
          Ethereum (ETH), USDT, and USDC addresses use the <b>ERC-20 network (Ethereum Mainnet)</b>.
        </div>
      </div>
    </div>
  </div>
  );
}

function LinksView() {
  const links = [
    { name: 'Delicious Fruit', desc: 'The historic flagship archive. The foundation of I Wanna cataloging and reviews for over a decade.', url: 'https://delicious-fruit.com/', color: '#ef4444' },
    { name: 'I Wanna Wiki', desc: 'A rich community-maintained encyclopedic database containing creator bios, detailed walkthroughs, and wiki listings.', url: 'https://www.iwannawiki.com/', color: '#3b82f6' },
    { name: 'Dappermink Archive', desc: 'An exceptionally complete vault hosting hundreds of classic, modern, and obscure I Wanna fangame binaries.', url: 'https://archive.dappermink.me/home', color: '#10b981' }
  ];

  return (
    <div style={{ width: '100%', height: '100%', overflowY: 'auto', minHeight: 0 }}>
      <div className="links-view" style={{ padding: '28px 36px', maxWidth: '820px', margin: '0 auto', fontFamily: 'var(--font-sans)', boxSizing: 'border-box' }}>
      <div style={{ marginBottom: '32px' }}>
        <h1 style={{ fontSize: '26px', fontWeight: '700', marginBottom: '12px', display: 'flex', alignItems: 'center', gap: '10px', color: 'var(--text)' }}>
          <span style={{ color: 'oklch(0.65 0.17 250)', display: 'inline-flex', alignItems: 'center', filter: 'drop-shadow(0 2px 4px rgba(59, 130, 246, 0.15))' }}>{ic.link}</span> Community Links
        </h1>
        <p style={{ color: 'var(--text-soft)', fontSize: '14px', lineHeight: '1.6', margin: 0 }}>
          Quick portals to our friendly archives, wiki databases, and fellow community platforms. 
          These sites form the backbone of the global I Wanna fangame legacy.
        </p>
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: '1fr', gap: '16px' }}>
        {links.map((l) => (
          <a key={l.name} href={l.url} target="_blank" rel="noopener noreferrer" style={{
            background: 'var(--panel)',
            border: '1px solid var(--border)',
            borderRadius: '12px',
            padding: '20px 24px',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'space-between',
            gap: '16px',
            textDecoration: 'none',
            color: 'inherit',
            boxShadow: 'var(--shadow-sm)',
            transition: 'all 0.2s ease',
          }} className="link-card" id={`link-${l.name.toLowerCase().replace(/\s+/g, '-')}`}>
            <div style={{ display: 'flex', alignItems: 'center', gap: '16px', flex: 1, minWidth: 0 }}>
              <div style={{
                width: '46px',
                height: '46px',
                borderRadius: '10px',
                background: `${l.color}15`,
                color: l.color,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                fontSize: '18px',
                fontWeight: '700',
                flexShrink: 0,
                border: `1px solid ${l.color}30`
              }}>
                {l.name[0]}
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: '14.5px', color: 'var(--text)', fontWeight: '600', marginBottom: '4px', display: 'flex', alignItems: 'center', gap: '6px' }}>
                  {l.name}
                </div>
                <div style={{ fontSize: '12.5px', color: 'var(--text-soft)', lineHeight: '1.5' }}>
                  {l.desc}
                </div>
              </div>
            </div>
            <div style={{
              width: '32px',
              height: '32px',
              borderRadius: '6px',
              background: 'var(--panel-active)',
              border: '1px solid var(--border)',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              color: 'var(--muted)',
              transition: 'all 0.15s ease'
            }}>
              {ic.chevron}
            </div>
          </a>
        ))}
      </div>
    </div>
  </div>
  );
}

function ContactView() {
  const techStack = {
    frontend: [
      { name: 'React (Standalone)', url: 'https://react.dev/' },
      { name: 'ES6+ JavaScript', url: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript' },
      { name: 'Babel Standalone', url: 'https://babeljs.io/' },
      { name: 'Vanilla CSS3', url: 'https://developer.mozilla.org/en-US/docs/Web/CSS' },
      { name: 'SVG Vectors', url: 'https://developer.mozilla.org/en-US/docs/Web/SVG' }
    ],
    backend: [
      { name: 'Python 3', url: 'https://www.python.org/' },
      { name: 'Go (Golang)', url: 'https://go.dev/' },
      { name: 'Node.js', url: 'https://nodejs.org/' },
      { name: 'BeautifulSoup4', url: 'https://www.crummy.com/software/BeautifulSoup/bs4/doc/' },
      { name: 'Ripgrep', url: 'https://github.com/BurntSushi/ripgrep' }
    ],
    database: [
      { name: 'JSON Database (Chunked)', url: 'https://www.json.org/' },
      { name: 'IndexedDB (High-Speed Client Cache)', url: 'https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API' },
      { name: 'Cloudflare R2', url: 'https://www.cloudflare.com/developer-platform/r2/' },
      { name: 'AWS S3 SDK', url: 'https://aws.amazon.com/s3/' }
    ],
    infrastructure: [
      { name: '7-Zip CLI (Decryption & Re-compression)', url: 'https://www.7-zip.org/' },
      { name: 'PowerShell / CMD', url: 'https://learn.microsoft.com/en-us/powershell/' }
    ]
  };

  return (
    <div style={{ width: '100%', height: '100%', overflowY: 'auto', minHeight: 0 }}>
      <div className="contact-view" style={{ padding: '28px 36px', maxWidth: '640px', margin: '0 auto', fontFamily: 'var(--font-sans)', boxSizing: 'border-box' }}>
        <div style={{ marginBottom: '32px' }}>
          <h1 style={{ fontSize: '26px', fontWeight: '700', marginBottom: '12px', display: 'flex', alignItems: 'center', gap: '10px', color: 'var(--text)' }}>
            <span style={{ color: 'var(--accent)', display: 'inline-flex', alignItems: 'center' }}>{ic.mail}</span> About & Contact
          </h1>
          <p style={{ color: 'var(--text-soft)', fontSize: '14px', lineHeight: '1.6', margin: 0 }}>
            Catalog details, credits, technical stack, and direct communications registry.
          </p>
        </div>

        {/* Creator & Special Thanks */}
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '16px', marginBottom: '24px' }}>
          <div style={{ background: 'var(--panel)', border: '1px solid var(--border)', borderRadius: '12px', padding: '16px 20px', boxShadow: 'var(--shadow-sm)' }}>
            <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '600', marginBottom: '6px', textTransform: 'uppercase', letterSpacing: '0.04em' }}>Creator</div>
            <div style={{ fontSize: '15.5px', fontWeight: '700', color: 'var(--fg)' }}>kureist</div>
          </div>
          <div style={{ background: 'var(--panel)', border: '1px solid var(--border)', borderRadius: '12px', padding: '16px 20px', boxShadow: 'var(--shadow-sm)' }}>
            <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '600', marginBottom: '6px', textTransform: 'uppercase', letterSpacing: '0.04em' }}>Special Thanks</div>
            <div style={{ fontSize: '15.5px', fontWeight: '700', color: 'var(--fg)', display: 'flex', alignItems: 'center', gap: '6px' }}>Chance</div>
          </div>
        </div>

        {/* Technical Stack */}
        <div style={{ background: 'var(--panel)', border: '1px solid var(--border)', borderRadius: '12px', padding: '20px 24px', boxShadow: 'var(--shadow-sm)', marginBottom: '24px' }}>
          <h3 style={{ fontSize: '13.5px', fontWeight: '600', color: 'var(--fg)', marginBottom: '16px', marginTop: 0 }}>Technical Stack</h3>
          <div style={{ display: 'flex', flexDirection: 'column', gap: '14px' }}>
            <div>
              <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '500', marginBottom: '6px' }}>Frontend Core</div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
                {techStack.frontend.map(t => (
                  <a key={t.name} href={t.url} target="_blank" rel="noopener noreferrer" className="tag" style={{ height: '22px', fontSize: '10.5px', textDecoration: 'none', color: 'inherit', cursor: 'pointer' }}>{t.name}</a>
                ))}
              </div>
            </div>
            <div>
              <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '500', marginBottom: '6px' }}>Backend & Crawlers</div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
                {techStack.backend.map(t => (
                  <a key={t.name} href={t.url} target="_blank" rel="noopener noreferrer" className="tag" style={{ height: '22px', fontSize: '10.5px', textDecoration: 'none', color: 'inherit', cursor: 'pointer' }}>{t.name}</a>
                ))}
              </div>
            </div>
            <div>
              <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '500', marginBottom: '6px' }}>Database & Cloud Storage</div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
                {techStack.database.map(t => (
                  <a key={t.name} href={t.url} target="_blank" rel="noopener noreferrer" className="tag" style={{ height: '22px', fontSize: '10.5px', textDecoration: 'none', color: 'inherit', cursor: 'pointer' }}>{t.name}</a>
                ))}
              </div>
            </div>
            <div>
              <div style={{ fontSize: '11px', color: 'var(--muted)', fontWeight: '500', marginBottom: '6px' }}>Infrastructure & Utilities</div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
                {techStack.infrastructure.map(t => (
                  <a key={t.name} href={t.url} target="_blank" rel="noopener noreferrer" className="tag" style={{ height: '22px', fontSize: '10.5px', textDecoration: 'none', color: 'inherit', cursor: 'pointer' }}>{t.name}</a>
                ))}
              </div>
            </div>
          </div>
        </div>

        {/* Contact Info Card */}
        <div style={{ marginBottom: '16px' }}>
          <div style={{ fontSize: '12.5px', color: 'var(--muted)', marginBottom: '8px', paddingLeft: '4px', fontStyle: 'italic' }}>
            Just in case you want to contact me:
          </div>
          <div style={{ 
            background: 'var(--panel)', 
            border: '1px solid var(--border)', 
            borderRadius: '12px', 
            padding: '20px 24px', 
            boxShadow: 'var(--shadow-sm)',
            display: 'flex',
            flexDirection: 'column',
            gap: '12px'
          }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
              <span style={{ fontWeight: '600', width: '80px', color: 'var(--text-soft)', fontSize: '13px' }}>Discord:</span>
              <span className="mono" style={{ color: 'var(--text)', background: 'var(--panel-active)', padding: '4px 8px', borderRadius: '4px', fontSize: '12.5px' }}>kureist</span>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
              <span style={{ fontWeight: '600', width: '80px', color: 'var(--text-soft)', fontSize: '13px' }}>Email:</span>
              <span className="mono" style={{ color: 'var(--text)', background: 'var(--panel-active)', padding: '4px 8px', borderRadius: '4px', fontSize: '12.5px' }}>kurath0307@gmail.com</span>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
              <span style={{ fontWeight: '600', width: '80px', color: 'var(--text-soft)', fontSize: '13px' }}>QQ:</span>
              <span className="mono" style={{ color: 'var(--text)', background: 'var(--panel-active)', padding: '4px 8px', borderRadius: '4px', fontSize: '12.5px' }}>865903566</span>
            </div>
          </div>
        </div>
        
        {/* Footer Copyright */}
        <div style={{ marginTop: '40px', paddingTop: '20px', borderTop: '1px solid var(--border)', textAlign: 'center', fontSize: '11px', color: 'var(--muted)', fontFamily: 'var(--font-sans)' }}>
          Fangame Archive - Developer & Designer © Kureist 2026
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { ic, Sidebar, Toasts, Lightbox, Drawer, BrandMark, DonationView, LinksView, ContactView });
