/* ============================================================
   chat.css — Estilos aislados del inbox de chat.
   Todas las clases llevan el prefijo `chat-`. No modifica styles.css.
   Objetivos táctiles >= 44px y contraste AA sobre el tema oscuro.
   ============================================================ */

.chat-app {
  --_chat-bg: var(--bg-card, #1c1f2e);
  --_chat-bg-alt: var(--bg-secondary, #161822);
  --_chat-border: var(--border-color, #2a2d3e);
  --_chat-text: var(--text-primary, #e8eaf0);
  --_chat-text-soft: #a9aec1;
  --_chat-accent: var(--accent, #4f8cff);
  --_chat-danger: #f87171;

  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 1px;
  height: clamp(460px, 72vh, 760px);
  min-height: 460px;
  overflow: hidden;
  background: var(--_chat-border);
  border: 1px solid var(--_chat-border);
  border-radius: var(--radius, 10px);
  color: var(--_chat-text);
  font-family: inherit;
}

.chat-app [hidden] {
  display: none !important;
}

/* ---------- Panel de conversaciones ---------- */

.chat-sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--_chat-bg-alt);
}

.chat-sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--_chat-border);
}

.chat-sidebar-title {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--_chat-text);
}

.chat-connection {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--_chat-text-soft);
  white-space: nowrap;
}

.chat-connection::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--_chat-danger);
}

.chat-connection.chat-online {
  color: #6ee7b7;
}

.chat-connection.chat-online::before {
  background: #34d399;
}

.chat-conversation-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.chat-conversation-items {
  display: flex;
  flex-direction: column;
}

.chat-sentinel {
  height: 1px;
}

.chat-conversation-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
  min-height: 64px;
  padding: 10px 14px;
  border: 0;
  border-bottom: 1px solid var(--_chat-border);
  border-left: 3px solid transparent;
  background: transparent;
  color: var(--_chat-text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.chat-conversation-item:hover {
  background: var(--bg-card-hover, #232738);
}

.chat-conversation-item.chat-active {
  background: rgba(79, 140, 255, 0.14);
  border-left-color: var(--_chat-accent);
}

.chat-conversation-item:focus-visible,
.chat-icon-btn:focus-visible,
.chat-send-btn:focus-visible,
.chat-load-more:focus-visible,
.chat-older:focus-visible,
.chat-input:focus-visible,
.chat-attachment-link:focus-visible {
  outline: 2px solid var(--border-focus, #4f8cff);
  outline-offset: 2px;
}

.chat-conversation-top,
.chat-conversation-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

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

.chat-conversation-time {
  flex: 0 0 auto;
  font-size: 11px;
  color: var(--_chat-text-soft);
}

.chat-conversation-preview {
  overflow: hidden;
  font-size: 12px;
  color: var(--_chat-text-soft);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: #dc2626;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}

.chat-load-more,
.chat-older {
  min-height: 44px;
  width: 100%;
  padding: 10px 14px;
  border: 0;
  border-top: 1px solid var(--_chat-border);
  background: transparent;
  color: var(--_chat-accent);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.chat-load-more:hover,
.chat-older:hover {
  background: var(--bg-card-hover, #232738);
}

.chat-older {
  border-top: 1px solid var(--_chat-border);
  border-bottom: 1px solid var(--_chat-border);
  text-align: center;
}

/* ---------- Hilo de mensajes ---------- */

.chat-thread {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--_chat-bg);
}

.chat-thread-head {
  display: flex;
  align-items: center;
  min-height: 56px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--_chat-border);
}

.chat-thread-titles {
  min-width: 0;
}

.chat-thread-title {
  margin: 0;
  overflow: hidden;
  font-size: 15px;
  font-weight: 700;
  color: var(--_chat-text);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-thread-subtitle {
  margin: 2px 0 0;
  overflow: hidden;
  font-size: 12px;
  color: var(--_chat-text-soft);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
}

.chat-message {
  display: flex;
  max-width: 78%;
}

.chat-message-in {
  align-self: flex-start;
}

.chat-message-out {
  align-self: flex-end;
}

.chat-bubble {
  padding: 9px 12px;
  border: 1px solid var(--_chat-border);
  border-radius: 12px;
  background: var(--bg-card-hover, #232738);
  color: var(--_chat-text);
  font-size: 13px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.chat-message-out .chat-bubble {
  border-color: rgba(79, 140, 255, 0.45);
  background: #1d355f;
  color: #f3f7ff;
}

.chat-message-pending .chat-bubble {
  opacity: 0.7;
}

.chat-message-failed .chat-bubble {
  border-color: var(--_chat-danger);
}

.chat-message-body {
  margin: 0;
  white-space: pre-wrap;
}

.chat-message-meta {
  display: block;
  margin-top: 5px;
  font-size: 10px;
  color: var(--_chat-text-soft);
}

.chat-message-out .chat-message-meta {
  color: #c9d9f7;
}

.chat-attachment {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}

.chat-attachment-name {
  font-size: 13px;
  font-weight: 600;
}

.chat-attachment-link {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0 6px;
  color: var(--_chat-accent);
  font-size: 12px;
  font-weight: 600;
  text-decoration: underline;
}

.chat-empty {
  margin: auto;
  max-width: 320px;
  color: var(--_chat-text-soft);
  font-size: 13px;
  text-align: center;
}

/* ---------- Composer ---------- */

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 16px 8px;
  border-top: 1px solid var(--_chat-border);
}

.chat-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border: 1px solid var(--_chat-border);
  border-radius: var(--radius-sm, 6px);
  background: transparent;
  color: var(--_chat-text);
  font-size: 18px;
  cursor: pointer;
}

.chat-icon-btn:hover {
  background: var(--bg-card-hover, #232738);
}

.chat-input {
  flex: 1 1 auto;
  min-height: 44px;
  max-height: 140px;
  padding: 11px 12px;
  resize: vertical;
  border: 1px solid var(--_chat-border);
  border-radius: var(--radius-sm, 6px);
  background: var(--_chat-bg-alt);
  color: var(--_chat-text);
  font: inherit;
  font-size: 13px;
}

.chat-input::placeholder {
  color: var(--_chat-text-soft);
  opacity: 0.85;
}

.chat-send-btn {
  flex: 0 0 auto;
  min-width: 88px;
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius-sm, 6px);
  background: var(--_chat-accent);
  color: #fff;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.chat-send-btn:hover {
  background: var(--accent-hover, #3a7af0);
}

.chat-status {
  min-height: 20px;
  margin: 0;
  padding: 0 16px 10px;
  font-size: 12px;
  color: var(--_chat-text-soft);
}

.chat-status-error {
  color: #ff9b9b;
  font-weight: 600;
}

.chat-status-warning {
  color: #fcd34d;
  font-weight: 600;
}

/* ---------- Sin permisos ---------- */

.chat-denied {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  justify-content: center;
  padding: 32px;
  background: var(--_chat-bg);
}

.chat-denied-title {
  margin: 0;
  font-size: 18px;
  color: var(--_chat-text);
}

.chat-denied-text {
  margin: 0;
  color: var(--_chat-text-soft);
  font-size: 14px;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .chat-app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(200px, 40%) minmax(0, 1fr);
    height: clamp(560px, 82vh, 860px);
  }

  .chat-sidebar {
    border-bottom: 1px solid var(--_chat-border);
  }

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

@media (max-width: 520px) {
  .chat-composer {
    flex-wrap: wrap;
  }

  .chat-input {
    flex-basis: calc(100% - 54px);
  }

  .chat-send-btn {
    flex-basis: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-conversation-item {
    transition: none;
  }
}
