:root {
  --brand: #25d366;
  --brand-dark: #1da851;
  --bg: #0b141a;
  --panel: #111b21;
  --bubble-in: #202c33;
  --bubble-out: #005c4b;
  --text: #e9edef;
  --text-dim: #8696a0;
  --border: #222d34;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.chat-shell {
  /* z-index:0 (no solo position:relative) para que forme su propio
     contexto de apilamiento — si no, el ::before con z-index:-1 se escapa
     al contexto raíz y termina detrás de TODO el chat, no solo del fondo. */
  position: relative;
  z-index: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
}

/* La trama de fondo (branding.php la rellena) vive en su propia capa, para
   que un mismo control de opacidad sirva para puntos, cuadrícula o una
   imagen subida — sin decolorar los mensajes que van encima. */
.chat-shell::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.profile-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  display: flex;
  flex-shrink: 0;
}
.profile-btn:hover { background: rgba(255,255,255,.06); color: var(--text); }

.chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--brand);
  color: #06231a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
}

.chat-header-name { font-size: 15px; font-weight: 600; }
.chat-header-status { font-size: 12px; color: var(--text-dim); }
.chat-header-status.typing { color: var(--brand); }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.bubble {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0,0,0,.2);
  animation: pop .15s ease-out;
}

@keyframes pop {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.bubble.ai {
  align-self: flex-start;
  background: var(--bubble-in);
  border-bottom-left-radius: 4px;
}

.bubble.visitor {
  align-self: flex-end;
  background: var(--bubble-out);
  border-bottom-right-radius: 4px;
}

.bubble .meta {
  display: block;
  margin-top: 4px;
  font-size: 10.5px;
  color: var(--text-dim);
  text-align: right;
}

.bubble.error {
  align-self: center;
  background: transparent;
  color: #e07a7a;
  font-size: 12.5px;
  box-shadow: none;
}

.typing-indicator {
  align-self: flex-start;
  display: flex;
  gap: 3px;
  padding: 10px 14px;
  background: var(--bubble-in);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
}
.typing-indicator span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-dim);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: .15s; }
.typing-indicator span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  resize: none;
  max-height: 120px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 14.5px;
  font-family: inherit;
  outline: none;
}
.chat-input:focus { border-color: var(--brand); }
/* Sin barra ni flechitas grises a un lado: el cuadro crece solo y sigue desplazándose con rueda o dedo. */
.chat-input { scrollbar-width: none; -ms-overflow-style: none; }
.chat-input::-webkit-scrollbar { display: none; }

.chat-send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--brand);
  color: #06231a;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform .1s ease, opacity .15s ease;
}
.chat-send:active { transform: scale(.92); }
.chat-send:disabled { opacity: .5; cursor: default; }

.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Página de acceso (registro / login) ────────────────────────── */
.access-shell {
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow-y: auto;
}
.access-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 22px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.access-brand { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.access-brand h1 { font-size: 20px; margin: 4px 0 0; }
.access-brand p { font-size: 13px; color: var(--text-dim); margin: 0; }
.access-avatar { width: 64px; height: 64px; font-size: 26px; }

/* ── Perfil del visitante (editar) ───────────────────────────────── */
.profile-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.profile-modal[hidden] { display: none; }

.profile-card {
  background: var(--panel);
  border-radius: 14px;
  width: 340px;
  max-width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.profile-modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.profile-modal-close {
  background: none; border: none; color: var(--text-dim);
  font-size: 22px; cursor: pointer; line-height: 1;
}

.profile-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.profile-body[hidden] { display: none; }

.profile-tabs { display: flex; gap: 6px; background: var(--bg); border-radius: 8px; padding: 3px; }
.profile-tab {
  flex: 1;
  background: none; border: none;
  color: var(--text-dim);
  padding: 8px;
  border-radius: 6px;
  font-size: 12.5px;
  cursor: pointer;
}
.profile-tab.active { background: var(--panel); color: var(--text); }

.profile-form { display: flex; flex-direction: column; gap: 10px; }
.profile-form[hidden] { display: none; }
.profile-form label { font-size: 12.5px; color: var(--text-dim); display: flex; flex-direction: column; gap: 5px; }
.profile-form input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  color: var(--text);
  font-size: 14px;
}
.profile-form input:focus { outline: none; border-color: var(--brand); }
.profile-form button[type="submit"] {
  background: var(--brand);
  color: #06231a;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.profile-section { border-top: 1px solid var(--border); padding-top: 14px; }
.profile-section h3 { font-size: 13px; margin: 0; color: var(--text); font-weight: 600; }

.profile-msg { font-size: 12px; color: #e07a7a; min-height: 14px; margin: 0; }
.profile-msg.ok { color: var(--brand); }

.profile-avatar-row { display: flex; align-items: center; gap: 14px; }
.profile-avatar-preview {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--bubble-in);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 18px; flex-shrink: 0;
  object-fit: cover;
}
.profile-file-btn {
  background: rgba(255,255,255,.06);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}
.profile-file-btn:hover { background: rgba(255,255,255,.1); }

.profile-email { font-size: 12px; color: var(--text-dim); margin: 0; }
.profile-logout {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px;
  font-size: 13px;
  color: #e07a7a;
  cursor: pointer;
}
.profile-logout:hover { background: rgba(224,122,122,.08); }

/* ── Adjuntos: botones, grabación, y contenido de mensajes multimedia ── */
.chat-icon-btn {
  width: 40px; height: 40px;
  border: none; background: none;
  color: var(--text-dim);
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
}
.chat-icon-btn:hover { background: rgba(255,255,255,.06); color: var(--text); }
.chat-icon-btn[hidden] { display: none; }
.chat-icon-btn.recording { color: #fff; background: #e5484d; }

.recording-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 13px;
  flex-shrink: 0;
}
.recording-bar[hidden] { display: none; }
.rec-dot { width: 9px; height: 9px; border-radius: 50%; background: #e5484d; animation: recpulse 1s infinite; }
@keyframes recpulse { 50% { opacity: .25; } }
.rec-label { color: var(--text-dim); flex: 1; }
.rec-cancel { background: none; border: none; color: #e07a7a; cursor: pointer; font-size: 13px; }
.rec-cancel[hidden] { display: none; }

/* Imagen: la burbuja se ajusta al tamaño real de la imagen (el tope va en
   unidades absolutas; con porcentajes el ancho de la burbuja se calculaba
   con la imagen a tamaño original y quedaba espacio sobrante). La hora se
   superpone abajo a la derecha, como en WhatsApp. */
.bubble:has(.msg-image-wrap) { position: relative; padding: 4px; }
.bubble .msg-img-link { display: block; line-height: 0; cursor: zoom-in; }
.bubble .msg-img {
  display: block;
  width: auto;
  height: auto;
  min-width: 120px;
  min-height: 80px;
  max-width: min(280px, 64vw);
  max-height: 340px;
  border-radius: 10px;
  background: rgba(255,255,255,.06);
}
.bubble:has(.msg-image-wrap) .meta {
  position: absolute;
  right: 10px;
  bottom: 9px;
  margin: 0;
  padding: 1px 7px;
  border-radius: 9px;
  background: rgba(0,0,0,.5);
  color: #fff;
}

/* Nota de voz: reproductor propio */
.voice { display: flex; align-items: center; gap: 10px; width: min(250px, 64vw); padding: 2px 0; }
.voice audio { display: none; }
.voice-play {
  width: 38px; height: 38px; flex-shrink: 0;
  border: none; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: transform .1s ease;
}
.voice-play:active { transform: scale(.92); }
.bubble.visitor .voice-play { background: #fff; color: var(--bubble-out); }
.bubble.ai .voice-play { background: var(--brand); color: #06231a; }

.voice-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.voice-wave { display: flex; align-items: center; gap: 2px; height: 26px; cursor: pointer; }
.voice-wave i {
  flex: 1;
  min-width: 2px;
  border-radius: 2px;
  background: rgba(255,255,255,.32);
  transition: background .15s ease;
}
.bubble.visitor .voice-wave i.on { background: #fff; }
.bubble.ai .voice-wave i.on { background: var(--brand); }

.voice-row { display: flex; align-items: center; justify-content: space-between; min-height: 16px; }
.voice-time { font-size: 11.5px; color: rgba(255,255,255,.75); font-variant-numeric: tabular-nums; }
.voice-speed {
  display: none;
  background: rgba(255,255,255,.16);
  border: none;
  border-radius: 9px;
  padding: 1px 7px;
  font-size: 10.5px;
  color: #fff;
  cursor: pointer;
}
.voice.playing .voice-speed { display: block; }

/* Visor de imágenes */
.lightbox {
  position: fixed; inset: 0;
  z-index: 300;
  background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
}
.lightbox[hidden] { display: none; }
.lightbox-bar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: flex-end; gap: 16px;
  padding: 12px 16px;
  z-index: 2;
  background: linear-gradient(rgba(0,0,0,.6), transparent);
}
.lightbox-bar a { color: #fff; font-size: 13px; text-decoration: none; opacity: .85; }
.lightbox-bar a:hover { opacity: 1; text-decoration: underline; }
.lightbox-close {
  background: none; border: none; color: #fff;
  font-size: 30px; line-height: 1; cursor: pointer; padding: 0 4px;
}
.lightbox-stage {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  overflow: auto;
  padding: 56px 16px 16px;
}
.lightbox-img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  border-radius: 6px;
  cursor: zoom-in;
}
.lightbox.zoomed .lightbox-stage { align-items: flex-start; justify-content: flex-start; }
.lightbox.zoomed .lightbox-img { max-width: none; max-height: none; cursor: zoom-out; margin: auto; }
.lightbox-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  z-index: 2;
  background: rgba(255,255,255,.14);
  border: none; color: #fff;
  width: 44px; height: 44px; border-radius: 50%;
  font-size: 30px; line-height: 1; cursor: pointer;
}
.lightbox-nav:hover { background: rgba(255,255,255,.26); }
.lightbox-nav[hidden] { display: none; }
.lightbox-nav.prev { left: 14px; }
.lightbox-nav.next { right: 14px; }

.bubble .msg-yt {
  width: min(300px, 62vw);
  aspect-ratio: 16 / 9;
  margin-top: 8px;
  border-radius: 10px;
  overflow: hidden;
  background: #000;
}
.bubble .msg-yt iframe { width: 100%; height: 100%; border: 0; display: block; }
.bubble a { color: #7ec8ff; word-break: break-all; }

/* ── Burbuja de video (círculo con anillo de progreso) ──────────── */
.bubble:has(.vnote) { background: transparent; box-shadow: none; padding: 0; position: relative; }
.vnote {
  position: relative;
  width: min(220px, 58vw);
  aspect-ratio: 1;
  margin: 4px;
  border-radius: 50%;
  cursor: pointer;
}
.vnote video {
  display: block;
  width: 100%; height: 100%;
  object-fit: cover;
  border-radius: 50%;
  background: #0a0f12;
}
.vnote-ring, .vrec-circle svg {
  position: absolute;
  inset: -5px;
  width: calc(100% + 10px);
  height: calc(100% + 10px);
  transform: rotate(-90deg);
  pointer-events: none;
}
.vnote-ring circle, .vrec-circle svg circle { fill: none; stroke-width: 3; }
.vnote-ring .bg { stroke: rgba(255,255,255,.22); }
.vnote-ring .fg { stroke: var(--brand); stroke-linecap: round; transition: stroke-dashoffset .25s linear; }
.vnote-time {
  position: absolute; left: 10px; bottom: 10px;
  background: rgba(0,0,0,.55); color: #fff;
  font-size: 11.5px; font-variant-numeric: tabular-nums;
  padding: 1px 8px; border-radius: 10px;
}
.vnote-play {
  position: absolute; inset: 0; margin: auto;
  width: 56px; height: 56px; border-radius: 50%;
  background: rgba(0,0,0,.45); color: #fff;
  display: flex; align-items: center; justify-content: center;
  transition: opacity .15s ease;
}
.vnote.playing .vnote-play { opacity: 0; }
.bubble:has(.vnote) .meta {
  position: absolute; right: 8px; bottom: 8px; margin: 0;
  padding: 1px 7px; border-radius: 9px;
  background: rgba(0,0,0,.5); color: #fff;
}

/* Pantalla de grabación de video */
.vrec {
  position: fixed; inset: 0; z-index: 250;
  background: rgba(0,0,0,.84);
  display: flex; align-items: center; justify-content: center;
}
.vrec[hidden] { display: none; }
.vrec-card { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.vrec-circle { position: relative; width: min(300px, 78vw); aspect-ratio: 1; }
.vrec-circle video {
  width: 100%; height: 100%;
  object-fit: cover;
  border-radius: 50%;
  transform: scaleX(-1);
  background: #111;
}
.vrec-circle svg .bg { stroke: rgba(255,255,255,.18); }
.vrec-circle svg .fg { stroke: #e5484d; stroke-linecap: round; }
.vrec-info { display: flex; align-items: center; gap: 8px; color: #fff; font-variant-numeric: tabular-nums; }
.vrec-actions { display: flex; gap: 12px; }
.vrec-actions button { border: none; border-radius: 22px; padding: 11px 26px; font-size: 14px; cursor: pointer; }
.vrec-cancel { background: rgba(255,255,255,.14); color: #fff; }
.vrec-send { background: var(--brand); color: #06231a; font-weight: 600; }

/* Con tres iconos de adjuntos el compositor necesita menos aire */
.chat-composer { gap: 6px; }

@media (max-width: 420px) {
  .chat-input { min-width: 0; padding: 10px 12px; font-size: 14px; }
}

/* Texto de una nota de voz (transcripción / lo que dijo la IA) y descripción de imágenes */
.msg-transcript { margin-top: 6px; max-width: min(250px, 64vw); }
.msg-transcript summary {
  cursor: pointer; list-style: none;
  font-size: 11.5px; color: rgba(255,255,255,.65);
}
.msg-transcript summary::-webkit-details-marker { display: none; }
.msg-transcript summary::before { content: '▸ '; }
.msg-transcript[open] summary::before { content: '▾ '; }
.msg-transcript div { margin-top: 4px; font-size: 13px; line-height: 1.4; white-space: normal; opacity: .92; }
.msg-caption { padding: 6px 6px 0; max-width: min(280px, 64vw); font-size: 12px; line-height: 1.4; color: rgba(255,255,255,.7); white-space: normal; }
.bubble:has(.msg-caption) .meta, .bubble:has(.msg-receipt) .meta {
  position: static; margin: 2px 0 0; padding: 0 6px 2px;
  background: none; color: var(--text-dim);
}

/* ── Comprobantes de pago (vista del negocio) ─────────────────── */
.msg-receipt {
  margin: 8px 2px 2px; padding: 10px 12px; max-width: min(300px, 70vw);
  background: rgba(0,0,0,.22); border: 1px solid rgba(255,255,255,.12); border-left: 3px solid #f59e0b;
  border-radius: 10px; white-space: normal; font-size: 12.5px; line-height: 1.4;
}
.msg-receipt.verified { border-left-color: #2fd6a0; }
.msg-receipt.rejected { border-left-color: #ff5b6e; }
.rc-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-weight: 600; margin-bottom: 6px; }
.rc-badge { font-size: 10.5px; padding: 2px 8px; border-radius: 999px; background: rgba(245,158,11,.22); color: #fbbf24; white-space: nowrap; }
.msg-receipt.verified .rc-badge { background: rgba(47,214,160,.2); color: #2fd6a0; }
.msg-receipt.rejected .rc-badge { background: rgba(255,91,110,.2); color: #ff8f9d; }
.msg-receipt dl { margin: 0; display: grid; gap: 3px; }
.msg-receipt dl div { display: flex; gap: 8px; }
.msg-receipt dt { color: rgba(255,255,255,.55); min-width: 82px; }
.msg-receipt dd { margin: 0; font-weight: 500; word-break: break-word; }
.rc-concerns { margin: 6px 0 0; color: #fbbf24; }
.rc-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.rc-btn { border: none; border-radius: 8px; padding: 7px 10px; font-size: 12px; font-weight: 600; cursor: pointer; color: #06231a; background: var(--brand); }
.rc-btn.no { background: rgba(255,255,255,.12); color: var(--text); }
.rc-btn:disabled { opacity: .5; cursor: default; }
.rc-silent { font-size: 11px; color: rgba(255,255,255,.55); display: flex; align-items: center; gap: 4px; }
.rc-note { margin: 6px 0 0; font-size: 11px; color: rgba(255,255,255,.5); }
.pp-receipt {
  font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: rgba(245,158,11,.2); color: #fbbf24; white-space: nowrap;
}

.crm-badge-ua { font-size: 13px; flex-shrink: 0; margin-right: 4px; }

.access-consent { flex-direction: row !important; align-items: flex-start; gap: 8px !important; font-size: 12px !important; line-height: 1.4; }
.access-consent input { margin-top: 2px; width: auto; flex-shrink: 0; }
.access-consent a { color: var(--brand); }
