/* ------------------------
   1. GAME STATE & VARIABLES
   ------------------------ */
let board = [];
let currentScore = 0;

// Ambil username dari sessionStorage (Cek apakah ada user login)
const loggedInUser = sessionStorage.getItem("loggedInUser");

// Tentukan user saat ini (jika tidak ada login, pakai "guest")
const currentUser = loggedInUser || "guest";

// Buat nama kunci unik untuk penyimpanan
const storageKey = 'highScore2048_' + currentUser;

// --- PERBAIKAN DI SINI ---
let highScore = 0;

if (loggedInUser) {
  // JIKA SUDAH LOGIN: Ambil High Score dari memori localStorage
  highScore = parseInt(localStorage.getItem(storageKey)) || 0;
} else {
  // JIKA BELUM LOGIN (GUEST): Selalu mulai High Score dari 0 saat refresh
  highScore = 0;
}
// -------------------------

let lastMoveDir = null;
let isMoving = false;
let mergesInCurrentMove = 0;

// Sound State (baca dari localStorage atau default ON)
let soundState = {
  bg: localStorage.getItem('sound_bg') !== 'false',
  pop: localStorage.getItem('sound_pop') !== 'false',
  merge: localStorage.getItem('sound_merge') !== 'false'
};

// Volume State (0-100 for each sound)
let volumeState = {
  music: parseInt(localStorage.getItem('vol_music')) || 25,
  pop: parseInt(localStorage.getItem('vol_pop')) || 90,
  merge: parseInt(localStorage.getItem('vol_merge')) || 100
};