/* ==========================================================================
   IPHONE STANDALONE WEB APP / PWA SPECIFIC OPTIMIZATIONS
   ========================================================================== */

html.standalone-app {
    overscroll-behavior-y: none;
    -webkit-text-size-adjust: 100%;
}

html.standalone-app body {
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: none;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
    /* Disable default top padding on body, handled by the topbar/navbar */
    padding-top: 0 !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
    padding-left: env(safe-area-inset-left) !important;
    padding-right: env(safe-area-inset-right) !important;
}

/* Soft page-enter for installed Web Clip */
html.standalone-app.app-page-enter body {
    opacity: 0;
    transform: translateY(8px);
}
html.standalone-app.app-page-enter-ready body {
    opacity: 1;
    transform: none;
    transition: opacity 0.2s ease-out, transform 0.22s ease-out;
}
html.standalone-app.app-navigating body {
    opacity: 0.55;
    transition: opacity 0.12s ease-out;
}

/* Faster tap response */
html.standalone-app a,
html.standalone-app button,
html.standalone-app .btn,
html.standalone-app .card,
html.standalone-app .sidebar-link,
html.standalone-app .nav-action-btn {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
html.standalone-app a:active,
html.standalone-app .sidebar-link:active,
html.standalone-app .nav-action-btn:active {
    opacity: 0.72;
    transform: scale(0.98);
}
html.standalone-app .btn:active,
html.standalone-app button.btn:active {
    transform: scale(0.96);
    filter: brightness(1.05);
}
html.standalone-app .card:active {
    transform: scale(0.995);
}

/* Drawer slide feel — mobile overlays only (desktop tabs stay visible in the header) */
@media (max-width: 991px) {
    html.standalone-app .panel-nav-drawer,
    html.standalone-app #navLinks.panel-nav-drawer,
    html.standalone-app #mobileMenu,
    html.standalone-app .mobile-menu {
        transition: transform 0.22s ease, opacity 0.2s ease, visibility 0.2s ease !important;
    }
    html.standalone-app .panel-nav-drawer:not(.active),
    html.standalone-app #navLinks.panel-nav-drawer:not(.active),
    html.standalone-app #mobileMenu:not(.active),
    html.standalone-app .mobile-menu:not(.active) {
        transform: translateX(12px) !important;
        opacity: 0 !important;
    }
    html.standalone-app .panel-nav-drawer.active,
    html.standalone-app #navLinks.panel-nav-drawer.active,
    html.standalone-app #mobileMenu.active,
    html.standalone-app .mobile-menu.active {
        transform: translateX(0) !important;
        opacity: 1 !important;
    }
}

html.standalone-app .panel-shell,
html.standalone-app .panel-main,
html.standalone-app .panel-content,
html.standalone-app .page-container,
html.standalone-app main,
html.standalone-app .auth-page {
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
}

/* Standalone: topbar without grid layout only */
html.standalone-app .panel-topbar:not(.panel-header-bar) {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
}

/* Let top header backgrounds flow seamlessly under the status bar (notch / dynamic island) */
html.standalone-app .navbar,
html.standalone-app .panel-nav-shell,
html.standalone-app .panel-navbar,
html.standalone-app .panel-topbar,
html.standalone-app .panel-topbar.panel-header-bar {
    position: sticky !important;
    top: 0 !important;
    z-index: 200010 !important;
    padding-top: env(safe-area-inset-top) !important;
    height: calc(54px + env(safe-area-inset-top)) !important;
    min-height: calc(54px + env(safe-area-inset-top)) !important;
    max-height: calc(54px + env(safe-area-inset-top)) !important;
    flex: 0 0 auto !important;
    box-sizing: border-box !important;
}

/* Menu overlay + drawer must sit above all dashboard/page content in Web Clip */
html.standalone-app .panel-menu-overlay {
    z-index: 199950 !important;
}

html.standalone-app .panel-menu-overlay.active {
    display: block !important;
    z-index: 200005 !important;
    pointer-events: auto !important;
}

@media (max-width: 991px) {
    html.standalone-app .panel-nav-drawer:not(.active),
    html.standalone-app #navLinks.panel-nav-drawer:not(.active),
    html.standalone-app #mobileMenu:not(.active),
    html.standalone-app .mobile-menu:not(.active) {
        display: none !important;
        pointer-events: none !important;
        visibility: hidden !important;
    }

    html.standalone-app .panel-nav-drawer.active,
    html.standalone-app #navLinks.panel-nav-drawer.active,
    html.standalone-app #mobileMenu.active,
    html.standalone-app .mobile-menu.active {
        display: flex !important;
        z-index: 200009 !important;
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
        transform: none !important;
        left: auto !important;
        right: 0 !important;
        width: min(340px, 86vw) !important;
    }

    html.standalone-app .panel-nav-drawer.active a,
    html.standalone-app #navLinks.panel-nav-drawer.active a,
    html.standalone-app #mobileMenu.active a {
        position: relative;
        z-index: 1;
        pointer-events: auto !important;
        touch-action: manipulation;
        cursor: pointer;
    }
}

/* Desktop / iPad landscape: keep text tabs in the sticky header (never require .active) */
@media (min-width: 992px) {
    html.standalone-app .panel-nav-shell .panel-nav-drawer,
    html.standalone-app .panel-nav-shell .panel-nav-drawer:not(.active),
    html.standalone-app .panel-header-bar > .panel-nav-drawer,
    html.standalone-app .panel-header-bar > .panel-nav-drawer:not(.active),
    html.standalone-app #navLinks.panel-nav-drawer,
    html.standalone-app #navLinks.panel-nav-drawer:not(.active) {
        display: flex !important;
        position: static !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        width: auto !important;
        max-width: 100% !important;
        height: auto !important;
        inset: auto !important;
        padding: 0 !important;
        margin: 0 !important;
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        transform: none !important;
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
    }

    html.standalone-app .panel-nav-drawer .panel-drawer-head,
    html.standalone-app .panel-nav-drawer .panel-drawer-logout {
        display: none !important;
    }

    html.standalone-app .panel-logout-btn {
        display: inline-flex !important;
    }
}

html.standalone-app .sidebar-overlay.active {
    display: block !important;
    z-index: 200015 !important;
}

html.standalone-app .panel-sidebar.open {
    z-index: 200020 !important;
}

html.standalone-app .panel-header-bar .nav-actions,
html.standalone-app .panel-header-bar .hamburger,
html.standalone-app .panel-header-bar .nav-action-btn,
html.standalone-app .navbar .hamburger,
html.standalone-app .navbar .nav-action-btn {
    position: relative;
    z-index: 200011 !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

html.standalone-app body.panel-menu-open .panel-content,
html.standalone-app body.panel-menu-open main,
html.standalone-app body.panel-menu-open .page-container {
    pointer-events: none;
}

html.standalone-app .table-responsive,
html.standalone-app .modal-content,
html.standalone-app .search-results {
    -webkit-overflow-scrolling: touch;
}

/* Enforce 16px font-size on all mobile inputs to block Safari's automatic zoom-on-focus glitch */
html.standalone-app input,
html.standalone-app select,
html.standalone-app textarea,
input:focus,
select:focus,
textarea:focus {
    font-size: 16px !important;
    touch-action: manipulation;
}

html.standalone-app a,
html.standalone-app button,
html.standalone-app .btn {
    touch-action: manipulation;
}

/* Push floating chat button safely above home indicator */
html.standalone-app #chatWidgetBtn,
html.standalone-app .chat-widget-button {
    bottom: calc(20px + env(safe-area-inset-bottom)) !important;
}

/* Push footer safe area in standalone mode */
html.standalone-app .panel-footer,
html.standalone-app footer {
    padding-bottom: calc(1.2rem + env(safe-area-inset-bottom)) !important;
}

/* Glassmorphic Loader Overlay for In-App Transitions */
.app-spa-loader {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999999;
    background: rgba(15, 12, 25, 0.75);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease-out, visibility 0.25s ease-out;
}

.app-spa-loader.active {
    opacity: 1;
    visibility: visible;
    pointer-events: all;
}

.app-spa-spinner-wrap {
    position: relative;
    width: 64px;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.app-spa-spinner {
    width: 48px;
    height: 48px;
    border: 3.5px solid rgba(128, 90, 213, 0.15);
    border-top: 3.5px solid #805AD5;
    border-radius: 50%;
    animation: app-spa-spin 0.9s cubic-bezier(0.55, 0.15, 0.45, 0.85) infinite;
    box-shadow: 0 0 12px rgba(128, 90, 213, 0.1);
}

.app-spa-spinner-glow {
    position: absolute;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    box-shadow: 0 0 25px rgba(128, 90, 213, 0.4);
    opacity: 0.6;
    animation: app-spa-pulse 1.8s ease-in-out infinite;
}

.app-spa-loader-text {
    margin-top: 20px;
    color: #E2E8F0;
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 1px;
    text-transform: uppercase;
    opacity: 0.85;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

@keyframes app-spa-spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

@keyframes app-spa-pulse {
    0%, 100% {
        transform: scale(0.95);
        opacity: 0.4;
    }
    50% {
        transform: scale(1.05);
        opacity: 0.7;
    }
}

/* ==========================================================================
   GLOBAL MOBILE RESPONSIVE OPTIMIZATIONS (ALL MOBILE DEVICES & BROWSERS)
   ========================================================================== */
@media (max-width: 768px) {
    /* STRICTLY ELIMINATE ALL HORIZONTAL OVERFLOW & SCROLL JITTER ON IOS */
    html,
    body {
        max-width: 100% !important;
        overflow-x: hidden !important;
        position: relative !important;
    }

    [class*="col-"],
    .card,
    .panel-content,
    .page-container,
    .row {
        min-width: 0 !important; /* Fix flexbox auto-width expansion bug */
    }

    html {
        font-size: 13.2px !important; /* Scales down all rem-based typography & paddings by ~18% for a native compact feel */
        --header-height: 54px !important;
    }

    body {
        line-height: 1.45 !important;
    }

    .container {
        padding-left: 0.65rem !important;
        padding-right: 0.65rem !important;
    }

    /* Headings adjustments */
    h1, 
    .page-title {
        font-size: 1.5rem !important;
        letter-spacing: -0.02em !important;
    }

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

    h3 {
        font-size: 1.05rem !important;
    }

    /* Cards scaling */
    .card,
    .welcome-hero,
    .vault-card {
        padding: 0.9rem 1rem !important;
        margin-bottom: 0.75rem !important;
        border-radius: 10px !important;
    }

    /* Stat values scaling */
    .stat-value {
        font-size: 2rem !important;
        font-weight: 800 !important;
    }

    .stat-label {
        font-size: 0.75rem !important;
        letter-spacing: 0.5px !important;
    }

    /* Mini stat grid and boxes */
    .mini-stat-grid {
        gap: 0.5rem !important;
    }

    .mini-stat-box {
        padding: 0.6rem !important;
        border-radius: 6px !important;
    }

    .mini-stat-box .value {
        font-size: 1.1rem !important;
    }

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

    /* Button and Input sizing */
    .btn {
        padding: 0.5rem 0.9rem !important;
        font-size: 0.82rem !important;
        border-radius: 6px !important;
    }

    .btn-sm {
        padding: 0.4rem 0.65rem !important;
        font-size: 0.75rem !important;
        border-radius: 5px !important;
    }

    .glass-input {
        padding: 0.55rem 0.75rem !important;
        font-size: 16px !important; /* Force 16px to prevent zoom */
        border-radius: 6px !important;
        margin-bottom: 0.75rem !important;
    }

    .form-group {
        margin-bottom: 0.75rem !important;
    }

    .form-label {
        font-size: 0.8rem !important;
        margin-bottom: 0.4rem !important;
    }

    /* Spacing adjustments */
    .page-container {
        padding-top: 0.6rem !important;
        padding-bottom: 1.2rem !important;
    }

    .row {
        margin: -0.3rem !important;
    }

    [class*="col-"] {
        padding: 0.3rem !important;
    }

    /* Table cell padding compacting */
    table th,
    table td {
        padding: 0.45rem 0.6rem !important;
    }

    .table-responsive {
        display: block !important;
        width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        margin-bottom: 1rem !important;
        border: 1px solid rgba(255, 255, 255, 0.05) !important;
        border-radius: 8px !important;
    }

    .table-stack,
    .table-stack tbody {
        display: block !important;
        width: 100% !important;
    }

    .table-stack td[data-label="Actions"],
    .table-stack td[data-label="Action"],
    .table-stack td[data-label="Credit"],
    .table-stack td.td-actions {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.5rem !important;
        overflow: visible !important;
    }

    .row-actions {
        display: flex !important;
        width: 100% !important;
        flex-direction: column !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    .row-actions form,
    .row-actions .btn,
    .row-actions button,
    .row-actions a.btn {
        display: flex !important;
        width: 100% !important;
        min-height: 44px !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    .panel-modal {
        z-index: 300010 !important;
    }

    .panel-content {
        overflow-x: clip !important;
        overflow-y: visible !important;
    }

    .card:has(.table-stack),
    .table-responsive:has(.table-stack) {
        overflow: visible !important;
    }

    /* Navbar/Header styling for mobile standalone & browser.
       Admin topbar uses grid via .panel-header-bar — do not force flex on it. */
    .panel-navbar,
    .navbar {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        height: 54px !important;
        padding: 0 0.65rem !important;
        z-index: 99999 !important;
    }

    .panel-topbar,
    .panel-topbar.panel-header-bar {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) auto !important;
        align-items: center !important;
        height: 54px !important;
        min-height: 54px !important;
        max-height: 54px !important;
        padding: 0 0.65rem !important;
        z-index: 99999 !important;
        flex: 0 0 auto !important;
        box-sizing: border-box !important;
    }

    .navbar .logo,
    .panel-navbar .logo {
        display: flex !important;
        align-items: center !important;
        gap: 0.4rem !important;
    }

    .navbar .logo-text,
    .panel-navbar .logo-text {
        font-size: 0.95rem !important;
    }

    .navbar .logo img,
    .panel-navbar .logo img {
        height: 24px !important;
    }

    /* Highlighted, globally visible glowing hamburger & sidebar-toggle buttons on mobile */
    .hamburger,
    .sidebar-toggle {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 38px !important;
        height: 38px !important;
        background: rgba(128, 90, 213, 0.15) !important; /* Brand purple glass */
        border: 1.5px solid #805AD5 !important; /* Highlighted Purple Border */
        border-radius: 8px !important;
        font-size: 1.15rem !important; /* For character icons like ☰ */
        color: #ffffff !important;
        padding: 0 !important;
        margin: 0 !important;
        cursor: pointer !important;
        z-index: 999999 !important;
        box-shadow: 0 0 10px rgba(128, 90, 213, 0.35) !important; /* Glowing brand highlight */
        transition: all 0.2s ease !important;
    }

    .hamburger:active,
    .sidebar-toggle:active {
        transform: scale(0.92) !important;
        background: rgba(128, 90, 213, 0.3) !important;
        box-shadow: 0 0 15px rgba(128, 90, 213, 0.6) !important;
    }

    .hamburger span {
        display: block !important;
        width: 18px !important;
        height: 2px !important;
        background-color: #ffffff !important; /* High contrast white lines */
        margin: 2px 0 !important;
        border-radius: 99px !important;
        transition: all 0.2s ease !important;
    }

    .hamburger.active span:nth-child(1) {
        transform: rotate(45deg) translate(5px, 5px) !important;
    }

    .hamburger.active span:nth-child(2) {
        opacity: 0 !important;
    }

    .hamburger.active span:nth-child(3) {
        transform: rotate(-45deg) translate(5px, -5px) !important;
    }

    /* Format admin header text on mobile to avoid squishing */
    .topbar-left > div {
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        min-width: 0 !important;
    }

    .topbar-title {
        font-size: 1.05rem !important;
        font-weight: 700 !important;
        line-height: 1.2 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    .topbar-subtitle {
        display: none !important; /* Hide subtitle on mobile to preserve height & keep alignment clean */
    }

    .nav-action-btn {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 36px !important;
        height: 36px !important;
        font-size: 0.9rem !important;
        border-radius: 8px !important;
        background: var(--bg-elevated) !important;
        border: 1px solid var(--glass-border) !important;
    }

    /* Mobile overlays only — do not hide desktop inline #navLinks tabs */
    .navbar .nav-links:not(.panel-nav-drawer):not(.active),
    .panel-navbar .nav-links:not(.panel-nav-drawer):not(.active),
    .panel-nav-drawer:not(.active),
    #navLinks.panel-nav-drawer:not(.active),
    #mobileMenu:not(.active),
    .mobile-menu:not(.active) {
        display: none !important;
        pointer-events: none !important;
        visibility: hidden !important;
    }

    .panel-nav-drawer.active,
    #navLinks.panel-nav-drawer.active,
    #mobileMenu.active,
    .mobile-menu.active {
        display: flex !important;
        position: fixed !important;
        top: 0 !important;
        left: auto !important;
        right: 0 !important;
        bottom: 0 !important;
        width: min(340px, 86vw) !important;
        flex-direction: column !important;
        padding: calc(54px + env(safe-area-inset-top, 0px) + 0.75rem) 1.25rem 1rem !important;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px)) !important;
        background: linear-gradient(180deg, #141417 0%, #0a0a0b 100%) !important;
        backdrop-filter: blur(25px) !important;
        border-left: 1px solid rgba(128, 90, 213, 0.28) !important;
        border-top: none !important;
        box-shadow: -16px 0 48px rgba(0, 0, 0, 0.55) !important;
        z-index: 200009 !important;
        transform: none !important;
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }

    #mobileMenu.active a,
    .mobile-menu.active a,
    .panel-nav-drawer.active .nav-item {
        color: #f1f5f9 !important;
        font-weight: 600 !important;
        padding: 14px 16px !important;
        border-radius: 12px !important;
        background: rgba(255, 255, 255, 0.03) !important;
        border: 1px solid rgba(255, 255, 255, 0.06) !important;
        width: 100% !important;
        display: flex !important;
        align-items: center !important;
    }

    .panel-nav-drawer.active .nav-item.active {
        background: rgba(128, 90, 213, 0.2) !important;
        border-color: rgba(128, 90, 213, 0.4) !important;
        color: #fff !important;
    }

    .panel-nav-drawer.active .panel-drawer-logout {
        display: flex !important;
        width: 100% !important;
        margin-top: auto !important;
        min-height: 48px !important;
        justify-content: center !important;
    }

    .panel-logout-btn {
        display: none !important;
    }

    .panel-menu-overlay.active {
        z-index: 200005 !important;
    }

    /* Transform boxy page header card into clean modern typography */
    .page-header {
        background: none !important;
        border: none !important;
        padding: 0 !important;
        margin-top: 0.5rem !important;
        margin-bottom: 1.1rem !important;
        border-radius: 0 !important;
        box-shadow: none !important;
    }

    .page-header::before {
        display: none !important; /* Hide top color line */
    }

    .page-header h1 {
        font-size: 1.45rem !important;
        font-weight: 800 !important;
        background: none !important;
        -webkit-text-fill-color: initial !important;
        color: var(--text-primary) !important;
    }

    .page-header p {
        font-size: 0.82rem !important;
        color: var(--text-muted) !important;
        margin-top: 0.15rem !important;
        text-shadow: none !important;
    }

    .page-header-actions {
        margin-top: 0.5rem !important;
        gap: 0.4rem !important;
    }
}
