document.addEventListener('DOMContentLoaded',()=>{const norm=v=>v.toLocaleUpperCase('fr-FR').normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[^A-Z0-9]/g,'');const n=document.querySelector('[name="nom"]'),p=document.querySelector('[name="prenom"]'),out=document.getElementById('codePreview');function code(){if(!n||!p||!out)return;let nom=norm(n.value).slice(0,11),pre=norm(p.value),r=Math.max(1,12-nom.length);out.textContent=('C'+nom+pre.slice(0,r)).slice(0,13)||'C…';}document.querySelectorAll('[data-uppercase]').forEach(i=>i.addEventListener('input',()=>{i.value=i.value.toLocaleUpperCase('fr-FR');code();}));document.querySelectorAll('[data-firstname]').forEach(i=>i.addEventListener('blur',()=>{i.value=i.value.toLocaleLowerCase('fr-FR').replace(/(^|[\s\-'])(\p{L})/gu,(m,s,l)=>s+l.toLocaleUpperCase('fr-FR'));code();}));document.querySelectorAll('[data-phone]').forEach(i=>i.addEventListener('blur',()=>{let d=i.value.replace(/\D/g,'');if(d.startsWith('0033'))d=d.slice(4);else if(d.startsWith('33'))d=d.slice(2);else if(d.startsWith('0'))d=d.slice(1);if(d.length===9)i.value='+33 '+d[0]+' '+d.slice(1,3)+' '+d.slice(3,5)+' '+d.slice(5,7)+' '+d.slice(7,9);}));code();});