// === F6 · Casos de éxito 3 de 3 — Drivioon (15s) ===
// Un ERP para desguaces CONSTRUIDO sobre Dinaup: gestiona sus licencias con Flex.
// Mismo patrón visual que F4 (Minigolf) y F5 (Desguace): píldora "CASOS DE ÉXITO"
// arriba-izquierda, título del caso centrado, card de sección grande y centrada
// cuyo interior es un FORMULARIO de dos columnas que se rellena campo a campo
// (label mono + caja de input + valor + chip de tipo), mini-cards conectadas
// a la derecha y sello final.
// Sincronía cues slide_flexdrivioon: 0.5-3.2 caso · 3.2-6.2 sección · 6.2-9.4
// campos · 9.4-12.2 mini-cards · 12.2-14.7 sello.
// NOTA: todos los helpers viven DENTRO del componente — los tres slides de casos
// se cargan como scripts babel globales y no deben pisarse funciones entre sí.

function SlideFlexDrivioon({ t }){

  // ---------- Iconos SVG de línea (sin emojis, sin placas detrás) ----------
  const iconKey = (size, color='#0085FF') => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
      <circle cx="7.2" cy="12" r="3.4" stroke={color} strokeWidth="1.8"/>
      <path d="M10.6 12 H20.6 M17.2 12 V15.4 M20.6 12 V14.6" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  );
  const iconLayers = (size, color='#0085FF') => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
      <path d="M12 2.8 L21 7.4 L12 12 L3 7.4 Z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
      <path d="M3 12.2 L12 16.8 L21 12.2" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M3 16.6 L12 21.2 L21 16.6" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" opacity="0.55"/>
    </svg>
  );
  const iconPuzzle = (size, color='#0085FF') => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
      <path d="M8.6 5 H10.8 A2.2 2.2 0 0 1 15.2 5 H19 V8.8 A2.2 2.2 0 0 0 19 13.2 V17 A2 2 0 0 1 17 19 H7 A2 2 0 0 1 5 17 V7 A2 2 0 0 1 7 5 H8.6 Z" stroke={color} strokeWidth="1.7" strokeLinejoin="round"/>
    </svg>
  );
  const iconUsers = (size, color='#0085FF') => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
      <circle cx="9" cy="7.4" r="3.6" stroke={color} strokeWidth="1.8"/>
      <path d="M3 20.4 V19 A4 4 0 0 1 7 15 H11 A4 4 0 0 1 15 19 V20.4" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
      <path d="M15.6 4.1 A3.6 3.6 0 0 1 15.6 10.7" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
      <path d="M18 15.1 A4 4 0 0 1 21 19 V20.4" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  );
  const iconGear = (size, color='#0085FF') => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}>
      <circle cx="12" cy="12" r="3.1" stroke={color} strokeWidth="1.8"/>
      {Array.from({length:8}).map((_,k)=>{
        const a = k * Math.PI / 4;
        const x1 = 12 + Math.cos(a)*6.2, y1 = 12 + Math.sin(a)*6.2;
        const x2 = 12 + Math.cos(a)*8.8, y2 = 12 + Math.sin(a)*8.8;
        return <path key={k} d={`M${x1.toFixed(2)} ${y1.toFixed(2)} L${x2.toFixed(2)} ${y2.toFixed(2)}`} stroke={color} strokeWidth="1.8" strokeLinecap="round"/>;
      })}
    </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>;
  };
  const miniIcon = (kind, size, color) => kind === 'users' ? iconUsers(size, color) : iconGear(size, color);

  // ---------- Timeline ----------
  const pillP = clamp((t-0.5)/0.5, 0, 1);
  const pillE = Easing.easeOutBack(pillP);
  const titP  = clamp((t-0.9)/0.7, 0, 1);
  const titE  = Easing.easeOutCubic(titP);
  const kickP = clamp((t-1.25)/0.6, 0, 1);
  const cardP = clamp((t-1.6)/0.8, 0, 1);
  const cardE = Easing.easeOutCubic(cardP);

  // Campos: cadencia 1.5s — cada beat: slot (label+caja vacía) → valor → chip de tipo
  const FIELDS = [
    { at:3.4, label:'Cuota mensual',        value:'29,00 €',            type:'importe',  typed:true,  x:0,   y:0,   w:282 },
    { at:4.9, label:'Nº de licencia',       value:'DRV-2026-0412',      type:'texto',    typed:true,  x:298, y:0,   w:282 },
    { at:6.4, label:'Estado',               value:'Activa',             type:'selector', typed:false, x:0,   y:96,  w:282 },
    { at:7.9, label:'Datos de facturación', value:'Talleres Ruiz S.L.', type:'enlace',   typed:false, x:0,   y:192, w:580 },
  ];
  // Mini-cards conectadas (una a una, pausado)
  const minis = [
    { at:9.6,  name:'Usuarios',        count:'486', icon:'users', y:314, srcY:408, dstY:358 },
    { at:10.7, name:'Configuraciones', count:'129', icon:'gear',  y:452, srcY:468, dstY:496 },
  ];
  const sealAt = 12.2;

  // ---------- Geometría (idéntica en F4/F5: card grande centrada) ----------
  // 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).
  const CX = 640, CY = 238, CW = 640, CH = 404;   // card centrada, 640 de ancho
  const FIELDS_TOP = 116;                          // zona de campos dentro de la card
  const F_W = 580, F_LBL = 14, F_LBLGAP = 6, F_IN = 46;  // zona, label, gap, alto de input
  const MC_X = 1402, MC_W = 268, MC_H = 88;        // mini-cards a la derecha

  // Estado vacío: visible tras entrar la card, se va justo antes del primer campo
  const emptyOp = Math.min(clamp((t-2.3)/0.5, 0, 1), clamp((3.4-t)/0.3, 0, 1));

  return (
    <SlideCanvas variant="light">

      {/* ===== Píldora de casos (estilo TimeBadge, sin hora) ===== */}
      <div style={{position:'absolute', top:44, left:60, zIndex:6,
        padding:'12px 20px', background:'rgba(15,23,42,0.92)',
        border:'1px solid rgba(255,255,255,0.10)', borderRadius:100,
        boxShadow:'0 12px 32px rgba(15,23,42,0.25)',
        opacity:clamp(pillP*1.3,0,1), transform:`translateY(${(1-pillE)*-14}px) scale(${lerp(0.85,1,pillE)})`}}>
        <span className="f-mono" style={{fontSize:11, fontWeight:700, letterSpacing:2.5, color:'rgba(255,255,255,0.92)', textTransform:'uppercase'}}>Casos de éxito · 3 de 3</span>
      </div>

      {/* ===== Título del caso + kicker ===== */}
      <div style={{position:'absolute', top:50, left:0, right:0, textAlign:'center', opacity:titE, transform:`translateY(${(1-titE)*14}px)`}}>
        <div style={{display:'flex', alignItems:'center', justifyContent:'center', gap:14}}>
          {iconLayers(36)}
          <span className="f-plex" style={{fontSize:44, fontWeight:700, letterSpacing:'-0.03em', color:'#0F172A', lineHeight:1}}>Drivioon</span>
        </div>
      </div>
      <div style={{position:'absolute', top:114, left:0, right:0, textAlign:'center', opacity:kickP, transform:`translateY(${(1-Easing.easeOutCubic(kickP))*8}px)`}}>
        <span className="f-plex" style={{fontSize:17, fontWeight:500, color:'#64748B', letterSpacing:'-0.01em'}}>Un ERP que corre sobre Dinaup</span>
      </div>

      {/* ===== Card de sección grande y centrada: "Licencias" ===== */}
      <div style={{position:'absolute', left:CX, top:CY, width:CW, height:CH,
        background:'#fff', border:'1px solid #E2E8F0', borderRadius:20,
        boxShadow:'0 30px 80px rgba(0,133,255,0.15), 0 4px 12px rgba(15,23,42,0.06)',
        opacity:clamp(cardP*1.2,0,1), transform:`translateY(${(1-cardE)*26}px)`}}>

        {/* Cabecera de la card */}
        <div style={{position:'absolute', left:28, top:24, right:28, height:60, display:'flex', alignItems:'center', gap:16}}>
          {iconKey(34)}
          <div style={{minWidth:0}}>
            <div className="f-plex" style={{fontSize:25, fontWeight:700, letterSpacing:'-0.02em', color:'#0F172A', lineHeight:1.1}}>Licencias</div>
            <div className="f-mono" style={{fontSize:10, fontWeight:600, letterSpacing:2, color:'#94A3B8', textTransform:'uppercase', marginTop:4}}>Nueva sección</div>
          </div>
          <div style={{flex:1}}/>
          <div style={{display:'inline-flex', alignItems:'center', gap:7, padding:'6px 13px', background:'rgba(0,133,255,0.08)', border:'1px solid rgba(0,133,255,0.25)', borderRadius:100}}>
            {iconPuzzle(14)}
            <span className="f-plex" style={{fontSize:12.5, fontWeight:700, color:'#0085FF'}}>Flex</span>
          </div>
        </div>
        <div style={{position:'absolute', left:28, right:28, top:100, height:1, background:'#EEF2F7'}}/>

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

          {/* Estado vacío antes del primer campo */}
          {emptyOp > 0 && (
            <div style={{position:'absolute', inset:0, border:'1.5px dashed #D3DDE8', borderRadius:12,
              display:'flex', alignItems:'center', justifyContent:'center', opacity:emptyOp, 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>

      {/* ===== Conectores hacia las mini-cards ===== */}
      <svg style={{position:'absolute', inset:0, pointerEvents:'none'}} width="1920" height="1080" viewBox="0 0 1920 1080" fill="none">
        {minis.map((m,i)=>{
          if (t < m.at) return null;
          const draw = Easing.easeInOutCubic(clamp((t-m.at)/0.7, 0, 1));
          const dotP = clamp((t-(m.at+0.55))/0.25, 0, 1);
          return (
            <g key={i}>
              <path d={`M 1282 ${m.srcY} C 1332 ${m.srcY}, 1352 ${m.dstY}, ${MC_X} ${m.dstY}`}
                stroke="rgba(0,133,255,0.55)" strokeWidth="2" pathLength="1"
                strokeDasharray="1" strokeDashoffset={1-draw}/>
              <circle cx="1282" cy={m.srcY} r="4" fill="#0085FF" opacity={clamp(draw*3,0,1)}/>
              <circle cx={MC_X} cy={m.dstY} r="4" fill="#0085FF" opacity={dotP}/>
            </g>
          );
        })}
      </svg>

      {/* ===== Mini-cards: "Usuarios" y "Configuraciones" ===== */}
      {minis.map((m,i)=>{
        if (t < m.at + 0.35) return null;
        const p = clamp((t-(m.at+0.35))/0.6, 0, 1);
        const e = Easing.easeOutCubic(p);
        return (
          <div key={i} style={{position:'absolute', left:MC_X, top:m.y, width:MC_W, height:MC_H,
            background:'#fff', border:'1px solid #E2E8F0', borderRadius:14,
            boxShadow:'0 14px 36px rgba(0,133,255,0.10), 0 3px 8px rgba(15,23,42,0.05)',
            display:'flex', alignItems:'center', gap:14, padding:'0 18px',
            opacity:p, transform:`translateX(${(1-e)*-14}px)`}}>
            {miniIcon(m.icon, 26, '#0085FF')}
            <div style={{minWidth:0}}>
              <div className="f-plex" style={{fontSize:16.5, fontWeight:700, color:'#0F172A', lineHeight:1.15}}>{m.name}</div>
              <div className="f-mono" style={{fontSize:10, fontWeight:500, letterSpacing:0.5, color:'#94A3B8', marginTop:3}}>Enlace a Licencias</div>
            </div>
            <div style={{flex:1}}/>
            <span className="f-mono" style={{fontSize:16, fontWeight:700, color:'#0085FF'}}>{m.count}</span>
          </div>
        );
      })}

      {/* ===== Sello final ===== */}
      {t >= sealAt && (()=>{
        const p = clamp((t-sealAt)/0.7, 0, 1);
        const e = Easing.easeOutCubic(p);
        const hlE = Easing.easeOutCubic(clamp((t-(sealAt+0.45))/0.6, 0, 1));
        return (
          <div style={{position:'absolute', top:716, left:0, right:0, textAlign:'center',
            opacity:e, transform:`translateY(${(1-e)*16}px) scale(${lerp(1.04,1,e)})`}}>
            <div className="f-plex" style={{fontSize:38, fontWeight:700, letterSpacing:'-0.025em', lineHeight:1.15}}>
              <span style={{color:'#0F172A'}}>Un producto entero, </span>
              <span className="grad-text">sobre el lienzo.</span>
            </div>
            <div style={{margin:'22px auto 0', width:272, height:3, borderRadius:100, overflow:'hidden',
              display:'flex', transform:`scaleX(${hlE})`}}>
              <div style={{flex:1, background:'#00C1FF'}}/>
              <div style={{flex:1, background:'#FF6751'}}/>
              <div style={{flex:1, background:'#56E16A'}}/>
              <div style={{flex:1, background:'#FFDC18'}}/>
            </div>
          </div>
        );
      })()}

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

Object.assign(window, { SlideFlexDrivioon });
