/* global React */
const { useState: useStateP, useEffect: useEffectP } = React;

// All content lives in elementa-content.jsx and is read via the t prop
// passed down from <App />.
// ───────────────────────────────────────────────────────────────────
function ElementaPanels({ openId, onClose, onOpenPresupuesto, t }) {
  const s = t.soluciones, pr = t.proceso, b = t.beneficios, f = t.faq, pl = t.planes, bg = t.blog;
  return (
    <>
      <Panel open={openId === "soluciones"} onClose={onClose} title={s.title} subtitle={s.subtitle} id="soluciones" t={t}>
        <SolucionesContent t={t} onCta={onOpenPresupuesto} />
      </Panel>
      <Panel open={openId === "proceso"} onClose={onClose} title={pr.title} subtitle={pr.subtitle} id="proceso" t={t}>
        <ProcesoContent t={t} onCta={onOpenPresupuesto} />
      </Panel>
      <Panel open={openId === "beneficios"} onClose={onClose} title={b.title} subtitle={b.subtitle} id="beneficios" t={t}>
        <BeneficiosContent t={t} onCta={onOpenPresupuesto} />
      </Panel>
      <Panel open={openId === "faq"} onClose={onClose} title={f.title} subtitle={f.subtitle} id="faq" t={t}>
        <FaqContent t={t} onCta={onOpenPresupuesto} />
      </Panel>
      <Panel open={openId === "planes"} onClose={onClose} title={pl.title} subtitle={pl.subtitle} id="planes" t={t}>
        <PlanesContent t={t} onCta={onOpenPresupuesto} />
      </Panel>
      <Panel open={openId === "blog"} onClose={onClose} title={bg.title} subtitle={bg.subtitle} id="blog" t={t}>
        <BlogContent t={t} />
      </Panel>
    </>
  );
}

// ───────────────────────────────────────────────────────────────────
// Generic panel shell
function Panel({ open, onClose, title, subtitle, id, children }) {
  // `mounted` = the panel exists in the DOM. `visible` = it's animated in.
  // Splitting them lets the browser paint the panel at opacity 0 / blur 0
  // before we flip to the final state on the next frame — avoids the flash.
  const [mounted, setMounted] = useStateP(false);
  const [visible, setVisible] = useStateP(false);

  useEffectP(() => {
    if (open) {
      setMounted(true);
      // Two RAFs: one to ensure initial styles paint, one to trigger transition.
      const r1 = requestAnimationFrame(() => {
        const r2 = requestAnimationFrame(() => setVisible(true));
        Panel._raf = r2;
      });
      return () => {
        cancelAnimationFrame(r1);
        if (Panel._raf) cancelAnimationFrame(Panel._raf);
      };
    } else {
      setVisible(false);
      // Wait for the exit transition before unmounting.
      const t = setTimeout(() => setMounted(false), 380);
      return () => clearTimeout(t);
    }
  }, [open]);

  if (!open && !mounted) return null;

  const shown = open && visible;

  return (
    <div
      style={{
        position: "fixed",
        inset: 0,
        zIndex: 30,
        pointerEvents: open ? "auto" : "none",
      }}
    >
      {/* Backdrop — blur animates in with opacity so it doesn't pop */}
      <div
        onClick={onClose}
        style={{
          position: "absolute",
          inset: 0,
          background: shown ? "rgba(6,6,7,0.55)" : "rgba(6,6,7,0)",
          backdropFilter: `blur(${shown ? 8 : 0}px)`,
          WebkitBackdropFilter: `blur(${shown ? 8 : 0}px)`,
          opacity: shown ? 1 : 0,
          transition: "opacity 460ms cubic-bezier(.2,.7,.25,1), background 460ms cubic-bezier(.2,.7,.25,1), backdrop-filter 460ms cubic-bezier(.2,.7,.25,1), -webkit-backdrop-filter 460ms cubic-bezier(.2,.7,.25,1)",
          willChange: "opacity, backdrop-filter",
        }}
      />
      {/* Panel container */}
      <div
        role="dialog"
        aria-modal="true"
        aria-labelledby={`panel-${id}-title`}
        className="elm-panel"
        style={{
          position: "absolute",
          top: "50%",
          left: "50%",
          transform: `translate(-50%, -50%) translateY(${shown ? 0 : 18}px) scale(${shown ? 1 : 0.99})`,
          opacity: shown ? 1 : 0,
          transition: "transform 520ms cubic-bezier(.2,.75,.25,1) 60ms, opacity 420ms cubic-bezier(.2,.7,.25,1) 60ms",
          willChange: "opacity, transform",
          width: "min(1080px, 92vw)",
          maxHeight: "86vh",
          background: "linear-gradient(180deg, #0d0d0f 0%, #0a0a0b 100%)",
          border: "1px solid var(--line)",
          borderRadius: 20,
          boxShadow: "0 30px 80px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.02)",
          display: "flex",
          flexDirection: "column",
          overflow: "hidden",
        }}
      >
        {/* Header */}
        <header className="elm-panel-header" style={panelStyle.header}>
          <div style={{ minWidth: 0, flex: 1 }}>
            <div className="elm-panel-tag" style={panelStyle.headerTag}>
              <span style={{ color: "var(--mint)" }}>●</span>
              <span>Elementa</span>
              <span className="elm-panel-tag-sep" style={{ opacity: 0.4 }}>·</span>
              <span className="elm-panel-tag-section" style={{ whiteSpace: "nowrap" }}>{title}</span>
            </div>
            <h2 id={`panel-${id}-title`} className="elm-panel-title" style={panelStyle.headerTitle}>{title}</h2>
            <div className="elm-panel-subtitle" style={panelStyle.headerSub}>{subtitle}</div>
          </div>
          <button onClick={onClose} aria-label="Cerrar" className="elm-close-btn" style={panelStyle.close}>
            <span className="elm-close-esc" style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--text-3)" }}>ESC</span>
            <span style={panelStyle.closeX}>×</span>
          </button>
        </header>
        {/* Body */}
        <div className="panel-body elm-panel-body" style={panelStyle.body}>
          {children}
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// SOLUCIONES
function SolucionesContent({ onCta, t }) {
  const sec = t.soluciones;
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>

      <div style={panelStyle.gridAuto(260)}>
        {sec.items.map((p) => <SolutionCard key={p.id} p={p} sec={sec} />)}
      </div>

      {/* ¿No es lo que buscás? → Contactanos */}
      <div className="elm-cta-bar" style={{
        marginTop: 28,
        padding: "18px 22px",
        border: "1px solid var(--line)",
        borderRadius: 14,
        display: "flex",
        gap: 18,
        alignItems: "center",
        justifyContent: "space-between",
        flexWrap: "wrap",
        background: "rgba(255,255,255,0.015)",
      }}>
        <div style={{ color: "var(--text-2)", fontSize: 14 }}>{sec.askNote}</div>
        <button onClick={onCta} style={{
          all: "unset",
          cursor: "pointer",
          color: "var(--text)",
          border: "1px solid var(--line-2)",
          fontWeight: 600,
          fontSize: 13.5,
          padding: "9px 18px",
          borderRadius: 999,
        }}>{sec.askButton} →</button>
      </div>

      {/* Probá Elementa, 15 días gratis */}
      <div style={{
        marginTop: 14,
        padding: "24px 26px",
        border: "1px solid rgba(179,240,216,0.22)",
        borderRadius: 16,
        display: "flex",
        gap: 24,
        alignItems: "center",
        justifyContent: "space-between",
        flexWrap: "wrap",
        background: "linear-gradient(160deg, rgba(179,240,216,0.06), #0c0c0e 70%)",
      }}>
        <div>
          <h3 style={{ margin: "0 0 6px", fontSize: 19, fontWeight: 600, letterSpacing: -0.3 }}>{sec.trialTitle}</h3>
          <p style={{ margin: 0, color: "var(--text-2)", fontSize: 14 }}>{sec.trialBody}</p>
        </div>
        <button onClick={onCta} style={{
          all: "unset",
          cursor: "pointer",
          background: "var(--grad)",
          color: "#0a0a0a",
          fontWeight: 600,
          fontSize: 13.5,
          padding: "12px 20px",
          borderRadius: 999,
        }}>{sec.trialButton} →</button>
      </div>
    </div>
  );
}

function SolutionCard({ p, sec }) {
  const [hover, setHover] = useStateP(false);
  const featured = p.featured;
  return (
    <div
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        position: "relative",
        background: featured
          ? "linear-gradient(160deg, rgba(179,240,216,0.06), #0c0c0e 70%)"
          : "#0c0c0e",
        border: `1px solid ${featured ? "rgba(179,240,216,0.3)" : "var(--line)"}`,
        borderRadius: 16,
        padding: "24px 22px 22px",
        transition: "transform 280ms ease, border-color 280ms ease",
        transform: hover ? "translateY(-3px)" : "translateY(0)",
        opacity: p.soon ? 0.92 : 1,
        display: "flex",
        flexDirection: "column",
      }}
    >
      {p.badge && (
        <div style={{
          position: "absolute",
          top: 14, right: 14,
          fontFamily: "var(--mono)",
          fontSize: 10,
          letterSpacing: 1.5,
          textTransform: "uppercase",
          color: featured ? "#0a0a0a" : "var(--text-3)",
          background: featured ? "var(--mint)" : "transparent",
          border: `1px solid ${featured ? "var(--mint)" : "var(--line-2)"}`,
          borderRadius: 999,
          padding: "3px 10px",
          fontWeight: 700,
        }}>{p.badge}</div>
      )}

      <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 14, paddingRight: 80 }}>
        <span style={{
          width: 40, height: 40, borderRadius: 11, flexShrink: 0,
          display: "flex", alignItems: "center", justifyContent: "center",
          background: "rgba(179,240,216,0.08)",
          border: "1px solid rgba(179,240,216,0.2)",
          color: "var(--mint)",
          fontFamily: "var(--mono)", fontWeight: 700, fontSize: 15,
        }}>{p.name.replace("Elementa ", "").charAt(0)}</span>
        <div style={{ minWidth: 0 }}>
          <div style={{ fontSize: 17, fontWeight: 600, letterSpacing: -0.2 }}>{p.name}</div>
          <div style={{ fontFamily: "var(--mono)", fontSize: 10.5, letterSpacing: 1, color: "var(--text-3)", textTransform: "uppercase", marginTop: 2 }}>{p.rubro}</div>
        </div>
      </div>

      <p style={{ fontSize: 13.5, color: "var(--text-2)", lineHeight: 1.6, marginTop: 0, marginBottom: 16 }}>{p.pitch}</p>

      <ul style={{ listStyle: "none", padding: 0, margin: "0 0 18px", flex: 1 }}>
        {p.feats.map((f) => (
          <li key={f} style={{
            display: "flex", gap: 10,
            padding: "6px 0",
            fontSize: 13,
            color: "var(--text-2)",
            lineHeight: 1.5,
            borderBottom: "1px solid rgba(255,255,255,0.03)",
          }}>
            <span style={{ color: "var(--mint)", flexShrink: 0 }}>✓</span> {f}
          </li>
        ))}
      </ul>

      <div>
        {p.soon ? (
          <span style={{ fontFamily: "var(--mono)", fontSize: 12, letterSpacing: 1, color: "var(--text-3)", textTransform: "uppercase" }}>
            {sec.soonLabel}
          </span>
        ) : (
          <div style={{ display: "flex", alignItems: "baseline", gap: 6 }}>
            <span style={{ fontFamily: "var(--mono)", fontSize: 13, color: "var(--text-3)" }}>{sec.cur}</span>
            <span className="grad-text" style={{ fontFamily: "var(--mono)", fontSize: 30, fontWeight: 700, lineHeight: 1 }}>{p.price}</span>
            <span style={{ fontFamily: "var(--mono)", fontSize: 13, color: "var(--text-3)" }}>{sec.perMonth}</span>
          </div>
        )}
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// PROCESO
function ProcesoContent({ onCta, t }) {
  const sec = t.proceso;
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>

      <div style={{ position: "relative", paddingLeft: 30, marginTop: 8 }}>
        <div style={{
          position: "absolute", left: 12, top: 14, bottom: 14,
          width: 1, background: "linear-gradient(180deg, var(--mint), var(--lav))",
          opacity: 0.4,
        }} />
        {sec.steps.map((p) => (
          <div key={p.num} style={{ position: "relative", marginBottom: 28 }}>
            <div style={{
              position: "absolute", left: -26, top: 6,
              width: 12, height: 12, borderRadius: "50%",
              background: "var(--bg)",
              border: "2px solid var(--mint)",
              boxShadow: "0 0 0 4px rgba(179,240,216,0.08)",
            }} />
            <h4 style={{ margin: "0 0 8px", fontSize: 17, fontWeight: 600 }}>
              <span style={{ fontFamily: "var(--mono)", color: "var(--text-3)", marginRight: 12, fontSize: 13 }}>{p.num}</span>
              {p.title}
            </h4>
            <p style={{ margin: 0, color: "var(--text-2)", fontSize: 14, lineHeight: 1.65, maxWidth: 720 }}>{p.body}</p>
            {p.bullets && (
              <ul style={{ listStyle: "none", padding: 0, margin: "10px 0 0", display: "flex", flexWrap: "wrap", gap: "6px 22px" }}>
                {p.bullets.map((it) => (
                  <li key={it} style={{ ...panelStyle.cardListItem, fontSize: 13, color: "var(--text-2)" }}>
                    <span style={panelStyle.cardListDash}>—</span>{it}
                  </li>
                ))}
              </ul>
            )}
            {p.note && (
              <p style={{ margin: "10px 0 0", color: "var(--text-3)", fontSize: 13, lineHeight: 1.6, maxWidth: 720 }}>{p.note}</p>
            )}
          </div>
        ))}
      </div>

      {/* Nuestro objetivo */}
      <div style={{
        marginTop: 8,
        padding: "22px 24px",
        border: "1px solid rgba(155,168,240,0.2)",
        borderRadius: 16,
        background: "linear-gradient(160deg, rgba(155,168,240,0.05), #0c0c0e 70%)",
      }}>
        <div style={{ fontFamily: "var(--mono)", fontSize: 10.5, letterSpacing: 2, textTransform: "uppercase", color: "var(--lav)", marginBottom: 8 }}>{sec.objetivoTitle}</div>
        <p style={{ margin: 0, color: "var(--text-2)", fontSize: 14.5, lineHeight: 1.7, maxWidth: 760 }}>{sec.objetivoBody}</p>
      </div>

      <CtaBar onClick={onCta} note={sec.ctaNote} cta={sec.ctaButton} />
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// BENEFICIOS / POR QUÉ
function BeneficiosContent({ onCta, t }) {
  const sec = t.beneficios;
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>

      <div style={panelStyle.gridAuto(220)}>
        {sec.stats.map((b) => (
          <div key={b.v} style={{
            ...panelStyle.card,
            padding: "26px 24px",
            background: "#0c0c0e",
            textAlign: "center",
          }}>
            <div className="grad-text" style={{
              fontFamily: "var(--mono)",
              fontWeight: 700,
              fontSize: 42,
              lineHeight: 1,
              marginBottom: 12,
            }}>{b.k}</div>
            <div style={{ fontWeight: 600, fontSize: 14, marginBottom: 6 }}>{b.v}</div>
            <p style={{ margin: 0, color: "var(--text-3)", fontSize: 12.5, lineHeight: 1.6 }}>{b.body}</p>
          </div>
        ))}
      </div>

      <div style={panelStyle.sectionLabel}>{sec.sectionDifferentLabel}</div>

      <div style={panelStyle.gridAuto(280)}>
        {sec.reasons.map((b) => (
          <div key={b.t} style={{
            ...panelStyle.card,
            background: "linear-gradient(160deg, rgba(155,168,240,0.04), #0c0c0e)",
            borderColor: "rgba(155,168,240,0.18)",
          }}>
            <h3 style={{ ...panelStyle.cardTitle, color: "var(--lav)" }}>{b.t}</h3>
            <p style={{ ...panelStyle.cardBody, marginBottom: 0 }}>{b.p}</p>
          </div>
        ))}
      </div>

      <CtaBar onClick={onCta} note={sec.ctaNote} cta={sec.ctaButton} />
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// FAQ
function FaqContent({ onCta, t }) {
  const sec = t.faq;
  const [openIdx, setOpenIdx] = useStateP(0);
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>

      <div style={{ borderTop: "1px solid var(--line)" }}>
        {sec.items.map((f, i) => (
          <FaqItem key={i} f={f} open={openIdx === i} onClick={() => setOpenIdx(openIdx === i ? -1 : i)} idx={i} />
        ))}
      </div>

      <CtaBar onClick={onCta} note={sec.ctaNote} cta={sec.ctaButton} />
    </div>
  );
}

function FaqItem({ f, open, onClick, idx }) {
  return (
    <div style={{ borderBottom: "1px solid var(--line)" }}>
      <button
        onClick={onClick}
        style={{
          all: "unset",
          cursor: "pointer",
          width: "100%",
          display: "flex",
          alignItems: "center",
          gap: 18,
          padding: "20px 4px",
        }}
      >
        <span style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--text-3)", minWidth: 30 }}>
          {String(idx + 1).padStart(2, "0")}
        </span>
        <span style={{ flex: 1, fontSize: 15.5, fontWeight: 500, color: open ? "var(--text)" : "var(--text-2)", transition: "color 200ms" }}>
          {f.q}
        </span>
        <span style={{
          fontFamily: "var(--mono)",
          fontSize: 18,
          color: open ? "var(--mint)" : "var(--text-3)",
          transform: `rotate(${open ? 45 : 0}deg)`,
          transition: "transform 280ms cubic-bezier(.2,.7,.2,1), color 200ms",
        }}>+</span>
      </button>
      <div style={{
        maxHeight: open ? 240 : 0,
        opacity: open ? 1 : 0,
        overflow: "hidden",
        transition: "max-height 380ms cubic-bezier(.2,.7,.2,1), opacity 280ms ease",
      }}>
        <div style={{ padding: "0 4px 22px 48px", color: "var(--text-2)", fontSize: 14.5, lineHeight: 1.7, maxWidth: 780 }}>
          {f.a}
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// PLANES
function PlanesContent({ onCta, t }) {
  const sec = t.planes;
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(min(260px, 100%), 1fr))", gap: 14, marginTop: 8 }}>
        {sec.items.map((p) => <PlanCard key={p.id} p={p} onCta={onCta} sec={sec} t={t} />)}
      </div>

      <div style={{
        marginTop: 28,
        padding: "18px 22px",
        border: "1px dashed var(--line-2)",
        borderRadius: 14,
        background: "rgba(255,255,255,0.015)",
        color: "var(--text-2)",
        fontSize: 13.5,
        lineHeight: 1.65,
      }}>
        <strong style={{ color: "var(--mint)", fontWeight: 600 }}>{sec.diagnosticBoxTitle}</strong>{" "}
        {sec.diagnosticBoxBody}
      </div>
    </div>
  );
}

function PlanCard({ p, onCta, sec, t }) {
  const [hover, setHover] = useStateP(false);
  const featured = p.featured;
  return (
    <div
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        position: "relative",
        background: featured
          ? "linear-gradient(160deg, rgba(179,240,216,0.06), #0c0c0e 70%)"
          : "#0c0c0e",
        border: `1px solid ${featured ? "rgba(179,240,216,0.3)" : "var(--line)"}`,
        borderRadius: 16,
        padding: "26px 22px 22px",
        transition: "transform 280ms ease, border-color 280ms ease",
        transform: hover ? "translateY(-3px)" : "translateY(0)",
        display: "flex",
        flexDirection: "column",
      }}
    >
      {p.badge && (
        <div style={{
          position: "absolute",
          top: 14, right: 14,
          fontFamily: "var(--mono)",
          fontSize: 10,
          letterSpacing: 1.5,
          textTransform: "uppercase",
          color: featured ? "#0a0a0a" : "var(--text-3)",
          background: featured ? "var(--mint)" : "transparent",
          border: `1px solid ${featured ? "var(--mint)" : "var(--line-2)"}`,
          borderRadius: 999,
          padding: "3px 10px",
          fontWeight: 700,
        }}>
          {sec.badges[p.badge] || p.badge}
        </div>
      )}
      <div style={{
        fontFamily: "var(--mono)",
        fontSize: 10.5,
        letterSpacing: 2,
        color: "var(--text-3)",
        textTransform: "uppercase",
        marginBottom: 6,
      }}>{p.tag}</div>
      <h3 style={{ fontSize: 22, margin: "0 0 14px", fontWeight: 600, letterSpacing: -0.3 }}>{p.name}</h3>

      <div style={{ marginBottom: 14 }}>
        <div className="grad-text" style={{ fontFamily: "var(--mono)", fontSize: 24, fontWeight: 700 }}>{p.setup}</div>
        <div style={{ fontSize: 13, color: "var(--text-3)", fontFamily: "var(--mono)" }}>{sec.setupLabel} · {p.monthly}{sec.perMonth}</div>
      </div>

      <p style={{ fontSize: 13.5, color: "var(--text-2)", lineHeight: 1.65, marginTop: 0, marginBottom: 16 }}>
        {p.pitch}
      </p>

      <ul style={{ listStyle: "none", padding: 0, margin: "0 0 18px", flex: 1 }}>
        {p.features.map((f) => (
          <li key={f} style={{
            display: "flex", gap: 10,
            padding: "6px 0",
            fontSize: 13,
            color: "var(--text-2)",
            lineHeight: 1.5,
            borderBottom: "1px solid rgba(255,255,255,0.03)",
          }}>
            <span style={{ color: "var(--mint)", flexShrink: 0 }}>✓</span> {f}
          </li>
        ))}
      </ul>

      <div style={{
        display: "flex", justifyContent: "space-between", alignItems: "center",
        fontFamily: "var(--mono)", fontSize: 11, color: "var(--text-3)",
        marginBottom: 14,
      }}>
        <span>{sec.deliveryLabel} · <span style={{ color: "var(--text-2)" }}>{p.delivery}</span></span>
      </div>

      <button
        onClick={onCta}
        style={{
          all: "unset",
          cursor: "pointer",
          textAlign: "center",
          padding: "11px 14px",
          borderRadius: 10,
          fontSize: 13,
          fontWeight: 600,
          background: featured ? "var(--grad)" : "transparent",
          color: featured ? "#0a0a0a" : "var(--text)",
          border: featured ? "none" : "1px solid var(--line-2)",
          transition: "background 220ms ease, color 220ms ease",
        }}
        onMouseEnter={(e) => {
          if (!featured) { e.currentTarget.style.background = "rgba(255,255,255,0.04)"; }
        }}
        onMouseLeave={(e) => {
          if (!featured) { e.currentTarget.style.background = "transparent"; }
        }}
      >
        {t.common.requestPlan} →
      </button>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
// BLOG
function BlogContent({ t }) {
  const sec = t.blog;
  return (
    <div>
      <p className="elm-panel-lead" style={panelStyle.lead}>{sec.lead}</p>

      <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(min(280px, 100%), 1fr))", gap: 14 }}>
        {sec.items.map((b, i) => <BlogCard key={i} b={b} i={i} readMin={sec.readMin} />)}
      </div>

      <div style={{
        marginTop: 32,
        padding: "24px 26px",
        border: "1px solid var(--line)",
        borderRadius: 14,
        display: "flex",
        gap: 24,
        alignItems: "center",
        justifyContent: "space-between",
        flexWrap: "wrap",
        background: "linear-gradient(160deg, rgba(155,168,240,0.04), #0c0c0e)",
      }}>
        <div>
          <div style={{ fontWeight: 600, fontSize: 15, marginBottom: 4 }}>{sec.newsletterTitle}</div>
          <div style={{ color: "var(--text-3)", fontSize: 13.5 }}>
            {sec.newsletterDesc}
          </div>
        </div>
        <form onSubmit={(e) => { e.preventDefault(); alert(sec.newsletterSuccess); }}
          className="elm-newsletter"
          style={{ display: "flex", gap: 8, flex: "1 1 280px", maxWidth: 420 }}>
          <input type="email" required placeholder={sec.newsletterPlaceholder}
            style={{
              flex: 1,
              background: "var(--bg)",
              border: "1px solid var(--line-2)",
              borderRadius: 10,
              padding: "10px 14px",
              color: "var(--text)",
              fontSize: 13.5,
              fontFamily: "var(--sans)",
              outline: "none",
            }}
          />
          <button type="submit" style={{
            all: "unset",
            cursor: "pointer",
            background: "var(--mint)",
            color: "#0a0a0a",
            fontWeight: 600,
            fontSize: 13,
            padding: "10px 16px",
            borderRadius: 10,
          }}>{sec.newsletterButton}</button>
        </form>
      </div>
    </div>
  );
}

function BlogCard({ b, i, readMin }) {
  const [hover, setHover] = useStateP(false);
  const colors = [
    ["#0e1a16", "#1a2620"],
    ["#0e1320", "#1a1f30"],
    ["#1a1322", "#221a2e"],
    ["#101a1c", "#1c2628"],
    ["#1a1612", "#241e18"],
    ["#101a22", "#1c2530"],
  ];
  const [c1, c2] = colors[i % colors.length];
  return (
    <article
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        ...panelStyle.card,
        padding: 0,
        cursor: "pointer",
        overflow: "hidden",
        transform: hover ? "translateY(-2px)" : "translateY(0)",
      }}
    >
      <div style={{
        height: 140,
        background: `linear-gradient(135deg, ${c1}, ${c2})`,
        position: "relative",
        overflow: "hidden",
      }}>
        <div style={{
          position: "absolute",
          inset: 0,
          background: hover
            ? "radial-gradient(60% 60% at 70% 30%, rgba(179,240,216,0.12), transparent 65%)"
            : "radial-gradient(60% 60% at 30% 70%, rgba(155,168,240,0.10), transparent 65%)",
          transition: "background 400ms ease",
        }} />
        <span style={{
          position: "absolute", top: 14, left: 14,
          fontFamily: "var(--mono)", fontSize: 10, letterSpacing: 1.5,
          color: "var(--mint)",
          textTransform: "uppercase",
          padding: "3px 10px",
          background: "rgba(179,240,216,0.08)",
          border: "1px solid rgba(179,240,216,0.2)",
          borderRadius: 999,
        }}>{b.tag}</span>
        <div style={{
          position: "absolute", bottom: 14, right: 14,
          fontFamily: "var(--mono)", fontSize: 10, color: "var(--text-3)",
          letterSpacing: 1.2,
        }}>{b.date} · {b.read} {readMin}</div>
      </div>
      <div style={{ padding: "18px 20px 22px" }}>
        <h3 style={{
          margin: 0,
          fontSize: 16,
          fontWeight: 600,
          lineHeight: 1.35,
          color: hover ? "var(--mint)" : "var(--text)",
          transition: "color 200ms",
        }}>{b.title}</h3>
      </div>
    </article>
  );
}

// ───────────────────────────────────────────────────────────────────
// Shared CTA bar at panel bottom
function CtaBar({ onClick, note, cta }) {
  return (
    <div className="elm-cta-bar" style={{
      marginTop: 30,
      padding: "20px 22px",
      border: "1px solid var(--line)",
      borderRadius: 14,
      display: "flex",
      gap: 18,
      alignItems: "center",
      justifyContent: "space-between",
      flexWrap: "wrap",
      background: "linear-gradient(160deg, rgba(179,240,216,0.04), #0c0c0e)",
    }}>
      <div style={{ color: "var(--text-2)", fontSize: 14 }}>{note}</div>
      <button onClick={onClick} style={{
        all: "unset",
        cursor: "pointer",
        background: "var(--grad)",
        color: "#0a0a0a",
        fontWeight: 600,
        fontSize: 13.5,
        padding: "10px 18px",
        borderRadius: 999,
      }}>
        {cta} →
      </button>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────
const panelStyle = {
  header: {
    display: "flex",
    justifyContent: "space-between",
    alignItems: "flex-start",
    padding: "28px 32px 22px",
    borderBottom: "1px solid var(--line)",
    flexShrink: 0,
    background: "rgba(255,255,255,0.01)",
  },
  headerTag: {
    fontFamily: "var(--mono)",
    fontSize: 11,
    letterSpacing: 1.8,
    color: "var(--text-3)",
    textTransform: "uppercase",
    marginBottom: 8,
    display: "flex",
    alignItems: "center",
    gap: 8,
  },
  headerTitle: {
    fontFamily: "var(--mono)",
    fontWeight: 700,
    fontSize: 28,
    letterSpacing: -0.5,
    margin: 0,
    color: "var(--text)",
  },
  headerSub: {
    color: "var(--text-3)",
    fontSize: 13.5,
    marginTop: 6,
  },
  close: {
    all: "unset",
    cursor: "pointer",
    display: "flex",
    alignItems: "center",
    gap: 12,
    padding: "8px 14px",
    borderRadius: 999,
    border: "1px solid var(--line-2)",
    color: "var(--text-2)",
    transition: "background 200ms ease, color 200ms ease",
  },
  closeX: {
    fontSize: 22,
    lineHeight: 1,
    color: "var(--text)",
  },
  body: {
    padding: "28px 32px 32px",
    overflowY: "auto",
    flex: 1,
  },
  lead: {
    fontSize: 16,
    lineHeight: 1.6,
    color: "var(--text-2)",
    marginTop: 0,
    marginBottom: 26,
    maxWidth: 780,
  },
  gridAuto: (min) => ({
    display: "grid",
    gridTemplateColumns: `repeat(auto-fit, minmax(min(${min}px, 100%), 1fr))`,
    gap: 14,
  }),
  card: {
    background: "#0c0c0e",
    border: "1px solid var(--line)",
    borderRadius: 14,
    padding: "22px 22px 20px",
    transition: "transform 300ms ease, border-color 300ms ease, background 300ms ease",
  },
  cardNum: {
    fontFamily: "var(--mono)",
    fontSize: 13,
    fontWeight: 700,
    color: "var(--mint)",
    letterSpacing: 0.5,
  },
  cardTitle: {
    fontSize: 16,
    fontWeight: 600,
    margin: "0 0 8px",
    color: "var(--text)",
    letterSpacing: -0.2,
  },
  cardBody: {
    color: "var(--text-2)",
    fontSize: 13.5,
    lineHeight: 1.6,
    margin: "0 0 14px",
  },
  cardList: {
    listStyle: "none",
    padding: 0,
    margin: 0,
  },
  cardListItem: {
    fontSize: 12.5,
    color: "var(--text-3)",
    padding: "3px 0",
    display: "flex",
    gap: 8,
    fontFamily: "var(--mono)",
  },
  cardListDash: {
    color: "var(--mint)",
  },
  sectionLabel: {
    fontFamily: "var(--mono)",
    fontSize: 10.5,
    letterSpacing: 2,
    textTransform: "uppercase",
    color: "var(--text-3)",
    marginTop: 38,
    marginBottom: 16,
    paddingBottom: 10,
    borderBottom: "1px solid var(--line)",
  },
};

window.ElementaPanels = ElementaPanels;
