/* Splash (ticket tears open → logo) + onboarding (3 distinct scenes: local radar, seat pick, one-tap pay) */
const STAGE_BG = 'radial-gradient(120% 70% at 50% 35%, #2B1640 0%, #160A24 55%, #0B0513 100%)';

function Splash({ onDone }) {
  const [ph, setPh] = useState(0);
  useEffect(() => { const t = [setTimeout(() => setPh(1), 1250), setTimeout(() => setPh(2), 1750), setTimeout(() => setPh(3), 4000), setTimeout(onDone, 4500)]; return () => t.forEach(clearTimeout); }, []);
  const half = (side) => { const L = side === 'L'; const d = L ? 'M14,0 H120 V44 A16,16 0 0 0 120,76 V120 H14 A14,14 0 0 1 0,106 V14 A14,14 0 0 1 14,0 Z' : 'M120,0 H226 A14,14 0 0 1 240,14 V106 A14,14 0 0 1 226,120 H120 V76 A16,16 0 0 0 120,44 Z';
    return <g style={{ transformOrigin: L ? '120px 120px' : '120px 120px', transform: ph >= 2 ? `translateX(${L ? -70 : 70}px) rotate(${L ? -14 : 14}deg)` : 'none', opacity: ph >= 2 ? 0 : 1, transition: 'transform .8s cubic-bezier(.5,0,.2,1), opacity .6s .25s' }}>
      <path d={d} fill={ph >= 1 ? 'url(#spTk)' : 'transparent'} stroke="#FFB6C0" strokeWidth="2" pathLength="1" style={{ strokeDasharray: 1, strokeDashoffset: 1, animation: 'spDraw 1.1s cubic-bezier(.6,0,.3,1) .1s forwards', transition: 'fill .4s' }} />
      {L ? <><rect x="18" y="20" width="58" height="8" rx="4" fill="rgba(255,255,255,.55)" style={{ opacity: ph >= 1 ? 1 : 0, transition: 'opacity .3s .1s' }} /><rect x="18" y="36" width="84" height="6" rx="3" fill="rgba(255,255,255,.3)" style={{ opacity: ph >= 1 ? 1 : 0, transition: 'opacity .3s .15s' }} /><rect x="18" y="88" width="40" height="14" rx="4" fill="rgba(255,255,255,.4)" style={{ opacity: ph >= 1 ? 1 : 0, transition: 'opacity .3s .2s' }} /></>
        : <g style={{ opacity: ph >= 1 ? 1 : 0, transition: 'opacity .3s .15s' }}>{[...Array(5)].map((_, r) => [...Array(5)].map((_, c) => ((r * 7 + c * 3) % 4 ? <rect key={r + '-' + c} x={150 + c * 12} y={30 + r * 12} width="9" height="9" rx="1.5" fill="rgba(255,255,255,.7)" /> : null)))}</g>}
    </g>; };
  return <div onClick={onDone} style={{ position: 'absolute', inset: 0, overflow: 'hidden', background: STAGE_BG, cursor: 'pointer', opacity: ph === 3 ? 0 : 1, transition: 'opacity .5s ease' }}>
    <div style={{ position: 'absolute', left: '50%', top: '44%', width: 520, height: 520, margin: '-260px 0 0 -260px', borderRadius: '50%', background: 'radial-gradient(circle, rgba(229,69,88,.5), rgba(123,92,255,.18) 40%, transparent 65%)', transform: `scale(${ph >= 2 ? 1.25 : .5})`, opacity: ph >= 2 ? 1 : .5, transition: 'transform 1.2s cubic-bezier(.2,.8,.2,1), opacity .8s' }}></div>
    <svg viewBox="-20 -20 280 160" style={{ position: 'absolute', left: '50%', top: '44%', width: 300, marginLeft: -150, marginTop: -86, overflow: 'visible' }}>
      <defs><linearGradient id="spTk" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stopColor="#E54558" /><stop offset="1" stopColor="#5B2A7A" /></linearGradient></defs>
      {half('L')}{half('R')}
      <line x1="120" y1="6" x2="120" y2="114" stroke="#fff" strokeOpacity={ph >= 1 && ph < 2 ? .7 : 0} strokeWidth="2" strokeDasharray="2 6" strokeLinecap="round" style={{ transition: 'stroke-opacity .3s' }} />
      {ph >= 2 && [...Array(18)].map((_, i) => { const a = i / 18 * Math.PI * 2; return <circle key={i} cx="120" cy="60" r={2 + (i % 3)} fill={['#E54558', '#FFD166', '#B9A6FF', '#3ED598', '#fff'][i % 5]} style={{ '--dx': Math.cos(a) * (110 + (i % 4) * 25) + 'px', '--dy': Math.sin(a) * (80 + (i % 3) * 20) + 'px', animation: 'dpBurst 1s cubic-bezier(.2,.8,.2,1) both' }} />; })}
    </svg>
    <div style={{ position: 'absolute', left: 0, right: 0, top: '44%', transform: 'translateY(-50%)', display: 'flex', flexDirection: 'column', alignItems: 'center', pointerEvents: 'none' }}>
      <img src={DP.A + 'desipass-logo-white.svg'} alt="DesiPass" style={{ width: 200, opacity: 0, animation: ph >= 2 ? 'dpLogoIn .9s cubic-bezier(.2,.8,.2,1) .2s both' : 'none', filter: 'drop-shadow(0 10px 40px rgba(229,69,88,.55))' }} />
      <div style={{ marginTop: 18, fontSize: 12.5, fontWeight: 600, letterSpacing: '.22em', textTransform: 'uppercase', color: 'rgba(255,255,255,.8)', fontFamily: 'var(--dp-font-mono)', opacity: 0, animation: ph >= 2 ? 'dpTag .6s ease .9s both' : 'none' }}>Connecting cultures worldwide</div>
    </div>
  </div>;
}

/* scene 1 - local radar */
function SceneLocal() {
  const { city } = useApp();
  const W = 360, H = 320, cx = W / 2, cy = H / 2 + 10;
  const roads = useMemo(() => { const r = rnd(3), o = []; for (let i = 0; i < 9; i++) o.push({ y: 20 + i * 36 + r() * 10, a: (r() - .5) * 18 }); for (let i = 0; i < 8; i++) o.push({ x: 10 + i * 48 + r() * 10, a: (r() - .5) * 12 }); return o; }, []);
  const local = DP.EVENTS.filter(e => e.city === city).slice(0, 4);
  const list = local.length >= 3 ? local : DP.EVENTS.filter(e => e.cc === DP.CITIES[city].cc).slice(0, 4);
  const spots = [[-92, -78], [88, -52], [-70, 70], [96, 84]];
  const eu = Object.keys(DP.CITIES).map(n => DP.CITIES[n]);
  return <div style={{ position: 'relative', width: '100%', height: '100%' }}>
    <svg viewBox={`0 0 ${W} ${H}`} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs><radialGradient id="olFade"><stop offset=".55" stopColor="#fff" /><stop offset="1" stopColor="#fff" stopOpacity="0" /></radialGradient><mask id="olMask"><rect width={W} height={H} fill="url(#olFade)" /></mask><linearGradient id="olSweep" x1="0" x2="1"><stop offset="0" stopColor="#E54558" stopOpacity="0" /><stop offset="1" stopColor="#E54558" stopOpacity=".35" /></linearGradient></defs>
      <g mask="url(#olMask)" stroke="rgba(255,255,255,.09)" strokeWidth="6" strokeLinecap="round">
        {roads.map((r, i) => r.y !== undefined ? <line key={i} x1="-20" y1={r.y} x2={W + 20} y2={r.y + r.a} /> : <line key={i} x1={r.x} y1="-20" x2={r.x + r.a} y2={H + 20} />)}
        <path d={`M-20,${H * .72} C80,${H * .6} 160,${H * .9} 380,${H * .66}`} stroke="rgba(123,92,255,.22)" strokeWidth="14" fill="none" />
      </g>
      {[60, 110, 160].map((r, i) => <circle key={r} cx={cx} cy={cy} r={r} fill="none" stroke="rgba(255,255,255,.08)" strokeDasharray="3 5" />)}
      <g style={{ transformOrigin: `${cx}px ${cy}px`, animation: 'dpSpin 3.2s linear infinite' }}><path d={`M${cx},${cy} L${cx + 160},${cy} A160,160 0 0 0 ${cx + 160 * Math.cos(-.9)},${cy + 160 * Math.sin(-.9)} Z`} fill="url(#olSweep)" /></g>
      <circle cx={cx} cy={cy} r="22" fill="#3B82F6" opacity=".18" style={{ transformOrigin: `${cx}px ${cy}px`, animation: 'spBeat 2s ease-out infinite' }} />
      <circle cx={cx} cy={cy} r="8" fill="#3B82F6" stroke="#fff" strokeWidth="3" />
    </svg>
    {list.map((e, i) => { const [dx, dy] = spots[i]; return <div key={e.id} style={{ position: 'absolute', left: `calc(50% + ${dx}px)`, top: `calc(50% + ${dy + 10}px)`, transform: 'translate(-50%,-100%)', animation: `spPop .5s cubic-bezier(.34,1.56,.64,1) ${.5 + i * .55}s both`, display: 'grid', justifyItems: 'center' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '4px 9px 4px 4px', borderRadius: 9999, background: '#fff', color: '#1D0D31', boxShadow: '0 10px 20px -8px rgba(0,0,0,.6)', whiteSpace: 'nowrap', marginBottom: 4 }}><img src={e.img} alt="" style={{ width: 24, height: 24, borderRadius: '50%', objectFit: 'cover' }} /><span style={{ display: 'grid' }}><span style={{ fontSize: 10, fontWeight: 700, maxWidth: 110, overflow: 'hidden', textOverflow: 'ellipsis' }}>{e.nm.split(' · ')[0].split(':')[0]}</span><span style={{ fontSize: 9, color: '#E54558', fontWeight: 700 }}>{e.day} · {e.dateLbl}</span></span></div>
      <svg width="18" height="22" viewBox="0 0 24 28"><path d="M12 27s9-8.5 9-15A9 9 0 1 0 3 12c0 6.5 9 15 9 15Z" fill="#E54558" stroke="#fff" strokeWidth="2" /><circle cx="12" cy="12" r="3.5" fill="#fff" /></svg>
    </div>; })}
    <div style={{ position: 'absolute', left: 4, bottom: 4, padding: '8px 10px', borderRadius: 12, background: 'rgba(255,255,255,.07)', border: '1px solid rgba(255,255,255,.1)', backdropFilter: 'blur(8px)', animation: 'dpTag .5s ease 2.8s both' }}>
      <svg width="74" height="46" viewBox="0 0 74 46">{eu.map((c, i) => <circle key={i} cx={(c.lng + 9) / 31 * 74} cy={(62.5 - c.lat) / 18.5 * 46} r="2" fill="#FF8E9B" style={{ animation: `dpPulse 2s ease-in-out ${i * .15}s infinite` }} />)}</svg>
      <div style={{ fontSize: 9.5, fontWeight: 700, color: 'rgba(255,255,255,.8)', marginTop: 2 }}>Across Europe & UK</div>
    </div>
  </div>;
}

/* scene 2 - pick seats */
function SceneSeats() {
  const rows = 'ABCDEFGH'.split(''), N = 12;
  const taken = useMemo(() => { const r = rnd(9), t = new Set(); rows.forEach(R => { for (let i = 1; i <= N; i++) if (r() < .38) t.add(R + i); }); ['C6', 'C7'].forEach(x => t.delete(x)); return t; }, []);
  return <div style={{ position: 'relative', width: '100%', height: '100%', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 14 }}>
    <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '7px 12px 7px 7px', borderRadius: 14, background: 'rgba(255,255,255,.08)', border: '1px solid rgba(255,255,255,.12)', animation: 'dpTag .5s ease both' }}><img src={DP.A + 'events/poster-04.png'} alt="" style={{ width: 30, height: 30, borderRadius: 8, objectFit: 'cover' }} /><div><div style={{ fontSize: 12.5, fontWeight: 700 }}>An Evening with Shreya Ghoshal</div><div style={{ fontSize: 10.5, color: 'rgba(255,255,255,.6)' }}>Festhalle, Frankfurt · Thu 22 Oct</div></div></div>
    <div style={{ width: '82%', height: 26, borderRadius: '0 0 50% 50% / 0 0 100% 100%', background: 'linear-gradient(180deg, rgba(229,69,88,.6), rgba(229,69,88,.1))', display: 'grid', placeItems: 'center', fontFamily: 'var(--dp-font-mono)', fontSize: 9, letterSpacing: '.3em', color: '#fff' }}>STAGE</div>
    <div style={{ position: 'relative', display: 'grid', gap: 6, perspective: 600 }}>
      <div style={{ display: 'grid', gap: 6, transform: 'rotateX(22deg)', transformOrigin: '50% 0' }}>{rows.map((R, ri) => <div key={R} style={{ display: 'flex', gap: 5, justifyContent: 'center' }}>{[...Array(N)].map((_, i) => { const id = R + (i + 1), t = taken.has(id), pick = id === 'C6' || id === 'C7'; return <span key={id} style={{ width: 17, height: 15, borderRadius: '5px 5px 3px 3px', marginLeft: i === 6 ? 10 : 0, background: t ? 'rgba(255,255,255,.12)' : ri < 3 ? 'rgba(185,166,255,.55)' : 'rgba(255,255,255,.38)', animation: pick ? `omSeat .35s ease ${id === 'C6' ? 1.6 : 2.3}s forwards` : 'none' }}></span>; })}</div>)}</div>
      <span style={{ position: 'absolute', left: 0, top: 0, width: 30, height: 30, marginLeft: -15, marginTop: -15, borderRadius: '50%', background: 'rgba(255,255,255,.35)', border: '2px solid #fff', boxShadow: '0 0 20px rgba(255,255,255,.5)', animation: 'omTap 3.2s cubic-bezier(.6,0,.3,1) .6s both' }}></span>
    </div>
    <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '9px 14px', borderRadius: 9999, background: '#fff', color: '#1D0D31', fontSize: 12.5, fontWeight: 700, boxShadow: '0 12px 26px -12px rgba(0,0,0,.6)', animation: 'dpTag .5s cubic-bezier(.2,.8,.2,1) 2.8s both' }}><Ico.seat size={15} style={{ color: '#E54558' }} />2 seats selected</div>
  </div>;
}

/* scene 3 - one tap to ticket */
function ScenePay() {
  const [s, setS] = useState(0);
  useEffect(() => { const t = setInterval(() => setS(x => (x + 1) % 4), 1500); return () => clearInterval(t); }, []);
  const ev = DP.EVENTS[0];
  return <div style={{ position: 'relative', width: '100%', height: '100%', display: 'grid', placeItems: 'center' }}>
    <div style={{ position: 'relative', width: 214, height: 300, borderRadius: 30, background: '#fff', color: '#1D0D31', overflow: 'hidden', boxShadow: '0 30px 60px -24px rgba(0,0,0,.7), 0 0 0 6px rgba(255,255,255,.08)' }}>
      <div style={{ position: 'relative', height: 132, overflow: 'hidden', display: 'grid', placeItems: 'center', background: '#1D0D31' }}><img src={ev.img} alt="" aria-hidden="true" style={{ position: 'absolute', inset: -20, width: 'calc(100% + 40px)', height: 'calc(100% + 40px)', objectFit: 'cover', filter: 'blur(18px) saturate(1.2)', opacity: .85 }} /><img src={ev.img} alt="" style={{ position: 'relative', height: 112, width: 'auto', borderRadius: 8, boxShadow: '0 8px 18px -8px rgba(0,0,0,.6)' }} /></div>
      <div style={{ padding: '10px 12px' }}><div style={{ fontFamily: 'var(--dp-font-mono)', fontSize: 8.5, color: '#E54558', fontWeight: 700, letterSpacing: '.08em' }}>{ev.day} · {ev.dateLbl.toUpperCase()}</div><div style={{ fontWeight: 700, fontSize: 12.5, marginTop: 3, lineHeight: 1.2 }}>{ev.nm}</div>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 10, padding: '7px 9px', borderRadius: 10, border: `1.5px solid ${s >= 1 ? '#1D0D31' : '#eee'}`, transition: 'border-color .3s' }}><span style={{ fontSize: 11, fontWeight: 700 }}>General</span><span style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 11, fontWeight: 700 }}><span style={{ width: 18, height: 18, borderRadius: '50%', border: '1px solid #ddd', display: 'grid', placeItems: 'center' }}>−</span><span key={s >= 1 ? 2 : 0} className={s >= 1 ? 'pop' : ''}>{s >= 1 ? 2 : 0}</span><span style={{ width: 18, height: 18, borderRadius: '50%', border: '1px solid #ddd', display: 'grid', placeItems: 'center', background: s === 0 ? '#FFF0F2' : '#fff' }}>+</span></span></div>
        <div style={{ marginTop: 10, height: 34, borderRadius: 9999, background: '#000', color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 4, fontSize: 12, fontWeight: 600, transform: s === 1 ? 'scale(.95)' : 'none', transition: 'transform .2s .9s' }}><Brand.apple />Pay</div>
      </div>
      <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 150, borderRadius: '20px 20px 0 0', background: '#f5f5f7', padding: '12px 14px', transform: s === 2 ? 'none' : 'translateY(105%)', transition: 'transform .45s cubic-bezier(.32,.72,0,1)', display: 'grid', justifyItems: 'center', alignContent: 'start', gap: 8 }}><div style={{ fontWeight: 700, fontSize: 13, display: 'flex', alignItems: 'center', gap: 3 }}><Brand.apple color="#000" />Pay</div><div style={{ fontSize: 11, color: '#666' }}>Pay DesiPass</div><div style={{ width: 44, height: 44, borderRadius: 12, border: '2.5px solid #0a84ff', display: 'grid', placeItems: 'center', color: '#0a84ff', marginTop: 4 }}><Ico.user size={22} /></div><div style={{ fontSize: 10.5, color: '#666' }}>Face ID</div></div>
      <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(200deg,#E54558,#3B2E4D 70%)', display: 'grid', placeItems: 'center', opacity: s === 3 ? 1 : 0, transform: s === 3 ? 'none' : 'scale(1.06)', transition: 'opacity .4s, transform .5s cubic-bezier(.2,.8,.2,1)' }}><div style={{ width: 160, borderRadius: 16, background: '#fff', padding: 12, textAlign: 'center' }}><div style={{ fontSize: 10, fontWeight: 700, color: '#129E6A', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 4 }}><Ico.check size={12} stroke={3} />You're going!</div><div style={{ margin: '8px auto 0', width: 96 }}><QRPattern seed={21} size={96} /></div><div style={{ fontFamily: 'var(--dp-font-mono)', fontSize: 9, marginTop: 6, letterSpacing: '.1em' }}>2 × GENERAL</div></div></div>
    </div>
    <div style={{ display: 'flex', gap: 6, marginTop: 16 }}>{['Select', 'Pay', 'Face ID', 'Ticket'].map((l, i) => <span key={l} style={{ fontSize: 10.5, fontWeight: 700, padding: '5px 10px', borderRadius: 9999, background: i === s ? '#E54558' : 'rgba(255,255,255,.1)', color: '#fff', transition: 'background .3s' }}>{l}</span>)}</div>
  </div>;
}

const ON_COPY = [
  ['Desi experiences,', 'around the corner', "See what's on near you tonight, across Europe and the UK."],
  ['Pick the', 'perfect seat', 'Choose your seats for concerts like Arijit Singh or Shreya Ghoshal.'],
  ['Tickets in', 'one tap', 'Pay with Apple Pay or Google Pay. Your QR is ready instantly.'],
];

function Onboarding({ onDone }) {
  const { topInset, bottomInset } = useApp();
  const [i, setI] = useState(0); const ref = useRef();
  const go = (n) => { const el = ref.current; el.scrollTo({ left: n * el.clientWidth, behavior: 'smooth' }); };
  const last = i === ON_COPY.length - 1;
  const Scene = [SceneLocal, SceneSeats, ScenePay][i];
  return <div className="fade-in" style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', background: STAGE_BG, color: '#fff' }}>
    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: `${topInset + 8}px 20px 0`, position: 'relative', zIndex: 3 }}><img src={DP.A + 'desipass-logo-white.svg'} alt="DesiPass" style={{ height: 22 }} /><button onClick={onDone} style={{ fontSize: 14, fontWeight: 600, color: 'rgba(255,255,255,.65)', padding: '8px 4px' }}>Skip</button></div>
    <div key={i} className="fade-in" style={{ position: 'relative', flex: 1, minHeight: 0, padding: '8px 20px 0' }}><Scene /></div>
    <div ref={ref} className="hs" onScroll={e => setI(Math.round(e.target.scrollLeft / e.target.clientWidth))} style={{ flex: 'none' }}>
      {ON_COPY.map(([a, em, d], k) => <div key={k} style={{ flex: '0 0 100%', padding: '16px 28px 0', minHeight: 146 }}><h1 style={{ margin: 0, fontFamily: 'var(--dp-font-display)', fontWeight: 600, fontSize: 32, lineHeight: 1.08, letterSpacing: '-.025em', color: '#fff', opacity: k === i ? 1 : .3, transition: 'opacity .4s' }}>{a} <em style={{ fontStyle: 'italic', fontWeight: 500, color: '#FF8E9B' }}>{em}</em></h1><p style={{ margin: '12px 0 0', fontSize: 15, lineHeight: 1.5, color: 'rgba(255,255,255,.7)', maxWidth: 320 }}>{d}</p></div>)}
    </div>
    <div style={{ padding: `12px 24px ${bottomInset + 20}px`, display: 'flex', alignItems: 'center', gap: 16 }}>
      <div style={{ display: 'flex', gap: 6, flex: 1 }}>{ON_COPY.map((_, k) => <span key={k} onClick={() => go(k)} style={{ width: k === i ? 22 : 7, height: 7, borderRadius: 4, background: k === i ? '#E54558' : 'rgba(255,255,255,.25)', transition: 'all .35s cubic-bezier(.2,.8,.2,1)', cursor: 'pointer' }}></span>)}</div>
      <Btn small onClick={() => last ? onDone() : go(i + 1)} style={{ minWidth: 130, height: 48, fontSize: 15 }}>{last ? 'Get Started' : 'Next'}{!last && <Ico.chev size={16} />}</Btn>
    </div>
  </div>;
}

Object.assign(window, { Splash, Onboarding });
