/* eslint-disable */
// Dashboard.jsx — Module 4 screens.

// ---------- S4.1 Dashboard — Variant A (balance hero + activity) ----------
const S4_1_Dashboard_A = () => (
  <ConsoleShell current="dashboard" breadcrumbs={['Dashboard']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.1 · Variant A"
      title="Good morning, Lin"
      sub="Reserves are in attestation. No active alerts."
      actions={
        <>
          <Button variant="secondary" icon={<Icons.arrowDown size={13} />}>Burn</Button>
          <Button icon={<Icons.arrowUp size={13} />}>Mint</Button>
        </>
      }
    />

    {/* Hero balance row */}
    <div style={{
      display: 'grid', gridTemplateColumns: '1.6fr 1fr 1fr', gap: 16, marginBottom: 22,
    }}>
      {/* Big balance */}
      <Card padding={26} style={{
        background: 'linear-gradient(135deg, rgba(82,0,4,0.20), rgba(20,18,16,0.4))',
        border: '1px solid var(--border-gold)', position: 'relative', overflow: 'hidden',
      }}>
        <div style={{ position: 'absolute', inset: 0, background: 'var(--grad-vignette)', opacity: 0.6, pointerEvents: 'none' }} />
        <span className="kgst-caption" style={{ color: 'var(--bone-3)', position: 'relative' }}>Total KGST · all chains</span>
        <div style={{ marginTop: 10, position: 'relative' }}>
          <Amount value="8,724,310.41" size={42} />
        </div>
        <div style={{ marginTop: 16, position: 'relative', display: 'flex', gap: 20 }}>
          {[['eth','Ethereum','5,224,310.41'],['tron','Tron','3,500,000.00'],['bnb','BNB','0.00']].map(([k,n,v]) => (
            <div key={k} style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                <ChainBadge chain={k} small /><span style={{ fontSize: 11, color: 'var(--fg-tertiary)' }}>{n}</span>
              </div>
              <span className="kgst-mono" style={{ fontSize: 13, color: 'var(--bone-5)' }}>{v}</span>
            </div>
          ))}
        </div>
      </Card>

      {/* Pending settle */}
      <Card padding={22}>
        <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>Pending settlement</span>
        <div style={{ marginTop: 10, fontFamily: 'var(--font-mono)', fontSize: 22, fontWeight: 600, color: 'var(--amber-400)' }}>
          5,000,000.00 <span style={{ fontSize: 11, color: 'var(--fg-tertiary)', fontWeight: 500, marginLeft: 4 }}>KGS</span>
        </div>
        <div style={{ marginTop: 6, fontSize: 11.5, color: 'var(--fg-tertiary)' }}>1 inbound mint · awaiting wire</div>
        <div style={{ marginTop: 14, paddingTop: 12, borderTop: '1px solid var(--border-subtle)', fontSize: 11.5, color: 'var(--fg-secondary)', display: 'flex', justifyContent: 'space-between' }}>
          <span>Outbound burn</span>
          <span className="kgst-mono">0.00 KGS</span>
        </div>
      </Card>

      {/* MTD activity */}
      <Card padding={22}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>Month to date</span>
          <span style={{ fontSize: 10.5, color: 'var(--fg-tertiary)' }}>Jan 2026</span>
        </div>
        <div style={{ marginTop: 10, display: 'flex', gap: 16 }}>
          <div>
            <div style={{ fontSize: 10.5, color: 'var(--fg-tertiary)' }}>Minted</div>
            <div className="kgst-mono" style={{ fontSize: 17, color: 'var(--gold-300)', fontWeight: 600 }}>12.4M</div>
          </div>
          <div>
            <div style={{ fontSize: 10.5, color: 'var(--fg-tertiary)' }}>Burned</div>
            <div className="kgst-mono" style={{ fontSize: 17, color: 'var(--bone-5)', fontWeight: 600 }}>4.2M</div>
          </div>
        </div>
        <div style={{ marginTop: 10, marginLeft: -6, marginRight: -6 }}>
          <Sparkline data={[2,3,5,4,7,9,8,10,7,12,11,14,10,16,18,15,21,19,22,18,24,20,27,25,28]} height={48} />
        </div>
      </Card>
    </div>

    {/* Activity + quick actions */}
    <div style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr', gap: 22 }}>
      <Card padding={0} style={{ overflow: 'hidden' }}>
        <div style={{ padding: '16px 22px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', borderBottom: '1px solid var(--border-subtle)' }}>
          <div className="kgst-h3" style={{ fontSize: 15 }}>Recent transactions</div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
            <div style={{ display: 'flex', gap: 6 }}>
              <FilterChip label="All" active />
              <FilterChip label="Mint" />
              <FilterChip label="Burn" />
              <FilterChip label="Failed" />
            </div>
            <a style={{ fontSize: 12, color: 'var(--gold-300)' }}>View all →</a>
          </div>
        </div>
        <TxTable rows={RECENT_TX.slice(0, 5)} />
      </Card>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
        <Card padding={20}>
          <div className="kgst-caption" style={{ color: 'var(--bone-3)', marginBottom: 12 }}>Quick actions</div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
            <QuickRow icon="arrowUp" title="Mint KGST" body="Wire KGS, receive on-chain" />
            <QuickRow icon="arrowDown" title="Redeem KGST" body="Burn KGST, receive KGS" />
            <QuickRow icon="bank" title="Manage bank accounts" body="2 accounts bound" />
            <QuickRow icon="spark" title="Generate API key" body="For programmatic mint/burn" />
          </div>
        </Card>
        <Card padding={18} style={{
          background: 'linear-gradient(135deg, rgba(82,0,4,0.25), rgba(20,18,16,0.4))',
          border: '1px solid var(--border-default)',
        }}>
          <div style={{ display: 'flex', alignItems: 'flex-start', gap: 12 }}>
            <Icons.audit size={18} style={{ color: 'var(--gold-300)', marginTop: 2 }} />
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 13, fontWeight: 500 }}>December reserve attestation published</div>
              <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)', marginTop: 3 }}>Independent auditor: BDO Kyrgyzstan · 99.98% coverage</div>
              <a style={{ fontSize: 11.5, color: 'var(--gold-300)', marginTop: 6, display: 'inline-block' }}>Open report →</a>
            </div>
          </div>
        </Card>
      </div>
    </div>
  </ConsoleShell>
);

// ---------- S4.1 Dashboard — Variant B (split chain panels) ----------
const S4_1_Dashboard_B = () => (
  <ConsoleShell current="dashboard" breadcrumbs={['Dashboard']} customer="Binance Asia Pte.">
    <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', marginBottom: 24 }}>
      <div>
        <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>S4.1 · Variant B · Treasury overview</span>
        <h1 className="kgst-h1" style={{ margin: '8px 0 4px', fontSize: 24, letterSpacing: '-0.015em' }}>Balances by chain</h1>
        <span style={{ fontSize: 12, color: 'var(--fg-tertiary)' }}>13 Jan 2026 · 14:24 UTC · auto-refresh 12s</span>
      </div>
      <div style={{ display: 'flex', gap: 10 }}>
        <Button variant="secondary" icon={<Icons.arrowDown size={13} />}>Burn</Button>
        <Button icon={<Icons.arrowUp size={13} />}>Mint</Button>
      </div>
    </div>

    {/* aggregate stripe */}
    <Card padding={26} style={{ background: 'var(--ink-2)', marginBottom: 18 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 32 }}>
        <div style={{ flex: 1 }}>
          <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>Aggregate KGST balance</span>
          <div style={{ marginTop: 8 }}>
            <Amount value="8,724,310.41" size={36} />
          </div>
        </div>
        <BarStat label="Inbound (MTD)" value="12.4M" up tone="gold" />
        <BarStat label="Outbound (MTD)" value="4.2M" tone="neutral" />
        <BarStat label="Net issued (MTD)" value="+8.2M" up tone="gold" />
        <div style={{ width: 1, alignSelf: 'stretch', background: 'var(--border-subtle)' }} />
        <div style={{ minWidth: 180 }}>
          <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>Pending</span>
          <div style={{ marginTop: 6, fontSize: 14 }}>
            <span style={{ color: 'var(--amber-400)', fontFamily: 'var(--font-mono)' }}>1 mint</span>
            <span style={{ color: 'var(--fg-tertiary)' }}> · 5.0M KGS in flight</span>
          </div>
        </div>
      </div>
    </Card>

    {/* per-chain cards */}
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 14, marginBottom: 22 }}>
      <ChainBalanceCard chain="eth" balance="5,224,310.41" pct={60} txs={42} addrs={2} />
      <ChainBalanceCard chain="tron" balance="3,500,000.00" pct={40} txs={18} addrs={1} />
      <ChainBalanceCard chain="bnb" balance="0.00" pct={0} txs={0} addrs={0} empty />
    </div>

    {/* table + side panel */}
    <div style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr', gap: 22 }}>
      <Card padding={0} style={{ overflow: 'hidden' }}>
        <div style={{ padding: '16px 22px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', borderBottom: '1px solid var(--border-subtle)' }}>
          <div className="kgst-h3" style={{ fontSize: 15 }}>Recent transactions</div>
          <div style={{ display: 'flex', gap: 6 }}>
            <FilterChip label="All" active />
            <FilterChip label="Mint" />
            <FilterChip label="Burn" />
            <FilterChip label="Failed" />
          </div>
        </div>
        <TxTable rows={RECENT_TX.slice(0, 5)} />
      </Card>

      <Card padding={20}>
        <div className="kgst-caption" style={{ color: 'var(--bone-3)', marginBottom: 14 }}>Compliance & alerts</div>
        <AlertRow tone="success" title="No active alerts" body="Last AML screening passed 12 minutes ago." />
        <AlertRow tone="amber"   title="December attestation due in 3 days" body="BDO Kyrgyzstan will publish independently." />
        <AlertRow tone="neutral" title="API key 'binance-prod' nearing expiry" body="Rotates automatically in 14 days." />
      </Card>
    </div>
  </ConsoleShell>
);

const BarStat = ({ label, value, up, tone }) => (
  <div style={{ minWidth: 110 }}>
    <span className="kgst-caption" style={{ color: 'var(--bone-3)' }}>{label}</span>
    <div style={{ marginTop: 6, fontFamily: 'var(--font-mono)', fontSize: 18, fontWeight: 600,
      color: tone === 'gold' ? 'var(--gold-300)' : 'var(--bone-5)',
    }}>{value}</div>
  </div>
);

const ChainBalanceCard = ({ chain, balance, pct, txs, addrs, empty }) => (
  <Card padding={20} style={{ opacity: empty ? 0.5 : 1 }}>
    <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 14 }}>
      <ChainBadge chain={chain} />
      <span style={{ fontSize: 13, fontWeight: 500 }}>{CHAIN_MAP[chain].name}</span>
      <span style={{ fontSize: 10.5, color: 'var(--fg-tertiary)', marginLeft: 'auto' }}>{CHAIN_MAP[chain].short}-20</span>
    </div>
    <Amount value={balance} size={24} />
    <div style={{ marginTop: 14, height: 4, background: 'var(--ink-2)', borderRadius: 999, overflow: 'hidden' }}>
      <div style={{ width: `${pct}%`, height: '100%', background: 'var(--grad-gold-bar)' }} />
    </div>
    <div style={{ marginTop: 8, display: 'flex', justifyContent: 'space-between', fontSize: 11, color: 'var(--fg-tertiary)' }}>
      <span>{pct}% of total · {txs} txs this month</span>
      <span>{addrs} address{addrs === 1 ? '' : 'es'}</span>
    </div>
  </Card>
);

const FilterChip = ({ label, active }) => (
  <span style={{
    padding: '4px 10px', borderRadius: 999, fontSize: 11, letterSpacing: '0.02em',
    color: active ? 'var(--gold-200)' : 'var(--fg-tertiary)',
    background: active ? 'rgba(201,152,64,0.08)' : 'transparent',
    border: '1px solid ' + (active ? 'var(--border-gold)' : 'var(--border-subtle)'),
    cursor: 'pointer',
  }}>{label}</span>
);

const QuickRow = ({ icon, title, body }) => {
  const I = Icons[icon];
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 12,
      padding: '10px 12px', borderRadius: 6, cursor: 'pointer',
    }}>
      <span style={{
        width: 28, height: 28, borderRadius: 6,
        background: 'rgba(201,152,64,0.08)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        color: 'var(--gold-300)', flexShrink: 0,
      }}><I size={14} /></span>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 13, fontWeight: 500 }}>{title}</div>
        <div style={{ fontSize: 11, color: 'var(--fg-tertiary)' }}>{body}</div>
      </div>
      <Icons.arrowRight size={13} style={{ color: 'var(--fg-tertiary)' }} />
    </div>
  );
};

const AlertRow = ({ tone, title, body }) => {
  const tones = {
    success: ['var(--green-400)', 'rgba(79,168,106,0.12)'],
    amber:   ['var(--amber-400)', 'rgba(225,165,58,0.12)'],
    danger:  ['var(--red-400)',   'rgba(196,69,69,0.12)'],
    neutral: ['var(--bone-4)',    'rgba(255,244,219,0.06)'],
  };
  const [c, bg] = tones[tone];
  return (
    <div style={{
      padding: '12px 0', display: 'flex', gap: 12, borderTop: '1px solid var(--border-subtle)',
    }}>
      <span style={{
        width: 8, height: 8, borderRadius: '50%', background: c, marginTop: 6, flexShrink: 0,
        boxShadow: `0 0 0 3px ${bg}`,
      }} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 12.5, fontWeight: 500 }}>{title}</div>
        <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)', marginTop: 3 }}>{body}</div>
      </div>
    </div>
  );
};

// ---------- Transaction table ----------
const RECENT_TX = [
  { id: 'KGST-MINT-2026000914', dir: 'mint',  amount: '5,000,000.00', chain: 'tron', status: 'pending', time: '13 Jan · 14:18', tx: '6FA2c8b1…ba31' },
  { id: 'KGST-BURN-2026000418', dir: 'burn',  amount: '2,500,000.00', chain: 'eth',  status: 'settled', time: '13 Jan · 09:02', tx: '0xd4f9…91a8' },
  { id: 'KGST-MINT-2026000877', dir: 'mint',  amount: '8,000,000.00', chain: 'tron', status: 'settled', time: '11 Jan · 16:44', tx: '6FA2…12c3' },
  { id: 'KGST-BURN-2026000391', dir: 'burn',  amount: '1,200,000.00', chain: 'eth',  status: 'settled', time: '10 Jan · 11:11', tx: '0x88ea…91fc' },
  { id: 'KGST-MINT-2026000842', dir: 'mint',  amount: '3,500,000.00', chain: 'tron', status: 'settled', time: '08 Jan · 10:02', tx: 'A9C1…78f0' },
  { id: 'KGST-MINT-2026000818', dir: 'mint',  amount:   '900,000.00', chain: 'eth',  status: 'failed',  time: '07 Jan · 19:14', tx: '0x77fd…1a23' },
  { id: 'KGST-BURN-2026000366', dir: 'burn',  amount: '1,000,000.00', chain: 'eth',  status: 'settled', time: '05 Jan · 13:31', tx: '0x40c1…aa54' },
];

const TxTable = ({ rows, dense, onRowClick }) => {
  const t = (typeof useT === 'function') ? useT() : ((en) => en);
  return (
  <div style={{ overflow: 'hidden' }}>
    <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 12.5 }}>
      <thead>
        <tr style={{ background: 'var(--ink-2)', color: 'var(--fg-tertiary)', textAlign: 'left' }}>
          {[t('Request', 'Запрос'), t('Direction', 'Направление'), t('Amount', 'Сумма'), t('Chain', 'Сеть'), t('Status', 'Статус'), t('When', 'Когда'), t('Tx hash', 'Хэш транз.')].map(h => (
            <th key={h} style={{ padding: dense ? '8px 14px' : '10px 18px', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', fontWeight: 500, borderBottom: '1px solid var(--border-subtle)' }}>{h}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        {rows.map((r, i) => (
          <tr key={r.id}
            onClick={onRowClick ? () => onRowClick(r) : undefined}
            style={{
              borderBottom: '1px solid var(--border-subtle)',
              cursor: onRowClick ? 'pointer' : 'default',
              transition: 'background 120ms ease',
            }}
            onMouseEnter={onRowClick ? e => { e.currentTarget.style.background = 'rgba(255,244,219,0.03)'; } : undefined}
            onMouseLeave={onRowClick ? e => { e.currentTarget.style.background = 'transparent'; } : undefined}
          >
            <td style={{ padding: dense ? '9px 14px' : '12px 18px' }}>
              <span className="kgst-mono" style={{ color: 'var(--bone-5)', fontSize: 12 }}>{r.id}</span>
            </td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px' }}>
              <span style={{
                display: 'inline-flex', alignItems: 'center', gap: 6,
                color: r.dir === 'mint' ? 'var(--gold-300)' : 'var(--bone-5)',
              }}>
                {r.dir === 'mint' ? <Icons.arrowUp size={12} /> : <Icons.arrowDown size={12} />}
                {r.dir === 'mint' ? t('Mint', 'Эмиссия') : t('Burn', 'Погашение')}
              </span>
            </td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px', fontFamily: 'var(--font-mono)' }}>
              {r.amount} <span style={{ color: 'var(--fg-tertiary)', fontSize: 10.5 }}>KGST</span>
            </td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px' }}>
              <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
                <ChainBadge chain={r.chain} small /> <span style={{ fontSize: 11.5 }}>{CHAIN_MAP[r.chain].name}</span>
              </span>
            </td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px' }}>
              <Pill tone={r.status === 'settled' ? 'success' : r.status === 'pending' ? 'warning' : 'danger'}>
                {r.status === 'settled' ? t('Settled', 'Исполнено') : r.status === 'pending' ? t('Pending', 'В ожидании') : t('Failed', 'Ошибка')}
              </Pill>
            </td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px', color: 'var(--fg-tertiary)' }}>{r.time}</td>
            <td style={{ padding: dense ? '9px 14px' : '12px 18px', fontFamily: 'var(--font-mono)', color: 'var(--gold-300)' }}>{r.tx}</td>
          </tr>
        ))}
      </tbody>
    </table>
  </div>
  );
};

// ---------- S4.2 Transactions list ----------
const S4_2_Transactions = () => (
  <ConsoleShell current="transactions" breadcrumbs={['Transactions']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.2 · Transactions"
      title="Transaction history"
      sub="Every mint and burn issued under your institutional account. Exports include signed receipts and on-chain proofs."
      actions={
        <>
          <Button variant="ghost" icon={<Icons.download size={13} />}>Export CSV</Button>
          <Button variant="secondary">Filters · 2</Button>
        </>
      }
    />

    {/* filter bar */}
    <Card padding={14} style={{ marginBottom: 14 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
        <FilterDropdown label="Direction" value="All" />
        <FilterDropdown label="Chain" value="Ethereum, Tron" />
        <FilterDropdown label="Status" value="All" />
        <FilterDropdown label="Date" value="Last 30 days" />
        <div style={{ flex: 1 }} />
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          padding: '0 10px', height: 30, background: 'var(--ink-2)', borderRadius: 6,
          border: '1px solid var(--border-subtle)', color: 'var(--bone-3)', fontSize: 11.5, width: 280,
        }}>
          <Icons.search size={12} />
          <span>Search request ID, address, tx hash…</span>
        </div>
      </div>
    </Card>

    <Card padding={0} style={{ overflow: 'hidden' }}>
      <TxTable rows={RECENT_TX} />
      <div style={{ padding: '12px 20px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', borderTop: '1px solid var(--border-subtle)', background: 'var(--ink-2)' }}>
        <span style={{ fontSize: 11.5, color: 'var(--fg-tertiary)' }}>Showing 1–7 of 142 requests</span>
        <div style={{ display: 'flex', gap: 4 }}>
          {[1,2,3,'…',21].map((p,i) => (
            <span key={i} style={{
              minWidth: 26, height: 26, padding: '0 8px', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              borderRadius: 5, fontSize: 11.5,
              color: p === 1 ? 'var(--gold-200)' : 'var(--fg-secondary)',
              background: p === 1 ? 'rgba(201,152,64,0.08)' : 'transparent',
              border: '1px solid ' + (p === 1 ? 'var(--border-gold)' : 'transparent'),
              cursor: 'pointer',
            }}>{p}</span>
          ))}
        </div>
      </div>
    </Card>
  </ConsoleShell>
);

const FilterDropdown = ({ label, value }) => (
  <div style={{
    display: 'inline-flex', alignItems: 'center', gap: 8,
    padding: '0 10px', height: 30, background: 'var(--ink-2)', borderRadius: 6,
    border: '1px solid var(--border-subtle)', fontSize: 11.5, cursor: 'pointer',
  }}>
    <span style={{ color: 'var(--fg-tertiary)' }}>{label}:</span>
    <span style={{ color: 'var(--fg-primary)' }}>{value}</span>
    <Icons.chevronDown size={11} style={{ color: 'var(--bone-3)' }} />
  </div>
);

// ---------- S4.3 Settings — Bank accounts ----------
const S4_3_Banks = () => (
  <ConsoleShell current="banks" breadcrumbs={['Account', 'Bank Accounts']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.3 · Account settings"
      title="Settlement bank accounts"
      sub="Outgoing KGS wires (burn settlements) are sent to one of these accounts. The account holder must match your verified business name."
      actions={<Button icon={<Icons.plus size={13} />}>Add account</Button>}
    />

    <SettingsLayout current="banks">
      <Card padding={0} style={{ overflow: 'hidden' }}>
        {[
          { bank: 'Optima Bank OJSC', swift: 'ENEJKG22', acct: '1170 0702 4000 5611', holder: 'Binance Asia Services Pte. Ltd.', country: 'Kyrgyzstan', isDefault: true, added: '14 Jan 2026' },
          { bank: 'DemirBank', swift: 'DEMIKG22', acct: '1090 4001 0023 8842', holder: 'Binance Asia Services Pte. Ltd.', country: 'Kyrgyzstan', isDefault: false, added: '02 Feb 2026' },
        ].map((b, i, arr) => (
          <div key={i} style={{
            padding: '20px 24px',
            borderBottom: i < arr.length - 1 ? '1px solid var(--border-subtle)' : 'none',
            display: 'flex', alignItems: 'center', gap: 18,
          }}>
            <div style={{
              width: 42, height: 42, borderRadius: 8,
              background: 'linear-gradient(135deg, var(--wine-500), var(--ink-4))',
              border: '1px solid var(--border-gold)',
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center', color: 'var(--gold-200)',
            }}><Icons.bank size={18} /></div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                <span style={{ fontSize: 14, fontWeight: 600 }}>{b.bank}</span>
                {b.isDefault ? <Pill tone="gold" dot>Default</Pill> : null}
              </div>
              <div style={{ marginTop: 4, fontSize: 12, color: 'var(--fg-secondary)' }}>
                <span className="kgst-mono">SWIFT {b.swift}</span> · <span className="kgst-mono">A/C {b.acct}</span>
              </div>
              <div style={{ marginTop: 2, fontSize: 11.5, color: 'var(--fg-tertiary)' }}>
                {b.holder} · {b.country} · added {b.added}
              </div>
            </div>
            <div style={{ display: 'flex', gap: 8 }}>
              {!b.isDefault ? <Button size="sm" variant="ghost">Set as default</Button> : null}
              <Button size="sm" variant="ghost">Edit</Button>
              <Button size="sm" variant="danger">Remove</Button>
            </div>
          </div>
        ))}
      </Card>

      <Card padding={16} style={{ marginTop: 14, background: 'rgba(93,139,217,0.06)', border: '1px solid rgba(93,139,217,0.20)' }}>
        <div style={{ display: 'flex', gap: 10, fontSize: 12, color: 'var(--bone-5)' }}>
          <Icons.shield size={14} style={{ color: 'var(--blue-400)', marginTop: 1, flexShrink: 0 }} />
          <span>Adding or modifying a bank account requires 2FA confirmation and an emailed click-through.</span>
        </div>
      </Card>
    </SettingsLayout>
  </ConsoleShell>
);

// ---------- S4.4 Settings — Wallet addresses ----------
const S4_4_Wallets = () => (
  <ConsoleShell current="wallets" breadcrumbs={['Account', 'Wallet Addresses']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.4 · Account settings"
      title="Wallet addresses"
      sub="Addresses bound to your institution. Mint deliveries can only go to these addresses, and burns can only originate from them."
      actions={<Button icon={<Icons.plus size={13} />}>Bind new address</Button>}
    />

    <SettingsLayout current="wallets">
      {[
        { chain: 'eth', count: 2, addrs: [
          { name: 'Treasury · Cold storage', addr: '0x9b21A8e7d1f5C4b8Ad3F8c2E9B7d3A1F6c2E4d8B', added: '12 Oct 2025', balance: '5,224,310.41' },
          { name: 'Operations · Hot wallet', addr: '0x4Ad7b2F9c1B8E6d2A5F3c8B1d9E2A6F4c7B3d1E5', added: '03 Dec 2025', balance: '0.00' },
        ]},
        { chain: 'tron', count: 1, addrs: [
          { name: 'Treasury · Tron', addr: 'TKgQ9b2A4Fz81G7tHpY5sLwM2eC8DfN6Vx', added: '21 Nov 2025', balance: '3,500,000.00' },
        ]},
        { chain: 'bnb', count: 0, addrs: [] },
      ].map((g, i) => (
        <Card key={i} padding={0} style={{ marginBottom: 14, overflow: 'hidden' }}>
          <div style={{ padding: '14px 22px', display: 'flex', alignItems: 'center', gap: 10, borderBottom: '1px solid var(--border-subtle)' }}>
            <ChainBadge chain={g.chain} />
            <span style={{ fontSize: 13.5, fontWeight: 600 }}>{CHAIN_MAP[g.chain].name}</span>
            <span style={{ fontSize: 11, color: 'var(--fg-tertiary)' }}>{g.count} address{g.count === 1 ? '' : 'es'}</span>
            <span style={{ flex: 1 }} />
            <Button size="sm" variant="ghost" icon={<Icons.plus size={12} />}>Add</Button>
          </div>
          {g.addrs.length === 0 ? (
            <div style={{ padding: '24px 22px', textAlign: 'center', fontSize: 12.5, color: 'var(--fg-tertiary)' }}>
              No addresses bound on this chain yet.
            </div>
          ) : g.addrs.map((a, j) => (
            <div key={j} style={{
              padding: '14px 22px', display: 'flex', alignItems: 'center', gap: 18,
              borderBottom: j < g.addrs.length - 1 ? '1px solid var(--border-subtle)' : 'none',
            }}>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 13, fontWeight: 500 }}>{a.name}</div>
                <div className="kgst-mono" style={{ marginTop: 2, fontSize: 11.5, color: 'var(--fg-tertiary)' }}>{a.addr}</div>
              </div>
              <div style={{ textAlign: 'right' }}>
                <div className="kgst-mono kgst-num" style={{ fontSize: 13, color: 'var(--bone-5)' }}>{a.balance} <span style={{ fontSize: 10, color: 'var(--fg-tertiary)' }}>KGST</span></div>
                <div style={{ fontSize: 10.5, color: 'var(--fg-tertiary)' }}>Bound {a.added}</div>
              </div>
              <div style={{ display: 'flex', gap: 6 }}>
                <Button size="sm" variant="ghost">Rename</Button>
                <Button size="sm" variant="danger">Remove</Button>
              </div>
            </div>
          ))}
        </Card>
      ))}
    </SettingsLayout>
  </ConsoleShell>
);

// ---------- S4.5 Settings — API keys ----------
const S4_5_API = () => (
  <ConsoleShell current="apikeys" breadcrumbs={['Account', 'API Keys']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.5 · Account settings"
      title="API keys"
      sub="Programmatic access for treasury automation. Keys are scoped per environment and never displayed twice — generate, copy, store."
      actions={<Button icon={<Icons.plus size={13} />}>Create key</Button>}
    />

    <SettingsLayout current="apikeys">
      <Card padding={0} style={{ overflow: 'hidden' }}>
        <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 12.5 }}>
          <thead>
            <tr style={{ background: 'var(--ink-2)', color: 'var(--fg-tertiary)', textAlign: 'left' }}>
              {['Name','Key','Scopes','Created','Last used','Status',''].map(h => (
                <th key={h} style={{ padding: '12px 18px', fontSize: 10.5, letterSpacing: '0.08em', textTransform: 'uppercase', fontWeight: 500, borderBottom: '1px solid var(--border-subtle)' }}>{h}</th>
              ))}
            </tr>
          </thead>
          <tbody>
            {[
              { name: 'binance-prod', key: 'kgst_live_a4F…7Q2', scopes: 'mint, burn, read', created: '14 Jan 2026', used: '2 min ago', status: 'active' },
              { name: 'binance-staging', key: 'kgst_test_xZ1…r9C', scopes: 'mint, burn, read', created: '14 Jan 2026', used: '3 days ago', status: 'active' },
              { name: 'treasury-readonly', key: 'kgst_live_p9Y…m1V', scopes: 'read', created: '02 Feb 2026', used: '17 days ago', status: 'active' },
              { name: 'legacy-ops', key: 'kgst_live_q8C…b3J', scopes: 'mint, burn, read', created: '03 Sep 2025', used: '120 days ago', status: 'revoked' },
            ].map((k, i, arr) => (
              <tr key={i} style={{ borderBottom: i < arr.length - 1 ? '1px solid var(--border-subtle)' : 'none' }}>
                <td style={{ padding: '14px 18px', fontWeight: 500 }}>{k.name}</td>
                <td style={{ padding: '14px 18px', fontFamily: 'var(--font-mono)', color: 'var(--gold-300)' }}>{k.key}</td>
                <td style={{ padding: '14px 18px' }}>
                  <div style={{ display: 'flex', gap: 4 }}>
                    {k.scopes.split(', ').map(s => <Pill key={s} tone="neutral" dot={false} style={{ fontSize: 10 }}>{s}</Pill>)}
                  </div>
                </td>
                <td style={{ padding: '14px 18px', color: 'var(--fg-tertiary)' }}>{k.created}</td>
                <td style={{ padding: '14px 18px', color: 'var(--fg-tertiary)' }}>{k.used}</td>
                <td style={{ padding: '14px 18px' }}>
                  <Pill tone={k.status === 'active' ? 'success' : 'danger'}>{k.status === 'active' ? 'Active' : 'Revoked'}</Pill>
                </td>
                <td style={{ padding: '14px 18px', textAlign: 'right' }}>
                  {k.status === 'active' ? <Button size="sm" variant="danger">Revoke</Button> : <Button size="sm" variant="ghost" disabled>Removed</Button>}
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </Card>

      <Card padding={18} style={{ marginTop: 14, background: 'rgba(225,165,58,0.06)', border: '1px solid rgba(225,165,58,0.20)' }}>
        <div style={{ display: 'flex', gap: 10, fontSize: 12, color: 'var(--bone-5)' }}>
          <Icons.shield size={14} style={{ color: 'var(--amber-400)', marginTop: 1, flexShrink: 0 }} />
          <span>Keys with mint/burn scope can move funds. Treat them with the same care as wire-instruction passwords; rotate every 90 days.</span>
        </div>
      </Card>
    </SettingsLayout>
  </ConsoleShell>
);

// ---------- S4.6 Settings — Security ----------
const S4_6_Security = () => (
  <ConsoleShell current="security" breadcrumbs={['Account', 'Security']} customer="Binance Asia Pte.">
    <PageHeader
      eyebrow="S4.6 · Account settings"
      title="Security"
      sub="Two-factor authentication, password, devices, and notifications."
    />

    <SettingsLayout current="security">
      <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
        <Card padding={22}>
          <div style={{ display: 'flex', alignItems: 'flex-start', gap: 16 }}>
            <div style={{
              width: 40, height: 40, borderRadius: 8, background: 'rgba(201,152,64,0.08)',
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              color: 'var(--gold-300)', flexShrink: 0,
              border: '1px solid var(--border-gold)',
            }}><Icons.shield size={18} /></div>
            <div style={{ flex: 1 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                <span style={{ fontSize: 14, fontWeight: 600 }}>Two-factor authentication</span>
                <Pill tone="gold" dot>TOTP active</Pill>
              </div>
              <div style={{ marginTop: 4, fontSize: 12, color: 'var(--fg-secondary)' }}>
                Authenticator app is your primary 2FA. Email is kept as a fallback.
              </div>
              <div style={{
                marginTop: 14, padding: 12, borderRadius: 8,
                background: 'var(--ink-2)', border: '1px solid var(--border-subtle)',
                display: 'flex', alignItems: 'center', gap: 14,
              }}>
                <span style={{ width: 8, height: 8, borderRadius: '50%', background: 'var(--gold-300)' }} />
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, fontWeight: 500 }}>Authenticator (TOTP) · primary</div>
                  <div style={{ fontSize: 11, color: 'var(--fg-tertiary)' }}>Bound 14 Jan 2026 · Last used 2 min ago</div>
                </div>
                <Button size="sm" variant="ghost">Reset</Button>
              </div>
              <div style={{
                marginTop: 8, padding: 12, borderRadius: 8,
                background: 'var(--ink-2)', border: '1px solid var(--border-subtle)',
                display: 'flex', alignItems: 'center', gap: 14,
              }}>
                <span style={{ width: 8, height: 8, borderRadius: '50%', background: 'var(--bone-3)' }} />
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, fontWeight: 500 }}>Email codes · fallback</div>
                  <div style={{ fontSize: 11, color: 'var(--fg-tertiary)' }}>treasury@binance.com · used if TOTP unavailable</div>
                </div>
              </div>
            </div>
          </div>
        </Card>

        <Card padding={22}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 16 }}>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 14, fontWeight: 600 }}>Password</div>
              <div style={{ fontSize: 12, color: 'var(--fg-tertiary)', marginTop: 4 }}>Last changed 03 Dec 2025 · 42 days ago</div>
            </div>
            <Button size="sm" variant="secondary">Change password</Button>
          </div>
        </Card>

        <Card padding={0} style={{ overflow: 'hidden' }}>
          <div style={{ padding: '16px 22px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', borderBottom: '1px solid var(--border-subtle)' }}>
            <div>
              <div style={{ fontSize: 14, fontWeight: 600 }}>Active sessions</div>
              <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)', marginTop: 3 }}>3 devices currently signed in.</div>
            </div>
            <Button size="sm" variant="danger">Sign out all</Button>
          </div>
          {[
            { dev: 'MacBook Pro · Chrome 132', loc: 'Singapore, SG · 13 Jan 14:24 UTC', current: true },
            { dev: 'iPhone 15 Pro · KGST iOS', loc: 'Singapore, SG · 13 Jan 11:48 UTC', current: false },
            { dev: 'Windows 11 · Edge 121', loc: 'Hong Kong, HK · 11 Jan 09:02 UTC', current: false },
          ].map((d, i, arr) => (
            <div key={i} style={{
              padding: '14px 22px', display: 'flex', alignItems: 'center', gap: 16,
              borderBottom: i < arr.length - 1 ? '1px solid var(--border-subtle)' : 'none',
            }}>
              <Icons.user size={16} style={{ color: 'var(--bone-3)' }} />
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 13, fontWeight: 500, display: 'flex', alignItems: 'center', gap: 8 }}>
                  {d.dev}
                  {d.current ? <Pill tone="success" style={{ fontSize: 10 }}>This device</Pill> : null}
                </div>
                <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)', marginTop: 2 }}>{d.loc}</div>
              </div>
              {!d.current ? <Button size="sm" variant="ghost">Sign out</Button> : null}
            </div>
          ))}
        </Card>

        <Card padding={22}>
          <div style={{ fontSize: 14, fontWeight: 600, marginBottom: 14 }}>Notifications</div>
          {[
            ['New sign-in', 'Email me when a new device signs into this account.', true],
            ['Mint / burn completion', 'Email a signed receipt on every settlement.', true],
            ['Reserve attestations', 'Notify when the monthly attestation is published.', true],
            ['Weekly summary', 'A digest of last week\'s activity.', false],
          ].map(([t, body, on], i) => (
            <div key={i} style={{
              padding: '14px 0', display: 'flex', alignItems: 'center', gap: 16,
              borderTop: '1px solid var(--border-subtle)',
            }}>
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 13, fontWeight: 500 }}>{t}</div>
                <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)', marginTop: 2 }}>{body}</div>
              </div>
              <ToggleSwitch on={on} />
            </div>
          ))}
        </Card>
      </div>
    </SettingsLayout>
  </ConsoleShell>
);

const ToggleSwitch = ({ on }) => (
  <span style={{
    width: 38, height: 22, borderRadius: 999,
    background: on ? 'var(--gold-400)' : 'var(--ink-4)',
    position: 'relative', cursor: 'pointer',
    border: '1px solid ' + (on ? 'var(--gold-500)' : 'var(--border-default)'),
    transition: 'all 150ms var(--ease-standard)',
  }}>
    <span style={{
      position: 'absolute', top: 2, left: on ? 18 : 2,
      width: 16, height: 16, borderRadius: '50%', background: '#fff',
      transition: 'left 150ms var(--ease-standard)',
      boxShadow: '0 1px 2px rgba(0,0,0,0.4)',
    }} />
  </span>
);

// ---------- Sub-nav for settings ----------
const SettingsLayout = ({ current, children }) => {
  const items = [
    ['banks', 'Bank accounts'],
    ['wallets', 'Wallet addresses'],
    ['apikeys', 'API keys'],
    ['security', 'Security'],
  ];
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '200px 1fr', gap: 24, alignItems: 'flex-start' }}>
      <nav style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
        {items.map(([k, l]) => (
          <div key={k} style={{
            padding: '8px 12px', borderRadius: 6, fontSize: 12.5,
            color: k === current ? 'var(--gold-200)' : 'var(--fg-secondary)',
            background: k === current ? 'rgba(201,152,64,0.08)' : 'transparent',
            border: '1px solid ' + (k === current ? 'var(--border-gold)' : 'transparent'),
            cursor: 'pointer',
          }}>{l}</div>
        ))}
      </nav>
      <div>{children}</div>
    </div>
  );
};

Object.assign(window, {
  S4_1_Dashboard_A, S4_1_Dashboard_B, S4_2_Transactions,
  S4_3_Banks, S4_4_Wallets, S4_5_API, S4_6_Security,
  RECENT_TX, TxTable,
});
