// vc1c2.jsx — Válvulas de Control · M1 · Clase 1.2 «Válvulas de vástago deslizante: términos y partes»
// Después de valve-lib.jsx, pf-valves.jsx y vc-art.jsx (dibujos en corte). Exporta SCENES_VC1C2.

// Panel con título y descripción, y un dibujo dentro (children)
function PanelV({ x, y, w = 400, h = 520, at, title, sub, accent = SL.sun, children }) {
  const t = useTime();
  const p = popS(t, at);
  return (
    <g opacity={p.op} transform={`translate(0 ${p.ty})`}>
      <rect x={x} y={y} width={w} height={h} rx="18" fill={SL.paper} stroke={SL.lineS} strokeWidth="2" />
      <rect x={x} y={y} width={w} height="6" rx="3" fill={accent} />
      {children}
      <text x={x + w / 2} y={y + h - 96} fill={SL.ink} fontFamily={DISP} fontSize="28" fontWeight="700" textAnchor="middle">{title}</text>
      <foreignObject x={x + 20} y={y + h - 80} width={w - 40} height="74">
        <div style={{ fontFamily: DISP, fontSize: 18, color: SL.mut, textAlign: 'center', lineHeight: 1.35 }}>{sub}</div>
      </foreignObject>
    </g>
  );
}

// ── Estilos de cuerpo ──────────────────────────────────────────────────────
function S_BodyStyles({ start, dur }) {
  const t = useTime(); const s = start;
  const lt = Math.max(0, t - s);
  const u = 0.5 + 0.4 * Math.sin(lt * 0.9);
  const W = 400, gap = 40, X = (i) => 140 + i * (W + gap), Y = 280;
  const clip = (i) => <clipPath key={i} id={`vcBS${i}`}><rect x={X(i) + 8} y={Y + 12} width={W - 16} height={400} rx="12" /></clipPath>;
  // tiempos de la voz: asiento simple 0,6 · balanceada 6,2 · ángulo 14 · tres vías 20,5
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Estilos de cuerpo" title={<>Cada cuerpo guía el fluido de otra manera.</>} y="15%" size={46} />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
        <ValveDefs />
        {[0, 1, 2, 3].map(clip)}
        <PanelV x={X(0)} y={Y} at={s + 1.2} title="Globo, asiento simple" sub="Cierre hermético; el obturador se guía por arriba o por el puerto.">
          <g clipPath="url(#vcBS0)"><g transform={`translate(${X(0) + 200} ${Y + 300}) scale(0.46)`}>
            <GlobeAssembly open={u} air={u} t={t} trim="simple" show={vcOnly(['body', 'seat', 'plug', 'stem', 'bonnet'])} />
          </g></g>
        </PanelV>
        <PanelV x={X(1)} y={Y} at={s + 6.2} title="Globo balanceado" sub="Obturador con orificios de balance dentro de una jaula: menos fuerza en ΔP altas.">
          <g clipPath="url(#vcBS1)"><g transform={`translate(${X(1) + 200} ${Y + 300}) scale(0.46)`}>
            <GlobeAssembly open={u} air={u} t={t} show={vcOnly(['body', 'seat', 'cage', 'plug', 'stem', 'bonnet'])} />
          </g></g>
        </PanelV>
        <PanelV x={X(2)} y={Y} at={s + 14} title="Ángulo" sub="Entra por un lado y sale hacia abajo: fluidos erosivos, con sólidos o con flasheo.">
          <g clipPath="url(#vcBS2)"><g transform={`translate(${X(2) + 235} ${Y + 190}) scale(0.48)`}><AngleBodyArt open={u} t={t} /></g></g>
        </PanelV>
        <PanelV x={X(3)} y={Y} at={s + 20.5} title="Tres vías" sub="Mezcla dos corrientes o divide una en dos. Típica en control de temperatura.">
          <g clipPath="url(#vcBS3)"><g transform={`translate(${X(3) + 200} ${Y + 215}) scale(0.5)`}><ThreeWayArt pos={u} t={t} /></g></g>
        </PanelV>
      </svg>
    </SceneS>
  );
}

// ── Bonetes ────────────────────────────────────────────────────────────────
function S_Bonnets({ start, dur }) {
  const t = useTime(); const s = start;
  const lt = Math.max(0, t - s);
  const travel = 0.5 + 0.5 * Math.sin(lt * 1.2);
  // tiempos de la voz: estándar 6,1 · extendido 9,1 · fuelle 14,2
  const B = [
    [420, 'std', 6.1, 'Estándar', 'Servicios de temperatura moderada.', 'proceso normal'],
    [960, 'ext', 9.1, 'Extendido', 'Aleja la empaquetadura del frío o del calor extremos.', 'criogénico o muy caliente'],
    [1500, 'bellows', 14.2, 'Con sello de fuelle', 'Un fuelle metálico sella el vástago: cero fugas al ambiente.', 'tóxico o regulado'],
  ];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Bonetes" title={<>El bonete aleja o protege la empaquetadura del proceso.</>} y="15%" size={46} />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
        <ValveDefs />
        {B.map(([x, type, at, title, sub, temp]) => {
          const p = popS(t, s + at);
          return (
            <g key={type} opacity={p.op} transform={`translate(0 ${p.ty})`}>
              <rect x={x - 230} y={250} width="460" height="680" rx="18" fill={SL.paper} stroke={SL.lineS} />
              <g transform={`translate(${x} 720) scale(0.78)`}><BonnetArt type={type} travel={travel} /></g>
              {type === 'bellows' && <text x={x + 40} y={580} fill={SL.cc} fontFamily={MONO} fontSize="18" fontWeight="700">fuelle</text>}
              <text x={x} y={856} fill={SL.ink} fontFamily={DISP} fontSize="28" fontWeight="700" textAnchor="middle">{title}</text>
              <foreignObject x={x - 210} y={868} width="420" height="56"><div style={{ fontFamily: DISP, fontSize: 18, color: SL.mut, textAlign: 'center', lineHeight: 1.3 }}>{sub}</div></foreignObject>
              <text x={x - 205} y={284} fill={SL.cc} fontFamily={MONO} fontSize="17">{temp}</text>
            </g>
          );
        })}
      </svg>
    </SceneS>
  );
}

// ── Empaquetadura ──────────────────────────────────────────────────────────
function S_Packing({ start, dur }) {
  const t = useTime(); const s = start;
  const lt = Math.max(0, t - s);
  // tiempos de la voz: PTFE 5,1 · grafito 10,5 · carga viva 14,3 · emisiones 21,3
  const type = lt < 10.5 ? 'ptfe' : lt < 14.3 ? 'graphite' : 'live';
  const wear = lt > 14.3 ? 0.5 + 0.5 * Math.sin((lt - 14.3) * 0.8) : 0;
  const label = { ptfe: 'Anillos en V de PTFE', graphite: 'Anillos de grafito', live: 'Carga viva: resortes Belleville' }[type];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Empaquetadura" title={<>Sellar el vástago <span style={{ color: SL.sun }}>sin frenarlo</span>: el gran compromiso.</>} y="15%" size={46} />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
        <ValveDefs />
        <g opacity={segS(t, s + 1)}>
          <rect x={250} y={260} width="700" height="660" rx="18" fill={SL.paper} stroke={SL.lineS} />
          <g transform="translate(600 610) scale(1.05)"><PackingArt type={type} t={t} wear={wear} /></g>
          <text x={600} y={900} fill={SL.sunD} fontFamily={DISP} fontSize="26" fontWeight="700" textAnchor="middle">{label}</text>
          {lt > 21.3 && <g><rect x={290} y={290} width="250" height="46" rx="23" fill="rgba(43,138,62,0.12)" stroke={SL.ok} strokeWidth="2" /><text x={415} y={320} fill={SL.ok} fontFamily={MONO} fontSize="18" fontWeight="700" textAnchor="middle">emisiones ≤ 100 ppm</text></g>}
        </g>
      </svg>
      <ListS x={1050} y={300} w={780} size={28} items={[
        { k: 'Anillos en V de PTFE', d: 'Baja fricción y buen sello. Hasta unos 230 °C.', at: s + 5.1 },
        { k: 'Grafito', d: 'Para alta temperatura y resistencia al fuego, con más fricción.', at: s + 10.5 },
        { k: 'Carga viva', d: 'Resortes que mantienen la compresión mientras la empaquetadura se desgasta.', at: s + 14.3 },
        { k: 'Emisiones fugitivas', d: 'Diseños certificados limitan las fugas de compuestos volátiles al ambiente.', at: s + 21.3 },
      ]} />
    </SceneS>
  );
}

// ── Acción del cuerpo y del actuador ───────────────────────────────────────
function S_Action({ start, dur }) {
  const t = useTime(); const s = start;
  const lt = Math.max(0, t - s);
  // con aire regulando; el aire se corta a los 19 s y mandan los resortes
  const cutAt = 19, k = clamp((lt - cutAt) / 1.5, 0, 1), air = lt < cutAt;
  const mod = 0.5 + 0.2 * Math.sin(lt * 1.1);
  const rows = [
    { act: 'ATC', k: 'Directo + empujar para cerrar', fail: 'falla abierta (FO)', open: mod + (1 - mod) * k, airv: (1 - mod) * (1 - k) },
    { act: 'ATO', k: 'Inverso + empujar para cerrar', fail: 'falla cerrada (FC)', open: mod * (1 - k), airv: mod * (1 - k) },
  ];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Acción del actuador y del cuerpo" title={<>La combinación decide <span style={{ color: SL.sun }}>qué hace la válvula sin aire</span>.</>} y="15%" size={46} />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
        <ValveDefs />
        {rows.map((r, i) => {
          const x = 420 + i * 560;
          const p = popS(t, s + (i === 0 ? 6.3 : 10));
          const st = r.open > 0.97 ? 'ABIERTA' : r.open < 0.03 ? 'CERRADA' : 'regulando';
          return (
            <g key={i} opacity={p.op}>
              <rect x={x - 240} y={260} width="480" height="660" rx="18" fill={SL.paper} stroke={SL.lineS} />
              <g transform={`translate(${x} 735) scale(0.4)`}><GlobeAssembly open={r.open} air={r.airv} act={r.act} t={t} /></g>
              <text x={x} y={872} fill={SL.ink} fontFamily={DISP} fontSize="25" fontWeight="700" textAnchor="middle">{r.k}</text>
              <text x={x} y={904} fill={SL.cc} fontFamily={MONO} fontSize="19" textAnchor="middle">{r.fail} · {st}</text>
            </g>
          );
        })}
        <g opacity={segS(t, s + 1.5)}>
          <rect x={1300} y={340} width="460" height="70" rx="35" fill={air ? 'rgba(232,89,12,0.12)' : 'rgba(201,42,42,0.12)'} stroke={air ? SL.cc : SL.bad} strokeWidth="2" />
          <text x={1530} y={384} fill={air ? SL.cc : SL.bad} fontFamily={DISP} fontSize="26" fontWeight="700" textAnchor="middle">{air ? 'Con aire en el actuador' : 'Falla de aire: manda el resorte'}</text>
        </g>
      </svg>
      <ListS x={1300} y={460} w={500} size={24} items={[
        { k: 'Actuador directo', d: 'El aire entra arriba y empuja el vástago hacia abajo.', at: s + 6.3 },
        { k: 'Actuador inverso', d: 'El aire entra abajo y lo sube; el resorte lo empuja hacia abajo.', at: s + 10 },
        { k: 'Cuerpo', d: 'Empujar para cerrar es lo más común en globo; también existe empujar para abrir.', at: s + 14.7 },
      ]} />
    </SceneS>
  );
}

// ── Términos del trim ──────────────────────────────────────────────────────
function S_Terms({ start, dur }) {
  const t = useTime(); const s = start;
  const lt = Math.max(0, t - s);
  const items = [
    ['Carrera', 'Recorrido del vástago de cerrada a abierta, en mm o en %.', 3.7],
    ['Puerto', 'Diámetro del orificio del asiento: fija la capacidad.', 5.9],
    ['Trim', 'Partes internas en contacto con el fluido que deciden el caudal: asiento, obturador, jaula y vástago.', 9],
    ['Trim reducido', 'Puerto menor que el nominal del cuerpo, para cuando la válvula quedaría sobredimensionada.', 11.9],
    ['Guiado', 'Por arriba, por el puerto o por jaula: mantiene el obturador centrado contra la vibración.', 15.1],
    ['Anillo de asiento', 'Pieza reemplazable donde asienta el obturador; puede llevar inserto blando.', 19.7],
  ];
  const open = 0.5 + 0.45 * Math.sin(lt * 0.8);
  const X = 470, Y = 690, S = 0.95, L = LIFT * open * S;
  const on = (a) => segS(t, s + a, 0.5);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Vocabulario del trim" title={<>Seis términos que verás en toda hoja de datos.</>} y="15%" size={46} />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
        <ValveDefs />
        <clipPath id="vcTerms"><rect x="80" y="270" width="780" height="660" rx="18" /></clipPath>
        <rect x="80" y="270" width="780" height="660" rx="18" fill={SL.paper} stroke={SL.lineS} />
        <g clipPath="url(#vcTerms)" opacity={on(1)}><g transform={`translate(${X} ${Y}) scale(${S})`}>
          <GlobeAssembly open={open} air={open} t={t} trim="simple" show={vcOnly(['body', 'seat', 'plug', 'stem', 'bonnet'])} />
        </g></g>
        {/* carrera */}
        <g opacity={on(3.7)}>
          <line x1={X + 150} y1={Y - 17 * S} x2={X + 150} y2={Y - 17 * S - L} stroke={SL.cc} strokeWidth="6" />
          <path d={`M ${X + 140} ${Y - 17 * S - L} h 20 M ${X + 140} ${Y - 17 * S} h 20`} stroke={SL.cc} strokeWidth="4" />
          <rect x={X + 160} y={Y - 56 - L / 2} width="186" height="36" rx="8" fill={SL.paper} opacity="0.92" />
          <text x={X + 170} y={Y - 30 - L / 2} fill={SL.cc} fontFamily={MONO} fontSize="20" fontWeight="700">carrera {Math.round(open * 100)} %</text>
        </g>
        {/* puerto */}
        <g opacity={on(5.9)}>
          <line x1={X - 64 * S} y1={Y + 40} x2={X + 64 * S} y2={Y + 40} stroke={SL.sunD} strokeWidth="5" />
          <path d={`M ${X - 64 * S} ${Y + 30} v 20 M ${X + 64 * S} ${Y + 30} v 20`} stroke={SL.sunD} strokeWidth="4" />
          <text x={X} y={Y + 80} fill={SL.sunD} fontFamily={MONO} fontSize="20" fontWeight="700" textAnchor="middle">puerto</text>
        </g>
        {/* guiado y anillo de asiento */}
        <g opacity={on(15.1)}>
          <line x1={X - 36 * S} y1={Y - 235 * S} x2={X - 210} y2={Y - 300} stroke={SL.ink} strokeWidth="2.5" strokeDasharray="6 5" />
          <text x={X - 215} y={Y - 306} fill={SL.ink} fontFamily={MONO} fontSize="20" fontWeight="700" textAnchor="end">guía</text>
        </g>
        <g opacity={on(19.7)}>
          <line x1={X - 90 * S} y1={Y} x2={X - 250} y2={Y + 120} stroke={SL.ink} strokeWidth="2.5" strokeDasharray="6 5" />
          <text x={X - 255} y={Y + 145} fill={SL.ink} fontFamily={MONO} fontSize="20" fontWeight="700" textAnchor="middle">anillo de asiento</text>
        </g>
      </svg>
      <div style={{ position: 'absolute', left: 920, top: 270, width: 900, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, fontFamily: DISP }}>
        {items.map(([h, b, at]) => {
          const p = popS(t, s + at);
          return (
            <div key={h} style={{ opacity: p.op, transform: `translateY(${p.ty}px)`, background: SL.paper, borderRadius: 14, padding: '16px 20px', borderLeft: `5px solid ${SL.sun}`, boxShadow: SL.shadowSm }}>
              <div style={{ fontSize: 25, fontWeight: 700, color: SL.ink }}>{h}</div>
              <div style={{ fontSize: 18, color: SL.mut, lineHeight: 1.4, marginTop: 4 }}>{b}</div>
            </div>
          );
        })}
      </div>
    </SceneS>
  );
}

const SCENES_VC1C2 = [
  { C: (p) => <TitleCardS {...p} moduleNo={1} claseNo={2} title="Válvulas de vástago deslizante" objetivo="Estilos de cuerpo, bonetes, empaquetaduras, la acción del actuador y del cuerpo, y el vocabulario del trim." />, dur: 16, label: 'Apertura' },
  { C: S_BodyStyles, dur: 28, label: 'Estilos de cuerpo' },
  { C: S_Bonnets, dur: 23, label: 'Bonetes' },
  { C: S_Packing, dur: 26, label: 'Empaquetadura' },
  { C: S_Action, dur: 24, label: 'Acción' },
  { C: S_Terms, dur: 26, label: 'Vocabulario' },
  { C: (p) => <ClosingS {...p} line="Cuerpo, bonete, empaquetadura y acción: cuatro decisiones que se toman según el servicio." question="Una válvula de vapor a 380 °C, con fluido no tóxico: ¿qué bonete y qué empaquetadura elegirías, y por qué?" />, dur: 16, label: 'Cierre' },
];

window.SCENES_VC1C2 = SCENES_VC1C2;
