// ps1c3.jsx — Clase 1.3 «Magnitudes eléctricas: V, A, W, Wh, kW, kWh · CC frente a CA»
// Después de solar-lib.jsx. Exporta SCENES_PS1C3.

const TIEMPOS_PS1C3 = [[0.6, 6.13], [0.6, 4.27, 7.24, 10.59], [0.6, 4.38, 5.96, 11.07], [0.6, 2.13, 4.05, 6.54, 10.75, 12.71, 14.89], [0.6, 3.28, 5.69, 9.48, 13.44], [0.6, 2.37, 10.88], [0.6, 2.65, 7.66, 10.78, 17.11], [0.6, 3.57, 7.5, 13.48], [0.6, 9.87, 12.04], [0.6, 8.9, 10.21]]; // auto
const fr = (i, k) => (TIEMPOS_PS1C3[i] || [])[k] ?? 0.6 + k * 3.5;

// 1 · Analogía hidráulica: tensión = presión, corriente = caudal
function S_Agua({ start, dur }) {
  const t = useTime(); const s = start;
  const flow = -((t * 70) % 40);
  const pV = popS(t, s + fr(1, 1)), pA = popS(t, s + fr(1, 2));
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Tensión y corriente" title="Piensa en una tubería con agua" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={popS(t, s + fr(1, 0)).op}>
          <rect x="180" y="470" width="1100" height="90" rx="45" fill="rgba(56,189,248,0.12)" stroke={SL.ca} strokeWidth="3" />
          <line x1="230" y1="515" x2="1230" y2="515" stroke={SL.ca} strokeWidth="8" strokeDasharray="22 18" strokeDashoffset={flow} strokeLinecap="round" />
          <g transform="translate(420 470)" opacity={pV.op}>
            <line x1="0" y1="0" x2="0" y2="-70" stroke={SL.mut} strokeWidth="4" />
            <circle cx="0" cy="-120" r="55" fill={SL.paper} stroke={SL.cc} strokeWidth="4" />
            <line x1="0" y1="-120" x2={34 * Math.cos(-0.6)} y2={-120 + 34 * Math.sin(-0.6)} stroke={SL.cc} strokeWidth="4" />
            <text x="0" y="-200" textAnchor="middle" fill={SL.cc} fontFamily={DISP} fontSize="26" fontWeight="700">presión</text>
          </g>
          <g transform="translate(980 560)" opacity={pA.op}>
            <path d="M-60 60 H60" stroke={SL.sun} strokeWidth="4" />
            <path d="M40 46 L62 60 L40 74" fill="none" stroke={SL.sun} strokeWidth="4" />
            <text x="0" y="110" textAnchor="middle" fill={SL.sun} fontFamily={DISP} fontSize="26" fontWeight="700">caudal</text>
          </g>
        </g>
      </svg>
      <CardS at={s + fr(1, 1)} x={1380} y={300} w={420} accent={SL.cc} no="V · VOLTIOS" title="Tensión">Lo que empuja, como la presión.</CardS>
      <CardS at={s + fr(1, 2)} x={1380} y={520} w={420} accent={SL.sun} no="A · AMPERIOS" title="Corriente">Lo que circula, como el caudal.</CardS>
      <CapS start={s + fr(1, 3)} dur={dur - fr(1, 3) - 0.3} size={30} weight={500} color={SL.mut} y="90%">Un módulo solar entrega tensión y corriente a la vez.</CapS>
    </SceneS>
  );
}

// 2 · Potencia = V × I
function S_Potencia({ start, dur }) {
  const t = useTime(); const s = start;
  const ladder = [['1 kW', '1 000 W'], ['1 MW', '1 000 kW']];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Potencia" title="El ritmo con que se entrega la energía" />
      <FormulaS at={s + fr(2, 1)} x="30%" y="44%" size={70} sub="potencia = tensión × corriente">P = V × I</FormulaS>
      <FormulaS at={s + fr(2, 2)} x="30%" y="72%" size={46} color={SL.ink} sub="un módulo típico">40 V × 10 A = 400 W</FormulaS>
      {ladder.map(([a, b], i) => {
        const p = popS(t, s + fr(2, 3) + i * 1.2);
        return (
          <div key={a} style={{ position: 'absolute', left: 1180, top: 380 + i * 200, opacity: p.op, transform: `translateY(${p.ty}px)`, display: 'flex', alignItems: 'baseline', gap: 26 }}>
            <span style={{ fontFamily: MONO, fontSize: 66, fontWeight: 700, color: SL.sun }}>{a}</span>
            <span style={{ fontFamily: MONO, fontSize: 40, color: SL.mut }}>=</span>
            <span style={{ fontFamily: MONO, fontSize: 52, fontWeight: 600, color: SL.ink }}>{b}</span>
          </div>
        );
      })}
    </SceneS>
  );
}

// 3 · Energía = potencia × tiempo
function S_Energia({ start, dur }) {
  const t = useTime(); const s = start;
  const x0 = 220, y0 = 760, px = 150, py = 150; // 1 h = 150 px · 1 kW = 150 px
  const axes = popS(t, s + fr(3, 1)).op;
  const fill = segS(t, s + fr(3, 3), 2.2);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Energía" title="Potencia sostenida en el tiempo" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={axes}>
          <line x1={x0} y1={y0} x2={x0 + 5 * px} y2={y0} stroke={SL.lineS} strokeWidth="3" />
          <line x1={x0} y1={y0} x2={x0} y2={y0 - 3 * py} stroke={SL.lineS} strokeWidth="3" />
          {[0, 1, 2, 3, 4].map(h => <text key={h} x={x0 + h * px} y={y0 + 34} textAnchor="middle" fill={SL.mut} fontFamily={MONO} fontSize="18">{h} h</text>)}
          {[1, 2].map(k => <text key={k} x={x0 - 16} y={y0 - k * py + 6} textAnchor="end" fill={SL.mut} fontFamily={MONO} fontSize="18">{k} kW</text>)}
          <rect x={x0} y={y0 - 2 * py} width={3 * px * fill} height={2 * py} fill="rgba(251,191,36,0.28)" stroke={SL.sun} strokeWidth="3" />
          {fill >= 1 && <text x={x0 + 1.5 * px} y={y0 - py + 12} textAnchor="middle" fill={SL.sun} fontFamily={MONO} fontSize="40" fontWeight="700">6 kWh</text>}
          <text x={x0 + 2.5 * px} y={y0 - 3 * py + 10} textAnchor="middle" fill={SL.ink} fontFamily={DISP} fontSize="22">2 kW durante 3 h</text>
        </g>
      </svg>
      <FormulaS at={s + fr(3, 2)} x="72%" y="34%" size={42} sub="vatios hora · kilovatios hora">E = P × t</FormulaS>
      <CardS at={s + fr(3, 4)} x={1150} y={470} w={330} accent={SL.cc} title="kW">Qué tan fuerte.</CardS>
      <CardS at={s + fr(3, 5)} x={1500} y={470} w={330} accent={SL.sun} title="kWh">Cuánto en total.</CardS>
      <CapS start={s + fr(3, 6)} dur={dur - fr(3, 6) - 0.3} size={30} weight={500} color={SL.mut} y="90%">La factura eléctrica cobra kilovatios hora.</CapS>
    </SceneS>
  );
}

// 4 · Vatio pico y condiciones STC
function S_Wp({ start, dur }) {
  const t = useTime(); const s = start;
  const stc = [
    { k: '1 000 W/m²', d: 'irradiancia' },
    { k: '25 °C', d: 'temperatura de la célula' },
    { k: 'AM 1,5', d: 'espectro de referencia' },
  ];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Potencia pico" title="El vatio pico: Wp" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={popS(t, s + fr(4, 1)).op}>
          <PanelG x={260} y={300} w={300} h={470} />
          <rect x={300} y={560} width="220" height="90" rx="10" fill={SL.paper} stroke={SL.sun} strokeWidth="3" />
          <text x={410} y={618} textAnchor="middle" fill={SL.sun} fontFamily={MONO} fontSize="40" fontWeight="700">400 Wp</text>
        </g>
      </svg>
      <div style={{ position: 'absolute', left: 700, top: 300, opacity: popS(t, s + fr(4, 2)).op, fontFamily: MONO, fontSize: 18, letterSpacing: '0.18em', color: SL.mut }}>CONDICIONES ESTÁNDAR · STC</div>
      {stc.map((c, i) => (
        <CardS key={c.k} at={s + fr(4, 3) + i * 0.9} x={700 + i * 380} y={350} w={350} accent={SL.sun} title={c.k}>{c.d}</CardS>
      ))}
      <CardS at={s + fr(4, 4)} x={700} y={600} w={1110} accent={SL.ca} title="Sirve para comparar módulos">En campo casi nunca se dan las tres condiciones a la vez: el módulo suele entregar menos que su potencia pico.</CardS>
    </SceneS>
  );
}

// 5 · Errores frecuentes
function S_Errores({ start, dur }) {
  const t = useTime(); const s = start;
  const curve = segS(t, s + fr(5, 2) - 1.2, 1.6);
  const errs = [
    { bad: 'kilovatios por hora', good: 'kilovatios hora · kWh' },
    { bad: 'sumar kW con kWh', good: 'son magnitudes distintas' },
    { bad: '10 kWp = 10 kW todo el día', good: 'solo en el mejor momento' },
  ];
  const x0 = 1100, y0 = 790, w = 640, h = 330;
  const pts = [...Array(41)].map((_, i) => { const f = i / 40; const p = Math.max(0, Math.sin(Math.PI * f)) ** 1.6 * 0.82; return `${x0 + f * w},${y0 - p * h}`; });
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Errores frecuentes" title="Tres confusiones que cuestan caro" />
      {errs.map((e, i) => {
        const p = popS(t, s + fr(5, 1) + i * 1.8);
        return (
          <div key={i} style={{ position: 'absolute', left: 170, top: 310 + i * 180, width: 820, opacity: p.op, transform: `translateY(${p.ty}px)`, borderRadius: 14, background: SL.paper, border: `1px solid ${SL.lineS}`, padding: '20px 26px', display: 'flex', flexDirection: 'column', gap: 8, boxShadow: SL.shadowSm }}>
            <div style={{ fontFamily: DISP, fontSize: 28, color: SL.bad, textDecoration: 'line-through', textDecorationThickness: 2 }}>✗ {e.bad}</div>
            <div style={{ fontFamily: DISP, fontSize: 26, color: SL.ok, fontWeight: 600 }}>✓ {e.good}</div>
          </div>
        );
      })}
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }} opacity={curve}>
        <line x1={x0} y1={y0} x2={x0 + w} y2={y0} stroke={SL.lineS} strokeWidth="2" />
        <line x1={x0} y1={y0 - h} x2={x0 + w} y2={y0 - h} stroke={SL.bad} strokeWidth="2" strokeDasharray="8 8" />
        <text x={x0 + w} y={y0 - h - 12} textAnchor="end" fill={SL.bad} fontFamily={MONO} fontSize="18">10 kWp</text>
        <polyline points={pts.join(' ')} fill="rgba(251,191,36,0.15)" stroke={SL.sun} strokeWidth="4" />
        <text x={x0} y={y0 + 30} fill={SL.mut} fontFamily={MONO} fontSize="17">6 h</text>
        <text x={x0 + w / 2} y={y0 + 30} textAnchor="middle" fill={SL.mut} fontFamily={MONO} fontSize="17">12 h</text>
        <text x={x0 + w} y={y0 + 30} textAnchor="end" fill={SL.mut} fontFamily={MONO} fontSize="17">18 h</text>
        <text x={x0 + w / 2} y={y0 - h - 50} textAnchor="middle" fill={SL.ink} fontFamily={DISP} fontSize="22">potencia de un día despejado</text>
      </svg>
    </SceneS>
  );
}

// 6 · Corriente continua y alterna
function S_CCCA({ start, dur }) {
  const t = useTime(); const s = start;
  const dc = segS(t, s + fr(6, 1), 1.0), ac = segS(t, s + fr(6, 3), 1.0);
  const W = 720, H = 300;
  const plot = (x, y, color, fn, k, title, sub) => {
    const pts = [...Array(121)].map((_, i) => { const f = i / 120; return `${x + f * W * k},${y + H / 2 - fn(f) * H * 0.38}`; });
    return (
      <g>
        <rect x={x - 20} y={y - 20} width={W + 40} height={H + 40} rx="14" fill={SL.paper} stroke={SL.lineS} strokeWidth="2" />
        <line x1={x} y1={y + H / 2} x2={x + W} y2={y + H / 2} stroke={SL.lineS} strokeWidth="2" strokeDasharray="6 6" />
        <polyline points={pts.join(' ')} fill="none" stroke={color} strokeWidth="5" />
        <text x={x} y={y - 40} fill={color} fontFamily={DISP} fontSize="32" fontWeight="700">{title}</text>
        <text x={x + W} y={y - 40} textAnchor="end" fill={SL.mut} fontFamily={DISP} fontSize="21">{sub}</text>
      </g>
    );
  };
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Tipos de corriente" title="Continua frente a alterna" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={dc}>{plot(150, 400, SL.cc, () => 0.8, dc, 'Continua (CC)', 'módulos y baterías')}<DcG x={400} y={350} w={40} /></g>
        <g opacity={ac}>{plot(1050, 400, SL.ca, (f) => Math.sin(f * Math.PI * 2 * 3 + t * 4), ac, '∿ Alterna (CA)', '50 o 60 Hz · la red')}</g>
      </svg>
      <CapS start={s + fr(6, 1)} dur={dur - fr(6, 1) - 0.3} size={24} weight={500} color={SL.mut} y="83%" x="24%" width={760}>Siempre en el mismo sentido · tensión constante</CapS>
      <CapS start={s + fr(6, 3)} dur={dur - fr(6, 3) - 0.3} size={24} weight={500} color={SL.mut} y="83%" x="71%" width={760}>Sube, baja y cambia de sentido</CapS>
    </SceneS>
  );
}

// 7 · El inversor y los símbolos
function S_Inversor({ start, dur }) {
  const t = useTime(); const s = start;
  const a = s + fr(7, 0);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="El inversor" title="Convierte la continua en alterna" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={popS(t, a).op}>
          <BlockG x={180} y={400} w={300} h={170} label="Módulos" icon={<IcoPanel />} color={SL.cc} />
          <BlockG x={810} y={400} w={300} h={170} label="Inversor" icon={<IcoInverter />} color={SL.ink} />
          <BlockG x={1440} y={400} w={300} h={170} label="Red" icon={<IcoGrid />} color={SL.ca} />
        </g>
        <FlowS d="M480 485 H810" at={a + 0.6} color={SL.cc} />
        <FlowS d="M1110 485 H1440" at={s + fr(7, 1)} color={SL.ca} />
        <g opacity={popS(t, s + fr(7, 2)).op}>
          <DcG x={645} y={436} w={56} sw={5} />
          <text x={1275} y={450} textAnchor="middle" fill={SL.ca} fontFamily={MONO} fontSize="56" fontWeight="700">∿</text>
        </g>
      </svg>
      {(() => { const p = popS(t, s + fr(7, 3)); return (
        <div style={{ position: 'absolute', left: '50%', top: 760, transform: `translate(-50%, ${p.ty}px)`, opacity: p.op, display: 'flex', gap: 60, fontFamily: DISP, fontSize: 28 }}>
          <span style={{ display: 'flex', alignItems: 'center', gap: 14, color: SL.ink }}><span style={{ width: 60, height: 8, borderRadius: 4, background: SL.cc }} /> corriente continua</span>
          <span style={{ display: 'flex', alignItems: 'center', gap: 14, color: SL.ink }}><span style={{ width: 60, height: 8, borderRadius: 4, background: SL.ca }} /> corriente alterna</span>
        </div>
      ); })()}
    </SceneS>
  );
}

// 8 · Ejemplo: de kWp a kWh
function S_Ejemplo({ start, dur }) {
  const t = useTime(); const s = start;
  const steps = [
    { k: '10 kWp', d: 'potencia pico del campo', at: fr(8, 0) },
    { k: '× 5 h', d: 'horas sol pico del sitio', at: fr(8, 0) + 2.6 },
    { k: '= 50 kWh', d: 'por día, antes de pérdidas', at: fr(8, 0) + 4.6 },
    { k: '≈ 40 kWh', d: 'con pérdidas reales', at: fr(8, 1) },
  ];
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Juntemos todo" title="De potencia pico a energía diaria" />
      {steps.map((st, i) => {
        const p = popS(t, s + st.at);
        return (
          <div key={i} style={{ position: 'absolute', left: 140 + i * 430, top: 400, width: 400, opacity: p.op, transform: `translateY(${p.ty}px)`, textAlign: 'center' }}>
            <div style={{ fontFamily: MONO, fontSize: 64, fontWeight: 700, color: i === 3 ? SL.ok : SL.sun }}>{st.k}</div>
            <div style={{ fontFamily: DISP, fontSize: 24, color: SL.mut, marginTop: 10 }}>{st.d}</div>
          </div>
        );
      })}
      <CapS start={s + fr(8, 2)} dur={dur - fr(8, 2) - 0.3} size={30} weight={500} color={SL.mut} y="80%">Este cálculo se detalla en el Módulo 5.</CapS>
    </SceneS>
  );
}

const SCENES_PS1C3 = [
  { C: (p) => <TitleCardS {...p} moduleNo={1} claseNo={3} title="Magnitudes eléctricas: CC y CA" objetivo="Tensión, corriente, potencia y energía sin confusiones, el vatio pico, y la diferencia entre corriente continua y alterna." />, dur: 14, label: 'Apertura' },
  { C: S_Agua, dur: 15, label: 'Tensión y corriente' },
  { C: S_Potencia, dur: 16, label: 'Potencia' },
  { C: S_Energia, dur: 19, label: 'Energía' },
  { C: S_Wp, dur: 20, label: 'Potencia pico' },
  { C: S_Errores, dur: 15, label: 'Errores frecuentes' },
  { C: S_CCCA, dur: 20, label: 'Continua y alterna' },
  { C: S_Inversor, dur: 19, label: 'El inversor y los símbolos' },
  { C: S_Ejemplo, dur: 18, label: 'Ejemplo' },
  { C: (p) => <ClosingS {...p} line="Voltios empujan, amperios circulan, vatios miden el ritmo y kilovatios hora, la cantidad." question="¿Cuántos kWh consume un aire acondicionado de 1,5 kW encendido 8 horas?" />, dur: 16, label: 'Cierre' },
];
window.SCENES_PS1C3 = SCENES_PS1C3;
