/* Mobile-first a propósito: este cliente se usa desde el celular.
   Nada depende de :hover para funcionar; los targets táctiles son >= 44px. */

:root {
  --bg: #0f1115;
  --bg-elevated: #171a21;
  --bg-input: #1f232c;
  --border: #2a2f3a;
  --text: #eef0f4;
  --text-dim: #9aa2b1;
  --accent: #4f7cff;
  --accent-dim: #2f4fb0;
  --danger: #ff5d5d;
  --danger-bg: rgba(255, 93, 93, 0.12);
  --bubble-mine: #2f4fb0;
  --bubble-theirs: #232833;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Badge STG/PRD -- fixed sobre TODO (login incluido, ver comentario en el
   HTML). z-index alto a propósito: tiene que ganarle a los modales, que ya
   usan valores altos para tapar el resto de la pantalla. */
.env-badge {
  position: fixed;
  top: calc(8px + var(--safe-top));
  right: 8px;
  z-index: 500;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  pointer-events: none; /* nunca debe interceptar un tap destinado a lo que está debajo */
}

.env-badge--staging {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
}

.env-badge--prod {
  background: rgba(79, 124, 255, 0.12);
  color: var(--accent);
  border: 1px solid var(--accent);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f8;
    --bg-elevated: #ffffff;
    --bg-input: #eef0f4;
    --border: #dde1e8;
    --text: #14161b;
    --text-dim: #666f7f;
    --accent: #3a5edb;
    --accent-dim: #d7dffa;
    --danger: #c62828;
    --danger-bg: rgba(198, 40, 40, 0.08);
    --bubble-mine: #3a5edb;
    --bubble-theirs: #e7e9ef;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* El atributo nativo `hidden` pierde contra `.screen { display: flex }` de
   arriba -- una regla de autor siempre gana sobre la regla por-defecto del
   navegador para [hidden], aunque la especificidad "parezca" igual. Sin
   esto, las 3 pantallas (login / cambiar password / chat) se ven todas
   apiladas a la vez en vez de una sola por turno. */
.screen[hidden] {
  display: none;
}

/* ───────────── Login / cambiar password ───────────── */

#screen-login,
#screen-change-password {
  align-items: center;
  justify-content: center;
  padding: max(16px, var(--safe-top)) 16px max(16px, var(--safe-bottom));
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 22px;
}

.card h1 {
  margin: 0 0 4px;
  font-size: 1.5rem;
}

.subtitle {
  margin: 0 0 20px;
  color: var(--text-dim);
  font-size: 0.95rem;
}

.field {
  display: block;
  margin-bottom: 16px;
}

.field span {
  display: block;
  margin-bottom: 6px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.field input {
  width: 100%;
  padding: 12px 14px;
  font-size: 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
}

.field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Botón mostrar/ocultar contraseña -- inyectado por JS (wirePasswordToggles),
   envuelve el <input type="password"> original sin tocar su markup en el HTML. */
.password-toggle-wrap {
  position: relative;
  display: block;
}

.password-toggle-wrap input {
  padding-right: 44px; /* deja lugar al botón sin que tape el texto tipeado */
}

.btn-toggle-password {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 6px;
  border-radius: 8px;
  color: var(--text-dim);
}

.btn-toggle-password:hover,
.btn-toggle-password:focus-visible {
  color: var(--text);
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Bloque "Opciones avanzadas" del modal de agregar contacto -- <details> nativo,
   sin JS propio para el toggle. */
.field-advanced {
  margin-bottom: 16px;
}

.field-advanced summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.field-advanced summary:hover {
  color: var(--text);
}

.field-advanced .field {
  margin-top: 10px;
}

.hint {
  margin: -8px 0 14px;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.error-text {
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 0.9rem;
}

.btn {
  appearance: none;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  padding: 0 18px;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-small {
  min-height: 40px;
  padding: 0 14px;
  font-size: 0.85rem;
  background: var(--accent);
  color: #fff;
}

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

/* ───────────── Chat (shell con pestañas) ───────────── */

#screen-chat {
  height: 100dvh;
}

/* Mismo problema que .screen[hidden] más arriba: `.view { display:flex }`
   le gana a la regla por-defecto de [hidden] del navegador. */
.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.view[hidden] {
  display: none;
}

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, var(--safe-top)) 14px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 1.05rem;
}

.btn-icon {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1.15rem;
  line-height: 1;
  min-width: 40px;
  min-height: 40px;
  border-radius: 50%;
  cursor: pointer;
}

.btn-icon:active {
  background: var(--bg-input);
}

.list-header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* ───────────── búsqueda GENERAL (todos los contactos) ─────────────
   Clases propias, sin reusar .search-bar/.search-input/.search-empty
   (esas son de la búsqueda POR HILO de #view-thread, otro elemento). */

.global-search-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.global-search-bar input {
  flex: 1;
  padding: 8px 12px;
  font-size: 0.9rem;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
}

.global-search-hint {
  margin: 0;
  padding: 6px 14px;
  font-size: 0.72rem;
  color: var(--text-dim);
  text-align: center;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.global-search-results {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.global-search-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.global-search-row:active {
  background: var(--bg-input);
}

.global-search-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.global-search-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.global-search-name {
  font-weight: 600;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.global-search-time {
  flex: 0 0 auto;
  font-size: 0.72rem;
  color: var(--text-dim);
}

.global-search-snippet {
  font-size: 0.85rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.global-search-snippet mark {
  background: var(--accent-dim);
  color: var(--accent);
  border-radius: 3px;
  padding: 0 1px;
}

.global-search-empty {
  margin: 40px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ───────────── lista de contactos ───────────── */

.contact-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

/* Estructura: <li class="contact-row-wrapper"> contiene el fondo rojo
   (.contact-swipe-bg, position:absolute, detrás) + la fila real
   (.contact-row, opaca, encima) -- swipe = trasladar .contact-row a la
   izquierda con JS, dejando ver el fondo. El borde va en el WRAPPER, no en
   la fila, para que no se corte raro cuando la fila se traslada. */
.contact-row-wrapper {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}

.contact-row-wrapper--removing {
  pointer-events: none;
}

.contact-swipe-bg {
  position: absolute;
  inset: 0;
  background: var(--danger);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 24px;
  font-size: 1.2rem;
  opacity: 0;
  cursor: pointer;
}

.contact-row {
  position: relative;
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.contact-row:active {
  background: var(--bg-input);
}

.contact-avatar {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1.05rem;
  background: var(--accent-dim);
  color: var(--accent);
}

.contact-avatar--system {
  background: rgba(224, 168, 0, 0.18);
  color: #d98324;
}

.contact-avatar--agent {
  background: var(--bg-input);
  color: var(--text-dim);
}

.contact-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.contact-name-row,
.contact-preview-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.contact-name {
  font-weight: 600;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-time {
  flex: 0 0 auto;
  font-size: 0.72rem;
  color: var(--text-dim);
}

.contact-preview {
  font-size: 0.85rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.unread-badge {
  flex: 0 0 auto;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-state {
  margin: 40px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ───────────── nav inferior ───────────── */

.bottom-nav {
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  padding-bottom: var(--safe-bottom);
  flex: 0 0 auto;
}

.nav-btn {
  flex: 1;
  appearance: none;
  border: none;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  color: var(--text-dim);
  font-size: 0.68rem;
  cursor: pointer;
}

.nav-btn.active {
  color: var(--accent);
}

.nav-icon {
  font-size: 1.25rem;
  line-height: 1;
}

/* ───────────── perfil ───────────── */

.perfil-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 16px max(20px, var(--safe-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
}

.perfil-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--accent-dim);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.perfil-email {
  margin: 0;
  font-weight: 600;
}

.perfil-sub {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--text-dim);
}

.perfil-section {
  width: 100%;
  max-width: 420px;
  margin-top: 20px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.perfil-section-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
}

.perfil-section-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin: 0 0 10px;
}

.perfil-row {
  width: 100%;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
  cursor: pointer;
  text-align: left;
}

.perfil-row:last-child {
  border-bottom: none;
}

.device-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
}

.device-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.device-label {
  font-weight: 600;
  font-size: 0.88rem;
  flex: 0 0 auto;
}

.device-meta {
  flex: 1;
  min-width: 0;
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* ───────────── modal: agregar contacto ───────────── */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 60;
}

/* 🔴 BUG REAL ENCONTRADO 2026-08-31 (reporte del owner: "Cancelar no hace
   nada", "no me deja volver", "parece que agregás a alguien" cada vez que
   entra al chat): mismo problema que .screen[hidden]/.view[hidden] de más
   arriba, pero acá faltaba el override -- SIN esto, `.modal { display:flex }`
   le gana al `hidden` nativo y el modal queda SIEMPRE visible apenas
   `#screen-chat` deja de estar oculto, sin importar lo que haga el JS
   (`hidden = true` cambia el atributo, no lo que se ve). "Cancelar" SÍ
   estaba funcionando del lado de JS -- el CSS lo tapaba. */
.modal[hidden] {
  display: none;
}

@media (min-width: 640px) {
  .modal {
    align-items: center;
  }
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elevated);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 22px;
  padding-bottom: max(22px, var(--safe-bottom));
}

@media (min-width: 640px) {
  .modal-card {
    border-radius: var(--radius);
  }
}

.modal-card h2 {
  margin: 0 0 4px;
  font-size: 1.1rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

/* ───────────── hilo (header + avatar del contacto) ───────────── */

.thread-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.95rem;
  background: var(--accent-dim);
  color: var(--accent);
  flex: 0 0 auto;
}

.thread-avatar--system {
  background: rgba(224, 168, 0, 0.18);
  color: #d98324;
}

.thread-avatar--agent {
  background: var(--bg-input);
  color: var(--text-dim);
}

.message-count {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 9px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
  vertical-align: middle;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.search-bar input {
  flex: 1;
  padding: 8px 12px;
  font-size: 0.9rem;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
}

.search-empty {
  margin: 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.day-divider {
  display: flex;
  justify-content: center;
  margin: 6px 0;
}

.day-divider span {
  background: var(--bg-elevated);
  color: var(--text-dim);
  font-size: 0.72rem;
  padding: 3px 12px;
  border-radius: 10px;
}

/* ───────────── login: passkey ───────────── */

.btn-passkey {
  margin-bottom: 14px;
}

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 16px;
  color: var(--text-dim);
  font-size: 0.78rem;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: max(12px, var(--safe-top)) 14px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.chat-header-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-title {
  font-weight: 700;
  font-size: 1.05rem;
}

.chat-subtitle {
  font-size: 0.78rem;
  color: var(--text-dim);
}

.chat-header-actions {
  display: flex;
  gap: 8px;
}

.conn-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.conn-dot--off {
  background: var(--text-dim);
}

.conn-dot--connecting {
  background: #e0a800;
}

.conn-dot--on {
  background: #2ecc71;
}

.no-history-notice {
  margin: 0;
  padding: 8px 14px;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.bubble-row {
  display: flex;
}

.bubble-row--mine {
  justify-content: flex-end;
}

.bubble-row--theirs {
  justify-content: flex-start;
}

.bubble {
  max-width: 78%;
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.35;
  word-break: break-word;
  white-space: pre-wrap;
}

.bubble-row--mine .bubble {
  background: var(--bubble-mine);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.bubble-row--theirs .bubble {
  background: var(--bubble-theirs);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.bubble-meta {
  display: block;
  margin-top: 4px;
  font-size: 0.68rem;
  opacity: 0.75;
}

.system-notice {
  align-self: center;
  font-size: 0.75rem;
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: 10px;
  background: var(--bg-elevated);
}

.typing-indicator {
  padding: 4px 14px;
  font-size: 0.78rem;
  color: var(--text-dim);
  font-style: italic;
}

.send-row {
  display: flex;
  gap: 8px;
  padding: 10px;
  padding-bottom: max(10px, var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
}

.send-row input {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  font-size: 1rem;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
}

.send-row input:disabled {
  opacity: 0.6;
}

.btn-send {
  border-radius: 22px;
  min-width: 84px;
}

/* ───────────── toast de error global ───────────── */

.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: max(12px, var(--safe-bottom));
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--danger);
  color: #fff;
  font-size: 0.9rem;
  text-align: center;
  z-index: 50;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

@media (min-width: 640px) {
  .toast {
    left: auto;
    right: 20px;
    bottom: 20px;
    max-width: 360px;
  }
}
