/* -------------------- */
/* ESTILOS GENERALES */
/* -------------------- */
:root {
  --primary-color: #1E3A8A; /* Azul institucional UPTVT */
  --primary-dark: #0F2D66;
  --light-gray: #f9f9f9;
  --medium-gray: #e6e6e6;
  --dark-gray: #333;
  --text-color: #333;
  --text-light: #555;
  --shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  --shadow-hover: 0 6px 12px rgba(0, 0, 0, 0.15);
  --danger-color: #D32F2F; /* Color para el botón de detener */
}

/* -------------------- */
/* BOTÓN FLOTANTE DE CHAT */
/* -------------------- */
#chat-button {
  position: fixed;
  bottom: 12px;
  right: 12px;
  width: 64px;
  height: 64px;
  background-color: transparent;
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.3s ease;
  z-index: 9998;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  overflow: hidden; 
}

#chat-button:hover {
  box-shadow: var(--shadow-hover);
  transform: translateY(-2px);
}

#chat-button.notification-ping::after {
  content: '';
  position: absolute;
  top: 8px;
  right: 8px;
  width: 12px;
  height: 12px;
  background-color: #f44336; /* Rojo para notificaciones */
  border-radius: 50%;
  border: 2px solid white;
  animation: ping-animation 1.5s infinite;
}

@keyframes ping-animation {
  0% {
    transform: scale(0.8);
    opacity: 1;
  }
  80% {
    transform: scale(1.2);
    opacity: 0;
  }
  100% {
    transform: scale(1.2);
    opacity: 0;
  }
}

/* ================================= */
/* ESTILOS PARA LA CARA DE POLIBOT   */
/* ================================= */
.polibot-face-background {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: var(--primary-color);
  border-radius: 50%;
  z-index: -1;
  transition: background-color 0.3s ease;
}

#chat-button:hover .polibot-face-background {
    background-color: var(--primary-dark);
}

.polibot-face-container {
  position: relative;
  width: 85%;
  height: 85%;
  transition: opacity 0.2s ease, transform 0.2s ease;
  border-radius: 50%;
  overflow: hidden;
}

#chat-button.chat-is-open .polibot-face-container {
  opacity: 0;
  transform: scale(0);
}

#chat-button.chat-is-open .icon-close {
  opacity: 1;
  transform: scale(1);
}

.polibot-face-part {
  position: absolute;
  width: 100%;
  left: 0;
  transition: opacity 0.1s ease;
}

#polibotOjos { top: 0; }
#polibotBoca { bottom: 0; }

#chat-button .icon-close {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  position: absolute;
  opacity: 0;
  transform: scale(0);
  transition: opacity 0.2s ease, transform 0.2s ease;
}


/* -------------------- */
/* CABECERA DEL CHAT */
/* -------------------- */
#chat-header {
  background-color: var(--primary-color);
  color: white;
  padding: 12px 16px;
  font-weight: 500;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#header-center {
  flex-grow: 1;
  text-align: center;
}

#chat-title {
  font-size: 18px;
  font-weight: bold;
}

#close-chat-btn {
  display: none;
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 0;
  margin: 0;
  line-height: 0;
}

#theme-toggle {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: white;
  transition: transform 0.2s ease;
}

#theme-toggle:hover {
  transform: scale(1.1);
}

/* -------------------- */
/* INDICADOR DE ESTADO */
/* -------------------- */
#polibot-status {
  font-size: 12px;
  font-weight: normal;
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

#polibot-status.online { background-color: #4CAF50; color: white; }
#polibot-status.offline { background-color: #f44336; color: white; }
#polibot-status.generating,
#polibot-status.connecting { 
  background-color: #ff9800; 
  color: white; 
}

/* -------------------- */
/* TEMA OSCURO */
/* -------------------- */
.dark-theme #chat-box { background: #2d3748; color: #f7fafc; }
.dark-theme #chat-header { background-color: #1a365d; }
.dark-theme #chat-messages { background: #1a202c; }
.dark-theme .message.ia { 
  background: #4a5568; 
  color: #f7fafc;
}
.dark-theme #input-area { background: #2d3748; border-top-color: #4a5568; }
.dark-theme #user-input { color: white; }
.dark-theme #user-input::placeholder { color: #a0aec0; }
.dark-theme #send-button:disabled { background-color: #4a5568; }
.dark-theme #send-button:disabled svg { color: #718096; }
.dark-theme #send-button:not(:disabled) { background-color: #1a365d; }
.dark-theme #send-button:not(:disabled):hover { background-color: #2d3748; }
.dark-theme #theme-toggle { color: #fbd38d; }
.dark-theme .avatar { background-color: #1a365d; }
.dark-theme .typing-indicator { background-color: #4a5568; color: #f7fafc; }
.dark-theme .inline-reply { background-color: #2d3748; color: #e2e8f0; border-color: #4a5568; }
.dark-theme .inline-reply:hover { background-color: #4a5568; color: white; }
.dark-theme .katex { color: #f7fafc; }
.dark-theme .action-icon { color: #a0aec0; }
.dark-theme .action-icon:hover { background-color: #4a5568; color: white; }
.dark-theme #stop-button { background-color: #B71C1C; } /* Color stop en tema oscuro */
.dark-theme #stop-button:hover { background-color: var(--danger-color); }


/* -------------------- */
/* CAJA DEL CHAT */
/* -------------------- */
#chat-box {
  position: fixed;
  bottom: 88px;
  right: 12px;
  width: 380px;
  max-height: 500px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  font-family: 'Outfit', 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', sans-serif;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  z-index: 9999;
}

#chat-box.show {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

#chat-box.hidden { display: none; }

/* -------------------- */
/* ÁREA DE MENSAJES */
/* -------------------- */
#chat-messages {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  font-size: 14px;
  color: var(--text-color);
  background-color: var(--light-gray);
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

#chat-messages::-webkit-scrollbar { width: 6px; }
#chat-messages::-webkit-scrollbar-track { background: transparent; }
#chat-messages::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
#chat-messages::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* =================================================================== */
/* BLOQUE DE ESTILOS FINAL, SIMPLE Y ESTABLE                          */
/* =================================================================== */
.message-container { 
  display: flex; 
  align-items: flex-start;
  gap: 8px; 
  margin-bottom: 8px; 
}
.message-container.user { 
  justify-content: flex-end; 
}
.message-container.user .avatar { 
  order: 2; 
}
.message-container.user .message-content-wrapper { 
  order: 1;
  align-items: flex-end;
}

.message-content-wrapper {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: calc(85% - 40px);
}

/* Regla GENERAL para TODAS las burbujas (user y ia). */
/* Esta es la regla que restaura el estilo de la burbuja de usuario y estabiliza la del bot. */
.message { 
  padding: 8px 14px;
  border-radius: 16px; 
  line-height: 1.4; 
  position: relative; 
  animation: messageIn 0.3s ease-out;
  text-align: left;
  /* La propiedad más importante para evitar el desbordamiento y el texto cortado */
  overflow-wrap: break-word;
}
@keyframes messageIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Estilo específico para la burbuja del USUARIO */
.message.user { 
  background-color: var(--primary-color); 
  color: white; 
}

/* Estilo específico para la burbuja del BOT */
.message.ia { 
  background-color: var(--medium-gray); 
  color: var(--text-color);
}

/* Estilos para las listas */
.message.ia ul {
  list-style: disc;
  padding-left: 20px;
  margin: 0.5em 0;
}

.message.ia li {
  margin-bottom: 0.5em;
}

/* Estilos para los párrafos dentro de las burbujas */
.message p { 
  margin: 0; 
}
.message p:not(:last-child) {
  margin-bottom: 0.5em;
}


/* -------------------- */
/* AVATARES */
/* -------------------- */
.avatar { 
  width: 32px; 
  height: 32px; 
  border-radius: 50%; 
  background-color: var(--primary-color); 
  display: flex; 
  align-items: center; 
  justify-content: center; 
  flex-shrink: 0; 
  box-shadow: 0 1px 3px rgba(0,0,0,0.1); 
}
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }

/* -------------------- */
/* ÁREA DE ENTRADA */
/* -------------------- */
#input-area {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-top: 1px solid #eee;
  background-color: white;
  position: relative;
}

#user-input {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px;
  font-size: 15px;
  resize: none;
  max-height: 120px;
  font-family: inherit;
  outline: none;
  line-height: 1.5;
  color: var(--text-color);
}

#send-button, #stop-button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-left: 8px;
  flex-shrink: 0;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

#send-button {
  background-color: var(--primary-color);
}
#send-button svg { transform: rotate(45deg); margin: -1px 0 0 -2px; }
#send-button:active { transform: scale(0.9); }
#send-button:disabled { background-color: #e0e0e0; cursor: not-allowed; }
#send-button:disabled svg { color: #a6a6a6; }

#stop-button {
    background-color: var(--danger-color);
    display: none;
}
#stop-button:hover {
    background-color: #C62828;
}
#stop-button:active {
    transform: scale(0.9);
}

#input-area.is-generating #send-button {
    display: none;
}
#input-area.is-generating #stop-button {
    display: flex;
}

/* -------------------- */
/* OTROS ESTILOS */
/* -------------------- */
#copy-notification { 
  position: fixed; 
  bottom: 100px; 
  left: 50%; 
  transform: translateX(-50%); 
  background-color: rgba(0, 0, 0, 0.7); 
  color: white; 
  padding: 10px 15px; 
  border-radius: 8px; 
  font-size: 14px; 
  opacity: 0; 
  visibility: hidden; 
  transition: opacity 0.3s ease, visibility 0.3s ease; 
  z-index: 10000; 
  white-space: nowrap; 
}
#copy-notification.show { opacity: 1; visibility: visible; }

.message-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.action-icon {
  width: 24px;
  height: 24px;
  background-color: transparent;
  color: var(--text-light);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.action-icon:hover {
  background-color: var(--medium-gray);
  color: var(--primary-color);
}

.inline-buttons { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.inline-reply { 
  background-color: rgba(255, 255, 255, 0.7); 
  color: var(--primary-color); 
  border: 1px solid var(--primary-color); 
  width: 100%; 
  box-sizing: border-box; 
  border-radius: 8px; 
  padding: 10px; 
  font-size: 14px; 
  font-family: inherit; 
  font-weight: 500; 
  cursor: pointer; 
  transition: all 0.2s ease; 
  text-align: center; 
}
.inline-reply:hover { background-color: var(--primary-color); color: white; transform: translateY(-1px); }

.typing-cursor {
  display: inline-block;
  width: 8px;
  height: 1.2em;
  background-color: var(--text-color);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursor-blink 1s infinite;
}
.dark-theme .typing-cursor { background-color: #f7fafc; }
@keyframes cursor-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* -------------------- */
/* RESPONSIVE DESIGN */
/* -------------------- */
@media (max-width: 480px) {
  #chat-box {
    width: 100%;
    height: 100%;
    max-height: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    border-radius: 0;
  }
  
  #chat-header { border-radius: 0; }
  #close-chat-btn { display: block; }
  #header-center { flex-grow: 1; display: flex; flex-direction: column; align-items: center; }
  #chat-title { line-height: 1; }
  #polibot-status { margin-left: 0; }
  #user-input, .message { font-size: 16px; }

  .message.ia .message-actions { 
    opacity: 1; 
    visibility: visible; 
  }
}

/*
  ===================================================================
  FIX DEFINITIVO PARA EL AJUSTE DE TEXTO EN BURBUJAS DE IA
  ===================================================================
*/
.message.ia pre {
  
  /* ✅ Hereda la fuente del resto del chat para un estilo uniforme */
  font-family: inherit; 

  /* Permite que el texto se ajuste automáticamente sin perder los saltos de línea */
  white-space: pre-wrap; 

  /* Asegura que las palabras muy largas se corten para evitar el desbordamiento */
  word-wrap: break-word; 
}
