// Booking Detail — slide-over drawer shown from Bookings list + Sales tab

const BookingDrawer = ({ open, onClose, booking }) => {
  const [tab, setTab] = React.useState('details');
  const [refundOpen, setRefundOpen] = React.useState(false);
  const [transferOpen, setTransferOpen] = React.useState(false);
  const [noteDraft, setNoteDraft] = React.useState('');

  // Default mock booking, overlaid with whatever summary data was passed in from the list row
  const defaults = {
    id: 'TX-24891',
    buyer: 'Marie Schäfer',
    email: 'marie.schaefer@posteo.de',
    phone: '+49 151 23456789',
    event: 'Apparat — LP5 Live',
    eventDate: 'Sa, 24. Mai · 22:00',
    venue: 'Berghain, Berlin',
    bookedAt: '17. Apr 2026 · 09:42',
    channel: 'Web · tixwerk.de',
    tickets: [
      { id: 'TKT-48920', tier: 'GA',  seat: 'Block A1 · Stehplatz', attendee: 'Marie Schäfer', status: 'valid', price: 45 },
      { id: 'TKT-48921', tier: 'GA',  seat: 'Block A1 · Stehplatz', attendee: 'Leon Wagner', status: 'valid', price: 45 },
    ],
    subtotal: 340,
    fees: 13.60,
    vat: 67.14,
    total: 420.74,
    paymentMethod: 'SEPA · Commerzbank',
    txnId: 'sepa_A1B2C3D4E5F6',
    invoice: 'RE-24891-2026',
    status: 'paid',
    checkedIn: false,
    note: '',
    activity: [
      { t: '17. Apr, 09:42', txt: 'Booking confirmed · ' + euro(420.74) + ' received', ic: 'check', tone: 'success' },
      { t: '17. Apr, 09:42', txt: 'Tickets emailed to marie.schaefer@posteo.de', ic: 'mail', tone: 'neutral' },
      { t: '17. Apr, 09:42', txt: 'SMS delivered to +49 151 23456789', ic: 'phone', tone: 'neutral' },
      { t: '17. Apr, 09:38', txt: 'Payment initiated · Stripe (SEPA)', ic: 'cart', tone: 'neutral' },
      { t: '17. Apr, 09:36', txt: 'Checkout started · IP 91.64.xx.xx · Berlin', ic: 'eye', tone: 'neutral' },
    ],
  };
  const b = { ...defaults, ...(booking || {}) };
  // Ensure nested arrays exist even if caller overrode id/buyer/etc
  if (!b.tickets) b.tickets = defaults.tickets;
  if (!b.activity) b.activity = defaults.activity;

  return (
    <>
      <Drawer open={open} onClose={onClose} width={560}
        title={null}
        footer={null}
      >
        {/* Custom header */}
        <div style={{ margin: '-20px -24px 0', padding: '20px 22px 18px', borderBottom: '1px solid var(--line)' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 10, flexWrap: 'wrap' }}>
            <span style={{ fontFamily: 'var(--font-mono)', fontSize: 12, color: 'var(--ink-3)', background: 'var(--bg-sunken)', padding: '3px 8px', borderRadius: 6, whiteSpace: 'nowrap' }}>{b.id}</span>
            <StatusChip status={b.status}/>
            {b.checkedIn ? <StatusChip status="checked-in"/> : null}
            <div style={{ flex: 1 }}/>
            <button onClick={onClose} style={{ background: 'transparent', border: 0, width: 28, height: 28, borderRadius: 7, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--ink-3)' }}>
              <Icon name="close" size={14}/>
            </button>
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
            <Avatar name={b.buyer} size={44}/>
            <div style={{ minWidth: 0, flex: 1 }}>
              <div style={{ fontSize: 17, fontWeight: 600 }}>{b.buyer}</div>
              <div style={{ fontSize: 12.5, color: 'var(--ink-3)', display: 'flex', gap: 12, marginTop: 2, flexWrap: 'wrap' }}>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, whiteSpace: 'nowrap' }}><Icon name="mail" size={11}/>{b.email}</span>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, whiteSpace: 'nowrap' }}><Icon name="phone" size={11}/>{b.phone}</span>
              </div>
            </div>
          </div>
        </div>

        {/* Action bar — primary ops */}
        <div style={{ margin: '0 -24px', padding: '12px 22px', borderBottom: '1px solid var(--line)', background: 'var(--bg-sunken)', display: 'flex', gap: 6, flexWrap: 'wrap' }}>
          <DrawerAction icon="mail" label="Resend"/>
          <DrawerAction icon="refresh" label="Transfer" onClick={() => setTransferOpen(true)}/>
          <DrawerAction icon="qr" label="Mark checked-in" tone={b.checkedIn ? 'subtle' : 'default'}/>
          <DrawerAction icon="close" label="Refund" tone="danger" onClick={() => setRefundOpen(true)}/>
          <div style={{ flex: 1 }}/>
          <DrawerAction icon="more" label="" small/>
        </div>

        {/* Tabs */}
        <div style={{ margin: '0 -24px', padding: '0 22px', borderBottom: '1px solid var(--line)' }}>
          <div style={{ display: 'flex', gap: 18 }}>
            {[
              { v: 'details', l: 'Details' },
              { v: 'tickets', l: `Tickets · ${b.tickets.length}` },
              { v: 'activity', l: 'Activity' },
              { v: 'notes', l: 'Notes' },
            ].map(t => (
              <button key={t.v} onClick={() => setTab(t.v)} style={{
                background: 'transparent', border: 0, padding: '12px 0',
                fontFamily: 'inherit', fontSize: 13, fontWeight: tab === t.v ? 600 : 500,
                color: tab === t.v ? 'var(--ink)' : 'var(--ink-3)',
                borderBottom: `2px solid ${tab === t.v ? 'var(--accent)' : 'transparent'}`,
                marginBottom: -1, cursor: 'pointer',
              }}>{t.l}</button>
            ))}
          </div>
        </div>

        {/* Tab content */}
        <div style={{ margin: '0 -24px', padding: '18px 22px' }}>
          {tab === 'details' && <BookingDetailsPanel b={b}/>}
          {tab === 'tickets' && <BookingTicketsPanel b={b}/>}
          {tab === 'activity' && <BookingActivityPanel b={b}/>}
          {tab === 'notes' && <BookingNotesPanel b={b} draft={noteDraft} setDraft={setNoteDraft}/>}
        </div>
      </Drawer>

      <RefundDialog open={refundOpen} onClose={() => setRefundOpen(false)} booking={b}/>
      <TransferDialog open={transferOpen} onClose={() => setTransferOpen(false)} booking={b}/>
    </>
  );
};

const DrawerAction = ({ icon, label, tone, onClick, small }) => {
  const colors = {
    default: { bg: 'var(--bg-raised)', border: 'var(--line)', color: 'var(--ink-2)' },
    danger:  { bg: 'var(--bg-raised)', border: 'var(--line)', color: 'var(--danger)' },
    subtle:  { bg: 'var(--bg-muted)', border: 'var(--line)', color: 'var(--ink-3)' },
  };
  const c = colors[tone || 'default'];
  return (
    <button onClick={onClick} style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: small ? '0 8px' : '6px 10px', height: 30,
      borderRadius: 8, background: c.bg, border: `1px solid ${c.border}`,
      color: c.color, fontSize: 12.5, fontWeight: 500, fontFamily: 'inherit', cursor: 'pointer',
      whiteSpace: 'nowrap',
    }}>
      <Icon name={icon} size={13}/>
      {label}
    </button>
  );
};

const StatusChip = ({ status }) => {
  const map = {
    paid:        { bg: 'color-mix(in oklch, var(--success) 14%, transparent)', color: 'var(--success)', label: 'Paid', dot: true },
    pending:     { bg: 'color-mix(in oklch, var(--warning) 18%, transparent)', color: 'oklch(0.48 0.14 75)', label: 'Pending', dot: true },
    refund:      { bg: 'var(--bg-muted)', color: 'var(--ink-3)', label: 'Refunded' },
    'checked-in':{ bg: 'color-mix(in oklch, var(--indigo) 14%, transparent)', color: 'var(--indigo)', label: 'Checked in' },
  };
  const s = map[status] || map.paid;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '3px 8px', borderRadius: 99, fontSize: 11, fontWeight: 600,
      background: s.bg, color: s.color,
    }}>
      {s.dot && <span style={{ width: 5, height: 5, borderRadius: 99, background: 'currentColor' }}/>}
      {s.label}
    </span>
  );
};

// ─── DETAILS TAB ──────────────────────────────────────────────────────────────

const BookingDetailsPanel = ({ b }) => (
  <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
    {/* Event card */}
    <div style={{ border: '1px solid var(--line)', borderRadius: 12, padding: 14, display: 'flex', gap: 12, alignItems: 'flex-start' }}>
      <div style={{
        width: 52, height: 52, borderRadius: 10, flexShrink: 0,
        background: 'radial-gradient(circle at 30% 20%, oklch(0.7 0.2 30), oklch(0.4 0.15 340) 70%)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: '#fff', fontFamily: 'var(--font-display)', fontStyle: 'italic', fontSize: 12, lineHeight: 1, textAlign: 'center',
      }}>Dil<br/>Lum</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 11, fontWeight: 600, color: 'var(--ink-3)', textTransform: 'uppercase', letterSpacing: '0.08em' }}>Event</div>
        <div style={{ fontSize: 14, fontWeight: 600, marginTop: 2 }}>{b.event}</div>
        <div style={{ fontSize: 12, color: 'var(--ink-3)', marginTop: 4, display: 'flex', gap: 12, flexWrap: 'wrap' }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}><Icon name="calendar" size={11}/>{b.eventDate}</span>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}><Icon name="pin" size={11}/>{b.venue}</span>
        </div>
      </div>
    </div>

    {/* Metadata grid */}
    <DetailGrid rows={[
      ['Booked at', b.bookedAt],
      ['Channel', b.channel],
      ['Payment method', b.paymentMethod],
      ['Txn ID', <span className="mono" style={{ fontSize: 12 }}>{b.txnId}</span>],
      ['Invoice', <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}><span className="mono" style={{ fontSize: 12 }}>{b.invoice}</span><button style={{ background:'transparent', border: 0, padding: 0, cursor: 'pointer', color: 'var(--accent)', fontSize: 12, fontFamily: 'inherit', textDecoration: 'underline' }}>Download PDF</button></span>],
    ]}/>

    {/* Price breakdown */}
    <div style={{ border: '1px solid var(--line)', borderRadius: 12, overflow: 'hidden' }}>
      <div style={{ padding: '12px 14px', background: 'var(--bg-sunken)', borderBottom: '1px solid var(--line)', fontSize: 12, fontWeight: 600, color: 'var(--ink-3)', textTransform: 'uppercase', letterSpacing: '0.06em' }}>Payment</div>
      <div style={{ padding: '6px 14px' }}>
        {[
          ['Subtotal', euro(b.subtotal)],
          ['Platform fee', euro(b.fees)],
          ['VAT (19% MwSt.)', euro(b.vat)],
        ].map(([l, v]) => (
          <div key={l} style={{ display: 'flex', justifyContent: 'space-between', padding: '8px 0', fontSize: 13, color: 'var(--ink-2)' }}>
            <span>{l}</span><span className="mono">{v}</span>
          </div>
        ))}
        <div style={{ borderTop: '1px solid var(--line)', marginTop: 4, paddingTop: 10, paddingBottom: 10, display: 'flex', justifyContent: 'space-between', fontSize: 14 }}>
          <span style={{ fontWeight: 600 }}>Total paid</span>
          <span className="mono" style={{ fontWeight: 600, fontSize: 16, fontFamily: 'var(--font-display)' }}>{euro(b.total)}</span>
        </div>
      </div>
    </div>
  </div>
);

const DetailGrid = ({ rows }) => (
  <div style={{ display: 'grid', gridTemplateColumns: '140px 1fr', rowGap: 10, columnGap: 12 }}>
    {rows.map(([k, v], i) => (
      <React.Fragment key={i}>
        <div style={{ fontSize: 12, color: 'var(--ink-3)', paddingTop: 1 }}>{k}</div>
        <div style={{ fontSize: 13, color: 'var(--ink)' }}>{v}</div>
      </React.Fragment>
    ))}
  </div>
);

// ─── TICKETS TAB ──────────────────────────────────────────────────────────────

const BookingTicketsPanel = ({ b }) => (
  <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
    {b.tickets.map((t, i) => (
      <div key={t.id} style={{ border: '1px solid var(--line)', borderRadius: 12, overflow: 'hidden', background: 'var(--bg-raised)' }}>
        <div style={{ display: 'flex', alignItems: 'stretch' }}>
          {/* Left: QR */}
          <div style={{ width: 92, background: 'var(--bg-sunken)', display: 'flex', alignItems: 'center', justifyContent: 'center', borderRight: '1px dashed var(--line-2)', position: 'relative' }}>
            <QrBlock seed={t.id}/>
            {/* perforation notches */}
            <div style={{ position: 'absolute', left: -6, top: -6, width: 12, height: 12, borderRadius: 99, background: 'var(--bg)' }}/>
            <div style={{ position: 'absolute', left: -6, bottom: -6, width: 12, height: 12, borderRadius: 99, background: 'var(--bg)' }}/>
          </div>
          {/* Right: details */}
          <div style={{ flex: 1, padding: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
              <span style={{ padding: '2px 8px', borderRadius: 99, fontSize: 10.5, fontWeight: 700, letterSpacing: '0.06em', textTransform: 'uppercase', background: t.tier === 'Platinum' ? 'oklch(0.92 0.04 265)' : 'oklch(0.94 0.04 80)', color: t.tier === 'Platinum' ? 'oklch(0.35 0.12 265)' : 'oklch(0.4 0.12 80)' }}>{t.tier}</span>
              <span style={{ fontFamily: 'var(--font-mono)', fontSize: 11, color: 'var(--ink-3)' }}>{t.id}</span>
              <div style={{ flex: 1 }}/>
              <StatusChip status={t.status === 'valid' ? 'paid' : t.status}/>
            </div>
            <div style={{ fontSize: 13.5, fontWeight: 600, marginBottom: 2 }}>{t.attendee}</div>
            <div style={{ fontSize: 12, color: 'var(--ink-3)', display: 'flex', gap: 14 }}>
              <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}><Icon name="seat" size={11}/>{t.seat}</span>
              <span className="mono">{euro(t.price)}</span>
            </div>
            <div style={{ marginTop: 10, display: 'flex', gap: 6 }}>
              <button style={ticketActionStyle}><Icon name="mail" size={11}/>Resend</button>
              <button style={ticketActionStyle}><Icon name="download" size={11}/>Download</button>
              <button style={ticketActionStyle}><Icon name="refresh" size={11}/>Transfer</button>
            </div>
          </div>
        </div>
      </div>
    ))}
    <button style={{
      padding: '10px 14px', border: '1px dashed var(--line-2)', borderRadius: 10,
      background: 'transparent', color: 'var(--ink-3)', fontSize: 12.5, fontFamily: 'inherit', cursor: 'pointer',
      display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6,
    }}>
      <Icon name="plus" size={12}/>Add ticket to this booking
    </button>
  </div>
);

const ticketActionStyle = {
  display: 'inline-flex', alignItems: 'center', gap: 5,
  padding: '4px 8px', borderRadius: 6, border: '1px solid var(--line)',
  background: 'var(--bg-raised)', color: 'var(--ink-2)',
  fontSize: 11.5, fontFamily: 'inherit', cursor: 'pointer',
};

// Deterministic pseudo-QR (visual only)
const QrBlock = ({ seed = 'x', size = 64 }) => {
  const n = 11;
  const cells = React.useMemo(() => {
    let s = 0;
    for (let i = 0; i < seed.length; i++) s = (s * 31 + seed.charCodeAt(i)) >>> 0;
    const out = [];
    for (let i = 0; i < n * n; i++) {
      s = (s * 1664525 + 1013904223) >>> 0;
      out.push((s & 1) === 1);
    }
    // Force corners (finder patterns)
    [[0,0],[0,1],[1,0],[1,1],[0,n-1],[0,n-2],[1,n-1],[1,n-2],[n-1,0],[n-1,1],[n-2,0],[n-2,1]].forEach(([r,c]) => out[r*n+c] = true);
    return out;
  }, [seed]);
  const cell = size / n;
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}>
      <rect width={size} height={size} fill="#fff"/>
      {cells.map((on, i) => on ? <rect key={i} x={(i % n) * cell} y={Math.floor(i / n) * cell} width={cell} height={cell} fill="#111"/> : null)}
    </svg>
  );
};

// ─── ACTIVITY TAB ─────────────────────────────────────────────────────────────

const BookingActivityPanel = ({ b }) => (
  <div style={{ position: 'relative', paddingLeft: 8 }}>
    <div style={{ position: 'absolute', left: 15, top: 6, bottom: 6, width: 1, background: 'var(--line)' }}/>
    {b.activity.map((a, i) => (
      <div key={i} style={{ display: 'flex', gap: 14, paddingBottom: 14, position: 'relative' }}>
        <div style={{
          width: 22, height: 22, borderRadius: 99, flexShrink: 0, zIndex: 1,
          background: a.tone === 'success' ? 'color-mix(in oklch, var(--success) 18%, var(--bg-raised))'
                    : a.tone === 'danger'  ? 'color-mix(in oklch, var(--danger) 18%, var(--bg-raised))'
                    : 'var(--bg-raised)',
          border: `1.5px solid ${a.tone === 'success' ? 'var(--success)' : a.tone === 'danger' ? 'var(--danger)' : 'var(--line-2)'}`,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          color: a.tone === 'success' ? 'var(--success)' : a.tone === 'danger' ? 'var(--danger)' : 'var(--ink-2)',
        }}>
          <Icon name={a.ic} size={10}/>
        </div>
        <div style={{ flex: 1, minWidth: 0, paddingTop: 1 }}>
          <div style={{ fontSize: 13 }}>{a.txt}</div>
          <div style={{ fontSize: 11, color: 'var(--ink-3)', marginTop: 2 }}>{a.t}</div>
        </div>
      </div>
    ))}
    <div style={{ fontSize: 11.5, color: 'var(--ink-4)', marginTop: 4, textAlign: 'center' }}>End of activity log</div>
  </div>
);

// ─── NOTES TAB ────────────────────────────────────────────────────────────────

const BookingNotesPanel = ({ b, draft, setDraft }) => {
  const notes = [
    { by: 'Priya Menon', at: '2h ago', text: 'VIP guest — Universal Music plus-one. Flagged for gate team.' },
  ];
  return (
    <div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10, marginBottom: 14 }}>
        {notes.map((n, i) => (
          <div key={i} style={{ padding: 12, background: 'var(--bg-sunken)', borderRadius: 10, border: '1px solid var(--line)' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 6, flexWrap: 'wrap' }}>
              <Avatar name={n.by} size={20}/>
              <span style={{ fontSize: 12.5, fontWeight: 600, whiteSpace: 'nowrap' }}>{n.by}</span>
              <span style={{ fontSize: 11.5, color: 'var(--ink-3)', whiteSpace: 'nowrap' }}>· {n.at}</span>
            </div>
            <div style={{ fontSize: 13, color: 'var(--ink)' }}>{n.text}</div>
          </div>
        ))}
      </div>
      <Textarea value={draft} onChange={e => setDraft(e.target.value)} placeholder="Add a private note about this booking — only visible to your team." rows={3} style={{ width: '100%', display: 'block', boxSizing: 'border-box' }}/>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 8 }}>
        <span style={{ fontSize: 11.5, color: 'var(--ink-3)' }}>Markdown supported · @mention to notify a teammate</span>
        <Button variant="primary" size="sm" disabled={!draft.trim()}>Save note</Button>
      </div>
    </div>
  );
};

// ─── REFUND DIALOG ────────────────────────────────────────────────────────────

const RefundDialog = ({ open, onClose, booking }) => {
  const [amount, setAmount] = React.useState('full');
  const [reason, setReason] = React.useState('buyer');
  const [notify, setNotify] = React.useState(true);
  return (
    <Modal open={open} onClose={onClose} size="md" zIndex={1100} title="Refund booking"
      desc={`Refund ${booking.id} — ${euro(booking.total)} paid by ${booking.buyer}`}
      footer={<>
        <Button variant="ghost" onClick={onClose}>Cancel</Button>
        <Button variant="primary" style={{ background: 'var(--danger)', borderColor: 'var(--danger)' }} icon="close">Process refund</Button>
      </>}>
      <Field label="Refund amount">
        <div style={{ display: 'flex', gap: 8 }}>
          <RadioTile checked={amount === 'full'} onClick={() => setAmount('full')} label="Full refund" sub={euro(booking.total)}/>
          <RadioTile checked={amount === 'partial'} onClick={() => setAmount('partial')} label="Partial" sub="Set amount"/>
        </div>
      </Field>
      {amount === 'partial' && (
        <Field label="Amount to refund" style={{ marginTop: 12 }}>
          <Input prefix="€" placeholder="0,00" type="text"/>
        </Field>
      )}
      <Field label="Reason" style={{ marginTop: 12 }}>
        <Select value={reason} onChange={e => setReason(e.target.value)} options={[
          { value: 'buyer', label: 'Buyer requested' },
          { value: 'duplicate', label: 'Duplicate booking' },
          { value: 'event_change', label: 'Event changed (date/venue)' },
          { value: 'fraud', label: 'Fraud / chargeback risk' },
          { value: 'other', label: 'Other' },
        ]}/>
      </Field>
      <Field label="Internal note (optional)" style={{ marginTop: 12 }}>
        <Textarea rows={2} placeholder="Why is this being refunded?"/>
      </Field>
      <div style={{ marginTop: 14, padding: 12, background: 'color-mix(in oklch, var(--warning) 8%, var(--bg-sunken))', borderRadius: 10, border: '1px solid color-mix(in oklch, var(--warning) 25%, var(--line))' }}>
        <div style={{ display: 'flex', gap: 10 }}>
          <Icon name="warning" size={14} style={{ color: 'oklch(0.55 0.16 75)', marginTop: 2 }}/>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 13, fontWeight: 600, color: 'oklch(0.42 0.14 75)' }}>This will invalidate all 2 tickets</div>
            <div style={{ fontSize: 12, color: 'var(--ink-2)', marginTop: 3 }}>Refund typically lands in 3–5 business days via SEPA. Platform fee ({euro(booking.fees || 8)}) is non-refundable unless you absorb it.</div>
            <label style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginTop: 8, fontSize: 12.5 }}>
              <input type="checkbox"/> Absorb platform fee (refund full {euro(booking.total)})
            </label>
          </div>
        </div>
      </div>
      <label style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginTop: 14, fontSize: 13 }}>
        <input type="checkbox" checked={notify} onChange={e => setNotify(e.target.checked)}/> Notify buyer via email
      </label>
    </Modal>
  );
};

const TransferDialog = ({ open, onClose, booking }) => (
  <Modal open={open} onClose={onClose} size="md" zIndex={1100} title="Transfer tickets"
    desc={`Move ${booking.tickets.length} ticket${booking.tickets.length > 1 ? 's' : ''} from ${booking.buyer} to someone else`}
    footer={<>
      <Button variant="ghost" onClick={onClose}>Cancel</Button>
      <Button variant="primary" icon="refresh">Transfer tickets</Button>
    </>}>
    <Field label="Recipient email">
      <Input placeholder="friend@example.com" prefix={<Icon name="mail" size={12}/>}/>
    </Field>
    <Field label="Recipient phone" style={{ marginTop: 10 }}>
      <Input placeholder="+91 98XXX XXXXX" prefix={<Icon name="phone" size={12}/>}/>
    </Field>
    <Field label="Which tickets?" style={{ marginTop: 10 }}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
        {booking.tickets.map((t, i) => (
          <label key={t.id} style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '10px 12px', borderRadius: 8, border: '1px solid var(--line)', background: 'var(--bg-raised)', cursor: 'pointer' }}>
            <input type="checkbox" defaultChecked={i === 0}/>
            <span style={{ fontFamily: 'var(--font-mono)', fontSize: 11.5, color: 'var(--ink-3)' }}>{t.id}</span>
            <span style={{ fontSize: 13 }}>{t.tier} · {t.attendee}</span>
          </label>
        ))}
      </div>
    </Field>
    <label style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginTop: 14, fontSize: 13 }}>
      <input type="checkbox" defaultChecked/> Invalidate original ticket QR codes
    </label>
  </Modal>
);

const RadioTile = ({ checked, onClick, label, sub }) => (
  <button onClick={onClick} style={{
    flex: 1, padding: '10px 12px', borderRadius: 10,
    border: `1.5px solid ${checked ? 'var(--accent)' : 'var(--line)'}`,
    background: checked ? 'color-mix(in oklch, var(--accent) 7%, var(--bg-raised))' : 'var(--bg-raised)',
    color: 'var(--ink)', cursor: 'pointer', fontFamily: 'inherit', textAlign: 'left',
  }}>
    <div style={{ fontSize: 13, fontWeight: 600 }}>{label}</div>
    <div style={{ fontSize: 11.5, color: 'var(--ink-3)', marginTop: 2 }}>{sub}</div>
  </button>
);

window.BookingDrawer = BookingDrawer;
