/**
 * CAMPULSE SOLUTION - Mobile & Native App Styling Layer
 * Optimizes touch targets, safe-area insets, horizontal scroll containment,
 * input zooming prevention, responsive tables, and PWA mobile UI enhancements.
 */

:root {
    --sat: env(safe-area-inset-top, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --mobile-tap-min: 44px;
}

/* ── 1. GLOBAL MOBILE TOUCH DEFAULTS ── */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: manipulation;
}

body {
    max-width: 100vw;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
    padding-top: var(--sat);
    padding-left: var(--sal);
    padding-right: var(--sar);
}

/* Prevent unwanted layout breakage from wide media */
img, svg, video, canvas {
    max-width: 100%;
    height: auto;
}

/* ── 2. INPUT ZOOM PREVENTION ON MOBILE SAFARI & WEBVIEWS ── */
@media (max-width: 768px) {
    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="email"],
    input[type="tel"],
    input[type="search"],
    select,
    textarea,
    .form-control,
    .form-select {
        font-size: 16px !important; /* Prevents auto-zoom on iOS */
        border-radius: 12px;
    }
}

/* ── 3. TOUCH TARGET SIZING ── */
button, 
.btn, 
.nav-link, 
.portal-action-btn, 
.module-btn, 
.section-nav-btn {
    touch-action: manipulation;
}

@media (max-width: 768px) {
    button:not(.btn-sm):not(.close):not(.tiny-btn),
    .btn:not(.btn-sm),
    .form-control,
    .form-select {
        min-height: var(--mobile-tap-min);
    }
}

/* ── 4. RESPONSIVE TABLES & HORIZONTAL OVERFLOW SHIELDS ── */
.table-responsive,
.table-wrap,
.data-table-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 14px;
    margin-bottom: 1rem;
    position: relative;
}

/* Subtle scroll hint shadow */
.table-responsive::-webkit-scrollbar {
    height: 5px;
}
.table-responsive::-webkit-scrollbar-track {
    background: rgba(148, 163, 184, 0.1);
    border-radius: 4px;
}
.table-responsive::-webkit-scrollbar-thumb {
    background: rgba(99, 159, 255, 0.4);
    border-radius: 4px;
}

@media (max-width: 768px) {
    .table th, .table td {
        padding: 8px 10px !important;
        font-size: 0.82rem;
    }
}

/* ── 5. DASHBOARDS MOBILE OPTIMIZATION ── */
@media (max-width: 768px) {
    /* Student Dashboard */
    .profile-grid, .receipt-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .student-photo, .teacher-photo {
        width: 110px !important;
        height: 110px !important;
        margin: 0 auto 12px auto !important;
    }

    .module-nav-grid, .section-nav-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .module-btn, .section-nav-btn {
        min-height: 60px !important;
        padding: 10px 14px !important;
    }

    /* Principal Dashboard */
    .overview-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    .card3d {
        padding: 14px 12px !important;
        border-radius: 14px !important;
    }

    .card3d .value {
        font-size: 1.45rem !important;
    }

    /* Topbars */
    .topbar {
        padding: 8px 12px !important;
    }
    
    .topbar .container,
    .topbar-inner {
        flex-direction: row !important;
        justify-content: space-between !important;
        gap: 8px !important;
    }

    .top-menu-btn {
        min-width: auto !important;
        padding: 6px 12px !important;
        font-size: 0.8rem !important;
    }
}

/* ── 6. PWA INSTALL BANNER ON MOBILE ── */
.pwa-install-banner {
    position: fixed;
    bottom: max(16px, env(safe-area-inset-bottom, 16px));
    left: 14px;
    right: 14px;
    background: linear-gradient(135deg, rgba(11, 21, 48, 0.96), rgba(15, 23, 42, 0.98));
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(99, 159, 255, 0.4);
    border-radius: 18px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    z-index: 99999;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), 0 0 30px rgba(37, 99, 235, 0.35);
    transform: translateY(120%);
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    color: #fff;
}

.pwa-install-banner.show {
    transform: translateY(0);
}

.pwa-install-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.pwa-install-brand img {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    flex-shrink: 0;
}

.pwa-install-copy {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.pwa-install-copy strong {
    font-size: 0.85rem;
    color: #ffffff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.pwa-install-copy span {
    font-size: 0.72rem;
    color: #94a3b8;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.pwa-install-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.pwa-btn-install {
    padding: 8px 16px;
    background: linear-gradient(135deg, #2563eb, #7c3aed);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
}

.pwa-btn-close {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    color: #cbd5e1;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    cursor: pointer;
}

/* ── 7. STANDALONE DISPLAY MODE (WHEN RUNNING INSIDE APP) ── */
@media all and (display-mode: standalone) {
    /* When installed as native app, remove redundant install banner */
    .pwa-install-banner {
        display: none !important;
    }
    
    /* Add safe header spacing for Android/iOS status bars */
    .topbar, .top-nav, header.topbar {
        padding-top: max(8px, var(--sat)) !important;
    }
}
