// === Escena IN01 — "Dónde vive tu negocio" (HERO de apertura) ===
// Duración real del mp3: 38.3s. SlideCanvas variant="hero".
// Hero en 3 actos: (1) la rutina de confiar la empresa a un programa + la pregunta
// que nadie hace; (2) quién habla: Dinaup; (3) la ÚNICA COSA del deck —"cada pieza
// está doblada"— con la PRIMERA aparición del MOTIVO GEMELO (3 pares de nodos + línea
// sync viva). Cierra con la píldora de salida hacia la escena del "suelo".
// LOCUCIÓN (audio/escena-01.mp3): "Confías tu negocio a un programa. Facturas,
// clientes, contabilidad... toda tu empresa, dentro. Y una pregunta que casi nadie te
// deja hacer: ¿dónde vive todo eso? Un software es tan fiable como lo que tiene debajo.
// Esto es Dinaup. Y esto es su infraestructura. Te la vamos a enseñar entera. La idea
// es simple: cada pieza está doblada. Los datos, el servidor, los archivos... todo, dos
// veces, en sitios distintos. Para que no haya un solo punto que se pueda romper.
// Empecemos por el suelo."
function EscenaIN01({ t }){
  const T = {
    confia:   0.0,    // "Confías tu negocio"                 → título gancho grande
    chips:    2.89,   // "Facturas, clientes"                 → fila de 4 chips en cascada
    pregunta: 11.56,  // "dónde vive todo eso"                → chips se atenúan; sube la PREGUNTA
    bookend:  14.61,  // "tan fiable como lo que tiene debajo"→ subtítulo bookend gris
    dinaup:   17.17,  // "Esto es Dinaup"                     → ACTO 2: logo Dinaup
    infra:    20.19,  // "su infraestructura"                 → subtítulo "Esto es su infraestructura."
    doblada:  25.33,  // "cada pieza está doblada"            → ACTO 3: la ÚNICA COSA
    gemelos:  30.08,  // "todo, dos veces"                    → 3 pares gemelos (stagger 0.25)
    remate:   34.20,  // "un solo punto que se pueda romper"  → subtítulo remate
    salida:   36.86,  // "Empecemos por el suelo"             → píldora salida
  };

  // ---- Actos: 1 sale al entrar Dinaup; 2 sale al entrar la ÚNICA COSA ----
  const act1Op = 1 - clamp((t - T.dinaup)/0.35, 0, 1);
  const act2Op = 1 - clamp((t - T.doblada)/0.40, 0, 1);
  const showAct1 = t < T.dinaup + 0.40;
  const showAct2 = t >= T.dinaup && t < T.doblada + 0.45;
  const showAct3 = t >= T.doblada - 0.40;

  // ---- Acto 1 ----
  const titP  = clamp((t - T.confia)/0.7, 0, 1),   titE  = Easing.easeOutBack(titP);
  const preP  = clamp((t - T.pregunta)/0.6, 0, 1), preE  = Easing.easeOutBack(preP); // protagonista del beat
  const bookP = clamp((t - T.bookend)/0.7, 0, 1),  bookE = Easing.easeOutCubic(bookP);
  const dimQ  = clamp((t - T.pregunta)/0.6, 0, 1); // al llegar la pregunta, título+chips se atenúan

  const CHIPS = ['Facturas', 'Clientes', 'Contabilidad', 'Archivos'];
  const chipIcon = (i)=>{
    const c = '#64748B';
    if (i === 0) return ( // factura: documento con líneas
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
        <path d="M6 3 H15 L19 7 V21 H6 Z" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/>
        <path d="M15 3 V7 H19" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/>
        <path d="M9 12 H16 M9 16 H16" stroke={c} strokeWidth="1.8" strokeLinecap="round"/>
      </svg>
    );
    if (i === 1) return ( // cliente: persona
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
        <circle cx="12" cy="8" r="3.4" stroke={c} strokeWidth="1.8"/>
        <path d="M5.5 20 C5.5 15.8 8.4 14 12 14 C15.6 14 18.5 15.8 18.5 20" stroke={c} strokeWidth="1.8" strokeLinecap="round"/>
      </svg>
    );
    if (i === 2) return ( // contabilidad: gráfico de barras
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
        <path d="M4 20 H20" stroke={c} strokeWidth="1.8" strokeLinecap="round"/>
        <rect x="6" y="12" width="3" height="6" stroke={c} strokeWidth="1.8"/>
        <rect x="11" y="8" width="3" height="10" stroke={c} strokeWidth="1.8"/>
        <rect x="16" y="5" width="3" height="13" stroke={c} strokeWidth="1.8"/>
      </svg>
    );
    return ( // archivos: carpeta
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
        <path d="M4 7 C4 6 5 5 6 5 H9 L11 7 H18 C19 7 20 8 20 9 V17 C20 18 19 19 18 19 H6 C5 19 4 18 4 17 Z" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/>
      </svg>
    );
  };

  // ---- Acto 2 ----
  const logoP = clamp((t - (T.dinaup+0.1))/0.6, 0, 1), logoE = Easing.easeOutBack(logoP);
  const infP  = clamp((t - T.infra)/0.7, 0, 1),        infE  = Easing.easeOutCubic(infP);

  // ---- Acto 3: la ÚNICA COSA ----
  const dobP     = clamp((t - T.doblada)/0.7, 0, 1), dobE = Easing.easeOutBack(dobP);
  const dobPulse = Math.sin(clamp((t - (T.doblada+0.9))/1.1, 0, 1) * Math.PI); // UN pulse de remate
  const remP     = clamp((t - T.remate)/0.6, 0, 1),  remE = Easing.easeOutCubic(remP);
  const salP     = clamp((t - T.salida)/0.5, 0, 1),  salE = Easing.easeOutBack(salP);

  // ---- 3 pares gemelos (motivo conductor del deck) ----
  const AZ = '#0085FF';
  const PAIRS = [
    { label:'DATOS',    start:T.gemelos + 0.00 },
    { label:'SERVIDOR', start:T.gemelos + 0.25 },
    { label:'ARCHIVOS', start:T.gemelos + 0.50 },
  ];
  const COLW = 380, COLG = 54;
  const ROWX = (1920 - (3*COLW + 2*COLG)) / 2;   // 336
  const ROWY = 468;
  const NODEY = 44, NODEH = 92, LINEY = NODEY + NODEH/2; // centro de nodos: 90
  const LX0 = 150, LX1 = 240;                    // extremos de la línea sync (coords de columna)

  const pairIcon = (i)=>{
    if (i === 0) return ( // DATOS: cilindro de base de datos
      <svg width="30" height="30" viewBox="0 0 32 32" fill="none">
        <ellipse cx="16" cy="8" rx="9" ry="3.4" stroke={AZ} strokeWidth="2"/>
        <path d="M7 8 V24 C7 25.9 11 27.4 16 27.4 C21 27.4 25 25.9 25 24 V8" stroke={AZ} strokeWidth="2"/>
        <path d="M7 16 C7 17.9 11 19.4 16 19.4 C21 19.4 25 17.9 25 16" stroke={AZ} strokeWidth="2"/>
      </svg>
    );
    if (i === 1) return ( // SERVIDOR: rack de servidor
      <svg width="30" height="30" viewBox="0 0 32 32" fill="none">
        <rect x="6" y="6" width="20" height="8" rx="2" stroke={AZ} strokeWidth="2"/>
        <rect x="6" y="18" width="20" height="8" rx="2" stroke={AZ} strokeWidth="2"/>
        <circle cx="10.5" cy="10" r="1.1" fill={AZ}/>
        <circle cx="10.5" cy="22" r="1.1" fill={AZ}/>
        <path d="M15 10 H22 M15 22 H22" stroke={AZ} strokeWidth="2" strokeLinecap="round"/>
      </svg>
    );
    return ( // ARCHIVOS: documentos apilados
      <svg width="30" height="30" viewBox="0 0 32 32" fill="none">
        <path d="M11 5 H19 L24 10 V25 H11 Z" stroke={AZ} strokeWidth="2" strokeLinejoin="round"/>
        <path d="M19 5 V10 H24" stroke={AZ} strokeWidth="2" strokeLinejoin="round"/>
        <path d="M8 9 V27 H21" stroke={AZ} strokeWidth="2" strokeLinejoin="round" opacity="0.45"/>
      </svg>
    );
  };

  return (
    <SlideCanvas variant="hero">

      {/* ================= ACTO 1 — la empresa dentro, y la pregunta que nadie hace ================= */}
      {showAct1 && (
        <div style={{position:'absolute', inset:0, opacity:act1Op}}>

          {/* Título gancho */}
          {titP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:175, textAlign:'center',
              opacity:clamp(titP*1.3,0,1)*lerp(1,0.42,dimQ),
              transform:`translateY(${(1-titE)*36}px)`,
              filter:`saturate(${lerp(1,0.6,dimQ)})`}}>
              <h1 className="f-plex" style={{fontSize:80, fontWeight:800, letterSpacing:'-0.04em', color:'#0F172A', margin:0, lineHeight:1.04}}>
                Confías tu negocio a un programa.
              </h1>
            </div>
          )}

          {/* Fila de 4 chips (facturas · clientes · contabilidad · archivos), cascada 0.12 */}
          <div style={{position:'absolute', left:0, right:0, top:360, display:'flex', alignItems:'center', justifyContent:'center', gap:20}}>
            {CHIPS.map((c,i)=>{
              const cp = clamp((t - (T.chips + i*0.12))/0.45, 0, 1);
              const ce = Easing.easeOutBack(cp);
              if (cp <= 0) return null;
              return (
                <div key={c} style={{
                  display:'flex', alignItems:'center', gap:11,
                  background:'#fff', border:'1px solid #E2E8F0', borderRadius:14,
                  padding:'13px 22px', boxShadow:'0 12px 30px rgba(15,23,42,0.07)',
                  opacity:clamp(cp*1.3,0,1)*lerp(1,0.28,dimQ),
                  filter:`saturate(${lerp(1,0.5,dimQ)})`,
                  transform:`translateY(${(1-ce)*18}px) scale(${lerp(0.7,1,ce)})`}}>
                  {chipIcon(i)}
                  <span className="f-plex" style={{fontSize:22, fontWeight:600, color:'#334155'}}>{c}</span>
                </div>
              );
            })}
          </div>

          {/* La PREGUNTA que casi nadie te deja hacer (protagonista del beat 11.56) */}
          {preP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:540, textAlign:'center',
              opacity:clamp(preP*1.3,0,1),
              transform:`translateY(${(1-preE)*34}px) scale(${lerp(0.9,1,preE)})`}}>
              <div className="f-plex" style={{fontSize:86, fontWeight:800, letterSpacing:'-0.04em', color:'#0F172A', lineHeight:1.0}}>
                ¿Dónde vive todo eso?
              </div>
            </div>
          )}

          {/* Subtítulo bookend */}
          {bookP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:702, textAlign:'center',
              opacity:bookE, transform:`translateY(${(1-bookE)*12}px)`}}>
              <div className="f-plex" style={{fontSize:30, fontWeight:500, color:'#64748B', letterSpacing:'-0.015em'}}>
                Un software es tan fiable como lo que tiene debajo.
              </div>
            </div>
          )}
        </div>
      )}

      {/* ================= ACTO 2 — quién habla: Dinaup ================= */}
      {showAct2 && (
        <div style={{position:'absolute', inset:0, opacity:act2Op}}>

          {/* Logo Dinaup */}
          {logoP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:388, display:'flex', justifyContent:'center',
              opacity:clamp(logoP*1.3,0,1), transform:`translateY(${(1-logoE)*30}px) scale(${lerp(0.86,1,logoE)})`}}>
              <DinaupLogo size={62}/>
            </div>
          )}

          {/* Esto es su infraestructura */}
          {infP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:540, textAlign:'center',
              opacity:infE, transform:`translateY(${(1-infE)*14}px)`}}>
              <div className="f-plex" style={{fontSize:34, fontWeight:500, color:'#475569', letterSpacing:'-0.015em'}}>
                Y esto es <span style={{color:'#0F172A', fontWeight:700}}>su infraestructura.</span>
              </div>
            </div>
          )}
        </div>
      )}

      {/* ================= ACTO 3 — la ÚNICA COSA + motivo gemelo ================= */}
      {showAct3 && (
        <div style={{position:'absolute', inset:0}}>

          {/* Cada pieza está doblada */}
          {dobP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:168, textAlign:'center',
              opacity:clamp(dobP*1.3,0,1),
              transform:`translateY(${(1-dobE)*30}px) scale(${1 + 0.014*dobPulse})`}}>
              <div className="f-plex" style={{fontSize:78, fontWeight:800, letterSpacing:'-0.04em', color:'#0F172A', lineHeight:1.05}}>
                Cada pieza está <span className="grad-text">doblada.</span>
              </div>
            </div>
          )}

          {/* 3 pares gemelos: dos nodos + línea sync viva + pulso que viaja una vez */}
          {PAIRS.map((pr,i)=>{
            const pp = clamp((t - pr.start)/0.6, 0, 1);
            if (pp <= 0) return null;
            const pe = Easing.easeOutBack(pp);
            const lineDraw = Easing.easeInOutCubic(clamp((t - (pr.start+0.3))/0.5, 0, 1));
            const pdP = clamp((t - (pr.start+0.6))/0.95, 0, 1);      // pulso A→B, una sola vez
            const x2P = clamp((t - (pr.start+0.7))/0.4, 0, 1), x2E = Easing.easeOutBack(x2P);
            const colLeft = ROWX + i*(COLW+COLG);

            const NodeCard = ()=> (
              <div style={{
                position:'absolute', top:NODEY, width:130, height:NODEH,
                background:'#fff', border:'1px solid #E2E8F0', borderRadius:14,
                boxShadow:'0 14px 34px rgba(15,23,42,0.08)',
                display:'flex', alignItems:'center', justifyContent:'center'}}>
                <div style={{width:44, height:44, borderRadius:11, background:'rgba(0,133,255,0.08)', display:'flex', alignItems:'center', justifyContent:'center'}}>
                  {pairIcon(i)}
                </div>
              </div>
            );

            return (
              <div key={pr.label} style={{position:'absolute', left:colLeft, top:ROWY, width:COLW, height:210,
                opacity:clamp(pp*1.3,0,1),
                transform:`translateY(${(1-pe)*24}px) scale(${lerp(0.86,1,pe)})`}}>

                {/* Etiqueta del par */}
                <div className="f-mono" style={{position:'absolute', top:0, left:0, right:0, textAlign:'center',
                  fontSize:13, fontWeight:700, letterSpacing:'3px', color:'#94A3B8'}}>{pr.label}</div>

                {/* Nodo izquierdo (left 20) y derecho (left 230): gemelos idénticos */}
                <div style={{position:'absolute', left:20}}><NodeCard/></div>
                <div style={{position:'absolute', left:230}}><NodeCard/></div>

                {/* Línea sync que se dibuja */}
                <svg width={COLW} height="210" viewBox={`0 0 ${COLW} 210`} style={{position:'absolute', left:0, top:0, pointerEvents:'none'}}>
                  <line x1={LX0} y1={LINEY} x2={LX1} y2={LINEY} stroke={AZ} strokeWidth="3" strokeLinecap="round"
                    strokeDasharray={LX1-LX0} strokeDashoffset={(LX1-LX0)*(1-lineDraw)} opacity="0.85"/>
                </svg>

                {/* Pulso "en tiempo real": viaja de A a B una vez */}
                {pdP > 0 && pdP < 1 && (
                  <div style={{position:'absolute', left:lerp(LX0, LX1, pdP), top:LINEY,
                    width:11, height:11, borderRadius:100, background:AZ,
                    transform:'translate(-50%,-50%)', boxShadow:`0 0 12px ${AZ}`, opacity:Math.sin(pdP*Math.PI)}}/>
                )}

                {/* Badge ×2 — "doblada", mostrar no declarar */}
                {x2P > 0 && (
                  <div className="f-mono" style={{position:'absolute', top:NODEY+NODEH+16, left:0, right:0, textAlign:'center',
                    opacity:clamp(x2P*1.3,0,1), transform:`scale(${lerp(0.6,1,x2E)})`}}>
                    <span style={{display:'inline-flex', alignItems:'center', gap:6,
                      fontSize:13, fontWeight:700, color:'#0085FF',
                      background:'rgba(0,133,255,0.08)', border:'1px solid rgba(0,133,255,0.3)',
                      borderRadius:100, padding:'5px 14px'}}>×2</span>
                  </div>
                )}
              </div>
            );
          })}

          {/* Subtítulo remate */}
          {remP > 0 && (
            <div style={{position:'absolute', left:0, right:0, top:872, textAlign:'center',
              opacity:remE, transform:`translateY(${(1-remE)*12}px)`}}>
              <div className="f-plex" style={{fontSize:36, fontWeight:600, letterSpacing:'-0.02em', color:'#0F172A'}}>
                Ni un solo punto que romper.
              </div>
            </div>
          )}

          {/* Píldora de salida hacia el "suelo" */}
          {salP > 0 && (
            <div style={{position:'absolute', left:0, right:0, bottom:74, textAlign:'center',
              opacity:clamp(salP*1.3,0,1), transform:`translateY(${(1-salE)*16}px)`}}>
              <span className="f-mono" style={{display:'inline-flex', alignItems:'center', gap:12,
                fontSize:13, fontWeight:700, letterSpacing:'2.5px', color:'#64748B',
                background:'#fff', border:'1px solid #E2E8F0', borderRadius:100, padding:'12px 26px',
                boxShadow:'0 10px 28px rgba(15,23,42,0.08)'}}>
                EMPECEMOS POR EL SUELO
                <span style={{color:'#0085FF', fontSize:16}}>↓</span>
              </span>
            </div>
          )}
        </div>
      )}
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaIN01 });
