// ✅ Game-এর নাম দেখে, Auto-Detect করা এটা "Popular" কিনা
// (সব Vendor-এই কাজ করবে, শুধু "All"-এ না)
const popularKeywords = [
    'fortune', 'dragon', 'gold', 'jackpot', 'wild', 'tiger', 'phoenix',
    'treasure', 'lucky', 'wealth', 'gems', 'diamond', 'ways', 'megaways',
    'bonanza', 'rich', 'money', 'coin', 'king', 'crown', 'empire', 'legend',
    'hot', 'blaze', 'fire', 'thunder', 'super', 'mega', 'grand'
];
function isPopularGame(name) {
    const lower = (name || '').toLowerCase();
    return popularKeywords.some(kw => lower.includes(kw));
}

function renderGrid() {
    const grid = document.getElementById('gamesGrid');
    let list = [];

    if (viewMode === 'favorite') list = getFavorites();
    else if (viewMode === 'recent') list = getRecent();
    else list = allGames;

    if (searchQuery) {
        list = list.filter(g => (g.gameNameEn || g.gameName || g.name || '').toLowerCase().includes(searchQuery));
    }

    // ✅ "ALL"-এ সব Game-ই থাকবে, শুধু Popular Game-গুলো
    // সবার উপরে সাজানো হয় (HOT Badge-সহ)
    if (viewMode === 'all' && !currentVendor) {
        list = [...list].sort((a, b) => {
            const nameA = a.gameNameEn || a.gameName || a.name || '';
            const nameB = b.gameNameEn || b.gameName || b.name || '';
            return (isPopularGame(nameB) ? 1 : 0) - (isPopularGame(nameA) ? 1 : 0);
        });
    }

    if (list.length === 0) {
        grid.innerHTML = `<div class="empty-state"><i class="fa-solid fa-ghost"></i>কোনো Game পাওয়া যায়নি।</div>`;
        return;
    }

    grid.innerHTML = list.map(g => {
        const name = g.gameNameEn || g.gameName || g.name || 'Game';
        const img = g.imgUrl || g.imageUrl || g.img || g.image || '';
        const code = g.gameCode || g.gameID || g.code || '';
        const vendor = g.vendorCode || g.vendor || '';
        const isFav = getFavorites().some(f => f.code === code);
        return `
            <div class="game-card" onclick="handleGameClick('${code.replace(/'/g,"\\'")}', '${name.replace(/'/g,"\\'")}', '${img}', '${vendor}')">
                ${isPopularGame(name) ? `<div style="position:absolute; top:8px; left:8px; z-index:3; background:linear-gradient(135deg,#fbbf24,#d97706); color:#1a1400; font-size:11px; font-weight:900; padding:4px 9px; border-radius:9px;">🔥 HOT</div>` : ''}
                <div class="fav-btn ${isFav ? 'active' : ''}" onclick="event.stopPropagation(); toggleFav('${code.replace(/'/g,"\\'")}','${name.replace(/'/g,"\\'")}','${img}','${vendor}', this)">
                    <i class="fa-solid fa-heart" style="font-size:10px;"></i>
                </div>
                <div class="game-img-wrap">
                    <img src="${img}" alt="${name}" loading="lazy" onload="this.classList.add('loaded')" onerror="this.style.display='none'">
                    ${vendor ? `<div class="provider-badge">${vendor}</div>` : ''}
                </div>
                <div class="game-name">${name}</div>
            </div>
        `;
    }).join('');
}
