/**
 * Mobile CSS - Phase 2: Mobile UX Patterns
 * Mobile-specific styles for gestures, animations, and loading states
 * Only applies to mobile viewports (< 768px)
 *
 * Phase 3: Added iOS safe area support for notch/Dynamic Island
 */

/* ========================================
   iOS SAFE AREA SUPPORT - Phase 3
   ======================================== */

/* Define CSS variables for iOS safe areas (notch, Dynamic Island, home indicator) */
:root {
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* Apply safe area to main content containers */
@media (max-width: 768px) {
    .main-content,
    .container,
    main {
        padding-top: calc(var(--safe-area-top) + 16px);
        padding-bottom: calc(var(--safe-area-bottom) + 16px);
        padding-left: calc(var(--safe-area-left) + 16px);
        padding-right: calc(var(--safe-area-right) + 16px);
    }

    /* Fixed positioned elements should respect safe areas */
    .workout-timer,
    .timer-container {
        top: var(--safe-area-top);
    }

    /* Fixed bottom elements (like floating action buttons) */
    .fab,
    .fixed-bottom {
        bottom: calc(var(--safe-area-bottom) + 16px);
    }

    /* Sidebar/navigation drawer */
    .sidebar-container {
        padding-top: var(--safe-area-top);
        padding-bottom: var(--safe-area-bottom);
    }

    /* Modals and overlays */
    .modal-content,
    .bottom-sheet {
        padding-bottom: calc(var(--safe-area-bottom) + 16px);
    }

    /* Ensure viewport-height elements account for safe areas */
    .full-height {
        height: calc(100vh - var(--safe-area-top) - var(--safe-area-bottom));
    }
}

/* ========================================
   SKELETON SCREENS
   ======================================== */

.skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s ease-in-out infinite;
    border-radius: 4px;
    position: relative;
    overflow: hidden;
}

.skeleton::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    animation: skeleton-shimmer 1.5s infinite;
}

@keyframes skeleton-loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

@keyframes skeleton-shimmer {
    100% {
        transform: translateX(100%);
    }
}

/* Skeleton variants for different content types */
.skeleton-text {
    height: 16px;
    margin-bottom: 8px;
    border-radius: 4px;
}

.skeleton-text.large {
    height: 24px;
}

.skeleton-text.small {
    height: 12px;
}

.skeleton-card {
    min-height: 120px;
    margin-bottom: 16px;
    border-radius: 8px;
}

.skeleton-header {
    height: 40px;
    margin-bottom: 16px;
    border-radius: 8px;
}

.skeleton-circle {
    border-radius: 50%;
    width: 40px;
    height: 40px;
}

.skeleton-button {
    height: 44px;
    border-radius: 8px;
    width: 100%;
}

/* Skeleton exercise card layout */
.skeleton-exercise {
    padding: 16px;
    background: var(--bg-primary);
    border-radius: 8px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}

.skeleton-exercise-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.skeleton-exercise-title {
    height: 20px;
    width: 60%;
    border-radius: 4px;
}

.skeleton-exercise-badge {
    height: 24px;
    width: 60px;
    border-radius: 12px;
}

.skeleton-exercise-detail {
    height: 14px;
    width: 80%;
    margin-bottom: 8px;
    border-radius: 4px;
}

.skeleton-exercise-detail:last-child {
    width: 50%;
}

/* Skeleton list for history/library */
.skeleton-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.skeleton-list-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--bg-primary);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.skeleton-list-icon {
    width: 48px;
    height: 48px;
    border-radius: 8px;
}

.skeleton-list-content {
    flex: 1;
}

.skeleton-list-title {
    height: 18px;
    width: 70%;
    margin-bottom: 8px;
    border-radius: 4px;
}

.skeleton-list-subtitle {
    height: 14px;
    width: 40%;
    border-radius: 4px;
}

/* ========================================
   PULL-TO-REFRESH
   ======================================== */

.pull-to-refresh-container {
    position: relative;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.pull-to-refresh-indicator {
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    height: 60px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 10;
}

.pull-to-refresh-indicator.refreshing .ptr-spinner {
    animation: spin 1s linear infinite;
}

.ptr-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid rgba(0, 0, 0, 0.1);
    border-top-color: var(--primary-color);
    border-radius: 50%;
}

.ptr-text {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* ========================================
   SWIPE ANIMATIONS
   ======================================== */

.swipe-container {
    position: relative;
    overflow: hidden;
}

.swipe-item {
    position: relative;
    transform: translateX(0);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: pan-y; /* Allow vertical scrolling */
}

.swipe-item.swiping {
    transition: none; /* Disable transition during swipe */
}

.swipe-item.swipe-left {
    transform: translateX(-80px);
}

.swipe-item.swipe-right {
    transform: translateX(80px);
}

.swipe-actions {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary-color);
    color: white;
    font-weight: 600;
    font-size: 14px;
    opacity: 0;
    transition: opacity 0.3s;
}

.swipe-item.swipe-left .swipe-actions {
    opacity: 1;
}

.swipe-actions button {
    background: transparent;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
    padding: 8px;
}

/* ========================================
   LONG-PRESS CONTEXT MENU
   ======================================== */

.context-menu {
    position: fixed;
    background: var(--bg-primary);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    z-index: 9999;
    min-width: 200px;
    opacity: 0;
    transform: scale(0.95);
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}

.context-menu.active {
    opacity: 1;
    transform: scale(1);
    pointer-events: auto;
}

.context-menu-item {
    padding: 14px 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border-color);
    transition: background-color 0.2s;
}

.context-menu-item:last-child {
    border-bottom: none;
}

.context-menu-item:hover,
.context-menu-item:active {
    background-color: var(--bg-secondary);
}

.context-menu-item i {
    font-size: 18px;
    width: 20px;
    text-align: center;
    color: var(--text-secondary);
}

.context-menu-item.danger {
    color: var(--primary-color);
}

.context-menu-item.danger i {
    color: var(--primary-color);
}

.context-menu-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

.context-menu-backdrop.active {
    opacity: 1;
    pointer-events: auto;
}

/* ========================================
   TOUCH FEEDBACK
   ======================================== */

.touch-feedback {
    position: relative;
    overflow: hidden;
}

.touch-feedback::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.1);
    transform: translate(-50%, -50%);
    opacity: 0;
    pointer-events: none;
}

.touch-feedback.touching::after {
    animation: ripple 0.6s ease-out;
}

@keyframes ripple {
    0% {
        width: 0;
        height: 0;
        opacity: 0.5;
    }
    100% {
        width: 200px;
        height: 200px;
        opacity: 0;
    }
}

/* Improved tap targets for mobile */
@media (max-width: 768px) {
    button,
    a.btn,
    .clickable {
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Add touch feedback to interactive elements */
    button:active,
    a.btn:active,
    .clickable:active {
        transform: scale(0.98);
        transition: transform 0.1s;
    }
}

/* ========================================
   SIDEBAR SWIPE OVERLAY
   ======================================== */

.sidebar-swipe-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.sidebar-swipe-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

/* Sidebar swipe indicator (visual cue at edge) */
.edge-swipe-hint {
    position: fixed;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 100px;
    background: linear-gradient(to right, var(--primary-color), transparent);
    opacity: 0;
    animation: edge-hint 2s ease-in-out infinite;
    pointer-events: none;
    z-index: 1;
}

@keyframes edge-hint {
    0%, 100% {
        opacity: 0;
    }
    50% {
        opacity: 0.3;
    }
}

/* ========================================
   LOADING STATES
   ======================================== */

/* Mobile-optimized loading overlay with dark background for better visibility */
@media (max-width: 768px) {
    .loading-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);  /* Dark overlay for better contrast on mobile */
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        z-index: 9999;
    }

    /* Support both display:none and .active class patterns for compatibility */
    .loading-overlay:not([style*="display: flex"]):not(.active) {
        display: none;
    }

    .loading-overlay.active {
        display: flex;
    }

    /* White spinner for visibility on dark background */
    .loading-overlay .loading-spinner,
    .loading-modal .loading-spinner {
        width: 48px;
        height: 48px;
        border: 4px solid rgba(255, 255, 255, 0.3);
        border-top-color: #ffffff;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }

    /* White text for visibility on dark background */
    .loading-overlay .loading-text,
    .loading-modal p {
        margin-top: 16px;
        font-size: 16px;
        color: #ffffff;
        font-weight: 500;
    }
}

/* ========================================
   MOBILE-SPECIFIC UTILITIES
   ======================================== */

@media (max-width: 768px) {
    /* Prevent text selection during gestures */
    .no-select {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }

    /* Prevent pull-to-refresh on specific elements */
    .no-pull-refresh {
        overscroll-behavior-y: contain;
    }

    /* Smooth scrolling for mobile */
    .smooth-scroll {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }

    /* Hide scrollbar on mobile for cleaner look */
    .hide-scrollbar {
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE/Edge */
    }

    .hide-scrollbar::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }
}

/* ========================================
   EXERCISE CARD SWIPE NAVIGATION
   ======================================== */

.exercise-swipe-container {
    position: relative;
    overflow: hidden;
    touch-action: pan-y;
}

.exercise-swipe-track {
    display: flex;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.exercise-swipe-track.swiping {
    transition: none;
}

.exercise-swipe-card {
    flex-shrink: 0;
    width: 100%;
}

.swipe-indicators {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-top: 12px;
}

.swipe-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--border-color);
    transition: background-color 0.3s, width 0.3s;
}

.swipe-indicator.active {
    background: var(--primary-color);
    width: 24px;
    border-radius: 4px;
}

/* ========================================
   ACCESSIBILITY & REDUCED MOTION
   ======================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .skeleton {
        background: linear-gradient(90deg, #d0d0d0 25%, #c0c0c0 50%, #d0d0d0 75%);
    }

    .context-menu {
        border: 2px solid var(--text-primary);
    }
}
