:root {
  --panel-primary: #128c7e;
  --panel-primary-dark: #0f6e63;
  --bg: #eae6df;
  --sidebar-bg: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --bubble-system: #fff3cd;
  --text: #111b21;
  --text-muted: #667781;
  --border: #e9edef;
}

* { box-sizing: border-box; }

/* El atributo [hidden] debe ganar sobre las reglas con display:flex/block de
   abajo — si no, las vistas marcadas hidden desde el JS seguirían visibles. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
}

/* ---------- Login ---------- */
.login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: var(--bg);
}
.login-card {
  background: #fff;
  padding: 32px;
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.1);
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-sub { margin: 0 0 6px; color: var(--text-muted); font-size: 13px; }
.login-card label { display: flex; flex-direction: column; font-size: 13px; gap: 4px; color: var(--text-muted); }
.login-card input {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.login-card button {
  margin-top: 8px;
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--panel-primary);
  color: #fff;
  font-size: 15px;
  cursor: pointer;
}
.login-card button:hover { background: var(--panel-primary-dark); }
.login-error { color: #c0392b; font-size: 13px; margin: 0; }

/* ---------- Panel ---------- */
.panel-view { display: flex; height: 100%; background: var(--bg); overflow: hidden; }

.sidebar {
  width: 400px;
  min-width: 340px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  /* min-height:0 permite que .conversation-list scrollee en vez de empujar
     el alto del sidebar — sin esto, el overflow-y:auto del list no actúa. */
  min-height: 0;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--panel-primary);
  color: #fff;
}
.brand { font-weight: 600; }
.sidebar-header-actions { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.18); }
#reload-btn { font-size: 20px; }
#reload-btn.spinning { animation: spin 0.6s linear; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.conversation-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
/* Nivel 2 (chats cerrados): área más corta con su propio scroll, para que el
   nivel de activos se quede con la mayor parte del alto. */
.closed-list {
  flex: 0 1 38%;
  max-height: 38%;
}
.conversation-item {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  align-items: center;
}
.conversation-item:hover { background: #f5f6f6; }
.conversation-item.active { background: #ebf5f3; }
/* Estado del chat por color de recuadro (borde izquierdo tipo WhatsApp):
   rojo/rosa = requiere asistencia humana (en el pool); azul = en atención por un asesor. */
.conversation-item.conv-needs-human {
  background: #fdecef;
  border-left: 4px solid #e0245e;
}

.conversation-item.conv-needs-human:hover {
  background: #fbdfe5;
}

.conversation-item.conv-attended {
  border-left: 4px solid #1d9bf0;
}

.conversation-item.active.conv-needs-human,
.conversation-item.active.conv-attended {
  background: #ebf5f3;
}
.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--panel-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}
.conversation-meta { flex: 1; min-width: 0; }
.conversation-name { font-weight: 600; font-size: 14px; display: flex; justify-content: space-between; }
.conversation-preview { color: var(--text-muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--panel-primary);
  color: #fff;
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 11px;
  margin-left: 6px;
}
.status-dot { font-size: 11px; margin-left: 6px; }
.status-human { color: #c0392b; }
.status-bot { color: var(--panel-primary); }
.status-closed {
  color: var(--text-muted);
}

.status-needs-human {
  color: #e0245e;
}

/* Mensaje de "vacío" dentro de las listas (ej. sin chats cerrados). */
.conversation-empty {
  padding: 14px 16px;
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
}

/* ---------- Chat ---------- */
.chat { flex: 1; display: flex; flex-direction: row; min-width: 0; min-height: 0; overflow: hidden; }
.chat-empty { margin: auto; color: var(--text-muted); }
.chat-active { flex: 1; display: flex; flex-direction: column; height: 100%; min-width: 0; min-height: 0; }
.chat-header-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  text-align: left;
  font: inherit;
}
.chat-header-info:hover .chat-title { text-decoration: underline; }
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--sidebar-bg);
  border-bottom: 1px solid var(--border);
}
.chat-title { font-weight: 600; display: block; }
.chat-status { font-size: 12px; color: var(--text-muted); }
/* Etiqueta en el encabezado: "Atiende: X" (azul) o "Requiere asistencia humana" (rojo). */
.chat-assignee {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 10px;
  white-space: nowrap;
}

.chat-assignee.attended {
  color: #1d9bf0;
  background: rgba(29, 155, 240, 0.12);
}

.chat-assignee.needs-human {
  color: #e0245e;
  background: rgba(224, 36, 94, 0.12);
}
.action-btn {
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  background: var(--panel-primary);
  color: #fff;
  cursor: pointer;
  font-size: 13px;
}
.action-btn.secondary { background: #8696a0; }
.action-btn.ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.action-btn.ghost:hover {
  background: rgba(0, 0, 0, 0.04);
  color: var(--text);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bubble {
  /* En esta columna flexible una burbuja no debe encogerse: las tarjetas (overflow: hidden) se aplastaban y quedaban solo con un
     pedazo de la foto cuando el hilo tenía muchos mensajes. */
  flex-shrink: 0;
  max-width: 68%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.35;
  white-space: pre-wrap;
  word-wrap: break-word;
}
/* Separador de fecha estilo WhatsApp: pastilla centrada entre mensajes. */
.day-divider {
  align-self: center;
  margin: 10px 0 4px;
}

.day-divider span {
  display: inline-block;
  padding: 5px 12px;
  background: var(--bubble-system, #fff);
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 12px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  text-transform: capitalize;
}
.bubble .meta { display: block; font-size: 11px; color: var(--text-muted); margin-top: 3px; text-align: right; }
.bubble.inbound .meta { text-align: left; }
.bubble.system .meta { text-align: center; }
/* En las tarjetas, la meta va dentro del cuerpo con un pequeño margen. */
.bubble.card-bubble .meta { padding: 0 12px 8px; margin-top: 0; }
.bubble code { font-family: Consolas, "Courier New", monospace; background: rgba(0,0,0,0.06); padding: 1px 4px; border-radius: 4px; font-size: 13px; }
.bubble.inbound { align-self: flex-start; background: var(--bubble-in); }
.bubble.outbound { align-self: flex-end; background: var(--bubble-out); }
.bubble.system { align-self: center; background: var(--bubble-system); font-size: 12px; color: #665c00; }
.bubble.operator { border-right: 3px solid var(--panel-primary); }

/* Constancia de adjunto enviado al cliente (no es el archivo, solo el aviso). */
.bubble.doc-bubble { background: var(--bubble-out); max-width: 85%; }
.doc-note { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text); line-height: 1.35; min-width: 0; }
.doc-ico { font-size: 26px; line-height: 1; flex: none; }
.doc-info { display: flex; flex-direction: column; min-width: 0; }
.doc-name { font-weight: 600; word-break: break-all; }
.doc-type { font-size: 11px; opacity: 0.7; margin-top: 2px; }

/* La foto de una tarjeta caduca: cuando ya no carga, queda este recuadro con el ícono. */
.card-img-missing { display: flex; align-items: center; justify-content: center; height: 84px; background: #f0f0f0; font-size: 34px; }

/* Tarjetas (producto / asesor) */
.bubble.card-bubble { padding: 0; max-width: 300px; overflow: hidden; }
.card { display: flex; flex-direction: column; }
.card-img { width: 100%; max-height: 220px; object-fit: cover; background: #f0f0f0; }
.card-body { padding: 10px 12px; }
.card-title { font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.card-line { font-size: 13px; color: var(--text); line-height: 1.4; }
.card-btn {
  display: inline-block;
  margin-top: 10px;
  padding: 7px 14px;
  background: var(--panel-primary);
  color: #fff;
  text-decoration: none;
  border-radius: 6px;
  font-size: 13px;
}

.composer { padding: 10px 20px 14px; background: var(--sidebar-bg); border-top: 1px solid var(--border); }

/* Barra de control (tomar/devolver), debajo del hilo y sobre la caja de texto. */
.control-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 0 10px;
}
.observing-note { flex: 1; color: var(--text-muted); font-size: 13px; }
.action-btn.danger { background: #d9342b; }
.action-btn.danger:hover { background: #b82a22; }

.composer-form { display: flex; gap: 10px; }
.composer-form input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 14px;
}
.composer-form input:disabled { background: #f0f2f5; color: var(--text-muted); cursor: not-allowed; }
.send-btn {
  padding: 0 20px;
  border: none;
  border-radius: 20px;
  background: var(--panel-primary);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
}
.send-btn:disabled { background: #a8c5c0; cursor: not-allowed; }

/* ---------- Modal de inactividad ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  background: #fff;
  border-radius: 12px;
  padding: 26px;
  width: 380px;
  max-width: 90vw;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
}
.modal h3 { margin: 0 0 10px; font-size: 18px; }
.modal p { margin: 0 0 20px; color: var(--text-muted); font-size: 14px; line-height: 1.5; }
.modal p strong { color: #d9342b; font-size: 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal-label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
}

.modal-select,
.modal-textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  box-sizing: border-box;
  background: #fff;
  color: var(--text);
}

.modal-textarea {
  resize: vertical;
  min-height: 48px;
}

.modal-select:focus,
.modal-textarea:focus {
  outline: none;
  border-color: var(--panel-primary);
}

/* ---------- Panel lateral de información del contacto (estilo WhatsApp) ---------- */
.contact-panel {
  width: 340px;
  min-width: 300px;
  background: #f0f2f5;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
}
.contact-panel-header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 18px 20px;
  background: var(--sidebar-bg);
  color: var(--text);
  font-weight: 500;
  font-size: 15px;
  border-bottom: 1px solid var(--border);
}
.contact-panel-header .icon-btn { color: var(--text); font-size: 16px; }
.contact-panel-body { overflow-y: auto; display: flex; flex-direction: column; gap: 10px; padding-bottom: 20px; }

/* Hero: avatar grande centrado sobre fondo blanco, nombre y número debajo. */
.contact-hero {
  background: var(--sidebar-bg);
  padding: 28px 20px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.contact-avatar {
  width: 120px;
  height: 120px;
  font-size: 44px;
}
.contact-name { font-size: 20px; font-weight: 500; text-align: center; color: var(--text); }
.contact-phone { font-size: 15px; color: var(--text-muted); }

/* Bloques tipo tarjeta, separados por el fondo gris del panel. */
.contact-block { background: var(--sidebar-bg); padding: 14px 20px 16px; }
.contact-block-title { margin: 0 0 10px; font-size: 13px; font-weight: 500; color: var(--panel-primary); }
.contact-fields { margin: 0; display: flex; flex-direction: column; gap: 12px; }
.contact-fields .field dt { font-size: 12px; color: var(--text-muted); margin: 0 0 2px; }
.contact-fields .field dd { margin: 0; font-size: 15px; color: var(--text); word-break: break-word; }
.contact-fields .muted { color: var(--text-muted); font-style: italic; }

.sidebar-search { padding: 8px 12px; background: var(--sidebar-bg); border-bottom: 1px solid var(--border); }
/* Separador entre el nivel de activos y el de cerrados. */
.sidebar-divider {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 6px;
  background: var(--sidebar-bg);
  border-top: 2px solid var(--border);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.sidebar-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}
.sidebar-search input { width: 100%; box-sizing: border-box; padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; background: #fff; color: inherit; }
.conversation-client { font-size: 12px; font-weight: 600; color: var(--panel-primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-client { display: block; font-size: 12px; font-weight: 600; color: var(--panel-primary); }

/* Distintivo de quién escribe (cliente / asesor / administrador), junto al nombre en la lista y en el encabezado del chat. */
.role-tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  vertical-align: middle;
}
.role-cliente { background: #e3f2fd; color: #1565c0; }
.role-asesor { background: #fff3e0; color: #e65100; }
.role-admin { background: #ede7f6; color: #4527a0; }
