function checkAndShowTutorial() {
    // Ambil user yang sedang login (atau guest)
    const currentUser = sessionStorage.getItem("loggedInUser") || "guest";
    const tutorialKey = 'tutorialSeen_' + currentUser;

    console.log(`[Tutorial Check] User: ${currentUser}`);
    console.log(`[Tutorial Check] Key: ${tutorialKey}`);

    // Cek apakah tutorial sudah pernah dilihat
    const hasSeenTutorial = localStorage.getItem(tutorialKey);
    console.log(`[Tutorial Check] Status Seen: ${hasSeenTutorial}`);

    const tutorialOverlay = document.getElementById('tutorial-overlay');

    // Tampilkan tutorial jika belum pernah dilihat
    if (!hasSeenTutorial && tutorialOverlay) {
        tutorialOverlay.style.display = 'flex';
    }
}

// Jalankan saat halaman selesai dimuat
document.addEventListener('DOMContentLoaded', () => {
    checkAndShowTutorial(); // Cek & tampilkan tutorial

    const closeTutorialBtn = document.getElementById('close-tutorial');
    const tutorialOverlay = document.getElementById('tutorial-overlay');

    // Event klik tombol tutup tutorial
    if (closeTutorialBtn) {
        closeTutorialBtn.addEventListener('click', () => {
            // Ambil user aktif saat ini
            const currentUser = sessionStorage.getItem("loggedInUser") || "guest";
            const tutorialKey = 'tutorialSeen_' + currentUser;

            // Sembunyikan tutorial & simpan status
            if (tutorialOverlay) tutorialOverlay.style.display = 'none';
            localStorage.setItem(tutorialKey, 'true');
        });
    }
});

window.checkTutorial = checkAndShowTutorial;