/**
 * SwapsStream Assistant - Frontend Styles
 * Optimizado para móviles y diseño no invasivo
 */

/* ========================================
   Variables CSS Dinámicas
======================================== */
:root {
  --sa-primary-color: #2563eb;
  --sa-secondary-color: #f3f4f6;
  --sa-text-color: #374151;
  --sa-text-light: #6b7280;
  --sa-border-color: #e5e7eb;
  --sa-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  --sa-shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.2);
  --sa-border-radius: 12px;
  --sa-animation-speed: 0.3s;
  --sa-z-index: 999999;
}

/* ========================================
   Chatbot Flotante Principal
======================================== */
.sa-floating-chatbot {
  position: fixed;
  z-index: var(--sa-z-index);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--sa-text-color);
  transition: all var(--sa-animation-speed) ease;
}

/* Posicionamiento */
.sa-floating-chatbot.sa-position-bottom-right {
  bottom: 20px;
  right: 20px;
}

.sa-floating-chatbot.sa-position-bottom-left {
  bottom: 20px;
  left: 20px;
}

.sa-floating-chatbot.sa-position-top-right {
  top: 20px;
  right: 20px;
}

.sa-floating-chatbot.sa-position-top-left {
  top: 20px;
  left: 20px;
}

/* Adaptación móvil */
@media (max-width: 768px) {
  .sa-floating-chatbot.sa-mobile {
    bottom: 15px !important;
    right: 15px !important;
    left: auto !important;
    top: auto !important;
  }
}

/* ========================================
   Botón Trigger (No Invasivo)
======================================== */
.sa-chatbot-trigger {
  position: relative;
  width: 60px;
  height: 60px;
  background: var(--sa-primary-color);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: var(--sa-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  transition: all var(--sa-animation-speed) ease;
  outline: none;
  overflow: hidden;
}

.sa-chatbot-trigger:hover {
  transform: scale(1.05);
  box-shadow: var(--sa-shadow-lg);
}

.sa-chatbot-trigger:focus {
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.3);
}

/* Tamaños del botón */
.sa-floating-chatbot.sa-size-small .sa-chatbot-trigger {
  width: 50px;
  height: 50px;
  font-size: 20px;
}

.sa-floating-chatbot.sa-size-large .sa-chatbot-trigger {
  width: 70px;
  height: 70px;
  font-size: 28px;
}

/* Iconos del trigger */
.sa-trigger-icon {
  transition: all var(--sa-animation-speed) ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sa-floating-chatbot[data-state="open"] .sa-icon-chat {
  opacity: 0;
  transform: rotate(90deg);
}

.sa-floating-chatbot[data-state="open"] .sa-icon-close {
  opacity: 1;
  transform: rotate(0deg);
}

.sa-icon-close {
  opacity: 0;
  transform: rotate(-90deg);
  position: absolute;
}

/* Tooltip discreto */
.sa-tooltip {
  position: absolute;
  bottom: 100%;
  right: 0;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transform: translateY(4px);
  transition: all var(--sa-animation-speed) ease;
  pointer-events: none;
  margin-bottom: 8px;
  max-width: 200px;
  text-align: center;
}

.sa-tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  right: 16px;
  border: 4px solid transparent;
  border-top-color: rgba(0, 0, 0, 0.8);
}

.sa-chatbot-trigger:hover .sa-tooltip {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* Animación de pulso sutil */
.sa-chatbot-trigger::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 50%;
  background: var(--sa-primary-color);
  opacity: 0.3;
  transform: scale(0.8);
  animation: sa-pulse 3s infinite ease-in-out;
}

@keyframes sa-pulse {
  0%, 100% {
    transform: scale(0.8);
    opacity: 0.3;
  }
  50% {
    transform: scale(1.2);
    opacity: 0;
  }
}

/* ========================================
   Ventana del Chat (Compacta y con Scroll)
======================================== */
.sa-chatbot-window {
  position: absolute;
  background: white;
  border-radius: var(--sa-border-radius);
  box-shadow: var(--sa-shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px) scale(0.95);
  transition: all var(--sa-animation-speed) ease;
  width: 380px;
  height: 500px;
  z-index: 1;
}

/* Posicionamiento de la ventana */
.sa-floating-chatbot.sa-position-bottom-right .sa-chatbot-window,
.sa-floating-chatbot.sa-position-bottom-left .sa-chatbot-window {
  bottom: 80px;
}

.sa-floating-chatbot.sa-position-top-right .sa-chatbot-window,
.sa-floating-chatbot.sa-position-top-left .sa-chatbot-window {
  top: 80px;
}

.sa-floating-chatbot.sa-position-bottom-right .sa-chatbot-window,
.sa-floating-chatbot.sa-position-top-right .sa-chatbot-window {
  right: 0;
}

.sa-floating-chatbot.sa-position-bottom-left .sa-chatbot-window,
.sa-floating-chatbot.sa-position-top-left .sa-chatbot-window {
  left: 0;
}

/* Estado abierto */
.sa-floating-chatbot[data-state="open"] .sa-chatbot-window {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* Adaptación móvil - Ventana compacta */
@media (max-width: 768px) {
  .sa-chatbot-window {
    width: calc(100vw - 30px);
    height: 400px;
    max-width: 350px;
  }
  
  .sa-floating-chatbot.sa-mobile .sa-chatbot-window {
    right: 0 !important;
    left: auto !important;
    bottom: 80px !important;
    top: auto !important;
  }
}

@media (max-width: 480px) {
  .sa-chatbot-window {
    width: calc(100vw - 20px);
    height: 350px;
  }
}

/* ========================================
   Header del Chat
======================================== */
.sa-chat-header {
  padding: 16px 20px;
  background: var(--sa-primary-color);
  color: white;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.sa-avatar {
  position: relative;
  flex-shrink: 0;
}

.sa-avatar-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  overflow: hidden;
  border-radius: 50%;
  margin-right: 0;
}

/* Estilo de la imagen del avatar */
.sa-avatar-icon img {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
  display: block;
}

.sa-status-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 8px;
  height: 8px;
  background: #10b981;
  border-radius: 50%;
  border: 2px solid white;
}

.sa-info {
  flex-grow: 1;
  min-width: 0;
}

.sa-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.2;
}

.sa-subtitle {
  margin: 0;
  font-size: 12px;
  opacity: 0.8;
  line-height: 1.2;
}

.sa-minimize {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0.8;
  transition: opacity var(--sa-animation-speed) ease;
  flex-shrink: 0;
}

.sa-minimize:hover {
  opacity: 1;
}

/* ========================================
   Área de Mensajes (Con Scroll Optimizado)
======================================== */
.sa-messages-container {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--sa-secondary-color);
}

.sa-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Scrollbar personalizada */
.sa-messages::-webkit-scrollbar {
  width: 4px;
}

.sa-messages::-webkit-scrollbar-track {
  background: transparent;
}

.sa-messages::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.sa-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* ========================================
   Mensajes
======================================== */
.sa-message {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  max-width: 100%;
  animation: sa-message-appear 0.3s ease;
}

.sa-message-user {
  flex-direction: row-reverse;
  margin-left: 40px;
}

.sa-message-bot {
  margin-right: 40px;
}

@keyframes sa-message-appear {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.sa-message-avatar {
  font-size: 20px;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.sa-message-content {
  flex: 1;
  min-width: 0;
}

.sa-message-bubble {
  background: white;
  padding: 12px 16px;
  border-radius: 18px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  word-wrap: break-word;
  line-height: 1.4;
}

.sa-message-user .sa-message-bubble {
  background: var(--sa-primary-color);
  /* El texto del usuario debe ser legible incluso si la variable primaria se personaliza a un color claro */
  color: white !important;
}

.sa-message-bubble p {
  margin: 0 0 8px 0;
}

.sa-message-bubble p:last-child {
  margin-bottom: 0;
}

.sa-message-bubble ul {
  margin: 8px 0;
  padding-left: 20px;
}

.sa-message-bubble li {
  margin-bottom: 4px;
}

.sa-message-time {
  font-size: 11px;
  color: var(--sa-text-light);
  margin-top: 4px;
  text-align: left;
}

.sa-message-user .sa-message-time {
  text-align: right;
}

/* ========================================
   Sugerencias Rápidas
======================================== */
.sa-quick-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  padding: 0 16px;
}

.sa-suggestion {
  background: white;
  border: 1px solid var(--sa-border-color);
  border-radius: 20px;
  padding: 8px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: all var(--sa-animation-speed) ease;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}

.sa-suggestion:hover {
  background: var(--sa-primary-color);
  color: white;
  border-color: var(--sa-primary-color);
  transform: translateY(-1px);
}

/* ========================================
   Indicador de Escritura
======================================== */
.sa-typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: rgba(0, 0, 0, 0.05);
  animation: sa-fade-in 0.3s ease;
}

@keyframes sa-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.sa-typing-dots {
  display: flex;
  gap: 3px;
}

.sa-typing-dots span {
  width: 6px;
  height: 6px;
  background: var(--sa-text-light);
  border-radius: 50%;
  animation: sa-typing-bounce 1.4s infinite ease-in-out;
}

.sa-typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.sa-typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes sa-typing-bounce {
  0%, 60%, 100% {
    opacity: 0.3;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-4px);
  }
}

.sa-typing-text {
  font-size: 12px;
  color: var(--sa-text-light);
}

/* ========================================
   Área de Input (Compacta)
======================================== */
.sa-input-area {
  padding: 12px 16px;
  background: white;
  border-top: 1px solid var(--sa-border-color);
  flex-shrink: 0;
}

.sa-chat-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sa-input-wrapper {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.sa-message-input {
  flex: 1;
  border: 1px solid var(--sa-border-color);
  border-radius: 20px;
  padding: 10px 50px 10px 16px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 80px;
  min-height: 40px;
  line-height: 1.4;
  transition: border-color var(--sa-animation-speed) ease;
}

.sa-message-input:focus {
  border-color: var(--sa-primary-color);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.sa-input-actions {
  position: absolute;
  right: 6px;
  bottom: 6px;
  display: flex;
  gap: 4px;
}

.sa-send-btn {
  width: 32px;
  height: 32px;
  background: var(--sa-primary-color);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--sa-animation-speed) ease;
  font-size: 16px;
}

.sa-send-btn:hover:not(:disabled) {
  background: #1d4ed8;
  transform: scale(1.05);
}

.sa-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.sa-input-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  color: var(--sa-text-light);
}

.sa-char-count {
  margin-left: auto;
}

/* ========================================
   Footer del Chat
======================================== */
.sa-chat-footer {
  padding: 8px 16px;
  background: #f9fafb;
  border-top: 1px solid var(--sa-border-color);
  display: flex;
  justify-content: center;
  gap: 16px;
  flex-shrink: 0;
}

.sa-footer-action {
  background: none;
  border: none;
  color: var(--sa-text-light);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: color var(--sa-animation-speed) ease;
}

.sa-footer-action:hover {
  color: var(--sa-primary-color);
}

/* ========================================
   Widgets y Shortcodes
======================================== */
.sa-assistant-widget {
  position: relative;
}

/* El botón flotante del widget necesita posicionamiento relativo para el tooltip */
.sa-floating-trigger {
  position: relative;
}

.sa-assistant-trigger {
  background: var(--sa-primary-color);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all var(--sa-animation-speed) ease;

  /* Permitir posicionar tooltips relativos al botón */
  position: relative;
}

.sa-assistant-trigger:hover {
  background: #1d4ed8;
  transform: translateY(-1px);
}

.sa-assistant-panel {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 8px;
  z-index: 1000;
}

.sa-chat-widget {
  border: 1px solid var(--sa-border-color);
  border-radius: var(--sa-border-radius);
  overflow: hidden;
  background: white;
}

/* ========================================
   Estados y Animaciones
======================================== */
.sa-loading {
  opacity: 0.6;
  pointer-events: none;
}

.sa-error {
  color: #dc2626;
  background: #fee2e2;
  padding: 12px;
  border-radius: 8px;
  margin: 8px 16px;
  font-size: 13px;
}

.sa-success {
  color: #059669;
  background: #d1fae5;
  padding: 12px;
  border-radius: 8px;
  margin: 8px 16px;
  font-size: 13px;
}

/* ========================================
   Modo Oscuro (Automático)
======================================== */
@media (prefers-color-scheme: dark) {
  .sa-chatbot-window {
    background: #1f2937;
    color: #f9fafb;
  }
  
  .sa-message-bubble {
    background: #374151;
    color: #f9fafb;
  }
  
  .sa-message-user .sa-message-bubble {
    background: var(--sa-primary-color);
  }
  
  .sa-input-area,
  .sa-chat-footer {
    background: #374151;
    border-color: #4b5563;
  }
  
  .sa-message-input {
    background: #4b5563;
    border-color: #6b7280;
    color: #f9fafb;
  }
  
  .sa-suggestion {
    background: #4b5563;
    border-color: #6b7280;
    color: #f9fafb;
  }
}

/* ========================================
   Accesibilidad
======================================== */
@media (prefers-reduced-motion: reduce) {
  .sa-floating-chatbot,
  .sa-chatbot-trigger,
  .sa-chatbot-window,
  .sa-message,
  .sa-suggestion {
    animation: none !important;
    transition: none !important;
  }
}

/* Focus visible para navegación por teclado */
.sa-chatbot-trigger:focus-visible,
.sa-send-btn:focus-visible,
.sa-suggestion:focus-visible {
  outline: 2px solid var(--sa-primary-color);
  outline-offset: 2px;
}

/* ========================================
   Responsive Final
======================================== */
@media (max-width: 320px) {
  .sa-chatbot-window {
    width: calc(100vw - 15px);
    height: 300px;
  }
  
  .sa-floating-chatbot.sa-size-large .sa-chatbot-trigger {
    width: 60px;
    height: 60px;
    font-size: 24px;
  }
}

/* Hide en print */
@media print {
  .sa-floating-chatbot {
    display: none !important;
  }
}
