// Mobile — Calls (history list + in-call screen + incoming-call sheet)
// Uses DIDComm voice/video. Reuses MIc icon set + dark surface tokens.

// ─── Demo data ──────────────────────────────────────────────
const M_CALL_LOG = [
  { name: 'Rohan Sharma',     initials: 'RS', bg: '#F472B6', avatar: 'https://i.pravatar.cc/120?img=12', dir: 'out',    kind: 'video', when: 'Today, 10:42',    dur: '24:08', conn: 'internet' },
  { name: 'Aishwarya Iyer',   initials: 'AI', bg: '#EF4444', avatar: 'https://i.pravatar.cc/120?img=49', dir: 'missed', kind: 'audio', when: 'Today, 09:14',    dur: null,    conn: 'ble' },
  { name: 'Priya Nair',       initials: 'PN', bg: '#22D3EE', avatar: 'https://i.pravatar.cc/120?img=47', dir: 'in',     kind: 'audio', when: 'Yesterday',         dur: '6:12',  conn: 'ble' },
  { name: 'Ajna · iPad team', initials: 'AT', bg: 'linear-gradient(135deg,#6EE7B7,#60A5FA)', isGroup: true, members: 5, dir: 'out',    kind: 'video', when: 'Yesterday',         dur: '48:30', conn: 'mesh' },
  { name: 'Arjun Mehta',      initials: 'AM', bg: '#6EE7B7', avatar: 'https://i.pravatar.cc/120?img=14', dir: 'out',    kind: 'audio', when: 'Mon · 17:21',       dur: '2:48',  conn: 'mesh' },
  { name: 'Dimitri Volkov',   initials: 'DV', bg: '#FBBF24', avatar: 'https://i.pravatar.cc/120?img=33', dir: 'missed', kind: 'video', when: 'Sun · 21:08',       dur: null,    conn: 'internet' },
  { name: 'Maya Chen',        initials: 'MC', bg: '#A78BFA',                                              dir: 'in',     kind: 'audio', when: 'Sat · 11:02',       dur: '0:38',  conn: 'mediator' },
];

// Direction icon (in / out / missed) — tiny arrow
function CallDirIcon({ dir }) {
  if (dir === 'missed') return (
    <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="#F87171" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 19 19 5"/><path d="M19 14V5h-9"/>
    </svg>
  );
  if (dir === 'in') return (
    <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke={M_ACCENT} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 19 19 5"/><path d="M19 14V5h-9"/>
    </svg>
  );
  return (
    <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke={M_FG_MUTED} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M19 5 5 19"/><path d="M5 10v9h9"/>
    </svg>
  );
}

// ─── Calls list screen ──────────────────────────────────────
function MScreenCalls({ onCall, onIncomingDemo, onShowEmpty, headerless }) {
  const [tab, setTab] = React.useState('all'); // all | missed
  const filtered = tab === 'missed' ? M_CALL_LOG.filter(c => c.dir === 'missed') : M_CALL_LOG;

  return (
    <div style={{ padding: headerless ? '6px 16px 100px' : '20px 16px 100px', height: '100%', overflowY: 'auto', boxSizing: 'border-box' }}>
      {/* Header */}
      {!headerless && (
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 14 }}>
        <div style={{ fontSize: 22, fontWeight: 600, fontFamily: 'Geist', letterSpacing: '-0.02em', color: M_FG }}>Calls</div>
        <div style={{ display: 'flex', gap: 8 }}>
          {onShowEmpty && <button onClick={onShowEmpty} style={{ height: 32, padding: '0 11px', borderRadius: 9, background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT, color: M_FG_MUTED, fontSize: 11.5, fontFamily: 'inherit', cursor: 'pointer' }}>Empty state</button>}
          <button style={{ width: 32, height: 32, borderRadius: 9, background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT, color: M_FG_MUTED, display: 'grid', placeItems: 'center', cursor: 'pointer' }} aria-label="Search">{MIc.search({ s: 14 })}</button>
          <button onClick={() => onCall && onCall(null, 'audio')} style={{ width: 32, height: 32, borderRadius: 9, background: 'linear-gradient(135deg, ' + M_ACCENT + ', ' + M_ACCENT_B + ')', border: 0, color: '#0B0C0F', display: 'grid', placeItems: 'center', cursor: 'pointer' }} aria-label="New call">{MIc.plus({ s: 14 })}</button>
        </div>
      </div>
      )}

      {/* All / Missed segmented */}
      <div style={{
        display: 'inline-flex', padding: 3, gap: 0,
        background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT,
        borderRadius: 10, marginBottom: 16,
      }}>
        {['all', 'missed'].map(id => {
          const active = tab === id;
          return (
            <button key={id} onClick={() => setTab(id)} style={{
              display: 'inline-flex', alignItems: 'center', gap: 6,
              padding: '6px 14px', borderRadius: 8,
              background: active ? M_BG : 'transparent',
              color: active ? M_FG : M_FG_MUTED,
              border: '1px solid ' + (active ? M_BORDER_SOFT : 'transparent'),
              fontSize: 12, fontWeight: active ? 600 : 500, fontFamily: 'inherit', cursor: 'pointer',
            }}>
              {id === 'all' ? 'All' : 'Missed'}
              {id === 'missed' && <span style={{
                fontSize: 9.5, padding: '0 5px', borderRadius: 4,
                background: 'rgba(248,113,113,0.18)', color: '#F87171',
                fontFamily: 'JetBrains Mono, monospace', fontWeight: 700,
              }}>2</span>}
            </button>
          );
        })}
      </div>

      {/* Quick dial — favorites, one tap to call */}
      <div style={{ fontSize: 10, fontWeight: 600, color: M_FG_DIM, letterSpacing: '0.12em', textTransform: 'uppercase', marginBottom: 10 }}>Quick dial</div>
      <div style={{ display: 'flex', gap: 16, marginBottom: 16, overflowX: 'auto', scrollbarWidth: 'none', paddingBottom: 4 }}>
        {M_CALL_LOG.filter(c => !c.isGroup).slice(0, 4).map(c => (
          <button key={c.name} onClick={() => onCall && onCall(c, 'audio')} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, background: 'transparent', border: 0, cursor: 'pointer', padding: 0, flexShrink: 0, fontFamily: 'inherit' }}>
            <div style={{ position: 'relative' }}>
              <MTile size={52} bg={c.bg} color="#fff" label={c.initials} img={c.avatar} radius={26}/>
              <div style={{ position: 'absolute', bottom: -2, right: -2, width: 20, height: 20, borderRadius: 10, background: M_ACCENT, color: '#0B0C0F', display: 'grid', placeItems: 'center', border: '2px solid ' + M_BG }}>{MIc.calls({ s: 10 })}</div>
            </div>
            <span style={{ fontSize: 10.5, color: M_FG_MUTED, fontFamily: 'Geist', maxWidth: 56, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.name.split(' ')[0]}</span>
          </button>
        ))}
        <button onClick={() => onCall && onCall(null, 'video')} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, background: 'transparent', border: 0, cursor: 'pointer', padding: 0, flexShrink: 0, fontFamily: 'inherit' }}>
          <div style={{ width: 52, height: 52, borderRadius: 26, background: M_SURFACE, border: '1px dashed ' + M_BORDER, color: M_FG_MUTED, display: 'grid', placeItems: 'center' }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
          </div>
          <span style={{ fontSize: 10.5, color: M_FG_MUTED, fontFamily: 'Geist' }}>Video</span>
        </button>
      </div>

      {/* Demo incoming-call trigger (review-only) */}
      <button onClick={onIncomingDemo} style={{
        width: '100%', display: 'flex', alignItems: 'center', gap: 10,
        padding: '11px 14px', marginBottom: 18,
        background: 'rgba(167,139,250,0.06)', border: '1px dashed rgba(167,139,250,0.32)',
        color: '#C4B5FD', fontSize: 12, fontFamily: 'inherit', cursor: 'pointer',
        borderRadius: 12,
      }}>
        <span style={{ display: 'grid', placeItems: 'center' }}>{MIc.bolt({ s: 13 })}</span>
        <span style={{ flex: 1, textAlign: 'left' }}>Simulate incoming call (demo)</span>
        <span style={{ color: '#C4B5FD' }}>›</span>
      </button>

      {/* Call list */}
      <div style={{ fontSize: 10, fontWeight: 600, color: M_FG_DIM, letterSpacing: '0.12em', textTransform: 'uppercase', marginBottom: 8 }}>Recent</div>
      <div style={{ background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT, borderRadius: 14, overflow: 'hidden' }}>
        {filtered.map((c, i, a) => (
          <button key={i}
            onClick={() => onCall && onCall(c, c.kind)}
            style={{
              display: 'flex', alignItems: 'center', gap: 12,
              width: '100%', padding: '12px 14px',
              background: 'transparent', border: 0,
              borderBottom: i < a.length - 1 ? '1px solid ' + M_BORDER_SOFT : 'none',
              textAlign: 'left', cursor: 'pointer', color: M_FG, fontFamily: 'inherit',
            }}>
            <div style={{ position: 'relative', flexShrink: 0 }}>
              {c.isGroup ? (
                <div style={{ width: 40, height: 40, borderRadius: 20, background: c.bg, display: 'grid', placeItems: 'center', color: '#0B0C0F', fontFamily: 'Geist', fontWeight: 700, fontSize: 11 }}>{c.members}</div>
              ) : (
                <MTile size={40} bg={c.bg} color="#fff" label={c.initials} img={c.avatar} radius={20}/>
              )}
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{
                fontSize: 13.5, fontWeight: c.dir === 'missed' ? 600 : 500,
                color: c.dir === 'missed' ? '#F87171' : M_FG,
                whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
              }}>{c.name}</div>
              <div style={{ display: 'flex', alignItems: 'center', gap: 5, marginTop: 3 }}>
                <CallDirIcon dir={c.dir}/>
                <span style={{ fontSize: 11, color: M_FG_MUTED }}>
                  {c.kind === 'video' ? 'Video' : 'Audio'}
                  {c.dur ? ' · ' + c.dur : c.dir === 'missed' ? ' · Missed' : ''}
                  {' · '}
                </span>
                <span style={{ fontSize: 10.5, color: M_FG_DIM, fontFamily: 'JetBrains Mono, monospace' }}>{c.when}</span>
              </div>
            </div>
            <MConnChip type={c.conn}/>
            <span onClick={(e) => { e.stopPropagation(); onCall && onCall(c, c.kind); }} style={{
              width: 30, height: 30, borderRadius: 15,
              background: c.dir === 'missed' ? M_ACCENT : c.kind === 'video' ? 'rgba(96,165,250,0.16)' : 'rgba(110,231,183,0.16)',
              color: c.dir === 'missed' ? '#0B0C0F' : c.kind === 'video' ? M_ACCENT_B : M_ACCENT,
              display: 'grid', placeItems: 'center', flexShrink: 0,
            }}>
              {c.kind === 'video'
                ? <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
                : MIc.calls({ s: 14 })
              }
            </span>
          </button>
        ))}
      </div>

      {/* Privacy footer */}
      <div style={{
        marginTop: 16, padding: '10px 12px',
        background: 'rgba(110,231,183,0.05)', border: '1px solid rgba(110,231,183,0.14)',
        borderRadius: 10, display: 'flex', alignItems: 'center', gap: 9,
      }}>
        <span style={{ color: M_ACCENT, display: 'grid', placeItems: 'center' }}>{MIc.shield({ s: 13 })}</span>
        <div style={{ flex: 1, fontSize: 11, color: M_FG_MUTED, lineHeight: 1.4 }}>
          Calls are E2E encrypted via DIDComm WebRTC. Ajna never sees who you talk to.
        </div>
      </div>
    </div>
  );
}

// ─── Incoming call sheet (full-bleed overlay) ────────────────
function MIncomingCall({ caller, onAccept, onDecline }) {
  const c = caller || M_CALL_LOG[0];
  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 90,
      background: 'radial-gradient(ellipse at 50% 20%, #0f1118 0%, #060708 70%)',
      display: 'flex', flexDirection: 'column',
      animation: 'mIcFade 220ms ease',
    }}>
      <style>{`
        @keyframes mIcFade { from { opacity: 0; } to { opacity: 1; } }
        @keyframes mIcRing { 0%,100% { transform: scale(1); opacity: 0.7; } 50% { transform: scale(1.18); opacity: 0; } }
      `}</style>

      <div style={{ paddingTop: 56, textAlign: 'center', flex: 1 }}>
        <div style={{ fontSize: 11, color: M_FG_DIM, fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.16em', textTransform: 'uppercase' }}>
          Incoming {c.kind === 'video' ? 'video' : 'audio'} call
        </div>

        {/* Avatar with pulsing rings */}
        <div style={{ display: 'grid', placeItems: 'center', margin: '34px auto 16px', position: 'relative', width: 160, height: 160 }}>
          {[0, 1, 2].map(i => (
            <div key={i} style={{
              position: 'absolute', inset: 0, borderRadius: '50%',
              border: '1px solid ' + (c.kind === 'video' ? 'rgba(96,165,250,0.6)' : 'rgba(110,231,183,0.6)'),
              animation: `mIcRing 2s ease-out ${i * 0.5}s infinite`,
            }}/>
          ))}
          <MTile size={130} bg={c.bg} color="#fff" label={c.initials} img={c.avatar} radius={65}/>
        </div>

        <div style={{ fontSize: 22, fontWeight: 600, fontFamily: 'Geist, Inter, sans-serif', letterSpacing: '-0.02em' }}>{c.name}</div>
        <div style={{ fontSize: 12.5, color: M_FG_MUTED, marginTop: 6, fontFamily: 'JetBrains Mono, monospace' }}>
          {c.kind === 'video' ? 'video' : 'voice'} · via {c.conn === 'ble' ? 'BLE mesh' : c.conn === 'mesh' ? 'mesh' : c.conn === 'mediator' ? 'mediator' : 'internet'}
        </div>

        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 5, marginTop: 14, padding: '4px 10px', borderRadius: 999, background: 'rgba(110,231,183,0.12)', color: M_ACCENT, fontSize: 10.5, fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.06em', textTransform: 'uppercase', fontWeight: 600 }}>
          {MIc.shield({ s: 11 })} verified · e2e
        </div>
      </div>

      {/* Quick actions row */}
      <div style={{ display: 'flex', justifyContent: 'space-around', padding: '0 32px 14px' }}>
        {[
          { id: 'remind', icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="13" r="9"/><path d="M12 7v6l4 2M5 3 2 6m17-3 3 3"/></svg>, label: 'Remind' },
          { id: 'msg',    icon: MIc.chat({ s: 16 }), label: 'Message' },
        ].map(a => (
          <button key={a.id} style={{
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
            background: 'transparent', border: 0, color: M_FG_MUTED, fontFamily: 'inherit', cursor: 'pointer',
          }}>
            <span style={{
              width: 42, height: 42, borderRadius: 21,
              background: 'var(--m-tint)', border: '1px solid ' + M_BORDER_SOFT,
              display: 'grid', placeItems: 'center', color: M_FG,
            }}>{a.icon}</span>
            <span style={{ fontSize: 11 }}>{a.label}</span>
          </button>
        ))}
      </div>

      {/* Decline / Accept */}
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '8px 40px 36px' }}>
        <button onClick={onDecline} style={{
          width: 68, height: 68, borderRadius: 34,
          background: '#EF4444', border: 0, color: '#fff',
          display: 'grid', placeItems: 'center', cursor: 'pointer',
          boxShadow: '0 10px 30px -8px rgba(239,68,68,0.6)',
        }} aria-label="Decline">
          <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" style={{ transform: 'rotate(135deg)' }}>
            <path d="M4 5c0 9 6 15 15 15l2-4-5-2-2 2c-3-1-5-3-6-6l2-2-2-5z"/>
          </svg>
        </button>
        <button onClick={onAccept} style={{
          width: 68, height: 68, borderRadius: 34,
          background: c.kind === 'video' ? '#3B82F6' : '#22C55E',
          border: 0, color: '#fff',
          display: 'grid', placeItems: 'center', cursor: 'pointer',
          boxShadow: c.kind === 'video' ? '0 10px 30px -8px rgba(59,130,246,0.6)' : '0 10px 30px -8px rgba(34,197,94,0.6)',
        }} aria-label="Accept">
          {c.kind === 'video'
            ? <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
            : <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="M4 5c0 9 6 15 15 15l2-4-5-2-2 2c-3-1-5-3-6-6l2-2-2-5z"/></svg>}
        </button>
      </div>
      <MHomeIndicator light/>
    </div>
  );
}

// ─── In-call screen (active call) ────────────────────────────
// Supports audio mode (avatar centered) and video mode (remote feed + PiP self).
function MInCall({ caller, kind = 'audio', onHangUp }) {
  const c = caller || M_CALL_LOG[0];
  const [elapsed, setElapsed] = React.useState(12); // seconds — visual only
  const [muted, setMuted] = React.useState(false);
  const [speaker, setSpeaker] = React.useState(true);
  const [videoOn, setVideoOn] = React.useState(kind === 'video');

  React.useEffect(() => {
    const id = setInterval(() => setElapsed(s => s + 1), 1000);
    return () => clearInterval(id);
  }, []);
  const fmt = (s) => `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;

  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 85,
      background: videoOn
        ? 'linear-gradient(160deg, #1a1f2b 0%, #0a0b0d 100%)'
        : 'radial-gradient(ellipse at 50% 20%, #161824 0%, #050608 70%)',
      display: 'flex', flexDirection: 'column',
      animation: 'mIcFade 200ms ease',
    }}>
      {/* Video mode — remote feed simulated as gradient with bottom self-PiP */}
      {videoOn && (
        <>
          <div style={{ position: 'absolute', inset: 0 }}>
            <BriefImageFallbackOrAvatar c={c}/>
          </div>
          {/* self PiP */}
          <div style={{
            position: 'absolute', top: 56, right: 16,
            width: 100, height: 140, borderRadius: 14, overflow: 'hidden',
            background: 'linear-gradient(180deg, #3a2a44, #1f1a2b)',
            border: '1px solid ' + M_BORDER, zIndex: 2,
            boxShadow: '0 8px 24px rgba(0,0,0,0.5)',
          }}>
            <div style={{ width: '100%', height: '100%', background: 'radial-gradient(ellipse at 50% 35%, var(--m-tint), transparent 60%)' }}/>
            <div style={{ position: 'absolute', bottom: 6, left: 6, fontSize: 9.5, color: '#fff', fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.08em', padding: '2px 6px', background: 'rgba(0,0,0,0.4)', borderRadius: 4 }}>You</div>
          </div>
        </>
      )}

      {/* Top bar */}
      <div style={{
        position: 'relative', zIndex: 1,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '12px 18px',
      }}>
        <button style={{ width: 32, height: 32, borderRadius: 10, background: 'rgba(0,0,0,0.4)', backdropFilter: 'blur(8px)', border: '1px solid ' + M_BORDER_SOFT, color: M_FG, display: 'grid', placeItems: 'center', cursor: 'pointer' }} aria-label="Minimize">
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M19 12H5"/></svg>
        </button>
        <div style={{ textAlign: 'center' }}>
          <div style={{ fontSize: 13.5, fontWeight: 600, color: M_FG, fontFamily: 'Geist, Inter, sans-serif', letterSpacing: '-0.01em' }}>{c.name}</div>
          <div style={{
            display: 'inline-flex', alignItems: 'center', gap: 5,
            fontSize: 10.5, color: M_FG_MUTED, marginTop: 2, fontFamily: 'JetBrains Mono, monospace',
          }}>
            <span style={{ width: 5, height: 5, borderRadius: 3, background: M_ACCENT, boxShadow: '0 0 8px ' + M_ACCENT, animation: 'mIcRing 1.6s ease infinite' }}/>
            {fmt(elapsed)} · E2E
          </div>
        </div>
        <button style={{ width: 32, height: 32, borderRadius: 10, background: 'rgba(0,0,0,0.4)', backdropFilter: 'blur(8px)', border: '1px solid ' + M_BORDER_SOFT, color: M_FG, display: 'grid', placeItems: 'center', cursor: 'pointer' }} aria-label="More">{MIc.more({ s: 16 })}</button>
      </div>

      {/* Centerpiece (audio mode only) */}
      {!videoOn && (
        <div style={{ flex: 1, display: 'grid', placeItems: 'center', position: 'relative', zIndex: 1 }}>
          <div style={{ position: 'relative', width: 200, height: 200, display: 'grid', placeItems: 'center' }}>
            {[0, 1].map(i => (
              <div key={i} style={{
                position: 'absolute', inset: 0, borderRadius: '50%',
                border: '1px solid rgba(110,231,183,0.4)',
                animation: `mIcRing 2.4s ease-out ${i * 0.7}s infinite`,
              }}/>
            ))}
            <MTile size={150} bg={c.bg} color="#fff" label={c.initials} img={c.avatar} radius={75}/>
          </div>
        </div>
      )}

      {videoOn && <div style={{ flex: 1 }}/>}

      {/* Call connection chip */}
      <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 14, position: 'relative', zIndex: 1 }}>
        <div style={{
          display: 'inline-flex', alignItems: 'center', gap: 6,
          padding: '5px 11px', borderRadius: 999,
          background: 'rgba(0,0,0,0.5)', backdropFilter: 'blur(10px)',
          border: '1px solid ' + M_BORDER_SOFT,
        }}>
          <MConnChip type={c.conn}/>
          <span style={{ fontSize: 10.5, color: M_FG_MUTED, fontFamily: 'JetBrains Mono, monospace' }}>32 kbps · 24 ms</span>
        </div>
      </div>

      {/* Action grid */}
      <div style={{
        display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 8,
        padding: '0 22px 12px', position: 'relative', zIndex: 1,
      }}>
        <CallBtn
          icon={muted
            ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M9 9v2a3 3 0 0 0 5 2.2M15 9V5a3 3 0 0 0-6 0v1"/><path d="M3 3l18 18M5 11a7 7 0 0 0 10 6"/></svg>
            : <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="3" width="6" height="12" rx="3"/><path d="M5 11a7 7 0 0 0 14 0M12 18v3"/></svg>
          }
          label={muted ? 'Unmute' : 'Mute'}
          active={muted}
          onClick={() => setMuted(m => !m)}
        />
        <CallBtn
          icon={<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="5" y="3" width="14" height="18" rx="2"/><path d="M11 18h2"/></svg>}
          label={speaker ? 'Speaker' : 'Phone'}
          active={speaker}
          onClick={() => setSpeaker(s => !s)}
        />
        <CallBtn
          icon={videoOn
            ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
            : <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/><path d="m3 3 18 18" stroke="currentColor"/></svg>
          }
          label={videoOn ? 'Video' : 'Off'}
          active={videoOn}
          onClick={() => setVideoOn(v => !v)}
        />
        <CallBtn
          icon={MIc.plus({ s: 18 })}
          label="Add"
          onClick={() => {}}
        />
      </div>

      {/* Hang up */}
      <div style={{ display: 'flex', justifyContent: 'center', paddingBottom: 28, position: 'relative', zIndex: 1 }}>
        <button onClick={onHangUp} style={{
          width: 64, height: 64, borderRadius: 32,
          background: '#EF4444', border: 0, color: '#fff',
          display: 'grid', placeItems: 'center', cursor: 'pointer',
          boxShadow: '0 12px 32px -8px rgba(239,68,68,0.55)',
        }} aria-label="End call">
          <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" style={{ transform: 'rotate(135deg)' }}>
            <path d="M4 5c0 9 6 15 15 15l2-4-5-2-2 2c-3-1-5-3-6-6l2-2-2-5z"/>
          </svg>
        </button>
      </div>
      <MHomeIndicator light/>
    </div>
  );
}

// Small button used inside the in-call action grid
function CallBtn({ icon, label, active, onClick }) {
  return (
    <button onClick={onClick} style={{
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
      padding: '12px 6px', borderRadius: 14,
      background: active ? 'rgba(255,255,255,0.18)' : 'var(--m-tint)',
      border: '1px solid ' + (active ? 'rgba(255,255,255,0.28)' : M_BORDER_SOFT),
      color: M_FG, cursor: 'pointer', fontFamily: 'inherit',
      backdropFilter: 'blur(8px)',
    }}>
      <span style={{ display: 'grid', placeItems: 'center' }}>{icon}</span>
      <span style={{ fontSize: 10.5, fontWeight: 500 }}>{label}</span>
    </button>
  );
}

// Tiny stand-in for a remote video feed — uses the peer's avatar tinted full-bleed
function BriefImageFallbackOrAvatar({ c }) {
  return (
    <div style={{
      width: '100%', height: '100%',
      background: c.avatar
        ? `center/cover url(${c.avatar})`
        : (typeof c.bg === 'string' ? c.bg : '#1F2937'),
      filter: 'saturate(0.85)',
    }}>
      <div style={{ width: '100%', height: '100%', background: 'radial-gradient(ellipse at 50% 30%, transparent, rgba(0,0,0,0.5))' }}/>
    </div>
  );
}

// ─── Empty calls state (zero history) ───────────────────────
// Designed zero-state: friendly illustration, primary CTAs, and a few
// suggested peers pulled from the mesh so the page never feels dead.
const M_CALL_SUGGESTED = [
  { name: 'Rohan Sharma',   initials: 'RS', bg: '#F472B6', avatar: 'https://i.pravatar.cc/120?img=12', conn: 'internet', sub: 'Online' },
  { name: 'Aishwarya Iyer', initials: 'AI', bg: '#EF4444', avatar: 'https://i.pravatar.cc/120?img=49', conn: 'ble',      sub: 'Nearby · BLE' },
  { name: 'Priya Nair',     initials: 'PN', bg: '#22D3EE', avatar: 'https://i.pravatar.cc/120?img=47', conn: 'mesh',     sub: 'On mesh' },
];

function MScreenCallsEmpty({ onCall, onShowHistory }) {
  return (
    <div style={{ padding: '20px 16px 100px', height: '100%', overflowY: 'auto', boxSizing: 'border-box' }}>
      {/* Header */}
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 24 }}>
        <div style={{ fontSize: 22, fontWeight: 600, fontFamily: 'Geist', letterSpacing: '-0.02em', color: M_FG }}>Calls</div>
        <div style={{ display: 'flex', gap: 8 }}>
          {onShowHistory && <button onClick={onShowHistory} style={{ height: 32, padding: '0 11px', borderRadius: 9, background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT, color: M_FG_MUTED, fontSize: 11.5, fontFamily: 'inherit', cursor: 'pointer' }}>Preview history</button>}
          <button onClick={() => onCall && onCall(null, 'audio')} style={{ width: 32, height: 32, borderRadius: 9, background: 'linear-gradient(135deg, ' + M_ACCENT + ', ' + M_ACCENT_B + ')', border: 0, color: '#0B0C0F', display: 'grid', placeItems: 'center', cursor: 'pointer' }} aria-label="New call">{MIc.plus({ s: 14 })}</button>
        </div>
      </div>

      {/* Zero-state hero */}
      <div style={{ textAlign: 'center', padding: '28px 16px 30px' }}>
        <div style={{ position: 'relative', width: 110, height: 110, margin: '0 auto 22px', display: 'grid', placeItems: 'center' }}>
          {/* concentric rings */}
          {[0, 1, 2].map(i => (
            <div key={i} style={{
              position: 'absolute', borderRadius: '50%',
              inset: i * 16,
              border: '1px solid rgba(110,231,183,' + (0.30 - i * 0.08) + ')',
            }}/>
          ))}
          <div style={{
            width: 64, height: 64, borderRadius: 20,
            background: 'linear-gradient(135deg, rgba(110,231,183,0.18), rgba(96,165,250,0.18))',
            border: '1px solid rgba(110,231,183,0.32)',
            display: 'grid', placeItems: 'center', color: M_ACCENT,
          }}>
            {MIc.calls({ s: 28 })}
          </div>
        </div>
        <div style={{ fontSize: 18, fontWeight: 700, fontFamily: 'Geist', letterSpacing: '-0.025em', color: M_FG, marginBottom: 8 }}>No calls yet</div>
        <div style={{ fontSize: 13, color: M_FG_MUTED, lineHeight: 1.5, maxWidth: 280, margin: '0 auto' }}>
          Start an end-to-end encrypted voice or video call over DIDComm — no phone number needed.
        </div>
      </div>

      {/* Primary CTAs */}
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10, marginBottom: 26 }}>
        <button onClick={() => onCall && onCall(null, 'audio')} style={{
          display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 10,
          padding: '15px 14px', borderRadius: 14,
          background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT,
          color: M_FG, fontFamily: 'inherit', cursor: 'pointer', textAlign: 'left',
        }}>
          <div style={{ width: 36, height: 36, borderRadius: 10, background: 'rgba(110,231,183,0.16)', color: M_ACCENT, display: 'grid', placeItems: 'center' }}>{MIc.calls({ s: 18 })}</div>
          <div>
            <div style={{ fontSize: 13.5, fontWeight: 600, fontFamily: 'Geist', letterSpacing: '-0.01em' }}>Audio call</div>
            <div style={{ fontSize: 10.5, color: M_FG_DIM, marginTop: 2, fontFamily: 'JetBrains Mono, monospace' }}>DIDComm voice</div>
          </div>
        </button>
        <button onClick={() => onCall && onCall(null, 'video')} style={{
          display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 10,
          padding: '15px 14px', borderRadius: 14,
          background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT,
          color: M_FG, fontFamily: 'inherit', cursor: 'pointer', textAlign: 'left',
        }}>
          <div style={{ width: 36, height: 36, borderRadius: 10, background: 'rgba(96,165,250,0.16)', color: M_ACCENT_B, display: 'grid', placeItems: 'center' }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
          </div>
          <div>
            <div style={{ fontSize: 13.5, fontWeight: 600, fontFamily: 'Geist', letterSpacing: '-0.01em' }}>Video call</div>
            <div style={{ fontSize: 10.5, color: M_FG_DIM, marginTop: 2, fontFamily: 'JetBrains Mono, monospace' }}>1:1 or group</div>
          </div>
        </button>
      </div>

      {/* Suggested peers */}
      <div style={{ fontSize: 10, fontWeight: 600, color: M_FG_DIM, letterSpacing: '0.12em', textTransform: 'uppercase', marginBottom: 8, fontFamily: 'JetBrains Mono, monospace' }}>Suggested · from your mesh</div>
      <div style={{ background: M_SURFACE, border: '1px solid ' + M_BORDER_SOFT, borderRadius: 14, overflow: 'hidden', marginBottom: 18 }}>
        {M_CALL_SUGGESTED.map((c, i, a) => (
          <div key={c.name} style={{
            display: 'flex', alignItems: 'center', gap: 12,
            padding: '11px 14px',
            borderBottom: i < a.length - 1 ? '1px solid ' + M_BORDER_SOFT : 'none',
          }}>
            <MTile size={38} bg={c.bg} color="#fff" label={c.initials} img={c.avatar} radius={19}/>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 13, fontWeight: 500, color: M_FG, fontFamily: 'Geist', letterSpacing: '-0.01em', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.name}</div>
              <div style={{ fontSize: 10.5, color: M_FG_DIM, fontFamily: 'JetBrains Mono, monospace', marginTop: 2 }}>{c.sub}</div>
            </div>
            <MConnChip type={c.conn}/>
            <div style={{ display: 'flex', gap: 6 }}>
              <button onClick={() => onCall && onCall(c, 'audio')} aria-label={'Call ' + c.name} style={{ width: 32, height: 32, borderRadius: 16, background: 'rgba(110,231,183,0.14)', border: 0, color: M_ACCENT, display: 'grid', placeItems: 'center', cursor: 'pointer' }}>{MIc.calls({ s: 14 })}</button>
              <button onClick={() => onCall && onCall(c, 'video')} aria-label={'Video ' + c.name} style={{ width: 32, height: 32, borderRadius: 16, background: 'rgba(96,165,250,0.14)', border: 0, color: M_ACCENT_B, display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m23 7-7 5 7 5z"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
              </button>
            </div>
          </div>
        ))}
      </div>

      {/* Privacy footer */}
      <div style={{
        padding: '10px 12px',
        background: 'rgba(110,231,183,0.05)', border: '1px solid rgba(110,231,183,0.14)',
        borderRadius: 10, display: 'flex', alignItems: 'center', gap: 9,
      }}>
        <span style={{ color: M_ACCENT, display: 'grid', placeItems: 'center' }}>{MIc.shield({ s: 13 })}</span>
        <div style={{ flex: 1, fontSize: 11, color: M_FG_MUTED, lineHeight: 1.4 }}>
          Calls are E2E encrypted via DIDComm WebRTC. Ajna never sees who you talk to.
        </div>
      </div>
    </div>
  );
}

Object.assign(window, {
  M_CALL_LOG, MScreenCalls, MScreenCallsEmpty, M_CALL_SUGGESTED, MIncomingCall, MInCall,
});
