/* ============================================
   TPO Dashboard v3 — Responsive Mobile Styles
   Auto-adapts: Mobile, Tablet, Desktop
   ============================================ */

/* ========================================
   BASE: Mobile Header (hidden by default)
   ======================================== */
.mobile-header {
    display: none;
}
.mobile-overlay {
    display: none;
}

/* ========================================
   MOBILE (< 768px) — Hamburger Menu
   ======================================== */
@media (max-width: 767px) {

    /* Body */
    html, body {
        overflow: auto !important;
        height: auto !important;
    }
    body {
        font-size: 14px;
        overflow-x: hidden;
    }

    /* ── App Shell ── */
    .app-shell {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
    }

    /* ── Sidebar: off-canvas drawer ── */
    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        width: 280px;
        height: 100vh;
        height: 100dvh;
        z-index: 9998;
        transition: left 0.3s cubic-bezier(.4,0,.2,1);
        box-shadow: none;
        overflow-y: auto;
    }
    .sidebar.open {
        left: 0;
        box-shadow: 4px 0 24px rgba(0,0,0,0.15);
    }
    /* Restore sidebar text that base 768px media query hides */
    .sidebar-brand span,
    .nav-item span,
    .user-meta {
        display: revert !important;
    }
    .sidebar-brand {
        justify-content: flex-start !important;
    }
    .nav-item {
        justify-content: flex-start !important;
        padding: .6rem .85rem !important;
    }

    /* ── Mobile Overlay ── */
    .mobile-overlay {
        display: none;
        position: fixed;
        top: 0; left: 0;
        width: 100%; height: 100vh; height: 100dvh;
        background: rgba(0,0,0,0.45);
        z-index: 9997;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-overlay.show {
        display: block;
    }

    /* ── Mobile Header ── */
    .mobile-header {
        display: flex;
        position: fixed;
        top: 0; left: 0; right: 0;
        height: 56px;
        background: rgba(255,255,255,0.88);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        border-bottom: 1px solid var(--border);
        padding: 0 12px;
        align-items: center;
        justify-content: space-between;
        z-index: 9999;
        box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    }
    [data-theme="dark"] .mobile-header {
        background: rgba(15,23,42,0.92);
    }
    .hamburger-btn {
        background: none;
        border: none;
        font-size: 22px;
        color: var(--text);
        cursor: pointer;
        padding: 8px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        -webkit-tap-highlight-color: transparent;
        min-width: 44px;
        min-height: 44px;
    }
    .hamburger-btn:active {
        background: var(--bg3);
    }
    .mobile-title {
        font-size: 15px;
        font-weight: 600;
        color: var(--text);
        flex: 1;
        text-align: center;
        font-family: var(--font-display);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .mobile-header-actions {
        display: flex;
        gap: 4px;
        align-items: center;
    }
    .mobile-header-actions button {
        min-width: 40px;
        min-height: 40px;
    }

    /* ── Main Content: full width ── */
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 66px 12px 20px !important;
        overflow-y: visible !important;
        min-height: calc(100vh - 56px);
    }

    /* ── AI Bar ── */
    .ai-bar {
        padding: .35rem .65rem;
        font-size: .72rem;
        border-radius: 10px;
        flex-wrap: wrap;
        gap: .35rem;
    }
    .ai-bar-text {
        font-size: .72rem;
    }
    .gsearch-wrap {
        flex: 1 1 100% !important;
        order: 10;
    }
    .gsearch-input {
        font-size: 16px !important; /* prevent iOS zoom */
        padding: 8px 10px;
    }

    /* ── Page Header ── */
    .page-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: .5rem;
    }
    .page-header h2 {
        font-size: 1.15rem !important;
        line-height: 1.4;
    }
    .period-selector {
        width: 100%;
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    .period-btn {
        flex-shrink: 0;
        font-size: .75rem;
        padding: .35rem .65rem;
    }

    /* ── Stat Cards: single column ── */
    .stat-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: .65rem !important;
    }
    .stat-card {
        padding: .85rem !important;
    }
    .stat-value {
        font-size: 1.5rem !important;
    }
    .stat-value.money {
        font-size: 1rem !important;
    }
    .stat-label {
        font-size: .7rem !important;
    }

    /* ── Chart Grids ── */
    .chart-grid {
        grid-template-columns: 1fr !important;
    }
    .chart-card {
        padding: .85rem;
    }
    .chart-card h3 {
        font-size: .88rem;
    }
    .chart-wrap {
        max-height: 220px !important;
    }
    .chart-wrap canvas {
        max-height: 200px !important;
    }

    /* ── BGN Grid ── */
    .bgn-detail-grid {
        grid-template-columns: 1fr !important;
    }

    /* ── Tables: horizontal scroll ── */
    .table-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -12px;
        padding: 0 12px;
    }
    .data-table {
        font-size: .75rem;
        min-width: 580px;
    }
    .data-table th,
    .data-table td {
        padding: .45rem .55rem;
        white-space: nowrap;
    }

    /* ── Search Bar ── */
    .search-bar {
        padding: .85rem;
    }
    .search-input-row {
        flex-direction: column;
    }
    .search-input-row input {
        font-size: 16px !important; /* prevent iOS zoom */
    }
    .search-type-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
    }
    .stab {
        flex-shrink: 0;
    }

    /* ── Buttons ── */
    .btn {
        padding: .55rem 1rem;
        font-size: .85rem;
        min-height: 44px;
    }
    .btn-full {
        width: 100%;
    }

    /* ── Forms ── */
    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 16px !important; /* prevent iOS zoom */
        padding: .65rem .85rem;
    }

    /* ── Modals: near full-screen ── */
    .modal {
        padding: 10px;
    }
    .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh;
        overflow-y: auto;
        border-radius: 12px;
    }

    .gmodal {
        padding: 0;
        align-items: flex-end;
    }
    .gmodal-box {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 92vh;
        border-radius: 16px 16px 0 0;
        padding: 1.25rem;
        animation: slideUp 0.3s ease;
    }
    @keyframes slideUp {
        from { transform: translateY(40px); opacity: 0; }
        to   { transform: translateY(0); opacity: 1; }
    }
    .gmodal-stat {
        flex-direction: column;
        gap: .5rem;
    }
    .gmodal-stat-item {
        min-width: unset;
    }

    /* ── Pattern Detection ── */
    .pd-summary {
        grid-template-columns: 1fr 1fr !important;
        gap: .5rem;
    }
    .pd-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
    }
    .pd-tab {
        flex-shrink: 0;
        font-size: .78rem;
        padding: .4rem .85rem;
    }

    /* ── Org Chart ── */
    .org-chart {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .org-branches {
        flex-direction: column !important;
        gap: .75rem;
    }

    /* ── Core Cards (Facts, etc.) ── */
    .core-cards {
        grid-template-columns: 1fr !important;
    }

    /* ── Report Groups ── */
    .report-groups {
        grid-template-columns: 1fr !important;
    }

    /* ── Documents ── */
    .doc-stats {
        grid-template-columns: 1fr !important;
    }
    .doc-grid {
        grid-template-columns: 1fr !important;
    }
    .doc-grid.doc-gallery-mode {
        grid-template-columns: 1fr !important;
    }

    /* ── Case Detail fields ── */
    .cd-fields-grid {
        grid-template-columns: 1fr !important;
    }

    /* ── Command Center ── */
    .cmd-grid {
        grid-template-columns: 1fr !important;
    }

    /* ── Analytics ── */
    .analytics-filters {
        flex-direction: column;
    }

    /* ── Login ── */
    .login-card {
        width: 100% !important;
        max-width: 100%;
        margin: 0 16px;
        padding: 1.5rem;
        border-radius: 12px;
    }
    .login-logo h1 {
        font-size: 1.75rem;
    }
    .login-logo p {
        font-size: .78rem;
    }

    /* ── Text Sizes ── */
    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.25rem; }
    h3 { font-size: 1.1rem; }
    h4 { font-size: 1rem; }

    /* ── Utilities ── */
    .hide-mobile { display: none !important; }
    .show-mobile { display: block !important; }

    /* ── Demo badge ── */
    #demo-badge {
        top: 60px !important;
        right: 12px !important;
        font-size: 9px !important;
    }

    /* ── Sidebar footer on mobile ── */
    .sidebar-footer {
        padding: .75rem;
    }
    .theme-toggle {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .btn-logout {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* ── Workload page ── */
    .wl-grid {
        grid-template-columns: 1fr !important;
    }

    /* ── Intel page ── */
    .intel-grid {
        grid-template-columns: 1fr !important;
    }

    /* ── Damage Analysis ── */
    .damage-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ========================================
   TABLET (768px – 1024px)
   ======================================== */
@media (min-width: 768px) and (max-width: 1024px) {

    .stat-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: .85rem !important;
    }

    .sidebar {
        width: 220px;
    }

    .main-content {
        padding: 1.25rem 1.5rem;
    }

    .chart-grid {
        grid-template-columns: 1fr !important;
    }

    .bgn-detail-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .data-table {
        font-size: .8rem;
    }

    .gmodal-box {
        width: 85vw;
    }

    .page-header h2 {
        font-size: 1.4rem;
    }
}

/* ========================================
   DESKTOP (> 1024px) — defaults
   ======================================== */
@media (min-width: 1025px) {
    .mobile-header {
        display: none !important;
    }
    .mobile-overlay {
        display: none !important;
    }
    .sidebar {
        left: 0 !important;
    }
}

/* ========================================
   TOUCH OPTIMIZATIONS
   ======================================== */
@media (hover: none) and (pointer: coarse) {
    /* Larger tap targets */
    button, a, .btn, .nav-item, .stab, .pd-tab, .period-btn {
        min-height: 44px;
    }
    .nav-item {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    /* Smooth momentum scrolling */
    .table-scroll,
    .sidebar-nav,
    .main-content {
        -webkit-overflow-scrolling: touch;
    }
}

/* ========================================
   PWA / Standalone mode
   ======================================== */
@media (display-mode: standalone) {
    .mobile-header {
        padding-top: env(safe-area-inset-top);
        height: calc(56px + env(safe-area-inset-top));
    }
    .main-content {
        padding-bottom: env(safe-area-inset-bottom);
    }
    .sidebar {
        padding-top: env(safe-area-inset-top);
    }
}

/* ========================================
   LANDSCAPE MOBILE
   ======================================== */
@media (max-width: 767px) and (orientation: landscape) {
    .mobile-header {
        height: 48px;
    }
    .main-content {
        padding-top: 58px !important;
    }
    .stat-cards {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

/* ========================================
   PRINT
   ======================================== */
@media print {
    .sidebar,
    .mobile-header,
    .mobile-overlay,
    .ai-bar,
    .theme-toggle,
    .btn-logout,
    #demo-badge {
        display: none !important;
    }
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 0 !important;
        overflow: visible !important;
    }
    .app-shell {
        display: block;
    }
    .stat-card, .chart-card, .card {
        page-break-inside: avoid;
    }
}
