: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;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  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%;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
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: 100vh; 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; }  /* dvh: sin salto con la barra del navegador móvil */

/* 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;
}
.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; }
.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 2px;
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.session-item:hover { background: var(--bg3); }
.session-item.active { background: var(--accent2); }
.session-item .s-label {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; font-size: 14px;
}
.session-item .s-date { color: var(--muted); font-size: 11px; white-space: nowrap; }
.session-item.active .s-date { color: #cfe0ff; }
.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-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);
}
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.header-right { display: flex; align-items: center; gap: 14px; }

.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); }
.gw-status.on { background: var(--ok); }

/* ---------- MESSAGES ---------- */
.messages { flex: 1; overflow-y: auto; padding: 20px 18px 10px; display: flex; flex-direction: column; gap: 14px; }
.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; }
.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; } }
.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: 78vw; max-width: 300px;
    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); }
  /* 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; }

  .messages { padding: 14px 12px 6px; gap: 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 { padding: 13px 12px; gap: 10px; }
  /* 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; }
  .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; }
}
