function handleGameClick(code, name, img, vendor) {
    addToRecent(code, name, img, vendor);
    launchGame(code, name);
}

async function launchGame(gameCode, gameName) {
    if (!gameCode) { alert('এই Game-এর Code পাওয়া যায়নি।'); return; }
    if (!token) { window.location.href = '/newlogin.php'; 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">
            <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(255,193,7,.15), transparent 45%),
                    radial-gradient(circle at 90% 100%, rgba(255,193,7,.08), transparent 40%), #061d22;
                    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(255,193,7,.12), transparent 70%);top:-100px;left:-100px;animation:float 8s ease-in-out infinite;}
                @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;
                    color:#FFC107;position:relative;z-index:1;}
                .spin-box{width:70px;height:70px;border-radius:20px;background:linear-gradient(135deg,#FFC107,#E0A800);
                    display:flex;align-items:center;justify-content:center;animation:spin 1s linear infinite;
                    box-shadow:0 0 40px rgba(255,193,7,.5);margin-bottom:20px;position:relative;z-index:1;}
                .spin-box span{font-size:30px;}
                @keyframes spin{to{transform:rotate(360deg);}}
                .txt{color:#fff;font-weight:800;font-size:15px;margin-bottom:6px;position:relative;z-index:1;}
                .sub{color:#8FAAB0;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,#E0A800,#FFC107);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>🎰</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();
        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 (e) {
        if (gameWindow) gameWindow.close();
        alert('একটা সমস্যা হয়েছে, আবার চেষ্টা করো।');
    }
}