// mf-art.jsx — Dibujos del curso «Medición Fiscal». Va después de flujo-art.jsx y level-art.jsx
// (usa FA, FlowDefs, FaText, FlowDots, TurbineMeter, ChordDisk, BallProver, Vessel).
// Todo en coordenadas de pantalla (1920 × 1080). Cada <svg> debe incluir <FlowDefs />.

const MF = { gas: '#e8eef6', gasD: '#b9c8da', oil: '#c99a52', oilL: '#ead3a6', sig: '#2f86e6', hl: '#f48a3c', hlW: 'rgba(244,138,60,0.18)' };

// Resalta un componente con un halo naranja
const MfHalo = ({ on, x, y, w, h, r = 14 }) => on ? <rect x={x - 10} y={y - 10} width={w + 20} height={h + 20} rx={r} fill={MF.hlW} stroke={MF.hl} strokeWidth="3" strokeDasharray="10 6" /> : null;

// Tubería simple en esquema (línea gruesa con borde)
function MfPipe({ d, w = 22, fill = MF.gas }) {
  return <g><path d={d} fill="none" stroke={FA.ink} strokeWidth={w + 6} strokeLinejoin="round" /><path d={d} fill="none" stroke={fill} strokeWidth={w} strokeLinejoin="round" /></g>;
}
// Flujo animado sobre una ruta horizontal
function MfFlow({ x1, x2, y, t, color = FA.dot, n = 10, speed = 60 }) {
  const L = x2 - x1;
  return <g>{Array.from({ length: n }, (_, i) => { const x = x1 + ((i * L / n + t * speed) % L); return <circle key={i} cx={x} cy={y} r="4" fill={color} opacity="0.75" />; })}</g>;
}
// Válvula de bola en esquema (corbatín)
function MfValve({ x, y, s = 16, open = true, color = FA.paint }) {
  return <g><path d={`M${x - s} ${y - s} L${x + s} ${y + s} L${x + s} ${y - s} L${x - s} ${y + s} Z`} fill={open ? color : FA.bad} stroke={FA.ink} strokeWidth="2.5" />
    <line x1={x} y1={y} x2={x} y2={y - s - 12} stroke={FA.ink} strokeWidth="3" /><rect x={x - 12} y={y - s - 18} width="24" height="7" fill={FA.ink} /></g>;
}
// Transmisor (círculo con etiqueta ISA)
function MfTx({ x, y, tag, r = 26, color = FA.ink }) {
  return <g><circle cx={x} cy={y} r={r} fill="#fff" stroke={color} strokeWidth="2.5" /><text x={x} y={y + 6} textAnchor="middle" fontFamily={MONO4} fontSize="16" fontWeight="700" fill={color}>{tag}</text></g>;
}
// Línea de señal punteada
const MfSig = ({ d, op = 1 }) => <path d={d} fill="none" stroke={MF.sig} strokeWidth="2.5" strokeDasharray="7 6" opacity={op} />;

// ── Computador de flujo ─────────────────────────────────────────────────────
function FlowComputer({ x, y, w = 210, h = 150, t = 0, lines = ['Q  12 480 m³/h', 'P  41,2 bar', 'T  24,6 °C'] }) {
  return <g>
    <rect x={x} y={y} width={w} height={h} rx="12" fill={FA.paintD} stroke={FA.ink} strokeWidth="3" />
    <rect x={x + 14} y={y + 14} width={w - 28} height={h - 58} rx="6" fill="#0f2b4a" />
    {lines.map((l, i) => <text key={i} x={x + 24} y={y + 38 + i * 22} fontFamily={MONO4} fontSize="15" fill="#7ee2a8">{l}</text>)}
    {[0, 1, 2, 3].map((i) => <rect key={i} x={x + 18 + i * ((w - 36) / 4)} y={y + h - 34} width={(w - 36) / 4 - 8} height="18" rx="4" fill={FA.paintL} />)}
    <circle cx={x + w - 18} cy={y + 8} r="5" fill={Math.floor(t * 2) % 2 ? '#7ee2a8' : FA.ok} />
  </g>;
}

// ── Cadena de custodia: del pozo al comprador ──────────────────────────────
function CustodyChain({ t = 0, s = 0, at = [0, 1, 2, 3, 4], y = 560 }) {
  const xs = [220, 600, 960, 1320, 1700];
  const op = (i) => pop4(t, s + at[i]).op;
  const node = (i, el, name, sub, hot) => <g opacity={op(i)}>
    {hot && <circle cx={xs[i]} cy={y} r="118" fill={MF.hlW} stroke={MF.hl} strokeWidth="3" strokeDasharray="10 6" />}
    {el}
    <FaText x={xs[i]} y={y + 160} size={24} anchor="middle" weight={700}>{name}</FaText>
    <FaText x={xs[i]} y={y + 192} size={18} anchor="middle" weight={400} color={FA.mut}>{sub}</FaText>
  </g>;
  return <g>
    <g opacity={op(1)}><MfPipe d={`M${xs[0]} ${y} H${xs[4]}`} w={18} fill={MF.oilL} /><MfFlow x1={xs[0]} x2={xs[4]} y={y} t={t} color={MF.oil} n={22} /></g>
    {node(0, <g>
      <rect x={xs[0] - 14} y={y - 90} width="28" height="90" fill={FA.steelD} stroke={FA.ink} strokeWidth="2.5" />
      {[-60, -30].map((dy) => <g key={dy}><rect x={xs[0] - 40} y={y + dy - 8} width="80" height="16" fill={FA.paint} stroke={FA.ink} strokeWidth="2" /><circle cx={xs[0] - 46} cy={y + dy} r="10" fill={FA.bad} stroke={FA.ink} strokeWidth="2" /></g>)}
      <rect x={xs[0] - 60} y={y} width="120" height="14" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
    </g>, 'Pozo', 'producción')}
    {node(1, <g>
      <rect x={xs[1] - 90} y={y - 60} width="180" height="100" rx="50" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
      <rect x={xs[1] - 80} y={y - 5} width="160" height="38" rx="18" fill={MF.oilL} />
      <line x1={xs[1]} y1={y - 60} x2={xs[1]} y2={y - 100} stroke={FA.ink} strokeWidth="6" />
    </g>, 'Separación', 'gas · crudo · agua')}
    {node(2, <g>
      <rect x={xs[2] - 70} y={y - 34} width="140" height="68" rx="10" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      <FaText x={xs[2]} y={y + 8} size={22} anchor="middle" color="#fff" mono>FQ</FaText>
      <rect x={xs[2] - 40} y={y - 110} width="80" height="58" rx="8" fill={FA.paintD} stroke={FA.ink} strokeWidth="2.5" />
      <FaText x={xs[2]} y={y - 73} size={20} anchor="middle" color="#7ee2a8" mono>$</FaText>
      <line x1={xs[2]} y1={y - 52} x2={xs[2]} y2={y - 34} stroke={MF.sig} strokeWidth="3" strokeDasharray="5 4" />
    </g>, 'Medición fiscal', 'aquí cambia de dueño', true)}
    {node(3, <g>
      <rect x={xs[3] - 100} y={y - 26} width="200" height="52" rx="26" fill={FA.steelD} stroke={FA.ink} strokeWidth="3" />
      {[-60, 0, 60].map((dx) => <rect key={dx} x={xs[3] + dx - 6} y={y + 26} width="12" height="40" fill={FA.ink} />)}
    </g>, 'Ducto o buque', 'transporte')}
    {node(4, <g>
      {[-50, 30].map((dx, k) => <g key={dx}><rect x={xs[4] + dx - 40} y={y - 80 + k * 20} width="80" height={110 - k * 20} fill={FA.steel} stroke={FA.ink} strokeWidth="3" /><ellipse cx={xs[4] + dx} cy={y - 80 + k * 20} rx="40" ry="10" fill={FA.trim} stroke={FA.ink} strokeWidth="2" /></g>)}
      <rect x={xs[4] - 100} y={y + 30} width="200" height="14" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
    </g>, 'Comprador', 'refinería o distribuidora')}
  </g>;
}

// ── Estación de medición de gas (esquema en planta) ────────────────────────
// hl: 'filtro' | 'acond' | 'medidor' | 'pt' | 'gc' | 'fc' | 'valv' | 'serie'
function GasStation({ t = 0, hl = '', y0 = 470 }) {
  const yA = y0, yB = y0 + 200, xin = 120, xh1 = 520, xh2 = 1480, xout = 1800, ym = y0 + 100;
  const run = (y, k) => <g key={y}>
    <MfPipe d={`M${xh1} ${y} H${xh2}`} />
    <MfFlow x1={xh1} x2={xh2} y={y} t={t + k} n={16} />
    <MfValve x={xh1 + 70} y={y} />
    <rect x={xh1 + 150} y={y - 26} width="22" height="52" fill="url(#faHatchT)" stroke={FA.ink} strokeWidth="2" />
    <rect x={xh1 + 470} y={y - 34} width="150" height="68" rx="10" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    {[0, 1, 2].map((i) => <rect key={i} x={xh1 + 485 + i * 44} y={y - 46} width="24" height="16" fill={FA.paintD} stroke={FA.ink} strokeWidth="1.5" />)}
    <FaText x={xh1 + 545} y={y + 7} size={18} anchor="middle" color="#fff" mono>FE</FaText>
    <line x1={xh1 + 690} y1={y - 14} x2={xh1 + 690} y2={y - 70} stroke={FA.ink} strokeWidth="3" /><MfTx x={xh1 + 690} y={y - 94} tag="PT" r={22} />
    <line x1={xh1 + 770} y1={y - 14} x2={xh1 + 770} y2={y - 70} stroke={FA.ink} strokeWidth="3" /><MfTx x={xh1 + 770} y={y - 94} tag="TT" r={22} />
    <MfValve x={xh2 - 70} y={y} />
  </g>;
  return <g>
    <MfHalo on={hl === 'filtro'} x={280} y={y0 - 20} w={150} h={240} />
    <MfHalo on={hl === 'acond'} x={xh1 + 130} y={yA - 50} w={300} h={yB - yA + 100} />
    <MfHalo on={hl === 'medidor'} x={xh1 + 460} y={yA - 60} w={170} h={yB - yA + 120} />
    <MfHalo on={hl === 'pt'} x={xh1 + 660} y={yA - 130} w={140} h={yB - yA + 150} />
    <MfHalo on={hl === 'valv'} x={xh1 + 40} y={yA - 50} w={60} h={yB - yA + 100} />
    <MfHalo on={hl === 'gc'} x={1540} y={y0 - 300} w={170} h={160} />
    <MfHalo on={hl === 'fc'} x={1060} y={y0 - 330} w={230} h={170} />
    <MfPipe d={`M${xin} ${ym} H280`} />
    <MfPipe d={`M430 ${ym} H${xh1}`} />
    <MfPipe d={`M${xh1} ${yA} V${yB}`} /><MfPipe d={`M${xh2} ${yA} V${yB}`} />
    <MfPipe d={`M${xh2} ${ym} H${xout}`} />
    {/* filtro separador */}
    <rect x={290} y={y0 - 10} width="130" height="220" rx="60" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={300} y={y0 + 150} width="110" height="40" rx="18" fill="#b9a57d" />
    {[0, 1, 2, 3, 4].map((i) => <line key={i} x1={320 + i * 18} y1={y0 + 30} x2={320 + i * 18} y2={y0 + 130} stroke={FA.mut} strokeWidth="4" />)}
    {run(yA, 0)}{run(yB, 0.37)}
    <MfFlow x1={xin} x2={280} y={ym} t={t} n={5} /><MfFlow x1={xh2} x2={xout} y={ym} t={t} n={6} />
    {/* computador de flujo y cromatógrafo */}
    <FlowComputer x={1070} y={y0 - 320} t={t} w={210} h={150} />
    <MfSig d={`M${xh1 + 545} ${yA - 46} V${y0 - 200} H1070`} />
    <MfSig d={`M${xh1 + 690} ${yA - 116} V${y0 - 140} H1175 V${y0 - 170}`} />
    <rect x={1550} y={y0 - 290} width="150" height="140" rx="10" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={1566} y={y0 - 272} width="118" height="46" rx="4" fill="#0f2b4a" />
    <path d={`M1576 ${y0 - 236} l14 -20 l10 14 l12 -26 l12 30 l10 -10 l20 0`} fill="none" stroke="#7ee2a8" strokeWidth="2.5" />
    <FaText x={1625} y={y0 - 176} size={18} anchor="middle" mono>AT · GC</FaText>
    <line x1={1625} y1={y0 - 150} x2={1625} y2={ym - 14} stroke={FA.ink} strokeWidth="3" />
    <MfSig d={`M1550 ${y0 - 220} H1280`} />
    <FaText x={xin} y={ym - 30} size={18} mono color={FA.mut}>entrada</FaText>
    <FaText x={xout} y={ym - 30} size={18} mono color={FA.mut} anchor="end">salida</FaText>
  </g>;
}

// ── Patín de medición de crudo (LACT) ──────────────────────────────────────
// hl: 'bomba' | 'filtro' | 'aire' | 'bsw' | 'muestra' | 'medidor' | 'pt' | 'probador' | 'bpv' | 'fc'
function LactSkid({ t = 0, hl = '', y = 640 }) {
  const fill = MF.oilL;
  return <g>
    <MfHalo on={hl === 'bomba'} x={150} y={y - 70} w={140} h={140} />
    <MfHalo on={hl === 'filtro'} x={330} y={y - 80} w={100} h={140} />
    <MfHalo on={hl === 'aire'} x={470} y={y - 190} w={100} h={210} />
    <MfHalo on={hl === 'bsw'} x={620} y={y - 110} w={80} h={110} />
    <MfHalo on={hl === 'muestra'} x={740} y={y - 40} w={150} h={250} />
    <MfHalo on={hl === 'medidor'} x={930} y={y - 70} w={230} h={140} />
    <MfHalo on={hl === 'pt'} x={1190} y={y - 130} w={160} h={130} />
    <MfHalo on={hl === 'probador'} x={1380} y={y - 330} w={300} h={330} />
    <MfHalo on={hl === 'bpv'} x={1700} y={y - 50} w={80} h={90} />
    <MfHalo on={hl === 'fc'} x={830} y={y - 420} w={230} h={170} />
    <MfPipe d={`M100 ${y} H1840`} fill={fill} />
    <MfFlow x1={100} x2={1840} y={y} t={t} color={MF.oil} n={34} />
    {/* bomba */}
    <circle cx={220} cy={y} r="52" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    <path d={`M${220 - 22} ${y - 30} L${220 + 34} ${y} L${220 - 22} ${y + 30} Z`} fill="#fff" opacity="0.85" />
    {/* filtro */}
    <rect x={345} y={y - 66} width="70" height="112" rx="10" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    {[0, 1, 2].map((i) => <line key={i} x1={360 + i * 20} y1={y - 50} x2={360 + i * 20} y2={y + 30} stroke={FA.mut} strokeWidth="3" strokeDasharray="4 4" />)}
    {/* eliminador de aire */}
    <rect x={485} y={y - 175} width="70" height="160" rx="30" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={495} y={y - 90} width="50" height="70" rx="14" fill={fill} />
    <line x1={520} y1={y - 175} x2={520} y2={y - 205} stroke={FA.ink} strokeWidth="4" /><MfValve x={520} y={y - 215} s={10} />
    {/* sonda BS&W */}
    <rect x={648} y={y - 95} width="24" height="80" fill={FA.paintD} stroke={FA.ink} strokeWidth="2" />
    <FaText x={660} y={y - 112} size={16} anchor="middle" mono>AT</FaText>
    {/* muestreador */}
    <rect x={782} y={y - 26} width="14" height="52" fill={FA.ink} />
    <line x1={789} y1={y + 26} x2={789} y2={y + 110} stroke={FA.ink} strokeWidth="4" />
    <rect x={755} y={y + 110} width="120" height="90" rx="12" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={765} y={y + 150} width="100" height="40" rx="8" fill={fill} />
    {/* medidor */}
    <rect x={945} y={y - 56} width="200" height="112" rx="14" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    <FaText x={1045} y={y + 8} size={22} anchor="middle" color="#fff" mono>FQ</FaText>
    {/* T y P */}
    <line x1={1230} y1={y - 14} x2={1230} y2={y - 70} stroke={FA.ink} strokeWidth="3" /><MfTx x={1230} y={y - 94} tag="TT" r={22} />
    <line x1={1310} y1={y - 14} x2={1310} y2={y - 70} stroke={FA.ink} strokeWidth="3" /><MfTx x={1310} y={y - 94} tag="PT" r={22} />
    {/* conexiones al probador */}
    <MfPipe d={`M1410 ${y} V${y - 300} H1650 V${y}`} w={14} fill={fill} />
    <rect x={1440} y={y - 324} width="180" height="48" rx="24" fill={FA.steelD} stroke={FA.ink} strokeWidth="3" />
    <circle cx={1440 + 30 + ((t * 40) % 120)} cy={y - 300} r="18" fill={FA.paintD} stroke={FA.ink} strokeWidth="2" />
    <FaText x={1530} y={y - 340} size={18} anchor="middle" mono color={FA.mut}>probador</FaText>
    <MfValve x={1410} y={y - 170} s={12} /><MfValve x={1650} y={y - 170} s={12} /><MfValve x={1530} y={y} s={14} />
    {/* contrapresión */}
    <path d={`M1724 ${y - 20} L1756 ${y + 20} L1756 ${y - 20} L1724 ${y + 20} Z`} fill={FA.paint} stroke={FA.ink} strokeWidth="2.5" />
    <rect x={1722} y={y - 52} width="36" height="22" rx="6" fill={FA.paintD} stroke={FA.ink} strokeWidth="2" />
    {/* computador */}
    <FlowComputer x={840} y={y - 410} t={t} w={210} h={150} lines={['NSV 9 925 bbl', 'MF  1,0012', 'T   29,4 °C']} />
    <MfSig d={`M1045 ${y - 56} V${y - 260}`} />
    <MfSig d={`M1230 ${y - 116} V${y - 340} H1050`} />
  </g>;
}

// ── Tiro al blanco: exactitud y repetibilidad ──────────────────────────────
function TargetMf({ cx, cy, r = 120, kind = 'ok', t = 0, label }) {
  const off = { ok: [0, 0, 0.12], bias: [0.45, -0.35, 0.12], spread: [0, 0, 0.6], bad: [0.4, 0.3, 0.6] }[kind];
  const pts = [[0.3, -0.2], [-0.4, 0.5], [0.7, 0.4], [-0.6, -0.5], [0.1, 0.8], [0.8, -0.6], [-0.2, -0.9]];
  return <g>
    {[1, 0.75, 0.5, 0.25].map((k, i) => <circle key={k} cx={cx} cy={cy} r={r * k} fill={i % 2 ? '#fff' : FA.trim} stroke={FA.ink} strokeWidth="2" />)}
    <circle cx={cx} cy={cy} r={r * 0.08} fill={FA.bad} />
    {pts.map(([a, b], i) => <circle key={i} cx={cx + r * (off[0] + off[2] * a)} cy={cy + r * (off[1] + off[2] * b)} r="9" fill={FA.dot} stroke="#fff" strokeWidth="2" opacity={clamp(t * 3 - i * 0.4, 0, 1)} />)}
    {label && <FaText x={cx} y={cy + r + 40} size={22} anchor="middle" weight={700}>{label}</FaText>}
  </g>;
}

// ── Tanque con medición manual (cinta y plomada) ───────────────────────────
function TankGauge({ x, y, w = 420, h = 360, level = 0.62, t = 0 }) {
  const ly = y + h - h * level;
  return <g>
    <rect x={x} y={y} width={w} height={h} fill="#fff" stroke={FA.ink} strokeWidth="3" />
    <rect x={x + 3} y={ly} width={w - 6} height={y + h - ly - 3} fill={MF.oilL} />
    <rect x={x + 3} y={y + h - 26} width={w - 6} height="23" fill="#9cc3ea" opacity="0.8" />
    <path d={`M${x - 10} ${y} L${x + w / 2} ${y - 50} L${x + w + 10} ${y}`} fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={x + w * 0.7 - 14} y={y - 34} width="28" height="20" fill={FA.paintD} />
    <line x1={x + w * 0.7} y1={y - 14} x2={x + w * 0.7} y2={y + h - 12} stroke={FA.hi} strokeWidth="3" strokeDasharray={`${4 + t * 0} 0`} />
    <path d={`M${x + w * 0.7 - 8} ${y + h - 30} h16 l-8 20 z`} fill={FA.ink} />
  </g>;
}

// ── Tramo de medición con placa de orificio en corte (gas) ─────────────────
// Placa entre bridas, tomas de brida hacia arriba y transmisor de ΔP encima (gas: drena hacia la línea).
function OrificeRun({ t = 0, x1 = 160, x2 = 1500, x0 = 760, yc = 640, R = 80, wall = 16, fh = 30, beta = 0.5, taps = 1, eddies = 1, worn = false, flip = false, dp = '', tag = 'FT-301', dots = true }) {
  const tab = React.useMemo(() => faTable(x1, x2, orificeBounds(x0, yc, R, beta)), [x1, x2, x0, yc, R, beta]);
  const top = yc - R - wall - fh, xH = x0 - 30, xL = x0 + 30, yT = top - 170;
  return <g>
    <PipeRun x1={x1} x2={x2} yc={yc} R={R} wall={wall} fill={MF.gas} />
    <g opacity={eddies}><OrificeEddies x0={x0} yc={yc} R={R} beta={beta} t={t} /></g>
    {dots && <FlowDots T={tab} t={t} n={44} speed={0.07} r={5} />}
    <FlangePair x={x0} yc={yc} R={R} wall={wall} h={fh} gap={12} w={34} />
    <OrificePlate x={x0} yc={yc} R={R} wall={wall} h={fh} beta={beta} worn={worn} flip={flip} />
    <g opacity={taps}>
      <circle cx={xH} cy={yc - R - wall / 2} r="5" fill={FA.hi} /><circle cx={xL} cy={yc - R - wall / 2} r="5" fill={FA.lo} />
      <Tube d={`M${xH} ${yc - R - wall / 2} V${top - 40} H${x0 - 72} V${yT}`} color={FA.hi} w={6} />
      <Tube d={`M${xL} ${yc - R - wall / 2} V${top - 40} H${x0 + 72} V${yT}`} color={FA.lo} w={6} />
      <DPCell x={x0} y={yT} val={dp} tag={tag} />
    </g>
  </g>;
}


// ── Módulo 4: medidores lineales de gas ──
// Tramo con turbina en corte, puntos de gas y captador con su tren de pulsos.
function TurbineRun({ t = 0, x1 = 120, x2 = 1100, x0 = 610, yc = 600, R = 120, rps = 0.6, labels = 1, pulses = 1, py = 860, dots = true, fill = MF.gas }) {
  const tab = React.useMemo(() => faTable(x1, x2, () => [yc - R, yc + R]), [x1, x2, yc, R]);
  return <g>
    <PipeRun x1={x1} x2={x2} yc={yc} R={R} wall={16} fill={fill} />
    {dots && <FlowDots T={tab} t={t} n={36} speed={0.05} r={4.5} />}
    <TurbineMeter x0={x0} yc={yc} R={R} t={t} rps={rps} N={6} />
    <g opacity={labels}>
      <FaText x={x0 - R * 1.25} y={yc + R + 46} size={18} anchor="middle" color={FA.mut} mono>enderezador</FaText>
      <FaText x={x0} y={yc + R + 46} size={18} anchor="middle" color={FA.mut} mono>rotor</FaText>
      <FaText x={x0 + 40} y={yc - R - 40} size={18} color={FA.mut} mono>captador</FaText>
    </g>
    <g opacity={pulses}><PulseTrain x={x0 - 320} y={py} w={640} h={60} t={t} f={6} /></g>
  </g>;
}
// Tramo con ultrasónico de tiempo de tránsito (una trayectoria diagonal).
function UsmRun({ t = 0, x1 = 120, x2 = 1100, xa = 470, xb = 750, yc = 600, R = 140, dots = true, path = 1, fill = MF.gas }) {
  const tab = React.useMemo(() => faTable(x1, x2, () => [yc - R, yc + R]), [x1, x2, yc, R]);
  return <g>
    <PipeRun x1={x1} x2={x2} yc={yc} R={R} wall={16} fill={fill} />
    {dots && <FlowDots T={tab} t={t} n={40} speed={0.05} r={4.5} />}
    <g opacity={path}><TransitPath xa={xa} xb={xb} yc={yc} R={R} wall={16} t={t} /></g>
  </g>;
}
// Términos de una ecuación que aparecen uno por uno: items = [[texto, explicación, at, color]]
function MfEq({ t, s, items, top = 380, size = 44, gap = 34 }) {
  return (
    <div style={{ position: 'absolute', left: 140, top, width: 1640, display: 'flex', alignItems: 'flex-start', justifyContent: 'center', gap }}>
      {items.map(([txt, sub, at, color], i) => { const p = pop4(t, s + at); return (
        <div key={i} style={{ opacity: p.op, transform: `translateY(${p.ty}px)`, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10 }}>
          <div style={{ fontFamily: MONO4, fontSize: size, fontWeight: 700, color: color || TL4.ink }}>{txt}</div>
          <div style={{ fontFamily: DISP4, fontSize: 20, color: TL4.mut, maxWidth: 300, textAlign: 'center' }}>{sub}</div>
        </div>); })}
    </div>
  );
}
// Cadena de pasos con flechas: steps = [[título, texto, at]]
function MfSteps({ t, s, steps, y = 420, w = 300 }) {
  return (
    <div style={{ position: 'absolute', left: 100, top: y, width: 1720, display: 'flex', alignItems: 'stretch', justifyContent: 'center', gap: 14 }}>
      {steps.map(([k, v, at], i) => { const p = pop4(t, s + at); const last = i === steps.length - 1; return (
        <React.Fragment key={i}>
          {i > 0 && <div style={{ opacity: p.op, alignSelf: 'center', fontSize: 40, color: TL4.mut }}>→</div>}
          <div style={{ opacity: p.op, transform: `translateY(${p.ty}px)`, background: TL4.paper, borderRadius: 14, boxShadow: TL4.shadow, padding: '18px 20px', borderTop: `6px solid ${last ? TL4.orange : TL4.blue}`, width: w }}>
            <div style={{ fontFamily: MONO4, fontSize: 22, fontWeight: 700, color: last ? TL4.orangeD : TL4.blue }}>{k}</div>
            <div style={{ fontFamily: DISP4, fontSize: 22, color: TL4.ink, marginTop: 8 }}>{v}</div>
          </div>
        </React.Fragment>); })}
    </div>
  );
}
// Barras horizontales: rows = [[etiqueta, valor, color, texto del valor]]; ats = tiempos de aparición
function MfBars({ t, s, rows, ats, max, x0 = 700, w = 700, y = 330, gap = 100, lw = 26 }) {
  return <svg style={SVG_T}>
    {rows.map(([l, v, c, txt], i) => { const y1 = y + i * gap; const p = pop4(t, s + ats[i]).op; const k = clamp((t - s - ats[i]) / 0.8, 0, 1); return (
      <g key={i} opacity={p}>
        <FaText x={x0 - 24} y={y1 + 34} size={lw} anchor="end" weight={700}>{l}</FaText>
        <rect x={x0} y={y1} width={Math.max(4, w * v / max * k)} height={48} rx="6" fill={c} />
        <FaText x={x0 + w * v / max * k + 16} y={y1 + 34} size={lw} weight={700} color={c}>{txt}</FaText>
      </g>); })}
  </svg>;
}


// ── Módulo 5: calidad del gas ──
// Cinta de calentamiento: zigzag naranja a lo largo de una ruta recta (de (x1,y1) a (x2,y2)).
function HeatTrace({ x1, y1, x2, y2, n = 14, a = 7, op = 1 }) {
  const L = Math.hypot(x2 - x1, y2 - y1), ux = (x2 - x1) / L, uy = (y2 - y1) / L, nx = -uy, ny = ux;
  const pts = Array.from({ length: n * 2 + 1 }, (_, i) => { const k = i / (n * 2), s = i % 2 ? a : -a; return `${(x1 + ux * L * k + nx * s).toFixed(1)} ${(y1 + uy * L * k + ny * s).toFixed(1)}`; });
  return <path d={'M' + pts.join(' L')} fill="none" stroke={FA.hi} strokeWidth="3" opacity={op} />;
}
// Cilindro de presión constante (pistón flotante), horizontal. fill = fracción con muestra (izquierda).
function PistonCylinder({ x, y, w = 360, h = 110, fill = 0.5, labels = 1 }) {
  const r = h / 2, xi = x + r * 0.6, xo = x + w - r * 0.6, xp = xi + (xo - xi - 24) * clamp(fill, 0, 1);
  return <g>
    <rect x={x} y={y} width={w} height={h} rx={r} fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={xi} y={y + 10} width={xp - xi} height={h - 20} fill={MF.gas} />
    {Array.from({ length: 8 }, (_, k) => <circle key={k} cx={xi + 10 + ((k * 37) % Math.max(10, xp - xi - 20))} cy={y + 24 + ((k * 23) % (h - 48))} r="4" fill={FA.dot} opacity={fill > 0.05 ? 0.8 : 0} />)}
    <rect x={xp + 24} y={y + 10} width={xo - xp - 24} height={h - 20} fill={FA.trimD} opacity="0.55" />
    <rect x={xp} y={y + 6} width="24" height={h - 12} rx="4" fill={FA.paint} stroke={FA.ink} strokeWidth="2.5" />
    <line x1={x - 30} y1={y + r} x2={x} y2={y + r} stroke={FA.ink} strokeWidth="6" />
    <line x1={x + w} y1={y + r} x2={x + w + 30} y2={y + r} stroke={FA.ink} strokeWidth="6" />
    <MfValve x={x - 40} y={y + r} s={12} /><MfValve x={x + w + 40} y={y + r} s={12} />
    <g opacity={labels}>
      <FaText x={xi + 6} y={y + h + 34} size={18} color={FA.dot} mono>muestra</FaText>
      <FaText x={xp + 12} y={y - 16} size={18} anchor="middle" color={FA.paintD} mono>pistón</FaText>
      <FaText x={xo - 6} y={y + h + 34} size={18} anchor="end" color={FA.mut} mono>gas de carga</FaText>
    </g>
  </g>;
}
// Tramo con sonda de muestreo: la punta queda en el tercio central, por arriba del tubo.
// Sube a una válvula, un regulador y sigue hacia la derecha (calentado si heat > 0).
function SampleProbeRun({ t = 0, x1 = 120, x2 = 1100, xp = 560, yc = 640, R = 130, band = 1, heat = 0, labels = 1, xe = 1080 }) {
  const tab = React.useMemo(() => faTable(x1, x2, () => [yc - R, yc + R]), [x1, x2, yc, R]);
  const top = yc - R - 16, tip = yc - R * 0.12, yv = top - 70, yr = top - 190;
  return <g>
    <PipeRun x1={x1} x2={x2} yc={yc} R={R} wall={16} fill={MF.gas} />
    <FlowDots T={tab} t={t} n={36} speed={0.05} r={4.5} />
    <g opacity={band}>
      <rect x={x1} y={yc - R / 3} width={x2 - x1} height={2 * R / 3} fill={MF.hlW} />
      <line x1={x1} y1={yc - R / 3} x2={x2} y2={yc - R / 3} stroke={MF.hl} strokeWidth="2" strokeDasharray="10 7" />
      <line x1={x1} y1={yc + R / 3} x2={x2} y2={yc + R / 3} stroke={MF.hl} strokeWidth="2" strokeDasharray="10 7" />
      <FaText x={x2 - 14} y={yc + R / 3 - 12} size={18} anchor="end" color={FA.hi} mono>tercio central</FaText>
    </g>
    <rect x={xp - 9} y={top - 10} width="18" height={tip - top + 10} fill={FA.steelD} stroke={FA.ink} strokeWidth="2.5" />
    <path d={`M${xp - 9} ${tip} L${xp + 9} ${tip - 14} L${xp + 9} ${tip} Z`} fill={FA.ink} />
    <rect x={xp - 30} y={top - 26} width="60" height="18" rx="3" fill={FA.paintL} stroke={FA.ink} strokeWidth="2.5" />
    <Tube d={`M${xp} ${top - 26} V${yr + 30}`} w={7} />
    <MfValve x={xp} y={yv} s={14} />
    <rect x={xp - 34} y={yr - 30} width="68" height="60" rx="8" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    <path d={`M${xp - 14} ${yr - 30} V${yr - 52} M${xp - 6} ${yr - 52} l6 -6 l6 6 l6 -6`} stroke={FA.ink} strokeWidth="3" fill="none" />
    <Tube d={`M${xp + 34} ${yr} H${xe}`} w={7} />
    {heat > 0 && <><HeatTrace x1={xp} y1={top - 30} x2={xp} y2={yr + 34} n={6} op={heat} /><HeatTrace x1={xp + 40} y1={yr} x2={xe} y2={yr} n={16} op={heat} /></>}
    <g opacity={labels}>
      <FaText x={xp + 26} y={yv + 6} size={18} color={FA.mut} mono>válvula</FaText>
      <FaText x={xp - 46} y={yr + 6} size={18} anchor="end" color={FA.mut} mono>regulador</FaText>
      <FaText x={xp + 18} y={tip + 4} size={18} color={FA.ink} mono>sonda</FaText>
    </g>
  </g>;
}
// Cromatograma: picos gaussianos; peaks = [[tiempo 0..1, alto 0..1, etiqueta, color]]; prog = fracción dibujada.
function Chromatogram({ x, y, w = 760, h = 300, prog = 1, peaks, labels = 1, sw = 0.012, hl = -1 }) {
  const N = 260, sig = (u) => peaks.reduce((acc, [c, a]) => acc + a * Math.exp(-((u - c) ** 2) / (2 * sw * sw)), 0);
  const pts = []; for (let i = 0; i <= N * clamp(prog, 0, 1); i++) { const u = i / N; pts.push(`${(x + u * w).toFixed(1)} ${(y + h - Math.min(1, sig(u)) * (h - 30)).toFixed(1)}`); }
  return <g>
    <line x1={x} y1={y + h} x2={x + w + 20} y2={y + h} stroke={FA.ink} strokeWidth="2.5" />
    <line x1={x} y1={y + h} x2={x} y2={y - 10} stroke={FA.ink} strokeWidth="2.5" />
    <FaText x={x + w + 20} y={y + h + 30} size={17} anchor="end" color={FA.mut} mono>tiempo →</FaText>
    <FaText x={x + 8} y={y - 16} size={17} color={FA.mut} mono>señal</FaText>
    {peaks.map(([c, a, l, col], i) => i === hl && prog >= c ? <path key={'h' + i} d={'M' + Array.from({ length: 41 }, (_, k) => { const u = c - 3 * sw + 6 * sw * k / 40; return `${(x + u * w).toFixed(1)} ${(y + h - Math.min(1, sig(u)) * (h - 30)).toFixed(1)}`; }).join(' L') + ` L${(x + (c + 3 * sw) * w).toFixed(1)} ${y + h} L${(x + (c - 3 * sw) * w).toFixed(1)} ${y + h} Z`} fill={MF.hlW} stroke="none" /> : null)}
    {pts.length > 1 && <path d={'M' + pts.join(' L')} fill="none" stroke={FA.dot} strokeWidth="3" strokeLinejoin="round" />}
    <g opacity={labels}>{peaks.map(([c, a, l, col], i) => prog >= c + 0.01 ? <FaText key={i} x={x + c * w} y={y + h - Math.min(1, a) * (h - 30) - 12} size={16} anchor="middle" weight={700} color={col || FA.ink} mono>{l}</FaText> : null)}</g>
  </g>;
}
// Cromatógrafo en corte: helio, válvula de muestra, horno con columna y detector.
function GcBox({ x, y, t = 0, run = 0, labels = 1 }) {
  const ox = x + 250, oy = y, ow = 360, oh = 260, dx = ox + ow + 40;
  const coil = Array.from({ length: 5 }, (_, k) => `M${ox + 40} ${oy + 50 + k * 38} H${ox + ow - 40}`).join(' ');
  const bands = [['#2fae6a', 0.15], [FA.dot, 0.35], [FA.hi, 0.6], ['#8a5cc7', 0.85]];
  return <g>
    <rect x={x} y={y + 40} width="70" height="200" rx="30" fill={FA.paintL} stroke={FA.ink} strokeWidth="3" />
    <rect x={x + 22} y={y + 20} width="26" height="24" fill={FA.ink} />
    <Tube d={`M${x + 70} ${y + 140} H${x + 150}`} w={6} />
    <circle cx={x + 180} cy={y + 140} r="30" fill="#fff" stroke={FA.ink} strokeWidth="3" />
    <path d={`M${x + 162} ${y + 140} H${x + 198} M${x + 180} ${y + 122} V${y + 158}`} stroke={FA.paint} strokeWidth="4" transform={`rotate(${run > 0 ? 45 : 0} ${x + 180} ${y + 140})`} />
    <Tube d={`M${x + 180} ${y + 250} V${y + 170}`} w={6} color={MF.gasD} />
    <Tube d={`M${x + 210} ${y + 140} H${ox}`} w={6} />
    <rect x={ox} y={oy} width={ow} height={oh} rx="14" fill="#fdf1e6" stroke={FA.ink} strokeWidth="3" />
    <path d={coil} stroke={FA.steelD} strokeWidth="9" fill="none" strokeLinecap="round" />
    {run > 0 && bands.map(([c, sp], i) => { const p = clamp(run * sp * 1.6, 0, 1); const row = Math.min(4, Math.floor(p * 5)), k = p * 5 - row; const xx = row % 2 ? ox + ow - 40 - k * (ow - 80) : ox + 40 + k * (ow - 80); return <circle key={i} cx={xx} cy={oy + 50 + row * 38} r="9" fill={c} opacity={p < 1 ? 1 : 0} />; })}
    <Tube d={`M${ox + ow} ${oy + 202} H${dx}`} w={6} />
    <rect x={dx} y={oy + 170} width="90" height="64" rx="8" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    <circle cx={dx + 45} cy={oy + 202} r="12" fill={Math.floor(t * 3) % 2 ? '#7ee2a8' : FA.ok} />
    <g opacity={labels}>
      <FaText x={x + 35} y={y + 280} size={18} anchor="middle" color={FA.mut} mono>helio</FaText>
      <FaText x={x + 180} y={y + 92} size={18} anchor="middle" color={FA.mut} mono>válvula</FaText>
      <FaText x={x + 180} y={y + 280} size={18} anchor="middle" color={FA.mut} mono>muestra</FaText>
      <FaText x={ox + ow / 2} y={oy + oh + 34} size={18} anchor="middle" color={FA.mut} mono>horno con columnas</FaText>
      <FaText x={dx + 45} y={oy + 160} size={18} anchor="middle" color={FA.mut} mono>detector</FaText>
    </g>
  </g>;
}
// Caseta de analizadores conectada a una sonda y al computador de flujo.
function AnalyzerShelter({ t = 0, s = 0, at = [0, 0, 0, 0], x1 = 120, x2 = 1180, yc = 860, R = 70 }) {
  const op = (i) => pop4(t, s + at[i]).op;
  const tab = React.useMemo(() => faTable(x1, x2, () => [yc - R, yc + R]), [x1, x2, yc, R]);
  const bx = 300, by = 320, bw = 760, bh = 300, xp = 380;
  const an = [['GC', 'cromatógrafo'], ['H₂O', 'humedad'], ['H₂S', 'azufre']];
  return <g>
    <PipeRun x1={x1} x2={x2} yc={yc} R={R} wall={14} fill={MF.gas} />
    <FlowDots T={tab} t={t} n={30} speed={0.05} r={4} />
    <g opacity={op(0)}>
      <rect x={xp - 8} y={yc - R - 14} width="16" height={R} fill={FA.steelD} stroke={FA.ink} strokeWidth="2" />
      <Tube d={`M${xp} ${yc - R - 14} V${by + bh}`} w={6} />
      <HeatTrace x1={xp} y1={yc - R - 20} x2={xp} y2={by + bh + 6} n={6} />
      <path d={`M${bx - 20} ${by + 30} L${bx + bw / 2} ${by - 50} L${bx + bw + 20} ${by + 30}`} fill={FA.paintL} stroke={FA.ink} strokeWidth="3" />
      <rect x={bx} y={by + 30} width={bw} height={bh - 30} fill="#f6f9fc" stroke={FA.ink} strokeWidth="3" />
      <Tube d={`M${xp} ${by + bh - 40} H${bx + 600}`} w={5} />
    </g>
    {an.map(([k, l], i) => { const ax = bx + 40 + i * 200; return <g key={i} opacity={op(1 + Math.min(i, 1))}>
      <Tube d={`M${ax + 70} ${by + bh - 40} V${by + 210}`} w={5} />
      <rect x={ax} y={by + 70} width="140" height="140" rx="10" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      <rect x={ax + 16} y={by + 86} width="108" height="50" rx="5" fill="#0f2b4a" />
      <text x={ax + 70} y={by + 120} textAnchor="middle" fontFamily={MONO4} fontSize="22" fontWeight="700" fill="#7ee2a8">{k}</text>
      <FaText x={ax + 70} y={by + 180} size={17} anchor="middle" color="#fff" mono>{l}</FaText>
    </g>; })}
    <g opacity={op(3)}>
      <MfSig d={`M${bx + 110} ${by + 70} V${by + 46} H${bx + bw + 140} V${by + 110}`} />
      <FlowComputer x={bx + bw + 60} y={by + 110} t={t} lines={['Vb  1 250 km³', 'HHV 38,60', 'E   48 250 GJ']} />
    </g>
  </g>;
}


// ── Módulo 6: computadores de flujo y auditoría ──
// Cadena electrónica: tramo con placa, transmisores P, T y ΔP, computador de flujo y SCADA.
function EfmChain({ t = 0, s = 0, at = [0, 0, 0, 0], yc = 800 }) {
  const op = (i) => pop4(t, s + at[i]).op;
  const tab = React.useMemo(() => faTable(100, 1180, () => [yc - 50, yc + 50]), [yc]);
  const tx = [['PT', 300], ['FT', 560], ['TT', 860]];
  const fcx = 1180, fcy = 330;
  return <g>
    <PipeRun x1={100} x2={1180} yc={yc} R={50} wall={14} fill={MF.gas} />
    <FlowDots T={tab} t={t} n={30} speed={0.05} r={4} />
    <rect x={555} y={yc - 70} width="10" height="140" fill={FA.ink} />
    <g opacity={op(0)}>
      {tx.map(([k, x], i) => <g key={i}>
        <line x1={x} y1={yc - 64} x2={x} y2={600} stroke={FA.ink} strokeWidth="5" />
        <MfTx x={x} y={572} tag={k} r={30} />
        <MfSig d={`M${x} 542 V${470 - i * 30} H${fcx}`} />
      </g>)}
      <FaText x={300} y={640} size={17} anchor="middle" color={FA.mut} mono>presión</FaText>
      <FaText x={560} y={640} size={17} anchor="middle" color={FA.mut} mono>ΔP</FaText>
      <FaText x={860} y={640} size={17} anchor="middle" color={FA.mut} mono>temperatura</FaText>
    </g>
    <g opacity={op(1)}>
      <MfSig d={`M1000 ${yc - 140} V410 H${fcx}`} />
      <rect x={950} y={yc - 210} width="100" height="70" rx="8" fill={FA.paint} stroke={FA.ink} strokeWidth="2.5" />
      <text x={1000} y={yc - 168} textAnchor="middle" fontFamily={MONO4} fontSize="20" fontWeight="700" fill="#fff">GC</text>
      <FlowComputer x={fcx} y={fcy} w={280} h={190} t={t} lines={['Q   52 410 m³/h', 'P   41,23 bar', 'T   24,62 °C', 'ΔP  31,80 kPa']} />
    </g>
    <g opacity={op(2)}>
      <MfSig d={`M${fcx + 280} ${fcy + 95} H1580`} />
      <rect x={1580} y={fcy + 30} width="200" height="130" rx="10" fill="#fff" stroke={FA.ink} strokeWidth="3" />
      <rect x={1596} y={fcy + 46} width="168" height="80" rx="4" fill="#0f2b4a" />
      <path d={`M1606 ${fcy + 110} l30 -20 l30 10 l30 -30 l30 12 l26 -16`} stroke="#7ee2a8" strokeWidth="3" fill="none" />
      <FaText x={1680} y={fcy + 190} size={18} anchor="middle" color={FA.mut} mono>SCADA</FaText>
    </g>
    <g opacity={op(3)}>
      <rect x={fcx + 30} y={fcy + 230} width="220" height="80" rx="8" fill={MF.hlW} stroke={MF.hl} strokeWidth="2.5" strokeDasharray="8 5" />
      <FaText x={fcx + 140} y={fcy + 262} size={18} anchor="middle" color={FA.hi} weight={700} mono>registros</FaText>
      <FaText x={fcx + 140} y={fcy + 290} size={16} anchor="middle" color={FA.hi} mono>hora · día · eventos</FaText>
    </g>
  </g>;
}
// Señal que varía y sus muestras: vals(u) 0..1 → 0..1; n muestras; avg = línea del promedio.
function SampleTrend({ x, y, w = 900, h = 300, prog = 1, f, n = 24, dots = 1, avg = null, avgLabel = '', label = '' }) {
  const N = 240, P = clamp(prog, 0, 1);
  const pts = []; for (let i = 0; i <= N * P; i++) { const u = i / N; pts.push(`${(x + u * w).toFixed(1)} ${(y + h - f(u) * h).toFixed(1)}`); }
  return <g>
    <line x1={x} y1={y + h} x2={x + w + 20} y2={y + h} stroke={FA.ink} strokeWidth="2.5" />
    <line x1={x} y1={y + h} x2={x} y2={y - 10} stroke={FA.ink} strokeWidth="2.5" />
    {label && <FaText x={x + 8} y={y - 18} size={17} color={FA.mut} mono>{label}</FaText>}
    {pts.length > 1 && <path d={'M' + pts.join(' L')} fill="none" stroke={FA.dot} strokeWidth="3" />}
    <g opacity={dots}>{Array.from({ length: n + 1 }, (_, k) => k / n).filter((u) => u <= P).map((u, k) => <circle key={k} cx={x + u * w} cy={y + h - f(u) * h} r="6" fill={FA.hi} stroke="#fff" strokeWidth="2" />)}</g>
    {avg != null && P > 0.98 && <g><line x1={x} y1={y + h - avg * h} x2={x + w} y2={y + h - avg * h} stroke={FA.ok} strokeWidth="3" strokeDasharray="12 6" /><FaText x={x + w + 10} y={y + h - avg * h + 6} size={18} color={FA.ok} weight={700} mono>{avgLabel}</FaText></g>}
  </g>;
}
// Banco de verificación: transmisor de ΔP con bomba manual y patrón.
function CalBench({ x = 260, y = 420, t = 0, val = '50,00', std = '50,02', labels = 1 }) {
  return <g>
    <DPCell x={x + 160} y={y + 150} val={val} tag="FT" unit="kPa" />
    <Tube d={`M${x + 102} ${y + 200} V${y + 300} H${x + 520}`} color={FA.hi} w={6} />
    <Tube d={`M${x + 218} ${y + 200} V${y + 250} H${x + 300}`} color={FA.lo} w={6} />
    <rect x={x + 300} y={y + 240} width="16" height="20" fill={FA.ink} />
    <rect x={x + 520} y={y + 230} width="150" height="120" rx="12" fill={FA.paintL} stroke={FA.ink} strokeWidth="3" />
    <rect x={x + 536} y={y + 246} width="118" height="44" rx="5" fill="#0f2b4a" />
    <text x={x + 595} y={y + 276} textAnchor="middle" fontFamily={MONO4} fontSize="22" fontWeight="700" fill="#7ee2a8">{std}</text>
    <Tube d={`M${x + 595} ${y + 350} V${y + 420} H${x + 740}`} w={6} />
    <rect x={x + 740} y={y + 395} width="160" height="50" rx="10" fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
    <rect x={x + 900} y={y + 410} width={60 + 10 * Math.sin(t * 2)} height="20" fill={FA.steelD} stroke={FA.ink} strokeWidth="2" />
    <g opacity={labels}>
      <FaText x={x + 160} y={y - 20} size={18} anchor="middle" color={FA.mut} mono>transmisor</FaText>
      <FaText x={x + 595} y={y + 220} size={18} anchor="middle" color={FA.mut} mono>patrón</FaText>
      <FaText x={x + 820} y={y + 475} size={18} anchor="middle" color={FA.mut} mono>bomba manual</FaText>
      <FaText x={x + 300} y={y + 230} size={16} anchor="middle" color={FA.lo} mono>L a la atmósfera</FaText>
    </g>
  </g>;
}
// Tabla de registros (HTML): cols = anchos CSS; rows = [[celdas...], at, color]
function LogTable({ t, s, head, rows, cols, x = 200, y = 320, w = 1520, size = 20 }) {
  return (
    <div style={{ position: 'absolute', left: x, top: y, width: w, fontFamily: MONO4, fontSize: size, background: TL4.paper, borderRadius: 12, boxShadow: TL4.shadow, overflow: 'hidden' }}>
      <div style={{ display: 'grid', gridTemplateColumns: cols, gap: 12, padding: '12px 20px', background: '#0f2b4a', color: '#7ee2a8', fontWeight: 700 }}>{head.map((h, i) => <div key={i}>{h}</div>)}</div>
      {rows.map(([cells, at, color], j) => { const p = pop4(t, s + at); return (
        <div key={j} style={{ opacity: p.op, display: 'grid', gridTemplateColumns: cols, gap: 12, padding: '12px 20px', borderTop: `1px solid ${TL4.line || '#dde6f0'}`, color: color || TL4.ink, background: color ? 'rgba(244,138,60,0.10)' : 'transparent' }}>{cells.map((c, i) => <div key={i}>{c}</div>)}</div>); })}
    </div>
  );
}

// ── Módulo 7: medidores y patines de líquidos ──
// Medidor de desplazamiento positivo en corte: engranes ovalados, tubos de entrada y salida,
// registro de pulsos arriba y, con slip > 0, el líquido que se cuela por la holgura sin contarse.
function PdMeter({ cx, cy, t = 0, slip = 0, a = 90, b = 56, speed = 0.35 }) {
  const sp = a + b, xa = cx - sp / 2, xb = cx + sp / 2, top = cy - a - 14;
  const L = 330, xl = xa - a - 14, xr = xb + a + 14;
  const leak = Array.from({ length: 7 }, (_, i) => { const u = ((i / 7 + t * 0.22) % 1); return <circle key={i} cx={xa + (xb - xa) * u} cy={top + 7} r="5" fill={FA.bad} opacity={slip} />; });
  return <g>
    <PipeRun x1={xl - L} x2={xl + 6} yc={cy} R={40} wall={12} />
    <PipeRun x1={xr - 6} x2={xr + L} yc={cy} R={40} wall={12} />
    <MfFlow x1={xl - L} x2={xl} y={cy} t={t} color={MF.oil} n={8} />
    <MfFlow x1={xr} x2={xr + L} y={cy} t={t} color={MF.oil} n={8} />
    <OvalGears cx={cx} cy={cy} a={a} b={b} t={t} speed={speed} />
    <line x1={cx} y1={top - 2} x2={cx} y2={top - 60} stroke={FA.ink} strokeWidth="5" />
    <rect x={cx - 70} y={top - 120} width="140" height="62" rx="10" fill={FA.paintD} stroke={FA.ink} strokeWidth="2.5" />
    <text x={cx} y={top - 81} textAnchor="middle" fontFamily={MONO4} fontSize="20" fill="#fff">pulsos</text>
    {leak}
    <g opacity={slip}><FaText x={cx} y={top - 140} size={20} anchor="middle" color={FA.bad} mono>deslizamiento por la holgura</FaText></g>
  </g>;
}

// Curva del factor K contra el caudal. curves = [{ f: (u) => 0..1, color, label, op }], band = [v0, v1] (0..1).
function KCurve({ x, y, w = 900, h = 380, prog = 1, curves, band = null, bandLabel = '', bandOp = 1, xl = 'caudal (% del máximo)', yl = 'factor K', ticks = ['0', '25', '50', '75', '100'] }) {
  const X = (u) => x + w * u, Y = (v) => y + h - h * v;
  return <g>
    <rect x={x - 30} y={y - 40} width={w + 60} height={h + 120} rx="14" fill="#fff" stroke={FA.trim} strokeWidth="2" />
    {band && <g opacity={bandOp}><rect x={x} y={Y(band[1])} width={w} height={Y(band[0]) - Y(band[1])} fill={MF.hlW} />
      <FaText x={x + w - 10} y={Y(band[1]) - 10} size={18} anchor="end" color={FA.mut} mono>{bandLabel}</FaText></g>}
    <line x1={x} y1={y + h} x2={x + w} y2={y + h} stroke={FA.ink} strokeWidth="2.5" />
    <line x1={x} y1={y} x2={x} y2={y + h} stroke={FA.ink} strokeWidth="2.5" />
    {ticks.map((tk, i) => <FaText key={i} x={X(i / (ticks.length - 1))} y={y + h + 30} size={16} anchor="middle" color={FA.mut} mono>{tk}</FaText>)}
    <FaText x={x + w / 2} y={y + h + 62} size={18} anchor="middle" color={FA.mut}>{xl}</FaText>
    <FaText x={x + 12} y={y - 12} size={18} color={FA.mut}>{yl}</FaText>
    {curves.map((c, k) => { const N = 60; const pts = []; for (let i = 0; i <= N; i++) { const u = 0.04 + 0.96 * i / N; if (u > prog) break; pts.push(`${X(u).toFixed(1)} ${Y(Math.max(0.02, Math.min(1, c.f(u)))).toFixed(1)}`); }
      return <g key={k} opacity={c.op === undefined ? 1 : c.op}>{pts.length > 1 && <path d={'M' + pts.join(' L')} fill="none" stroke={c.color || FA.hi} strokeWidth="4" />}
        {c.label && <FaText x={X(0.97)} y={Y(c.f(0.97)) + (c.dy || -14)} size={18} anchor="end" color={c.color || FA.hi} weight={700}>{c.label}</FaText>}</g>; })}
  </g>;
}

// Trenes de medición en paralelo entre dos cabezales. on = [1,1,0,...] abre o cierra cada tren.
function MeterBank({ t = 0, on = [1, 1, 1, 0], x1 = 160, x2 = 1760, y0 = 380, gap = 120, label = (i) => `FQ-${i + 1}` }) {
  const n = on.length, ya = y0, yb = y0 + (n - 1) * gap, ym = (ya + yb) / 2, hx1 = x1 + 140, hx2 = x2 - 140;
  return <g>
    <MfPipe d={`M${x1} ${ym} H${hx1}`} fill={MF.oilL} /><MfPipe d={`M${hx2} ${ym} H${x2}`} fill={MF.oilL} />
    <MfPipe d={`M${hx1} ${ya} V${yb}`} fill={MF.oilL} /><MfPipe d={`M${hx2} ${ya} V${yb}`} fill={MF.oilL} />
    <MfFlow x1={x1} x2={hx1} y={ym} t={t} color={MF.oil} n={4} /><MfFlow x1={hx2} x2={x2} y={ym} t={t} color={MF.oil} n={4} />
    {on.map((o, i) => { const y = ya + i * gap, xm = (hx1 + hx2) / 2; return <g key={i}>
      <MfPipe d={`M${hx1} ${y} H${hx2}`} w={18} fill={o ? MF.oilL : '#eef2f7'} />
      {o ? <MfFlow x1={hx1 + 10} x2={hx2 - 10} y={y} t={t} color={MF.oil} n={18} /> : null}
      <MfValve x={hx1 + 110} y={y} s={14} open={!!o} /><MfValve x={hx2 - 110} y={y} s={14} open={!!o} />
      <rect x={xm - 90} y={y - 32} width="180" height="64" rx="12" fill={o ? FA.paint : FA.steel} stroke={FA.ink} strokeWidth="2.5" />
      <FaText x={xm} y={y + 7} size={20} anchor="middle" color="#fff" mono>{label(i)}</FaText>
      {!o && <FaText x={xm + 120} y={y + 7} size={18} color={FA.mut} mono>reserva</FaText>}
    </g>; })}
  </g>;
}

// Válvula de doble bloqueo y purga en corte: dos asientos, cámara entre ellos y purga con visor.
// leak 0..1: gotas en el visor (un asiento no sella).
function DbbValve({ cx, cy, t = 0, leak = 0, R = 60 }) {
  const drops = Array.from({ length: 3 }, (_, i) => { const u = ((t * 0.6 + i / 3) % 1); return <circle key={i} cx={cx} cy={cy + R + 130 + u * 60} r="7" fill={MF.oil} opacity={leak * (1 - u)} />; });
  return <g>
    <PipeRun x1={cx - 560} x2={cx - 110} yc={cy} R={R} wall={14} />
    <PipeRun x1={cx + 110} x2={cx + 560} yc={cy} R={R} wall={14} fill={'#eef2f7'} />
    <MfFlow x1={cx - 560} x2={cx - 120} y={cy} t={t} color={MF.oil} n={12} />
    <rect x={cx - 120} y={cy - R - 60} width="240" height={2 * R + 120} rx="26" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="3" />
    <rect x={cx - 80} y={cy - R} width="160" height={2 * R} fill={leak ? MF.oilL : '#eef2f7'} stroke={FA.ink} strokeWidth="2" />
    {[-1, 1].map((k) => <rect key={k} x={cx + k * 70 - 10} y={cy - R - 10} width="20" height={2 * R + 20} rx="6" fill={FA.paintD} stroke={FA.ink} strokeWidth="2" />)}
    <line x1={cx} y1={cy + R} x2={cx} y2={cy + R + 110} stroke={FA.ink} strokeWidth="6" />
    <MfValve x={cx} y={cy + R + 60} s={12} />
    <path d={`M${cx - 34} ${cy + R + 110} h68 l-14 90 h-40 z`} fill="#eaf5ff" stroke={FA.ink} strokeWidth="2.5" />
    {drops}
    <FaText x={cx - 70} y={cy - R - 80} size={18} anchor="middle" color={FA.mut} mono>asiento 1</FaText>
    <FaText x={cx + 70} y={cy - R - 80} size={18} anchor="middle" color={FA.mut} mono>asiento 2</FaText>
    <FaText x={cx + 60} y={cy + R + 230} size={18} color={FA.mut} mono>purga con visor</FaText>
  </g>;
}

Object.assign(window, { TurbineRun, UsmRun, MfEq, MfSteps, MfBars });
Object.assign(window, { EfmChain, SampleTrend, CalBench, LogTable });
Object.assign(window, { PdMeter, KCurve, MeterBank, DbbValve });
Object.assign(window, { HeatTrace, PistonCylinder, SampleProbeRun, Chromatogram, GcBox, AnalyzerShelter });
Object.assign(window, { MF, MfHalo, MfPipe, MfFlow, MfValve, MfTx, MfSig, FlowComputer, CustodyChain, GasStation, LactSkid, TargetMf, TankGauge, OrificeRun });
