/* global React, ReactDOM */
// Clickable prototype — FG Wallet 2.0 + EVM (Ethereum / Base).
// Single state machine. Reuses FG design tokens from the storyboard.
//
// Flow:
//   home → assetDetail (ETH) → [pick chain]
//     ↳ Ethereum → sendInput(eth) → sendConfirm(eth) → sent(eth)
//     ↳ Base     → sendInput(base) → sendConfirm(base) → sent(base)
//   Side branches: receive (per chain), unsupported (modal)

const { useState, useEffect, useRef } = React;

const FG = {
  ink: '#1a2f4c', ink2: '#4a5a76', muted: '#9aa4b8',
  line: '#e5eaf2', paper: '#ffffff', paper2: '#f4f7fb',
  brandMint: '#27d6b1', brandBlue: '#3a86ff', brandDeep: '#1e3fbf',
  sans: '-apple-system, "SF Pro", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif',
  mono: '"SF Mono", ui-monospace, Menlo, Consolas, monospace',
};

// ─── Chrome ────────────────────────────────────────────────
function StatusBar() {
  return (
    <div style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'center',
      padding: '14px 28px 8px', fontFamily: FG.sans, fontSize: 15, fontWeight: 600,
      color: FG.ink, position: 'relative', zIndex: 5,
    }}>
      <span>17:07</span>
      <div style={{ width: 90, height: 24, background: '#000', borderRadius: 14 }} />
      <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
        <div style={{ display: 'flex', gap: 2 }}>
          {[3,4,5,6].map(h => <div key={h} style={{ width: 2, height: h, background: FG.muted, borderRadius: 1 }} />)}
        </div>
        <svg width="14" height="10" viewBox="0 0 14 10"><path d="M7 1 A8 8 0 0 1 13 9 L1 9 A8 8 0 0 1 7 1Z" fill={FG.ink}/></svg>
        <div style={{ width: 20, height: 9, border: `1px solid ${FG.ink}`, borderRadius: 2, background: FG.ink }} />
      </div>
    </div>
  );
}

function NavBar({ title, onBack, right }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '4px 14px 10px', minHeight: 36, position: 'relative', zIndex: 5,
    }}>
      <button
        onClick={onBack}
        disabled={!onBack}
        style={{
          width: 36, height: 36, border: 'none', background: 'transparent',
          fontSize: 24, color: onBack ? FG.ink : 'transparent', cursor: onBack ? 'pointer' : 'default',
          fontWeight: 500, lineHeight: 1, padding: 0,
        }}
      >‹</button>
      <div style={{ flex: 1, textAlign: 'center', fontSize: 16, fontWeight: 700, color: FG.ink }}>
        {title}
      </div>
      <div style={{ width: 36, textAlign: 'right' }}>{right}</div>
    </div>
  );
}

function PrimaryButton({ children, onClick, disabled, style = {} }) {
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      style={{
        width: '100%', padding: '15px 18px', borderRadius: 12,
        background: disabled ? '#a8c1eb' : FG.brandBlue, color: '#fff',
        border: 'none', fontSize: 15, fontWeight: 700,
        fontFamily: FG.sans, cursor: disabled ? 'not-allowed' : 'pointer',
        ...style,
      }}
    >{children}</button>
  );
}

function SecondaryButton({ children, onClick, style = {} }) {
  return (
    <button
      onClick={onClick}
      style={{
        width: '100%', padding: '15px 18px', borderRadius: 12,
        background: '#fff', color: FG.ink,
        border: `1.5px solid ${FG.line}`, fontSize: 15, fontWeight: 600,
        fontFamily: FG.sans, cursor: 'pointer',
        ...style,
      }}
    >{children}</button>
  );
}

function BottomTabs({ active = 'home', onChange }) {
  const tabs = [
    { id: 'home', label: 'Home', icon: '⌂' },
    { id: 'tx', label: 'Transaction', icon: '↻' },
    { id: 'chart', label: 'Chart', icon: '⊿' },
    { id: 'set', label: 'Setting', icon: '⚙' },
  ];
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0,
      display: 'flex', justifyContent: 'space-around',
      padding: '10px 0 28px', background: '#fff',
      borderTop: `1px solid ${FG.line}`,
    }}>
      {tabs.map(t => (
        <button
          key={t.id}
          onClick={() => onChange && onChange(t.id)}
          style={{
            background: 'none', border: 'none', cursor: 'pointer', padding: 0,
            textAlign: 'center',
            color: t.id === active ? FG.brandBlue : FG.muted,
            fontWeight: t.id === active ? 700 : 500,
            fontFamily: FG.sans,
          }}
        >
          <div style={{ fontSize: 18 }}>{t.icon}</div>
          <div style={{ fontSize: 10, marginTop: 2 }}>{t.label}</div>
        </button>
      ))}
    </div>
  );
}

// ─── Home backdrop ─────────────────────────────────────────
function HomeBackdrop({ children }) {
  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: `
        radial-gradient(ellipse at 100% 100%, #2a5aff 0%, transparent 55%),
        radial-gradient(ellipse at 0% 20%, #a8f3d9 0%, transparent 60%),
        linear-gradient(180deg, #a4e8d8 0%, #d9f3e8 35%, #ffffff 55%)
      `,
    }}>{children}</div>
  );
}

function FGHeader() {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '6px 20px 14px', position: 'relative', zIndex: 5,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
        <div style={{
          width: 26, height: 26,
          background: `linear-gradient(135deg, ${FG.brandBlue} 0%, #6ba7ff 100%)`,
          transform: 'rotate(45deg)', borderRadius: 4,
        }} />
        <div style={{ fontSize: 22, fontWeight: 800, letterSpacing: '-0.02em', color: FG.ink }}>
          <span style={{ color: FG.brandBlue }}>F</span>
          <span style={{ background: `linear-gradient(90deg, ${FG.brandBlue}, ${FG.brandMint})`, WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent' }}>G</span>
          wallet
        </div>
      </div>
      <div style={{
        width: 36, height: 36, borderRadius: 18,
        background: '#fff', border: `1px solid ${FG.line}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        fontSize: 14, color: FG.ink,
      }}>🔔</div>
    </div>
  );
}

function HomeActionRow({ onSend, onReceive }) {
  return (
    <div style={{
      margin: '0 16px 14px', padding: '12px 10px', borderRadius: 16,
      background: '#fff', border: `1px solid ${FG.line}`,
      display: 'flex', justifyContent: 'space-around',
    }}>
      {[
        ['↑', 'Send',    onSend],
        ['↓', 'Receive', onReceive],
        ['◎', 'G3 NFT',  null],
      ].map(([ic, lb, on]) => (
        <button key={lb}
          onClick={on || undefined}
          style={{
            background: 'none', border: 'none', padding: 0, cursor: on ? 'pointer' : 'default',
            textAlign: 'center', fontFamily: FG.sans,
          }}>
          <div style={{
            width: 40, height: 40, borderRadius: 20, background: FG.brandMint,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            color: '#fff', fontSize: 16, fontWeight: 700, margin: '0 auto 4px',
          }}>{ic}</div>
          <div style={{ fontSize: 11, color: FG.ink, fontWeight: 500 }}>{lb}</div>
        </button>
      ))}
    </div>
  );
}

// ─── Asset rows ───────────────────────────────────────────
function AssetRow({ icon, sym, label, sub, amt = '0', subAmt = '0.0 JPY', highlighted, onClick }) {
  return (
    <button
      onClick={onClick}
      style={{
        position: 'relative', width: '100%',
        padding: '12px 14px', borderRadius: 14,
        background: highlighted ? '#e0f1fd' : FG.paper2,
        display: 'flex', alignItems: 'center', gap: 12,
        marginBottom: 8, border: 'none', cursor: onClick ? 'pointer' : 'default',
        fontFamily: FG.sans, textAlign: 'left',
        transition: 'transform 0.08s',
      }}
      onMouseDown={e => { if (onClick) e.currentTarget.style.transform = 'scale(0.99)'; }}
      onMouseUp={e => { e.currentTarget.style.transform = 'scale(1)'; }}
      onMouseLeave={e => { e.currentTarget.style.transform = 'scale(1)'; }}
    >
      <div style={{
        width: 38, height: 38, borderRadius: 19,
        background: '#fff', border: `1px solid ${FG.line}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        fontSize: 16, flexShrink: 0, fontWeight: 700, color: FG.ink2,
      }}>{icon}</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink }}>
          {sym} <span style={{ color: FG.ink, fontWeight: 500 }}>({label})</span>
        </div>
        <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>{sub}</div>
      </div>
      <div style={{ textAlign: 'right' }}>
        <div style={{ fontSize: 15, fontWeight: 700, color: FG.ink }}>{amt}</div>
        <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>{subAmt}</div>
      </div>
    </button>
  );
}

// USDT — MODE A: flat chain-separated rows.
function USDTFlatRows() {
  const chains = [
    ['Ethereum',  '120.40 USDT'],
    ['Base',      '85.10 USDT'],
    ['BSC',       '54.00 USDT'],
    ['Arbitrum',  '40.22 USDT'],
    ['TRON',      '210.00 USDT'],
  ];
  return (
    <>
      {chains.map(([c, bal]) => (
        <button key={c} style={{
          width: '100%', padding: '12px 14px', borderRadius: 14,
          background: FG.paper2, marginBottom: 8, border: 'none',
          display: 'flex', alignItems: 'center', gap: 12,
          fontFamily: FG.sans, textAlign: 'left', cursor: 'pointer',
        }}>
          <div style={{
            width: 38, height: 38, borderRadius: 19,
            background: '#26a17b', color: '#fff',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 14, fontWeight: 800,
          }}>₮</div>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink }}>
              USDT <span style={{ color: FG.ink, fontWeight: 500 }}>({c})</span>
            </div>
            <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>残高　{bal}</div>
          </div>
          <div style={{ fontSize: 13, fontWeight: 700, color: FG.ink }}>{bal.split(' ')[0]}</div>
        </button>
      ))}
    </>
  );
}

// USDT — MODE B: grouped asset-first card.
function USDTGroupedRow() {
  const chains = [
    ['Ethereum',  '120.40'],
    ['Base',      '85.10'],
    ['BSC',       '54.00'],
    ['Arbitrum',  '40.22'],
    ['TRON',      '210.00'],
  ];
  return (
    <button style={{
      position: 'relative', width: '100%',
      padding: '12px 14px 10px', borderRadius: 14,
      background: '#fff', marginBottom: 8,
      border: `1.5px solid #26a17b`,
      boxShadow: '0 1px 8px rgba(38,161,123,0.12)',
      cursor: 'pointer', fontFamily: FG.sans, textAlign: 'left',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        <div style={{
          width: 38, height: 38, borderRadius: 19,
          background: '#26a17b', color: '#fff',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 14, fontWeight: 800,
        }}>₮</div>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink }}>USDT</div>
          <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>対応: Ethereum, Base, BSC, Arbitrum, TRON</div>
        </div>
        <div style={{ textAlign: 'right' }}>
          <div style={{ fontSize: 15, fontWeight: 700, color: FG.ink }}>509.72</div>
          <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>USDT</div>
        </div>
      </div>
      <div style={{ marginTop: 10, paddingLeft: 50, borderTop: `1px dashed ${FG.line}`, paddingTop: 8 }}>
        {chains.map(([c, v], i) => (
          <div key={c} style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            padding: '3px 0',
            borderBottom: i < chains.length - 1 ? `1px dotted ${FG.line}` : 'none',
          }}>
            <span style={{ fontSize: 12, color: FG.ink2 }}>· {c}</span>
            <span style={{ fontSize: 12, color: FG.ink2, fontWeight: 600 }}>{v} USDT</span>
          </div>
        ))}
      </div>
    </button>
  );
}

function ModeBadge({ mode }) {
  const isA = mode === 'a';
  return (
    <div style={{
      position: 'absolute', top: 50, left: 20,
      display: 'flex', gap: 6, zIndex: 30,
      fontFamily: FG.mono, fontSize: 10, fontWeight: 700, letterSpacing: '0.08em',
    }}>
      <div style={{
        padding: '3px 9px', borderRadius: 3,
        background: isA ? '#1a1a1a' : 'rgba(0,0,0,0.18)',
        color: '#fff',
      }}>MODE {mode.toUpperCase()}</div>
      <div style={{
        padding: '3px 9px', borderRadius: 3,
        background: 'rgba(255,255,255,0.85)', color: FG.ink,
      }}>{isA ? 'FLAT — chain-separated' : 'GROUPED — asset-first'}</div>
    </div>
  );
}

function ETHFlatRows({ onClick }) {
  const chains = [
    ['Ethereum', '0.25'],
    ['Base',     '0.080'],
  ];
  return (
    <>
      {chains.map(([c, bal]) => (
        <button key={c} onClick={() => onClick && onClick(c)} style={{
          width: '100%', padding: '12px 14px', borderRadius: 14,
          background: FG.paper2, marginBottom: 8, border: 'none',
          display: 'flex', alignItems: 'center', gap: 12,
          fontFamily: FG.sans, textAlign: 'left', cursor: 'pointer',
        }}>
          <div style={{
            width: 38, height: 38, borderRadius: 19,
            background: '#fff', border: `1px solid ${FG.line}`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 16, fontWeight: 700, color: FG.ink2,
          }}>Ξ</div>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink }}>
              ETH <span style={{ color: FG.ink, fontWeight: 500 }}>({c})</span>
            </div>
            <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>残高　{bal} ETH</div>
          </div>
          <div style={{ fontSize: 13, fontWeight: 700, color: FG.ink }}>{bal}</div>
        </button>
      ))}
    </>
  );
}

function ETHGroupedRow({ onClick }) {
  return (
    <button
      onClick={onClick}
      style={{
        position: 'relative', width: '100%',
        padding: '12px 14px 10px', borderRadius: 14,
        background: '#fff', marginBottom: 8,
        border: `1.5px solid ${FG.brandBlue}`,
        boxShadow: '0 1px 8px rgba(58,134,255,0.12)',
        cursor: 'pointer', fontFamily: FG.sans, textAlign: 'left',
      }}
    >
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        <div style={{
          width: 38, height: 38, borderRadius: 19,
          background: '#fff', border: `1px solid ${FG.line}`,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 16, fontWeight: 700, color: FG.ink2,
        }}>Ξ</div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink, display: 'flex', alignItems: 'center', gap: 6 }}>
            ETH
            <span style={{
              fontSize: 9, fontFamily: FG.mono, fontWeight: 700, letterSpacing: '0.05em',
              background: FG.brandBlue, color: '#fff', padding: '2px 6px', borderRadius: 3,
            }}>EVM</span>
          </div>
          <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>対応: Ethereum, Base</div>
        </div>
        <div style={{ textAlign: 'right' }}>
          <div style={{ fontSize: 15, fontWeight: 700, color: FG.ink }}>0.33</div>
          <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>—</div>
        </div>
      </div>
      <div style={{ marginTop: 10, paddingLeft: 50, borderTop: `1px dashed ${FG.line}`, paddingTop: 8 }}>
        {[['Ethereum', '0.25 ETH'], ['Base', '0.080 ETH']].map(([n, v], i) => (
          <div key={n} style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            padding: '3px 0',
            borderBottom: i === 0 ? `1px dotted ${FG.line}` : 'none',
          }}>
            <span style={{ fontSize: 12, color: FG.ink2 }}>· {n}</span>
            <span style={{ fontSize: 12, color: FG.ink2, fontWeight: 600 }}>{v}</span>
          </div>
        ))}
      </div>
      <div style={{
        position: 'absolute', top: -7, right: 12,
        background: '#2e7d4f', color: '#fff',
        fontSize: 9, fontFamily: FG.mono, fontWeight: 700,
        padding: '2px 7px', borderRadius: 3, letterSpacing: '0.05em',
      }}>NEW</div>
    </button>
  );
}

// ═══════════════════════════════════════════════════════════
// SCREENS
// ═══════════════════════════════════════════════════════════

function HomeScreen({ go, mode }) {
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', fontFamily: FG.sans }}>
      <HomeBackdrop>
        <StatusBar />
        <FGHeader />

        <div style={{ textAlign: 'center', padding: '0 20px 12px' }}>
          <div style={{
            display: 'inline-flex', alignItems: 'center', gap: 6,
            padding: '5px 14px', borderRadius: 16,
            background: '#fff', border: `1px solid ${FG.line}`,
            fontSize: 11, color: FG.ink, fontWeight: 600,
          }}>
            <span>◎</span> Total Asset
          </div>
          <div style={{ fontSize: 30, fontWeight: 700, color: FG.ink, marginTop: 6 }}>
            — JPY
          </div>
        </div>

        <HomeActionRow />

        <div style={{ overflowY: 'auto', maxHeight: 'calc(100% - 300px)', paddingBottom: 90 }}>
          <div style={{ padding: '4px 16px 6px', fontSize: 10, fontFamily: FG.mono, color: FG.ink2, letterSpacing: '0.06em' }}>
            資産一覧
          </div>
          <div style={{ padding: '0 16px' }}>
            <AssetRow icon="🔶" sym="G3"    label="Jetton"     sub="0.9777 JPY"      highlighted />
            <AssetRow icon="₿"  sym="BTC"   label="Bitcoin"    sub="12,424,531 JPY" />
            {mode === 'a' ? <USDTFlatRows /> : <USDTGroupedRow />}
            {mode === 'a' ? <ETHFlatRows onClick={(chain) => go('sendInput', { chain })} /> : <ETHGroupedRow onClick={() => go('assetDetail')} />}
          </div>
        </div>

        <BottomTabs active="home" />
      </HomeBackdrop>

      <ModeBadge mode={mode} />
    </div>
  );
}

function HintBubble({ children }) {
  // Persistent hand-drawn hint anchored bottom-center
  return (
    <div style={{
      position: 'absolute', bottom: 100, left: '50%', transform: 'translateX(-50%)',
      background: '#1a1a1a', color: '#fff',
      fontFamily: FG.mono, fontSize: 10, fontWeight: 700,
      padding: '5px 10px', borderRadius: 3, letterSpacing: '0.05em',
      pointerEvents: 'none', zIndex: 30, whiteSpace: 'nowrap',
      animation: 'fghint 1.8s ease-in-out infinite',
    }}>{children}</div>
  );
}

function AssetDetailETH({ go }) {
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', background: '#fff', overflow: 'hidden', fontFamily: FG.sans }}>
      <StatusBar />
      <NavBar title="ETH" onBack={() => go('home')} />

      <div style={{ textAlign: 'center', padding: '4px 20px 18px' }}>
        <div style={{
          width: 46, height: 46, borderRadius: 23,
          background: FG.paper2, border: `1px solid ${FG.line}`,
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 20, fontWeight: 700, color: FG.ink2,
        }}>Ξ</div>
        <div style={{ fontSize: 28, fontWeight: 800, color: FG.ink, marginTop: 10 }}>
          0.33 ETH
        </div>
        <div style={{ fontSize: 13, color: FG.muted, marginTop: 2 }}>≈ — JPY</div>
      </div>

      <div style={{ padding: '0 20px' }}>
        <div style={{
          fontSize: 10, fontFamily: FG.mono, color: FG.ink2,
          letterSpacing: '0.1em', textTransform: 'uppercase', marginBottom: 8,
        }}>
          ネットワークを選択
        </div>

        <ChainCard
          name="Ethereum"
          icon="Ξ"
          balance="0.25 ETH"
          tone="#627eea"
          onClick={() => go('sendInput', { chain: 'Ethereum' })}
          onReceive={() => go('receive', { chain: 'Ethereum' })}
        />
        <ChainCard
          name="Base"
          icon="Ξ"
          balance="0.080 ETH"
          tone="#0052ff"
          onClick={() => go('sendInput', { chain: 'Base' })}
          onReceive={() => go('receive', { chain: 'Base' })}
        />

        <div style={{
          marginTop: 18, padding: '10px 12px', borderRadius: 8,
          background: FG.paper2, border: `1px dashed ${FG.line}`,
          fontSize: 11, color: FG.ink2, lineHeight: 1.5,
        }}>
          💡 ETH はネットワークごとに残高が分かれます。送るときは、宛先と同じネットワークを選んでください。
        </div>
      </div>

      <FooterStrip>
        プロト：ここでチェーンが <b>文脈的に</b> 確定 — グローバル切替なし
      </FooterStrip>
    </div>
  );
}

function ChainCard({ name, icon, balance, tone, onClick, onReceive }) {
  return (
    <div style={{
      marginBottom: 12, borderRadius: 14, background: '#fff',
      border: `1px solid ${FG.line}`, overflow: 'hidden',
    }}>
      <button
        onClick={onClick}
        style={{
          width: '100%', padding: '14px 14px', display: 'flex', alignItems: 'center', gap: 12,
          background: 'transparent', border: 'none', cursor: 'pointer', textAlign: 'left',
          fontFamily: FG.sans,
        }}
      >
        <div style={{
          width: 40, height: 40, borderRadius: 20, background: tone,
          color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 16, fontWeight: 700,
        }}>{icon}</div>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 15, fontWeight: 700, color: FG.ink }}>ETH（{name}）</div>
          <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>残高　{balance}</div>
        </div>
        <div style={{ fontSize: 20, color: FG.muted, fontWeight: 300 }}>›</div>
      </button>
      <div style={{ display: 'flex', borderTop: `1px solid ${FG.line}` }}>
        <button
          onClick={onClick}
          style={{
            flex: 1, padding: '10px 0', background: 'transparent', border: 'none',
            color: FG.brandBlue, fontWeight: 700, fontSize: 13, cursor: 'pointer',
            borderRight: `1px solid ${FG.line}`, fontFamily: FG.sans,
          }}
        >↑ 送る</button>
        <button
          onClick={onReceive}
          style={{
            flex: 1, padding: '10px 0', background: 'transparent', border: 'none',
            color: FG.brandBlue, fontWeight: 700, fontSize: 13, cursor: 'pointer',
            fontFamily: FG.sans,
          }}
        >↓ 受け取る</button>
      </div>
    </div>
  );
}

function SendInput({ chain, go }) {
  const [address, setAddress] = useState(chain === 'Ethereum' ? '0x7f3d…Ab92' : '0x3c1a…ff01');
  const [amount,  setAmount]  = useState(chain === 'Ethereum' ? '0.25' : '0.080');
  const fee = chain === 'Ethereum' ? '0.0008' : '0.00004';

  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', background: '#fff', overflow: 'hidden', fontFamily: FG.sans }}>
      <StatusBar />
      <NavBar title="送る" onBack={() => go('assetDetail')} />

      <div style={{ padding: '4px 20px', overflowY: 'auto', height: 'calc(100% - 80px)', paddingBottom: 30 }}>
        {/* Asset header */}
        <div style={{
          display: 'flex', alignItems: 'center', gap: 12,
          padding: '12px 14px', borderRadius: 14,
          background: FG.paper2, border: `1px solid ${FG.line}`,
          marginBottom: 12,
        }}>
          <div style={{
            width: 40, height: 40, borderRadius: 20,
            background: '#fff', border: `1px solid ${FG.line}`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 18, fontWeight: 700, color: FG.ink2,
          }}>Ξ</div>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 14, fontWeight: 700, color: FG.ink }}>
              ETH <span style={{ color: FG.ink, fontWeight: 500 }}>({chain})</span>
            </div>
            <div style={{ fontSize: 11, color: FG.muted, marginTop: 2 }}>
              残高　{chain === 'Ethereum' ? '0.25 ETH' : '0.080 ETH'}
            </div>
          </div>
        </div>

        {/* Network lock */}
        <div style={{
          padding: '10px 14px', borderRadius: 12,
          border: `1.5px solid ${FG.brandBlue}`, background: '#eef4ff',
          display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          marginBottom: 14,
        }}>
          <span style={{ fontSize: 11, color: FG.ink2, fontWeight: 600 }}>ネットワーク</span>
          <span style={{ fontSize: 14, color: FG.ink, fontWeight: 700 }}>{chain} 🔒</span>
        </div>

        {/* Destination */}
        <div style={{ marginBottom: 12 }}>
          <div style={{ fontSize: 11, color: FG.muted, marginBottom: 6 }}>宛先</div>
          <input
            value={address}
            onChange={e => setAddress(e.target.value)}
            style={{
              width: '100%', padding: '14px', borderRadius: 12,
              border: `1px solid ${FG.line}`, background: '#fff',
              fontFamily: FG.mono, fontSize: 12, color: FG.ink2,
              boxSizing: 'border-box',
            }}
          />
        </div>

        {/* Amount */}
        <div style={{ marginBottom: 12 }}>
          <div style={{ fontSize: 11, color: FG.muted, marginBottom: 6 }}>金額</div>
          <div style={{
            padding: '10px 14px', borderRadius: 12,
            border: `1px solid ${FG.line}`, background: '#fff',
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          }}>
            <input
              value={amount}
              onChange={e => setAmount(e.target.value)}
              style={{
                flex: 1, border: 'none', outline: 'none', background: 'transparent',
                fontSize: 24, fontWeight: 700, color: FG.ink, fontFamily: FG.sans,
              }}
            />
            <span style={{ fontSize: 13, fontWeight: 600, color: FG.ink2 }}>ETH</span>
          </div>
        </div>

        {/* Fee + Gas */}
        <div style={{
          background: FG.paper2, borderRadius: 12,
          border: `1px solid ${FG.line}`,
          marginBottom: 14, overflow: 'hidden',
        }}>
          <div style={{
            display: 'flex', justifyContent: 'space-between',
            padding: '10px 14px', borderBottom: `1px solid ${FG.line}`, fontSize: 12,
          }}>
            <span style={{ color: FG.muted }}>手数料（概算）</span>
            <span style={{ color: FG.ink, fontWeight: 600 }}>{fee} ETH</span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', padding: '10px 14px', fontSize: 12 }}>
            <span style={{ color: FG.muted }}>必要なガス</span>
            <span style={{ color: FG.ink, fontWeight: 700 }}>ETH（{chain}）</span>
          </div>
        </div>

        <PrimaryButton onClick={() => go('sendConfirm', { chain, address, amount, fee })}>
          確認
        </PrimaryButton>
      </div>

      <FooterStrip>
        プロト：ネットワーク行と「必要なガス」を <b>必ず</b> 表示
      </FooterStrip>
    </div>
  );
}

function SendConfirm({ chain, address, amount, fee, go }) {
  const rows = [
    ['1', 'ネットワーク',     chain,              false, true],
    ['2', '必要なガス',       `ETH（${chain}）`,  false, true],
    ['3', '宛先',             address,            true,  false],
    ['4', '金額',             `${amount} ETH`,    false, false],
    ['5', '手数料（概算）',   `${fee} ETH（${chain}）`, false, false],
  ];
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', background: '#fff', overflow: 'hidden', fontFamily: FG.sans }}>
      <StatusBar />
      <NavBar title="確認" onBack={() => go('sendInput', { chain })} />

      <div style={{ padding: '4px 20px', overflowY: 'auto', height: 'calc(100% - 80px)', paddingBottom: 30 }}>
        <div style={{
          padding: '12px 14px', borderRadius: 12,
          background: '#eef4ff', border: `1.5px solid ${FG.brandBlue}`,
          textAlign: 'center', marginBottom: 14,
        }}>
          <div style={{ fontSize: 10, color: FG.muted, letterSpacing: '0.12em', fontFamily: FG.mono }}>
            このトランザクションのネットワーク
          </div>
          <div style={{ fontSize: 24, fontWeight: 800, color: FG.ink, marginTop: 4 }}>
            {chain}
          </div>
        </div>

        <div style={{
          background: '#fff', borderRadius: 14,
          border: `1px solid ${FG.line}`, marginBottom: 14, overflow: 'hidden',
        }}>
          {rows.map(([n, k, v, mono, emph], i) => (
            <div key={n} style={{
              display: 'flex', justifyContent: 'space-between', alignItems: 'center',
              padding: '13px 16px',
              borderBottom: i < rows.length - 1 ? `1px solid ${FG.line}` : 'none',
              background: emph ? FG.paper2 : '#fff',
            }}>
              <span style={{
                color: emph ? FG.ink : FG.muted, fontSize: 12,
                fontWeight: emph ? 700 : 500,
              }}>
                <span style={{
                  display: 'inline-block', width: 18, color: FG.muted,
                  fontFamily: FG.mono, fontSize: 10,
                }}>{n}.</span>
                {k}
              </span>
              <span style={{
                color: FG.ink, fontSize: 13,
                fontWeight: emph ? 800 : 600,
                fontFamily: mono ? FG.mono : FG.sans,
              }}>{v}</span>
            </div>
          ))}
        </div>

        <div style={{
          padding: '10px 12px', borderRadius: 8,
          background: '#fdf5dc', border: '1px solid #b8860b',
          fontSize: 11, color: '#7a5a00', lineHeight: 1.5, marginBottom: 14,
        }}>
          このトランザクションには <b>ETH（{chain}）</b> が必要です。他のネットワークの ETH では支払えません。
        </div>

        <div style={{ display: 'flex', gap: 10 }}>
          <SecondaryButton onClick={() => go('sendInput', { chain })} style={{ flex: 1 }}>戻る</SecondaryButton>
          <PrimaryButton onClick={() => go('sent', { chain, address, amount, fee })} style={{ flex: 2 }}>送る</PrimaryButton>
        </div>
      </div>

      <FooterStrip>
        プロト：順序 ① ネットワーク → ② ガス → ③ 宛先 → ④ 金額 → ⑤ 手数料
      </FooterStrip>
    </div>
  );
}

function Sent({ chain, amount, go }) {
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', background: '#fff', overflow: 'hidden', fontFamily: FG.sans }}>
      <StatusBar />
      <NavBar title="" />

      <div style={{
        flex: 1, height: 'calc(100% - 200px)',
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
        padding: '0 30px', textAlign: 'center',
      }}>
        <div style={{
          width: 80, height: 80, borderRadius: 40,
          background: '#e4f1e8', border: '2px solid #2e7d4f',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 40, color: '#2e7d4f', marginBottom: 20,
        }}>✓</div>
        <div style={{ fontSize: 22, fontWeight: 800, color: FG.ink, marginBottom: 8 }}>
          送信を受け付けました
        </div>
        <div style={{ fontSize: 14, color: FG.ink2, lineHeight: 1.6 }}>
          {amount} ETH を <b>{chain}</b> で送信中です。<br/>
          残高はネットワーク確定後に更新されます。
        </div>
      </div>

      <div style={{ padding: '0 20px 30px' }}>
        <PrimaryButton onClick={() => go('home')}>ホームに戻る</PrimaryButton>
        <div style={{ height: 8 }} />
        <SecondaryButton onClick={() => go('assetDetail')}>続けて操作する</SecondaryButton>
      </div>

      <FooterStrip>
        プロト：送信後もネットワーク名を必ず読み上げる
      </FooterStrip>
    </div>
  );
}

function Receive({ chain, go }) {
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', background: '#fff', overflow: 'hidden', fontFamily: FG.sans }}>
      <StatusBar />
      <NavBar title="受け取る" onBack={() => go('assetDetail')} />

      <div style={{ padding: '0 20px' }}>
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 14 }}>
          <div style={{
            padding: '6px 14px', borderRadius: 14,
            background: FG.paper2, border: `1px solid ${FG.line}`,
            fontSize: 12, fontWeight: 700, color: FG.ink,
          }}>⟡ ネットワーク：{chain}</div>
        </div>

        <div style={{
          background: '#fff', borderRadius: 16, padding: 16,
          border: `1px solid ${FG.line}`, marginBottom: 10,
        }}>
          <div style={{
            width: 180, height: 180, margin: '0 auto 12px', borderRadius: 8,
            background: `repeating-linear-gradient(0deg, ${FG.ink} 0 3px, #fff 3px 7px), repeating-linear-gradient(90deg, ${FG.ink} 0 3px, #fff 3px 7px)`,
            backgroundBlendMode: 'multiply',
            position: 'relative',
          }}>
            <div style={{
              position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)',
              width: 40, height: 40, background: '#fff',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
            }}>
              <div style={{ width: 26, height: 26, background: FG.brandBlue, transform: 'rotate(45deg)', borderRadius: 3 }} />
            </div>
          </div>
          <div style={{ textAlign: 'center', fontSize: 11, color: FG.muted, marginBottom: 6 }}>
            {chain} 受取アドレス
          </div>
          <div style={{
            padding: '10px 12px', borderRadius: 8,
            background: FG.paper2, border: `1px solid ${FG.line}`,
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          }}>
            <div style={{ fontFamily: FG.mono, fontSize: 11, color: FG.ink2 }}>
              0x7f3d…Ab92
            </div>
            <button style={{
              background: 'none', border: 'none', color: FG.brandBlue,
              fontWeight: 600, fontSize: 12, cursor: 'pointer', fontFamily: FG.sans,
            }}>Copy</button>
          </div>
        </div>

        <div style={{
          padding: 10, borderRadius: 8, fontSize: 11, lineHeight: 1.5,
          background: '#fdf5dc', border: '1px solid #b8860b', color: '#7a5a00',
          marginTop: 10,
        }}>
          ⚠ 送信者には「<b>{chain}</b>」で送るよう伝えてください。
        </div>

        <div style={{ marginTop: 14 }}>
          <SecondaryButton onClick={() => go('unsupported')}>
            （プロト用）未対応チェーンの例を見る
          </SecondaryButton>
        </div>
      </div>

      <FooterStrip>
        プロト：受け取りもチェーンが文脈的に確定
      </FooterStrip>
    </div>
  );
}

function UnsupportedModal({ go }) {
  return (
    <div style={{
      position: 'relative', width: '100%', height: '100%',
      background: 'rgba(10,20,40,0.5)', overflow: 'hidden', fontFamily: FG.sans,
    }}>
      <StatusBar />
      <div style={{ padding: '0 20px', opacity: 0.25 }}>
        <div style={{ height: 14, background: '#fff', borderRadius: 4, marginBottom: 14 }} />
        <div style={{ height: 80, background: '#fff', borderRadius: 12, marginBottom: 10 }} />
        <div style={{ height: 120, background: '#fff', borderRadius: 12 }} />
      </div>

      <div style={{
        position: 'absolute', bottom: 0, left: 0, right: 0,
        background: '#fff', borderRadius: '20px 20px 0 0',
        padding: '14px 22px 32px',
        boxShadow: '0 -8px 24px rgba(0,0,0,0.15)',
      }}>
        <div style={{ width: 40, height: 4, background: FG.line, borderRadius: 2, margin: '0 auto 16px' }} />
        <div style={{ textAlign: 'center', padding: '0 10px' }}>
          <div style={{
            width: 56, height: 56, borderRadius: 28,
            background: '#fbe9ea', border: '1.5px solid #c1272d',
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 28, color: '#c1272d', marginBottom: 12,
          }}>⊘</div>
          <div style={{ fontSize: 18, fontWeight: 700, color: FG.ink, marginBottom: 8 }}>
            このネットワークはまだ使えません
          </div>
          <div style={{ fontSize: 13, color: FG.ink2, lineHeight: 1.6, marginBottom: 14 }}>
            「Polygon」での操作を求められましたが、<br/>
            現在 FG Wallet で使えるのは次のとおりです：
          </div>
          <div style={{
            background: FG.paper2, borderRadius: 10, padding: '10px 14px',
            fontSize: 13, color: FG.ink, marginBottom: 16,
          }}>
            <b>Ethereum</b>　／　<b>Base</b>
          </div>
        </div>
        <PrimaryButton onClick={() => go('receive', { chain: 'Ethereum' })}>閉じる</PrimaryButton>
      </div>

      <FooterStrip>
        プロト：未対応チェーンは曖昧な失敗にしない
      </FooterStrip>
    </div>
  );
}

function CompareView({ openSingle }) {
  // Read-only side-by-side. Both phones render the same dataset.
  const noop = () => {};
  return (
    <div className="cmp-root">
      <div className="cmp-head">
        <div className="cmp-h-title">A / B サイド・バイ・サイド</div>
        <div className="cmp-h-sub">同じデータセット（G3 / BTC / USDT×5 / ETH×2）を 2 つの構造で見せる</div>
        <div className="cmp-share">
          <code>?mode=a</code> <span>= A 単体ピュア</span>
          <code>?mode=b</code> <span>= B 単体ピュア</span>
          <button className="cmp-link" onClick={() => openSingle('a')}>A プロトを開く →</button>
          <button className="cmp-link" onClick={() => openSingle('b')}>B プロトを開く →</button>
        </div>
      </div>

      <div className="cmp-phones">
        <div className="cmp-col">
          <div className="cmp-tag a"><b>A</b>　Flat — chain-separated rows</div>
          <IOSDevice width={402} height={874} hideStatusBar={true}>
            <HomeScreen go={noop} mode="a" />
          </IOSDevice>
          <div className="cmp-cap">9 行　｜　ラベル重複あり　｜　スクロール長い</div>
        </div>
        <div className="cmp-col">
          <div className="cmp-tag b"><b>B</b>　Grouped — asset-first cards</div>
          <IOSDevice width={402} height={874} hideStatusBar={true}>
            <HomeScreen go={noop} mode="b" />
          </IOSDevice>
          <div className="cmp-cap">4 カード　｜　チェーンは中で展開　｜　スケール可能</div>
        </div>
      </div>

      <div className="cmp-notice">
        <div><b>まず見るべきところ:</b> USDT が A では 5 行に分裂、B では 1 カードに集約されている。 ETH も同様。チェーンが増えると A は線形に長くなり、B は一定のまま。</div>
      </div>

      <div className="cmp-grid">
        <CompareCell title="今の見やすさ"          a="チェーン名も含めて一望で明確。初見でわかりやすい。"  b="チェーン明細はタップ後。Home は静か。" />
        <CompareCell title="スケーラビリティ"          a="チェーン×資産 でリストが爆発する。"               b="資産数だけ伸びる。チェーン追加はカード内部。" highlight="b" />
        <CompareCell title="実装複雑度"            a="既存の一行=一チェーンモデルのまま。小さい。"        b="集約ロジックと拡張カードが必要。やや増。" highlight="a" />
        <CompareCell title="サポート負担"            a="「USDT が 5 個ある」という問い合わせが起きる。" b="「これは 5 チェーン合計」と説明しやすい。" highlight="b" />
        <CompareCell title="将来のチェーン拡張"      a="BSC、Arbitrum を足すたびに Home が伸びる。"   b="既存カードに行が追加されるだけ。" highlight="b" />
        <CompareCell title="認知負荷"              a="同じシンボルが何度も並ぶ。読み飛ばしリスク。" b="資産がチェーンより先に考えられるモデル。" highlight="b" />
      </div>

      <div className="cmp-foot">
        <div className="cmp-foot-title">スケール后の推奨</div>
        <div className="cmp-foot-body">
          EVM チェーンが 2、3 と増えたとき、<b>B（grouped）</b> のほうが Home の長さを一定に保てる。
          A の「チェーン名明示」の価値は、カード内や Send 画面の「ネットワーク」行で取り戻せる。
          したがって<b>推奨: B を Home の骨格とし、A スタイルのチェーン被覆表示は資産詳細·Send 画面に集約する</b>。
        </div>
      </div>
    </div>
  );
}

function CompareCell({ title, a, b, highlight }) {
  return (
    <div className="cmp-cell">
      <div className="cmp-cell-t">{title}</div>
      <div className={`cmp-cell-row ${highlight === 'a' ? 'win' : ''}`}>
        <span className="k">A</span><span className="v">{a}</span>
      </div>
      <div className={`cmp-cell-row ${highlight === 'b' ? 'win' : ''}`}>
        <span className="k">B</span><span className="v">{b}</span>
      </div>
    </div>
  );
}

// ─── Footer strip (proto annotation, hide-able) ────────────
function FooterStrip({ children }) {
  return (
    <div className="proto-strip" style={{
      position: 'absolute', left: 0, right: 0, bottom: 0,
      padding: '6px 12px',
      background: 'rgba(26,26,26,0.92)', color: '#fff',
      fontFamily: FG.mono, fontSize: 10, letterSpacing: '0.04em',
      textAlign: 'center', zIndex: 50, lineHeight: 1.4,
    }}>{children}</div>
  );
}

// ═══════════════════════════════════════════════════════════
// State machine
// ═══════════════════════════════════════════════════════════

const screenIndex = ['home', 'assetDetail', 'sendInput', 'sendConfirm', 'sent', 'receive', 'unsupported'];

function App() {
  const params0 = new URLSearchParams(location.search);
  const initialModeRaw = (params0.get('mode') || '').toLowerCase();
  const isCompareView = !['a', 'b'].includes(initialModeRaw);
  const initialMode = initialModeRaw === 'b' ? 'b' : 'a';
  const [mode, setMode] = useState(initialMode);
  const [view, setView] = useState(isCompareView ? 'compare' : 'single');
  const [screen, setScreen] = useState(() => {
    return (location.hash || '').replace('#', '').split('?')[0] || 'home';
  });
  const [params, setParams] = useState({});

  const switchMode = (m) => {
    setMode(m);
    const u = new URL(location.href);
    u.searchParams.set('mode', m);
    history.replaceState(null, '', u.toString());
  };

  const go = (next, p = {}) => {
    setScreen(next);
    setParams(prev => ({ ...prev, ...p }));
    // persist
    const merged = { ...params, ...p };
    const qs = Object.entries(merged)
      .filter(([, v]) => v != null && v !== '')
      .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
    history.replaceState(null, '', `#${next}${qs ? '?' + qs : ''}`);
  };

  useEffect(() => {
    const onHash = () => {
      const h = (location.hash || '').replace('#', '');
      const [s, qs] = h.split('?');
      if (s && screenIndex.includes(s)) {
        setScreen(s);
        if (qs) {
          const p = {};
          qs.split('&').forEach(pair => {
            const [k, v] = pair.split('=');
            p[decodeURIComponent(k)] = decodeURIComponent(v || '');
          });
          setParams(prev => ({ ...prev, ...p }));
        }
      }
    };
    window.addEventListener('hashchange', onHash);
    onHash();
    return () => window.removeEventListener('hashchange', onHash);
  }, []);

  const openSingle = (m) => {
    setMode(m);
    setView('single');
    const u = new URL(location.href);
    u.searchParams.set('mode', m);
    history.pushState(null, '', u.toString());
  };

  if (view === 'compare') {
    return <CompareView openSingle={openSingle} />;
  }

  let body;
  switch (screen) {
    case 'home':         body = <HomeScreen go={go} mode={mode} />; break;
    case 'assetDetail':  body = <AssetDetailETH go={go} />; break;
    case 'sendInput':    body = <SendInput chain={params.chain || 'Ethereum'} go={go} />; break;
    case 'sendConfirm':  body = <SendConfirm
                                  chain={params.chain || 'Ethereum'}
                                  address={params.address || '0x7f3d…Ab92'}
                                  amount={params.amount || '0.25'}
                                  fee={params.fee || (params.chain === 'Base' ? '0.00004' : '0.0008')}
                                  go={go} />; break;
    case 'sent':         body = <Sent chain={params.chain || 'Ethereum'} amount={params.amount || '0.25'} go={go} />; break;
    case 'receive':      body = <Receive chain={params.chain || 'Ethereum'} go={go} />; break;
    case 'unsupported':  body = <UnsupportedModal go={go} />; break;
    default:             body = <HomeScreen go={go} />;
  }

  return (
    <div className="proto-root">
      <div className="proto-stage">
        <IOSDevice width={402} height={874} hideStatusBar={true}>
          {body}
        </IOSDevice>
      </div>
      <FlowSidebar current={screen} go={go} params={params} mode={mode} switchMode={switchMode} />
    </div>
  );
}

function FlowSidebar({ current, go, params, mode, switchMode }) {
  const aURL = `${location.pathname}?mode=a`;
  const bURL = `${location.pathname}?mode=b`;
  return (
    <div className="flow-side">
      <div className="flow-side-h">
        <div className="flow-title">A / B COMPARISON</div>
        <div className="flow-sub">FG Wallet — USDT を多チェーンでどう見せるか</div>
      </div>
      <div className="ab-row">
        <button className={`ab-btn ${mode === 'a' ? 'is-on' : ''}`} onClick={() => switchMode('a')}>
          <div className="ab-k">A</div>
          <div className="ab-t">
            <div className="ab-l">Flat — chain-separated</div>
            <div className="ab-n">USDT (Ethereum) / (Base) / (BSC)…</div>
          </div>
        </button>
        <button className={`ab-btn ${mode === 'b' ? 'is-on' : ''}`} onClick={() => switchMode('b')}>
          <div className="ab-k">B</div>
          <div className="ab-t">
            <div className="ab-l">Grouped — asset-first</div>
            <div className="ab-n">USDT → 5 chains nested</div>
          </div>
        </button>
      </div>
      <div className="share-row">
        <div className="share-l">共有 URL</div>
        <code className="share-u">?mode=a</code>
        <code className="share-u">?mode=b</code>
      </div>
      <FlowStepList current={current} go={go} params={params} />
      <div className="flow-foot">
        <b>使い方:</b> A/B を切り替えて Home の USDT の見え方を比較。Send フローは両モード共通。
      </div>
    </div>
  );
}

function FlowStepList({ current, go, params }) {
  const steps = [
    { id: 'home',        label: 'Home',                        sub: '資産一覧から ETH カードを発見',           args: {} },
    { id: 'assetDetail', label: 'Asset Detail (ETH)',          sub: 'Ethereum / Base を文脈で選択',           args: {} },
    { id: 'sendInput',   label: 'Send Input — Ethereum',       sub: 'ネットワーク行 + 必要なガス併記',         args: { chain: 'Ethereum' } },
    { id: 'sendConfirm', label: 'Send Confirm — Ethereum',     sub: '順序 1→5 を固定',                          args: { chain: 'Ethereum' } },
    { id: 'assetDetail', label: '↩ Back to Asset Detail',     sub: 'Base を選び直す経路',                     args: {} },
    { id: 'sendInput',   label: 'Send Input — Base',           sub: 'Ethereum と完全に左右対称',               args: { chain: 'Base' } },
    { id: 'sendConfirm', label: 'Send Confirm — Base',         sub: '"ETH（Base）" を明示',                    args: { chain: 'Base' } },
    { id: 'receive',     label: 'Receive (option)',            sub: 'チェーンも文脈で確定',                     args: { chain: 'Ethereum' } },
    { id: 'unsupported', label: 'Unsupported chain (option)', sub: '曖昧な失敗を作らない',                    args: {} },
  ];

  return (
    <ol className="flow-list">
      {steps.map((s, i) => {
        const matchesScreen = s.id === current;
        const matchesChain = (!s.args.chain) || s.args.chain === params.chain;
        const active = matchesScreen && matchesChain;
        return (
          <li key={i} className={`flow-step ${active ? 'is-active' : ''}`}>
            <button onClick={() => go(s.id, s.args)} className="flow-btn">
              <span className="flow-num">{String(i + 1).padStart(2, '0')}</span>
              <span className="flow-text">
                <span className="flow-label">{s.label}</span>
                <span className="flow-note">{s.sub}</span>
              </span>
            </button>
          </li>
        );
      })}
    </ol>
  );
}

// Expose components for print/standalone usage
Object.assign(window, {
  App, HomeScreen, AssetDetailETH, SendInput, SendConfirm,
  Sent, Receive, UnsupportedModal,
  USDTFlatRows, USDTGroupedRow, ModeBadge, ETHFlatRows,
  ETHGroupedRow, AssetRow, HomeBackdrop, FGHeader, HomeActionRow,
  StatusBar, NavBar, BottomTabs,
});

if (!window.__PRINT_MODE__) {
  ReactDOM.createRoot(document.getElementById('root')).render(<App />);
}
