/* =======================================================
   1. RESET E ESTRUTURA BLINDADA (Previne bugs visuais)
   ======================================================= */
/* Otimização de reflow: Limita bounding box de imagens dinâmicas */
img { max-width: 100%; display: block; }

/* Container Principal do Chat */
.active-chat-view {
    display: flex; flex-direction: column;
    height: 100%; width: 100%;
    overflow: hidden; position: relative;
    background: #fff;
}

/* =======================================================
   2. SIDEBAR (Lista de Conversas)
   ======================================================= */
.chat-sidebar-header { 
    height: 70px; flex-shrink: 0; display: flex; align-items: center; justify-content: space-between; padding: 0 20px; 
}
.new-chat-btn { width: 36px; height: 36px; border-radius: 50%; border: none; background: #f0f2f5; cursor: pointer; color: #333; display: flex; align-items: center; justify-content: center; transition: 0.2s; }
.new-chat-btn:hover { background: #e4e6eb; }

.chat-search-bar { padding: 0 20px 10px; border-bottom: 1px solid #f0f2f5; }
.chat-search-bar h4 { margin: 0; font-family: 'Montserrat', sans-serif; font-size: 1.2rem; font-weight: 800; color: #1c1e21; }

.conversations-list { flex: 1; overflow-y: auto; padding: 5px 10px; }

.conversation-item { display: flex; align-items: center; gap: 12px; padding: 10px; cursor: pointer; border-radius: 12px; transition: 0.2s; }
.conversation-item:hover { background: #f5f5f5; }
.conversation-item.active { background: #eef9ee; }

/* Strict width/height clamps para evitar Cumulative Layout Shift (CLS) */
.conv-avatar { 
    width: 48px !important; 
    height: 48px !important; 
    min-width: 48px;
    border-radius: 50%; 
    object-fit: cover; 
    border: 1px solid #eee;
}

.conv-info { flex: 1; overflow: hidden; }
.conv-name { font-weight: 700; font-size: 0.95rem; color: #333; }
.conv-last-msg { font-size: 0.85rem; color: #777; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* =======================================================
   3. HEADER DO CHAT (Topo)
   ======================================================= */
.chat-header { 
    height: 70px; flex-shrink: 0; 
    display: flex; align-items: center; justify-content: space-between; 
    padding: 0 15px; border-bottom: 1px solid #eee; background: #fff; z-index: 20; 
}

.user-details { display: flex; align-items: center; gap: 10px; }

.avatar-container { position: relative; width: 40px; height: 40px; flex-shrink: 0; }
.header-avatar { 
    width: 40px !important; 
    height: 40px !important; 
    border-radius: 50%; object-fit: cover; 
}
.status-dot { width: 10px; height: 10px; border-radius: 50%; border: 2px solid #fff; position: absolute; bottom: 0; right: 0; }
.status-dot.online { background: #4caf50; }
.status-dot.offline { background: #ccc; }

.header-info h4 { margin: 0; font-size: 1rem; font-weight: 700; }
.header-info span { font-size: 0.75rem; color: #888; }
.action-btn { background: none; border: none; font-size: 1.1rem; color: #53954a; cursor: pointer; padding: 8px; border-radius: 50%; transition: 0.2s; }
.action-btn:hover { background: #f0f2f5; }
.btn-back-mobile { display: none; background: none; border: none; font-size: 1.2rem; cursor: pointer; margin-right: 5px; }
@media(max-width: 768px) { .btn-back-mobile { display: block; } }

/* =======================================================
   4. ÁREA DE MENSAGENS
   ======================================================= */
.messages-area { 
    flex: 1; overflow-y: auto; overflow-x: hidden; 
    padding: 20px; display: flex; flex-direction: column; gap: 8px;
    background-image: radial-gradient(#eee 1px, transparent 1px); background-size: 20px 20px;
}

.message-row { display: flex; width: 100%; }
.message-row.mine { justify-content: flex-end; }
.message-row.theirs { justify-content: flex-start; }

.message-bubble { 
    max-width: 75%; padding: 10px 14px; border-radius: 18px; 
    font-size: 0.95rem; line-height: 1.4; position: relative; 
    word-wrap: break-word; 
}

/* Herança de UI State para balões padrão e GIFs encapsulados.
   Delega o color-mapping para as classes root (.mine / .theirs) */
.mine .message-bubble:not(.no-bubble) {
    background: linear-gradient(135deg, #53954a, #3e7a35);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.theirs .message-bubble:not(.no-bubble) {
    background: #f0f2f5;
    color: #1c1e21;
    border-bottom-left-radius: 4px;
}

/* Transparência aplicada exclusivamente em imagens estáticas via .no-bubble (Separation of Concerns) */
.image-message,
.image-message .message-bubble {
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
    padding: 0 !important;
    color: inherit !important;
}

/* Modifier para encapsulamento de mídia animada (GIFs) dentro do balão de status */
.media-gif-bubble {
    padding: 6px 6px 4px 6px !important;
    border-radius: 16px;
}

.media-gif-bubble img {
    border-radius: 12px;
    display: block;
    margin-bottom: 2px;
}

.media-message-bubble {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    max-width: 220px;
    width: fit-content;
}

.theirs .media-message-bubble {
    align-items: flex-start;
}

.media-message-bubble .msg-time {
    display: block;
    margin-top: 4px;
    line-height: 1;
}

.theirs .media-message-bubble .msg-time {
    text-align: left;
}

/* Override de limites de renderização para imagens puras */
.message-bubble img { 
    max-width: 100% !important; height: auto; 
    border-radius: 12px; margin-bottom: 5px; 
}

/* Wrapper de GIF refatorado para preservar o flex-flow da classe pai (.message-row) */
.gif-message {
    margin-bottom: 8px;
}

.gif-message img {
    max-width: 220px; /* Bounding box clamp para estabilidade visual */
    width: 100%;
    height: auto;
}

.gif-message .msg-time {
    display: block;
    width: auto;
    margin-top: 4px;
    opacity: 0.9;
}

.msg-time { display: block; font-size: 0.65rem; text-align: right; margin-top: 4px; opacity: 0.8; }

/* =======================================================
   5. MENU DE ANEXOS (FLUTUANTE & BONITO)
   ======================================================= */
.attachment-menu {
    position: absolute;
    bottom: 75px;
    right: 20px;
    background: #fff;
    padding: 15px;
    border-radius: 16px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.15);
    display: grid; grid-template-columns: 1fr 1fr; gap: 15px;
    z-index: 100;
    width: 200px;
    border: 1px solid #f0f0f0;
    
    transform-origin: bottom right;
    animation: popUp 0.2s cubic-bezier(0.17, 0.67, 0.83, 0.67);
}
@keyframes popUp { from { opacity: 0; transform: scale(0.8) translateY(10px); } to { opacity: 1; transform: scale(1) translateY(0); } }

.attachment-menu.hidden { display: none !important; }

.attach-item { display: flex; flex-direction: column; align-items: center; gap: 5px; cursor: pointer; transition: 0.2s; }
.attach-item:hover { transform: translateY(-3px); }

.attach-icon { 
    width: 45px; height: 45px; border-radius: 50%; 
    display: flex; align-items: center; justify-content: center; 
    color: #fff; font-size: 1.2rem;
}
.color-gallery { background: linear-gradient(135deg, #9C27B0, #BA68C8); }
.color-camera { background: linear-gradient(135deg, #F44336, #E57373); }
.color-gif { background: linear-gradient(135deg, #2196F3, #64B5F6); }
.color-sticker { background: linear-gradient(135deg, #FF9800, #FFB74D); }

.attach-item span { font-size: 0.75rem; font-weight: 600; color: #555; }

/* =======================================================
   6. BARRA DE INPUT
   ======================================================= */
.chat-input-area { 
    flex-shrink: 0; background: #fff; padding: 10px 15px 15px; 
    border-top: 1px solid #eee; position: relative; z-index: 50; 
}
.input-wrapper { 
    background: #f0f2f5; border-radius: 24px; padding: 8px 15px; 
    display: flex; align-items: center; gap: 10px; 
}
.input-wrapper input { flex: 1; border: none; background: transparent; outline: none; font-size: 1rem; }
.btn-input-icon { border: none; background: none; font-size: 1.3rem; color: #888; cursor: pointer; padding: 5px; }
.btn-input-icon:hover { color: #53954a; }
.btn-send-text { border: none; background: #53954a; color: #fff; padding: 8px 16px; border-radius: 20px; font-weight: 700; cursor: pointer; }
.btn-send-text.hidden { display: none; }
.btn-send-text:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    background: #53954a;
    color: #fff;
}
.right-actions { display: flex; align-items: center; gap: 5px; }

.reply-preview { display: flex; justify-content: space-between; align-items: center; background: #f0f2f5; padding: 8px 15px; margin-bottom: 5px; border-radius: 8px; border-left: 4px solid #53954a; }
.reply-info { display: flex; flex-direction: column; font-size: 0.85rem; }
.reply-label { font-weight: bold; color: #53954a; }
.btn-close-reply { background: none; border: none; cursor: pointer; color: #666; }

/* =======================================================
   7. MODAIS
   ======================================================= */
.modal-overlay { 
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
    background: rgba(0,0,0,0.6); backdrop-filter: blur(4px); 
    z-index: 2000; display: none !important; justify-content: center; align-items: center; 
}
.modal-overlay.open { display: flex !important; }

.modal-content-new-chat, .modal-content-media { 
    background: #fff; width: 90%; max-width: 450px; height: 80%; border-radius: 20px; 
    display: flex; flex-direction: column; overflow: hidden; 
}
.modal-header-nc { display: flex; align-items: center; justify-content: space-between; padding: 15px; border-bottom: 1px solid #eee; }
.modal-header-nc h3 { margin: 0; font-family: 'Montserrat', sans-serif; }
.btn-close-nc { background: none; border: none; font-size: 1.2rem; cursor: pointer; }
.modal-search-nc { padding: 10px 15px; display: flex; align-items: center; gap: 10px; background: #f9f9f9; border-bottom: 1px solid #eee; }
.modal-search-nc input { border: none; background: transparent; outline: none; flex: 1; padding: 5px; }
.modal-results-nc, .media-grid { flex: 1; overflow-y: auto; padding: 10px; }
.user-result-item { display: flex; gap: 10px; padding: 12px; cursor: pointer; border-bottom: 1px solid #eee; align-items: center; transition: 0.2s; }
.user-result-item:hover { background: #f5f5f5; }
.user-result-item img { width: 40px; height: 40px; border-radius: 50%; }

/* Tema verde aplicado apenas ao modal da área de conversas (Nova mensagem). */
.modal-content-new-chat--green {
    border: 1px solid #d9e9d5;
    box-shadow: 0 20px 45px rgba(62, 122, 53, 0.22);
}

.modal-content-new-chat--green .modal-header-nc {
    background: linear-gradient(135deg, #53954a 0%, #3e7a35 100%);
    border-bottom: 0;
}

.modal-content-new-chat--green .modal-header-nc h3,
.modal-content-new-chat--green .btn-close-nc {
    color: #fff;
}

.modal-content-new-chat--green .modal-search-nc {
    background: #f2f8f1;
    border-bottom: 1px solid #d9e9d5;
}

.modal-content-new-chat--green .modal-search-nc span {
    color: #2f6a2a;
    font-weight: 700;
}

.modal-content-new-chat--green .modal-search-nc input {
    color: #2f6a2a;
}

.modal-content-new-chat--green .modal-search-nc input::placeholder {
    color: #6f8a6d;
}

.modal-content-new-chat--green .modal-results-nc {
    background: #fbfefb;
}

.modal-content-new-chat--green .user-result-item:hover {
    background: #eef9ee;
}

.media-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 8px;
}

.media-grid .media-item {
    width: 100%;
    height: 100px;
    border-radius: 8px;
    overflow: hidden;
    background: #edf6ec;
}

.media-grid .media-item img,
.media-grid img.media-item {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    border-radius: 8px;
}

/* =======================================================
   8. EMPTY STATE REDESIGN (Sênior UI)
   ======================================================= */
.empty-chat-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    background-color: #fcfcfc;
    padding: 30px;
    text-align: center;
    position: relative;
    background-image: 
        radial-gradient(#e1e8e1 1.5px, transparent 1.5px), 
        radial-gradient(#e1e8e1 1.5px, transparent 1.5px);
    background-size: 30px 30px;
    background-position: 0 0, 15px 15px;
}

.empty-art-container {
    position: relative;
    width: 140px;
    height: 140px;
    margin-bottom: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.empty-icon-circle {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #53954a, #45823d);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.8rem;
    color: #fff;
    box-shadow: 0 10px 25px rgba(83, 149, 74, 0.3);
    z-index: 2;
    position: relative;
}

.pulsing-circle {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    width: 100%; height: 100%;
    border-radius: 50%;
    border: 2px solid rgba(83, 149, 74, 0.3);
    animation: pulseWave 3s infinite;
    z-index: 1;
}
.pulsing-circle.delay-1 { animation-delay: 0s; }
.pulsing-circle.delay-2 { animation-delay: 1.5s; }

@keyframes pulseWave {
    0% { width: 100%; height: 100%; opacity: 1; }
    100% { width: 220%; height: 220%; opacity: 0; }
}

.empty-content-text { max-width: 400px; margin-bottom: 35px; }

.empty-chat-state h2 {
    font-family: 'Montserrat', sans-serif;
    font-size: 1.8rem;
    font-weight: 800;
    color: #2c3e50;
    margin: 0 0 12px 0;
}

.empty-chat-state p {
    font-family: 'Lato', sans-serif;
    font-size: 1rem;
    color: #7f8c8d;
    line-height: 1.6;
    margin: 0;
}

.btn-send-msg-primary {
    background: linear-gradient(90deg, #53954a, #3e7a35);
    color: #fff;
    border: none;
    padding: 14px 32px;
    border-radius: 50px;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(83, 149, 74, 0.3);
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-send-msg-primary:hover {
    transform: translateY(-3px) scale(1.02);
    box-shadow: 0 10px 25px rgba(83, 149, 74, 0.4);
}

.btn-send-msg-primary:active { transform: translateY(1px); }

.encrypted-hint {
    position: absolute;
    bottom: 30px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #bdc3c7;
    font-size: 0.8rem;
    font-weight: 600;
}
.encrypted-hint i { font-size: 0.7rem; }

.hidden { display: none !important; }