// ─── Gunzaru: more screens (Chat, Create, Check-in, Payment, Packages, Profile, My Events) ───

// ═══════════════════════════════════════════════════════
// CHAT (event-specific)
// ═══════════════════════════════════════════════════════
const ChatScreen = ({ eventId, onBack }) => {
  const event = EVENTS.find(e => e.id === eventId) || EVENTS[0];
  const [draft, setDraft] = React.useState('');
  const [messages, setMessages] = React.useState([
    { id: 1, from: 'Ahmed Rasheed', isOrg: true, t: '4:12 PM', text: "Hey team! We'll meet at the east gate. Two teams will rotate every 12 minutes." },
    { id: 2, from: 'Hassan', t: '4:18 PM', text: "I'll bring an extra ball just in case 🙌" },
    { id: 3, from: 'Mariyam', t: '4:22 PM', text: 'Anyone passing by Sosun Magu? Can hop in your taxi.' },
    { id: 4, from: 'me', t: '4:24 PM', text: "I'm 10 mins away, can pick you up at the corner." },
    { id: 5, from: 'Ahmed Rasheed', isOrg: true, t: '4:30 PM', text: 'Reminder: water and dark/light bibs. Cleats optional.' },
    { id: 6, from: 'Ibrahim', t: '4:41 PM', text: 'On my way 🏃' },
  ]);
  const scrollRef = React.useRef(null);
  React.useEffect(() => { if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight; }, [messages]);

  const send = () => {
    if (!draft.trim()) return;
    setMessages([...messages, { id: Date.now(), from: 'me', t: 'now', text: draft }]);
    setDraft('');
  };

  return (
    <div style={{ height: '100%', background: 'var(--bg)', display: 'flex', flexDirection: 'column' }}>
      {/* Header */}
      <div style={{
        paddingTop: 56, paddingBottom: 12, paddingLeft: 12, paddingRight: 16,
        display: 'flex', alignItems: 'center', gap: 12,
        background: 'var(--surface)', borderBottom: '1px solid var(--line-2)',
      }}>
        <button onClick={onBack} style={{
          width: 38, height: 38, borderRadius: 12, border: 'none',
          background: 'var(--bg-2)', display: 'grid', placeItems: 'center', cursor: 'pointer',
        }}>
          <Icon name="chevronL" size={18} />
        </button>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600, color: 'var(--ink)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
            {event.title}
          </div>
          <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)', display: 'flex', alignItems: 'center', gap: 6 }}>
            <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--ok)' }} />
            {event.going} going · 12 online
          </div>
        </div>
        <button style={{ ...iconBtn, width: 38, height: 38 }}>
          <Icon name="users" size={18} />
        </button>
      </div>

      {/* Pinned event banner */}
      <div style={{ padding: '10px 16px 0' }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 10,
          padding: '10px 12px', background: 'var(--primary-soft)',
          borderRadius: 14, border: '1px solid var(--primary-soft)',
        }}>
          <Icon name="pin" size={14} color="var(--primary)" />
          <div style={{ flex: 1 }}>
            <div style={{ fontFamily: 'Geist', fontSize: 12, fontWeight: 600, color: 'var(--primary)' }}>
              {event.date} · {event.time} · {event.where}
            </div>
          </div>
          <span style={{ fontFamily: 'Geist Mono', fontSize: 9, color: 'var(--primary)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>2d to go</span>
        </div>
      </div>

      {/* Messages */}
      <div ref={scrollRef} className="scroll" style={{
        flex: 1, overflow: 'auto', padding: '14px 16px',
        display: 'flex', flexDirection: 'column', gap: 12,
      }}>
        <div style={{ textAlign: 'center', fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>
          — Today —
        </div>
        {messages.map((m, i) => {
          const mine = m.from === 'me';
          const prev = messages[i - 1];
          const sameFrom = prev && prev.from === m.from;
          return (
            <div key={m.id} style={{ display: 'flex', gap: 8, flexDirection: mine ? 'row-reverse' : 'row' }}>
              {!mine && (
                <div style={{ width: 32 }}>
                  {!sameFrom && <Avatar name={m.from} size={32} />}
                </div>
              )}
              <div style={{ maxWidth: '74%', display: 'flex', flexDirection: 'column', alignItems: mine ? 'flex-end' : 'flex-start' }}>
                {!sameFrom && !mine && (
                  <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 3, padding: '0 4px' }}>
                    <span style={{ fontFamily: 'Geist', fontSize: 11, fontWeight: 600, color: 'var(--ink)' }}>{m.from}</span>
                    {m.isOrg && <Chip tone="primary" style={{ padding: '1px 6px', fontSize: 9 }}>Organiser</Chip>}
                  </div>
                )}
                <div style={{
                  padding: '10px 14px',
                  background: mine ? 'var(--ink)' : 'var(--surface)',
                  color: mine ? 'var(--bg)' : 'var(--ink)',
                  borderRadius: mine ? '18px 18px 6px 18px' : '18px 18px 18px 6px',
                  fontFamily: 'Geist', fontSize: 14, lineHeight: 1.4,
                  border: mine ? 'none' : '1px solid var(--line-2)',
                  boxShadow: mine ? 'none' : 'var(--shadow-sm)',
                }}>{m.text}</div>
                <div style={{ fontFamily: 'Geist Mono', fontSize: 9, color: 'var(--ink-3)', marginTop: 3, padding: '0 4px' }}>{m.t}</div>
              </div>
            </div>
          );
        })}
      </div>

      {/* Composer */}
      <div style={{ padding: '8px 12px 28px', background: 'var(--bg)' }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          background: 'var(--surface)', borderRadius: 24,
          padding: 6, border: '1px solid var(--line-2)', boxShadow: 'var(--shadow-sm)',
        }}>
          <button style={{ width: 36, height: 36, border: 'none', background: 'var(--bg-2)', borderRadius: '50%', display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
            <Icon name="plus" size={20} color="var(--ink-2)" />
          </button>
          <input
            value={draft}
            onChange={e => setDraft(e.target.value)}
            onKeyDown={e => e.key === 'Enter' && send()}
            placeholder="Message the group…"
            style={{
              flex: 1, border: 'none', outline: 'none',
              fontFamily: 'Geist', fontSize: 14, color: 'var(--ink)',
              background: 'transparent', padding: '8px 4px',
            }}
          />
          <button onClick={send} style={{
            width: 36, height: 36, border: 'none', borderRadius: '50%',
            background: draft.trim() ? 'var(--primary)' : 'var(--bg-2)',
            display: 'grid', placeItems: 'center', cursor: 'pointer',
            transition: 'background .15s',
          }}>
            <Icon name="send" size={16} color={draft.trim() ? 'var(--primary-ink)' : 'var(--ink-3)'} />
          </button>
        </div>
      </div>
    </div>
  );
};

// ═══════════════════════════════════════════════════════
// CREATE EVENT
// ═══════════════════════════════════════════════════════
const CreateEventScreen = ({ onBack, onPublish }) => {
  const [step, setStep] = React.useState(1);
  const [data, setData] = React.useState({
    title: '', category: 'Sports', date: '', time: '',
    where: '', cap: 12, price: 0, chat: true, private: false,
  });

  const update = (k, v) => setData(d => ({ ...d, [k]: v }));

  return (
    <div style={{ height: '100%', background: 'var(--bg)', display: 'flex', flexDirection: 'column' }}>
      <div style={{
        paddingTop: 56, paddingBottom: 16, paddingLeft: 12, paddingRight: 16,
        display: 'flex', alignItems: 'center', gap: 12,
      }}>
        <button onClick={onBack} style={{
          width: 38, height: 38, borderRadius: 12, border: 'none',
          background: 'var(--bg-2)', display: 'grid', placeItems: 'center', cursor: 'pointer',
        }}>
          <Icon name="x" size={18} />
        </button>
        <div style={{ flex: 1 }}>
          <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>Step {step} of 3</div>
          <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 19, fontWeight: 600, color: 'var(--ink)', letterSpacing: '-0.02em' }}>
            {step === 1 ? 'What & where' : step === 2 ? 'When & who' : 'Review'}
          </div>
        </div>
        <Chip tone="accent" icon="sparkle">Gold</Chip>
      </div>

      {/* Progress */}
      <div style={{ padding: '0 20px 16px' }}>
        <div style={{ display: 'flex', gap: 6 }}>
          {[1,2,3].map(s => (
            <div key={s} style={{
              flex: 1, height: 4, borderRadius: 2,
              background: s <= step ? 'var(--primary)' : 'var(--line)',
              transition: 'background .25s',
            }} />
          ))}
        </div>
      </div>

      <div className="scroll" style={{ flex: 1, overflow: 'auto', padding: '0 20px 20px' }}>
        {step === 1 && (
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <Field label="Event title">
              <input value={data.title} onChange={e => update('title', e.target.value)} placeholder="e.g. Pickup Football at Galolhu" style={inputStyle} />
            </Field>

            <Field label="Category">
              <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8 }}>
                {['Sports','Outdoors','Study','Tech','Food','Community'].map(c => (
                  <button key={c} onClick={() => update('category', c)} style={{
                    padding: '10px 6px', borderRadius: 12,
                    border: '1px solid ' + (data.category === c ? 'var(--ink)' : 'var(--line)'),
                    background: data.category === c ? 'var(--ink)' : 'var(--surface)',
                    color: data.category === c ? 'var(--bg)' : 'var(--ink)',
                    fontFamily: 'Geist', fontSize: 12, fontWeight: 500, cursor: 'pointer',
                  }}>{c}</button>
                ))}
              </div>
            </Field>

            <Field label="Location">
              <div style={{ ...inputStyle, display: 'flex', alignItems: 'center', gap: 8, padding: '0 14px' }}>
                <Icon name="pin" size={16} color="var(--ink-3)" />
                <input value={data.where} onChange={e => update('where', e.target.value)} placeholder="Galolhu Stadium, Malé" style={{ flex: 1, border: 'none', outline: 'none', fontFamily: 'Geist', fontSize: 14, background: 'transparent', padding: '14px 0' }} />
              </div>
              <Placeholder h={120} label="map preview · drop a pin" hue={195} style={{ marginTop: 8 }} />
            </Field>

            <Field label="Photo">
              <button style={{
                width: '100%', padding: '24px', borderRadius: 16,
                border: '1.5px dashed var(--line)', background: 'var(--bg-2)',
                display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, cursor: 'pointer',
              }}>
                <Icon name="camera" size={22} color="var(--ink-3)" />
                <div style={{ fontFamily: 'Geist', fontSize: 13, color: 'var(--ink-2)', fontWeight: 500 }}>Add a cover photo</div>
                <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>Optional · 16:9 recommended</div>
              </button>
            </Field>
          </div>
        )}

        {step === 2 && (
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
              <Field label="Date">
                <div style={{ ...inputStyle, display: 'flex', alignItems: 'center', gap: 8, padding: '14px' }}>
                  <Icon name="calendar" size={16} color="var(--ink-3)" />
                  <span style={{ fontFamily: 'Geist', fontSize: 14 }}>Fri, 22 May</span>
                </div>
              </Field>
              <Field label="Time">
                <div style={{ ...inputStyle, display: 'flex', alignItems: 'center', gap: 8, padding: '14px' }}>
                  <Icon name="clock" size={16} color="var(--ink-3)" />
                  <span style={{ fontFamily: 'Geist', fontSize: 14 }}>5:30 PM</span>
                </div>
              </Field>
            </div>

            <Field label="Capacity" sub={`Max attendees · ${data.cap} people`}>
              <input type="range" min="4" max="100" step="2" value={data.cap} onChange={e => update('cap', +e.target.value)}
                style={{ width: '100%', accentColor: 'var(--primary)' }} />
              <div style={{ display: 'flex', justifyContent: 'space-between', fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)' }}>
                <span>4</span><span>50</span><span>100+</span>
              </div>
            </Field>

            <Field label="Price per person (MVR)">
              <div style={{ display: 'flex', gap: 8 }}>
                {[0, 50, 100, 250].map(p => (
                  <button key={p} onClick={() => update('price', p)} style={{
                    flex: 1, padding: '12px 6px', borderRadius: 12,
                    border: '1px solid ' + (data.price === p ? 'var(--ink)' : 'var(--line)'),
                    background: data.price === p ? 'var(--ink)' : 'var(--surface)',
                    color: data.price === p ? 'var(--bg)' : 'var(--ink)',
                    fontFamily: 'Geist', fontSize: 13, fontWeight: 600, cursor: 'pointer',
                  }}>{p === 0 ? 'Free' : mvr(p)}</button>
                ))}
              </div>
            </Field>

            <Field label="Options">
              <Card padding={0} style={{ overflow: 'hidden' }}>
                <OptRow icon="chat" label="Enable event chat" sub="Only attendees can post" on={data.chat} onClick={() => update('chat', !data.chat)} />
                <OptRow icon="lock" label="Private event" sub="Invite-only, hidden from feed" on={data.private} onClick={() => update('private', !data.private)} isLast />
              </Card>
            </Field>
          </div>
        )}

        {step === 3 && (
          <div>
            <Card padding={0} style={{ overflow: 'hidden' }}>
              <Placeholder h={140} label="cover · tap to change" hue={35} style={{ borderRadius: 0 }} />
              <div style={{ padding: 16 }}>
                <Chip tone="primary">{data.category}</Chip>
                <h3 style={{ margin: '8px 0 0', fontFamily: 'Bricolage Grotesque', fontSize: 20, fontWeight: 600, letterSpacing: '-0.02em' }}>
                  {data.title || 'Pickup Football at Galolhu'}
                </h3>
                <div style={{ display: 'flex', gap: 14, marginTop: 8, color: 'var(--ink-2)', fontSize: 13 }}>
                  <span><Icon name="calendar" size={13} style={{ verticalAlign: -1 }}/> Fri, 22 May · 5:30 PM</span>
                </div>
                <div style={{ display: 'flex', gap: 14, marginTop: 4, color: 'var(--ink-2)', fontSize: 13 }}>
                  <span><Icon name="pin" size={13} style={{ verticalAlign: -1 }}/> {data.where || 'Galolhu Stadium, Malé'}</span>
                </div>
              </div>
            </Card>
            <div style={{ marginTop: 14, display: 'flex', flexDirection: 'column', gap: 10 }}>
              <ReviewRow l="Capacity" v={`${data.cap} people`} />
              <ReviewRow l="Price" v={data.price === 0 ? 'Free' : mvr(data.price) + ' / person'} />
              <ReviewRow l="Chat" v={data.chat ? 'Enabled' : 'Off'} />
              <ReviewRow l="Privacy" v={data.private ? 'Private' : 'Public'} />
            </div>
            <div style={{
              marginTop: 16, padding: 14, background: 'var(--accent-soft)',
              borderRadius: 16, display: 'flex', gap: 10,
            }}>
              <Icon name="sparkle" size={18} color="var(--accent)" />
              <div style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-2)', lineHeight: 1.45 }}>
                Once published, attendees can RSVP and join the chat. You'll get a notification for every check-in.
              </div>
            </div>
          </div>
        )}
      </div>

      <div style={{ padding: '12px 16px 28px', background: 'var(--bg)', borderTop: '1px solid var(--line-2)' }}>
        <div style={{ display: 'flex', gap: 10 }}>
          {step > 1 && <Button variant="secondary" size="lg" onClick={() => setStep(step - 1)} style={{ flex: 1 }}>Back</Button>}
          {step < 3 ? (
            <Button variant="primary" size="lg" onClick={() => setStep(step + 1)} style={{ flex: 2 }}>Continue</Button>
          ) : (
            <Button variant="primary" size="lg" onClick={onPublish} style={{ flex: 2 }}>Publish event</Button>
          )}
        </div>
      </div>
    </div>
  );
};

const inputStyle = {
  width: '100%', height: 50, borderRadius: 14,
  border: '1px solid var(--line)', background: 'var(--surface)',
  fontFamily: 'Geist', fontSize: 14, color: 'var(--ink)',
  padding: '0 14px', outline: 'none',
};
const Field = ({ label, sub, children }) => (
  <div>
    <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 8 }}>
      <label style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>{label}</label>
      {sub && <span style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>{sub}</span>}
    </div>
    {children}
  </div>
);
const OptRow = ({ icon, label, sub, on, onClick, isLast }) => (
  <div style={{
    display: 'flex', alignItems: 'center', gap: 12, padding: 14,
    borderBottom: isLast ? 'none' : '1px solid var(--line-2)',
  }}>
    <div style={{ width: 32, height: 32, borderRadius: 9, background: 'var(--bg-2)', display: 'grid', placeItems: 'center' }}>
      <Icon name={icon} size={16} color="var(--ink-2)" />
    </div>
    <div style={{ flex: 1 }}>
      <div style={{ fontFamily: 'Geist', fontSize: 13, fontWeight: 600 }}>{label}</div>
      <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>{sub}</div>
    </div>
    <Toggle on={on} onClick={onClick} />
  </div>
);
const ReviewRow = ({ l, v }) => (
  <div style={{
    display: 'flex', justifyContent: 'space-between', alignItems: 'center',
    padding: '12px 16px', background: 'var(--surface)', borderRadius: 14,
    border: '1px solid var(--line-2)',
  }}>
    <span style={{ fontFamily: 'Geist', fontSize: 13, color: 'var(--ink-3)' }}>{l}</span>
    <span style={{ fontFamily: 'Geist', fontSize: 14, fontWeight: 600, color: 'var(--ink)' }}>{v}</span>
  </div>
);

// ═══════════════════════════════════════════════════════
// CHECK-IN
// ═══════════════════════════════════════════════════════
const CheckinScreen = ({ eventId, onBack }) => {
  const event = EVENTS.find(e => e.id === eventId) || EVENTS[0];
  const [done, setDone] = React.useState(false);

  return (
    <div style={{ height: '100%', background: 'var(--ink)', color: 'var(--bg)', position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <div style={{ paddingTop: 56, paddingBottom: 16, paddingLeft: 12, paddingRight: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
        <button onClick={onBack} style={{ width: 38, height: 38, borderRadius: 12, border: 'none', background: 'rgba(255,255,255,0.1)', display: 'grid', placeItems: 'center', cursor: 'pointer', color: 'var(--bg)' }}>
          <Icon name="x" size={18} />
        </button>
        <div style={{ flex: 1, textAlign: 'center' }}>
          <div style={{ fontFamily: 'Geist Mono', fontSize: 10, letterSpacing: '0.14em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.5)' }}>Check in</div>
          <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600 }}>{event.title}</div>
        </div>
        <div style={{ width: 38 }}/>
      </div>

      {!done ? (
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 32 }}>
          <div style={{
            width: 260, height: 260, borderRadius: 28, position: 'relative',
            background: 'rgba(255,255,255,0.05)', padding: 16,
            border: '1px solid rgba(255,255,255,0.1)',
          }}>
            {/* QR-like pattern */}
            <div style={{
              width: '100%', height: '100%', borderRadius: 12,
              background: 'var(--bg)', padding: 16, position: 'relative',
            }}>
              <FauxQR />
              {/* corner marks */}
              <CornerMark pos="tl" />
              <CornerMark pos="tr" />
              <CornerMark pos="bl" />
              <CornerMark pos="br" />
            </div>
            {/* scan animation line */}
            <div style={{
              position: 'absolute', top: 16, left: 16, right: 16, height: 2,
              background: 'linear-gradient(90deg, transparent, var(--accent), transparent)',
              animation: 'scan 2.2s ease-in-out infinite',
              boxShadow: '0 0 14px var(--accent)',
            }} />
            <style>{`@keyframes scan { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(220px); } }`}</style>
          </div>
          <div style={{ marginTop: 32, textAlign: 'center', maxWidth: 280 }}>
            <h2 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 600, letterSpacing: '-0.025em' }}>
              Show this to the organiser
            </h2>
            <p style={{ margin: '8px 0 0', fontFamily: 'Geist', fontSize: 14, color: 'rgba(255,255,255,0.7)', lineHeight: 1.4 }}>
              Or tap below to check yourself in when you've arrived at <em style={{ fontFamily: 'Instrument Serif', fontStyle: 'italic' }}>{event.where}</em>.
            </p>
          </div>
          <div style={{ marginTop: 28, display: 'flex', gap: 10, alignItems: 'center' }}>
            <div style={{ width: 8, height: 8, borderRadius: '50%', background: 'var(--accent)', boxShadow: '0 0 8px var(--accent)' }} />
            <div style={{ fontFamily: 'Geist Mono', fontSize: 11, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.6)' }}>
              Waiting · Organiser nearby
            </div>
          </div>
        </div>
      ) : (
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 32, textAlign: 'center' }}>
          <div style={{
            width: 96, height: 96, borderRadius: '50%',
            background: 'var(--ok)', display: 'grid', placeItems: 'center',
            boxShadow: '0 0 40px rgba(95, 180, 130, 0.4)', marginBottom: 24,
          }}>
            <Icon name="check" size={48} color="#fff" strokeWidth={3} />
          </div>
          <h2 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 30, fontWeight: 600, letterSpacing: '-0.03em' }}>You're checked in</h2>
          <p style={{ margin: '8px 0 0', fontFamily: 'Geist', fontSize: 14, color: 'rgba(255,255,255,0.7)' }}>
            {event.going + 1} of {event.cap} confirmed
          </p>
        </div>
      )}

      <div style={{ padding: '12px 16px 32px' }}>
        <Button variant="accent" size="lg" full onClick={() => setDone(d => !d)}>
          {done ? "Back to event" : "I'm here · check me in"}
        </Button>
      </div>
    </div>
  );
};

const FauxQR = () => {
  // 17x17 grid of pseudo-random squares
  const cells = React.useMemo(() => Array.from({ length: 17 * 17 }, () => Math.random() > 0.5), []);
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: 'repeat(17, 1fr)',
      width: '100%', height: '100%', gap: 1,
    }}>
      {cells.map((on, i) => {
        const x = i % 17, y = Math.floor(i / 17);
        // hollow out corners for finder rectangles
        if ((x < 4 && y < 4) || (x > 12 && y < 4) || (x < 4 && y > 12)) return <div key={i}/>;
        return <div key={i} style={{ background: on ? 'var(--ink)' : 'transparent' }} />;
      })}
    </div>
  );
};
const CornerMark = ({ pos }) => {
  const s = { position: 'absolute', width: 38, height: 38, background: 'var(--ink)', borderRadius: 6, padding: 6 };
  const m = pos.includes('t') ? { top: 16 } : { bottom: 16 };
  const m2 = pos.includes('l') ? { left: 16 } : { right: 16 };
  return (
    <div style={{ ...s, ...m, ...m2 }}>
      <div style={{ width: '100%', height: '100%', borderRadius: 3, background: 'var(--bg)', display: 'grid', placeItems: 'center' }}>
        <div style={{ width: 12, height: 12, borderRadius: 2, background: 'var(--ink)' }} />
      </div>
    </div>
  );
};

// ═══════════════════════════════════════════════════════
// PAYMENT — card scanner
// ═══════════════════════════════════════════════════════
const PaymentScreen = ({ pkg, onBack, onDone }) => {
  const selected = PACKAGES.find(p => p.id === pkg) || PACKAGES[2];
  const [stage, setStage] = React.useState('scan'); // scan -> form -> done
  const [cvv, setCvv] = React.useState('');

  return (
    <div style={{ height: '100%', background: 'var(--bg)', display: 'flex', flexDirection: 'column' }}>
      <div style={{ paddingTop: 56, paddingBottom: 16, paddingLeft: 12, paddingRight: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
        <button onClick={onBack} style={{ width: 38, height: 38, borderRadius: 12, border: 'none', background: 'var(--bg-2)', display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
          <Icon name="chevronL" size={18} />
        </button>
        <div style={{ flex: 1 }}>
          <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>Subscribe to {selected.name}</div>
          <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 19, fontWeight: 600 }}>Payment</div>
        </div>
        <Chip>{mvr(selected.price)}/mo</Chip>
      </div>

      <div className="scroll" style={{ flex: 1, overflow: 'auto', padding: '0 20px 20px' }}>
        {stage === 'scan' && (
          <div>
            <div style={{
              borderRadius: 22, overflow: 'hidden', position: 'relative',
              aspectRatio: '1.586', background: '#0e1a26',
              boxShadow: '0 20px 60px rgba(0,0,0,0.25)',
            }}>
              {/* fake card viewfinder */}
              <div style={{
                position: 'absolute', inset: 0,
                background:
                  'radial-gradient(80% 60% at 30% 20%, rgba(120,180,220,0.25), transparent 60%),' +
                  'linear-gradient(135deg, #0e1a26, #1c3a52)',
              }} />
              {/* grid */}
              <svg width="100%" height="100%" style={{ position: 'absolute', inset: 0, opacity: 0.06 }}>
                <defs>
                  <pattern id="g" width="20" height="20" patternUnits="userSpaceOnUse">
                    <path d="M20 0 L 0 0 0 20" stroke="#fff" strokeWidth="0.5" fill="none"/>
                  </pattern>
                </defs>
                <rect width="100%" height="100%" fill="url(#g)" />
              </svg>
              {/* card frame */}
              <div style={{
                position: 'absolute', inset: '14% 8%',
                borderRadius: 14,
                background: 'linear-gradient(135deg, oklch(70% 0.06 230), oklch(55% 0.08 240))',
                boxShadow: 'inset 0 -3px 0 rgba(0,0,0,0.2), 0 4px 20px rgba(0,0,0,0.4)',
                padding: 16, display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
                color: '#fff',
              }}>
                <div style={{ display: 'flex', justifyContent: 'space-between' }}>
                  <div style={{ fontFamily: 'Geist Mono', fontSize: 10, letterSpacing: '0.2em', color: 'rgba(255,255,255,0.7)' }}>BML · DEBIT</div>
                  <div style={{ width: 30, height: 22, background: 'oklch(80% 0.12 75)', borderRadius: 4, opacity: 0.8 }} />
                </div>
                <div style={{ fontFamily: 'Geist Mono', fontSize: 16, letterSpacing: '0.16em' }}>
                  4532 ••18 •••• 7204
                </div>
                <div style={{ display: 'flex', justifyContent: 'space-between', fontFamily: 'Geist Mono', fontSize: 10, letterSpacing: '0.1em' }}>
                  <span>M. HASSAN</span>
                  <span>08/29</span>
                </div>
              </div>
              {/* corner brackets */}
              {['tl','tr','bl','br'].map(p => (
                <div key={p} style={{
                  position: 'absolute', width: 28, height: 28,
                  border: '2px solid var(--accent)',
                  ...(p.includes('t') ? { top: 14 } : { bottom: 14 }),
                  ...(p.includes('l') ? { left: 14, borderRight: 'none', borderBottom: 'none' } : { right: 14, borderLeft: 'none', borderBottom: 'none' }),
                  ...(p.includes('b') ? { borderTop: 'none' } : {}),
                  ...(p.includes('br') ? { borderTop: 'none', borderLeft: 'none', borderBottom: '2px solid var(--accent)', borderRight: '2px solid var(--accent)' } : {}),
                  ...(p.includes('bl') ? { borderTop: 'none', borderRight: 'none', borderBottom: '2px solid var(--accent)', borderLeft: '2px solid var(--accent)' } : {}),
                  borderRadius: 4,
                }} />
              ))}
              {/* scanning line */}
              <div style={{
                position: 'absolute', left: '8%', right: '8%', height: 2,
                background: 'linear-gradient(90deg, transparent, var(--accent), transparent)',
                boxShadow: '0 0 12px var(--accent)',
                top: '50%', animation: 'cardscan 2s ease-in-out infinite',
              }} />
              <style>{`@keyframes cardscan { 0% { top: 16%; } 100% { top: 84%; } }`}</style>
            </div>

            <div style={{ marginTop: 22, textAlign: 'center' }}>
              <h2 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600, letterSpacing: '-0.02em' }}>
                Scan your card
              </h2>
              <p style={{ margin: '6px 24px 0', fontFamily: 'Geist', fontSize: 13, color: 'var(--ink-2)', lineHeight: 1.5 }}>
                Point your camera at the front of your debit or credit card. We auto-fill the number and expiry.
              </p>
            </div>

            <div style={{ marginTop: 24, display: 'flex', flexDirection: 'column', gap: 10 }}>
              <Button variant="primary" size="lg" full icon="camera" onClick={() => setStage('form')}>Capture card</Button>
              <Button variant="ghost" size="md" full onClick={() => setStage('form')} style={{ color: 'var(--ink-2)' }}>Enter manually</Button>
            </div>

            <div style={{ marginTop: 24, display: 'flex', gap: 8, padding: 12, background: 'var(--bg-2)', borderRadius: 14 }}>
              <Icon name="lock" size={16} color="var(--ink-3)" />
              <div style={{ flex: 1, fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)', lineHeight: 1.4 }}>
                Card data is encrypted. We never store the CVV. Powered by BML Gateway.
              </div>
            </div>
          </div>
        )}

        {stage === 'form' && (
          <div>
            <div style={{
              padding: 14, borderRadius: 18, background: 'var(--ok-soft)',
              display: 'flex', alignItems: 'center', gap: 10, marginBottom: 18,
            }}>
              <div style={{ width: 28, height: 28, borderRadius: '50%', background: 'var(--ok)', display: 'grid', placeItems: 'center' }}>
                <Icon name="check" size={16} color="#fff" strokeWidth={3} />
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ fontFamily: 'Geist', fontSize: 12, fontWeight: 600, color: 'var(--ok)' }}>Card scanned</div>
                <div style={{ fontFamily: 'Geist Mono', fontSize: 11, color: 'var(--ok)' }}>•••• 7204 · expires 08/29</div>
              </div>
            </div>

            <Field label="Card number">
              <div style={{ ...inputStyle, display: 'flex', alignItems: 'center', gap: 8, padding: '0 14px' }}>
                <Icon name="card" size={16} color="var(--ink-3)" />
                <span style={{ fontFamily: 'Geist Mono', fontSize: 14, letterSpacing: '0.1em', flex: 1 }}>4532 0418 •••• 7204</span>
                <Chip tone="ok" style={{ padding: '2px 7px', fontSize: 10 }}>AUTO</Chip>
              </div>
            </Field>

            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12, marginTop: 14 }}>
              <Field label="Expiry">
                <div style={{ ...inputStyle, display: 'flex', alignItems: 'center', padding: '0 14px' }}>
                  <span style={{ fontFamily: 'Geist Mono', fontSize: 14, letterSpacing: '0.1em', flex: 1 }}>08/29</span>
                  <Chip tone="ok" style={{ padding: '2px 7px', fontSize: 10 }}>AUTO</Chip>
                </div>
              </Field>
              <Field label="CVV">
                <input value={cvv} onChange={e => setCvv(e.target.value.replace(/\D/g, '').slice(0, 4))}
                  placeholder="123" inputMode="numeric"
                  style={{ ...inputStyle, fontFamily: 'Geist Mono', letterSpacing: '0.2em' }} />
              </Field>
            </div>

            <div style={{ marginTop: 22 }}>
              <SectionTitle title="Order summary" style={{ padding: 0, margin: '0 0 10px' }} />
              <Card padding={0} style={{ overflow: 'hidden' }}>
                <SumRow l={`${selected.name} subscription`} v={mvr(selected.price) + '/mo'} />
                <SumRow l="Billed monthly" v="Cancel anytime" sub />
                <div style={{ height: 1, background: 'var(--line-2)' }} />
                <SumRow l="Today's total" v={mvr(selected.price)} big />
              </Card>
            </div>
          </div>
        )}
      </div>

      {stage === 'form' && (
        <div style={{ padding: '12px 16px 28px', borderTop: '1px solid var(--line-2)' }}>
          <Button variant="primary" size="lg" full onClick={() => setStage('done')}>
            Pay {mvr(selected.price)} · subscribe
          </Button>
          <div style={{ textAlign: 'center', marginTop: 8, fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>
            By continuing you agree to Gunzaru's terms
          </div>
        </div>
      )}

      {stage === 'done' && (
        <div style={{
          position: 'absolute', inset: 0, background: 'var(--bg)',
          display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
          padding: 32, textAlign: 'center',
        }}>
          <div style={{
            width: 96, height: 96, borderRadius: '50%',
            background: 'var(--ok)', display: 'grid', placeItems: 'center', marginBottom: 24,
            boxShadow: '0 0 40px oklch(58% 0.13 155 / 0.4)',
          }}>
            <Icon name="check" size={48} color="#fff" strokeWidth={3} />
          </div>
          <h2 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 30, fontWeight: 600, letterSpacing: '-0.03em' }}>
            Welcome to {selected.name}
          </h2>
          <p style={{ margin: '8px 24px 24px', fontFamily: 'Geist', fontSize: 14, color: 'var(--ink-2)', lineHeight: 1.5 }}>
            You can now create unlimited events and use event chat.
          </p>
          <Button variant="primary" size="lg" onClick={onDone}>Start organising</Button>
        </div>
      )}
    </div>
  );
};

const SumRow = ({ l, v, sub, big }) => (
  <div style={{
    display: 'flex', justifyContent: 'space-between', alignItems: 'center',
    padding: '13px 16px',
  }}>
    <span style={{ fontFamily: 'Geist', fontSize: big ? 14 : 13, color: sub ? 'var(--ink-3)' : 'var(--ink-2)' }}>{l}</span>
    <span style={{ fontFamily: big ? 'Bricolage Grotesque' : 'Geist', fontWeight: big ? 600 : 500, fontSize: big ? 17 : 13, color: 'var(--ink)' }}>{v}</span>
  </div>
);

// ═══════════════════════════════════════════════════════
// PACKAGES
// ═══════════════════════════════════════════════════════
const PackagesScreen = ({ onBack, onSelect }) => {
  const [sel, setSel] = React.useState('gold');
  return (
    <div style={{ height: '100%', background: 'var(--bg)', display: 'flex', flexDirection: 'column' }}>
      <div style={{ paddingTop: 56, paddingBottom: 8, paddingLeft: 12, paddingRight: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
        <button onClick={onBack} style={{ width: 38, height: 38, borderRadius: 12, border: 'none', background: 'var(--bg-2)', display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
          <Icon name="chevronL" size={18} />
        </button>
        <div style={{ flex: 1 }}>
          <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600, letterSpacing: '-0.025em' }}>
            Choose your plan
          </div>
        </div>
      </div>

      <div style={{ padding: '0 20px 8px' }}>
        <p style={{ margin: 0, fontFamily: 'Geist', fontSize: 13, color: 'var(--ink-2)', lineHeight: 1.5 }}>
          Casual joiners start free. Organisers unlock event creation on Gold.
        </p>
      </div>

      <div className="scroll" style={{ flex: 1, overflow: 'auto', padding: '12px 16px 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
        {PACKAGES.map(p => {
          const isSel = sel === p.id;
          return (
            <div key={p.id} onClick={() => setSel(p.id)} style={{
              padding: 18, borderRadius: 22, cursor: 'pointer',
              background: isSel ? 'var(--ink)' : 'var(--surface)',
              color: isSel ? 'var(--bg)' : 'var(--ink)',
              border: '1px solid ' + (isSel ? 'var(--ink)' : 'var(--line-2)'),
              boxShadow: isSel ? '0 12px 30px rgba(20,40,60,0.2)' : 'var(--shadow-sm)',
              position: 'relative',
              transition: 'all .2s',
            }}>
              {p.recommended && (
                <div style={{
                  position: 'absolute', top: -10, right: 16,
                  background: 'var(--accent)', color: '#fff',
                  fontFamily: 'Geist Mono', fontSize: 10, letterSpacing: '0.1em',
                  textTransform: 'uppercase', padding: '4px 10px', borderRadius: 99,
                  fontWeight: 500,
                }}>Most popular</div>
              )}
              <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between' }}>
                <div>
                  <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600, letterSpacing: '-0.025em' }}>{p.name}</div>
                  <div style={{ fontFamily: 'Geist', fontSize: 12, color: isSel ? 'rgba(255,255,255,0.7)' : 'var(--ink-3)', marginTop: 2 }}>{p.tagline}</div>
                </div>
                <div style={{ textAlign: 'right' }}>
                  <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 700, letterSpacing: '-0.03em', lineHeight: 1 }}>
                    {p.price === 0 ? 'Free' : mvr(p.price)}
                  </div>
                  {p.price > 0 && <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: isSel ? 'rgba(255,255,255,0.6)' : 'var(--ink-3)', letterSpacing: '0.06em' }}>PER MONTH</div>}
                </div>
              </div>
              <div style={{
                marginTop: 14, paddingTop: 14,
                borderTop: '1px solid ' + (isSel ? 'rgba(255,255,255,0.12)' : 'var(--line-2)'),
                display: 'flex', flexDirection: 'column', gap: 7,
              }}>
                {p.features.map((f, i) => (
                  <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 10, fontFamily: 'Geist', fontSize: 13 }}>
                    <div style={{
                      width: 18, height: 18, borderRadius: '50%',
                      background: f.ok ? (isSel ? 'rgba(255,255,255,0.15)' : 'var(--ok-soft)') : (isSel ? 'rgba(255,255,255,0.08)' : 'var(--bg-2)'),
                      display: 'grid', placeItems: 'center', flexShrink: 0,
                    }}>
                      <Icon name={f.ok ? 'check' : 'x'} size={11}
                        color={f.ok ? (isSel ? '#fff' : 'var(--ok)') : (isSel ? 'rgba(255,255,255,0.4)' : 'var(--ink-3)')}
                        strokeWidth={2.5} />
                    </div>
                    <span style={{ color: f.ok ? (isSel ? '#fff' : 'var(--ink)') : (isSel ? 'rgba(255,255,255,0.5)' : 'var(--ink-3)'), textDecoration: f.ok ? 'none' : 'line-through' }}>{f.t}</span>
                  </div>
                ))}
              </div>
            </div>
          );
        })}
      </div>

      <div style={{ padding: '12px 16px 28px', borderTop: '1px solid var(--line-2)' }}>
        <Button variant="primary" size="lg" full onClick={() => onSelect(sel)}>
          Continue with {PACKAGES.find(p => p.id === sel).name}
        </Button>
        <div style={{ textAlign: 'center', marginTop: 6, fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>
          Cancel any time · No commitment
        </div>
      </div>
    </div>
  );
};

// ═══════════════════════════════════════════════════════
// PROFILE
// ═══════════════════════════════════════════════════════
const ProfileScreen = ({ onTab, onCreate, onPackages }) => (
  <div style={{ height: '100%', background: 'var(--bg)', position: 'relative' }}>
    <div className="scroll" style={{ height: '100%', overflow: 'auto', paddingBottom: 120 }}>
      <div style={{ paddingTop: 60, padding: '60px 20px 16px' }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 18 }}>
          <h1 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 600, letterSpacing: '-0.025em' }}>Profile</h1>
          <button style={iconBtn}><Icon name="settings" size={20} /></button>
        </div>

        <Card padding={18}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
            <Avatar name="Mahfooz Hassan" size={66} />
            <div style={{ flex: 1 }}>
              <h2 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 19, fontWeight: 600, letterSpacing: '-0.02em' }}>Mahfooz Hassan</h2>
              <div style={{ fontFamily: 'Geist Mono', fontSize: 11, color: 'var(--ink-3)', marginTop: 2 }}>@mahfooz · Malé</div>
              <Chip tone="primary" icon="sparkle" style={{ marginTop: 6 }}>Gold member</Chip>
            </div>
          </div>
          <div style={{
            display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)',
            marginTop: 16, paddingTop: 14, borderTop: '1px solid var(--line-2)',
          }}>
            <Stat n="42" l="Joined" />
            <Stat n="8" l="Organised" />
            <Stat n="98%" l="Show-up" />
          </div>
        </Card>
      </div>

      {/* Plan card */}
      <div style={{ padding: '4px 20px 18px' }}>
        <Card padding={0} onClick={onPackages} style={{
          background: 'linear-gradient(135deg, oklch(38% 0.085 215), oklch(28% 0.06 230))',
          color: '#fff', overflow: 'hidden', position: 'relative',
        }}>
          <svg width="100%" height="100" style={{ position: 'absolute', top: 0, left: 0, opacity: 0.15 }}>
            <circle cx="80%" cy="20" r="80" fill="oklch(80% 0.13 75)"/>
          </svg>
          <div style={{ padding: 18, position: 'relative' }}>
            <div style={{ fontFamily: 'Geist Mono', fontSize: 10, letterSpacing: '0.14em', textTransform: 'uppercase', opacity: 0.7 }}>Your plan</div>
            <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 24, fontWeight: 600, letterSpacing: '-0.025em', marginTop: 4 }}>
              Gold · {mvr(89)}<span style={{ fontSize: 13, opacity: 0.7, fontWeight: 400 }}> / month</span>
            </div>
            <div style={{ fontFamily: 'Geist', fontSize: 12, opacity: 0.8, marginTop: 4 }}>Renews 14 Jun · Tap to manage</div>
          </div>
        </Card>
      </div>

      {/* Settings list */}
      <div style={{ padding: '0 20px' }}>
        <Card padding={0} style={{ overflow: 'hidden' }}>
          <SettingsRow icon="ticket" label="My events" right="12" />
          <SettingsRow icon="bell" label="Notifications" />
          <SettingsRow icon="eye" label="Privacy & visibility" />
          <SettingsRow icon="card" label="Payment methods" right="•••• 7204" />
          <SettingsRow icon="qm" label="Help & support" last />
        </Card>
      </div>

      {/* Achievements */}
      <div style={{ marginTop: 22 }}>
        <SectionTitle title="Streaks" />
        <div style={{ padding: '0 20px', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
          <Card style={{ padding: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
              <Icon name="flame" size={20} color="var(--accent)" />
              <span style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>Show-up</span>
            </div>
            <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 600, letterSpacing: '-0.025em', marginTop: 6 }}>14 <span style={{ fontSize: 13, color: 'var(--ink-3)', fontWeight: 400 }}>weeks</span></div>
          </Card>
          <Card style={{ padding: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
              <Icon name="star" size={20} color="var(--warn)" />
              <span style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>Host rating</span>
            </div>
            <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 600, letterSpacing: '-0.025em', marginTop: 6 }}>4.9 <span style={{ fontSize: 13, color: 'var(--ink-3)', fontWeight: 400 }}>· 23 reviews</span></div>
          </Card>
        </div>
      </div>
    </div>
    <TabBar active="profile" onChange={onTab} onCreate={onCreate} />
  </div>
);

const Stat = ({ n, l }) => (
  <div style={{ textAlign: 'center' }}>
    <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600, color: 'var(--ink)', letterSpacing: '-0.025em' }}>{n}</div>
    <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>{l}</div>
  </div>
);
const SettingsRow = ({ icon, label, right, last }) => (
  <div style={{
    display: 'flex', alignItems: 'center', gap: 12, padding: '14px 16px',
    borderBottom: last ? 'none' : '1px solid var(--line-2)', cursor: 'pointer',
  }}>
    <div style={{ width: 32, height: 32, borderRadius: 9, background: 'var(--bg-2)', display: 'grid', placeItems: 'center' }}>
      <Icon name={icon} size={16} color="var(--ink-2)" />
    </div>
    <div style={{ flex: 1, fontFamily: 'Geist', fontSize: 14, fontWeight: 500, color: 'var(--ink)' }}>{label}</div>
    {right && <span style={{ fontFamily: 'Geist Mono', fontSize: 11, color: 'var(--ink-3)' }}>{right}</span>}
    <Icon name="chevron" size={16} color="var(--ink-3)" />
  </div>
);

// ═══════════════════════════════════════════════════════
// MY EVENTS
// ═══════════════════════════════════════════════════════
const MyEventsScreen = ({ onTab, onCreate, onOpenEvent }) => {
  const [tab, setTab] = React.useState('going');
  const tabs = [
    { id: 'going', l: 'Going', n: 4 },
    { id: 'hosting', l: 'Hosting', n: 2 },
    { id: 'past', l: 'Past', n: 38 },
  ];
  return (
    <div style={{ height: '100%', background: 'var(--bg)', position: 'relative' }}>
      <div className="scroll" style={{ height: '100%', overflow: 'auto', paddingBottom: 120 }}>
        <div style={{ padding: '60px 20px 16px' }}>
          <h1 style={{ margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 26, fontWeight: 600, letterSpacing: '-0.025em' }}>My Events</h1>
        </div>
        <div style={{ padding: '0 20px 16px', display: 'flex', gap: 6 }}>
          {tabs.map(t => (
            <button key={t.id} onClick={() => setTab(t.id)} style={{
              flex: 1, padding: '10px 8px', borderRadius: 14, border: 'none', cursor: 'pointer',
              background: tab === t.id ? 'var(--ink)' : 'var(--surface)',
              color: tab === t.id ? 'var(--bg)' : 'var(--ink-2)',
              fontFamily: 'Geist', fontWeight: 500, fontSize: 13,
              border: '1px solid ' + (tab === t.id ? 'var(--ink)' : 'var(--line-2)'),
            }}>
              {t.l} <span style={{ opacity: 0.6, marginLeft: 4 }}>{t.n}</span>
            </button>
          ))}
        </div>

        {tab === 'going' && (
          <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
            {EVENTS.slice(0, 4).map(e => (
              <Card key={e.id} onClick={() => onOpenEvent(e.id)} padding={0} style={{ overflow: 'hidden' }}>
                <div style={{ display: 'flex' }}>
                  <Placeholder h={92} hue={e.img.hue} style={{ width: 92, borderRadius: 0 }} />
                  <div style={{ padding: 12, flex: 1, minWidth: 0 }}>
                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                      <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>{e.date} · {e.time}</div>
                      <Chip tone="ok" style={{ padding: '2px 7px', fontSize: 10 }}>Going</Chip>
                    </div>
                    <h3 style={{ margin: '6px 0 0', fontFamily: 'Bricolage Grotesque', fontSize: 15, fontWeight: 600, letterSpacing: '-0.02em', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{e.title}</h3>
                    <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)', marginTop: 4, display: 'flex', alignItems: 'center', gap: 4 }}>
                      <Icon name="pin" size={10} /> {e.where}
                    </div>
                  </div>
                </div>
              </Card>
            ))}
          </div>
        )}
        {tab === 'hosting' && (
          <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
            {EVENTS.slice(0, 2).map(e => (
              <Card key={e.id} onClick={() => onOpenEvent(e.id)} padding={14}>
                <div style={{ display: 'flex', justifyContent: 'space-between' }}>
                  <Chip tone="accent" icon="sparkle">You're hosting</Chip>
                  <span style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)' }}>{e.date}</span>
                </div>
                <h3 style={{ margin: '8px 0 6px', fontFamily: 'Bricolage Grotesque', fontSize: 17, fontWeight: 600, letterSpacing: '-0.02em' }}>{e.title}</h3>
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 10 }}>
                  <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                    <AvatarStack names={e.going_list} size={22} max={4} />
                    <span style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-2)', fontWeight: 500 }}>{e.going} going · {e.maybe} maybe</span>
                  </div>
                  <div style={{ display: 'flex', gap: 6 }}>
                    <Button size="sm" variant="secondary">Manage</Button>
                  </div>
                </div>
              </Card>
            ))}
          </div>
        )}
        {tab === 'past' && (
          <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 8 }}>
            {['Coffee & Code · 12 May', 'Saturday Football · 9 May', 'Marina Walk · 5 May', 'Book Club · 2 May'].map((t,i) => (
              <Card key={i} padding={12} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                <div>
                  <div style={{ fontFamily: 'Geist', fontSize: 13, fontWeight: 600 }}>{t.split('·')[0]}</div>
                  <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)' }}>{t.split('·')[1]}</div>
                </div>
                <Chip tone="ok" icon="check" style={{ padding: '3px 8px', fontSize: 10 }}>Attended</Chip>
              </Card>
            ))}
          </div>
        )}
      </div>
      <TabBar active="my" onChange={onTab} onCreate={onCreate} />
    </div>
  );
};

Object.assign(window, {
  ChatScreen, CreateEventScreen, CheckinScreen,
  PaymentScreen, PackagesScreen, ProfileScreen, MyEventsScreen,
});
