:root {
  color-scheme: light;
  --bg: #f6f7fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d9deea;
  --brand: #176b5b;
  --brand-strong: #0d4f43;
  --accent: #cc6b2c;
  /* Relleno de las tarjetas. Las tablas lo usan para llegar al borde sin doble margen. */
  --relleno: 1.25rem;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

a {
  color: var(--brand);
  text-decoration: none;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--brand);
  color: white;
  cursor: pointer;
  font-weight: 700;
  padding: 0.7rem 1rem;
}

button:hover {
  background: var(--brand-strong);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  padding: 0.7rem;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 0.7rem;
  text-align: left;
  /* Los correos institucionales son larguisimos: sin esto la tabla se sale del panel. */
  overflow-wrap: anywhere;
}

th {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  white-space: nowrap;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover td {
  background: #f8fafc;
}

td.derecha,
th.derecha {
  text-align: right;
}

.topbar {
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 64px;
  padding: 0 5vw;
}

.brand {
  color: var(--text);
  font-weight: 800;
}

.topbar nav {
  align-items: center;
  display: flex;
  gap: 1rem;
}

.topbar form {
  margin: 0;
}

.layout {
  margin: 0 auto;
  max-width: 1320px;
  padding: 2rem clamp(1rem, 4vw, 3rem);
}

.login-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 8vh auto;
  max-width: 430px;
  padding: 2rem;
}

.form {
  display: grid;
  gap: 1rem;
}

.form label {
  color: var(--muted);
  display: grid;
  font-size: 0.85rem;
  gap: 0.35rem;
}

.form label.inline {
  align-items: center;
  display: flex;
  gap: 0.5rem;
}

.hint {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 0;
}

.muted {
  color: var(--muted);
}

.alert {
  background: #fff2f0;
  border: 1px solid #e3a49a;
  border-radius: 6px;
  color: #8a2f20;
  padding: 0.75rem;
}

.success {
  background: #edf7f4;
  border: 1px solid #9bcbbf;
  border-radius: 6px;
  color: var(--brand-strong);
  padding: 0.75rem;
}

.section-head {
  align-items: end;
  display: flex;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}

.section-head h1,
.section-head h2 {
  margin: 0 0 0.4rem;
}

.compact {
  margin-top: 2rem;
}

.agent-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.agent-card,
.admin-grid article {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.25rem;
}

.agent-icon {
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 800;
  text-transform: uppercase;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0;
}

.tags span {
  background: #edf7f4;
  border-radius: 999px;
  color: var(--brand-strong);
  font-size: 0.8rem;
  padding: 0.3rem 0.55rem;
}

.history-list {
  display: grid;
  gap: 0.6rem;
}

.history-list a {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  display: flex;
  justify-content: space-between;
  padding: 1rem;
}

.history-list span {
  color: var(--muted);
}

.chat-shell {
  display: grid;
  gap: 1rem;
  grid-template-columns: minmax(200px, 260px) minmax(0, 1fr);
}

.chat-side,
.chat-main {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.chat-side {
  padding: 1.25rem;
}

.chat-main {
  display: grid;
  grid-template-rows: minmax(460px, 68vh) auto;
  overflow: hidden;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  overflow-y: auto;
  padding: 1.5rem;
}

.message {
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-width: min(78%, 70ch);
  padding: 0.85rem 1.1rem;
}

.message .autor {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  opacity: 0.65;
  text-transform: uppercase;
}

/* El texto del modelo trae parrafos, listas y opciones: cada uno con su propio espacio. */
.message .texto p {
  margin: 0 0 0.6rem;
}

.message .texto p:last-child,
.message .texto ul:last-child {
  margin-bottom: 0;
}

.message .texto ul {
  list-style: none;
  margin: 0 0 0.6rem;
  padding-left: 0.2rem;
}

.message .texto li {
  margin-bottom: 0.3rem;
  padding-left: 1.1rem;
  position: relative;
}

.message .texto li.vineta::before {
  content: "·";
  left: 0.25rem;
  position: absolute;
}

/* Las opciones a) b) c) y los pasos numerados ya traen su marca dentro del texto. */
.message .texto li.marcado {
  padding-left: 0.2rem;
}

/* Los botones que borran no deben parecerse a los que guardan. */
button.peligro {
  background: #fff;
  border: 1px solid #c0392b;
  color: #c0392b;
}

button.peligro:hover {
  background: #c0392b;
  color: #fff;
}

/* "Pregunta 3 de 10": rotulo pequeno, para ubicarse sin leer el parrafo completo. */
.message .texto p.rotulo {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  margin: 0.9rem 0 0.25rem;
  opacity: 0.6;
  text-transform: uppercase;
}

.message .texto p.rotulo:first-child {
  margin-top: 0;
}

/* El enunciado es lo que el estudiante debe responder: va mas marcado que el resto. */
.message .texto p.enunciado {
  font-weight: 600;
  margin-bottom: 0.5rem;
}

/* Las opciones de una pregunta se leen como bloque aparte del enunciado. */
.message .texto ul.opciones {
  border-left: 3px solid var(--line);
  margin: 0.15rem 0 0.75rem;
  padding-left: 0.75rem;
}

.message .texto ul.opciones li {
  margin-bottom: 0.4rem;
}

.message.user .texto ul.opciones {
  border-left-color: rgba(255, 255, 255, 0.45);
}

.message.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
}

.message.agent {
  align-self: flex-start;
  background: #f4f6f9;
  border: 1px solid var(--line);
}

.message.sistema {
  background: #fdf3f2;
  border-color: #e8c4c0;
}

/* Tres puntos mientras el agente responde: sin esto la espera parece que fallo. */
.message.escribiendo .texto {
  display: flex;
  gap: 0.3rem;
}

.message.escribiendo .texto span {
  animation: latido 1.2s infinite ease-in-out;
  background: var(--muted);
  border-radius: 50%;
  height: 0.5rem;
  width: 0.5rem;
}

.message.escribiendo .texto span:nth-child(2) { animation-delay: 0.2s; }
.message.escribiendo .texto span:nth-child(3) { animation-delay: 0.4s; }

@keyframes latido {
  0%, 60%, 100% { opacity: 0.25; }
  30% { opacity: 1; }
}

.chat-side .volver {
  display: inline-block;
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
}

.chat-side .etiqueta {
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  margin: 0 0 0.5rem;
  text-transform: uppercase;
}

.aviso-lectura {
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  margin: 0;
  padding: 0.9rem 1.5rem;
}

.composer {
  border-top: 1px solid var(--line);
  display: grid;
  gap: 0.8rem;
  grid-template-columns: 1fr auto;
  padding: 1rem;
}

.metrics,
.admin-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  margin-bottom: 1rem;
}

.metrics div {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
}

.metrics strong {
  display: block;
  font-size: 1.8rem;
}

.metrics span {
  color: var(--muted);
}

.inline {
  align-items: center;
  display: flex;
  gap: 0.5rem;
}

.inline input {
  width: auto;
}

@media (max-width: 760px) {
  .topbar,
  .topbar nav,
  .section-head,
  .history-list a {
    align-items: flex-start;
    flex-direction: column;
  }

  .chat-shell {
    grid-template-columns: 1fr;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .message {
    max-width: 100%;
  }
}

/* Pantallas de administracion: una sola columna a lo ancho, para tablas largas y
   formularios con textos extensos como el system prompt o los DBA. */
.panel {
  --relleno: clamp(1rem, 2.5vw, 1.5rem);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 1.25rem;
  padding: var(--relleno);
}

.panel h2 {
  font-size: 1.05rem;
  margin-top: 0;
}

/* Envoltura de las tablas: en pantallas angostas se desplaza sola en vez de desbordar el panel.
   Los margenes negativos hacen que la tabla llegue al borde del panel, sin dobles espacios. */
.tabla {
  margin: 0 calc(var(--relleno) * -1);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tabla table {
  min-width: 680px;
}

.tabla th:first-child,
.tabla td:first-child {
  padding-left: var(--relleno);
}

.tabla th:last-child,
.tabla td:last-child {
  padding-right: var(--relleno);
}

.tabla thead th {
  background: #f8fafc;
  border-bottom: 1px solid var(--line);
}

/* Nombre y correo en una sola columna: el correo institucional es tan largo que en su propia
   columna empujaba la tabla fuera del panel. */
.celda-usuario {
  display: grid;
  gap: 0.15rem;
  min-width: 15rem;
}

.celda-usuario strong {
  font-weight: 600;
}

.celda-usuario .correo {
  color: var(--muted);
  font-size: 0.82rem;
}

.etiqueta {
  border: 1px solid var(--line);
  border-radius: 999px;
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.15rem 0.6rem;
  white-space: nowrap;
}

.etiqueta.rol-admin {
  background: #f3eefc;
  border-color: #d9c9f2;
  color: #5b3a9b;
}

.etiqueta.rol-teacher {
  background: #eef5fd;
  border-color: #c6dcf5;
  color: #1f5c9e;
}

.etiqueta.rol-student {
  background: #f4f6f9;
  color: var(--muted);
}

.etiqueta.propietario {
  background: #fdf4ec;
  border-color: #f0d3b6;
  color: #9a5a1c;
}

.etiqueta.si {
  background: #edf7f2;
  border-color: #c2e2d3;
  color: #1a6b4e;
}

.etiqueta.no {
  background: #fdf1f0;
  border-color: #efc9c4;
  color: #a33b2e;
}

/* Enlace con aspecto de boton secundario, para las acciones de cada fila. */
.accion {
  border: 1px solid var(--line);
  border-radius: 6px;
  display: inline-block;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.35rem 0.8rem;
  white-space: nowrap;
}

.accion:hover {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* Cuanto material lleva cargado un tutor: importa porque viaja en cada mensaje. */
.medidor .barra {
  background: var(--line);
  border-radius: 999px;
  height: 6px;
  margin-bottom: 0.4rem;
  overflow: hidden;
}

.medidor .barra span {
  background: var(--brand);
  display: block;
  height: 100%;
}

.medidor .barra span[style*="100%"] {
  background: #c0392b;
}

.filtros input[type="file"] {
  background: var(--panel);
  padding: 0.5rem;
}

.cursos-docente {
  display: flex;
  gap: 0.4rem;
  min-width: 12rem;
}

.cursos-docente input {
  /* Sin min-width el campo no cede y empuja al boton, que parte la palabra en dos lineas. */
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.4rem 0.5rem;
}

.cursos-docente button {
  flex: 0 0 auto;
  font-size: 0.8rem;
  padding: 0.4rem 0.7rem;
  white-space: nowrap;
}

.acciones {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  margin-bottom: 1.5rem;
}

.acciones a {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: block;
  padding: 1.25rem;
  text-decoration: none;
}

.acciones a strong {
  display: block;
  font-size: 1.05rem;
  margin-bottom: 0.35rem;
}

.acciones a span {
  color: var(--muted);
  font-size: 0.9rem;
}

.filtros {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.filtros label {
  color: var(--muted);
  display: grid;
  font-size: 0.85rem;
  gap: 0.3rem;
}

/* El campo de busqueda se queda con el espacio sobrante; los demas conservan su tamano. */
.filtros label.crece {
  flex: 1 1 18rem;
}

.paginacion {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  font-size: 0.9rem;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

.paginacion span {
  margin-right: auto;
}

/* Los campos cortos se reparten en columnas; los largos ocupan todo el ancho. */
.form-ancho {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
}

.form-ancho .ancho-total {
  grid-column: 1 / -1;
}

.form-ancho textarea {
  width: 100%;
}

/* Barra para saltar entre secciones sin volver atras. */
.secciones {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.secciones a {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: inherit;
  font-size: 0.9rem;
  padding: 0.5rem 1rem;
  text-decoration: none;
}

.secciones a:hover {
  border-color: var(--accent);
}

.secciones a.activa {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* La navegacion vive en la barra superior; en pantallas angostas baja a una segunda linea. */
.topbar {
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
  padding-bottom: 0.6rem;
  padding-top: 0.6rem;
}

.topbar .secciones {
  flex: 1 1 auto;
  margin-bottom: 0;
}

.sesion {
  align-items: center;
  display: flex;
  gap: 1rem;
}

.sesion span {
  color: var(--muted);
  font-size: 0.9rem;
}
