// supabase-sync.jsx — branchement de l'app sur Supabase (auth + chargement + synchro + leads temps réel)
// La base est partagée : les 3 rôles (partenaire / admin / client) lisent le même store, donc relier le store = relier les 3.
(function(){
  const SUPA_URL = 'https://bxkzhyxdmtfutsaogxxk.supabase.co';
  const SUPA_ANON = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ4a3poeXhkbXRmdXRzYW9neHhrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3ODEyNTQ5MTYsImV4cCI6MjA5NjgzMDkxNn0.IWdomNH2VYgkK7pxjf1C6vaiLAgmdZiiio6Q7-ElZuE';

  // Modèle table ↔ store (appKey -> colonne). Tout le reste est identique.
  const TBL = {
    moves:    { table:'demenagements', fields:{ id:'id', num:'num', createdAt:'created_at', prenom:'prenom', nom:'nom', tel:'tel', email:'email', source:'source', partenaire:'partenaire', partnerId:'partner_id', formule:'formule', statut:'statut', formulaireType:'formulaire_type', volume:'volume', cartons:'cartons', notes:'notes', montant:'montant', cout:'cout', acompte:'acompte', date:'date', heure:'heure', rdvDate:'rdv_date', rdvHeure:'rdv_heure', acompteRecu:'acompte_recu', soldeRecu:'solde_recu', commissionVerse:'commission_verse', avis:'avis', dep:'dep', arr:'arr', inventaire:'inventaire', options:'options', equipe:'equipe', km:'km', allerRetour:'aller_retour', peage:'peage', peageAR:'peage_ar', fraisMateriel:'frais_materiel', camions:'camions', fraisAnnexes:'frais_annexes', mainOeuvre:'main_oeuvre', kmApplique:'km_applique', flexibilite:'flexibilite', contactPref:'contact_pref', fragiles:'fragiles', demonter:'demonter', extId:'ext_id', devisEnvoyeAt:'devis_envoye_at', paieFrom:'paie_from', relanceN:'relance_n', relanceAt:'relance_at', relancePaieN:'relance_paie_n', relancePaieAt:'relance_paie_at' } },
    tasks:    { table:'taches',     fields:{ id:'id', createdAt:'created_at', title:'title', quad:'quad', done:'done', due:'due', auto:'auto', subtasks:'subtasks', kind:'kind', heure:'heure', lieu:'lieu' } },
    objectifs:{ table:'objectifs',  fields:{ id:'id', mois:'mois', libelle:'libelle', cible:'cible', type:'type' } },
    camions:  { table:'camions',    fields:{ id:'id', nom:'nom', immat:'immat', volume:'volume', entretien:'entretien', etat:'etat', kmInitial:'km_initial', conso:'conso', marque:'marque', modele:'modele', motorisation:'motorisation', taille:'taille', entretiens:'entretiens', chargeUtile:'charge_utile' } },
    equipe:   { table:'employes',   fields:{ id:'id', prenom:'prenom', nom:'nom', tel:'tel', role:'role', permis:'permis', actif:'actif', tarif:'tarif', tarifType:'tarif_type', aCamion:'a_camion', niveauExp:'niveau_exp' } },
    materiel: { table:'materiel',   fields:{ id:'id', nom:'nom', unites:'unites', prix:'prix', seuil:'seuil' } },
    charges:  { table:'charges',    fields:{ id:'id', date:'date', categorie:'categorie', libelle:'libelle', montant:'montant', recurrent:'recurrent', frequence:'frequence', canal:'canal' } },
    incidents:{ table:'incidents',  fields:{ id:'id', createdAt:'created_at', titre:'titre', desc:'description', client:'client', moveId:'move_id', date:'date', statut:'statut', assuranceRef:'assurance_ref', montant:'montant', assuranceNote:'assurance_note' } },
    documents:{ table:'documents',  fields:{ id:'id', createdAt:'created_at', nom:'nom', type:'type', kind:'kind', mois:'mois', chargeId:'charge_id', categorie:'categorie', libelle:'libelle', date:'date', taille:'taille', dataURL:'data_url' } },
    boxes:    { table:'boxes',       fields:{ id:'id', createdAt:'created_at', statut:'statut', client:'client', clientEmail:'client_email', tier:'tier', size:'size', price:'price', startDate:'start_date', months:'months', libre:'libre', moveId:'move_id' } },
  };
  const PARAM_FIELDS = { entreprise:'entreprise', email:'email', tel:'tel', adresse:'adresse', cp:'cp', ville:'ville', siret:'siret', tva:'tva', iban:'iban', objectifCA:'objectif_ca', coutDefautPct:'cout_defaut_pct', coutEssenceKm:'cout_essence_km', prixLitre:'prix_litre', consoDefaut:'conso_defaut', prixM3:'prix_m3', prixKmVente:'prix_km_vente', peageKm:'peage_km', cplCible:'cpl_cible' };

  // '' -> null : évite que les colonnes numériques (conso, volume, prix…) rejettent tout le lot
  const toRow=(fields,obj)=>{ const r={}; for(const k in fields){ if(obj[k]!==undefined){ let v=obj[k]; if(v==='') v=null; r[fields[k]]=v; } } return r; };
  const fromRow=(fields,row)=>{ const o={}; for(const k in fields){ const v=row[fields[k]]; if(v!==undefined) o[k]=v; } return o; };

  const SB = { client:null, ready:false, loading:false, user:null, known:{}, pending:false, _t:null };
  window.LBC_SB = SB;

  function ensureClient(){
    if(SB.client) return SB.client;
    if(!window.supabase || !window.supabase.createClient){ console.warn('[LBC] supabase-js non chargé'); return null; }
    SB.client = window.supabase.createClient(SUPA_URL, SUPA_ANON, { auth:{ persistSession:true, autoRefreshToken:true } });
    return SB.client;
  }

  SB.signUp = async (email,password,meta)=>{ const c=ensureClient(); if(!c) throw new Error('Supabase indisponible'); const { data, error } = await c.auth.signUp({ email, password, options:{ data: meta||{} } }); if(error) throw error; if(!data.session){ const r=await c.auth.signInWithPassword({ email, password }); if(r.error) throw new Error('Compte créé. Confirme ton email (ou désactive « Confirm email » dans Supabase), puis connecte-toi.'); } return true; };
  SB.signIn = async (email,password)=>{ const c=ensureClient(); if(!c) throw new Error('Supabase indisponible'); const { error } = await c.auth.signInWithPassword({ email, password }); if(error) throw error; return true; };
  SB.signOut = async ()=>{ const c=ensureClient(); if(c) await c.auth.signOut(); SB.ready=false; SB.profile=null; };
  SB.getProfile = async ()=>{ const c=ensureClient(); if(!c) return null; try{ const { data } = await c.auth.getUser(); const uid=data&&data.user&&data.user.id; if(!uid) return null; const { data:p } = await c.from('profiles').select('*').eq('id', uid).maybeSingle(); SB.profile=p||{ role:'partenaire', statut:'en_attente' }; return SB.profile; }catch(e){ return null; } };
  // Gestion des comptes (admin) : lister / mettre à jour les profils
  SB.listProfiles = async ()=>{ const c=ensureClient(); if(!c) return []; try{ const { data } = await c.from('profiles').select('*').order('created_at'); return data||[]; }catch(e){ return []; } };
  SB.updateProfile = async (id,patch)=>{ const c=ensureClient(); if(!c) return false; try{ const { error } = await c.from('profiles').update(patch).eq('id', id); return !error; }catch(e){ return false; } };

  // Charge toutes les tables -> store
  async function loadAll(){
    const c=ensureClient(); if(!c) return false;
    SB.loading=true;
    try{
      const db = window.LBC_INT.store.db;
      for(const sk in TBL){
        const { table, fields } = TBL[sk];
        const { data, error } = await c.from(table).select('*');
        if(error) throw error;
        db[sk] = (data||[]).map(row=>fromRow(fields,row));
        SB.known[sk] = db[sk].map(o=>o.id);
      }
      // paramètres (ligne unique)
      const { data:pdata } = await c.from('parametres').select('*').eq('id','default').maybeSingle();
      if(pdata){ const s={}; for(const k in PARAM_FIELDS){ const v=pdata[PARAM_FIELDS[k]]; if(v!==undefined&&v!==null) s[k]=v; } db.settings=Object.assign({}, db.settings, s); }
      window.LBC_INT.notify();
      SB.ready=true; SB.loading=false;
      return true;
    }catch(e){ console.warn('[LBC] loadAll échec', e.message||e); SB.loading=false; return false; }
  }

  // Pousse le store -> Supabase (upsert + suppression des disparus)
  async function pushAll(){
    const c=ensureClient(); if(!c||!SB.ready) return;
    const db = window.LBC_INT.store.db;
    try{
      for(const sk in TBL){
        const { table, fields } = TBL[sk];
        const rows=(db[sk]||[]).map(o=>toRow(fields,o));
        const ids=rows.map(r=>r.id);
        if(rows.length) await c.from(table).upsert(rows, { onConflict:'id' });
        const del=(SB.known[sk]||[]).filter(id=>ids.indexOf(id)<0);
        // garde-fou : ne jamais vider une collection entière par erreur (évite les pertes de données)
        if(del.length && (ids.length>0 || del.length<=3)) await c.from(table).delete().in('id', del);
        SB.known[sk]=ids;
      }
      // paramètres
      const prow={ id:'default' }; for(const k in PARAM_FIELDS){ if(db.settings[k]!==undefined){ let v=db.settings[k]; if(v==='') v=null; prow[PARAM_FIELDS[k]]=v; } }
      await c.from('parametres').upsert(prow, { onConflict:'id' });
    }catch(e){ console.warn('[LBC] pushAll échec', e.message||e); }
  }
  function schedulePush(){ if(!SB.ready||SB.loading) return; clearTimeout(SB._t); SB._t=setTimeout(pushAll, 700); }

  // Leads du site web -> déménagements
  async function processLeads(){
    const c=ensureClient(); if(!c) return;
    try{
      const { data } = await c.from('leads').select('*').eq('traite', false);
      const existing = new Set((window.LBC_INT.store.db.moves||[]).map(m=>m.extId).filter(Boolean).map(String));
      for(const row of (data||[])){
        const ext=String(row.id);
        if(existing.has(ext)){ await c.from('leads').update({ traite:true }).eq('id', row.id); continue; }
        if(row.payload){ window.LBC_INT.ingestWebLead(Object.assign({}, row.payload, { _extId:ext })); existing.add(ext); }
        await c.from('leads').update({ traite:true }).eq('id', row.id);
      }
    }catch(e){ console.warn('[LBC] processLeads', e.message||e); }
  }
  function subscribeLeads(){
    const c=ensureClient(); if(!c) return;
    try{
      c.channel('leads-rt').on('postgres_changes', { event:'INSERT', schema:'public', table:'leads' }, (payload)=>{
        const row=payload.new; if(row && row.payload){ window.LBC_INT.ingestWebLead(row.payload); c.from('leads').update({ traite:true }).eq('id', row.id); }
      }).subscribe();
    }catch(e){ console.warn('[LBC] subscribeLeads', e.message||e); }
  }

  // Badge "partenaires en attente" (admin)
  async function refreshPartnerBadge(){ const c=ensureClient(); if(!c) return; try{ const { count } = await c.from('profiles').select('id',{ count:'exact', head:true }).eq('role','partenaire').eq('statut','en_attente'); window.LBC_INT.store.db._partnersPending = count||0; window.LBC_INT.notify(); }catch(e){} }
  function subscribeProfiles(){ const c=ensureClient(); if(!c) return; try{ c.channel('prof-rt').on('postgres_changes', { event:'*', schema:'public', table:'profiles' }, ()=>refreshPartnerBadge()).subscribe(); }catch(e){} }
  SB.refreshPartnerBadge = refreshPartnerBadge;
  // Un partenaire (ou le site) dépose un lead dans la table leads -> l'admin l'ingère
  SB.submitLead = async (payload)=>{ const c=ensureClient(); if(!c) return false; try{ const { error } = await c.from('leads').insert({ payload }); return !error; }catch(e){ return false; } };
  // Crée le compte client (rôle client) et lui envoie un lien magique vers son espace
  // URL de retour propre (sans hash ni query) — évite le double encodage du token
  const REDIRECT = ()=> window.location.origin + window.location.pathname;
  SB.inviteClient = async (email)=>{ const c=ensureClient(); if(!c||!email) return { ok:false, error:'email manquant' }; try{ const { error } = await c.auth.signInWithOtp({ email, options:{ shouldCreateUser:true, data:{ role:'client' }, emailRedirectTo: REDIRECT() } }); return { ok:!error, error: error && error.message }; }catch(e){ return { ok:false, error:e.message }; } };
  // Lien de connexion par email pour un compte EXISTANT (re-connexion sans mot de passe)
  SB.magicLink = async (email)=>{ const c=ensureClient(); if(!c||!email) return { ok:false, error:'email manquant' }; try{ const { error } = await c.auth.signInWithOtp({ email, options:{ shouldCreateUser:false, emailRedirectTo: REDIRECT() } }); return { ok:!error, error: error && error.message }; }catch(e){ return { ok:false, error:e.message }; } };

  // Appelé après connexion réussie -> renvoie le profil (role/partenaire) ou null
  SB.afterLogin = async ()=>{
    const profile = await SB.getProfile();
    const ok = await loadAll();
    if(!ok) return null;
    window.LBC_INT.store.subs.add(schedulePush); // toute modif locale -> push (admin uniquement en pratique, RLS bloque le reste)
    // Identité partenaire dynamique (le partenaire voit son nom/sa société)
    if(profile && profile.role==='partenaire' && window.LBC_DATA && window.LBC_DATA.PARTNER){
      const P=window.LBC_DATA.PARTNER;
      if(profile.partenaire) P.company=profile.partenaire;
      if(profile.contact){ const parts=profile.contact.split(' '); P.firstName=parts[0]||P.firstName; P.lastName=parts.slice(1).join(' ')||''; P.initials=((P.firstName||'')[0]||'')+((P.lastName||'')[0]||''); }
    }
    // Identité client : on remplit le dossier avec SON vrai déménagement (chargé par RLS sur son email)
    if(profile && profile.role==='client' && window.CLIENT){
      const C=window.CLIENT; const mv=(window.LBC_INT.store.db.moves||[])[0];
      const addr=(s)=> s? [s.rue, [s.cp,s.ville].filter(Boolean).join(' ')].filter(Boolean).join(', ') : '';
      const fmtD=(d)=>{ try{ return d? new Date(d+'T00:00:00').toLocaleDateString('fr-FR',{day:'numeric',month:'long',year:'numeric'}) : ''; }catch(e){ return d||''; } };
      if(mv){
        C.firstName=mv.prenom||''; C.lastName=mv.nom||''; C.ref=mv.id;
        C.from=mv.dep.ville||''; C.to=mv.arr.ville||''; C.fromAddr=addr(mv.dep); C.toAddr=addr(mv.arr);
        C.date=fmtD(mv.date); C.window=mv.heure?(mv.heure+' – '+(parseInt(mv.heure)+4)+' h'):'';
        C.rooms=mv.volume?('≈ '+mv.volume+' m³'):''; C.notes=mv.notes||'';
        C.formule=mv.formule||'standard'; C.devis=Number(mv.montant)||0; C.partner=mv.partenaire||'';
        // détail complet pour l'espace client (devis détaillé)
        C.volume=mv.volume||''; C.cartons=mv.cartons||''; C.km=mv.km||''; C.flexibilite=mv.flexibilite||'';
        C.acompte=Number(mv.acompte)||0; C.statut=mv.statut||'';
        C.inventaire=Array.isArray(mv.inventaire)?mv.inventaire:[];
        C.fragiles=Array.isArray(mv.fragiles)?mv.fragiles:[];
        C.demonter=Array.isArray(mv.demonter)?mv.demonter:[];
        C.dep=mv.dep||{}; C.arr=mv.arr||{};
      } else {
        C.firstName=(profile.email||'client').split('@')[0]; C.lastName=''; C.ref='';
        C.from=''; C.to=''; C.fromAddr=''; C.toAddr=''; C.date=''; C.window=''; C.rooms=''; C.notes=''; C.devis=0; C.partner='';
        C.volume=''; C.cartons=''; C.km=''; C.flexibilite=''; C.acompte=0; C.statut=''; C.inventaire=[]; C.fragiles=[]; C.demonter=[]; C.dep={}; C.arr={};
      }
      { const st=(window.LBC_INT.store.db.settings)||{}; C.advisor={ name:st.ownerName||'Votre conseiller LBC', phone:st.tel||'' }; }
      { const bx=(window.LBC_INT.store.db.boxes||[]).find(b=>b.statut==='actif' && (b.clientEmail===(profile&&profile.email) || (mv&&b.moveId===mv.id)));
        C.storage = bx ? { active:true, tier:bx.tier, box:'Box '+bx.tier, size:bx.size, volume:'', price:bx.price, since:fmtD(bx.startDate), startDate:bx.startDate, months:bx.months, libre:bx.libre, nextBill:window.LBC_INT.boxNextBill(bx), cumul:window.LBC_INT.boxFactureCumul(bx), site:'Entrepôt LBC sécurisé · Nice' } : { active:false }; }
      C.referral={ code:((C.firstName||'CLIENT').toUpperCase().replace(/\s/g,''))+'50', reward:50, earned:0, pending:0, friends:[] };
    }
    if(profile && profile.role==='admin'){ await processLeads(); subscribeLeads(); await refreshPartnerBadge(); subscribeProfiles(); }
    return profile || { role:'partenaire', statut:'en_attente' };
  };

  // Remplit le dossier client (window.CLIENT) depuis un déménagement — utilisé pour l'aperçu Client de l'admin
  SB.fillClientFromMove = (mv)=>{
    const C=window.CLIENT; if(!C) return;
    const addr=(s)=> s? [s.rue, [s.cp,s.ville].filter(Boolean).join(' ')].filter(Boolean).join(', ') : '';
    const fmtD=(d)=>{ try{ return d? new Date(d+'T00:00:00').toLocaleDateString('fr-FR',{day:'numeric',month:'long',year:'numeric'}) : ''; }catch(e){ return d||''; } };
    if(mv){
      C.firstName=mv.prenom||''; C.lastName=mv.nom||''; C.ref=mv.id;
      C.from=(mv.dep&&mv.dep.ville)||''; C.to=(mv.arr&&mv.arr.ville)||''; C.fromAddr=addr(mv.dep); C.toAddr=addr(mv.arr);
      C.date=fmtD(mv.date); C.window=mv.heure?(mv.heure+' – '+(parseInt(mv.heure)+4)+' h'):'';
      C.rooms=mv.volume?('≈ '+mv.volume+' m³'):''; C.notes=mv.notes||'';
      C.formule=mv.formule||'standard'; C.devis=Number(mv.montant)||0; C.partner=mv.partenaire||'';
      C.volume=mv.volume||''; C.cartons=mv.cartons||''; C.km=mv.km||''; C.flexibilite=mv.flexibilite||'';
      C.acompte=Number(mv.acompte)||0; C.statut=mv.statut||'';
      C.inventaire=Array.isArray(mv.inventaire)?mv.inventaire:[];
      C.fragiles=Array.isArray(mv.fragiles)?mv.fragiles:[];
      C.demonter=Array.isArray(mv.demonter)?mv.demonter:[];
      C.dep=mv.dep||{}; C.arr=mv.arr||{};
      { const st=(window.LBC_INT.store.db.settings)||{}; C.advisor={ name:st.ownerName||'Votre conseiller LBC', phone:st.tel||'' }; }
      { const bx=(window.LBC_INT.store.db.boxes||[]).find(b=>b.statut==='actif' && (b.moveId===mv.id || (b.client||'')===((mv.prenom||'')+' '+(mv.nom||'')).trim()));
        C.storage = bx ? { active:true, tier:bx.tier, box:'Box '+bx.tier, size:bx.size, volume:'', price:bx.price, since:fmtD(bx.startDate), startDate:bx.startDate, months:bx.months, libre:bx.libre, nextBill:window.LBC_INT.boxNextBill(bx), cumul:window.LBC_INT.boxFactureCumul(bx), site:'Entrepôt LBC sécurisé · Nice' } : { active:false }; }
    }
  };

  // Reprise de session : lien magique (token dans le hash) OU session déjà active.
  // Tolère un hash mal encodé (#%23access_token=…) en le décodant et en posant la session à la main.
  SB.restore = async ()=>{ const c=ensureClient(); if(!c) return null;
    try{
      let raw = (window.location.hash||'').replace(/^#/, '');
      if(raw.indexOf('access_token')===-1 && raw.indexOf('%23')!==-1){ try{ raw = decodeURIComponent(raw); }catch(e){} }
      raw = raw.replace(/^#/, '');
      if(raw.indexOf('access_token')!==-1){
        const p = new URLSearchParams(raw); const at=p.get('access_token'), rt=p.get('refresh_token');
        if(at && rt){ const { error } = await c.auth.setSession({ access_token:at, refresh_token:rt });
          try{ window.history.replaceState(null,'',window.location.origin+window.location.pathname); }catch(e){}
          if(!error){ const { data } = await c.auth.getSession(); if(data&&data.session){ SB.user=data.session.user; return await SB.afterLogin(); } }
        }
      }
    }catch(e){ console.warn('[LBC] restore hash', e&&e.message); }
    try{ const { data } = await c.auth.getSession(); if(data && data.session){ SB.user=data.session.user; return await SB.afterLogin(); } }catch(e){}
    return null; };

  /* ---------- Écran de connexion (React) ---------- */
  const I_FIELD = { width:'100%', height:42, borderRadius:9, border:'1px solid var(--border-2)', background:'var(--bg)', padding:'0 12px', fontSize:14, margin:'6px 0 14px' };
  window.SbLogin = function SbLogin({ onLogin }){
    const { useState } = React;
    const [mode,setMode]=useState('login');
    const [email,setEmail]=useState(''); const [pw,setPw]=useState('');
    const [role,setRole]=useState('partenaire'); const [company,setCompany]=useState(''); const [contact,setContact]=useState('');
    const [busy,setBusy]=useState(false); const [err,setErr]=useState(''); const [info,setInfo]=useState('');
    const sendMagic=async()=>{ setErr(''); setInfo(''); if(!email){ setErr('Entre ton email pour recevoir le lien'); return; } setBusy(true); try{ const r=await SB.magicLink(email); if(r.ok) setInfo('Lien de connexion envoyé à '+email+'. Regarde tes emails (et les spams).'); else setErr(r.error||'Aucun compte avec cet email, ou envoi impossible.'); }catch(e){ setErr(e.message); } finally{ setBusy(false); } };
    const submit=async()=>{ setErr(''); setInfo(''); if(!email||!pw){ setErr('Email et mot de passe requis'); return; }
      if(mode==='signup' && role==='partenaire' && !company.trim()){ setErr('Indique le nom de ta société'); return; }
      setBusy(true);
      try{
        if(mode==='login'){ await SB.signIn(email,pw); }
        else { await SB.signUp(email,pw,{ role, company:role==='partenaire'?company:'', contact }); }
        const profile=await SB.afterLogin();
        if(!profile){ setErr('Connexion OK mais chargement impossible.'); return; }
        if(mode==='signup' && role==='partenaire' && profile.statut!=='actif'){ setInfo('Compte créé. En attente de validation par LBC pour accéder à tes leads.'); }
        onLogin(profile);
      }catch(e){ setErr(e.message||'Échec'); } finally{ setBusy(false); } };
    return (
      <div style={{ height:'100%', display:'flex', alignItems:'center', justifyContent:'center', background:'var(--navy-deep)', padding:24 }}>
        <div style={{ width:'min(400px,100%)', background:'var(--card)', borderRadius:16, padding:'30px 28px', boxShadow:'var(--shadow-pop)' }}>
          <div style={{ fontSize:20, fontWeight:800, color:'var(--ink)' }}>LBC Déménagement</div>
          <div style={{ fontSize:13, color:'var(--muted)', marginTop:4, marginBottom:20 }}>{mode==='login'?'Connecte-toi à ton espace.':'Crée ton compte.'}</div>
          {mode==='signup' && (
            <div style={{ marginBottom:14 }}>
              <label style={{ fontSize:12.5, fontWeight:600, color:'var(--ink-2)' }}>Je suis</label>
              <div style={{ display:'flex', gap:8, marginTop:6 }}>
                {[['partenaire','Partenaire'],['client','Client']].map(([k,l])=>(
                  <button key={k} type="button" onClick={()=>setRole(k)} style={{ flex:1, height:38, borderRadius:9, fontSize:13, fontWeight:600, border:'1px solid', borderColor:role===k?'var(--brand)':'var(--border-2)', background:role===k?'var(--brand)':'var(--bg)', color:role===k?'#fff':'var(--ink-2)' }}>{l}</button>))}
              </div>
            </div>)}
          <label style={{ fontSize:12.5, fontWeight:600, color:'var(--ink-2)' }}>Email</label>
          <input value={email} onChange={e=>setEmail(e.target.value)} type="email" autoFocus style={I_FIELD} />
          <label style={{ fontSize:12.5, fontWeight:600, color:'var(--ink-2)' }}>Mot de passe</label>
          <input value={pw} onChange={e=>setPw(e.target.value)} type="password" onKeyDown={e=>{ if(e.key==='Enter') submit(); }} style={I_FIELD} />
          {mode==='signup' && role==='partenaire' && (<>
            <label style={{ fontSize:12.5, fontWeight:600, color:'var(--ink-2)' }}>Société</label>
            <input value={company} onChange={e=>setCompany(e.target.value)} placeholder="Ex : Agence Riviera Immobilier" style={I_FIELD} />
            <label style={{ fontSize:12.5, fontWeight:600, color:'var(--ink-2)' }}>Contact (prénom nom)</label>
            <input value={contact} onChange={e=>setContact(e.target.value)} style={I_FIELD} />
          </>)}
          {err && <div style={{ fontSize:12.5, color:'var(--perdu)', marginBottom:12 }}>{err}</div>}
          {info && <div style={{ fontSize:12.5, color:'var(--accepte)', marginBottom:12 }}>{info}</div>}
          <button onClick={submit} disabled={busy} style={{ width:'100%', height:44, borderRadius:10, background:'var(--brand)', color:'#fff', fontSize:14.5, fontWeight:700, opacity:busy?.6:1, marginTop:2 }}>{busy?'…':(mode==='login'?'Se connecter':'Créer le compte')}</button>
          {mode==='login' && <>
            <div style={{ display:'flex', alignItems:'center', gap:10, margin:'14px 0 12px', color:'var(--faint)', fontSize:11.5 }}><span style={{ flex:1, height:1, background:'var(--border)' }} />ou<span style={{ flex:1, height:1, background:'var(--border)' }} /></div>
            <button onClick={sendMagic} disabled={busy} style={{ width:'100%', height:42, borderRadius:10, background:'var(--bg)', border:'1px solid var(--border-2)', color:'var(--ink-2)', fontSize:13.5, fontWeight:600, display:'inline-flex', alignItems:'center', justifyContent:'center', gap:8 }}><Icon name="mail" size={16} />Recevoir un lien de connexion par email</button>
            <div style={{ fontSize:11.5, color:'var(--muted)', textAlign:'center', marginTop:8 }}>Idéal pour les clients (sans mot de passe).</div>
          </>}
          <div style={{ textAlign:'center', marginTop:14, fontSize:12.5, color:'var(--muted)' }}>
            {mode==='login'? <>Pas encore de compte ? <a onClick={()=>setMode('signup')} style={{ color:'var(--brand)', fontWeight:600, cursor:'pointer' }}>Créer un compte</a></>
                            : <>Déjà un compte ? <a onClick={()=>setMode('login')} style={{ color:'var(--brand)', fontWeight:600, cursor:'pointer' }}>Se connecter</a></>}
          </div>
        </div>
      </div>
    );
  };
})();
