/* ══════════════════════════════════════════════════════════════════════════
   Nexo · Conversa e chamada
   ══════════════════════════════════════════════════════════════════════════ */

@layer components {

  /* ════════════════════════════════════════════════════════════════════════
     Conversa
     ════════════════════════════════════════════════════════════════════════ */

  .chat {
    display: flex;
    flex-direction: column;
    min-height: 0;
    height: 100%;
  }

  .chat__scroll {
    flex: 1 1 auto;
    min-height: 0;
    padding: var(--sp-6) 0 var(--sp-4);
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) transparent;
  }

  .chat__scroll::-webkit-scrollbar { width: 10px; }
  .chat__scroll::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border: 3px solid transparent;
    border-radius: var(--r-full);
    background-clip: padding-box;
  }

  /* Começo da conversa: fica no topo, acima da primeira mensagem. */
  .chat__intro {
    padding: var(--sp-9) var(--sp-8) var(--sp-8);
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: var(--sp-6);
  }

  .chat__load-more {
    display: flex;
    justify-content: center;
    padding: var(--sp-4);
  }

  /* ── Mensagem ─────────────────────────────────────────────────────────── */

  .msg {
    position: relative;
    display: flex;
    gap: var(--sp-5);
    padding: 2px var(--sp-8);
    scroll-margin: var(--sp-8);
  }

  .msg:hover { background: var(--surface-hover); }

  /* Mensagens seguidas da mesma pessoa em menos de 5 minutos são agrupadas:
     sem avatar e sem repetir o nome. O horário aparece só no hover, na
     goteira à esquerda. */
  .msg--grouped { margin-top: 0; }
  .msg--first { margin-top: var(--sp-5); }

  .msg__gutter {
    flex: none;
    width: 40px;
    display: flex;
    justify-content: center;
  }

  .msg__time-hover {
    display: none;
    font-size: 10px;
    line-height: 21px;
    color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
    user-select: none;
  }

  .msg--grouped:hover .msg__time-hover { display: block; }

  .msg__body { flex: 1 1 auto; min-width: 0; }

  .msg__head {
    display: flex;
    align-items: baseline;
    gap: var(--sp-4);
    margin-bottom: 1px;
  }

  .msg__author {
    font-size: var(--fs-ui);
    font-weight: var(--fw-semibold);
    color: var(--text-primary);
  }

  .msg__time {
    font-size: var(--fs-micro);
    color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
  }

  .msg__text {
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    color: var(--text-primary);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }

  .msg__edited {
    margin-left: var(--sp-3);
    font-size: var(--fs-micro);
    color: var(--text-tertiary);
  }

  .msg__deleted {
    font-size: var(--fs-body);
    font-style: italic;
    color: var(--text-tertiary);
  }

  /* Resposta: citação compacta acima, com a linha de conexão à esquerda. */
  .msg__reply {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    margin-bottom: var(--sp-2);
    font-size: var(--fs-small);
    color: var(--text-secondary);
    cursor: pointer;
  }

  .msg__reply::before {
    content: "";
    width: 22px;
    height: 9px;
    margin-left: 4px;
    border-left: 2px solid var(--border-strong);
    border-top: 2px solid var(--border-strong);
    border-top-left-radius: var(--r-sm);
  }

  .msg__reply-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .85; }

  /* Estados de envio otimista. */
  .msg--sending { opacity: .55; }

  .msg--failed .msg__text { color: var(--danger-text); }

  .msg__retry {
    margin-left: var(--sp-4);
    font-size: var(--fs-small);
    color: var(--danger-text);
    text-decoration: underline;
    cursor: pointer;
  }

  /* Ações no hover. Aparecem sobrepostas no canto superior direito. */
  .msg__actions {
    position: absolute;
    top: -14px;
    right: var(--sp-8);
    display: none;
    gap: 2px;
    padding: 2px;
    background: var(--surface-overlay);
    border: 1px solid var(--border-default);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-sm);
  }

  .msg:hover .msg__actions,
  .msg:focus-within .msg__actions { display: flex; }

  /* ── Reações ──────────────────────────────────────────────────────────── */

  .reactions { display: flex; flex-wrap: wrap; gap: var(--sp-2); margin-top: var(--sp-3); }

  .reaction {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-3);
    height: 22px;
    padding: 0 var(--sp-4);
    font-size: var(--fs-small);
    line-height: 1;
    color: var(--text-secondary);
    background: var(--surface-inset);
    border: 1px solid var(--border-subtle);
    border-radius: var(--r-full);
    cursor: pointer;
    transition: border-color var(--t-fast) var(--ease), background-color var(--t-fast) var(--ease);
  }

  .reaction:hover { border-color: var(--border-strong); }

  .reaction--mine {
    color: var(--accent-text);
    background: var(--accent-subtle);
    border-color: var(--accent-border);
  }

  .reaction__count { font-variant-numeric: tabular-nums; font-weight: var(--fw-semibold); }

  /* ── Separador de dia e de não lidas ──────────────────────────────────── */

  .chat__divider {
    display: flex;
    align-items: center;
    gap: var(--sp-5);
    margin: var(--sp-6) var(--sp-8) var(--sp-5);
    font-size: var(--fs-micro);
    font-weight: var(--fw-semibold);
    color: var(--text-tertiary);
  }

  .chat__divider::before,
  .chat__divider::after {
    content: "";
    flex: 1 1 auto;
    height: 1px;
    background: var(--border-subtle);
  }

  .chat__divider--unread { color: var(--danger-text); }
  .chat__divider--unread::before,
  .chat__divider--unread::after { background: var(--danger); opacity: .5; }

  /* ── Digitando ────────────────────────────────────────────────────────── */

  .typing {
    display: flex;
    align-items: center;
    gap: var(--sp-4);
    height: 22px;
    padding: 0 var(--sp-8);
    font-size: var(--fs-small);
    color: var(--text-tertiary);
  }

  .typing__dots { display: inline-flex; gap: 3px; }

  .typing__dot {
    width: 4px;
    height: 4px;
    background: currentColor;
    border-radius: var(--r-full);
    animation: nx-typing 1.2s ease-in-out infinite;
  }

  .typing__dot:nth-child(2) { animation-delay: .15s; }
  .typing__dot:nth-child(3) { animation-delay: .3s; }

  @keyframes nx-typing {
    0%, 60%, 100% { opacity: .25; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-2px); }
  }

  /* ── Campo de mensagem ────────────────────────────────────────────────── */

  .composer { padding: 0 var(--sp-8) var(--sp-6); }

  .composer__reply {
    display: flex;
    align-items: center;
    gap: var(--sp-4);
    padding: var(--sp-3) var(--sp-5);
    font-size: var(--fs-small);
    color: var(--text-secondary);
    background: var(--surface-inset);
    border: 1px solid var(--border-default);
    border-bottom: 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
  }

  .composer__box {
    display: flex;
    align-items: flex-end;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-3) var(--sp-3) var(--sp-5);
    background: var(--surface-inset);
    border: 1px solid var(--border-default);
    border-radius: var(--r-lg);
    transition: border-color var(--t-fast) var(--ease);
  }

  .composer__box:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
  }

  .composer--replying .composer__box { border-radius: 0 0 var(--r-lg) var(--r-lg); }

  .composer__input {
    flex: 1 1 auto;
    max-height: 200px;
    min-height: 24px;
    padding: 6px 0;
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    color: var(--text-primary);
    background: none;
    border: none;
    outline: none;
    resize: none;
  }

  .composer__actions { display: flex; align-items: center; gap: 2px; }

  .composer__hint {
    margin-top: var(--sp-3);
    font-size: var(--fs-micro);
    color: var(--text-tertiary);
  }

  .composer__blocked {
    padding: var(--sp-5);
    font-size: var(--fs-small);
    color: var(--text-secondary);
    text-align: center;
    background: var(--surface-inset);
    border: 1px solid var(--border-default);
    border-radius: var(--r-lg);
  }

  /* ── Lista lateral de conversas ───────────────────────────────────────── */

  .dm-item { position: relative; }

  .dm-item__preview {
    display: block;
    font-size: var(--fs-micro);
    color: var(--text-tertiary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .dm-item--unread .item__label { color: var(--text-primary); font-weight: var(--fw-semibold); }
  .dm-item--unread .dm-item__preview { color: var(--text-secondary); }

  /* ════════════════════════════════════════════════════════════════════════
     Chamada
     ════════════════════════════════════════════════════════════════════════ */

  .call {
    position: fixed;
    inset: 0;
    z-index: var(--z-modal);
    display: flex;
    flex-direction: column;
    background: var(--surface-sunken);
    animation: nx-fade-in var(--t-base) var(--ease);
  }

  .call__bar {
    display: flex;
    align-items: center;
    gap: var(--sp-5);
    height: 48px;
    padding: 0 var(--sp-6);
    border-bottom: 1px solid var(--border-subtle);
  }

  .call__title { font-size: var(--fs-ui); font-weight: var(--fw-semibold); }

  .call__timer {
    font-size: var(--fs-small);
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
  }

  /* Palco: o grid se adapta à quantidade de participantes. */
  .call__stage {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    gap: var(--sp-4);
    padding: var(--sp-4);
    grid-template-columns: 1fr;
    align-content: center;
  }

  .call__stage[data-count="2"] { grid-template-columns: repeat(2, 1fr); }
  .call__stage[data-count="3"],
  .call__stage[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
  .call__stage[data-count="5"],
  .call__stage[data-count="6"] { grid-template-columns: repeat(3, 1fr); }
  .call__stage[data-count="7"],
  .call__stage[data-count="8"],
  .call__stage[data-count="9"] { grid-template-columns: repeat(3, 1fr); }
  .call__stage[data-many="1"] { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

  /* Modo apresentação: a tela compartilhada ocupa o palco e os participantes
     vão para uma faixa lateral. */
  .call__stage--presenting {
    grid-template-columns: minmax(0, 1fr) 220px;
    align-content: stretch;
  }

  .call__presentation {
    grid-row: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    border-radius: var(--r-2xl);
    overflow: hidden;
  }

  .call__presentation video { width: 100%; height: 100%; object-fit: contain; }

  .call__filmstrip {
    display: flex;
    flex-direction: column;
    gap: var(--sp-4);
    overflow-y: auto;
    scrollbar-width: thin;
  }

  .tile {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    aspect-ratio: 16 / 10;
    background: var(--surface-raised);
    border: 2px solid transparent;
    border-radius: var(--r-2xl);
    overflow: hidden;
    transition: border-color 120ms linear;
  }

  .tile video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
  }

  /* Espelha só o próprio vídeo: ver a si mesmo invertido é desconcertante,
     ver o outro invertido é errado. */
  .tile--self video { transform: scaleX(-1); }
  .tile--self.tile--screen video { transform: none; }

  /* Indicador de fala: anel discreto, sem piscar. */
  .tile--speaking { border-color: var(--success); }

  .tile__placeholder { display: flex; flex-direction: column; align-items: center; gap: var(--sp-4); }

  .tile__label {
    position: absolute;
    left: var(--sp-4);
    bottom: var(--sp-4);
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    max-width: calc(100% - var(--sp-8));
    padding: var(--sp-2) var(--sp-4);
    font-size: var(--fs-small);
    color: #fff;
    background: rgb(0 0 0 / .55);
    border-radius: var(--r-md);
    backdrop-filter: blur(4px);
  }

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

  .tile__badges {
    position: absolute;
    right: var(--sp-4);
    top: var(--sp-4);
    display: flex;
    gap: var(--sp-2);
  }

  .tile__badge {
    display: grid;
    place-items: center;
    width: 24px;
    height: 24px;
    color: #fff;
    background: rgb(0 0 0 / .55);
    border-radius: var(--r-md);
    backdrop-filter: blur(4px);
  }

  .tile__badge--muted { color: #ff8b93; }

  /* Qualidade: três barras que se acendem conforme o nível. */
  .quality { display: inline-flex; align-items: flex-end; gap: 2px; height: 12px; }

  .quality__bar {
    width: 3px;
    background: currentColor;
    border-radius: 1px;
    opacity: .25;
  }

  .quality__bar:nth-child(1) { height: 5px; }
  .quality__bar:nth-child(2) { height: 8px; }
  .quality__bar:nth-child(3) { height: 11px; }

  .quality[data-level="1"] .quality__bar:nth-child(1),
  .quality[data-level="2"] .quality__bar:nth-child(-n+2),
  .quality[data-level="3"] .quality__bar { opacity: 1; }

  .quality[data-level="1"] { color: var(--danger); }
  .quality[data-level="2"] { color: var(--warning); }
  .quality[data-level="3"] { color: var(--success); }

  /* ── Controles ────────────────────────────────────────────────────────── */

  .call__controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-4);
    padding: var(--sp-6);
    border-top: 1px solid var(--border-subtle);
  }

  .call-btn {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    color: var(--text-primary);
    background: var(--surface-raised);
    border: 1px solid var(--border-default);
    border-radius: var(--r-full);
    cursor: pointer;
    transition: background-color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
  }

  .call-btn:hover { background: var(--surface-overlay); border-color: var(--border-strong); }

  /* Desligado: fundo vermelho E barra diagonal no ícone. Cor sozinha não
     comunica estado para quem não a distingue. */
  .call-btn--off {
    color: #fff;
    background: var(--danger);
    border-color: var(--danger);
  }

  .call-btn--active {
    color: var(--text-on-accent);
    background: var(--accent);
    border-color: var(--accent);
  }

  /* Sair tem largura e cor próprias para nunca ser confundido com mutar. */
  .call-btn--leave {
    width: auto;
    padding: 0 var(--sp-7);
    gap: var(--sp-3);
    color: #fff;
    background: var(--danger);
    border-color: var(--danger);
    font-size: var(--fs-ui);
    font-weight: var(--fw-medium);
  }

  .call-btn--leave:hover { background: var(--danger-hover); border-color: var(--danger-hover); }

  .call__separator { width: 1px; height: 28px; background: var(--border-default); }

  /* ── Chamada recebida ─────────────────────────────────────────────────── */

  .ringing {
    position: fixed;
    right: var(--sp-6);
    bottom: var(--sp-6);
    z-index: var(--z-toast);
    width: 320px;
    padding: var(--sp-6);
    background: var(--surface-overlay);
    border: 1px solid var(--border-default);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-xl);
    animation: nx-toast-in var(--t-slow) var(--ease-out);
  }

  .ringing__actions { display: flex; gap: var(--sp-4); margin-top: var(--sp-6); }

  .ringing__pulse {
    position: relative;
    display: inline-flex;
  }

  .ringing__pulse::after {
    content: "";
    position: absolute;
    inset: -4px;
    border: 2px solid var(--success);
    border-radius: var(--r-full);
    animation: nx-ring 1.6s var(--ease) infinite;
  }

  @keyframes nx-ring {
    0% { opacity: .8; transform: scale(.9); }
    100% { opacity: 0; transform: scale(1.25); }
  }

  /* ── Faixa de chamada em andamento ────────────────────────────────────── */

  .call-banner {
    display: flex;
    align-items: center;
    gap: var(--sp-5);
    margin: var(--sp-5) var(--sp-8) 0;
    padding: var(--sp-4) var(--sp-5);
    background: var(--success-subtle);
    border: 1px solid var(--success);
    border-radius: var(--r-lg);
  }

  @media (max-width: 859px) {
    .call__stage,
    .call__stage[data-count="2"],
    .call__stage[data-count="3"],
    .call__stage[data-count="4"] { grid-template-columns: 1fr; }

    .call__stage--presenting { grid-template-columns: 1fr; grid-template-rows: 1fr 96px; }
    .call__filmstrip { flex-direction: row; }
    .call-btn { width: 48px; height: 48px; }
    .msg { padding-inline: var(--sp-5); }
    .composer { padding-inline: var(--sp-5); }
  }
}
