/* Colores: siempre desde themeParams de Telegram (--tg-theme-*), con valores de reserva. */
:root {
  --bg: var(--tg-theme-bg-color, #fff);
  --txt: var(--tg-theme-text-color, #000);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --bg2: var(--tg-theme-secondary-bg-color, #efeff3);
  --acento: var(--tg-theme-button-color, #2481cc);
  --acento-txt: var(--tg-theme-button-text-color, #fff);
  --acento-texto: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2481cc));
  --sep: var(--tg-theme-section-separator-color, rgba(128, 128, 128, .2));
  --sub: var(--tg-theme-subtitle-text-color, var(--hint));
  --peligro: var(--tg-theme-destructive-text-color, #e53935);
  --mia: var(--bg2);
  --suya: var(--bg);
  --fondo-chat: var(--bg2);

  /* Insets de Telegram (Bot API 8.0+). Fuera de pantalla completa suelen ser 0. */
  --arriba: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --abajo: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
  --izq: var(--tg-safe-area-inset-left, 0px);
  --der: var(--tg-safe-area-inset-right, 0px);
}
@supports (color: color-mix(in srgb, red, blue)) {
  :root { --mia: color-mix(in srgb, var(--acento) 20%, var(--bg)); }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--txt);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation; /* sin zoom por doble toque ni retardo de 300 ms */
}
/* Con teclado abierto sobra el hueco de la barra de inicio del iPhone */
.teclado { --abajo: 0px; }
/* La interfaz no se selecciona ni saca el menú de iOS al mantener pulsado; las burbujas se copian con pulsación larga */
.barra, .fila, .seccion, .composer button, .burbuja, .dia, .evento, .bajar {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
:focus-visible { outline: 2px solid var(--acento); outline-offset: 2px; }
.acento { color: var(--acento-texto); }

#app {
  position: relative; overflow: hidden;
  height: var(--app-h, var(--tg-viewport-stable-height, 100dvh));
}

/* Dos pantallas apiladas; el chat entra deslizando desde la derecha. */
.pantalla {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  background: var(--bg);
  transition: transform .25s ease;
}
#chat { transform: translateX(100%); background: var(--fondo-chat); }
#chat.activa { transform: none; box-shadow: -8px 0 24px rgba(0, 0, 0, .12); }
#lista.atras { transform: translateX(-25%); }
@media (prefers-reduced-motion: reduce) { .pantalla { transition: none; } }

/* Cabecera */
.barra {
  display: flex; align-items: center; gap: 10px;
  min-height: 56px;
  padding: calc(8px + var(--arriba)) calc(12px + var(--der)) 8px calc(12px + var(--izq));
  background: var(--bg);
  border-bottom: 1px solid var(--sep);
  flex: none;
}
.barra-texto { flex: 1; min-width: 0; }
.barra h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.barra h2 { margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub { margin: 1px 0 0; font-size: 13px; color: var(--sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icono { width: 36px; height: 36px; border-radius: 50%; font-size: 22px; line-height: 1; display: grid; place-items: center; color: var(--acento-texto); }
.icono:active { background: var(--bg2); }
#atras { font-size: 34px; margin-left: -6px; width: 30px; }
.solo-mock { display: none; }
.mock .solo-mock { display: grid; }

/* Lista de agentes */
.agentes { list-style: none; margin: 0; padding: 0 0 var(--abajo); overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.fila {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 8px calc(12px + var(--der)) 8px calc(12px + var(--izq));
  min-height: 72px;
}
.fila:active { background: var(--bg2); }
.fila .cuerpo { flex: 1; min-width: 0; align-self: stretch; display: flex; flex-direction: column; justify-content: center; gap: 3px; border-bottom: 1px solid var(--sep); padding-bottom: 2px; }
li:last-child .fila .cuerpo, li:has(+ .seccion) .fila .cuerpo { border-bottom: 0; }
.linea { display: flex; align-items: center; gap: 8px; min-width: 0; }
.nombre { flex: 1; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hora { font-size: 12px; color: var(--hint); flex: none; }
.vista { flex: 1; font-size: 14px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.vista b { font-weight: 600; }
.vista .borrador { color: var(--peligro); }
.badge {
  flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--acento); color: var(--acento-txt);
  font-size: 12px; font-weight: 600; line-height: 20px; text-align: center;
}
.seccion {
  padding: 14px 16px 6px; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  color: var(--tg-theme-section-header-text-color, var(--hint)); background: var(--bg2);
}
.apagada { opacity: .55; }

/* Avatar con punto de estado (idea: el estado del agente se ve de un vistazo) */
.avatar {
  --c: #888;
  position: relative; flex: none;
  width: 50px; height: 50px; border-radius: 50%;
  display: grid; place-items: center; font-size: 24px;
  background: var(--c);
  background: color-mix(in srgb, var(--c) 28%, var(--bg));
}
.avatar.pequeno { width: 38px; height: 38px; font-size: 19px; }
.punto { position: absolute; right: 0; bottom: 0; width: 13px; height: 13px; border-radius: 50%; border: 2px solid var(--bg); background: #31b545; }
.avatar.pequeno .punto { width: 11px; height: 11px; }
.punto.trabajando { background: #f0a020; animation: latido 1.2s ease-in-out infinite; }
.punto.esperando { background: var(--acento); }
.punto.bloqueado { background: var(--peligro); }
@keyframes latido { 50% { transform: scale(.7); opacity: .6; } }

/* Chat */
.zona { flex: 1; position: relative; min-height: 0; }
.mensajes {
  position: absolute; inset: 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 8px calc(10px + var(--der)) 12px calc(10px + var(--izq));
  display: flex; flex-direction: column; gap: 4px;
}
.mensajes > :first-child { margin-top: auto; } /* mensajes pegados abajo, como en WhatsApp */
.dia, .evento {
  align-self: center; margin: 8px 0 4px; padding: 4px 10px; border-radius: 10px;
  font-size: 12.5px; color: var(--hint); background: var(--bg); text-align: center; max-width: 90%;
}
.evento::before { content: "⚙︎ "; }
.burbuja {
  position: relative; max-width: 82%; padding: 6px 10px 6px; border-radius: 16px;
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .08);
}
.suya { align-self: flex-start; background: var(--suya); border-bottom-left-radius: 5px; }
.mia { align-self: flex-end; background: var(--mia); border-bottom-right-radius: 5px; }
.meta { float: right; margin: 6px 0 -4px 10px; font-size: 11px; color: var(--hint); white-space: nowrap; }
.check { color: var(--acento-texto); }
/* Mensajes seguidos del mismo lado: más juntos y sin "pico" repetido */
.burbuja.seguida { margin-top: -2px; }
.suya.seguida { border-top-left-radius: 5px; }
.mia.seguida { border-top-right-radius: 5px; }
.burbuja.pulsada { transform: scale(.97); filter: brightness(.92); transition: transform .12s, filter .12s; }
.fallo { opacity: .7; cursor: pointer; }
.meta.error { color: var(--peligro); }

/* Botón "ir al final" con contador de mensajes nuevos */
.bajar {
  position: absolute; right: calc(12px + var(--der)); bottom: 12px;
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: var(--bg); color: var(--hint); box-shadow: 0 2px 8px rgba(0, 0, 0, .18);
}
.bajar .badge { position: absolute; top: -6px; right: -4px; }
.bajar[hidden], .badge[hidden] { display: none; }

.tarjeta { padding: 10px 12px; border-left: 3px solid var(--acento); white-space: normal; }
.tarjeta strong { display: block; font-size: 15px; }
.tarjeta p { margin: 4px 0 8px; }
.acciones { display: flex; gap: 8px; flex-wrap: wrap; }
.acciones button {
  padding: 7px 14px; border-radius: 16px; font-size: 14px; font-weight: 600;
  background: var(--acento); color: var(--acento-txt);
}
.acciones button + button { background: var(--bg2); color: var(--acento-texto); }
.hecho { color: var(--hint); font-size: 13px; }

.tecleo { display: flex; gap: 4px; padding: 12px 14px; }
.tecleo i { width: 7px; height: 7px; border-radius: 50%; background: var(--hint); animation: puntos 1.2s infinite; }
.tecleo i:nth-child(2) { animation-delay: .15s; }
.tecleo i:nth-child(3) { animation-delay: .3s; }
@keyframes puntos { 0%, 60%, 100% { opacity: .3; transform: none; } 30% { opacity: 1; transform: translateY(-3px); } }

/* Campo de texto fijo abajo */
.composer {
  flex: none; display: flex; align-items: flex-end; gap: 8px;
  padding: 6px calc(8px + var(--der)) calc(6px + var(--abajo)) calc(8px + var(--izq));
  background: var(--bg); border-top: 1px solid var(--sep);
}
.composer textarea {
  flex: 1; resize: none; max-height: 126px; min-height: 40px;
  padding: 9px 14px; border-radius: 20px; border: 1px solid var(--sep);
  background: var(--bg2); color: var(--txt);
  font: inherit; font-size: 16px; /* 16px: iOS no hace zoom al enfocar */
  line-height: 22px; outline: none;
}
.composer textarea::placeholder { color: var(--hint); }
.redondo { flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: var(--acento); color: var(--acento-txt); }
.redondo:active { transform: scale(.94); }
#enviar { display: none; }
.con-texto #enviar { display: grid; }
.con-texto #microfono { display: none; }

/* Aviso tipo toast */
.aviso {
  position: fixed; left: 50%; bottom: calc(76px + var(--abajo)); transform: translateX(-50%);
  width: max-content; max-width: calc(100% - 32px);
  padding: 10px 14px; border-radius: 12px; font-size: 14px; text-align: center;
  background: var(--txt); color: var(--bg); box-shadow: 0 4px 16px rgba(0, 0, 0, .2); z-index: 10;
}
.aviso[hidden] { display: none; }

/* Pasos del agente (progreso en vivo) */
.pasos {
  align-self: center; max-width: 90%; margin: 6px 0 2px;
  font-size: 12.5px; color: var(--hint); background: var(--bg); border-radius: 10px;
  -webkit-user-select: none; user-select: none;
}
.pasos-cab { display: flex; align-items: center; gap: 6px; padding: 4px 10px; font-size: inherit; color: inherit; }
.pasos.vivo .pasos-cab { color: var(--acento-texto); cursor: default; }
.pasos.vivo .pasos-cab::before {
  content: ""; flex: none; width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid currentColor; border-top-color: transparent;
  animation: girar .8s linear infinite; /* solo transform: lo hace la GPU */
}
@keyframes girar { to { transform: rotate(360deg); } }
.pasos-lista { margin: 0; padding: 0 12px 8px 30px; }
.pasos-lista li { margin: 2px 0; }
.pasos-lista[hidden] { display: none; }

/* Respuesta llegando en streaming: cursor al final */
.burbuja.escribiendo .txt::after { content: "▍"; color: var(--acento-texto); animation: parpadeo 1s steps(1) infinite; }
@keyframes parpadeo { 50% { opacity: 0; } }

/* Aviso de otro agente, tipo notificación de iOS */
.banner {
  position: fixed; z-index: 20;
  top: calc(8px + var(--arriba)); left: calc(8px + var(--izq)); right: calc(8px + var(--der));
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; text-align: left;
  border-radius: 14px; background: var(--bg); color: var(--txt);
  border: 1px solid var(--sep); box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  transform: translateY(calc(-100% - 24px)); transition: transform .3s ease;
  -webkit-user-select: none; user-select: none;
}
.banner.visible { transform: none; }
.banner[hidden] { display: none; }
.banner-texto { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 14px; line-height: 1.3; }
.banner-texto span { color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (prefers-reduced-motion: reduce) { .banner { transition: none; } .punto.trabajando, .pasos.vivo .pasos-cab::before { animation: none; } }

/* Al final para ganar a .vista/.hora/.sub */
.vista.acento, .hora.acento, .sub.acento { color: var(--acento-texto); }
