/**
 * Mobile-First Responsive Design
 *
 * Optimized for:
 * - Touch interactions (44px minimum touch targets)
 * - Bottom sheet modals for mobile
 * - Off-canvas sidebar navigation
 * - Swipe gestures
 * - Mobile map optimization
 * - Responsive typography
 * - Safe area handling (notch support)
 */

/* ============================================================================
   MOBILE BREAKPOINTS
   ============================================================================ */
/*
  xs: 0-575px (mobile portrait)
  sm: 576-767px (mobile landscape)
  md: 768-991px (tablet)
  lg: 992-1199px (desktop)
  xl: 1200px+ (large desktop)
*/

/* ============================================================================
   SAFE AREA SUPPORT (iPhone notch, etc.)
   ============================================================================ */
:root {
    --safe-area-inset-top: env(safe-area-inset-top);
    --safe-area-inset-right: env(safe-area-inset-right);
    --safe-area-inset-bottom: env(safe-area-inset-bottom);
    --safe-area-inset-left: env(safe-area-inset-left);
}

/* Apply safe area padding */
.safe-area-top {
    padding-top: calc(var(--safe-area-inset-top) + 1rem);
}

.safe-area-bottom {
    padding-bottom: calc(var(--safe-area-inset-bottom) + 1rem);
}

/* ============================================================================
   TOUCH TARGET OPTIMIZATION
   ============================================================================ */
/* Minimum 44px touch targets per accessibility guidelines */
@media (max-width: 991px) {
    .btn,
    .nav-link,
    button,
    a[role="button"],
    input[type="checkbox"],
    input[type="radio"] {
        min-height: 44px;
        min-width: 44px;
    }

    /* Increase form input sizes */
    .form-control,
    .form-select {
        padding: 0.875rem 1rem;
        font-size: 1rem; /* Prevent zoom on iOS */
    }

    /* Larger tap targets for icons */
    .btn-icon,
    .icon-button {
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* ============================================================================
   MOBILE TYPOGRAPHY
   ============================================================================ */
@media (max-width: 767px) {
    html {
        font-size: 14px;
    }

    h1 {
        font-size: 1.75rem;
        line-height: 1.2;
    }

    h2 {
        font-size: 1.5rem;
    }

    h3 {
        font-size: 1.25rem;
    }

    h4 {
        font-size: 1.125rem;
    }

    h5,
    h6 {
        font-size: 1rem;
    }

    /* Increase body line height for readability */
    body {
        line-height: 1.6;
    }
}

/* ============================================================================
   MOBILE SIDEBAR (Off-Canvas Navigation)
   ============================================================================ */
@media (max-width: 991px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1040;
        box-shadow: none;
    }

    .sidebar.show {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
    }

    /* Overlay backdrop */
    .sidebar-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1039;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }

    .sidebar-backdrop.show {
        opacity: 1;
        visibility: visible;
    }

    /* Main wrapper adjustments */
    .main-wrapper {
        margin-left: 0;
        width: 100%;
        padding: 1rem;
        padding-top: 5rem; /* Space for mobile menu toggle */
    }

    /* Mobile menu toggle button */
    .mobile-menu-toggle {
        display: flex;
        position: fixed;
        top: 1rem;
        left: 1rem;
        z-index: 1041;
    }
}

/* ============================================================================
   MOBILE HEADER
   ============================================================================ */
@media (max-width: 991px) {
    header {
        flex-direction: column;
        gap: 1rem;
        align-items: flex-start;
        padding: 1rem;
        margin-bottom: 1.5rem;
    }

    .header-actions {
        width: 100%;
        justify-content: flex-end;
        gap: 0.5rem;
    }

    .title-group h1 {
        font-size: 1.5rem;
    }
}

@media (max-width: 576px) {
    header {
        padding: 0.75rem;
    }

    .title-group h1 {
        font-size: 1.25rem;
    }
}

/* ============================================================================
   MOBILE CARDS & CONTENT
   ============================================================================ */
@media (max-width: 991px) {
    .app-card,
    .metric-card {
        border-radius: 1rem;
        padding: 1rem;
        margin-bottom: 1rem;
    }

    .app-card-header {
        padding: 1rem;
        font-size: 1rem;
    }

    .app-card-body {
        padding: 1rem;
    }

    .metric-value {
        font-size: 2rem;
    }
}

@media (max-width: 576px) {
    .app-card,
    .metric-card {
        padding: 0.875rem;
    }

    .metric-value {
        font-size: 1.75rem;
    }
}

/* ============================================================================
   MOBILE GRID SYSTEM
   ============================================================================ */
@media (max-width: 991px) {
    /* Stack columns on mobile */
    .row > [class*="col-"] {
        width: 100%;
        max-width: 100%;
    }

    /* Reduce gap between grid items */
    .row {
        margin: 0 -0.5rem;
    }

    .row > [class*="col-"] {
        padding: 0 0.5rem;
    }
}

/* ============================================================================
   MOBILE FORMS
   ============================================================================ */
@media (max-width: 767px) {
    .form-group {
        margin-bottom: 1rem;
    }

    /* Stack form rows */
    .form-row {
        flex-direction: column;
    }

    .form-row > * {
        width: 100%;
        margin-bottom: 1rem;
    }

    /* Full width buttons on mobile */
    .btn-block-mobile {
        width: 100%;
        display: block;
    }

    /* Button groups stack */
    .btn-group {
        flex-direction: column;
        width: 100%;
    }

    .btn-group .btn {
        width: 100%;
        border-radius: 0.875rem !important;
        margin-bottom: 0.5rem;
    }
}

/* ============================================================================
   MOBILE TABLES
   ============================================================================ */
@media (max-width: 767px) {
    /* Card-based table layout for mobile */
    .table-mobile-cards {
        border: 0;
    }

    .table-mobile-cards thead {
        display: none;
    }

    .table-mobile-cards tbody,
    .table-mobile-cards tr,
    .table-mobile-cards td {
        display: block;
        width: 100%;
    }

    .table-mobile-cards tr {
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 1rem;
        padding: 1rem;
        margin-bottom: 1rem;
    }

    .table-mobile-cards td {
        text-align: right;
        padding: 0.5rem 0;
        border: 0;
        position: relative;
        padding-left: 50%;
    }

    .table-mobile-cards td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 45%;
        padding-right: 10px;
        text-align: left;
        font-weight: 600;
        color: var(--text-muted);
    }

    /* Horizontal scroll for regular tables */
    .table-responsive-mobile {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================================================
   MOBILE MODALS (Bottom Sheet)
   ============================================================================ */
@media (max-width: 767px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        height: auto;
        max-height: 90vh;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
    }

    .modal-content {
        border-radius: 1.5rem 1.5rem 0 0;
        border: none;
        box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.3);
    }

    /* Pull handle for bottom sheet */
    .modal-header::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: var(--border-color);
        border-radius: 2px;
    }

    .modal-header {
        padding-top: 1.5rem;
        position: relative;
    }

    .modal-body {
        max-height: 60vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================================================
   MOBILE MAP OPTIMIZATION
   ============================================================================ */
@media (max-width: 767px) {
    .map-container {
        height: 300px;
        border-radius: 1rem;
    }

    .map-fullscreen-mobile {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        height: 100vh !important;
        z-index: 1050;
        border-radius: 0;
    }

    /* Map controls */
    .map-controls {
        position: absolute;
        top: 1rem;
        right: 1rem;
        z-index: 10;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .map-control-btn {
        width: 44px;
        height: 44px;
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    }

    /* Location card overlay */
    .location-card-mobile {
        position: absolute;
        bottom: 1rem;
        left: 1rem;
        right: 1rem;
        background: var(--card-bg);
        border-radius: 1rem;
        padding: 1rem;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
        z-index: 10;
    }
}

/* ============================================================================
   SWIPE GESTURE SUPPORT
   ============================================================================ */
.swipe-container {
    overflow-x: hidden;
    touch-action: pan-y;
}

.swipe-item {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Swipe actions (reveal buttons on swipe) */
.swipe-actions {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0 1rem;
    transform: translateX(100%);
}

.swipe-item.swiped {
    transform: translateX(-100px);
}

.swipe-item.swiped .swipe-actions {
    transform: translateX(0);
}

/* ============================================================================
   MOBILE SPACING & PADDING
   ============================================================================ */
@media (max-width: 767px) {
    /* Reduce overall spacing */
    .container,
    .container-fluid {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Section spacing */
    section {
        padding: 2rem 0;
    }

    /* Reduce margin utilities on mobile */
    .mb-4 {
        margin-bottom: 1.5rem !important;
    }

    .mb-5 {
        margin-bottom: 2rem !important;
    }

    .mt-4 {
        margin-top: 1.5rem !important;
    }

    .mt-5 {
        margin-top: 2rem !important;
    }
}

/* ============================================================================
   MOBILE NAVIGATION TABS
   ============================================================================ */
@media (max-width: 767px) {
    .nav-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-bottom: 1px solid var(--border-color);
    }

    .nav-tabs .nav-link {
        white-space: nowrap;
        padding: 0.75rem 1rem;
        border: none;
        border-bottom: 2px solid transparent;
    }

    .nav-tabs .nav-link.active {
        border-bottom-color: var(--accent);
        background: transparent;
    }
}

/* ============================================================================
   MOBILE FAB (Floating Action Button)
   ============================================================================ */
@media (max-width: 767px) {
    .fab-mobile {
        position: fixed;
        bottom: calc(1rem + var(--safe-area-inset-bottom));
        right: 1rem;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: var(--accent);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 4px 12px rgba(126, 58, 242, 0.4);
        z-index: 1030;
        border: none;
        font-size: 1.5rem;
    }

    .fab-mobile:active {
        transform: scale(0.95);
    }
}

/* ============================================================================
   MOBILE PULL TO REFRESH
   ============================================================================ */
.pull-to-refresh {
    position: relative;
}

.pull-to-refresh-indicator {
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease, top 0.3s ease;
}

.pull-to-refresh.pulling .pull-to-refresh-indicator {
    opacity: 1;
    top: 10px;
}

/* ============================================================================
   MOBILE BOTTOM NAVIGATION BAR
   ============================================================================ */
@media (max-width: 767px) {
    .bottom-nav-mobile {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: calc(60px + var(--safe-area-inset-bottom));
        background: var(--card-bg);
        border-top: 1px solid var(--border-color);
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding-bottom: var(--safe-area-inset-bottom);
        z-index: 1030;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    }

    .bottom-nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 0.5rem;
        color: var(--text-muted);
        text-decoration: none;
        font-size: 0.75rem;
        transition: color 0.2s ease;
    }

    .bottom-nav-item.active {
        color: var(--accent);
    }

    .bottom-nav-icon {
        font-size: 1.5rem;
        margin-bottom: 0.25rem;
    }

    /* Add padding to content to prevent overlap */
    .main-wrapper {
        padding-bottom: calc(80px + var(--safe-area-inset-bottom));
    }
}

/* ============================================================================
   MOBILE IMAGE OPTIMIZATION
   ============================================================================ */
@media (max-width: 767px) {
    img {
        max-width: 100%;
        height: auto;
    }

    /* Prevent layout shift with aspect ratio */
    .img-responsive {
        width: 100%;
        height: auto;
        aspect-ratio: 16 / 9;
        object-fit: cover;
    }
}

/* ============================================================================
   MOBILE UTILITY CLASSES
   ============================================================================ */
/* Hide on mobile */
.hide-mobile {
    display: none !important;
}

@media (min-width: 768px) {
    .hide-mobile {
        display: block !important;
    }
}

/* Show only on mobile */
.show-mobile {
    display: block !important;
}

@media (min-width: 768px) {
    .show-mobile {
        display: none !important;
    }
}

/* Mobile text alignment */
@media (max-width: 767px) {
    .text-mobile-center {
        text-align: center !important;
    }

    .text-mobile-left {
        text-align: left !important;
    }

    .text-mobile-right {
        text-align: right !important;
    }
}

/* ============================================================================
   MOBILE ACCESSIBILITY
   ============================================================================ */
@media (max-width: 767px) {
    /* Increase focus indicators on mobile */
    *:focus-visible {
        outline: 3px solid var(--accent);
        outline-offset: 2px;
    }

    /* Prevent text selection issues */
    .no-select-mobile {
        -webkit-user-select: none;
        user-select: none;
    }

    /* Disable hover effects on touch devices */
    @media (hover: none) and (pointer: coarse) {
        .hover-effect-mobile-disable:hover {
            transform: none;
            box-shadow: none;
        }
    }
}

/* ============================================================================
   LANDSCAPE ORIENTATION OPTIMIZATIONS
   ============================================================================ */
@media (max-width: 767px) and (orientation: landscape) {
    .main-wrapper {
        padding-top: 3rem;
    }

    header {
        padding: 0.5rem 1rem;
        margin-bottom: 1rem;
    }

    .title-group h1 {
        font-size: 1.125rem;
    }

    /* Optimize modals for landscape */
    .modal-body {
        max-height: 50vh;
    }
}
