(() => {
"use strict";
const EUROPE = new Set(["Albania","Austria","Belgium","Bosnia-Herzegovina","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","England","Estonia","Finland","France","Georgia","Germany","Greece","Hungary","Iceland","Ireland","Israel","Italy","Kosovo","Latvia","Lithuania","Luxembourg","Montenegro","Netherlands","North Macedonia","Northern Ireland","Norway","Poland","Portugal","Romania","Scotland","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Turkey","Ukraine","Wales"]);
const NATION_FR={Argentina:"Argentine",Australia:"Australie",Austria:"Autriche",Belgium:"Belgique",Brazil:"Brésil",Canada:"Canada",Chile:"Chili",Colombia:"Colombie",Croatia:"Croatie",Denmark:"Danemark",Ecuador:"Équateur",England:"Angleterre",France:"France",Germany:"Allemagne",Ghana:"Ghana",Italy:"Italie",Japan:"Japon",Mexico:"Mexique",Morocco:"Maroc",Netherlands:"Pays-Bas",Nigeria:"Nigeria",Norway:"Norvège",Poland:"Pologne",Portugal:"Portugal",Scotland:"Écosse",Senegal:"Sénégal",Serbia:"Serbie",Spain:"Espagne",Sweden:"Suède",Switzerland:"Suisse",Turkey:"Turquie",Ukraine:"Ukraine",Uruguay:"Uruguay",Wales:"Pays de Galles","United States":"États-Unis","South Korea":"Corée du Sud"};
const CLUB_PLAYERS=new Map();for(const p of FOOTBALL_DATA.players){if(!CLUB_PLAYERS.has(p.clubId))CLUB_PLAYERS.set(p.clubId,[]);CLUB_PLAYERS.get(p.clubId).push(p)}
const playable=FOOTBALL_DATA.clubs.filter(c=>(CLUB_PLAYERS.get(c.id)||[]).length>=18);
const avg=id=>{let a=(CLUB_PLAYERS.get(id)||[]).slice().sort((x,y)=>y.rating-x.rating).slice(0,18);return a.length?a.reduce((s,p)=>s+p.rating,0)/a.length:0};
const eur=playable.filter(c=>EUROPE.has(c.country)).sort((a,b)=>avg(b.id)-avg(a.id));

// UEFA 2026/27 : 29 qualifiés directs officiels (liste UEFA au 20/08/2026).
const UEFA_UCL_DIRECT=["11","405","13","16","131","27","2282","1047","234","141","46","23826","826","1082","31","281","985","6195","583","720","383","150","418","12","660","62","336","79","1050"];

// Les 7 dernières places sont encore en barrages UEFA au 20/08/2026.
// On part des vraies affiches et des vrais scores aller connus, puis le jeu simule le retour.
// Les clubs absents du snapshot joueur restent des adversaires officiels non sélectionnables.
const UEFA_UCL_PLAYOFFS=[
 ["UEFA_PO_LEVSKI","2441",0,0],
 ["419","239",2,2],
 ["36","1041",1,1],
 ["UEFA_PO_HAPOEL","UEFA_PO_SABAH",2,1],
 ["371","413",3,0],
 ["540","UEFA_PO_CELJE",1,1],
 ["467","501",1,3]
];
function uefaPower(id){
 const a=(CLUB_PLAYERS.get(id)||[]).slice().sort((x,y)=>y.rating-x.rating).slice(0,18);
 return a.length>=11?a.reduce((z,p)=>z+p.rating,0)/a.length:68;
}
function resolveUclPlayoff([home,away,h1,a1]){
 // Retour : l'équipe "away" reçoit. Simulation déterministe à partir de la force du snapshot.
 const ph=uefaPower(home),pa=uefaPower(away);
 let h2=Math.max(0,Math.round(1.25+(pa-ph)/18));
 let a2=Math.max(0,Math.round(1.15+(ph-pa)/18));
 let H=h1+a2,A=a1+h2;
 if(H===A){ if(ph>=pa)H++; else A++; }
 return H>A?home:away;
}
const ucl=[...UEFA_UCL_DIRECT,...UEFA_UCL_PLAYOFFS.map(resolveUclPlayoff)];

// La phase de ligue Conference League 2026/27 n'est pas encore connue au 20/08 :
// ses barrages se terminent le 27/08. On conserve temporairement le pool jouable,
// mais il n'est plus utilisé pour fabriquer la Champions League.
const uclSet=new Set(ucl);
const uecl=eur.filter(c=>!uclSet.has(c.id)).slice(0,36).map(c=>c.id);
const worldNations=["NT_CZE", "NT_MEX", "NT_RSA", "NT_KOR", "NT_BIH", "NT_CAN", "NT_QAT", "NT_SUI", "NT_BRA", "NT_HAI", "NT_MAR", "NT_SCO", "NT_AUS", "NT_PAR", "NT_TUR", "NT_USA", "NT_CUW", "NT_ECU", "NT_GER", "NT_CIV", "NT_JPN", "NT_NED", "NT_SWE", "NT_TUN", "NT_BEL", "NT_EGY", "NT_IRN", "NT_NZL", "NT_CPV", "NT_KSA", "NT_ESP", "NT_URU", "NT_FRA", "NT_IRQ", "NT_NOR", "NT_SEN", "NT_ALG", "NT_ARG", "NT_AUT", "NT_JOR", "NT_COL", "NT_COD", "NT_POR", "NT_UZB", "NT_CRO", "NT_ENG", "NT_GHA", "NT_PAN"];
const officialEuroNations=["NT_CZE", "NT_BIH", "NT_SUI", "NT_SCO", "NT_TUR", "NT_GER", "NT_NED", "NT_SWE", "NT_BEL", "NT_ESP", "NT_FRA", "NT_NOR", "NT_AUT", "NT_POR", "NT_CRO", "NT_ENG"];
function nationOf(p){return String(p.nation||"").split(/\s{2,}/)[0].trim()}
const byNation={};for(const p of FOOTBALL_DATA.players){if(p.nationalCopy)continue;const n=nationOf(p);if(!n)continue;(byNation[n]||(byNation[n]=[])).push(p)}
const POS_GROUP={GK:new Set(["GK"]),DF:new Set(["CB","LB","RB","LWB","RWB"]),MF:new Set(["DM","CDM","CM","CAM","AM","LM","RM"]),FW:new Set(["LW","RW","CF","ST","SS"])};
function balancedSquad(ps){const sorted=ps.slice().sort((a,b)=>b.rating-a.rating),used=new Set(),out=[];const take=(set,n)=>{for(const p of sorted){if(n<=0)break;if(!used.has(p.id)&&set.has(p.pos)){out.push(p);used.add(p.id);n--}}};take(POS_GROUP.GK,3);take(POS_GROUP.DF,8);take(POS_GROUP.MF,8);take(POS_GROUP.FW,7);for(const p of sorted){if(out.length>=26)break;if(!used.has(p.id)){out.push(p);used.add(p.id)}}return out.slice(0,26)}
function ensureEuroNation(nation){let existing=FOOTBALL_DATA.clubs.find(c=>c.national&&c.country===nation);if(existing)return existing.id;const ps=byNation[nation]||[];if(ps.length<23)return null;const id="NT_EURO_"+nation.replace(/[^A-Za-z0-9]/g,"_");FOOTBALL_DATA.clubs.push({id,name:NATION_FR[nation]||nation,country:nation,competition:"Sélection nationale",division:"Sélection nationale",competitionId:"NATIONAL",countryFr:NATION_FR[nation]||nation,competitionFr:"Sélection nationale",national:true});for(const p of balancedSquad(ps))FOOTBALL_DATA.players.push({...p,id:`${id}_${p.id}`,clubId:id,club:NATION_FR[nation]||nation,nationalCopy:true});return id}
const euroCandidates=Object.keys(byNation).filter(n=>EUROPE.has(n)).map(n=>({n,score:(byNation[n].slice().sort((a,b)=>b.rating-a.rating).slice(0,18).reduce((z,p)=>z+p.rating,0)/Math.max(1,Math.min(18,byNation[n].length)))})).sort((a,b)=>b.score-a.score);
const euroNations=officialEuroNations.slice();for(const x of euroCandidates){if(euroNations.length>=24)break;const id=ensureEuroNation(x.n);if(id&&!euroNations.includes(id))euroNations.push(id)}
window.FOOTBALL_TOURNAMENTS=[
 {id:"ucl",name:"UEFA Champions League",kind:"league36",teams:ucl,leagueMatches:8,description:"UEFA 2026/27 : 29 qualifiés directs officiels + 7 vainqueurs des vrais barrages UEFA simulés à partir des scores aller connus. 36 clubs, 8 matchs de phase de ligue."},
 {id:"uecl",name:"UEFA Conference League",kind:"league36",teams:uecl,leagueMatches:6,description:"36 clubs, 6 matchs de phase de ligue. 1-8 en huitièmes, 9-24 en barrages."},
 {id:"euro",name:"UEFA EURO",kind:"groups",teams:euroNations,groups:6,groupSize:4,bestThird:4,description:"24 sélections, 6 groupes de 4. Les deux premiers et les 4 meilleurs troisièmes vont en huitièmes."},
 {id:"worldcup",name:"Coupe du monde FIFA",kind:"groups",teams:worldNations,groups:12,groupSize:4,bestThird:8,description:"48 sélections, 12 groupes de 4. Les deux premiers et les 8 meilleurs troisièmes vont en seizièmes."}
];
})();
