/* Quando o chat está aberto: evita scroll lateral no site (mobile e desktop) */
html.chatbook-panel-open,
html.chatbook-panel-open body,
body.chatbook-panel-open {
  overflow-x: hidden !important;
  max-width: 100vw !important;
}
html.chatbook-panel-open body {
  position: relative;
  width: 100%;
}

/* Chatbook widget - tema escuro, Cine Araújo Ajuda, canto inferior direito */
.chatbook-root {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.4;
  padding-bottom: env(safe-area-inset-bottom, 0);
  max-width: 100vw;
  overflow: visible;
}

/* Botão do chat: círculo perfeito, mascote com metade para fora no topo */
.chatbook-toggle {
  position: relative;
  width: 64px;
  height: 64px;
  min-width: 64px;
  min-height: 64px;
  background: transparent;
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 0 0 6px;
  transition: transform 0.2s;
  overflow: visible;
  border-radius: 50%;
}
.chatbook-toggle::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #00ADEF;
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.chatbook-toggle:hover {
  transform: scale(1.05);
}
.chatbook-toggle:hover::before {
  background: #00ADEF;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
}
.chatbook-toggle:focus {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}
.chatbook-toggle-mascote {
  width: 56px;
  height: 56px;
  object-fit: contain;
  margin-top: -28px;
  animation: chatbook-arara-pulo 1.2s ease-in-out 2;
}
@keyframes chatbook-arara-pulo {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-12px); }
}
.chatbook-toggle-label {
  font-size: 10px;
  font-weight: 600;
  color: #fff;
  margin-top: 0;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

/* Ocultar toggle quando limite de tokens atingido */
.chatbook-root.chatbook-limit-reached .chatbook-toggle {
  display: none !important;
}

/* Painel: tema escuro - largura limitada ao viewport para evitar overflow horizontal */
.chatbook-panel {
  position: absolute;
  bottom: 100px;
  right: 0;
  width: 360px;
  max-width: min(360px, calc(100vw - 24px));
  height: 500px;
  max-height: calc(100vh - 120px);
  background: #1a1a2e;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  overflow: hidden;
  padding: 0;
  box-sizing: border-box;
  overflow-x: hidden;
}
.chatbook-panel[hidden] {
  display: none !important;
}

/* Cabeçalho fixo no topo do pop-up (sobrescreve estilos globais de header) */
.chatbook-panel .chatbook-header,
.chatbook-header {
  order: 1;
  flex-shrink: 0;
  flex: 0 0 auto;
  margin: 0 !important;
  margin-top: 0 !important;
  background: #16213e;
  color: #fff;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-radius: 12px 12px 0 0;
}
.chatbook-header-avatar {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chatbook-header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.chatbook-header-info {
  flex: 1;
  min-width: 0;
}
.chatbook-header-name {
  font-weight: 600;
  display: block;
  font-size: 15px;
}
.chatbook-header-status {
  font-size: 12px;
  opacity: 0.85;
}
.chatbook-header-btn,
.chatbook-close {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  opacity: 0.9;
}
.chatbook-delete {
  margin-right: 4px;
}
.chatbook-header-btn:hover,
.chatbook-close:hover {
  background: rgba(255, 255, 255, 0.15);
  opacity: 1;
}

/* Área de mensagens - fundo escuro; evita overflow horizontal */
.chatbook-messages {
  order: 2;
  flex: 1;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #1a1a2e;
  max-width: 100%;
}
.chatbook-welcome {
  background: #16213e;
  padding: 12px 14px;
  border-radius: 10px;
  max-width: 90%;
  align-self: flex-start;
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.chatbook-welcome p {
  margin: 0 0 12px 0;
  color: #e2e8f0;
  font-size: 14px;
}
.chatbook-suggested-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chatbook-suggested-btn {
  background: #2563eb;
  color: #fff;
  border: none;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 0.2s;
}
.chatbook-suggested-btn:hover {
  background: #1d4ed8;
}

/* Bolhas de mensagem – sem max-height/overflow para não cortar conteúdo; rolagem fica em .chatbook-messages */
.chatbook-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 10px;
  word-wrap: break-word;
  overflow: visible;
}
.chatbook-msg-user {
  align-self: flex-end;
  background: #2563eb;
  color: #fff;
}
.chatbook-msg-assistant {
  align-self: flex-start;
  background: #16213e;
  color: #e2e8f0;
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.chatbook-msg-time {
  font-size: 11px;
  color: #94a3b8;
  margin-top: 4px;
}
.chatbook-msg-content {
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  max-width: 100%;
  overflow: visible;
}
.chatbook-msg-content strong {
  font-weight: 700;
  color: inherit;
}
.chatbook-msg-content a {
  color: #60a5fa;
  text-decoration: underline;
}

/* Botões de comprar ingresso – apenas horário, altura do texto */
.chatbook-msg-content a.chatbook-msg-link-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 6px 14px 6px 14px;
  padding: 2px 14px;
  background: #00ADEF;
  color: #fff !important;
  text-decoration: none !important;
  border-radius: 8px;
  font-weight: 700;
  font-size: 18px;
  line-height: 1.25;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 173, 239, 0.3);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
  border: none;
  cursor: pointer;
}
.chatbook-msg-content a.chatbook-msg-link-btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: -6px;
  transform: translateY(-50%);
  width: 12px;
  height: 12px;
  background: #16213E;
  border-radius: 50%;
  border: 2px solid #16213E;
}
.chatbook-msg-content a.chatbook-msg-link-btn::after {
  content: '';
  position: absolute;
  top: 50%;
  right: -6px;
  transform: translateY(-50%);
  width: 12px;
  height: 12px;
  background: #16213E;
  border-radius: 50%;
  border: 2px solid #16213E;
}
.chatbook-msg-content a.chatbook-msg-link-btn:hover {
  color: #fff !important;
  background: #0099d6;
  box-shadow: 0 4px 12px rgba(0, 173, 239, 0.45);
}
.chatbook-msg-content a.chatbook-msg-link-btn:focus {
  outline: 2px solid rgba(255, 255, 255, 0.5);
  outline-offset: 2px;
}

/* Digitando... - contenção para não gerar scroll lateral durante a animação */
.chatbook-typing {
  align-self: flex-start;
  background: #16213e;
  padding: 12px 16px;
  border-radius: 10px;
  display: flex;
  gap: 4px;
  align-items: center;
  border: 1px solid rgba(255, 255, 255, 0.08);
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  flex-shrink: 0;
}
.chatbook-typing span {
  width: 8px;
  height: 8px;
  min-width: 8px;
  border-radius: 50%;
  background: #94a3b8;
  animation: chatbook-bounce 1.4s ease-in-out infinite both;
}
.chatbook-typing span:nth-child(1) { animation-delay: 0s; }
.chatbook-typing span:nth-child(2) { animation-delay: 0.2s; }
.chatbook-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes chatbook-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* Enquanto "digitando..." está visível: reforça contenção do painel para evitar scroll lateral */
.chatbook-panel.chatbook-panel--typing {
  overflow-x: hidden !important;
  max-width: 100vw !important;
}
.chatbook-panel.chatbook-panel--typing .chatbook-messages {
  overflow-x: hidden !important;
  min-width: 0 !important;
}

/* Formulário - barra inferior escura; padding-bottom para safe area (iOS) e barra do navegador */
.chatbook-form {
  order: 3;
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  background: #16213e;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.chatbook-input {
  flex: 1;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 24px;
  padding: 10px 16px;
  font-size: 14px;
  background: #1a1a2e;
  color: #e2e8f0;
}
.chatbook-input::placeholder {
  color: #64748b;
}
.chatbook-input:focus {
  outline: 2px solid #2563eb;
  outline-offset: 0;
}
.chatbook-send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #2563eb;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.chatbook-send:hover {
  background: #1d4ed8;
}
.chatbook-send:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Variáveis para ajuste quando o teclado abre (Visual Viewport) no mobile */
.chatbook-panel {
  --chatbook-vp-top: auto;
  --chatbook-vp-height: auto;
}

/* Quando o teclado abre no mobile: painel usa Visual Viewport; largura limitada a 100vw para não causar scroll */
.chatbook-panel.chatbook-panel--keyboard-open {
  position: fixed !important;
  top: var(--chatbook-vp-top) !important;
  left: var(--chatbook-vp-left, 0) !important;
  width: var(--chatbook-vp-width, 100%) !important;
  max-width: 100vw !important;
  height: var(--chatbook-vp-height) !important;
  max-height: none !important;
  bottom: auto !important;
  right: auto !important;
  border-radius: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
}
.chatbook-panel.chatbook-panel--keyboard-open .chatbook-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Mobile: pop-up em tela cheia; largura fixa em 100vw para nunca ultrapassar a tela */
@media (max-width: 768px) {
  html.chatbook-panel-open,
  body.chatbook-panel-open {
    overflow: hidden !important;
    height: 100%;
  }
  .chatbook-panel:not(.chatbook-panel--keyboard-open) {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    min-width: 0 !important;
    min-height: 100vh;
    min-height: 100dvh;
    height: 100% !important;
    height: 100dvh !important;
    max-height: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    margin: 0 !important;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    box-sizing: border-box !important;
    overflow-x: hidden !important;
  }
  /* Evita que flex filhos forcem largura maior que a tela */
  .chatbook-panel .chatbook-header,
  .chatbook-panel .chatbook-messages,
  .chatbook-panel .chatbook-form {
    min-width: 0;
  }
  /* Botão enviar e campo sempre visíveis: mais padding inferior no mobile */
  .chatbook-form {
    padding: 12px 12px calc(24px + env(safe-area-inset-bottom, 0px)) 12px !important;
    flex-shrink: 0;
  }
  .chatbook-input {
    font-size: 16px;
  }
  .chatbook-panel.chatbook-panel--keyboard-open .chatbook-form {
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px)) !important;
  }
  /* Mobile: mesmo estilo – círculo, metade do mascote para fora no topo */
  .chatbook-root {
    bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    right: 16px;
  }
}

@media (max-width: 480px) {
  .chatbook-toggle {
    width: 56px;
    height: 56px;
    min-width: 56px;
    min-height: 56px;
    padding: 0 0 4px;
  }
  .chatbook-toggle-mascote {
    width: 48px;
    height: 48px;
    margin-top: -24px;
  }
  .chatbook-toggle-label {
    font-size: 9px;
  }
}
