:root {
  /* Tema oscuro (por defecto) */
  --bg: #0f1419;
  --surface: #171e26;
  --surface-2: #1f2933;
  --border: #2b3540;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #0a7d8c;
  --accent-soft: #12a3b5;
  --ok: #2f9e63;
  --warn: #c9852a;
  --danger: #c2453f;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

/* Tema claro */
:root[data-theme="light"] {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef2f6;
  --border: #dce3ea;
  --text: #1b2430;
  --muted: #61708a;
  --accent: #0a7d8c;
  --accent-soft: #0d93a5;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* Barra de scroll fina y adaptada al tema */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border: 2px solid transparent;
  background-clip: padding-box;
  border-radius: 8px;
}
::-webkit-scrollbar-thumb:hover { background: var(--muted); background-clip: padding-box; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
}

a { color: var(--accent-soft); }

/* ---------------------------------------------------------------- login */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
}

.brand {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 4px;
}

.brand span { color: var(--accent-soft); }

.sub { color: var(--muted); font-size: 13px; margin-bottom: 24px; }

/* --------------------------------------------------------------- forms */
label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 14px 0 6px;
}

input, select, textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

textarea { resize: vertical; min-height: 70px; }

button {
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  color: #fff;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 10px 16px;
}

button:hover { background: var(--accent-soft); }
button:disabled { opacity: .5; cursor: not-allowed; }

button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

button.ghost:hover { background: var(--surface-2); }
button.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
button.danger:hover { background: var(--danger); color: #fff; }

.full { width: 100%; margin-top: 20px; }

.error {
  background: rgba(194, 69, 63, .12);
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: #f0a9a5;
  font-size: 13px;
  margin-top: 16px;
  padding: 10px 12px;
}

.error:empty { display: none; }

/* -------------------------------------------------------------- layout */
.topbar {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 16px;
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar .brand { font-size: 16px; margin: 0; }
.spacer { flex: 1; }

.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 10px;
}

/* ============================ CHAT (full-width) ======================== */
.chat-app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* ------ Sidebar / historial ------ */
.chat-side {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.side-head {
  align-items: center;
  display: flex;
  gap: 10px;
  padding: 16px 16px 12px;
}

.side-head .brand { font-size: 16px; }
.side-head .logo {
  align-items: center;
  background: linear-gradient(135deg, var(--accent-soft), var(--accent));
  border-radius: 9px;
  color: #fff;
  display: flex;
  font-size: 16px;
  height: 32px;
  justify-content: center;
  width: 32px;
}

.btn-nuevo {
  align-items: center;
  background: var(--accent);
  border-radius: 9px;
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 0 12px 8px;
  padding: 11px;
}
.btn-nuevo:hover { background: var(--accent-soft); }

.side-titulo {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.04em;
  padding: 12px 18px 6px;
  text-transform: uppercase;
}

.conversaciones {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}

.conv {
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  position: relative;
}
.conv:hover { background: var(--surface-2); }
.conv.activa { background: var(--surface-2); box-shadow: inset 2px 0 0 var(--accent-soft); }
.conv .txt { flex: 1; min-width: 0; }
.conv .t {
  font-size: 13.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv .f { color: var(--muted); font-size: 11px; }
.conv .del {
  background: transparent;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  opacity: 0;
  padding: 3px 7px;
}
.conv:hover .del { opacity: 1; }
.conv .del:hover { background: rgba(194, 69, 63, .18); color: var(--danger); }
.conv-vacio { color: var(--muted); font-size: 13px; padding: 14px 12px; }

.side-foot {
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  padding: 12px;
}
.side-foot button, .side-foot a { flex: 1; }
.side-foot .ghost { padding: 9px; text-align: center; }

/* ------ Zona de chat ------ */
.chat-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.chat-top {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 12px;
  padding: 14px 24px;
}
.chat-top .bot-nombre { font-size: 15px; font-weight: 600; }
.icono-btn {
  align-items: center;
  display: inline-flex;
  height: 34px;
  justify-content: center;
  padding: 0;
  width: 34px;
}
.chat-top .estado-online {
  align-items: center;
  color: var(--muted);
  display: flex;
  font-size: 12px;
  gap: 6px;
}
.chat-top .punto {
  background: var(--ok);
  border-radius: 50%;
  box-shadow: 0 0 0 3px rgba(47, 158, 99, .2);
  height: 8px;
  width: 8px;
}

.messages {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 20px;
  margin: 0 auto;
  max-width: 920px;
  overflow-y: auto;
  padding: 28px 24px;
  width: 100%;
}

.msg { display: flex; gap: 12px; max-width: 88%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.bot { align-self: flex-start; }

.avatar {
  align-items: center;
  border-radius: 9px;
  display: flex;
  flex-shrink: 0;
  font-size: 15px;
  height: 32px;
  justify-content: center;
  margin-top: 2px;
  width: 32px;
}
.msg.bot .avatar { background: linear-gradient(135deg, var(--accent-soft), var(--accent)); color: #fff; }
.msg.user .avatar { background: var(--surface-2); border: 1px solid var(--border); }

.bubble {
  border-radius: 14px;
  padding: 13px 17px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.bot .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
}
.msg.user .bubble {
  background: linear-gradient(135deg, var(--accent), #0b6b78);
  border-top-right-radius: 4px;
  color: #fff;
}

/* Markdown ligero dentro de las respuestas */
.tabla-scroll {
  margin: 10px 0;
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.tabla-scroll table { border-collapse: collapse; width: max-content; min-width: 100%; }
.bubble th, .bubble td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 7px 12px;
  text-align: left;
  white-space: nowrap;  /* no partir celdas: mejor que la tabla se desplace */
}
.bubble tr:last-child td { border-bottom: 0; }
.bubble th:last-child, .bubble td:last-child { border-right: 0; }
.bubble th { background: var(--surface-2); font-size: 12px; position: sticky; top: 0; }
.msg.bot .bubble strong { color: var(--text); font-weight: 700; }
.msg.user .bubble strong { color: #fff; font-weight: 700; }
.bubble ul, .bubble ol { margin: 6px 0 6px 20px; padding: 0; }
.bubble li { margin: 3px 0; }
.bubble h3, .bubble h4, .bubble h5 { margin: 12px 0 6px; line-height: 1.3; font-weight: 700; }
.bubble h3 { font-size: 1.08em; }
.bubble h4 { font-size: 1em; }
.bubble h5 { font-size: .95em; color: var(--muted); }
.bubble em { font-style: italic; }
.msg.bot .bubble a { color: var(--accent, #4f7cff); text-decoration: underline; }
.msg.user .bubble a { color: #fff; text-decoration: underline; }

/* Barra de scroll fina para la tabla */
.tabla-scroll::-webkit-scrollbar { height: 8px; }
.tabla-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.tabla-scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }

.meta {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  font-size: 11px;
  gap: 6px;
  margin-top: 11px;
  padding-top: 9px;
  border-top: 1px solid var(--border);
}

.badge {
  align-items: center;
  border-radius: 999px;
  display: inline-flex;
  font-size: 11px;
  gap: 3px;
  padding: 3px 9px;
}
.badge.ok { background: rgba(47, 158, 99, .16); color: #6fd39a; }
.badge.warn { background: rgba(201, 133, 42, .16); color: #e0a95c; }
.badge.tool { background: var(--surface-2); color: var(--muted); font-family: ui-monospace, monospace; }

/* Composer */
.composer-wrap { border-top: 1px solid var(--border); padding: 16px 24px 22px; }
.composer {
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  display: flex;
  gap: 8px;
  margin: 0 auto;
  max-width: 920px;
  padding: 8px 8px 8px 16px;
  transition: border-color 0.15s;
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  background: transparent;
  border: 0;
  max-height: 180px;
  min-height: 26px;
  padding: 8px 0;
  resize: none;
}
.composer textarea:focus { outline: none; }
.composer .enviar {
  align-items: center;
  border-radius: 11px;
  display: flex;
  height: 40px;
  justify-content: center;
  padding: 0;
  width: 40px;
}
.composer .enviar:disabled { opacity: .4; }
.pista { color: var(--muted); font-size: 11px; margin: 8px auto 0; max-width: 920px; text-align: center; }

/* Indicador de "procesando" */
.bubble.esperando { align-items: center; color: var(--muted); display: inline-flex; gap: 10px; }
.spinner {
  animation: girar 0.6s linear infinite;
  border: 3px solid var(--border);
  border-radius: 50%;
  border-top-color: var(--accent-soft);
  flex-shrink: 0;
  height: 18px;
  width: 18px;
}
@keyframes girar { to { transform: rotate(360deg); } }
.typing-dots { display: inline-flex; gap: 5px; }
.typing-dots span {
  animation: rebote 1.2s infinite ease-in-out;
  background: var(--accent-soft);
  border-radius: 50%;
  height: 7px;
  width: 7px;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes rebote {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.35; }
  40% { transform: translateY(-5px); opacity: 1; }
}
.bubble.esperando .estado { font-size: 13px; font-style: italic; }

.msg, .conv { animation: aparecer 0.25s ease; }
@keyframes aparecer {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Móvil: la sidebar se colapsa */
@media (max-width: 760px) {
  .chat-app { grid-template-columns: 1fr; }
  .chat-side {
    height: 100%;
    left: 0;
    position: fixed;
    top: 0;
    transform: translateX(-100%);
    transition: transform 0.2s;
    width: 82%;
    z-index: 20;
  }
  .chat-side.abierta { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .msg { max-width: 95%; }
}
.menu-btn { display: none; }
@media (max-width: 760px) { .menu-btn { display: inline-flex; } }

/* --------------------------------------------------------------- admin */
.admin-wrap { margin: 0 auto; max-width: 960px; padding: 24px 16px 60px; }

.tabs {
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 4px;
  margin-bottom: 24px;
  overflow-x: auto;
}

.tab {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  padding: 10px 14px;
  white-space: nowrap;
}

.tab:hover { background: transparent; color: var(--text); }
.tab.active { border-bottom-color: var(--accent-soft); color: var(--text); }

.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 20px;
  padding: 20px;
}

.card h2 { font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.card .hint { color: var(--muted); font-size: 13px; margin-bottom: 8px; }

.grid-2 { display: grid; gap: 0 16px; grid-template-columns: 1fr 1fr; }
.row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }

.stats { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.stat .n { font-size: 24px; font-weight: 600; line-height: 1.25; }
/* Los valores de texto ("Sin configurar") no deben partirse como una cifra. */
.stat .n.texto { font-size: 15px; padding: 6px 0 5px; }
.stat .l { color: var(--muted); font-size: 12px; }

table { border-collapse: collapse; font-size: 13px; width: 100%; }
th, td { border-bottom: 1px solid var(--border); padding: 9px 8px; text-align: left; }
th { color: var(--muted); font-size: 12px; font-weight: 500; }
td.actions { text-align: right; white-space: nowrap; }
td.actions button { font-size: 12px; padding: 4px 10px; }

/* Auditoría legible */
.aud-fecha { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.aud-accion {
  align-items: center;
  background: var(--surface-2, rgba(127, 127, 127, .1));
  border: 1px solid var(--border);
  border-radius: 999px;
  display: inline-flex;
  font-size: 12px;
  gap: 5px;
  padding: 3px 10px;
  white-space: nowrap;
}
.aud-detalle { color: var(--muted); font-size: 12.5px; line-height: 1.45; }

/* Diagnóstico */
.ev-card { border-left: 3px solid var(--warn, #c9852a); }
.ev-card.ev-resuelto { opacity: .55; border-left-color: var(--border); }
.ev-cab { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.ev-titulo { font-size: 14px; line-height: 1.4; }
.ev-traza {
  background: var(--surface-2, rgba(127,127,127,.08));
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 12px; margin: 8px 0 0; max-height: 240px; overflow: auto;
  padding: 10px; white-space: pre-wrap; word-break: break-word;
}
.ev-card details summary { cursor: pointer; color: var(--muted); font-size: 12.5px; margin-top: 6px; }
.ev-diag {
  background: rgba(47, 158, 99, .10); border-radius: 8px;
  font-size: 13px; line-height: 1.5; margin-top: 10px; padding: 12px 14px;
}

.note {
  background: rgba(201, 133, 42, .1);
  border-left: 3px solid var(--warn);
  border-radius: 6px;
  color: #e0b878;
  font-size: 13px;
  margin: 14px 0;
  padding: 12px 14px;
}

.badge-estado {
  border-radius: 999px;
  display: inline-block;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 6px;
  padding: 4px 12px;
}
.badge-estado.on  { background: rgba(47, 158, 99, .16); color: #4fbf87; }
.badge-estado.off { background: rgba(194, 69, 63, .16); color: #f0a9a5; }
.badge-estado.demo { background: rgba(201, 133, 42, .16); color: #e0b878; }
.estado-linea { color: var(--muted); font-size: 12.5px; line-height: 1.7; }

/* Bandeja de correos */
.bandeja-item {
  align-items: center; border: 1px solid var(--border); border-radius: 8px;
  cursor: pointer; display: flex; gap: 12px; justify-content: space-between;
  margin-top: 8px; padding: 10px 14px;
}
.bandeja-item:hover { background: var(--surface-2, rgba(127,127,127,.08)); }
.badge-canal {
  background: var(--surface-2, rgba(127,127,127,.12)); border: 1px solid var(--border);
  border-radius: 999px; font-size: 11px; margin-right: 8px; padding: 2px 8px; white-space: nowrap;
}
.bandeja-hilo { margin: 4px 0 4px 12px; padding-left: 12px; border-left: 2px solid var(--border); }
.hilo-msg { margin: 8px 0; }
.hilo-rol { color: var(--muted); font-size: 11.5px; font-weight: 600; margin-bottom: 2px; }
.hilo-msg.user .hilo-texto {
  background: var(--surface-2, rgba(127,127,127,.1)); border-radius: 8px; padding: 8px 12px;
}
.hilo-msg.assistant .hilo-texto {
  background: rgba(79,124,255,.08); border-radius: 8px; padding: 8px 12px;
}
.hilo-texto { font-size: 13px; line-height: 1.5; }

.check {
  align-items: flex-start;
  background: var(--surface-2, rgba(127, 127, 127, .06));
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  gap: 10px;
  font-size: 13px;
  line-height: 1.45;
  margin: 4px 0 16px;
  padding: 12px 14px;
}
.check input { margin-top: 2px; width: 16px; height: 16px; flex: none; accent-color: var(--accent, #4f7cff); }
.check strong { color: var(--text); }

.result { border-radius: 8px; font-size: 13px; margin-top: 14px; padding: 10px 12px; }
.result.ok { background: rgba(47, 158, 99, .12); color: #7fd7a5; }
.result.ko { background: rgba(194, 69, 63, .12); color: #f0a9a5; }
.result:empty { display: none; padding: 0; }

@media (max-width: 640px) {
  .grid-2 { grid-template-columns: 1fr; }
  .bubble { max-width: 95%; }
}
