:root {
  --bg: #e5ddd5;
  --header-bg: #075e54;
  --bubble-ia: #dcf8c6;
  --bubble-user: #25d366;
  --bubble-system: #f1f0ec;
  --text-dark: #1f2c33;
  --text-light: #fff;
  --text-muted: #667781;
  /* Cinza mais escuro para textos pequenos (o #667781 dava 4.0:1 em 13px). */
  --text-muted-forte: #4a5860;
  --radius: 12px;
  --foco: #044f45;

  /* Recortes do aparelho (notch, barra de gestos). Com viewport-fit=cover o
     app desenha embaixo deles; e' aqui que devolvemos o espaco. */
  --sa-topo: env(safe-area-inset-top, 0px);
  --sa-base: env(safe-area-inset-bottom, 0px);
  --sa-esq: env(safe-area-inset-left, 0px);
  --sa-dir: env(safe-area-inset-right, 0px);
}

/* Com o teclado aberto a barra de gestos fica coberta por ele: devolver o
   espaco de novo criaria uma faixa morta em cima do teclado. */
html.teclado-aberto {
  --sa-base: 0px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* O atributo hidden precisa vencer os display: block/flex daqui de baixo. */
[hidden] {
  display: none !important;
}

html, body {
  height: 100%;
}

html {
  /* Impede o Safari de aumentar a fonte sozinho ao girar o aparelho. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
  background: linear-gradient(160deg, #128c7e 0%, #075e54 45%, #e5ddd5 45.1%, #dcd3c9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  overscroll-behavior: none;
  /* Mata o zoom de toque duplo (e o atraso de 300 ms) sem bloquear a pinca:
     o usuario continua podendo ampliar a tela se precisar. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.06);
}

.chat-container {
  width: 100%;
  max-width: 480px;
  /* Ordem importa: vh para navegador velho, dvh para os atuais. Quando o JS
     consegue medir o visualViewport (teclado aberto), ele manda. */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: #efeae2;
  background-image: radial-gradient(circle at 1px 1px, rgba(0, 0, 0, 0.045) 1px, transparent 0);
  background-size: 18px 18px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  position: relative;
}

/* O body tambem precisa encolher para a area visivel. Sem isso, no iOS (que NAO
   encolhe o layout quando o teclado abre) o body continua com a tela inteira e
   o align-items: center joga o app para o meio: metade dele, inclusive a barra
   de digitacao, fica atras do teclado. */
html.tem-vv body {
  height: var(--altura-app);
}

html.tem-vv .chat-container {
  height: var(--altura-app);
}

@media (min-width: 600px) {
  .chat-container {
    height: 92vh;
    max-height: 800px;
    border-radius: 16px;
  }
  html.tem-vv .chat-container {
    height: min(92vh, var(--altura-app));
    max-height: 800px;
  }
}

/* Foco sempre visivel — sem isso quem navega por teclado fica perdido. */
:focus-visible {
  outline: 3px solid var(--foco);
  outline-offset: 2px;
}

.chat-header :focus-visible {
  outline-color: #fff;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--sa-topo)) calc(16px + var(--sa-dir)) 10px calc(16px + var(--sa-esq));
  background: var(--header-bg);
  color: var(--text-light);
  flex-shrink: 0;
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #25d366;
  color: #fff;
  font-size: 24px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.header-info {
  min-width: 0;
}

.header-info h1 {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.subtitle {
  font-size: 12px;
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  font-size: 12px;
  color: #a7f3c5;
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #25d366;
  animation: pulse 2s infinite;
  flex-shrink: 0;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(37, 211, 102, 0.5); }
  50% { box-shadow: 0 0 0 5px rgba(37, 211, 102, 0); }
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  /* Uma lista de 10 boletos nunca pode virar rolagem lateral. */
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px calc(12px + var(--sa-dir)) 16px calc(12px + var(--sa-esq));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  min-width: 0;
  max-width: 100%;
  animation: msg-in 0.25s ease forwards;
}

.message-row.user {
  justify-content: flex-end;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .message-row { animation: none; }
  .status-dot, .typing-dots span, .ponto-rec { animation: none; }
}

.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #25d366;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.bubble {
  max-width: 78%;
  /* min-width:0 e' o que impede um codigo tipo 2904+025+000 (ou 500 caracteres
     sem espaco) de esticar a bolha alem da tela dentro do flex. */
  min-width: 0;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.4;
  /* anywhere quebra ate palavra unica gigante; break-word cobre navegador velho. */
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: pre-wrap;
}

.bubble.ia {
  background: var(--bubble-ia);
  color: var(--text-dark);
  border-top-left-radius: 4px;
}

.bubble.user {
  background: var(--bubble-user);
  /* Branco sobre #25d366 dava 1.8:1 — ilegivel no sol. Texto escuro sobre o
     mesmo verde da 8.5:1 e mantem a cor da marca. */
  color: #06331c;
  border-top-right-radius: 4px;
}

.bubble.system {
  align-self: center;
  background: var(--bubble-system);
  color: var(--text-muted-forte);
  font-size: 13px;
  text-align: center;
  border-radius: 10px;
  max-width: 88%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 2px;
  vertical-align: middle;
}

.typing-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #6c7a80;
  animation: bounce 1.2s infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* Rotulo do que a Teresa esta fazendo + cronometro. Sem isso, uma leitura de
   foto de 146 s parece a tela travada. */
.typing-rotulo {
  display: block;
  font-size: 12.5px;
  color: var(--text-muted-forte);
  margin-top: 2px;
}

/* Botao "descer": flutua logo acima da barra de digitacao quando a pessoa
   rolou para cima, para nenhuma resposta nova passar despercebida. */
.ir-fim {
  position: absolute;
  right: 12px;
  bottom: 100%;
  margin-bottom: 8px;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: var(--header-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.28);
  animation: sobe 0.16s ease;
}

.ir-fim:hover {
  background: #eef2f0;
}

.input-area {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px calc(10px + var(--sa-dir)) calc(8px + var(--sa-base)) calc(10px + var(--sa-esq));
  background: #f0f0f0;
  border-top: 1px solid #dcdcdc;
  flex-shrink: 0;
}

.linha-digitacao {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  min-width: 0;
}

#message-input {
  flex: 1;
  /* Sem isso o campo se recusa a encolher e empurra os botoes para fora. */
  min-width: 0;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  background: #fff;
  border-radius: 22px;
  padding: 11px 14px;
  /* 16px e' o minimo: abaixo disso o Safari do iPhone da zoom sozinho ao focar
     e o usuario fica com a tela ampliada e torta. */
  font-size: 16px;
  font-family: inherit;
  line-height: 1.35;
  color: var(--text-dark);
  min-height: 44px;
  max-height: 120px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

#message-input::placeholder {
  color: #6b797f;
}

#send-button {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: #25d366;
  color: #063d1f;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
  transition: transform 0.1s ease, background 0.2s ease;
}

#send-button:hover {
  background: #1ebe5b;
}

#send-button:active {
  transform: scale(0.92);
}

#send-button:disabled {
  background: #b9b9b9;
  color: #4a4a4a;
  cursor: not-allowed;
  transform: none;
}

/* Tela de senha (uma senha compartilhada, sem usuario). */
.tela-senha {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100vh;
  height: 100dvh;
  z-index: 50;
  display: flex;
  /* flex-start + margin:auto no filho centraliza quando cabe e NAO corta o topo
     quando o teclado come metade da tela. */
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(160deg, #128c7e 0%, #075e54 100%);
  padding: calc(16px + var(--sa-topo)) calc(16px + var(--sa-dir))
           calc(16px + var(--sa-base)) calc(16px + var(--sa-esq));
}

html.tem-vv .tela-senha {
  height: var(--altura-app);
}

.tela-senha[hidden] {
  display: none;
}

.caixa-senha {
  width: 100%;
  max-width: 340px;
  margin: auto;
  background: #fff;
  border-radius: 16px;
  padding: 24px 20px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.caixa-senha .avatar {
  width: 56px;
  height: 56px;
  font-size: 28px;
}

.caixa-senha h2 {
  font-size: 18px;
  color: var(--text-dark);
}

.caixa-senha-sub {
  font-size: 13px;
  color: var(--text-muted-forte);
  line-height: 1.4;
}

#senha-input {
  width: 100%;
  border: 1px solid #b8c0c4;
  border-radius: 22px;
  padding: 12px 16px;
  /* 16px pelo mesmo motivo do campo de mensagem: evita o zoom do iOS. */
  font-size: 16px;
  font-family: inherit;
  color: var(--text-dark);
  outline: none;
  text-align: center;
  min-height: 46px;
}

#senha-input:focus {
  border-color: #0a7a54;
  box-shadow: 0 0 0 3px rgba(10, 122, 84, 0.18);
}

#senha-button {
  width: 100%;
  border: none;
  border-radius: 22px;
  padding: 12px;
  min-height: 46px;
  /* #128c7e com texto branco dava 4.14:1, abaixo do minimo de 4.5:1. */
  background: #0f7a6e;
  color: #fff;
  font-size: 16px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.2s ease;
}

#senha-button:hover {
  background: #0b6b60;
}

#senha-button:disabled {
  background: #8d9a9d;
  cursor: not-allowed;
}

.senha-erro {
  font-size: 13.5px;
  color: #a5281b;
  font-weight: 600;
}

.senha-erro[hidden] {
  display: none;
}

/* Botoes auxiliares: anexar foto e gravar audio.
   44x44 e' o minimo recomendado para o dedo (antes eram 40). */
.aux-button {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: var(--header-bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  transition: transform 0.1s ease, background 0.2s ease, color 0.2s ease;
}

.aux-button:hover {
  background: #e8e8e8;
}

.aux-button:active {
  transform: scale(0.92);
}

.aux-button[aria-expanded="true"] {
  background: var(--header-bg);
  color: #fff;
}

.aux-button:disabled {
  color: #9aa2a5;
  cursor: not-allowed;
  transform: none;
}

/* Estado de gravacao em andamento. */
.aux-button.gravando {
  background: #c0271a;
  color: #fff;
  animation: rec-pulse 1.2s infinite;
}

@keyframes rec-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 39, 26, 0.55); }
  50% { box-shadow: 0 0 0 7px rgba(192, 39, 26, 0); }
}

/* O <select> segue no DOM (e' o valor que vai no POST), so nao ocupa a barra. */
#tipo-imagem {
  display: none;
}

/* --- Folha de escolha do tipo da foto -------------------------------------- */
.folha-tipo[hidden],
.barra-status[hidden] {
  display: none;
}

.folha-tipo {
  background: #fff;
  border: 1px solid #dcdcdc;
  border-radius: 14px;
  padding: 10px;
  animation: sobe 0.16s ease;
}

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

.folha-titulo {
  font-size: 13px;
  color: var(--text-muted-forte);
  margin-bottom: 8px;
}

.folha-opcoes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  flex: 1 1 auto;
  min-width: 88px;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid #c7d0cc;
  border-radius: 22px;
  background: #f4faf6;
  color: #0c4f3f;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s ease;
}

.chip:hover { background: #e3f4e9; }
.chip:active { transform: scale(0.97); }

/* --- Barras de estado: gravando / enviando -------------------------------- */
.barra-status {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  background: #fff;
  border: 1px solid #dcdcdc;
  border-radius: 14px;
  padding: 8px 10px;
}

.status-texto {
  flex: 1 1 120px;
  min-width: 0;
  font-size: 13.5px;
  color: var(--text-muted-forte);
}

.status-texto strong {
  color: var(--text-dark);
  font-variant-numeric: tabular-nums;
}

.botao-texto {
  border: 1px solid #c7d0cc;
  background: #fff;
  color: #a5281b;
  border-radius: 20px;
  min-height: 40px;
  padding: 8px 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  flex-shrink: 0;
}

.botao-texto:hover { background: #fdeceb; }

.ponto-rec {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #c0271a;
  flex-shrink: 0;
  animation: rec-blink 1s infinite;
}

@keyframes rec-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

.progresso-trilha {
  flex: 1 0 100%;
  height: 6px;
  border-radius: 3px;
  background: #dfe4e2;
  overflow: hidden;
}

.progresso-preenchido {
  height: 100%;
  width: 0%;
  border-radius: 3px;
  background: #128c7e;
  transition: width 0.2s ease;
}

/* Espera sem progresso conhecido (servidor pensando): barra andando. */
.progresso-preenchido.indeterminado {
  width: 35%;
  animation: vai-e-vem 1.4s ease-in-out infinite;
}

@keyframes vai-e-vem {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(300%); }
}

/* --- Teclado aberto: o cabecalho encolhe para sobrar tela de conversa ----- */
html.teclado-aberto .chat-header {
  padding-top: 6px;
  padding-bottom: 6px;
}

html.teclado-aberto .subtitle {
  display: none;
}

html.teclado-aberto .avatar {
  width: 36px;
  height: 36px;
  font-size: 19px;
}

html.teclado-aberto #message-input {
  max-height: 84px;
}

/* Com o teclado aberto sobram ~278 px no iPhone SE. A caixa de senha inteira
   nao cabe, e sem isso o botao Entrar fica embaixo do teclado logo no primeiro
   contato com o sistema. */
html.teclado-aberto .caixa-senha {
  padding: 14px 16px;
  gap: 8px;
}

html.teclado-aberto .caixa-senha .avatar {
  width: 40px;
  height: 40px;
  font-size: 20px;
}

html.teclado-aberto .caixa-senha-sub {
  display: none;
}

/* Paisagem + teclado sobra ~140 px de tela util. Aqui so importa conseguir
   digitar a senha: o resto sai da frente. O teclado do celular envia o
   formulario pelo "Ir" (enterkeyhint=go), entao nao depende do botao. */
html.tela-baixa .tela-senha {
  padding: 8px;
}

html.tela-baixa .caixa-senha {
  padding: 10px 12px;
  gap: 8px;
}

html.tela-baixa .caixa-senha .avatar,
html.tela-baixa .caixa-senha h2,
html.tela-baixa .caixa-senha-sub {
  display: none;
}

/* --- Paisagem baixinha: cabe muito pouco, entao tudo encolhe -------------- */
@media (orientation: landscape) and (max-height: 480px) {
  .chat-header {
    padding-top: calc(5px + var(--sa-topo));
    padding-bottom: 5px;
    gap: 8px;
  }
  .avatar { width: 34px; height: 34px; font-size: 18px; }
  .header-info h1 { font-size: 14px; }
  .subtitle { display: none; }
  .status { font-size: 11px; }
  .messages { padding-top: 8px; padding-bottom: 8px; gap: 6px; }
  .input-area { padding-top: 6px; padding-bottom: calc(6px + var(--sa-base)); }
  #message-input { max-height: 72px; }
  .caixa-senha { padding: 16px; gap: 8px; }
  .caixa-senha .avatar { width: 40px; height: 40px; font-size: 20px; }
  .folha-tipo { padding: 8px; }
  .folha-titulo { margin-bottom: 6px; font-size: 12px; }
  /* Fonte menor faz os 5 tipos caberem numa linha so: 52 px de conversa
     de volta, sem abrir mao dos 44 px de altura do alvo de toque. */
  .chip { font-size: 13px; padding: 10px 8px; min-width: 66px; }
}

/* Telas bem estreitas (iPhone SE = 320 px): cada pixel do campo conta. */
@media (max-width: 359px) {
  .linha-digitacao { gap: 5px; }
  .input-area {
    padding-left: calc(8px + var(--sa-esq));
    padding-right: calc(8px + var(--sa-dir));
  }
  #message-input { padding: 11px 12px; }
  .chip { min-width: 74px; font-size: 14px; }
  .bubble { max-width: 82%; }
}
