// ─── Gunzaru: core screens (Discovery, Event Detail, Chat, Profile, Onboarding) ───

// Bottom tab bar
const TabBar = ({ active, onChange, onCreate }) => {
  const tabs = [
    { id: 'home', icon: 'home', label: 'Discover' },
    { id: 'my', icon: 'ticket', label: 'My Events' },
    { id: 'create', icon: 'plus', label: '', center: true },
    { id: 'chat', icon: 'chat', label: 'Chat' },
    { id: 'profile', icon: 'user', label: 'Profile' },
  ];
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 50,
      padding: '8px 16px 30px',
      background: 'linear-gradient(180deg, rgba(255,255,255,0) 0%, var(--bg) 28%)',
      pointerEvents: 'none',
    }}>
      <div style={{
        background: 'var(--surface)', borderRadius: 28,
        boxShadow: 'var(--shadow-md)', border: '1px solid var(--line-2)',
        display: 'flex', alignItems: 'center', justifyContent: 'space-around',
        padding: '8px 8px', pointerEvents: 'auto',
      }}>
        {tabs.map(t => {
          if (t.center) {
            return (
              <button key={t.id} onClick={onCreate} style={{
                width: 52, height: 52, borderRadius: '50%',
                background: 'var(--ink)', color: 'var(--bg)',
                display: 'grid', placeItems: 'center', border: 'none',
                cursor: 'pointer', marginTop: -22,
                boxShadow: '0 8px 24px rgba(20,40,60,0.25), inset 0 -2px 0 rgba(0,0,0,0.2)',
              }}>
                <Icon name="plus" size={26} color="var(--bg)" strokeWidth={2.4} />
              </button>
            );
          }
          const isActive = active === t.id;
          return (
            <button key={t.id} onClick={() => onChange(t.id)} style={{
              flex: 1, padding: '8px 4px', background: 'none', border: 'none',
              display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
              cursor: 'pointer', color: isActive ? 'var(--primary)' : 'var(--ink-3)',
            }}>
              <Icon name={t.icon} size={22} strokeWidth={isActive ? 2.1 : 1.7} />
              <span style={{ fontFamily: 'Geist', fontSize: 10.5, fontWeight: isActive ? 600 : 500, letterSpacing: '-0.005em' }}>{t.label}</span>
            </button>
          );
        })}
      </div>
    </div>
  );
};

// ═══════════════════════════════════════════════════════
// 1. ONBOARDING
// ═══════════════════════════════════════════════════════
const OnboardingScreen = ({ onDone }) => {
  return (
    <div style={{
      height: '100%', background: 'var(--bg)', position: 'relative',
      display: 'flex', flexDirection: 'column',
      paddingTop: 60,
    }}>
      {/* visual hero */}
      <div style={{ flex: 1, position: 'relative', overflow: 'hidden', margin: '20px 16px 0' }}>
        <div style={{
          position: 'absolute', inset: 0, borderRadius: 28,
          background: 'linear-gradient(160deg, oklch(58% 0.12 215), oklch(38% 0.08 230))',
          overflow: 'hidden',
        }}>
          {/* wave decoration */}
          <svg viewBox="0 0 400 600" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
            <defs>
              <radialGradient id="sun" cx="78%" cy="22%" r="35%">
                <stop offset="0%" stopColor="oklch(85% 0.16 55)" stopOpacity="0.9"/>
                <stop offset="100%" stopColor="oklch(85% 0.16 55)" stopOpacity="0"/>
              </radialGradient>
            </defs>
            <rect width="400" height="600" fill="url(#sun)" />
            <path d="M0 360 Q 100 340, 200 360 T 400 360 V 600 H 0 Z" fill="rgba(255,255,255,0.08)" />
            <path d="M0 410 Q 100 390, 200 410 T 400 410 V 600 H 0 Z" fill="rgba(255,255,255,0.06)" />
            <path d="M0 470 Q 100 450, 200 470 T 400 470 V 600 H 0 Z" fill="rgba(255,255,255,0.08)" />
            {/* floating event cards */}
            <g transform="translate(40 120) rotate(-6)" opacity="0.95">
              <rect width="200" height="60" rx="14" fill="rgba(255,255,255,0.95)" />
              <circle cx="22" cy="30" r="14" fill="oklch(72% 0.16 38)" />
              <rect x="46" y="18" width="90" height="8" rx="3" fill="oklch(30% 0.02 235)" />
              <rect x="46" y="32" width="60" height="6" rx="3" fill="oklch(60% 0.02 235)" />
              <rect x="160" y="22" width="28" height="16" rx="8" fill="oklch(94% 0.04 155)" />
            </g>
            <g transform="translate(140 200) rotate(4)" opacity="0.95">
              <rect width="200" height="60" rx="14" fill="rgba(255,255,255,0.95)" />
              <circle cx="22" cy="30" r="14" fill="oklch(60% 0.13 215)" />
              <rect x="46" y="18" width="110" height="8" rx="3" fill="oklch(30% 0.02 235)" />
              <rect x="46" y="32" width="50" height="6" rx="3" fill="oklch(60% 0.02 235)" />
              <rect x="160" y="22" width="28" height="16" rx="8" fill="oklch(94% 0.04 155)" />
            </g>
            <g transform="translate(60 280) rotate(-3)" opacity="0.95">
              <rect width="200" height="60" rx="14" fill="rgba(255,255,255,0.95)" />
              <circle cx="22" cy="30" r="14" fill="oklch(72% 0.13 75)" />
              <rect x="46" y="18" width="80" height="8" rx="3" fill="oklch(30% 0.02 235)" />
              <rect x="46" y="32" width="70" height="6" rx="3" fill="oklch(60% 0.02 235)" />
              <rect x="160" y="22" width="28" height="16" rx="8" fill="oklch(94% 0.04 155)" />
            </g>
          </svg>
        </div>
      </div>

      {/* copy */}
      <div style={{ padding: '28px 28px 24px' }}>
        <div style={{
          fontFamily: 'Geist Mono', fontSize: 11,
          color: 'var(--ink-3)', letterSpacing: '0.16em',
          textTransform: 'uppercase', marginBottom: 14,
        }}>Gunzaru · ގުންޒަރު</div>
        <h1 style={{
          margin: 0, fontFamily: 'Bricolage Grotesque',
          fontSize: 34, lineHeight: 1.02, fontWeight: 600,
          letterSpacing: '-0.035em', color: 'var(--ink)',
          textWrap: 'pretty',
        }}>
          Real plans.<br/>
          <span style={{ fontFamily: 'Instrument Serif', fontStyle: 'italic', fontWeight: 400 }}>Real</span> attendance.
        </h1>
        <p style={{
          margin: '14px 0 0', fontFamily: 'Geist', fontSize: 15,
          color: 'var(--ink-2)', lineHeight: 1.5, maxWidth: 320,
        }}>
          Stop guessing who's actually showing up. Discover, RSVP, and run events around Malé, Hulhumalé and Addu.
        </p>
      </div>
      <div style={{ padding: '0 20px 40px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        <Button variant="primary" size="lg" full onClick={onDone}>Get started</Button>
        <Button variant="ghost" size="md" full onClick={onDone} style={{ color: 'var(--ink-2)' }}>I already have an account</Button>
      </div>
    </div>
  );
};

// ═══════════════════════════════════════════════════════
// 2. DISCOVERY / HOME
// ═══════════════════════════════════════════════════════
const DiscoveryScreen = ({ onOpenEvent, onTab, onCreate }) => {
  const [filter, setFilter] = React.useState('All');
  const filters = ['All', 'Today', 'Sports', 'Outdoors', 'Study', 'Community'];
  const featured = EVENTS[0];
  const rest = EVENTS.slice(1);

  return (
    <div style={{ height: '100%', background: 'var(--bg)', position: 'relative' }}>
      <div className="scroll" style={{ height: '100%', overflow: 'auto', paddingBottom: 120 }}>

        {/* Header */}
        <div style={{ padding: '60px 20px 14px' }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <div>
              <div style={{ fontFamily: 'Geist Mono', fontSize: 10.5, color: 'var(--ink-3)', letterSpacing: '0.14em', textTransform: 'uppercase' }}>
                <Icon name="pin" size={11} style={{ verticalAlign: -1, marginRight: 4 }} /> Malé · Henveiru
              </div>
              <h1 style={{
                margin: '6px 0 0', fontFamily: 'Bricolage Grotesque',
                fontSize: 30, fontWeight: 600, letterSpacing: '-0.03em',
                color: 'var(--ink)',
              }}>Hi, Mahfooz<span style={{ fontFamily: 'Instrument Serif', fontStyle: 'italic', fontWeight: 400, marginLeft: 4 }}>.</span></h1>
            </div>
            <div style={{ display: 'flex', gap: 8 }}>
              <button style={iconBtn}><Icon name="search" size={20} /></button>
              <button style={iconBtn}>
                <Icon name="bell" size={20} />
                <span style={{
                  position: 'absolute', top: 8, right: 8, width: 7, height: 7,
                  background: 'var(--accent)', borderRadius: '50%',
                  border: '2px solid var(--surface)',
                }}/>
              </button>
            </div>
          </div>
        </div>

        {/* Stats strip */}
        <div style={{ padding: '4px 20px 18px' }}>
          <Card padding={0} style={{ overflow: 'hidden' }}>
            <div style={{ display: 'flex' }}>
              <div style={statCell}>
                <div style={statN}>3</div>
                <div style={statL}>Joined this week</div>
              </div>
              <div style={{ width: 1, background: 'var(--line)' }} />
              <div style={statCell}>
                <div style={statN}>12</div>
                <div style={statL}>Near you today</div>
              </div>
              <div style={{ width: 1, background: 'var(--line)' }} />
              <div style={statCell}>
                <div style={{ ...statN, color: 'var(--accent)' }}>+47</div>
                <div style={statL}>This weekend</div>
              </div>
            </div>
          </Card>
        </div>

        {/* Filter chips */}
        <div className="scroll" style={{ display: 'flex', gap: 8, padding: '0 20px 18px', overflowX: 'auto' }}>
          {filters.map(f => (
            <button key={f} onClick={() => setFilter(f)} style={{
              flexShrink: 0, padding: '8px 14px', borderRadius: 999,
              fontFamily: 'Geist', fontSize: 13, fontWeight: 500,
              border: '1px solid ' + (filter === f ? 'var(--ink)' : 'var(--line)'),
              background: filter === f ? 'var(--ink)' : 'var(--surface)',
              color: filter === f ? 'var(--bg)' : 'var(--ink-2)',
              cursor: 'pointer',
            }}>{f}</button>
          ))}
        </div>

        {/* Featured */}
        <SectionTitle title="Tonight near you" action="Map" />
        <div style={{ padding: '0 20px 18px' }}>
          <FeaturedEventCard event={featured} onOpen={() => onOpenEvent(featured.id)} />
        </div>

        {/* List */}
        <SectionTitle title="This week" action="See all" />
        <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
          {rest.map(e => (
            <EventListCard key={e.id} event={e} onOpen={() => onOpenEvent(e.id)} />
          ))}
        </div>

        {/* Categories grid */}
        <SectionTitle title="Browse" style={{ marginTop: 22 }} />
        <div style={{ padding: '0 20px', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
          {[
            { l: 'Sports', i: 'flame', h: 35 },
            { l: 'Study', i: 'sparkle', h: 250 },
            { l: 'Outdoors', i: 'waves', h: 215 },
            { l: 'Community', i: 'users', h: 155 },
          ].map(c => (
            <button key={c.l} style={{
              padding: '20px 16px', borderRadius: 22,
              background: `linear-gradient(135deg, oklch(95% 0.025 ${c.h}), oklch(90% 0.04 ${c.h}))`,
              border: '1px solid var(--line-2)', cursor: 'pointer',
              display: 'flex', flexDirection: 'column', gap: 8, alignItems: 'flex-start',
            }}>
              <Icon name={c.i} size={22} color={`oklch(45% 0.1 ${c.h})`} />
              <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600, color: 'var(--ink)' }}>{c.l}</div>
            </button>
          ))}
        </div>
      </div>

      <TabBar active="home" onChange={onTab} onCreate={onCreate} />
    </div>
  );
};

const iconBtn = {
  width: 42, height: 42, borderRadius: 14,
  background: 'var(--surface)', border: '1px solid var(--line-2)',
  display: 'grid', placeItems: 'center', cursor: 'pointer',
  color: 'var(--ink)', position: 'relative', boxShadow: 'var(--shadow-sm)',
};
const statCell = { flex: 1, padding: '12px 8px', textAlign: 'center' };
const statN = { fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600, color: 'var(--ink)', letterSpacing: '-0.02em' };
const statL = { fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)', marginTop: 2 };

// Featured card (tall)
const FeaturedEventCard = ({ event, onOpen }) => (
  <div onClick={onOpen} style={{
    borderRadius: 24, overflow: 'hidden', cursor: 'pointer',
    background: 'var(--surface)', boxShadow: 'var(--shadow-md)',
    border: '1px solid var(--line-2)',
  }}>
    <div style={{ position: 'relative' }}>
      <Placeholder h={180} label={event.img.label} hue={event.img.hue} style={{ borderRadius: 0 }} />
      <div style={{ position: 'absolute', top: 12, left: 12, display: 'flex', gap: 6 }}>
        <Chip tone="ink" icon="flame">Tonight</Chip>
        <Chip tone="ink">{event.tags[0]}</Chip>
      </div>
      <div style={{
        position: 'absolute', top: 12, right: 12,
        background: 'rgba(255,255,255,0.94)', backdropFilter: 'blur(8px)',
        padding: '8px 12px', borderRadius: 14, textAlign: 'center',
      }}>
        <div style={{ fontFamily: 'Geist Mono', fontSize: 9, color: 'var(--ink-3)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>FRI</div>
        <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 18, fontWeight: 700, color: 'var(--ink)', lineHeight: 1 }}>22</div>
      </div>
    </div>
    <div style={{ padding: '16px 18px 18px' }}>
      <h3 style={{
        margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 22, fontWeight: 600,
        letterSpacing: '-0.025em', color: 'var(--ink)',
      }}>{event.title}</h3>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 8, color: 'var(--ink-2)', fontSize: 13 }}>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}>
          <Icon name="clock" size={14} /> {event.time}
        </span>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}>
          <Icon name="pin" size={14} /> {event.where}
        </span>
      </div>
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        marginTop: 14, paddingTop: 14, borderTop: '1px solid var(--line-2)',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <AvatarStack names={event.going_list} size={26} max={4} />
          <div>
            <div style={{ fontFamily: 'Geist', fontSize: 13, fontWeight: 600, color: 'var(--ink)' }}>
              {event.going} going
            </div>
            <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>
              {event.maybe} maybe · {event.cap - event.going} spots left
            </div>
          </div>
        </div>
        <Button size="sm" variant="primary">Join</Button>
      </div>
    </div>
  </div>
);

// List card
const EventListCard = ({ event, onOpen }) => (
  <div onClick={onOpen} style={{
    background: 'var(--surface)', borderRadius: 'var(--radius-card)',
    border: '1px solid var(--line-2)', cursor: 'pointer',
    display: 'flex', gap: 12, padding: 12, boxShadow: 'var(--shadow-sm)',
  }}>
    <div style={{ width: 84, flexShrink: 0, position: 'relative' }}>
      <Placeholder h={108} label="" hue={event.img.hue} />
      <div style={{
        position: 'absolute', top: 6, left: 6,
        background: 'rgba(255,255,255,0.94)', padding: '4px 8px',
        borderRadius: 8, textAlign: 'center', backdropFilter: 'blur(4px)',
      }}>
        <div style={{ fontFamily: 'Geist Mono', fontSize: 8, color: 'var(--ink-3)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>{event.date.split(',')[0]}</div>
        <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 14, fontWeight: 700, color: 'var(--ink)', lineHeight: 1 }}>{event.date.match(/\d+/)?.[0]}</div>
      </div>
    </div>
    <div style={{ flex: 1, minWidth: 0, paddingRight: 4, paddingTop: 2 }}>
      <div style={{ display: 'flex', gap: 6, marginBottom: 6 }}>
        <Chip tone={event.price ? 'accent' : 'ok'} style={{ padding: '3px 8px', fontSize: 10 }}>
          {event.price ? mvr(event.price) : 'Free'}
        </Chip>
        <Chip style={{ padding: '3px 8px', fontSize: 10 }}>{event.category}</Chip>
      </div>
      <h3 style={{
        margin: 0, fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600,
        letterSpacing: '-0.02em', color: 'var(--ink)',
        overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
      }}>{event.title}</h3>
      <div style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-2)', marginTop: 4, display: 'flex', alignItems: 'center', gap: 6 }}>
        <Icon name="pin" size={11} />
        <span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{event.where}</span>
        <span style={{ color: 'var(--ink-3)' }}>· {event.distance}</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 8 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
          <AvatarStack names={event.going_list} size={20} max={3} />
          <span style={{ fontFamily: 'Geist', fontSize: 11, fontWeight: 500, color: 'var(--ink-2)' }}>
            {event.going} going
          </span>
        </div>
        <span style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)' }}>{event.time}</span>
      </div>
    </div>
  </div>
);

// ═══════════════════════════════════════════════════════
// 3. EVENT DETAIL
// ═══════════════════════════════════════════════════════
const EventDetailScreen = ({ eventId, onBack, onChat, onCheckin }) => {
  const event = EVENTS.find(e => e.id === eventId) || EVENTS[0];
  const [rsvp, setRsvp] = React.useState(null);
  const [hidden, setHidden] = React.useState(false);

  return (
    <div style={{ height: '100%', background: 'var(--bg)', position: 'relative', display: 'flex', flexDirection: 'column' }}>
      <div className="scroll" style={{ flex: 1, overflow: 'auto', paddingBottom: 140 }}>
        {/* Hero */}
        <div style={{ position: 'relative' }}>
          <Placeholder h={300} label={event.img.label} hue={event.img.hue} style={{ borderRadius: 0 }} />
          <div style={{
            position: 'absolute', inset: 0,
            background: 'linear-gradient(180deg, rgba(0,0,0,0.3) 0%, transparent 30%, transparent 60%, rgba(0,0,0,0.5) 100%)',
          }} />
          {/* Status bar overlay controls */}
          <div style={{
            position: 'absolute', top: 56, left: 16, right: 16,
            display: 'flex', justifyContent: 'space-between',
          }}>
            <button onClick={onBack} style={glassBtn}>
              <Icon name="chevronL" size={20} color="#fff" />
            </button>
            <div style={{ display: 'flex', gap: 8 }}>
              <button style={glassBtn}><Icon name="heart" size={18} color="#fff"/></button>
              <button style={glassBtn}><Icon name="share" size={18} color="#fff"/></button>
            </div>
          </div>
          {/* Title overlay */}
          <div style={{ position: 'absolute', bottom: 18, left: 20, right: 20, color: '#fff' }}>
            <div style={{ display: 'flex', gap: 6, marginBottom: 10 }}>
              {event.tags.map(t => (
                <span key={t} style={{
                  fontSize: 11, padding: '4px 10px', borderRadius: 99,
                  background: 'rgba(255,255,255,0.2)', backdropFilter: 'blur(8px)',
                  fontFamily: 'Geist', fontWeight: 500,
                }}>{t}</span>
              ))}
            </div>
            <h1 style={{
              margin: 0, fontFamily: 'Bricolage Grotesque',
              fontSize: 28, fontWeight: 600, letterSpacing: '-0.03em',
              textWrap: 'balance', lineHeight: 1.05,
            }}>{event.title}</h1>
          </div>
        </div>

        {/* Meta grid */}
        <div style={{ padding: '18px 20px 0' }}>
          <Card padding={0} style={{ overflow: 'hidden' }}>
            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr' }}>
              <MetaCell icon="calendar" l="DATE" v={event.date} sub={event.time} />
              <MetaCell icon="pin" l="WHERE" v={event.where} sub={event.distance + ' away'} divider />
              <MetaCell icon="users" l="GOING" v={`${event.going}${event.cap ? ' / ' + event.cap : ''}`} sub={`${event.maybe} maybe`} top />
              <MetaCell icon={event.price ? 'card' : 'ticket'} l="PRICE" v={event.price ? mvr(event.price) : 'Free'} sub={event.price ? 'pay on join' : 'no payment'} top divider />
            </div>
          </Card>
        </div>

        {/* Organiser */}
        <div style={{ padding: '14px 20px 0' }}>
          <Card>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <Avatar name={event.org} size={44} />
              <div style={{ flex: 1 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                  <div style={{ fontFamily: 'Geist', fontSize: 14, fontWeight: 600, color: 'var(--ink)' }}>{event.org}</div>
                  {event.orgVerified && (
                    <div style={{ width: 16, height: 16, borderRadius: '50%', background: 'var(--primary)', display: 'grid', placeItems: 'center' }}>
                      <Icon name="check" size={11} color="#fff" strokeWidth={3} />
                    </div>
                  )}
                </div>
                <div style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-3)' }}>Organiser · 24 past events</div>
              </div>
              <Button size="sm" variant="secondary">View</Button>
            </div>
          </Card>
        </div>

        {/* About */}
        <div style={{ padding: '14px 20px 0' }}>
          <SectionTitle title="About" style={{ padding: 0, margin: '4px 0 8px' }} />
          <p style={{ margin: 0, fontFamily: 'Geist', fontSize: 14, color: 'var(--ink-2)', lineHeight: 1.55 }}>
            {event.about}
          </p>
        </div>

        {/* Attendees */}
        <div style={{ padding: '20px 20px 0' }}>
          <SectionTitle title={`Going · ${event.going}`} action="See all" style={{ padding: 0, margin: '4px 0 12px' }} />
          <div style={{ display: 'flex', gap: 12, overflowX: 'auto', paddingBottom: 4 }} className="scroll">
            {event.going_list.map((n, i) => (
              <div key={i} style={{ textAlign: 'center', flexShrink: 0 }}>
                <Avatar name={n} size={48} />
                <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-2)', marginTop: 4 }}>{n.split(' ')[0]}</div>
              </div>
            ))}
            <div style={{ textAlign: 'center', flexShrink: 0 }}>
              <div style={{
                width: 48, height: 48, borderRadius: '50%',
                background: 'var(--bg-2)', display: 'grid', placeItems: 'center',
                color: 'var(--ink-2)', fontFamily: 'Geist', fontSize: 12, fontWeight: 600,
                border: '1px dashed var(--line)',
              }}>+{event.going - event.going_list.length}</div>
              <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)', marginTop: 4 }}>more</div>
            </div>
          </div>
        </div>

        {/* Privacy toggle */}
        <div style={{ padding: '14px 20px 0' }}>
          <Card style={{ display: 'flex', alignItems: 'center', gap: 12, padding: 14 }}>
            <div style={{
              width: 36, height: 36, borderRadius: 10,
              background: 'var(--primary-soft)', display: 'grid', placeItems: 'center',
            }}>
              <Icon name={hidden ? 'eyeOff' : 'eye'} size={18} color="var(--primary)" />
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ fontFamily: 'Geist', fontSize: 13, fontWeight: 600, color: 'var(--ink)' }}>
                Hide my name publicly
              </div>
              <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>
                Organiser still sees you. Counts toward total.
              </div>
            </div>
            <Toggle on={hidden} onClick={() => setHidden(!hidden)} />
          </Card>
        </div>

        {/* Chat preview if RSVP'd */}
        {rsvp === 'going' && (
          <div style={{ padding: '14px 20px 0' }}>
            <Card onClick={onChat} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <div style={{
                width: 36, height: 36, borderRadius: 10,
                background: 'var(--accent-soft)', display: 'grid', placeItems: 'center',
              }}>
                <Icon name="chat" size={18} color="var(--accent)" />
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ fontFamily: 'Geist', fontSize: 13, fontWeight: 600 }}>Event chat is open</div>
                <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>3 new messages</div>
              </div>
              <Icon name="chevron" size={18} color="var(--ink-3)" />
            </Card>
          </div>
        )}
      </div>

      {/* RSVP dock */}
      <div style={{
        position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 50,
        padding: '14px 16px 28px',
        background: 'linear-gradient(180deg, rgba(255,255,255,0) 0%, var(--bg) 30%)',
      }}>
        <Card padding={10} style={{ borderRadius: 22 }}>
          {rsvp === null ? (
            <div>
              <div style={{ fontFamily: 'Geist Mono', fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.1em', textTransform: 'uppercase', textAlign: 'center', marginBottom: 8 }}>
                Will you be there?
              </div>
              <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 6 }}>
                <RsvpBtn icon="check" label="Going" tone="ok" onClick={() => setRsvp('going')} />
                <RsvpBtn icon="qm" label="Maybe" tone="warn" onClick={() => setRsvp('maybe')} />
                <RsvpBtn icon="x" label="Can't go" tone="neutral" onClick={() => setRsvp('no')} />
              </div>
            </div>
          ) : rsvp === 'going' ? (
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '6px 8px' }}>
              <div style={{ width: 38, height: 38, borderRadius: 10, background: 'var(--ok-soft)', display: 'grid', placeItems: 'center' }}>
                <Icon name="check" size={20} color="var(--ok)" strokeWidth={2.4} />
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600, color: 'var(--ink)' }}>You're going</div>
                <div style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-3)' }}>We'll remind you 1h before.</div>
              </div>
              <Button size="sm" variant="dark" onClick={onCheckin}>Check in</Button>
            </div>
          ) : (
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '6px 8px' }}>
              <div style={{ flex: 1 }}>
                <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600 }}>
                  {rsvp === 'maybe' ? 'Marked as maybe' : "Marked can't go"}
                </div>
                <div style={{ fontFamily: 'Geist', fontSize: 12, color: 'var(--ink-3)' }}>You can change this any time.</div>
              </div>
              <Button size="sm" variant="secondary" onClick={() => setRsvp(null)}>Change</Button>
            </div>
          )}
        </Card>
      </div>
    </div>
  );
};

const glassBtn = {
  width: 38, height: 38, borderRadius: 12,
  background: 'rgba(255,255,255,0.18)', backdropFilter: 'blur(10px)',
  border: '1px solid rgba(255,255,255,0.3)',
  display: 'grid', placeItems: 'center', cursor: 'pointer',
};

const MetaCell = ({ icon, l, v, sub, divider, top }) => (
  <div style={{
    padding: '14px 14px',
    borderLeft: divider ? '1px solid var(--line)' : 'none',
    borderTop: top ? '1px solid var(--line)' : 'none',
  }}>
    <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
      <Icon name={icon} size={14} color="var(--ink-3)" />
      <span style={{ fontFamily: 'Geist Mono', fontSize: 9, letterSpacing: '0.1em', color: 'var(--ink-3)', textTransform: 'uppercase' }}>{l}</span>
    </div>
    <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 16, fontWeight: 600, color: 'var(--ink)', marginTop: 4, letterSpacing: '-0.015em' }}>{v}</div>
    {sub && <div style={{ fontFamily: 'Geist', fontSize: 11, color: 'var(--ink-3)' }}>{sub}</div>}
  </div>
);

const RsvpBtn = ({ icon, label, tone, onClick }) => {
  const tones = {
    ok: { bg: 'var(--ok-soft)', c: 'var(--ok)' },
    warn: { bg: 'var(--warn-soft)', c: 'var(--warn)' },
    neutral: { bg: 'var(--bg-2)', c: 'var(--ink-2)' },
  };
  const t = tones[tone];
  return (
    <button onClick={onClick} style={{
      background: t.bg, border: 'none', borderRadius: 16,
      padding: '12px 6px', cursor: 'pointer',
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
    }}>
      <Icon name={icon} size={22} color={t.c} strokeWidth={2.2} />
      <span style={{ fontFamily: 'Geist', fontSize: 12, fontWeight: 600, color: t.c }}>{label}</span>
    </button>
  );
};

const Toggle = ({ on, onClick }) => (
  <button onClick={onClick} style={{
    width: 48, height: 28, borderRadius: 99, padding: 2, border: 'none',
    background: on ? 'var(--primary)' : 'var(--line)', cursor: 'pointer',
    display: 'flex', alignItems: 'center',
    transition: 'background .2s',
  }}>
    <div style={{
      width: 24, height: 24, borderRadius: '50%', background: '#fff',
      transform: on ? 'translateX(20px)' : 'translateX(0)',
      transition: 'transform .2s', boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
    }} />
  </button>
);

Object.assign(window, {
  TabBar, OnboardingScreen, DiscoveryScreen, EventDetailScreen,
  FeaturedEventCard, EventListCard, Toggle, RsvpBtn, MetaCell, glassBtn, iconBtn,
});
