/* 
 * Monisys CRM - Sistema de Atendimento Multi-usuário
 * Responsividade Dinâmica (Celular, Tablet, Desktop)
 * Versão: 3.0.0 - Correção Estrutural Completa
 * Correções: sidebar mobile, chat responsivo, modais, layout principal
 */

/* ============================================
   RESET E BASE GLOBAL
   ============================================ */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    height: 100%;
    width: 100%;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    margin: 0;
    padding: 0;
}

/* ============================================
   LAYOUT PRINCIPAL (#app)
   ============================================ */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100%;
    overflow: hidden;
    position: relative;
}

@media (min-width: 1024px) {
    #app {
        flex-direction: row;
    }
}

/* ============================================
   SIDEBAR RESPONSIVA
   ============================================ */

/* Mobile: sidebar oculta por padrão, desliza da esquerda */
.sidebar-mobile {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 280px !important;
    min-width: 280px !important;
    max-width: 280px !important;
    box-shadow: 10px 0 30px -5px rgba(0, 0, 0, 0.3);
    overflow-y: auto;
    overflow-x: hidden;
}

/* Sidebar aberta no mobile */
.sidebar-mobile.active {
    transform: translateX(0);
}

/* Desktop: sidebar visível e fixa no layout */
@media (min-width: 1024px) {
    .sidebar-mobile {
        position: relative !important;
        transform: translateX(0) !important;
        width: 256px !important;
        min-width: 256px !important;
        max-width: 256px !important;
        box-shadow: none !important;
        flex-shrink: 0;
        overflow-y: auto;
        overflow-x: hidden;
    }
}

/* Itens da sidebar no mobile - garantir visibilidade */
@media (max-width: 1023px) {
    .sidebar-mobile nav {
        display: flex !important;
        flex-direction: column !important;
    }

    .sidebar-mobile nav button,
    .sidebar-mobile nav a {
        display: flex !important;
        align-items: center !important;
        padding: 12px 20px !important;
        width: 100% !important;
        text-align: left !important;
    }

    .sidebar-mobile nav button i,
    .sidebar-mobile nav a i {
        width: 24px !important;
        margin-right: 12px !important;
        text-align: center !important;
        font-size: 1.1rem !important;
        flex-shrink: 0;
    }

    /* Forçar exibição do texto dos itens no mobile */
    .sidebar-mobile nav button span,
    .sidebar-mobile nav a span {
        display: inline !important;
        font-size: 0.9rem !important;
        font-weight: 500 !important;
    }

    /* Forçar visibilidade de spans com hidden lg:block */
    .sidebar-mobile .hidden.lg\:block,
    .sidebar-mobile .hidden.lg\:inline,
    .sidebar-mobile .hidden.lg\:flex {
        display: block !important;
    }

    /* Esconder apenas o status textual online para limpar visual */
    .sidebar-mobile .online-status-text {
        display: none !important;
    }
}

/* ============================================
   OVERLAY MOBILE
   ============================================ */
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.65);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 190;
    display: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.sidebar-overlay.active {
    display: block;
    opacity: 1;
    pointer-events: all;
}

/* ============================================
   CONTEÚDO PRINCIPAL
   ============================================ */
main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0; /* Evitar overflow em flex */
    position: relative;
}

/* ============================================
   CHAT RESPONSIVO - CORREÇÃO ESTRUTURAL
   ============================================ */

/* Container da view de chats */
.chat-view-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    height: 100%;
}

/* Lista de chats (painel esquerdo) */
.chat-list-container {
    width: 320px;
    min-width: 320px;
    max-width: 320px;
    border-right: 1px solid #e2e8f0;
    background: white;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

/* Janela de conversa (painel direito) */
.chat-conversation-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #f8fafc;
    overflow: hidden;
    min-width: 0;
}

/* Mobile: layout de chat em tela cheia */
@media (max-width: 767px) {
    .chat-view-container {
        position: relative;
        min-height: 0;
        background: #fff;
    }

    .chat-list-container {
        width: 100% !important;
        min-width: 100% !important;
        max-width: 100% !important;
        border-right: none;
        position: relative;
        z-index: 1;
    }

    /* Janela de conversa: sobrepõe a lista no mobile */
    .chat-conversation-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 150;
        background: white;
        display: none;
        flex-direction: column;
        min-height: 100%;
    }

    /* Mostrar conversa quando chat está selecionado */
    .chat-conversation-container.chat-open {
        display: flex !important;
    }
}

@media (min-width: 768px) {
    .chat-conversation-container {
        position: relative;
        display: flex !important;
    }
}

/* Tablet: lista menor */
@media (min-width: 768px) and (max-width: 1023px) {
    .chat-list-container {
        width: 260px;
        min-width: 260px;
        max-width: 260px;
    }
}

/* ============================================
   BOTÃO VOLTAR NO CHAT MOBILE
   ============================================ */
.chat-back-btn {
    display: none;
}

@media (max-width: 767px) {
    .chat-back-btn {
        display: flex !important;
        align-items: center;
        gap: 8px;
        padding: 6px 12px;
        background: transparent;
        border: none;
        color: #3b82f6;
        font-weight: 700;
        font-size: 0.875rem;
        cursor: pointer;
        margin-right: 8px;
    }
}

/* ============================================
   MODAIS - CORREÇÃO ESTRUTURAL
   ============================================ */

/* Overlay de modal */
.modal-overlay,
.fixed.inset-0.items-center.justify-center:not(.hidden):not([style*="display:none"]):not([style*="display: none"]),
[class*="fixed"][class*="inset-0"][class*="items-center"][class*="justify-center"]:not(.hidden):not([style*="display:none"]):not([style*="display: none"]) {
    position: fixed !important;
    inset: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: 16px !important;
    z-index: 10000 !important;
    overflow-y: auto !important;
}

.hidden {
    display: none !important;
}

#whatsappConfigModal[style*="display:none"],
#whatsappConfigModal[style*="display: none"],
#integrationsConfigModal[style*="display:none"],
#integrationsConfigModal[style*="display: none"] {
    display: none !important;
}

/* Container do modal */
.modal-container {
    width: 100% !important;
    max-width: 560px !important;
    max-height: calc(100vh - 32px) !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    margin: auto !important;
    border-radius: 1.5rem !important;
    animation: modalFadeIn 0.25s ease-out !important;
    position: relative !important;
    flex-shrink: 0;
}

/* Modais maiores */
.modal-container.modal-lg {
    max-width: 720px !important;
}

/* Modais menores */
.modal-container.modal-sm {
    max-width: 420px !important;
}

@media (max-width: 640px) {
    .modal-container {
        max-width: 100% !important;
        max-height: calc(100vh - 24px) !important;
        border-radius: 1.25rem !important;
        margin: 12px !important;
        width: calc(100% - 24px) !important;
    }

    .modal-content {
        padding: 1rem !important;
    }

    /* Botões de ação em modais - empilhar no mobile */
    .modal-footer-actions {
        flex-direction: column !important;
    }

    .modal-footer-actions button {
        width: 100% !important;
    }
}

/* Scroll interno em modais com conteúdo longo */
.modal-body-scroll {
    overflow-y: auto;
    max-height: 60vh;
    -webkit-overflow-scrolling: touch;
}

/* ============================================
   GRIDS RESPONSIVOS
   ============================================ */
.responsive-grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: repeat(1, minmax(0, 1fr));
}

@media (min-width: 640px) {
    .responsive-grid-2 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (min-width: 1024px) {
    .responsive-grid-4 {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}

/* ============================================
   TABELAS RESPONSIVAS
   ============================================ */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ============================================
   TIPOGRAFIA E INPUTS
   ============================================ */
@media (max-width: 640px) {
    h1 { font-size: 1.5rem !important; }
    h2 { font-size: 1.25rem !important; }

    /* Prevenir zoom em inputs no iOS */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ============================================
   BOTÃO HAMBURGER - DENTRO DA SIDEBAR (CORREÇÃO 5)
   ============================================ */
/* O hamburguer foi movido para dentro da sidebar no lado esquerdo do header.
   O estilo FAB flutuante foi removido. */
.hamburger-fab {
    display: none !important;
}

/* ============================================
   GESTÃO DE ESTOQUE - AJUSTES MOBILE
   ============================================ */
@media (max-width: 640px) {
    .stock-management-view .table-responsive table {
        min-width: 760px;
    }

    .stock-management-view .table-responsive th,
    .stock-management-view .table-responsive td {
        white-space: nowrap;
    }
}

/* ============================================
   DASHBOARD - CARDS RESPONSIVOS
   ============================================ */
@media (max-width: 640px) {
    .dashboard-container {
        padding: 1rem !important;
    }

    /* Cards de métricas em 2 colunas no mobile */
    .metrics-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }
}

/* ============================================
   HEADER DO CHAT - BOTÕES RESPONSIVOS
   ============================================ */
@media (max-width: 640px) {
    /* Botões de ação do chat em scroll horizontal */
    .chat-action-buttons {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        gap: 6px;
        padding-bottom: 4px;
        -webkit-overflow-scrolling: touch;
    }

    .chat-action-buttons button {
        flex-shrink: 0;
        white-space: nowrap;
    }

    /* Input de mensagem sempre visível */
    .message-input-container {
        position: sticky;
        bottom: 0;
        background: white;
        z-index: 10;
        padding: 12px !important;
    }
    
    /* Regras do modal de chat (JS puro) movidas para seção dedicada abaixo */
}

/* ============================================
   WHATSAPP CLOUD SETUP - RESPONSIVO
   ============================================ */
@media (max-width: 768px) {
    .main-grid {
        grid-template-columns: 1fr !important;
    }
    .card {
        padding: 20px;
    }
}

@media (max-width: 480px) {
    .button-group {
        flex-direction: column;
    }
    .button-group button {
        width: 100%;
    }
}

/* ============================================
   SCROLL SUAVE GLOBAL
   ============================================ */
.overflow-y-auto {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

/* ============================================
   PREVENIR SCROLL HORIZONTAL GLOBAL
   ============================================ */
@media (max-width: 1023px) {
    body {
        overflow-x: hidden !important;
    }

    #app > * {
        max-width: 100vw;
    }
}

/* ============================================
   ANIMAÇÕES
   ============================================ */
@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(-10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

@keyframes slideInFromLeft {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideInFromBottom {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ============================================
   CORREÇÃO DE RESPONSIVIDADE E CLIQUES (Manus)
   ============================================ */

/* Garantir que botões na sidebar sejam clicáveis e não sobrepostos */
.sidebar-mobile nav button, 
.sidebar-mobile nav a {
    position: relative !important;
    z-index: 10 !important;
    cursor: pointer !important;
    pointer-events: auto !important;
    -webkit-tap-highlight-color: transparent;
}

/* Corrigir sobreposição de modais em mobile */
@media (max-width: 1023px) {
    .fixed.inset-0 {
        z-index: 9999 !important;
    }
    
    /* Garantir que o overlay do menu não bloqueie modais */
    .sidebar-overlay.active {
        z-index: 180 !important;
    }
    
    .sidebar-mobile.active {
        z-index: 200 !important;
    }
}

/* Ajuste para botões de fechar modais no mobile */
.modal-container button {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ============================================
   MELHORIAS PARA TABLETS E IPADS (768px - 1024px)
   ============================================ */
@media (min-width: 768px) and (max-width: 1024px) {
    /* Ajustar o grid do dashboard para 2 colunas em vez de 4 ou 1 */
    .grid-cols-1.sm\:grid-cols-2.lg\:grid-cols-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    /* Aumentar o tamanho dos modais em tablets */
    .modal-container {
        max-width: 90% !important;
        width: 90% !important;
    }

    /* Ajustar o sidebar para não ocupar tanto espaço */
    .sidebar-mobile {
        width: 240px !important;
    }

    /* Melhorar o espaçamento das tabelas */
    .p-6 {
        padding: 1.25rem !important;
    }

    /* Ajustar o tamanho das fontes para melhor leitura */
    .text-3xl {
        font-size: 1.5rem !important;
        line-height: 2rem !important;
    }
}

/* ============================================
   AJUSTES FINOS PARA TELA DE LOGIN (MOBILE)
   ============================================ */
@media (max-width: 480px) {
    .bg-white.p-6.sm\:p-10 {
        padding: 1.5rem !important;
        border-radius: 1.5rem !important;
    }

    h1.text-2xl {
        font-size: 1.5rem !important;
    }

    .w-16.h-16 {
        width: 3.5rem !important;
        height: 3.5rem !important;
    }

    input, button {
        font-size: 0.875rem !important;
        padding-top: 0.75rem !important;
        padding-bottom: 0.75rem !important;
    }
}

/* ============================================
   FIX PARA O MODAL DE CONVERSAS (MOBILE/TABLET)
   ============================================ */
@media (max-width: 1024px) {
    /* Regras de tamanho do modal movidas para seção CORREÇÃO DEFINITIVA v7 abaixo */
    #chatsModalList {
        width: 100% !important;
        max-width: 100% !important;
    }

    /* Quando o chat está aberto no mobile, a lista some */
    #chatsModalChat[style*="display: flex"] ~ #chatsModalList,
    #chatsModalChat[style*="display: flex"] + #chatsModalList {
        display: none !important;
    }
}

/* ============================================
   CORREÇÃO DEFINITIVA v7: MODAL DE CONVERSAS - INPUT MOBILE
   Problema: inputContainer era comprimido pelo flex layout em mobile
   Solução: flex-shrink-0, min-height:0 no msgContainer, sticky bottom
   ============================================ */

/* Garantir que o chatPanel seja um flex container coluna correto */
#chatsModalChat {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

/* Container de mensagens: flex:1 com min-height:0 para não bloquear o input */
#chatsModalMessages {
    flex: 1 1 0% !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
}

/* CORREÇÃO CRÍTICA: Input area NÃO pode ser comprimido */
#chatsModalInputArea {
    flex-shrink: 0 !important;
    flex-grow: 0 !important;
    position: sticky !important;
    bottom: 0 !important;
    z-index: 20 !important;
    background: white !important;
    display: block !important;
}

/* Form dentro do input area */
#chatsModalForm {
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
}

/* Input de texto: prevenir zoom no iOS e garantir visibilidade */
#chatsModalInput {
    font-size: 16px !important; /* Previne zoom automático no iOS */
    display: block !important;
    pointer-events: auto !important;
    visibility: visible !important;
    opacity: 1 !important;
    -webkit-user-select: text !important;
    user-select: text !important;
}

/* Mobile específico: garantir que o modal use altura dinâmica */
@media (max-width: 1023px) {
    #chatsModal > div {
        height: 95dvh !important; /* dvh = dynamic viewport height (considera teclado virtual) */
        max-height: 95dvh !important;
        width: 98% !important;
        margin: auto !important;
        display: flex !important;
        flex-direction: column !important;
    }

    /* Container flex do modal */
    #chatsModal > div > .flex.flex-1 {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    /* ChatPanel em mobile: ocupa 100% */
    #chatsModalChat {
        width: 100% !important;
        height: 100% !important;
        min-height: 0 !important;
        flex: 1 1 0% !important;
    }

    /* Garantir que o inputArea seja sempre visível no mobile */
    #chatsModalInputArea {
        min-height: 60px !important;
        padding: 10px 12px !important;
    }

    /* Botão de envio: tamanho mínimo para toque */
    #chatsModalForm button[type="submit"] {
        min-width: 44px !important;
        min-height: 44px !important;
        width: 44px !important;
        height: 44px !important;
    }

    /* Input: altura mínima para toque confortável */
    #chatsModalInput {
        min-height: 44px !important;
        padding: 10px 16px !important;
    }
}

/* Fallback para navegadores que não suportam dvh */
@supports not (height: 100dvh) {
    @media (max-width: 1023px) {
        #chatsModal > div {
            height: 95vh !important;
            max-height: 95vh !important;
        }
    }
}
