/* ============================================================
   MOBILE CSS - ONLY applies on screens <= 768px
   Desktop styles in main.css remain untouched
   ============================================================ */

/* === PWA Install Banner (visible on any size if PWA-installable) === */
.pwa-install-banner {
    position: fixed;
    bottom: 16px;
    left: 16px;
    right: 16px;
    background: linear-gradient(135deg, #0f172a, #1e293b);
    color: white;
    padding: 14px 16px;
    border-radius: 14px;
    display: none;
    justify-content: space-between;
    align-items: center;
    z-index: 95;
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
    max-width: 500px;
    margin: 0 auto;
}
.pwa-install-banner.show { display: flex; }
.pwa-install-banner .btn {
    background: white !important;
    color: #3b82f6 !important;
    padding: 8px 14px;
    border-radius: 8px;
    border: none;
    font-weight: 700;
    font-size: 13px;
    cursor: pointer;
}

/* Mobile bottom nav - hidden on desktop, shown on mobile */
.mobile-bottom-nav { display: none; }
.mobile-menu-btn { display: none; }
.sidebar-backdrop { display: none; }

/* ===================== MOBILE ONLY (<=768px) ===================== */
@media (max-width: 768px) {
    
    /* === GLOBAL RESETS === */
    * { -webkit-tap-highlight-color: transparent; }
    
    html, body {
        overflow-x: hidden;
        -webkit-text-size-adjust: 100%;
        -webkit-font-smoothing: antialiased;
    }
    
    /* Prevent iOS zoom */
    input, textarea, select {
        font-size: 16px !important;
    }
    
    /* Larger touch targets */
    .btn, button {
        min-height: 40px;
    }
    .btn-xs { min-height: 32px; }
    .btn-sm { min-height: 36px; }
    
    /* ===== APP LAYOUT - sidebar slides in ===== */
    .app-layout {
        display: block !important;
    }
    
    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        width: 260px;
        height: 100vh;
        z-index: 200;
        transition: left 0.3s ease;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 !important;
        border-radius: 0 !important;
    }
    
    .sidebar.open {
        left: 0;
        box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    }
    
    .main-content {
        margin-left: 0 !important;
        padding: 64px 12px 80px 12px !important;
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Hamburger button */
    .mobile-menu-btn {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border-radius: 10px;
        background: white;
        border: 1px solid #e2e8f0;
        cursor: pointer;
        position: fixed;
        top: 12px;
        left: 12px;
        z-index: 150;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.4);
        z-index: 199;
        display: none;
    }
    .sidebar-backdrop.show { display: block; }
    
    /* === AGENT SHELL === */
    .agent-shell {
        max-width: 100% !important;
        padding: 12px 12px 80px 12px !important;
        margin: 0 !important;
    }
    
    .top-bar {
        position: sticky;
        top: 0;
        z-index: 100;
        padding: 10px 12px !important;
        margin-bottom: 12px !important;
        border-radius: 12px !important;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .top-bar > div:last-child {
        flex-wrap: wrap;
        gap: 6px !important;
    }
    
    .top-bar .btn {
        padding: 8px 10px !important;
        font-size: 11px !important;
    }
    
    /* === CARDS === */
    .card {
        border-radius: 12px !important;
        margin-bottom: 12px !important;
    }
    
    .card-header {
        padding: 12px !important;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .card-title { font-size: 14px !important; }
    .card-subtitle { font-size: 11px !important; }
    .card-body { padding: 12px !important; }
    
    /* === PAGE HEADER === */
    .page-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 10px;
        margin-bottom: 14px !important;
    }
    
    .page-title { font-size: 20px !important; }
    .page-subtitle { font-size: 12px !important; }
    
    .page-header > .flex {
        width: 100%;
        flex-wrap: wrap;
    }
    
    /* === STATS GRID === */
    .stats-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
    
    .stat-card { padding: 12px !important; }
    .stat-label { font-size: 10px !important; }
    .stat-value { font-size: 20px !important; line-height: 1.1 !important; }
    .stat-meta { font-size: 10px !important; }
    
    /* === FORMS === */
    .form-grid {
        grid-template-columns: 1fr !important;
    }
    
    .form-grid > .form-group[style*="grid-column: span 2"] {
        grid-column: span 1 !important;
    }
    
    .form-label { font-size: 12px !important; }
    
    input, textarea, select {
        padding: 11px 12px !important;
        border-radius: 10px !important;
    }
    
    /* === TABS === */
    .tabs {
        overflow-x: auto;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch;
        gap: 4px !important;
    }
    .tabs::-webkit-scrollbar { display: none; }
    .tab {
        white-space: nowrap;
        padding: 8px 12px !important;
        font-size: 12px !important;
        flex-shrink: 0;
    }
    
    /* === TABLES === */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .data-table { min-width: 600px; }
    .data-table th, .data-table td {
        padding: 8px 6px !important;
        font-size: 12px !important;
    }
    .data-table th { font-size: 10px !important; }
    
    /* === BUTTONS === */
    .btn {
        padding: 10px 14px !important;
        font-size: 13px !important;
    }
    
    /* === MODALS - bottom sheet === */
    .modal-overlay {
        padding: 0 !important;
        align-items: flex-end !important;
    }
    
    .modal {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 95vh !important;
        border-radius: 20px 20px 0 0 !important;
        margin: 0 !important;
        animation: slideUp 0.25s ease-out;
    }
    
    @keyframes slideUp {
        from { transform: translateY(100%); }
        to { transform: translateY(0); }
    }
    
    .modal-header {
        padding: 14px 16px !important;
        position: sticky;
        top: 0;
        background: white;
        z-index: 5;
    }
    
    .modal-header::before {
        content: '';
        position: absolute;
        top: 6px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: #cbd5e1;
        border-radius: 999px;
    }
    
    .modal-title { font-size: 16px !important; margin-top: 8px; }
    .modal-body {
        padding: 14px 16px !important;
        max-height: calc(85vh - 130px);
        overflow-y: auto;
    }
    
    .modal-footer {
        padding: 12px 16px !important;
        position: sticky;
        bottom: 0;
        background: white;
        gap: 8px;
    }
    .modal-footer .btn { flex: 1; }
    
    /* === VISIT-SPECIFIC === */
    .visit-header { padding: 16px !important; }
    .visit-header h1 { font-size: 18px !important; }
    .visit-stats {
        grid-template-columns: 1fr 1fr 1fr 1fr !important;
        gap: 6px !important;
    }
    .visit-stat .v { font-size: 16px !important; }
    .visit-stat .l { font-size: 9px !important; }
    
    .pc-row, .pc-card {
        padding: 12px !important;
        border-radius: 12px !important;
    }
    
    .hw-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }
    .hw-grid label { font-size: 10px !important; }
    
    .photo-upload-row {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
    
    /* === BOTTOM NAV (agent only) === */
    .mobile-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        border-top: 1px solid #e2e8f0;
        display: flex !important;
        justify-content: space-around;
        padding: 6px 4px 8px;
        box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
        z-index: 99;
    }
    
    .mobile-bottom-nav a {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        padding: 6px 2px;
        border-radius: 8px;
        text-decoration: none;
        color: #94a3b8;
        font-size: 10px;
        font-weight: 600;
    }
    
    .mobile-bottom-nav a.active {
        color: #3b82f6;
        background: #eff6ff;
    }
    
    .mobile-bottom-nav .nav-icon-bottom { font-size: 20px; }
    
    /* === PWA banner position adjust on mobile === */
    .pwa-install-banner {
        bottom: 80px !important;
    }
    
    /* === HIDE on mobile === */
    .hide-mobile { display: none !important; }
    
    /* === iOS Safe Area === */
    @supports (padding: max(0px)) {
        .main-content, .agent-shell {
            padding-bottom: max(80px, env(safe-area-inset-bottom)) !important;
        }
        .mobile-bottom-nav {
            padding-bottom: max(8px, env(safe-area-inset-bottom)) !important;
        }
    }
}

/* ===== Larger phones/tablets ===== */
@media (max-width: 480px) {
    .stats-grid {
        grid-template-columns: 1fr 1fr !important;
    }
    
    .page-header > .flex .btn {
        flex: 1;
    }
}

/* ============================================================
   UNIVERSAL MOBILE OVERFLOW FIX - prevents content cut-off
   ============================================================ */
@media (max-width: 768px) {
    /* Hard stop horizontal scroll at root */
    html, body {
        overflow-x: hidden !important;
        width: 100% !important;
        max-width: 100vw !important;
        position: relative;
    }
    
    /* Everything respects box model + never exceeds viewport */
    *, *::before, *::after {
        box-sizing: border-box !important;
        max-width: 100% !important;
    }
    
    /* Containers stay within screen */
    .main-content, .agent-shell, .card, .card-body, .card-header,
    .page-header, section, main, .container {
        max-width: 100% !important;
        overflow-x: hidden;
    }
    
    /* === Force ALL inline multi-column grids to single column === */
    [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }
    /* Exception: keep 2-column for small stat-like pairs that fit */
    .stats-grid[style*="grid-template-columns"],
    .stats-grid {
        grid-template-columns: 1fr 1fr !important;
    }
    
    /* === Inline flex rows wrap instead of overflow === */
    [style*="display: flex"],
    [style*="display:flex"] {
        flex-wrap: wrap !important;
    }
    /* But keep tight button groups / badges on one line where small */
    .badge, .btn-xs, .btn-sm { flex-wrap: nowrap !important; }
    
    /* === Any fixed-width inline element becomes fluid === */
    [style*="width:"][style*="px"],
    [style*="min-width:"][style*="px"] {
        width: auto !important;
        min-width: 0 !important;
    }
    
    /* Wide tables: wrap them so they scroll inside their box */
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100% !important;
        white-space: nowrap;
    }
    /* But data-table inside .table-wrap keeps its own scroll */
    .table-wrap table { display: table; }
    
    /* Images / canvas / video never overflow */
    img, canvas, svg, video, iframe {
        max-width: 100% !important;
        height: auto;
    }
    
    /* Long text / URLs break instead of pushing width */
    p, span, div, td, th, a, label, h1, h2, h3, h4 {
        overflow-wrap: break-word;
        word-wrap: break-word;
    }
    
    /* Preformatted code blocks scroll instead of stretch */
    pre, code {
        overflow-x: auto;
        white-space: pre-wrap;
        word-break: break-word;
        max-width: 100% !important;
    }
    
    /* Modals never exceed screen width */
    .modal, .modal-body {
        max-width: 100vw !important;
        overflow-x: hidden;
    }
    .modal-body [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }
    
    /* PC cards / nested forms in corporate entry - single column */
    .pc-card [style*="grid-template-columns"],
    .health-summary,
    [id$="_summary"] {
        grid-template-columns: 1fr !important;
        max-width: 100% !important;
    }
    
    /* Totals / summary rows wrap */
    [style*="justify-content: flex-end"],
    [style*="justify-content:flex-end"] {
        flex-wrap: wrap !important;
    }
}

/* Extra-small phones */
@media (max-width: 400px) {
    .stats-grid {
        grid-template-columns: 1fr !important;
    }
    .main-content {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }
    .card-body { padding: 12px !important; }
}
