// === FLEX · Caso de éxito 2 de 3 — Autodesguaces Alicante (15s) ===
// Mismo patrón visual que el caso Minigolf (F4): card de sección grande y
// centrada ("Vehículos") cuyo interior es un FORMULARIO de dos columnas que se
// rellena campo a campo (label mono + caja de input + valor + chip de tipo,
// cadencia ~1.5s), mini-card conectada ("Recambios") a la derecha, y sello final.
// Sincronizado con cues slide_flexdesguace (0.5-3.0 / 3.0-6.0 / 6.0-9.4 /
// 9.4-12.2 / 12.2-14.7).

function SlideFlexDesguace({ t }){
  // --- Geometría (idéntica en los 3 casos de éxito) ---
  // Card protagonista: 640 de ancho, centrada → x 640..1280, top 250, alto 414.
  // Formulario (IDÉNTICO en F4/F5/F6): zona de 580×258 a 30 del borde izq. de la card.
  // 3 filas con paso vertical 96: fila 1 = dos celdas emparejadas de 282 (gutter 16),
  // fila 2 = una celda a media anchura, fila 3 = campo a ancho completo (580).
  // Cada campo: label mono 11px (h 14 + gap 6) + caja de input h 46 (borde #D3DDE8, radio 10).
  // Mini-card conectada: x 1360..1660, top 386. Sello centrado en y 786.
  const CARD_X = 640, CARD_W = 640, CARD_TOP = 250, CARD_H = 414;
  const F_W = 580, F_LBL = 14, F_LBLGAP = 6, F_IN = 46;  // zona, label, gap, alto de input

  // --- Timeline ---
  const FIELDS = [
    { at:3.4, label:'Matrícula',     value:'4527-KLM',    type:'texto',    typed:true,  x:0,   y:0,   w:282 },
    { at:4.9, label:'Nº de puertas', value:'5',           type:'numero',   typed:true,  x:298, y:0,   w:282 },
    { at:6.4, label:'Motor',         value:'1.6 TDCi',    type:'selector', typed:false, x:0,   y:96,  w:282 },
    { at:7.9, label:'Titular',       value:'María López', type:'enlace',   typed:false, x:0,   y:192, w:580 },
  ];
  const subSwapAt = 9.0;   // "Nueva sección" → "4 campos · Lista"
  const miniAt    = 9.6;   // mini-card Recambios (cue 9.4-12.2)
  const lineAt    = 9.9;   // la línea conectora se dibuja
  const phraseAt  = 10.7;  // "Cada pieza, colgando de su coche"
  const selloAt   = 12.3;  // sello final (cue 12.2-14.7) — muere en 12.9 (< 14.2)

  // --- Iconos SVG de línea (cero emojis en la UI) ---
  const carIcon = (size, color) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M3.2 16.2 V13.6 C3.2 12.5 4 11.6 5.1 11.4 L6.7 11.1 L8.5 8.5 C8.9 7.9 9.6 7.5 10.3 7.5 H14 C14.8 7.5 15.5 7.9 15.9 8.5 L17.7 11.1 L19.3 11.4 C20.4 11.6 21.2 12.5 21.2 13.6 V16.2" stroke={color} strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M6.7 11.1 H17.7" stroke={color} strokeWidth="1.7" strokeLinecap="round"/>
      <path d="M12.2 7.5 V11.1" stroke={color} strokeWidth="1.5"/>
      <circle cx="7.6" cy="16.6" r="1.9" stroke={color} strokeWidth="1.7"/>
      <circle cx="16.8" cy="16.6" r="1.9" stroke={color} strokeWidth="1.7"/>
      <path d="M9.5 16.6 H14.9" stroke={color} strokeWidth="1.7" strokeLinecap="round"/>
    </svg>
  );
  const puzzleIcon = (size, color) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M9.5 4.8 A2.1 2.1 0 0 1 13.7 4.8 V6.2 H17.2 A1.6 1.6 0 0 1 18.8 7.8 V11.3 H17.4 A2.1 2.1 0 0 0 17.4 15.5 H18.8 V19 A1.6 1.6 0 0 1 17.2 20.6 H13.7 V19.2 A2.1 2.1 0 0 0 9.5 19.2 V20.6 H6 A1.6 1.6 0 0 1 4.4 19 V15.5 H5.8 A2.1 2.1 0 0 0 5.8 11.3 H4.4 V7.8 A1.6 1.6 0 0 1 6 6.2 H9.5 Z" stroke={color} strokeWidth="1.6" strokeLinejoin="round"/>
    </svg>
  );
  const nutIcon = (size) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 3.4 L19 7.4 V16 L12 20 L5 16 V7.4 Z" stroke="#fff" strokeWidth="1.7" strokeLinejoin="round"/>
      <circle cx="12" cy="11.7" r="3" stroke="#fff" strokeWidth="1.7"/>
    </svg>
  );
  // Catálogo de tipos de dato (paleta de chips: Número=azul, Texto=gris,
  // Fecha/Hora=violeta, Importe=verde, Selector=ámbar, Enlace=brand)
  const TYPES = {
    texto:    { label:'Texto',      fg:'#64748B', bg:'#F1F5F9',               brd:'#E2E8F0' },
    numero:   { label:'Número',     fg:'#1D4ED8', bg:'rgba(37,99,235,0.08)',  brd:'rgba(37,99,235,0.30)' },
    fecha:    { label:'Fecha/Hora', fg:'#7C3AED', bg:'rgba(124,58,237,0.08)', brd:'rgba(124,58,237,0.30)' },
    importe:  { label:'Importe €',  fg:'#059669', bg:'rgba(5,150,105,0.08)',  brd:'rgba(5,150,105,0.30)' },
    selector: { label:'Selector',   fg:'#B45309', bg:'rgba(217,119,6,0.10)',  brd:'rgba(217,119,6,0.32)' },
    enlace:   { label:'Enlace',     fg:'#0085FF', bg:'rgba(0,133,255,0.08)',  brd:'rgba(0,133,255,0.30)' },
  };
  const chipIcon = (kind, color) => {
    const s = { stroke:color, strokeWidth:2, strokeLinecap:'round', strokeLinejoin:'round', fill:'none' };
    if (kind === 'texto')    return <svg width="12" height="12" viewBox="0 0 24 24"><path d="M6 7 V5 H18 V7 M12 5 V19 M9.4 19 H14.6" {...s}/></svg>;
    if (kind === 'numero')   return <svg width="12" height="12" viewBox="0 0 24 24"><path d="M9.6 4.8 L8 19.2 M16 4.8 L14.4 19.2 M5.4 9.4 H19 M5 14.6 H18.6" {...s}/></svg>;
    if (kind === 'fecha')    return <svg width="12" height="12" viewBox="0 0 24 24"><rect x="4" y="5.5" width="16" height="14.5" rx="3" {...s}/><path d="M8 3.5 V7.5 M16 3.5 V7.5 M4 10.5 H20" {...s}/></svg>;
    if (kind === 'importe')  return <svg width="12" height="12" viewBox="0 0 24 24"><path d="M17 7.2 A6.4 6.4 0 1 0 17 16.8 M4.8 10.4 H12.2 M4.8 13.6 H11.4" {...s}/></svg>;
    if (kind === 'selector') return <svg width="12" height="12" viewBox="0 0 24 24"><rect x="3.6" y="6" width="16.8" height="12" rx="3" {...s}/><path d="M9.2 10.6 L12 13.4 L14.8 10.6" {...s}/></svg>;
    return <svg width="12" height="12" viewBox="0 0 24 24"><path d="M10 13 a5 5 0 0 0 7.5 .5 l2 -2 a5 5 0 0 0 -7 -7 l-1.2 1.2 M14 11 a5 5 0 0 0 -7.5 -.5 l-2 2 a5 5 0 0 0 7 7 l1.2 -1.2" {...s}/></svg>;
  };

  // --- Entradas de cabecera ---
  const kickP  = clamp((t-0.2)/0.5, 0, 1);
  const kickE  = Easing.easeOutBack(kickP);
  const eyeP   = clamp((t-0.45)/0.6, 0, 1);
  const titleP = clamp((t-0.65)/0.7, 0, 1);
  const titleE = Easing.easeOutCubic(titleP);

  // --- Card ---
  const cardP = clamp((t-1.3)/0.7, 0, 1);
  const cardE = Easing.easeOutCubic(cardP);
  const iconP = clamp((t-1.7)/0.5, 0, 1);
  const iconE = Easing.easeOutBack(iconP);
  const nameP = clamp((t-1.9)/0.5, 0, 1);
  const subP  = clamp((t-2.1)/0.5, 0, 1);
  const chipP = clamp((t-2.3)/0.5, 0, 1);
  const swapP = clamp((t-subSwapAt)/0.4, 0, 1);

  // --- Mini-card + conector ---
  const miniP  = clamp((t-miniAt)/0.55, 0, 1);
  const miniE  = Easing.easeOutBack(miniP);
  const lineP  = Easing.easeInOutCubic(clamp((t-lineAt)/0.7, 0, 1));
  const phraP  = clamp((t-phraseAt)/0.6, 0, 1);
  const phraE  = Easing.easeOutCubic(phraP);

  // --- Sello ---
  const selloP = clamp((t-selloAt)/0.55, 0, 1);
  const selloE = Easing.easeOutCubic(selloP);

  return (
    <SlideCanvas variant="light">

      {/* ===== Píldora "CASOS DE ÉXITO · 2 de 3" (estilo TimeBadge, sin hora) ===== */}
      <div style={{position:'absolute', top:44, left:60, zIndex:6, display:'flex', alignItems:'center', gap:12, padding:'10px 18px 10px 14px', background:'rgba(15,23,42,0.92)', border:'1px solid rgba(255,255,255,0.10)', borderRadius:14, boxShadow:'0 12px 32px rgba(15,23,42,0.25)', backdropFilter:'blur(10px)', opacity:clamp(kickP*1.3,0,1), transform:`translateY(${(1-kickE)*-14}px) scale(${lerp(0.85,1,kickE)})`}}>
        <div style={{width:34, height:34, borderRadius:10, background:'linear-gradient(135deg,#0085FF,#56E16A)', display:'flex', alignItems:'center', justifyContent:'center'}}>
          {puzzleIcon(19, '#fff')}
        </div>
        <div>
          <div className="f-mono" style={{fontSize:21, fontWeight:700, color:'#fff', lineHeight:1, letterSpacing:'0.02em'}}>2 de 3</div>
          <div className="f-mono" style={{fontSize:9, fontWeight:600, color:'rgba(255,255,255,0.55)', letterSpacing:'2.5px', marginTop:3, textTransform:'uppercase'}}>Casos de éxito</div>
        </div>
      </div>

      {/* ===== Cabecera: eyebrow + título del caso con icono coche ===== */}
      <div style={{position:'absolute', top:46, left:0, right:0, textAlign:'center', opacity:eyeP, zIndex:5}}>
        <div className="f-plex" style={{fontSize:14, fontWeight:600, letterSpacing:'2px', color:'var(--dn-primary)'}}>DINAUP FLEX</div>
      </div>
      <div style={{position:'absolute', top:78, left:0, right:0, display:'flex', alignItems:'center', justifyContent:'center', gap:16, opacity:titleE, transform:`translateY(${(1-titleE)*14}px)`, zIndex:5}}>
        <div style={{width:52, height:52, borderRadius:14, background:'#E8F3FF', border:'1px solid rgba(0,133,255,0.22)', display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0}}>
          {carIcon(30, '#0085FF')}
        </div>
        <h2 className="f-plex" style={{fontSize:42, fontWeight:700, letterSpacing:'-0.03em', margin:0, color:'#0F172A'}}>Autodesguaces Alicante</h2>
      </div>

      {/* ===== Card protagonista: "Vehículos" ===== */}
      <div style={{position:'absolute', left:CARD_X, top:CARD_TOP, width:CARD_W, height:CARD_H, background:'#fff', borderRadius:20, border:'1px solid #E2E8F0', boxShadow:'0 30px 80px rgba(0,133,255,0.14), 0 4px 14px rgba(15,23,42,0.05)', padding:'24px 24px', opacity:cardP, transform:`translateY(${(1-cardE)*28}px) scale(${lerp(0.96,1,cardE)})`}}>

        {/* Cabecera de la card */}
        <div style={{display:'flex', alignItems:'center', gap:16, height:60}}>
          <div style={{width:56, height:56, borderRadius:16, background:'linear-gradient(135deg,#0085FF,#00C1FF)', display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0, boxShadow:'0 10px 26px rgba(0,133,255,0.30)', opacity:clamp(iconP*1.3,0,1), transform:`scale(${lerp(0.6,1,iconE)})`}}>
            {carIcon(30, '#fff')}
          </div>
          <div style={{flex:1, minWidth:0}}>
            <div className="f-plex" style={{fontSize:26, fontWeight:700, color:'#0F172A', letterSpacing:'-0.02em', lineHeight:1.1, opacity:nameP, transform:`translateY(${(1-Easing.easeOutCubic(nameP))*8}px)`}}>Vehículos</div>
            <div style={{position:'relative', height:15, marginTop:5, opacity:subP}}>
              <span className="f-mono" style={{position:'absolute', left:0, top:0, fontSize:10.5, fontWeight:600, letterSpacing:'1.8px', color:'#94A3B8', textTransform:'uppercase', opacity:1-swapP}}>Nueva sección</span>
              <span className="f-mono" style={{position:'absolute', left:0, top:0, fontSize:10.5, fontWeight:600, letterSpacing:'1.8px', color:'#059669', textTransform:'uppercase', opacity:swapP}}>4 campos · Lista</span>
            </div>
          </div>
          <span style={{display:'inline-flex', alignItems:'center', gap:6, padding:'6px 13px', background:'rgba(0,133,255,0.08)', border:'1px solid rgba(0,133,255,0.25)', borderRadius:100, opacity:chipP, transform:`scale(${lerp(0.8,1,Easing.easeOutBack(chipP))})`}}>
            {puzzleIcon(14, '#0085FF')}
            <span className="f-plex" style={{fontSize:12.5, fontWeight:700, color:'#0085FF'}}>Flex</span>
          </span>
        </div>

        <RuedaDivider style={{margin:'18px 0'}}/>

        {/* Zona de campos: FORMULARIO de dos columnas (geometría idéntica en F4/F5/F6) */}
        <div style={{position:'relative', marginLeft:6, width:F_W, height:258}}>

          {/* Estado vacío antes del primer campo */}
          {(() => {
            const emptyP = Math.min(clamp((t-2.3)/0.5, 0, 1), clamp((3.4-t)/0.3, 0, 1));
            if (emptyP <= 0) return null;
            return (
              <div style={{position:'absolute', inset:0, border:'1.5px dashed #D3DDE8', borderRadius:12,
                display:'flex', alignItems:'center', justifyContent:'center', opacity:emptyP, pointerEvents:'none'}}>
                <span style={{fontSize:13, fontStyle:'italic', color:'#94A3B8'}}>Sin campos todavía</span>
              </div>
            );
          })()}

          {/* Campos: cada beat = slot (label + caja vacía) → valor (typewriter o fade) → chip de tipo */}
          {FIELDS.map((f, i) => {
            if (t < f.at) return null;
            const ty = TYPES[f.type];
            const typeDur = f.typed ? Math.min(0.7, 0.22 + 0.055*f.value.length) : 0.35;
            const slotP = clamp((t-f.at)/0.4, 0, 1);                       // slot: label + caja vacía
            const slotE = Easing.easeOutCubic(slotP);
            const valP  = clamp((t-(f.at+0.42))/typeDur, 0, 1);            // valor de ejemplo
            const valE  = Easing.easeOutCubic(valP);
            const chipP = clamp((t-(f.at+0.42+typeDur+0.12))/0.4, 0, 1);   // chip al acabar el valor
            const chipE = Easing.easeOutBack(chipP);
            const focus = clamp((t-f.at)/0.25, 0, 1) * (1 - clamp((t-(f.at+0.42+typeDur+0.1))/0.45, 0, 1));
            const shown = f.typed ? f.value.slice(0, Math.ceil(valP*f.value.length)) : f.value;
            const caretOn = f.typed && t >= f.at+0.3 && valP < 1;
            const bR = Math.round(lerp(211,0,focus)), bG = Math.round(lerp(221,133,focus)), bB = Math.round(lerp(232,255,focus));
            return (
              <div key={i} style={{position:'absolute', left:f.x, top:f.y, width:f.w,
                opacity:clamp(slotP*1.5,0,1), transform:`translateY(${(1-slotE)*10}px)`}}>
                <div className="f-mono" style={{height:F_LBL, lineHeight:`${F_LBL}px`, fontSize:11, fontWeight:600, letterSpacing:'1.2px', textTransform:'uppercase', color:'#94A3B8', whiteSpace:'nowrap'}}>{f.label}</div>
                <div style={{marginTop:F_LBLGAP, height:F_IN, background:'#fff', borderRadius:10,
                  border:`1px solid rgb(${bR},${bG},${bB})`,
                  boxShadow:`0 1px 2px rgba(15,23,42,0.04), 0 0 0 3px rgba(0,133,255,${0.12*focus})`,
                  display:'flex', alignItems:'center', gap:10, padding:'0 14px'}}>
                  <span style={{display:'inline-flex', alignItems:'center', minWidth:0}}>
                    <span className="f-plex" style={{fontSize:16, fontWeight:600, whiteSpace:'pre',
                      color: f.type === 'enlace' ? '#0085FF' : '#0F172A',
                      textDecoration: f.type === 'enlace' ? 'underline' : 'none',
                      textDecorationColor:'rgba(0,133,255,0.35)', textUnderlineOffset:3,
                      opacity: f.typed ? 1 : valE,
                      transform: f.typed ? 'none' : `translateY(${(1-valE)*6}px)`}}>{shown}</span>
                    {caretOn && <span style={{width:1.5, height:20, marginLeft:2, background:'#0F172A', opacity:Math.sin(t*10) > 0 ? 0.9 : 0.15}}/>}
                  </span>
                  <span style={{flex:1}}/>
                  {f.type === 'selector' && (
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" style={{flexShrink:0, opacity:slotE}}>
                      <path d="M6.5 9.5 L12 15 L17.5 9.5" stroke="#94A3B8" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
                    </svg>
                  )}
                  {chipP > 0 && (
                    <span style={{display:'inline-flex', alignItems:'center', gap:5, height:24, padding:'0 9px', borderRadius:100,
                      background:ty.bg, border:`1px solid ${ty.brd}`, flexShrink:0,
                      opacity:clamp(chipP*1.4,0,1), transform:`scale(${lerp(0.6,1,chipE)})`, transformOrigin:'right center'}}>
                      {chipIcon(f.type, ty.fg)}
                      <span className="f-mono" style={{fontSize:10, fontWeight:700, letterSpacing:'0.3px', color:ty.fg, whiteSpace:'nowrap'}}>{ty.label}</span>
                    </span>
                  )}
                </div>
              </div>
            );
          })}
        </div>
      </div>

      {/* ===== Conector Recambios → Vehículos ===== */}
      {t >= lineAt && (
        <svg style={{position:'absolute', inset:0, pointerEvents:'none', zIndex:4}} width="1920" height="1080" viewBox="0 0 1920 1080">
          <path d="M1356 434 C1322 434, 1318 457, 1288 457" stroke="#0085FF" strokeWidth="2.2" fill="none" strokeLinecap="round" pathLength="100" strokeDasharray="100" strokeDashoffset={100*(1-lineP)}/>
          <circle cx="1356" cy="434" r="4" fill="#0085FF" opacity={clamp(lineP*3,0,1)}/>
          <path d="M1300 451 L1288 457 L1300 463 Z" fill="#0085FF" opacity={clamp((lineP-0.9)*10,0,1)}/>
        </svg>
      )}

      {/* ===== Mini-card "Recambios" ===== */}
      {t >= miniAt && (
        <div style={{position:'absolute', left:1360, top:386, width:300, background:'#fff', borderRadius:14, border:'1px solid #E2E8F0', boxShadow:'0 14px 40px rgba(0,133,255,0.10)', padding:'18px 20px', display:'flex', alignItems:'center', gap:14, opacity:clamp(miniP*1.3,0,1), transform:`translateX(${(1-miniE)*16}px) scale(${lerp(0.82,1,miniE)})`, zIndex:5}}>
          <div style={{width:44, height:44, borderRadius:12, background:'linear-gradient(135deg,#FF6751,#FF9057)', display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0, boxShadow:'0 8px 20px rgba(255,103,81,0.30)'}}>
            {nutIcon(24)}
          </div>
          <div style={{minWidth:0}}>
            <div className="f-plex" style={{fontSize:19, fontWeight:700, color:'#0F172A', letterSpacing:'-0.01em'}}>Recambios</div>
            <div className="f-mono" style={{fontSize:11, color:'#64748B', marginTop:2}}>2.148 piezas</div>
          </div>
        </div>
      )}
      {t >= phraseAt && (
        <div className="f-plex" style={{position:'absolute', left:1330, top:496, width:360, textAlign:'center', fontSize:17, fontWeight:600, color:'#334155', lineHeight:1.4, opacity:phraE, transform:`translateY(${(1-phraE)*10}px)`, zIndex:5}}>
          Cada pieza, colgando de su coche
        </div>
      )}

      {/* ===== Sello final ===== */}
      {t >= selloAt && (
        <div style={{position:'absolute', top:786, left:0, right:0, display:'flex', justifyContent:'center', zIndex:6}}>
          <div style={{display:'flex', alignItems:'center', gap:14, opacity:clamp(selloP*1.4,0,1), transform:`scale(${lerp(1.16,1,selloE)}) rotate(${lerp(-2.5,0,selloE)}deg)`}}>
            <svg width="34" height="34" viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
              <circle cx="12" cy="12" r="9" stroke="#059669" strokeWidth="1.8"/>
              <path d="M7.8 12.3 L10.8 15.3 L16.4 9.2" stroke="#059669" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
            <span className="f-plex" style={{fontSize:34, fontWeight:700, letterSpacing:'-0.02em', color:'#0F172A', whiteSpace:'nowrap'}}>
              Compras, ventas y Verifactu: <span style={{color:'#059669', fontWeight:800}}>ya venían de serie.</span>
            </span>
          </div>
        </div>
      )}

      <SocialCaption t={t} cues={CAPTIONS.slide_flexdesguace}/>
    </SlideCanvas>
  );
}

Object.assign(window, { SlideFlexDesguace });
