/**
 * Touch Target Optimizations - PWA Phase 6
 *
 * Improves mobile usability by:
 * - Ensuring all interactive elements are at least 44-48px
 * - Adding touch feedback animations
 * - Improving tap target spacing
 * - Preventing accidental zoom on iOS
 * - Enhancing mobile gestures
 *
 * Reference: WCAG 2.1 Success Criterion 2.5.5 (Target Size)
 * Minimum target size: 44×44 CSS pixels
 */

/* =============================================================================
   CSS VARIABLES FOR TOUCH
   ============================================================================= */

:root {
    /* Touch target sizes */
    --touch-target-min: 44px;
    --touch-target-comfortable: 48px;
    --touch-target-large: 56px;

    /* Touch feedback */
    --touch-feedback-scale: 0.95;
    --touch-feedback-duration: 0.1s;
    --touch-highlight-color: rgba(67, 97, 238, 0.15);

    /* Spacing for touch targets */
    --touch-spacing-min: 8px;
    --touch-spacing-comfortable: 12px;
}

/* =============================================================================
   DISABLE TAP HIGHLIGHT (We'll use custom feedback)
   ============================================================================= */

* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

/* Re-enable text selection where needed */
input,
textarea,
p,
span,
h1, h2, h3, h4, h5, h6 {
    -webkit-touch-callout: default;
    user-select: text;
}

/* =============================================================================
   IMPROVED BUTTON TOUCH TARGETS
   ============================================================================= */

.btn {
    /* Ensure minimum touch target height */
    min-height: var(--touch-target-min);
    padding: 12px 20px;
    font-size: 0.95rem;
    position: relative;

    /* Touch feedback */
    transition: transform var(--touch-feedback-duration) ease,
                background-color 0.2s ease,
                box-shadow 0.2s ease;
}

/* Active state for touch feedback */
.btn:active {
    transform: scale(var(--touch-feedback-scale));
}

/* Larger buttons for primary actions */
.btn-primary,
.btn-large {
    min-height: var(--touch-target-comfortable);
    padding: 14px 24px;
    font-size: 1rem;
}

/* Extra large buttons for critical actions */
.btn-xl {
    min-height: var(--touch-target-large);
    padding: 16px 28px;
    font-size: 1.1rem;
    font-weight: 700;
}

/* Icon-only buttons need special handling */
.btn-icon {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    padding: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-icon-large {
    min-width: var(--touch-target-comfortable);
    min-height: var(--touch-target-comfortable);
    padding: 14px;
}

/* Button spacing to prevent accidental taps */
.btn + .btn {
    margin-left: var(--touch-spacing-comfortable);
}

/* =============================================================================
   LINK TOUCH TARGETS
   ============================================================================= */

/* Make links easier to tap */
a {
    position: relative;
    /* Add invisible padding for easier tapping */
    padding: 4px 0;
}

/* Card links need proper touch area */
a.listing-card,
a.category-card,
a.quick-action {
    /* Touch feedback */
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

a.listing-card:active,
a.category-card:active,
a.quick-action:active {
    transform: scale(0.98);
}

/* Navigation links */
.nav-buttons a {
    min-height: var(--touch-target-min);
    display: inline-flex;
    align-items: center;
}

/* =============================================================================
   FORM INPUT TOUCH TARGETS
   ============================================================================= */

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"],
textarea,
select {
    min-height: var(--touch-target-min);
    padding: 12px 15px;
    font-size: 16px; /* Prevents iOS zoom on focus */
    border-radius: var(--border-radius);
}

/* Checkbox and radio buttons */
input[type="checkbox"],
input[type="radio"] {
    min-width: 24px;
    min-height: 24px;
    cursor: pointer;
}

/* Label touch targets for checkboxes/radios */
label {
    cursor: pointer;
    padding: 8px 0;
    display: inline-block;
    -webkit-tap-highlight-color: var(--touch-highlight-color);
}

/* =============================================================================
   MOBILE MENU TOUCH TARGETS
   ============================================================================= */

.mobile-menu-toggle {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    padding: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--dark);
    cursor: pointer;
    transition: transform var(--touch-feedback-duration) ease;
}

.mobile-menu-toggle:active {
    transform: scale(var(--touch-feedback-scale));
}

.mobile-menu-toggle i {
    font-size: 1.5rem;
}

/* Mobile menu items */
.mobile-menu a,
.mobile-menu button {
    min-height: var(--touch-target-comfortable);
    padding: 14px 20px;
    display: flex;
    align-items: center;
    width: 100%;
    border: none;
    background: none;
    text-align: left;
    font-size: 1rem;
}

/* =============================================================================
   ICON BUTTON TOUCH TARGETS
   ============================================================================= */

/* Favorite buttons */
.favorite-btn {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    padding: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Close buttons */
.close-btn,
.modal-close {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    padding: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Dropdown toggles */
.dropdown-toggle {
    min-height: var(--touch-target-min);
    padding: 10px 16px;
}

/* =============================================================================
   TOUCH FEEDBACK ANIMATIONS
   ============================================================================= */

/* Ripple effect for touch feedback */
@keyframes ripple {
    0% {
        transform: scale(0);
        opacity: 1;
    }
    100% {
        transform: scale(2.5);
        opacity: 0;
    }
}

.touch-ripple {
    position: relative;
    overflow: hidden;
}

.touch-ripple::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100px;
    height: 100px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    transform: scale(0) translate(-50%, -50%);
    transform-origin: 0 0;
    pointer-events: none;
}

.touch-ripple:active::after {
    animation: ripple 0.6s ease-out;
}

/* Pulse feedback for important actions */
@keyframes pulse-touch {
    0% {
        box-shadow: 0 0 0 0 rgba(67, 97, 238, 0.7);
    }
    70% {
        box-shadow: 0 0 0 10px rgba(67, 97, 238, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(67, 97, 238, 0);
    }
}

/* =============================================================================
   SPACING BETWEEN TOUCH TARGETS
   ============================================================================= */

/* Ensure adequate spacing between buttons */
.btn-group .btn {
    margin: 0 4px;
}

.btn-group .btn:first-child {
    margin-left: 0;
}

.btn-group .btn:last-child {
    margin-right: 0;
}

/* List items with touch targets */
.touch-list li {
    min-height: var(--touch-target-min);
    padding: 12px 0;
    border-bottom: 1px solid var(--gray-light);
}

/* Navigation items */
nav a,
nav button {
    min-height: var(--touch-target-min);
    padding: 12px 16px;
    display: flex;
    align-items: center;
}

/* =============================================================================
   GESTURE SUPPORT - IMAGE GALLERY SWIPE
   ============================================================================= */

.swipeable {
    touch-action: pan-y;
    cursor: grab;
}

.swipeable:active {
    cursor: grabbing;
}

/* Gallery container */
.swipe-gallery {
    position: relative;
    overflow: hidden;
    touch-action: pan-y;
}

.swipe-gallery-track {
    display: flex;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
}

.swipe-gallery-track.no-transition {
    transition: none;
}

.swipe-gallery-item {
    flex: 0 0 100%;
    width: 100%;
}

/* Swipe indicators */
.swipe-indicators {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 8px;
    z-index: 10;
}

.swipe-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.5);
    transition: background-color 0.3s ease, width 0.3s ease;
    cursor: pointer;
    /* Make indicators easier to tap */
    padding: 4px;
    margin: -4px;
}

.swipe-indicator.active {
    background: white;
    width: 24px;
    border-radius: 4px;
}

/* Swipe navigation arrows (optional) */
.swipe-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: none;
    border-radius: 50%;
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.swipe-gallery:hover .swipe-arrow {
    opacity: 1;
}

.swipe-arrow-left {
    left: 16px;
}

.swipe-arrow-right {
    right: 16px;
}

.swipe-arrow:active {
    transform: translateY(-50%) scale(0.95);
}

/* =============================================================================
   IOS SPECIFIC FIXES
   ============================================================================= */

/* Prevent zoom on double-tap (iOS) */
@media (max-width: 768px) {
    /* All buttons */
    button,
    a,
    input,
    textarea,
    select {
        touch-action: manipulation;
    }

    /* Prevent iOS text size adjustment */
    body {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }
}

/* iOS safe areas (notch support) */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }

    header {
        padding-left: max(20px, env(safe-area-inset-left));
        padding-right: max(20px, env(safe-area-inset-right));
    }
}

/* =============================================================================
   MOBILE-SPECIFIC TOUCH IMPROVEMENTS
   ============================================================================= */

@media (max-width: 768px) {
    /* Increase all button sizes on mobile */
    .btn {
        min-height: var(--touch-target-comfortable);
        padding: 14px 20px;
        font-size: 1rem;
    }

    .btn-primary,
    .btn-large {
        min-height: var(--touch-target-large);
        padding: 16px 24px;
        font-size: 1.05rem;
    }

    /* More spacing between elements */
    .btn + .btn {
        margin-left: 10px;
    }

    /* Larger tap targets for critical actions */
    .listing-card,
    .category-card,
    .quick-action {
        margin-bottom: 16px;
    }

    /* Improve form input touch targets */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    textarea,
    select {
        min-height: var(--touch-target-comfortable);
        padding: 14px 16px;
        font-size: 16px; /* Prevents iOS zoom */
    }

    /* Better spacing for mobile navigation */
    .nav-buttons a {
        padding: 12px 16px;
    }

    /* Comfortable tap targets for list items */
    li {
        padding: 12px 0;
    }

    /* Larger touch targets for icons */
    .btn-icon {
        min-width: var(--touch-target-comfortable);
        min-height: var(--touch-target-comfortable);
        padding: 14px;
    }
}

/* =============================================================================
   VERY SMALL SCREENS (< 375px)
   ============================================================================= */

@media (max-width: 374px) {
    /* Even larger buttons on very small screens */
    .btn {
        width: 100%;
        justify-content: center;
        margin-bottom: 8px;
    }

    .btn + .btn {
        margin-left: 0;
    }
}

/* =============================================================================
   ACCESSIBILITY - KEYBOARD NAVIGATION
   ============================================================================= */

/* Visible focus indicators for keyboard navigation */
button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

/* =============================================================================
   PULL-TO-REFRESH INDICATOR
   ============================================================================= */

.pull-to-refresh {
    position: fixed;
    top: 60px;
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    background: white;
    padding: 12px 24px;
    border-radius: 24px;
    box-shadow: var(--box-shadow);
    display: flex;
    align-items: center;
    gap: 12px;
    z-index: 999;
    transition: transform 0.3s ease;
}

.pull-to-refresh.visible {
    transform: translateX(-50%) translateY(16px);
}

.pull-to-refresh-spinner {
    width: 20px;
    height: 20px;
    border: 3px solid var(--gray-light);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* =============================================================================
   HAPTIC FEEDBACK CLASSES (for JavaScript)
   ============================================================================= */

.haptic-light {
    /* Light haptic feedback - use for subtle interactions */
}

.haptic-medium {
    /* Medium haptic feedback - use for button presses */
}

.haptic-heavy {
    /* Heavy haptic feedback - use for important actions */
}

/* =============================================================================
   UTILITIES
   ============================================================================= */

/* No-scroll when modal/menu is open */
.no-scroll {
    overflow: hidden;
    position: fixed;
    width: 100%;
}

/* Touch-optimized scrollbar */
@media (hover: none) and (pointer: coarse) {
    /* Hide scrollbar on touch devices for cleaner UI */
    ::-webkit-scrollbar {
        display: none;
    }

    * {
        -ms-overflow-style: none;
        scrollbar-width: none;
    }
}
