// --- REFERENCIAS DOM ---
let rankingList;

function initRankingRefs() {
    rankingList = document.getElementById('ranking-list');
}

// --- RENDERIZAR RANKING ---
function renderRanking() {
    // Sort parties by votes
    const sorted = Object.entries(partidos)
        .map(([id, p]) => ({ id, ...p }))
        .sort((a, b) => b.votos - a.votos);

    // Count regions per party
    const regionCount = {};
    for (let key in partidos) { regionCount[key] = 0; }
    for (let regionId in regiones) {
        const ctrl = regiones[regionId].controlador;
        if (ctrl) regionCount[ctrl]++;
    }

    rankingList.innerHTML = sorted.map((p, i) => {
        const isPlayer = p.isPlayer ? 'player' : '';
        const votosStr = p.votos >= 1000000
            ? (p.votos / 1000000).toFixed(2) + 'M'
            : Math.floor(p.votos).toLocaleString();
        return `
            <div class="ranking-item ${isPlayer}" style="border-left: 4px solid ${p.color};">
                <div class="ranking-position">${i + 1}.</div>
                <div class="ranking-emoji">${p.emoji}</div>
                <div class="ranking-name" style="color: ${p.color};">${p.name}</div>
                <div class="ranking-regiones">🗺️${regionCount[p.id]}</div>
                <div class="ranking-votos">${votosStr}</div>
            </div>
        `;
    }).join('');
}
