/* ==========================================
   1. VARIÁVEIS DE CORES E DESIGN FLUIDO
   ========================================== */
:root {
    --whatsapp-primary: #0d6efd;
    --whatsapp-dark: #0a4fb0;
    --bg-app: #f4f6f9;
    --bg-chat-panel: #f0f3f7;
    --bg-message-me: #dbe9ff;
    --bg-message-them: #ffffff;
    --text-main: #222;
    --text-sec: #667;
    --border-color: #dde3ea;
    --modal-overlay: rgba(16, 24, 40, 0.4);
    
    /* Dimensionamento fluido automático baseado na tela */
    --sidebar-width: clamp(320px, 25vw, 420px);
    --chat-padding: clamp(10px, 4vw, 80px);
}

/* ==========================================
   2. RESET E ESTRUTURA GLOBAL
   ========================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body, html {
    height: 100%;
    width: 100%;
    font-family: 'Segoe UI', Helvetica, Arial, sans-serif;
    background-color: var(--bg-app);
    color: var(--text-main);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Scrollbar moderna */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #bcc0c4; border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: #a6abb0; }

/* ==========================================
   3. TELA DE LOGIN (MOLDADA PRO SEU CARD)
   ========================================== */
#loginScreen.overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100vw; height: 100dvh;
    background: linear-gradient(135deg, var(--whatsapp-primary) 0%, var(--whatsapp-dark) 100%);
    display: flex; align-items: center; justify-content: center;
    z-index: 9999;
    padding: 20px;
}

.login-card {
    background: #ffffff;
    padding: clamp(25px, 5vw, 40px);
    border-radius: 16px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    width: 100%;
    max-width: 420px;
}

.login-header { text-align: center; margin-bottom: 25px; }
.login-header i { font-size: 40px; color: var(--whatsapp-dark); margin-bottom: 10px; }
.login-header h2 { font-size: 24px; color: #333; }
.login-header p { color: var(--text-sec); font-size: 14px; }

.input-group {
    display: flex; flex-direction: column; gap: 6px; margin-bottom: 15px; text-align: left;
}
.input-group label { font-size: 14px; font-weight: 600; color: #444; }
.input-group input, .input-group select {
    width: 100%; padding: 12px; border: 1px solid var(--border-color); border-radius: 8px; font-size: 15px; outline: none;
}
.input-group input:focus { border-color: var(--whatsapp-primary); }

.btn-login {
    width: 100%; padding: 14px; background-color: var(--whatsapp-primary); color: white;
    border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; transition: 0.2s;
}
.btn-login:hover { background-color: var(--whatsapp-dark); }

/* ==========================================
   4. CONTAINER PRINCIPAL DO APP
   ========================================== */
.app-container {
    display: flex; /* Quando o JS remove o display: none, assume flex automaticamente */
    height: 100dvh;
    width: 100vw;
    background-color: #ffffff;
    overflow: hidden;
    position: relative;
}

/* ==========================================
   5. SIDEBAR FLUIDA
   ========================================== */
.sidebar {
    flex: 0 0 var(--sidebar-width);
    background-color: #ffffff;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    z-index: 20;
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.sidebar-header {
    padding: 14px 16px; background: var(--bg-app);
    border-bottom: 1px solid var(--border-color);
    display: flex; align-items: center; justify-content: space-between; flex-shrink: 0;
}
.sidebar-header h3 { font-size: 18px; font-weight: 600; }

.header-actions { display: flex; gap: 8px; }
.header-actions button {
    background: none; border: none; font-size: 18px; color: var(--text-sec); cursor: pointer; padding: 6px; border-radius: 50%;
}
.header-actions button:hover { background: rgba(0,0,0,0.05); color: var(--text-main); }

.sidebar-search { padding: 10px 16px; background: #fff; border-bottom: 1px solid #f0f2f5; }
.sidebar-search select {
    width: 100%; padding: 8px 12px; border: 1px solid var(--border-color); border-radius: 6px; background-color: var(--bg-app); outline: none;
}

/* Lista de contatos */
.chat-list { flex: 1; overflow-y: auto; list-style: none; }
.chat-list li {
    padding: 12px 16px; cursor: pointer; border-bottom: 1px solid #f9f9f9;
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.chat-list li:hover { background-color: #f5f6f6; }
.chat-list li.active { background-color: var(--bg-app); }

/* ==========================================
   6. ÁREA DE CONVERSA DINÂMICA
   ========================================== */
.chat-area {
    flex: 1; min-width: 0; display: flex; flex-direction: column; background-color: var(--bg-chat-panel); height: 100%;
}

.chat-header {
    height: 60px; background-color: var(--bg-app); padding: 0 16px;
    display: flex; align-items: center; justify-content: space-between;
    border-bottom: 1px solid var(--border-color); flex-shrink: 0; z-index: 10;
}
.contact-info h2 { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.btn-finish {
    background: #dc3545; color: white; border: none; padding: 8px 14px; border-radius: 6px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 6px; transition: 0.2s;
}
.btn-finish:hover { background: #c82333; }

/* Mensagens */
.messages-container {
    flex: 1; overflow-y: auto; padding: 20px var(--chat-padding); display: flex; flex-direction: column; gap: 8px;
}

/* Telas de Boas-vindas */
.welcome-screen { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; color: var(--text-sec); padding: 20px; }
.welcome-icon i { font-size: 60px; color: var(--whatsapp-primary); margin-bottom: 15px; }
.welcome-screen h2 { margin-bottom: 8px; color: var(--text-main); font-weight: 400; font-size: clamp(20px, 4vw, 28px); }

/* Bolhas das mensagens */
.message {
    padding: 8px 12px; border-radius: 8px; width: max-content; max-width: clamp(70%, 50vw, 85%);
    font-size: 14.5px; line-height: 1.4; word-wrap: break-word; box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13); display: flex; flex-direction: column;
}
.me { align-self: flex-end; background-color: var(--bg-message-me); border-top-right-radius: 0; }
.them { align-self: flex-start; background-color: var(--bg-message-them); border-top-left-radius: 0; }

.message img, .message video { max-width: 100%; height: auto; border-radius: 6px; margin-top: 4px; }
.message audio { width: clamp(180px, 45vw, 260px); height: 40px; margin-top: 4px; outline: none; }

/* ==========================================
   7. CHAT FOOTER (AVIAOZINHO TRAVADO NA DIREITA)
   ========================================== */
.chat-footer {
    min-height: 62px; 
    background-color: var(--bg-app); 
    padding: 10px 16px;
    display: flex !important; 
    flex-direction: row !important; 
    align-items: center !important; 
    gap: 12px; 
    flex-shrink: 0; 
    border-top: 1px solid var(--border-color);
}

/* O input ganha flex: 1 para crescer e empurrar o botão totalmente para o lado */
#messageInput {
    flex: 1 !important;
    min-width: 0 !important;
    padding: 12px 16px; 
    border-radius: 8px; 
    border: none; 
    outline: none;
    font-size: 15px; 
    background-color: #ffffff; 
    box-shadow: 0 1px 1px rgba(0,0,0,0.05);
}

/* Estilização perfeita do aviãozinho na direita */
.btn-send {
    background: var(--whatsapp-primary) !important; 
    color: white !important; 
    border: none !important;
    width: 42px !important; 
    height: 42px !important; 
    border-radius: 50% !important; 
    display: flex !important; 
    align-items: center !important; 
    justify-content: center !important; 
    cursor: pointer !important; 
    flex-shrink: 0 !important;
    transition: background 0.2s, transform 0.1s !important;
}
.btn-send:hover { background: var(--whatsapp-dark) !important; }
.btn-send:active { transform: scale(0.95) !important; }

/* ==========================================
   8. MODAIS (GERAL, CONFIG E NOVO CHAT)
   ========================================== */
.modal {
    z-index: 9999; background: var(--modal-overlay); display: none;
    align-items: center; justify-content: center; position: fixed;
    top: 0; left: 0; width: 100%; height: 100%; backdrop-filter: blur(2px); padding: 15px;
}
/* Força o display flex quando os modais forem ativados por JS */
.modal[style*="display: block"], .modal[style*="display: flex"] {
    display: flex !important;
}

.modal-content {
    background: white; padding: clamp(20px, 4vw, 30px); border-radius: 12px;
    width: 100%; max-width: 500px; max-height: 85vh; overflow-y: auto;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}

.modal-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #eee; padding-bottom: 12px; margin-bottom: 15px; }
.btn-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--text-sec); }

/* Abas internas */
.tabs { display: flex; gap: 8px; margin-bottom: 15px; }
.tabs button { flex: 1; padding: 10px; border: 1px solid var(--border-color); border-radius: 8px; cursor: pointer; font-weight: 600; background: #f5f5f5; }

.config-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.config-group label { font-size: 14px; font-weight: 600; }
.config-group input, .config-group textarea { width: 100%; padding: 10px; border: 1px solid var(--border-color); border-radius: 8px; outline: none; }
.config-group textarea { height: 80px; resize: none; }

/* Botões de Ação do Modal */
.modal-content .btn-save, .modal-content .btn-confirm { width: 100%; padding: 12px; background: var(--whatsapp-primary); color: white; border: none; border-radius: 8px; font-weight: bold; cursor: pointer; }
.modal-actions { display: flex; gap: 10px; margin-top: 15px; }
.modal-actions button { flex: 1; padding: 12px; border-radius: 8px; border: none; font-weight: bold; cursor: pointer; }
.modal-actions .btn-cancel { background: #f0f2f5; color: var(--text-sec); }

/* Switch Slider */
.switch-label { display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 600; }

/* ==========================================
   9. RESPONSIVIDADE EXCLUSIVA (CELULAR)
   ========================================== */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        top: 0; left: 0;
        width: 100%; height: 100%;
        max-width: 100%;
        z-index: 1000;
    }

    /* Esconde a lista lateral empurrando suavemente para a esquerda */
    .sidebar.hidden {
        transform: translateX(-100%);
        pointer-events: none;
    }

    .chat-area { width: 100%; }
    #btnBackMobile { display: block !important; background: none; border: none; font-size: 18px; color: var(--text-sec); cursor: pointer; margin-right: 8px; }
    .message { max-width: 88%; }
    .message audio { width: 200px; }
    .chat-header { padding: 0 10px; }
}