/**
 * Global Mobile-First CSS
 * Applies to entire application for consistent mobile UX
 * 
 * Purpose: Create native app-like experience across all pages
 * Priority: Mobile-first, progressive enhancement for desktop
 * 
 * Usage: Include in header after Bootstrap but before page-specific CSS
 */

/* ===================================
   MOBILE-FIRST BASE RESETS
   =================================== */

/* Prevent horizontal scroll on mobile */
html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Better text rendering on mobile */
body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* Prevent text size adjustment on orientation change */
html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
}

/* ===================================
   TOUCH INTERACTIONS
   =================================== */

/* Remove tap highlight on iOS/Android */
* {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-touch-callout: none;
}

/* Allow text selection for inputs and content */
input, textarea, p, div, span, a {
    -webkit-user-select: text;
    user-select: text;
}

/* Prevent accidental text selection on buttons */
button, .btn {
    -webkit-user-select: none;
    user-select: none;
}

/* Touch-friendly button sizing */
@media (max-width: 767px) {
    .btn, button, a.btn {
        min-height: 44px;
        min-width: 44px;
        padding: 0.625rem 1rem;
        font-size: 0.9375rem;
    }
    
    .btn-sm {
        min-height: 38px;
        min-width: 38px;
        padding: 0.5rem 0.75rem;
        font-size: 0.8125rem;
    }
    
    .btn-lg {
        min-height: 50px;
        min-width: 50px;
        padding: 0.75rem 1.25rem;
        font-size: 1.0625rem;
    }
}

/* Touch feedback for interactive elements */
@media (hover: none) and (pointer: coarse) {
    .btn:active,
    a:active,
    button:active,
    .card:active,
    .list-group-item:active {
        opacity: 0.7;
        transform: scale(0.98);
        transition: all 0.1s ease;
    }
}

/* ===================================
   MOBILE TYPOGRAPHY
   =================================== */

@media (max-width: 767px) {
    /* Base font size for mobile */
    body {
        font-size: 0.9375rem; /* 15px */
        line-height: 1.6;
    }
    
    /* Headings optimized for mobile */
    h1, .h1 {
        font-size: 1.75rem; /* 28px */
        line-height: 1.3;
        margin-bottom: 1rem;
    }
    
    h2, .h2 {
        font-size: 1.5rem; /* 24px */
        line-height: 1.3;
        margin-bottom: 0.875rem;
    }
    
    h3, .h3 {
        font-size: 1.25rem; /* 20px */
        line-height: 1.4;
        margin-bottom: 0.75rem;
    }
    
    h4, .h4 {
        font-size: 1.125rem; /* 18px */
        line-height: 1.4;
        margin-bottom: 0.75rem;
    }
    
    h5, .h5 {
        font-size: 1rem; /* 16px */
        line-height: 1.4;
        margin-bottom: 0.625rem;
    }
    
    h6, .h6 {
        font-size: 0.9375rem; /* 15px */
        line-height: 1.4;
        margin-bottom: 0.625rem;
    }
    
    /* Paragraph spacing */
    p {
        margin-bottom: 1rem;
        line-height: 1.6;
    }
    
    /* Link styling */
    a {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        text-decoration: underline;
        text-decoration-thickness: 1px;
        text-underline-offset: 2px;
    }
    
    a:hover {
        text-decoration-thickness: 2px;
    }
}

/* ===================================
   MOBILE FORM ELEMENTS
   =================================== */

@media (max-width: 767px) {
    .form-control,
    .form-select,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    select,
    textarea {
        min-height: 44px;
        padding: 0.625rem 0.875rem;
        font-size: 0.9375rem;
        border-radius: 8px;
        border: 2px solid #dee2e6;
    }
    
    .form-control:focus,
    .form-select:focus {
        border-color: #0d6efd;
        box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.15);
    }
    
    /* Checkbox and radio sizing */
    .form-check-input {
        width: 24px;
        height: 24px;
        margin-top: 0;
        cursor: pointer;
    }
    
    .form-check-label {
        padding-left: 0.5rem;
        cursor: pointer;
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    /* Form labels */
    .form-label,
    label {
        font-weight: 600;
        margin-bottom: 0.5rem;
        font-size: 0.875rem;
        color: #212529;
    }
    
    /* Textarea */
    textarea.form-control {
        min-height: 100px;
        resize: vertical;
    }
}

/* ===================================
   MOBILE CARDS & CONTAINERS
   =================================== */

@media (max-width: 767px) {
    .card {
        border-radius: 12px;
        margin-bottom: 1rem;
        border: 1px solid rgba(0, 0, 0, 0.1);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    }
    
    .card-header {
        padding: 0.875rem 1rem;
        border-radius: 12px 12px 0 0;
        background: #f8f9fa;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    }
    
    .card-body {
        padding: 1rem;
    }
    
    .card-footer {
        padding: 0.875rem 1rem;
        border-top: 1px solid rgba(0, 0, 0, 0.1);
        background: #f8f9fa;
        border-radius: 0 0 12px 12px;
    }
}

/* ===================================
   MOBILE NAVIGATION
   =================================== */

@media (max-width: 767px) {
    /* Navbar */
    .navbar {
        padding: 0.75rem 1rem;
    }
    
    .navbar-brand {
        font-size: 1.125rem;
    }
    
    .navbar-toggler {
        min-height: 44px;
        min-width: 44px;
        padding: 0.5rem 0.75rem;
        border: 2px solid rgba(0, 0, 0, 0.1);
    }
    
    .navbar-nav .nav-link {
        min-height: 44px;
        padding: 0.75rem 1rem;
        display: flex;
        align-items: center;
        border-radius: 8px;
        margin-bottom: 0.25rem;
    }
    
    .navbar-nav .nav-link:active {
        background: rgba(0, 0, 0, 0.05);
    }
    
    /* Dropdown menus */
    .dropdown-menu {
        border-radius: 12px;
        border: 1px solid rgba(0, 0, 0, 0.1);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        padding: 0.5rem;
    }
    
    .dropdown-item {
        min-height: 44px;
        padding: 0.75rem 1rem;
        border-radius: 8px;
        display: flex;
        align-items: center;
        margin-bottom: 0.25rem;
    }
    
    .dropdown-item:active {
        background: rgba(13, 110, 253, 0.1);
    }
}

/* ===================================
   MOBILE TABLES
   =================================== */

@media (max-width: 767px) {
    .table-responsive {
        border-radius: 12px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .table {
        font-size: 0.875rem;
    }
    
    .table thead th {
        padding: 0.75rem 0.5rem;
        font-size: 0.8125rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        white-space: nowrap;
    }
    
    .table tbody td {
        padding: 0.75rem 0.5rem;
        min-height: 44px;
    }
    
    /* Stack table on very small screens */
    @media (max-width: 576px) {
        .table-mobile-stack thead {
            display: none;
        }
        
        .table-mobile-stack tr {
            display: block;
            margin-bottom: 1rem;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 0.75rem;
        }
        
        .table-mobile-stack td {
            display: block;
            text-align: left !important;
            padding: 0.5rem 0;
            border: none;
        }
        
        .table-mobile-stack td::before {
            content: attr(data-label);
            font-weight: 700;
            display: block;
            margin-bottom: 0.25rem;
            color: #6c757d;
            font-size: 0.75rem;
            text-transform: uppercase;
        }
    }
}

/* ===================================
   MOBILE MODALS
   =================================== */

@media (max-width: 767px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 16px;
        border: none;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    }
    
    .modal-header {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        border-radius: 16px 16px 0 0;
    }
    
    .modal-title {
        font-size: 1.125rem;
        font-weight: 700;
    }
    
    .modal-body {
        padding: 1.25rem;
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
    
    .modal-footer {
        padding: 1rem 1.25rem;
        border-top: 1px solid rgba(0, 0, 0, 0.1);
        border-radius: 0 0 16px 16px;
    }
    
    .modal-footer .btn {
        flex: 1;
        min-height: 48px;
    }
}

/* ===================================
   MOBILE ALERTS & NOTIFICATIONS
   =================================== */

@media (max-width: 767px) {
    .alert {
        border-radius: 12px;
        padding: 1rem;
        margin-bottom: 1rem;
        border: none;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
    
    .alert-dismissible .btn-close {
        min-height: 44px;
        min-width: 44px;
        padding: 0.75rem;
    }
    
    /* Toast notifications */
    .toast {
        min-width: 250px;
        max-width: calc(100% - 2rem);
        border-radius: 12px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
}

/* ===================================
   MOBILE UTILITY CLASSES
   =================================== */

@media (max-width: 767px) {
    /* Spacing utilities for mobile */
    .mobile-p-0 { padding: 0 !important; }
    .mobile-p-1 { padding: 0.5rem !important; }
    .mobile-p-2 { padding: 0.75rem !important; }
    .mobile-p-3 { padding: 1rem !important; }
    
    .mobile-m-0 { margin: 0 !important; }
    .mobile-m-1 { margin: 0.5rem !important; }
    .mobile-m-2 { margin: 0.75rem !important; }
    .mobile-m-3 { margin: 1rem !important; }
    
    /* Hide on mobile */
    .mobile-hide {
        display: none !important;
    }
    
    /* Show only on mobile */
    .mobile-only {
        display: block !important;
    }
    
    /* Full width on mobile */
    .mobile-full-width {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Text sizing for mobile */
    .mobile-text-sm {
        font-size: 0.8125rem !important;
    }
    
    .mobile-text-base {
        font-size: 0.9375rem !important;
    }
    
    .mobile-text-lg {
        font-size: 1.0625rem !important;
    }
}

/* Desktop: hide mobile-only elements */
@media (min-width: 768px) {
    .mobile-only {
        display: none !important;
    }
}

/* ===================================
   ACCESSIBILITY ENHANCEMENTS
   =================================== */

/* Focus visible for all interactive elements */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.btn:focus-visible {
    outline: 3px solid #0d6efd;
    outline-offset: 2px;
    border-radius: 4px;
}

/* Reduced motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    * {
        border-color: currentColor !important;
    }
    
    .btn {
        border: 2px solid currentColor !important;
    }
}

/* ===================================
   PERFORMANCE OPTIMIZATIONS
   =================================== */

/* Hardware acceleration for animations */
.btn,
.card,
.modal,
.dropdown-menu {
    will-change: transform;
    transform: translateZ(0);
}

/* Prevent layout shifts */
img,
video,
iframe {
    max-width: 100%;
    height: auto;
}

/* Lazy loading support */
img[loading="lazy"] {
    opacity: 0;
    transition: opacity 0.3s ease;
}

img[loading="lazy"].loaded {
    opacity: 1;
}

/* ===================================
   PRINT STYLES
   =================================== */

@media print {
    /* Hide navigation and interactive elements */
    .navbar,
    .sidebar,
    .btn,
    button,
    .modal,
    .dropdown-menu,
    .alert {
        display: none !important;
    }
    
    /* Optimize text for print */
    body {
        font-size: 12pt;
        line-height: 1.5;
        color: #000;
        background: #fff;
    }
    
    /* Page breaks */
    h1, h2, h3, h4, h5, h6 {
        page-break-after: avoid;
    }
    
    .card,
    .table {
        page-break-inside: avoid;
    }
}

/* ===================================
   MOBILE COMPACT SPACING (20% more content visible)
   =================================== */

@media (max-width: 767px) {
    /* Container padding: 32px → 12px (40px saved) */
    .container,
    .container-fluid {
        padding-left: 0.75rem !important;   /* 12px instead of 16px */
        padding-right: 0.75rem !important;
        padding-top: 0.75rem !important;
        padding-bottom: 0.75rem !important;
    }
    
    /* Card spacing: tighter gaps */
    .card {
        margin-bottom: 0.75rem !important;  /* 12px instead of 24px */
        border-radius: 8px;
    }
    
    /* Card body padding: 20px → 12px */
    .card-body {
        padding: 0.75rem !important;        /* 12px instead of 20px */
    }
    
    /* Row gutters: smaller gaps */
    .row {
        margin-left: -0.5rem !important;    /* 8px gap instead of 24px */
        margin-right: -0.5rem !important;
    }
    
    .row > * {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }
    
    /* Section headings: tighter */
    h1, h2, h3, h4, h5, h6 {
        margin-bottom: 0.75rem !important;  /* Reduced from 1.5rem */
    }
    
    /* Form groups: compact */
    .mb-3 {
        margin-bottom: 0.75rem !important;  /* 12px instead of 16px */
    }
    
    .mb-4 {
        margin-bottom: 1rem !important;     /* 16px instead of 24px */
    }
    
    /* List spacing */
    .list-group-item {
        padding: 0.625rem 0.75rem !important; /* 10px 12px instead of 12px 20px */
    }
    
    /* Table spacing */
    .table td,
    .table th {
        padding: 0.5rem !important;         /* 8px instead of 12px */
    }
    
    /* Alert spacing */
    .alert {
        padding: 0.75rem !important;        /* 12px instead of 16px */
        margin-bottom: 0.75rem !important;
    }
    
    /* Modal spacing */
    .modal-body {
        padding: 0.75rem !important;        /* 12px instead of 16px */
    }
    
    /* Breadcrumb spacing */
    .breadcrumb {
        padding: 0.5rem 0.75rem !important; /* Compact breadcrumbs */
        margin-bottom: 0.75rem !important;
    }
}

/* ===================================
   ULTRA-COMPACT MOBILE SPACING
   Priority: Show 20% more content
   Phase 1: Critical Mobile Transformation
   =================================== */

@media (max-width: 767px) {
    /* Compact container spacing */
    .container,
    .container-fluid {
        padding-left: 0.5rem !important;   /* 8px instead of 32px */
        padding-right: 0.5rem !important;
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
    }
    
    /* Compact card spacing */
    .card {
        margin-bottom: 0.5rem !important;  /* 8px instead of 32px */
        border-radius: 8px;
    }
    
    .card-body {
        padding: 0.75rem !important;       /* 12px instead of 20px */
    }
    
    .card-header,
    .card-footer {
        padding: 0.5rem 0.75rem !important;  /* 8px 12px */
    }
    
    /* Tighter row gaps */
    .row {
        margin-left: -0.25rem !important;
        margin-right: -0.25rem !important;
    }
    
    .row > * {
        padding-left: 0.25rem !important;  /* 4px instead of 12px */
        padding-right: 0.25rem !important;
    }
    
    /* Compact headings */
    h1 { 
        font-size: 1.75rem !important;     /* Down from 2.5rem */
        margin-bottom: 0.5rem !important;
        line-height: 1.2 !important;
    }
    
    h2 { 
        font-size: 1.5rem !important;      /* Down from 2rem */
        margin-bottom: 0.5rem !important; 
        line-height: 1.2 !important;
    }
    
    h3 { 
        font-size: 1.25rem !important;     /* Down from 1.75rem */
        margin-bottom: 0.5rem !important; 
        line-height: 1.2 !important;
    }
    
    h4 { 
        font-size: 1.1rem !important;      /* Down from 1.5rem */
        margin-bottom: 0.5rem !important; 
    }
    
    h5 { 
        font-size: 1rem !important;        /* Down from 1.25rem */
        margin-bottom: 0.5rem !important; 
    }
    
    /* Compact margins for paragraphs and lists */
    p, ul, ol {
        margin-bottom: 0.5rem !important;  /* Down from 1rem */
    }
    
    /* Compact buttons */
    .btn {
        padding: 0.5rem 0.75rem !important; /* Slightly reduced */
        font-size: 0.9375rem !important;    /* 15px */
    }
    
    .btn-sm {
        padding: 0.375rem 0.625rem !important;
        font-size: 0.8125rem !important;    /* 13px */
    }
    
    /* Compact form elements */
    .form-control,
    .form-select {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.9375rem !important;
    }
    
    /* Compact table cells */
    .table td,
    .table th {
        padding: 0.5rem !important;         /* Down from 0.75rem */
        font-size: 0.875rem !important;     /* 14px */
    }
}

/* ===================================
   LOADING OVERLAY (AJAX Navigation)
   =================================== */

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    backdrop-filter: blur(2px);
}

.loading-overlay .spinner-border {
    width: 3rem;
    height: 3rem;
}

