/* Chat interno del panel · Fase 1
 * Usa los tokens del panel (panel.css). El modo oscuro funciona solo porque
 * todo se apoya en variables que panel-dark.css ya redefine. Mobile-first. */

.erp-content--flush { padding: 0 !important; }

.chat-app {
  display: flex;
  height: calc(100dvh - 84px);
  min-height: 460px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin: 12px;
}

/* ---- Panel izquierdo · conversaciones ---- */
.chat-aside {
  width: 320px;
  flex: 0 0 320px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}
.chat-aside-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 16px; border-bottom: 1px solid var(--border);
}
.chat-aside-head h1 { font-size: 20px; margin: 0; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

.chat-conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.chat-empty-hint { color: var(--text-muted); font-size: 13px; padding: 16px; line-height: 1.5; }

.chat-conv {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 10px 12px; border: 0; background: transparent; cursor: pointer;
  border-radius: var(--radius); text-align: left; color: var(--text);
  min-height: 56px; transition: background .12s ease;
}
.chat-conv:hover { background: var(--navy-50); }
.chat-conv.is-active { background: var(--navy-50); }
.chat-conv-avatar {
  flex: 0 0 40px; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
  background: var(--navy-600); color: #fff;
}
.chat-conv-avatar--canal { background: var(--green-dark); }
.chat-conv-avatar--ia { background: linear-gradient(135deg, var(--green-dark), #0EA5E9); }
.chat-conv-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.chat-conv-title {
  font-weight: 600; font-size: 14px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.chat-conv-preview {
  font-size: 12.5px; color: var(--text-muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.chat-conv-badge {
  flex: 0 0 auto; min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 10px; background: var(--green); color: #fff;
  font-size: 11px; font-weight: 700; display: grid; place-items: center;
}

/* ---- Panel derecho · hilo ---- */
.chat-thread { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-thread-empty {
  flex: 1; display: grid; place-items: center; padding: 24px;
  color: var(--text-muted); text-align: center;
}
.chat-thread-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.chat-thread-head {
  display: flex; align-items: center; gap: 10px; padding: 14px 18px;
  border-bottom: 1px solid var(--border); background: var(--bg-elevated);
}
.chat-thread-title { font-weight: 700; font-size: 16px; }
.chat-thread-sub { font-size: 12.5px; color: var(--text-muted); }
.chat-back {
  display: none; border: 0; background: transparent; cursor: pointer;
  font-size: 26px; line-height: 1; color: var(--text); padding: 0 4px;
}

.chat-messages {
  flex: 1; overflow-y: auto; padding: 18px;
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg);
}
.chat-msg { display: flex; }
.chat-msg--mine { justify-content: flex-end; }
.chat-msg-bubble {
  max-width: min(72%, 560px); padding: 8px 12px; border-radius: 14px;
  background: var(--bg-elevated); border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 2px;
}
.chat-msg--mine .chat-msg-bubble {
  background: var(--navy-600); border-color: var(--navy-600); color: #fff;
}
.chat-msg-author { font-size: 11.5px; font-weight: 700; color: var(--green-dark); }
.chat-msg-text { font-size: 14px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }
.chat-msg-time { font-size: 10.5px; color: var(--text-soft); align-self: flex-end; }
.chat-msg--mine .chat-msg-time { color: rgba(255,255,255,.7); }
.chat-msg--ia .chat-msg-bubble { background: var(--green-bg); border-color: #BBF7D0; }
.chat-msg--ia .chat-msg-author { color: var(--green-dark); }
.chat-typing { flex-direction: row !important; gap: 5px; padding: 12px 14px; }
.chat-typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--green-dark); opacity: .4; animation: chatBlink 1.2s infinite both; }
.chat-typing span:nth-child(2) { animation-delay: .2s; }
.chat-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes chatBlink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

.chat-composer {
  display: flex; align-items: flex-end; gap: 10px; padding: 12px 16px;
  border-top: 1px solid var(--border); background: var(--bg-elevated);
}
.chat-composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border-strong);
  border-radius: var(--radius); padding: 10px 12px; font: inherit;
  font-size: 16px; line-height: 1.4; max-height: 140px; background: var(--bg);
  color: var(--text);
}
.chat-composer textarea:focus { outline: none; border-color: var(--navy-500); }
.chat-send { padding: 10px 14px; }

/* ---- Modal nueva conversación ---- */
.chat-modal { position: fixed; inset: 0; z-index: 1200; display: flex; }
.chat-modal-backdrop { position: absolute; inset: 0; background: rgb(7 24 42 / .45); }
.chat-modal-box {
  position: relative; margin: auto; width: min(440px, 92vw);
  max-height: 86dvh; background: var(--bg-elevated); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; overflow: hidden;
}
.chat-modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
}
.chat-modal-x { border: 0; background: transparent; cursor: pointer; font-size: 16px; color: var(--text-muted); }
.chat-modal-tabs { display: flex; gap: 4px; padding: 12px 16px 0; }
.chat-tab {
  flex: 1; padding: 9px; border: 1px solid var(--border); background: var(--bg);
  border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; font-size: 13px;
  color: var(--text-muted);
}
.chat-tab.is-active { background: var(--navy-600); color: #fff; border-color: var(--navy-600); }
.chat-tabpane { padding: 14px 16px 18px; overflow-y: auto; }

.chat-user-search, .chat-canal-name {
  width: 100%; padding: 10px 12px; font-size: 16px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); margin-bottom: 10px; background: var(--bg); color: var(--text);
}
.chat-user-list { display: flex; flex-direction: column; gap: 2px; max-height: 46dvh; overflow-y: auto; }
.chat-user {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 8px 10px;
  border: 0; background: transparent; cursor: pointer; border-radius: var(--radius);
  text-align: left; color: var(--text); min-height: 48px;
}
.chat-user:hover { background: var(--navy-50); }
.chat-user--check { cursor: default; }
.chat-user--check input { width: 18px; height: 18px; accent-color: var(--green); }
.chat-user-avatar {
  flex: 0 0 34px; width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 13px;
  background: var(--navy-500); color: #fff;
}
.chat-user-meta { display: flex; flex-direction: column; min-width: 0; }
.chat-user-name { font-weight: 600; font-size: 14px; }
.chat-user-role { font-size: 11.5px; color: var(--text-muted); }

/* ---- Badge del icono de la topbar ---- */
.topbar-chat { position: relative; }
.topbar-chat-badge {
  position: absolute; top: -3px; right: -3px; min-width: 16px; height: 16px;
  padding: 0 4px; border-radius: 8px; background: var(--green); color: #fff;
  font-size: 10px; font-weight: 700; display: grid; place-items: center; line-height: 1;
}

/* ===================================================================== */
/* WIDGET FLOTANTE · botón pequeño + panel overlay (no muy grande)        */
/* ===================================================================== */
.cw { position: fixed; right: 22px; bottom: 22px; z-index: 1100; }

/* Launcher · botón pequeño */
.cw-launcher {
  position: relative; width: 48px; height: 48px; border-radius: 50%;
  border: 0; cursor: pointer; display: grid; place-items: center;
  background: var(--navy-600); color: #fff; box-shadow: var(--shadow-lg);
  transition: transform .15s ease, background .15s ease;
}
.cw-launcher:hover { background: var(--navy-700); transform: translateY(-2px); }
.cw-launcher .cw-ico-close { display: none; }
.cw--open .cw-launcher .cw-ico-open { display: none; }
.cw--open .cw-launcher .cw-ico-close { display: block; }
.cw-launcher-badge {
  position: absolute; top: -3px; right: -3px; min-width: 18px; height: 18px;
  padding: 0 5px; border-radius: 9px; background: var(--green); color: #fff;
  font-size: 10.5px; font-weight: 700; line-height: 1; display: grid; place-items: center;
  border: 2px solid var(--bg-elevated);
}
.cw--open .cw-launcher-badge { display: none; }

/* Panel overlay · tamaño contenido */
.cw-panel {
  position: absolute; right: 0; bottom: 62px;
  width: 360px; height: 520px; max-height: calc(100dvh - 96px);
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; overflow: hidden;
}
.cw-view { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.cw-head {
  display: flex; align-items: center; gap: 8px; padding: 12px 14px;
  border-bottom: 1px solid var(--border); background: var(--navy-600); color: #fff;
}
.cw-head-title { flex: 1; font-size: 15px; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cw-head-actions { display: flex; align-items: center; gap: 2px; }
.cw-ibtn {
  border: 0; background: transparent; color: #fff; cursor: pointer;
  width: 30px; height: 30px; border-radius: 7px; font-size: 16px; line-height: 1;
  display: grid; place-items: center; text-decoration: none;
}
.cw-ibtn:hover { background: rgb(255 255 255 / .15); }

/* Lista de conversaciones */
.cw-conv-list { flex: 1; overflow-y: auto; padding: 6px; }
.cw-conv {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px;
  border: 0; background: transparent; cursor: pointer; border-radius: var(--radius);
  text-align: left; color: var(--text); min-height: 52px;
}
.cw-conv:hover { background: var(--navy-50); }
.cw-avatar {
  flex: 0 0 36px; width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
  background: var(--navy-500); color: #fff;
}
.cw-avatar--ia { background: linear-gradient(135deg, var(--green-dark), #0EA5E9); font-size: 18px; }
.cw-avatar--canal { background: var(--green-dark); }
.cw-avatar--sm { flex-basis: 30px; width: 30px; height: 30px; font-size: 12px; }
.cw-conv-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.cw-conv-title { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cw-conv-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cw-conv-badge {
  flex: 0 0 auto; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--green); color: #fff; font-size: 10.5px; font-weight: 700;
  display: grid; place-items: center;
}

/* Mensajes */
.cw-messages { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 7px; background: var(--bg); }
.cw-msg { display: flex; }
.cw-msg--mine { justify-content: flex-end; }
.cw-msg-bubble {
  max-width: 82%; padding: 7px 11px; border-radius: 13px;
  background: var(--bg-elevated); border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 2px;
}
.cw-msg--mine .cw-msg-bubble { background: var(--navy-600); border-color: var(--navy-600); color: #fff; }
.cw-msg--ia .cw-msg-bubble { background: var(--green-bg); border-color: #BBF7D0; }
.cw-msg-author { font-size: 11px; font-weight: 700; color: var(--green-dark); }
.cw-msg-text { font-size: 13.5px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }
.cw-msg-time { font-size: 10px; color: var(--text-soft); align-self: flex-end; }
.cw-msg--mine .cw-msg-time { color: rgb(255 255 255 / .7); }

/* Indicador "escribiendo…" de la IA */
.cw-typing { flex-direction: row; gap: 4px; padding: 11px; }
.cw-typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--green-dark);
  opacity: .4; animation: cwBlink 1.2s infinite both;
}
.cw-typing span:nth-child(2) { animation-delay: .2s; }
.cw-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes cwBlink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

/* Composer */
.cw-composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--border); background: var(--bg-elevated); }
.cw-composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: 9px 11px; font: inherit; font-size: 16px; line-height: 1.4; max-height: 110px;
  background: var(--bg); color: var(--text);
}
.cw-composer textarea:focus { outline: none; border-color: var(--navy-500); }
.cw-send {
  flex: 0 0 auto; width: 40px; height: 40px; border-radius: var(--radius); border: 0;
  background: var(--navy-600); color: #fff; cursor: pointer; display: grid; place-items: center;
}
.cw-send:hover { background: var(--navy-700); }
.cw-mic {
  flex: 0 0 auto; width: 38px; height: 38px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--bg); color: var(--text-muted);
  cursor: pointer; display: grid; place-items: center;
}
.cw-mic:hover { color: var(--text); }
.cw-mic.is-rec { background: #FEE2E2; border-color: #FCA5A5; color: #DC2626; animation: cwPulse 1.1s infinite; }
.cw-mic.is-loading { opacity: .55; pointer-events: none; }
@keyframes cwPulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(220,38,38,.45); } 50% { box-shadow: 0 0 0 6px rgba(220,38,38,0); } }

/* Nueva conversación */
.cw-newtabs { display: flex; gap: 4px; padding: 10px 12px 0; }
.cw-tab {
  flex: 1; padding: 8px; border: 1px solid var(--border); background: var(--bg);
  border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; font-size: 12.5px; color: var(--text-muted);
}
.cw-tab.is-active { background: var(--navy-600); color: #fff; border-color: var(--navy-600); }
.cw-newpane { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 10px 12px; }
.cw-field {
  width: 100%; padding: 9px 11px; font-size: 16px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); margin-bottom: 8px; background: var(--bg); color: var(--text);
}
.cw-user-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.cw-user {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 7px 8px;
  border: 0; background: transparent; cursor: pointer; border-radius: var(--radius);
  text-align: left; color: var(--text); min-height: 44px;
}
.cw-user:hover { background: var(--navy-50); }
.cw-user--check { cursor: default; }
.cw-user--check input { width: 17px; height: 17px; accent-color: var(--green); }
.cw-user-meta { display: flex; flex-direction: column; min-width: 0; }
.cw-user-name { font-weight: 600; font-size: 13px; }
.cw-user-role { font-size: 11px; color: var(--text-muted); }
.cw-create { margin-top: 8px; padding: 10px; border: 0; border-radius: var(--radius); background: var(--navy-600); color: #fff; font-weight: 600; cursor: pointer; }
.cw-hint { color: var(--text-muted); font-size: 12.5px; padding: 12px; }

@media (max-width: 480px) {
  .cw { right: 14px; bottom: 14px; }
  .cw-panel { width: calc(100vw - 24px); height: calc(100dvh - 90px); right: -6px; }
}
@media (prefers-reduced-motion: reduce) {
  .cw-launcher { transition: none; }
  .cw-typing span { animation: none; }
}

.is-hidden { display: none !important; }

/* ---- Responsive · móvil: una sola columna con navegación back ---- */
@media (max-width: 720px) {
  .chat-app { height: calc(100dvh - 64px); margin: 0; border-radius: 0; border: 0; }
  .chat-aside { width: 100%; flex: 1 0 100%; }
  .chat-thread { display: none; }
  .chat-back { display: block; }
  .chat-app--thread-open .chat-aside { display: none; }
  .chat-app--thread-open .chat-thread { display: flex; flex: 1 0 100%; }
}

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

/* ---- Markdown ligero en las respuestas de Spine (IA) ---- */
/* Enlace interno clicable (navega dentro del panel). Es un <button> para no
   depender del PREFIX en el markup; se ve como un enlace de acción. */
.cw-md-link {
  display: inline; padding: 0; border: 0; background: transparent;
  font: inherit; color: var(--green-dark); font-weight: 700;
  text-decoration: underline; text-underline-offset: 2px;
  cursor: pointer; word-break: break-word;
}
.cw-md-link:hover { color: var(--navy-600); }
.cw-md-link:focus-visible { outline: 2px solid var(--navy-500); outline-offset: 2px; border-radius: 3px; }
/* Texto de un enlace externo/no navegable: resaltado pero inerte (sin url). */
.cw-md-strong { font-weight: 700; }
