// ── Shared Components ─────────────────────────────────────────
const { useState: useSh, useEffect: useEfSh, useRef: useRefSh } = React;
const C = window.SC;
const ELY_BLUE = '#70BAD2';

// Inject scrollbar-hide style for .filter-cats
(function() {
  const sharedStyle = document.createElement('style');
  sharedStyle.textContent = '.filter-cats::-webkit-scrollbar{display:none}';
  if (!document.getElementById('shared-style')) {
    sharedStyle.id = 'shared-style';
    document.head.appendChild(sharedStyle);
  }
})();
const BRAND_FONT = '"Helvetica Neue", Helvetica, Arial, sans-serif';

// "evently" with "ly" in brand blue
function EventlyText({ size, weight, style: extra }) {
  return (
    <span style={{ fontSize: size, fontWeight: weight, fontFamily: BRAND_FONT, letterSpacing: '-0.02em', ...extra }}>
      event<span style={{ color: ELY_BLUE }}>ly</span>
    </span>
  );
}

// Evently logo SVG (M+E heart)
function EventlyLogoSVG({ size = 24 }) {
  return (
    <svg width={size} height={size * (1056/976)} viewBox="0 0 976 1056" fill="none" style={{ display:'inline-block', flexShrink:0 }}>
      <path fillRule="evenodd" d="m340.5 452.98l-59 58.97c149.58 149.81 196.05 196.28 199.79 199.92l6.79 6.63 31.93-32-174.51-174.5 54.5-54.5 88.09 88 87.41-87.54 86.32 86.54c23.78-24.57 30.79-31.99 30.93-32.35 0.14-0.36-26.19-27.1-58.5-59.43l-58.76-58.78c-67.42 67.54-87.22 87.15-87.49 87.14-0.27 0-20.07-19.6-44-43.56-23.93-23.96-43.73-43.55-44-43.54-0.28 0.01-27.05 26.56-59.5 59z" fill="#fff"/>
      <path fillRule="evenodd" d="m531.99 559.5l-15.49 15.51c44.17 44.17 57.45 57 58 57.01 0.55 0 7.98-7.02 16.5-15.62l15.5-15.62c-44.95-44.06-58.23-56.83-58.51-56.81-0.28 0.01-7.48 7.01-16 15.53z" fill="#70BAD2"/>
    </svg>
  );
}

// MegaETH icon (M circle)
function MegaETHIcon({ size = 13 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 105 105" fill="none">
      <path d="M62.75 81.63C65.98 81.63 68.58 79.03 68.58 75.82C68.58 72.61 65.98 70.01 62.75 70.01C59.55 70.01 56.95 72.61 56.95 75.82C56.95 79.03 59.55 81.63 62.75 81.63Z" fill="currentColor"/>
      <path d="M41.86 81.81C45.09 81.81 47.67 79.2 47.67 75.99C47.67 72.78 45.09 70.18 41.86 70.18C38.67 70.18 36.06 72.78 36.06 75.99C36.06 79.2 38.67 81.81 41.86 81.81Z" fill="currentColor"/>
      <path d="M29.14 21.08H42.99C45.59 28.13 52.39 48.1 52.89 49.22C53.01 48.66 59.86 26.89 61.8 21.2H76.19V70.86C74.4 69.87 72.62 68.88 70.68 67.77C69.34 67.09 68.1 66.34 66.74 65.79C66.61 56.14 66.49 46.56 66.19 36.54C64.25 42.29 57.58 62.57 57.03 63.13H48.12C48.12 63.13 39.46 38.52 39.04 37.4C38.92 46.87 38.79 56.33 38.47 66.1C33.16 68.82 30.01 70.36 29.01 70.73V21.08H29.14Z" fill="currentColor"/>
      <path d="M52.51 8.35C76.81 8.35 96.66 28.14 96.66 52.5C96.66 76.86 76.88 96.65 52.51 96.65C28.14 96.65 8.36 76.86 8.36 52.5C8.36 28.14 28.14 8.35 52.51 8.35ZM52.51 0C23.5 0 0 23.5 0 52.5C0 81.5 23.5 105 52.51 105C81.5 105 105 81.5 105 52.5C105 23.5 81.5 0 52.51 0Z" fill="currentColor"/>
    </svg>
  );
}

// Market thumbnail
function MarketThumb({ market, size = 32, showBorder = false }) {
  const r = Math.round(size * 0.22);
  const bStyle = showBorder ? { border:'1px solid rgba(255,255,255,0.14)', boxSizing:'border-box' } : {};
  if (market.img) return (
    <div style={{width:size,height:size,borderRadius:r,flexShrink:0,overflow:'hidden',
      background:market.imgBg||'transparent',
      display:'flex',alignItems:'center',justifyContent:'center', ...bStyle}}>
      <img src={market.img} style={{
        width: market.imgBg ? '78%' : '100%',
        height: market.imgBg ? '78%' : '100%',
        objectFit:'contain', display:'block',
      }}/>
    </div>
  );
  return (
    <div style={{width:size,height:size,borderRadius:r,background:market.imgColor||'#2a2a2a',
      display:'flex',alignItems:'center',justifyContent:'center',flexShrink:0,
      color:market.imgTextColor||'#fff',fontSize:Math.round(size*0.28),fontWeight:800,
      letterSpacing:-0.5,fontFamily:BRAND_FONT, ...bStyle}}>
      {market.imgText||'?'}
    </div>
  );
}

// Circular donut progress
function CircleProgress({ pct, size = 52 }) {
  const r = (size - 6) / 2, cx = size/2, cy = size/2;
  const circ = 2 * Math.PI * r;
  const fill = circ * (pct / 100);
  const color = pct >= 50 ? C.green : pct >= 25 ? C.gold : C.red;
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} style={{transform:'rotate(-90deg)',flexShrink:0}}>
      <circle cx={cx} cy={cy} r={r} fill="none" stroke={C.border} strokeWidth={4}/>
      <circle cx={cx} cy={cy} r={r} fill="none" stroke={color} strokeWidth={4}
        strokeDasharray={`${fill} ${circ}`} strokeLinecap="round"/>
      <text x={cx} y={cy} textAnchor="middle" dominantBaseline="central"
        fill={color} fontSize={size*0.22} fontWeight={700} fontFamily={BRAND_FONT}
        style={{transform:`rotate(90deg)`,transformOrigin:`${cx}px ${cy}px`}}>
        {pct}%
      </text>
    </svg>
  );
}

// Shared wallet value — synced across navbar and wallet view
window.ELY_WALLET_VAL  = window.ELY_WALLET_VAL  || 21543;
window.ELY_POINTS_VAL  = window.ELY_POINTS_VAL  || 5542762;
window.ELY_VOL_VAL     = window.ELY_VOL_VAL     || 2318400;   // cents → display as $X.XM
window.ELY_TRADES_VAL  = window.ELY_TRADES_VAL  || 14832;
window.ELY_MARKETS_VAL = window.ELY_MARKETS_VAL || 847;

// Start global tickers
if (!window._elyTickerStarted) {
  window._elyTickerStarted = true;
  setInterval(() => {
    window.ELY_WALLET_VAL  += Math.round((Math.random() - 0.47) * 15);
    window.ELY_POINTS_VAL  += Math.round((Math.random() - 0.45) * 20);
    window.ELY_VOL_VAL     += Math.round(Math.random() * 1200 + 200);
    window.ELY_TRADES_VAL  += Math.floor(Math.random() * 4 + 1);
    if (Math.random() < 0.12) window.ELY_MARKETS_VAL += 1;
    window.dispatchEvent(new Event('ely-tick'));
  }, 2200);
}

function useElyTicker() {
  const [w,  setW]  = useSh(window.ELY_WALLET_VAL);
  const [p,  setP]  = useSh(window.ELY_POINTS_VAL);
  const [v,  setV]  = useSh(window.ELY_VOL_VAL);
  const [tr, setTr] = useSh(window.ELY_TRADES_VAL);
  const [mk, setMk] = useSh(window.ELY_MARKETS_VAL);
  useEfSh(() => {
    const fn = () => {
      setW(window.ELY_WALLET_VAL);
      setP(window.ELY_POINTS_VAL);
      setV(window.ELY_VOL_VAL);
      setTr(window.ELY_TRADES_VAL);
      setMk(window.ELY_MARKETS_VAL);
    };
    window.addEventListener('ely-tick', fn);
    return () => window.removeEventListener('ely-tick', fn);
  }, []);
  return { wallet: w, points: p, vol: v, trades: tr, markets: mk };
}

// App top navbar
function AppNavbar({ appTab, setAppTab, isDark, isMobile }) {
  // Mettre à jour les couleurs selon le mode
  if (isDark) {
    window.SC = window.SC_DARK;
  } else {
    window.SC = window.SC_LIGHT;
  }

  const { wallet, points } = useElyTicker();
  const navItems = isMobile ? ['Markets'] : ['Markets','Bridge','Swap'];

  return (
    <div style={{background:C.appBg,borderBottom:`1px solid ${C.border}`,padding: isMobile ? '0 10px' : '0 14px',
      height:42,display:'flex',alignItems:'center',gap:0,flexShrink:0,zIndex:10,overflow:'hidden'}}>
      {/* Logo */}
      <div style={{display:'flex',alignItems:'center',gap:3,marginRight: isMobile ? 10 : 18,cursor:'pointer',flexShrink:0}}
        onClick={()=>setAppTab('markets')}>
        <EventlyLogoSVG size={isMobile ? 20 : 26}/>
        {!isMobile && <EventlyText size={14} weight={700}/>}
      </div>
      {/* Nav links */}
      <div style={{display:'flex',alignItems:'center',gap:0}}>
        {navItems.map(item => (
          <button key={item}
            onClick={()=>item==='Markets'?setAppTab('markets'):(window.parent||window).location.href='/'+item.toLowerCase()}
            style={{background:(appTab==='markets'&&item==='Markets')?'rgba(255,255,255,0.08)':'none',
              border:'none',color:(appTab==='markets'&&item==='Markets')?'#fff':C.muted,
              fontSize: isMobile ? 11 : 12,fontWeight:(appTab==='markets'&&item==='Markets')?600:400,
              padding: isMobile ? '0 8px' : '0 10px',cursor:'pointer',fontFamily:BRAND_FONT,
              borderRadius:5,height:28,whiteSpace:'nowrap'}}>
            {item}
          </button>
        ))}
      </div>
      {/* Right side */}
      <div style={{marginLeft:'auto',display:'flex',alignItems:'center',gap: isMobile ? 6 : 10,flexShrink:0}}>
        {/* Wallet */}
        <div style={{display:'flex',flexDirection:'column',alignItems:'flex-end'}}>
          <span style={{fontSize:8,color:C.muted,textTransform:'uppercase',letterSpacing:0.5,fontFamily:BRAND_FONT}}>Wallet</span>
          <span style={{color:C.green,fontSize:10,fontWeight:700,fontFamily:'JetBrains Mono,Courier New,monospace'}}>
            ${wallet.toLocaleString()}
          </span>
        </div>
        {!isMobile && <><div style={{width:1,height:22,background:C.border}}/>
        {/* Analytics badge */}
        <span style={{fontSize:8,color:C.muted,background:'rgba(255,255,255,0.05)',border:`1px solid ${C.border}`,
          borderRadius:4,padding:'2px 6px',fontFamily:BRAND_FONT,letterSpacing:0.5,fontWeight:600,
          textTransform:'uppercase',userSelect:'none'}}>Analytics</span>
        <div style={{width:1,height:22,background:C.border}}/>
        {/* ELY Points */}
        <div style={{display:'flex',flexDirection:'column',alignItems:'flex-end'}}>
          <span style={{fontSize:8,color:C.muted,textTransform:'uppercase',letterSpacing:0.5,fontFamily:BRAND_FONT}}>
            <span style={{color:ELY_BLUE,fontWeight:700}}>ELY</span> Points
          </span>
          <span style={{color:'#fff',fontSize:10,fontWeight:700,fontFamily:'JetBrains Mono,Courier New,monospace'}}>
            {points.toLocaleString()}
          </span>
        </div>
        <div style={{width:1,height:22,background:C.border}}/></>}
        {/* MegaPill gold — slim & elegant */}
        <span className="mega-pill" style={{display:'inline-flex',alignItems:'center',gap:5,
          padding: isMobile ? '3px 6px 3px 8px' : '4px 6px 4px 10px',borderRadius:100,cursor:'default',userSelect:'none',flexShrink:0}}>
          <MegaETHIcon size={isMobile ? 12 : 13}/>
          <span className="mega-pill-text" style={{fontSize: isMobile ? 11 : 12,fontWeight:600,letterSpacing:'-0.01em',
            fontFamily:BRAND_FONT,whiteSpace:'nowrap'}}>
            anon.mega
          </span>
          <img src="images/pfp.png" style={{width: isMobile ? 20 : 22,height: isMobile ? 20 : 22,borderRadius:'50%',objectFit:'cover',
            border:'1px solid rgba(160,100,40,0.4)',flexShrink:0}}/>
        </span>
      </div>
    </div>
  );
}

// Markets filter bar — redesigned with scrollable category pills
function AppFilterBar({ catFilter, setCatFilter, isDark }) {
  const cats = ['Crypto','Politics','Sports','Tech','Economy','Culture','Other'];
  const [selCat, setSelCat] = useSh(null);

  const IconHot = () => (
    <svg width={11} height={11} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
      <path d="M8.5 14.5A2.5 2.5 0 0011 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 01-7 7c-1.657 0-3-.5-4.03-1.22A5 5 0 008.5 14.5z"/>
    </svg>
  );

  const IconHeart = () => (
    <svg width={11} height={11} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
      <path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/>
    </svg>
  );

  const IconUsers = () => (
    <svg width={11} height={11} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
      <path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx={9} cy={7} r={4}/>
      <path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/>
    </svg>
  );

  const IconStar = () => (
    <svg width={11} height={11} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
      <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>
    </svg>
  );

  return (
    <div style={{background:C.appBg, borderBottom:`1px solid ${C.border}`, padding:'0 0 0 0',
      height:34, display:'flex', alignItems:'center', flexShrink:0, overflow:'hidden'}}>

      {/* Left: view tabs — fixed */}
      <div style={{display:'flex', gap:0, flexShrink:0, paddingLeft: 10}}>
        {[
          {icon:<IconHot/>, label:'Trending', active:true},
          {icon:<IconHeart/>, label:'Favorites'},
          {icon:<IconUsers/>, label:'Community'},
          {icon:<IconStar/>, label:'New'},
        ].map(t => (
          <div key={t.label} style={{
            borderBottom: t.active ? '2px solid #fff' : '2px solid transparent',
            color: t.active ? '#fff' : C.muted, fontSize: 10, padding: '0 8px',
            fontFamily: BRAND_FONT, fontWeight: t.active ? 600 : 400,
            display:'flex', alignItems:'center', gap:3, height:34,
            whiteSpace:'nowrap', cursor:'default', userSelect:'none'
          }}>
            <span style={{color: t.active ? '#fff' : C.muted}}>{t.icon}</span>
            {t.label}
          </div>
        ))}
      </div>

      {/* Divider */}
      <div style={{width:1, height:14, background:C.border, marginLeft:6, flexShrink:0}}/>

      {/* Right: scrollable category pills */}
      <div className="filter-cats" style={{
        display:'flex', gap:4, overflowX:'auto', padding:'0 8px',
        scrollbarWidth:'none', msOverflowStyle:'none', flex:1
      }}>
        {cats.map(cat => (
          <div key={cat}
            onClick={() => setSelCat(selCat === cat ? null : cat)}
            style={{
              background: selCat === cat ? 'rgba(92,200,230,0.15)' : 'none',
              color: selCat === cat ? '#5CC8E6' : C.muted,
              border: selCat === cat ? '1px solid rgba(92,200,230,0.35)' : '1px solid transparent',
              fontSize: 10, padding: '2px 8px', borderRadius: 4,
              fontFamily: BRAND_FONT, fontWeight: selCat === cat ? 600 : 400,
              whiteSpace:'nowrap', cursor:'pointer', userSelect:'none',
              transition: 'all 0.15s ease', flexShrink:0
            }}>
            {cat}
          </div>
        ))}
      </div>
    </div>
  );
}

// Bottom bar — icons only with proper SVGs
function AppBottomBar({ isDark }) {
  const [dark, setDark] = useSh(isDark !== undefined ? isDark : true);
  const textColor = dark ? 'rgba(255,255,255,0.38)' : 'rgba(0,0,0,0.38)';
  const textColorHov = dark ? 'rgba(255,255,255,0.85)' : 'rgba(0,0,0,0.85)';
  const ic = { background:'none', border:'none', color:textColor, cursor:'pointer',
    padding:'0 6px', display:'flex', alignItems:'center', justifyContent:'center',
    height:28, transition:'color .15s', flexShrink:0 };
  const hov = (e) => e.currentTarget.style.color=textColorHov;
  const lev = (e) => e.currentTarget.style.color=textColor;

  const { vol, trades, markets } = useElyTicker();
  const fmtVol = (v) => { const m = v / 1000000; return m >= 1 ? `$${m.toFixed(2)}M` : `$${Math.round(v/1000)}K`; };
  const statStyle = {fontSize:8.5,fontFamily:'JetBrains Mono,Courier New,monospace',letterSpacing:0.2,whiteSpace:'nowrap'};
  const valStyle  = {color:C.green,fontWeight:700,transition:'color 0.2s'};

  return (
    <div style={{background:C.appBg,borderTop:`1px solid ${C.border}`,height:28,display:'flex',
      alignItems:'center',justifyContent:'space-between',padding:'0 14px',flexShrink:0,gap:2}}>
      {/* Daily stats */}
      <div style={{display:'flex',alignItems:'center',gap:10,flexShrink:0}}>
        <span style={{...statStyle,color:C.muted}}>VOL 24H <span style={valStyle}>{fmtVol(vol)}</span></span>
        <span style={{...statStyle,color:C.muted}}>TRADES <span style={valStyle}>{trades.toLocaleString()}</span></span>
        <span style={{...statStyle,color:C.muted}}>MARKETS <span style={valStyle}>{markets.toLocaleString()}</span></span>
      </div>
      {/* Moon/Sun */}
      <button title="Toggle theme" style={ic} onMouseEnter={hov} onMouseLeave={lev} onClick={()=>setDark(d=>!d)}>
        {dark
          ? <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2}><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
          : <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2}><circle cx={12} cy={12} r={5}/><line x1={12} y1={1} x2={12} y2={3}/><line x1={12} y1={21} x2={12} y2={23}/><line x1={4.22} y1={4.22} x2={5.64} y2={5.64}/><line x1={18.36} y1={18.36} x2={19.78} y2={19.78}/><line x1={1} y1={12} x2={3} y2={12}/><line x1={21} y1={12} x2={23} y2={12}/></svg>}
      </button>
      {/* Docs */}
      <a href="https://docs.evently.market" target="_blank" rel="noopener noreferrer" title="Docs" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
          <path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><path d="M14 2v6h6M16 13H8M16 17H8M10 9H8"/>
        </svg>
      </a>
      {/* Support / Discord */}
      <a href="https://discord.gg/axCYVSPKzc" target="_blank" rel="noopener noreferrer" title="Support" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
          <path d="M3 18v-6a9 9 0 0 1 18 0v6"/>
          <path d="M21 19a2 2 0 0 1-2 2h-1a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h3zM3 19a2 2 0 0 0 2 2h1a2 2 0 0 0 2-2v-3a2 2 0 0 0-2-2H3z"/>
        </svg>
      </a>
      {/* X */}
      <a href="https://twitter.com/eventlymarket" target="_blank" rel="noopener noreferrer" title="X / Twitter" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={11} height={11} viewBox="0 0 24 24" fill="currentColor"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
      </a>
      {/* Discord */}
      <a href="https://discord.gg/axCYVSPKzc" target="_blank" rel="noopener noreferrer" title="Discord" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="currentColor"><path d="M20.317 4.37a19.791 19.791 0 0 0-4.885-1.515.074.074 0 0 0-.079.037c-.21.375-.444.864-.608 1.25a18.27 18.27 0 0 0-5.487 0 12.64 12.64 0 0 0-.617-1.25.077.077 0 0 0-.079-.037A19.736 19.736 0 0 0 3.677 4.37a.07.07 0 0 0-.032.027C.533 9.046-.32 13.58.099 18.057a.082.082 0 0 0 .031.057 19.9 19.9 0 0 0 5.993 3.03.078.078 0 0 0 .084-.028c.462-.63.874-1.295 1.226-1.994a.076.076 0 0 0-.041-.106 13.107 13.107 0 0 1-1.872-.892.077.077 0 0 1-.008-.128c.126-.094.246-.198.373-.292a.074.074 0 0 1 .077-.01c3.928 1.793 8.18 1.793 12.062 0a.074.074 0 0 1 .078.01c.12.098.246.198.373.292a.077.077 0 0 1-.006.127 12.299 12.299 0 0 1-1.873.892.077.077 0 0 0-.041.107c.36.698.772 1.362 1.225 1.993a.076.076 0 0 0 .084.028 19.839 19.839 0 0 0 6.002-3.03.077.077 0 0 0 .032-.054c.5-5.177-.838-9.674-3.549-13.66a.061.061 0 0 0-.031-.03zM8.02 15.33c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.956-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.956 2.418-2.157 2.418zm7.975 0c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.955-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.946 2.418-2.157 2.418z"/></svg>
      </a>
      {/* DeBank */}
      <a href="https://debank.com/official/120126/stream" target="_blank" rel="noopener noreferrer" title="DeBank" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={13} height={13} viewBox="0 0 280 280" fill="none">
          <g transform="translate(51,39)">
            <path d="M177.124,140.948C177.124,174.309 149.674,201.354 115.812,201.354L0,201.354L0,161.083L115.812,161.083C127.099,161.083 136.249,152.068 136.249,140.948C136.249,129.827 127.099,120.812 115.812,120.812L74.937,120.812L74.937,80.541L115.812,80.541C127.099,80.541 136.249,71.527 136.249,60.406C136.249,49.286 127.099,40.271 115.812,40.271L0,40.271L0,0L115.812,0C149.674,0 177.124,27.045 177.124,60.406C177.124,75.877 171.221,89.99 161.512,100.677C171.221,111.364 177.124,125.476 177.124,140.948Z" fill="currentColor"/>
            <path d="M0,0C56.436,0 102.187,45.075 102.187,100.677C102.187,156.279 56.436,201.354 0,201.354L0,161.083C33.862,161.083 61.312,134.038 61.312,100.677C61.312,67.315 33.862,40.271 0,40.271L0,0Z" fill="currentColor" opacity="0.65"/>
          </g>
        </svg>
      </a>
      {/* DeFiLlama */}
      <a href="https://defillama.com/protocol/evently" target="_blank" rel="noopener noreferrer" title="DeFiLlama" style={{...ic,textDecoration:'none'}} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
          <path d="M3 3v18h18"/><path d="M18 9l-5 5-3-3-4 4"/>
        </svg>
      </a>
      {/* Settings */}
      <button title="Settings" style={ic} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
          <circle cx={12} cy={12} r={3}/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
        </svg>
      </button>
      {/* Sounds */}
      <button title="Sounds" style={ic} onMouseEnter={hov} onMouseLeave={lev}>
        <svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round">
          <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5"/><path d="M19.07 4.93a10 10 0 0 1 0 14.14M15.54 8.46a5 5 0 0 1 0 7.07"/>
        </svg>
      </button>
    </div>
  );
}

// ── Global constants used in markets/trading files ──
window.BRAND_FONT = "'Plus Jakarta Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif";

window.ACTIVITY_NAMES = [
  'alphatrader.mega', '0x8b2…11', 'nightowl.mega', '0x91c…aa',
  'republic.mega', 'degen42.mega', '0x3f7…cc', 'kaleidoscope.mega',
  'anon.mega', 'moonshot.mega', '0x55e…f0', 'cryptowizard.mega',
];
window.ACTIVITY_ACTIONS = ['bought YES', 'bought NO', 'sold YES', 'sold NO'];
window.ACTIVITY_MARKETS = ['Bad Bunnz', 'Nacci Cartel', 'Hyperliquid', 'WLFI Subpoena', 'Fed Rate Cuts', 'Brent Oil', 'China GDP', 'House 2026', 'Megalio'];

// ── Market probability state (drifts over time) ──
window.MARKET_PROB = {};
(window.SM || []).forEach(m => {
  if (m.type === 'multi' && m.outcomes) {
    window.MARKET_PROB[m.id] = { outcomes: m.outcomes.map(o => o.pct) };
  } else if (m.type === 'binary') {
    window.MARKET_PROB[m.id] = { pct: m.pct };
  }
});

if (!window._mktDriftStarted) {
  window._mktDriftStarted = true;
  setInterval(() => {
    (window.SM || []).forEach(m => {
      const prob = window.MARKET_PROB[m.id];
      if (!prob) return;
      if (m.type === 'multi' && prob.outcomes) {
        prob.outcomes = prob.outcomes.map(p =>
          Math.max(1, Math.min(99, +(p + (Math.random() - 0.5) * 0.8).toFixed(1)))
        );
      } else if (m.type === 'binary') {
        prob.pct = Math.max(2, Math.min(98, +(prob.pct + (Math.random() - 0.5) * 0.8).toFixed(1)));
      }
    });
    window.dispatchEvent(new Event('mkt-drift'));
  }, 1800);
}

function useMarketDrift() {
  const [, setTick] = useSh(0);
  useEfSh(() => {
    const fn = () => setTick(n => n + 1);
    window.addEventListener('mkt-drift', fn);
    return () => window.removeEventListener('mkt-drift', fn);
  }, []);
}

function useAnimatedNumber(value, duration) {
  const dur = duration || 300;
  const [display, setDisplay] = useSh(value);
  const rafRef = useRefSh(null);

  useEfSh(() => {
    const from = display;
    const to = value;
    if (from === to) return;
    const start = performance.now();
    const tick = (now) => {
      const t = Math.min(1, (now - start) / dur);
      const eased = 1 - Math.pow(1 - t, 3);
      setDisplay(from + (to - from) * eased);
      if (t < 1) rafRef.current = requestAnimationFrame(tick);
    };
    rafRef.current = requestAnimationFrame(tick);
    return () => { if (rafRef.current) cancelAnimationFrame(rafRef.current); };
  }, [value]);

  return display;
}

Object.assign(window, {
  EventlyText, EventlyLogoSVG, MegaETHIcon,
  MarketThumb, CircleProgress,
  AppNavbar, AppFilterBar, AppBottomBar,
  useElyTicker, useMarketDrift, useAnimatedNumber,
});
