// ═══ PARTICLE SYSTEM ═══
const cvs=document.getElementById('particles'),ctx=cvs.getContext('2d');
function resizeCvs(){cvs.width=window.innerWidth;cvs.height=window.innerHeight;}
resizeCvs();window.addEventListener('resize',resizeCvs);
const pts=[];for(let i=0;i<60;i++)pts.push({x:Math.random()*cvs.width,y:Math.random()*cvs.height,r:Math.random()*2+0.5,dx:(Math.random()-0.5)*0.4,dy:(Math.random()-0.5)*0.4,a:Math.random()*0.5+0.1});
function drawPts(){ctx.clearRect(0,0,cvs.width,cvs.height);pts.forEach(p=>{p.x+=p.dx;p.y+=p.dy;if(p.x<0||p.x>cvs.width)p.dx*=-1;if(p.y<0||p.y>cvs.height)p.dy*=-1;ctx.beginPath();ctx.arc(p.x,p.y,p.r,0,Math.PI*2);ctx.fillStyle=`rgba(255,165,0,${p.a})`;ctx.fill();});
// Draw lines between close particles
for(let i=0;i<pts.length;i++)for(let j=i+1;j<pts.length;j++){const d=Math.hypot(pts[i].x-pts[j].x,pts[i].y-pts[j].y);if(d<120){ctx.beginPath();ctx.moveTo(pts[i].x,pts[i].y);ctx.lineTo(pts[j].x,pts[j].y);ctx.strokeStyle=`rgba(255,165,0,${0.08*(1-d/120)})`;ctx.stroke();}}
requestAnimationFrame(drawPts);}
drawPts();

// ═══ SCREEN FLASH ═══
function screenFlash(){const f=document.createElement('div');f.className='flash';document.body.appendChild(f);setTimeout(()=>f.remove(),400);}

// ═══ PREDICTION LOGIC ═══
let predInterval=null,predRunning=false;
function startPrediction(){
  if(predRunning)return;
  predRunning=true;
  const statusEl=document.getElementById('pred-status');
  const resultEl=document.getElementById('pred-result');
  const numEl=document.getElementById('pred-number');
  const colorEl=document.getElementById('pred-color');
  const sizeEl=document.getElementById('pred-size');
  const confEl=document.getElementById('pred-conf');
  const btnStart=document.getElementById('btn-start');
  const btnHide=document.getElementById('btn-hide');

  statusEl.textContent='ANALYZING...';
  statusEl.style.fontSize='24px';
  statusEl.style.color='#FFD700';

  // Fake analysis phase
  let dots=0;
  const analyzeInt=setInterval(()=>{dots=(dots+1)%4;statusEl.textContent='ANALYZING'+'.'.repeat(dots);},300);

  setTimeout(()=>{
    clearInterval(analyzeInt);
    statusEl.style.display='none';
    resultEl.style.display='flex';
    btnStart.style.display='none';
    btnHide.style.display='inline-block';
    screenFlash();
    generatePrediction();
    // Auto-refresh prediction
    predInterval=setInterval(()=>{
      numEl.classList.add('glitching');
      setTimeout(()=>{numEl.classList.remove('glitching');generatePrediction();screenFlash();},500);
    },getIntervalMs());
  },2000+Math.random()*1500);
}

function getIntervalMs(){
  switch(MODE){
    case'30sec':return 30000;
    case'1min':return 60000;
    case'3min':return 180000;
    case'5min':return 300000;
    default:return 30000;
  }
}

function generatePrediction(){
  const num=Math.floor(Math.random()*10);
  const colors=['green','red','violet'];
  const colorIdx=num===0?2:(num%2===0?0:1); // even=green, odd=red, 0=violet
  const color=colors[colorIdx];
  const size=num>=5?'BIG':'SMALL';
  const conf=Math.floor(Math.random()*15+82); // 82-97%

  document.getElementById('pred-number').textContent=num;
  const colorEl=document.getElementById('pred-color');
  colorEl.textContent=color.toUpperCase();
  colorEl.className='pred-color '+color;
  document.getElementById('pred-size').textContent=size;
  document.getElementById('pred-conf').textContent=conf;
}

function hidePrediction(){
  predRunning=false;
  if(predInterval)clearInterval(predInterval);
  const statusEl=document.getElementById('pred-status');
  statusEl.style.display='block';
  statusEl.style.fontSize='36px';
  statusEl.style.color='#FFA500';
  statusEl.textContent='READY';
  document.getElementById('pred-result').style.display='none';
  document.getElementById('btn-start').style.display='inline-block';
  document.getElementById('btn-hide').style.display='none';
}

// ═══ SERVER MS FLUCTUATION ═══
setInterval(()=>{document.querySelectorAll('.serv-ms').forEach(el=>{el.textContent=Math.floor(Math.random()*60+12)+'ms';});},3000);
