async function launchGame(gameCode, gameName) {
    if (!gameCode) { alert('এই Game-এর Code পাওয়া যায়নি।'); return; }
    if (!token) { window.location.href = '/newlogin.php'; return; }

    // ✅ নতুন যোগ — Game খোলার সময়, একটা আকর্ষণীয় Lucky-Spin
    // Popup দেখানো (Game-Loading আটকাবে না, শুধু Advertise করবে)
    showLuckySpinPromo();

    // ✅ ঠিক করা হলো — আগে "খেলতে চাও?" Confirm-Popup একটা
    // Extra ধাপ ছিল, যেটা User-কে থামিয়ে দিতো। এখন সেটা
    // সরিয়ে, সরাসরি তাৎক্ষণিক Loading-Feedback দেখানো হচ্ছে
    showGameLoadingOverlay(gameName);

    // ✅ ঠিক করা হলো — Click-Gesture থাকা অবস্থাতেই (Async-এর
    // আগে) window.open() Call করা হচ্ছে, যাতে Browser এটাকে
    // Popup-Blocker দিয়ে না আটকায়।
    const gameWindow = window.open('about:blank', '_blank');

    // ✅ নতুন যোগ — সাদা/Blank Page-এর বদলে, নতুন Tab-এই সাথে
    // সাথে Premium, Full-Brand Loading-Page লিখে দেওয়া হচ্ছে
    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">
            <link rel="preconnect" href="https://fonts.googleapis.com">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700;800;900&display=swap">
            <style>
                *{margin:0;padding:0;box-sizing:border-box;}
                html,body{width:100%;height:100%;margin:0;padding:0;}
                body{
                    min-height:100vh;min-width:100vw;background:radial-gradient(circle at 20% 0%, rgba(155,93,229,.18), transparent 45%),
                    radial-gradient(circle at 90% 100%, rgba(245,197,66,.1), transparent 40%), #0A0714;
                    display:flex;align-items:center;justify-content:center;flex-direction:column;
                    font-family:'Poppins',sans-serif;overflow:hidden;position:relative;
                }
                body::before{content:'';position:absolute;width:300px;height:300px;border-radius:50%;
                    background:radial-gradient(circle, rgba(155,93,229,.15), transparent 70%);top:-100px;left:-100px;animation:float 8s ease-in-out infinite;}
                body::after{content:'';position:absolute;width:220px;height:220px;border-radius:50%;
                    background:radial-gradient(circle, rgba(245,197,66,.12), transparent 70%);bottom:-80px;right:-60px;animation:float 10s ease-in-out infinite reverse;}
                @keyframes float{0%,100%{transform:translateY(0);}50%{transform:translateY(-20px);}}

                .brand{font-size:20px;font-weight:900;font-style:italic;letter-spacing:1.5px;margin-bottom:36px;position:relative;z-index:1;
                    background:linear-gradient(135deg,#F5C542,#E0A926);-webkit-background-clip:text;background-clip:text;color:transparent;}

                .spin-box{width:70px;height:70px;border-radius:20px;background:linear-gradient(135deg,#F5C542,#E0A926);
                    display:flex;align-items:center;justify-content:center;animation:spin 1s linear infinite;
                    box-shadow:0 0 40px rgba(245,197,66,.5), 0 0 80px rgba(155,93,229,.2);margin-bottom:20px;position:relative;z-index:1;}
                .spin-box .dice{font-size:30px;}
                @keyframes spin{to{transform:rotate(360deg);}}

                .txt{color:#fff;font-weight:800;font-size:15px;letter-spacing:.3px;margin-bottom:6px;position:relative;z-index:1;}
                .sub{color:#9691B0;font-size:11.5px;position:relative;z-index:1;margin-bottom:24px;}

                .bar-wrap{width:160px;height:4px;border-radius:10px;background:rgba(255,255,255,.08);overflow:hidden;position:relative;z-index:1;}
                .bar-fill{position:absolute;top:0;left:-40%;height:100%;width:40%;border-radius:10px;
                    background:linear-gradient(90deg,#6C3FC5,#9B5DE5,#F5C542);animation:slide 1.1s ease-in-out infinite;}
                @keyframes slide{0%{left:-40%;}100%{left:100%;}}
            </style></head>
            <body>
                <div class="brand">CV111</div>
                <div class="spin-box"><span class="dice">🎲</span></div>
                <div class="txt">${gameName || 'Game'} খোলা হচ্ছে...</div>
                <div class="sub">একটু অপেক্ষা করো, প্রায় হয়ে এসেছে</div>
                <div class="bar-wrap"><div class="bar-fill"></div></div>
            </body></html>
        `);
        gameWindow.document.close();
    }

    try {
        const res = await fetch('/deepanshu/api/webapi/GetGameUrl.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ gameCode: gameCode })
        });
        const data = await res.json();
        hideGameLoadingOverlay();

        if (data.code === 0 && data.data && data.data.url) {
            if (gameWindow) gameWindow.location.href = data.data.url;
            else window.location.href = data.data.url; // Popup Block হলে, একই Tab-এ Fallback
        } 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 (e) {
        hideGameLoadingOverlay();
        if (gameWindow) gameWindow.close();
        alert('একটা সমস্যা হয়েছে, আবার চেষ্টা করো।');
    }
}

// ✅ নতুন যোগ — Game-Loading-Overlay (Confirm-Dialog-এর বদলে,
// তাৎক্ষণিক, Non-Blocking Visual Feedback)
function showGameLoadingOverlay(gameName) {
    let overlay = document.getElementById('gameLoadingOverlay');
    if (!overlay) {
        overlay = document.createElement('div');
        overlay.id = 'gameLoadingOverlay';
        overlay.style.cssText = 'position:fixed; inset:0; z-index:9998; background:rgba(10,7,20,.85); display:flex; align-items:center; justify-content:center; flex-direction:column; backdrop-filter:blur(4px);';
        document.body.appendChild(overlay);
    }
    overlay.innerHTML = `
        <div style="width:50px; height:50px; border-radius:14px; background:linear-gradient(135deg,#F5C542,#E0A926); display:flex; align-items:center; justify-content:center; animation:gameLoadSpin 0.8s linear infinite; margin-bottom:14px;">
            <i class="fa-solid fa-dice" style="color:#1a1400; font-size:20px;"></i>
        </div>
        <div style="color:#fff; font-weight:800; font-size:13px;">${gameName || 'Game'} খোলা হচ্ছে...</div>
    `;
    overlay.style.display = 'flex';
    if (!document.getElementById('gameLoadSpinStyle')) {
        const style = document.createElement('style');
        style.id = 'gameLoadSpinStyle';
        style.textContent = '@keyframes gameLoadSpin { to { transform: rotate(360deg); } }';
        document.head.appendChild(style);
    }
}
function hideGameLoadingOverlay() {
    const overlay = document.getElementById('gameLoadingOverlay');
    if (overlay) overlay.style.display = 'none';
}

// ✅ নতুন যোগ — Game খোলার সময়, Lucky-Spin-এর একটা আকর্ষণীয়
// Promotional-Popup দেখানো (৪ সেকেন্ড পর নিজে থেকেই বন্ধ হয়ে যায়)
function showLuckySpinPromo() {
    // ✅ একই Session-এ বারবার না দেখাতে, Cooldown রাখা (৩০ মিনিট)
    const lastShown = localStorage.getItem('luckySpinPromoLastShown');
    const now = Date.now();
    if (lastShown && (now - parseInt(lastShown)) < 30 * 60 * 1000) return;
    localStorage.setItem('luckySpinPromoLastShown', now.toString());

    let promo = document.getElementById('luckySpinPromo');
    if (!promo) {
        promo = document.createElement('div');
        promo.id = 'luckySpinPromo';
        promo.style.cssText = `
            position:fixed; bottom:20px; right:16px; left:16px; max-width:340px; margin:0 auto;
            background:linear-gradient(135deg,#6C3FC5,#9B5DE5,#B57EF0); border-radius:18px;
            padding:14px 16px; z-index:99999; box-shadow:0 20px 50px -10px rgba(108,63,197,.6);
            display:flex; align-items:center; gap:12px; cursor:pointer;
            animation:luckySpinSlideIn .4s ease;
        `;
        promo.innerHTML = `
            <div style="width:44px; height:44px; border-radius:12px; background:rgba(255,255,255,.2); display:flex; align-items:center; justify-content:center; font-size:22px; flex-shrink:0;">🎡</div>
            <div style="flex:1; color:#fff;">
                <div style="font-weight:800; font-size:13px;">Lucky Spin অপেক্ষা করছে!</div>
                <div style="font-size:11px; opacity:.85;">দিনে ১ বার Free Spin করে, Bonus জিতো</div>
            </div>
            <i class="fa-solid fa-xmark" style="color:#fff; opacity:.7; padding:4px;" onclick="event.stopPropagation(); document.getElementById('luckySpinPromo').remove();"></i>
        `;
        promo.onclick = () => { window.location.href = '/newluckyspin.php'; };
        document.body.appendChild(promo);

        const style = document.createElement('style');
        style.textContent = '@keyframes luckySpinSlideIn { from { transform: translateY(80px); opacity:0; } to { transform: translateY(0); opacity:1; } }';
        document.head.appendChild(style);

        setTimeout(() => { if (promo) promo.remove(); }, 6000);
    }
}

