// 14 — Agente IA Gastro (botón flotante bottom-right + panel)
function CAgent() {
  const [, t] = window.useLang();
  const [open, setOpen] = React.useState(false);
  const ag = t.agent || {};

  React.useEffect(() => {
    if (!open) return;
    const onKey = (e) => { if (e.key === 'Escape') setOpen(false); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [open]);

  return (
    <>
      <style>{`
        .cgclm-agent {
          position: fixed;
          bottom: 20px; right: 20px;
          z-index: 100;
          display: inline-flex; align-items: center; gap: 0;
          padding: 0;
          background: transparent;
          color: var(--c-ink);
          border: none;
          border-radius: 0;
          cursor: pointer;
          font: 600 14px var(--font-body);
          letter-spacing: 0.01em;
          box-shadow: none;
          transition: transform .35s cubic-bezier(.6,.2,.2,1);
        }
        .cgclm-agent:hover {
          transform: translateY(-4px);
          background: transparent;
          box-shadow: none;
        }
        .cgclm-agent:hover .cgclm-agent-mascot {
          transform: rotate(-8deg) scale(1.06);
        }
        .cgclm-agent-mascot {
          width: 120px; height: 120px;
          display: grid; place-items: center;
          filter: drop-shadow(0 14px 22px rgba(20,22,16,0.32)) drop-shadow(0 4px 8px rgba(20,22,16,0.18));
          transition: transform .35s cubic-bezier(.6,.2,.2,1);
          pointer-events: none;
          transform: rotate(-6deg);
        }
        .cgclm-agent-mascot img {
          width: 100%; height: 100%;
          object-fit: contain;
        }
        .cgclm-agent-text {
          position: absolute;
          right: 130px; top: 50%;
          transform: translateY(-50%) translateX(8px);
          background: var(--c-ink);
          color: white;
          padding: 10px 16px;
          border-radius: 999px;
          white-space: nowrap;
          font: 600 13px var(--font-body);
          letter-spacing: 0.01em;
          box-shadow: 0 8px 20px -6px rgba(20,22,16,0.32);
          opacity: 0;
          pointer-events: none;
          transition: opacity .25s ease, transform .25s ease;
        }
        .cgclm-agent-text::after {
          content: "";
          position: absolute;
          right: -5px; top: 50%;
          transform: translateY(-50%) rotate(45deg);
          width: 10px; height: 10px;
          background: var(--c-ink);
        }
        .cgclm-agent:hover .cgclm-agent-text {
          opacity: 1;
          transform: translateY(-50%) translateX(0);
        }
        .cgclm-agent-pulse {
          position: absolute;
          top: 14px; right: 18px;
          width: 14px; height: 14px;
          border-radius: 50%;
          background: var(--c-red);
          border: 2px solid white;
          box-shadow: 0 0 0 0 rgba(200, 37, 62, 0.6);
          animation: agentPulse 2s infinite;
        }
        @keyframes agentPulse {
          0% { box-shadow: 0 0 0 0 rgba(200, 37, 62, 0.55); }
          70% { box-shadow: 0 0 0 14px rgba(200, 37, 62, 0); }
          100% { box-shadow: 0 0 0 0 rgba(200, 37, 62, 0); }
        }
        @media (max-width: 600px) {
          .cgclm-agent { bottom: 12px; right: 12px; }
          .cgclm-agent-mascot { width: 92px; height: 92px; }
          .cgclm-agent-text { display: none; }
          .cgclm-agent-pulse { top: 8px; right: 10px; width: 12px; height: 12px; }
        }

        /* Panel */
        .cgclm-agent-panel {
          position: fixed;
          bottom: 110px; right: 24px;
          z-index: 101;
          width: min(380px, calc(100vw - 32px));
          background: var(--c-cream);
          border-radius: 18px;
          box-shadow: 0 30px 60px -20px rgba(20,22,16,0.28), 0 8px 20px -8px rgba(0,0,0,0.15);
          overflow: hidden;
          animation: agentRise .35s cubic-bezier(.2,.7,.2,1);
          display: flex; flex-direction: column;
          max-height: calc(100vh - 160px);
        }
        @keyframes agentRise { from { transform: translateY(16px); opacity: 0 } to { transform: translateY(0); opacity: 1 } }

        .cgclm-agent-head {
          background: var(--c-green-deep);
          color: white;
          padding: 18px 22px;
          display: flex; align-items: center; gap: 14px;
        }
        .cgclm-agent-head-mascot {
          width: 56px; height: 56px;
          border-radius: 50%;
          background: white;
          display: grid; place-items: center;
          flex-shrink: 0;
          overflow: hidden;
        }
        .cgclm-agent-head-mascot img {
          width: 92%; height: 92%; object-fit: contain;
        }
        .cgclm-agent-head-text { flex: 1; }
        .cgclm-agent-head-text h4 {
          margin: 0;
          font-family: var(--font-display);
          font-weight: 600;
          font-size: 18px;
          letter-spacing: -0.01em;
        }
        .cgclm-agent-head-text p {
          margin: 2px 0 0;
          font: 500 12px var(--font-body);
          color: rgba(255,255,255,0.78);
          letter-spacing: 0.02em;
        }
        .cgclm-agent-status {
          display: inline-flex; align-items: center; gap: 6px;
          font: 500 11px var(--font-body);
          color: rgba(255,255,255,0.85);
        }
        .cgclm-agent-status::before {
          content: '';
          width: 7px; height: 7px;
          background: #b6e07a;
          border-radius: 50%;
          box-shadow: 0 0 6px #b6e07a;
        }
        .cgclm-agent-close {
          position: absolute;
          top: 12px; right: 12px;
          width: 30px; height: 30px;
          border-radius: 50%;
          background: rgba(255,255,255,0.18);
          color: white;
          border: none;
          cursor: pointer;
          display: grid; place-items: center;
          font: 600 18px/1 var(--font-body);
          transition: background .2s;
        }
        .cgclm-agent-close:hover { background: rgba(255,255,255,0.32); }

        .cgclm-agent-body {
          padding: 22px 22px 18px;
          display: flex; flex-direction: column; gap: 14px;
          overflow: auto;
        }
        .cgclm-agent-bubble {
          background: white;
          border: 1px solid var(--c-line);
          border-radius: 14px 14px 14px 4px;
          padding: 14px 16px;
          font: 400 14px/1.5 var(--font-body);
          color: var(--c-ink);
          max-width: 92%;
        }
        .cgclm-agent-bubble strong { color: var(--c-green-deep); }
        .cgclm-agent-suggest-title {
          font: 600 11px var(--font-body);
          letter-spacing: 0.1em;
          text-transform: uppercase;
          color: var(--c-ink-soft);
          margin-top: 4px;
        }
        .cgclm-agent-suggests { display: flex; flex-direction: column; gap: 8px; }
        .cgclm-agent-suggest {
          background: white;
          border: 1px solid var(--c-line);
          border-radius: 999px;
          padding: 10px 16px;
          font: 500 13px var(--font-body);
          color: var(--c-ink);
          text-align: left;
          cursor: pointer;
          transition: background .2s, border-color .2s, transform .2s;
        }
        .cgclm-agent-suggest:hover {
          background: var(--c-cream-deep);
          border-color: var(--c-green-deep);
          transform: translateX(2px);
        }

        .cgclm-agent-foot {
          padding: 14px 18px 18px;
          border-top: 1px solid var(--c-line);
          background: var(--c-cream-deep);
        }
        .cgclm-agent-input {
          display: flex; gap: 8px;
          background: white;
          border: 1px solid var(--c-line);
          border-radius: 999px;
          padding: 6px 6px 6px 16px;
          align-items: center;
        }
        .cgclm-agent-input input {
          flex: 1; border: none; outline: none; background: transparent;
          font: 400 14px var(--font-body);
          color: var(--c-ink);
          padding: 8px 0;
        }
        .cgclm-agent-input input::placeholder { color: var(--c-ink-soft); opacity: 0.7; }
        .cgclm-agent-send {
          width: 36px; height: 36px;
          border-radius: 50%;
          background: var(--c-green-deep);
          color: white;
          border: none;
          display: grid; place-items: center;
          cursor: pointer;
          transition: background .2s, transform .2s;
        }
        .cgclm-agent-send:hover { background: var(--c-red); transform: scale(1.05); }
        .cgclm-agent-disclaimer {
          margin: 10px 4px 0;
          font: 500 10px var(--font-body);
          color: var(--c-ink-soft);
          letter-spacing: 0.04em;
          text-align: center;
        }

        @media (max-width: 600px) {
          .cgclm-agent-panel {
            bottom: 80px; right: 12px; left: 12px; width: auto;
            max-height: calc(100vh - 130px);
          }
        }
      `}</style>

      <button type="button" className="cgclm-agent" onClick={() => setOpen(o => !o)} aria-label={ag.label || 'Habla con Gastro'}>
        <span className="cgclm-agent-mascot">
          <img src="assets/gastro-mascot.png" alt="" />
        </span>
        <span className="cgclm-agent-text">{ag.label || 'Habla con Gastro'}</span>
        <span className="cgclm-agent-pulse"></span>
      </button>

      {open && (
        <div className="cgclm-agent-panel" role="dialog" aria-modal="false">
          <div className="cgclm-agent-head">
            <div className="cgclm-agent-head-mascot">
              <img src="assets/gastro-mascot.png" alt="Gastro" />
            </div>
            <div className="cgclm-agent-head-text">
              <h4>{ag.title || 'Hola, soy Gastro'}</h4>
              <p>
                <span className="cgclm-agent-status">{ag.status || 'En línea · Asistente IA'}</span>
              </p>
            </div>
            <button className="cgclm-agent-close" onClick={() => setOpen(false)} aria-label="Cerrar">×</button>
          </div>

          <div className="cgclm-agent-body">
            <div className="cgclm-agent-bubble">
              {ag.greet || (
                <>
                  ¡Hola! Soy <strong>Gastro</strong>, el asistente de la Cátedra. Estoy preparándome para responder a tus preguntas sobre <strong>productos, IGPs/DOPs, jornadas, formación y recetas</strong> de Castilla-La Mancha.
                </>
              )}
            </div>
            <div className="cgclm-agent-bubble">
              {ag.soon || (
                <>
                  Aún estoy en <strong>fase de aprendizaje</strong> — muy pronto podrás chatear conmigo. Mientras tanto, prueba algunas preguntas frecuentes 👇
                </>
              )}
            </div>
            <div className="cgclm-agent-suggest-title">{ag.suggests || 'Sugerencias'}</div>
            <div className="cgclm-agent-suggests">
              {(ag.questions || [
                '¿Qué es la IGP Cordero Manchego?',
                '¿Cuándo es el próximo curso de verano?',
                '¿Cómo me apongo en contacto con la Cátedra?',
                'Receta tradicional de zarajos',
              ]).map((q, i) => (
                <button key={i} type="button" className="cgclm-agent-suggest">{q}</button>
              ))}
            </div>
          </div>

          <div className="cgclm-agent-foot">
            <form className="cgclm-agent-input" onSubmit={(e) => e.preventDefault()}>
              <input type="text" placeholder={ag.placeholder || 'Escribe tu pregunta…'} disabled />
              <button type="submit" className="cgclm-agent-send" aria-label={ag.send || 'Enviar'} disabled>
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M5 12h14M13 6l6 6-6 6"/>
                </svg>
              </button>
            </form>
            <p className="cgclm-agent-disclaimer">{ag.disclaimer || 'Próximamente · Demo en preparación'}</p>
          </div>
        </div>
      )}
    </>
  );
}
window.CAgent = CAgent;
