// backoffice.jsx — Narrio Operator Console
// The admin surface an Operator (paid licensee) sees once signed in.
// Reuses tokens. Multi-community portfolio overview + per-community
// deep-dive + waiver brokerage + sponsor pipeline + revenue waterfall.

const COMMUNITIES = [
  { id: 'wealth',    name: 'Wealth Signal',     niche: 'Personal finance',     subs: 4280, mrr: 8420, lead: 'A. Ohanian',     contribs: 3, status: 'healthy', growth: 0.124, openRate: 0.48, churn: 0.018, streak: 16 },
  { id: 'aiwork',    name: 'The AI Workplace',  niche: 'AI × future of work',  subs: 5120, mrr: 6890, lead: 'M. Patel',        contribs: 3, status: 'healthy', growth: 0.092, openRate: 0.52, churn: 0.022, streak: 14 },
  { id: 'climate',   name: 'Climate Capital',   niche: 'Climate finance',      subs: 2940, mrr: 5240, lead: 'D. Castellanos',  contribs: 2, status: 'attention', growth: 0.031, openRate: 0.41, churn: 0.039, streak: 9 },
  { id: 'longev',    name: 'Longevity Stack',   niche: 'Health × longevity',   subs: 3380, mrr: 4720, lead: 'S. Park',         contribs: 3, status: 'healthy', growth: 0.078, openRate: 0.46, churn: 0.024, streak: 11 },
  { id: 'brick',     name: 'Brick Intelligence',niche: 'Construction × tech',  subs: 0,    mrr: 0,    lead: '— open seat',     contribs: 0, status: 'opening',   growth: 0,     openRate: 0,    churn: 0,     streak: 0 },
];

function App() {
  const [tab, setTab] = React.useState('overview');
  const [active, setActive] = React.useState('wealth');

  return (
    <div style={{ minHeight: '100vh', display: 'grid', gridTemplateColumns: '240px 1fr' }}>
      <Sidebar tab={tab} setTab={setTab} active={active} setActive={setActive} />
      <main style={{ borderLeft: '.5px solid var(--bd)' }}>
        <TopBar />
        <div style={{ padding: '32px 40px' }}>
          {tab === 'overview' && <Overview />}
          {tab === 'community' && <CommunityDetail id={active} />}
          {tab === 'brokerage' && <Brokerage />}
          {tab === 'sponsors' && <Sponsors />}
          {tab === 'revenue' && <Revenue />}
          {tab === 'team' && <Team />}
        </div>
      </main>
    </div>
  );
}

function Sidebar({ tab, setTab, active, setActive }) {
  const items = [
    { id: 'overview',  l: 'Portfolio overview', shortcut: 'O' },
    { id: 'community', l: 'Community',          shortcut: 'C' },
    { id: 'brokerage', l: 'Coverage brokerage', shortcut: 'B' },
    { id: 'sponsors',  l: 'Sponsor pipeline',   shortcut: 'S' },
    { id: 'revenue',   l: 'Revenue & payouts',  shortcut: 'R' },
    { id: 'team',      l: 'Team & seats',       shortcut: 'T' },
  ];
  return (
    <aside style={{ background: 'var(--bg-1)', padding: '20px 0', display: 'grid', gridTemplateRows: 'auto 1fr auto', gap: 0 }}>
      <div style={{ padding: '0 22px 20px', borderBottom: '.5px solid var(--bd)' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 9, fontWeight: 600, fontSize: 16, letterSpacing: '-0.025em' }}>
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
            <circle cx="12" cy="12" r="11" stroke="var(--ac)" strokeWidth="1" opacity=".35"/>
            <path d="M5 16 C 5 8, 12 6, 14 10 S 19 16, 19 8" stroke="var(--ac)" strokeWidth="1.6" strokeLinecap="round"/>
            <circle cx="5" cy="16" r="1.6" fill="var(--ac)"/>
            <circle cx="19" cy="8" r="1.6" fill="var(--ac)"/>
          </svg>
          Narrio
          <span style={{ fontSize: 8.5, padding: '2px 6px', border: '.5px solid var(--bd-hi)', borderRadius: 4, marginLeft: 4, color: 'var(--t2)', letterSpacing: '.1em', textTransform: 'uppercase', fontFamily: 'var(--f-mono)' }}>OS</span>
        </div>
        <div style={{ marginTop: 14, padding: '8px 10px', background: 'var(--bg-2)', border: '.5px solid var(--bd)', borderRadius: 6, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
          <span style={{ fontSize: 12, color: 'var(--t1)' }}>Operator · Growth</span>
          <span style={{ fontSize: 10, fontFamily: 'var(--f-mono)', color: 'var(--green)' }}>● ACTIVE</span>
        </div>
      </div>

      <nav style={{ padding: '16px 12px' }}>
        {items.map((it) => (
          <button key={it.id} onClick={() => setTab(it.id)}
            style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', width: '100%', padding: '9px 12px', borderRadius: 6, fontSize: 13, textAlign: 'left', background: tab === it.id ? 'var(--bg-2)' : 'transparent', color: tab === it.id ? 'var(--t1)' : 'var(--t2)', border: 'none', cursor: 'pointer', fontFamily: 'inherit', marginBottom: 2 }}>
            <span>{it.l}</span>
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', padding: '2px 5px', border: '.5px solid var(--bd)', borderRadius: 3 }}>{it.shortcut}</span>
          </button>
        ))}

        <div style={{ marginTop: 20, padding: '0 12px' }}>
          <div style={{ fontSize: 10, fontFamily: 'var(--f-mono)', letterSpacing: '.1em', color: 'var(--t3)', marginBottom: 10 }}>YOUR COMMUNITIES</div>
          {COMMUNITIES.map((c) => (
            <button key={c.id} onClick={() => { setTab('community'); setActive(c.id); }}
              style={{ display: 'flex', alignItems: 'center', gap: 8, width: '100%', padding: '7px 0', fontSize: 12.5, textAlign: 'left', background: 'transparent', color: tab === 'community' && active === c.id ? 'var(--ac)' : 'var(--t2)', border: 'none', cursor: 'pointer', fontFamily: 'inherit' }}>
              <span style={{ width: 6, height: 6, borderRadius: 3, background: c.status === 'healthy' ? 'var(--green)' : c.status === 'attention' ? 'var(--amber)' : 'var(--t3)' }} />
              {c.name}
            </button>
          ))}
        </div>
      </nav>

      <div style={{ padding: '16px 22px', borderTop: '.5px solid var(--bd)', display: 'flex', alignItems: 'center', gap: 10 }}>
        <div style={{ width: 28, height: 28, borderRadius: 14, background: 'var(--ac)', color: 'var(--ac-fg)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, fontWeight: 600 }}>MC</div>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 12.5, color: 'var(--t1)', fontWeight: 500 }}>M. Cortés</div>
          <div style={{ fontSize: 10.5, color: 'var(--t3)' }}>marccortes.eu</div>
        </div>
        <button style={{ background: 'transparent', border: 'none', color: 'var(--t3)', cursor: 'pointer' }}>⚙</button>
      </div>
    </aside>
  );
}

function TopBar() {
  return (
    <header style={{ height: 56, display: 'flex', alignItems: 'center', padding: '0 40px', borderBottom: '.5px solid var(--bd)', background: 'var(--bg)', justifyContent: 'space-between' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <span className="pulse" />
        <span style={{ fontFamily: 'var(--f-mono)', fontSize: 11, color: 'var(--t2)', letterSpacing: '.05em' }}>SYSTEM · OPERATIONAL</span>
        <span style={{ width: 1, height: 14, background: 'var(--bd)' }} />
        <span style={{ fontSize: 12.5, color: 'var(--t2)' }}>Last sync <span style={{ color: 'var(--t1)', fontFamily: 'var(--f-mono)' }}>14s ago</span></span>
      </div>
      <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}>
        <input placeholder="Search communities, contributors, sponsors..." style={{ width: 320, padding: '7px 12px', background: 'var(--bg-1)', border: '.5px solid var(--bd-hi)', borderRadius: 6, color: 'var(--t1)', fontSize: 12.5, fontFamily: 'inherit', outline: 'none' }} />
        <a href="profile.html" style={{ fontSize: 12.5, color: 'var(--t2)', fontFamily: 'var(--f-mono)', letterSpacing: '.04em', padding: '7px 12px', border: '.5px solid var(--bd-hi)', borderRadius: 6 }}>Dashboard</a>
        <a href="editorial.html" style={{ fontSize: 12.5, color: 'var(--t2)', fontFamily: 'var(--f-mono)', letterSpacing: '.04em', padding: '7px 12px', border: '.5px solid var(--bd-hi)', borderRadius: 6 }}>← Public site</a>
        <button className="btn btn-pri" style={{ padding: '7px 14px', fontSize: 12.5 }}>Publish queue · 3</button>
      </div>
    </header>
  );
}

// ─── Overview ───────────────────────────────────────────────
function Overview() {
  const total = COMMUNITIES.reduce((a, c) => a + c.mrr, 0);
  const subs = COMMUNITIES.reduce((a, c) => a + c.subs, 0);
  const tier = { share: 0.15, license: 199 }; // Growth tier
  const yourTake = total * (1 - tier.share) - tier.license;

  return (
    <>
      <Header eyebrow="PORTFOLIO · LAST 30 DAYS" title="Four communities. One operator." sub="A snapshot of every revenue line. Click a community to drill in." />

      {/* KPI strip */}
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 10, marginBottom: 32 }}>
        <KPI label="PORTFOLIO MRR"    value={'$' + total.toLocaleString()} delta="+$2,140" trend="up" />
        <KPI label="YOU TAKE / MO"     value={'$' + Math.round(yourTake).toLocaleString()} sub={`after ${tier.share * 100}% share + $${tier.license} license`} highlight />
        <KPI label="PAID SUBSCRIBERS"  value={subs.toLocaleString()} delta="+312" trend="up" />
        <KPI label="OPEN SEATS"        value="3" sub="2 contrib · 1 lead" />
      </div>

      {/* Community grid */}
      <div style={{ marginBottom: 32 }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 14 }}>
          <h3 style={{ fontFamily: 'var(--f-display)', fontSize: 22, fontWeight: 400, letterSpacing: '-0.02em' }}>Communities</h3>
          <span style={{ fontFamily: 'var(--f-mono)', fontSize: 11, color: 'var(--t3)' }}>4 ACTIVE · 1 OPENING</span>
        </div>
        <div style={{ border: '.5px solid var(--bd)', borderRadius: 'var(--r-lg)', overflow: 'hidden', background: 'var(--bg-1)' }}>
          <div style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr 90px 110px 110px 100px 100px', padding: '12px 22px', borderBottom: '.5px solid var(--bd)', background: 'var(--bg-2)' }}>
            {['COMMUNITY', 'LEAD', 'STATUS', 'SUBS', 'MRR', 'GROWTH', 'OPEN RATE'].map((h) => (
              <span key={h} style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em', textAlign: ['SUBS','MRR','GROWTH','OPEN RATE'].includes(h) ? 'right' : 'left' }}>{h}</span>
            ))}
          </div>
          {COMMUNITIES.map((c, i) => (
            <div key={c.id} style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr 90px 110px 110px 100px 100px', padding: '14px 22px', borderBottom: i < COMMUNITIES.length - 1 ? '.5px solid var(--bd)' : 'none', alignItems: 'center' }}>
              <div>
                <div style={{ fontSize: 14, color: 'var(--t1)', fontWeight: 500 }}>{c.name}</div>
                <div style={{ fontSize: 11.5, color: 'var(--t3)', marginTop: 2 }}>{c.niche}</div>
              </div>
              <span style={{ fontSize: 13, color: c.lead.startsWith('—') ? 'var(--amber)' : 'var(--t2)' }}>{c.lead}</span>
              <StatusBadge s={c.status} />
              <span className="num" style={{ fontFamily: 'var(--f-mono)', fontSize: 13, color: 'var(--t1)', textAlign: 'right' }}>{c.subs > 0 ? c.subs.toLocaleString() : '—'}</span>
              <span className="num" style={{ fontFamily: 'var(--f-mono)', fontSize: 13, color: 'var(--t1)', textAlign: 'right' }}>{c.mrr > 0 ? '$' + c.mrr.toLocaleString() : '—'}</span>
              <span className="num" style={{ fontFamily: 'var(--f-mono)', fontSize: 12.5, color: c.growth > 0.05 ? 'var(--green)' : c.growth > 0 ? 'var(--amber)' : 'var(--t3)', textAlign: 'right' }}>
                {c.growth > 0 ? '+' + (c.growth * 100).toFixed(1) + '%' : '—'}
              </span>
              <span className="num" style={{ fontFamily: 'var(--f-mono)', fontSize: 12.5, color: 'var(--t2)', textAlign: 'right' }}>{c.openRate > 0 ? (c.openRate * 100).toFixed(0) + '%' : '—'}</span>
            </div>
          ))}
        </div>
      </div>

      {/* Two-up: events + alerts */}
      <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 16 }}>
        <Card title="System feed" sub="OS events, last 12 minutes">
          {[
            { t: 'PUBLISH', m: 'Wealth Signal · Issue #42 sent to 4,280 subs', tm: '2m' },
            { t: 'INTEL',   m: 'Climate Capital · 3 intel items accepted', tm: '6m' },
            { t: 'GROWTH',  m: 'AI Workplace · +47 paid subs (24h)', tm: '11m' },
            { t: 'ROUTE',   m: 'Brick Intelligence · Lead candidate matched', tm: '14m' },
          ].map((e, i) => (
            <div key={i} style={{ display: 'grid', gridTemplateColumns: '70px 1fr 40px', gap: 10, padding: '10px 0', borderBottom: i < 3 ? '.5px solid var(--bd)' : 'none', alignItems: 'center' }}>
              <span style={{ fontFamily: 'var(--f-mono)', fontSize: 9.5, fontWeight: 600, letterSpacing: '.08em', color: 'var(--ac)', padding: '3px 7px', background: 'var(--ac-d)', borderRadius: 3, textAlign: 'center' }}>{e.t}</span>
              <span style={{ fontSize: 13, color: 'var(--t1)' }}>{e.m}</span>
              <span style={{ fontFamily: 'var(--f-mono)', fontSize: 10.5, color: 'var(--t3)', textAlign: 'right' }}>{e.tm}</span>
            </div>
          ))}
        </Card>
        <Card title="Needs your attention" sub="3 items" tone="amber">
          {[
            { l: 'Climate Capital churn ↑ to 3.9%', d: 'Lead Editor briefed · review draft pivot', tm: 'Today' },
            { l: 'Brick Intelligence still seeking Lead', d: '12 candidates · 3 above 9.0 rep', tm: 'Day 4' },
            { l: 'Sponsor renewal · Wealth Signal Q4',  d: 'Acme · $4,200 · expires in 12d', tm: '12d' },
          ].map((a, i) => (
            <div key={i} style={{ padding: '12px 0', borderBottom: i < 2 ? '.5px solid var(--bd)' : 'none' }}>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                <span style={{ fontSize: 13, color: 'var(--t1)', fontWeight: 500 }}>{a.l}</span>
                <span style={{ fontFamily: 'var(--f-mono)', fontSize: 10.5, color: 'var(--amber)' }}>{a.tm}</span>
              </div>
              <div style={{ fontSize: 11.5, color: 'var(--t3)', marginTop: 3 }}>{a.d}</div>
            </div>
          ))}
        </Card>
      </div>
    </>
  );
}

function CommunityDetail({ id }) {
  const c = COMMUNITIES.find((x) => x.id === id) || COMMUNITIES[0];
  return (
    <>
      <div style={{ marginBottom: 32 }}>
        <div style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em', marginBottom: 8 }}>COMMUNITY · {c.niche.toUpperCase()}</div>
        <h2 style={{ fontFamily: 'var(--f-display)', fontSize: 36, fontWeight: 400, letterSpacing: '-0.025em' }}>{c.name}</h2>
        <div style={{ marginTop: 10, display: 'flex', gap: 16, fontSize: 12.5, color: 'var(--t2)' }}>
          <span>Lead: <span style={{ color: 'var(--ac)' }}>{c.lead}</span></span>
          <span>·</span><span>Contributors: {c.contribs} / 3</span>
          <span>·</span><span>Streak: {c.streak}w</span>
          <span>·</span><StatusBadge s={c.status} />
        </div>
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 10, marginBottom: 24 }}>
        <KPI label="SUBSCRIBERS" value={c.subs.toLocaleString()} delta={'+' + Math.round(c.subs * c.growth / 4)} trend="up" />
        <KPI label="MRR"          value={'$' + c.mrr.toLocaleString()} highlight />
        <KPI label="OPEN RATE"    value={(c.openRate * 100).toFixed(0) + '%'} sub="last 4 issues" />
        <KPI label="CHURN"        value={(c.churn * 100).toFixed(1) + '%'} trend={c.churn > 0.03 ? 'down' : 'up'} />
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 16 }}>
        <Card title="Issue cadence" sub="Last 8 weeks">
          <Spark />
        </Card>
        <Card title="Subscriber growth" sub="Cumulative">
          <Spark inverted />
        </Card>
      </div>
    </>
  );
}

function Spark({ inverted }) {
  const pts = [4, 6, 5, 7, 8, 7, 10, 12];
  const W = 480, H = 120;
  const max = Math.max(...pts);
  const path = pts.map((y, i) => `${i ? 'L' : 'M'}${(i / (pts.length - 1)) * (W - 20) + 10},${H - 10 - (y / max) * (H - 30)}`).join(' ');
  return (
    <svg width="100%" viewBox={`0 0 ${W} ${H}`}>
      <path d={path} stroke={inverted ? 'var(--blue)' : 'var(--ac)'} strokeWidth="1.5" fill="none" />
      <path d={path + ` L ${W-10},${H-10} L 10,${H-10} Z`} fill={inverted ? 'var(--blue)' : 'var(--ac)'} opacity=".08" />
    </svg>
  );
}

function Brokerage() {
  return (
    <>
      <Header eyebrow="WAIVER + COVERAGE BROKERAGE" title="Continuity, not punishment." sub="Active waivers and the contributors covering them." />
      <Card title="Active coverage assignments">
        {[
          { from: 'A. Ohanian', to: 'S. Park', com: 'Wealth Signal', dur: '14d', split: '40 / 25 / 35', status: 'Active' },
          { from: 'M. Patel',   to: 'I. Rao',  com: 'AI Workplace',  dur: '7d',  split: '40 / 25 / 35', status: 'Active' },
          { from: 'L. Tomek',   to: 'pending', com: 'SaaS Signals',  dur: '10d', split: '—',           status: 'Brokering' },
        ].map((r, i) => (
          <div key={i} style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 130px 90px 130px 90px', padding: '14px 0', borderBottom: i < 2 ? '.5px solid var(--bd)' : 'none', alignItems: 'center', gap: 12 }}>
            <span style={{ fontSize: 13.5, color: 'var(--t1)' }}>{r.from} → <span style={{ color: r.to === 'pending' ? 'var(--amber)' : 'var(--ac)' }}>{r.to}</span></span>
            <span style={{ fontSize: 13, color: 'var(--t2)' }}>{r.com}</span>
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 12, color: 'var(--t2)' }}>{r.dur}</span>
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 11, color: 'var(--t3)' }}>{r.split}</span>
            <StatusBadge s={r.status === 'Active' ? 'healthy' : 'attention'} label={r.status} />
            <button className="btn btn-ghost" style={{ padding: '6px 12px', fontSize: 12 }}>Manage</button>
          </div>
        ))}
      </Card>
    </>
  );
}

function Sponsors() {
  const rows = [
    { com: 'Wealth Signal',     sponsor: 'Acme Capital',   amt: 4200, status: 'Booked',   issue: '#43' },
    { com: 'AI Workplace',      sponsor: 'Ramp',           amt: 3800, status: 'Booked',   issue: '#48' },
    { com: 'Climate Capital',   sponsor: 'Stripe Climate', amt: 2900, status: 'Negotiating', issue: '#21' },
    { com: 'Longevity Stack',   sponsor: 'Function Health',amt: 3400, status: 'Renewal due',  issue: '#28' },
  ];
  return (
    <>
      <Header eyebrow="SPONSOR PIPELINE" title="$14,300 booked this cycle." sub="Q3 · 4 active sponsors across 4 communities." />
      <Card title="Active deals">
        <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1.4fr 100px 130px 100px 100px', padding: '10px 0', borderBottom: '.5px solid var(--bd)' }}>
          {['COMMUNITY','SPONSOR','ISSUE','STATUS','AMOUNT', ''].map((h) => (
            <span key={h} style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em' }}>{h}</span>
          ))}
        </div>
        {rows.map((r, i) => (
          <div key={i} style={{ display: 'grid', gridTemplateColumns: '1.4fr 1.4fr 100px 130px 100px 100px', padding: '12px 0', borderBottom: i < rows.length - 1 ? '.5px solid var(--bd)' : 'none', alignItems: 'center' }}>
            <span style={{ fontSize: 13, color: 'var(--t1)' }}>{r.com}</span>
            <span style={{ fontSize: 13, color: 'var(--t2)' }}>{r.sponsor}</span>
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 12, color: 'var(--t2)' }}>{r.issue}</span>
            <StatusBadge s={r.status === 'Booked' ? 'healthy' : 'attention'} label={r.status} />
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 13, color: 'var(--t1)' }}>${r.amt.toLocaleString()}</span>
            <button className="btn btn-ghost" style={{ padding: '6px 12px', fontSize: 12 }}>View</button>
          </div>
        ))}
      </Card>
    </>
  );
}

function Revenue() {
  const total = 25270;
  const license = 199;
  const share = total * 0.15;
  const take = total - license - share;

  return (
    <>
      <Header eyebrow="REVENUE & PAYOUTS · CURRENT CYCLE" title={'$' + total.toLocaleString() + ' gross'} sub="Where every dollar goes." />
      <Card title="Waterfall · this month">
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 12, marginBottom: 14 }}>
          {[
            { l: 'Gross',           v: total,   c: 'var(--t1)' },
            { l: 'Platform share',  v: share,   c: 'var(--purple)', neg: true },
            { l: 'License (Growth)',v: license, c: 'var(--amber)',  neg: true },
            { l: 'You take',        v: take,    c: 'var(--green)' },
          ].map((it) => (
            <div key={it.l}>
              <div style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em', marginBottom: 8 }}>{it.l.toUpperCase()}</div>
              <div style={{ fontFamily: 'var(--f-mono)', fontSize: 22, color: it.c, fontWeight: 500 }}>{it.neg ? '−' : ''}${Math.round(it.v).toLocaleString()}</div>
              <div style={{ marginTop: 8, height: 4, background: 'var(--bg-3)', borderRadius: 2, overflow: 'hidden' }}>
                <div style={{ width: Math.max(2, (it.v / total) * 100) + '%', height: '100%', background: it.c, opacity: it.neg ? .5 : 1 }} />
              </div>
            </div>
          ))}
        </div>
      </Card>
      <div style={{ height: 16 }} />
      <Card title="Contributor payouts · queued" sub="Settled at month end">
        {[
          { name: 'A. Ohanian', com: 'Wealth Signal · Lead',     amt: 2140 },
          { name: 'S. Park',    com: 'Longevity Stack · Lead',   amt: 1820 },
          { name: 'M. Patel',   com: 'AI Workplace · Lead',      amt: 1740 },
          { name: 'I. Rao',     com: 'Wealth Signal · Contrib',  amt: 680 },
          { name: 'L. Tomek',   com: 'AI Workplace · Contrib',   amt: 520 },
        ].map((p, i) => (
          <div key={i} style={{ display: 'grid', gridTemplateColumns: '1fr 1.4fr 100px', padding: '10px 0', borderBottom: i < 4 ? '.5px solid var(--bd)' : 'none', alignItems: 'center' }}>
            <span style={{ fontSize: 13, color: 'var(--t1)' }}>{p.name}</span>
            <span style={{ fontSize: 12.5, color: 'var(--t2)' }}>{p.com}</span>
            <span style={{ fontFamily: 'var(--f-mono)', fontSize: 13, color: 'var(--green)', textAlign: 'right' }}>${p.amt.toLocaleString()}</span>
          </div>
        ))}
      </Card>
    </>
  );
}

function Team() {
  return (
    <>
      <Header eyebrow="TEAM & SEATS" title="13 contributors. 3 open seats." sub="The four-slot rule applies portfolio-wide. Reduce / pause / replace from here." />
      <Card title="All seats">
        <div style={{ fontSize: 12.5, color: 'var(--t2)' }}>Detailed seat-management table — names, slots used, reputation, streak, recent activity.</div>
      </Card>
    </>
  );
}

// ─── Building blocks ────────────────────────────────────────
function Header({ eyebrow, title, sub }) {
  return (
    <div style={{ marginBottom: 28 }}>
      <div style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em', marginBottom: 10 }}>{eyebrow}</div>
      <h2 style={{ fontFamily: 'var(--f-display)', fontSize: 36, fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.1 }}>{title}</h2>
      {sub && <p style={{ marginTop: 8, fontSize: 14, color: 'var(--t2)' }}>{sub}</p>}
    </div>
  );
}

function KPI({ label, value, sub, delta, trend, highlight }) {
  return (
    <div style={{ padding: '20px 22px', background: highlight ? 'var(--ac-d)' : 'var(--bg-1)', border: '.5px solid ' + (highlight ? 'var(--bd-ac)' : 'var(--bd)'), borderRadius: 'var(--r-lg)' }}>
      <div style={{ fontFamily: 'var(--f-mono)', fontSize: 10, color: 'var(--t3)', letterSpacing: '.1em', marginBottom: 12 }}>{label}</div>
      <div style={{ fontFamily: 'var(--f-mono)', fontSize: 28, color: highlight ? 'var(--ac)' : 'var(--t1)', fontWeight: 500, letterSpacing: '-0.02em' }}>{value}</div>
      {sub && <div style={{ fontSize: 11.5, color: 'var(--t3)', marginTop: 6 }}>{sub}</div>}
      {delta && <div style={{ fontFamily: 'var(--f-mono)', fontSize: 11, color: trend === 'up' ? 'var(--green)' : 'var(--red)', marginTop: 6 }}>{delta}</div>}
    </div>
  );
}

function Card({ title, sub, tone, children }) {
  return (
    <div style={{ padding: '22px 24px', background: 'var(--bg-1)', border: '.5px solid ' + (tone === 'amber' ? 'rgba(251,176,64,.2)' : 'var(--bd)'), borderRadius: 'var(--r-lg)' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 16 }}>
        <h4 style={{ fontFamily: 'var(--f-display)', fontSize: 18, fontWeight: 500, letterSpacing: '-0.015em' }}>{title}</h4>
        {sub && <span style={{ fontFamily: 'var(--f-mono)', fontSize: 11, color: 'var(--t3)' }}>{sub}</span>}
      </div>
      {children}
    </div>
  );
}

function StatusBadge({ s, label }) {
  const map = {
    healthy:   { c: 'var(--green)',  bg: 'var(--green-d)', l: label || 'Healthy' },
    attention: { c: 'var(--amber)',  bg: 'var(--amber-d)', l: label || 'Attention' },
    opening:   { c: 'var(--blue)',   bg: 'var(--blue-d)',  l: label || 'Opening' },
  };
  const m = map[s] || map.healthy;
  return <span style={{ fontFamily: 'var(--f-mono)', fontSize: 9.5, fontWeight: 600, letterSpacing: '.08em', color: m.c, padding: '3px 8px', background: m.bg, borderRadius: 3, justifySelf: 'start' }}>{m.l.toUpperCase()}</span>;
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
