/* ============================================================================
   GLOBAL CHAT v2.0 — Letras & Versos
   FAB siempre visible · Ícono activo con actividad · Cola de espera
   ============================================================================ */

/* ══════════════════════════════════════════════
   FAB — Botón flotante (SIEMPRE VISIBLE)
══════════════════════════════════════════════ */
#chatFab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9997;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  /* En reposo: muy sutil, casi transparente */
  background: rgba(20, 10, 10, 0.52);
  border: 1.5px solid rgba(255, 255, 255, 0.1);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  box-shadow: none;
  /* Opacidad reducida en reposo */
  opacity: 0.55;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
              box-shadow 0.3s ease,
              background 0.4s ease,
              border-color 0.4s ease,
              opacity 0.35s ease;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* ── Dos burbujas separadas del FAB ── */
.chat-fab-bubbles {
  position: relative;
  width: 1.5em;
  height: 1.25em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Burbuja grande (atrás, arriba-derecha) */
.chat-fab-bubbles .chat-bubble-a {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 1em;
  color: rgba(255, 255, 255, 0.72);
  transition: color 0.4s ease, filter 0.4s ease, transform 0.4s ease;
  transform: scaleX(-1);
  z-index: 1;
}

/* Burbuja pequeña (frente, abajo-izquierda) */
.chat-fab-bubbles .chat-bubble-b {
  position: absolute;
  bottom: 0;
  left: 0;
  font-size: 0.68em;
  color: rgba(255, 255, 255, 0.55);
  transition: color 0.4s ease, filter 0.4s ease, transform 0.4s ease;
  z-index: 2;
}

#chatFab:hover {
  transform: scale(1.08);
  opacity: 0.85;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
  background: rgba(40, 20, 20, 0.6);
  border-color: rgba(255, 255, 255, 0.18);
}

#chatFab:active {
  transform: scale(0.95);
}

/* ── Estado CON ACTIVIDAD NUEVA ── */
/* El ícono se vuelve dorado y el botón pulsa suavemente */
#chatFab.has-activity {
  /* El botón solo gana visibilidad — el color lo dan las burbujas */
  opacity: 1;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}

/* Con actividad: cada burbuja tiene su propio color */
#chatFab.has-activity .chat-bubble-a {
  color: #fbbf24;  /* ámbar brillante */
  filter: drop-shadow(0 0 6px rgba(251, 191, 36, 0.9));
  animation: glowA 1.8s ease-in-out infinite;
}

#chatFab.has-activity .chat-bubble-b {
  color: #a78bfa;  /* violeta suave */
  filter: drop-shadow(0 0 6px rgba(167, 139, 250, 0.9));
  animation: glowB 1.8s ease-in-out infinite 0.5s;
}

@keyframes fabActivityPulse {
  0%, 100% {
    box-shadow: 0 4px 20px rgba(155, 44, 44, 0.55),
                0 0 0 0 rgba(212, 175, 55, 0.4);
  }
  50% {
    box-shadow: 0 4px 28px rgba(155, 44, 44, 0.7),
                0 0 0 8px rgba(212, 175, 55, 0);
  }
}

@keyframes glowA {
  0%, 100% { filter: drop-shadow(0 0 3px rgba(251, 191, 36, 0.6)); }
  50%       { filter: drop-shadow(0 0 10px rgba(251, 191, 36, 1)); }
}

@keyframes glowB {
  0%, 100% { filter: drop-shadow(0 0 3px rgba(167, 139, 250, 0.6)); }
  50%       { filter: drop-shadow(0 0 10px rgba(167, 139, 250, 1)); }
}

/* Badge numérico de no leídos */
#chatFabBadge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #dc2626;
  color: white;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  font-family: var(--font-sans, 'Inter', sans-serif);
  pointer-events: none;
  border: 2px solid var(--bg-primary, #0f0f0f);
}

#chatFabBadge.visible {
  display: flex;
  animation: badge-pulse 2s ease-in-out infinite;
}

@keyframes badge-pulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.15); }
}

/* ══════════════════════════════════════════════
   PANEL DEL CHAT
══════════════════════════════════════════════ */
#chatPanel {
  position: fixed;
  z-index: 9998;
  background: var(--modal-bg, #ffffff);
  border: 1.5px solid var(--border-color, #d1d5db);
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1),
              opacity 0.3s ease;
  opacity: 0;
  pointer-events: none;
}

#chatPanel.open {
  opacity: 1;
  pointer-events: all;
}

/* Desktop */
@media (min-width: 769px) {
  #chatPanel {
    bottom: 88px;
    right: 24px;
    width: 340px;
    height: 480px;
    border-radius: 16px;
    transform: translateY(20px) scale(0.97);
  }
  #chatPanel.open {
    transform: translateY(0) scale(1);
  }
}

/* Móvil: slide-up */
@media (max-width: 768px) {
  #chatPanel {
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 70vh;
    max-height: 520px;
    border-radius: 20px 20px 0 0;
    border-bottom: none;
    transform: translateY(100%);
  }
  #chatPanel.open {
    transform: translateY(0);
  }
}

/* ══════════════════════════════════════════════
   HEADER
══════════════════════════════════════════════ */
#chatHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--border-color, #d1d5db);
  background: var(--panel-bg, #fafafa);
  border-radius: 16px 16px 0 0;
  flex-shrink: 0;
  gap: 0.5rem;
  position: relative;
}

@media (max-width: 768px) {
  #chatHeader {
    border-radius: 20px 20px 0 0;
    padding: 1rem 1rem 0.875rem;
  }
  #chatHeader::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: var(--border-strong, #9ca3af);
    border-radius: 2px;
    opacity: 0.35;
  }
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.chat-header-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-burgundy, #9b2c2c), var(--accent-gold, #d4af37));
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.chat-header-icon i {
  color: white;
  font-size: 0.85rem;
}

.chat-header-info { min-width: 0; }

.chat-header-title {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.9375rem;
  font-weight: 700;
  color: var(--text-primary, #1a1a1a);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-status {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.72rem;
  color: var(--text-tertiary, #6a6a6a);
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.chat-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
  animation: statusPulse 2s ease-in-out infinite;
}

.chat-status-dot.readonly {
  background: #f59e0b;
  animation: none;
}

@keyframes statusPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.5; }
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
}

.chat-header-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  border-radius: 8px;
  color: var(--text-secondary, #4a4a4a);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  transition: background 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.chat-header-btn:hover {
  background: var(--btn-bg-hover, #f3f4f6);
  color: var(--accent-burgundy, #9b2c2c);
}

/* ══════════════════════════════════════════════
   BANNER SALA LLENA (genérico)
══════════════════════════════════════════════ */
#chatFullBanner {
  display: none;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: rgba(245, 158, 11, 0.1);
  border-bottom: 1px solid rgba(245, 158, 11, 0.25);
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.75rem;
  color: #92400e;
  flex-shrink: 0;
}

#chatFullBanner.visible { display: flex; }
#chatFullBanner i { color: #f59e0b; flex-shrink: 0; }



/* ══════════════════════════════════════════════
   BANNER ACCESO DENEGADO
══════════════════════════════════════════════ */
#chatAccessDenied {
  display: none;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 1rem;
  background: linear-gradient(135deg, rgba(30,58,138,0.12), rgba(30,58,138,0.06));
  border-bottom: 1px solid rgba(59,130,246,0.3);
  flex-shrink: 0;
}

#chatAccessDenied.visible { display: flex; }

#chatAccessDenied i {
  color: #3b82f6;
  flex-shrink: 0;
  font-size: 0.85rem;
}

#chatAccessDenied span {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.75rem;
  color: #3b82f6;
  line-height: 1.4;
}

/* ══════════════════════════════════════════════
   BANNER VETADO
══════════════════════════════════════════════ */
#chatBanBanner {
  display: none;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 1rem;
  background: linear-gradient(135deg, rgba(192,57,43,0.12), rgba(192,57,43,0.06));
  border-bottom: 1px solid rgba(192,57,43,0.3);
  flex-shrink: 0;
}

#chatBanBanner.visible { display: flex; }

#chatBanBanner i {
  color: #dc2626;
  flex-shrink: 0;
  font-size: 0.85rem;
}

#chatBanBanner #chatBanMsg {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.75rem;
  color: #dc2626;
  line-height: 1.4;
}

/* ══════════════════════════════════════════════
   BANNER COLA DE ESPERA
══════════════════════════════════════════════ */
#chatQueueBanner {
  display: none;
  flex-direction: column;
  gap: 0.3rem;
  padding: 0.75rem 1rem;
  background: linear-gradient(135deg,
    rgba(155, 44, 44, 0.08),
    rgba(212, 175, 55, 0.08));
  border-bottom: 1px solid rgba(212, 175, 55, 0.25);
  flex-shrink: 0;
}

#chatQueueBanner.visible { display: flex; }

.chat-queue-top {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-burgundy, #9b2c2c);
}

.chat-queue-spin {
  animation: hourglassSpin 2s ease-in-out infinite;
  color: var(--accent-gold, #d4af37);
}

@keyframes hourglassSpin {
  0%, 45%  { transform: rotate(0deg); }
  55%, 100%{ transform: rotate(180deg); }
}

.chat-queue-pos {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary, #4a4a4a);
  padding-left: 1.5rem;
}

.chat-queue-note {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.72rem;
  color: var(--text-tertiary, #6a6a6a);
  padding-left: 1.5rem;
  line-height: 1.4;
}

/* ══════════════════════════════════════════════
   ÁREA DE MENSAJES
══════════════════════════════════════════════ */
#chatMessages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  scroll-behavior: smooth;
}

#chatMessages::-webkit-scrollbar { width: 4px; }
#chatMessages::-webkit-scrollbar-track { background: transparent; }
#chatMessages::-webkit-scrollbar-thumb {
  background: var(--border-color, #d1d5db);
  border-radius: 999px;
}

/* Estado vacío / cargando */
.chat-state-msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-tertiary, #6a6a6a);
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.875rem;
  gap: 0.75rem;
}

.chat-state-msg i {
  font-size: 2.5rem;
  opacity: 0.35;
}

/* ── Burbujas de mensaje ── */
.chat-msg {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
  animation: msgIn 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  max-width: 100%;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.chat-msg.own { flex-direction: row-reverse; }

.chat-msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  margin-top: 2px;
}

.chat-msg-avatar-fallback {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-burgundy, #9b2c2c), var(--accent-gold, #d4af37));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
  font-family: var(--font-sans, 'Inter', sans-serif);
  flex-shrink: 0;
  margin-top: 2px;
}

.chat-msg-bubble {
  max-width: calc(100% - 44px);
  min-width: 60px;
}

.chat-msg-name {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-tertiary, #6a6a6a);
  margin-bottom: 0.15rem;
  padding: 0 0.5rem;
}

.chat-msg.own .chat-msg-name { text-align: right; }

.chat-msg-name.is-admin { color: var(--accent-burgundy, #9b2c2c); }

.chat-msg-text {
  background: var(--card-bg, #ffffff);
  border: 1px solid var(--border-color, #d1d5db);
  border-radius: 12px 12px 12px 4px;
  padding: 0.5rem 0.75rem;
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.875rem;
  color: var(--text-primary, #1a1a1a);
  line-height: 1.45;
  word-break: break-word;
}

.chat-msg.own .chat-msg-text {
  background: linear-gradient(135deg, var(--accent-burgundy, #9b2c2c), #7c2d12);
  color: white;
  border-color: transparent;
  border-radius: 12px 12px 4px 12px;
}

.chat-msg.is-admin-msg .chat-msg-text {
  background: linear-gradient(135deg, rgba(212,175,55,0.12), rgba(155,44,44,0.08));
  border-color: rgba(212, 175, 55, 0.35);
}

.chat-msg-time {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.65rem;
  color: var(--text-quaternary, #9ca3af);
  padding: 0.1rem 0.5rem 0;
  display: block;
  text-align: right;
}

/* ══════════════════════════════════════════════
   INPUT
══════════════════════════════════════════════ */
#chatInputArea {
  padding: 0.75rem;
  border-top: 1px solid var(--border-color, #d1d5db);
  background: var(--panel-bg, #fafafa);
  border-radius: 0 0 16px 16px;
  flex-shrink: 0;
}

@media (max-width: 768px) {
  #chatInputArea {
    border-radius: 0;
    padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
  }
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

#chatInput {
  flex: 1;
  border: 1.5px solid var(--input-border, #d1d5db);
  background: var(--input-bg, #ffffff);
  border-radius: 20px;
  padding: 0.55rem 0.875rem;
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.875rem;
  color: var(--text-primary, #1a1a1a);
  resize: none;
  max-height: 80px;
  min-height: 36px;
  transition: border-color 0.2s, box-shadow 0.2s;
  line-height: 1.4;
  overflow-y: auto;
}

#chatInput::placeholder { color: var(--text-tertiary, #6a6a6a); }

#chatInput:focus {
  outline: none;
  border-color: var(--accent-burgundy, #9b2c2c);
  box-shadow: 0 0 0 3px rgba(155, 44, 44, 0.1);
}

#chatInput:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  background: var(--bg-hover, #f3f4f6);
}

#chatSendBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--accent-burgundy, #9b2c2c), var(--accent-gold, #d4af37));
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  flex-shrink: 0;
  transition: transform 0.2s, opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
}

#chatSendBtn:hover  { transform: scale(1.08); }
#chatSendBtn:active { transform: scale(0.93); }

#chatSendBtn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

#chatCharCount {
  font-family: var(--font-sans, 'Inter', sans-serif);
  font-size: 0.68rem;
  color: var(--text-quaternary, #9ca3af);
  text-align: right;
  margin-top: 0.25rem;
  padding-right: 0.25rem;
  transition: color 0.2s;
}

#chatCharCount.warn { color: #f59e0b; }
#chatCharCount.over { color: #dc2626; font-weight: 600; }

/* ══════════════════════════════════════════════
   OVERLAY MÓVIL
══════════════════════════════════════════════ */
#chatOverlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9996;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

#chatOverlay.visible { display: block; }

@media (min-width: 769px) {
  /* En desktop NO usamos overlay — el cierre al clic fuera lo maneja JS */
  #chatOverlay { display: none !important; }
}

/* ══════════════════════════════════════════════
   ACCESIBILIDAD
══════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  #chatFab,
  #chatFab.has-activity,
  .chat-fab-bubbles .chat-bubble-a,
  .chat-fab-bubbles .chat-bubble-b,
  #chatPanel,
  .chat-msg,
  .chat-queue-spin {
    transition: none;
    animation: none;
  }
}