:root {
  /* Paleta OSCURA del chat (barra lateral, estilo sidebar de SIPLAN). */
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #9aa1ad;
  --accent: #5b60d8;      /* indigo SIPLAN (antes #7c5cff) */
  --accent-2: #1fc7dc;    /* turquesa SIPLAN (2º acento) */
  --accent-soft: #23264d; /* tinte indigo oscuro para badges sobre fondo oscuro */
  --user: #2563eb;
  --ok: #2ecc71;
  --warn: #f5a623;
}

* { box-sizing: border-box; }

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

.layout {
  display: grid;
  /* Chat (ancho ajustable con --chat-w) + divisor + área principal. */
  grid-template-columns: var(--chat-w, 460px) 5px 1fr;
  height: 100vh;
}

/* Divisor arrastrable: 5px de agarre, con una línea fina de 1px al centro que
   se resalta al pasar el mouse o durante el arrastre. */
.resizer { position: relative; cursor: col-resize; background: transparent; }
.resizer::before {
  content: ""; position: absolute; top: 0; bottom: 0; left: 50%;
  width: 1px; transform: translateX(-50%); background: var(--border);
}
.resizer:hover::before,
body.resizing .resizer::before { background: var(--accent); width: 2px; }
body.resizing { cursor: col-resize; user-select: none; }

/* ---- Área principal ---- */
.app-area {
  display: flex;
  flex-direction: column;
  overflow: hidden; /* el scroll vive en .panel, así el stepper queda fijo arriba */
  padding: 32px;
  /* Paleta CLARA para el contenido (estilo SIPLAN). Redefinir las variables
     aquí hace que TODAS las tarjetas/tablas/stepper de dentro las hereden, sin
     tocar el chat, que se queda con la paleta oscura de :root. */
  --bg: #eef0f4;
  --panel: #ffffff;
  --panel-2: #f4f6f9;
  --border: #e2e6ec;
  --text: #2f3242;
  --muted: #7b8290;
  --accent-soft: #e9eafc;
  --warn: #c77d0e;
  --ok: #1fa855;
  background: var(--bg);
  color: var(--text);
}

/* Nombre de la conversación actual, arriba a la izquierda del panel
   (encima del stepper Carga → Validación → Corrida → Resultados). El
   margin-top negativo compensa el padding-top de .app-area (32px) contra el
   de .chat-header (14px), para que quede a la misma altura que el botón
   "Nuevo Escenario" del panel de chat. */
.session-badge {
  align-self: flex-start;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 5px 12px;
  border-radius: 999px;
  margin: -18px 0 12px;
  flex: 0 0 auto;
}
.session-badge[hidden] { display: none; }

/* El hero es el estado vacío: 'margin: auto' lo centra mientras es lo único
   visible. Al empezar el flujo, se oculta y aparecen el stepper y el panel. */
.app-hero { max-width: 460px; margin: auto; }

/* ---- Stepper del flujo (carga -> validación -> corrida -> resultados) ---- */
.flow { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; margin-bottom: 16px; }
.flow[hidden] { display: none; }
.step { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.step-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: transparent; border: 1px solid var(--border);
}
.step.active { color: var(--text); }
.step.active .step-dot { background: var(--warn); border-color: var(--warn); animation: pulse 1s infinite; }
.step.done { color: var(--text); }
.step.done .step-dot { background: var(--ok); border-color: var(--ok); }
.step:not(:last-child)::after {
  content: ""; width: 18px; height: 1px; background: var(--border); margin-left: 8px;
}

/* ---- Panel de UI generativa (tarjetas que "pinta" el agente) ---- */
.panel {
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
}
/* Sin esto, la regla display:flex de arriba anula el atributo [hidden]. */
.panel[hidden] { display: none; }

/* Estado de carga en el panel mientras corre una herramienta. */
.loading-row { display: flex; align-items: center; gap: 10px; }
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent-2);
  animation: spin 0.8s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Tabla genérica (detalle_corrida): scroll horizontal si es ancha. */
.tabla-scroll { overflow-x: auto; }
.tabla { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.tabla th, .tabla td {
  border: 1px solid var(--border); padding: 5px 9px; text-align: left; white-space: nowrap;
}
.tabla thead th { background: var(--panel-2); }
.tabla tbody tr:nth-child(even) { background: rgba(0, 0, 0, 0.025); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.card.infactible { border-color: #e6a9a4; }
/* Diagnóstico: se distingue del resultado a propósito (banda punteada), para
   que sus cifras no se lean como un plan ejecutable. */
.card.diagnostico { border-style: dashed; border-color: var(--warn); }
.card-nota { color: var(--muted); font-size: 12px; margin: 12px 0 0; font-style: italic; }

.card-title { font-size: 16px; font-weight: 600; margin: 0 0 4px; }
.card-sub { color: var(--muted); font-size: 12.5px; margin: 0 0 16px; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 12px;
}
.stat {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.stat .label { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; }
.stat .value { font-size: 18px; font-weight: 600; margin-top: 3px; }
.stat.total .value { color: var(--accent); }

/* Stat expandible (ficha de resumen): clic despliega el detalle bajo la
   grilla. La flechita del label indica que hay más información. */
.stat.expandible { cursor: pointer; }
.stat.expandible:hover { border-color: var(--accent); }
.stat.expandible .label::after { content: " ▸"; color: var(--accent); }
.stat.expandible.abierto { border-color: var(--accent); }
.stat.expandible.abierto .label::after { content: " ▾"; }
.stat-detalle[hidden] { display: none; }

/* Botón de descarga (turquesa, como los botones de acción de SIPLAN). */
.btn-descarga {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent-2); color: #fff;
  padding: 8px 14px; border-radius: 8px;
  font-size: 13px; font-weight: 600; text-decoration: none;
}
.btn-descarga:hover { filter: brightness(0.96); }

.card-section { margin-top: 16px; }
.card-section h4 {
  font-size: 12.5px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .03em; margin: 0 0 8px;
}

.alerta { border-radius: 10px; padding: 8px 12px; margin-bottom: 8px; font-size: 13px; }
.alerta.real { background: #fff4e2; border: 1px solid #f0cf9a; }
.alerta.look { background: transparent; border: 1px dashed var(--border); color: var(--muted); }
.alerta-titulo { font-weight: 600; }
.alerta-detalle { color: var(--muted); font-size: 12.5px; margin-top: 3px; }

.warn-list { margin: 0; padding-left: 18px; color: var(--warn); font-size: 13px; }
.warn-list li { margin-bottom: 4px; }

.conflict-list { margin: 0; padding-left: 18px; color: #c0392b; font-size: 12.5px; }
.conflict-list li { margin-bottom: 4px; font-family: ui-monospace, "Cascadia Code", monospace; }

/* Conflicto ya traducido a negocio: son frases, no nombres internos */
.causa-list li { font-family: inherit; font-size: 13px; }
.link-boton {
  background: none; border: 0; padding: 4px 0; margin-top: 8px;
  color: var(--muted); font-size: 12.5px; cursor: pointer; text-decoration: underline;
}
.link-boton:hover { color: inherit; }

/* Grupos de hallazgos repetidos (mismo código) dentro de warn-list/conflict-list */
.warn-list li details, .conflict-list li details { margin: -2px 0; }
.warn-list li details summary, .conflict-list li details summary { cursor: pointer; }
.warn-list li details ul, .conflict-list li details ul { margin: 4px 0 4px 4px; }

.app-hero h1 { font-size: 38px; margin: 0 0 16px; }
.app-hero p { color: var(--muted); line-height: 1.6; margin: 0 0 12px; }
.app-hero .hint { color: var(--accent); font-weight: 500; }

/* ---- Panel de chat ---- */
.chat-panel {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  min-height: 0;
}

/* Barras de scroll del chat: por defecto el navegador las pinta claras (track
   gris claro + pulgar gris), que desentona con la paleta oscura del panel.
   `scrollbar-color` es el estándar (Firefox y Chrome/Edge modernos) y se
   hereda a los descendientes; los `::-webkit-scrollbar` cubren los WebKit que
   no lo soportan todavía. Solo se aplica dentro de .chat-panel: el panel
   derecho es claro y se queda con las barras del sistema. */
.chat-panel {
  scrollbar-width: thin;
  scrollbar-color: #3a4150 var(--panel);
}
.chat-panel ::-webkit-scrollbar { width: 10px; height: 10px; }
.chat-panel ::-webkit-scrollbar-track { background: var(--panel); }
.chat-panel ::-webkit-scrollbar-thumb {
  background: #3a4150;
  border-radius: 999px;
  /* El borde del color del fondo deja aire alrededor del pulgar: se ve más
     delgado sin perder área de agarre. */
  border: 2px solid var(--panel);
}
.chat-panel ::-webkit-scrollbar-thumb:hover { background: #4d566a; }
.chat-panel ::-webkit-scrollbar-corner { background: var(--panel); }

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* Si el chat se angosta demasiado (el divisor es arrastrable), los botones
     bajan a una segunda línea en vez de partir las palabras del título. */
  flex-wrap: wrap;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.chat-title { display: flex; align-items: center; gap: 8px; font-weight: 600; white-space: nowrap; }

.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted); transition: background .2s;
}
.dot.running { background: var(--warn); animation: pulse 1s infinite; }
.dot.idle { background: var(--ok); }
.dot.error { background: #ff5a5a; }

@keyframes pulse { 50% { opacity: .35; } }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
}
.ghost-btn:hover { color: var(--text); border-color: var(--muted); }

.chat-actions { display: flex; align-items: center; gap: 8px; }

/* ---- Menú de conversaciones anteriores ---- */
/* Cuelga bajo el header del chat (el chat-panel es el contenedor posicionado). */
.chat-panel { position: relative; }
.sessions-menu {
  position: absolute;
  top: 56px; left: 12px; right: 12px;
  max-height: 340px; overflow-y: auto;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 10;
}
.sessions-menu[hidden] { display: none; }
.session-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none; cursor: pointer;
  padding: 10px 14px; color: var(--text); font: inherit;
  border-bottom: 1px solid var(--border);
}
.session-item:last-child { border-bottom: none; }
.session-item:hover { background: var(--panel); }
.session-item.current { border-left: 3px solid var(--accent); padding-left: 11px; }
.session-title { font-size: 13.5px; font-weight: 600; }
.session-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.sessions-empty { padding: 12px 14px; color: var(--muted); font-size: 13px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 85%;
  padding: 10px 13px;
  border-radius: 14px;
  line-height: 1.5;
  font-size: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user { align-self: flex-end; background: var(--user); color: #fff; border-bottom-right-radius: 4px; }
.msg.agent { align-self: flex-start; background: var(--panel-2); border-bottom-left-radius: 4px; }

/* ---- Markdown renderizado en las burbujas del agente ---- */
.msg.agent { white-space: normal; } /* el HTML ya maneja los saltos de línea */
.msg.agent > *:first-child { margin-top: 0; }
.msg.agent > *:last-child { margin-bottom: 0; }
.msg.agent p { margin: 0 0 8px; }
.msg.agent ul, .msg.agent ol { margin: 0 0 8px; padding-left: 20px; }
.msg.agent li { margin: 2px 0; }
.msg.agent h1, .msg.agent h2, .msg.agent h3, .msg.agent h4 { margin: 12px 0 6px; line-height: 1.3; }
.msg.agent h1 { font-size: 18px; }
.msg.agent h2 { font-size: 16px; }
.msg.agent h3 { font-size: 14.5px; }
.msg.agent h4 { font-size: 13.5px; }
.msg.agent a { color: var(--accent); }
.msg.agent code {
  background: #0d0f13; border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 5px; font-size: 12.5px;
  font-family: ui-monospace, "Cascadia Code", monospace;
}
.msg.agent pre {
  background: #0d0f13; border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; margin: 0 0 8px; overflow-x: auto;
}
.msg.agent pre code { background: none; border: none; padding: 0; font-size: 12px; }
.msg.agent blockquote {
  margin: 0 0 8px; padding-left: 10px;
  border-left: 3px solid var(--border); color: var(--muted);
}
/* Tablas: cabecera destacada y scroll horizontal si la tabla es ancha. */
.msg.agent table {
  display: block; width: max-content; max-width: 100%;
  overflow-x: auto; border-collapse: collapse; margin: 0 0 8px; font-size: 12.5px;
}
.msg.agent th, .msg.agent td {
  border: 1px solid var(--border); padding: 6px 10px; text-align: left;
}
.msg.agent thead th { background: var(--panel); }

/* ---- Feedback: pulgares al pie de cada respuesta del agente ---- */
/* Aparecen al pasar el mouse por la burbuja para no ensuciar la lectura, y se
   quedan fijos cuando ya hay un voto (así se ve que ese mensaje ya se reportó). */
.fb-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px;
  margin-top: 8px; opacity: 0; transition: opacity .15s ease;
}
.msg.agent:hover .fb-bar,
.fb-bar:focus-within,
.fb-bar.marcada { opacity: 1; }

.fb-btn {
  background: none; border: 1px solid transparent; border-radius: 6px;
  padding: 2px 6px; font-size: 13px; line-height: 1.2; cursor: pointer;
  filter: grayscale(1); opacity: .7;
}
.fb-btn:hover { background: var(--panel); filter: none; opacity: 1; }
.fb-btn.activo {
  filter: none; opacity: 1;
  border-color: var(--accent); background: var(--accent-soft);
}
.fb-aviso { color: var(--muted); font-size: 11.5px; margin-left: 4px; }

.fb-form {
  flex-basis: 100%;
  display: flex; flex-direction: column; gap: 6px;
  margin-top: 8px; padding: 10px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
}
.fb-select, .fb-comentario {
  width: 100%; box-sizing: border-box;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 8px; font: inherit; font-size: 12.5px;
}
.fb-comentario { resize: vertical; }
.fb-form-acciones { display: flex; gap: 6px; justify-content: flex-end; }
.fb-enviar, .fb-cancelar {
  border-radius: 6px; padding: 5px 12px; font-size: 12.5px; cursor: pointer;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.fb-enviar { background: var(--accent); border-color: var(--accent); color: #fff; }

.msg.tool {
  align-self: flex-start;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 12.5px;
  padding: 6px 10px;
}
.msg.error { align-self: stretch; background: #3a1d1d; color: #ffb4b4; max-width: 100%; }

.composer {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
}

#input {
  flex: 1;
  resize: none;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  font-size: 14px;
  max-height: 140px;
}
#input:focus { outline: none; border-color: var(--accent); }

#send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 16px;
  font-weight: 600;
  cursor: pointer;
}
#send:disabled { opacity: .45; cursor: not-allowed; }

.icon-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  width: 40px;
  font-size: 17px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover:not(:disabled) { color: var(--text); border-color: var(--muted); }
.icon-btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---- Pantalla de inicio de sesión (login.html) ---- */
/* Hereda la paleta clara porque el <main> también tiene la clase .app-area. */
.login-screen {
  height: 100vh;
  align-items: center;
  justify-content: center;
}

.login-card {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
}
.login-card h1 { font-size: 26px; margin: 0 0 12px; }
.login-intro { color: var(--muted); line-height: 1.6; margin: 0 0 22px; font-size: 14px; }

.login-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.login-card input {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font: inherit;
  margin-bottom: 16px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }

.login-card button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:disabled { opacity: .6; cursor: not-allowed; }

.login-error {
  background: #fdecea;
  border: 1px solid #f5c2bd;
  color: #b3261e;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 16px;
}
.login-error[hidden] { display: none; }

.login-hint { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 18px 0 0; }

/* ---- Usuario logueado, junto a los botones del encabezado ---- */
/* Cabe ahí porque .chat-header hace wrap: con el título largo, la fila de
   acciones baja entera a una segunda línea y sobra ancho. */
.user-badge {
  font-size: 11px;
  color: var(--muted);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 2px;
}
.user-badge:empty { display: none; }
