/* Widget "Pregúntale al libro" — estilo coherente con tema Cosmo del libro */

#ia-fab {
  position: fixed;
  bottom: 1.4rem;
  right: 1.4rem;
  z-index: 9998;
  background: #2c5282;
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 0.85rem 1.3rem;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  font-size: 0.92rem;
  font-weight: 500;
  box-shadow: 0 4px 14px rgba(44, 82, 130, 0.35);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  /* Pulso de atención al cargar la página (2 ciclos, se detiene solo) */
  animation: ia-attention 2.2s ease-in-out 1.5s 2;
}
@keyframes ia-attention {
  0%, 100% { box-shadow: 0 4px 14px rgba(44, 82, 130, 0.35); transform: scale(1); }
  50%       { box-shadow: 0 4px 28px rgba(44, 82, 130, 0.55), 0 0 0 8px rgba(44, 82, 130, 0.12); transform: scale(1.04); }
}
#ia-fab:hover {
  transform: translateY(-1px) scale(1.02);
  box-shadow: 0 6px 18px rgba(44, 82, 130, 0.45);
}
#ia-fab::before {
  content: "✦  ";
  font-weight: 700;
}

#ia-panel {
  position: fixed;
  right: 1.4rem;
  bottom: 5rem;
  width: min(420px, calc(100vw - 2.5rem));
  max-height: calc(100vh - 7rem);
  background: #fdfdfc;
  border: 1px solid #d8d8d3;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
  display: none;
  flex-direction: column;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  z-index: 9999;
  overflow: hidden;
}
#ia-panel.open { display: flex; }

#ia-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.1rem;
  background: #2c5282;
  color: #fff;
  font-weight: 600;
  font-size: 0.95rem;
}
#ia-head .sub {
  font-weight: 400;
  font-size: 0.78rem;
  opacity: 0.85;
  display: block;
  margin-top: 0.15rem;
}
#ia-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 0.3rem;
}

#ia-body {
  padding: 1rem 1.1rem 0.8rem;
  overflow-y: auto;
  flex: 1 1 auto;
  font-size: 0.9rem;
  line-height: 1.5;
  color: #2b2b2b;
}

#ia-body .hint {
  color: #6b6b6b;
  font-size: 0.83rem;
  margin: 0 0 0.8rem;
}

#ia-answer {
  margin-bottom: 0.8rem;
}
#ia-answer p { margin: 0 0 0.8rem; }
#ia-answer a {
  color: #2c5282;
  text-decoration: none;
  border-bottom: 1px dotted #2c5282;
}
#ia-answer a:hover { border-bottom-style: solid; }

#ia-sources {
  border-top: 1px solid #e2e2df;
  padding-top: 0.7rem;
  margin-top: 0.6rem;
  font-size: 0.8rem;
}
#ia-sources h4 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b6b6b;
  margin: 0 0 0.4rem;
}
#ia-sources ol { padding-left: 1.1rem; margin: 0; }
#ia-sources li { margin-bottom: 0.35rem; color: #4a4a4a; }
#ia-sources li a { color: #2c5282; text-decoration: none; }
#ia-sources li a:hover { text-decoration: underline; }

#ia-actions {
  display: none;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding-top: 0.7rem;
  border-top: 1px solid #e2e2df;
  margin-top: 0.6rem;
}
#ia-actions.visible { display: flex; }
#ia-actions a {
  flex: 1 1 auto;
  text-align: center;
  padding: 0.5rem 0.7rem;
  background: #f2f1ed;
  color: #2c5282;
  border: 1px solid #d8d8d3;
  border-radius: 5px;
  font-size: 0.8rem;
  text-decoration: none;
  font-weight: 500;
}
#ia-actions a:hover { background: #e9e7e1; }
#ia-actions a.primary { background: #2c5282; color: #fff; border-color: #2c5282; }
#ia-actions a.primary:hover { background: #244268; }

#ia-gallery-link {
  display: block;
  text-align: center;
  padding: 0.5rem;
  font-size: 0.75rem;
  color: #6b6b6b;
  text-decoration: none;
  border-top: 1px solid #e2e2df;
}
#ia-gallery-link:hover { color: #2c5282; }

#ia-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.8rem 1.1rem;
  border-top: 1px solid #e2e2df;
  background: #f9f8f4;
}
#ia-input {
  flex: 1;
  padding: 0.55rem 0.7rem;
  border: 1px solid #d8d8d3;
  border-radius: 5px;
  font-family: inherit;
  font-size: 0.88rem;
  background: #fff;
}
#ia-input:focus {
  outline: none;
  border-color: #2c5282;
  box-shadow: 0 0 0 2px rgba(44, 82, 130, 0.15);
}
#ia-submit {
  padding: 0 1rem;
  background: #2c5282;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}
#ia-submit:hover { background: #244268; }
#ia-submit:disabled { background: #8a9ab3; cursor: wait; }

.ia-error {
  color: #a83232;
  background: #fcf0f0;
  border: 1px solid #ecc;
  border-radius: 5px;
  padding: 0.6rem 0.8rem;
  font-size: 0.83rem;
}

.ia-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(44, 82, 130, 0.25);
  border-top-color: #2c5282;
  border-radius: 50%;
  animation: ia-spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 0.5rem;
}
@keyframes ia-spin { to { transform: rotate(360deg); } }

/* Bubble de descubrimiento (solo desktop, primera visita) */
#ia-hint {
  position: fixed;
  bottom: 4.3rem;
  right: 1.4rem;
  max-width: 240px;
  background: #fff;
  border: 1px solid #d8d8d3;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(44, 82, 130, 0.18);
  padding: 0.7rem 0.9rem 0.75rem 2.2rem;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  font-size: 0.85rem;
  line-height: 1.35;
  color: #2b2b2b;
  z-index: 9997;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.35s, transform 0.35s;
  cursor: pointer;
}
#ia-hint.show { opacity: 1; transform: translateY(0); }
#ia-hint::before {
  content: "✦";
  position: absolute;
  left: 0.8rem;
  top: 0.72rem;
  color: #2c5282;
  font-weight: 700;
}
#ia-hint::after {
  content: "";
  position: absolute;
  bottom: -8px;
  right: 26px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid #fff;
  filter: drop-shadow(0 2px 1px rgba(0, 0, 0, 0.05));
}
#ia-hint .ia-hint-close {
  position: absolute;
  right: 0.35rem;
  top: 0.2rem;
  font-size: 1rem;
  color: #8a8a8a;
  cursor: pointer;
  line-height: 1;
  padding: 0.15rem 0.35rem;
}
#ia-hint .ia-hint-close:hover { color: #2b2b2b; }

/* Mobile: panel a pantalla completa, hint oculto */
@media (max-width: 640px) {
  /* Panel: ocupa toda la pantalla excepto el espacio del FAB en la parte inferior */
  #ia-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
    /* Margen inferior dinámico: deja el FAB visible */
    padding-bottom: calc(4rem + env(safe-area-inset-bottom, 0px));
  }

  /* FAB centrado en la parte inferior para no tapar gestos de navegación */
  #ia-fab {
    bottom: calc(0.8rem + env(safe-area-inset-bottom, 0px));
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    font-size: 0.82rem;
    padding: 0.75rem 1.3rem;
    white-space: nowrap;
  }

  /* Input a 16px mínimo para evitar auto-zoom de iOS al hacer focus */
  #ia-input {
    font-size: 16px;
  }

  /* Evitar que el scroll del body traspase al panel (sin touch-action que rompe gestos) */
  #ia-body {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  body.ia-panel-open {
    overflow: hidden;
  }

  #ia-hint { display: none; }
}
