// ✅ Game Launcher — Login চেক করে Real Game URL নতুন ট্যাবে খোলে
function launchRealGame(gameCode, gameName) {
    const authToken = localStorage.getItem('token');
    if (!authToken) { window.location.href = '/newlogin.php'; return; }
    if (!gameCode) { alert('এই Game-এর Code পাওয়া যায়নি।'); return; }

    const gameWindow = window.open('about:blank', '_blank');
    if (gameWindow) {
        gameWindow.document.write(`
            <!DOCTYPE html><html><head><meta charset="UTF-8"><title>CV111 - Loading</title>
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover">
            <style>
                *{margin:0;padding:0;box-sizing:border-box;}
                html,body{touch-action:pan-x pan-y;}
                body{min-height:100vh;background:#012222;display:flex;align-items:center;justify-content:center;
                    flex-direction:column;font-family:sans-serif;color:#fff;}
                .brand{
                    font-size:22px;font-weight:900;font-style:italic;letter-spacing:1.5px;margin-bottom:24px;
                    background:linear-gradient(90deg,#a5f3fc 0%,#22d3ee 22%,#f0fdff 42%,#2dd4bf 62%,#0e7490 85%,#22d3ee 100%);
                    background-size:250% auto; -webkit-background-clip:text; background-clip:text;
                    -webkit-text-fill-color:transparent; color:transparent;
                    filter:drop-shadow(0 1px 1px rgba(0,0,0,.5)) drop-shadow(0 0 6px rgba(34,211,238,.4));
                    animation:brandShimmer 4s linear infinite;
                }
                @keyframes brandShimmer{0%{background-position:0% center;}100%{background-position:-250% center;}}
                .spin-box{width:60px;height:60px;display:flex;align-items:center;justify-content:center;margin-bottom:18px;}
                .spin-box img{width:100%;height:100%;object-fit:contain;animation:spin 1s linear infinite;}
                @keyframes spin{to{transform:rotate(360deg);}}
                .txt{font-weight:700;font-size:14px;}
            </style></head>
            <body>
                <div class="brand">CV111</div>
                <div class="spin-box"><img src="/spin.png" alt="Loading" onerror="this.style.display='none'; this.parentElement.textContent='🎰';"></div>
                <div class="txt">${gameName || 'Game'} খোলা হচ্ছে...</div>
                <script>
                    // ✅ Zoom বন্ধ করা (Pinch + Double-Tap)
                    document.addEventListener('gesturestart', function(e){ e.preventDefault(); });
                    document.addEventListener('touchmove', function(e){ if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
                    let __lastTouchEnd = 0;
                    document.addEventListener('touchend', function(e){
                        const now = Date.now();
                        if (now - __lastTouchEnd <= 300) e.preventDefault();
                        __lastTouchEnd = now;
                    }, { passive: false });
                </script>
            </body></html>
        `);
        gameWindow.document.close();
    }

    fetch('/deepanshu/api/webapi/GetGameUrl.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authToken },
        body: JSON.stringify({ gameCode: gameCode })
    })
    .then(res => res.json())
    .then(data => {
        if (data.code === 0 && data.data && data.data.url) {
            if (gameWindow) gameWindow.location.href = data.data.url;
            else window.location.href = data.data.url;
        } else if (data.code === 4) {
            if (gameWindow) gameWindow.close();
            localStorage.removeItem('token');
            window.location.href = '/newlogin.php';
        } else {
            if (gameWindow) gameWindow.close();
            alert('Game চালু করা যায়নি: ' + (data.msg || 'অজানা সমস্যা'));
        }
    })
    .catch(() => {
        if (gameWindow) gameWindow.close();
        alert('একটা সমস্যা হয়েছে, আবার চেষ্টা করো।');
    });
}
