// === F3 · Flex — El lienzo (18s) — el DESPUÉS: de serie + tu backoffice ===
// Ventana Dinaup con la nav premium "doble piso sobre hairline" (np3 de
// Identidad Corporativa, replicada inline simplificada): piso 1 logo+buscar+avatar,
// piso 2 enlaces + botón destacado Flex, hairline 2px con los 4 colores.
// Arco: dashboard Ready To Go → ola de respiración → click en Flex →
// lienzo punteado vacío → "+ Añadir sección" → 3 secciones construyéndose → claim.

function SlideFlexLienzo({ t }){
  // ---------- Geometría ----------
  // Ventana: top 175 · left/right 280 → 1360 × 850 (base y=1025).
  // Chrome 76 + piso1 52 + piso2 46 + hairline 2 = 176 → contenido abs y=351, alto 674.
  const flexX = 1572, flexY = 326;   // centro del pill Flex (piso 2, derecha)
  const btnX  = 960,  btnY  = 680;   // centro de «+ Añadir sección»

  // ---------- Timeline (sincronizada con cues slide_flexlienzo) ----------
  const clickFlexAt = 8.4;                  // cue 6.2-9.0 · "Y arriba, un botón: Flex"
  const fadeAt      = 9.0;                  // cue 9.0-12.0 · el lienzo
  const clickAddAt  = 12.5;                 // cue 12.0-15.0 · añadir secciones
  const builds      = [12.8, 13.45, 14.1];  // esqueleto→card, cadencia pausada
  const claimAt     = 15.3;                 // cue 15.0-17.7 · reposo desde 16.0

  const winP  = clamp((t-0.5)/0.7, 0, 1);
  const winE  = Easing.easeOutCubic(winP);
  const hairE = Easing.easeInOutCubic(clamp((t-1.4)/0.8, 0, 1));

  // Pill Flex: entrada → hover (llega el cursor) → pressed → activo (sólido)
  const pillP    = clamp((t-1.7)/0.5, 0, 1);
  const pillE    = Easing.easeOutBack(pillP);
  const hovering = t >= 7.75 && t < clickFlexAt;
  const pressing = t >= clickFlexAt && t < clickFlexAt + 0.18;
  const flexOn   = t >= clickFlexAt + 0.18;

  // Dashboard → lienzo
  const dashFadeE = Easing.easeInOutQuad(clamp((t-fadeAt)/0.8, 0, 1));
  const dashOp    = 1 - dashFadeE;
  const gridE  = Easing.easeOutCubic(clamp((t-9.4)/0.8, 0, 1));
  const msgEo  = Easing.easeOutCubic(clamp((t-10.1)/0.8, 0, 1));
  const btnEo  = Easing.easeOutBack(clamp((t-10.7)/0.6, 0, 1));
  const shiftP = clamp((t-(clickAddAt+0.1))/0.7, 0, 1);
  const shiftE = Easing.easeInOutCubic(shiftP);
  const addPressing = t >= clickAddAt && t < clickAddAt + 0.2;

  const links = [
    { l:'Inicio',    k:'home' },
    { l:'Ventas',    k:'receipt' },
    { l:'Compras',   k:'cart' },
    { l:'Productos', k:'box' },
    { l:'Análisis',  k:'chart' },
  ];
  const cards = [
    { n:'Ventas',    k:'receipt', d:'96 tickets hoy' },
    { n:'Compras',   k:'cart',    d:'8 pedidos en curso' },
    { n:'Productos', k:'box',     d:'342 referencias' },
    { n:'Clientes',  k:'people',  d:'1.284 clientes' },
    { n:'Tesorería', k:'bank',    d:'2 cuentas al día' },
    { n:'Análisis',  k:'chart',   d:'14 informes' },
  ];
  const nuevas = [
    { n:'Flotas', k:'truck' },
    { n:'Obras',  k:'helmet' },
    { n:'Citas',  k:'calendar' },
  ];

  // Iconos SVG de línea (Material Symbols no está cargado en el deck)
  const ic = (kind, size, color) => {
    const s = { stroke:color, strokeWidth:1.8, strokeLinecap:'round', strokeLinejoin:'round', fill:'none' };
    const g = {
      home:     <><path d="M4.5 11.5 L12 4.5 L19.5 11.5" {...s}/><path d="M6.8 9.8 V19.5 H17.2 V9.8" {...s}/></>,
      receipt:  <><path d="M6.5 3.5 H17.5 V20.5 L15.6 19 L13.8 20.5 L12 19 L10.2 20.5 L8.4 19 L6.5 20.5 Z" {...s}/><path d="M9.5 8.2 H14.5 M9.5 11.8 H13" {...s}/></>,
      cart:     <><path d="M3.5 4.5 H6.2 L8.6 15 H17.8 L20.2 7.5 H7.2" {...s}/><circle cx="9.9" cy="18.6" r="1.6" {...s}/><circle cx="16.4" cy="18.6" r="1.6" {...s}/></>,
      box:      <><path d="M12 3.5 L20 7.5 V16.5 L12 20.5 L4 16.5 V7.5 Z" {...s}/><path d="M4 7.5 L12 11.5 L20 7.5 M12 11.5 V20.5" {...s}/></>,
      people:   <><circle cx="9" cy="8.6" r="3.1" {...s}/><path d="M3.5 19 A5.5 5.5 0 0 1 14.5 19" {...s}/><circle cx="16.6" cy="9.6" r="2.4" {...s}/><path d="M16.2 14.6 A4.4 4.4 0 0 1 20.5 19" {...s}/></>,
      bank:     <><path d="M3.5 9.2 L12 3.8 L20.5 9.2 Z" {...s}/><path d="M6 12 V16.8 M10 12 V16.8 M14 12 V16.8 M18 12 V16.8" {...s}/><path d="M3.5 20.2 H20.5" {...s}/></>,
      chart:    <><path d="M3.5 20.5 H20.5" {...s}/><path d="M7.5 20.5 V12.5 M12 20.5 V6.5 M16.5 20.5 V15.5" {...s} strokeWidth={2.4}/></>,
      puzzle:   <path d="M6 6 H9.6 A2.4 2.4 0 0 1 14.4 6 H18 V9.6 A2.4 2.4 0 0 1 18 14.4 V18 H6 Z" {...s}/>,
      truck:    <><path d="M2.8 7.5 H13.5 V16 H2.8 Z" {...s}/><path d="M13.5 10.2 H17.4 L20.6 13.4 V16 H13.5" {...s}/><circle cx="7" cy="17.6" r="1.8" {...s}/><circle cx="16.6" cy="17.6" r="1.8" {...s}/></>,
      helmet:   <><path d="M5 15 A7 7 0 0 1 19 15" {...s}/><path d="M3.2 15 H20.8" {...s}/><path d="M9.8 9 V7.2 A2.2 2.2 0 0 1 14.2 7.2 V9" {...s}/></>,
      calendar: <><rect x="4" y="5.5" width="16" height="14.5" rx="2" {...s}/><path d="M4 10 H20 M8.3 3.5 V7 M15.7 3.5 V7" {...s}/><circle cx="12" cy="14.8" r="1.3" fill={color} stroke="none"/></>,
      search:   <><circle cx="10.8" cy="10.8" r="5.6" {...s}/><path d="M15.1 15.1 L19.6 19.6" {...s}/></>,
      plus:     <path d="M12 5.5 V18.5 M5.5 12 H18.5" {...s}/>,
    };
    return <svg width={size} height={size} viewBox="0 0 24 24" style={{display:'block', flexShrink:0}}>{g[kind]}</svg>;
  };

  // Cursor: sube al pill Flex (llega 7.8, click 8.4) → baja a «+ Añadir sección»
  // (llega 12.05, click 12.5) → aparca abajo-derecha para no tapar las cards.
  const cursorFrames = [
    { t:6.4,  x:1610,  y:1005, arc:70 },
    { t:7.8,  x:flexX, y:flexY },
    { t:10.9, x:flexX, y:flexY, arc:46 },
    { t:12.05,x:btnX,  y:btnY },
    { t:12.9, x:btnX,  y:btnY },
    { t:13.8, x:1565,  y:965 },
  ];

  return (
    <SlideCanvas variant="light">
      <DemoHeader t={t} eyebrow="DINAUP FLEX · EL LIENZO" title={<>De serie, todo. <span className="grad-text">Lo tuyo, en un lienzo.</span></>}/>

      {/* ===== Ventana Dinaup ===== */}
      <div style={{position:'absolute',top:175,left:280,right:280,bottom:55,background:'#fff',borderRadius:12,boxShadow:'0 30px 80px rgba(0,133,255,0.15)',overflow:'hidden',border:'1px solid #E2E8F0',opacity:winE,transform:`translateY(${(1-winE)*22}px)`}}>
        <BrowserChrome url="play.dinaup.com/App/Inicio" title="Dinaup — Tu empresa"/>

        {/* ---- Nav doble piso sobre hairline (np3 simplificada) ---- */}
        {/* Piso 1: logo · buscador pill · avatar */}
        <div style={{height:52,display:'flex',alignItems:'center',padding:'0 26px',background:'#fff'}}>
          <div style={{opacity:clamp((t-0.9)/0.5,0,1)}}><DinaupLogo size={22}/></div>
          <div style={{marginLeft:'auto',display:'flex',alignItems:'center',gap:10}}>
            <div style={{opacity:clamp((t-1.05)/0.5,0,1),display:'flex',alignItems:'center',gap:8,height:34,padding:'0 9px 0 13px',background:'#F4F7FA',border:'1px solid #E6ECF3',borderRadius:100}}>
              {ic('search',15,'#64748B')}
              <span style={{fontSize:12.5,color:'#64748B',fontWeight:500}}>Buscar</span>
              <span className="f-mono" style={{fontSize:10,fontWeight:600,color:'#8B99A8',background:'#fff',border:'1px solid #D3DDE8',borderRadius:5,padding:'3px 6px'}}>Ctrl K</span>
            </div>
            <div style={{width:1,height:22,background:'#E6ECF3',opacity:clamp((t-1.15)/0.4,0,1)}}/>
            <div className="f-plex" style={{opacity:clamp((t-1.2)/0.5,0,1),width:32,height:32,borderRadius:100,background:'rgba(0,133,255,0.10)',border:'1px solid rgba(0,133,255,0.22)',color:'#0066CC',display:'flex',alignItems:'center',justifyContent:'center',fontSize:11,fontWeight:700}}>MJ</div>
          </div>
        </div>

        {/* Piso 2: enlaces + botón destacado Flex */}
        <div style={{height:46,display:'flex',alignItems:'stretch',padding:'0 16px',borderTop:'1px solid #EEF2F7',background:'#fff'}}>
          {links.map((ln,i)=>{
            const lp = clamp((t-(1.25+i*0.08))/0.4, 0, 1);
            const isActive = i === 0 && flexOn === false;
            return (
              <div key={ln.l} className="f-plex" style={{display:'inline-flex',alignItems:'center',gap:7,padding:'0 13px',fontSize:13.5,fontWeight:isActive?600:500,color:isActive?'#0085FF':'#64748B',opacity:lp}}>
                {ic(ln.k, 15, isActive ? '#0085FF' : '#8B99A8')}
                {ln.l}
              </div>
            );
          })}
          <div style={{marginLeft:'auto',alignSelf:'center',paddingRight:10}}>
            <div className="f-plex" style={{
              display:'inline-flex',alignItems:'center',gap:7,height:32,padding:'0 15px',borderRadius:100,
              background: flexOn ? 'var(--dn-primary)' : hovering ? 'rgba(0,133,255,0.16)' : 'rgba(0,133,255,0.09)',
              border:`1px solid ${flexOn ? 'var(--dn-primary)' : 'rgba(0,133,255,0.38)'}`,
              color: flexOn ? '#fff' : '#0085FF',
              boxShadow: flexOn ? '0 6px 18px rgba(0,133,255,0.35)' : 'none',
              fontSize:13.5,fontWeight:700,
              opacity:clamp(pillP*1.3,0,1),
              transform:`scale(${lerp(0.6,1,pillE) * (pressing ? 0.93 : hovering ? 1.05 : 1)})`}}>
              {ic('puzzle', 15, flexOn ? '#fff' : '#0085FF')}
              Flex
            </div>
          </div>
        </div>

        {/* Hairline: los 4 colores corporativos, tramos sólidos de 2px */}
        <div style={{height:2,display:'flex',transform:`scaleX(${hairE})`,transformOrigin:'left center'}}>
          {['#00C1FF','#FF6751','#56E16A','#FFDC18'].map(c => <div key={c} style={{flex:1,background:c}}/>)}
        </div>

        {/* ---- Contenido bajo la nav ---- */}
        <div style={{position:'relative',height:'calc(100% - 176px)',background:'#F8FAFC',overflow:'hidden'}}>

          {/* === Fase 1: dashboard Ready To Go === */}
          {dashOp > 0.001 && (
            <div style={{position:'absolute',inset:0,padding:'32px 46px',opacity:dashOp,transform:`translateY(${-10*dashFadeE}px) scale(${1-0.015*dashFadeE})`}}>
              <div style={{display:'flex',alignItems:'flex-end',justifyContent:'space-between',opacity:clamp((t-1.25)/0.6,0,1)}}>
                <div>
                  <div style={{display:'flex',alignItems:'center',gap:12}}>
                    <div className="f-plex" style={{fontSize:21,fontWeight:700,color:'#0F172A',letterSpacing:'-0.02em'}}>Inicio</div>
                    {/* Etiqueta comercial de las configuraciones de serie */}
                    {(()=>{
                      const p = clamp((t-3.4)/0.5, 0, 1);
                      const e = Easing.easeOutBack(p);
                      return (
                        <span className="f-mono" style={{display:'inline-flex',alignItems:'center',gap:7,height:26,padding:'0 12px',borderRadius:100,
                          background:'linear-gradient(135deg, rgba(0,133,255,0.10), rgba(86,225,106,0.10))',
                          border:'1px solid rgba(0,133,255,0.35)',color:'#0066CC',
                          fontSize:10.5,fontWeight:700,letterSpacing:'1.8px',whiteSpace:'nowrap',
                          opacity:clamp(p*1.3,0,1),transform:`scale(${lerp(0.6,1,e)})`}}>
                          <svg width="11" height="11" viewBox="0 0 24 24" fill="none" style={{flexShrink:0}}><path d="M13 2 L4.8 13.4 H11 L9.6 22 L19.2 9.8 H12.6 Z" stroke="#0066CC" strokeWidth="1.8" strokeLinejoin="round"/></svg>
                          READY TO GO
                        </span>
                      );
                    })()}
                  </div>
                  <div style={{fontSize:12,color:'#64748B',marginTop:3}}>Tu empresa, de un vistazo</div>
                </div>
              </div>
              <div style={{display:'grid',gridTemplateColumns:'repeat(3,1fr)',gap:22,marginTop:26}}>
                {cards.map((c,i)=>{
                  // Entrada escalonada 1.6→3.3 · ola de respiración 3.4→6.2 (una a una)
                  const e   = Easing.easeOutCubic(clamp((t-(1.6+i*0.24))/0.5, 0, 1));
                  const amp = Math.sin(clamp((t-(3.4+i*0.42))/0.8, 0, 1) * Math.PI);
                  return (
                    <div key={c.n} style={{height:190,background:'#fff',borderRadius:14,padding:'26px 26px',
                      border:`1px solid ${amp > 0.03 ? `rgba(0,133,255,${0.15+0.35*amp})` : '#E2E8F0'}`,
                      boxShadow:`0 ${8+14*amp}px ${24+18*amp}px rgba(0,133,255,${0.06+0.12*amp}), 0 2px 8px rgba(15,23,42,0.05)`,
                      opacity:e,transform:`translateY(${(1-e)*18 - 6*amp}px) scale(${1+0.02*amp})`}}>
                      {ic(c.k, 32, '#0085FF')}
                      <div className="f-plex" style={{fontSize:18,fontWeight:700,color:'#0F172A',marginTop:18,letterSpacing:'-0.01em'}}>{c.n}</div>
                      <div className="f-mono" style={{fontSize:12,color:'#64748B',marginTop:7}}>{c.d}</div>
                    </div>
                  );
                })}
              </div>
            </div>
          )}

          {/* === Fase 2: el lienzo === */}
          {t >= 9.3 && (
            <div style={{position:'absolute',inset:0}}>
              <div className="dot-grid-bg" style={{position:'absolute',inset:0,opacity:0.55*gridE}}/>

              {/* Mensaje central (tras el click sube y se hace pequeño) */}
              <div style={{position:'absolute',left:0,right:0,top:224-165*shiftE,textAlign:'center',opacity:msgEo*(1-0.3*shiftE),transform:`translateY(${(1-msgEo)*16}px) scale(${1-0.38*shiftE})`,transformOrigin:'center top'}}>
                <span className="f-plex" style={{fontSize:42,fontWeight:700,letterSpacing:'-0.03em',color:'#0F172A'}}>Tu lienzo. <span className="grad-text">Sin código.</span></span>
              </div>

              {/* Botón + Añadir sección (se consume con el click) */}
              {t < 13.4 && (
                <div style={{position:'absolute',left:0,right:0,top:305-165*shiftE,textAlign:'center',opacity:btnEo*(1-clamp(shiftP*1.6,0,1))}}>
                  <span className="f-plex" style={{display:'inline-flex',alignItems:'center',gap:9,height:48,padding:'0 24px',borderRadius:12,background:addPressing?'#0066CC':'var(--dn-primary)',color:'#fff',fontSize:15.5,fontWeight:700,boxShadow:'0 10px 28px rgba(0,133,255,0.35)',transform:`scale(${lerp(0.7,1,btnEo)*(addPressing?0.94:1)})`}}>
                    {ic('plus',17,'#fff')}
                    Añadir sección
                  </span>
                </div>
              )}

              {/* Tres secciones nuevas: esqueleto → card */}
              {nuevas.map((c,i)=>{
                const b = builds[i];
                if (t < b) return null;
                const skE = Easing.easeOutCubic(clamp((t-b)/0.45, 0, 1));
                const fiP = clamp((t-b-0.55)/0.35, 0, 1);
                const fiE = Easing.easeOutBack(fiP);
                return (
                  <div key={c.n} style={{position:'absolute',left:204+i*326,top:209,width:300,height:172,borderRadius:14,padding:'24px 26px',
                    background: fiP > 0 ? '#fff' : '#FBFDFF',
                    border: fiP > 0.4 ? '1px solid #E2E8F0' : '1.5px dashed rgba(0,133,255,0.35)',
                    boxShadow:`0 ${18*fiP}px ${44*fiP}px rgba(0,133,255,${0.12*fiP})`,
                    opacity:skE,transform:`translateY(${(1-skE)*18}px)`}}>
                    {fiP < 1 && (
                      <div style={{position:'absolute',inset:0,padding:'24px 26px',opacity:1-fiP}}>
                        <div style={{width:30,height:30,borderRadius:9,background:'#E8EDF4'}}/>
                        <div style={{width:'62%',height:12,borderRadius:6,background:'#E8EDF4',marginTop:20}}/>
                        <div style={{width:'40%',height:9,borderRadius:5,background:'#EEF2F7',marginTop:10}}/>
                      </div>
                    )}
                    {fiP > 0 && (
                      <div style={{position:'relative',opacity:fiP,transform:`scale(${lerp(0.88,1,fiE)})`,transformOrigin:'left top'}}>
                        {ic(c.k, 30, '#0085FF')}
                        <div className="f-plex" style={{fontSize:18,fontWeight:700,color:'#0F172A',marginTop:16}}>{c.n}</div>
                        <div className="f-mono" style={{fontSize:10.5,color:'#94A3B8',marginTop:6,letterSpacing:0.5,textTransform:'uppercase'}}>Nueva sección</div>
                      </div>
                    )}
                  </div>
                );
              })}

              {/* Claim final */}
              {t >= claimAt && (()=>{
                const e = Easing.easeOutCubic(clamp((t-claimAt)/0.7, 0, 1));
                return (
                  <div style={{position:'absolute',left:0,right:0,top:449,textAlign:'center',opacity:e,transform:`translateY(${(1-e)*14}px)`}}>
                    <span className="f-plex" style={{fontSize:30,fontWeight:700,letterSpacing:'-0.02em',color:'#0F172A'}}>Cualquier sección. <span className="grad-text">Cualquier negocio.</span></span>
                  </div>
                );
              })()}
            </div>
          )}
        </div>
      </div>

      {/* Pulso de atención sobre el pill Flex (cue "Y arriba, un botón: Flex") */}
      {[6.6, 7.3].map((k,i)=>{
        if (t < k || t > k + 0.75) return null;
        const p = clamp((t-k)/0.75, 0, 1);
        return <div key={i} style={{position:'absolute',left:flexX-52,top:flexY-21,width:104,height:42,borderRadius:100,border:'2px solid rgba(0,133,255,0.6)',opacity:(1-p)*0.8,transform:`scale(${1+0.4*Easing.easeOutCubic(p)})`,pointerEvents:'none',zIndex:6}}/>;
      })}

      {/* Cursor */}
      {t >= 6.4 && <ScriptedCursor frames={cursorFrames} t={t} clickTimes={[clickFlexAt, clickAddAt]}/>}

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

Object.assign(window, { SlideFlexLienzo });
