// sc-screens-inperson.jsx — In-Person tab + Item Detail + Peek session + Witness confirm + Result

// ─── In-Person tab: per-item summary cards ────────────────────────────
function InPersonScreen({ viewerId, onOpenItem, onPeek }){
  const s = useStore();
  const ipForMe = inpersonForUser(s, viewerId);
  // Group by itemId
  const byItem = {};
  ipForMe.forEach(r=>{
    (byItem[r.itemId] = byItem[r.itemId] || []).push(r);
  });
  // For items not yet in any pair, optionally hide; show all catalog items the user has
  const itemEntries = Object.entries(byItem).sort((a,b)=>{
    // sort by most recent activity
    const ma = Math.max(...a[1].flatMap(r=>r.transfers.map(t=>t.at)));
    const mb = Math.max(...b[1].flatMap(r=>r.transfers.map(t=>t.at)));
    return mb-ma;
  });

  return (
    <>
      <NavBar
        title="In Person"
        subtitle="Bilateral crowns — one per item, per friend"
      />
      <ScrollBody>
        {/* Quick action */}
        <button onClick={()=>onPeek()} className="tap" style={{
          width:'100%', border:'none', marginBottom:18,
          background:'#fff', borderRadius:16, padding:'14px 16px',
          display:'flex', alignItems:'center', gap:14,
          boxShadow:'0 0 0 1px var(--hair), 0 1px 2px rgba(0,0,0,0.04)',
          textAlign:'left',
        }}>
          <div style={{
            width:42, height:42, borderRadius:12,
            background:'linear-gradient(160deg, var(--crown), var(--crown-2))',
            display:'flex', alignItems:'center', justifyContent:'center',
          }}>
            <Icon name="qr" size={22} color="#fff" stroke={2.2}/>
          </div>
          <div style={{flex:1}}>
            <div style={{fontSize:15.5, fontWeight:600, letterSpacing:'-.01em'}}>Spot something nearby</div>
            <div style={{fontSize:12.5, color:'var(--muted)'}}>Open a peek for friends to witness</div>
          </div>
          <Icon name="chev" size={18} color="var(--muted)"/>
        </button>

        <div style={{fontSize:12, fontWeight:600, color:'var(--muted)', letterSpacing:'.08em', padding:'0 4px 8px'}}>YOUR ITEMS</div>

        {itemEntries.length===0 && (
          <Card style={{padding:'30px 16px', textAlign:'center'}}>
            <div style={{fontSize:36, marginBottom:8}}>🦝</div>
            <div style={{fontSize:14.5, fontWeight:600}}>No bilateral crowns yet</div>
            <div style={{fontSize:13, color:'var(--muted)', marginTop:4}}>Spot something while you're with someone to start.</div>
          </Card>
        )}

        <div style={{display:'flex', flexDirection:'column', gap:10}}>
          {itemEntries.map(([itemId, rows])=>{
            const it = s.itemById[itemId];
            const heldByMe = rows.filter(r=>r.holderId===viewerId).length;
            const heldByThem = rows.length - heldByMe;
            const lastTransfer = Math.max(...rows.flatMap(r=>r.transfers.map(t=>t.at)));
            const tints = {
              rooster:['#F5D7B8','#E08A3A'],tanuki:['#EADBC0','#8C6B3D'],maneki:['#F6DCDC','#CF8080'],
              torii:['#F1CFC4','#B05238'],daruma:['#F7CFC0','#B84C38'],matcha:['#DDEBC9','#6B9452'],
              stolper:['#EFE1B8','#8C6E2B'],phone:['#F2C6C0','#AA3A2A'],azulejo:['#D5E3EF','#3E6E99'],
              lovelock:['#E9D7C3','#8E6B3B'],tuktuk:['#F3D9B8','#C17A2A'],cat:['#E5D8C2','#5C5048'],
              piano:['#E0E0E6','#5C5C7A'],
            };
            const [a,b] = tints[itemId] || ['#F4E7CE','#C9A66B'];
            return (
              <div key={itemId} onClick={()=>onOpenItem(itemId)} className="tap" style={{
                background:'#fff', borderRadius:16, padding:14,
                boxShadow:'0 1px 2px rgba(0,0,0,0.04), 0 0 0 0.5px var(--hair)',
                display:'flex', alignItems:'center', gap:14,
              }}>
                <div style={{
                  width:60, height:60, borderRadius:14,
                  background:`linear-gradient(150deg, ${a}, ${b})`,
                  display:'flex', alignItems:'center', justifyContent:'center',
                  fontSize:32,
                  boxShadow:'inset 0 -3px 8px rgba(0,0,0,0.08)',
                }}>{it.emoji}</div>
                <div style={{flex:1, minWidth:0}}>
                  <div style={{fontSize:16, fontWeight:600, letterSpacing:'-.01em'}}>{it.name}</div>
                  <div style={{fontSize:12.5, color:'var(--muted)', marginTop:2}}>
                    Last spot · {fmtDaysAgo(lastTransfer)}
                  </div>
                  <div style={{display:'flex', gap:6, marginTop:6}}>
                    <Chip tone="crown" style={{fontSize:11}}>
                      <Crown size={10} color="var(--crown-2)"/> You {heldByMe}
                    </Chip>
                    {heldByThem>0 && (
                      <Chip tone="rival" style={{fontSize:11}}>
                        Them {heldByThem}
                      </Chip>
                    )}
                  </div>
                </div>
                <div style={{display:'flex'}}>
                  {rows.slice(0,3).map((r,i)=>(
                    <div key={r.withId} style={{marginLeft:i===0?0:-8, position:'relative'}}>
                      <Ava person={s.people[r.withId]} size={26} ring={r.holderId===viewerId}/>
                    </div>
                  ))}
                </div>
                <Icon name="chev" size={16} color="var(--muted)"/>
              </div>
            );
          })}
        </div>

        <div style={{height:16}}/>
      </ScrollBody>
    </>
  );
}

// ─── Item Detail: bilateral list per friend ───────────────────────────
function ItemDetailScreen({ itemId, viewerId, onBack, onOpenHead, onPeek }){
  const s = useStore();
  const it = s.itemById[itemId];
  const ipForMe = inpersonForUser(s, viewerId).filter(r=>r.itemId===itemId);
  const heldByMe = ipForMe.filter(r=>r.holderId===viewerId);

  return (
    <>
      <NavBar compact title={it.name} onBack={onBack}/>
      <ScrollBody>
        {/* Hero */}
        <Card style={{padding:'18px 16px', marginBottom:14, textAlign:'center',
          background:'linear-gradient(180deg, #FFFBF0, #FFFDF8)'}}>
          <div style={{display:'flex', justifyContent:'center', marginBottom:10}}>
            <ItemTile item={it} size={86} rounded={20}/>
          </div>
          <div style={{fontSize:24, fontWeight:600, letterSpacing:'-.02em'}}>
            {heldByMe.length} {it.title}{heldByMe.length===1?'':'s'}
          </div>
          <div style={{fontSize:13.5, color:'var(--muted)', marginTop:2}}>
            You hold {heldByMe.length} of {ipForMe.length} bilateral crowns for this item
          </div>
          <div style={{marginTop:14}}>
            <Btn onClick={()=>onPeek(itemId)} leading={<Icon name="qr" size={16} color="#fff" stroke={2.2}/>}>
              Spot a {it.name.toLowerCase()} now
            </Btn>
          </div>
        </Card>

        <div style={{fontSize:12, fontWeight:600, color:'var(--muted)', letterSpacing:'.08em', padding:'0 4px 8px'}}>
          BY FRIEND
        </div>
        <Card style={{padding:0, overflow:'hidden', marginBottom:14}}>
          {ipForMe.map((r, i)=>{
            const op = s.people[r.withId];
            const mine = r.holderId===viewerId;
            const lastT = r.transfers[0];
            return (
              <div key={r.withId} onClick={()=>onOpenHead(r.withId)} className="tap" style={{
                padding:'12px 14px', display:'flex', alignItems:'center', gap:12,
                borderBottom: i<ipForMe.length-1?'0.5px solid var(--hair)':'none',
              }}>
                <div style={{position:'relative'}}>
                  <Ava person={op} size={42} ring={!mine}/>
                  {mine && (
                    <div style={{position:'absolute', top:-4, left:'50%', transform:'translateX(-50%) rotate(-6deg)'}}>
                      <Crown size={18} color="var(--crown)"/>
                    </div>
                  )}
                </div>
                <div style={{flex:1, minWidth:0}}>
                  <div style={{fontSize:15, fontWeight:600, letterSpacing:'-.01em'}}>{op.name}</div>
                  <div style={{fontSize:12.5, color: mine?'var(--crown-2)':'var(--muted)'}}>
                    {mine ? `You hold · ${fmtHeld(r.heldSince)}` : `${op.first} holds · ${fmtHeld(r.heldSince)}`}
                  </div>
                  {lastT && lastT.comment && (
                    <div style={{fontSize:12, color:'var(--ink-2)', marginTop:3, fontStyle:'italic',
                      whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis'}}>
                      "{lastT.comment}"
                    </div>
                  )}
                </div>
                <div style={{display:'flex', alignItems:'center', gap:4}}>
                  {r.streak>1 && <Chip tone="crown" style={{fontSize:11}}><Icon name="flame" size={10} color="var(--crown-2)"/>{r.streak}</Chip>}
                  <Icon name="chev" size={16} color="var(--muted)"/>
                </div>
              </div>
            );
          })}
        </Card>

        {/* Combined timeline */}
        <div style={{fontSize:12, fontWeight:600, color:'var(--muted)', letterSpacing:'.08em', padding:'0 4px 8px'}}>
          RECENT TRANSFERS
        </div>
        <Card style={{padding:0, overflow:'hidden', marginBottom:16}}>
          {ipForMe.flatMap(r=>r.transfers.map(t=>({...t, withId:r.withId}))).sort((a,b)=>b.at-a.at).slice(0,10).map((t, i, arr)=>{
            const by = s.people[t.byId];
            const op = s.people[t.withId];
            const youDid = t.byId===viewerId;
            return (
              <div key={t.id} style={{
                padding:'10px 14px', display:'flex', alignItems:'flex-start', gap:10,
                borderBottom: i<arr.length-1?'0.5px solid var(--hair)':'none',
              }}>
                <Ava person={by} size={32}/>
                <div style={{flex:1, minWidth:0}}>
                  <div style={{fontSize:13.5, letterSpacing:'-.01em'}}>
                    <b>{youDid?'You':by.first}</b> {youDid?'spotted with':'spotted while with'} <b>{youDid?op.first:'you'}</b>
                  </div>
                  {t.comment && (
                    <div style={{fontSize:12.5, color:'var(--ink-2)', marginTop:2}}>"{t.comment}"</div>
                  )}
                  <div style={{fontSize:11.5, color:'var(--muted)', marginTop:2}}>
                    {fmtDaysAgo(t.at)}{t.photo?' · with photo':''}
                  </div>
                </div>
              </div>
            );
          })}
        </Card>
        <div style={{height:8}}/>
      </ScrollBody>
    </>
  );
}

// ─── Peek session modal (host) ────────────────────────────────────────
function PeekFlow({ viewerId, onClose, presetItemId=null }){
  const s = useStore();
  // 'item' picker → 'live' (showing crown + QR + waiting) → 'review' (comment+confirm) → 'done'
  const [step, setStep] = React.useState(presetItemId ? 'live' : 'item');
  const [itemId, setItemId] = React.useState(presetItemId || null);
  const [comment, setComment] = React.useState('');
  const [withPhoto, setWithPhoto] = React.useState(false);

  const session = s.peekSession;
  const itemRef = React.useRef(itemId);
  itemRef.current = itemId;

  // Start session when entering 'live'
  React.useEffect(()=>{
    if (step==='live' && itemRef.current && !s.peekSession){
      SC.actions.startPeek(viewerId, itemRef.current);
    }
  }, [step]);

  // Auto-advance: when at least one witness confirms, host can move to review
  // But review gating: tap "Done" once witnesses confirmed.
  const stepRef = React.useRef(step);
  stepRef.current = step;

  React.useEffect(()=>{
    return ()=>{
      // cancel session if user closes before finalizing
      if (SC.get().peekSession && stepRef.current!=='done') SC.actions.cancelPeek();
    };
  }, []);

  function chooseItem(id){ setItemId(id); setStep('live'); }
  function endLive(){
    SC.actions.setPeekComment(comment);
    SC.actions.setPeekPhoto(withPhoto);
    setStep('review');
  }
  function finalize(){
    SC.actions.setPeekComment(comment);
    SC.actions.setPeekPhoto(withPhoto);
    SC.actions.finalizePeek();
    setStep('done');
    setTimeout(()=>onClose(), 2400);
  }
  function cancel(){
    SC.actions.cancelPeek();
    onClose();
  }

  if (step==='item'){
    // Item picker — show items for which I'm in a pair, plus all catalog items
    const eligibleItems = s.items;
    return (
      <div style={{position:'absolute', inset:0, background:'var(--bg)', zIndex:50, display:'flex', flexDirection:'column'}}>
        <div style={{height:STATUS_H}}/>
        <div style={{padding:'6px 16px', height:44, display:'flex', alignItems:'center', justifyContent:'space-between'}}>
          <button onClick={cancel} className="tap" style={{border:'none', background:'transparent', color:'var(--crown-2)', fontSize:16, padding:4}}>Cancel</button>
          <div style={{fontSize:16, fontWeight:600}}>What did you spot?</div>
          <div style={{width:60}}/>
        </div>
        <div style={{flex:1, overflow:'auto', padding:'10px 20px 20px'}}>
          <div style={{fontSize:13.5, color:'var(--muted)', marginBottom:14, textAlign:'center'}}>
            Pick the item, then friends nearby will witness it.
          </div>
          <div style={{display:'grid', gridTemplateColumns:'1fr 1fr 1fr', gap:10}}>
            {eligibleItems.map(it=>(
              <div key={it.id} onClick={()=>chooseItem(it.id)} className="tap" style={{
                background:'#fff', borderRadius:14, padding:10, textAlign:'center',
                boxShadow:'0 0 0 0.5px var(--hair), 0 1px 2px rgba(0,0,0,0.03)',
              }}>
                <div style={{margin:'0 auto', width:'100%'}}>
                  <ItemTile item={it} size={68} rounded={14}/>
                </div>
                <div style={{fontSize:12, fontWeight:600, marginTop:6, letterSpacing:'-.01em'}}>{it.name}</div>
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  if (step==='live' && session){
    const item = s.itemById[session.itemId];
    if (!item) return null;
    const elapsed = Date.now() - session.startedAt;
    const witnesses = session.witnesses;
    const confirmed = witnesses.filter(w=>w.status==='confirmed');
    return (
      <div style={{position:'absolute', inset:0, zIndex:50, display:'flex', flexDirection:'column',
        background:'linear-gradient(180deg, #1A1815 0%, #2C2620 100%)', color:'#fff'}}>
        <div style={{height:STATUS_H}}/>
        <div style={{padding:'6px 16px', height:44, display:'flex', alignItems:'center', justifyContent:'space-between'}}>
          <button onClick={cancel} className="tap" style={{border:'none', background:'rgba(255,255,255,0.12)', color:'#fff', fontSize:14, fontWeight:600, padding:'6px 12px', borderRadius:14}}>Cancel</button>
          <div style={{fontSize:14, fontWeight:600, letterSpacing:'.04em', textTransform:'uppercase', color:'rgba(255,255,255,0.7)'}}>Live · Peek</div>
          <div style={{width:62}}/>
        </div>

        <div style={{flex:1, display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', padding:'20px 24px'}}>
          {/* Animated crown over item */}
          <div style={{position:'relative', marginBottom:20}}>
            <div style={{
              width:170, height:170, borderRadius:'50%',
              background:'radial-gradient(circle at 50% 35%, var(--crown-soft), var(--crown) 60%, var(--crown-2))',
              display:'flex', alignItems:'center', justifyContent:'center',
              boxShadow:'0 24px 60px rgba(214,158,61,0.5)',
              animation:'peekPulse 2.4s ease-in-out infinite',
            }}>
              <div style={{fontSize:90}}>{item.emoji}</div>
            </div>
            <div style={{position:'absolute', top:-12, left:'50%', transform:'translateX(-50%) rotate(-4deg)'}}>
              <Crown size={50} color="#fff"/>
            </div>
            <style>{`
              @keyframes peekPulse {
                0%,100% { box-shadow: 0 24px 60px rgba(214,158,61,0.5), 0 0 0 0 rgba(214,158,61,0.0); }
                50%     { box-shadow: 0 24px 60px rgba(214,158,61,0.5), 0 0 0 26px rgba(214,158,61,0.0); }
                25%     { box-shadow: 0 24px 60px rgba(214,158,61,0.5), 0 0 0 14px rgba(214,158,61,0.4); }
              }
              @keyframes witnessIn {
                0% { transform: translateY(20px) scale(.8); opacity: 0; }
                60%{ transform: translateY(-3px) scale(1.06); opacity: 1; }
                100%{ transform: translateY(0) scale(1); opacity: 1; }
              }
            `}</style>
          </div>
          <div style={{fontSize:28, fontWeight:500, letterSpacing:'-.02em', textAlign:'center'}}>
            {item.name}
          </div>
          <div style={{fontSize:13, color:'rgba(255,255,255,0.6)', marginTop:4, textAlign:'center'}}>
            Show this to people nearby — they'll get a buzz to confirm
          </div>

          {/* QR code area */}
          <div style={{margin:'22px 0 16px', padding:14, background:'#fff', borderRadius:18,
            boxShadow:'0 12px 40px rgba(0,0,0,0.3)'}}>
            <QRCode value={`spotcrown.app/peek/${session.id}`} size={120}/>
          </div>

          {/* Witnesses */}
          <div style={{width:'100%'}}>
            <div style={{fontSize:11, fontWeight:600, color:'rgba(255,255,255,0.5)', letterSpacing:'.1em', textAlign:'center', marginBottom:8}}>
              {witnesses.length===0 ? 'WAITING FOR WITNESSES' : `${confirmed.length}/${witnesses.length} CONFIRMED`}
            </div>
            <div style={{display:'flex', flexDirection:'column', gap:8, maxHeight:160, overflow:'auto'}}>
              {witnesses.map((w, i)=>{
                const p = s.people[w.personId];
                return (
                  <div key={w.personId} style={{
                    background: w.status==='confirmed' ? 'rgba(62,140,90,0.25)' : 'rgba(255,255,255,0.08)',
                    border: w.status==='confirmed' ? '1px solid rgba(62,140,90,0.5)' : '1px solid rgba(255,255,255,0.12)',
                    borderRadius:12, padding:'8px 12px', display:'flex', alignItems:'center', gap:10,
                    animation: 'witnessIn 360ms cubic-bezier(.2,.9,.3,1.2) both',
                  }}>
                    <Ava person={p} size={32}/>
                    <div style={{flex:1, fontSize:14, fontWeight:600}}>{p.name}</div>
                    {w.status==='confirmed' ? (
                      <div style={{display:'flex', alignItems:'center', gap:4, color:'#A4D9B5', fontSize:12, fontWeight:600}}>
                        <Icon name="check" size={14} color="#A4D9B5" stroke={2.4}/> Confirmed
                      </div>
                    ) : (
                      <div style={{fontSize:11.5, color:'rgba(255,255,255,0.5)'}}>
                        Tap their phone…
                      </div>
                    )}
                  </div>
                );
              })}
            </div>
          </div>
        </div>

        <div style={{padding:'10px 20px 30px'}}>
          <Btn onClick={endLive} disabled={confirmed.length===0}
            leading={<Icon name="check" size={18} color="#fff" stroke={2.4}/>}>
            Done — {confirmed.length} witness{confirmed.length===1?'':'es'}
          </Btn>
          {confirmed.length===0 && (
            <div style={{fontSize:11.5, color:'rgba(255,255,255,0.4)', textAlign:'center', marginTop:8}}>
              Need at least one witness to claim
            </div>
          )}
        </div>
      </div>
    );
  }

  if (step==='review' && session){
    const item = s.itemById[session.itemId];
    if (!item) return null;
    const confirmed = session.witnesses.filter(w=>w.status==='confirmed');
    return (
      <div style={{position:'absolute', inset:0, background:'var(--bg)', zIndex:50, display:'flex', flexDirection:'column'}}>
        <div style={{height:STATUS_H}}/>
        <NavBar compact title="Add a note" onBack={()=>setStep('live')}/>
        <div style={{flex:1, overflow:'auto', padding:'0 20px 20px'}}>
          <Card style={{padding:14, marginBottom:14, display:'flex', alignItems:'center', gap:14}}>
            <ItemTile item={item} size={56} rounded={14}/>
            <div style={{flex:1}}>
              <div style={{fontSize:15.5, fontWeight:600, letterSpacing:'-.01em'}}>{item.name}</div>
              <div style={{fontSize:13, color:'var(--muted)', marginTop:2}}>
                {confirmed.length} witness{confirmed.length===1?'':'es'} · {fmtDaysAgo(session.startedAt)}
              </div>
              <div style={{display:'flex', marginTop:6}}>
                {confirmed.slice(0,5).map((w,i)=>(
                  <div key={w.personId} style={{marginLeft:i===0?0:-8}}>
                    <Ava person={s.people[w.personId]} size={26}/>
                  </div>
                ))}
              </div>
            </div>
          </Card>

          <div style={{fontSize:11.5, fontWeight:600, color:'var(--muted)', letterSpacing:'.08em', marginBottom:6, textTransform:'uppercase'}}>
            Comment (optional)
          </div>
          <Card style={{padding:0, marginBottom:14, overflow:'hidden'}}>
            <textarea value={comment} onChange={e=>setComment(e.target.value)}
              placeholder="Where, what, why… your bragging rights."
              style={{
                width:'100%', minHeight:80, border:'none', outline:'none',
                background:'transparent', padding:'12px 14px',
                fontSize:14.5, fontFamily:'inherit', color:'var(--ink)', resize:'none',
              }}/>
          </Card>

          <Card style={{padding:0, overflow:'hidden', marginBottom:14}}>
            <ToggleRow label="Attach photo (later)" value={withPhoto} onChange={setWithPhoto} last/>
          </Card>

          <Btn onClick={finalize} leading={<Crown size={18} color="#fff"/>}>
            Claim {confirmed.length} crown{confirmed.length===1?'':'s'}
          </Btn>
          <div style={{height:8}}/>
          <Btn kind="ghost" onClick={cancel}>Discard</Btn>
        </div>
      </div>
    );
  }

  if (step==='done'){
    return (
      <div style={{position:'absolute', inset:0, background:'linear-gradient(180deg, #1A1815 0%, #2C2620 100%)', zIndex:60,
        display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', color:'#fff', padding:'40px 20px'}}>
        <div style={{animation:'crownPop 500ms cubic-bezier(.2,.9,.3,1.4) both'}}>
          <div style={{
            width:140, height:140, borderRadius:'50%',
            background:'radial-gradient(circle at 50% 40%, var(--crown-soft), var(--crown) 70%, var(--crown-2))',
            display:'flex', alignItems:'center', justifyContent:'center',
            boxShadow:'0 20px 60px rgba(214,158,61,0.55)',
          }}>
            <Crown size={72} color="#fff"/>
          </div>
        </div>
        <div style={{fontSize:30, fontWeight:500, letterSpacing:'-.02em', marginTop:24, textAlign:'center'}}>
          Crown claimed!
        </div>
        <div style={{fontSize:14, color:'rgba(255,255,255,0.7)', marginTop:8, textAlign:'center', maxWidth:280}}>
          Witnessed in person — final.
        </div>
        <style>{`@keyframes crownPop { 0%{transform:scale(.2) rotate(-30deg); opacity:0} 100%{transform:scale(1) rotate(0); opacity:1} }`}</style>
      </div>
    );
  }

  return null;
}

// ─── Witness sheet — appears on the OTHER device when peek is live ────
function WitnessSheet({ viewerId, onClose }){
  const s = useStore();
  const session = s.peekSession;
  const isHost = !!session && session.hostId === viewerId;
  const sessionId = session ? session.id : null;
  const me = session ? session.witnesses.find(w=>w.personId===viewerId) : null;

  // Hooks MUST run before any early return.
  React.useEffect(()=>{
    if (session && !isHost && !me) SC.actions.joinPeek(viewerId);
  }, [sessionId, isHost]);

  if (!session || isHost) return null;
  const host = s.people[session.hostId];
  const item = s.itemById[session.itemId];
  if (!item || !host) return null;
  const confirmed = me && me.status==='confirmed';

  function confirm(){ SC.actions.confirmPeek(viewerId); }
  function dismiss(){ onClose(); }

  return (
    <div style={{position:'absolute', inset:0, zIndex:60, pointerEvents:'auto'}}>
      <div onClick={dismiss} style={{
        position:'absolute', inset:0, background:'rgba(20,17,12,0.55)', animation:'fadeIn 200ms ease both',
      }}/>
      <div style={{
        position:'absolute', left:0, right:0, bottom:0, background:'#FFFDF8',
        borderTopLeftRadius:28, borderTopRightRadius:28,
        padding:'12px 20px 30px',
        boxShadow:'0 -16px 40px rgba(0,0,0,0.2)',
        animation:'sheetUp 380ms cubic-bezier(.2,.8,.2,1) both',
      }}>
        <style>{`
          @keyframes fadeIn { from{opacity:0} to{opacity:1} }
          @keyframes sheetUp { from{transform:translateY(100%)} to{transform:translateY(0)} }
        `}</style>
        <div style={{display:'flex', justifyContent:'center', marginBottom:14}}>
          <div style={{width:40, height:5, borderRadius:3, background:'var(--hair)'}}/>
        </div>
        <div style={{textAlign:'center', marginBottom:14}}>
          <div style={{display:'inline-block', position:'relative', marginBottom:10}}>
            <ItemTile item={item} size={70} rounded={18}/>
            <div style={{position:'absolute', bottom:-6, right:-6}}>
              <Ava person={host} size={32} ring/>
            </div>
          </div>
          <div style={{fontSize:24, fontWeight:600, letterSpacing:'-.02em'}}>
            {host.first} is spotting a {item.name.toLowerCase()}
          </div>
          <div style={{fontSize:13.5, color:'var(--muted)', marginTop:4}}>
            {confirmed
              ? 'You confirmed — they\'ll claim the crown'
              : 'Are you actually with them right now?'}
          </div>
        </div>

        {!confirmed ? (
          <>
            <Btn onClick={confirm} leading={<Icon name="check" size={18} color="#fff" stroke={2.4}/>}>
              Yes, I'm with {host.first}
            </Btn>
            <div style={{height:8}}/>
            <Btn kind="ghost" onClick={dismiss}>Not nearby</Btn>
          </>
        ) : (
          <>
            <Card style={{padding:'12px 14px', marginBottom:10, background:'#E8F2EC',
              boxShadow:'0 0 0 1px rgba(62,140,90,0.3)', display:'flex', alignItems:'center', gap:10}}>
              <div style={{width:32, height:32, borderRadius:'50%', background:'var(--ok)',
                display:'flex', alignItems:'center', justifyContent:'center'}}>
                <Icon name="check" size={16} color="#fff" stroke={2.6}/>
              </div>
              <div style={{flex:1, fontSize:13.5, fontWeight:600, color:'var(--ok)'}}>
                Confirmed — waiting for {host.first} to wrap up
              </div>
            </Card>
            <Btn kind="secondary" onClick={dismiss}>Close</Btn>
          </>
        )}
      </div>
    </div>
  );
}

Object.assign(window, { InPersonScreen, ItemDetailScreen, PeekFlow, WitnessSheet });
