/**
 * SISMADLMPA - Otimização Mobile
 * Baseado no sistema de projetos com adaptações para SISMADLMPA
 */

/* ===== Geral para Smartphones (até 768px) ===== */
@media (max-width: 768px) {
    /* Sidebar - Escondida por padrão em mobile */
    .sidebar {
        width: 280px !important;
        transform: translateX(-100%) !important;
        transition: transform 0.3s ease !important;
    }

    .sidebar.active {
        transform: translateX(0) !important;
    }

    .sidebar .components li a {
        padding: 1rem 1.5rem !important;
        min-height: 48px !important;
    }

    .sidebar .components li a i {
        width: 20px !important;
        height: 20px !important;
        margin-right: 0.875rem !important;
    }

    /* Overlay quando sidebar está ativa */
    .sidebar-overlay {
        display: none !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        background: rgba(0, 0, 0, 0.5) !important;
        z-index: 999 !important;
    }

    .sidebar-overlay.active {
        display: block !important;
    }

    /* Ajustes do content */
    #content {
        width: 100% !important;
        margin-left: 0 !important;
    }

    /* Topbar compacta */
    .topbar {
        padding: 0.5rem !important;
        min-height: 56px !important;
    }

    .btn-toggle {
        font-size: 20px !important;
        padding: 0.5rem !important;
        width: 40px !important;
        height: 40px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    /* Breadcrumb simplificado */
    .breadcrumb {
        font-size: 0.75rem !important;
        display: none !important; /* Esconde em mobile muito pequeno */
    }

    /* Cards mais compactos */
    .card {
        margin-bottom: 0.75rem !important;
        border-radius: 8px !important;
    }

    .card-header {
        padding: 0.75rem !important;
        font-size: 0.9rem !important;
    }

    .card-body {
        padding: 0.75rem !important;
    }

    /* Page Header responsivo */
    .page-header {
        flex-direction: column !important;
        gap: 1rem !important;
        align-items: flex-start !important;
    }

    .page-header h2 {
        font-size: 1.25rem !important;
    }

    .page-header p {
        font-size: 0.8rem !important;
    }

    /* Botões do page header em tela cheia */
    .page-header .btn-group,
    .btn-group {
        display: flex !important;
        flex-wrap: wrap !important;
        width: 100% !important;
        gap: 0.5rem !important;
    }

    .btn-group .btn {
        flex: 1 1 calc(50% - 0.25rem) !important;
        min-width: calc(50% - 0.25rem) !important;
        padding: 0.625rem 0.75rem !important;
        font-size: 0.75rem !important;
        white-space: nowrap !important;
    }

    /* Filtros responsivos */
    .filters-panel {
        padding: 0.75rem !important;
    }

    /* Filtros e formulários: 2 por linha em mobile */
    .filters-panel .row .col-md-2,
    .filters-panel .row .col-md-3,
    .filters-panel .row .col-md-4,
    form .row .col-md-2,
    form .row .col-md-3,
    form .row .col-md-4 {
        width: 50% !important;
        flex: 0 0 50% !important;
        max-width: 50% !important;
        margin-bottom: 0.5rem !important;
        padding-left: 0.25rem !important;
        padding-right: 0.25rem !important;
    }

    /* Colunas maiores ficam 100% */
    .filters-panel .row .col-md-6,
    .filters-panel .row .col-md-8,
    .filters-panel .row .col-md-12,
    form .row .col-md-6,
    form .row .col-md-8,
    form .row .col-md-12 {
        width: 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
        margin-bottom: 0.5rem !important;
    }

    .form-label {
        font-size: 0.75rem !important;
        margin-bottom: 0.25rem !important;
    }

    .form-control,
    .form-select {
        padding: 0.625rem !important;
        font-size: 16px !important; /* Previne zoom no iOS */
        min-height: 44px !important; /* Touch-friendly */
    }

    /* Tabela responsiva com scroll horizontal */
    .table-responsive {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        border-radius: 8px !important;
        margin: 0 -0.75rem !important;
        padding: 0 0.75rem !important;
    }

    .table {
        font-size: 0.75rem !important;
        white-space: nowrap !important;
    }

    .table thead th {
        padding: 0.5rem 0.375rem !important;
        font-size: 0.7rem !important;
    }

    .table tbody td {
        padding: 0.5rem 0.375rem !important;
        font-size: 0.75rem !important;
    }

    /* Badge na tabela */
    .table .badge {
        font-size: 0.65rem !important;
        padding: 0.2rem 0.4rem !important;
    }

    /* Paginação responsiva */
    .pagination {
        display: flex !important;
        flex-wrap: wrap !important;
        justify-content: center !important;
        gap: 0.25rem !important;
    }

    .page-link {
        padding: 0.375rem 0.625rem !important;
        font-size: 0.75rem !important;
        min-width: 36px !important;
    }

    /* Alert responsivo */
    .alert {
        padding: 0.75rem !important;
        font-size: 0.8rem !important;
        margin-bottom: 0.75rem !important;
    }

    .alert .btn-close {
        padding: 0.5rem !important;
    }

    /* Cards de resumo em coluna única */
    .row .col-md-6,
    .row .col-md-4,
    .row .col-lg-3,
    .row .col-lg-4 {
        width: 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
    }

    /* Footer simplificado */
    .footer {
        padding: 1rem 0 !important;
        font-size: 0.75rem !important;
    }

    .footer .row {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }

    .footer p {
        text-align: center !important;
    }
}

/* ===== Smartphones pequenos (até 480px) ===== */
@media (max-width: 480px) {
    /* Sidebar um pouco menor */
    .sidebar {
        width: 250px !important;
    }

    /* Botões em coluna única */
    .btn-group .btn {
        flex: 1 1 100% !important;
        min-width: 100% !important;
    }

    /* Tabela ainda menor */
    .table {
        font-size: 0.7rem !important;
    }

    .table thead th,
    .table tbody td {
        padding: 0.375rem 0.25rem !important;
    }

    /* Form labels menores */
    .form-label {
        font-size: 0.7rem !important;
    }

    /* Header compacto */
    .page-header h2 {
        font-size: 1.1rem !important;
    }
}

/* ===== Tablets (481px - 768px) ===== */
@media (min-width: 481px) and (max-width: 768px) {
    /* Filtros em 2 colunas */
    .row .col-md-3,
    .row .col-md-4 {
        width: 50% !important;
        flex: 0 0 50% !important;
        max-width: 50% !important;
    }

    .row .col-md-6 {
        width: 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
    }

    /* Stats em 2 colunas */
    .row .col-lg-3,
    .row .col-lg-4 {
        width: 50% !important;
        flex: 0 0 50% !important;
        max-width: 50% !important;
    }

    /* Botões em 2 colunas */
    .btn-group .btn {
        flex: 1 1 calc(50% - 0.25rem) !important;
        min-width: calc(50% - 0.25rem) !important;
    }
}

/* ===== Melhorias de touch para dispositivos móveis ===== */
@media (max-width: 768px) {
    /* Aumentar área de toque em botões */
    .btn,
    button,
    a.btn {
        min-height: 44px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    /* Exceções para botões em tabelas */
    .table .btn,
    .table button {
        min-height: auto !important;
        padding: 0.25rem 0.5rem !important;
        font-size: 0.7rem !important;
    }

    /* Select e input touch-friendly */
    select.form-control,
    input.form-control,
    textarea.form-control {
        min-height: 44px !important;
    }

    /* Links na tabela */
    .table a {
        padding: 0.25rem 0.5rem !important;
        display: inline-block !important;
    }
}

/* ===== Orientação paisagem em smartphones ===== */
@media (max-width: 768px) and (orientation: landscape) {
    .main-content {
        padding-top: 50px !important;
    }

    .topbar {
        min-height: 50px !important;
    }

    /* Ajuste para paisagem */
    .row .col-md-3,
    .row .col-md-4 {
        width: 33.333% !important;
        flex: 0 0 33.333% !important;
        max-width: 33.333% !important;
    }
}

/* ===== Otimizações para telas com notch (iPhone X+) ===== */
@supports (padding-top: env(safe-area-inset-top)) {
    .topbar {
        padding-top: calc(0.5rem + env(safe-area-inset-top)) !important;
        min-height: calc(60px + env(safe-area-inset-top)) !important;
    }

    .main-content {
        padding-top: calc(60px + env(safe-area-inset-top)) !important;
    }

    .sidebar {
        padding-top: env(safe-area-inset-top) !important;
    }
}

/* ===== Animações suaves para mobile ===== */
@media (max-width: 768px) {
    .card,
    .btn,
    .sidebar {
        transition: all 0.2s ease-in-out !important;
    }

    .btn:active {
        transform: scale(0.95) !important;
    }
}

/* ===== Overlay para sidebar ===== */
@media (max-width: 768px) {
    #sidebarCollapse {
        z-index: 1001 !important;
    }
}

/* ===== Print styles melhorados ===== */
@media print {
    .sidebar,
    .topbar,
    .btn-toggle,
    .footer,
    .no-print {
        display: none !important;
    }

    #content {
        width: 100% !important;
        margin-left: 0 !important;
    }

    .card {
        box-shadow: none !important;
        border: 1px solid #ddd !important;
        page-break-inside: avoid !important;
    }
}

/* ===== Scroll suave em tabelas ===== */
@media (max-width: 768px) {
    .table-responsive {
        position: relative !important;
    }

    /* Indicador de scroll */
    .table-responsive::after {
        content: '↔';
        position: absolute;
        bottom: 0.5rem;
        right: 0.5rem;
        background: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 0.25rem 0.5rem;
        border-radius: 0.25rem;
        font-size: 0.65rem;
        pointer-events: none;
        opacity: 0.5;
    }
}

/* ===== Dashboard específico ===== */
@media (max-width: 768px) {
    .stat-card {
        padding: 1rem !important;
        margin-bottom: 0.75rem !important;
    }

    .stat-card .icon {
        font-size: 24px !important;
    }

    .stat-card .value {
        font-size: 1.5rem !important;
    }

    .stat-card .label {
        font-size: 0.7rem !important;
    }
}

/* ===== Login específico ===== */
@media (max-width: 768px) {
    .login-container {
        padding: 1rem !important;
    }

    .login-card {
        border-radius: 12px !important;
    }

    .login-header {
        padding: 1.5rem !important;
    }

    .login-body {
        padding: 1.5rem !important;
    }
}
