/* ===============================================
   CRM BUG FIXES & MELHORIAS UX
   Aplicar DEPOIS de todos os outros estilos
   =============================================== */

/* ===== FIX 1: GRID DE MESES - 3 LINHAS DE 4 NO MOBILE ===== */
@media (max-width: 768px) {
    .months-grid,
    .months-container {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr) !important; /* 4 colunas */
        grid-template-rows: repeat(3, auto) !important; /* 3 linhas */
        gap: 8px !important;
        margin-top: 15px !important;
        padding: 0 !important;
        max-width: 100% !important;
        overflow: visible !important;
    }
    
    .month-item {
        width: 100% !important;
        height: auto !important;
        min-height: 48px !important;
        padding: 8px 4px !important;
        font-size: 11px !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 4px !important;
        border-radius: 8px !important;
        text-align: center !important;
        overflow: hidden !important;
    }
    
    .month-icon {
        font-size: 16px !important;
        line-height: 1 !important;
    }
    
    .month-name {
        font-size: 10px !important;
        font-weight: 600 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        max-width: 100% !important;
    }
}

/* Desktop: 4 linhas de 3 */
@media (min-width: 769px) and (max-width: 1200px) {
    .months-grid,
    .months-container {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important; /* 3 colunas */
        grid-template-rows: repeat(4, auto) !important; /* 4 linhas */
        gap: 12px !important;
    }
}

/* Desktop grande: 2 linhas de 6 */
@media (min-width: 1201px) {
    .months-grid,
    .months-container {
        display: grid !important;
        grid-template-columns: repeat(6, 1fr) !important;
        grid-template-rows: repeat(2, auto) !important;
        gap: 15px !important;
    }
}

/* ===== FIX 2: REMOVER BOTÃO PRETO ESTRANHO NO MOBILE ===== */
@media (max-width: 768px) {
    /* Esconder botões sem texto ou ícone visível */
    button:empty,
    .btn:empty,
    button[style*="background: black"],
    button[style*="background:black"],
    button[style*="background:#000"],
    .mystery-button,
    button:not([class]):not([id]) {
        display: none !important;
    }
    
    /* Esconder seta estranha no meio da tela */
    button > svg:only-child:not([aria-label]) {
        display: none !important;
    }
    
    /* Verificar floating buttons sem propósito */
    .floating-btn:empty,
    .fab:empty {
        display: none !important;
    }
}

/* ===== FIX 3: BOTÕES DO PIPELINE - NÃO DUPLICAR ===== */
.pipeline-card-actions {
    display: flex !important;
    gap: 8px !important;
    opacity: 1 !important;
    visibility: visible !important;
    margin-top: 12px !important;
    padding-top: 12px !important;
    border-top: 1px solid #2a2a2a !important;
    flex-wrap: wrap !important;
}

/* Remover duplicatas via CSS (backup caso JS falhe) */
.pipeline-card-actions .icon-btn:nth-child(n+5) {
    display: none !important;
}

/* Mobile: botões em linha única */
@media (max-width: 768px) {
    .pipeline-card-actions {
        width: 100% !important;
    }
    
    .pipeline-card-actions .icon-btn {
        flex: 1 !important;
        min-width: 60px !important;
        min-height: 44px !important;
        padding: 10px 8px !important;
        font-size: 13px !important;
        background: #2a2a2a !important;
        border: 1px solid #3a3a3a !important;
        border-radius: 8px !important;
        display: inline-flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 4px !important;
        transition: all 0.2s !important;
    }
    
    .pipeline-card-actions .icon-btn:active {
        transform: scale(0.95) !important;
        background: #3a3a3a !important;
    }
    
    /* Garantir máximo de 4 botões */
    .pipeline-card-actions .icon-btn:nth-child(1) { order: 1; } /* Voltar */
    .pipeline-card-actions .icon-btn:nth-child(2) { order: 2; } /* Editar */
    .pipeline-card-actions .icon-btn:nth-child(3) { order: 3; } /* Excluir */
    .pipeline-card-actions .icon-btn:nth-child(4) { order: 4; } /* Avançar */
    .pipeline-card-actions .icon-btn:nth-child(n+5) { 
        display: none !important; /* Esconder extras */
    }
}

/* ===== FIX 4: DATA NaN DIAS NOS TICKETS ===== */
.ticket-card p {
    word-break: break-word !important;
}

/* Esconder temporariamente até JS corrigir */
.ticket-card p:has-text("NaN") {
    opacity: 0;
    animation: fadeIn 0.3s forwards;
    animation-delay: 0.5s;
}

@keyframes fadeIn {
    to { opacity: 1; }
}

/* ===== FIX 5: MELHORAR VISUAL DOS TICKETS ===== */
@media (max-width: 768px) {
    .ticket-card {
        padding: 16px !important;
        margin-bottom: 12px !important;
        border-radius: 12px !important;
        background: #1a1a1a !important;
        border: 1px solid #2a2a2a !important;
    }
    
    .ticket-card h4 {
        font-size: 15px !important;
        margin-bottom: 10px !important;
        color: #ffffff !important;
        font-weight: 600 !important;
    }
    
    .ticket-card p {
        font-size: 13px !important;
        color: #999999 !important;
        margin-bottom: 8px !important;
        line-height: 1.5 !important;
    }
    
    /* Badges de status */
    .ticket-card span[style*="padding"] {
        display: inline-block !important;
        padding: 4px 10px !important;
        font-size: 11px !important;
        border-radius: 6px !important;
        font-weight: 600 !important;
        margin: 2px !important;
    }
    
    /* Botões dos tickets */
    .ticket-card button {
        min-width: 44px !important;
        min-height: 44px !important;
        padding: 10px 14px !important;
        margin: 4px !important;
        border-radius: 8px !important;
        font-size: 13px !important;
        border: 1px solid #3a3a3a !important;
        background: #2a2a2a !important;
        color: #ffffff !important;
        transition: all 0.2s !important;
    }
    
    .ticket-card button:active {
        transform: scale(0.95) !important;
        background: #3a3a3a !important;
    }
}

/* ===== FIX 6: HEADER RESPONSIVO MELHORADO ===== */
@media (max-width: 768px) {
    .header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 12px !important;
        padding: 15px !important;
    }
    
    .header-left {
        width: 100% !important;
        justify-content: center !important;
        padding-bottom: 12px !important;
        border-bottom: 1px solid #2a2a2a !important;
    }
    
    .header-actions {
        width: 100% !important;
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }
    
    .header-actions .btn {
        padding: 12px 10px !important;
        font-size: 13px !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    /* Logout ocupa linha inteira */
    .header-actions .logout-btn,
    .header-actions .btn-danger {
        grid-column: 1 / -1 !important;
    }
}

/* ===== FIX 7: TABS NAVEGAÇÃO LIMPA ===== */
@media (max-width: 768px) {
    .nav-tabs {
        overflow-x: auto !important;
        overflow-y: hidden !important;
        white-space: nowrap !important;
        padding: 15px 10px !important;
        gap: 8px !important;
        display: flex !important;
        -webkit-overflow-scrolling: touch !important;
        scroll-snap-type: x mandatory !important;
        scrollbar-width: thin !important;
    }
    
    .tab {
        flex-shrink: 0 !important;
        min-width: 100px !important;
        padding: 12px 18px !important;
        font-size: 13px !important;
        font-weight: 600 !important;
        border-radius: 10px !important;
        scroll-snap-align: start !important;
        transition: all 0.3s !important;
        background: #1a1a1a !important;
        border: 1px solid #2a2a2a !important;
        color: #999999 !important;
    }
    
    .tab.active {
        background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%) !important;
        color: #ffffff !important;
        transform: scale(1.05) !important;
        box-shadow: 0 4px 15px rgba(255, 140, 66, 0.4) !important;
        border: none !important;
    }
}

/* ===== FIX 8: MODAIS MOBILE FRIENDLY ===== */
@media (max-width: 768px) {
    .modal {
        padding: 0 !important;
        align-items: flex-end !important; /* Modal vem de baixo */
    }
    
    .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh !important;
        margin: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        padding: 20px !important;
        overflow-y: auto !important;
    }
    
    .modal-header {
        position: sticky !important;
        top: 0 !important;
        background: #1a1a1a !important;
        z-index: 10 !important;
        padding-bottom: 15px !important;
        margin-bottom: 15px !important;
        border-bottom: 1px solid #2a2a2a !important;
    }
    
    .close-btn {
        position: absolute !important;
        top: 15px !important;
        right: 15px !important;
        font-size: 28px !important;
        min-width: 44px !important;
        min-height: 44px !important;
        background: transparent !important;
        border: none !important;
        color: #999999 !important;
        cursor: pointer !important;
    }
    
    .form-group input,
    .form-group select,
    .form-group textarea {
        width: 100% !important;
        padding: 14px !important;
        font-size: 16px !important; /* Previne zoom no iOS */
        border-radius: 10px !important;
        background: #0a0a0a !important;
        border: 2px solid #2a2a2a !important;
        color: #ffffff !important;
    }
    
    .form-group input:focus,
    .form-group select:focus,
    .form-group textarea:focus {
        border-color: #ff8c42 !important;
        outline: none !important;
        background: #1a1a1a !important;
    }
    
    .modal-footer {
        position: sticky !important;
        bottom: 0 !important;
        background: #1a1a1a !important;
        padding-top: 20px !important;
        margin-top: 20px !important;
        border-top: 1px solid #2a2a2a !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
    }
    
    .modal-footer .btn {
        width: 100% !important;
        padding: 16px !important;
        font-size: 15px !important;
        font-weight: 600 !important;
        min-height: 50px !important;
    }
}

/* ===== FIX 9: CARDS DE CLIENTE MELHORADOS ===== */
@media (max-width: 768px) {
    .client-card {
        padding: 18px !important;
        margin-bottom: 15px !important;
        border-radius: 12px !important;
        background: #1a1a1a !important;
        border: 1px solid #2a2a2a !important;
        overflow: hidden !important;
    }
    
    .client-header {
        display: flex !important;
        flex-direction: column !important;
        gap: 12px !important;
        margin-bottom: 15px !important;
    }
    
    .client-info h3 {
        font-size: 16px !important;
        font-weight: 700 !important;
        color: #ffffff !important;
        margin-bottom: 6px !important;
    }
    
    .client-info p {
        font-size: 13px !important;
        color: #999999 !important;
    }
    
    .client-actions {
        display: flex !important;
        gap: 8px !important;
        width: 100% !important;
    }
    
    .client-actions button {
        flex: 1 !important;
        min-height: 44px !important;
        padding: 10px 12px !important;
        font-size: 13px !important;
        border-radius: 8px !important;
        font-weight: 600 !important;
    }
}

/* ===== FIX 10: PREVENIR SCROLL HORIZONTAL ===== */
html, body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
}

.container,
.modal-content,
.card,
.pipeline-card,
.ticket-card,
.client-card {
    max-width: 100% !important;
    overflow-x: hidden !important;
}

/* ===== FIX 11: TOUCH FEEDBACK VISUAL ===== */
@media (max-width: 768px) {
    button:active,
    .btn:active,
    .tab:active,
    .card:active {
        opacity: 0.8 !important;
        transform: scale(0.98) !important;
    }
}

/* ===== FIX 12: LOADING STATES ===== */
.loading {
    pointer-events: none !important;
    opacity: 0.6 !important;
    cursor: wait !important;
}

.loading::after {
    content: "..." !important;
    animation: dots 1.5s steps(4, end) infinite !important;
}

@keyframes dots {
    0%, 20% { content: "."; }
    40% { content: ".."; }
    60%, 100% { content: "..."; }
}

/* ===== FIX 13: SAFE AREAS (NOTCH iOS) ===== */
@media (max-width: 768px) {
    body {
        padding-top: max(20px, env(safe-area-inset-top)) !important;
        padding-bottom: max(20px, env(safe-area-inset-bottom)) !important;
        padding-left: max(20px, env(safe-area-inset-left)) !important;
        padding-right: max(20px, env(safe-area-inset-right)) !important;
    }
}

/* ===== FIX 14: MELHORAR CONTRASTE ===== */
@media (max-width: 768px) {
    h1, h2, h3, h4, h5, h6 {
        color: #ffffff !important;
        font-weight: 700 !important;
    }
    
    p {
        color: #e0e0e0 !important;
        line-height: 1.6 !important;
    }
    
    .text-muted {
        color: #999999 !important;
    }
    
    label {
        color: #cccccc !important;
        font-weight: 600 !important;
    }
}

/* ===== FIX 15: ACESSIBILIDADE ===== */
button:focus-visible,
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 3px solid #ff8c42 !important;
    outline-offset: 2px !important;
}

/* Skip Link para navegação por teclado */
.skip-link {
    position: absolute !important;
    top: -40px !important;
    left: 0 !important;
    background: #ff8c42 !important;
    color: white !important;
    padding: 8px 12px !important;
    text-decoration: none !important;
    z-index: 100000 !important;
}

.skip-link:focus {
    top: 0 !important;
}

/* ===== ANIMAÇÕES SUAVES ===== */
* {
    transition: background-color 0.3s ease, 
                color 0.3s ease, 
                border-color 0.3s ease,
                transform 0.2s ease,
                opacity 0.3s ease !important;
}

button, .btn, .tab {
    transition: all 0.2s ease !important;
}

/* Reduzir movimento para acessibilidade */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
