/**
 * Chat_Widget — estilos de la burbuja flotante y el panel de conversación.
 *
 * Coherente con la SPA (src/public/styles.css). Diseño responsivo: el panel se
 * adapta desde 360 px de ancho sin desbordamiento horizontal, con controles
 * siempre dentro del área visible (posicionamiento fixed + box-sizing).
 *
 * _Requirements: 1.1, 1.2, 1.3, 1.5, 10.1, 10.3, 11.4_
 */

/* Contenedor raíz: no ocupa espacio; sus hijos usan position: fixed. */
#chat-widget-root,
#chat-widget-root * {
  box-sizing: border-box;
}

/* ------------------------------------------------------------------ */
/* Burbuja flotante (acceso al chat)                                   */
/* ------------------------------------------------------------------ */
.cw-bubble {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 1000;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: #1f6feb;
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background 0.15s ease;
}

.cw-bubble:hover {
  background: #1a5fd0;
  transform: translateY(-2px);
}

.cw-bubble:focus-visible {
  outline: 3px solid #9ecbff;
  outline-offset: 2px;
}

/* Oculta la burbuja mientras el panel está abierto. */
.cw-bubble[hidden] {
  display: none;
}

/* ------------------------------------------------------------------ */
/* Panel de conversación                                               */
/* ------------------------------------------------------------------ */
.cw-panel {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 1001;
  width: 380px;
  max-width: calc(100vw - 40px);
  height: 560px;
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
  background: #ffffff;
  border: 1px solid #d0d7de;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.28);
  overflow: hidden;
}

.cw-panel[hidden] {
  display: none;
}

.cw-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: #1f6feb;
  color: #fff;
  flex: 0 0 auto;
}

.cw-header h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.cw-cerrar {
  border: none;
  background: transparent;
  color: #fff;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}

.cw-cerrar:focus-visible {
  outline: 2px solid #9ecbff;
  outline-offset: 2px;
}

/* Área de mensajes: única zona con scroll vertical. */
.cw-mensajes {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f6f8fa;
}

.cw-msg {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.cw-msg-user {
  align-self: flex-end;
  background: #1f6feb;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.cw-msg-bot {
  align-self: flex-start;
  background: #ffffff;
  color: #1f2328;
  border: 1px solid #d0d7de;
  border-bottom-left-radius: 4px;
}

.cw-msg-error {
  align-self: stretch;
  background: #fff1f0;
  color: #a4272b;
  border: 1px solid #f3b6b6;
  max-width: 100%;
}

/* Indicador "escribiendo…" (Req 1.4) */
.cw-typing {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #57606a;
  font-size: 13px;
  font-style: italic;
}

.cw-typing .cw-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8c959f;
  display: inline-block;
  animation: cw-blink 1.2s infinite ease-in-out;
}

.cw-typing .cw-dot:nth-child(2) { animation-delay: 0.2s; }
.cw-typing .cw-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes cw-blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

/* Controles de feedback 👍/👎 (Req 10.1, 10.3) */
.cw-feedback {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.cw-fb-btn {
  border: 1px solid #d0d7de;
  background: #fff;
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 15px;
  line-height: 1.4;
  cursor: pointer;
}

.cw-fb-btn:disabled {
  cursor: default;
}

.cw-fb-btn.cw-fb-activo {
  background: #dff4e3;
  border-color: #2da44e;
}

.cw-fb-confirma {
  font-size: 12px;
  color: #1a7f37;
}

/* Caja de observación de mejora (aparece al dar 👎) */
.cw-fb-obs {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 6px;
  width: 100%;
}

.cw-fb-obs-input {
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  font: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid #d0d7de;
  border-radius: 8px;
}

.cw-fb-obs-enviar {
  align-self: flex-start;
  cursor: pointer;
  border: 1px solid #d0d7de;
  background: #f6f8fa;
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
}

.cw-fb-obs-enviar:disabled {
  opacity: 0.6;
  cursor: default;
}

.cw-fb-obs-estado {
  font-size: 12px;
  color: #57606a;
}

/* Confirmación de acciones de escritura — Fase 2 (Req 12.1) */
.cw-accion {
  align-self: flex-start;
  max-width: 85%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid #d0a72c;
  border-radius: 10px;
  background: #fff8e5;
}

.cw-accion-resumen {
  font-size: 13px;
  line-height: 1.4;
  color: #4d3b00;
}

.cw-accion-controles {
  display: flex;
  gap: 8px;
}

.cw-accion-btn {
  border: 1px solid #d0d7de;
  background: #fff;
  border-radius: 6px;
  padding: 4px 12px;
  font-size: 13px;
  line-height: 1.4;
  cursor: pointer;
}

.cw-accion-btn:disabled {
  cursor: default;
  opacity: 0.6;
}

.cw-accion-confirmar {
  background: #dff4e3;
  border-color: #2da44e;
  color: #1a7f37;
}

.cw-accion-cancelar {
  background: #ffebe9;
  border-color: #cf222e;
  color: #cf222e;
}

.cw-accion-estado {
  font-size: 12px;
  color: #57606a;
}

/* Bloque de error con reintento (Req 1.7, 11.4) */
.cw-retry {
  align-self: stretch;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cw-retry-btn {
  align-self: flex-start;
  border: 1px solid #d0d7de;
  background: #fff;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}

.cw-retry-btn:hover {
  background: #f3f4f6;
}

/* Pie: entrada de texto + envío */
.cw-footer {
  flex: 0 0 auto;
  border-top: 1px solid #d0d7de;
  padding: 10px;
  background: #fff;
}

.cw-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.cw-textarea {
  flex: 1 1 auto;
  min-width: 0;
  resize: none;
  border: 1px solid #d0d7de;
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.4;
  max-height: 120px;
}

.cw-textarea:focus-visible {
  outline: 2px solid #9ecbff;
  outline-offset: 0;
  border-color: #1f6feb;
}

.cw-enviar {
  flex: 0 0 auto;
  border: none;
  background: #1f6feb;
  color: #fff;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  cursor: pointer;
}

.cw-enviar:disabled {
  background: #94b8ef;
  cursor: default;
}

.cw-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
  min-height: 16px;
}

.cw-contador {
  font-size: 12px;
  color: #57606a;
}

.cw-contador.cw-excedido {
  color: #a4272b;
  font-weight: 600;
}

.cw-limite {
  font-size: 12px;
  color: #a4272b;
}

/* ------------------------------------------------------------------ */
/* Responsivo: desde 360 px, el panel ocupa el ancho disponible.       */
/* ------------------------------------------------------------------ */
@media (max-width: 480px) {
  .cw-panel {
    right: 8px;
    left: 8px;
    bottom: 8px;
    top: 8px;
    width: auto;
    max-width: calc(100vw - 16px);
    height: auto;
    max-height: calc(100vh - 16px);
  }

  .cw-bubble {
    right: 14px;
    bottom: 14px;
  }
}

/* ------------------------------------------------------------------ */
/* Respuestas enriquecidas del asistente (tarjetas, pasos, badges)     */
/* Clases cw-* instruidas desde el system prompt del backend.          */
/* ------------------------------------------------------------------ */

/* El cuerpo del bot puede contener HTML; anula el pre-wrap heredado
   para que las tarjetas no muestren espacios/saltos extra. */
.cw-msg-bot .cw-msg-texto {
  white-space: normal;
  display: block;
}

/* Título de una respuesta */
.cw-response-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13.5px;
  font-weight: 600;
  color: #1f2328;
  margin: 2px 0 8px;
}

/* Tarjeta de datos */
.cw-card {
  border: 1px solid #d0d7de;
  border-radius: 8px;
  background: #fbfcfd;
  padding: 8px 10px;
  margin: 6px 0;
}

.cw-card + .cw-card {
  margin-top: 8px;
}

.cw-card-header {
  font-size: 13px;
  font-weight: 600;
  color: #1f2328;
  padding-bottom: 6px;
  margin-bottom: 6px;
  border-bottom: 1px solid #eaeef2;
  display: flex;
  align-items: center;
  gap: 6px;
}

.cw-card-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 3px 0;
  font-size: 13px;
  line-height: 1.4;
}

.cw-card-label {
  flex: 0 1 auto;
  color: #57606a;
  min-width: 60px;
  max-width: 45%;
  overflow-wrap: anywhere;
}

.cw-card-value {
  flex: 1 1 auto;
  min-width: 0;
  color: #1f2328;
  overflow-wrap: anywhere;
  word-break: normal;
}

/* Defensa: si el modelo emite una tabla HTML/markdown a pesar del prompt,
   que ajuste al ancho del panel y no parta palabras a la mitad. */
.cw-msg-bot .cw-msg-texto table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 13px;
}

.cw-msg-bot .cw-msg-texto th,
.cw-msg-bot .cw-msg-texto td {
  padding: 4px 6px;
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
  word-break: normal;
  border-bottom: 1px solid #eaeef2;
}

/* Paso numerado (preguntas de "cómo hago…") */
.cw-step {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #1f6feb;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
}

/* Badges de estado */
.cw-badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}

.cw-badge-blue {
  background: #ddf4ff;
  color: #0969da;
}

.cw-badge-green {
  background: #dff4e3;
  color: #1a7f37;
}

.cw-badge-red {
  background: #ffebe9;
  color: #cf222e;
}

.cw-badge-amber {
  background: #fff8c5;
  color: #9a6700;
}

/* Valores monetarios */
.cw-money {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: #1a7f37;
}

/* Texto secundario / tips */
.cw-text-muted {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  color: #57606a;
}
