/* =====================================================================
   Sistema de Mensagens — estilo conversa (WhatsApp-like)
   + Popover do sino + Toast de boas-vindas
   ===================================================================== */

/* ========= Página principal (layout 2 colunas) ========= */
.msg-wrap {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    overflow: hidden;
    height: calc(100vh - var(--topbar-height) - 100px);
    min-height: 500px;
}
@media (max-width: 820px) {
    .msg-wrap { grid-template-columns: 1fr; }
    .msg-wrap.show-thread .msg-sidebar { display: none; }
    .msg-wrap:not(.show-thread) .msg-main { display: none; }
}

/* ========= Coluna esquerda (lista de threads) ========= */
.msg-sidebar {
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg-surface-alt);
}
.msg-sidebar-head {
    padding: 1rem 1.15rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    background: var(--bg-surface);
}
.msg-sidebar-head h2 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
}
.msg-sidebar-head .btn {
    padding: 0.35rem 0.7rem;
    font-size: 0.78rem;
}
.msg-threads {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}
.msg-thread-item {
    display: flex;
    gap: 0.7rem;
    align-items: flex-start;
    padding: 0.8rem 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background var(--t-fast);
}
.msg-thread-item:hover { background: var(--bg-surface); }
.msg-thread-item.active { background: var(--bg-surface); border-left: 3px solid var(--color-info, #0ea5e9); padding-left: calc(1rem - 3px); }
.msg-thread-avatar {
    width: 40px; height: 40px; min-width: 40px;
    border-radius: 50%;
    background: var(--text-primary);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.95rem;
    font-weight: 700;
}
.msg-thread-avatar.bc-empresa { background: #d97706; }
.msg-thread-avatar.bc-setor   { background: #8b5cf6; }
.msg-thread-avatar.grupo      { background: #10b981; }
.msg-thread-corpo {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.msg-thread-topo {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
}
.msg-thread-nome {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.msg-thread-tempo {
    font-size: 0.7rem;
    color: var(--text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}
.msg-thread-preview {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
}
.msg-thread-texto {
    font-size: 0.82rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}
.msg-thread-badge {
    background: var(--color-danger, #dc2626);
    color: #fff;
    font-size: 0.68rem;
    font-weight: 700;
    border-radius: var(--radius-full);
    padding: 1px 7px;
    min-width: 20px;
    text-align: center;
}

.msg-threads-vazio {
    padding: 2rem 1.5rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
    line-height: 1.6;
}
.msg-threads-vazio i { font-size: 2rem; display: block; margin-bottom: 0.5rem; opacity: 0.4; }

/* ========= Coluna direita (thread ativa) ========= */
.msg-main {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg-surface);
}
.msg-main-placeholder {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    color: var(--text-muted);
    padding: 2rem;
    text-align: center;
}
.msg-main-placeholder i { font-size: 3rem; opacity: 0.3; }
.msg-main-placeholder p { margin: 0; font-size: 0.9rem; }

.msg-main-head {
    padding: 0.85rem 1.2rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.7rem;
    background: var(--bg-surface-alt);
    flex-shrink: 0;
}
.msg-main-voltar {
    display: none;
    width: 32px; height: 32px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
}
.msg-main-voltar:hover { background: var(--border-color); }
@media (max-width: 820px) { .msg-main-voltar { display: inline-flex; align-items: center; justify-content: center; } }

.msg-main-nome { font-weight: 700; color: var(--text-primary); font-size: 0.95rem; }
.msg-main-sub  { font-size: 0.75rem; color: var(--text-muted); }

.msg-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.2rem;
    background:
        radial-gradient(var(--bg-surface-alt) 1px, transparent 1px) 0 0 / 18px 18px,
        var(--bg-surface);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 0;
}

.msg-bubble {
    max-width: 70%;
    padding: 0.55rem 0.85rem;
    border-radius: 14px;
    font-size: 0.88rem;
    line-height: 1.5;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    box-shadow: var(--shadow-sm);
    word-wrap: break-word;
    white-space: pre-wrap;
    position: relative;
}
.msg-bubble.mine {
    align-self: flex-end;
    background: #dcfce7;
    border-color: #bbf7d0;
    color: #052e16;
}
.msg-bubble.theirs {
    align-self: flex-start;
}
.msg-bubble-remetente {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 700;
    margin-bottom: 2px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.msg-bubble-remetente.msg-bubble-eu {
    color: #15803d;  /* verde para combinar com o bubble mine */
}

/* Prefixo "Você:" / "Nome:" na lista de conversas à esquerda */
.msg-thread-prefixo {
    color: var(--text-primary);
    font-weight: 600;
}
.msg-bubble-rodape {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.3rem;
    font-size: 0.68rem;
    color: var(--text-muted);
    margin-top: 3px;
}
.msg-bubble.mine .msg-bubble-rodape { color: #065f46; }
.msg-bubble-tiques {
    display: inline-flex;
    align-items: center;
    gap: 1px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
}
.msg-bubble-tiques:hover { background: rgba(0,0,0,0.05); }
.msg-bubble-tiques.entregue { color: var(--text-muted); }
.msg-bubble-tiques.parcial  { color: var(--text-muted); }
.msg-bubble-tiques.lida     { color: #2563eb; }

/* Popover "quem leu" */
.msg-leituras-popover {
    position: absolute;
    bottom: calc(100% + 6px);
    right: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem;
    font-size: 0.8rem;
    color: var(--text-primary);
    box-shadow: var(--shadow);
    min-width: 220px;
    z-index: 100;
}
.msg-leituras-popover h4 {
    margin: 0 0 0.4rem;
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 700;
}
.msg-leituras-popover ul {
    margin: 0; padding: 0; list-style: none;
    display: flex; flex-direction: column; gap: 3px;
}
.msg-leituras-popover li {
    display: flex; align-items: center; gap: 0.4rem;
    font-size: 0.78rem;
}
.msg-leituras-popover li i { font-size: 0.72rem; width: 14px; }
.msg-leituras-popover li .l-nome { flex: 1; color: var(--text-primary); }
.msg-leituras-popover li .l-tempo { color: var(--text-muted); font-size: 0.7rem; }
.msg-leituras-popover li.lida   i { color: #2563eb; }
.msg-leituras-popover li.nao-lida i { color: var(--text-muted); }

/* Input de envio */
.msg-form {
    padding: 0.85rem 1.2rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-surface-alt);
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    flex-shrink: 0;
}
.msg-form textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 0.6rem 0.8rem;
    font-family: inherit;
    font-size: 0.9rem;
    background: var(--bg-surface);
    color: var(--text-primary);
    max-height: 120px;
    min-height: 40px;
    line-height: 1.4;
}
.msg-form textarea:focus {
    outline: none;
    border-color: var(--color-primary);
}
.msg-form-enviar {
    background: var(--color-info, #0ea5e9);
    color: #fff;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background var(--t-fast);
}
.msg-form-enviar:hover { background: #0284c7; }
.msg-form-enviar:disabled { background: var(--text-muted); cursor: not-allowed; }
.msg-form-char {
    position: absolute;
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* ========= Modal de nova mensagem (escolher destinatários) ========= */
.msg-modal-destinatarios {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}
.msg-dest-opcoes {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}
.msg-dest-opcao {
    padding: 0.7rem 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-secondary);
    background: var(--bg-surface);
    transition: border-color var(--t-fast), background var(--t-fast);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.msg-dest-opcao:hover { background: var(--bg-surface-alt); border-color: var(--border-color-strong); }
.msg-dest-opcao.ativa { border-color: var(--color-info, #0ea5e9); background: #eff6ff; color: var(--text-primary); font-weight: 600; }
.msg-dest-opcao i { color: var(--text-muted); }
.msg-dest-opcao.ativa i { color: var(--color-info, #0ea5e9); }
.msg-dest-opcao.disabled { opacity: 0.45; cursor: not-allowed; pointer-events: none; }

.msg-dest-busca {
    position: relative;
}
.msg-dest-busca input {
    width: 100%;
    padding: 0.55rem 0.8rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    background: var(--bg-surface);
}
.msg-dest-lista {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.3rem;
}
.msg-dest-lista label {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.45rem 0.6rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-secondary);
    transition: background var(--t-fast);
}
.msg-dest-lista label:hover { background: var(--bg-surface-alt); }
.msg-dest-lista label.disabled { opacity: 0.4; cursor: not-allowed; }
.msg-dest-lista input[type="checkbox"] { margin: 0; }
.msg-dest-lista .nome { flex: 1; }
.msg-dest-lista .setor { font-size: 0.72rem; color: var(--text-muted); }

.msg-dest-contagem {
    font-size: 0.78rem;
    color: var(--text-muted);
    text-align: right;
}
.msg-dest-contagem strong { color: var(--text-primary); }
.msg-dest-contagem.excedeu { color: var(--color-danger, #dc2626); }

/* ========= Popover do sino (topbar) ========= */
.sino-wrap {
    position: relative;
}
.sino-popover {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    width: 360px;
    max-width: 95vw;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    opacity: 0;
    transform: translateY(-6px);
    pointer-events: none;
    transition: opacity var(--t-fast), transform var(--t-fast);
}
.sino-wrap.aberto .sino-popover {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
.sino-popover-head {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.sino-popover-head h3 {
    margin: 0;
    font-size: 0.92rem;
    color: var(--text-primary);
    font-weight: 700;
}
.sino-popover-head .contador {
    font-size: 0.72rem;
    color: var(--text-muted);
    font-weight: 600;
}
.sino-popover-lista {
    max-height: 360px;
    overflow-y: auto;
}
.sino-popover-item {
    padding: 0.7rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    gap: 0.6rem;
    cursor: pointer;
    transition: background var(--t-fast);
    text-decoration: none;
    color: inherit;
}
.sino-popover-item:hover { background: var(--bg-surface-alt); }
.sino-popover-item.nao-lida { background: #eff6ff; }
.sino-popover-item.nao-lida:hover { background: #dbeafe; }
.sino-popover-item-avatar {
    width: 34px; height: 34px; min-width: 34px;
    border-radius: 50%;
    background: var(--text-primary);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.8rem;
    font-weight: 700;
}
.sino-popover-item-corpo {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.sino-popover-item-topo {
    display: flex;
    justify-content: space-between;
    gap: 0.4rem;
    align-items: baseline;
}
.sino-popover-item-nome {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sino-popover-item-tempo {
    font-size: 0.68rem;
    color: var(--text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}
.sino-popover-item-texto {
    font-size: 0.8rem;
    color: var(--text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.sino-popover-vazio {
    padding: 2rem 1rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.sino-popover-vazio i { font-size: 1.8rem; opacity: 0.3; display: block; margin-bottom: 0.4rem; }

.sino-popover-rodape {
    padding: 0.6rem 1rem;
    border-top: 1px solid var(--border-color);
    text-align: center;
}
.sino-popover-rodape a {
    color: var(--color-info, #0ea5e9);
    font-size: 0.82rem;
    font-weight: 600;
    text-decoration: none;
}
.sino-popover-rodape a:hover { text-decoration: underline; }

/* ========= Toast de boas-vindas ========= */
.toast-boasvindas {
    position: fixed;
    bottom: 24px;
    right: 24px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-left: 3px solid var(--color-info, #0ea5e9);
    border-radius: var(--radius);
    padding: 1rem 1.15rem;
    box-shadow: var(--shadow-lg);
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
    max-width: 360px;
    z-index: 2000;
    animation: toast-in 0.4s ease;
}
@keyframes toast-in {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
}
.toast-boasvindas.saindo { animation: toast-out 0.3s ease forwards; }
@keyframes toast-out {
    to { opacity: 0; transform: translateY(20px); }
}
.toast-boasvindas-icone {
    width: 40px; height: 40px; min-width: 40px;
    border-radius: 50%;
    background: var(--color-info, #0ea5e9);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.92rem;
    font-weight: 700;
    overflow: hidden;
    flex-shrink: 0;
}
.toast-boasvindas-icone img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.toast-boasvindas-corpo {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.toast-boasvindas-corpo strong {
    font-size: 0.9rem;
    color: var(--text-primary);
    font-weight: 700;
}
.toast-boasvindas-corpo p {
    margin: 0;
    font-size: 0.84rem;
    color: var(--text-secondary);
    line-height: 1.45;
}
.toast-boasvindas-corpo a {
    margin-top: 4px;
    color: var(--color-info, #0ea5e9);
    font-size: 0.82rem;
    font-weight: 600;
    text-decoration: none;
}
.toast-boasvindas-corpo a:hover { text-decoration: underline; }
.toast-boasvindas-fechar {
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    line-height: 1;
    flex-shrink: 0;
    transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
}
.toast-boasvindas-fechar:hover {
    background: var(--color-danger, #dc2626);
    color: #fff;
    border-color: var(--color-danger, #dc2626);
}

/* Badge do sino na topbar: mostra número real de não lidas */
.topbar-btn[data-sino] .counter.zero { display: none; }

/* ========= Toast de notificação de mensagem nova (canto superior direito) ========= */
.msg-notif-toast {
    position: fixed;
    top: calc(var(--topbar-height) + 12px);
    right: 16px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-left: 3px solid var(--color-info, #0ea5e9);
    border-radius: var(--radius);
    padding: 0.85rem 1rem;
    box-shadow: var(--shadow-lg);
    display: flex;
    gap: 0.7rem;
    align-items: flex-start;
    max-width: 360px;
    min-width: 280px;
    z-index: 1100;
    animation: msg-notif-in 0.35s ease;
}
@keyframes msg-notif-in {
    from { opacity: 0; transform: translateX(20px); }
    to   { opacity: 1; transform: translateX(0); }
}
.msg-notif-toast.saindo { animation: msg-notif-out 0.3s ease forwards; }
@keyframes msg-notif-out {
    to { opacity: 0; transform: translateX(20px); }
}
.msg-notif-icone {
    width: 34px; height: 34px; min-width: 34px;
    border-radius: 50%;
    background: var(--color-info, #0ea5e9);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.92rem;
}
.msg-notif-corpo {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}
.msg-notif-corpo strong {
    font-size: 0.86rem;
    color: var(--text-primary);
    font-weight: 700;
}
.msg-notif-corpo p {
    margin: 0;
    font-size: 0.82rem;
    color: var(--text-secondary);
    line-height: 1.45;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.msg-notif-corpo a {
    margin-top: 3px;
    color: var(--color-info, #0ea5e9);
    font-size: 0.78rem;
    font-weight: 600;
    text-decoration: none;
}
.msg-notif-corpo a:hover { text-decoration: underline; }
.msg-notif-fechar {
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    line-height: 1;
    flex-shrink: 0;
    transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
}
.msg-notif-fechar:hover {
    background: var(--color-danger, #dc2626);
    color: #fff;
    border-color: var(--color-danger, #dc2626);
}
