// temp-art.jsx — Dibujos en corte de sensores de temperatura (Módulo 3 · Tecnologías de medición)
// Va después de flujo-art.jsx (usa FA, FlowDefs, PipeRun, FaText, VDim…). Coordenadas 1920 × 1080.
// Cada <svg> que use estas piezas debe incluir <FlowDefs /> y <TempDefs /> una vez.

const TA = {
  hot: '#e4572e', hotD: '#b23a1a', hotL: '#ffc2a3', cold: '#2f86e6', coldL: '#b7d8fb',
  cu: '#c87533', cuL: '#e8a46a', ni: '#7d8b99', pt: '#aab4be', mgo: '#f4ecd8', cer: '#efe6d6', cerD: '#cbbfa8',
  glass: '#eaf4fb', glassD: '#9cc3e0', red: '#d7263d', fib: '#f2b134',
};
const taMix = (k) => { // 0 = frío (azul) · 1 = caliente (rojo)
  const c0 = [47, 134, 230], c1 = [228, 87, 46], q = Math.max(0, Math.min(1, k));
  return `rgb(${c0.map((v, i) => Math.round(v + (c1[i] - v) * q)).join(',')})`;
};

function TempDefs() {
  return (
    <defs>
      <radialGradient id="taGlow"><stop offset="0" stopColor="#ffb38a" stopOpacity="0.95" /><stop offset="1" stopColor="#ffb38a" stopOpacity="0" /></radialGradient>
      <linearGradient id="taSheath" x1="0" x2="0" y1="0" y2="1"><stop offset="0" stopColor="#eef2f6" /><stop offset="0.5" stopColor="#b9c4cf" /><stop offset="1" stopColor="#e3e9ef" /></linearGradient>
      <linearGradient id="taSheathV" x1="0" x2="1" y1="0" y2="0"><stop offset="0" stopColor="#e3e9ef" /><stop offset="0.5" stopColor="#b9c4cf" /><stop offset="1" stopColor="#eef2f6" /></linearGradient>
      <linearGradient id="taHotRod" x1="0" x2="1"><stop offset="0" stopColor="#e4572e" /><stop offset="1" stopColor="#2f86e6" /></linearGradient>
      <pattern id="taMgO" width="9" height="9" patternUnits="userSpaceOnUse"><rect width="9" height="9" fill={TA.mgo} /><circle cx="2.5" cy="2.5" r="1.1" fill="#cdbf9c" /><circle cx="7" cy="6.5" r="0.9" fill="#d9cdae" /></pattern>
      <pattern id="taCer" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(30)"><rect width="10" height="10" fill={TA.cer} /><line x1="0" y1="0" x2="0" y2="10" stroke={TA.cerD} strokeWidth="1.4" /></pattern>
      <marker id="taArrHot" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="5" markerHeight="5" orient="auto-start-reverse"><path d="M0 0 L10 5 L0 10 z" fill={TA.hot} /></marker>
      <marker id="taArrCold" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="5" markerHeight="5" orient="auto-start-reverse"><path d="M0 0 L10 5 L0 10 z" fill={TA.cold} /></marker>
    </defs>
  );
}

// ── Moléculas que vibran más cuanto más caliente ────────────────────────────
// k: 0 (frío) a 1 (caliente). Rejilla de partículas con agitación proporcional.
function Molecules({ x, y, w, h, k = 0.5, t = 0, cols = 9, rows = 6, r = 9, seed = 0 }) {
  const els = [];
  for (let i = 0; i < cols; i++) for (let j = 0; j < rows; j++) {
    const n = i * 31 + j * 17 + seed;
    const amp = 3 + 22 * k;
    const sp = 2 + 9 * k;
    const dx = amp * Math.sin(t * sp + n * 1.37) * Math.cos(t * sp * 0.61 + n);
    const dy = amp * Math.cos(t * sp * 0.83 + n * 2.11) * Math.sin(t * sp * 0.47 + n * 0.3);
    els.push(<circle key={`${i}-${j}`} cx={x + (i + 0.5) * w / cols + dx} cy={y + (j + 0.5) * h / rows + dy} r={r} fill={taMix(k)} opacity="0.85" />);
  }
  return <g>{els}</g>;
}

// ── Termómetro de vidrio ────────────────────────────────────────────────────
function GlassThermo({ x, yTop, h = 420, frac = 0.5, w = 34, ticks = 10, color = TA.red }) {
  const yb = yTop + h, rB = w * 0.95, inner = w * 0.36;
  const lvl = yb - 10 - (h - 30) * Math.max(0, Math.min(1, frac));
  return (
    <g>
      <rect x={x - w / 2} y={yTop} width={w} height={h} rx={w / 2} fill={TA.glass} stroke={FA.ink} strokeWidth="3" />
      <circle cx={x} cy={yb + rB * 0.55} r={rB} fill={TA.glass} stroke={FA.ink} strokeWidth="3" />
      <rect x={x - w / 2 + 3} y={yb - 20} width={w - 6} height="30" fill={TA.glass} />
      <circle cx={x} cy={yb + rB * 0.55} r={rB - 9} fill={color} />
      <rect x={x - inner / 2} y={lvl} width={inner} height={yb + rB * 0.55 - lvl} fill={color} />
      {Array.from({ length: ticks + 1 }, (_, i) => { const yy = yb - 10 - (h - 30) * i / ticks; return <line key={i} x1={x + w / 2} y1={yy} x2={x + w / 2 + (i % 5 ? 10 : 18)} y2={yy} stroke={FA.ink} strokeWidth="2" />; })}
    </g>
  );
}

// ── Transferencia de calor ──────────────────────────────────────────────────
function ConductionArt({ cx, cy, t = 0 }) {
  const x1 = cx - 180, x2 = cx + 180;
  const pulses = Array.from({ length: 5 }, (_, i) => { const p = ((t * 0.35 + i / 5) % 1); return <circle key={i} cx={x1 + 40 + (x2 - x1 - 60) * p} cy={cy} r={9} fill={TA.hot} opacity={0.9 * (1 - p)} />; });
  return (
    <g>
      <rect x={x1} y={cy - 26} width={x2 - x1} height="52" rx="8" fill="url(#taHotRod)" stroke={FA.ink} strokeWidth="2.5" />
      {pulses}
      <path d={`M${x1 - 10} ${cy + 70} q12 -22 0 -44 q-12 -22 0 -44`} fill="none" stroke={TA.hot} strokeWidth="5" strokeLinecap="round" />
      <path d={`M${x1 + 20} ${cy + 70} q12 -22 0 -44 q-12 -22 0 -44`} fill="none" stroke={TA.hot} strokeWidth="5" strokeLinecap="round" opacity="0.7" />
      <ellipse cx={x1} cy={cy + 82} rx="44" ry="12" fill={TA.hot} opacity="0.35" />
    </g>
  );
}
function ConvectionArt({ cx, cy, t = 0 }) {
  const loop = (sx) => {
    const pts = Array.from({ length: 6 }, (_, i) => { const a = 2 * Math.PI * (((t * 0.25) + i / 6) % 1); return <circle key={i} cx={cx + sx * (70 + 55 * Math.cos(a))} cy={cy + 10 - 70 * Math.sin(a)} r="8" fill={taMix(0.5 - 0.5 * Math.sin(a))} />; });
    return <g>{pts}<ellipse cx={cx + sx * 70} cy={cy + 10} rx="55" ry="70" fill="none" stroke={FA.mut} strokeWidth="2" strokeDasharray="6 6" /></g>;
  };
  return (
    <g>
      <path d={`M${cx - 170} ${cy - 90} L${cx - 170} ${cy + 100} L${cx + 170} ${cy + 100} L${cx + 170} ${cy - 90}`} fill={TA.coldL} fillOpacity="0.4" stroke={FA.ink} strokeWidth="4" />
      {loop(-1)}{loop(1)}
      <rect x={cx - 120} y={cy + 106} width="240" height="16" rx="4" fill={TA.hot} />
      {[-80, -30, 20, 70].map((d, i) => <path key={i} d={`M${cx + d} ${cy + 150} q10 -10 0 -20`} fill="none" stroke={TA.hot} strokeWidth="4" opacity={0.5 + 0.5 * Math.sin(t * 6 + i)} />)}
    </g>
  );
}
function RadiationArt({ cx, cy, t = 0 }) {
  const waves = [-50, 0, 50].map((dy, i) => {
    const pts = Array.from({ length: 41 }, (_, k) => { const x = cx - 90 + 220 * k / 40; return `${x.toFixed(1)} ${(cy + dy + 10 * Math.sin(k / 3 - t * 8 + i)).toFixed(1)}`; });
    return <path key={i} d={'M' + pts.join(' L')} fill="none" stroke={TA.hot} strokeWidth="3.5" markerEnd="url(#taArrHot)" />;
  });
  return (
    <g>
      <circle cx={cx - 150} cy={cy} r="80" fill="url(#taGlow)" />
      <circle cx={cx - 150} cy={cy} r="44" fill={TA.hot} stroke={TA.hotD} strokeWidth="3" />
      {waves}
      <rect x={cx + 150} y={cy - 80} width="24" height="160" rx="4" fill={FA.steel} stroke={FA.ink} strokeWidth="2.5" />
    </g>
  );
}

// ── Respuesta de primer orden ───────────────────────────────────────────────
// Curvas T(t) para un escalón de T0 a T1 con constantes de tiempo taus (s).
function StepResponse({ x, y, w, h, tmax = 200, T0 = 20, T1 = 100, taus = [8, 40], colors = [TA.cold, TA.hot], prog = 1, marks = true }) {
  const px = (s) => x + w * s / tmax, py = (T) => y + h - h * (T - T0) / (T1 - T0) * 0.86;
  const curve = (tau) => 'M' + Array.from({ length: 121 }, (_, i) => { const s = tmax * prog * i / 120; return `${px(s).toFixed(1)} ${py(T0 + (T1 - T0) * (1 - Math.exp(-s / tau))).toFixed(1)}`; }).join(' L');
  return (
    <g>
      <line x1={x} y1={y + h} x2={x + w + 16} y2={y + h} stroke={FA.ink} strokeWidth="2.5" markerEnd="url(#faArr)" />
      <line x1={x} y1={y + h} x2={x} y2={y - 16} stroke={FA.ink} strokeWidth="2.5" markerEnd="url(#faArr)" />
      <path d={`M${x} ${py(T1)} L${x + w} ${py(T1)}`} stroke={FA.mut} strokeWidth="2" strokeDasharray="8 6" />
      <path d={`M${x} ${py(T0)} L${x + 6} ${py(T0)} L${x + 6} ${py(T1)} L${x + w} ${py(T1)}`} fill="none" stroke={FA.ink} strokeWidth="2.5" opacity="0.45" />
      {prog > 0 && taus.map((tau, i) => <path key={tau} d={curve(tau)} fill="none" stroke={colors[i]} strokeWidth="5" />)}
      {marks && prog > 0.3 && taus.map((tau, i) => { const T63 = T0 + 0.632 * (T1 - T0); return <g key={`m${tau}`}><line x1={px(tau)} y1={y + h} x2={px(tau)} y2={py(T63)} stroke={colors[i]} strokeWidth="2" strokeDasharray="5 5" /><circle cx={px(tau)} cy={py(T63)} r="7" fill={colors[i]} /></g>; })}
    </g>
  );
}

// ── Termopar: alambres y junta ──────────────────────────────────────────────
// Dos alambres horizontales de x1 a xj, unidos en la junta (xj, y).
function TCPair({ x1, xj, y, gap = 70, cA = TA.hot, cB = TA.cold, w = 10, glow = 0, t = 0, label }) {
  return (
    <g>
      {glow > 0 && <circle cx={xj + 18} cy={y} r={60 + 8 * Math.sin(t * 4)} fill="url(#taGlow)" opacity={glow} />}
      <path d={`M${x1} ${y - gap / 2} H${xj - 10} Q${xj + 26} ${y - gap / 2} ${xj + 22} ${y}`} fill="none" stroke={cA} strokeWidth={w} strokeLinecap="round" />
      <path d={`M${x1} ${y + gap / 2} H${xj - 10} Q${xj + 26} ${y + gap / 2} ${xj + 22} ${y}`} fill="none" stroke={cB} strokeWidth={w} strokeLinecap="round" />
      <circle cx={xj + 22} cy={y} r={w * 1.1} fill={FA.ink} />
      {label}
    </g>
  );
}

// ── Sonda con aislamiento mineral (MI) en corte ─────────────────────────────
// kind: 'grounded' (junta soldada a la vaina), 'ungrounded' (aislada), 'exposed' (expuesta).
function MIProbe({ x1, x2, yc, r = 40, kind = 'ungrounded', cA = TA.hot, cB = TA.cold }) {
  const tip = kind === 'exposed' ? x2 - 40 : x2;
  const wy = r * 0.34, jx = kind === 'grounded' ? tip - 8 : kind === 'exposed' ? x2 + 10 : tip - r * 0.9;
  return (
    <g>
      <path d={`M${x1} ${yc - r} H${tip - r} A${r} ${r} 0 0 1 ${tip - r} ${yc + r} H${x1} Z`} fill="url(#taSheath)" stroke={FA.ink} strokeWidth="3" />
      <path d={`M${x1} ${yc - r + 7} H${tip - r} A${r - 7} ${r - 7} 0 0 1 ${tip - r} ${yc + r - 7} H${x1} Z`} fill="url(#taMgO)" stroke={FA.steelD} strokeWidth="1.5" />
      {kind === 'exposed' && <rect x={tip - r - 2} y={yc - r + 7} width={r + 4} height={2 * r - 14} fill="url(#taMgO)" />}
      <path d={`M${x1} ${yc - wy} H${jx - 14} Q${jx} ${yc - wy} ${jx} ${yc}`} fill="none" stroke={cA} strokeWidth="5" />
      <path d={`M${x1} ${yc + wy} H${jx - 14} Q${jx} ${yc + wy} ${jx} ${yc}`} fill="none" stroke={cB} strokeWidth="5" />
      <circle cx={jx} cy={yc} r="7" fill={FA.ink} />
      {kind === 'grounded' && <rect x={jx - 4} y={yc - 5} width={tip - jx + 2} height="10" fill={FA.ink} />}
      <line x1={x1} y1={yc - r - 4} x2={x1} y2={yc + r + 4} stroke={FA.ink} strokeWidth="2" strokeDasharray="4 4" />
    </g>
  );
}

// ── RTD de alambre bobinado y de película delgada (en corte) ────────────────
function RTDWire({ x, y, w = 360, h = 90, t = 0, heat = 0 }) {
  const coil = [];
  const n = 26;
  for (let k = 0; k < 2; k++) {
    const yy = y + h * (k ? 0.68 : 0.32);
    const pts = Array.from({ length: n * 8 + 1 }, (_, i) => { const xx = x + 40 + (w - 80) * i / (n * 8); return `${xx.toFixed(1)} ${(yy + 11 * Math.sin(i / 8 * 2 * Math.PI)).toFixed(1)}`; });
    coil.push(<path key={k} d={'M' + pts.join(' L')} fill="none" stroke={heat > 0 ? taMix(heat) : TA.pt} strokeWidth="3" />);
  }
  return (
    <g>
      <rect x={x} y={y} width={w} height={h} rx={h / 2} fill="url(#taCer)" stroke={FA.ink} strokeWidth="3" />
      <rect x={x + 30} y={y + h * 0.18} width={w - 60} height={h * 0.28} rx="6" fill="#fff" opacity="0.7" />
      <rect x={x + 30} y={y + h * 0.54} width={w - 60} height={h * 0.28} rx="6" fill="#fff" opacity="0.7" />
      {coil}
      <path d={`M${x + w - 40} ${y + h * 0.32} Q${x + w - 14} ${y + h * 0.5} ${x + w - 40} ${y + h * 0.68}`} fill="none" stroke={TA.pt} strokeWidth="3" />
      <line x1={x + 40} y1={y + h * 0.32} x2={x - 70} y2={y + h * 0.32} stroke={TA.cu} strokeWidth="5" />
      <line x1={x + 40} y1={y + h * 0.68} x2={x - 70} y2={y + h * 0.68} stroke={TA.cu} strokeWidth="5" />
    </g>
  );
}
function RTDFilm({ x, y, w = 300, h = 140, heat = 0 }) {
  const m = 9, pts = [];
  for (let i = 0; i <= m; i++) { const xx = x + 40 + (w - 80) * i / m; pts.push(i % 2 ? `${xx} ${y + h - 34} L${xx} ${y + 30}` : `${xx} ${y + 30} L${xx} ${y + h - 34}`); }
  return (
    <g>
      <rect x={x} y={y} width={w} height={h} rx="8" fill="#e9e4da" stroke={FA.ink} strokeWidth="3" />
      <path d={`M${x + 40} ${y + h - 10} L` + pts.join(' L') + ` L${x + w - 40} ${y + h - 10}`} fill="none" stroke={heat > 0 ? taMix(heat) : '#8e99a5'} strokeWidth="5" strokeLinejoin="round" />
      <rect x={x + 22} y={y + h - 16} width="36" height="22" fill={TA.cuL} stroke={FA.ink} strokeWidth="2" />
      <rect x={x + w - 58} y={y + h - 16} width="36" height="22" fill={TA.cuL} stroke={FA.ink} strokeWidth="2" />
      <line x1={x + 40} y1={y + h + 6} x2={x + 40} y2={y + h + 70} stroke={TA.cu} strokeWidth="5" />
      <line x1={x + w - 40} y1={y + h + 6} x2={x + w - 40} y2={y + h + 70} stroke={TA.cu} strokeWidth="5" />
    </g>
  );
}

// ── Termopozo en corte (vertical), cabezal y transmisor ─────────────────────
// La brida queda en yTop; la punta en yTip. d = diámetro exterior en la raíz, dt en la punta.
function Thermowell({ x, yTop, yTip, d = 60, dt = 40, bore = 18, sensor = true, heat = 0, flange = true }) {
  const half = (yy) => faLerp(d / 2, dt / 2, (yy - yTop) / (yTip - yTop));
  const outer = [[x - d / 2, yTop], [x - dt / 2, yTip - dt / 2], [x + dt / 2, yTip - dt / 2], [x + d / 2, yTop]];
  return (
    <g>
      <path d={`M${x - d / 2} ${yTop} L${x - dt / 2} ${yTip - dt / 2} A${dt / 2} ${dt / 2} 0 0 0 ${x + dt / 2} ${yTip - dt / 2} L${x + d / 2} ${yTop} Z`} fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2.5" />
      <rect x={x - bore / 2} y={yTop - 2} width={bore} height={yTip - yTop - dt / 2 + 4} rx={bore / 2} fill="#fff" stroke={FA.ink} strokeWidth="1.8" />
      {sensor && <rect x={x - bore / 2 + 3} y={yTop - 60} width={bore - 6} height={yTip - yTop - dt / 2 + 58} rx={(bore - 6) / 2} fill="url(#taSheathV)" stroke={FA.steelD} strokeWidth="1.5" />}
      {sensor && heat > 0 && <circle cx={x} cy={yTip - dt / 2 - 6} r={bore * 0.9} fill="url(#taGlow)" opacity={heat} />}
      {flange && <rect x={x - d / 2 - 46} y={yTop - 26} width={d + 92} height="26" rx="4" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2.5" />}
      {outer && null}
    </g>
  );
}
function TempHead({ x, y, w = 130, h = 92, tx = true, open = false }) {
  return (
    <g>
      <rect x={x - 22} y={y + h - 4} width="44" height="64" fill="url(#taSheathV)" stroke={FA.ink} strokeWidth="2.5" />
      <path d={`M${x - w / 2} ${y + h} L${x - w / 2} ${y + 24} Q${x - w / 2} ${y} ${x - w / 2 + 26} ${y} L${x + w / 2 - 26} ${y} Q${x + w / 2} ${y} ${x + w / 2} ${y + 24} L${x + w / 2} ${y + h} Z`} fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      {open && <rect x={x - w / 2 + 12} y={y + 14} width={w - 24} height={h - 24} rx="10" fill="#f4f7fa" stroke={FA.ink} strokeWidth="2" />}
      {tx && <g><circle cx={x} cy={y + h / 2 + 4} r={h * 0.32} fill="#fff" stroke={FA.ink} strokeWidth="2.5" /><text x={x} y={y + h / 2 + 10} textAnchor="middle" fontFamily={MONO4} fontSize="15" fontWeight="700" fill={FA.ink}>TT</text></g>}
      <rect x={x + w / 2} y={y + h / 2 - 8} width="30" height="20" fill={FA.steelD} stroke={FA.ink} strokeWidth="2" />
    </g>
  );
}

// ── Termómetro bimetálico ───────────────────────────────────────────────────
// Lámina de dos metales empotrada en x; se curva hacia arriba con bend (0 a 1).
function BimetalStrip({ x, y, L = 420, th = 14, bend = 0 }) {
  const R = bend > 0.02 ? L / (bend * 1.6) : 1e6;
  const pts = (off) => Array.from({ length: 41 }, (_, i) => { const s = L * i / 40, a = s / R; return [x + (R - off) * Math.sin(a), y - (R - off) * (1 - Math.cos(a)) + off * 0]; });
  const top = pts(-th), mid = pts(0), bot = pts(th);
  const P = (arr) => arr.map((p) => `${p[0].toFixed(1)} ${p[1].toFixed(1)}`).join(' L');
  return (
    <g>
      <path d={`M${P(top)} L${P(mid.slice().reverse())} Z`} fill={TA.cuL} stroke={FA.ink} strokeWidth="2" />
      <path d={`M${P(mid)} L${P(bot.slice().reverse())} Z`} fill={TA.pt} stroke={FA.ink} strokeWidth="2" />
      <rect x={x - 46} y={y - 40} width="46" height="80" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2.5" />
    </g>
  );
}
function DialGauge({ cx, cy, r = 110, val = 0.5, label = '°C', ticks = 10, min = 0, max = 200 }) {
  const a0 = Math.PI * 0.75, a1 = Math.PI * 2.25, a = a0 + (a1 - a0) * Math.max(0, Math.min(1, val));
  return (
    <g>
      <circle cx={cx} cy={cy} r={r + 12} fill={FA.steel} stroke={FA.ink} strokeWidth="3" />
      <circle cx={cx} cy={cy} r={r} fill="#fff" stroke={FA.ink} strokeWidth="2" />
      {Array.from({ length: ticks + 1 }, (_, i) => { const th = a0 + (a1 - a0) * i / ticks; return <g key={i}><line x1={cx + (r - 14) * Math.cos(th)} y1={cy + (r - 14) * Math.sin(th)} x2={cx + (r - 2) * Math.cos(th)} y2={cy + (r - 2) * Math.sin(th)} stroke={FA.ink} strokeWidth={i % 5 ? 2 : 3.5} />{i % 5 === 0 && <text x={cx + (r - 34) * Math.cos(th)} y={cy + (r - 34) * Math.sin(th) + 6} textAnchor="middle" fontFamily={MONO4} fontSize={r * 0.14} fill={FA.ink}>{Math.round(min + (max - min) * i / ticks)}</text>}</g>; })}
      <line x1={cx} y1={cy} x2={cx + (r - 22) * Math.cos(a)} y2={cy + (r - 22) * Math.sin(a)} stroke={TA.red} strokeWidth="5" strokeLinecap="round" />
      <circle cx={cx} cy={cy} r="9" fill={FA.ink} />
      <text x={cx} y={cy + r * 0.5} textAnchor="middle" fontFamily={MONO4} fontSize={r * 0.15} fill={FA.mut}>{label}</text>
    </g>
  );
}
// Hélice bimetálica dentro del vástago (corte), para el termómetro de carátula.
function BimetalHelix({ x, yTop, yBot, w = 40, turns = 7, k = 0 }) {
  const pts = Array.from({ length: turns * 24 + 1 }, (_, i) => { const u = i / (turns * 24); return `${(x + (w / 2 - 8) * Math.sin(u * turns * 2 * Math.PI + k * 3)).toFixed(1)} ${(yTop + 20 + (yBot - yTop - 40) * u).toFixed(1)}`; });
  return (
    <g>
      <rect x={x - w / 2} y={yTop} width={w} height={yBot - yTop} rx="6" fill="url(#taSheathV)" stroke={FA.ink} strokeWidth="2.5" />
      <rect x={x - w / 2 + 5} y={yTop + 6} width={w - 10} height={yBot - yTop - 12} rx="4" fill="#fbfcfd" />
      <path d={'M' + pts.join(' L')} fill="none" stroke={TA.cu} strokeWidth="4" />
      <line x1={x} y1={yTop - 10} x2={x} y2={yBot - 16} stroke={FA.ink} strokeWidth="2" strokeDasharray="3 4" />
    </g>
  );
}
// Sistema lleno: bulbo, capilar y tubo Bourdon con carátula.
function FilledSystem({ xb, yb, xg, yg, r = 100, k = 0.5, fill = TA.cold }) {
  const cap = `M${xb} ${yb - 80} L${xb} ${yb - 140} C${xb} ${yg + 200} ${xg} ${yg + 260} ${xg} ${yg + r + 20}`;
  return (
    <g>
      <rect x={xb - 26} y={yb - 80} width="52" height="200" rx="26" fill={fill} fillOpacity="0.35" stroke={FA.ink} strokeWidth="3" />
      <path d={cap} fill="none" stroke={FA.ink} strokeWidth="7" />
      <path d={cap} fill="none" stroke={fill} strokeWidth="3" />
      <DialGauge cx={xg} cy={yg} r={r} val={k} />
    </g>
  );
}

// ── Infrarrojo ──────────────────────────────────────────────────────────────
function IRGun({ x, y, s = 1, flip = false }) {
  const sx = flip ? -s : s;
  return (
    <g transform={`translate(${x} ${y}) scale(${sx} ${s})`}>
      <path d="M0 0 L190 0 Q210 0 210 20 L210 58 Q210 78 190 78 L118 78 L92 190 Q88 204 72 204 L46 204 Q32 204 36 190 L58 78 L0 78 Z" fill={FA.paint} stroke={FA.ink} strokeWidth="4" strokeLinejoin="round" />
      <rect x="196" y="18" width="26" height="42" rx="6" fill={FA.ink} />
      <rect x="20" y="16" width="70" height="40" rx="6" fill="#0f2b4a" /><text x="55" y="44" textAnchor="middle" fontFamily={MONO4} fontSize="18" fill="#9ef0c4" fontWeight="700">IR</text>
      <path d="M82 92 L100 92 L96 124 L80 124 Z" fill={FA.paintD} />
    </g>
  );
}
// Cono de visión: del lente (x1, y) hacia el objetivo a una distancia; diámetro del punto = dist / DS.
function SpotCone({ x1, y, x2, ds = 12, lens = 30, color = TA.hot, op = 0.18 }) {
  const D = (x2 - x1) / ds;
  return (
    <g>
      <path d={`M${x1} ${y - lens / 2} L${x2} ${y - D / 2} L${x2} ${y + D / 2} L${x1} ${y + lens / 2} Z`} fill={color} opacity={op} />
      <line x1={x1} y1={y - lens / 2} x2={x2} y2={y - D / 2} stroke={color} strokeWidth="2" strokeDasharray="7 6" />
      <line x1={x1} y1={y + lens / 2} x2={x2} y2={y + D / 2} stroke={color} strokeWidth="2" strokeDasharray="7 6" />
      <ellipse cx={x2} cy={y} rx="8" ry={D / 2} fill={color} opacity="0.7" />
    </g>
  );
}
// Curvas de Planck (cuerpo negro) normalizadas a la más caliente. lam en µm.
const taPlanck = (lam, T) => { const c2 = 14388; return 1 / (lam ** 5 * (Math.exp(c2 / (lam * T)) - 1)); };
function PlanckPlot({ x, y, w, h, temps = [600, 900, 1200], lmax = 10, prog = 1, colors }) {
  const cs = colors || temps.map((_, i) => taMix(i / Math.max(1, temps.length - 1)));
  const top = Math.max(...temps.map((T) => taPlanck(2898 / T, T)));
  const px = (l) => x + w * l / lmax, py = (v) => y + h - h * 0.92 * v / top;
  return (
    <g>
      <line x1={x} y1={y + h} x2={x + w + 16} y2={y + h} stroke={FA.ink} strokeWidth="2.5" markerEnd="url(#faArr)" />
      <line x1={x} y1={y + h} x2={x} y2={y - 16} stroke={FA.ink} strokeWidth="2.5" markerEnd="url(#faArr)" />
      {temps.map((T, i) => { const pts = Array.from({ length: 121 }, (_, k) => { const l = 0.2 + (lmax - 0.2) * prog * k / 120; return `${px(l).toFixed(1)} ${py(taPlanck(l, T)).toFixed(1)}`; }); return <path key={T} d={'M' + pts.join(' L')} fill="none" stroke={cs[i]} strokeWidth="4.5" />; })}
    </g>
  );
}

// ── Fibra óptica con DTS ────────────────────────────────────────────────────
// Pulso de láser que recorre la fibra; el eco Raman vuelve. hot = [x0, x1] tramo caliente.
function FiberDTS({ x1, x2, y, t = 0, period = 3, hot = null }) {
  const p = ((t / period) % 1), xp = x1 + (x2 - x1) * p;
  return (
    <g>
      {hot && <rect x={hot[0]} y={y - 40} width={hot[1] - hot[0]} height="80" rx="10" fill={TA.hot} opacity="0.14" />}
      <line x1={x1} y1={y} x2={x2} y2={y} stroke={FA.ink} strokeWidth="9" strokeLinecap="round" />
      <line x1={x1} y1={y} x2={x2} y2={y} stroke={TA.fib} strokeWidth="4" strokeLinecap="round" />
      <circle cx={xp} cy={y} r="10" fill="#fff" stroke={TA.hot} strokeWidth="4" />
      {Array.from({ length: 6 }, (_, i) => { const q = ((p - i * 0.04) + 1) % 1; const xe = x1 + (x2 - x1) * q * (1 - i * 0.03); return <circle key={i} cx={xe - 30 * i} cy={y - 18} r={4} fill={hot && xe > hot[0] && xe < hot[1] ? TA.hot : TA.cold} opacity={0.75 - i * 0.1} />; })}
    </g>
  );
}

Object.assign(window, {
  TA, taMix, TempDefs, Molecules, GlassThermo, ConductionArt, ConvectionArt, RadiationArt, StepResponse,
  TCPair, MIProbe, RTDWire, RTDFilm, Thermowell, TempHead, BimetalStrip, DialGauge, BimetalHelix, FilledSystem,
  IRGun, SpotCone, taPlanck, PlanckPlot, FiberDTS,
});

// ── Piezas de escena compartidas por las clases 3.11 a 3.14 ─────────────────
const SVG_T = { position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' };
function NoteT({ op = 1, children, bg, accent, size = 20 }) {
  const b = bg || TL4.paper;
  return <div style={{ opacity: op, background: b, borderRadius: 14, padding: '14px 22px', boxShadow: b === TL4.paper ? TL4.shadowSm : 'none', borderLeft: accent ? `5px solid ${accent}` : 'none', fontFamily: DISP4, fontSize: size, color: TL4.ink, lineHeight: 1.42 }}>{children}</div>;
}
const EqT = ({ children, size = 30 }) => <div style={{ fontFamily: MONO4, fontSize: size, color: TL4.orangeD, fontWeight: 600 }}>{children}</div>;
const SubT = ({ children }) => <div style={{ fontSize: 18, color: TL4.mut, marginTop: 2 }}>{children}</div>;
// Tarjetas con dibujo. items = [titulo, texto, momento]; art(i, cx, cy). cols = tarjetas por fila.
function CardsT({ t, s, items, art, x0 = 150, y0 = 290, W = 520, H = 520, gap = 20, artY = 150, textY = 330, cols = 3, rowGap = 20 }) {
  const P = (i) => ({ x: x0 + (i % cols) * (W + gap), y: y0 + Math.floor(i / cols) * (H + rowGap) });
  return (
    <>
      {items.map((it, i) => <div key={i} style={{ position: 'absolute', left: P(i).x, top: P(i).y, width: W, height: H, opacity: pop4(t, s + it[2]).op, background: TL4.paper, borderRadius: 14, boxShadow: TL4.shadowSm }} />)}
      <svg style={SVG_T}><FlowDefs /><TempDefs />{items.map((it, i) => <g key={i} opacity={pop4(t, s + it[2]).op}>{art(i, P(i).x + W / 2, P(i).y + artY)}</g>)}</svg>
      {items.map(([n, d, at], i) => <div key={n} style={{ position: 'absolute', left: P(i).x + 24, top: P(i).y + textY, width: W - 48, opacity: pop4(t, s + at).op, fontFamily: DISP4 }}>
        <div style={{ fontSize: 24, fontWeight: 700 }}>{n}</div>
        <div style={{ fontSize: 18, color: TL4.mut, lineHeight: 1.4, marginTop: 6 }}>{d}</div>
      </div>)}
    </>
  );
}
Object.assign(window, { SVG_T, NoteT, EqT, SubT, CardsT });
