// phone-demo.jsx — Gasless Wallet main screen (matches app screenshot) + send flow
// Renders custom wallet screens inside the IOSDevice bezel.
// Exports: WalletDemo

const W_ACCENT = 'var(--accent)';
const W_INK = '#0C1512';
const W_MUTED = '#7C857F';
const W_LINE = 'rgba(12,21,18,0.07)';
const W_BG = '#FFFFFF';
const W_DARK = '#1E1E1E';
const W_TRON = '#EF0027';
const W_USDT = '#26A17B';

// — tiny inline icons —
const WIcon = {
  gear: <svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M12 15.5a3.5 3.5 0 100-7 3.5 3.5 0 000 7z" stroke="currentColor" strokeWidth="1.8" /><path d="M19.4 13a1.7 1.7 0 00.34 1.87l.06.06a2 2 0 11-2.83 2.83l-.06-.06a1.7 1.7 0 00-1.87-.34 1.7 1.7 0 00-1 1.56V19a2 2 0 11-4 0v-.09a1.7 1.7 0 00-1.11-1.56 1.7 1.7 0 00-1.87.34l-.06.06a2 2 0 11-2.83-2.83l.06-.06a1.7 1.7 0 00.34-1.87 1.7 1.7 0 00-1.56-1H3a2 2 0 110-4h.09a1.7 1.7 0 001.56-1.11 1.7 1.7 0 00-.34-1.87l-.06-.06a2 2 0 112.83-2.83l.06.06a1.7 1.7 0 001.87.34H9a1.7 1.7 0 001-1.56V3a2 2 0 114 0v.09a1.7 1.7 0 001 1.56 1.7 1.7 0 001.87-.34l.06-.06a2 2 0 112.83 2.83l-.06.06a1.7 1.7 0 00-.34 1.87V9a1.7 1.7 0 001.56 1H21a2 2 0 110 4h-.09a1.7 1.7 0 00-1.51 1z" stroke="currentColor" strokeWidth="1.6" /></svg>,
  support: <svg width="22" height="22" viewBox="0 0 24 24" fill="none"><path d="M4 13v-1a8 8 0 0116 0v1" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" /><rect x="3" y="13" width="4" height="7" rx="2" stroke="currentColor" strokeWidth="1.9" /><rect x="17" y="13" width="4" height="7" rx="2" stroke="currentColor" strokeWidth="1.9" data-comment-anchor="a611360572-rect-17-259" /><path d="M20 19v.5a3.5 3.5 0 01-3.5 3.5H13" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" /></svg>,
  copy: <svg width="17" height="17" viewBox="0 0 24 24" fill="none"><rect x="9" y="9" width="11" height="11" rx="2.6" stroke="currentColor" strokeWidth="1.9" /><path d="M5 15.5A2.5 2.5 0 014 13.5V6A2.5 2.5 0 016.5 3.5H14" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" /></svg>,
  chevron: <svg width="15" height="15" viewBox="0 0 24 24" fill="none"><path d="M6 9l6 6 6-6" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" /></svg>,
  send: <svg width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M7 17L17 7M17 7H9M17 7v8" stroke="currentColor" strokeWidth="2.3" strokeLinecap="round" strokeLinejoin="round" /></svg>,
  plus: <svg width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M12 5v14M5 12h14" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" /></svg>,
  receive: <svg width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M17 7L7 17M7 17h8M7 17V9" stroke="currentColor" strokeWidth="2.3" strokeLinecap="round" strokeLinejoin="round" /></svg>,
  back: <svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M15 5l-7 7 7 7" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" /></svg>,
  check: <svg width="34" height="34" viewBox="0 0 24 24" fill="none"><path d="M5 13l4 4L19 7" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" /></svg>,
  bolt: <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M13 2L4 14h6l-1 8 9-12h-6l1-8z" fill="currentColor" /></svg>
};

// TRON geometric mark (red origami triangle)
function TronMark({ size = 48, color = W_TRON }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none" style={{ display: 'block' }}>
      <path d="M8 13L40 13L24.5 41Z" stroke={color} strokeWidth="2.6" strokeLinejoin="round" />
      <path d="M8 13L24.5 22.5L40 13M24.5 22.5L24.5 41" stroke={color} strokeWidth="2.6" strokeLinejoin="round" strokeLinecap="round" />
    </svg>);

}

function USDT({ size = 28 }) {
  return (
    <div style={{ width: size, height: size, borderRadius: '50%', background: W_USDT, display: 'grid', placeItems: 'center', flexShrink: 0 }}>
      <span style={{ color: '#fff', fontWeight: 700, fontSize: size * 0.52, fontFamily: 'var(--font-display)', lineHeight: 1, marginTop: size * 0.01 }}>₮</span>
    </div>);

}

// — hover tooltip for elements that carry teammate comments —
function Tooltip({ children, label, place = 'top', align = 'center', display = 'inline-flex', maxWidth = 200, cursor = 'help', focusable = true, style }) {
  const [show, setShow] = React.useState(false);
  const offset = 9;
  const anchor = { position: 'absolute', zIndex: 90, pointerEvents: 'none' };
  if (place === 'top') anchor.bottom = `calc(100% + ${offset}px)`;else
  anchor.top = `calc(100% + ${offset}px)`;
  if (align === 'center') {anchor.left = '50%';anchor.transform = 'translateX(-50%)';} else
  if (align === 'left') anchor.left = 0;else
  anchor.right = 0;
  return (
    <span
      onMouseEnter={() => setShow(true)}
      onMouseLeave={() => setShow(false)}
      onFocus={() => setShow(true)}
      onBlur={() => setShow(false)}
      tabIndex={focusable ? 0 : undefined}
      style={{ position: 'relative', display, alignItems: 'center', cursor, outline: 'none', ...style }}>

      {children}
      <span style={anchor}>
        <span className="wd-tip" data-show={show ? '1' : '0'} data-place={place} data-align={align} style={{ maxWidth }}>{label}</span>
      </span>
    </span>);

}

function ScreenShell({ children, top = 56 }) {
  return (
    <div style={{ position: 'absolute', inset: 0, paddingTop: top, display: 'flex', flexDirection: 'column', background: W_BG, fontFamily: 'var(--font-body)', color: W_INK }}>
      {children}
    </div>);

}

// ── status bar matching the screenshot: 23:58 · 5G · low battery ──
function WalletStatusBar() {
  const c = '#0C1512';
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '16px 28px 6px', fontFamily: '-apple-system, "SF Pro", system-ui' }}>
      <span style={{ fontWeight: 650, fontSize: 17, color: c, letterSpacing: 0.2 }}>23:58</span>
      <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
        {/* signal dots */}
        <svg width="18" height="12" viewBox="0 0 18 12"><circle cx="2" cy="6" r="1.6" fill={c} /><circle cx="6.5" cy="6" r="1.6" fill={c} /><circle cx="11" cy="6" r="1.6" fill={c} /><circle cx="15.5" cy="6" r="1.6" fill={c} /></svg>
        <span style={{ fontWeight: 650, fontSize: 15, color: c }}>5G</span>
        {/* battery (low) */}
        <svg width="26" height="13" viewBox="0 0 26 13"><rect x="0.6" y="0.6" width="22" height="11.8" rx="3.4" stroke={c} strokeOpacity="0.4" fill="none" /><rect x="2.4" y="2.4" width="6.2" height="8.2" rx="1.6" fill={c} /><path d="M24.4 4.6v3.8c.8-.3 1.3-1 1.3-1.9s-.5-1.6-1.3-1.9z" fill={c} fillOpacity="0.4" /></svg>
      </div>
    </div>);

}

// ── HOME (main screen) ──
function HomeScreen({ go }) {
  const actions = [
  { k: 'send', label: 'Send', icon: WIcon.send, tip: 'Send USDT to any address' },
  { k: 'buy', label: 'Get TRX', icon: WIcon.plus, tip: 'Buy TRX to cover network fees' },
  { k: 'recv', label: 'Receive', icon: WIcon.receive, tip: 'Receive USDT or TRX' }];

  return (
    <ScreenShell top={52}>
      {/* app header */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '4px 18px 14px' }}>
        <Tooltip label="Open settings" place="bottom" align="left" cursor="pointer" focusable={false} maxWidth={130} style={{ flexShrink: 0 }}>
          <button onClick={() => go('home')} style={{ width: 48, height: 48, borderRadius: '50%', border: 'none', background: W_DARK, color: '#fff', display: 'grid', placeItems: 'center', cursor: 'pointer', flexShrink: 0 }}>{WIcon.gear}</button>
        </Tooltip>
        <div style={{ flex: 1, display: 'flex', justifyContent: 'center' }}>
          <Tooltip label="Switch between your wallets" place="bottom" align="center" cursor="pointer" maxWidth={180}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 9, background: '#fff', border: `1.5px solid ${W_LINE}`, borderRadius: 999, padding: '7px 7px 7px 16px', boxShadow: '0 1px 2px rgba(12,21,18,.04)' }}>
              <span style={{ width: 9, height: 9, borderRadius: '50%', background: '#1FB85A', flexShrink: 0 }} />
              <span style={{ fontWeight: 700, fontSize: 15.5, whiteSpace: 'nowrap' }}>Gasless Wallet</span>
              <span style={{ color: W_INK, display: 'grid', placeItems: 'center', opacity: 0.85 }}>{WIcon.chevron}</span>
              <span style={{ width: 32, height: 32, borderRadius: '50%', border: `1.5px solid ${W_LINE}`, color: W_MUTED, display: 'grid', placeItems: 'center', marginLeft: 1 }}>{WIcon.copy}</span>
            </div>
          </Tooltip>
        </div>
        <Tooltip label="Support — message us if you need help" place="bottom" align="right" cursor="pointer" focusable={false} maxWidth={172} style={{ flexShrink: 0 }}>
          <button style={{ width: 48, height: 48, borderRadius: '50%', border: 'none', background: 'transparent', color: W_INK, display: 'grid', placeItems: 'center', cursor: 'pointer', flexShrink: 0 }}>{WIcon.support}</button>
        </Tooltip>
      </div>

      {/* balance card */}
      <div style={{ margin: '6px 16px 0', background: 'linear-gradient(180deg,#EFEFEF,#E8E8E8)', borderRadius: 30, padding: '26px 20px 24px' }}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'center', gap: 2 }}>
          <span style={{ fontFamily: 'var(--font-display)', fontSize: 30, fontWeight: 700, color: '#9AA09C', marginRight: 4, alignSelf: 'center' }}>$</span>
          <span style={{ fontFamily: 'var(--font-display)', fontSize: 60, fontWeight: 800, letterSpacing: '-0.04em', color: '#15201B', lineHeight: 1 }}>2,464</span>
          <span style={{ fontFamily: 'var(--font-display)', fontSize: 32, fontWeight: 700, color: '#9AA09C' }}>.70</span>
        </div>
        <div style={{ display: 'flex', justifyContent: 'center', gap: 26, marginTop: 22 }}>
          {actions.map((a) =>
          <Tooltip key={a.k} label={a.tip} place="bottom" align="center" cursor="pointer" focusable={false} maxWidth={160}>
            <button onClick={() => a.k === 'send' && go('send')}
            style={{ background: 'none', border: 'none', cursor: 'pointer', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 9, padding: 0, fontFamily: 'var(--font-body)' }}>
              <span style={{ width: 64, height: 64, borderRadius: '50%', background: W_DARK, color: '#fff', display: 'grid', placeItems: 'center', position: 'relative' }} className={a.k === 'send' ? 'wd-cta-icon' : undefined}>
                {a.k === 'send' && <><span className="wd-ring" /><span className="wd-ring" style={{ animationDelay: '.9s' }} /></>}
                {a.k === 'send' && (
                  <span className="wd-sticker" aria-hidden="true">
                    Tap here
                    <span className="wd-sticker-tail" />
                  </span>
                )}
                {a.icon}
              </span>
              <span style={{ fontSize: 14.5, fontWeight: 600, color: a.k === 'send' ? 'var(--accent)' : '#2A332E' }}>{a.label}</span>
            </button>
          </Tooltip>
          )}
        </div>
      </div>

      {/* assets */}
      <div style={{ padding: '22px 22px 6px', fontSize: 17, fontWeight: 700, color: '#3A433D' }}>Assets</div>
      <div style={{ padding: '0 20px' }}>
        {/* USDT */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '12px 2px' }}>
          <USDT size={50} />
          <div style={{ flex: 1, lineHeight: 1.25 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
              <span style={{ fontSize: 18.5, fontWeight: 700 }}>USDT</span>
              <Tooltip label="How many USDT transfers you can make with your current TRX balance" place="top" align="center" maxWidth={200}>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, background: '#FBE6D2', borderRadius: 999, padding: '2px 8px 2px 4px' }}>
                  <span style={{ width: 16, height: 16, borderRadius: '50%', background: '#fff', display: 'grid', placeItems: 'center' }}><TronMark size={11} color="#EF7A2A" /></span>
                  <span style={{ fontSize: 12.5, fontWeight: 700, color: '#C2641F' }}>11</span>
                </span>
              </Tooltip>
            </div>
            <div style={{ fontSize: 14, color: W_MUTED, marginTop: 2 }}>$0.999623</div>
          </div>
          <div style={{ textAlign: 'right', lineHeight: 1.25 }}>
            <div style={{ fontSize: 18.5, fontWeight: 700 }}>2,450.00</div>
            <div style={{ fontSize: 14, color: W_MUTED, marginTop: 2 }}>$2,450.00</div>
          </div>
        </div>
        <div style={{ height: 1, background: W_LINE, margin: '2px 0' }} />
        {/* TRX */}
        <Tooltip label="TRX is used to pay network transaction fees" place="top" align="center" display="flex" cursor="help" maxWidth={190} style={{ width: '100%' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '12px 2px', width: '100%' }}>
            <span style={{ width: 50, height: 50, display: 'grid', placeItems: 'center', flexShrink: 0 }}><TronMark size={46} /></span>
            <div style={{ flex: 1, lineHeight: 1.25 }}>
              <div style={{ fontSize: 18.5, fontWeight: 700 }} data-comment-anchor="19c07bab84-div-141-13">TRX</div>
              <div style={{ fontSize: 14, color: W_MUTED, marginTop: 2 }}>$0.322157</div>
            </div>
            <div style={{ textAlign: 'right', lineHeight: 1.25 }}>
              <div style={{ fontSize: 18.5, fontWeight: 700 }}>45.62</div>
              <div style={{ fontSize: 14, color: W_MUTED, marginTop: 2 }}>$14.70</div>
            </div>
          </div>
        </Tooltip>
      </div>

      {/* referral card */}
      <div style={{ margin: '22px 16px 0', background: '#E7EDF0', borderRadius: 24, padding: '22px 22px', display: 'flex', alignItems: 'center', gap: 14, overflow: 'hidden' }}>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 21, fontWeight: 800, color: '#1A2620', fontFamily: 'var(--font-display)', letterSpacing: '-0.02em' }}>Earn with referrals</div>
          <div style={{ fontSize: 15.5, color: '#6A746E', marginTop: 6, lineHeight: 1.35 }}>10% from each friend's transaction fee</div>
        </div>
        <div style={{ position: 'relative', width: 118, height: 96, flexShrink: 0 }}>
          <span style={{ position: 'absolute', top: 2, right: 8, fontFamily: 'var(--font-display)', fontSize: 30, fontWeight: 800, color: '#0FA968', letterSpacing: '-0.03em' }}>10%</span>
          {/* tron coin */}
          <div style={{ position: 'absolute', left: 10, bottom: 2, width: 60, height: 60, borderRadius: '50%', background: 'radial-gradient(circle at 35% 30%,#FF3B4E,#D90021)', display: 'grid', placeItems: 'center', boxShadow: '0 6px 14px rgba(217,0,33,.35)', zIndex: 2 }}>
            <TronMark size={34} color="#fff" />
          </div>
          {/* tether coin */}
          <div style={{ position: 'absolute', right: 0, bottom: 22, width: 52, height: 52, borderRadius: '50%', background: 'radial-gradient(circle at 35% 30%,#4FC78E,#1E9D63)', display: 'grid', placeItems: 'center', boxShadow: '0 6px 14px rgba(30,157,99,.3)', zIndex: 1 }}>
            <span style={{ color: '#fff', fontWeight: 700, fontSize: 26, fontFamily: 'var(--font-display)', lineHeight: 1 }}>₮</span>
          </div>
        </div>
      </div>
    </ScreenShell>);

}

// ── SEND ──
function SendScreen({ go, amount, setAmount }) {
  const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', '⌫'];
  const press = (k) => {
    setAmount((prev) => {
      if (k === '⌫') return prev.length <= 1 ? '0' : prev.slice(0, -1);
      if (k === '.') return prev.includes('.') ? prev : prev + '.';
      if (prev === '0') return k;
      return prev.replace('.', '').length >= 6 ? prev : prev + k;
    });
  };
  return (
    <ScreenShell top={52}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '6px 20px 14px' }}>
        <Tooltip label="Back to wallet" place="bottom" align="left" cursor="pointer" focusable={false} maxWidth={120} style={{ flexShrink: 0 }}>
          <button onClick={() => go('home')} style={{ width: 44, height: 44, borderRadius: '50%', border: `1.5px solid ${W_LINE}`, background: '#fff', display: 'grid', placeItems: 'center', cursor: 'pointer', color: W_INK, flexShrink: 0 }}>{WIcon.back}</button>
        </Tooltip>
        <div style={{ flex: 1, textAlign: 'center', fontWeight: 700, fontSize: 17 }} data-comment-anchor="fd2db7cf1a-div-188-9">Send USDT</div>
        <div style={{ width: 44, flexShrink: 0 }} />
      </div>
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', padding: '8px 22px 0' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 11, background: '#fff', border: `1.5px solid ${W_LINE}`, borderRadius: 16, padding: '12px 14px' }}>
          <div style={{ width: 34, height: 34, borderRadius: '50%', background: '#EFEAFE', display: 'grid', placeItems: 'center', fontWeight: 700, color: '#6D4AFF', fontSize: 13 }}>T7</div>
          <div style={{ lineHeight: 1.2 }}>
            <div style={{ fontSize: 12.5, color: W_MUTED }}>To</div>
            <div style={{ fontFamily: 'var(--font-mono)', fontSize: 14, fontWeight: 600 }}>T7bkq…M2dE9</div>
          </div>
          <Tooltip label="Edit recipient address" place="top" align="right" cursor="pointer" style={{ marginLeft: 'auto' }}>
            <div style={{ fontSize: 13, color: W_ACCENT, fontWeight: 700 }}>Edit</div>
          </Tooltip>
        </div>
        <div style={{ flex: 1, display: 'grid', placeItems: 'center', textAlign: 'center' }}>
          <div>
            <div style={{ fontSize: 13.5, color: W_MUTED, fontWeight: 600 }}>You send</div>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'center', gap: 6, marginTop: 6 }}>
              <span style={{ fontFamily: 'var(--font-display)', fontSize: 56, fontWeight: 800, letterSpacing: '-0.03em' }}>{amount}</span>
              <span style={{ fontSize: 22, fontWeight: 700, color: W_MUTED }}>USDT</span>
            </div>
            <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginTop: 12, background: 'var(--tint)', color: W_ACCENT, padding: '6px 12px', borderRadius: 999, fontSize: 12.5, fontWeight: 600 }}>
              {WIcon.bolt} 3.5 TRX fees
            </div>
          </div>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 4, marginBottom: 10 }}>
          {keys.map((k) =>
          <button key={k} onClick={() => press(k)} style={{ height: 46, border: 'none', background: 'transparent', fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 600, color: W_INK, cursor: 'pointer', borderRadius: 12 }}>{k}</button>
          )}
        </div>
        <Tooltip label="Review your transfer" place="top" align="center" cursor="pointer" focusable={false} display="block" maxWidth={170} style={{ marginBottom: 34 }}>
          <button onClick={() => go('review')} style={{ width: '100%', border: 'none', background: W_DARK, color: '#fff', borderRadius: 18, padding: '16px 0', fontSize: 16, fontWeight: 700, cursor: 'pointer', fontFamily: 'var(--font-body)' }} data-comment-anchor="26eb0b8611-button-216-9">Review</button>
        </Tooltip>
      </div>
    </ScreenShell>);

}

// ── REVIEW ──
function ReviewScreen({ go, amount, onConfirm }) {
  const amt = parseFloat(amount || '0') || 0;
  return (
    <ScreenShell top={52}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '6px 20px 14px' }}>
        <Tooltip label="Back to amount" place="bottom" align="left" cursor="pointer" focusable={false} maxWidth={120} style={{ flexShrink: 0 }}>
          <button onClick={() => go('send')} style={{ width: 44, height: 44, borderRadius: '50%', border: `1.5px solid ${W_LINE}`, background: '#fff', display: 'grid', placeItems: 'center', cursor: 'pointer', color: W_INK, flexShrink: 0 }}>{WIcon.back}</button>
        </Tooltip>
        <div style={{ flex: 1, textAlign: 'center', fontWeight: 700, fontSize: 17 }}>Review</div>
        <div style={{ width: 44, flexShrink: 0 }} />
      </div>
      <div style={{ flex: 1, padding: '8px 22px 0', display: 'flex', flexDirection: 'column' }}>
        <div style={{ background: 'linear-gradient(180deg,#EFEFEF,#E8E8E8)', borderRadius: 22, padding: 22, textAlign: 'center' }}>
          <div style={{ display: 'grid', placeItems: 'center' }}><USDT size={44} /></div>
          <div style={{ fontFamily: 'var(--font-display)', fontSize: 40, fontWeight: 800, marginTop: 12, letterSpacing: '-0.03em' }}>{amt.toFixed(2)}</div>
          <div style={{ fontSize: 13.5, color: W_MUTED, fontWeight: 600 }}>USDT to T7bkq…M2dE9</div>
        </div>
        <div style={{ background: '#fff', border: `1.5px solid ${W_LINE}`, borderRadius: 18, padding: '6px 18px', marginTop: 14 }}>
          {[
          ['Amount', `${amt.toFixed(2)} USDT`],
          ['Network', 'Tron (TRC-20)']].
          map(([k, v], i) =>
          <div key={i} style={{ display: 'flex', justifyContent: 'space-between', padding: '13px 0', borderBottom: `1px solid ${W_LINE}`, fontSize: 14 }}>
              <span style={{ color: W_MUTED }}>{k}</span><span style={{ fontWeight: 600, fontFamily: 'var(--font-mono)' }}>{v}</span>
            </div>
          )}
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '13px 0' }}>
            <span style={{ color: W_MUTED, fontSize: 14 }}>Network fee</span>
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 3 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                <span style={{ fontFamily: 'var(--font-mono)', fontSize: 13, color: W_MUTED, textDecoration: 'line-through' }}>7 TRX</span>
                <span style={{ fontWeight: 700, fontFamily: 'var(--font-mono)', fontSize: 14 }}>3.5 TRX</span>
                <span style={{ fontSize: 11, fontWeight: 700, color: '#fff', background: W_ACCENT, padding: '3px 7px', borderRadius: 7 }}>−50%</span>
              </div>
              <span style={{ fontSize: 11.5, color: W_MUTED, fontFamily: 'var(--font-mono)' }}>≈ $1.10</span>
            </div>
          </div>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 9, marginTop: 14, color: W_MUTED, fontSize: 12.5 }}>
          <span style={{ color: W_ACCENT }}>{WIcon.bolt}</span>
          Network fee is just 3.5 TRX — half what most wallets charge.
        </div>
        <div style={{ flex: 1 }} />
        <Tooltip label="Sign and send the transfer" place="top" align="center" cursor="pointer" focusable={false} display="block" maxWidth={190} style={{ marginBottom: 34 }}>
          <button onClick={onConfirm} style={{ width: '100%', border: 'none', background: W_DARK, color: '#fff', borderRadius: 18, padding: '16px 0', fontSize: 16, fontWeight: 700, cursor: 'pointer', fontFamily: 'var(--font-body)' }}>Confirm &amp; send</button>
        </Tooltip>
      </div>
    </ScreenShell>);

}

// — count-up number, ticks to a target then keeps gently climbing —
function SavingsCount({ target, duration = 1600, delay = 0, format, live = 0, style }) {
  const [val, setVal] = React.useState(0);
  const fmt = format || ((n) => Math.round(n).toLocaleString('en-US'));
  React.useEffect(() => {
    const reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    if (reduce) { setVal(target); return; }
    let raf, liveTimer, current = target;
    const start = performance.now() + delay;
    const ease = (t) => 1 - Math.pow(1 - t, 3);
    const tick = (now) => {
      const t = Math.min(1, Math.max(0, (now - start) / duration));
      setVal(target * ease(t));
      if (t < 1) { raf = requestAnimationFrame(tick); }
      else if (live > 0) {
        liveTimer = setInterval(() => {
          current += Math.floor(Math.random() * live) + 1;
          setVal(current);
        }, 1600);
      }
    };
    raf = requestAnimationFrame(tick);
    return () => { cancelAnimationFrame(raf); clearInterval(liveTimer); };
  }, [target]);
  return <span style={style}>{fmt(val)}</span>;
}

// ── SUCCESS ──
function SuccessScreen({ go, amount }) {
  const amt = parseFloat(amount || '0') || 0;
  const sparks = [0, 60, 120, 180, 240, 300];
  return (
    <ScreenShell top={52}>
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '0 26px', textAlign: 'center' }}>

        {/* check + one-shot burst */}
        <div style={{ position: 'relative', width: 92, height: 92 }}>
          <span className="wd-burst" />
          {sparks.map((deg, i) => {
            const rad = deg * Math.PI / 180, dist = 62;
            return <span key={i} className="wd-spark" style={{ '--sx': `${Math.cos(rad) * dist - 3}px`, '--sy': `${Math.sin(rad) * dist - 3}px`, background: i % 2 ? 'var(--accent-2)' : 'var(--accent)' }} />;
          })}
          <div className="wd-check-pop" style={{ position: 'absolute', inset: 0, borderRadius: '50%', background: W_ACCENT, color: '#fff', display: 'grid', placeItems: 'center', boxShadow: '0 16px 40px -10px var(--ring)' }}>{WIcon.check}</div>
        </div>

        <div className="wd-rise" style={{ animationDelay: '.18s', fontFamily: 'var(--font-display)', fontSize: 28, fontWeight: 800, marginTop: 22 }}>Sent!</div>
        <div className="wd-rise" style={{ animationDelay: '.24s', color: W_MUTED, fontSize: 14.5, marginTop: 7 }}>{amt.toFixed(2)} USDT delivered in seconds.</div>

        {/* you just saved — the win on THIS transfer */}
        <div className="wd-rise" style={{ animationDelay: '.42s', width: '100%', marginTop: 22, background: 'var(--tint)', border: `1.5px solid var(--tint-2)`, borderRadius: 20, padding: '18px 20px' }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7, fontSize: 12.5, fontWeight: 700, letterSpacing: '0.04em', textTransform: 'uppercase', color: 'var(--accent-2)' }}>
            <span style={{ color: W_ACCENT }}>{WIcon.bolt}</span> You just saved
          </div>
          <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'center', gap: 9, marginTop: 8 }}>
            <span style={{ fontFamily: 'var(--font-display)', fontSize: 44, fontWeight: 800, letterSpacing: '-0.03em', color: W_INK, lineHeight: 1 }}>$1.10</span>
            <span style={{ fontSize: 11.5, fontWeight: 800, color: '#fff', background: W_ACCENT, padding: '4px 8px', borderRadius: 8 }}>−50%</span>
          </div>
          <div style={{ fontSize: 13, color: W_MUTED, marginTop: 9 }}>
            <span style={{ fontFamily: 'var(--font-mono)', textDecoration: 'line-through', opacity: 0.7 }}>7 TRX</span>
            <span style={{ margin: '0 6px' }}>→</span>
            <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, color: W_INK }}>3.5 TRX</span>
            <span> kept in your wallet</span>
          </div>
        </div>

        {/* the aggregate — community total, counts up + gently climbs */}
        <div className="wd-rise" style={{ animationDelay: '.62s', marginTop: 18 }}>
          <div style={{ fontSize: 12.5, color: W_MUTED, fontWeight: 600 }}>Together, Gasless users have saved</div>
          <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'center', gap: 1, marginTop: 6 }}>
            <span style={{ fontFamily: 'var(--font-display)', fontSize: 38, fontWeight: 800, color: W_ACCENT, letterSpacing: '-0.03em', lineHeight: 1 }}>$</span>
            <SavingsCount target={159000} delay={650} duration={1700} live={4}
              style={{ fontFamily: 'var(--font-display)', fontSize: 38, fontWeight: 800, color: W_ACCENT, letterSpacing: '-0.03em', lineHeight: 1, fontVariantNumeric: 'tabular-nums' }} />
          </div>
          <div style={{ fontSize: 12.5, color: W_MUTED, marginTop: 6 }}>in network fees — and counting</div>
        </div>

        {/* return to start */}
        <div className="wd-rise" style={{ animationDelay: '.82s', display: 'flex', gap: 10, marginTop: 26, width: '100%' }}>
          <button onClick={() => go('send')} style={{ flex: 1, border: `1.5px solid ${W_LINE}`, background: '#fff', color: W_INK, borderRadius: 14, padding: '13px 0', fontSize: 14.5, fontWeight: 600, cursor: 'pointer', fontFamily: 'var(--font-body)' }}>Send again</button>
          <button onClick={() => go('home')} style={{ flex: 1, border: 'none', background: W_DARK, color: '#fff', borderRadius: 14, padding: '13px 0', fontSize: 14.5, fontWeight: 700, cursor: 'pointer', fontFamily: 'var(--font-body)' }}>Back to wallet</button>
        </div>
      </div>
    </ScreenShell>);

}

// ── iOS-style navigation stack ──
// Depth defines forward (push, slide in from right) vs back (pop, slide off to right).
const SCREEN_DEPTH = { home: 0, send: 1, review: 2, sending: 2, success: 3 };

function IOSNavigator({ screen, render, depth }) {
  const depthMap = depth || SCREEN_DEPTH;
  const [active, setActive] = React.useState(screen);
  const [out, setOut] = React.useState(null); // { key, dir }
  const depthRef = React.useRef(depthMap[screen] ?? 0);

  React.useEffect(() => {
    if (screen === active) return;
    const reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    const newDepth = depthMap[screen] ?? 0;
    const oldDepth = depthRef.current;
    depthRef.current = newDepth;
    const dir = newDepth > oldDepth ? 'push' : newDepth < oldDepth ? 'pop' : 'fade';
    if (reduce) { setActive(screen); setOut(null); return; }
    setOut({ key: active, dir });
    setActive(screen);
  }, [screen]); // eslint-disable-line

  // Fallback cleanup in case animationend never fires (throttled/background tab,
  // interrupted animation) — never leave a stale outgoing pane on top.
  React.useEffect(() => {
    if (!out) return;
    const t = setTimeout(() => setOut(null), 650);
    return () => clearTimeout(t);
  }, [out]);

  const dir = out ? out.dir : null;
  const inCls = dir === 'push' ? 'nav-push-in' : dir === 'pop' ? 'nav-pop-in' : dir === 'fade' ? 'nav-fade-in' : '';
  const outCls = dir === 'push' ? 'nav-push-out' : dir === 'pop' ? 'nav-pop-out' : dir === 'fade' ? 'nav-fade-out' : '';
  // incoming sits on top for push/fade; on pop the leaving screen slides off over it
  const inZ = dir === 'pop' ? 1 : 2;
  const outZ = dir === 'pop' ? 2 : 1;

  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      <div key={active} className={`nav-pane ${out ? inCls : ''}`} style={{ zIndex: inZ }}>
        {render(active)}
        {dir === 'pop' && <div className="nav-scrim nav-scrim-pop" />}
      </div>
      {out &&
      <div key={out.key} className={`nav-pane ${outCls}`} style={{ zIndex: outZ }}
        onAnimationEnd={() => setOut(null)}>
          {render(out.key)}
          {dir === 'push' && <div className="nav-scrim nav-scrim-push" />}
        </div>
      }
    </div>);

}

function WalletDemo({ scale = 1 }) {
  const [screen, setScreen] = React.useState('home');
  const [amount, setAmount] = React.useState('250');
  const go = (s) => setScreen(s);
  const confirm = () => {setScreen('sending');setTimeout(() => setScreen('success'), 1100);};

  const renderScreen = (key) => {
    switch (key) {
      case 'home': return <HomeScreen go={go} />;
      case 'send': return <SendScreen go={go} amount={amount} setAmount={setAmount} />;
      case 'review': return <ReviewScreen go={go} amount={amount} onConfirm={confirm} />;
      case 'sending': return (
          <ScreenShell top={52}>
            <div style={{ flex: 1, display: 'grid', placeItems: 'center' }}>
              <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 16, color: W_MUTED }}>
                <div className="wd-spin" style={{ width: 46, height: 46, borderRadius: '50%', border: `3px solid ${W_LINE}`, borderTopColor: W_ACCENT }} />
                <span style={{ fontSize: 14, fontWeight: 600 }}>Signing &amp; broadcasting…</span>
              </div>
            </div>
          </ScreenShell>);
      case 'success': return <SuccessScreen go={go} amount={amount} />;
      default: return null;
    }
  };

  return (
    <div style={{ transform: `scale(${scale})`, transformOrigin: 'center top' }}>
      <IOSDevice width={392} height={812} statusBar={<WalletStatusBar />} hideIsland>
        <IOSNavigator screen={screen} render={renderScreen} />
      </IOSDevice>
    </div>);

}

window.WalletDemo = WalletDemo;

// Shared helpers reused by no-trx-demo.jsx (the beginners-section demo)
Object.assign(window, { WalletStatusBar, ScreenShell, USDT, TronMark, WIcon, Tooltip, IOSNavigator, SavingsCount });