document.addEventListener('visibilitychange', ()=>{ if(document.visibilityState==='visible' && !activated){ trySilentAutoplay(3, 250); } });
['mouseenter','touchstart'].forEach(evt=>{ target.addEventListener(evt, ()=>{ if(!activated){ trySilentAutoplay(2,200); } }, {passive:true}); });
player.on(player.Events.Playing, ()=>{ if(activated){ startTick(); } }); player.on(player.Events.Paused, ()=>{ if(activated) stopTick(); }); }); };
const activate=target.querySelector('.cv-activate');
activate.addEventListener('click', async ()=>{ if(!player || activated) return;
activated=true; target.classList.add('is-active');
try{ await player.stop(); await player.unmute(); await player.setVolume(1); await player.play(); enforceMaxVolume(); }catch(e){} startTick(); });
async function updateProgress(){ if(!player||!duration) return; try{ const current=await player.getCurrentTime(); const remaining=Math.max(0, duration-current); const frac=remaining/duration; ringEl.style.strokeDashoffset=(1-frac)*C;
if(activated && !collapsing && remaining <= 0.2){ collapseToIdle(); } }catch(e){} } function tick(){ updateProgress(); rafId=requestAnimationFrame(tick); } function startTick(){ cancelAnimationFrame(rafId); tick(); } function stopTick(){ cancelAnimationFrame(rafId); updateProgress(); }
async function collapseToIdle(){ if(!player || collapsing) return; collapsing = true;
activated=false; target.classList.remove('is-active'); stopTick();
try{ await player.stop(); await player.mute(); await player.setVolume(0); await new Promise(r=>setTimeout(r, 120)); await player.play(); }catch(e){} finally{ collapsing = false; } } })();