// ============================================================
// HUNGRY HOLE 3D — power-up icons + simulated rewarded-Ad modal
// Ad-only economy: every unlock/refill is "watch a rewarded ad".
// Exposes: PowerIcon, AdModal, AdButton
// ============================================================
const { useState: useStatePU, useEffect: useEffectPU, useRef: useRefPU } = React;

// Every power-up has its OWN glyph so the tray reads at a glance (no more 5 power-ups
// sharing one bolt). One cohesive rounded style: 2px stroke, round caps; filled accents
// use fill={color}. Legacy keys (bolt/bomb/clock/grow/magnet/shield/slow) stay as aliases
// for the decorative hardcoded usages around the menus.
function PowerIcon({ id, size = 24, color = 'currentColor' }) {
  const aiPower = {
    magnet: 'magnet-3d',
    magnetx: 'magnet-3d',
    time: 'time-3d',
    clock: 'time-3d',
    slow: 'time-3d',
    blast: 'blast-3d',
    bomb: 'blast-3d',
    grow: 'grow-3d',
    shield: 'shield-3d',
    aegis: 'shield-3d',
    frenzy: 'frenzy-3d',
    tornado: 'grow-3d',
  }[id];
  if (aiPower) {
    return <img src={`assets/ui-ai/icon-${aiPower}.webp`} alt="" style={{ width: size, height: size, objectFit: 'contain', display: 'block',
      filter: 'drop-shadow(0 2px 3px rgba(0,0,0,0.28))' }} />;
  }
  const p = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: color, strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round' };
  switch (id) {
    /* ---- core ---- */
    case 'magnet': return <svg {...p}><path d="M7.2 14.2V9.4a4.8 4.8 0 019.6 0v4.8" fill="none" stroke={color} strokeWidth="3.6" strokeLinecap="butt"/><rect x="5.4" y="13.8" width="3.6" height="3.3" rx="0.8" fill="#EDEFF4" stroke="none"/><rect x="15" y="13.8" width="3.6" height="3.3" rx="0.8" fill="#EDEFF4" stroke="none"/></svg>;
    case 'magnetx': return <svg {...p}><path d="M6.6 13.6V9.2a4.6 4.6 0 019.2 0v4.4" fill="none" stroke={color} strokeWidth="3.4" strokeLinecap="butt"/><rect x="4.9" y="13.2" width="3.4" height="3.1" rx="0.8" fill="#EDEFF4" stroke="none"/><rect x="14.1" y="13.2" width="3.4" height="3.1" rx="0.8" fill="#EDEFF4" stroke="none"/><path d="M19 4v3.6M17.2 5.8h3.6" strokeWidth="2.1"/></svg>;
    case 'time': return <svg {...p}><circle cx="11" cy="13" r="7.4"/><path d="M11 9v4l2.6 1.6"/><path d="M19 3v3.4M17.3 4.7h3.4" strokeWidth="2.1"/></svg>;
    case 'clock': return <svg {...p}><circle cx="12" cy="12" r="8.5"/><path d="M12 7.5V12l3 2"/></svg>;
    case 'slow': return <svg {...p}><path d="M7 3h10M7 21h10M8.2 3c0 4.4 3 5.4 3.8 8.8C12.8 8.4 15.8 7.4 15.8 3M8.2 21c0-4.4 3-5.4 3.8-8.8.8 3.4 3.8 4.4 3.8 8.8"/></svg>;
    case 'frenzy': return <svg {...p}><path d="M10 2L5 12h3l-1 8 5-10H9z" fill={color} stroke="none"/><path d="M18.5 4l-4 8h2.4l-1 6 4-8h-2.4z" fill={color} stroke="none" opacity="0.82"/></svg>;
    case 'blast': case 'bomb': return <svg {...p}><circle cx="11" cy="14" r="7"/><path d="M16 9l3-3M18 5l1.5.5M20 7l.5 1.5"/></svg>;
    case 'grow': return <svg {...p}><circle cx="12" cy="12" r="4"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3M5 5l2 2M17 17l2 2M19 5l-2 2M7 17l-2 2"/></svg>;
    case 'shield': return <svg {...p}><path d="M12 3l7 3v5c0 5-3 8-7 10-4-2-7-5-7-10V6z"/><path d="M9 12l2 2 4-4"/></svg>;
    case 'aegis': return <svg {...p}><path d="M12 3l7 3v5c0 5-3 8-7 10-4-2-7-5-7-10V6z"/><path d="M12 7.8l1.25 2.6 2.85.3-2.1 2 .55 2.8L12 16.2l-2.55 1.3.55-2.8-2.1-2 2.85-.3z" fill={color} stroke="none"/></svg>;
    case 'bolt': return <svg {...p}><path d="M13 2L4 14h7l-1 8 9-12h-7z" fill={color} stroke="none"/></svg>;
    case 'ad': return <svg {...p}><rect x="3" y="5" width="18" height="14" rx="3"/><path d="M10.5 9.5l4 2.5-4 2.5z" fill={color} stroke="none"/></svg>;
    case 'play': return <svg {...p}><path d="M8 5.5l11 6.5-11 6.5z" fill={color} stroke="none"/></svg>;
    /* ---- big/rivals ---- */
    case 'tornado': return <svg {...p}><path d="M4 5h16M6 9h12M8.5 13h7M10.5 16.5h4.2M12.4 20h1.4"/></svg>;
    case 'titan': return <svg {...p}><circle cx="12" cy="12" r="9"/><path d="M12 16.5V8.2M8.4 11.8L12 8.2l3.6 3.6"/></svg>;
    case 'colossus': return <svg {...p}><path d="M5 13l7-6 7 6M5 18.5l7-6 7 6"/></svg>;
    case 'vortex': return <svg {...p}><path d="M4.5 4.5L8 8M4.5 4.5v3.2M4.5 4.5h3.2"/><path d="M19.5 4.5L16 8M19.5 4.5v3.2M19.5 4.5h-3.2"/><path d="M4.5 19.5L8 16M4.5 19.5v-3.2M4.5 19.5h3.2"/><path d="M19.5 19.5L16 16M19.5 19.5v-3.2M19.5 19.5h-3.2"/><circle cx="12" cy="12" r="1.9" fill={color} stroke="none"/></svg>;
    case 'blackhole': return <svg {...p}><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5.4" opacity="0.65"/><circle cx="12" cy="12" r="2.5" fill={color} stroke="none"/></svg>;
    case 'phase': return <svg {...p}><path d="M5 21V10a7 7 0 0114 0v11l-2.3-2-2.3 2-2.4-2-2.3 2-2.4-2z"/><circle cx="9.6" cy="10.6" r="1.2" fill={color} stroke="none"/><circle cx="14.4" cy="10.6" r="1.2" fill={color} stroke="none"/></svg>;
    case 'dash': return <svg {...p}><path d="M4 6l6 6-6 6M12 6l6 6-6 6"/></svg>;
    case 'goldrush': return <svg {...p}><circle cx="12" cy="12" r="9"/><text x="12" y="16.3" textAnchor="middle" fontFamily="Fredoka,sans-serif" fontWeight="800" fontSize="12" fill={color} stroke="none">$</text></svg>;
    case 'coinrush': return <svg {...p}><circle cx="13" cy="12" r="6.6"/><text x="13" y="15.6" textAnchor="middle" fontFamily="Fredoka,sans-serif" fontWeight="800" fontSize="9" fill={color} stroke="none">$</text><path d="M2 9h3.6M1.6 12h3.4M2 15h3.6"/></svg>;
    case 'scout': return <svg {...p}><path d="M2.6 12s3.7-6.4 9.4-6.4S21.4 12 21.4 12 17.7 18.4 12 18.4 2.6 12 2.6 12z"/><circle cx="12" cy="12" r="3"/></svg>;
    case 'gift': return <svg {...p}><rect x="4" y="9" width="16" height="11" rx="2.4"/><path d="M4 13h16M12 9v11M12 9S9 4.6 7.2 6.2 12 9 12 9zM12 9s3-4.4 4.8-2.8S12 9 12 9z"/></svg>;
    case 'target': return <svg {...p}><circle cx="12" cy="12" r="8.6"/><circle cx="12" cy="12" r="4.2"/><circle cx="12" cy="12" r="1.4" fill={color} stroke="none"/></svg>;
    case 'crown': return <svg {...p}><path d="M3 18.4l-1.2-9.2 5.2 4 5-8.2 5 8.2 5.2-4-1.2 9.2z" fill={color} stroke="none"/><path d="M4.2 19.5h15.6"/><circle cx="7" cy="14.2" r="1" fill="#fff" stroke="none" opacity=".9"/><circle cx="12" cy="12.3" r="1.1" fill="#fff" stroke="none" opacity=".95"/><circle cx="17" cy="14.2" r="1" fill="#fff" stroke="none" opacity=".9"/></svg>;
    case 'levels': return <svg {...p}><path d="M4 18c4-7 8 1 12-6 1.2-2.1 2.5-3.2 4-3.8" strokeDasharray="0.2 3.4"/><circle cx="4" cy="18" r="2.2" fill={color} stroke="none"/><circle cx="20" cy="7.6" r="2.2" fill={color} stroke="none"/></svg>;
    case 'hole': return <svg {...p}><ellipse cx="12" cy="13" rx="9" ry="6" fill="rgba(255,255,255,0.28)" stroke={color}/><ellipse cx="12" cy="13" rx="4.4" ry="2.8" fill={color} stroke="none"/></svg>;
    case 'home': return <svg {...p}><path d="M3.5 11.2L12 4l8.5 7.2"/><path d="M5.5 10.5v9h13v-9"/><rect x="10" y="13.4" width="4" height="6" rx="1" fill={color} stroke="none"/></svg>;
    /* ---- city / weather ---- */
    case 'quake': return <svg {...p}><path d="M2 12h3l2-6 3 12 3-15 2.6 13 2-4H22"/></svg>;
    case 'freeze': return <svg {...p}><path d="M12 2.5v19M4 7l16 10M20 7L4 17"/><path d="M12 6.4l-2.1-2M12 6.4l2.1-2M12 17.6l-2.1 2M12 17.6l2.1 2M6.8 9.2L4 8.4M6.8 9.2L6 6.4M17.2 14.8l2.8.8M17.2 14.8l.8 2.8M17.2 9.2L18 6.4M17.2 9.2l2.8-.8M6.8 14.8L6 17.6M6.8 14.8L4 15.6" strokeWidth="1.5"/></svg>;
    case 'storm': return <svg {...p}><path d="M3 8h10a2.8 2.8 0 10-2.8-2.8"/><path d="M3 12.4h14a2.8 2.8 0 11-2.8 2.8"/><path d="M3 16.8h8a2.4 2.4 0 11-2.4 2.4"/></svg>;
    default: return <svg {...p}><circle cx="12" cy="12" r="8"/></svg>;
  }
}

function GameIcon({ id = 'bolt', size = 48, tone = 'sky', color, label }) {
  const aiIcon = {
    bolt: 'power',
    power: 'power',
    gift: 'daily',
    daily: 'daily',
    target: 'missions',
    missions: 'missions',
    crown: 'rank',
    rank: 'rank',
    levels: 'levels',
    home: 'home',
    hole: 'holes',
    holes: 'holes',
    play: 'play',
  }[id];
  if (aiIcon) {
    return <span className="hh-gameicon hh-ai-icon" aria-hidden={!label} aria-label={label} style={{
      width: size, height: size, display: 'grid', placeItems: 'center', flexShrink: 0, overflow: 'visible',
      background: 'transparent', border: 'none', boxShadow: 'none', borderRadius: 0
    }}>
      <img src={`assets/ui-ai/icon-${aiIcon}.webp`} alt="" style={{
        width: '112%', height: '112%', objectFit: 'contain', display: 'block',
        filter: 'drop-shadow(0 4px 4px rgba(0,0,0,0.34)) drop-shadow(0 10px 14px rgba(0,0,0,0.20))'
      }} />
    </span>;
  }
  const tones = {
    sky: ['#72D7FF', '#2087EF', '#1262B7'],
    grape: ['#B59BFF', '#7654F2', '#5532C8'],
    sun: ['#FFE58A', '#FFC23D', '#B97108'],
    mint: ['#69EDCC', '#20C79E', '#0B795F'],
    coral: ['#FF9B9B', '#F05258', '#B92C38'],
    dark: ['#506684', '#243653', '#111B2F'],
  };
  const pal = color ? [color, color, color] : (tones[tone] || tones.sky);
  const rim = pal[2];
  return <span className="hh-gameicon" aria-hidden={!label} aria-label={label} style={{
    width: size, height: size, borderRadius: Math.max(14, Math.round(size * 0.32)), position: 'relative',
    display: 'grid', placeItems: 'center', flexShrink: 0, overflow: 'hidden',
    background: `linear-gradient(180deg,${pal[0]} 0%,${pal[1]} 62%,${pal[2]} 100%)`,
    color: '#fff', transform: 'translateZ(0)',
    boxShadow: `0 ${Math.max(4, size * 0.12)}px 0 ${rim}, 0 ${Math.max(8, size * 0.22)}px ${Math.max(13, size * 0.35)}px ${pal[1]}66, inset 0 2px 0 rgba(255,255,255,0.62), inset 0 -5px 9px rgba(0,0,0,0.20)`,
    border: '1px solid rgba(255,255,255,0.66)'
  }}>
    <span aria-hidden style={{ position: 'absolute', inset: '0 0 46% 0', background: 'linear-gradient(180deg,rgba(255,255,255,0.62),rgba(255,255,255,0.08))' }} />
    <span aria-hidden style={{ position: 'absolute', top: size * 0.14, left: size * 0.16, width: size * 0.34, height: size * 0.18, borderRadius: '50%', background: 'rgba(255,255,255,0.34)', transform: 'rotate(-24deg)' }} />
    <span style={{ position: 'relative', display: 'grid', placeItems: 'center',
      filter: 'drop-shadow(0 2px 0 rgba(0,0,0,0.24)) drop-shadow(0 5px 8px rgba(0,0,0,0.22))' }}>
      <PowerIcon id={id} size={Math.round(size * 0.56)} color="#fff" />
    </span>
  </span>;
}

// simulated rewarded ad: countdown → reward claim. onClaim(true) when watched.
function AdModal({ title = 'Rewarded Ad', rewardLabel = 'Reward', onClaim, onClose, accent = '#FFB627' }) {
  const [t, setT] = useStatePU(5);
  const [done, setDone] = useStatePU(false);
  const firedRef = useRefPU(false);
  const nativeAd = !!(window.HHAds && window.HHAds.available);
  // On device: show the REAL rewarded ad. onClaim fires only if the reward is earned.
  useEffectPU(() => {
    if (!nativeAd || firedRef.current) return;
    firedRef.current = true;
    window.HHAds.showRewarded(
      () => { onClaim && onClaim(); },
      () => { onClose && onClose(); }
    );
  }, []);
  useEffectPU(() => {
    if (nativeAd || done) return;
    const iv = setInterval(() => setT(v => { if (v <= 1) { clearInterval(iv); setDone(true); return 0; } return v - 1; }), 1000);
    return () => clearInterval(iv);
  }, [done]);
  const C = window.HH.C;
  const pct = (5 - t) / 5; // 0 → 1 across the countdown
  // Native real-ad path: the ad auto-shows the moment it's loaded (ads.js waits
  // for us) — meanwhile a friendly frosted veil holds the screen (hole.io candy look).
  if (nativeAd) {
    return (
      <div style={{ position: 'absolute', inset: 0, zIndex: 120, background: 'rgba(12,18,34,0.93)', backdropFilter: 'blur(8px)', WebkitBackdropFilter: 'blur(8px)',
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 18, padding: 24 }}>
        <div style={{ position: 'relative', width: 96, height: 96, display: 'grid', placeItems: 'center' }}>
          <div style={{ position: 'absolute', inset: 0, borderRadius: '50%', border: '5px solid rgba(255,255,255,0.14)',
            borderTopColor: accent, animation: 'hh-spin 0.9s linear infinite' }} />
          <div style={{ animation: 'hh-bob 2.4s ease-in-out infinite' }}><HoleVisual r={26} skin="rainbow" /></div>
        </div>
        <div className="disp hh-outline-sm" style={{ fontSize: 19 }}>Preparing your reward…</div>
        <div className="chip" style={{ gap: 7, background: 'rgba(255,255,255,0.12)', border: '1px solid rgba(255,255,255,0.18)', color: '#fff' }}>
          <PowerIcon id="ad" size={15} color={accent} />{rewardLabel}</div>
      </div>
    );
  }
  return (
    <div style={{ position: 'absolute', inset: 0, zIndex: 120, background: 'rgba(12,18,34,0.80)', backdropFilter: 'blur(9px)', WebkitBackdropFilter: 'blur(9px)',
      display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 22, animation: 'hh-fadeup .22s ease both' }}>
      {/* candy ad creative card */}
      <div className="pop" style={{ width: '100%', maxWidth: 322, borderRadius: 28, overflow: 'hidden', boxShadow: 'var(--shadow-lg)', border: '1px solid rgba(255,255,255,0.2)' }}>
        {/* header — ADVERTISEMENT eyebrow + skip/countdown */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '11px 14px', background: 'linear-gradient(180deg,#243758,#16223C)', color: '#fff' }}>
          <span className="eyebrow" style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: 'rgba(255,255,255,0.7)', letterSpacing: 1.6 }}>
            <PowerIcon id="ad" size={16} color="rgba(255,255,255,0.78)" /> {title === 'Rewarded Ad' ? 'Advertisement' : title}</span>
          {/* circular countdown / close */}
          <button onClick={() => onClose && onClose()} disabled={!done} style={{ width: 30, height: 30, borderRadius: '50%', border: 'none', padding: 0, cursor: done ? 'pointer' : 'default',
            position: 'relative', display: 'grid', placeItems: 'center',
            background: done ? 'rgba(255,255,255,0.2)' : 'rgba(255,255,255,0.07)', color: done ? '#fff' : 'rgba(255,255,255,0.85)', fontWeight: 800, fontSize: 14, lineHeight: 1 }}>
            {!done && <svg viewBox="0 0 36 36" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', transform: 'rotate(-90deg)' }}>
              <circle cx="18" cy="18" r="15" fill="none" stroke="rgba(255,255,255,0.12)" strokeWidth="3" />
              <circle cx="18" cy="18" r="15" fill="none" stroke={accent} strokeWidth="3" strokeLinecap="round"
                strokeDasharray={2 * Math.PI * 15} strokeDashoffset={2 * Math.PI * 15 * (1 - pct)} style={{ transition: 'stroke-dashoffset 1s linear' }} />
            </svg>}
            <span style={{ position: 'relative' }}>{done ? '✕' : t}</span></button>
        </div>
        {/* mock creative */}
        <div style={{ height: 236, position: 'relative', overflow: 'hidden',
          background: 'radial-gradient(130% 100% at 50% -10%, #36468a, #181d3a 72%)' }}>
          {/* soft drifting glow blobs for depth */}
          <div style={{ position: 'absolute', top: -40, left: -30, width: 160, height: 160, borderRadius: '50%', background: `radial-gradient(circle, ${accent}33, transparent 70%)`, filter: 'blur(6px)' }} />
          <div style={{ position: 'absolute', bottom: -50, right: -30, width: 180, height: 180, borderRadius: '50%', background: 'radial-gradient(circle, rgba(124,92,252,0.28), transparent 70%)', filter: 'blur(8px)' }} />
          {/* twinkling star sprinkles */}
          {[[18,30],[78,22],[30,78],[86,70],[55,14]].map((s, i) => <span key={i} style={{ position: 'absolute', left: `${s[0]}%`, top: `${s[1]}%`, width: 5, height: 5, borderRadius: '50%', background: '#fff', boxShadow: '0 0 6px #fff', animation: `hh-bob ${1.6 + i * 0.25}s ease-in-out infinite`, opacity: 0.75 }} />)}
          <div style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 14 }}>
            <div style={{ animation: 'hh-bob 2.4s ease-in-out infinite' }}><HoleVisual r={42} skin="rainbow" /></div>
            <div className="disp hh-outline-sm" style={{ fontSize: 23, textAlign: 'center' }}>More Hungry Hole?</div>
            <div style={{ display: 'flex', gap: 10 }}>{['cube','donut','rocket','crab'].map((tp,i)=><div key={i} style={{ animation:`hh-bob ${2+i*0.3}s ease-in-out infinite` }}><Prop type={tp} size={28}/></div>)}</div>
          </div>
          {/* reward badge — floating chip showing what you'll earn */}
          <div className="chip" style={{ position: 'absolute', top: 12, left: 12, gap: 7, padding: '6px 12px', background: 'rgba(255,255,255,0.14)', border: `1px solid ${accent}88`, color: '#fff', backdropFilter: 'blur(4px)', WebkitBackdropFilter: 'blur(4px)', fontSize: 12 }}>
            <span style={{ width: 8, height: 8, borderRadius: '50%', background: accent, boxShadow: `0 0 8px ${accent}` }} />Reward: <b style={{ color: accent }}>{rewardLabel}</b></div>
          {/* progress bar (hole.io green→yellow) */}
          <div className="hh-track" style={{ position: 'absolute', bottom: 8, left: 12, right: 12, height: 7 }}>
            <div className="hh-fill" style={{ width: `${pct * 100}%`, transition: 'width 1s linear' }} /></div>
        </div>
        {/* footer CTA */}
        <div style={{ padding: 16, background: 'var(--cloud)', textAlign: 'center' }}>
          {!done
            ? <div style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontWeight: 800, color: 'var(--slate)', fontSize: 14 }}>
                <span style={{ width: 16, height: 16, borderRadius: '50%', border: '2.5px solid var(--line)', borderTopColor: accent, animation: 'hh-spin 0.9s linear infinite', display: 'inline-block' }} />
                Reward in <span className="num" style={{ color: 'var(--ink)' }}>{t}s</span>…</div>
            : <button className="btn sun block shimmer pop" onClick={() => onClaim && onClaim()}>
                <PowerIcon id="ad" size={18} color="#5a3d00" /> Claim {rewardLabel}</button>}
        </div>
      </div>
      <div style={{ marginTop: 16, fontSize: 11.5, fontWeight: 800, color: 'rgba(255,255,255,0.55)', textAlign: 'center', maxWidth: 290, lineHeight: 1.5 }}>
        Optional ad reward.</div>
    </div>
  );
}

// a button that says "Watch ad" and opens an AdModal; calls onReward when claimed
function AdButton({ label = 'Watch ad', rewardLabel = 'Reward', onReward, className = 'btn sun', style, accent }) {
  const [open, setOpen] = useStatePU(false);
  return <>
    <button className={className + ' shimmer'} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, ...style }}
      onClick={() => setOpen(true)}><PowerIcon id="ad" size={18} color="currentColor" /> {label}</button>
    {open && <AdModal rewardLabel={rewardLabel} accent={accent} onClose={() => setOpen(false)}
      onClaim={() => { setOpen(false); onReward && onReward(); }} />}
  </>;
}

Object.assign(window, { PowerIcon, GameIcon, AdModal, AdButton });
