/* ==========================================================================
   ESTILOS INBOX WHATSAPP CLOUD API + AGENTE IA (MOBILE-FIRST RESPONSIVE)
   ========================================================================== */

:root {
  --bg-app: #0f172a;
  --bg-panel: #1e293b;
  --bg-panel-alt: #111827;
  --bg-hover: #334155;
  --bg-active: #2563eb22;
  --border-color: #334155;
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  /* Colores de burbujas por remitente */
  --bubble-client-bg: #1e293b;
  --bubble-client-border: #334155;
  --bubble-human-bg: #065f46;
  --bubble-human-border: #059669;
  --bubble-ia-bg: #3730a3;
  --bubble-ia-border: #6366f1;

  --accent-green: #10b981;
  --accent-indigo: #6366f1;
  --accent-amber: #f59e0b;
  --accent-red: #ef4444;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background-color: var(--bg-app);
  color: var(--text-primary);
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ==========================================================================
   PANTALLA DE LOGIN
   ========================================================================== */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 1rem;
  background: radial-gradient(circle at top, #1e293b 0%, #0f172a 100%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 2rem 1.5rem;
  box-shadow: 0 20px 35px rgba(0, 0, 0, 0.45);
}

.login-header {
  text-align: center;
  margin-bottom: 1.5rem;
}

.login-logo {
  font-size: 2.5rem;
  display: inline-block;
  margin-bottom: 0.5rem;
}

.login-header h1 {
  font-size: 1.35rem;
  margin-bottom: 0.35rem;
}

.login-header p {
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.4rem;
}

.form-group input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 0.92rem;
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus {
  border-color: var(--accent-indigo);
}

.checkbox-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.checkbox-inline input {
  width: auto;
}

.checkbox-inline label {
  margin-bottom: 0;
  cursor: pointer;
}

.alert-error {
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid var(--accent-red);
  color: #fca5a5;
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

/* Botones reutilizables */
.btn-primary {
  background: var(--accent-green);
  color: #052e16;
  font-weight: 700;
  border: none;
  padding: 0.7rem 1.1rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: opacity 0.2s;
}

.btn-primary:hover {
  opacity: 0.92;
}

.btn-secondary {
  background: #334155;
  color: var(--text-primary);
  border: 1px solid #475569;
  padding: 0.45rem 0.8rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
}

.btn-secondary:hover {
  background: #475569;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  padding: 0.45rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
}

.btn-ghost:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

.btn-block {
  width: 100%;
}

/* ==========================================================================
   BARRA SUPERIOR Y CONTENEDOR PRINCIPAL
   ========================================================================== */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

.topbar {
  height: 56px;
  background: var(--bg-panel-alt);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  flex-shrink: 0;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.brand-title {
  font-weight: 700;
  font-size: 0.98rem;
}

.socket-badge {
  font-size: 0.72rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
}

.socket-badge.connected {
  background: rgba(16, 185, 129, 0.18);
  color: #34d399;
}

.socket-badge.disconnected {
  background: rgba(239, 68, 68, 0.18);
  color: #f87171;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* ==========================================================================
   LAYOUT MOBILE-FIRST (1 COLUMNA EN MÓVIL, 2 EN ESCRITORIO)
   ========================================================================== */
.inbox-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* En móvil (< 768px): por defecto se ve la lista de chats al 100% */
.sidebar-chats {
  width: 100%;
  height: 100%;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.chat-panel {
  width: 100%;
  height: 100%;
  background: var(--bg-app);
  display: none;
  flex-direction: column;
}

/* Cuando se selecciona un chat en móvil, ocultamos la lista y mostramos el chat */
.inbox-layout.mobile-chat-open .sidebar-chats {
  display: none;
}

.inbox-layout.mobile-chat-open .chat-panel {
  display: flex;
}

/* ==========================================================================
   COLUMNA IZQUIERDA: BUSCADOR Y LISTA DE CHATS
   ========================================================================== */
.sidebar-search {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-search input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 0.88rem;
  outline: none;
}

.conversations-list {
  flex: 1;
  overflow-y: auto;
}

.empty-list {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 0.95rem;
  border-bottom: 1px solid rgba(51, 65, 85, 0.55);
  cursor: pointer;
  transition: background 0.15s;
}

.conversation-item:hover {
  background: var(--bg-hover);
}

.conversation-item.active {
  background: var(--bg-active);
  border-left: 3px solid var(--accent-green);
}

.conv-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #334155;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
  color: #e2e8f0;
}

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

.conv-top-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.25rem;
  gap: 0.4rem;
}

.conv-name {
  font-weight: 600;
  font-size: 0.93rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 0.74rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.conv-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.conv-last-msg {
  font-size: 0.82rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-badges {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.conv-mode-icon {
  font-size: 0.85rem;
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
}

.unread-badge {
  background: var(--accent-green);
  color: #052e16;
  font-size: 0.73rem;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ==========================================================================
   PANEL DERECHO: CABECERA, BURBUJAS Y COMPOSITOR
   ========================================================================== */
.chat-empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--text-secondary);
}

.empty-icon {
  font-size: 2.8rem;
  margin-bottom: 0.8rem;
}

.chat-empty-state h2 {
  color: var(--text-primary);
  font-size: 1.2rem;
  margin-bottom: 0.5rem;
}

.chat-empty-state p {
  max-width: 420px;
  font-size: 0.9rem;
  line-height: 1.45;
}

.chat-active-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.chat-header {
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  padding: 0.65rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-shrink: 0;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.btn-back-mobile {
  background: #334155;
  color: var(--text-primary);
  border: none;
  padding: 0.4rem 0.65rem;
  border-radius: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.chat-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #334155;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}

.chat-contact-info {
  min-width: 0;
}

.chat-contact-name-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.chat-contact-name-row h3 {
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mode-pill {
  font-size: 0.72rem;
  padding: 0.12rem 0.45rem;
  border-radius: 999px;
  font-weight: 600;
  flex-shrink: 0;
}

.mode-pill.ia {
  background: rgba(99, 102, 241, 0.2);
  color: #a5b4fc;
  border: 1px solid rgba(99, 102, 241, 0.4);
}

.mode-pill.humano {
  background: rgba(245, 158, 11, 0.18);
  color: #fcd34d;
  border: 1px solid rgba(245, 158, 11, 0.35);
}

.chat-contact-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.76rem;
  color: var(--text-secondary);
  margin-top: 0.15rem;
}

.window-badge.open {
  color: #34d399;
}

.window-badge.closed {
  color: #f87171;
}

/* Switch Agente IA ON/OFF */
.ai-switch-label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  cursor: pointer;
  user-select: none;
  background: var(--bg-app);
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--border-color);
}

.ai-switch-text {
  font-size: 0.78rem;
  font-weight: 600;
}

.switch-wrapper {
  position: relative;
  width: 38px;
  height: 20px;
}

.switch-wrapper input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background-color: #475569;
  border-radius: 20px;
  transition: 0.2s;
}

.switch-slider::before {
  position: absolute;
  content: '';
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  border-radius: 50%;
  transition: 0.2s;
}

.switch-wrapper input:checked + .switch-slider {
  background-color: var(--accent-indigo);
}

.switch-wrapper input:checked + .switch-slider::before {
  transform: translateX(18px);
}

.ai-state-tag {
  font-size: 0.72rem;
  font-weight: 700;
  min-width: 24px;
}

/* ==========================================================================
   CONTENEDOR DE MENSAJES Y BURBUJAS POR REMITENTE
   ========================================================================== */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  background-image: radial-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 20px 20px;
}

.message-bubble {
  max-width: 82%;
  padding: 0.6rem 0.8rem;
  border-radius: 12px;
  position: relative;
  word-wrap: break-word;
  line-height: 1.4;
  font-size: 0.9rem;
}

/* Remitente: Cliente (izquierda) */
.message-bubble.remitente-cliente {
  align-self: flex-start;
  background: var(--bubble-client-bg);
  border: 1px solid var(--bubble-client-border);
  border-top-left-radius: 2px;
}

/* Remitente: Agente IA (derecha, tono índigo) */
.message-bubble.remitente-ia {
  align-self: flex-end;
  background: var(--bubble-ia-bg);
  border: 1px solid var(--bubble-ia-border);
  border-top-right-radius: 2px;
}

/* Remitente: Operador Humano (derecha, tono esmeralda) */
.message-bubble.remitente-humano {
  align-self: flex-end;
  background: var(--bubble-human-bg);
  border: 1px solid var(--bubble-human-border);
  border-top-right-radius: 2px;
}

.bubble-sender-label {
  font-size: 0.72rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  opacity: 0.9;
}

.remitente-cliente .bubble-sender-label {
  color: #38bdf8;
}

.remitente-ia .bubble-sender-label {
  color: #c7d2fe;
}

.remitente-humano .bubble-sender-label {
  color: #6ee7b7;
}

.bubble-text {
  white-space: pre-wrap;
}

.bubble-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.35rem;
  margin-top: 0.3rem;
  font-size: 0.7rem;
  color: rgba(255, 255, 255, 0.7);
}

.status-icon.leido {
  color: #38bdf8;
  font-weight: 700;
}

.status-icon.fallido {
  color: #fca5a5;
  font-weight: 700;
}

/* ==========================================================================
   ALERTA VENTANA 24H Y CAJA DE TEXTO
   ========================================================================== */
.window-24h-alert {
  background: rgba(245, 158, 11, 0.15);
  border-top: 1px solid var(--accent-amber);
  color: #fde68a;
  padding: 0.6rem 1rem;
  font-size: 0.82rem;
}

.message-composer {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
}

.message-composer textarea {
  flex: 1;
  resize: none;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  max-height: 110px;
}

.message-composer textarea:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-send {
  background: var(--accent-green);
  color: #052e16;
  border: none;
  padding: 0.65rem 1.1rem;
  border-radius: 8px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

.btn-send:disabled {
  background: #475569;
  color: #94a3b8;
  cursor: not-allowed;
}

/* ==========================================================================
   MODAL DE CREDENCIALES DE META
   ========================================================================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 540px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.55);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
  font-size: 1.05rem;
}

.btn-close-modal {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 1.1rem;
  cursor: pointer;
}

.modal-body {
  padding: 1.25rem;
}

.modal-description {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 1rem;
  line-height: 1.4;
}

.webhook-info-box {
  background: var(--bg-app);
  border: 1px solid var(--border-color);
  padding: 0.75rem;
  border-radius: 8px;
  margin-bottom: 1.1rem;
}

.webhook-info-box label {
  display: block;
  font-size: 0.76rem;
  color: var(--text-secondary);
  margin-bottom: 0.35rem;
}

.webhook-url-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.webhook-url-row code {
  font-size: 0.82rem;
  color: #38bdf8;
  word-break: break-all;
}

.btn-small {
  background: #334155;
  color: var(--text-primary);
  border: none;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
}

.config-feedback {
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  font-size: 0.84rem;
  margin-bottom: 1rem;
}

.config-feedback.success {
  background: rgba(16, 185, 129, 0.16);
  border: 1px solid var(--accent-green);
  color: #6ee7b7;
}

.config-feedback.error {
  background: rgba(239, 68, 68, 0.16);
  border: 1px solid var(--accent-red);
  color: #fca5a5;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1rem;
}

/* ==========================================================================
   MEDIA QUERIES (TABLET Y ESCRITORIO >= 768px)
   ========================================================================== */
@media (min-width: 768px) {
  .sidebar-chats {
    width: 350px;
    flex-shrink: 0;
    display: flex !important;
  }

  .chat-panel {
    flex: 1;
    display: flex !important;
  }

  .btn-back-mobile {
    display: none;
  }

  .message-bubble {
    max-width: 68%;
  }
}

@media (min-width: 1100px) {
  .sidebar-chats {
    width: 390px;
  }
}
