// Account.jsx — member dashboard with internal nav: overview / orders / subscription / settings / logout
// SECURITY: identity is read from props (server-validated session in prod). No URL param parsing.
// All user-supplied fields rendered via React {} (no innerHTML, no reflection).

const Account = ({ 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',     glyph:'~' },
    { id:'orders',       label:'ORDERS',       glyph:'>' },
    { id:'subscription', label:'SUBSCRIPTION', glyph:'#' },
    { id:'settings',     label:'SETTINGS',     glyph:'$' },
  ];

  return (
    <section style={{ padding:'40px 24px 80px', maxWidth: 1280, margin:'0 auto' }}>
      {/* breadcrumb + identity strip */}
      <div style={{ display:'flex', alignItems:'baseline', gap: 14, marginBottom: 28, flexWrap:'wrap' }}>
        <h2 style={{ fontSize: 36, letterSpacing:'0.04em', textTransform:'uppercase', fontWeight: 800, color:'#FFF', margin: 0 }}>{'>'} /account</h2>
        <span style={{ fontSize: 13, color:'#BFBFBF', letterSpacing:'0.18em' }}>// authenticated as <span style={{ color:'#00FF41' }}>{user.handle}</span> · tier: <span style={{ color:'#FFD23F' }}>{user.tier}</span></span>
        <button onClick={onLogout} style={{ marginLeft:'auto', background:'transparent', border:'1px solid #FF3344', color:'#FF3344', padding:'8px 14px', fontFamily:'inherit', fontSize: 11, letterSpacing:'0.2em', cursor:'pointer', borderRadius: 2, fontWeight: 700 }}>
          [ EXIT ] ./logout
        </button>
      </div>

      <div style={{ display:'grid', gridTemplateColumns:'220px 1fr', gap: 28 }}>
        {/* sidebar */}
        <aside style={{ position:'sticky', top: 120, alignSelf:'start' }}>
          <div style={{ fontSize: 11, letterSpacing:'0.22em', color:'#E0E0E0', fontWeight: 700, marginBottom: 10 }}>// SECTIONS</div>
          <div style={{ display:'flex', flexDirection:'column', gap: 4 }}>
            {navItems.map((n) => (
              <button key={n.id} onClick={() => setView(n.id)} style={{
                padding:'12px 14px', textAlign:'left', display:'flex', alignItems:'center', gap: 10,
                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: 12, fontWeight: 700, letterSpacing:'0.12em',
                cursor:'pointer', borderRadius: 2,
              }}>
                <span style={{ width: 14 }}>{view === n.id ? '>' : n.glyph}</span>
                <span>{n.label}</span>
              </button>
            ))}
          </div>

          {/* mini quick-stats */}
          <div style={{ marginTop: 24, padding: 14, border:'1px dashed rgba(255,255,255,0.28)' }}>
            <div style={{ fontSize: 10, color:'#BFBFBF', letterSpacing:'0.22em' }}>POINTS</div>
            <div style={{ fontSize: 22, fontWeight: 800, color:'#00FF41', marginTop: 6 }}>{user.points.toLocaleString()}</div>
            <div style={{ fontSize: 10, color:'#9A9A9A', letterSpacing:'0.15em', marginTop: 4 }}>// 1pt per $1</div>
          </div>
        </aside>

        {/* main area */}
        <div>
          {view === 'overview'     && <Overview user={user} orders={orders} sub={sub} onView={setView} onShop={onShop} />}
          {view === 'orders'       && <Orders orders={orders} />}
          {view === 'subscription' && <Subscription sub={sub} onShop={onShop} />}
          {view === 'settings'     && <Settings user={user} />}
        </div>
      </div>
    </section>
  );
};

// ===================== OVERVIEW =====================
const Overview = ({ 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>
      <div style={{ padding:'18px 20px', background:'rgba(0,255,65,0.06)', border:'1px solid rgba(0,255,65,0.4)', color:'#00FF41', fontSize: 14, letterSpacing:'0.04em', lineHeight: 1.7, marginBottom: 28 }}>
        $ cat welcome.txt<br/>
        # welcome back, <span style={{ fontWeight: 800 }}>{user.handle}</span>.<br/>
        # last login: just now. ip: redacted. session: {Math.random().toString(36).slice(2, 10).toUpperCase()}.<span style={{ background:'#00FF41', color:'#000', marginLeft: 4, padding:'0 4px' }}>_</span>
      </div>

      {/* stat tiles */}
      <div style={{ display:'grid', gridTemplateColumns:'repeat(4, 1fr)', gap: 14, marginBottom: 28 }}>
        <Stat label="OPEN ORDERS"        value={open} color="#00FF41" />
        <Stat label="LIFETIME ORDERS"    value={orders.length} color="#FFD23F" />
        <Stat label="LIFETIME SPEND"     value={`$${total}`} color="#00E5FF" />
        <Stat label="SUBSCRIPTION"       value={sub.active ? 'ACTIVE' : 'PAUSED'} color={sub.active ? '#00FF41' : '#FF8C42'} />
      </div>

      {/* quick blocks */}
      <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr', gap: 16 }}>
        {/* last order */}
        <Block title="// LAST ORDER" onClick={() => onView('orders')} cta="./view-all-orders →">
          {lastOrder ? (
            <>
              <div style={{ display:'flex', alignItems:'baseline', justifyContent:'space-between' }}>
                <span style={{ fontSize: 16, fontWeight: 800, color:'#00FF41', letterSpacing:'0.04em' }}>{lastOrder.id}</span>
                <StatusPill status={lastOrder.status} />
              </div>
              <div style={{ marginTop: 10, fontSize: 12, color:'#BFBFBF', letterSpacing:'0.06em' }}>placed: {lastOrder.placed} · ${lastOrder.total}</div>
              <div style={{ marginTop: 12, fontSize: 13, color:'#FFFFFF', lineHeight: 1.6 }}>
                {lastOrder.items.map((i, k) => (<div key={k}>&gt; {i.name} × {i.qty}</div>))}
              </div>
            </>
          ) : <div style={{ color:'#BFBFBF', fontSize: 12 }}>// no orders yet.</div>}
        </Block>

        {/* sub */}
        <Block title="// SUBSCRIPTION" onClick={() => onView('subscription')} cta="./manage →">
          <div style={{ display:'flex', alignItems:'baseline', justifyContent:'space-between' }}>
            <span style={{ fontSize: 16, fontWeight: 800, color:'#B266FF', letterSpacing:'0.04em' }}>{sub.plan}</span>
            <StatusPill status={sub.active ? 'ACTIVE' : 'PAUSED'} />
          </div>
          <div style={{ marginTop: 10, fontSize: 12, color:'#BFBFBF', letterSpacing:'0.06em' }}>next ship: {sub.next} · {sub.bag} · {sub.grind}</div>
          <div style={{ marginTop: 12, fontSize: 13, color:'#FFFFFF' }}>${sub.price}/mo · charged to •••• {sub.cardLast4}</div>
        </Block>

        {/* shipping */}
        <Block title="// SHIPPING ADDRESS" onClick={() => onView('settings')} cta="./edit →">
          <div style={{ fontSize: 13, color:'#FFFFFF', lineHeight: 1.6 }}>
            {user.shipping.line1}<br/>
            {user.shipping.line2 && (<>{user.shipping.line2}<br/></>)}
            {user.shipping.city}, {user.shipping.state} {user.shipping.zip}<br/>
            {user.shipping.country}
          </div>
        </Block>

        {/* tier */}
        <Block title="// TIER PROGRESS" onClick={() => onShop()} cta="./shop →">
          <div style={{ display:'flex', alignItems:'baseline', justifyContent:'space-between' }}>
            <span style={{ fontSize: 16, fontWeight: 800, color:'#FFD23F', letterSpacing:'0.04em' }}>{user.tier}</span>
            <span style={{ fontSize: 11, color:'#BFBFBF', letterSpacing:'0.18em' }}>{user.points.toLocaleString()} / 2000 PTS</span>
          </div>
          <div style={{ marginTop: 12, height: 8, background:'rgba(255,255,255,0.14)' }}>
            <div style={{ width: `${Math.min(100, (user.points / 2000) * 100)}%`, height:'100%', background:'#FFD23F' }}></div>
          </div>
          <div style={{ marginTop: 10, fontSize: 11, color:'#BFBFBF', letterSpacing:'0.06em' }}>// {2000 - user.points} pts to <span style={{ color:'#FF3344' }}>ROOT</span> tier (free shipping, early drops)</div>
        </Block>
      </div>
    </div>
  );
};

const Stat = ({ label, value, color }) => (
  <div style={{ padding:'18px 16px', background:'#0A0A0A', border:'1px solid rgba(255,255,255,0.22)' }}>
    <div style={{ fontSize: 10, color:'#BFBFBF', letterSpacing:'0.22em', fontWeight: 700 }}>{label}</div>
    <div style={{ marginTop: 8, fontSize: 24, fontWeight: 800, color, letterSpacing:'0.04em' }}>{value}</div>
  </div>
);

const Block = ({ title, children, cta, onClick }) => (
  <div onClick={onClick} style={{ padding: 18, background:'#0A0A0A', border:'1px solid rgba(255,255,255,0.22)', cursor: onClick ? 'pointer' : 'default', transition:'border-color 120ms, box-shadow 120ms' }}
    onMouseEnter={(e) => { if (onClick) { e.currentTarget.style.borderColor = '#00FF41'; e.currentTarget.style.boxShadow = '0 0 18px rgba(0,255,65,0.18)'; } }}
    onMouseLeave={(e) => { e.currentTarget.style.borderColor = 'rgba(255,255,255,0.22)'; e.currentTarget.style.boxShadow = 'none'; }}>
    <div style={{ display:'flex', alignItems:'baseline', justifyContent:'space-between', marginBottom: 12 }}>
      <span style={{ fontSize: 11, color:'#BFBFBF', letterSpacing:'0.22em', fontWeight: 700 }}>{title}</span>
      {cta && <span style={{ fontSize: 11, color:'#00FF41', letterSpacing:'0.18em' }}>{cta}</span>}
    </div>
    {children}
  </div>
);

const StatusPill = ({ status }) => {
  const colors = { SHIPPED:'#FFD23F', DELIVERED:'#00FF41', PROCESSING:'#00E5FF', CANCELED:'#FF3344', ACTIVE:'#00FF41', PAUSED:'#FF8C42' };
  const c = colors[status] || '#BFBFBF';
  return <span style={{ padding:'3px 10px', border:`1px solid ${c}`, color: c, fontSize: 10, fontWeight: 700, letterSpacing:'0.18em' }}>[{status}]</span>;
};

// ===================== ORDERS =====================
const Orders = ({ orders }) => {
  const [open, setOpen] = React.useState(null);
  return (
    <div>
      <h3 style={{ fontSize: 22, fontWeight: 800, color:'#FFF', letterSpacing:'0.04em', textTransform:'uppercase', margin: '0 0 6px' }}>./orders --all</h3>
      <div style={{ fontSize: 12, color:'#BFBFBF', letterSpacing:'0.18em', marginBottom: 22 }}>// {orders.length} records · sorted desc by date</div>

      {/* table header */}
      <div style={{ display:'grid', gridTemplateColumns:'1.2fr 1fr 1fr 1fr 0.6fr', gap: 12, padding:'10px 14px', background:'#0A0A0A', borderTop:'1px solid rgba(255,255,255,0.22)', borderBottom:'1px solid rgba(255,255,255,0.22)', fontSize: 10, letterSpacing:'0.22em', color:'#BFBFBF', fontWeight: 700 }}>
        <span>ORDER ID</span>
        <span>PLACED</span>
        <span>STATUS</span>
        <span style={{ textAlign:'right' }}>TOTAL</span>
        <span></span>
      </div>

      {orders.map((o) => (
        <div key={o.id}>
          <div onClick={() => setOpen(open === o.id ? null : o.id)} style={{
            display:'grid', gridTemplateColumns:'1.2fr 1fr 1fr 1fr 0.6fr', gap: 12,
            padding:'14px', borderBottom:'1px solid rgba(255,255,255,0.12)',
            cursor:'pointer', alignItems:'center', fontSize: 13, color:'#FFFFFF',
            background: open === o.id ? 'rgba(0,255,65,0.04)' : 'transparent',
          }}
            onMouseEnter={(e) => e.currentTarget.style.background = open === o.id ? 'rgba(0,255,65,0.06)' : 'rgba(255,255,255,0.03)'}
            onMouseLeave={(e) => e.currentTarget.style.background = open === o.id ? 'rgba(0,255,65,0.04)' : 'transparent'}>
            <span style={{ color:'#00FF41', fontWeight: 700, letterSpacing:'0.04em' }}>{o.id}</span>
            <span style={{ color:'#E0E0E0' }}>{o.placed}</span>
            <StatusPill status={o.status} />
            <span style={{ textAlign:'right', fontWeight: 700 }}>${o.total}</span>
            <span style={{ textAlign:'right', color:'#BFBFBF', fontSize: 11, letterSpacing:'0.15em' }}>{open === o.id ? '[ - ]' : '[ + ]'}</span>
          </div>
          {open === o.id && (
            <div style={{ padding:'18px 22px', background:'#0A0A0A', borderBottom:'1px solid rgba(255,255,255,0.22)' }}>
              <div style={{ display:'grid', gridTemplateColumns:'1.4fr 1fr', gap: 22 }}>
                {/* items */}
                <div>
                  <div style={{ fontSize: 10, letterSpacing:'0.22em', color:'#BFBFBF', marginBottom: 10 }}>// ITEMS</div>
                  {o.items.map((i, k) => (
                    <div key={k} style={{ display:'flex', justifyContent:'space-between', padding:'8px 0', borderBottom:'1px dashed rgba(255,255,255,0.18)', fontSize: 13 }}>
                      <span><span style={{ color:'#00FF41' }}>{'>'}</span> {i.name} × {i.qty} <span style={{ color:'#BFBFBF', fontSize: 11, letterSpacing:'0.1em' }}>· {i.grind}{i.note ? ` ${i.note}` : ''}</span></span>
                      <span style={{ color:'#FFF' }}>${i.price * i.qty}</span>
                    </div>
                  ))}
                </div>
                {/* tracking + actions */}
                <div>
                  <div style={{ fontSize: 10, letterSpacing:'0.22em', color:'#BFBFBF', marginBottom: 10 }}>// SHIPMENT</div>
                  <div style={{ fontSize: 12, color:'#E0E0E0', lineHeight: 1.7 }}>tracking: <span style={{ color:'#00FF41' }}>{o.track}</span></div>
                  <div style={{ marginTop: 14, display:'flex', flexDirection:'column', gap: 6 }}>
                    <SmallBtn>./track-shipment</SmallBtn>
                    <SmallBtn>./reorder</SmallBtn>
                    <SmallBtn>./request-help</SmallBtn>
                  </div>
                </div>
              </div>
            </div>
          )}
        </div>
      ))}
    </div>
  );
};

const SmallBtn = ({ 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.18em',
    cursor:'pointer', borderRadius: 2, textAlign:'left',
  }}>{'>'} {children}</button>
);

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

  return (
    <div>
      <h3 style={{ fontSize: 22, fontWeight: 800, color:'#FFF', letterSpacing:'0.04em', textTransform:'uppercase', margin: '0 0 6px' }}>./subscription</h3>
      <div style={{ fontSize: 12, color:'#BFBFBF', letterSpacing:'0.18em', marginBottom: 22 }}>// recurring caffeine delivery</div>

      <div style={{ padding: 22, background:'#0A0A0A', border:'1px solid rgba(255,255,255,0.22)', marginBottom: 18 }}>
        <div style={{ display:'flex', alignItems:'baseline', justifyContent:'space-between' }}>
          <div>
            <div style={{ fontSize: 11, color:'#BFBFBF', letterSpacing:'0.22em' }}>CURRENT PLAN</div>
            <div style={{ fontSize: 28, fontWeight: 800, color:'#B266FF', marginTop: 6, letterSpacing:'0.04em' }}>{sub.plan}</div>
          </div>
          <StatusPill status={paused ? 'PAUSED' : 'ACTIVE'} />
        </div>

        <div style={{ marginTop: 22, display:'grid', gridTemplateColumns:'repeat(4, 1fr)', gap: 1, background:'rgba(255,255,255,0.22)', border:'1px solid rgba(255,255,255,0.22)' }}>
          {[
            ['NEXT SHIP', paused ? '— PAUSED —' : sub.next],
            ['FREQUENCY', sub.frequency],
            ['BAG / GRIND', `${sub.bag} · ${sub.grind}`],
            ['PRICE / SHIP', `$${sub.price}`],
          ].map(([k, v]) => (
            <div key={k} style={{ background:'#000', padding:'12px 14px' }}>
              <div style={{ fontSize: 10, letterSpacing:'0.22em', color:'#BFBFBF', fontWeight: 700 }}>{k}</div>
              <div style={{ marginTop: 5, fontSize: 14, color:'#FFF', fontWeight: 600, letterSpacing:'0.06em' }}>{v}</div>
            </div>
          ))}
        </div>

        <div style={{ marginTop: 18, fontSize: 12, color:'#BFBFBF', letterSpacing:'0.06em' }}>
          // payment: {sub.cardBrand} •••• {sub.cardLast4} · auto-renew on · cancel anytime.
        </div>

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

      {/* confirm dialog (inline, no enumeration vector) */}
      {confirming && (
        <div style={{ padding: 18, border:`1px solid ${confirming === 'cancel' ? '#FF3344' : '#FFD23F'}`, background:'#0A0A0A', marginBottom: 18, color:'#FFF', fontSize: 13, lineHeight: 1.7 }}>
          <div style={{ color: confirming === 'cancel' ? '#FF3344' : '#FFD23F', letterSpacing:'0.18em', fontSize: 11, fontWeight: 700 }}>// CONFIRM ./{confirming}</div>
          <div style={{ marginTop: 8 }}>
            {confirming === 'cancel'
              ? "cancel subscription? your next shipment won't be charged or sent. you can re-subscribe anytime."
              : (paused ? "resume subscription? next shipment will be scheduled."
                        : "pause subscription? next shipment will be skipped. resume anytime.")}
          </div>
          <div style={{ marginTop: 14, display:'flex', gap: 8 }}>
            <ActionBtn color={confirming === 'cancel' ? '#FF3344' : '#FFD23F'} onClick={() => {
              if (confirming === 'pause') setPaused(!paused);
              setConfirming(null);
            }}>./confirm</ActionBtn>
            <ActionBtn color="#BFBFBF" onClick={() => setConfirming(null)}>./abort</ActionBtn>
          </div>
        </div>
      )}

      {/* shipment history */}
      <div style={{ padding:'18px 20px', background:'#0A0A0A', border:'1px solid rgba(255,255,255,0.22)' }}>
        <div style={{ fontSize: 11, color:'#BFBFBF', letterSpacing:'0.22em', fontWeight: 700, marginBottom: 14 }}>// SHIPMENT HISTORY</div>
        {sub.history.map((h, i) => (
          <div key={i} style={{ display:'flex', justifyContent:'space-between', padding:'10px 0', borderBottom: i === sub.history.length - 1 ? 'none' : '1px dashed rgba(255,255,255,0.18)', fontSize: 13 }}>
            <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 ActionBtn = ({ children, color = '#00FF41', onClick }) => (
  <button onClick={onClick} style={{
    background:'transparent', border:`1px solid ${color}`, color,
    padding:'10px 16px', fontFamily:'inherit', fontSize: 12, letterSpacing:'0.18em', fontWeight: 700,
    cursor:'pointer', borderRadius: 2,
  }}>{children}</button>
);

// ===================== SETTINGS =====================
const Settings = ({ 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();
    // Demo only — no submit. In prod, POST to backend w/ CSRF token + server-side validation.
    setSaved(true);
    setTimeout(() => setSaved(false), 2400);
  };

  return (
    <form onSubmit={submit}>
      <h3 style={{ fontSize: 22, fontWeight: 800, color:'#FFF', letterSpacing:'0.04em', textTransform:'uppercase', margin: '0 0 6px' }}>./settings</h3>
      <div style={{ fontSize: 12, color:'#BFBFBF', letterSpacing:'0.18em', marginBottom: 22 }}>// identity · shipping · security</div>

      <SettingsGroup title="// IDENTITY">
        <SField label="HANDLE" value={form.handle} onChange={(v) => set('handle', v)} hint="// public. shown in your receipts." />
        <SField label="DISPLAY NAME" value={form.name} onChange={(v) => set('name', v)} />
        <SField label="EMAIL" value={form.email} onChange={(v) => set('email', v)} type="email" hint="// used for receipts + 2FA. changes require re-verification." />
      </SettingsGroup>

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

      <SettingsGroup title="// SECURITY">
        <SField label="CURRENT PASSWORD" value="" onChange={() => {}} type="password" placeholder="required to change anything sensitive" />
        <SField label="NEW PASSWORD" value="" onChange={() => {}} type="password" placeholder="min 12 chars · pwned-passwords checked" />
        <div style={{ marginTop: 8, padding: 12, border:'1px dashed rgba(255,255,255,0.28)', fontSize: 12, color:'#BFBFBF', lineHeight: 1.7 }}>
          // 2fa: <span style={{ color:'#00FF41' }}>ENABLED</span> (TOTP) ·
          {' '}<span style={{ color:'#00FF41', cursor:'pointer' }}>./regenerate-recovery-codes</span> ·
          {' '}<span style={{ color:'#00FF41', cursor:'pointer' }}>./view-active-sessions</span>
        </div>
      </SettingsGroup>

      <SettingsGroup title="// NOTIFICATIONS">
        <Toggle label="ORDER UPDATES" defaultChecked />
        <Toggle label="NEW DROP ALERTS" defaultChecked />
        <Toggle label="SUBSCRIPTION REMINDERS" defaultChecked />
        <Toggle label="MARKETING (we promise: tasteful)" />
      </SettingsGroup>

      <div style={{ marginTop: 32, display:'flex', alignItems:'center', gap: 12 }}>
        <button type="submit" style={{
          background:'#00FF41', color:'#000', border:'1px solid #00FF41',
          padding:'14px 22px', fontFamily:'inherit', fontWeight: 800, fontSize: 13,
          letterSpacing:'0.22em', cursor:'pointer', borderRadius: 2,
        }}>{'>'} ./save-changes</button>
        {saved && (
          <span style={{ color:'#00FF41', fontSize: 12, letterSpacing:'0.18em' }}>
            [ OK ] saved.
          </span>
        )}
        <span style={{ marginLeft:'auto', fontSize: 10, color:'#7A7A7A', letterSpacing:'0.18em' }}>// changes audit-logged.</span>
      </div>
    </form>
  );
};

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

const SField = ({ label, value, onChange, type = 'text', placeholder, hint }) => (
  <div>
    <div style={{ fontSize: 11, color:'#E0E0E0', letterSpacing:'0.22em', fontWeight: 700, marginBottom: 5 }}>{'>'} {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: 13, padding:'10px 12px', borderRadius: 2,
      }}
      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'; }}
    />
    {hint && <div style={{ marginTop: 4, fontSize: 10, color:'#9A9A9A', letterSpacing:'0.05em' }}>{hint}</div>}
  </div>
);

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

window.Account = Account;
