/**
 * Zyra Mobile Native Experience
 * ==============================
 * CSS for native-like iOS/mobile behavior
 * Target: iPhone 16 Safari/WebView
 * 
 * Contents:
 * 1. iOS Safe Area Support
 * 2. Prevent Zoom & Touch Glitches  
 * 3. Mobile Layout Shell
 * 4. Form Input Stability
 * 5. Modal & Sheet Behavior
 * 6. Table Scroll Containment
 * 7. Tap Target Sizing
 * 8. Horizontal Overflow Prevention
 * 9. Text Overflow Guards
 * 10. Sticky Action Bars
 */

/* ==========================================================================
   1. iOS SAFE AREA SUPPORT
   ========================================================================== */

/* Root safe area CSS variables */
:root {
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
}

/* Apply safe area to body for notch support */
body {
    padding-top: var(--safe-area-top);
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

/* Footer respects home indicator */
footer {
    padding-bottom: calc(1rem + var(--safe-area-bottom)) !important;
}

/* Utility classes for safe area */
.safe-area-top {
    padding-top: var(--safe-area-top);
}

.safe-area-bottom {
    padding-bottom: var(--safe-area-bottom);
}

.safe-area-inset {
    padding: var(--safe-area-top) var(--safe-area-right) var(--safe-area-bottom) var(--safe-area-left);
}

/* ==========================================================================
   2. PREVENT ZOOM & TOUCH GLITCHES
   ========================================================================== */

/* Prevent double-tap zoom on interactive elements */
button,
a,
.btn,
.nav-link,
.dropdown-item,
.card,
.list-group-item,
[role="button"],
[onclick] {
    touch-action: manipulation;
}

/* Prevent text selection on UI elements (native feel) */
.no-select,
.btn,
.nav-link,
.dropdown-toggle {
    -webkit-user-select: none;
    user-select: none;
}

/* Prevent callout on long-press */
a, button, .btn {
    -webkit-touch-callout: none;
}

/* Smooth momentum scrolling for scroll containers */
.scroll-container,
.table-responsive,
.overflow-auto,
.modal-body {
    -webkit-overflow-scrolling: touch;
}

/* ==========================================================================
   3. MOBILE LAYOUT SHELL
   ========================================================================== */

/* Dynamic viewport height fix (avoids iOS 100vh bug) */
@supports (height: 100dvh) {
    .full-height,
    .min-full-height {
        min-height: 100dvh;
    }
}

@supports not (height: 100dvh) {
    .full-height,
    .min-full-height {
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }
}

/* Mobile page shell pattern */
@media (max-width: 768px) {
    .mobile-page-shell {
        display: flex;
        flex-direction: column;
        min-height: 100dvh;
        min-height: -webkit-fill-available;
    }
    
    .mobile-page-shell .mobile-header {
        flex-shrink: 0;
        position: sticky;
        top: 0;
        z-index: 100;
        background: inherit;
    }
    
    .mobile-page-shell .mobile-content {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }
    
    .mobile-page-shell .mobile-footer-actions {
        flex-shrink: 0;
        position: sticky;
        bottom: 0;
        z-index: 100;
        padding-bottom: var(--safe-area-bottom);
        background: inherit;
    }
}

/* ==========================================================================
   4. FORM INPUT STABILITY (Prevent iOS Zoom)
   ========================================================================== */

/* iOS zooms on inputs < 16px - enforce minimum */
@media (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    select,
    textarea {
        font-size: 16px !important;
    }
    
    /* Form control sizing */
    .form-control,
    .form-select {
        font-size: 16px !important;
        min-height: 44px;
    }
}

/* ==========================================================================
   5. MODAL & SHEET BEHAVIOR
   ========================================================================== */

/* Mobile modals as full-width sheets */
@media (max-width: 576px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100%;
    }
    
    .modal-content {
        border: none;
        border-radius: 0;
        min-height: 100vh;
        min-height: 100dvh;
    }
    
    /* Bottom sheet variant */
    .modal-dialog.modal-bottom-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        margin: 0;
        min-height: auto;
        max-height: 90vh;
        max-height: 90dvh;
    }
    
    .modal-dialog.modal-bottom-sheet .modal-content {
        border-radius: 16px 16px 0 0;
        min-height: auto;
        max-height: 90vh;
        max-height: 90dvh;
        padding-bottom: var(--safe-area-bottom);
    }
}

/* Scroll lock when modal is open */
body.modal-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   6. TABLE SCROLL CONTAINMENT
   ========================================================================== */

/* Tables scroll horizontally, not break layout */
.table-responsive,
.table-scroll-container {
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}

/* Table minimum width to prevent cramping */
@media (max-width: 576px) {
    .table-responsive table {
        min-width: 500px;
    }
    
    /* Compact table variant */
    .table-responsive.table-compact table {
        min-width: 100%;
    }
    
    .table-compact td,
    .table-compact th {
        padding: 0.5rem 0.25rem;
        font-size: 0.875rem;
    }
}

/* ==========================================================================
   7. TAP TARGET SIZING (44px minimum for iOS)
   ========================================================================== */

@media (max-width: 768px) {
    /* Primary buttons */
    .btn,
    button:not(.btn-sm):not(.btn-close) {
        min-height: 44px;
        min-width: 44px;
        padding: 0.75rem 1rem;
    }
    
    /* Navigation links */
    .nav-link {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    /* Dropdown items */
    .dropdown-item {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    /* List items */
    .list-group-item {
        min-height: 44px;
    }
    
    /* Icon buttons */
    .btn-icon,
    .icon-btn {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Close buttons */
    .btn-close {
        width: 44px;
        height: 44px;
        padding: 0;
    }
}

/* ==========================================================================
   8. HORIZONTAL OVERFLOW PREVENTION
   ========================================================================== */

/* Prevent accidental horizontal scroll at page level */
html {
    overflow-x: hidden;
}

body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

/* Containers respect viewport - EXCLUDE navbar for dropdown support */
main,
section,
main .container,
main .container-fluid {
    max-width: 100%;
    overflow-x: hidden;
}

/* Navbar must allow overflow for dropdowns */
.navbar,
.navbar .container-fluid,
.navbar .container {
    overflow: visible !important;
}

/* Allow intentional scroll containers */
.scroll-x,
.table-responsive,
.overflow-x-auto {
    overflow-x: auto !important;
}

/* ==========================================================================
   9. TEXT OVERFLOW GUARDS
   ========================================================================== */

/* Truncate long text */
.truncate,
.text-truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

/* Multi-line truncate (2 lines) */
.truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Multi-line truncate (3 lines) */
.truncate-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Word break for long words */
.break-word {
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    hyphens: auto;
}

/* ==========================================================================
   10. STICKY ACTION BARS
   ========================================================================== */

@media (max-width: 768px) {
    /* Sticky bottom action bar */
    .sticky-actions,
    .fixed-bottom-actions {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1030;
        background: rgba(26, 26, 26, 0.98);
        backdrop-filter: blur(20px);
        padding: 1rem;
        padding-bottom: calc(1rem + var(--safe-area-bottom));
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
    }
    
    /* Add spacing to body when sticky actions present */
    body.has-sticky-actions {
        padding-bottom: calc(80px + var(--safe-area-bottom));
    }
    
    /* Keyboard-safe spacing */
    .keyboard-safe-bottom {
        padding-bottom: calc(1rem + var(--safe-area-bottom));
    }
}

/* ==========================================================================
   11. MOBILE-SPECIFIC UTILITIES
   ========================================================================== */

/* Hide on mobile */
@media (max-width: 768px) {
    .hide-mobile {
        display: none !important;
    }
}

/* Show only on mobile */
@media (min-width: 769px) {
    .show-mobile-only {
        display: none !important;
    }
}

/* Compact spacing on mobile */
@media (max-width: 768px) {
    .mobile-compact {
        padding: 0.5rem !important;
    }
    
    .mobile-compact-x {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }
    
    .mobile-compact-y {
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
    }
    
    /* Reduce margins on mobile */
    .mobile-m-0 {
        margin: 0 !important;
    }
    
    .mobile-p-2 {
        padding: 0.5rem !important;
    }
    
    .mobile-p-3 {
        padding: 1rem !important;
    }
}

/* ==========================================================================
   12. CARDS & COMPONENTS - MAX WIDTH GUARDS
   ========================================================================== */

/* Ensure cards don't expand beyond viewport */
.card,
.list-group,
.alert,
.toast {
    max-width: 100%;
}

@media (max-width: 576px) {
    .card {
        border-radius: 12px;
    }
    
    .card-body {
        padding: 1rem;
    }
}

/* ==========================================================================
   13. ACCESSIBILITY - FOCUS STATES
   ========================================================================== */

/* Visible focus for accessibility */
:focus-visible {
    outline: 2px solid var(--glow-teal, #00FFC6);
    outline-offset: 2px;
}

/* Remove focus ring on tap (native feel) */
@media (hover: none) and (pointer: coarse) {
    *:focus {
        outline: none;
    }
    
    *:focus-visible {
        outline: 2px solid var(--glow-teal, #00FFC6);
        outline-offset: 2px;
    }
}

