/* Chat System - WhatsApp Style UI */
/* Integra-se ao design system existente (glassmorphism, dark/light/auto themes) */

/* Variáveis de tema herdadas do style.css */
.chat-container {
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: 1rem;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ===== Mobile: Full-screen chat ===== */
@media (max-width: 1024px) {
  .chat-mobile {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-height: 100vh;
  }

  .chat-header {
    display: flex;
    flex-direction: column;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--glass-border);
    background: rgba(255, 255, 255, 0.03);
    height: auto;
  }

  .chat-header h2 {
    font-size: 1.25rem;
    margin-bottom: 0.25rem;
  }

  .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
  }

  .chat-input-area {
    padding: 1rem;
    border-top: 1px solid var(--glass-border);
    background: rgba(255, 255, 255, 0.03);
    flex-shrink: 0;
  }
}

/* ===== Desktop: Half-screen with sidebar ===== */
@media (min-width: 1025px) {
  .chat-desktop {
    display: flex;
    flex-direction: row;
    height: 80vh;
    max-height: 80vh;
  }

  .chat-sidebar {
    width: 280px;
    min-width: 260px;
    border-right: 1px solid var(--glass-border);
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.03);
    overflow: hidden;
  }

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

  .sidebar-header h3 {
    font-size: 1.1rem;
    margin: 0;
  }

  .sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
  }

  .sidebar-section {
    margin-bottom: 1rem;
  }

  .sidebar-section-title {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    padding: 0.5rem 1rem;
    font-weight: 600;
  }

  .sidebar-users-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0 0.5rem;
  }

  .sidebar-user-item:hover {
    background: rgba(255, 255, 255, 0.05);
  }

  .sidebar-user-item.inactive:hover {
    background: rgba(255, 255, 255, 0.03);
  }

  /* Item da lista de usuários: UMA ÚNICA LINHA (ponto + @user + tempo à direita) */
  .sidebar-user-item {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.25rem 0.5rem;
    min-width: 0;
  }

  .sidebar-username {
    font-size: 0.85rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
  }

  .sidebar-user-item.inactive .sidebar-username {
    color: var(--text-muted);
  }

  .sidebar-since {
    margin-left: auto;
    padding-left: 0.5rem;
    font-size: 0.7rem;
    color: var(--text-muted);
    white-space: nowrap;
    flex-shrink: 0;
  }

  .chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
}

/* ===== Chat Header ===== */
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.03);
  flex-shrink: 0;
}

.chat-header h2 {
  font-size: 1.1rem;
  margin: 0;
}

/* ===== Chat Messages (WhatsApp style) ===== */
.chat-messages-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 1rem;
}

/* Container de mensagens (desktop: é o próprio elemento de scroll).
   Garante afastamento de 2px entre os cards em ambos os layouts. */
.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 1rem;
}

/* Messages from the current user on the RIGHT */
.chat-message {
  max-width: 70%;
  word-wrap: break-word;
  word-break: break-word;
  padding: 2px;
}

.chat-message .message-bubble {
  display: flex;
  flex-direction: column;
  padding: 0.6rem 0.85rem;
  border-radius: 1.1rem;
  font-size: 0.875rem;
  line-height: 1.4;
  position: relative;
}

/* Own messages: aligned right, primary color background */
.chat-message.own {
  margin-left: auto;
  text-align: right;
}

.chat-message.own .message-bubble {
  background: rgba(var(--primary-rgb), 0.2);
  border: 1px solid rgba(var(--primary-rgb), 0.3);
  border-bottom-right-radius: 0.35rem;
  color: var(--text-primary);
}

/* Other messages: aligned left, secondary background */
.chat-message.other {
  margin-right: auto;
}

.chat-message.other .message-bubble {
  background: var(--bg-panel);
  border: 1px solid var(--glass-border);
  border-bottom-left-radius: 0.35rem;
  color: var(--text-primary);
}

/* Message metadata */
.message-sender {
  font-size: 0.7rem;
  opacity: 0.7;
  margin-bottom: 0.2rem;
}

.message-time {
  font-size: 0.65rem;
  opacity: 0.5;
  margin-top: 0.3rem;
  text-align: right;
}

/* ===== Quick mention indicator ===== */
.mention-badge {
  position: absolute;
  top: -0.4rem;
  right: -0.4rem;
  background: var(--primary);
  color: white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* ===== User List ===== */
/* Ponto de status (substitui a letra de iniciais): verde = online, cinza = offline */
.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}
.status-dot.online {
  background: #22c55e;
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.6);
}
.status-dot.offline {
  background: var(--text-muted);
  opacity: 0.6;
}

/* Aviso de retenção de 90 dias (exibido abaixo do cabeçalho do chat) */
.chat-retention-note {
  flex-shrink: 0;
  padding: 0.4rem 1rem;
  font-size: 0.7rem;
  line-height: 1.3;
  text-align: center;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.02);
  border-bottom: 1px solid var(--glass-border);
}

.user-list-sidebar {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.user-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.03);
  flex-shrink: 0;
}

.user-list-header h3 {
  font-size: 1.1rem;
  margin: 0;
}

.user-list-content {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0;
}

.user-list-section {
  margin-bottom: 1rem;
}

.user-list-section-title {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0.5rem 1rem;
  font-weight: 600;
}

.user-list-users {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0 0.5rem;
}

.user-list-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.user-list-item.inactive:hover {
  background: rgba(255, 255, 255, 0.03);
}

/* ===== Input Area ===== */
.chat-input-area {
  padding: 1rem;
  border-top: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.03);
  flex-shrink: 0;
}

.chat-input-area form {
  display: flex;
  gap: 0.5rem;
}

.chat-input-area input[type="text"] {
  flex: 1;
  padding: 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid var(--glass-border);
  background: var(--bg-panel);
  color: var(--text-primary);
  font-size: 0.875rem;
  outline: none;
  transition: all 0.2s ease;
}

.chat-input-area input[type="text"]:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--input-focus-shadow);
  background: var(--bg-card);
}

/* ===== Theme Support ===== */
body.dark-theme .chat-container,
body.dark-theme .chat-header,
body.dark-theme .chat-input-area,
body.dark-theme .user-list-sidebar,
body.dark-theme .sidebar-header {
  background: rgba(30, 41, 59, 0.7);
  border-color: rgba(255, 255, 255, 0.1);
}

body.light-theme .chat-container,
body.light-theme .chat-header,
body.light-theme .chat-input-area,
body.light-theme .user-list-sidebar,
body.light-theme .sidebar-header {
  background: rgba(255, 255, 255, 0.7);
  border-color: rgba(0, 0, 0, 0.1);
}

body.auto-theme .chat-container,
body.auto-theme .chat-header,
body.auto-theme .chat-input-area,
body.auto-theme .user-list-sidebar,
body.auto-theme .sidebar-header {
  background: rgba(30, 41, 59, 0.7);
  border-color: rgba(255, 255, 255, 0.1);
}

@media (prefers-color-scheme: dark) {
  body.auto-theme .chat-container,
  body.auto-theme .chat-header,
  body.auto-theme .chat-input-area,
  body.auto-theme .user-list-sidebar,
  body.auto-theme .sidebar-header {
    background: rgba(30, 41, 59, 0.7);
    border-color: rgba(255, 255, 255, 0.1);
  }
}

@media (prefers-color-scheme: light) {
  body.auto-theme .chat-container,
  body.auto-theme .chat-header,
  body.auto-theme .chat-input-area,
  body.auto-theme .user-list-sidebar,
  body.auto-theme .sidebar-header {
    background: rgba(255, 255, 255, 0.7);
    border-color: rgba(0, 0, 0, 0.1);
  }
}

/* ===== Mobile touch adjustments ===== */
@media (max-width: 1024px) {
  .chat-input-area input[type="text"] {
    font-size: 16px; /* Prevents iOS zoom */
    padding: 0.75rem;
  }

  .chat-input-area .btn {
    min-height: 44px;
    padding: 0.75rem 1.25rem;
  }

  .chat-message {
    max-width: 80%;
  }
}

/* ===== Custom scrollbar for chat messages ===== */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

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

.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
}

.light-theme .chat-messages::-webkit-scrollbar-thumb,
body.light-theme .chat-messages::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: var(--primary);
}

/* ===== Pagination styling ===== */
.chat-pagination {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 0.75rem;
  border-top: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.03);
}

.chat-pagination .page-btn {
  min-width: 2rem;
  padding: 0.25rem 0.5rem;
  font-size: 0.8rem;
  border-radius: 0.4rem;
  border: 1px solid var(--glass-border);
  background: var(--bg-panel);
  color: var(--text-primary);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.chat-pagination .page-btn:hover:not(:disabled) {
  border-color: var(--primary);
}

.chat-pagination .page-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

.chat-pagination .page-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.chat-pagination .page-ellipsis {
  color: var(--text-muted);
  padding: 0 0.2rem;
}

.chat-pagination .page-info {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-left: 0.5rem;
}

/* Pill de "novas mensagens" — leva à última página ao clicar */
.chat-newpill {
  flex-shrink: 0;
  align-self: center;
  margin: 0.4rem auto 0;
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  user-select: none;
  text-align: center;
}

.chat-newpill:hover {
  filter: brightness(1.08);
}

/* ===== Empty state ===== */
.chat-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}

.chat-empty i {
  width: 3rem;
  height: 3rem;
  opacity: 0.3;
  margin-bottom: 1rem;
}