/* ============================================================
   Surebet Card — liquid glass, sport-accented
   ============================================================ */
const { useState, useMemo } = React;

function BookmakerChip({ id, size = 'sm' }) {
  const bk = window.BOOKMAKERS[id] || { name: id, mono: id.slice(0, 3).toUpperCase(), bg: '#1d2740', fg: '#caa86b' };
  const css = size === 'lg'
    ? { fontSize: 13, padding: '6px 12px', borderRadius: 8 }
    : { fontSize: 11, padding: '4px 9px', borderRadius: 6 };
  return (
    <span className="bk-chip mono" style={{
      ...css,
      background: bk.bg,
      color: bk.fg,
      fontWeight: 800,
      letterSpacing: '0.04em',
      display: 'inline-flex',
      alignItems: 'center',
      gap: 6,
      boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.18), inset 0 -6px 12px -10px rgba(0,0,0,0.5)',
      border: '1px solid rgba(255,255,255,0.06)',
    }}>
      <span style={{
        display: 'inline-grid',
        placeItems: 'center',
        width: size === 'lg' ? 20 : 16,
        height: size === 'lg' ? 20 : 16,
        borderRadius: 4,
        background: 'rgba(0,0,0,0.30)',
        color: bk.fg,
        fontSize: size === 'lg' ? 10 : 8.5,
        fontWeight: 900,
        letterSpacing: 0,
      }}>{bk.mono}</span>
      <span style={{ opacity: 0.92 }}>{bk.name.toUpperCase()}</span>
    </span>
  );
}

function WaveBars({ n = 32, accent = 'var(--acc)' }) {
  const bars = useMemo(() => {
    const out = [];
    for (let i = 0; i < n; i++) {
      out.push({ d: (0.6 + Math.random() * 0.9).toFixed(2), del: (Math.random() * -1).toFixed(2) });
    }
    return out;
  }, [n]);
  return (
    <div style={{ flex: 1, display: 'flex', alignItems: 'center', gap: 2, height: 26 }}>
      {bars.map((b, i) => (
        <i key={i} style={{
          flex: 1,
          borderRadius: 2,
          background: accent,
          opacity: 0.55,
          height: '100%',
          display: 'block',
          animation: `eq ${b.d}s ease-in-out ${b.del}s infinite alternate`,
        }} />
      ))}
    </div>
  );
}

function SportBall({ sport, accent }) {
  const s = window.SPORTS[sport] || { emoji: '🎯' };
  return (
    <div style={{
      position: 'absolute', top: 14, right: 14, width: 52, height: 52,
      borderRadius: '50%', display: 'grid', placeItems: 'center',
      fontSize: 25, zIndex: 2,
      background: 'radial-gradient(circle at 32% 26%, rgba(255,255,255,0.30), rgba(18,28,50,0.7))',
      border: '1px solid rgba(170,210,255,0.34)',
      boxShadow: 'inset 0 2px 9px rgba(255,255,255,0.22), inset 0 -6px 12px rgba(0,0,0,0.5), 0 10px 22px -6px rgba(0,0,0,0.75)',
    }}>{s.emoji}</div>
  );
}

function OddsTile({ sel, accent, side = 'left', fallbackLink }) {
  const tilt = side === 'left' ? 4 : -4;
  const [hovered, setHovered] = useState(false);
  const betLink = sel.betLink || fallbackLink || '';
  const Tag = betLink ? 'a' : 'div';
  const linkProps = betLink
    ? { href: betLink, target: '_blank', rel: 'noopener noreferrer' }
    : {};
  return (
    <Tag
      {...linkProps}
      onMouseEnter={() => setHovered(true)}
      onMouseLeave={() => setHovered(false)}
      style={{
        textDecoration: 'none',
      position: 'relative',
      borderRadius: 24,
      padding: '18px 14px 16px',
      textAlign: 'center',
      background:
        'radial-gradient(140% 70% at 50% -8%, rgba(255,255,255,0.32), transparent 55%), ' +
        'linear-gradient(168deg, rgba(94,118,166,0.62) 0%, rgba(30,44,74,0.55) 38%, rgba(8,14,28,0.78) 100%)',
      border: `1px solid ${hovered ? `color-mix(in srgb, ${accent} 70%, transparent)` : 'rgba(180,215,255,0.28)'}`,
      boxShadow: hovered
        ? 'inset 0 1.8px 0 rgba(255,255,255,0.70), ' +
          'inset 0 -22px 32px -22px rgba(0,0,0,0.95), ' +
          '0 22px 48px -14px rgba(0,0,0,0.92), ' +
          `0 0 40px -6px color-mix(in srgb, ${accent} 70%, transparent)`
        : 'inset 0 1.8px 0 rgba(255,255,255,0.55), ' +
          'inset 0 -22px 32px -22px rgba(0,0,0,0.95), ' +
          'inset 0 0 0 1px rgba(255,255,255,0.04), ' +
          '0 18px 36px -14px rgba(0,0,0,0.85), ' +
          `0 0 26px -10px color-mix(in srgb, ${accent} 40%, transparent)`,
      backdropFilter: 'blur(18px) saturate(1.7) brightness(1.06)',
      WebkitBackdropFilter: 'blur(18px) saturate(1.7) brightness(1.06)',
      overflow: 'hidden',
      isolation: 'isolate',
      transform: `perspective(900px) rotateY(${tilt}deg) translateY(${hovered ? -3 : 0}px)`,
      transformOrigin: side === 'left' ? 'right center' : 'left center',
      transition: 'transform .28s cubic-bezier(.22,1,.36,1), box-shadow .25s, border-color .25s',
      cursor: 'pointer',
    }}>
      {/* domed top reflection — large soft glow */}
      <div style={{
        position: 'absolute', left: '8%', right: '8%', top: 2, height: '46%',
        background: 'radial-gradient(60% 100% at 50% 0%, rgba(255,255,255,0.40), rgba(255,255,255,0.10) 50%, transparent 80%)',
        borderRadius: '50% 50% 60% 60% / 60% 60% 100% 100%',
        pointerEvents: 'none',
        filter: 'blur(1.5px)',
      }} />
      {/* edge sheen */}
      <div style={{
        position: 'absolute', inset: 0, borderRadius: 24, pointerEvents: 'none',
        background: 'linear-gradient(115deg, transparent 28%, rgba(255,255,255,0.14) 48%, transparent 62%)',
      }} />
      {/* faint bottom shadow */}
      <div style={{
        position: 'absolute', inset: 0, borderRadius: 24, pointerEvents: 'none',
        background: 'radial-gradient(80% 30% at 50% 105%, rgba(0,0,0,0.45), transparent 70%)',
      }} />

      {/* logo */}
      <div style={{ minHeight: 52, display: 'grid', placeItems: 'center', marginTop: 2, marginBottom: 12, position: 'relative', zIndex: 1 }}>
        <BookmakerLogo id={sel.book} size="lg" />
      </div>

      {/* odds number */}
      <div className="mono" style={{
        fontSize: 38, fontWeight: 800, lineHeight: 1,
        color: accent,
        textShadow:
          `0 0 30px color-mix(in srgb, ${accent} 85%, transparent), ` +
          `0 0 10px color-mix(in srgb, ${accent} 65%, transparent)`,
        letterSpacing: '-0.02em',
        position: 'relative', zIndex: 1,
      }}>{(sel.odds || 0).toFixed(2)}</div>

      {/* pick + stake — pick label first, stake amount smaller next to it */}
      <div style={{
        marginTop: 12, position: 'relative', zIndex: 1,
        display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
      }}>
        <div style={{ fontSize: 13, fontWeight: 700, color: 'var(--fg)', lineHeight: 1.1 }}>{sel.pick}</div>
        <div className="mono" style={{
          display: 'inline-block',
          fontSize: 11, fontWeight: 700,
          padding: '3px 11px', borderRadius: 999,
          color: accent,
          border: `1px solid color-mix(in srgb, ${accent} 60%, transparent)`,
          background: `color-mix(in srgb, ${accent} 10%, transparent)`,
          boxShadow:
            `0 0 14px -3px color-mix(in srgb, ${accent} 65%, transparent), ` +
            `inset 0 1px 0 rgba(255,255,255,0.16)`,
          textShadow: `0 0 6px color-mix(in srgb, ${accent} 45%, transparent)`,
        }}>{sel.ccy}{(sel.stake || 0).toFixed(2)}</div>
      </div>
      {/* BET badge — shows when hovering and betLink available */}
      {betLink && (
        <div style={{
          marginTop: 10, position: 'relative', zIndex: 1,
          opacity: hovered ? 1 : 0,
          transform: hovered ? 'translateY(0)' : 'translateY(4px)',
          transition: 'opacity .2s, transform .2s',
          display: 'flex', justifyContent: 'center',
        }}>
          <span className="mono" style={{
            fontSize: 10, fontWeight: 800, letterSpacing: '0.12em',
            padding: '4px 12px', borderRadius: 999,
            color: '#05080f',
            background: `linear-gradient(135deg, ${accent}, color-mix(in srgb, ${accent} 60%, #4fe4ff))`,
            boxShadow: `0 2px 12px -2px color-mix(in srgb, ${accent} 70%, transparent)`,
          }}>BET NOW →</span>
        </div>
      )}
    </Tag>
  );
}

function StakeChart({ stake, payout, accent }) {
  const stakePct = Math.round((stake / payout) * 100);
  return (
    <div style={{
      padding: '8px 8px 6px',
      borderRadius: 15,
      background: 'rgba(12,18,32,0.6)',
      border: '1px solid var(--bd)',
      boxShadow: 'inset 0 1.2px 0 rgba(255,255,255,0.22), 0 6px 16px -10px rgba(0,0,0,0.6)',
    }}>
      <span className="cap" style={{ display: 'block', textAlign: 'center', fontSize: 8.5, letterSpacing: '0.1em' }}>STAKE vs PAYOUT</span>
      <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'center', gap: 10, height: 42, marginTop: 5 }}>
        <div style={{
          width: 22, height: `${stakePct}%`, borderRadius: '5px 5px 2px 2px',
          background: 'linear-gradient(180deg, rgba(125,155,215,0.85), rgba(125,155,215,0.3))',
          boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.3)',
          position: 'relative',
        }}>
          <span style={{ position: 'absolute', top: -13, left: '50%', transform: 'translateX(-50%)', fontFamily: 'var(--font-mono)', fontSize: 8, color: 'var(--fg-muted)', whiteSpace: 'nowrap' }}>${(stake || 0).toFixed(0)}</span>
        </div>
        <div style={{
          width: 22, height: '100%', borderRadius: '5px 5px 2px 2px',
          background: `linear-gradient(180deg, ${accent}, color-mix(in srgb, ${accent} 22%, transparent))`,
          boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.3)',
          position: 'relative',
        }}>
          <span style={{ position: 'absolute', top: -13, left: '50%', transform: 'translateX(-50%)', fontFamily: 'var(--font-mono)', fontSize: 8, color: 'var(--fg-muted)', whiteSpace: 'nowrap' }}>${(payout || 0).toFixed(0)}</span>
        </div>
      </div>
      <div style={{ display: 'flex', justifyContent: 'center', gap: 14, marginTop: 3 }}>
        <i className="mono" style={{ fontSize: 8, fontStyle: 'normal', color: 'var(--fg-dim)' }}>Stake</i>
        <i className="mono" style={{ fontSize: 8, fontStyle: 'normal', color: 'var(--fg-dim)' }}>Total</i>
      </div>
    </div>
  );
}

function formatStartTime(st) {
  if (!st) return '';
  const d = new Date(st);
  if (isNaN(d.getTime())) return '';
  const pad = n => n.toString().padStart(2, '0');
  return `${pad(d.getDate())}/${pad(d.getMonth()+1)} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
}

function SurebetCard({ sb, onHide, compact = false }) {
  const sport = window.SPORTS[sb.sport] || { acc: '#27f0a3', emoji: '🎯', label: sb.sport };
  const accent = sport.acc;
  const [toggleOn, setToggleOn] = useState(sb.live);
  const stakePct = Math.round((sb.totalStake / sb.payout) * 100);

  // ── Locked / encrypted card for free users ──────────────────
  if (sb._encrypted) {
    return (
      <div style={{ position: 'relative', borderRadius: 26, overflow: 'hidden' }}>
        {/* blurred ghost card */}
        <div style={{
          borderRadius: 26, padding: '18px 16px 14px',
          background: 'linear-gradient(165deg, rgba(36,52,84,0.92) 0%, rgba(18,26,46,0.95) 46%, rgba(10,16,30,0.96) 100%)',
          border: `1px solid color-mix(in srgb, ${accent} 30%, transparent)`,
          filter: 'blur(6px)',
          userSelect: 'none', pointerEvents: 'none',
        }}>
          {/* fake header */}
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 10 }}>
            <div style={{ display: 'flex', gap: 8 }}>
              <div style={{ width: 70, height: 22, borderRadius: 999, background: `color-mix(in srgb, ${accent} 18%, rgba(8,13,24,0.55))` }} />
              <div style={{ width: 40, height: 22, borderRadius: 999, background: 'rgba(255,60,60,0.25)' }} />
            </div>
            <div className="mono" style={{ fontSize: 22, fontWeight: 800, color: accent }}>+{(sb.profit || 0).toFixed(2)}%</div>
          </div>
          <div style={{ height: 14, borderRadius: 6, background: 'rgba(255,255,255,0.08)', marginBottom: 6, width: '70%' }} />
          <div style={{ height: 11, borderRadius: 6, background: 'rgba(255,255,255,0.05)', marginBottom: 18, width: '45%' }} />
          {/* fake tiles */}
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
            {[0,1].map(i => (
              <div key={i} style={{ borderRadius: 20, padding: '18px 14px 16px', background: 'linear-gradient(168deg, rgba(94,118,166,0.55) 0%, rgba(8,14,28,0.78) 100%)', border: '1px solid rgba(180,215,255,0.18)' }}>
                <div style={{ height: 40, borderRadius: 6, background: 'rgba(255,255,255,0.06)', marginBottom: 12 }} />
                <div style={{ height: 36, borderRadius: 6, background: `color-mix(in srgb, ${accent} 12%, transparent)`, marginBottom: 10 }} />
                <div style={{ height: 18, borderRadius: 999, background: `color-mix(in srgb, ${accent} 8%, transparent)`, width: '60%', margin: '0 auto' }} />
              </div>
            ))}
          </div>
          <div style={{ height: 11, borderRadius: 6, background: 'rgba(255,255,255,0.05)', marginTop: 14, width: '80%', margin: '14px auto 0' }} />
        </div>
        {/* overlay */}
        <div style={{
          position: 'absolute', inset: 0, borderRadius: 26,
          background: 'linear-gradient(180deg, rgba(5,8,15,0.25) 0%, rgba(5,8,15,0.72) 100%)',
          display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'space-between', padding: '14px 16px',
        }}>
          {/* top — ROI badge (visible, unblurred) */}
          <div style={{ alignSelf: 'flex-end' }}>
            <span className="mono" style={{
              fontSize: 22, fontWeight: 800,
              color: accent,
              textShadow: `0 0 20px color-mix(in srgb, ${accent} 75%, transparent)`,
            }}>+{(sb.profit || 0).toFixed(2)}%</span>
          </div>
          {/* center — lock */}
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10 }}>
            <div className="mono" style={{ fontSize: 11, color: 'var(--fg-dim)', letterSpacing: '0.08em', textTransform: 'uppercase' }}>🔒 Premium Surebet</div>
            <button
              onClick={() => { if (window.__fm_nav) window.__fm_nav('payment'); }}
              style={{
                padding: '10px 22px', borderRadius: 10, cursor: 'pointer', border: 'none',
                background: 'linear-gradient(135deg, #4fe4ff, #27f0a3)',
                color: '#05080f', fontWeight: 700, fontSize: 13, letterSpacing: '0.02em',
                boxShadow: '0 4px 20px -4px rgba(79,228,255,0.55)',
                transition: 'transform .18s, box-shadow .18s',
              }}
              onMouseEnter={e => { e.currentTarget.style.transform = 'scale(1.06)'; e.currentTarget.style.boxShadow = '0 6px 28px -4px rgba(79,228,255,0.75)'; }}
              onMouseLeave={e => { e.currentTarget.style.transform = ''; e.currentTarget.style.boxShadow = '0 4px 20px -4px rgba(79,228,255,0.55)'; }}
            >Upgrade to Unlock</button>
          </div>
          {/* bottom spacer */}
          <div />
        </div>
      </div>
    );
  }

  return (
    <div className="sb-card" style={{
      '--acc': accent,
      position: 'relative',
      borderRadius: 26,
      padding: '18px 16px 14px',
      isolation: 'isolate',
      color: 'var(--fg)',
      background: 'linear-gradient(165deg, rgba(36,52,84,0.92) 0%, rgba(18,26,46,0.95) 46%, rgba(10,16,30,0.96) 100%)',
      border: '1px solid rgba(150,190,255,0.16)',
      boxShadow: 'var(--shadow-card-lg)',
      animation: 'rise .55s cubic-bezier(.22,1,.36,1) both',
      transition: 'transform .35s cubic-bezier(.22,1,.36,1), box-shadow .35s',
      contain: 'layout paint',
      overflow: 'hidden',
    }}>
      {/* gradient rim */}
      <div style={{
        content: '""', position: 'absolute', inset: 0, borderRadius: 26,
        padding: 1.3, zIndex: -1, pointerEvents: 'none',
        background: `linear-gradient(150deg, color-mix(in srgb, ${accent} 80%, white) 0%, rgba(255,255,255,0.30) 22%, transparent 50%, rgba(0,0,0,0.35) 100%)`,
        WebkitMask: 'linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)',
        WebkitMaskComposite: 'xor',
        maskComposite: 'exclude',
      }} />
      {/* sheen */}
      <div style={{
        position: 'absolute', inset: 0, borderRadius: 26, zIndex: -1,
        pointerEvents: 'none', overflow: 'hidden',
        background:
          'radial-gradient(150px 90px at 18% 0%, rgba(255,255,255,0.22), transparent 70%), ' +
          'linear-gradient(115deg, transparent 38%, rgba(255,255,255,0.16) 47%, rgba(255,255,255,0.04) 52%, transparent 60%)',
        backgroundSize: '100% 100%, 280% 100%',
        backgroundRepeat: 'no-repeat',
        animation: 'sheen 7s ease-in-out infinite',
      }} />

      {/* header — sport pill + live + ROI hero on the right */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <span className="mono" style={{
          display: 'inline-flex', alignItems: 'center', gap: 6,
          fontSize: 11, fontWeight: 700, letterSpacing: '0.06em',
          padding: '6px 12px', borderRadius: 999,
          border: `1px solid color-mix(in srgb, ${accent} 50%, transparent)`,
          background: `color-mix(in srgb, ${accent} 12%, rgba(8,13,24,0.55))`,
          color: accent,
          textShadow: `0 0 12px color-mix(in srgb, ${accent} 70%, transparent)`,
          boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.14)',
        }}>{sport.emoji} {sport.label.toUpperCase()}</span>

        <span className="mono" style={{
          display: 'inline-flex', alignItems: 'center', gap: 6,
          fontSize: 11, fontWeight: 800, letterSpacing: '0.10em',
          padding: '6px 12px', borderRadius: 999,
          border: sb.live ? '1px solid color-mix(in srgb, #ff3b4d 60%, transparent)' : '1px solid var(--bd)',
          background: sb.live ? 'color-mix(in srgb, #ff3b4d 18%, rgba(8,13,24,0.55))' : 'rgba(8,13,24,0.55)',
          color: sb.live ? '#ff5566' : '#fff',
          textShadow: sb.live ? '0 0 12px color-mix(in srgb, #ff3b4d 75%, transparent)' : 'none',
          boxShadow: sb.live
            ? '0 0 16px -2px color-mix(in srgb, #ff3b4d 60%, transparent), inset 0 1px 0 rgba(255,255,255,0.18)'
            : 'inset 0 1px 0 rgba(255,255,255,0.14)',
          animation: sb.live ? 'live-pulse 1.4s ease-in-out infinite' : 'none',
        }}>
          {sb.live && (
            <span style={{
              width: 7, height: 7, borderRadius: '50%',
              background: '#ff3b4d',
              boxShadow: '0 0 8px #ff3b4d, 0 0 16px -2px #ff3b4d',
              animation: 'pulse-dot 1.1s ease-in-out infinite',
            }} />
          )}
          {sb.live ? 'LIVE' : 'PRE'}
        </span>

        <div style={{ flex: 1 }} />

        {/* ROI hero — top right */}
        <div className="mono" style={{
          fontSize: 22, fontWeight: 800, color: accent, lineHeight: 1,
          textShadow: `0 0 20px color-mix(in srgb, ${accent} 75%, transparent)`,
          letterSpacing: '-0.02em',
        }}>+{(sb.roi || 0).toFixed(2)}%</div>
      </div>

      {/* match meta */}
      <div style={{ margin: '13px 2px 4px' }}>
        <div style={{ fontSize: 16.5, fontWeight: 700, lineHeight: 1.25 }}>
          {sb.home} <i style={{ color: 'var(--fg-dim)', fontStyle: 'normal' }}>vs</i> {sb.away}
          {sb.startTime && <span className="mono" style={{ fontSize: 13, color: 'var(--fg-muted)', marginLeft: 8, fontWeight: 500 }}>({formatStartTime(sb.startTime)})</span>}
        </div>
        <div className="mono" style={{
          fontSize: 11, color: 'var(--fg-muted)', marginTop: 4,
          display: 'flex', flexWrap: 'wrap', gap: '4px 6px', alignItems: 'center',
        }}>
          <span>{sb.league}</span>
          {sb.clock && <><s style={{ color: 'var(--fg-dim)', textDecoration: 'none' }}>•</s><span>⏱ {sb.clock}</span></>}
          {sb.score && <><s style={{ color: 'var(--fg-dim)', textDecoration: 'none' }}>•</s><em style={{ color: accent, fontStyle: 'normal', fontWeight: 600 }}>{sb.score}</em></>}
        </div>
      </div>

      <div style={{ height: 1, margin: '14px 0 8px', background: 'linear-gradient(90deg, transparent, rgba(150,190,255,0.22), transparent)' }} />

      {/* market label — bigger, left aligned with connector line going right */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '6px 0 2px', minWidth: 0 }}>
        <div className="mono" style={{
          fontSize: 16, fontWeight: 700, letterSpacing: '0.01em',
          color: accent,
          textShadow: `0 0 18px color-mix(in srgb, ${accent} 65%, transparent)`,
          minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
          flexShrink: 1,
        }}>{sb.market}{sb.line ? ` (${sb.line})` : ''}</div>
        <div style={{
          flexShrink: 0, width: 36, height: 1,
          background: `linear-gradient(90deg, color-mix(in srgb, ${accent} 50%, transparent), transparent)`,
          position: 'relative',
        }}>
          <div style={{
            position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)',
            width: 5, height: 5, borderRadius: '50%',
            background: accent,
            boxShadow: `0 0 8px ${accent}`,
          }} />
        </div>
      </div>

      {/* body — full-width tiles (picks now embedded in each tile) */}
      <div style={{ marginTop: 6 }}>
        <div style={{ position: 'relative', display: 'grid', gridTemplateColumns: `repeat(${Math.min((sb.selections || []).length, 3)}, 1fr)`, gap: 12 }}>
          {sb.selections.slice(0, 3).map((sel, i) => {
            // Match fallbackLink by bookmaker identity, not array index
            const bk = (sel.book || '').toLowerCase();
            const bk1 = (sb.bookmaker1 || '').toLowerCase();
            const bk2 = (sb.bookmaker2 || '').toLowerCase();
            const fb = bk && bk2 && (bk === bk2 || bk2.includes(bk) || bk.includes(bk2))
              ? sb.betLink2
              : bk && bk1 && (bk === bk1 || bk1.includes(bk) || bk.includes(bk1))
                ? sb.betLink1
                : i === 0 ? sb.betLink1 : sb.betLink2;
            return <OddsTile key={i} sel={sel} accent={accent} side={i === 0 ? 'left' : 'right'} fallbackLink={fb} />;
          })}
        </div>
      </div>

      <div className="mono" style={{
        marginTop: 14,
        marginTop: 11, paddingTop: 10,
        borderTop: '1px solid var(--bd)',
        fontSize: 11, color: 'var(--fg-muted)',
        display: 'flex', flexWrap: 'wrap', gap: '4px 10px', justifyContent: 'center',
      }}>
        <span>Stake: <b style={{ color: 'var(--fg)' }}>${(sb.totalStake || 0).toFixed(2)}</b></span>
        <span>Payout: <b style={{ color: 'var(--fg)' }}>${(sb.payout || 0).toFixed(2)}</b></span>
        <span>Profit: <b style={{ color: accent, fontWeight: 700 }}>${(sb.profit || 0).toFixed(2)}</b></span>
      </div>
    </div>
  );
}

// Memo: skip re-render when surebet content hasn't changed.
// Without this, every 5s socket update re-renders ALL 200+ cards, triggering
// expensive backdropFilter GPU compositing for every visible card on desktop.
const SurebetCardMemo = React.memo(SurebetCard, (prev, next) => {
  if (prev.sb.id !== next.sb.id) return false;
  if (prev.sb._encrypted !== next.sb._encrypted) return false;
  if (prev.sb.profit !== next.sb.profit) return false;
  const ps = prev.sb.selections, ns = next.sb.selections;
  if (!ps || !ns || ps.length !== ns.length) return false;
  for (let i = 0; i < ps.length; i++) {
    if (ps[i].odds !== ns[i].odds) return false;
  }
  return true; // props equal — skip re-render
});

Object.assign(window, { SurebetCard: SurebetCardMemo, BookmakerChip, WaveBars });
