:root {
  --bg: #0f1115; --bg2: #161a22; --bg3: #1e2430; --border: #2a3242;
  --text: #e6e9ef; --muted: #8b94a7; --accent: #4f8cff; --accent2: #2c5cb0;
  --user: #24405e; --danger: #e5534b; --ok: #3fb950; --warn: #d29922;
  --tree-clr: rgba(79,140,255,.28);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
/* El documento NUNCA scrollea: en móvil el scroll del documento arrastraba la
   app hacia arriba y dejaba ver negro por debajo (la app "desaparecía").
   Queda clavado; solo scrollean los contenedores internos (.messages, listas). */
html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
html { background: var(--bg); }
body {
  position: fixed; inset: 0;
  background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
/* El cromo de la app NUNCA entra en una selección: al mantener pulsado en una
   burbuja, el navegador acababa seleccionando la cabecera. Solo se selecciona
   el contenido del chat (y solo cuando toca: ver .msg.selecting). */
#sidebar, #chat-header, #composer, .run-status, .agent-strip, .chips, .sidebar-foot, .login-wrap {
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
/* ...pero los campos de texto SÍ se seleccionan (si no, no se puede ni corregir
   lo que escribes ni copiar de un input). */
#input, .login-card input, .s-filter, .model-select {
  -webkit-user-select: text; user-select: text; -webkit-touch-callout: default;
}
button {
  background: var(--bg3); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 15px;
}
button:hover { background: var(--border); }

/* ---------- LOGIN ---------- */
.login-wrap { height: 100dvh; display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 16px;
  padding: 40px; width: 320px; display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-logo { font-size: 48px; }
.login-card h1 { font-size: 26px; }
.login-sub { color: var(--muted); margin-top: -10px; margin-bottom: 10px; font-size: 13px; }
.login-card input {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; color: var(--text); font-size: 15px;
}
.login-card input:focus { outline: 1px solid var(--accent); }
.login-card button { background: var(--accent); border: none; padding: 10px; font-weight: 600; }
.login-card button:hover { background: var(--accent2); }
.login-error { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- LAYOUT ---------- */
.app { display: flex; height: 100vh; height: 100dvh; max-height: 100%; }
/* dvh: sin salto con la barra del navegador móvil; max-height: nunca desborda el body fijo */

/* Botón hamburguesa: solo móvil */
.btn-menu { display: none; background: none; border: none; font-size: 22px; padding: 4px 10px 4px 0; }
.scrim { display: none; }   /* solo existe en móvil, con el cajón abierto */
#sidebar {
  width: 260px; min-width: 260px; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
/* Wrapper del buscador + lista de sesiones: ocupa el espacio flexible */
.sessions-wrap { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; }
.sidebar-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 14px 10px; border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 17px; }
.btn-nav-back { display: none; }
.s-mobile-date { display: none; }
.s-tree-tools { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 2px; }
.s-tree-tools > span { font-size: 12px; color: var(--muted); }
.s-tree-tools .archive-toggle { margin: 0; min-height: 44px; }
.s-branch { font-size: 11px; color: var(--accent); }
.s-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
/* Selector de agente */
.agent-strip {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 10px 8px;
  border-bottom: 1px solid var(--border);
}
.agent-chip {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 999px; font-size: 13px;
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
}
.agent-chip .a-emoji { font-size: 15px; line-height: 1; }
.agent-chip:hover { border-color: var(--accent); }
.agent-chip.active { background: var(--accent2); border-color: var(--accent); font-weight: 600; }
.agent-chip.off { opacity: .45; }
.agent-chip.off .a-name::after { content: " \2022"; color: var(--danger); }

.session-list { flex: 1; padding: 8px; }
.session-item {
  --pad: 5px; --ind: 0px; --rail: calc(var(--pad) + 6px);
  padding: 9px 10px 9px calc(var(--pad) + var(--ind)); border-radius: 8px; cursor: pointer; margin-bottom: 2px;
  display: flex; align-items: center; gap: 4px; position: relative;
}
/* La indentación se calcula sobre --pad: si el móvil cambia el padding, el árbol
   sigue cuadrando (antes la media query pisaba el sangrado y no se veía el nivel) */
/* Columna de árbol: slot fijo en TODAS las filas, así el título no baila */
.session-item .s-tw { flex: none; width: 32px; display: flex; align-items: center; justify-content: center; position: relative; z-index: 2; }
.session-item:hover { background: var(--bg3); }
.session-item.active { background: var(--accent2); }
.session-item .s-main { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 1px; }
.session-item .s-right { display: flex; align-items: center; gap: 4px; flex: none; }
.session-item.child::before {
  content: ""; position: absolute; top: 0; bottom: 0; border-left: 2px solid var(--tree-clr);
}
.session-item.child::after {
  content: ""; position: absolute; top: 50%; height: 2px; width: 7px;
  background: var(--tree-clr); margin-top: -1px;
}
.session-item.child::before, .session-item.child::after {
  left: calc(var(--pad) + var(--ind) - 8px); pointer-events: none;
}
.session-item.child.last::before { bottom: 50%; }
/* El hijo se lee algo más pequeño, pero sin perder contraste */
.session-item.child .s-label { font-size: 13.5px; opacity: .96; }
.session-item.child .s-main { min-width: 0; }   /* el texto del hijo no queda aplastado */
.s-toggle {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  font-size: 13px; padding: 0; border-radius: 10px; line-height: 1.1;
  opacity: .95; display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 36px;
}
.s-toggle:hover { background: var(--border); opacity: 1; }
.s-toggle .chev { font-size: 11px; transition: transform .15s ease; display: inline-block; }
.s-toggle.open .chev { transform: rotate(90deg); }
.session-item .s-label {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px;
}
.session-item .s-label.unread { font-weight: 600; }
/* fallo del LLM: tachado sutil + icono rojo */
.session-item .s-fail { color: var(--danger); font-size: 0.9em; }
/* Columna fija: el punto de no leído no desplaza el título entre filas */
.session-item .s-dot {
  flex: none; width: 7px; height: 7px; border-radius: 50%; background: transparent;
}
.session-item .s-dot.on { background: var(--accent); }
.session-item .s-ask { color: var(--warn); }
/* de qué va la sesión: sin esto, varios "Sin título" son indistinguibles */
.session-item .s-sub {
  font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.session-item.active .s-sub { color: #cfe0ff; opacity: .85; }
.session-item .s-sub.s-tag { font-style: italic; opacity: .8; }
.session-item .s-date { color: var(--muted); font-size: 11px; white-space: nowrap; }
.session-item.active .s-date { color: #cfe0ff; }
/* Cabecera de día: pegajosa, versalitas y con ancla temporal, para que no se
   confunda con un título de sesión al hacer scroll largo. */
.session-item .s-day,
.s-day {
  position: sticky; top: 0; z-index: 3;
  background: var(--bg2);
  font-size: 10px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted);
  padding: 12px 10px 5px; margin: 0 -2px 4px; font-weight: 700; opacity: .85;
  border-bottom: 1px solid var(--border);
}
/* Buscador del panel lateral */
.s-filter {
  width: calc(100% - 4px); margin: 2px 2px 6px; padding: 7px 10px; font: inherit; font-size: 13px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; color: var(--text);
}
.s-filter:focus { outline: 1px solid var(--accent); }
.s-ctrl { display: flex; gap: 6px; padding: 2px; }
.s-ctrl .s-other { font-size: 11.5px; color: var(--muted); padding: 7px 10px; font-style: italic; }
.s-ctrl .archive-toggle { flex: 1; margin: 0; }
/* Botón fijar/desfijar: visible siempre, gris tenue si no fijado, acento solo si fijado */
.session-item .s-pin-btn {
  background: none; border: none; padding: 0 2px; font-size: 14px; cursor: pointer;
  filter: grayscale(1) opacity(.4);
  transition: opacity .15s;
}
.session-item .s-pin-btn.on { filter: none; opacity: 1; }
/* Sin hover de color en táctil: el color acento solo cuando está fijado */
.session-item .s-del { visibility: hidden; background: none; border: none; color: var(--muted); padding: 0 2px; font-size: 14px; }
.session-item:hover .s-del { visibility: visible; }
.session-item .s-del:hover { color: var(--danger); }
.archive-toggle {
  margin: 8px 4px 4px; padding: 7px 10px; border-radius: 8px; cursor: pointer;
  color: var(--muted); font-size: 12.5px; text-align: center; border: 1px dashed var(--border);
}
.archive-toggle:hover { color: var(--text); border-color: var(--accent); }
.sidebar-empty { padding: 14px 10px; color: var(--muted); font-size: 13px; text-align: center; }
.sidebar-loading { padding: 24px 10px; color: var(--muted); font-size: 13px; text-align: center; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }

/* Avisos de error persistentes en el sidebar */
.s-errors {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; margin: 4px 8px; border-radius: 8px;
  cursor: pointer; font-size: 12px; line-height: 1.3;
}
.s-errors.warn { background: rgba(210,153,34,.18); border: 1px solid rgba(210,153,34,.35); }
.s-errors.crit { background: rgba(229,83,75,.18); border: 1px solid rgba(229,83,75,.35); }
.s-errors .s-err-icon { flex: none; font-size: 14px; }
.s-errors .s-err-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.s-errors .s-err-count {
  flex: none; min-width: 18px; height: 18px; border-radius: 999px; text-align: center;
  line-height: 18px; font-size: 11px; font-weight: 700; background: var(--danger); color: #fff;
}
.s-errors .s-err-close {
  flex: none; opacity: .5; cursor: pointer; padding: 4px; font-size: 13px; color: var(--text);
}
.s-errors .s-err-close:hover { opacity: 1; }

.sidebar-foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-top: 1px solid var(--border); color: var(--muted); font-size: 13px;
}

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

#chat-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 18px; border-bottom: 1px solid var(--border); background: var(--bg2);
}
.head-titles { min-width: 0; flex: 1; }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.header-right { display: flex; align-items: center; gap: 12px; flex: none; }

/* Selector de modelo */
.model-select {
  background: var(--bg3); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 8px; font-size: 12.5px; max-width: 210px;
}
.model-select:hover { border-color: var(--accent); }
.model-select:focus { outline: 1px solid var(--accent); }

.ctx-meter { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.ctx-meter.warn { color: var(--warn); }
.ctx-meter.danger { color: var(--danger); }
/* aviso visible aunque el texto se oculte en pantallas pequeñas */
.ctx-meter.warn::after, .ctx-meter.danger::after { content: "\25cf"; font-size: 10px; }
.ctx-bar { width: 90px; height: 6px; background: var(--bg3); border-radius: 3px; overflow: hidden; }
#ctx-fill { height: 100%; background: var(--ok); width: 0%; transition: width .4s; }
#ctx-fill.warn { background: var(--warn); }
#ctx-fill.danger { background: var(--danger); }

.gw-status { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); flex: none; }
.gw-status.on { background: var(--ok); }
/* el agente está trabajando: el punto late */
.gw-status.busy.on { animation: gw-pulse 1.4s infinite; }
@keyframes gw-pulse {
  0% { box-shadow: 0 0 0 0 rgba(63,185,80,.55); }
  70% { box-shadow: 0 0 0 7px rgba(63,185,80,0); }
  100% { box-shadow: 0 0 0 0 rgba(63,185,80,0); }
}

/* ---------- PREGUNTAS DEL AGENTE Y ACEPTACIONES ---------- */
/* Bandeja de avisos: un contenedor con scroll PROPIO.
   Antes era un hijo flex sin min-height:0, así que cuando una pregunta con opciones largas
   no cabía, flex la aplastaba (la tarjeta se encogía y el texto de una opción invadía la
   siguiente), y al no tener overflow no había forma de scrollear: el resto quedaba perdido. */
.prompts {
  display: flex; flex-direction: column; gap: 8px;
  flex: 0 1 auto; min-height: 0;
  max-height: min(62dvh, 560px);
  overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 0 14px;
}
.prompts:empty { display: none; }
/* Los hijos flex no se encogen: manda el contenido, no el hueco disponible. */
.prompts > * { flex: none; }
.prompt {
  background: var(--bg2); border: 1px solid var(--accent); border-radius: 12px;
  padding: 12px 14px; box-shadow: 0 -2px 14px rgba(0,0,0,.25);
  overflow-wrap: anywhere;
}
.prompt.approval { border-color: var(--warn); }
.prompt.push-prompt { border-color: var(--accent); background: linear-gradient(180deg, rgba(79,140,255,.06), transparent); }
.prompt-head { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.prompt-head .who { color: var(--accent); font-weight: 600; }
.prompt.approval .prompt-head .who { color: var(--warn); }
.prompt-head .clock { margin-left: auto; font-variant-numeric: tabular-nums; }
.prompt-src { font-size: 11.5px; color: var(--accent); background: transparent; border: 1px solid var(--accent); border-radius: 999px; padding: 3px 9px; cursor: pointer; min-height: 0; white-space: nowrap; max-width: 45%; overflow: hidden; text-overflow: ellipsis; }
.prompt-src:active { background: var(--accent2); }
.prompt-q { font-size: 14.5px; margin-bottom: 8px; }
.prompt-q .qh { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); display: block; margin-bottom: 2px; }
body.prompt-open #composer .composer-row { opacity: .3; pointer-events: none; }
.prompt-hint { font-size: 11.5px; color: var(--muted); margin-top: 8px; }
.prompt-opts { display: flex; flex-direction: column; gap: 6px; }
/* Marca de opción: círculo que se rellena al elegir (visible incluso sin elegir) */
.prompt-opt { display: flex; align-items: flex-start; gap: 9px; }
.prompt-opt .mark {
  flex: none; width: 17px; height: 17px; border-radius: 50%; margin-top: 2px;
  border: 2px solid var(--muted); position: relative;
}
.prompt-opt.sel .mark { border-color: #fff; background: #fff; }
.prompt-opt.sel .mark::after { content: ""; position: absolute; inset: 3px; border-radius: 50%; background: var(--accent2); }
.prompt-opt .o { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.prompt-opt .ot { min-width: 0; overflow-wrap: anywhere; }
.prompt-opt {
  text-align: left; padding: 10px 12px; border-radius: 9px; font-size: 14px;
  background: var(--bg3); border: 1px solid var(--border); color: var(--text); min-height: 42px;
}
.prompt-opt .od { display: block; font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.prompt-opt.sel { border-color: var(--accent); background: var(--accent2); }
.prompt-opt .rec { color: var(--accent); font-size: 11px; margin-left: 6px; }
.prompt-multi { font-size: 11px; color: var(--muted); margin: 6px 0 0; }
.prompt-actions { display: flex; gap: 8px; margin-top: 10px; align-items: center; }
.prompt-actions button { min-height: 40px; }
.prompt-actions .send { background: var(--accent); border: none; font-weight: 600; padding: 8px 16px; }
.prompt-actions .send:disabled { background: var(--bg3); color: var(--muted); }
.prompt-actions .skip { background: none; border: 1px solid var(--border); color: var(--muted); }
.prompt-actions .deny { background: var(--danger); border: none; color: #fff; font-weight: 600; padding: 8px 16px; margin-left: 6px; }
.prompt-actions .allow { background: var(--ok); border: none; color: #08120b; font-weight: 700; padding: 8px 16px; }
/* "Permitir siempre" secundario: que no se pulse por inercia al lado del otro verde */
.prompt-actions .allow.ghost { background: none; border: 1px solid var(--ok); color: var(--ok); font-weight: 600; }
.prompt-actions { flex-wrap: wrap; }
.prompt-other { width: 100%; margin-top: 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 9px; color: var(--text); padding: 10px 12px; font: inherit; font-size: 15px; }
.prompt-detail {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px;
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 120px; overflow-y: auto;
}
.prompt-done { font-size: 12.5px; color: var(--muted); padding: 4px 2px; }
.prompt-done b { color: var(--ok); }

/* ---------- BARRA DE ESTADO DE EJECUCIÓN (feedback "qué está haciendo") ---------- */
.run-status {
  display: flex; align-items: center; gap: 9px;
  padding: 6px 18px; font-size: 12.5px; color: var(--muted);
  background: linear-gradient(90deg, rgba(79,140,255,.12), rgba(79,140,255,0) 62%);
  border-top: 1px solid var(--border);
  min-width: 0;
}
.run-status .rs-dots { display: inline-flex; gap: 3px; flex: none; }
.run-status .rs-dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
  animation: rs-bounce 1.2s infinite ease-in-out;
}
.run-status .rs-dots i:nth-child(2) { animation-delay: .15s; }
.run-status .rs-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes rs-bounce {
  0%, 80%, 100% { opacity: .25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}
.run-status .rs-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run-status.done { color: var(--ok); background: none; }
.run-status.done .rs-dots { display: none; }
/* Error persistente en la sesión: se ve hasta que el usuario envía un nuevo mensaje */
.run-status.error {
  background: rgba(229,83,75,.18);
  border-color: var(--danger);
  color: var(--danger);
}
.run-status.error .rs-dots { display: none; }

/* ---------- MESSAGES ---------- */
.messages {
  flex: 1; overflow-y: auto; padding: 20px 18px 10px; display: flex; flex-direction: column; gap: 14px;
  overscroll-behavior: contain;        /* el rebote del chat no arrastra al documento */
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: 78%; padding: 10px 14px; border-radius: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;   /* rutas/URLs largas no desbordan ni parten a lo bruto */
  hyphens: none;
}
.msg blockquote {
  border-left: 3px solid var(--accent); margin: 8px 0; padding: 4px 0 4px 12px;
  color: var(--muted); background: rgba(79,140,255,.06); border-radius: 0 6px 6px 0;
}
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--bg2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.system { align-self: center; background: none; color: var(--muted); font-size: 12px; padding: 2px; }

/* ---------- SELECCIÓN DE TEXTO (mantener pulsado en una burbuja) ----------
   En táctil la selección nativa está desactivada: se seleccionaba lo que pillara
   (cabecera incluida). El long-press abre un menú con las dos opciones: copiar el
   mensaje entero, o entrar en modo selección para quedarse con un trozo. */
@media (pointer: coarse) {
  .msg, .msg * { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
  /* Los enlaces sí se pueden tocar: abren el navegador */
  .msg a { -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
}
body.sel-mode .msg.selecting,
body.sel-mode .msg.selecting * {
  -webkit-user-select: text !important; user-select: text !important;
  -webkit-touch-callout: default !important;
}
.msg.selecting { outline: 2px solid var(--accent); outline-offset: 2px; }
/* Mensaje sobre el que actúa el menú: sin marcarlo no se sabe de cuál se trata */
.msg.menu-target { outline: 2px solid var(--accent); outline-offset: 2px; box-shadow: 0 0 0 6px rgba(79,140,255,.14); }
body.sel-mode #composer .composer-row { opacity: .35; pointer-events: none; }

.scrim-menu { position: fixed; inset: 0; z-index: 55; background: rgba(0,0,0,.5); }
.msg-menu {
  position: fixed; z-index: 60; display: flex; flex-direction: column; gap: 4px;
  background: var(--bg3); border: 1px solid var(--border); border-radius: 12px;
  padding: 6px; min-width: 210px; box-shadow: 0 10px 28px rgba(0,0,0,.55);
}
.msg-menu button, .sel-bar button {
  text-align: left; background: none; border: none; padding: 11px 12px;
  font-size: 15px; border-radius: 8px; min-height: 44px; color: var(--text);
}
.msg-menu button:hover, .sel-bar button:hover { background: var(--border); }
.sel-bar {
  position: fixed; z-index: 60; display: flex; gap: 6px; align-items: center;
  background: var(--bg3); border: 1px solid var(--border); border-radius: 12px;
  padding: 6px; box-shadow: 0 10px 28px rgba(0,0,0,.55);
  max-width: calc(100vw - 16px);
}
.sel-bar .sel-hint { color: var(--muted); font-size: 12px; padding: 0 4px 0 8px; white-space: nowrap; flex: none; }
.sel-bar button { flex: none; text-align: center; }
.sel-bar button.primary { background: var(--accent); color: #fff; font-weight: 600; }
.sel-bar button.primary:hover { background: var(--accent2); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 84px; z-index: 70;
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  padding: 10px 16px; border-radius: 999px; font-size: 14px;
  box-shadow: 0 8px 22px rgba(0,0,0,.5); pointer-events: none;
}
.msg .meta { font-size: 10.5px; color: var(--muted); margin-top: 5px; opacity: .7; text-align: right; }
.msg .meta:empty { display: none; }
.msg.assistant .meta { opacity: .7; }
.msg.streaming::after { content: "▊"; animation: blink 1s infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
/* "pensando": todavía sin texto -> tres puntos en vez del cursor */
.msg .dots { display: none; gap: 4px; align-items: center; padding: 3px 0; }
.msg.thinking .dots { display: inline-flex; }
.msg.thinking::after { content: none; }
.msg .dots i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--muted);
  animation: think 1.3s infinite ease-in-out;
}
.msg .dots i:nth-child(2) { animation-delay: .18s; }
.msg .dots i:nth-child(3) { animation-delay: .36s; }
@keyframes think { 0%, 70%, 100% { opacity: .3; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-3px); } }
.msg pre {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; overflow-x: auto; margin: 8px 0; white-space: pre; font-size: 13px;
}
.msg code { background: var(--bg); border-radius: 4px; padding: 1px 5px; font-size: 13px; }
.msg pre code { background: none; padding: 0; }
.msg a { color: var(--accent); }
.msg img.chat-img { max-width: 340px; max-height: 260px; border-radius: 10px; margin-top: 8px; cursor: zoom-in; display: block; }
.msg .file-card {
  display: flex; align-items: center; gap: 10px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; margin-top: 8px;
  text-decoration: none; color: var(--text); font-size: 14px;
  max-width: 100%;
}
.msg .file-card:hover { border-color: var(--accent); }
.msg .file-card .f-icon { font-size: 20px; flex: none; }
/* nombre largo: una linea con puntos suspensivos, nunca parte la palabra */
.msg .file-card .f-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1;
}
.msg .file-card .f-size { color: var(--muted); font-size: 15px; flex: none; }

/* ---------- COMPOSER ---------- */
.composer { border-top: 1px solid var(--border); background: var(--bg2); padding: 10px 14px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.chips:empty { margin-bottom: 0; }
.chip {
  display: flex; align-items: center; gap: 6px; background: var(--bg3);
  border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; font-size: 13px;
}
.chip img { max-height: 34px; border-radius: 5px; }
.chip .x { cursor: pointer; color: var(--muted); }
.chip .x:hover { color: var(--danger); }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); padding: 10px 12px; font: inherit; resize: none;
  min-height: 41px; max-height: 160px; line-height: 1.35;
  field-sizing: content;          /* auto-size nativo (Chrome 123+, FF 132+) */
  overflow-y: auto;
  scrollbar-width: none;          /* Firefox: sin barra visible */
}
#input::-webkit-scrollbar { display: none; }  /* Chrome: nunca mostrar flechas */
#input:focus { outline: 1px solid var(--accent); }
#btn-send { background: var(--accent); border: none; padding: 8px 16px; }
#btn-send:hover { background: var(--accent2); }
#btn-send:disabled { background: var(--bg3); color: var(--muted); cursor: default; }
#btn-stop { background: var(--danger); border: none; padding: 8px 14px; }
#btn-attach { padding: 8px 12px; }

.drop-overlay {
  position: absolute; inset: 0; background: rgba(15,17,21,.85); z-index: 10;
  display: flex; align-items: center; justify-content: center;
}
.drop-overlay div {
  border: 2px dashed var(--accent); border-radius: 16px; padding: 40px 60px; font-size: 20px;
}

.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 100;
  display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 8px; }

.empty-hint { margin: auto; text-align: center; color: var(--muted); }
.empty-hint .big { font-size: 42px; margin-bottom: 10px; }

/* ---------- MÓVIL ---------- */
@media (max-width: 820px) {
  /* Sidebar como cajón deslizante: el chat ocupa toda la pantalla */
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 20;
    width: 100%; max-width: none; min-width: 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 2px 0 18px rgba(0,0,0,.5);
    padding-top: env(safe-area-inset-top);
  }
  body.nav-open #sidebar { transform: translateX(0); }
  body.nav-open #main { visibility: hidden; }
  .btn-nav-back { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 8px 12px; }
  .sidebar-head { gap: 10px; }
  .sidebar-head .brand { flex: 1; text-align: center; }
  /* Fondo oscuro tras el cajón: tapa el chat y captura el toque para cerrar */
  body.nav-open .scrim:not(.hidden) {
    display: block;
    position: fixed; inset: 0; z-index: 19;
    background: rgba(0,0,0,.55);
    -webkit-backdrop-filter: blur(1px); backdrop-filter: blur(1px);
  }

  .btn-menu { display: block; }

  #chat-header {
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
    gap: 8px;
  }
  .chat-title { font-size: 15px; }
  .header-right { gap: 10px; }
  .ctx-bar { width: 54px; }
  #ctx-text { font-size: 11px; }
  .model-select { max-width: 116px; font-size: 12px; padding: 5px 6px; }
  .chat-sub { display: none; }
  .agent-strip { padding: 8px; gap: 5px; }
  .agent-chip { padding: 8px 11px; font-size: 13.5px; }

  .messages { padding: 14px 12px 6px; gap: 12px; }
  .prompts { padding: 0 12px; max-height: min(58dvh, 480px); }
  .run-status { padding: 7px 12px; font-size: 12px; }
  .msg { max-width: 90%; font-size: 15px; }
  .msg img.chat-img { max-width: 100%; max-height: 300px; }

  .composer {
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  /* 16px evita el zoom automático de iOS al enfocar el campo */
  #input { font-size: 16px; padding: 11px 12px; min-height: 44px; }
  /* Targets táctiles cómodos (mínimo 44px) */
  #btn-send, #btn-stop, #btn-attach { min-width: 46px; min-height: 44px; padding: 8px 12px; font-size: 17px; }
  .session-item { --pad: 8px; padding: 8px 4px 8px calc(var(--pad) + var(--ind)); gap: 6px; }
  .session-item .s-tw { width: 44px; }
  .s-toggle { width: 44px; height: 44px; }
  .s-toggle .chev { font-size: 15px; }
  .session-item .s-label { font-size: 15px; white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; line-height: 1.35; }
  .session-item .s-sub { font-size: 12px; }
  .session-item .s-right { flex-direction: column; gap: 0; width: 44px; }
  .session-item .s-right .s-date { display: none; }
  .session-item .s-mobile-date { display: block; font-size: 10px; }
  .session-item .s-pin-btn, .session-item .s-del { width: 44px; min-height: 44px; }
  .session-item .s-main { gap: 3px; }
  .s-filter { min-height: 44px; }
  .s-ctrl { flex-wrap: wrap; }
  .s-filter { font-size: 16px; padding: 9px 10px; }   /* 16px evita el zoom de iOS */
  /* separado del reloj para no borrar sesiones sin querer */
  .session-item .s-del { visibility: visible; padding: 6px 4px 6px 10px; font-size: 15px; opacity: .55; }
  .session-item .s-pin-btn { padding: 6px 4px; font-size: 15px; visibility: visible; }
  .sidebar-head { padding: 14px 14px 12px; }
  #btn-new { min-width: 44px; min-height: 40px; font-size: 20px; }
  .sidebar-foot { padding: 12px 14px calc(12px + env(safe-area-inset-bottom)); }
  #btn-logout { min-width: 44px; min-height: 38px; }

  .login-card { width: min(92vw, 340px); padding: 32px 24px; }
  .login-card input { font-size: 16px; padding: 12px; }
  .login-card button { padding: 13px; }

  .drop-overlay { display: none !important; }  /* no aplica en táctil */
}

/* Cabecera compacta: en móvil el contador estorba, basta el % */
@media (max-width: 560px) {
  .ctx-meter .ctx-bar { display: none; }
  #ctx-text { font-size: 11px; opacity: .85; }
  .chat-title { max-width: 52vw; }
}
@media (max-width: 380px) {
  #ctx-text { display: none; }
  .chat-title { max-width: 60vw; }
}
