// === Escena 05 — "El equipo" ===
// «Leaderboard — Pipeline por Comercial»: insignia #N, nombre, «(N opp.)», total en verde y
// «(N € ponderado)» en azul (Dashboard:145-155). Sin paginar (:143) y ordenado por el PONDERADO
// (:254): por eso el primero tiene menos total que el segundo, y se ve sin que la voz lo explique.
// Solo entra la oportunidad con comercial asignado (:244) — de ahí que el reparto sume 214.900 €
// y 27 frente a los 231.700 € y 31 de los marcadores. Y la ayuda vive plegada tras un «?»
// (CardTitle:38-43, 47-70): en pantalla, por defecto, solo hay un interrogante.
function EscenaPN05({ t }){
  // LOCUCIÓN (audio/escena-05.mp3): el reparto por comercial → puesto y nombre → total en verde →
  // pausa → un detalle que cambia la lectura → no se ordena por el total → se ordena por el
  // ponderado → el que más apunta → solo con comercial asignado → detrás del interrogante.
  const T = {
    reparto:0.3,      // "El reparto por comercial" -> la tarjeta del leaderboard entra
    filas:3.0,        // "su puesto, su nombre" -> las 3 filas en cascada; insignias #1 #2 #3
    filaStep:0.16,     // (incremento de la cascada de filas)
    cifras:7.8,       // "su total en verde" -> los totales y los ponderados aterrizan
    detalle:11.6,     // "un detalle que cambia la lectura" -> nada se mueve: el beat es la calma
    noTotal:14.1,     // "no se ordena por el total" -> la columna verde, tachada como criterio
    porPond:16.0,     // "Se ordena por el ponderado" -> la columna azul se subraya; flecha ↓
    apunta:19.7,      // "el que más apunta" -> el 96.400 de la fila 2 pulsa sobre el 74.200
    asignado:23.3,    // "llevan comercial asignado" -> 4 oportunidades sin cara caen fuera
    caraStep:0.11,     // (incremento de la caída de las cuatro sin comercial)
    interrogante:27.8,// "detrás del interrogante" -> el botón «?»; el panel se despliega
    salida:32.6,      // "no te está diciendo" -> salida: tres huecos numerados esperando
  };

  const LEADER = [
    { pos:'#1', nom:'Sergio Lamas', opp:'(7 opp.)',  tot:'74.200 €', pon:'(44.900 € ponderado)' },
    { pos:'#2', nom:'Nuria Peláez', opp:'(11 opp.)', tot:'96.400 €', pon:'(28.100 € ponderado)' },
    { pos:'#3', nom:'Iván Castro',  opp:'(9 opp.)',  tot:'44.300 €', pon:'(23.300 € ponderado)' },
  ];

  const Ico = ({ n, s=24, c='#0F172A', w=2 })=>{
    const P = {
      leader:<><path d="M4 18 H20"/><path d="M6.5 18 V10 M12 18 V5.5 M17.5 18 V13"/></>,
      user:<><circle cx="12" cy="8.4" r="3.8"/><path d="M4.8 19.4 C 5.8 15.6, 8.6 14, 12 14 C 15.4 14, 18.2 15.6, 19.2 19.4"/></>,
      help:<><circle cx="12" cy="12" r="9.2"/><path d="M9.4 9.4 A2.7 2.7 0 1 1 12 12.6 V14.2"/><circle cx="12" cy="17.4" r="0.95" fill={c} stroke="none"/></>,
    };
    return <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth={w} strokeLinecap="round" strokeLinejoin="round">{P[n]}</svg>;
  };

  // ── Geometría de la fila (contenido 1640 dentro de la tarjeta de 1700) ──
  const COL = { badge:76, opp:140, tot:200, pon:300, gap:24 };
  const CARD_Y = 290, HEAD_H = 84, ROW_H = 96;
  const X_TOT = 1256, X_PON = 1480;   // x en el lienzo de las dos columnas de cifras

  // ── Progresos ──
  const rpP = clamp((t-T.reparto)/0.8,0,1), rpE = Easing.easeOutBack(rpP);
  const noTotP = clamp((t-T.noTotal)/0.5,0,1);
  const noTotTacha = Easing.easeInOutCubic(clamp((t-T.noTotal-0.2)/0.7,0,1));
  const ponP = clamp((t-T.porPond)/0.5,0,1);
  const ponSub = Easing.easeInOutCubic(clamp((t-T.porPond-0.15)/0.7,0,1));
  const flecha = Easing.easeInOutCubic(clamp((t-T.porPond-0.4)/0.9,0,1));
  const pulso = t >= T.apunta && t < T.apunta+1.4 ? Math.sin((t-T.apunta)/1.4*Math.PI) : 0;
  const asP = clamp((t-T.asignado)/0.6,0,1);
  const anexosOut = clamp((t-T.interrogante+0.5)/0.5,0,1);
  const ayudaP = clamp((t-T.interrogante)/0.7,0,1), ayudaE = Easing.easeOutCubic(ayudaP);
  const ayudaH = 152*ayudaE;
  const salP = clamp((t-T.salida)/0.8,0,1), salE = Easing.easeOutCubic(salP);

  const cursor = [
    { t:0.5,  x:1860, y:1035 },
    { t:26.9, x:1860, y:1035 },
    { t:27.7, x:1758, y:332 },
    { t:31.6, x:1758, y:332 },
    { t:32.6, x:1860, y:1035 },
  ];

  return (
    <SlideCanvas variant="light">
      <div style={{opacity:1-salE*0.65}}>
        <DemoHeader t={t} eyebrow="EL REPARTO" title={<span>Quién lleva cada cosa</span>}/>
      </div>

      <div style={{position:'absolute', left:80, top:130, width:1760, height:890,
                   borderRadius:18, border:'1px solid #E2E8F0', background:'#F6F8FB',
                   boxShadow:'0 34px 84px rgba(15,23,42,0.13)', overflow:'hidden',
                   opacity:1-salE*0.62}}>
        <BrowserChrome url="play.dinaup.com/App/CRM" title="CRM — Dinaup"/>
        <DinaupAppHeader breadcrumb={["CRM","Inicio"]} company="Suministros del Norte, S.L." initials="NP"/>

        {/* ── Leaderboard — Pipeline por Comercial ── */}
        <div style={{position:'absolute', left:30, top:CARD_Y-130, width:1700,
                     background:'#fff', borderRadius:16, border:'1px solid #E2E8F0', overflow:'hidden',
                     boxShadow:'0 22px 56px rgba(15,23,42,0.12)',
                     opacity:rpP, transform:`translateY(${(1-rpE)*28}px)`}}>
          <div style={{display:'flex', alignItems:'center', gap:14, height:HEAD_H, padding:'0 30px', borderBottom:'1px solid #EEF2F6'}}>
            <Ico n="leader" s={28} c="#1565c0" w={2.2}/>
            <span className="f-plex" style={{fontSize:30, fontWeight:700, color:'#0F172A'}}>Leaderboard — Pipeline por Comercial</span>
            <div style={{flex:1}}/>
            <span style={{width:44, height:44, borderRadius:99, display:'flex', alignItems:'center', justifyContent:'center',
                          border:`1.6px solid ${ayudaP > 0 ? '#1565c0' : '#CBD5E1'}`,
                          background:ayudaP > 0 ? 'rgba(21,101,192,0.10)' : '#fff',
                          transform:`scale(${lerp(1,1.1,ayudaP)})`}}>
              <Ico n="help" s={26} c={ayudaP > 0 ? '#1565c0' : '#64748B'} w={2}/>
            </span>
          </div>

          {/* El panel de ayuda, plegado hasta que se pulsa el «?» */}
          <div style={{height:ayudaH, overflow:'hidden', background:'rgba(21,101,192,0.05)',
                       borderBottom:ayudaH > 2 ? '1px solid rgba(21,101,192,0.18)' : 'none'}}>
            <div style={{padding:'26px 30px', opacity:clamp((ayudaE-0.35)/0.5,0,1)}}>
              <span className="f-plex" style={{fontSize:28, color:'#334155', lineHeight:1.45}}>
                Solo oportunidades abiertas. El valor ponderado se calcula multiplicando el importe
                estimado por la probabilidad de cierre de cada oportunidad.
              </span>
            </div>
          </div>

          {LEADER.map((r,i)=>{
            const e = Easing.easeOutCubic(clamp((t-T.filas-i*T.filaStep)/0.5,0,1));
            const cf = clamp((t-T.cifras-i*0.12)/0.5,0,1);
            const cfE = Easing.easeOutBack(cf);
            const glow = i === 1 ? pulso : 0;
            return (
              <div key={r.nom} style={{display:'flex', alignItems:'center', gap:COL.gap, height:ROW_H, padding:'0 30px',
                                       borderBottom:i < 2 ? '1px solid #F1F5F9' : 'none',
                                       opacity:clamp(e*1.25,0,1), transform:`translateY(${(1-e)*14}px)`}}>
                <span className="f-plex" style={{width:COL.badge, fontSize:24, fontWeight:800, color:'#fff',
                                                 background:i === 0 ? '#1565c0' : '#94A3B8', borderRadius:99,
                                                 textAlign:'center', padding:'6px 0'}}>{r.pos}</span>
                <span className="f-plex" style={{flex:1, fontSize:28, fontWeight:700, color:'#0F172A'}}>{r.nom}</span>
                <span className="f-plex" style={{width:COL.opp, fontSize:20, color:'#94A3B8', textAlign:'right'}}>{r.opp}</span>
                <span className="f-plex" style={{width:COL.tot, fontSize:28, fontWeight:700, color:'#2e7d32', textAlign:'right',
                                                 opacity:cf, transform:`translateY(${(1-cfE)*10}px)`,
                                                 textShadow:0 < glow ? `0 0 ${glow*26}px rgba(46,125,50,0.85)` : 'none'}}>{r.tot}</span>
                <span style={{width:COL.pon, textAlign:'right', position:'relative'}}>
                  <span className="f-plex" style={{fontSize:22, color:'#1565c0', opacity:cf,
                                                   transform:`translateY(${(1-cfE)*10}px)`, display:'inline-block'}}>{r.pon}</span>
                  <span style={{position:'absolute', left:0, right:0, bottom:-9, height:3, borderRadius:2, background:'#1565c0',
                                transform:`scaleX(${clamp((ponSub-i*0.12)*1.35,0,1)})`, transformOrigin:'right'}}/>
                </span>
              </div>
            );
          })}
        </div>

        {/* La columna del total, tachada como criterio de orden */}
        {noTotP > 0.01 && (
          <div style={{position:'absolute', left:X_TOT-80, top:CARD_Y-130+HEAD_H+ayudaH, width:COL.tot, height:ROW_H*3,
                       background:`rgba(239,68,68,${0.06*noTotP})`, borderRadius:10, opacity:1-anexosOut}}>
            {[0,1,2].map(i=>(
              <span key={i} style={{position:'absolute', left:6, right:6, top:ROW_H*i+ROW_H/2, height:3, borderRadius:2,
                                    background:'#EF4444', transform:`scaleX(${clamp((noTotTacha-i*0.12)*1.35,0,1)})`, transformOrigin:'right'}}/>
            ))}
          </div>
        )}
      </div>

      {/* ─────────── Los dos criterios, bajo sus columnas ─────────── */}
      {noTotP > 0.01 && (
        <div style={{position:'absolute', left:X_TOT-30, top:702, width:260, textAlign:'center',
                     opacity:noTotP*(1-anexosOut), transform:`translateY(${(1-Easing.easeOutCubic(noTotP))*10}px)`}}>
          <span className="f-plex" style={{display:'inline-block', fontSize:23, fontWeight:600, color:'#B42318',
                                           background:'rgba(239,68,68,0.10)', border:'1.4px solid rgba(239,68,68,0.3)',
                                           borderRadius:99, padding:'8px 18px'}}>no es el criterio</span>
        </div>
      )}
      {ponP > 0.01 && (
        <div style={{position:'absolute', left:X_PON, top:686, width:300, textAlign:'center', opacity:ponP*(1-anexosOut)}}>
          <svg width="34" height="40" viewBox="0 0 34 40" style={{display:'block', margin:'0 auto 8px'}}>
            <path d="M17 2 V30" stroke="#1565c0" strokeWidth="4" strokeLinecap="round"
                  strokeDasharray="28" strokeDashoffset={28*(1-flecha)}/>
            <path d="M8 22 L17 31 L26 22" stroke="#1565c0" strokeWidth="4" fill="none" strokeLinecap="round" strokeLinejoin="round"
                  opacity={clamp((flecha-0.75)/0.25,0,1)}/>
          </svg>
          <span className="f-plex" style={{display:'inline-block', fontSize:23, fontWeight:700, color:'#1565c0',
                                           background:'rgba(21,101,192,0.10)', border:'1.4px solid rgba(21,101,192,0.32)',
                                           borderRadius:99, padding:'8px 18px'}}>este es el criterio</span>
        </div>
      )}

      {/* ─────────── Las cuatro que no llevan comercial se quedan fuera ─────────── */}
      {asP > 0.01 && (
        <div style={{position:'absolute', left:110, top:844, width:1700, opacity:asP*(1-salP)}}>
          <div style={{display:'flex', alignItems:'center', gap:34}}>
            <div style={{width:620, border:'3px dashed #CBD5E1', borderRadius:14, padding:'18px 20px', background:'rgba(255,255,255,0.7)'}}>
              <div className="f-plex" style={{fontSize:21, fontWeight:700, letterSpacing:'1.6px', color:'#94A3B8'}}>SIN COMERCIAL ASIGNADO</div>
              <div style={{display:'flex', gap:14, marginTop:14}}>
                {[0,1,2,3].map(i=>{
                  const e = Easing.easeOutCubic(clamp((t-T.asignado-0.2-i*T.caraStep)/0.55,0,1));
                  return (
                    <div key={i} style={{flex:1, display:'flex', alignItems:'center', gap:10, padding:'12px 12px',
                                         background:'#fff', border:'1px solid #E2E8F0', borderRadius:10,
                                         opacity:e, transform:`translateY(${(1-e)*-46}px)`}}>
                      <span style={{width:36, height:36, borderRadius:99, background:'#F1F5F9',
                                    display:'flex', alignItems:'center', justifyContent:'center'}}>
                        <Ico n="user" s={22} c="#CBD5E1" w={1.9}/>
                      </span>
                      <div>
                        <div style={{width:64, height:12, borderRadius:4, background:'#EEF2F6'}}/>
                        <div style={{width:44, height:10, borderRadius:4, background:'#F4F7FA', marginTop:7}}/>
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>
            {/* La cuenta que cierra: el reparto no suma lo que suman los marcadores */}
            <div style={{flex:1, display:'flex', alignItems:'center', gap:22}}>
              <div>
                <div className="f-plex" style={{fontSize:34, fontWeight:800, color:'#0F172A'}}>214.900 €</div>
                <div className="f-plex" style={{fontSize:20, color:'#64748B', marginTop:5}}>27 en el reparto</div>
              </div>
              <span className="f-plex" style={{fontSize:32, color:'#94A3B8'}}>+</span>
              <div>
                <div className="f-plex" style={{fontSize:34, fontWeight:800, color:'#94A3B8'}}>16.800 €</div>
                <div className="f-plex" style={{fontSize:20, color:'#64748B', marginTop:5}}>4 sin comercial</div>
              </div>
              <span className="f-plex" style={{fontSize:32, color:'#94A3B8'}}>=</span>
              <div>
                <div className="f-plex" style={{fontSize:34, fontWeight:800, color:'#2e7d32'}}>231.700 €</div>
                <div className="f-plex" style={{fontSize:20, color:'#64748B', marginTop:5}}>31 en el marcador</div>
              </div>
            </div>
          </div>
        </div>
      )}

      {/* ─────────── Salida: quedan tres cosas por decir ─────────── */}
      {salP > 0.01 && (
        <div style={{position:'absolute', left:0, right:0, top:868, display:'flex', justifyContent:'center', gap:40, opacity:salE}}>
          {['1','2','3'].map((n,i)=>{
            const e = Easing.easeOutBack(clamp((t-T.salida-i*0.14)/0.5,0,1));
            return (
              <span key={n} className="f-plex" style={{width:86, height:86, borderRadius:99,
                            border:'3px dashed #CBD5E1', color:'#94A3B8', fontSize:38, fontWeight:800,
                            display:'flex', alignItems:'center', justifyContent:'center',
                            transform:`scale(${lerp(0.6,1,e)})`, opacity:clamp(e*1.3,0,1)}}>{n}</span>
            );
          })}
        </div>
      )}

      <ScriptedCursor frames={cursor} t={t} clickTimes={[T.interrogante]}/>
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaPN05 });
