// MobileAccount.jsx — mobile account dashboard with vertical sections + scrollable nav pills
// SECURITY: identity from props (server-validated session). No URL param parsing.

const MobileAccount = ({ user, onLogout, onShop }) => {
  const [view, setView] = React.useState('overview');
  const [orders, setOrders] = React.useState([]);
  const [sub, setSub] = React.useState({ active: false, plan: 'NONE', frequency: '--', bag: '--', grind: '--', price: 0, next: '--', cardBrand: '--', cardLast4: '0000', history: [] });

  React.useEffect(() => {
    window.OBC_API.getOrders().then(setOrders);
    window.OBC_API.getSubscription().then((s) => { if (s) setSub(s); });
  }, []);

  const navItems = [
    { id: 'overview', label: 'OVERVIEW' },
    { id: 'orders', label: 'ORDERS' },
    { id: 'subscription', label: 'SUB' },
    { id: 'settings', label: 'SETTINGS' },
  ];

  return (
    <section style={{ padding: '20px 16px 60px', maxWidth: 480, margin: '0 auto' }}>
      {/* Welcome strip */}
      <div style={{ marginBottom: 16 }}>
        <div style={{ fontSize: 11, color: '#00FF41', letterSpacing: '0.22em', marginBottom: 4 }}>$ /account</div>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 8, flexWrap: 'wrap' }}>
          <span style={{ fontSize: 20, fontWeight: 800, color: '#FFF', letterSpacing: '0.04em' }}>{user.handle}</span>
          <span style={{ padding: '3px 8px', border: '1px solid #FFD23F', color: '#FFD23F', fontSize: 10, fontWeight: 700, letterSpacing: '0.18em' }}>[{user.tier}]</span>
        </div>
      </div>

      {/* Nav pills — horizontal scrollable */}
      <div style={{ display: 'flex', gap: 6, marginBottom: 20, overflowX: 'auto', WebkitOverflowScrolling: 'touch', paddingBottom: 4 }}>
        {navItems.map((n) => (
          <button key={n.id} onClick={() => setView(n.id)} style={{
            padding: '10px 16px', whiteSpace: 'nowrap',
            background: view === n.id ? '#00FF41' : 'transparent',
            color: view === n.id ? '#000' : '#E0E0E0',
            border: '1px solid ' + (view === n.id ? '#00FF41' : 'rgba(255,255,255,0.22)'),
            fontFamily: 'inherit', fontSize: 11, fontWeight: 700, letterSpacing: '0.12em',
            cursor: 'pointer', borderRadius: 2, minHeight: 44,
          }}>{view === n.id ? '> ' : ''}{n.label}</button>
        ))}
        <button onClick={onLogout} style={{
          padding: '10px 16px', whiteSpace: 'nowrap',
          background: 'transparent', border: '1px solid #FF3344', color: '#FF3344',
          fontFamily: 'inherit', fontSize: 11, fontWeight: 700, letterSpacing: '0.12em',
          cursor: 'pointer', borderRadius: 2, minHeight: 44,
        }}>EXIT</button>
      </div>

      {/* Content */}
      {view === 'overview' && <MobileOverview user={user} orders={orders} sub={sub} onView={setView} onShop={onShop} />}
      {view === 'orders' && <MobileOrders orders={orders} />}
      {view === 'subscription' && <MobileSubscription sub={sub} onShop={onShop} />}
      {view === 'settings' && <MobileSettings user={user} />}
    </section>
  );
};

// ===================== OVERVIEW =====================
const MobileOverview = ({ user, orders, sub, onView, onShop }) => {
  const open = orders.filter((o) => o.status !== 'DELIVERED').length;
  const total = orders.reduce((s, o) => s + o.total, 0);
  const lastOrder = orders[0];

  return (
    <div>
      {/* 2x2 stat grid */}
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 16 }}>
        <MStat label="OPEN ORDERS" value={open} color="#00FF41" />
        <MStat label="LIFETIME" value={orders.length} color="#FFD23F" />
        <MStat label="SPEND" value={'$' + total} color="#00E5FF" />
        <MStat label="SUBSCRIPTION" value={sub.active ? 'ACTIVE' : 'PAUSED'} color={sub.active ? '#00FF41' : '#FF8C42'} />
      </div>

      {/* Last order card */}
      <MobileCard title="// LAST ORDER" cta="./all-orders" onClick={() => onView('orders')}>
        {lastOrder ? (
          <>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between' }}>
              <span style={{ fontSize: 14, fontWeight: 800, color: '#00FF41', letterSpacing: '0.04em' }}>{lastOrder.id}</span>
              <MobileStatusPill status={lastOrder.status} />
            </div>
            <div style={{ marginTop: 8, fontSize: 12, color: '#BFBFBF', letterSpacing: '0.06em' }}>{lastOrder.placed} · ${lastOrder.total}</div>
            <div style={{ marginTop: 8, fontSize: 12, color: '#FFFFFF', lineHeight: 1.6 }}>
              {lastOrder.items.map((i, k) => (<div key={k}>&gt; {i.name} x {i.qty}</div>))}
            </div>
          </>
        ) : <div style={{ color: '#BFBFBF', fontSize: 12 }}>// no orders yet.</div>}
      </MobileCard>

      {/* Subscription card */}
      <MobileCard title="// SUBSCRIPTION" cta="./manage" onClick={() => onView('subscription')}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between' }}>
          <span style={{ fontSize: 14, fontWeight: 800, color: '#B266FF', letterSpacing: '0.04em' }}>{sub.plan}</span>
          <MobileStatusPill status={sub.active ? 'ACTIVE' : 'PAUSED'} />
        </div>
        <div style={{ marginTop: 8, fontSize: 12, color: '#BFBFBF', letterSpacing: '0.06em' }}>
          next: {sub.next} · {sub.bag}
        </div>
        <div style={{ marginTop: 4, fontSize: 12, color: '#FFFFFF' }}>${sub.price}/mo</div>
      </MobileCard>
    </div>
  );
};

// ===================== SUBSCRIPTION =====================
const MobileSubscription = ({ sub, onShop }) => {
  const [paused, setPaused] = React.useState(!sub.active);
  const [confirming, setConfirming] = React.useState(null);

  const SubActionBtn = ({ children, color = '#00FF41', onClick }) => (
    <button onClick={onClick} style={{
      background: 'transparent', border: '1px solid ' + color, color,
      padding: '10px 14px', fontFamily: 'inherit', fontSize: 11, letterSpacing: '0.12em', fontWeight: 700,
      cursor: 'pointer', borderRadius: 2, minHeight: 44,
    }}>{children}</button>
  );

  return (
    <div>
      <div style={{ fontSize: 11, color: '#BFBFBF', letterSpacing: '0.18em', marginBottom: 14 }}>// recurring caffeine delivery</div>

      {/* Plan card */}
      <div style={{ padding: 14, background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)', marginBottom: 10 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 12 }}>
          <div style={{ fontSize: 18, fontWeight: 800, color: '#B266FF', letterSpacing: '0.04em' }}>{sub.plan}</div>
          <MobileStatusPill status={paused ? 'PAUSED' : 'ACTIVE'} />
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 1, background: 'rgba(255,255,255,0.12)', marginBottom: 14 }}>
          {[
            ['NEXT SHIP', paused ? '— PAUSED —' : sub.next],
            ['FREQUENCY', sub.frequency],
            ['BAG / GRIND', sub.bag + ' · ' + sub.grind],
            ['PRICE', '$' + sub.price + '/mo'],
          ].map(([k, v]) => (
            <div key={k} style={{ background: '#000', padding: '10px 12px' }}>
              <div style={{ fontSize: 9, letterSpacing: '0.18em', color: '#6E6E6E', fontWeight: 700 }}>{k}</div>
              <div style={{ marginTop: 4, fontSize: 12, color: '#FFF', fontWeight: 600 }}>{v}</div>
            </div>
          ))}
        </div>

        <div style={{ fontSize: 11, color: '#6E6E6E', letterSpacing: '0.06em', marginBottom: 14 }}>
          // payment: {sub.cardBrand} •••• {sub.cardLast4}
        </div>

        <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}>
          <SubActionBtn color="#00FF41" onClick={() => onShop()}>./change-bean</SubActionBtn>
          <SubActionBtn color="#FFD23F" onClick={() => setConfirming('pause')}>./{paused ? 'resume' : 'pause'}</SubActionBtn>
          <SubActionBtn color="#FF3344" onClick={() => setConfirming('cancel')}>./cancel</SubActionBtn>
        </div>
      </div>

      {/* Confirm dialog */}
      {confirming && (
        <div style={{
          padding: 14, marginBottom: 10,
          border: '1px solid ' + (confirming === 'cancel' ? '#FF3344' : '#FFD23F'),
          background: '#0A0A0A',
        }}>
          <div style={{
            fontSize: 10, letterSpacing: '0.18em', fontWeight: 700, marginBottom: 8,
            color: confirming === 'cancel' ? '#FF3344' : '#FFD23F',
          }}>
            // CONFIRM ./{confirming}
          </div>
          <div style={{ fontSize: 12, color: '#E0E0E0', lineHeight: 1.7, marginBottom: 12 }}>
            {confirming === 'cancel'
              ? "cancel subscription? your next shipment won't be charged. re-subscribe anytime."
              : (paused ? 'resume subscription? next shipment will be scheduled.'
                        : 'pause subscription? next shipment will be skipped.')}
          </div>
          <div style={{ display: 'flex', gap: 8 }}>
            <SubActionBtn color={confirming === 'cancel' ? '#FF3344' : '#FFD23F'} onClick={() => {
              if (confirming === 'pause') setPaused(!paused);
              setConfirming(null);
            }}>./confirm</SubActionBtn>
            <SubActionBtn color="#BFBFBF" onClick={() => setConfirming(null)}>./abort</SubActionBtn>
          </div>
        </div>
      )}

      {/* Shipment history */}
      <div style={{ padding: 14, background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)' }}>
        <div style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.22em', fontWeight: 700, marginBottom: 10 }}>// SHIPMENT HISTORY</div>
        {sub.history.map((h, i) => (
          <div key={i} style={{
            display: 'flex', justifyContent: 'space-between', padding: '8px 0',
            borderBottom: i === sub.history.length - 1 ? 'none' : '1px dashed rgba(255,255,255,0.12)',
            fontSize: 12,
          }}>
            <span style={{ color: '#E0E0E0' }}>
              <span style={{ color: '#00FF41' }}>{'>'}</span> {h.date} — {h.sku}
            </span>
            <span style={{ color: '#FFF', fontWeight: 600 }}>${h.price}</span>
          </div>
        ))}
      </div>
    </div>
  );
};

const MStat = ({ label, value, color }) => (
  <div style={{ padding: '14px 12px', background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)' }}>
    <div style={{ fontSize: 9, color: '#BFBFBF', letterSpacing: '0.22em', fontWeight: 700 }}>{label}</div>
    <div style={{ marginTop: 6, fontSize: 20, fontWeight: 800, color, letterSpacing: '0.04em' }}>{value}</div>
  </div>
);

const MobileCard = ({ title, children, cta, onClick }) => (
  <div onClick={onClick} style={{
    padding: 14, background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)',
    cursor: onClick ? 'pointer' : 'default', marginBottom: 10,
  }}>
    <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 10 }}>
      <span style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.22em', fontWeight: 700 }}>{title}</span>
      {cta && <span style={{ fontSize: 10, color: '#00FF41', letterSpacing: '0.15em' }}>{cta}</span>}
    </div>
    {children}
  </div>
);

const MobileStatusPill = ({ status }) => {
  const colors = { SHIPPED: '#FFD23F', DELIVERED: '#00FF41', PROCESSING: '#00E5FF', CANCELED: '#FF3344', ACTIVE: '#00FF41', PAUSED: '#FF8C42' };
  const c = colors[status] || '#BFBFBF';
  return <span style={{ padding: '2px 8px', border: '1px solid ' + c, color: c, fontSize: 9, fontWeight: 700, letterSpacing: '0.15em' }}>[{status}]</span>;
};

// ===================== ORDERS =====================
const MobileOrders = ({ orders }) => {
  const [open, setOpen] = React.useState(null);

  return (
    <div>
      <div style={{ fontSize: 11, color: '#BFBFBF', letterSpacing: '0.18em', marginBottom: 14 }}>// {orders.length} orders</div>

      {orders.map((o) => (
        <div key={o.id} style={{ marginBottom: 8 }}>
          <div onClick={() => setOpen(open === o.id ? null : o.id)} style={{
            padding: '14px', background: open === o.id ? 'rgba(0,255,65,0.04)' : '#0A0A0A',
            border: '1px solid rgba(255,255,255,0.22)', cursor: 'pointer',
          }}>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 6 }}>
              <span style={{ color: '#00FF41', fontWeight: 700, fontSize: 13, letterSpacing: '0.04em' }}>{o.id}</span>
              <span style={{ color: '#BFBFBF', fontSize: 11 }}>{open === o.id ? '[-]' : '[+]'}</span>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', fontSize: 12 }}>
              <span style={{ color: '#E0E0E0' }}>{o.placed}</span>
              <MobileStatusPill status={o.status} />
              <span style={{ color: '#FFF', fontWeight: 700 }}>${o.total}</span>
            </div>
          </div>

          {open === o.id && (
            <div style={{ padding: '14px', background: '#0A0A0A', borderTop: 'none', border: '1px solid rgba(255,255,255,0.22)', borderTopWidth: 0 }}>
              <div style={{ fontSize: 10, letterSpacing: '0.22em', color: '#BFBFBF', marginBottom: 8 }}>// ITEMS</div>
              {o.items.map((i, k) => (
                <div key={k} style={{ display: 'flex', justifyContent: 'space-between', padding: '6px 0', borderBottom: '1px dashed rgba(255,255,255,0.18)', fontSize: 12 }}>
                  <span><span style={{ color: '#00FF41' }}>{'>'}</span> {i.name} x {i.qty}</span>
                  <span style={{ color: '#FFF' }}>${i.price * i.qty}</span>
                </div>
              ))}
              {o.track && (
                <div style={{ marginTop: 10, fontSize: 11, color: '#E0E0E0' }}>
                  tracking: <span style={{ color: '#00FF41' }}>{o.track}</span>
                </div>
              )}
              <div style={{ marginTop: 10, display: 'flex', gap: 6, flexWrap: 'wrap' }}>
                <MSmallBtn>./track</MSmallBtn>
                <MSmallBtn>./reorder</MSmallBtn>
                <MSmallBtn>./help</MSmallBtn>
              </div>
            </div>
          )}
        </div>
      ))}
    </div>
  );
};

const MSmallBtn = ({ children }) => (
  <button style={{
    background: 'transparent', border: '1px solid rgba(255,255,255,0.28)', color: '#E0E0E0',
    padding: '8px 12px', fontFamily: 'inherit', fontSize: 11, letterSpacing: '0.12em',
    cursor: 'pointer', borderRadius: 2, minHeight: 36,
  }}>{'>'} {children}</button>
);

// ===================== SETTINGS =====================
const MobileSettings = ({ user }) => {
  const [form, setForm] = React.useState({
    name: user.name,
    email: user.email,
    handle: user.handle,
    ...user.shipping,
  });
  const [saved, setSaved] = React.useState(false);
  const set = (k, v) => { setForm((f) => ({ ...f, [k]: v })); setSaved(false); };

  const submit = (e) => {
    e.preventDefault();
    setSaved(true);
    setTimeout(() => setSaved(false), 2400);
  };

  return (
    <form onSubmit={submit}>
      <MSettingsGroup title="// IDENTITY">
        <MSField label="HANDLE" value={form.handle} onChange={(v) => set('handle', v)} />
        <MSField label="DISPLAY NAME" value={form.name} onChange={(v) => set('name', v)} />
        <MSField label="EMAIL" value={form.email} onChange={(v) => set('email', v)} type="email" />
      </MSettingsGroup>

      <MSettingsGroup title="// SHIPPING ADDRESS">
        <MSField label="STREET" value={form.line1} onChange={(v) => set('line1', v)} />
        <MSField label="UNIT / APT" value={form.line2} onChange={(v) => set('line2', v)} />
        <MSField label="CITY" value={form.city} onChange={(v) => set('city', v)} />
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
          <MSField label="STATE" value={form.state} onChange={(v) => set('state', v)} />
          <MSField label="ZIP" value={form.zip} onChange={(v) => set('zip', v)} />
        </div>
      </MSettingsGroup>

      <MobileMfaSettings mfaEnabled={user.mfa_enabled || false} />

      <MSettingsGroup title="// NOTIFICATIONS">
        <MobileToggle label="ORDER UPDATES" defaultChecked />
        <MobileToggle label="NEW DROP ALERTS" defaultChecked />
        <MobileToggle label="SUBSCRIPTION REMINDERS" defaultChecked />
        <MobileToggle label="MARKETING" />
      </MSettingsGroup>

      <button type="submit" style={{
        width: '100%', marginTop: 20, background: '#00FF41', color: '#000',
        border: '1px solid #00FF41', padding: '16px', fontFamily: 'inherit',
        fontWeight: 800, fontSize: 14, letterSpacing: '0.22em', cursor: 'pointer',
        borderRadius: 2, minHeight: 52,
      }}>{'>'} ./save-changes</button>
      {saved && (
        <div style={{ marginTop: 10, textAlign: 'center', color: '#00FF41', fontSize: 12, letterSpacing: '0.18em' }}>
          [ OK ] saved.
        </div>
      )}
    </form>
  );
};

// ===================== MOBILE MFA SETTINGS =====================
const MobileMfaSettings = ({ mfaEnabled: initialEnabled }) => {
  const [phase, setPhase] = React.useState('idle');
  const [status, setStatus] = React.useState({ totp_enabled: initialEnabled, passkeys: [], recovery_codes_remaining: 0 });
  const [enrollData, setEnrollData] = React.useState(null);
  const [code, setCode] = React.useState('');
  const [err, setErr] = React.useState(null);
  const [busy, setBusy] = React.useState(false);
  const [showRecovery, setShowRecovery] = React.useState(false);
  const [justEnabled, setJustEnabled] = React.useState(null);
  const [passkeyName, setPasskeyName] = React.useState('');
  const qrRef = React.useRef(null);
  const webauthnSupported = typeof window !== 'undefined' && window.OBC_WebAuthn?.isSupported();

  const refreshStatus = async () => {
    try {
      const s = await window.OBC_API.mfaStatus();
      if (s.ok) setStatus(s);
    } catch (e) { /* silent */ }
  };
  React.useEffect(() => { refreshStatus(); }, []);

  const hasAny = status.totp_enabled || status.passkeys.length > 0;

  React.useEffect(() => {
    if (enrollData?.otpauth_uri && phase === 'enrolling-totp' && qrRef.current && typeof qrcode !== 'undefined') {
      try {
        const qr = qrcode(0, 'M');
        qr.addData(enrollData.otpauth_uri);
        qr.make();
        qrRef.current.innerHTML = qr.createSvgTag({ cellSize: 3, margin: 3 });
        const svg = qrRef.current.querySelector('svg');
        if (svg) {
          svg.style.display = 'block';
          svg.style.margin = '0 auto';
          svg.querySelectorAll('rect').forEach(r => {
            if (r.getAttribute('fill') === '#000000') r.setAttribute('fill', '#00FF41');
          });
        }
      } catch (e) { /* qr library not loaded */ }
    }
  }, [enrollData, phase]);

  const startTotpEnroll = async () => {
    setErr(null); setBusy(true);
    try {
      const data = await window.OBC_API.enrollMfa();
      setEnrollData(data);
      setPhase('enrolling-totp');
    } catch (e) { setErr('// failed to start enrollment.'); }
    setBusy(false);
  };

  const confirmTotpEnroll = async () => {
    setErr(null);
    if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) { setErr('// enter the 6-digit code.'); return; }
    setBusy(true);
    try {
      const result = await window.OBC_API.confirmMfaEnroll(code);
      if (result.ok) {
        setJustEnabled('totp');
        setPhase('idle'); setCode('');
        await refreshStatus();
      } else { setErr('// invalid code.'); }
    } catch (e) { setErr('// verification failed.'); }
    setBusy(false);
  };

  const startPasskeyEnroll = async () => {
    setErr(null); setBusy(true);
    try {
      const { options } = await window.OBC_API.enrollPasskey();
      const attestation = await window.OBC_WebAuthn.registerPasskey(options);
      setPhase('naming-passkey');
      setEnrollData({ attestation });
    } catch (e) {
      if (e.name === 'NotAllowedError') { setErr('// passkey registration cancelled.'); }
      else { setErr('// failed to register passkey.'); }
    }
    setBusy(false);
  };

  const confirmPasskeyEnroll = async () => {
    setErr(null); setBusy(true);
    try {
      const result = await window.OBC_API.confirmPasskey(enrollData.attestation, passkeyName || 'Passkey');
      if (result.ok) {
        if (result.recovery_codes) {
          setEnrollData({ ...enrollData, recovery_codes: result.recovery_codes });
          setJustEnabled('passkey-with-codes');
        } else {
          setJustEnabled('passkey');
        }
        setPhase('idle'); setPasskeyName('');
        await refreshStatus();
      } else { setErr('// passkey verification failed.'); }
    } catch (e) { setErr('// failed to save passkey.'); }
    setBusy(false);
  };

  const disableTotp = async () => {
    setErr(null);
    if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) { setErr('// enter your current 6-digit TOTP code.'); return; }
    setBusy(true);
    try {
      const result = await window.OBC_API.disableMfa(code, 'totp');
      if (result.ok !== false) { setPhase('idle'); setCode(''); setJustEnabled(null); await refreshStatus(); }
      else { setErr('// invalid code.'); }
    } catch (e) { setErr('// failed to disable TOTP.'); }
    setBusy(false);
  };

  const removePasskey = async (credId) => {
    setErr(null);
    if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) { setErr('// enter TOTP code to confirm.'); return; }
    setBusy(true);
    try {
      await window.OBC_API.disableMfa(code, 'passkey', credId);
      setCode(''); await refreshStatus();
    } catch (e) { setErr('// failed to remove passkey.'); }
    setBusy(false);
  };

  const regenerateCodes = async () => {
    setErr(null);
    if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) { setErr('// enter TOTP code to regenerate.'); return; }
    setBusy(true);
    try {
      const result = await window.OBC_API.regenerateRecoveryCodes(code);
      if (result.ok) {
        setEnrollData({ recovery_codes: result.recovery_codes });
        setShowRecovery(true); setCode('');
        await refreshStatus();
      } else { setErr('// invalid code.'); }
    } catch (e) { setErr('// failed to regenerate.'); }
    setBusy(false);
  };

  const downloadCodes = (codes) => {
    const text = '# Operator Brewing — Recovery Codes\n# Each code works once. Store safely.\n\n' + codes.join('\n');
    const blob = new Blob([text], { type: 'text/plain' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = 'obc-recovery-codes.txt';
    a.click();
    URL.revokeObjectURL(a.href);
  };

  const cancelFlow = () => { setPhase('idle'); setCode(''); setErr(null); setEnrollData(null); setPasskeyName(''); };

  const MfaActionBtn = ({ children, color = '#00FF41', onClick }) => (
    <button onClick={onClick} style={{
      background: 'transparent', border: '1px solid ' + color, color,
      padding: '10px 14px', fontFamily: 'inherit', fontSize: 11, letterSpacing: '0.15em', fontWeight: 700,
      cursor: 'pointer', borderRadius: 2, minHeight: 44,
    }}>{children}</button>
  );

  return (
    <MSettingsGroup title="// SECURITY — MFA">
      {/* Success banners */}
      {justEnabled === 'totp' && (
        <div style={{ padding: 12, border: '1px solid #00FF41', background: 'rgba(0,255,65,0.06)', marginBottom: 12, fontSize: 12, color: '#00FF41', letterSpacing: '0.06em' }}>
          // TOTP 2FA enabled.
        </div>
      )}
      {justEnabled === 'passkey' && (
        <div style={{ padding: 12, border: '1px solid #00FF41', background: 'rgba(0,255,65,0.06)', marginBottom: 12, fontSize: 12, color: '#00FF41', letterSpacing: '0.06em' }}>
          // passkey registered.
        </div>
      )}
      {justEnabled === 'passkey-with-codes' && enrollData?.recovery_codes && (
        <div style={{ padding: 12, border: '1px solid #FFD23F', background: 'rgba(255,210,63,0.06)', marginBottom: 12 }}>
          <div style={{ fontSize: 12, color: '#FFD23F', letterSpacing: '0.06em', marginBottom: 8 }}>
            // passkey registered. SAVE RECOVERY CODES:
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 3, fontSize: 12, color: '#FFD23F', letterSpacing: '0.1em', marginBottom: 8 }}>
            {enrollData.recovery_codes.map((c, i) => <div key={i} style={{ padding: '3px 6px', background: 'rgba(255,210,63,0.06)' }}>{c}</div>)}
          </div>
          <MfaActionBtn color="#FFD23F" onClick={() => downloadCodes(enrollData.recovery_codes)}>./download</MfaActionBtn>
        </div>
      )}

      {/* IDLE — status overview */}
      {phase === 'idle' && (
        <div>
          <div style={{ fontSize: 12, color: '#E0E0E0', letterSpacing: '0.06em', marginBottom: 10 }}>
            TOTP: <span style={{ color: status.totp_enabled ? '#00FF41' : '#FF3344', fontWeight: 700 }}>[{status.totp_enabled ? 'ENABLED' : 'DISABLED'}]</span>
          </div>

          {status.passkeys.length > 0 && (
            <div style={{ marginBottom: 10 }}>
              <div style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.18em', marginBottom: 6 }}>// PASSKEYS</div>
              {status.passkeys.map(pk => (
                <div key={pk.credential_id} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '8px 10px', background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.18)', marginBottom: 3, fontSize: 12 }}>
                  <span style={{ color: '#00E5FF' }}>&#x1F511;</span>
                  <span style={{ color: '#E0E0E0', flex: 1 }}>{pk.device_name || 'Passkey'}</span>
                  <span style={{ fontSize: 10, color: pk.backed_up ? '#00FF41' : '#BFBFBF' }}>{pk.backed_up ? '[synced]' : '[device]'}</span>
                </div>
              ))}
            </div>
          )}

          {hasAny && (
            <div style={{ fontSize: 11, color: '#BFBFBF', marginBottom: 10 }}>
              recovery codes: <span style={{ color: status.recovery_codes_remaining > 3 ? '#FFD23F' : '#FF3344', fontWeight: 700 }}>{status.recovery_codes_remaining}</span>
            </div>
          )}

          <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}>
            {!status.totp_enabled && (
              <MfaActionBtn color="#00FF41" onClick={startTotpEnroll}>{busy ? '//...' : './add-totp'}</MfaActionBtn>
            )}
            {webauthnSupported && (
              <MfaActionBtn color="#00E5FF" onClick={startPasskeyEnroll}>{busy ? '//...' : './add-passkey'}</MfaActionBtn>
            )}
            {status.totp_enabled && (
              <MfaActionBtn color="#FF3344" onClick={() => { setPhase('disabling'); setCode(''); setErr(null); }}>./disable-totp</MfaActionBtn>
            )}
            {hasAny && (
              <MfaActionBtn color="#FFD23F" onClick={() => { setPhase('regenerating'); setCode(''); setErr(null); }}>./regen-codes</MfaActionBtn>
            )}
            {status.passkeys.length > 0 && (
              <MfaActionBtn color="#FF3344" onClick={() => { setPhase('removing-passkey'); setCode(''); setErr(null); }}>./rm-passkey</MfaActionBtn>
            )}
          </div>

          {!hasAny && (
            <div style={{ fontSize: 11, color: '#BFBFBF', lineHeight: 1.7, marginTop: 10 }}>
              // protect your account with TOTP or a passkey.
            </div>
          )}
        </div>
      )}

      {/* ENROLLING TOTP */}
      {phase === 'enrolling-totp' && enrollData && (
        <div>
          <div style={{ fontSize: 11, color: '#FFD23F', letterSpacing: '0.22em', marginBottom: 12 }}>$ ./mfa --enroll-totp</div>

          {/* QR code */}
          <div style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.18em', marginBottom: 8 }}>// STEP 1: SCAN QR CODE</div>
          <div ref={qrRef} style={{ background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)', padding: 14, textAlign: 'center', minHeight: 160, display: 'flex', alignItems: 'center', justifyContent: 'center', marginBottom: 10 }}>
            <span style={{ color: '#9A9A9A', fontSize: 11 }}>loading qr...</span>
          </div>
          <div style={{ fontSize: 10, color: '#9A9A9A', marginBottom: 6 }}>// manual entry:</div>
          <div style={{ padding: '8px 10px', background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)', fontSize: 12, color: '#00FF41', letterSpacing: '0.08em', wordBreak: 'break-all', userSelect: 'all', marginBottom: 14 }}>
            {enrollData.secret}
          </div>

          {/* Code input */}
          <div style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.18em', marginBottom: 8 }}>// STEP 2: ENTER CODE</div>
          <input value={code} onChange={(e) => { setCode(e.target.value.replace(/\D/g, '').slice(0, 6)); setErr(null); }}
            maxLength={6} pattern="[0-9]*" inputMode="numeric" autoComplete="one-time-code" autoFocus placeholder="000000"
            style={{ width: '100%', background: '#000', border: '1px solid rgba(255,255,255,0.28)', color: '#FFF', fontFamily: 'inherit', fontSize: 22, padding: '14px', borderRadius: 2, letterSpacing: '0.3em', textAlign: 'center', minHeight: 52, marginBottom: 10 }}
            onFocus={(e) => { e.target.style.borderColor = '#00FF41'; e.target.style.boxShadow = '0 0 0 1px #00FF41'; }}
            onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; e.target.style.boxShadow = 'none'; }}
          />
          {err && <div style={{ marginBottom: 10, padding: '10px 12px', border: '1px solid #FF3344', color: '#FF3344', fontSize: 12 }}>{err}</div>}
          <div style={{ display: 'flex', gap: 8 }}>
            <MfaActionBtn color="#00FF41" onClick={confirmTotpEnroll}>{busy ? '//...' : './confirm'}</MfaActionBtn>
            <MfaActionBtn color="#BFBFBF" onClick={cancelFlow}>./cancel</MfaActionBtn>
          </div>

          {/* Recovery codes toggle */}
          <div style={{ marginTop: 14 }}>
            <div onClick={() => setShowRecovery(!showRecovery)} style={{ fontSize: 11, color: '#FFD23F', letterSpacing: '0.12em', cursor: 'pointer' }}>
              {showRecovery ? '[-]' : '[+]'} recovery codes ({enrollData.recovery_codes?.length || 0})
            </div>
            {showRecovery && enrollData.recovery_codes && (
              <div style={{ marginTop: 8, padding: 10, background: '#0A0A0A', border: '1px solid rgba(255,210,63,0.4)' }}>
                <div style={{ fontSize: 10, color: '#FF3344', letterSpacing: '0.12em', marginBottom: 6 }}>// SAVE THESE NOW.</div>
                <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 3, fontSize: 12, color: '#FFD23F', letterSpacing: '0.1em' }}>
                  {enrollData.recovery_codes.map((c, i) => <div key={i} style={{ padding: '3px 6px', background: 'rgba(255,210,63,0.06)' }}>{c}</div>)}
                </div>
                <div style={{ marginTop: 8 }}>
                  <MfaActionBtn color="#FFD23F" onClick={() => downloadCodes(enrollData.recovery_codes)}>./download</MfaActionBtn>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* NAMING PASSKEY */}
      {phase === 'naming-passkey' && (
        <div>
          <div style={{ fontSize: 11, color: '#00E5FF', letterSpacing: '0.22em', marginBottom: 12 }}>$ ./mfa --register-passkey</div>
          <div style={{ fontSize: 12, color: '#E0E0E0', lineHeight: 1.7, marginBottom: 12 }}>
            // passkey captured. name it:
          </div>
          <input value={passkeyName} onChange={(e) => setPasskeyName(e.target.value.slice(0, 64))}
            autoFocus placeholder="e.g. iPhone, YubiKey"
            style={{ width: '100%', background: '#000', border: '1px solid rgba(255,255,255,0.28)', color: '#FFF', fontFamily: 'inherit', fontSize: 14, padding: '12px', borderRadius: 2, minHeight: 48, marginBottom: 10 }}
            onFocus={(e) => { e.target.style.borderColor = '#00E5FF'; }}
            onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; }}
          />
          {err && <div style={{ marginBottom: 10, padding: '10px 12px', border: '1px solid #FF3344', color: '#FF3344', fontSize: 12 }}>{err}</div>}
          <div style={{ display: 'flex', gap: 8 }}>
            <MfaActionBtn color="#00E5FF" onClick={confirmPasskeyEnroll}>{busy ? '//...' : './save-passkey'}</MfaActionBtn>
            <MfaActionBtn color="#BFBFBF" onClick={cancelFlow}>./cancel</MfaActionBtn>
          </div>
        </div>
      )}

      {/* DISABLING TOTP */}
      {phase === 'disabling' && (
        <div>
          <div style={{ fontSize: 11, color: '#FF3344', letterSpacing: '0.22em', marginBottom: 12 }}>$ ./mfa --disable-totp</div>
          <div style={{ fontSize: 12, color: '#E0E0E0', lineHeight: 1.7, marginBottom: 12 }}>
            // enter your current TOTP code to disable.
          </div>
          <input value={code} onChange={(e) => { setCode(e.target.value.replace(/\D/g, '').slice(0, 6)); setErr(null); }}
            maxLength={6} pattern="[0-9]*" inputMode="numeric" autoComplete="one-time-code" autoFocus placeholder="000000"
            style={{ width: '100%', background: '#000', border: '1px solid rgba(255,255,255,0.28)', color: '#FFF', fontFamily: 'inherit', fontSize: 22, padding: '14px', borderRadius: 2, letterSpacing: '0.3em', textAlign: 'center', minHeight: 52, marginBottom: 10 }}
            onFocus={(e) => { e.target.style.borderColor = '#FF3344'; }}
            onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; }}
          />
          {err && <div style={{ marginBottom: 10, padding: '10px 12px', border: '1px solid #FF3344', color: '#FF3344', fontSize: 12 }}>{err}</div>}
          <div style={{ display: 'flex', gap: 8 }}>
            <MfaActionBtn color="#FF3344" onClick={disableTotp}>{busy ? '//...' : './confirm-disable'}</MfaActionBtn>
            <MfaActionBtn color="#BFBFBF" onClick={cancelFlow}>./cancel</MfaActionBtn>
          </div>
        </div>
      )}

      {/* REMOVING PASSKEY */}
      {phase === 'removing-passkey' && (
        <div>
          <div style={{ fontSize: 11, color: '#FF3344', letterSpacing: '0.22em', marginBottom: 12 }}>$ ./mfa --remove-passkey</div>
          {status.passkeys.map(pk => (
            <div key={pk.credential_id} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '10px', background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.18)', marginBottom: 4, cursor: 'pointer', minHeight: 44 }}
              onClick={() => { if (code.length === 6) removePasskey(pk.credential_id); }}>
              <span style={{ color: '#FF3344', fontSize: 11 }}>[x]</span>
              <span style={{ fontSize: 12, color: '#E0E0E0', flex: 1 }}>{pk.device_name || 'Passkey'}</span>
            </div>
          ))}
          <input value={code} onChange={(e) => { setCode(e.target.value.replace(/\D/g, '').slice(0, 6)); setErr(null); }}
            maxLength={6} inputMode="numeric" autoFocus placeholder="TOTP code"
            style={{ width: '100%', marginTop: 10, background: '#000', border: '1px solid rgba(255,255,255,0.28)', color: '#FFF', fontFamily: 'inherit', fontSize: 22, padding: '14px', borderRadius: 2, letterSpacing: '0.3em', textAlign: 'center', minHeight: 52, marginBottom: 10 }}
            onFocus={(e) => { e.target.style.borderColor = '#FF3344'; }}
            onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; }}
          />
          {err && <div style={{ marginBottom: 10, padding: '10px 12px', border: '1px solid #FF3344', color: '#FF3344', fontSize: 12 }}>{err}</div>}
          <MfaActionBtn color="#BFBFBF" onClick={cancelFlow}>./cancel</MfaActionBtn>
        </div>
      )}

      {/* REGENERATING CODES */}
      {phase === 'regenerating' && (
        <div>
          <div style={{ fontSize: 11, color: '#FFD23F', letterSpacing: '0.22em', marginBottom: 12 }}>$ ./mfa --regenerate-codes</div>
          <div style={{ fontSize: 12, color: '#E0E0E0', lineHeight: 1.7, marginBottom: 12 }}>
            // invalidates existing codes. enter TOTP to confirm.
          </div>
          <input value={code} onChange={(e) => { setCode(e.target.value.replace(/\D/g, '').slice(0, 6)); setErr(null); }}
            maxLength={6} inputMode="numeric" autoFocus placeholder="000000"
            style={{ width: '100%', background: '#000', border: '1px solid rgba(255,255,255,0.28)', color: '#FFF', fontFamily: 'inherit', fontSize: 22, padding: '14px', borderRadius: 2, letterSpacing: '0.3em', textAlign: 'center', minHeight: 52, marginBottom: 10 }}
            onFocus={(e) => { e.target.style.borderColor = '#FFD23F'; }}
            onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; }}
          />
          {err && <div style={{ marginBottom: 10, padding: '10px 12px', border: '1px solid #FF3344', color: '#FF3344', fontSize: 12 }}>{err}</div>}
          {showRecovery && enrollData?.recovery_codes && (
            <div style={{ marginBottom: 10, padding: 10, background: '#0A0A0A', border: '1px solid rgba(255,210,63,0.4)' }}>
              <div style={{ fontSize: 10, color: '#FF3344', letterSpacing: '0.12em', marginBottom: 6 }}>// NEW CODES:</div>
              <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 3, fontSize: 12, color: '#FFD23F', letterSpacing: '0.1em' }}>
                {enrollData.recovery_codes.map((c, i) => <div key={i} style={{ padding: '3px 6px', background: 'rgba(255,210,63,0.06)' }}>{c}</div>)}
              </div>
              <div style={{ marginTop: 8 }}><MfaActionBtn color="#FFD23F" onClick={() => downloadCodes(enrollData.recovery_codes)}>./download</MfaActionBtn></div>
            </div>
          )}
          <div style={{ display: 'flex', gap: 8 }}>
            <MfaActionBtn color="#FFD23F" onClick={regenerateCodes}>{busy ? '//...' : './regenerate'}</MfaActionBtn>
            <MfaActionBtn color="#BFBFBF" onClick={cancelFlow}>./cancel</MfaActionBtn>
          </div>
        </div>
      )}
    </MSettingsGroup>
  );
};

const MSettingsGroup = ({ title, children }) => (
  <div style={{ marginBottom: 16, padding: 14, background: '#0A0A0A', border: '1px solid rgba(255,255,255,0.22)' }}>
    <div style={{ fontSize: 10, color: '#BFBFBF', letterSpacing: '0.22em', fontWeight: 700, marginBottom: 12 }}>{title}</div>
    <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>{children}</div>
  </div>
);

const MSField = ({ label, value, onChange, type = 'text', placeholder }) => (
  <div>
    <div style={{ fontSize: 10, color: '#E0E0E0', letterSpacing: '0.22em', fontWeight: 700, marginBottom: 4 }}>{'>'} {label}</div>
    <input
      type={type} value={value || ''} placeholder={placeholder} maxLength={500}
      onChange={(e) => onChange(e.target.value)}
      style={{
        width: '100%', background: '#000', border: '1px solid rgba(255,255,255,0.28)',
        color: '#FFF', fontFamily: 'inherit', fontSize: 14, padding: '12px', borderRadius: 2,
        minHeight: 48,
      }}
      onFocus={(e) => { e.target.style.borderColor = '#00FF41'; e.target.style.boxShadow = '0 0 0 1px #00FF41'; }}
      onBlur={(e) => { e.target.style.borderColor = 'rgba(255,255,255,0.28)'; e.target.style.boxShadow = 'none'; }}
    />
  </div>
);

const MobileToggle = ({ label, defaultChecked }) => {
  const [on, setOn] = React.useState(!!defaultChecked);
  return (
    <label style={{ display: 'flex', alignItems: 'center', gap: 10, fontSize: 12, color: '#E0E0E0', cursor: 'pointer', minHeight: 44 }}>
      <span onClick={() => setOn(!on)} style={{
        width: 40, height: 22, background: on ? '#00FF41' : 'transparent',
        border: '1px solid ' + (on ? '#00FF41' : 'rgba(255,255,255,0.28)'),
        position: 'relative', borderRadius: 2, flexShrink: 0,
      }}>
        <span style={{
          position: 'absolute', top: 2, left: on ? 20 : 2,
          width: 16, height: 16, background: on ? '#000' : '#888',
          transition: 'left 80ms ease-out',
        }}></span>
      </span>
      <span style={{ letterSpacing: '0.1em' }}>{label}</span>
    </label>
  );
};

window.MobileAccount = MobileAccount;
