/* LuckZoo Fixed Game Canvas v2
   Uniformly scales the approved 1366 x 640 canvas.  It does not touch game,
   RNG, drawing, audio, betting, or API state. */
(()=>{
'use strict';

const DESIGN_W=1366;
const DESIGN_H=640;
let raf=0;

function fitLuckZoo(){
  const game=document.getElementById('game');
  const view=game && game.querySelector('.viewport');
  if(!game || !view) return;

  const vv=window.visualViewport;
  const availableW=Math.max(1, vv ? vv.width : window.innerWidth);
  const availableH=Math.max(1, vv ? vv.height : window.innerHeight);
  const scale=Math.min(availableW/DESIGN_W, availableH/DESIGN_H);

  view.style.setProperty('--lz-fit-scale', String(scale));
  window.LuckZooViewportScale=scale;

  window.dispatchEvent(new CustomEvent('luckzoo:viewport-fit',{
    detail:{
      scale,
      designWidth:DESIGN_W,
      designHeight:DESIGN_H,
      viewportWidth:availableW,
      viewportHeight:availableH
    }
  }));
}

function scheduleFit(){
  cancelAnimationFrame(raf);
  raf=requestAnimationFrame(fitLuckZoo);
}

if(document.readyState==='loading'){
  document.addEventListener('DOMContentLoaded',scheduleFit,{once:true});
}else{
  scheduleFit();
}

window.addEventListener('resize',scheduleFit,{passive:true});
window.addEventListener('orientationchange',scheduleFit,{passive:true});
if(window.visualViewport){
  window.visualViewport.addEventListener('resize',scheduleFit,{passive:true});
  window.visualViewport.addEventListener('scroll',scheduleFit,{passive:true});
}
})();
