const c=document.querySelector('#game'),x=c.getContext('2d'),menu=document.querySelector('#menu'),rank=document.querySelector('#ranking');let W,H,dpr,run=0,pause=0,last=0,time=45,score=0,combo=0,phase=1,orb,parts=[],sound=1,bg=new Image;bg.src='assets/space.webp';
const rnd=(a,b)=>a+Math.random()*(b-a);function size(){const r=c.getBoundingClientRect();dpr=Math.min(devicePixelRatio||1,2);c.width=r.width*dpr;c.height=r.height*dpr;x.setTransform(dpr,0,0,dpr,0,0);W=r.width;H=r.height}new ResizeObserver(size).observe(c);
function rankings(){return JSON.parse(localStorage.neuroloopRanking||'[]')}function showRank(){const a=rankings();rank.innerHTML='<b>TOP 5</b>'+(!a.length?'<li>Nenhuma pontuação salva</li>':a.map(v=>'<li>'+v+' pontos</li>').join(''))}
function spawn(){const r=Math.max(28,52-phase*2);orb={x:rnd(r+15,W-r-15),y:rnd(115+r,H-r-20),r,vx:rnd(-90,90)*(1+phase*.12),vy:rnd(-85,85)*(1+phase*.12),h:rnd(165,320)}}
function start(){run=1;pause=0;time=45;score=0;combo=0;phase=1;parts=[];spawn();menu.classList.add('hidden');last=performance.now();requestAnimationFrame(loop)}
function update(dt){time-=dt;phase=Math.min(12,1+Math.floor(score/900));orb.x+=orb.vx*dt;orb.y+=orb.vy*dt;if(orb.x<orb.r||orb.x>W-orb.r)orb.vx*=-1;if(orb.y<110+orb.r||orb.y>H-orb.r)orb.vy*=-1;for(const p of parts){p.x+=p.vx*dt;p.y+=p.vy*dt;p.life-=dt}parts=parts.filter(p=>p.life>0);if(time<=0)finish()}
function draw(){x.clearRect(0,0,W,H);if(bg.complete)x.drawImage(bg,0,0,W,H);else{x.fillStyle='#020817';x.fillRect(0,0,W,H)}x.fillStyle='#020513bb';x.fillRect(0,0,W,96);x.font='800 20px system-ui';x.fillStyle='#fff';x.fillText('SCORE: '+score,18,38);x.fillStyle='#00edff';x.fillText('COMBO: '+combo,Math.max(150,W*.35),38);x.fillStyle='#ffd93b';x.fillText('FASE: '+phase,Math.max(300,W*.68),38);x.font='700 16px system-ui';x.fillStyle='#d9e6ff';x.fillText('TEMPO '+Math.max(0,time).toFixed(1),18,72);const g=x.createRadialGradient(orb.x,orb.y,4,orb.x,orb.y,orb.r*1.7);g.addColorStop(0,'#fff');g.addColorStop(.25,'hsl('+orb.h+' 100% 70%)');g.addColorStop(.65,'hsl('+orb.h+' 100% 45% / .7)');g.addColorStop(1,'transparent');x.fillStyle=g;x.beginPath();x.arc(orb.x,orb.y,orb.r*1.7,0,7);x.fill();x.strokeStyle='#fff';x.lineWidth=2;x.beginPath();x.arc(orb.x,orb.y,orb.r,0,7);x.stroke();for(const p of parts){x.globalAlpha=p.life;x.fillStyle=p.col;x.fillRect(p.x,p.y,5,5)}x.globalAlpha=1;if(pause){x.fillStyle='#02040dcc';x.fillRect(0,0,W,H);x.textAlign='center';x.fillStyle='#00edff';x.font='900 42px system-ui';x.fillText('JOGO PAUSADO',W/2,H/2);x.textAlign='left'}}
function loop(t){if(!run)return;const dt=Math.min(.04,(t-last)/1000);last=t;if(!pause)update(dt);draw();requestAnimationFrame(loop)}
function tap(px,py){if(!run||pause)return;const d=Math.hypot(px-orb.x,py-orb.y);if(d<=orb.r*1.25){combo++;score+=100+combo*10;time=Math.min(50,time+.35);for(let i=0;i<24;i++)parts.push({x:orb.x,y:orb.y,vx:rnd(-200,200),vy:rnd(-200,200),life:rnd(.25,.8),col:'hsl('+orb.h+' 100% 65%)'});beep(260+combo*18);spawn()}else{combo=0;score=Math.max(0,score-35);beep(100)}}
function beep(f){if(!sound)return;const A=window.AudioContext||window.webkitAudioContext,a=new A,o=a.createOscillator(),g=a.createGain();o.frequency.value=f;o.type='sine';g.gain.setValueAtTime(.06,a.currentTime);g.gain.exponentialRampToValueAtTime(.001,a.currentTime+.11);o.connect(g).connect(a.destination);o.start();o.stop(a.currentTime+.12)}
function finish(){run=0;let a=rankings();a.push(score);a.sort((a,b)=>b-a);localStorage.neuroloopRanking=JSON.stringify(a.slice(0,5));menu.querySelector('h1').textContent='RESULTADO';menu.querySelector('p').textContent='Score: '+score+' · Fase: '+phase+'. Concentre-se e supere seu recorde!';menu.querySelector('#start').textContent='REINICIAR';showRank();menu.classList.remove('hidden')}
c.addEventListener('pointerdown',e=>{const r=c.getBoundingClientRect();tap(e.clientX-r.left,e.clientY-r.top)});document.querySelector('#start').onclick=start;document.querySelector('#pause').onclick=()=>pause=!pause;document.querySelector('#settings').onclick=e=>{sound=!sound;e.target.textContent='SOM: '+(sound?'LIGADO':'DESLIGADO')};document.querySelector('#pix').onclick=async e=>{try{await navigator.clipboard.writeText('17909502877');e.target.textContent='Chave Pix copiada!'}catch{prompt('Copie a chave Pix:','17909502877')}};showRank();
