/* ------------------------
   7. MAIN INITIALIZATION
   ------------------------ */
function restartGame() {
  hideGameOver();
  resetScore();
  setupBoard();
  addNewTile();
  addNewTile();
  refreshBoard();
  isMoving = false;
}

function playAgain() {
  restartGame();
}

function goHome() {
  try { 
    audio.bg.pause(); 
    audio.bg.currentTime = 0; 
  } catch (e) {}
  window.location.href = "Homepage.html";
}

/* Event Listeners Setup */
function setupEventListeners() {
  // Tutorial Modal
  const btnTutorial = document.getElementById('btn-tutorial');
  const tutorialOverlay = document.getElementById('tutorial-overlay');
  const closeTutorial = document.getElementById('close-tutorial');

  if (btnTutorial) btnTutorial.addEventListener('click', () => tutorialOverlay.style.display = 'flex');
  if (closeTutorial) closeTutorial.addEventListener('click', () => tutorialOverlay.style.display = 'none');
  if (tutorialOverlay) tutorialOverlay.addEventListener('click', (e) => {
      if (e.target === tutorialOverlay) tutorialOverlay.style.display = 'none';
  });

  // Restart & Game Over buttons
  const btnRestart = document.getElementById('btn-restart');
  if (btnRestart) btnRestart.addEventListener('click', restartGame);

  const btnPlayAgain = document.getElementById('btn-play-again');
  if (btnPlayAgain) btnPlayAgain.addEventListener('click', playAgain);
  
  const btnHome = document.getElementById('btn-home');
  if (btnHome) btnHome.addEventListener('click', goHome);

  const gameOverClose = document.getElementById('game-over-close');
  if (gameOverClose) gameOverClose.addEventListener('click', hideGameOver);

  const gameOverOverlay = document.getElementById('game-over-overlay');
  if (gameOverOverlay) gameOverOverlay.addEventListener('click', function(e) {
      if (e.target === this) hideGameOver();
  });

  // Sound Buttons (Mute Toggles)
  const btnSoundBg = document.getElementById('btn-sound-bg');
  const btnSoundPop = document.getElementById('btn-sound-pop');
  const btnSoundMerge = document.getElementById('btn-sound-merge');

  if (btnSoundBg) btnSoundBg.addEventListener('click', () => { 
      soundState.bg = !soundState.bg; 
      localStorage.setItem('sound_bg', soundState.bg);
      updateAudioVolumes();
      if(soundState.bg) tryPlayBg(); else audio.bg.pause();
      // Tambahkan logika update tombol UI jika ada (toggle class)
  });
  
  if (btnSoundPop) btnSoundPop.addEventListener('click', () => {
      soundState.pop = !soundState.pop;
      localStorage.setItem('sound_pop', soundState.pop);
      updateAudioVolumes();
  });

  if (btnSoundMerge) btnSoundMerge.addEventListener('click', () => {
      soundState.merge = !soundState.merge;
      localStorage.setItem('sound_merge', soundState.merge);
      updateAudioVolumes();
  });
}

/* DOM Ready */
document.addEventListener("DOMContentLoaded", () => {
  updateHighScoreDisplay();
  setupBoard();
  addNewTile();
  addNewTile();
  initVolumeControl(); // Starts audio logic
  tryPlayBg();
  document.addEventListener("keydown", handleKey);
  setupEventListeners();
  checkAndShowTutorial();  
});