/* ── Reset & Variables ────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue:       #b83232;
  --blue-light: #d44f4f;
  --blue-dark:  #8f1f1f;
  --gray-50:    #fdf8f8;
  --gray-100:   #f5f0f0;
  --gray-200:   #e8dede;
  --gray-400:   #a08080;
  --gray-600:   #5a3a3a;
  --gray-800:   #2c1a1a;
  --red:        #dc2626;
  --green:      #22c55e;
  --radius:     10px;
  --shadow:     0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md:  0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

body { font-family: 'Inter', system-ui, -apple-system, sans-serif; line-height: 1.5; color: var(--gray-800); }

/* ── Login ────────────────────────────────────────────────────────────────── */
.login-body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #fff5f5 0%, #ffe0e0 100%);
}

.login-card {
  background: white;
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(184,50,50,.15);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.login-logo { margin-bottom: 8px; }
.login-title { font-size: 1.5rem; font-weight: 700; color: var(--gray-800); }
.login-subtitle { font-size: .9rem; color: var(--gray-400); margin-bottom: 16px; }

.login-form { width: 100%; display: flex; flex-direction: column; gap: 16px; }

.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.password-wrapper input { width: 100%; padding-right: 42px; }
.btn-show-password {
  position: absolute;
  right: 10px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-400);
  padding: 4px;
  display: flex;
  align-items: center;
  transition: color .15s;
}
.btn-show-password:hover { color: var(--gray-600); }

.field-group { display: flex; flex-direction: column; gap: 6px; }
.field-group label { font-size: .85rem; font-weight: 600; color: var(--gray-600); }
.field-group input {
  padding: 10px 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: .95rem;
  transition: border-color .15s;
  outline: none;
}
.field-group input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(184,50,50,.12); }

.login-error {
  background: #fef2f2;
  color: var(--red);
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: .875rem;
}

.btn-primary {
  background: var(--blue);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background .15s;
  margin-top: 4px;
}
.btn-primary:hover { background: var(--blue-dark); }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }

/* ── Chat layout ──────────────────────────────────────────────────────────── */
.chat-body {
  display: flex;
  height: 100vh;
  height: 100dvh;  /* dvh descuenta la barra del navegador móvil */
  overflow: hidden;
  background: var(--gray-50);
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w, 260px);
  position: relative;
  flex-shrink: 0;
  background: white;
  border-right: 1px solid var(--gray-200);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 0 16px;
  border-bottom: 1px solid var(--gray-200);
}

.sidebar-brand { font-weight: 700; font-size: 1.05rem; color: var(--gray-800); }

.btn-new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  background: var(--gray-100);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--gray-600);
  transition: all .15s;
  width: 100%;
}
.btn-new-chat:hover { background: var(--gray-200); color: var(--gray-800); }

.sidebar-section-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--gray-400);
  padding: 4px 0;
}

/* Conversation list */
.conv-list { display: flex; flex-direction: column; gap: 2px; max-height: 280px; overflow-y: auto; }
.conv-empty { font-size: .8rem; color: var(--gray-400); padding: 6px 4px; }
.conv-item {
  display: flex;
  align-items: center;
  border-radius: var(--radius);
  transition: background .12s;
  overflow: hidden;
}
.conv-item:hover { background: var(--gray-100); }
.conv-item.active { background: #fff0f0; }
.conv-title {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: .8rem;
  color: var(--gray-600);
  text-align: left;
  min-width: 0;
}
.conv-item.active .conv-title { color: var(--blue); font-weight: 500; }
.conv-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-title svg { flex-shrink: 0; color: var(--gray-400); }
.conv-delete {
  background: none;
  border: none;
  padding: 6px 8px;
  cursor: pointer;
  color: var(--gray-400);
  opacity: 0;
  transition: opacity .12s, color .12s;
  flex-shrink: 0;
}
.conv-item:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: var(--red); }

.suggestions { display: flex; flex-direction: column; gap: 6px; }
.suggestion-chip {
  text-align: left;
  padding: 8px 12px;
  background: none;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: .8rem;
  color: var(--gray-600);
  cursor: pointer;
  transition: all .15s;
  line-height: 1.4;
}
.suggestion-chip:hover { background: #fff0f0; border-color: var(--blue-light); color: var(--blue); }

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0 0;
  border-top: 1px solid var(--gray-200);
}

.user-info { display: flex; align-items: center; gap: 8px; }
.user-avatar {
  width: 32px; height: 32px;
  background: var(--blue);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .8rem;
  font-weight: 700;
  text-transform: uppercase;
}
#usernameDisplay { font-size: .875rem; font-weight: 500; color: var(--gray-600); }

.btn-logout {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-400);
  padding: 6px;
  border-radius: 6px;
  transition: all .15s;
}
.btn-logout:hover { background: #fef2f2; color: var(--red); }

/* Main chat area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scroll-behavior: smooth;
}

/* Welcome */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  gap: 12px;
  padding: 40px;
  color: var(--gray-400);
}
.welcome-screen h2 { font-size: 1.5rem; color: var(--gray-600); }
.welcome-screen p { font-size: .95rem; line-height: 1.6; }

/* Messages */
.message { display: flex; flex-direction: column; max-width: 820px; }
.message.user { align-self: flex-end; align-items: flex-end; }
.message.assistant { align-self: flex-start; align-items: flex-start; }

.message-bubble {
  padding: 12px 16px;
  border-radius: 14px;
  font-size: .95rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.message.user .message-bubble {
  background: var(--blue);
  color: white;
  border-bottom-right-radius: 4px;
}
.message.assistant .message-bubble {
  background: white;
  border: 1px solid var(--gray-200);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
  min-width: 200px;
}

/* Timestamps */
.msg-timestamp {
  font-size: .72rem;
  color: var(--gray-400);
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.ts-user  { justify-content: flex-end; }
.ts-assistant { justify-content: flex-start; padding-left: 2px; }
.ts-sep   { opacity: .5; }
.ts-ttft  { color: var(--gray-400); font-variant-numeric: tabular-nums; }

/* Thinking indicator */
.thinking-dots {
  display: flex;
  gap: 5px;
  padding: 14px 16px;
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
}
.thinking-dots span:not(.thinking-label) {
  width: 8px; height: 8px;
  background: var(--gray-400);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.thinking-label {
  font-size: .8rem;
  color: var(--gray-400);
  margin-left: 6px;
  font-variant-numeric: tabular-nums;
  animation: none;
  width: auto;
  height: auto;
  background: none;
  border-radius: 0;
}
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-6px); } }

/* SQL block */
.sql-block {
  margin-top: 8px;
  width: 100%;
}
.sql-toggle {
  background: none;
  border: none;
  font-size: .78rem;
  color: var(--gray-400);
  cursor: pointer;
  padding: 2px 0;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: color .15s;
}
.sql-toggle:hover { color: var(--gray-600); }
.sql-code {
  margin-top: 6px;
  background: var(--gray-800);
  color: #e2e8f0;
  border-radius: 8px;
  padding: 12px 16px;
  font-family: 'Menlo', 'Consolas', monospace;
  font-size: .8rem;
  overflow-x: auto;
  white-space: pre;
}

/* Data table */
.data-table-wrapper {
  margin-top: 10px;
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--gray-200);
  box-shadow: var(--shadow);
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
  background: white;
}
.data-table th {
  background: var(--blue);
  color: white;
  padding: 9px 14px;
  text-align: left;
  font-weight: 600;
  white-space: nowrap;
  text-transform: capitalize;
}
.data-table td {
  padding: 8px 14px;
  border-bottom: 1px solid var(--gray-100);
  color: var(--gray-800);
  white-space: normal;
  word-break: break-word;
  max-width: 320px;
  vertical-align: top;
}
/* Columnas de texto largo: limitar y permitir wrap */
.data-table td.col-wide {
  max-width: 420px;
}
/* Columnas cortas (fechas, estado, prioridad): no romper */
.data-table td.col-narrow {
  white-space: nowrap;
  max-width: none;
}
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: var(--gray-50); }
/* Alinear a la derecha columnas numéricas (detectado por contenido) */
.data-table td:not(:first-child) { text-align: right; }
.data-table th:not(:first-child) { text-align: right; }
.data-table-footer {
  padding: 6px 14px;
  font-size: .75rem;
  color: var(--gray-400);
  background: var(--gray-50);
  border-top: 1px solid var(--gray-200);
  text-align: right;
}

/* Chart */
.chart-container {
  margin-top: 12px;
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 700px;
}

.chart-zoom-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 10;
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  padding: 5px 7px;
  cursor: pointer;
  color: var(--gray-400);
  display: flex;
  align-items: center;
  box-shadow: var(--shadow);
  transition: all .15s;
}
.chart-zoom-btn:hover { color: var(--blue); border-color: var(--blue); }

/* Modal */
.chart-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.chart-modal.hidden { display: none !important; }
.chart-modal-box {
  background: white;
  border-radius: 12px;
  width: 92vw;
  height: 85vh;
  height: 85dvh;
  position: relative;
  padding: 20px;
  box-shadow: 0 25px 60px rgba(0,0,0,.3);
  display: flex;
  flex-direction: column;
}
.chart-modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--gray-400);
  line-height: 1;
  z-index: 10;
}
.chart-modal-close:hover { color: var(--gray-800); }
#chartModalPlot { flex: 1; }

/* Error */
.message-error {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: .875rem;
}

/* Input area */
.chat-input-area {
  padding: 16px 24px 20px;
  background: var(--gray-50);
  border-top: 1px solid var(--gray-200);
}

.chat-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: white;
  border: 1.5px solid var(--gray-200);
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  transition: border-color .15s;
}
.chat-input-wrapper:focus-within { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(184,50,50,.12); }

textarea#messageInput {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-size: .95rem;
  font-family: inherit;
  line-height: 1.5;
  max-height: 160px;
  background: transparent;
  color: var(--gray-800);
}
textarea#messageInput::placeholder { color: var(--gray-400); }

.input-actions { display: flex; align-items: center; gap: 4px; }

.btn-icon {
  width: 36px; height: 36px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .15s;
  flex-shrink: 0;
}

.btn-mic { background: var(--gray-100); color: var(--gray-600); }
.btn-mic:hover { background: var(--gray-200); }
.btn-mic.recording { background: #fef2f2; color: var(--red); animation: pulse 1s infinite; }

.btn-send { background: var(--blue); color: white; }
.btn-send:hover { background: var(--blue-dark); }
.btn-send:disabled { background: var(--gray-200); color: var(--gray-400); cursor: not-allowed; }

/* Distintivo de entorno (DEV / PROD) */
/* display explícito gana al atributo hidden, hay que reponerlo */
.env-badge[hidden] { display: none; }

.env-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  border: 1px solid transparent;
}
/* Contraste suficiente para leerse de un vistazo, no para alarmar */
.env-badge.env-dev  { background: #dbeafe; color: #1e40af; border-color: #93c5fd; }
.env-badge.env-prod { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }

/* Franja superior SIEMPRE, en los dos entornos. Que la señal exista siempre y
   cambie de color es más fiable que fiarse de la ausencia de señal: si un día
   falla el JS, la ausencia de franja roja no debe leerse como "estoy en dev". */
body.is-dev::before,
body.is-prod::before {
  content: "";
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 3px;
  z-index: 9999;
}
body.is-dev::before  { background: #3b82f6; }
body.is-prod::before { background: var(--red); }

/* Botón de enviar convertido en botón de parar mientras el agente genera */
.btn-send.btn-stop { background: var(--red); color: white; }
.btn-send.btn-stop:hover { background: #b91c1c; }

.stopped-note {
  margin-top: 8px;
  font-size: 12px;
  color: var(--gray-500);
  font-style: italic;
}

.mic-status {
  text-align: center;
  font-size: .8rem;
  color: var(--red);
  margin-top: 6px;
}

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .5; } }

/* Utilities */
.hidden { display: none !important; }
.spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); }

/* ── Responsive mobile ────────────────────────────────────────────────────── */
/* Botón del cajón lateral: solo existe en móvil */
.btn-menu {
  display: none;
  place-items: center;
  width: 40px; height: 40px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  background: white;
  color: var(--gray-600);
  cursor: pointer;
  flex-shrink: 0;
}
.btn-menu:hover { background: var(--gray-100); }
.topbar { display: none; }


/* ── Barra lateral redimensionable y plegable (solo escritorio) ───────────── */

.sidebar-resizer {
  position: absolute;
  top: 0; right: -3px; bottom: 0;
  width: 6px;
  cursor: col-resize;
  z-index: 20;
  background: transparent;
  transition: background .15s;
}
.sidebar-resizer:hover,
.sidebar-resizer.arrastrando { background: var(--blue); }

/* Mientras se arrastra: sin selección de texto ni transiciones que estorben */
body.redimensionando { user-select: none; cursor: col-resize; }
body.redimensionando .sidebar { transition: none; }

/* Botón de plegar, anclado al borde de la barra */
.btn-plegar {
  position: absolute;
  top: 50%;
  right: -11px;
  transform: translateY(-50%);
  width: 22px; height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--gray-200);
  border-radius: 0 6px 6px 0;
  background: white;
  color: var(--gray-500);
  cursor: pointer;
  z-index: 21;
  padding: 0;
  transition: background .15s, color .15s;
}
.btn-plegar:hover { background: var(--gray-100); color: var(--gray-800); }
.btn-plegar svg { transition: transform .2s; }

/* Plegada: se recoge del todo y el botón queda asomando */
body.lateral-plegado .sidebar {
  width: 0;
  padding-left: 0;
  padding-right: 0;
  overflow: hidden;
  border-right: none;
}
body.lateral-plegado .sidebar > *:not(.btn-plegar):not(.sidebar-resizer) { display: none; }
body.lateral-plegado .sidebar-resizer { display: none; }
body.lateral-plegado .btn-plegar { right: -23px; border-radius: 0 6px 6px 0; }
body.lateral-plegado .btn-plegar svg { transform: rotate(180deg); }

@media (max-width: 768px) {

  /* Login */
  .login-card { padding: 28px 20px; margin: 16px; }

  /* ── Barra superior + cajón lateral ──────────────────────────────────────
     La lista de conversaciones no cabe en una barra de 52px. En vez de
     ocultarla y perder el historial, la barra lateral pasa a ser un cajón
     deslizante, que es el patrón esperado en móvil. */
  .chat-body { flex-direction: column; }

  .btn-menu { display: grid; }

  /* El cajón tiene su propia anchura y su propio gesto: aquí no aplica */
  .sidebar-resizer, .btn-plegar { display: none; }
  body.lateral-plegado .sidebar { width: min(82vw, 300px); padding: 16px; }
  body.lateral-plegado .sidebar > * { display: revert; }

  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(82vw, 300px);
    max-height: none;
    flex-direction: column;
    align-items: stretch;
    padding: 16px;
    border-right: 1px solid var(--gray-200);
    border-bottom: none;
    overflow-y: auto;
    transform: translateX(-100%);
    transition: transform .22s ease;
    z-index: 60;
  }
  .sidebar.abierta { transform: translateX(0); }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 55;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  .sidebar-backdrop.visible { opacity: 1; pointer-events: auto; }

  /* Dentro del cajón sí caben: se recuperan */
  .sidebar-section-title, .suggestions { display: block; }
  .conv-list { max-height: none; }
  #usernameDisplay { display: block; }
  .sidebar-header img { max-height: 64px; }

  /* Barra superior propia, fuera del cajón */
  .topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: white;
    border-bottom: 1px solid var(--gray-200);
    flex-shrink: 0;
    min-height: 52px;
  }
  .topbar-title {
    font-size: .95rem;
    font-weight: 600;
    color: var(--gray-800);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Mensajes */
  .chat-messages { padding: 14px 10px; gap: 12px; }
  .message { max-width: 100%; }
  .message-bubble { font-size: .95rem; padding: 10px 13px; }

  /* Input: 16px evita que iOS haga zoom al enfocar */
  .chat-input-area { padding: 8px 10px 12px; }
  textarea#messageInput { font-size: 16px; }

  /* Objetivos táctiles: 44px es el mínimo recomendado */
  .btn-icon { width: 44px; height: 44px; }

  /* Gráficos */
  .chart-container { max-width: 100%; }
}
