/* ===== WhatsApp Panel CSS ===== */

/* Sidebar — Conversas */
.wa-panel {
  display: flex;
  height: calc(100vh - var(--header-height));
  height: calc(100dvh - var(--header-height));
  position: fixed;
  top: var(--header-height);
  right: 0;
  width: var(--sidebar-width);
  background: var(--bg-solid);
  border-left: 1px solid var(--glass-border);
  z-index: 900;
  flex-direction: column;
  transition: transform 0.3s ease;
}

/* Main content offset when panel open (desktop) */
.main-wrapper.with-wa {
  margin-right: var(--sidebar-width);
}

/* ===== Conversa List ===== */
.wa-header {
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--divider);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.wa-header h3 {
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.wa-header .wa-count {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
}

.wa-refresh {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 16px;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.wa-refresh:hover {
  color: var(--primary);
  background: var(--bg-card);
}

.wa-refresh.loading {
  animation: spin 0.8s linear infinite;
}

.wa-search {
  padding: var(--space-sm) var(--space-lg);
  border-bottom: 1px solid var(--divider);
  display: flex;
  gap: var(--space-xs);
  align-items: center;
}

.wa-search input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-full);
  padding: var(--space-sm) var(--space-md);
  color: var(--text-primary);
  font-size: 13px;
  font-family: var(--font-family);
  outline: none;
  transition: var(--transition);
}

/* Botão Filtro Não Lidas */
.wa-filter-unread {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-input);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-full);
  padding: 6px 10px;
  color: var(--text-secondary);
  font-size: 12px;
  font-family: var(--font-family);
  cursor: pointer;
  white-space: nowrap;
  transition: var(--transition);
}

.wa-filter-unread:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.wa-filter-unread.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.wa-filter-unread .filter-count {
  background: var(--danger, #ef4444);
  color: #fff;
  border-radius: 10px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 600;
  min-width: 18px;
  text-align: center;
}

.wa-filter-unread.active .filter-count {
  background: rgba(255,255,255,0.3);
}

.wa-search input:focus {
  border-color: var(--primary);
}

.wa-search input::placeholder {
  color: var(--text-muted);
}

.wa-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.wa-contact {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  cursor: pointer;
  border-bottom: 1px solid var(--divider);
  transition: var(--transition);
}

.wa-contact:hover {
  background: var(--bg-card-hover);
}

.wa-contact.unread {
  background: rgba(227, 30, 36, 0.12);
  border-left: 3px solid var(--primary);
}

.wa-contact.unread:hover {
  background: rgba(227, 30, 36, 0.18);
}

.wa-contact.unread .wa-contact-name {
  font-weight: 700;
}

.wa-contact.active {
  background: var(--primary-light);
  border-left: 3px solid var(--primary);
}

.wa-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
  text-transform: uppercase;
}

.wa-contact-info {
  flex: 1;
  min-width: 0;
}

.wa-contact-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.wa-contact-name.no-name {
  color: var(--text-muted);
  font-weight: 400;
}

.wa-contact-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.wa-contact-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}

.wa-contact-time {
  font-size: 10px;
  color: var(--text-muted);
}

.wa-badge {
  background: var(--success);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: var(--radius-full);
  min-width: 18px;
  text-align: center;
}

/* ===== Chat View ===== */
.wa-chat {
  display: none;
  flex-direction: column;
  height: 100%;
}

.wa-chat.active {
  display: flex;
}

.wa-chat-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--divider);
  background: var(--bg-header);
}

.wa-chat-back {
  display: none;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 20px;
  padding: var(--space-xs);
}

.wa-chat-header .wa-avatar {
  width: 36px;
  height: 36px;
  font-size: 14px;
}

.wa-chat-header-info {
  flex: 1;
  min-width: 0;
}

.wa-chat-name {
  font-size: 15px;
  font-weight: 600;
}

.wa-chat-phone {
  font-size: 12px;
  color: var(--text-muted);
}

.wa-chat-toggle {
  background: none;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-sm);
  padding: var(--space-xs) var(--space-sm);
  font-size: 11px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: var(--transition);
}

.wa-chat-toggle:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.wa-chat-toggle.off {
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.3);
}

.wa-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md) var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.wa-msg {
  max-width: 80%;
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  position: relative;
}

.wa-msg.inbound {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-bottom-left-radius: var(--space-xs);
}

.wa-msg.outbound {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: var(--space-xs);
}

.wa-msg-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
  text-align: right;
}

.wa-msg.outbound .wa-msg-time {
  color: rgba(255, 255, 255, 0.6);
}

.wa-msg-provider {
  font-size: 9px;
  color: var(--text-muted);
  font-style: italic;
}

.wa-msg-date-divider {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  padding: var(--space-sm) 0;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* ===== Chat Input ===== */
.wa-input-area {
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  border-top: 1px solid var(--divider);
  background: var(--bg-header);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.wa-input-area.drag-over {
  background: var(--primary-light);
  border-top-color: var(--primary);
}

.wa-input-area.drag-over::after {
  content: '📎 Solte o arquivo aqui';
  position: absolute;
  top: -40px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary);
  color: #fff;
  padding: 6px 16px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: var(--shadow-md);
}

.wa-attach-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: var(--transition);
  flex-shrink: 0;
  padding: 0;
}

.wa-attach-btn:hover {
  color: var(--primary);
  background: var(--bg-card-hover);
}

.wa-attach-menu {
  position: absolute;
  bottom: 50px;
  left: var(--space-lg);
  background: var(--bg-solid);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  padding: var(--space-sm);
  display: none;
  flex-direction: column;
  gap: var(--space-xs);
  box-shadow: var(--shadow-lg);
  z-index: 20;
  min-width: 160px;
}

.wa-attach-menu.visible {
  display: flex;
}

.wa-attach-menu button {
  background: none;
  border: none;
  color: var(--text-primary);
  padding: var(--space-sm) var(--space-md);
  text-align: left;
  cursor: pointer;
  font-size: 13px;
  border-radius: var(--radius-sm);
  transition: var(--transition);
  font-family: var(--font-family);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.wa-attach-menu button:hover {
  background: var(--bg-card-hover);
}

.wa-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  color: var(--text-primary);
  font-size: 14px;
  font-family: var(--font-family);
  outline: none;
  resize: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.4;
  transition: var(--transition);
}

.wa-input:focus {
  border-color: var(--primary);
}

.wa-send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
  flex-shrink: 0;
}

.wa-send-btn:hover {
  transform: scale(1.05);
}

.wa-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ===== Loading & Empty States ===== */
.wa-loading {
  text-align: center;
  padding: var(--space-xl);
  color: var(--text-muted);
  font-size: 13px;
}

.wa-loading .spinner-sm {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 3px solid var(--glass-border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: var(--space-md);
}

.wa-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: var(--space-md);
  color: var(--text-muted);
  text-align: center;
  padding: var(--space-xl);
}

.wa-empty .icon {
  font-size: 48px;
}

.wa-empty .title {
  font-size: 15px;
  color: var(--text-secondary);
}

.wa-empty .hint {
  font-size: 12px;
}

/* ===== Toggle Button (open/close panel on desktop) ===== */
.wa-toggle-btn {
  position: fixed;
  top: calc(var(--header-height) + var(--space-md));
  right: var(--space-md);
  z-index: 950;
  background: var(--primary);
  border: none;
  border-radius: var(--radius-full);
  width: 52px;
  height: 52px;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  transition: var(--transition);
  color: #fff;
}

.wa-toggle-btn:hover {
  transform: scale(1.05);
}

.wa-toggle-btn .badge {
  position: absolute;
  top: -2px;
  right: -2px;
  background: var(--success);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: var(--radius-full);
  min-width: 20px;
  text-align: center;
}

/* ===== Agent Toggle + WhatsApp Link ===== */
.wa-agent-toggle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: var(--success);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  line-height: 1;
}

.wa-agent-toggle:hover {
  transform: scale(1.1);
}

.wa-agent-toggle.off {
  background: rgba(255, 255, 255, 0.2);
  color: var(--text-muted);
}

.wa-wa-link {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #25D366;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  transition: var(--transition);
  flex-shrink: 0;
  font-size: 12px;
  padding: 0;
  line-height: 1;
}

.wa-wa-link::before {
  content: 'W';
  color: #fff;
  font-weight: 800;
  font-size: 9px;
}

.wa-wa-link:hover {
  transform: scale(1.1);
}

/* ===== Connection Indicator ===== */
.conn-indicator {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-left: var(--space-xs);
  vertical-align: middle;
}

.conn-indicator.online {
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
}

.conn-indicator.offline {
  background: var(--danger);
  box-shadow: 0 0 6px var(--danger);
  animation: pulse-offline 1.5s ease infinite;
}

@keyframes pulse-offline {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ===== Typing Indicator (visual feedback) ===== */
.wa-typing {
  font-size: 11px;
  color: var(--text-muted);
  padding: var(--space-xs) var(--space-md);
  font-style: italic;
  display: none;
}

.wa-typing.visible {
  display: block;
}

/* ===== New Message Animation ===== */
@keyframes msgSlideIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.wa-msg {
  animation: msgSlideIn 0.25s ease;
}

/* ===== Pulse for new badge ===== */
@keyframes badgePulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.wa-badge {
  animation: badgePulse 0.5s ease;
}

/* ===== Responsive — Mobile ===== */
@media (max-width: 1024px) {
  .wa-panel {
    width: 100%;
    z-index: 1100;
    transform: translateX(100%);
    height: calc(100vh - var(--header-height));
    height: calc(100dvh - var(--header-height));
  }

  .wa-panel.open {
    transform: translateX(0);
  }

  .wa-panel.mode-chat .wa-list-view {
    display: none;
  }

  .wa-panel.mode-chat .wa-chat {
    display: flex;
  }

  .wa-panel.mode-chat .wa-chat-back {
    display: flex;
  }

  .main-wrapper.with-wa {
    margin-right: 0;
  }

  .wa-toggle-btn {
    bottom: var(--space-lg);
    top: auto;
    right: var(--space-lg);
  }

  /* Input area fixa no rodapé do painel */
  .wa-chat {
    height: 100%;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .wa-messages {
    flex: 1;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
  }

  .wa-input-area {
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--space-sm));
  }

  .wa-input {
    font-size: 16px;
  }
}

@media (min-width: 1025px) {
  .wa-toggle-btn {
    display: none;
  }

  /* Desktop: panel always visible */
  .wa-panel {
    transform: translateX(0);
  }

  .main-wrapper {
    margin-right: var(--sidebar-width);
  }

  /* Desktop: show list and chat side by side within sidebar */
  .wa-panel.mode-list {
    /* default — show list */
  }

  .wa-chat {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-solid);
    z-index: 10;
  }

  .wa-chat.active {
    display: flex;
  }

  .wa-chat-back {
    display: flex !important;
  }
}

/* ===== CRM Slide-Over Panel ===== */
.crm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease;
  z-index: 1200;
}
.crm-overlay.active { opacity: 1; pointer-events: auto; }

.crm-panel {
  position: fixed;
  top: 0;
  right: -440px;
  width: 420px;
  max-width: 92vw;
  height: 100dvh;
  background: var(--bg-solid);
  border-left: 1px solid var(--glass-border);
  box-shadow: -8px 0 40px rgba(0,0,0,0.6);
  z-index: 1201;
  transition: right .35s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}
.crm-panel.active { right: 0; }

.crm-header {
  position: sticky;
  top: 0;
  background: var(--bg-header);
  backdrop-filter: blur(15px);
  border-bottom: 1px solid var(--divider);
  padding: 16px 20px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
}
.crm-header-info { flex: 1; min-width: 0; }
.crm-header-name {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.crm-header-doc { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.crm-close {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  color: var(--text-primary);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: var(--transition);
}
.crm-close:hover { background: rgba(227,30,36,0.3); border-color: var(--primary); }

.crm-body { padding: 16px 20px 40px; }

/* Scores */
.crm-scores { display: flex; gap: 12px; margin-bottom: 20px; }
.crm-score-card {
  flex: 1;
  border-radius: 14px;
  padding: 14px;
  text-align: center;
  border: 1px solid var(--glass-border);
}
.crm-score-card.interno { background: rgba(34,197,94,0.08); }
.crm-score-card.externo { background: rgba(59,130,246,0.08); }
.crm-score-label { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); margin-bottom: 4px; }
.crm-score-value { font-size: 28px; font-weight: 800; color: var(--text-primary); }
.crm-score-value.verde { color: #22c55e; }
.crm-score-value.amarelo { color: #f59e0b; }
.crm-score-value.vermelho { color: #ef4444; }
.crm-score-value.cinza { color: #666; }
.crm-score-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

/* Sections */
.crm-section { margin-bottom: 22px; }
.crm-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--primary);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(227,30,36,0.2);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Info rows */
.crm-info-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
.crm-info-label { color: var(--text-muted); font-size: 13px; }
.crm-info-value { color: var(--text-primary); font-size: 13px; font-weight: 500; text-align: right; word-break: break-all; }

/* Boleto card */
.crm-boleto {
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 8px;
}
.crm-boleto.vencido { border-color: rgba(239,68,68,0.3); background: rgba(239,68,68,0.05); }
.crm-boleto-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.crm-boleto-venda { font-weight: 700; font-size: 14px; color: var(--text-primary); }
.crm-boleto-status { font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 600; }
.crm-boleto-status.pago { background: rgba(34,197,94,0.15); color: #22c55e; }
.crm-boleto-status.vencido { background: rgba(239,68,68,0.15); color: #ef4444; }
.crm-boleto-status.aberto { background: rgba(245,158,11,0.15); color: #f59e0b; }
.crm-boleto-info { font-size: 12px; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 12px; }
.crm-boleto-link { margin-top: 6px; }
.crm-boleto-link a { color: var(--primary); text-decoration: none; font-size: 12px; font-weight: 600; }
.crm-boleto-link a:hover { text-decoration: underline; }

/* Processo card */
.crm-processo {
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  font-size: 12px;
}
.crm-processo-numero { font-weight: 700; color: var(--text-primary); }
.crm-processo-info { color: var(--text-muted); margin-top: 2px; }

/* Envio card */
.crm-envio {
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  font-size: 12px;
}
.crm-envio-tracking { font-weight: 700; color: var(--text-primary); }
.crm-envio-status { color: var(--text-muted); margin-top: 2px; }
.crm-envio-link a { color: var(--primary); text-decoration: none; font-weight: 600; }

/* Empty/loading states */
.crm-empty { text-align: center; color: var(--text-muted); font-size: 13px; padding: 12px; font-style: italic; }
.crm-loading { text-align: center; padding: 40px; color: var(--text-muted); }
.crm-total-divida {
  background: rgba(239,68,68,0.08);
  border: 1px solid rgba(239,68,68,0.2);
  border-radius: 10px;
  padding: 12px;
  margin-top: 8px;
  text-align: center;
}
.crm-total-divida .valor { font-size: 22px; font-weight: 800; color: #ef4444; }
.crm-total-divida .label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px; }

/* Credenciais */
.crm-credencial {
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  font-size: 13px;
}
.crm-credencial-link { color: var(--primary); text-decoration: none; font-weight: 600; font-size: 12px; }
