// === Escena IN05 — "Cuatro copias. Dos empresas. Dos países." ===
// Duración real del mp3: 36.2s. SlideCanvas variant="light".
// La imagen-momento: un clúster se cae y el otro SIGUE con tus archivos enteros → "×4".
// Es un VALLE de anticipación antes del clímax de la 06 (la subida): baja energía en 14.38,
// no hace gran reveal en 34.62, solo apunta a "la subida".
// LOCUCIÓN (audio/escena-05.mp3):
// "Tus archivos: facturas, fotos, documentos, contratos. No se guardan en un sitio. Se guardan
//  en cuatro almacenes distintos. En dos empresas diferentes. Y en países diferentes. Ninguno
//  depende del otro. Piénsalo un momento. Aunque una empresa entera se cayera, con todos sus
//  servidores... la otra seguiría teniendo tus archivos, enteros. Cuatro copias, repartidas.
//  Imposibles de perder a la vez. Pero lo importante no es dónde se guardan. Es cómo llegan hasta
//  ahí. Y aquí está lo que casi nadie hace."
function EscenaIN05({ t }){
  const T = {
    chips:      1.45,  // "facturas, fotos, documentos, contratos" → 4 chips de tipo de archivo arriba
    buckets:    7.36,  // "cuatro almacenes distintos"             → 4 tarjetas bucket entran (stagger)
    cluster:    9.41,  // "dos empresas diferentes"                → se agrupan en 2 clústeres (OVHcloud · Cloudflare R2)
    pais:      11.02,  // "en países diferentes"                    → etiqueta de país en cada bucket (FR/DE/NL/IE)
    indep:     12.76,  // "Ninguno depende del otro"                → símbolo ⊘ sin dependencia entre clústeres
    valle:     14.38,  // "Piénsalo un momento"                     → VALLE: todo se atenúa, línea calma centrada
    cae:       16.70,  // "una empresa entera se cayera"            → el clúster R2 se ATENÚA/cae (gris, flash rojo)
    sigue:     20.89,  // "seguiría teniendo tus archivos"          → el clúster OVH sigue verde, check "A salvo"
    cuatro:    24.40,  // "Cuatro copias, repartidas"               → los 4 buckets se resaltan a la vez, contador ×4
    comoLlegan:31.92,  // "cómo llegan hasta ahí"                    → buckets se atenúan; "No es dónde. Es cómo llegan."
    subida:    34.62,  // "lo que casi nadie hace"                   → cue tenue "LA SUBIDA ↓" (anticipación, sin reveal)
  };

  // ---- Chips de tipo de archivo (arriba) ----
  const FILES = [
    { name:'factura',   ac:'#0085FF' },
    { name:'foto',      ac:'#10B981' },
    { name:'documento', ac:'#64748B' },
    { name:'contrato',  ac:'#FB8C00' },
  ];
  const chipsFade = clamp((t-T.cluster)/0.8, 0, 1);   // se apagan al formarse los clústeres (ya cumplieron)

  // ---- Valle: atenuación transitoria 14.38 → arranque de la caída ----
  const valleIn  = clamp((t-T.valle)/0.5, 0, 1);
  const valleOut = clamp((t-T.cae)/0.5, 0, 1);
  const valleDim = valleIn*(1-valleOut);

  // ---- Caída del clúster R2 (16.70) y recuperación en el ×4 (24.40) ----
  const flashK   = Math.sin(clamp((t-T.cae)/0.5, 0, 1) * Math.PI);          // UN destello rojo al golpe
  const grayK    = Easing.easeOutCubic(clamp((t-(T.cae+0.15))/0.7, 0, 1));  // se asienta en gris
  const recoverK = Easing.easeOutCubic(clamp((t-T.cuatro)/0.6, 0, 1));      // vuelve todo a normal
  const r2Down   = grayK*(1-recoverK);
  const r2Flash  = flashK*(1-recoverK);

  // ---- Check del clúster OVH "a salvo" (20.89), se retira al igualarse en el ×4 ----
  const checkE   = Easing.easeOutBack(clamp((t-T.sigue)/0.5, 0, 1));
  const checkVis = clamp((t-T.sigue)/0.5, 0, 1)*(1-recoverK);

  // ---- ×4: contador + pulso simultáneo en los 4 buckets ----
  const x4       = useCountUp(0, 4, T.cuatro, 0.7, t);
  const cuatroP  = clamp((t-T.cuatro)/0.5, 0, 1);
  const cuatroE  = Easing.easeOutBack(cuatroP);
  const allPulse = Math.sin(clamp((t-T.cuatro)/1.2, 0, 1) * Math.PI);       // UN pulse de remate
  const cuatroOut= clamp((t-T.comoLlegan)/0.5, 0, 1);
  const cuatroVis= cuatroP*(1-cuatroOut);

  // ---- Puente final: los buckets se atenúan, entra la frase-remate ----
  const dimLlegan= Easing.easeOutCubic(clamp((t-T.comoLlegan)/0.6, 0, 1));
  const lleganP  = Easing.easeOutCubic(clamp((t-(T.comoLlegan+0.2))/0.7, 0, 1));

  // ---- ⊘ sin dependencia (12.76) ----
  const indepE   = Easing.easeOutBack(clamp((t-T.indep)/0.5, 0, 1));
  const indepVis = clamp((t-T.indep)/0.5, 0, 1)*(1-dimLlegan);

  // ---- Cue de salida hacia la 06 (34.62) ----
  const subE = Easing.easeOutBack(clamp((t-T.subida)/0.5, 0, 1));

  // ===================== Un clúster (par de buckets gemelos) =====================
  // side left=292 · derecha=1132. Buckets abs: centros 417/663 (izq) · 1257/1503 (der).
  const Cluster = (cfg)=>{
    if (t < T.buckets - 0.05) return null;
    const down = cfg.down, flash = cfg.flash, glow = cfg.glow;

    const wrapStyle = {
      position:'absolute', left:cfg.left, top:405, width:496, height:300,
      opacity:(1-0.5*down)*(1-0.55*dimLlegan)*(1-0.15*valleDim),
      filter:`saturate(${(1-0.85*down)*(1-0.45*dimLlegan)})`,
      transform:`translateY(${down*26}px)`,
    };
    const panelE  = Easing.easeOutCubic(clamp((t-T.cluster)/0.7, 0, 1));
    const lineDraw= Easing.easeOutCubic(clamp((t-(T.cluster+0.1))/0.6, 0, 1));
    const travP   = clamp((t-(T.cluster+0.5+cfg.stagger))/1.0, 0, 1);  // pulso "en tiempo real" A→B, una vez
    const paisP   = clamp((t-T.pais)/0.5, 0, 1);

    return (
      <div style={wrapStyle}>
        {/* Panel del clúster (aparece al agruparse) */}
        <div style={{position:'absolute', inset:0, background:'rgba(255,255,255,0.5)', border:'1px solid #E2E8F0',
          borderRadius:22, boxShadow:'0 14px 34px rgba(15,23,42,0.06)', opacity:panelE,
          transform:`scale(${lerp(0.955,1,panelE)})`}}/>

        {/* Píldora de marca + tag técnico */}
        <div style={{position:'absolute', left:248-95, top:16, width:190, height:38, opacity:panelE,
          transform:`translateY(${(1-panelE)*-8}px)`, display:'flex', alignItems:'center', justifyContent:'center', gap:8,
          background:'#fff', border:'1px solid #E2E8F0', borderRadius:100, boxShadow:'0 6px 16px rgba(15,23,42,0.07)'}}>
          <span style={{width:8, height:8, borderRadius:100, background:cfg.brandColor}}/>
          <span className="f-mono" style={{fontSize:13, fontWeight:700, color:cfg.brandColor, letterSpacing:'0.3px'}}>{cfg.brand}</span>
          <span className="f-mono" style={{fontSize:10, fontWeight:700, color:'#94A3B8', background:'#F1F5F9', borderRadius:6, padding:'2px 6px'}}>{cfg.tech}</span>
        </div>

        {/* Línea de sincronía viva entre los gemelos + pulso que viaja una vez */}
        <svg width="496" height="300" style={{position:'absolute', inset:0, pointerEvents:'none'}}>
          <line x1="125" y1="150" x2="371" y2="150" stroke="#10B981" strokeWidth="2.5" strokeLinecap="round"
            strokeDasharray="246" strokeDashoffset={246*(1-lineDraw)} opacity={0.55*lineDraw}/>
          {travP > 0 && travP < 1 && (
            <circle cx={lerp(125,371,Easing.easeInOutCubic(travP))} cy="150" r="5" fill="#10B981"
              opacity={clamp(Math.min(travP*6,(1-travP)*4),0,1)}/>
          )}
        </svg>

        {/* Los dos buckets gemelos */}
        {cfg.buckets.map((bk,i)=>{
          const bp = clamp((t-(T.buckets+i*0.15+cfg.stagger))/0.6, 0, 1);
          if (bp <= 0) return null;
          const be = Easing.easeOutBack(bp);
          const relLeft = i===0 ? 20 : 266;
          const bd = flash > 0.06 ? `rgba(239,68,68,${0.4+0.5*flash})`
                   : down  > 0.45 ? '#CBD5E1'
                   : `rgba(16,185,129,${0.16+0.2*(1-down)+0.45*glow})`;
          const dotColor = flash > 0.06 ? '#EF4444' : down > 0.45 ? '#94A3B8' : '#10B981';
          const cylFill  = down > 0.45 ? '#EEF2F7' : 'rgba(0,133,255,0.10)';
          const cylStroke= down > 0.45 ? '#CBD5E1' : '#0085FF';
          return (
            <div key={bk.name} style={{position:'absolute', left:relLeft, top:78, width:210, height:132,
              background:'#fff', border:`1.5px solid ${bd}`, borderRadius:16,
              boxShadow:`0 10px 26px rgba(15,23,42,0.07), 0 8px 26px rgba(16,185,129,${0.5*glow*(1-down)})`,
              opacity:clamp(bp*1.3,0,1), transform:`translateY(${(1-be)*-24}px)`, overflow:'hidden'}}>
              {/* Cilindro (bucket S3/R2) + nombre */}
              <div style={{display:'flex', alignItems:'center', gap:12, padding:'16px 14px 0'}}>
                <svg width="34" height="40" viewBox="0 0 34 40" style={{flexShrink:0}}>
                  <ellipse cx="17" cy="8" rx="15" ry="6" fill={cylFill} stroke={cylStroke} strokeWidth="1.6"/>
                  <path d="M2 8 V32 C2 35.3 8.7 38 17 38 C25.3 38 32 35.3 32 32 V8" fill={cylFill} stroke={cylStroke} strokeWidth="1.6"/>
                  <path d="M2 20 C2 23.3 8.7 26 17 26 C25.3 26 32 23.3 32 20" fill="none" stroke={cylStroke} strokeWidth="1.2" opacity="0.55"/>
                </svg>
                <div style={{minWidth:0}}>
                  <div className="f-mono" style={{fontSize:14, fontWeight:800, color:'#0F172A', whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis'}}>{bk.name}</div>
                  <div className="f-mono" style={{fontSize:10, fontWeight:600, color:'#94A3B8', letterSpacing:'1.5px', marginTop:3}}>BUCKET</div>
                </div>
              </div>
              {/* País + estado */}
              <div style={{position:'absolute', left:14, right:14, bottom:12, display:'flex', alignItems:'center', justifyContent:'space-between'}}>
                <span className="f-mono" style={{fontSize:12, fontWeight:700, color: down>0.45?'#94A3B8':'#334155',
                  background:'#F1F5F9', border:'1px solid #E2E8F0', borderRadius:7, padding:'3px 9px',
                  opacity:paisP, transform:`translateY(${(1-paisP)*6}px)`}}>{bk.pais}</span>
                <span style={{width:9, height:9, borderRadius:100, background:dotColor,
                  boxShadow:`0 0 0 3px ${dotColor}22`}}/>
              </div>
            </div>
          );
        })}

        {/* Etiqueta de estado del clúster: check "a salvo" (OVH) / "sin servicio" (R2) */}
        {cfg.showCheck && checkVis > 0.02 && (
          <div style={{position:'absolute', left:248-78, top:252, width:156, height:34, opacity:clamp(checkVis*1.3,0,1),
            transform:`scale(${lerp(0.8,1,checkE)})`, display:'flex', alignItems:'center', justifyContent:'center', gap:8,
            background:'#ECFDF5', border:'1px solid #A7F3D0', borderRadius:100, boxShadow:'0 8px 20px rgba(16,185,129,0.18)'}}>
            <svg width="15" height="15" viewBox="-12 -12 24 24"><path d="M-7 0 L-2 5 L8 -6" stroke="#059669" strokeWidth="3.2" strokeLinecap="round" strokeLinejoin="round" fill="none"/></svg>
            <span className="f-mono" style={{fontSize:12, fontWeight:800, letterSpacing:'1.5px', color:'#059669'}}>A SALVO</span>
          </div>
        )}
        {!cfg.showCheck && down > 0.35 && (
          <div style={{position:'absolute', left:248-80, top:252, width:160, height:34, opacity:clamp((down-0.3)/0.4,0,1),
            display:'flex', alignItems:'center', justifyContent:'center', gap:8,
            background:'#F1F5F9', border:'1px solid #E2E8F0', borderRadius:100}}>
            <span style={{width:8, height:8, borderRadius:100, background:'#94A3B8'}}/>
            <span className="f-mono" style={{fontSize:12, fontWeight:800, letterSpacing:'1.5px', color:'#94A3B8'}}>SIN SERVICIO</span>
          </div>
        )}
      </div>
    );
  };

  return (
    <SlideCanvas variant="light">
      <DemoHeader t={t} eyebrow="INFRAESTRUCTURA · TUS ARCHIVOS"
        title={<span>Cuatro copias. Dos empresas. Dos países.</span>}/>

      {/* ===== Chips de tipo de archivo (arriba) ===== */}
      {FILES.map((f,i)=>{
        const p = clamp((t-(T.chips+i*0.1))/0.5, 0, 1);
        if (p <= 0) return null;
        const e = Easing.easeOutBack(p);
        return (
          <div key={f.name} style={{position:'absolute', left:597+i*186, top:168, width:168, height:52,
            background:'#fff', border:'1px solid #E2E8F0', borderRadius:14, boxShadow:'0 8px 20px rgba(15,23,42,0.06)',
            display:'flex', alignItems:'center', gap:11, padding:'0 16px',
            opacity:clamp(p*1.3,0,1)*(1-0.6*chipsFade)*(1-0.5*dimLlegan),
            transform:`translateY(${(1-e)*-12}px) scale(${lerp(0.85,1,e)})`}}>
            <div style={{width:28, height:28, borderRadius:8, background:`${f.ac}1A`, display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0}}>
              <svg width="15" height="15" viewBox="0 0 24 24" fill="none">
                <path d="M6 3 H14 L19 8 V21 H6 Z" stroke={f.ac} strokeWidth="1.8" strokeLinejoin="round"/>
                <path d="M14 3 V8 H19" stroke={f.ac} strokeWidth="1.8" strokeLinejoin="round"/>
              </svg>
            </div>
            <span className="f-plex" style={{fontSize:15, fontWeight:600, color:'#0F172A'}}>{f.name}</span>
          </div>
        );
      })}

      {/* storage.dinaup.com — atrezzo de sistema real */}
      {t >= T.chips+0.3 && (
        <div style={{position:'absolute', left:0, right:0, top:238, textAlign:'center',
          opacity:clamp((t-(T.chips+0.3))/0.6,0,1)*(1-0.55*chipsFade)*(1-0.5*dimLlegan)}}>
          <span className="f-mono" style={{fontSize:12, fontWeight:600, letterSpacing:'2px', color:'#94A3B8',
            background:'#fff', border:'1px solid #E2E8F0', borderRadius:100, padding:'5px 14px'}}>storage.dinaup.com</span>
        </div>
      )}

      {/* ===== Los dos clústeres gemelos ===== */}
      {Cluster({ left:292,  down:0,      flash:0,       glow:allPulse, showCheck:true,  stagger:0,
                 brand:'OVHcloud',      brandColor:'#123F6D', tech:'S3',
                 buckets:[{name:'facturas-fr', pais:'FR'}, {name:'docs-de', pais:'DE'}] })}
      {Cluster({ left:1132, down:r2Down, flash:r2Flash, glow:allPulse, showCheck:false, stagger:0.3,
                 brand:'Cloudflare R2', brandColor:'#F6821F', tech:'R2',
                 buckets:[{name:'fotos-nl', pais:'NL'}, {name:'contratos-ie', pais:'IE'}] })}

      {/* ===== ⊘ sin dependencia (entre clústeres) ===== */}
      {indepVis > 0.02 && (
        <div style={{position:'absolute', left:960-70, top:512, width:140, textAlign:'center',
          opacity:clamp(indepVis*1.3,0,1), transform:`translateY(${(1-indepE)*10}px) scale(${lerp(0.85,1,indepE)})`}}>
          <div style={{width:56, height:56, margin:'0 auto', borderRadius:100, background:'#fff', border:'1.5px solid #E2E8F0',
            boxShadow:'0 10px 26px rgba(15,23,42,0.08)', display:'flex', alignItems:'center', justifyContent:'center'}}>
            <svg width="30" height="30" viewBox="0 0 24 24" fill="none">
              <circle cx="12" cy="12" r="9" stroke="#64748B" strokeWidth="2"/>
              <path d="M6 6 L18 18" stroke="#64748B" strokeWidth="2" strokeLinecap="round"/>
            </svg>
          </div>
          <div className="f-mono" style={{fontSize:11, fontWeight:700, letterSpacing:'1.5px', color:'#94A3B8', marginTop:9, lineHeight:1.3}}>SIN<br/>DEPENDENCIA</div>
        </div>
      )}

      {/* ===== VALLE — "Piénsalo un momento." (baja de energía antes del clímax) ===== */}
      {valleDim > 0.02 && (
        <div style={{position:'absolute', left:0, right:0, top:812, textAlign:'center', opacity:clamp(valleDim*1.4,0,1)}}>
          <div className="f-plex" style={{fontSize:34, fontWeight:500, letterSpacing:'-0.02em', color:'#64748B'}}>Piénsalo un momento.</div>
        </div>
      )}

      {/* ===== ×4 — cuatro copias repartidas (resalte simultáneo) ===== */}
      {cuatroVis > 0.02 && (
        <div style={{position:'absolute', left:0, right:0, top:772, textAlign:'center',
          opacity:clamp(cuatroVis*1.4,0,1), transform:`translateY(${(1-cuatroE)*14}px) scale(${lerp(0.9,1,cuatroE)})`}}>
          <div style={{display:'inline-flex', alignItems:'center', gap:16, background:'#fff', border:'1px solid #A7F3D0',
            borderRadius:18, padding:'12px 26px', boxShadow:`0 14px 36px rgba(16,185,129,${0.14+0.22*allPulse})`}}>
            <span className="f-mono" style={{fontSize:44, fontWeight:800, color:'#059669', lineHeight:1}}>×{x4}</span>
            <div style={{textAlign:'left'}}>
              <div className="f-plex" style={{fontSize:20, fontWeight:700, color:'#0F172A', lineHeight:1.1}}>Cuatro copias, repartidas</div>
              <div className="f-plex" style={{fontSize:14, fontWeight:500, color:'#64748B', marginTop:2}}>Imposibles de perder a la vez.</div>
            </div>
          </div>
        </div>
      )}

      {/* ===== Puente hacia la 06 — "No es dónde. Es cómo llegan." ===== */}
      {lleganP > 0.02 && (
        <div style={{position:'absolute', left:0, right:0, top:498, textAlign:'center',
          opacity:clamp(lleganP*1.3,0,1), transform:`translateY(${(1-lleganP)*14}px)`}}>
          <div className="f-plex" style={{fontSize:52, fontWeight:800, letterSpacing:'-0.03em', color:'#0F172A', lineHeight:1.1}}>
            No es <span style={{color:'#94A3B8', fontWeight:600}}>dónde</span>. Es <span className="grad-text">cómo llegan</span>.
          </div>
        </div>
      )}

      {/* ===== Cue de salida: LA SUBIDA ↓ (anticipación, sin reveal) ===== */}
      {subE > 0.02 && (
        <div style={{position:'absolute', left:0, right:0, bottom:70, textAlign:'center',
          opacity:clamp(subE,0,1)*0.85, transform:`translateY(${(1-subE)*10}px)`}}>
          <span className="f-mono" style={{fontSize:14, fontWeight:700, letterSpacing:'3px', color:'#64748B',
            background:'#fff', border:'1px solid #E2E8F0', borderRadius:100, padding:'10px 22px', boxShadow:'0 8px 22px rgba(15,23,42,0.07)'}}>
            LA SUBIDA ↓
          </span>
        </div>
      )}
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaIN05 });
