/*
 * chat.css — Mensagens (DMs + salas), visual v3 "app nativo".
 * Depende de tokens.css (carregado antes). Cores próprias do chat ficam em
 * variáveis --chat-* com variantes claro/escuro (mesmo esquema do tokens.css).
 *
 * Integração com o shell: a view adiciona em <html> as classes
 *  .jpd-chat-route        enquanto /mensagens* ou /salas/* estiver montada
 *  .jpd-chat-thread-open  quando há uma conversa aberta (no celular ela cobre a tela)
 * e usa --app-top (altura ocupada no topo pela casca; 0 no desktop).
 *
 * Balões: os meus em amarelo (--primary) com texto escuro; os dos outros
 * claros com texto escuro nos dois temas (como na referência).
 */

/* ============================================================ VARIÁVEIS */

:root {
  --chat-bg: var(--bg);
  --chat-row-hover: var(--surface-2);
  --chat-row-active: var(--surface-2);
  --chat-divider: var(--border);
  --chat-seg-thumb: var(--surface);
  --chat-seg-shadow: 0 1px 2px rgba(16, 18, 24, .08), 0 1px 1px rgba(16, 18, 24, .04);
  --chat-mine: var(--primary);
  --chat-mine-text: var(--on-primary);
  --chat-theirs: #f1f2f5;
  --chat-theirs-text: #141414;
  --chat-meta: rgba(20, 20, 20, .5);
  --chat-tick: rgba(20, 20, 20, .45);
  --chat-tick-read: #141414;
  --chat-link: inherit;
  --chat-online: var(--accent);
  --chat-name-0: #4f6286;
  --chat-name-1: #74569a;
  --chat-name-2: #2f7560;
  --chat-name-3: #9a553a;
  --chat-name-4: #6f6230;
  --chat-name-5: #36699a;
  --chat-name-6: #93485f;
  --chat-name-7: #4c7536;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --chat-seg-thumb: var(--surface-3);
    --chat-seg-shadow: none;
    --chat-theirs: #eceef2;
  }
}

:root[data-theme="dark"] {
  --chat-seg-thumb: var(--surface-3);
  --chat-seg-shadow: none;
  --chat-theirs: #eceef2;
}

/* ============================================================== BÁSICO */

.chat,
.chat-menu {
  font-family: var(--font-body);
  color: var(--text);
}

.chat *,
.chat *::before,
.chat *::after {
  box-sizing: border-box;
}

.chat-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.chat-ic {
  width: 22px;
  height: 22px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chat-ic--send {
  fill: currentColor;
  stroke-width: 1.2;
}

.chat button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.chat a {
  -webkit-tap-highlight-color: transparent;
}

.chat button:focus-visible,
.chat a:focus-visible,
.chat input:focus-visible,
.chat-menu button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.chat-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--text-muted);
  animation: chat-spin .8s linear infinite;
}

@keyframes chat-spin {
  to { transform: rotate(360deg); }
}

.chat-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 44px;
  padding: 0 var(--sp-5);
  border: 0;
  border-radius: var(--r-full);
  font-weight: 600;
  font-size: var(--fs-base);
  transition: background var(--dur) var(--ease), transform var(--dur-fast) var(--ease);
}

.chat-btn .chat-ic { width: 18px; height: 18px; }
.chat-btn:active { transform: scale(.97); }

.chat-btn--primary {
  background: var(--primary);
  color: var(--on-primary);
}

.chat-btn--primary:hover { background: var(--primary-hover); }

.chat-btn--ghost {
  background: var(--surface-2);
  color: var(--text);
}

.chat-btn--ghost:hover { background: var(--surface-3); }

/* ============================================================== LAYOUT */

.chat {
  position: relative;
  display: block;
  width: 100%;
  min-height: 60vh;
  background: var(--chat-bg);
}

.chat-pane { display: none; }

.chat.has-thread .chat-list { display: none; }
.chat.has-thread .chat-pane { display: block; }

/* Celular: conversa em tela cheia, acompanhando o teclado (visualViewport). */
.chat.has-thread .chat-thread {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--chat-vvtop, 0px);
  height: 100vh;
  height: var(--chat-vvh, 100dvh);
  z-index: 100;
}

@media (max-width: 1023.98px) {
  html.jpd-chat-thread-open,
  html.jpd-chat-thread-open body {
    overflow: hidden;
    overscroll-behavior: none;
  }
}

@media (min-width: 1024px) {
  .chat {
    display: grid;
    grid-template-columns: minmax(320px, 380px) 1fr;
    height: calc(100vh - var(--app-top, 0px));
    height: calc(100dvh - var(--app-top, 0px));
    min-height: 480px;
    overflow: hidden;
  }

  .chat .chat-list,
  .chat.has-thread .chat-list {
    display: flex;
    min-height: 0;
    border-right: 1px solid var(--chat-divider);
  }

  .chat-pane,
  .chat.has-thread .chat-pane {
    display: block;
    min-width: 0;
    min-height: 0;
    position: relative;
  }

  .chat.has-thread .chat-thread {
    position: absolute;
    inset: 0;
    height: auto;
    top: 0;
    z-index: auto;
  }

  .chat-list__scroll {
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
  }
}

/* ======================================================== LISTA (ESQ.) */

.chat-list {
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  min-width: 0;
}

.chat-list__head {
  padding: var(--sp-4) var(--gutter) 0;
  background: var(--chat-bg);
}

.chat-list__titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  min-height: 44px;
  margin-bottom: var(--sp-3);
}

.chat-list__title {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-2xl);
  font-weight: 700;
  line-height: var(--lh-tight);
  letter-spacing: var(--tracking-tight);
  outline: none;
}

.chat-list__compose {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-right: -10px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  transition: background var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.chat-list__compose .chat-ic { width: 23px; height: 23px; }
.chat-list__compose:hover { background: var(--surface-2); }
.chat-list__compose:active { transform: scale(.94); }

/* Segmentado Conversas · Salas */
.chat-tabs {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface-2);
}

.chat-tab {
  flex: 1 1 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 var(--sp-3);
  border-radius: 10px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  font-weight: 600;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.chat-tab:hover { color: var(--text); }

.chat-tab[aria-selected="true"] {
  background: var(--chat-seg-thumb);
  color: var(--text);
  box-shadow: var(--chat-seg-shadow);
}

.chat-tab__badge,
.chat-pill {
  flex: none;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--r-full);
  background: var(--primary);
  color: var(--on-primary);
  font-size: 11px;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.chat-tab__badge[hidden] { display: none; }

.chat-list__scroll {
  flex: 1;
  min-height: 0;
  padding-bottom: var(--sp-4);
}

.chat-search {
  position: relative;
  display: flex;
  align-items: center;
  margin: var(--sp-3) var(--gutter) var(--sp-1);
}

.chat-search[hidden] { display: none; }

.chat-search .chat-ic {
  position: absolute;
  left: 14px;
  width: 18px;
  height: 18px;
  color: var(--text-muted);
  pointer-events: none;
}

.chat-search__input {
  width: 100%;
  min-height: 44px;
  padding: 0 var(--sp-3) 0 42px;
  border: 0;
  border-radius: var(--r-md);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* ≥ 16px evita zoom automático no iOS */
  -webkit-appearance: none;
  appearance: none;
}

.chat-search__input::placeholder { color: var(--text-subtle); }
.chat-search__input:focus { outline: none; box-shadow: 0 0 0 2px var(--text); }
.chat-search__input::-webkit-search-cancel-button { cursor: pointer; }

/* Fileira de pessoas (online primeiro) */
.chat-people {
  display: flex;
  gap: var(--sp-2);
  margin: 0;
  padding: var(--sp-3) var(--gutter) var(--sp-2);
  list-style: none;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  scroll-padding-inline: var(--gutter);
}

.chat-people::-webkit-scrollbar { display: none; }

.chat-people__item {
  flex: none;
  scroll-snap-align: start;
}

.chat-people__link {
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 66px;
  padding: 2px 0;
  border-radius: var(--r-md);
  color: var(--text);
  text-decoration: none;
  transition: transform var(--dur-fast) var(--ease);
}

.chat-people__link:active { transform: scale(.96); }

.chat-people__name {
  max-width: 100%;
  overflow: hidden;
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-people .chat-av__dot { right: 2px; bottom: 2px; }

/* Linhas */
.chat-rows {
  list-style: none;
  margin: 0;
  padding: var(--sp-1) 0 var(--sp-2);
}

.chat-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: 76px;
  padding: 0 var(--gutter);
  color: inherit;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease);
}

.chat-row:hover { background: var(--chat-row-hover); }
.chat-row:active { background: var(--chat-row-active); }
.chat-row.is-active { background: var(--chat-row-active); }

.chat-row:focus-visible {
  box-shadow: inset 0 0 0 2px var(--text) !important;
}

.chat-row__main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-self: stretch;
  justify-content: center;
  border-bottom: 1px solid var(--chat-divider);
  padding: var(--sp-2) 0;
}

.chat-row-item:last-child .chat-row__main,
.chat-row:hover .chat-row__main,
.chat-row.is-active .chat-row__main { border-bottom-color: transparent; }

.chat-row__top,
.chat-row__bottom {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-width: 0;
}

.chat-row__name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  font-size: var(--fs-base);
  letter-spacing: -.005em;
}

.chat-row__time {
  flex: none;
  font-size: var(--fs-xs);
  color: var(--text-subtle);
  font-variant-numeric: tabular-nums;
}

.chat-row.has-unread .chat-row__name { font-weight: 700; }

.chat-row.has-unread .chat-row__time {
  color: var(--text);
  font-weight: 600;
}

.chat-row__preview {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 3px;
  color: var(--text-muted);
  font-size: .875rem;
}

.chat-row.has-unread .chat-row__preview { color: var(--text); }
.chat-row__preview.is-deleted { font-style: italic; }

.chat-row__preview-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-row__preview .chat-tick { width: 16px; height: 16px; color: var(--text-subtle); }
.chat-row__preview .chat-tick.is-read { color: var(--text); }

.chat-row--person { min-height: 64px; }

.chat-row__go {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text);
}

.chat-row__go .chat-ic { width: 18px; height: 18px; }

.chat-group__title {
  margin: var(--sp-4) var(--gutter) 0;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-muted);
}

.chat-group .chat-rows { padding: 0; }

.chat-list__hint,
.chat-list__error {
  margin: var(--sp-6) var(--gutter);
  color: var(--text-muted);
  text-align: center;
  font-size: var(--fs-sm);
}

.chat-list__hint--left {
  margin: var(--sp-3) var(--gutter);
  text-align: left;
}

.chat-group .chat-list__hint {
  margin: var(--sp-2) var(--gutter) var(--sp-4);
  text-align: left;
}

.chat-list__error { display: grid; gap: var(--sp-3); justify-items: center; }

.chat-list__more {
  display: flex;
  justify-content: center;
  padding: var(--sp-3) var(--gutter) var(--sp-6);
}

/* Visitante na aba Salas: uma linha discreta com o convite. */
.chat-joinrow {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-3) var(--gutter) 0;
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--chat-divider);
}

.chat-joinrow__text {
  flex: 1;
  margin: 0;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  line-height: var(--lh-snug);
}

.chat-joinrow__btn {
  flex: none;
  min-height: 36px;
  padding: 0 var(--sp-4);
  border: 0;
  border-radius: var(--r-full);
  background: var(--primary);
  color: var(--on-primary);
  font-size: var(--fs-sm);
  font-weight: 600;
}

/* Esqueleto de carregamento */
.chat-skel {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: 76px;
  padding: 0 var(--gutter);
}

.chat-skel__av {
  width: 52px;
  height: 52px;
  flex: none;
  border-radius: 50%;
  background: var(--surface-2);
}

.chat-skel__lines { flex: 1; display: grid; gap: 9px; }

.chat-skel__lines span {
  display: block;
  height: 11px;
  border-radius: 6px;
  background: var(--surface-2);
}

.chat-skel__lines span:first-child { width: 42%; }
.chat-skel__lines span:last-child { width: 72%; }

.chat-skel__av,
.chat-skel__lines span { animation: chat-pulse 1.4s ease-in-out infinite; }

@keyframes chat-pulse {
  50% { opacity: .55; }
}

/* ============================================================= AVATARES */

.chat-av {
  --av-size: 48px;
  position: relative;
  flex: none;
  display: inline-block;
  width: var(--av-size);
  height: var(--av-size);
  border-radius: 50%;
}

.chat-av__img,
.chat-av__ini {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
}

.chat-av__ini {
  display: grid;
  place-items: center;
  background: var(--av-tone, #6b7a8f);
  color: #fff;
  font-weight: 600;
  font-size: calc(var(--av-size) * .38);
  letter-spacing: .01em;
  user-select: none;
}

.chat-av--ring {
  padding: 2px;
  background: var(--ring-gradient);
}

.chat-av__dot {
  position: absolute;
  right: 0;
  bottom: 0;
  width: calc(var(--av-size) * .27);
  height: calc(var(--av-size) * .27);
  min-width: 10px;
  min-height: 10px;
  max-width: 16px;
  max-height: 16px;
  border-radius: 50%;
  background: var(--chat-online);
  border: 2.5px solid var(--chat-bg);
}

.chat-room-av {
  --av-size: 48px;
  flex: none;
  display: grid;
  place-items: center;
  width: var(--av-size);
  height: var(--av-size);
  border-radius: 32%;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  font-size: calc(var(--av-size) * .32);
  letter-spacing: .02em;
}

.chat-room-av .chat-ic {
  width: calc(var(--av-size) * .44);
  height: calc(var(--av-size) * .44);
}

.chat-room-av--geral {
  background: var(--primary);
  color: var(--on-primary);
}

.chat-room-av--tag { color: var(--text-muted); }

/* ============================================================== CONVERSA */

.chat-thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--chat-bg);
  overflow: hidden;
}

.chat-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 60px;
  padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-1);
  padding-top: calc(var(--sp-2) + env(safe-area-inset-top, 0px));
  padding-left: calc(var(--sp-1) + env(safe-area-inset-left, 0px));
  padding-right: calc(var(--sp-2) + env(safe-area-inset-right, 0px));
  background: var(--chat-bg);
  border-bottom: 1px solid var(--chat-divider);
  z-index: 2;
}

.chat-head__back,
.chat-head__menu {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  transition: background var(--dur-fast) var(--ease);
}

.chat-head__back .chat-ic { width: 26px; height: 26px; }
.chat-head__menu .chat-ic { width: 24px; height: 24px; stroke-width: 2.6; }

.chat-head__back:hover,
.chat-head__menu:hover { background: var(--surface-2); }

.chat-head__av { display: inline-flex; flex: none; }
.chat-head__avlink { display: inline-flex; border-radius: 50%; }
.chat-head .chat-av__dot { border-color: var(--chat-bg); }

.chat-head__titles {
  flex: 1;
  min-width: 0;
  padding-left: 2px;
}

.chat-head__title {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  outline: none;
}

.chat-head__name {
  color: inherit;
  text-decoration: none;
}

.chat-head__name:hover { text-decoration: underline; }

.chat-head__sub {
  margin: 1px 0 0;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-head__sub.is-online { color: var(--accent); font-weight: 600; }

@media (min-width: 1024px) {
  .chat-head { padding-left: var(--sp-5); min-height: 68px; }
  .chat-head__back { display: none; }
}

.chat-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
}

.chat-body--center {
  align-items: center;
  justify-content: center;
  padding: var(--sp-6) var(--sp-4) calc(var(--sp-6) + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
}

.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overflow-anchor: none;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: var(--sp-2) 12px var(--sp-3);
  outline: none;
}

@media (min-width: 1024px) {
  .chat-scroll { padding: var(--sp-3) clamp(16px, 4vw, 56px) var(--sp-4); }
}

.chat-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  min-height: 40vh;
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

.chat-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  padding-top: var(--sp-3);
}

.chat-top:empty { display: none; }

.chat-top__loading { padding: var(--sp-2); }

/* Linha de sistema (uma só, discreta) */
.chat-info {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  max-width: 100%;
  margin: 0 auto;
  padding: 0 var(--sp-4);
  color: var(--text-subtle);
  font-size: var(--fs-xs);
  line-height: var(--lh-snug);
  text-align: center;
}

.chat-info .chat-ic { width: 12px; height: 12px; stroke-width: 2.2; }

/* Conversa vazia */
.chat-hello {
  display: grid;
  justify-items: center;
  gap: 4px;
  margin: var(--sp-8) auto var(--sp-4);
  max-width: 300px;
  text-align: center;
}

.chat-hello .chat-av,
.chat-hello .chat-room-av { margin-bottom: var(--sp-2); }

.chat-hello__title {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 700;
  letter-spacing: -.01em;
}

.chat-hello__text {
  margin: 0;
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

.chat-items {
  display: flex;
  flex-direction: column;
}

/* Data (gruda no topo enquanto rola) */
.chat-date {
  position: sticky;
  top: 6px;
  z-index: 1;
  display: flex;
  justify-content: center;
  margin: var(--sp-4) 0 var(--sp-2);
  pointer-events: none;
}

.chat-date span {
  padding: 4px 10px;
  border-radius: var(--r-full);
  background: var(--surface-glass);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 600;
}

/* Divisor de não lidas: texto entre dois fios */
.chat-unread {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-4) 4px var(--sp-2);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 600;
}

.chat-unread::before,
.chat-unread::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border-strong);
}

/* --------------------------------------------------------------- balões */

.chat-msg {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  margin-top: 3px;
  padding: 0 4px;
}

.chat-msg.is-first { margin-top: var(--sp-3); }
.chat-msg.is-mine { flex-direction: row-reverse; }

.chat-msg__av {
  flex: none;
  width: 28px;
  align-self: flex-end;
}

.chat-msg__av a { display: inline-flex; border-radius: 50%; }

/* Nas salas, o avatar acompanha a última mensagem do grupo. */
.chat-thread--room .chat-msg__av { align-self: flex-start; }

.chat-bubble {
  --r-big: 20px;
  --r-small: 6px;
  position: relative;
  max-width: min(78%, 520px);
  padding: 8px 12px 8px 14px;
  border-radius: var(--r-small) var(--r-big) var(--r-big) var(--r-small);
  background: var(--chat-theirs);
  color: var(--chat-theirs-text);
  font-size: var(--fs-base);
  line-height: 1.38;
  overflow-wrap: anywhere;
  word-break: break-word;
  transition: filter var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

/* Dos outros: canto pequeno do lado esquerdo, só o topo do grupo arredondado. */
.chat-msg.is-theirs.is-first .chat-bubble { border-top-left-radius: var(--r-big); }

.chat-msg.is-mine .chat-bubble {
  border-radius: var(--r-big) var(--r-small) var(--r-small) var(--r-big);
  background: var(--chat-mine);
  color: var(--chat-mine-text);
}

.chat-msg.is-mine.is-first .chat-bubble { border-top-right-radius: var(--r-big); }

.chat-bubble.is-selected { filter: brightness(.94); transform: scale(.99); }

.chat-bubble__name {
  display: block;
  margin-bottom: 2px;
  font-size: var(--fs-xs);
  font-weight: 700;
  text-decoration: none;
  color: var(--chat-name-0);
}

.chat-bubble__name:hover { text-decoration: underline; }

.chat-name-0 { color: var(--chat-name-0); }
.chat-name-1 { color: var(--chat-name-1); }
.chat-name-2 { color: var(--chat-name-2); }
.chat-name-3 { color: var(--chat-name-3); }
.chat-name-4 { color: var(--chat-name-4); }
.chat-name-5 { color: var(--chat-name-5); }
.chat-name-6 { color: var(--chat-name-6); }
.chat-name-7 { color: var(--chat-name-7); }

.chat-bubble__text { white-space: pre-wrap; }

.chat-bubble__text.is-deleted {
  font-style: italic;
  color: var(--chat-meta);
}

.chat-bubble__text.is-deleted .chat-ic {
  width: 15px;
  height: 15px;
  vertical-align: -3px;
}

/* Espaço reservado para a hora/ticks no fim da última linha */
.chat-bubble__spacer {
  display: inline-block;
  width: 42px;
  height: 1px;
}

.chat-bubble__spacer.is-mine { width: 60px; }

.chat-bubble__meta {
  position: absolute;
  right: 11px;
  bottom: 6px;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  line-height: 1;
  color: var(--chat-meta);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.chat-tick { width: 15px; height: 15px; color: var(--chat-tick); stroke-width: 2.2; }
.chat-bubble__meta .chat-ic--clock { width: 12px; height: 12px; }
.chat-tick.is-read { color: var(--chat-tick-read); }
.chat-tick.is-failed { color: var(--danger); }

.chat-link {
  color: var(--chat-link);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

/* Botão ⌄ de opções (aparece no hover, no computador) */
.chat-bubble__more {
  position: absolute;
  top: 3px;
  right: 3px;
  display: grid;
  place-items: center;
  width: 24px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: inherit;
  color: var(--chat-meta);
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease);
}

.chat-bubble__more .chat-ic { width: 17px; height: 17px; }

.chat-bubble:hover .chat-bubble__more,
.chat-bubble__more:focus-visible,
.chat-bubble.is-selected .chat-bubble__more { opacity: 1; }

@media (hover: none), (pointer: coarse) {
  .chat-bubble__more { display: none; }

  .chat-bubble {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}

.chat-msg.is-pending .chat-bubble { opacity: .8; }

.chat-retry {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  align-self: center;
  border: 0;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
}

.chat-retry .chat-ic { width: 18px; height: 18px; }

/* Botão "ir para o fim" com contador */
.chat-jump {
  position: absolute;
  right: 14px;
  bottom: 12px;
  z-index: 3;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-md), 0 0 0 1px var(--border);
  animation: chat-pop var(--dur) var(--ease);
}

.chat-jump[hidden] { display: none; }

.chat-jump__count {
  position: absolute;
  top: -6px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: var(--r-full);
  background: var(--primary);
  color: var(--on-primary);
  font-size: 11px;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
}

.chat-jump__count[hidden] { display: none; }

@keyframes chat-pop {
  from { transform: scale(.6); opacity: 0; }
}

/* ============================================================== COMPOSER */

.chat-composer {
  position: relative;
  flex: none;
  padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  padding-left: calc(12px + env(safe-area-inset-left, 0px));
  padding-right: calc(12px + env(safe-area-inset-right, 0px));
  background: var(--chat-bg);
  z-index: 2;
}

/* Com o teclado aberto o inset de baixo não se aplica (visualViewport já encolheu a tela). */
.chat-composer:focus-within { padding-bottom: max(8px, calc(env(safe-area-inset-bottom, 0px) - 20px)); }

@media (min-width: 1024px) {
  .chat-composer { padding: 10px clamp(16px, 4vw, 56px) 14px; }
}

.chat-composer__form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.chat-composer__form[hidden] { display: none; }

.chat-composer__field {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: flex-end;
  gap: 0;
  min-height: 44px;
  padding: 0 14px 0 2px;
  border-radius: 22px;
  background: var(--surface-2);
  transition: box-shadow var(--dur) var(--ease);
}

.chat-composer__emoji {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  transition: color var(--dur-fast) var(--ease);
}

.chat-composer__emoji:hover,
.chat-composer__emoji[aria-expanded="true"] { color: var(--text); }

.chat-composer__input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 144px;
  margin: 0;
  padding: 11px 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* ≥ 16px evita zoom automático no iOS */
  line-height: 22px;
  resize: none;
  overflow-y: auto;
}

.chat-composer__input:focus,
.chat-composer__input:focus-visible { outline: none; box-shadow: none; }
.chat-composer__field:focus-within { box-shadow: inset 0 0 0 1.5px var(--border-strong); }
.chat-composer__input::placeholder { color: var(--text-subtle); }

.chat-composer__count {
  align-self: center;
  font-size: 11px;
  color: var(--text-subtle);
}

.chat-composer__count[hidden] { display: none; }
.chat-composer__count.is-over { color: var(--danger); font-weight: 700; }

.chat-composer__send {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  color: var(--on-primary);
  transition: transform var(--dur-fast) var(--ease), background var(--dur) var(--ease);
}

.chat-composer__send .chat-ic { width: 20px; height: 20px; margin-left: 2px; transition: opacity var(--dur) var(--ease); }
.chat-composer__send:hover:not(:disabled) { background: var(--primary-hover); }
.chat-composer__send:active:not(:disabled) { transform: scale(.92); }
.chat-composer__send:disabled { cursor: default; }
.chat-composer__send:disabled .chat-ic { opacity: .4; }

.chat-composer__error {
  margin: 0 0 8px;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: var(--fs-sm);
  font-weight: 500;
}

.chat-composer__error[hidden],
.chat-composer__notice[hidden] { display: none; }

.chat-composer__notice {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
  min-height: 44px;
  padding: 6px 12px;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  text-align: center;
}

.chat-composer__notice .chat-ic { width: 15px; height: 15px; }

.chat-composer__notice-action {
  min-height: 36px;
  padding: 0 var(--sp-4);
  border: 0;
  border-radius: var(--r-full);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
}

/* Seletor de emojis */
.chat-emoji {
  position: absolute;
  left: 12px;
  bottom: calc(100% + 6px);
  z-index: 5;
  width: min(340px, calc(100% - 24px));
  padding: var(--sp-2);
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  animation: chat-rise var(--dur) var(--ease);
}

.chat-emoji[hidden] { display: none; }

.chat-emoji__grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  max-height: 220px;
  overflow-y: auto;
}

.chat-emoji__btn {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 36px;
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  font-size: 22px;
  line-height: 1;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  transition: background var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.chat-emoji__btn:hover { background: var(--surface-2); transform: scale(1.1); }

@keyframes chat-rise {
  from { transform: translateY(8px); opacity: 0; }
}

/* ================================================================ MENU */

.chat-menu {
  position: fixed;
  z-index: 130;
  min-width: 200px;
  padding: 6px;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: scale(.96);
  transform-origin: top right;
  transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
  outline: none;
}

.chat-menu.is-open { opacity: 1; transform: none; }

.chat-menu__item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  min-height: 44px;
  padding: 0 var(--sp-3);
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--fs-base);
  text-align: left;
  cursor: pointer;
}

.chat-menu__item .chat-ic { width: 19px; height: 19px; color: var(--text-muted); }
.chat-menu__item:hover,
.chat-menu__item:focus-visible { background: var(--surface-2); outline: none; }
.chat-menu__item.is-danger,
.chat-menu__item.is-danger .chat-ic { color: var(--danger); }

/* ======================================================= ESTADOS VAZIOS */

.chat-empty {
  display: grid;
  justify-items: center;
  gap: var(--sp-2);
  max-width: 380px;
  margin: 0 auto;
  padding: var(--sp-10) var(--sp-5);
  text-align: center;
}

.chat-empty--compact { padding: var(--sp-8) var(--sp-5); }

.chat-empty__art { margin-bottom: var(--sp-2); }

.chat-empty__mascot {
  display: block;
  width: 96px;
  height: 96px;
}

.chat-empty__icon {
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
}

.chat-empty__icon .chat-ic { width: 28px; height: 28px; stroke-width: 1.7; }

.chat-empty__title {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-lg);
  font-weight: 700;
  letter-spacing: -.01em;
}

.chat-empty__text {
  margin: 0;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  line-height: var(--lh-base);
}

.chat-empty__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--sp-2);
  margin-top: var(--sp-3);
}

.chat-welcome {
  display: grid;
  place-items: center;
  height: 100%;
  background: var(--chat-bg);
}

.chat-thread--visitor .chat-head { display: none; }

.chat-boot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  min-height: 50vh;
  color: var(--text-muted);
}

@media (prefers-reduced-motion: reduce) {
  .chat-spinner,
  .chat-skel__av,
  .chat-skel__lines span,
  .chat-jump,
  .chat-emoji { animation: none; }

  .chat-menu,
  .chat-bubble,
  .chat-composer__send,
  .chat-people__link,
  .chat-list__compose { transition: none; }
}
