// ─────────────────────────────────────────────────────────────────────
// COLLECTION MANAGER — Contributors (Team).
//
// Invite trusted individuals into the collection, see who's active, manage
// access. Accepting an invitation gives the person the Individual
// Contributor experience — never the management interface.
// ─────────────────────────────────────────────────────────────────────

const { useState: useCT } = React;

function CtBar({ eyebrow, title, onClose }) {
  return (
    <div style={{
      padding: '10px 18px 12px', background: P.ink, flex: '0 0 auto',
      display: 'flex', alignItems: 'center', gap: 12, borderBottom: `1px solid ${P.graphite}`,
    }}>
      <button onClick={onClose} aria-label="Back" style={{
        background: P.gold, border: 'none', borderRadius: 999, width: 34, height: 34,
        flexShrink: 0, color: P.ink, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="M19 12H5M5 12l6-6M5 12l6 6" /></svg>
      </button>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontFamily: T.display, fontWeight: 600, fontSize: 15, letterSpacing: '0.02em', color: P.paper, lineHeight: 1.1 }}>{title}</div>
        <RcMono size={8.5} color={P.gold} tracking="0.16em" style={{ marginTop: 3, display: 'block' }}>{eyebrow}</RcMono>
      </div>
      <CAMonogram size={20} color={P.paper} stroke={1.6} />
    </div>
  );
}

// Invite sheet — email or SMS.
function InviteSheet({ onClose, onInvite }) {
  const [method, setMethod] = useCT('Email');
  const [to, setTo] = useCT('');
  const [role, setRole] = useCT('');
  const valid = to.trim().length > 2;

  const node = (
    <div style={{ position: 'absolute', inset: 0, zIndex: 97 }}>
      <div onClick={onClose} style={{ position: 'absolute', inset: 0, background: 'rgba(10,10,11,0.82)', backdropFilter: 'blur(2px)', WebkitBackdropFilter: 'blur(2px)', animation: 'sheetFade .2s ease' }} />
      <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, background: P.graphite, color: P.paper, borderTop: `1px solid ${P.gold}`, padding: '14px 22px 30px', animation: 'sheetIn .25s cubic-bezier(.2,.7,.3,1)' }}>
        <div style={{ width: 40, height: 4, background: P.slate, margin: '0 auto 16px' }} />
        <Eyebrow color={P.gold}>INVITE CONTRIBUTOR</Eyebrow>
        <div style={{ fontFamily: T.display, fontSize: 24, fontWeight: 700, color: P.paper, marginTop: 6, letterSpacing: '-0.015em' }}>ADD TO THE TEAM</div>
        <div style={{ fontFamily: T.body, fontSize: 12, color: P.mute, marginTop: 8, lineHeight: 1.5 }}>
          They'll get the simple contributor experience — capture and submit only.
        </div>

        {/* method toggle */}
        <div style={{ display: 'inline-flex', border: `1px solid ${P.slate}`, marginTop: 16 }}>
          {['Email', 'SMS'].map((m, i) => {
            const on = method === m;
            return (
              <button key={m} onClick={() => { setMethod(m); setTo(''); }} style={{
                padding: '9px 18px', background: on ? P.paper : 'transparent', color: on ? P.ink : P.paper,
                border: 'none', borderLeft: i > 0 ? `1px solid ${P.slate}` : 'none',
                fontFamily: T.mono, fontSize: 9, fontWeight: 600, letterSpacing: '0.16em', textTransform: 'uppercase', cursor: 'pointer',
              }}>{m}</button>
            );
          })}
        </div>

        <div style={{ marginTop: 14, display: 'grid', gap: 10 }}>
          <div>
            <RcMono size={8.5} color={P.mute} tracking="0.16em" style={{ display: 'block', marginBottom: 6 }}>{method === 'Email' ? 'Email address' : 'Mobile number'}</RcMono>
            <input value={to} onChange={(e) => setTo(e.target.value)} placeholder={method === 'Email' ? 'name@shop.com' : '+1 (555) 000-0000'}
              style={{ width: '100%', height: 44, boxSizing: 'border-box', background: P.ink, border: `1px solid ${to.trim() ? P.gold : P.slate}`, outline: 'none', color: P.paper, fontFamily: method === 'Email' ? T.body : T.mono, fontSize: 13, padding: '0 13px' }} />
          </div>
          <div>
            <RcMono size={8.5} color={P.mute} tracking="0.16em" style={{ display: 'block', marginBottom: 6 }}>Role · optional</RcMono>
            <input value={role} onChange={(e) => setRole(e.target.value)} placeholder="Mechanic · Photographer · Transport…"
              style={{ width: '100%', height: 44, boxSizing: 'border-box', background: P.ink, border: `1px solid ${P.slate}`, outline: 'none', color: P.paper, fontFamily: T.body, fontSize: 13, padding: '0 13px' }} />
          </div>
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginTop: 18 }}>
          <button onClick={onClose} style={{ height: 48, background: 'transparent', border: `1px solid ${P.slate}`, color: P.paper, fontFamily: T.mono, fontSize: 10, fontWeight: 600, letterSpacing: '0.16em', textTransform: 'uppercase', cursor: 'pointer' }}>Cancel</button>
          <button onClick={() => valid && onInvite({ to: to.trim(), role: role.trim() || 'Contributor', via: method })} disabled={!valid} style={{
            height: 48, background: valid ? P.gold : 'transparent', border: `1px solid ${valid ? P.gold : P.slate}`, color: valid ? P.ink : P.mute,
            fontFamily: T.mono, fontSize: 10, fontWeight: 700, letterSpacing: '0.16em', textTransform: 'uppercase', cursor: valid ? 'pointer' : 'not-allowed',
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
          }}>
            <Icon name="Send" size={14} color={valid ? P.ink : P.mute} strokeWidth={2} />Send Invite
          </button>
        </div>
      </div>
    </div>
  );
  const stage = (typeof document !== 'undefined') ? document.getElementById('proto-stage') : null;
  return stage ? ReactDOM.createPortal(node, stage) : node;
}

// ─────────────────────────────────────────────────────────────────────
// ADMINISTRATION — the account / people-&-access surface.
//
// Reached from the manager's identity (the avatar in the Operations Hub
// header), NOT from the operational action grid. This is where managing
// WHO can contribute lives — deliberately separate from the operational
// workflows that act on vehicles and their history. It is built to grow:
// today, Team & Access; over time, owners & family, additional managers,
// per-vehicle permissions, and collection settings.
// ─────────────────────────────────────────────────────────────────────
function AdminRow({ icon, title, desc, badge, onClick, danger }) {
  return (
    <InkCard onClick={onClick} padding={13} style={{
      display: 'grid', gridTemplateColumns: '34px 1fr auto', gap: 13, alignItems: 'center',
    }}>
      <span style={{
        width: 34, height: 34, flexShrink: 0, border: `1px solid ${danger ? P.slate : P.gold}`,
        background: danger ? 'transparent' : 'rgba(184,144,85,0.1)',
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <Icon name={icon} size={16} color={danger ? P.mute : P.gold} strokeWidth={1.6} />
      </span>
      <div style={{ minWidth: 0 }}>
        <div style={{ fontFamily: T.display, fontSize: 15, fontWeight: 600, color: danger ? P.mute : P.paper, lineHeight: 1.1 }}>{title}</div>
        {desc ? <RcMono size={8} color={P.mute} tracking="0.1em" style={{ marginTop: 4, display: 'block' }}>{desc}</RcMono> : null}
      </div>
      <span style={{ display: 'inline-flex', alignItems: 'center', gap: 9, flexShrink: 0 }}>
        {typeof badge === 'number' && badge > 0 ? (
          <span style={{ minWidth: 18, height: 18, padding: '0 5px', borderRadius: 999, background: P.gold, color: P.ink, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.mono, fontSize: 9, fontWeight: 700 }}>{badge}</span>
        ) : null}
        <Icon name="arrow" size={16} color={danger ? P.mute : P.gold} strokeWidth={1.4} />
      </span>
    </InkCard>
  );
}

function AdminHome({ onBack, onOpenTeam, onOpenEmergency, onOpenTransfer, onOpenOwner, onOpenBilling, onSignOut }) {
  const M = (typeof COLLECTION_MANAGER !== 'undefined') ? COLLECTION_MANAGER
    : { name: 'Collection Manager', initials: 'CM', role: 'Collection Manager', collection: 'The Collection', email: '' };
  const activeCount = (typeof CONTRIBUTORS_ACTIVE !== 'undefined') ? CONTRIBUTORS_ACTIVE.filter(c => c.access === 'active').length : 0;
  const pendingCount = (typeof CONTRIBUTORS_PENDING !== 'undefined') ? CONTRIBUTORS_PENDING.length : 0;

  return (
    <React.Fragment>
      <CtBar title="Manage Account" eyebrow="Profile · Access · Billing" onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-admin-home">
        {/* Identity */}
        <div style={{ padding: '18px 22px 6px' }}>
          <RcMono size={9} color={P.gold} style={{ display: 'block' }}>SIGNED IN AS</RcMono>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 12 }}>
            <span style={{ width: 50, height: 50, flexShrink: 0, borderRadius: 999, border: `1px solid ${P.gold}`, background: 'rgba(184,144,85,0.1)', color: P.gold, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.mono, fontSize: 15, fontWeight: 700 }}>{M.initials}</span>
            <div style={{ minWidth: 0 }}>
              <div style={{ fontFamily: T.display, fontSize: 20, fontWeight: 700, color: P.paper, letterSpacing: '-0.01em', lineHeight: 1.05 }}>{M.name}</div>
              <RcMono size={8.5} color={P.gold} tracking="0.14em" style={{ marginTop: 5, display: 'block' }}>{M.role} · {M.collection}</RcMono>
            </div>
          </div>
        </div>

        <div style={{ padding: '16px 0 8px' }}><RowRule>PEOPLE & ACCESS</RowRule></div>
        <div style={{ padding: '10px 22px 8px', display: 'grid', gap: 8 }}>
          <AdminRow icon="Users" title="Contributors"
            desc={`${activeCount} active · ${pendingCount} pending`}
            onClick={onOpenTeam} />
          <AdminRow icon="KeyRound" title="Emergency Access"
            desc="Estate & family fail-safe" onClick={onOpenEmergency} />
          <AdminRow icon="ArrowLeftRight" title="Change Collection Manager"
            desc="Assign a new manager" onClick={onOpenTransfer} />
          <AdminRow icon="Contact" title="Collection Owner"
            desc="Who the collection belongs to" onClick={onOpenOwner} />
        </div>

        <div style={{ padding: '16px 0 8px' }}><RowRule>ACCOUNT</RowRule></div>
        <div style={{ padding: '10px 22px 8px', display: 'grid', gap: 8 }}>
          <AdminRow icon="CreditCard" title="Billing"
            desc="Plan · payment · invoices" onClick={onOpenBilling} />
          <AdminRow icon="LogOut" title="Sign out" desc={M.email} danger onClick={onSignOut} />
        </div>

        {/* why this is separate */}
        <div style={{ padding: '14px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.gold, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            The Collection Manager controls access, ownership, and billing — kept separate from the operations that act on the collection.
          </RcMono>
        </div>
      </div>
    </React.Fragment>
  );
}

function Contributors({ onBack }) {
  const [active, setActive] = useCT(CONTRIBUTORS_ACTIVE);
  const [pending, setPending] = useCT(CONTRIBUTORS_PENDING);
  const [invite, setInvite] = useCT(false);
  const [menuFor, setMenuFor] = useCT(null);

  const toggleAccess = (id) => setActive(list => list.map(c => c.id === id ? { ...c, access: c.access === 'active' ? 'disabled' : 'active' } : c));
  const removeContrib = (id) => setActive(list => list.filter(c => c.id !== id));
  const cancelInvite = (id) => setPending(list => list.filter(i => i.id !== id));
  const addInvite = (inv) => { setPending(list => [{ id: 'i-' + Date.now(), to: inv.to, role: inv.role, via: inv.via, sent: 'Just now' }, ...list]); setInvite(false); };

  const activeCount = active.filter(c => c.access === 'active').length;

  return (
    <React.Fragment>
      <CtBar title="Contributors" eyebrow={`${activeCount} active · ${pending.length} pending`} onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-contributors">
        <div style={{ padding: '18px 22px 12px' }}>
          <RcMono size={9} color={P.gold} style={{ display: 'block' }}>WHO DOCUMENTS THE WORK</RcMono>
          <div style={{ fontFamily: T.display, fontWeight: 700, fontSize: 30, lineHeight: 0.92, letterSpacing: '-0.02em', color: P.paper, marginTop: 7 }}>THE TEAM</div>
        </div>

        {/* invite CTA */}
        <div style={{ padding: '6px 22px 14px' }}>
          <button onClick={() => setInvite(true)} style={{
            width: '100%', height: 50, background: P.gold, border: `1px solid ${P.gold}`, color: P.ink, cursor: 'pointer',
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
            fontFamily: T.mono, fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase',
          }}>
            <Icon name="add" size={16} color={P.ink} strokeWidth={2.2} />Invite Contributor
          </button>
        </div>

        {/* pending invitations */}
        {pending.length > 0 && (
          <React.Fragment>
            <div style={{ padding: '6px 0 8px' }}><RowRule>PENDING INVITATIONS · {pending.length}</RowRule></div>
            <div style={{ padding: '10px 22px 8px', display: 'flex', flexDirection: 'column', gap: 8 }}>
              {pending.map(inv => (
                <div key={inv.id} style={{ display: 'flex', alignItems: 'center', gap: 12, background: P.graphite, border: `1px dashed ${P.slate}`, padding: '12px 13px' }}>
                  <span style={{ width: 34, height: 34, flexShrink: 0, border: `1px solid ${P.slate}`, background: P.ink, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
                    <Icon name={inv.via === 'SMS' ? 'MessageSquare' : 'Mail'} size={15} color={P.gold} strokeWidth={1.6} />
                  </span>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ fontFamily: T.body, fontSize: 12.5, color: P.paper, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{inv.to}</div>
                    <RcMono size={8} color={P.mute} tracking="0.1em" style={{ marginTop: 3, display: 'block' }}>{inv.role} · {inv.via} · sent {inv.sent}</RcMono>
                  </div>
                  <button onClick={() => cancelInvite(inv.id)} aria-label="Cancel invitation" style={{ flexShrink: 0, padding: '6px 9px', background: 'transparent', border: `1px solid ${P.slate}`, color: P.mute, fontFamily: T.mono, fontSize: 8, fontWeight: 600, letterSpacing: '0.14em', textTransform: 'uppercase', cursor: 'pointer' }}>Cancel</button>
                </div>
              ))}
            </div>
          </React.Fragment>
        )}

        {/* active contributors */}
        <div style={{ padding: '10px 0 8px' }}><RowRule>ACTIVE CONTRIBUTORS · {active.length}</RowRule></div>
        <div style={{ padding: '10px 22px 8px', display: 'flex', flexDirection: 'column', gap: 8 }}>
          {active.map(c => {
            const disabled = c.access === 'disabled';
            return (
              <div key={c.id} style={{ background: P.graphite, border: `1px solid ${P.slate}`, padding: '12px 13px', opacity: disabled ? 0.6 : 1 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                  <span style={{ width: 38, height: 38, flexShrink: 0, borderRadius: 999, border: `1px solid ${P.gold}`, background: 'rgba(184,144,85,0.1)', color: P.gold, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.mono, fontSize: 11, fontWeight: 700 }}>{c.initials}</span>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ fontFamily: T.display, fontSize: 15, fontWeight: 600, color: P.paper, lineHeight: 1.1, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.name}</div>
                    <RcMono size={8} color={P.gold} tracking="0.12em" style={{ marginTop: 3, display: 'block' }}>{c.role} · {c.org}</RcMono>
                  </div>
                  <button onClick={() => setMenuFor(menuFor === c.id ? null : c.id)} aria-label="Manage" style={{ width: 30, height: 30, flexShrink: 0, background: 'transparent', border: 'none', color: P.paper, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><circle cx="12" cy="5" r="1.2" /><circle cx="12" cy="12" r="1.2" /><circle cx="12" cy="19" r="1.2" /></svg>
                  </button>
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 10, paddingTop: 10, borderTop: `1px solid ${P.slate}` }}>
                  <RcMono size={8} color={P.mute} tracking="0.1em">{c.submissions} SUBMISSIONS</RcMono>
                  <RcMono size={8} color={P.mute} tracking="0.1em">VIA {c.via}</RcMono>
                  <span style={{ flex: 1 }} />
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontFamily: T.mono, fontSize: 8, fontWeight: 700, letterSpacing: '0.14em', textTransform: 'uppercase', color: disabled ? P.mute : P.gold }}>
                    <span style={{ width: 5, height: 5, borderRadius: 999, background: disabled ? P.mute : P.gold }} />{disabled ? 'Disabled' : 'Active'}
                  </span>
                </div>
                {menuFor === c.id && (
                  <div style={{ marginTop: 10, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
                    <button onClick={() => { toggleAccess(c.id); setMenuFor(null); }} style={{ height: 40, background: 'transparent', border: `1px solid ${P.slate}`, color: P.paper, fontFamily: T.mono, fontSize: 9, fontWeight: 600, letterSpacing: '0.12em', textTransform: 'uppercase', cursor: 'pointer' }}>{disabled ? 'Enable Access' : 'Disable Access'}</button>
                    <button onClick={() => { removeContrib(c.id); setMenuFor(null); }} style={{ height: 40, background: 'transparent', border: `1px solid ${P.signal}`, color: P.signal, fontFamily: T.mono, fontSize: 9, fontWeight: 600, letterSpacing: '0.12em', textTransform: 'uppercase', cursor: 'pointer' }}>Remove</button>
                  </div>
                )}
              </div>
            );
          })}
        </div>

        {/* note on what a contributor gets */}
        <div style={{ padding: '12px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.gold, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            Contributors get capture &amp; submit only — never allocation, reports, or the archive.
          </RcMono>
        </div>
      </div>

      {invite && <InviteSheet onClose={() => setInvite(false)} onInvite={addInvite} />}
    </React.Fragment>
  );
}

// ─────────────────────────────────────────────────────────────────────
// BILLING — account billing summary, reached from Manage Account.
// Modest by design: current plan, payment method on file, billing contact.
// ─────────────────────────────────────────────────────────────────────
function BillingLine({ k, v, mono }) {
  return (
    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 14, padding: '13px 0', borderBottom: `1px solid ${P.slate}` }}>
      <RcMono size={8.5} color={P.mute} tracking="0.14em">{k}</RcMono>
      <span style={{ fontFamily: mono ? T.mono : T.body, fontSize: mono ? 12 : 13, color: P.paper, textAlign: 'right' }}>{v}</span>
    </div>
  );
}

function BillingScreen({ onBack }) {
  const M = (typeof COLLECTION_MANAGER !== 'undefined') ? COLLECTION_MANAGER
    : { name: 'Collection Manager', collection: 'The Collection', email: '' };
  return (
    <React.Fragment>
      <CtBar title="Billing" eyebrow="Account · Plan & Payment" onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-billing">
        <div style={{ padding: '18px 22px 6px' }}>
          <div style={{ fontFamily: T.display, fontWeight: 700, fontSize: 30, lineHeight: 0.92, letterSpacing: '-0.02em', color: P.paper }}>PLAN &amp;<br />PAYMENT</div>
        </div>

        {/* plan card */}
        <div style={{ padding: '16px 22px 6px' }}>
          <div style={{ background: P.graphite, border: `1px solid ${P.gold}`, padding: '16px 16px 14px' }}>
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', gap: 12 }}>
              <div>
                <RcMono size={8.5} color={P.gold} tracking="0.16em">CURRENT PLAN</RcMono>
                <div style={{ fontFamily: T.display, fontSize: 22, fontWeight: 700, color: P.paper, letterSpacing: '-0.01em', marginTop: 6 }}>Collection</div>
              </div>
              <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontFamily: T.mono, fontSize: 8, fontWeight: 700, letterSpacing: '0.14em', textTransform: 'uppercase', color: P.gold, flexShrink: 0, marginTop: 4 }}>
                <span style={{ width: 5, height: 5, borderRadius: 999, background: P.gold }} />Active
              </span>
            </div>
            <BillingLine k="Billing cycle" v="Annual" />
            <BillingLine k="Next renewal" v="Apr 14, 2027" />
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 14, paddingTop: 13 }}>
              <RcMono size={8.5} color={P.mute} tracking="0.14em">AMOUNT</RcMono>
              <span style={{ fontFamily: T.display, fontSize: 18, fontWeight: 700, color: P.paper }}>$1,200<span style={{ fontFamily: T.mono, fontSize: 9, color: P.mute }}> / yr</span></span>
            </div>
          </div>
        </div>

        {/* payment method */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>PAYMENT METHOD</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, background: P.graphite, border: `1px solid ${P.slate}`, padding: '13px 14px' }}>
            <span style={{ width: 38, height: 38, flexShrink: 0, border: `1px solid ${P.gold}`, background: 'rgba(184,144,85,0.1)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
              <Icon name="CreditCard" size={16} color={P.gold} strokeWidth={1.6} />
            </span>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: T.mono, fontSize: 12, color: P.paper, letterSpacing: '0.08em' }}>•••• •••• •••• 4242</div>
              <RcMono size={8} color={P.mute} tracking="0.12em" style={{ marginTop: 4, display: 'block' }}>VISA · EXPIRES 09 / 28</RcMono>
            </div>
            <button style={{ flexShrink: 0, padding: '7px 11px', background: 'transparent', border: `1px solid ${P.slate}`, color: P.paper, fontFamily: T.mono, fontSize: 8, fontWeight: 600, letterSpacing: '0.14em', textTransform: 'uppercase', cursor: 'pointer' }}>Update</button>
          </div>
        </div>

        {/* billing contact */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>BILLING CONTACT</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <BillingLine k="Name" v={M.name} />
          <BillingLine k="Email" v={M.email} mono />
          <BillingLine k="Collection" v={M.collection} />
        </div>

        <div style={{ padding: '14px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.gold, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            Invoices are emailed to the billing contact and archived with the collection record.
          </RcMono>
        </div>
      </div>
    </React.Fragment>
  );
}

// ─────────────────────────────────────────────────────────────────────
// EMERGENCY ACCESS — estate & family fail-safe, reached from Manage Account.
// Designate who can reach the archive if the manager becomes unavailable.
// Aligns with the series principle: collections outlive their managers.
// ─────────────────────────────────────────────────────────────────────
function EmergencyAccessScreen({ onBack }) {
  return (
    <React.Fragment>
      <CtBar title="Emergency Access" eyebrow="Account · Estate & Family" onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-emergency">
        <div style={{ padding: '18px 22px 6px' }}>
          <div style={{ fontFamily: T.display, fontWeight: 700, fontSize: 30, lineHeight: 0.92, letterSpacing: '-0.02em', color: P.paper }}>THE<br />FAIL-SAFE</div>
          <div style={{ fontFamily: T.body, fontSize: 12, color: P.mute, marginTop: 10, lineHeight: 1.5, maxWidth: 320 }}>
            Collections outlive the people who manage them. Designate who can reach the archive if you become unavailable.
          </div>
        </div>

        {/* designated contact */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>DESIGNATED CONTACT</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <div style={{ background: P.graphite, border: `1px solid ${P.gold}`, padding: '14px 14px 12px' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <span style={{ width: 40, height: 40, flexShrink: 0, borderRadius: 999, border: `1px solid ${P.gold}`, background: 'rgba(184,144,85,0.1)', color: P.gold, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.mono, fontSize: 12, fontWeight: 700 }}>CV</span>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontFamily: T.display, fontSize: 16, fontWeight: 600, color: P.paper, lineHeight: 1.1 }}>Clara Voss</div>
                <RcMono size={8} color={P.gold} tracking="0.12em" style={{ marginTop: 4, display: 'block' }}>FAMILY · ESTATE EXECUTOR</RcMono>
              </div>
              <button style={{ flexShrink: 0, padding: '7px 11px', background: 'transparent', border: `1px solid ${P.slate}`, color: P.paper, fontFamily: T.mono, fontSize: 8, fontWeight: 600, letterSpacing: '0.14em', textTransform: 'uppercase', cursor: 'pointer' }}>Change</button>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 11, paddingTop: 11, borderTop: `1px solid ${P.slate}` }}>
              <RcMono size={8} color={P.mute} tracking="0.1em">c.voss@vosscollection.com</RcMono>
            </div>
          </div>
        </div>

        {/* trigger condition */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>WHEN ACCESS UNLOCKS</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 14, padding: '13px 0', borderBottom: `1px solid ${P.slate}` }}>
            <RcMono size={8.5} color={P.mute} tracking="0.14em">AFTER INACTIVITY</RcMono>
            <span style={{ fontFamily: T.body, fontSize: 13, color: P.paper }}>90 days</span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 14, padding: '13px 0', borderBottom: `1px solid ${P.slate}` }}>
            <RcMono size={8.5} color={P.mute} tracking="0.14em">GRANTS</RcMono>
            <span style={{ fontFamily: T.body, fontSize: 13, color: P.paper, textAlign: 'right' }}>Full archive · read &amp; export</span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 14, padding: '13px 0' }}>
            <RcMono size={8.5} color={P.mute} tracking="0.14em">NOTIFICATION</RcMono>
            <span style={{ fontFamily: T.body, fontSize: 13, color: P.paper, textAlign: 'right' }}>You're warned first, twice</span>
          </div>
        </div>

        <div style={{ padding: '14px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.gold, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            Emergency access never exposes the management tools — only the archive and its documentation.
          </RcMono>
        </div>
      </div>
    </React.Fragment>
  );
}

// ─────────────────────────────────────────────────────────────────────
// TRANSFER ACCOUNT — permanent succession of the collection, from
// Manage Account. Sale, inheritance, or estate handoff: the whole archive
// moves to a new owner. Deliberately weighty and reversible only before
// the recipient accepts.
// ─────────────────────────────────────────────────────────────────────
function TransferAccountScreen({ onBack }) {
  const transfers = [
    { icon: 'Inbox', label: 'Reviewing & filing submissions' },
    { icon: 'FileSpreadsheet', label: 'Reports & reimbursements' },
    { icon: 'Users', label: 'The contributor team' },
    { icon: 'CreditCard', label: 'Billing administration' },
  ];
  return (
    <React.Fragment>
      <CtBar title="Change Collection Manager" eyebrow="Account · Succession" onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-transfer">
        <div style={{ padding: '18px 22px 6px' }}>
          <div style={{ fontFamily: T.display, fontWeight: 700, fontSize: 30, lineHeight: 0.92, letterSpacing: '-0.02em', color: P.paper }}>HAND<br />OVER</div>
          <div style={{ fontFamily: T.body, fontSize: 12, color: P.mute, marginTop: 10, lineHeight: 1.5, maxWidth: 320 }}>
            Assign a new collection manager to run the archive day to day. Ownership stays with the collector — only who manages it changes.
          </div>
        </div>

        {/* recipient — empty state */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>NEW MANAGER</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <button style={{
            width: '100%', display: 'flex', alignItems: 'center', gap: 12, cursor: 'pointer', textAlign: 'left',
            background: P.graphite, border: `1px dashed ${P.slate}`, padding: '14px 14px',
          }}>
            <span style={{ width: 40, height: 40, flexShrink: 0, borderRadius: 999, border: `1px solid ${P.slate}`, background: P.ink, color: P.gold, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
              <Icon name="add" size={18} color={P.gold} strokeWidth={2} />
            </span>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: T.display, fontSize: 15, fontWeight: 600, color: P.paper, lineHeight: 1.1 }}>Choose New Manager</div>
              <RcMono size={8} color={P.mute} tracking="0.12em" style={{ marginTop: 4, display: 'block' }}>BY EMAIL · THEY MUST ACCEPT</RcMono>
            </div>
            <Icon name="arrow" size={16} color={P.gold} strokeWidth={1.4} />
          </button>
        </div>

        {/* what transfers */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>WHAT THEY TAKE ON</RowRule></div>
        <div style={{ padding: '10px 22px 8px', display: 'grid', gap: 8 }}>
          {transfers.map(t => (
            <div key={t.label} style={{ display: 'flex', alignItems: 'center', gap: 12, background: P.graphite, border: `1px solid ${P.slate}`, padding: '12px 13px' }}>
              <span style={{ width: 32, height: 32, flexShrink: 0, border: `1px solid ${P.slate}`, background: P.ink, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name={t.icon} size={15} color={P.gold} strokeWidth={1.6} />
              </span>
              <div style={{ fontFamily: T.body, fontSize: 13, color: P.paper }}>{t.label}</div>
            </div>
          ))}
        </div>

        {/* action */}
        <div style={{ padding: '16px 22px 10px' }}>
          <button disabled style={{
            width: '100%', height: 50, background: 'transparent', border: `1px solid ${P.signal}`, color: P.signal, cursor: 'not-allowed',
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10, opacity: 0.7,
            fontFamily: T.mono, fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase',
          }}>
            <Icon name="ArrowLeftRight" size={15} color={P.signal} strokeWidth={2} />Assign Manager
          </button>
        </div>

        <div style={{ padding: '6px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.signal, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            Ownership is unchanged. Choose a manager to continue — nothing changes until they accept.
          </RcMono>
        </div>
      </div>
    </React.Fragment>
  );
}

// ─────────────────────────────────────────────────────────────────────
// COLLECTION OWNER — who the collection belongs to, from Manage Account.
// The collector / family. They receive organized reimbursement reports —
// never raw receipts. Distinct from the manager, who runs the platform.
// ─────────────────────────────────────────────────────────────────────
function CollectionOwnerScreen({ onBack }) {
  const receives = [
    { icon: 'FileSpreadsheet', label: 'Reimbursement reports' },
    { icon: 'Coins', label: 'Financial summaries' },
    { icon: 'FileText', label: 'Supporting documentation' },
  ];
  return (
    <React.Fragment>
      <CtBar title="Collection Owner" eyebrow="Account · Ownership" onClose={onBack} />
      <div className="proto-scroll" style={{ flex: 1, overflow: 'auto', background: P.ink }} data-screen-label="mgr-owner">
        <div style={{ padding: '18px 22px 6px' }}>
          <div style={{ fontFamily: T.display, fontWeight: 700, fontSize: 30, lineHeight: 0.92, letterSpacing: '-0.02em', color: P.paper }}>THE<br />OWNER</div>
          <div style={{ fontFamily: T.body, fontSize: 12, color: P.mute, marginTop: 10, lineHeight: 1.5, maxWidth: 320 }}>
            Who the collection belongs to. The owner receives organized reports — never raw receipts — and is who reimbursements are prepared for.
          </div>
        </div>

        {/* owner card */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>OWNER OF RECORD</RowRule></div>
        <div style={{ padding: '10px 22px 8px' }}>
          <div style={{ background: P.graphite, border: `1px solid ${P.gold}`, padding: '14px 14px 12px' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <span style={{ width: 40, height: 40, flexShrink: 0, borderRadius: 999, border: `1px solid ${P.gold}`, background: 'rgba(184,144,85,0.1)', color: P.gold, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.mono, fontSize: 12, fontWeight: 700 }}>HV</span>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontFamily: T.display, fontSize: 16, fontWeight: 600, color: P.paper, lineHeight: 1.1 }}>Henrik Voss</div>
                <RcMono size={8} color={P.gold} tracking="0.12em" style={{ marginTop: 4, display: 'block' }}>COLLECTOR · THE VOSS COLLECTION</RcMono>
              </div>
              <button style={{ flexShrink: 0, padding: '7px 11px', background: 'transparent', border: `1px solid ${P.slate}`, color: P.paper, fontFamily: T.mono, fontSize: 8, fontWeight: 600, letterSpacing: '0.14em', textTransform: 'uppercase', cursor: 'pointer' }}>Edit</button>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 11, paddingTop: 11, borderTop: `1px solid ${P.slate}` }}>
              <RcMono size={8} color={P.mute} tracking="0.1em">h.voss@vosscollection.com</RcMono>
            </div>
          </div>
        </div>

        {/* what the owner receives */}
        <div style={{ padding: '14px 0 8px' }}><RowRule>WHAT THE OWNER RECEIVES</RowRule></div>
        <div style={{ padding: '10px 22px 8px', display: 'grid', gap: 8 }}>
          {receives.map(r => (
            <div key={r.label} style={{ display: 'flex', alignItems: 'center', gap: 12, background: P.graphite, border: `1px solid ${P.slate}`, padding: '12px 13px' }}>
              <span style={{ width: 32, height: 32, flexShrink: 0, border: `1px solid ${P.slate}`, background: P.ink, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name={r.icon} size={15} color={P.gold} strokeWidth={1.6} />
              </span>
              <div style={{ fontFamily: T.body, fontSize: 13, color: P.paper }}>{r.label}</div>
            </div>
          ))}
        </div>

        <div style={{ padding: '14px 22px 28px', display: 'flex', alignItems: 'flex-start', gap: 10 }}>
          <span style={{ width: 5, height: 5, background: P.gold, flexShrink: 0, marginTop: 4 }} />
          <RcMono size={8.5} color={P.mute} tracking="0.14em" style={{ lineHeight: 1.7 }}>
            The owner never sees raw receipts — only the reports assembled from them.
          </RcMono>
        </div>
      </div>
    </React.Fragment>
  );
}

Object.assign(window, { Contributors, InviteSheet, AdminHome, AdminRow, BillingScreen, EmergencyAccessScreen, TransferAccountScreen, CollectionOwnerScreen });
