/* =========================================
   Consultia Chat Widget — LM Studio
   ========================================= */

:root {
  --chat-primary: #0d6efd;
  --chat-primary-dark: #0a58ca;
  --chat-bg: #ffffff;
  --chat-surface: #f8f9fa;
  --chat-border: #e9ecef;
  --chat-text: #212529;
  --chat-text-muted: #6c757d;
  --chat-radius: 16px;
  --chat-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  --chat-width: 380px;
  --chat-height: 540px;
}

/* --- Botón flotante --- */
/* Alineado horizontalmente con .btn-scroll-top, a su izquierda */
#consultia-chat-btn {
  position: fixed;
  bottom: 1.5rem;
  right: calc(4.5rem + 14px); /* 1.5rem(scroll-right) + 3rem(scroll-width) + 14px gap */
  width: auto;
  height: 3rem;
  border-radius: 50px;
  padding: 0 20px 0 14px;
  gap: 8px;
  white-space: nowrap;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--chat-shadow);
  z-index: 9999;
  transition: transform 0.22s ease, background 0.22s ease, border-radius 0.25s ease, width 0.25s ease, padding 0.25s ease;
  outline: none;
}

/* Estado abierto: circular con X */
#consultia-chat-btn.open {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  padding: 0;
  gap: 0;
}

#consultia-chat-btn:hover {
  background: var(--chat-primary-dark);
  transform: scale(1.05);
}

#consultia-chat-btn svg {
  pointer-events: none;
  transition: opacity 0.18s ease;
  flex-shrink: 0;
}

.chat-btn-label {
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  pointer-events: none;
}

#consultia-chat-btn .icon-close {
  display: none;
}

#consultia-chat-btn.open .icon-chat {
  display: none;
}

#consultia-chat-btn.open .icon-close {
  display: block;
}

#consultia-chat-btn.open .chat-btn-label {
  display: none;
}

/* Badge de notificación */
#consultia-chat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 20px;
  height: 20px;
  background: #dc3545;
  border-radius: 50%;
  border: 2px solid #fff;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  display: none;      /* oculto por defecto */
  align-items: center;
  justify-content: center;
  line-height: 1;
  pointer-events: none;
}

/* Visible tras carga de página (JS añade .show-badge) y solo cuando el chat está cerrado */
#consultia-chat-btn.show-badge:not(.open) #consultia-chat-badge {
  display: flex;
}

/* --- Panel de chat --- */
#consultia-chat-panel {
  position: fixed;
  bottom: calc(1.5rem + 60px + 14px); /* encima del chat-btn cuando está abierto (60px) */
  right: 1.5rem;
  width: var(--chat-width);
  height: var(--chat-height);
  background: var(--chat-bg);
  border-radius: var(--chat-radius);
  box-shadow: var(--chat-shadow);
  display: flex;
  flex-direction: column;
  z-index: 9998;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px) scale(0.97);
  transition: opacity 0.22s ease, transform 0.22s ease;
}

#consultia-chat-panel.visible {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0) scale(1);
}

/* --- Cabecera --- */
.chat-header {
  background: var(--chat-primary);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.chat-header-avatar {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.chat-header-info {
  flex: 1;
  min-width: 0;
}

.chat-header-name {
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  line-height: 1.2;
}

.chat-header-status {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  gap: 5px;
  transition: color 0.3s;
}

@keyframes pulse-dot {
  0%, 100% { transform: scale(1);   opacity: 1; }
  50%       { transform: scale(1.35); opacity: 0.65; }
}

.chat-header-status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4ade80;
  display: inline-block;
  transition: background 0.4s, transform 0.4s;
  animation: pulse-dot 2s ease-in-out infinite;
}

/* Karla respondiendo — verde pulsante (default) */
.chat-header-status.online::before {
  background: #4ade80;
  animation: pulse-dot 2s ease-in-out infinite;
}

/* Usuario escribió / inactivo — amarillo suave, sin pulso */
.chat-header-status.away::before {
  background: #64b5f6;
  animation: none;
  transform: scale(1);
}

/* API offline — rojo, sin pulso */
.chat-header-status.offline::before {
  background: #f87171;
  animation: none;
  transform: scale(1);
}

.chat-close-btn {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  transition: background 0.15s;
}

.chat-close-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}

/* --- Cuerpo de mensajes --- */
.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--chat-surface);
  scroll-behavior: smooth;
}

.chat-body::-webkit-scrollbar {
  width: 4px;
}

.chat-body::-webkit-scrollbar-track {
  background: transparent;
}

.chat-body::-webkit-scrollbar-thumb {
  background: #ced4da;
  border-radius: 4px;
}

/* Mensajes */
.chat-message {
  display: flex;
  gap: 8px;
  max-width: 88%;
  animation: msgIn 0.2s ease;
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chat-message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-message.assistant {
  align-self: flex-start;
}

.chat-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  max-width: 100%;
}

.chat-message.user .chat-bubble {
  background: var(--chat-primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-message.assistant .chat-bubble {
  background: #fff;
  color: var(--chat-text);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

/* Timestamp estilo WhatsApp ───────────────────────────── */
.bubble-text {
  display: block;
}

.bubble-time {
  display: block;
  text-align: right;
  font-size: 10px;
  line-height: 1;
  margin-top: 5px;
  letter-spacing: 0.02em;
  user-select: none;
  pointer-events: none;
}

.chat-message.user .bubble-time {
  color: rgba(255, 255, 255, 0.75);
}

.chat-message.assistant .bubble-time {
  color: #aaa;
}

.chat-message .chat-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--chat-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
}

.chat-message.user .chat-avatar {
  background: #6c757d;
}

/* Indicador de escritura */
.typing-indicator {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 10px 14px;
  background: #fff;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  width: fit-content;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

/* Variante usuario: fondo azul, puntos blancos, radio en esquina derecha */
.chat-message.user .typing-indicator {
  background: var(--chat-primary);
  border-bottom-left-radius: 14px;
  border-bottom-right-radius: 4px;
  box-shadow: none;
}
.chat-message.user .typing-indicator span {
  background: rgba(255, 255, 255, 0.85);
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #adb5bd;
  animation: bounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}
.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes bounce {
  0%, 60%, 100% {
    transform: translateY(0);
  }
  30% {
    transform: translateY(-6px);
  }
}

/* Estado offline */
.chat-offline-msg {
  text-align: center;
  padding: 20px 16px;
  color: var(--chat-text-muted);
  font-size: 13px;
}

.chat-offline-msg svg {
  display: block;
  margin: 0 auto 10px;
  opacity: 0.4;
}

/* --- Footer / Input --- */
.chat-footer {
  padding: 12px 14px;
  background: var(--chat-bg);
  border-top: 1px solid var(--chat-border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}

#consultia-chat-input {
  flex: 1;
  border: 1.5px solid var(--chat-border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 14px;
  color: var(--chat-text);
  resize: none;
  min-height: 40px;
  max-height: 100px;
  line-height: 1.45;
  outline: none;
  transition: border-color 0.18s;
  font-family: inherit;
}

#consultia-chat-input:focus {
  border-color: var(--chat-primary);
}

#consultia-chat-input::placeholder {
  color: #adb5bd;
}

#consultia-chat-send {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.18s, transform 0.1s;
}

#consultia-chat-send:hover {
  background: var(--chat-primary-dark);
}

#consultia-chat-send:active {
  transform: scale(0.93);
}

#consultia-chat-send:disabled {
  background: #ced4da;
  cursor: not-allowed;
}

/* Aviso de fuente */
.chat-disclaimer {
  font-size: 11px;
  color: var(--chat-text-muted);
  text-align: center;
  padding: 6px 14px 10px;
  background: var(--chat-bg);
}

/* ── Modal de diagnóstico gratuito (overlay dentro del panel) ── */
#consultia-diag-modal {
  position: absolute;
  inset: 0;
  background: var(--chat-bg);
  z-index: 20;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: translateY(14px);
  transition: opacity 0.22s ease, transform 0.22s ease;
  border-radius: var(--chat-radius);
}

#consultia-diag-modal.visible {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0);
}

.diag-modal-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.diag-modal-head {
  background: var(--chat-primary);
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  border-radius: var(--chat-radius) var(--chat-radius) 0 0;
}

.diag-modal-title {
  color: #fff;
  font-weight: 600;
  font-size: 0.93rem;
}

.diag-modal-close {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.82);
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}

.diag-modal-close:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.15);
}

.diag-modal-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
}

.diag-modal-scroll::-webkit-scrollbar { width: 4px; }
.diag-modal-scroll::-webkit-scrollbar-track { background: transparent; }
.diag-modal-scroll::-webkit-scrollbar-thumb { background: var(--chat-border); border-radius: 2px; }

.diag-modal-desc {
  font-size: 0.78rem;
  color: var(--chat-text-muted);
  margin: 0 0 12px;
}

.diag-field {
  margin-bottom: 10px;
}

.diag-field label {
  display: block;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--chat-text);
  margin-bottom: 4px;
}

.diag-field input,
.diag-field select,
.diag-field textarea {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  font-size: 0.82rem;
  color: var(--chat-text);
  background: var(--chat-surface);
  outline: none;
  font-family: inherit;
  transition: border-color 0.15s, background 0.15s;
}

.diag-field input:focus,
.diag-field select:focus,
.diag-field textarea:focus {
  border-color: var(--chat-primary);
  background: #fff;
}

.diag-field textarea {
  resize: vertical;
  min-height: 58px;
}

.diag-modal-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--chat-border);
  flex-shrink: 0;
  background: var(--chat-bg);
}

#consultia-diag-submit {
  width: 100%;
  padding: 10px 16px;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.18s, transform 0.15s;
}

#consultia-diag-submit:hover {
  background: var(--chat-primary-dark);
  transform: translateY(-1px);
}

#consultia-diag-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.diag-modal-note {
  font-size: 0.71rem;
  color: var(--chat-text-muted);
  text-align: center;
  margin: 8px 0 0;
}

/* ── Botón CTA del formulario en el chat ── */
.chat-form-cta {
  display: block;
  width: calc(100% - 32px);
  margin: 8px 16px 4px;
  padding: 11px 16px;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: background 0.18s, transform 0.15s;
  letter-spacing: 0.01em;
}

.chat-form-cta:hover {
  background: var(--chat-primary-dark);
  transform: translateY(-1px);
}

@media (max-width: 480px) {
  /* scroll-top móvil: right:1rem, width:2.6rem, bottom:2.5rem */
  #consultia-chat-btn {
    bottom: 2.5rem;
    right: calc(1rem + 2.6rem + 12px);
    height: 2.6rem;
    width: auto;
    padding: 0 14px 0 10px;
    border-radius: 50px;
    font-size: 0.76rem;
  }

  #consultia-chat-btn.open {
    width: 54px;
    height: 54px;
    padding: 0;
  }

  #consultia-chat-panel {
    left: 10px;
    right: 10px;
    width: auto;
    bottom: calc(2.5rem + 2.6rem + 14px);
    height: calc(100dvh - calc(2.5rem + 80px));
    max-height: calc(100dvh - calc(2.5rem + 80px));
  }
}
