// === Escena 03 — "El desglose" (el caso limpio) ===  [VALLE antes del clímax]
// La pregunta de la pantalla es "cuánto falta por explicar". El punteado = no está puesto.
// El sello verde exige las TRES cosas (nombre + importe al céntimo + fecha), y aun así no escribe:
// escribe el clic. Un caso limpio no pregunta nada — eso también hay que enseñarlo.
function EscenaCN03({ t }){
  // LOCUCIÓN (audio/escena-03.mp3): esto es el desglose → quién, cuándo y cuánto → cuánto falta
  // por explicar → faltan mil doscientos → candidatos abajo → borde punteado → no está puesto →
  // sello verde → nombre, importe, fecha → pulsas conciliar → un clic → baja a cero → cuadra exacto →
  // no te ha preguntado → no hace sola → ni una línea → ella propone, firmas tú → flecha abajo.
  const T = {
    panel:0.13,      // "Esto es el desglose"
    cab:3.73,        // "quién, cuándo y cuánto"
    pregunta:8.75,   // "cuánto falta por explicar"
    faltan:10.65,    // "Faltan mil doscientos treinta y cuatro euros"
    cands:14.28,     // "Los candidatos están abajo"
    punteado:16.87,  // "el borde punteado"
    nopuesto:20.24,  // "esto no está puesto"
    sello:22.92,     // "el sello verde"
    okNombre:24.02,  // "Coinciden el nombre"
    okImporte:25.29, // "el importe al céntimo"
    okFecha:26.91,   // "y la fecha"
    clic:28.34,      // "Pulsas conciliar"
    unclic:29.93,    // "Un clic"
    cero:31.67,      // "el marcador baja a cero"
    cuadra:33.47,    // "cuadra exacto"
    sinpreg:35.88,   // "no te ha preguntado"
    nosola:40.01,    // "no hace sola"
    nilinea:42.49,   // "no escribe ni una línea"
    prop:44.6,       // "Ella propone"
    firmas:46.09,    // "Firmas tú"
    flecha:48.59,    // "flecha abajo"
    salida:50.65,    // "viene acompañado"
  };

  const pnP = clamp((t-T.panel)/0.7,0,1), pnE = Easing.easeOutCubic(pnP);
  const cabP = clamp((t-T.cab)/0.55,0,1), cabE = Easing.easeOutCubic(cabP);
  const prGlow = t >= T.pregunta && t < T.pregunta+1.4 ? Math.sin((t-T.pregunta)/1.4*Math.PI) : 0;
  const faP = clamp((t-T.faltan)/0.5,0,1);
  const caP = clamp((t-T.cands)/0.6,0,1);
  const puDraw = Easing.easeInOutCubic(clamp((t-T.punteado)/0.8,0,1));
  const npP = clamp((t-T.nopuesto)/0.5,0,1), npE = Easing.easeOutCubic(npP);
  const seP = clamp((t-T.sello)/0.5,0,1), seE = Easing.easeOutBack(seP);
  const ucP = clamp((t-T.unclic)/0.45,0,1), ucE = Easing.easeOutBack(ucP);

  // En CÉNTIMOS: useCountUp redondea a entero (primitives.jsx:214) y «1.234,56» se quedaba
  // en «1.234,00» mientras la cabecera de al lado seguía diciendo 1.234,56.
  const resto = useCountUp(123456, 0, T.cero, 1.1, t) / 100;
  const conciliado = T.clic <= t;
  const cuP = clamp((t-T.cuadra)/0.5,0,1), cuE = Easing.easeOutBack(cuP);

  // "No te ha preguntado": un diálogo fantasma que se dibuja y se tacha
  const spP = clamp((t-T.sinpreg)/0.6,0,1), spE = Easing.easeOutCubic(spP);
  const spTacha = Easing.easeInOutCubic(clamp((t-T.sinpreg-0.7)/0.6,0,1));

  const nsP = clamp((t-T.nosola)/0.6,0,1), nsE = Easing.easeOutCubic(nsP);
  const nlP = clamp((t-T.nilinea)/0.5,0,1);
  const veloOp = t < T.prop ? 0 : clamp((t-T.prop)/0.7,0,1)*0.93;
  const prP = clamp((t-T.prop)/0.55,0,1), prE = Easing.easeOutCubic(prP);
  const fiP = clamp((t-T.firmas)/0.55,0,1), fiE = Easing.easeOutCubic(fiP);
  const flP = clamp((t-T.flecha)/0.45,0,1), flE = Easing.easeOutBack(flP);
  const saP = clamp((t-T.salida)/0.55,0,1), saE = Easing.easeOutCubic(saP);

  // Cursor: aparca → botón Conciliar de la primera propuesta (clic 28.34)
  const cursor = { frames:[
    { t:0.8,  x:1780, y:1000 },
    { t:27.6, x:1780, y:1000 },
    { t:28.2, x:1268, y:646 },
  ], clicks:[T.clic] };

  // ── Fila de propuesta. `sup` = coincide en las tres cosas ──
  const Propuesta = ({ i, doc, ent, importe, fecha, sup, hecha })=>{
    const p = clamp((t-T.cands-i*0.12)/0.5,0,1), e = Easing.easeOutCubic(p);
    const verde = sup && T.sello <= t;
    return (
      <div style={{position:'relative', display:'grid', gridTemplateColumns:'74px minmax(0,1fr) 250px 210px',
                   alignItems:'center', gap:16, padding:'15px 12px', borderBottom:'1px solid #F1F5F9',
                   opacity:clamp(p*1.3,0,1), transform:`translateY(${(1-e)*10}px)`,
                   background:verde && !hecha ? 'linear-gradient(90deg, rgba(16,185,129,0.20) 0%, rgba(16,185,129,0.07) 50%, transparent 90%)'
                            : (hecha ? 'rgba(16,185,129,0.06)' : 'transparent')}}>
        {/* Punteado azul mientras NO está puesto; verde continuo cuando coincide todo */}
        <span style={{position:'absolute', left:0, top:6, bottom:6, width:4, borderRadius:2,
                      background:verde ? '#059669' : 'transparent',
                      backgroundImage:verde ? 'none' : 'repeating-linear-gradient(180deg,#38BDF8 0 9px,transparent 9px 17px)',
                      transform:`scaleY(${verde ? 1 : puDraw})`, transformOrigin:'top'}}/>
        <div style={{display:'flex', flexDirection:'column', alignItems:'center', gap:2}}>
          {hecha
            ? <svg width="34" height="34" viewBox="0 0 24 24" fill="none" stroke="#059669" strokeWidth="2.3"><circle cx="12" cy="12" r="10"/><path d="M7 12.5 L10.5 16 L17 9" strokeLinecap="round"/></svg>
            : <svg width="34" height="34" viewBox="0 0 24 24" fill="none" stroke="#38BDF8" strokeWidth="2.3"><circle cx="12" cy="12" r="10"/><path d="M9.4 9.2 a2.6 2.6 0 1 1 2.6 3.3 v1.2" strokeLinecap="round"/><circle cx="12" cy="17" r="1" fill="#38BDF8" stroke="none"/></svg>}
          <span className="f-plex" style={{fontSize:14, fontWeight:600, color:'#94A3B8'}}>{hecha ? 'Hecho' : 'Venta'}</span>
        </div>
        <div style={{minWidth:0}}>
          <div className="f-plex" style={{fontSize:22, fontWeight:700, color:'#0F172A'}}>{doc}</div>
          <div className="f-plex" style={{fontSize:verde ? 19 : 17, fontWeight:verde ? 600 : 400, color:verde ? '#0F172A' : '#94A3B8'}}>{ent}</div>
        </div>
        <div style={{textAlign:'right'}}>
          <div className="f-mono" style={{fontSize:21, fontWeight:700, color:'#0F172A'}}>{importe}</div>
          <div className="f-plex" style={{fontSize:16, color:'#94A3B8'}}>{fecha}</div>
        </div>
        <div style={{display:'flex', justifyContent:'flex-end', gap:10, alignItems:'center'}}>
          {sup && seP > 0 && !hecha && (
            <span style={{display:'inline-flex', alignItems:'center', padding:'4px 12px', borderRadius:99,
                          background:'rgba(16,185,129,0.16)', transform:`scale(${lerp(0.7,1,seE)})`}}>
              <span className="f-plex" style={{fontSize:15, fontWeight:700, color:'#059669'}}>coincide todo</span>
            </span>
          )}
          <span style={{display:'inline-flex', alignItems:'center', padding:'9px 20px', borderRadius:9,
                        background:hecha ? '#EEF2F6' : '#059669', boxShadow:hecha ? 'none' : '0 6px 16px rgba(5,150,105,0.3)'}}>
            <span className="f-plex" style={{fontSize:18, fontWeight:700, color:hecha ? '#94A3B8' : '#fff'}}>
              {hecha ? 'Conciliada' : 'Conciliar'}
            </span>
          </span>
        </div>
      </div>
    );
  };

  const Check = ({ p, txt }) => {
    const e = Easing.easeOutBack(p);
    return (
      <span style={{display:'inline-flex', alignItems:'center', gap:10, opacity:clamp(p*1.4,0,1), transform:`scale(${lerp(0.75,1,e)})`}}>
        <svg width="26" height="26" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="rgba(16,185,129,0.16)"/><path d="M7 12.5 L10.5 16 L17 9" fill="none" stroke="#059669" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round"/></svg>
        <span className="f-plex" style={{fontSize:25, fontWeight:600, color:'#0F172A'}}>{txt}</span>
      </span>
    );
  };

  return (
    <SlideCanvas variant="light">
      <DemoHeader t={t} eyebrow="EL DESGLOSE" title={<span>¿Cuánto falta por explicar?</span>}/>

      {pnP > 0 && (
        <div style={{position:'absolute', left:150, top:246, width:1240,
                     opacity:pnE*(1-veloOp/0.93*0.82), transform:`translateY(${(1-pnE)*22}px)`,
                     filter:veloOp > 0 ? 'saturate(0.55)' : 'none'}}>
          <div style={{background:'#fff', borderRadius:18, border:'1px solid #E2E8F0', boxShadow:'0 30px 72px rgba(15,23,42,0.12)', overflow:'hidden'}}>

            {/* Cabecera: a la izquierda quién y qué pone, a la derecha CUÁNTO */}
            <div style={{display:'grid', gridTemplateColumns:'minmax(0,1fr) auto', gap:20, alignItems:'start',
                         padding:'26px 30px', borderBottom:'1px solid #EEF2F6', opacity:cabE, transform:`translateY(${(1-cabE)*8}px)`}}>
              <div style={{display:'flex', gap:16, minWidth:0}}>
                <div style={{width:56, height:56, flexShrink:0, borderRadius:99, background:'linear-gradient(135deg,#38BDF8,#0085FF)',
                             display:'flex', alignItems:'center', justifyContent:'center'}}>
                  <span className="f-plex" style={{color:'#fff', fontSize:19, fontWeight:800}}>RS</span>
                </div>
                <div style={{minWidth:0}}>
                  <div className="f-mono" style={{fontSize:20, color:'#334155', lineHeight:1.45}}>
                    TRANSF /Bizum DE <span style={{color:'#375a8b', fontWeight:700}}>RESOEX S.L.</span>( FRANCISCO QUIJADA...) (Ext: <span style={{color:'#c2185b', fontWeight:700}}>71689</span>)
                  </div>
                  <div className="f-plex" style={{fontSize:17, color:'#94A3B8', marginTop:6}}>12/08/2026 · Transferencia · RESOEX, S.L.</div>
                </div>
              </div>
              <div style={{textAlign:'right'}}>
                <div className="f-plex" style={{fontSize:15, color:'#94A3B8'}}>importe del ingreso</div>
                <div className="f-mono" style={{fontSize:34, fontWeight:800, color:'#086F0C'}}>+1.234,56 €</div>
              </div>
            </div>

            {/* El marcador: la meta de la pantalla */}
            <div style={{padding:'20px 30px 14px', borderBottom:'1px solid #F1F5F9'}}>
              <div style={{display:'flex', alignItems:'center', gap:14}}>
                <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke={conciliado ? '#059669' : '#0085FF'} strokeWidth="2.2">
                  <circle cx="12" cy="12" r="9"/><path d="M8 12.5 L11 15.5 L16.5 9.5" strokeLinecap="round"/>
                </svg>
                <span className="f-plex" style={{fontSize:21, fontWeight:700, color:'#0F172A'}}>Conciliado</span>
                {faP > 0 && !conciliado && (
                  <span className="f-plex" style={{fontSize:30, fontWeight:800, color:'#0085FF', opacity:faP,
                          textShadow:0 < prGlow ? `0 0 ${prGlow*20}px rgba(0,133,255,0.75)` : 'none'}}>
                    Faltan 1.234,56 €
                  </span>
                )}
                {conciliado && (
                  <span className="f-plex" style={{fontSize:30, fontWeight:800, color:T.cuadra <= t ? '#059669' : '#0085FF'}}>
                    {T.cuadra <= t ? 'Cuadra exacto' : `Faltan ${Math.max(0,resto).toLocaleString('es-ES',{minimumFractionDigits:2, maximumFractionDigits:2})} €`}
                  </span>
                )}
                {cuP > 0 && (
                  <svg width="32" height="32" viewBox="0 0 24 24" style={{transform:`scale(${lerp(0.4,1,cuE)})`}}>
                    <circle cx="12" cy="12" r="10" fill="rgba(16,185,129,0.18)"/>
                    <path d="M7 12.5 L10.5 16 L17 9" fill="none" stroke="#059669" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round"/>
                  </svg>
                )}
              </div>
              <div style={{height:6, borderRadius:99, background:'#EEF2F6', marginTop:12}}>
                <div style={{height:'100%', borderRadius:99, background:T.cuadra <= t ? '#059669' : '#0085FF',
                             width:`${conciliado ? clamp((t-T.cero)/1.1,0,1)*100 : 0}%`}}/>
              </div>
            </div>

            {/* La linde y las propuestas */}
            {caP > 0 && (
              <div style={{padding:'12px 30px 26px'}}>
                <div style={{display:'flex', alignItems:'baseline', gap:14, padding:'8px 0 7px', borderBottom:'1px solid #E2E8F0'}}>
                  <b className="f-plex" style={{fontSize:20, color:'#0F172A'}}>¿Es alguna de estas operaciones?</b>
                  {npP > 0 && (
                    <span className="f-plex" style={{fontSize:18, color:'#0085FF', fontWeight:600, opacity:npE}}>
                      punteado = todavía no está puesto
                    </span>
                  )}
                </div>
                <div style={{background:'rgba(0,133,255,0.05)', borderRadius:12, padding:'2px 6px', marginTop:8}}>
                  <Propuesta i={0} doc="Venta 1001" ent="RESOEX, S.L." importe="1.234,56 €" fecha="11/08/2026" sup hecha={conciliado}/>
                  <Propuesta i={1} doc="Venta 1042" ent="Comercial Levante, S.A." importe="1.234,56 €" fecha="02/07/2026"/>
                  <Propuesta i={2} doc="Venta 0987" ent="RESOEX, S.L." importe="1.180,00 €" fecha="28/07/2026"/>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Los tres checks del sello verde */}
      {clamp((t-T.okNombre)/0.4,0,1) > 0 && veloOp < 0.5 && (
        <div style={{position:'absolute', left:150, width:1240, top:922, display:'flex', justifyContent:'center', gap:38,
                     opacity:(1-veloOp/0.93)}}>
          <Check p={clamp((t-T.okNombre)/0.4,0,1)} txt="el nombre"/>
          <Check p={clamp((t-T.okImporte)/0.4,0,1)} txt="el importe al céntimo"/>
          <Check p={clamp((t-T.okFecha)/0.4,0,1)} txt="la fecha"/>
        </div>
      )}

      {/* Columna derecha: el clic, el diálogo que no salió, la garantía */}
      {ucP > 0 && veloOp < 0.5 && (
        <div style={{position:'absolute', left:1450, top:270, width:380, opacity:clamp(ucP*1.3,0,1)*(1-veloOp/0.93),
                     transform:`scale(${lerp(0.7,1,ucE)})`, transformOrigin:'left top'}}>
          <div className="f-plex" style={{fontSize:96, fontWeight:800, color:'#059669', lineHeight:1}}>1</div>
          <div className="f-plex" style={{fontSize:28, fontWeight:600, color:'#64748B'}}>clic</div>
        </div>
      )}

      {spP > 0 && veloOp < 0.5 && (
        <div style={{position:'absolute', left:1450, top:436, width:380, opacity:spE*0.95*(1-veloOp/0.93), transform:`translateY(${(1-spE)*12}px)`}}>
          <div style={{position:'relative', background:'#fff', borderRadius:14, border:'2px dashed #CBD5E1', padding:'22px 24px'}}>
            <div className="f-plex" style={{fontSize:20, fontWeight:700, color:'#94A3B8'}}>¿Seguro que quieres conciliar?</div>
            <div style={{display:'flex', gap:10, marginTop:16}}>
              <span style={{padding:'8px 16px', borderRadius:7, background:'#F1F5F9'}}><span className="f-plex" style={{fontSize:16, color:'#94A3B8'}}>Cancelar</span></span>
              <span style={{padding:'8px 16px', borderRadius:7, background:'#F1F5F9'}}><span className="f-plex" style={{fontSize:16, color:'#94A3B8'}}>Aceptar</span></span>
            </div>
            <span style={{position:'absolute', left:16, right:16, top:'50%', height:4, borderRadius:2, background:'#EF4444',
                          transform:`scaleX(${spTacha}) rotate(-8deg)`, transformOrigin:'left'}}/>
          </div>
          <div className="f-plex" style={{marginTop:12, fontSize:21, fontWeight:700, color:'#B42318', textAlign:'center'}}>
            No había nada que dudar
          </div>
        </div>
      )}

      {nsP > 0 && veloOp < 0.5 && (
        <div style={{position:'absolute', left:1450, top:680, width:380, opacity:clamp(nsP*1.3,0,1)*(1-veloOp/0.93),
                     transform:`translateY(${(1-nsE)*12}px)`}}>
          <div style={{display:'flex', alignItems:'center', gap:14}}>
            <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="#0085FF" strokeWidth="2">
              <rect x="4" y="10.5" width="16" height="10" rx="2.2"/><path d="M8 10.5 V7.5 a4 4 0 0 1 8 0 v3"/>
            </svg>
            <span className="f-plex" style={{fontSize:25, fontWeight:700, color:'#0F172A', lineHeight:1.25}}>
              Las otras dos<br/>siguen sin tocarse
            </span>
          </div>
          {nlP > 0 && (
            <div className="f-plex" style={{marginTop:14, fontSize:21, color:'#64748B', opacity:nlP, lineHeight:1.35}}>
              Hasta que pulsas, no se escribe ni una línea.
            </div>
          )}
        </div>
      )}

      {/* Cierre: ella propone, firmas tú */}
      {veloOp > 0 && <div style={{position:'absolute', inset:0, background:'#fff', opacity:veloOp}}/>}
      {prP > 0 && (
        <div className="f-plex" style={{position:'absolute', left:0, right:0, top:404, textAlign:'center', fontSize:68, fontWeight:800,
                                        letterSpacing:'-0.03em', color:'#64748B', opacity:prE, transform:`translateY(${(1-prE)*16}px)`}}>
          Ella propone.
        </div>
      )}
      {fiP > 0 && (
        <div className="f-plex" style={{position:'absolute', left:0, right:0, top:508, textAlign:'center', fontSize:88, fontWeight:800,
                                        letterSpacing:'-0.03em', opacity:fiE, transform:`translateY(${(1-fiE)*18}px)`}}>
          <span className="grad-text">Firmas tú.</span>
        </div>
      )}
      {flP > 0 && (
        <div style={{position:'absolute', left:0, right:0, top:690, display:'flex', justifyContent:'center', alignItems:'center', gap:18,
                     opacity:clamp(flP*1.3,0,1), transform:`scale(${lerp(0.85,1,flE)})`}}>
          <KeyCap t={t} label="↓" start={T.flecha} size={62}/>
          <span className="f-plex" style={{fontSize:32, fontWeight:600, color:'#64748B'}}>al siguiente, sin soltar el teclado</span>
        </div>
      )}
      {saP > 0 && (
        <div className="f-plex" style={{position:'absolute', left:0, right:0, top:830, textAlign:'center', fontSize:34, fontWeight:600,
                                        color:'#94A3B8', opacity:saE, transform:`translateY(${(1-saE)*10}px)`}}>
          Pero este viene acompañado.
        </div>
      )}

      <ScriptedCursor frames={cursor.frames} t={t} clickTimes={cursor.clicks}/>
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaCN03 });
