/* ============================================================
   GER COMERCIAL - ESTILOS MOBILE

   Este arquivo contém exclusivamente estilos que ativam
   em telas pequenas via media queries.
   Nenhum estilo do desktop é afetado.

   Breakpoints:
     1024px  Tablet
      768px  Mobile
      480px  Small Mobile

   Como usar:
     <link rel="stylesheet" href="css/mobile.css">
   ============================================================ */


/* ============================================================
   BOTÃO TOGGLE DE FILTROS
   Injetado pelo js/mobile.js — visível apenas em mobile.
   No desktop fica display:none por padrão.
   ============================================================ */

.mobile-filter-toggle {
    display: none;
}


/* ============================================================
   TABLET (max-width: 1024px)
   ============================================================ */

@media (max-width: 1024px) {

    .cards-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}


/* ============================================================
   MOBILE (max-width: 768px)
   ============================================================ */

@media (max-width: 768px) {

    /* --- Safe area support for notched phones --- */
    body {
        padding: 0 !important;
        padding-left: env(safe-area-inset-left) !important;
        padding-right: env(safe-area-inset-right) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }


    /* ============================================================
       FILTER TOGGLE BUTTON
       ============================================================ */

    .mobile-filter-toggle {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: calc(100% - 32px);
        margin: 12px 16px 0;
        padding: 14px 20px;
        background: linear-gradient(135deg, #fc0303 0%, #b50909 100%);
        color: white;
        border: none;
        border-radius: 12px;
        font-size: 1em;
        font-weight: 600;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        font-family: inherit;
        box-shadow: 0 2px 8px rgba(252, 3, 3, 0.25);
        transition: transform 0.15s ease, box-shadow 0.15s ease;
    }

    .mobile-filter-toggle:active {
        transform: scale(0.97);
        box-shadow: 0 1px 4px rgba(252, 3, 3, 0.2);
    }

    .mobile-filter-arrow {
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        font-size: 0.75em;
        display: inline-block;
    }

    .mobile-filter-toggle[aria-expanded="true"] .mobile-filter-arrow {
        transform: rotate(180deg);
    }


    /* ============================================================
       SEÇÃO DE FILTROS — collapsible em mobile com animacao
       ============================================================ */

    .filters-section {
        flex-direction: column !important;
        margin: 10px 16px !important;
        padding: 14px !important;
        transition: opacity 0.25s ease, max-height 0.3s ease;
    }

    .filters-section.mobile-filters-collapsed {
        display: none !important;
    }

    .filters-grid {
        grid-template-columns: 1fr !important;
        flex-wrap: wrap !important;
        gap: 12px !important;
    }

    .filter-group,
    .filter-group-periodo {
        min-width: unset !important;
        width: 100% !important;
    }

    /* Inputs e selects maiores para toque */
    .filter-group select,
    .filter-group input[type="date"],
    .filter-group input[type="text"] {
        min-height: 44px !important;
        font-size: 16px !important; /* Prevent iOS zoom on focus */
    }

    .filter-group label {
        font-size: 0.85em !important;
        margin-bottom: 6px !important;
    }

    .filters-buttons {
        flex-direction: row !important;
        min-width: unset !important;
        width: 100% !important;
        gap: 8px !important;
        margin-top: 4px !important;
    }

    .filters-buttons .btn-atualizar,
    .filters-buttons .btn-limpar,
    .filters-buttons .btn-whatsapp,
    .filters-buttons .btn-export {
        flex: 1 !important;
        min-height: 44px !important;
        font-size: 0.9em !important;
    }

    .filter-group select[multiple] {
        min-height: 80px !important;
    }

    .quick-dates {
        gap: 6px !important;
        flex-wrap: wrap !important;
    }

    .quick-date-btn {
        padding: 8px 12px !important;
        font-size: 0.8em !important;
        min-height: 36px !important;
    }


    /* ============================================================
       HEADER — páginas de dashboard (flex layout)
       ============================================================ */

    .header {
        padding: 10px 16px !important;
        gap: 8px !important;
        position: sticky;
        top: 0;
        z-index: 100;
    }

    .header-left {
        gap: 8px !important;
        flex: 1;
        min-width: 0;
    }

    .header-logo img {
        height: 32px !important;
    }

    .header h1 {
        font-size: 0.95em !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .btn-voltar,
    .btn-back {
        padding: 8px 12px !important;
        font-size: 0.82em !important;
        white-space: nowrap;
        min-height: 36px !important;
        border-radius: 8px !important;
    }

    .header-right {
        gap: 6px !important;
    }

    .btn-logout {
        padding: 8px 12px !important;
        font-size: 0.8em !important;
        min-height: 36px !important;
    }


    /* ============================================================
       HEADER — index.html (grid 3 colunas com .header-content)
       Converte para coluna unica centrada
       ============================================================ */

    .header:has(.header-content) {
        grid-template-columns: 1fr !important;
        text-align: center !important;
        padding: 20px 16px !important;
        gap: 10px !important;
        position: relative;
    }

    .header:has(.header-content) .header-logo {
        justify-self: center !important;
    }

    .header:has(.header-content) .header-logo img {
        height: 50px !important;
    }

    .header:has(.header-content) .header h1 {
        font-size: 1.35em !important;
        white-space: normal !important;
        overflow: visible !important;
    }

    .header:has(.header-content) .header-user {
        justify-self: center !important;
        text-align: center !important;
        display: flex !important;
        align-items: center !important;
        gap: 12px !important;
    }


    /* ============================================================
       CARDS GRID — index.html (home)
       Desktop: 5 colunas -> Mobile: 2 colunas
       ============================================================ */

    .cards-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
        margin-top: 20px !important;
    }

    .card {
        padding: 16px !important;
        border-radius: 10px !important;
        transition: transform 0.15s ease, box-shadow 0.15s ease !important;
    }

    .card:active {
        transform: scale(0.97) !important;
    }

    .card-icon {
        width: 40px !important;
        height: 40px !important;
        font-size: 1.3em !important;
        margin-bottom: 8px !important;
        border-radius: 8px !important;
    }

    .card-title {
        font-size: 0.95em !important;
        margin-bottom: 6px !important;
        line-height: 1.3 !important;
    }

    .card-description {
        font-size: 0.78em !important;
        line-height: 1.4 !important;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .badge {
        font-size: 0.7em !important;
        padding: 3px 8px !important;
        margin-top: 8px !important;
    }


    /* ============================================================
       CONTEUDO PRINCIPAL — grid 70/30 -> coluna unica
       ============================================================ */

    .main-content {
        grid-template-columns: 1fr !important;
        margin: 12px 12px !important;
        gap: 14px !important;
    }


    /* ============================================================
       KPI GRIDS
       ============================================================ */

    .kpi-grid {
        gap: 8px !important;
    }

    .kpis-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
        margin: 0 12px !important;
    }

    .kpi-card {
        padding: 12px 10px !important;
        border-radius: 10px !important;
    }

    .kpi-icon {
        font-size: 1.3em !important;
        margin-bottom: 4px !important;
    }

    .kpi-label {
        font-size: 0.7em !important;
        letter-spacing: 0.02em !important;
    }

    .kpi-value {
        font-size: 0.95em !important;
        word-break: break-word !important;
    }


    /* ============================================================
       TABELAS — scroll horizontal com indicadores visuais
       ============================================================ */

    .table-wrapper,
    .table-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        position: relative;
        border-radius: 8px !important;
    }

    /* Gradiente de fade na borda direita para indicar scroll */
    .table-wrapper::after,
    .table-container::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 24px;
        background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.8));
        pointer-events: none;
        opacity: 1;
        transition: opacity 0.3s ease;
    }

    .table-wrapper.scrolled-end::after,
    .table-container.scrolled-end::after {
        opacity: 0;
    }

    .mobile-scroll-hint {
        font-size: 0.72em;
        color: #6c757d;
        text-align: center;
        padding: 6px 0 4px;
        font-style: italic;
        background: #f8f9fa;
        border-radius: 6px;
        margin-bottom: 4px;
    }

    th, td {
        padding: 10px 8px !important;
        font-size: 0.8em !important;
        white-space: nowrap;
    }

    th:first-child,
    td:first-child {
        white-space: normal !important;
        min-width: 90px;
        position: sticky;
        left: 0;
        background: inherit;
        z-index: 1;
    }

    /* Alternating row colors para facilitar leitura em telas pequenas */
    tbody tr:nth-child(even) {
        background-color: #f8f9fa;
    }

    th {
        position: sticky;
        top: 0;
        z-index: 2;
    }


    /* ============================================================
       TABLE HEADER
       ============================================================ */

    .table-header {
        padding: 12px 14px !important;
        font-size: 0.9em !important;
    }


    /* ============================================================
       TABLE FOOTER — empilha os botoes de export
       ============================================================ */

    .table-footer {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        padding: 10px 14px !important;
    }

    .table-footer .btn-export {
        margin-left: 0 !important;
        text-align: center;
        min-height: 44px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    .export-buttons {
        flex-direction: row !important;
        gap: 6px !important;
        flex-wrap: wrap !important;
    }

    .export-buttons .btn-export {
        text-align: center;
        flex: 1 !important;
        min-width: 0 !important;
        min-height: 44px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 8px !important;
    }


    /* ============================================================
       PAGINACAO — melhor para toque
       ============================================================ */

    .pagination {
        flex-wrap: wrap !important;
        justify-content: center !important;
        gap: 4px !important;
    }

    .pagination button,
    .pagination .page-btn,
    .btn-page {
        min-width: 40px !important;
        min-height: 40px !important;
        font-size: 0.85em !important;
        border-radius: 8px !important;
    }

    .pagination-info {
        font-size: 0.78em !important;
        width: 100% !important;
        text-align: center !important;
        order: -1 !important;
    }


    /* ============================================================
       GRAFICOS — altura adaptativa
       ============================================================ */

    .chart-container {
        height: 220px !important;
        padding: 12px !important;
        border-radius: 10px !important;
    }

    .chart-title {
        font-size: 0.85em !important;
        margin-bottom: 8px !important;
    }


    /* ============================================================
       MAPA (Leaflet)
       ============================================================ */

    .map-section {
        width: 100%;
    }

    #map {
        height: 280px !important;
        border-radius: 10px !important;
    }

    .map-legend {
        gap: 8px !important;
        font-size: 0.78em !important;
        flex-wrap: wrap !important;
    }


    /* ============================================================
       RIGHT SECTION (clientes-sem-compras)
       ============================================================ */

    .right-section {
        gap: 14px !important;
    }

    .summary-header {
        padding: 10px 14px !important;
        font-size: 0.9em !important;
    }

    .summary-total {
        padding: 10px 14px !important;
        font-size: 0.95em !important;
    }

    .summary-content {
        max-height: 220px !important;
        padding: 0 !important;
    }

    .summary-rota,
    .summary-subrota {
        padding: 8px 12px !important;
        font-size: 0.85em !important;
    }

    .time-summary {
        padding: 10px !important;
    }

    .time-item {
        padding: 8px 10px !important;
        font-size: 0.85em !important;
    }

    .time-badge {
        font-size: 0.82em !important;
        padding: 3px 10px !important;
    }


    /* ============================================================
       MODE BUTTONS (ranking-clientes)
       ============================================================ */

    .mode-buttons {
        flex-wrap: wrap !important;
        gap: 6px !important;
    }

    .btn-mode {
        padding: 10px 16px !important;
        font-size: 0.85em !important;
        min-height: 40px !important;
        border-radius: 8px !important;
    }


    /* ============================================================
       USERS TABLE (gerenciar-usuarios)
       ============================================================ */

    .users-table-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .users-table th,
    .users-table td {
        padding: 10px 8px !important;
        font-size: 0.82em !important;
        white-space: nowrap;
    }

    .users-table th:first-child,
    .users-table td:first-child {
        white-space: normal !important;
        min-width: 80px;
    }

    .action-buttons {
        gap: 4px !important;
        flex-wrap: wrap !important;
    }

    .btn-action {
        padding: 8px 10px !important;
        font-size: 0.78em !important;
        min-height: 36px !important;
        border-radius: 6px !important;
    }

    .users-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 10px !important;
    }


    /* ============================================================
       MODAL (gerenciar-usuarios) — tela cheia em mobile
       ============================================================ */

    .modal-content {
        margin: 10px !important;
        padding: 20px !important;
        max-width: calc(100vw - 20px) !important;
        max-height: calc(100vh - 20px) !important;
        overflow-y: auto !important;
        border-radius: 12px !important;
    }

    .modal-content h3 {
        font-size: 1.1em !important;
    }

    .modal-content input,
    .modal-content select,
    .modal-content textarea {
        min-height: 44px !important;
        font-size: 16px !important;
    }

    .modal-content .btn-action,
    .modal-content button[type="submit"] {
        min-height: 44px !important;
        font-size: 1em !important;
    }

    /* Tabs no gerenciar-usuarios */
    .tabs {
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 0 !important;
        padding: 0 !important;
    }

    .tab-btn,
    .tab {
        flex-shrink: 0 !important;
        padding: 10px 14px !important;
        font-size: 0.82em !important;
        white-space: nowrap !important;
        min-height: 40px !important;
    }


    /* ============================================================
       ALERTA GERAL
       ============================================================ */

    .alert {
        margin: 10px 16px !important;
        padding: 12px 16px !important;
        border-radius: 10px !important;
        font-size: 0.88em !important;
    }


    /* ============================================================
       LOADING STATE
       ============================================================ */

    .loading-overlay,
    .loading {
        font-size: 0.9em !important;
    }


    /* ============================================================
       MANUAL — melhorias de leitura mobile
       ============================================================ */

    .toc {
        padding: 20px !important;
    }

    .toc h2 {
        font-size: 1.4em !important;
    }

    .toc li {
        margin: 8px 0 !important;
    }

    .toc a {
        font-size: 0.95em !important;
        display: block !important;
        padding: 6px 0 !important;
    }

    .section {
        padding: 20px 16px !important;
        margin-bottom: 16px !important;
    }

    .section h2 {
        font-size: 1.4em !important;
        margin-bottom: 14px !important;
    }

    .section h3 {
        font-size: 1.15em !important;
        margin-top: 20px !important;
    }

    .section h4 {
        font-size: 1.05em !important;
    }

    .section p,
    .section li {
        font-size: 0.95em !important;
    }

    .section ul,
    .section ol {
        margin-left: 20px !important;
    }

    .tip-box,
    .warning-box,
    .success-box {
        padding: 14px !important;
        margin: 14px 0 !important;
    }

    .step {
        padding: 14px !important;
    }

    .step-number {
        width: 26px !important;
        height: 26px !important;
        font-size: 0.85em !important;
    }

    .code-box {
        padding: 12px !important;
        font-size: 0.85em !important;
        overflow-x: auto !important;
    }

    /* Manual header sticky em mobile */
    .header:has(.header-content):has(.btn-voltar) {
        padding: 14px 16px !important;
    }


    /* ============================================================
       SCROLL TO TOP BUTTON
       ============================================================ */

    .scroll-to-top {
        position: fixed;
        bottom: calc(20px + env(safe-area-inset-bottom));
        right: 16px;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        background: linear-gradient(135deg, #fc0303 0%, #b50909 100%);
        color: white;
        border: none;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
        cursor: pointer;
        font-size: 1.2em;
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease, transform 0.15s ease;
        z-index: 999;
        -webkit-tap-highlight-color: transparent;
    }

    .scroll-to-top.visible {
        opacity: 1;
        visibility: visible;
    }

    .scroll-to-top:active {
        transform: scale(0.9);
    }
}


/* ============================================================
   SMALL MOBILE (max-width: 480px)
   ============================================================ */

@media (max-width: 480px) {

    /* Cards: coluna unica */
    .cards-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    /* Cards em lista horizontal compacta */
    .card {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 12px !important;
        padding: 14px !important;
    }

    .card-icon {
        width: 38px !important;
        height: 38px !important;
        font-size: 1.2em !important;
        margin-bottom: 0 !important;
        flex-shrink: 0 !important;
    }

    .card-content {
        flex: 1;
        min-width: 0;
    }

    .card-title {
        font-size: 0.92em !important;
        margin-bottom: 2px !important;
    }

    .card-description {
        font-size: 0.75em !important;
        -webkit-line-clamp: 2 !important;
    }

    .badge {
        font-size: 0.65em !important;
        padding: 2px 6px !important;
        margin-top: 4px !important;
    }

    /* Header index — logo menor */
    .header:has(.header-content) .header-logo img {
        height: 40px !important;
    }

    .header:has(.header-content) .header h1 {
        font-size: 1.15em !important;
    }

    /* KPIs mantem 2x2 mesmo em small */
    .kpi-grid,
    .kpis-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .kpi-value {
        font-size: 0.85em !important;
    }

    .kpis-grid {
        margin: 0 8px !important;
    }

    /* Filtros: botoes empilham */
    .filters-buttons {
        flex-direction: column !important;
    }

    /* Toggle menor */
    .mobile-filter-toggle {
        margin: 10px 12px 0 !important;
        width: calc(100% - 24px) !important;
        padding: 12px 16px !important;
        font-size: 0.92em !important;
    }

    /* Main content margem menor */
    .main-content {
        margin: 8px 8px !important;
        gap: 10px !important;
    }

    /* Grafico menor */
    .chart-container {
        height: 180px !important;
    }

    /* Mapa menor */
    #map {
        height: 240px !important;
    }

    /* Export buttons em coluna no small mobile */
    .export-buttons {
        flex-direction: column !important;
    }

    /* Paginacao mais compacta */
    .pagination button,
    .pagination .page-btn,
    .btn-page {
        min-width: 36px !important;
        min-height: 36px !important;
        font-size: 0.8em !important;
    }
}


/* ============================================================
   LANDSCAPE MOBILE (max-height: 500px and landscape)
   ============================================================ */

@media (max-height: 500px) and (orientation: landscape) {

    .header {
        padding: 6px 16px !important;
    }

    .header-logo img {
        height: 28px !important;
    }

    .header h1 {
        font-size: 0.9em !important;
    }

    .chart-container {
        height: 160px !important;
    }

    #map {
        height: 200px !important;
    }

    .kpi-card {
        padding: 8px !important;
    }

    .kpi-icon {
        font-size: 1em !important;
    }

    .kpi-value {
        font-size: 0.85em !important;
    }

    .scroll-to-top {
        bottom: 12px;
        right: 12px;
        width: 36px;
        height: 36px;
    }
}


/* ============================================================
   PRINT STYLES
   ============================================================ */

@media print {

    .mobile-filter-toggle,
    .scroll-to-top,
    .mobile-scroll-hint {
        display: none !important;
    }

    .filters-section.mobile-filters-collapsed {
        display: flex !important;
    }
}
