// PepLab Co — Articles + FAQ sections

const ArticlesFull = () => {
  const [filter, setFilter] = React.useState("Todos");
  const tags = ["Todos", ...new Set(PEPLAB_GUIDE.map(g => g.tag))];
  const filtered = filter === "Todos" ? PEPLAB_GUIDE : PEPLAB_GUIDE.filter(g => g.tag === filter);

  const featured = filtered[0];
  const rest = filtered.slice(1);

  return (
    <section id="articulos" style={{ background: "var(--ivory)", padding: "120px 0", borderTop: "1px solid rgba(201, 169, 97, 0.2)" }}>
      <div className="container">
        {/* Header */}
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", alignItems: "end", gap: 60, marginBottom: 64 }}>
          <div>
            <div style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: 24 }}>
              <span className="hairline" />
              <span className="uppercase-eyebrow" style={{ color: "var(--navy-deep)" }}>Hub editorial · 05 artículos</span>
            </div>
            <h2 className="serif" style={{ fontSize: "clamp(36px, 5vw, 64px)", lineHeight: 1.05, color: "var(--navy-deep)", fontWeight: 400, letterSpacing: "-0.015em" }}>
              Guía de péptidos.<br />
              <em style={{ color: "var(--champagne)", fontWeight: 300 }}>Sin fluff.</em>
            </h2>
          </div>
          <div style={{ paddingBottom: 8 }}>
            <p style={{ fontSize: 17, color: "var(--gray-text)", lineHeight: 1.6, maxWidth: 460 }}>
              Aprende antes de comprar. Cero spam, cero promesas vacías —
              solo la información técnica y aplicada que necesitas para
              decidir bien.
            </p>
          </div>
        </div>

        {/* Filter chips */}
        <div style={{ display: "flex", flexWrap: "wrap", gap: 8, marginBottom: 56, paddingBottom: 32, borderBottom: "1px solid var(--gray-line)" }}>
          {tags.map((t) => (
            <button
              key={t}
              onClick={() => setFilter(t)}
              style={{
                padding: "10px 18px",
                background: filter === t ? "var(--navy-deep)" : "transparent",
                color: filter === t ? "var(--champagne)" : "var(--navy-deep)",
                border: `1px solid ${filter === t ? "var(--navy-deep)" : "var(--gray-line)"}`,
                borderRadius: 100,
                fontFamily: "var(--sans)",
                fontSize: 13,
                fontWeight: 500,
                letterSpacing: "0.02em",
                cursor: "pointer",
                transition: "all 0.2s",
              }}
            >
              {t}
            </button>
          ))}
        </div>

        {/* Featured + grid */}
        {featured && (
          <a href={`/article/${featured.slug}`} style={{
            display: "grid", gridTemplateColumns: "1.1fr 1fr",
            gap: 56, marginBottom: 64,
            textDecoration: "none", color: "inherit",
            alignItems: "stretch",
          }}
          className="article-featured"
          >
            <div style={{ aspectRatio: "5/4", position: "relative", overflow: "hidden", borderRadius: 2 }}>
              {featured.image ? (
                <img
                  src={featured.image}
                  alt={featured.title}
                  loading="lazy"
                  decoding="async"
                  style={{
                    width: "100%", height: "100%",
                    objectFit: "cover",
                    display: "block",
                  }}
                />
              ) : (
                <div className="lifestyle-placeholder" style={{ alignItems: "flex-start", justifyContent: "flex-start", padding: 20 }}>
                  <span className="placeholder-label dark">EDITORIAL · {featured.tag.toUpperCase()} · DESTACADO</span>
                </div>
              )}
              <div style={{
                position: "absolute", top: 20, right: 20,
                background: "var(--champagne)", color: "var(--navy-deep)",
                padding: "6px 14px", fontSize: 10, fontWeight: 600,
                letterSpacing: "0.18em", textTransform: "uppercase",
              }}>Destacado</div>
            </div>
            <div style={{ display: "flex", flexDirection: "column", justifyContent: "center" }}>
              <div className="mono" style={{ fontSize: 11, color: "var(--champagne)", letterSpacing: "0.18em", textTransform: "uppercase", marginBottom: 16 }}>
                {featured.tag} · Lectura {featured.read}
              </div>
              <h3 className="serif" style={{ fontSize: "clamp(28px, 3.5vw, 44px)", lineHeight: 1.1, color: "var(--navy-deep)", fontWeight: 500, marginBottom: 20, letterSpacing: "-0.01em" }}>
                {featured.title}
              </h3>
              <p style={{ fontSize: 17, color: "var(--gray-text)", lineHeight: 1.6, marginBottom: 28 }}>
                {featured.excerpt}
              </p>
              <div style={{ display: "inline-flex", alignItems: "center", gap: 8, color: "var(--navy-deep)", fontWeight: 600, fontSize: 14, borderBottom: "1px solid var(--champagne)", paddingBottom: 4, alignSelf: "flex-start" }}>
                Leer artículo completo <ArrowRight size={14} />
              </div>
            </div>
          </a>
        )}

        {/* Grid of remaining */}
        {rest.length > 0 && (
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 24 }}>
            {rest.map((g, i) => (
              <a key={g.slug} href={`/article/${g.slug}`} style={{
                display: "flex", flexDirection: "column",
                textDecoration: "none", color: "inherit",
                background: "var(--white)",
                border: "1px solid var(--gray-line)",
                borderRadius: 2, overflow: "hidden",
                transition: "all 0.3s",
              }}
              onMouseEnter={(e) => { e.currentTarget.style.transform = "translateY(-4px)"; e.currentTarget.style.boxShadow = "0 18px 40px rgba(11,37,69,0.10)"; }}
              onMouseLeave={(e) => { e.currentTarget.style.transform = "none"; e.currentTarget.style.boxShadow = "none"; }}
              >
                <div style={{ aspectRatio: "16/10", position: "relative", overflow: "hidden" }}>
                  {g.image ? (
                    <img
                      src={g.image}
                      alt={g.title}
                      loading="lazy"
                      decoding="async"
                      style={{
                        width: "100%", height: "100%",
                        objectFit: "cover",
                        display: "block",
                      }}
                    />
                  ) : (
                    <div className="lifestyle-placeholder" style={{ alignItems: "flex-end", justifyContent: "flex-start", padding: 14 }}>
                      <span className="placeholder-label dark" style={{ fontSize: 9 }}>EDITORIAL · {g.tag.toUpperCase()}</span>
                    </div>
                  )}
                </div>
                <div style={{ padding: "24px 22px 28px", display: "flex", flexDirection: "column", flex: 1 }}>
                  <div className="mono" style={{ fontSize: 10, color: "var(--champagne)", letterSpacing: "0.15em", textTransform: "uppercase", marginBottom: 12 }}>
                    {g.tag} · {g.read}
                  </div>
                  <h3 className="serif" style={{ fontSize: 20, lineHeight: 1.2, color: "var(--navy-deep)", fontWeight: 500, marginBottom: 12, flex: 1 }}>
                    {g.title}
                  </h3>
                  <div style={{ marginTop: 16, color: "var(--navy-deep)", fontWeight: 600, fontSize: 12, display: "inline-flex", alignItems: "center", gap: 6, letterSpacing: "0.02em" }}>
                    Leer <ArrowRight size={11} />
                  </div>
                </div>
              </a>
            ))}
          </div>
        )}
      </div>
    </section>
  );
};

// Usos de los péptidos — SEO accordion section
// Real h2/h3 headings and text always present in the DOM (collapsed via max-height,
// never unmounted) so search engines index the full content.
const UsesSection = () => {
  const uses = window.PEPLAB_USES || [];
  const [open, setOpen] = React.useState(0); // index of expanded item (-1 = all closed)
  const toggle = (i) => setOpen((cur) => (cur === i ? -1 : i));

  return (
    <section id="usos" style={{ background: "var(--ivory)", padding: "120px 0", borderTop: "1px solid rgba(201, 169, 97, 0.2)" }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "0.9fr 1.4fr", gap: 80, alignItems: "flex-start" }}>
          {/* Side */}
          <div style={{ position: "sticky", top: 120 }}>
            <div style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: 24 }}>
              <span className="hairline" />
              <span className="uppercase-eyebrow" style={{ color: "var(--navy-deep)" }}>Usos de los péptidos</span>
            </div>
            <h2 className="serif" style={{ fontSize: "clamp(32px, 4vw, 56px)", lineHeight: 1.05, color: "var(--navy-deep)", fontWeight: 400, marginBottom: 28, letterSpacing: "-0.015em" }}>
              Para qué sirve <em style={{ color: "var(--champagne)", fontWeight: 300 }}>cada péptido</em>.
            </h2>
            <p style={{ fontSize: 16, color: "var(--gray-text)", lineHeight: 1.6, marginBottom: 28 }}>
              Una guía rápida de las aplicaciones más conocidas de cada molécula.
              Información de referencia — no constituye consejo médico.
            </p>
            <a href={waUrl("Hola, quiero asesoría sobre qué péptido se ajusta a mi objetivo.")} target="_blank" rel="noopener noreferrer" className="btn btn-whatsapp btn-sm">
              <WhatsAppIcon size={14} color="white" />
              Pedir asesoría
            </a>
          </div>

          {/* Accordion list */}
          <div>
            {uses.map((use, i) => {
              const isOpen = open === i;
              return (
                <div key={use.name} style={{ borderBottom: "1px solid var(--gray-line)" }}>
                  <h3 style={{ margin: 0 }}>
                    <button
                      onClick={() => toggle(i)}
                      style={{
                        width: "100%",
                        display: "flex", justifyContent: "space-between", alignItems: "center", gap: 24,
                        padding: "26px 0",
                        background: "none",
                        border: "none",
                        cursor: "pointer",
                        textAlign: "left",
                      }}
                      aria-expanded={isOpen}
                    >
                      <span style={{ display: "flex", alignItems: "baseline", gap: 16 }}>
                        <span className="mono" style={{ fontSize: 12, color: "var(--champagne)", fontWeight: 500, letterSpacing: "0.1em" }}>
                          0{i + 1}
                        </span>
                        <span className="serif" style={{ fontSize: 22, color: "var(--navy-deep)", fontWeight: 500, lineHeight: 1.3 }}>
                          {use.name}
                        </span>
                      </span>
                      <span style={{
                        width: 32, height: 32,
                        borderRadius: "50%",
                        border: `1px solid ${isOpen ? "var(--champagne)" : "var(--gray-line)"}`,
                        display: "flex", alignItems: "center", justifyContent: "center",
                        flexShrink: 0,
                        background: isOpen ? "var(--champagne)" : "transparent",
                        color: "var(--navy-deep)",
                        fontSize: 18,
                        fontWeight: 300,
                        transition: "all 0.3s",
                        transform: isOpen ? "rotate(45deg)" : "rotate(0)",
                      }}>+</span>
                    </button>
                  </h3>
                  <div style={{
                    maxHeight: isOpen ? 400 : 0,
                    overflow: "hidden",
                    transition: "max-height 0.4s cubic-bezier(0.2, 0.8, 0.2, 1), padding 0.3s",
                    paddingBottom: isOpen ? 26 : 0,
                  }}>
                    <p style={{ fontSize: 16, color: "var(--gray-text)", lineHeight: 1.65, paddingRight: 56, paddingLeft: 32 }}>
                      {use.text}
                    </p>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </div>
    </section>
  );
};

// FAQ section
const FaqSection = () => {
  const [open, setOpen] = React.useState({ 0: 0 }); // {catIndex: itemIndex} - one per category open
  const toggle = (cat, i) => {
    setOpen((s) => ({ ...s, [cat]: s[cat] === i ? -1 : i }));
  };

  return (
    <section id="faq" style={{ background: "var(--white)", padding: "120px 0", borderTop: "1px solid var(--gray-line)" }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "0.9fr 1.4fr", gap: 80, alignItems: "flex-start" }}>
          {/* Side */}
          <div style={{ position: "sticky", top: 120 }}>
            <div style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: 24 }}>
              <span className="hairline" />
              <span className="uppercase-eyebrow" style={{ color: "var(--navy-deep)" }}>Preguntas frecuentes</span>
            </div>
            <h2 className="serif" style={{ fontSize: "clamp(32px, 4vw, 56px)", lineHeight: 1.05, color: "var(--navy-deep)", fontWeight: 400, marginBottom: 28, letterSpacing: "-0.015em" }}>
              Las dudas que <em style={{ color: "var(--champagne)", fontWeight: 300 }}>todos tienen</em> antes de pedir.
            </h2>
            <p style={{ fontSize: 16, color: "var(--gray-text)", lineHeight: 1.6, marginBottom: 28 }}>
              Si tu pregunta no aparece acá, escríbenos por WhatsApp.
              Respondemos en menos de 30 minutos en horario hábil.
            </p>
            <a href={waUrl("Hola, tengo una pregunta sobre PepLab Co.")} target="_blank" rel="noopener noreferrer" className="btn btn-whatsapp btn-sm">
              <WhatsAppIcon size={14} color="white" />
              Escribir por WhatsApp
            </a>

            <div style={{ marginTop: 48, paddingTop: 32, borderTop: "1px solid var(--gray-line)" }}>
              <div className="mono" style={{ fontSize: 11, color: "var(--gray-text)", letterSpacing: "0.12em", textTransform: "uppercase", marginBottom: 12 }}>
                Horario
              </div>
              <div style={{ fontSize: 15, color: "var(--graphite)", lineHeight: 1.6 }}>
                Lunes a sábado<br />
                <span className="mono" style={{ fontSize: 14, color: "var(--navy-deep)" }}>09:00 — 22:00</span>
              </div>
            </div>
          </div>

          {/* FAQ list */}
          <div>
            {PEPLAB_FAQ.map((cat, ci) => (
              <div key={cat.cat} style={{ marginBottom: ci < PEPLAB_FAQ.length - 1 ? 56 : 0 }}>
                <div style={{ display: "flex", alignItems: "baseline", gap: 16, marginBottom: 20, paddingBottom: 16, borderBottom: "1px solid var(--gray-line)" }}>
                  <span className="mono" style={{ fontSize: 12, color: "var(--champagne)", fontWeight: 500, letterSpacing: "0.1em" }}>
                    0{ci + 1}
                  </span>
                  <h3 className="serif" style={{ fontSize: 24, color: "var(--navy-deep)", fontWeight: 500 }}>
                    {cat.cat}
                  </h3>
                </div>
                <div>
                  {cat.items.map((it, i) => {
                    const isOpen = open[ci] === i;
                    return (
                      <div key={i} style={{ borderBottom: "1px solid var(--gray-line)" }}>
                        <button
                          onClick={() => toggle(ci, i)}
                          style={{
                            width: "100%",
                            display: "flex", justifyContent: "space-between", alignItems: "center", gap: 24,
                            padding: "22px 0",
                            background: "none",
                            border: "none",
                            cursor: "pointer",
                            textAlign: "left",
                          }}
                          aria-expanded={isOpen}
                        >
                          <span className="serif" style={{ fontSize: 19, color: "var(--navy-deep)", fontWeight: 500, lineHeight: 1.3 }}>
                            {it.q}
                          </span>
                          <span style={{
                            width: 32, height: 32,
                            borderRadius: "50%",
                            border: `1px solid ${isOpen ? "var(--champagne)" : "var(--gray-line)"}`,
                            display: "flex", alignItems: "center", justifyContent: "center",
                            flexShrink: 0,
                            background: isOpen ? "var(--champagne)" : "transparent",
                            color: isOpen ? "var(--navy-deep)" : "var(--navy-deep)",
                            fontSize: 18,
                            fontWeight: 300,
                            transition: "all 0.3s",
                            transform: isOpen ? "rotate(45deg)" : "rotate(0)",
                          }}>+</span>
                        </button>
                        <div style={{
                          maxHeight: isOpen ? 400 : 0,
                          overflow: "hidden",
                          transition: "max-height 0.4s cubic-bezier(0.2, 0.8, 0.2, 1), padding 0.3s",
                          paddingBottom: isOpen ? 24 : 0,
                        }}>
                          <p style={{ fontSize: 15, color: "var(--gray-text)", lineHeight: 1.65, paddingRight: 56, paddingLeft: 0 }}>
                            {it.a}
                          </p>
                        </div>
                      </div>
                    );
                  })}
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
};

Object.assign(window, { ArticlesFull, UsesSection, FaqSection });
