// === Escena 02 — "Quién entra en la lista" ===
// Las dos mitades de la condición (ReposicionSubpage.razor:134) se marcan sobre una tira de cuatro
// filas reales; la de Cinta aislante —mínimo cero, o sea sin configurar— NO entra ni estando a cero
// unidades; corte al estado vacío literal «Todo en orden» (:16-19, rama excluyente de la tabla);
// y vuelta a la tira para recordar que la comparación mira el DISPONIBLE, no el físico.
// El ámbar #FB8C00 de esta escena es el de ANOTACIÓN del deck, no un color del producto.
function EscenaRP02({ t }){
  const T = {
    dos:2.2,         // "dos cosas a la vez" -> las 2 casillas de condición, punteadas
    cond1:6.7,       // "un mínimo mayor que cero" -> se marca la casilla 1 (único overshoot)
    cond2:9.8,       // "su disponible está por debajo" -> se marca la casilla 2
    ojo:12.9,        // "Ojo con la primera" -> calma; la casilla 2 se atenúa
    cero:16.9,       // "no quieras tener nada" -> un 0 grande en el mínimo de Cinta aislante
    nadie:18.8,      // "nadie lo ha puesto" -> el 0 pasa a «sin configurar», en gris
    aunque:23.2,     // "ni estando a cero unidades" -> su disponible parpadea y la fila se apaga
    vacio:27.8,      // "un todo en orden" -> CORTE: el estado vacío en su marco
    disponible:31.7, // "mira el disponible" -> vuelve la tira; el 40 en ámbar y línea del 40 al 60
    salida:35.3,     // "Ya sabes quién entra" -> salida
    filaDelay:0.55,   // retardo de la tira de filas desde T.dos
    filaStep:0.16,    // cascada de entrada de las 4 filas
    veredictoDelay:0.35, // retardo del veredicto de cada condición desde su casilla
    veredictoStep:0.14,  // cascada del veredicto fila a fila
    entraDelay:1.30,  // retardo de la columna «¿Entra?» desde T.cond2
    dosCosasDelay:0.55, // retardo de los dos significados del estado vacío
  };

  const AMBAR_ANOT = '#FB8C00';   // ámbar de anotación del deck (NO es color de la pantalla)
  const VERDE = '#059669';
  const GRIS = '#94A3B8';

  // ── Geometría ──
  const CARDX = 240, CARDY = 366, CARDW = 1440;
  const IX = CARDX + 24, IW = CARDW - 48;   // 264 · 1392
  const HY = CARDY + 24;                    // 390 — cabecera de la rejilla
  const RY = HY + 54;                       // 444 — primera fila
  const RH = 88;
  const COL = { prod:290, alm:140, disp:165, min:185, v1:180, v2:210, ent:222 };
  const XD = IX + COL.prod + COL.alm + COL.disp;        // 859  fin de «Disponible»
  const XM = XD + COL.min;                              // 1044 fin de «Mínimo»

  // ── Progresos ──
  const dosP = clamp((t-T.dos)/0.5, 0, 1);
  const c1P = clamp((t-T.cond1)/0.5, 0, 1), c1E = Easing.easeOutBack(c1P);
  const c2P = clamp((t-T.cond2)/0.5, 0, 1), c2E = Easing.easeOutCubic(c2P);
  const ojoP = clamp((t-T.ojo-0.5)/0.5, 0, 1);
  const ojoRing = (t >= T.ojo+0.5 && t < T.ojo+1.6) ? Math.sin((t-T.ojo-0.5)/1.1*Math.PI) : 0;

  const ceroP = clamp((t-T.cero)/0.5, 0, 1), ceroE = Easing.easeOutBack(ceroP);
  const nadieP = clamp((t-T.nadie)/0.45, 0, 1);
  const aunqueP = clamp((t-T.aunque)/0.4, 0, 1);
  const parpadeo = (t >= T.aunque && t < T.aunque+0.9) ? Math.sin((t-T.aunque)/0.9*Math.PI) : 0;

  const vaP = clamp((t-T.vacio)/0.5, 0, 1), vaE = Easing.easeOutCubic(vaP);
  const vaOut = clamp((t-T.disponible)/0.5, 0, 1);
  const velo = vaP * (1-vaOut) * 0.94;
  const dosCosasP = clamp((t-T.vacio-T.dosCosasDelay)/0.6, 0, 1);

  const diP = clamp((t-T.disponible-0.3)/0.5, 0, 1);
  const diDraw = Easing.easeInOutCubic(clamp((t-T.disponible-0.45)/0.7, 0, 1));
  const salP = clamp((t-T.salida)/0.7, 0, 1);

  // ── Las cuatro filas de la persona continua ──
  const FILAS = [
    { p:'Cemento gris',    a:'Tienda', d:'40',  m:'60', c1:true,  c2:true,  entra:true  },
    { p:'Cemento gris',    a:'Nave',   d:'100', m:'80', c1:true,  c2:false, entra:false },
    { p:'Silicona blanca', a:'Nave',   d:'5',   m:'20', c1:true,  c2:true,  entra:true  },
    { p:'Cinta aislante',  a:'Nave',   d:'0',   m:'0',  c1:false, c2:null,  entra:false },
  ];

  // ── Marca de veredicto: ✓, ✗ o «—» ──
  const Marca = ({ v, start })=>{
    const p = clamp((t-start)/0.35, 0, 1), e = Easing.easeOutCubic(p);
    if (v === null) return <span className="f-mono" style={{fontSize:24, color:'#CBD5E1', opacity:p}}>—</span>;
    const c = v ? VERDE : '#B42318';
    return (
      <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="2.6"
           strokeLinecap="round" strokeLinejoin="round" style={{opacity:p, transform:`scale(${lerp(0.5,1,e)})`}}>
        <circle cx="12" cy="12" r="9.5" fill={v ? 'rgba(5,150,105,0.12)' : 'rgba(180,35,24,0.10)'} stroke="none"/>
        {v ? <path d="M7.2 12.4 L10.6 15.8 L16.8 8.8"/> : <><path d="M8.2 8.2 L15.8 15.8"/><path d="M15.8 8.2 L8.2 15.8"/></>}
      </svg>
    );
  };

  // ── Una casilla de condición ──
  const Casilla = ({ n, titulo, codigo, x, marcada, prog, ease, atenua, ring })=>(
    <div style={{position:'absolute', left:x, top:180, width:660, height:150, borderRadius:16,
                 background:'#fff', border:`2px ${marcada ? 'solid' : 'dashed'} ${marcada ? VERDE : '#CBD5E1'}`,
                 boxShadow:marcada ? `0 14px 36px rgba(5,150,105,${0.18*(1-atenua)})` : 'none',
                 display:'flex', alignItems:'center', gap:24, padding:'0 28px',
                 opacity:dosP*(1-atenua*0.55)*(1-salP), transform:`translateY(${(1-Easing.easeOutCubic(dosP))*12}px)`}}>
      {ring > 0 && (
        <div style={{position:'absolute', inset:-10, borderRadius:22, border:`3px solid ${AMBAR_ANOT}`, opacity:ring*0.9}}/>
      )}
      <div style={{width:54, height:54, borderRadius:12, flexShrink:0,
                   border:`2px ${marcada ? 'solid' : 'dashed'} ${marcada ? VERDE : '#CBD5E1'}`,
                   background:marcada ? 'rgba(5,150,105,0.10)' : '#F8FAFC',
                   display:'flex', alignItems:'center', justifyContent:'center'}}>
        {prog > 0 && (
          <svg width="34" height="34" viewBox="0 0 24 24" fill="none" stroke={VERDE} strokeWidth="3"
               strokeLinecap="round" strokeLinejoin="round" style={{transform:`scale(${lerp(0.4,1,ease)})`, opacity:clamp(prog*1.4,0,1)}}>
            <path d="M5 12.6 L10 17.6 L19 6.8"/>
          </svg>
        )}
      </div>
      <div style={{lineHeight:1.25}}>
        <div className="f-plex" style={{fontSize:15, fontWeight:700, letterSpacing:'2.4px', color:GRIS}}>CONDICIÓN {n}</div>
        <div className="f-plex" style={{fontSize:28, fontWeight:700, color:'#0F172A', marginTop:6}}>{titulo}</div>
        <div className="f-mono" style={{fontSize:19, color:'#64748B', marginTop:7}}>{codigo}</div>
      </div>
    </div>
  );

  return (
    <SlideCanvas variant="light">
      <DemoHeader t={t} eyebrow="LA CONDICIÓN" title={<span>Quién entra, y quién no</span>}/>

      {/* ─────────── Las dos casillas ─────────── */}
      <Casilla n="1" x={236} titulo="Tiene un mínimo mayor que cero" codigo="StockMinimo > 0"
               marcada={c1P > 0} prog={c1P} ease={c1E} atenua={0} ring={ojoRing}/>
      <Casilla n="2" x={1024} titulo="Su disponible, por debajo de ese mínimo" codigo="StockDisponible < StockMinimo"
               marcada={c2P > 0} prog={c2P} ease={c2E} atenua={ojoP} ring={0}/>
      <div style={{position:'absolute', left:930, top:225, width:60, height:60, borderRadius:100,
                   background:'#0F172A', display:'flex', alignItems:'center', justifyContent:'center', opacity:dosP*(1-salP)}}>
        <span className="f-plex" style={{fontSize:26, fontWeight:800, color:'#fff'}}>Y</span>
      </div>

      {/* ─────────── La tira de cuatro filas ─────────── */}
      <div style={{position:'absolute', left:CARDX, top:CARDY, width:CARDW, height:454, borderRadius:16,
                   background:'#fff', border:'1px solid #E2E8F0', boxShadow:'0 26px 62px rgba(15,23,42,0.10)',
                   opacity:clamp((t-T.dos-T.filaDelay+0.4)/0.5,0,1)*(1-salP)}}/>

      {/* Cabecera de la tira */}
      <div style={{position:'absolute', left:IX, top:HY, width:IW, height:54,
                   display:'grid', gridTemplateColumns:`${COL.prod}px ${COL.alm}px ${COL.disp}px ${COL.min}px ${COL.v1}px ${COL.v2}px ${COL.ent}px`,
                   alignItems:'center', opacity:clamp((t-T.dos-T.filaDelay+0.3)/0.5,0,1)*(1-salP)}}>
        {['Producto','Almacén'].map(h=>(
          <span key={h} className="f-plex" style={{fontSize:18, fontWeight:600, color:'#64748B'}}>{h}</span>
        ))}
        {['Disponible','Mínimo'].map(h=>(
          <span key={h} className="f-plex" style={{fontSize:18, fontWeight:600, color:'#64748B', textAlign:'right', paddingRight:8}}>{h}</span>
        ))}
        <span className="f-mono" style={{fontSize:15, fontWeight:600, color:GRIS, textAlign:'center', opacity:1-diP*0.7}}>mín &gt; 0</span>
        <span className="f-mono" style={{fontSize:15, fontWeight:600, color:GRIS, textAlign:'center', opacity:1-diP*0.7}}>disp &lt; mín</span>
        <span className="f-plex" style={{fontSize:18, fontWeight:700, color:'#0F172A', textAlign:'center'}}>¿Entra?</span>
      </div>

      {/* Las cuatro filas */}
      {FILAS.map((f,i)=>{
        const p = clamp((t-T.dos-T.filaDelay-i*T.filaStep)/0.45, 0, 1), e = Easing.easeOutCubic(p);
        const esCinta = i === 3;
        const apagada = esCinta ? clamp((t-T.nadie)/0.6,0,1)*0.35 + clamp((t-T.aunque)/0.5,0,1)*0.35 : 0;
        const resalta = i === 0 && diP > 0;
        const cuarenta = resalta;
        return (
          <div key={f.p+f.a} style={{position:'absolute', left:IX, top:RY+i*RH, width:IW, height:RH,
                       display:'grid', gridTemplateColumns:`${COL.prod}px ${COL.alm}px ${COL.disp}px ${COL.min}px ${COL.v1}px ${COL.v2}px ${COL.ent}px`,
                       alignItems:'center', borderTop:'1px solid #F1F5F9',
                       background:resalta ? 'rgba(251,140,0,0.07)' : (f.entra && t > T.cond2+T.entraDelay ? 'rgba(5,150,105,0.045)' : 'transparent'),
                       opacity:p*(1-apagada)*(1-salP), transform:`translateY(${(1-e)*10}px)`}}>
            <span className="f-plex" style={{fontSize:23, fontWeight:600, color:'#0F172A'}}>{f.p}</span>
            <span className="f-plex" style={{fontSize:21, color:'#64748B'}}>{f.a}</span>
            <span className="f-mono" style={{fontSize:cuarenta ? 34 : 30, fontWeight:700, textAlign:'right', paddingRight:8,
                                             color:cuarenta ? AMBAR_ANOT : '#0F172A',
                                             opacity:esCinta && parpadeo > 0 ? 1-parpadeo*0.85 : 1}}>{f.d}</span>
            <span className="f-mono" style={{fontSize:30, fontWeight:700, color:'#0F172A', textAlign:'right', paddingRight:8,
                                             opacity:esCinta ? 1-clamp((t-T.cero)/0.35,0,1) : 1}}>{f.m}</span>
            <span style={{display:'flex', justifyContent:'center', opacity:1-diP*0.75}}>
              <Marca v={f.c1} start={T.cond1+T.veredictoDelay+i*T.veredictoStep}/>
            </span>
            <span style={{display:'flex', justifyContent:'center', opacity:1-diP*0.75}}>
              <Marca v={f.c2} start={T.cond2+T.veredictoDelay+i*T.veredictoStep}/>
            </span>
            <span style={{display:'flex', justifyContent:'center'}}>
              {clamp((t-T.cond2-T.entraDelay-i*T.veredictoStep)/0.4,0,1) > 0 && (
                <span className="f-plex" style={{fontSize:20, fontWeight:700, padding:'8px 18px', borderRadius:99,
                        color:f.entra ? VERDE : GRIS, background:f.entra ? 'rgba(5,150,105,0.12)' : '#F1F5F9',
                        opacity:clamp((t-T.cond2-T.entraDelay-i*T.veredictoStep)/0.4,0,1)}}>
                  {f.entra ? 'Sí' : 'No'}
                </span>
              )}
            </span>
          </div>
        );
      })}

      {/* El 0 grande sobre el mínimo de Cinta aislante, y su relevo por «sin configurar» */}
      {ceroP > 0 && nadieP < 1 && (
        <div style={{position:'absolute', left:XM-150, top:RY+3*RH, width:142, height:RH,
                     display:'flex', alignItems:'center', justifyContent:'flex-end', gap:12,
                     opacity:(1-nadieP)}}>
          <span className="f-mono" style={{fontSize:66, fontWeight:800, color:'#0F172A',
                  transform:`scale(${lerp(0.4,1,ceroE)})`}}>0</span>
        </div>
      )}
      {ceroP > 0 && (
        <div style={{position:'absolute', left:XM+14, top:RY+3*RH+22, opacity:clamp(ceroP*1.3,0,1)*(1-nadieP)}}>
          <span className="f-plex" style={{fontSize:44, fontWeight:800, color:AMBAR_ANOT}}>?</span>
        </div>
      )}
      {nadieP > 0 && (
        <div style={{position:'absolute', left:XM-244, top:RY+3*RH+26, width:236, textAlign:'right',
                     opacity:nadieP*(1-clamp((t-T.nadie)/0.6,0,1)*0.35-clamp((t-T.aunque)/0.5,0,1)*0.35)*(1-salP)}}>
          <span className="f-plex" style={{fontSize:22, fontWeight:600, color:GRIS, background:'#F1F5F9',
                  border:'1px dashed #CBD5E1', borderRadius:99, padding:'8px 18px'}}>sin configurar</span>
        </div>
      )}

      {/* «mira el disponible»: el 40 en ámbar, la línea al 60 y el físico tachado al lado */}
      {diDraw > 0 && (
        <svg style={{position:'absolute', left:XD+8, top:RY, pointerEvents:'none', opacity:1-salP}} width="200" height="88" viewBox="0 0 200 88" fill="none">
          <path d="M6 44 H124" stroke={AMBAR_ANOT} strokeWidth="3.5" strokeLinecap="round"
                strokeDasharray="118" strokeDashoffset={118*(1-diDraw)}/>
          <path d="M112 34 L124 44 L112 54" stroke={AMBAR_ANOT} strokeWidth="3.5" strokeLinecap="round" strokeLinejoin="round"
                opacity={clamp((diDraw-0.82)/0.18,0,1)}/>
        </svg>
      )}
      {diP > 0 && (
        <div style={{position:'absolute', left:XM+34, top:RY+22, opacity:diP*(1-salP)}}>
          <span style={{position:'relative', display:'inline-block'}}>
            <span className="f-plex" style={{fontSize:22, fontWeight:600, color:GRIS,
                    background:'#F8FAFC', border:'1px solid #E2E8F0', borderRadius:99, padding:'9px 20px'}}>el físico</span>
            <span style={{position:'absolute', left:8, right:8, top:'50%', height:3, borderRadius:2, background:'#B42318',
                          transform:`scaleX(${Easing.easeInOutCubic(clamp((t-T.disponible-0.7)/0.45,0,1))})`, transformOrigin:'left'}}/>
          </span>
        </div>
      )}

      {/* ─────────── CORTE · el estado vacío, en su propio marco ─────────── */}
      {velo > 0 && <div style={{position:'absolute', inset:0, background:'#fff', opacity:velo}}/>}
      {vaP > 0 && vaOut < 1 && (
        <div style={{position:'absolute', left:560, top:250, width:800, opacity:(1-vaOut)*clamp(vaP*1.3,0,1),
                     transform:`translateY(${(1-vaE)*18 - vaOut*20}px)`}}>
          <div style={{background:'#fff', border:'1px solid #E2E8F0', borderRadius:20,
                       boxShadow:'0 34px 82px rgba(15,23,42,0.14)', padding:'44px 40px 40px', textAlign:'center'}}>
            <svg width="260" height="200" viewBox="0 0 130 100" fill="none" style={{display:'block', margin:'0 auto'}}>
              <path d="M22 42 L65 26 L108 42 L65 58 Z" fill="#EEF3FA" stroke="#CBD5E1" strokeWidth="2.4" strokeLinejoin="round"/>
              <path d="M25 47 V78 L65 93 L105 78 V47" stroke="#CBD5E1" strokeWidth="2.4" strokeLinejoin="round" fill="none"/>
              <path d="M65 58 V93" stroke="#E2E8F0" strokeWidth="2"/>
              <path d="M52 16 A16 16 0 0 1 78 16" stroke="#E2E8F0" strokeWidth="2.4" strokeLinecap="round" strokeDasharray="5 7"/>
            </svg>
            <div className="f-plex" style={{fontSize:42, fontWeight:800, color:'#0F172A', marginTop:22}}>Todo en orden</div>
            <div className="f-plex" style={{fontSize:25, color:'#64748B', marginTop:12, lineHeight:1.4}}>
              Ningún producto está por debajo de su stock mínimo en ningún almacén.
            </div>
          </div>
        </div>
      )}
      {vaP > 0 && vaOut < 1 && (
        <div style={{position:'absolute', left:0, right:0, top:790, textAlign:'center', opacity:(1-vaOut)*dosCosasP}}>
          <div className="f-plex" style={{fontSize:30, fontWeight:600, color:GRIS}}>puede querer decir dos cosas</div>
          <div style={{display:'flex', justifyContent:'center', gap:26, marginTop:22}}>
            {['no falta nada', 'nadie ha puesto un mínimo'].map((s,i)=>(
              <span key={s} className="f-plex" style={{fontSize:24, fontWeight:600, color:'#64748B',
                      background:'#F8FAFC', border:`1px dashed ${i === 1 ? AMBAR_ANOT : '#CBD5E1'}`, borderRadius:99, padding:'12px 26px',
                      opacity:clamp((t-T.vacio-T.dosCosasDelay-0.25-i*0.22)/0.4,0,1)}}>{s}</span>
            ))}
          </div>
        </div>
      )}
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaRP02 });
