/**
 * Mobile UI Fixes for BonnieDaysOut.scot
 * Fixes critical mobile UX issues identified during testing
 * 
 * Priority Issues Fixed:
 * 1. Filter panel overflow
 * 2. Cookie banner height
 * 3. Touch target sizes
 * 4. Hero section layout
 * 5. Featured cards scrolling
 * 6. Navigation menu size
 * 7. Search/filter feedback
 */

/* ===== CRITICAL FIX 1: Filter Panel Mobile Overlay ===== */
@media (max-width: 768px) {
    /* Hide filters panel by default on mobile */
    #filters-panel {
        position: fixed !important;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for iOS */
        background: white;
        z-index: var(--z-modal, 1050);
        transition: left 0.3s ease-in-out;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
        overscroll-behavior: contain;
    }

    /* Active/open state for filter panel */
    #filters-panel.active,
    #filters-panel.open {
        left: 0;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    }

    /* Filter panel header */
    .filter-panel-header {
        position: sticky;
        top: 0;
        background: white;
        border-bottom: 1px solid var(--color-border-light, #e5e7eb);
        padding: 16px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        z-index: 10;
    }

    /* Close button for filter panel */
    .filter-panel-close {
        min-width: 44px !important;
        min-height: 44px !important;
        padding: 8px !important;
        background: var(--color-bg-secondary, #f8fafc);
        border: none;
        border-radius: 8px;
        font-size: 24px;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        transition: transform 0.1s ease;
    }

    .filter-panel-close:active {
        transform: scale(0.95);
    }

    /* Filter panel content */
    #filters-panel .filters-content {
        padding: 16px;
        padding-bottom: 80px; /* Space for apply button */
    }

    /* Apply filters button - fixed at bottom */
    .filter-apply-button {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        border-top: 1px solid var(--color-border-light, #e5e7eb);
        padding: 16px;
        z-index: 11;
    }

    .filter-apply-button button {
        width: 100%;
        min-height: 48px !important;
        background: var(--color-primary-600, #002F85);
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        transition: transform 0.1s ease;
    }

    .filter-apply-button button:active {
        transform: scale(0.98);
    }

    /* Filter toggle button */
    .mobile-filter-toggle {
        position: fixed;
        bottom: 80px;
        right: 16px;
        min-width: 56px !important;
        min-height: 56px !important;
        background: var(--color-primary-600, #002F85);
        color: white;
        border: none;
        border-radius: 28px;
        font-size: 24px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        cursor: pointer;
        z-index: var(--z-sticky, 1020);
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        transition: all 0.2s ease;
        display: flex;
        align-items: center;
        justify-content: center;
    }

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

    /* Backdrop for filter panel */
    .filter-panel-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: calc(var(--z-modal, 1050) - 1);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease;
    }

    .filter-panel-backdrop.active {
        opacity: 1;
        visibility: visible;
    }
}

/* ===== CRITICAL FIX 2: Cookie Banner Height ===== */
@media (max-width: 768px) {
    .cookie-consent-banner {
        max-height: 30vh !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    /* Ensure scrollable content area */
    .cookie-consent-content {
        max-height: calc(30vh - 2rem) !important;
        overflow-y: auto !important;
        padding: 12px !important;
    }

    /* Compact header */
    .cookie-consent-header {
        position: sticky;
        top: 0;
        background: rgba(255, 255, 255, 0.98);
        z-index: 1;
        padding-bottom: 8px;
        margin-bottom: 8px !important;
    }

    .cookie-consent-header h3 {
        font-size: 1rem !important;
    }

    /* Smaller description text */
    .cookie-consent-description {
        font-size: 0.875rem !important;
        line-height: 1.4 !important;
        margin-bottom: 12px !important;
    }

    /* Accordion for cookie options */
    .cookie-options-details {
        max-height: 120px;
        overflow-y: auto;
    }

    /* Sticky action buttons */
    .cookie-consent-actions {
        position: sticky;
        bottom: 0;
        background: rgba(255, 255, 255, 0.98);
        padding-top: 8px;
        margin-top: 8px;
        border-top: 1px solid var(--color-border-light, #e5e7eb);
    }
}

/* ===== CRITICAL FIX 3: Touch Target Sizes ===== */
@media (max-width: 768px) {
    /* Universal touch target sizing */
    button,
    a.btn,
    .btn,
    input[type="checkbox"],
    input[type="radio"],
    .clickable,
    .interactive {
        min-width: 44px !important;
        min-height: 44px !important;
        touch-action: manipulation !important;
        -webkit-tap-highlight-color: transparent !important;
    }

    /* Checkbox and radio button sizing */
    input[type="checkbox"],
    input[type="radio"] {
        width: 24px !important;
        height: 24px !important;
        cursor: pointer;
    }

    /* Label touch targets */
    label {
        display: flex;
        align-items: center;
        min-height: 44px !important;
        padding: 8px 0 !important;
        cursor: pointer;
    }

    /* Navigation links */
    .nav-link,
    .mobile-nav-link {
        min-height: 48px !important;
        padding: 12px 16px !important;
        display: flex;
        align-items: center;
    }

    /* Form inputs */
    input[type="text"],
    input[type="search"],
    input[type="email"],
    input[type="tel"],
    textarea,
    select {
        min-height: 48px !important;
        padding: 12px !important;
        font-size: 16px !important; /* Prevents zoom on iOS */
    }

    /* Card action buttons */
    .card-actions button,
    .card-actions a {
        min-height: 44px !important;
        padding: 10px 16px !important;
    }

    /* Close buttons */
    .close-btn,
    [aria-label*="Close"],
    [aria-label*="close"] {
        min-width: 44px !important;
        min-height: 44px !important;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Social media icons */
    .social-icon {
        min-width: 44px !important;
        min-height: 44px !important;
        padding: 10px !important;
    }

    /* Pagination buttons */
    .pagination button,
    .pagination a {
        min-width: 44px !important;
        min-height: 44px !important;
        margin: 2px !important;
    }

    /* Dropdown triggers */
    .dropdown-toggle {
        min-height: 44px !important;
        padding: 8px 16px !important;
    }

    /* Tab navigation */
    .tab-button {
        min-height: 44px !important;
        padding: 10px 16px !important;
    }
}

/* ===== MAJOR FIX 4: Hero Section Mobile Layout ===== */
@media (max-width: 768px) {
    .hero {
        padding: 60px 16px 40px !important; /* Account for mobile header */
        min-height: auto !important;
    }

    .hero-content {
        text-align: center;
        max-width: 100% !important;
    }

    .hero h1 {
        font-size: 1.75rem !important;
        line-height: 1.2 !important;
        margin-bottom: 12px !important;
        padding: 0 8px;
    }

    .hero-subtitle {
        font-size: 1rem !important;
        line-height: 1.4 !important;
        margin-bottom: 16px !important;
        padding: 0 8px;
    }

    /* Stats badges - horizontal scroll with snap */
    .hero-stats {
        display: flex;
        gap: 12px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scroll-snap-type: x mandatory;
        padding: 8px 0;
        margin: 0 -16px; /* Full width scroll */
        padding-left: 16px;
        padding-right: 16px;
    }

    .stat-item {
        flex: 0 0 auto;
        min-width: 120px;
        background: rgba(255, 255, 255, 0.9);
        padding: 12px 16px;
        border-radius: 12px;
        text-align: center;
        scroll-snap-align: start;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .stat-number {
        display: block;
        font-size: 1.5rem !important;
        font-weight: 700;
        color: var(--color-primary-600, #002F85);
        margin-bottom: 4px;
    }

    .stat-label {
        font-size: 0.75rem !important;
        color: var(--color-text-secondary, #64748b);
        white-space: nowrap;
    }

    .stat-divider {
        display: none !important;
    }

    /* Hero CTA buttons */
    .hero-cta {
        display: flex;
        flex-direction: column;
        gap: 12px;
        margin-top: 24px;
        padding: 0 16px;
    }

    .hero-cta .btn {
        width: 100%;
        min-height: 48px !important;
        font-size: 1rem !important;
    }
}

/* ===== MAJOR FIX 5: Featured Cards Mobile Layout ===== */
@media (max-width: 768px) {
    .featured-grid,
    .location-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 16px !important;
        padding: 0 !important;
    }

    .location-card {
        width: 100% !important;
        margin: 0 !important;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    /* Card image aspect ratio */
    .card-image {
        aspect-ratio: 16/9;
        object-fit: cover;
        width: 100%;
        height: auto;
    }

    /* Card content padding */
    .card-content {
        padding: 16px !important;
    }

    .card-title {
        font-size: 1.125rem !important;
        margin-bottom: 8px !important;
    }

    .card-description {
        font-size: 0.875rem !important;
        line-height: 1.4 !important;
        margin-bottom: 12px !important;
    }

    /* Card meta information */
    .card-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        font-size: 0.75rem !important;
    }

    /* Card actions */
    .card-actions {
        display: flex;
        gap: 8px;
        margin-top: 12px;
    }

    .card-actions button,
    .card-actions a {
        flex: 1;
        text-align: center;
    }
}

/* ===== MAJOR FIX 6: Mobile Navigation Menu ===== */
@media (max-width: 768px) {
    /* Mobile nav overlay animation */
    .mobile-nav-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
        z-index: var(--z-modal-backdrop, 1040);
    }

    .mobile-nav-overlay.open,
    .mobile-nav-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    /* Mobile nav content */
    .mobile-nav-content {
        position: fixed;
        top: 0;
        right: -100%;
        width: 80%;
        max-width: 320px;
        height: 100vh;
        height: 100dvh;
        background: white;
        transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: var(--z-modal, 1050);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .mobile-nav-overlay.open .mobile-nav-content,
    .mobile-nav-overlay.active .mobile-nav-content {
        right: 0;
        box-shadow: -4px 0 20px rgba(0, 0, 0, 0.2);
    }

    /* Nav header */
    .mobile-nav-header {
        padding: 16px;
        border-bottom: 1px solid var(--color-border-light, #e5e7eb);
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: sticky;
        top: 0;
        background: white;
        z-index: 1;
    }

    /* Nav links styling */
    .mobile-nav-links {
        padding: 16px 0;
    }

    .mobile-nav-link {
        display: flex;
        align-items: center;
        min-height: 56px !important;
        padding: 16px 24px !important;
        font-size: 1rem !important;
        color: var(--color-text-primary, #111827);
        text-decoration: none;
        transition: background-color 0.2s ease;
        border-bottom: 1px solid var(--color-border-light, #e5e7eb);
    }

    .mobile-nav-link:active {
        background-color: var(--color-bg-secondary, #f8fafc);
    }

    .mobile-nav-link.active {
        color: var(--color-primary-600, #002F85);
        font-weight: 600;
        background-color: var(--color-primary-50, #ecfdf5);
    }

    /* Close button */
    .mobile-nav-close {
        min-width: 48px !important;
        min-height: 48px !important;
        background: var(--color-bg-secondary, #f8fafc);
        border: none;
        border-radius: 8px;
        font-size: 24px;
        color: var(--color-text-secondary, #64748b);
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: transform 0.1s ease;
    }

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

    /* Hamburger menu button */
    .mobile-menu-toggle {
        display: flex !important;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-width: 48px !important;
        min-height: 48px !important;
        padding: 12px;
        background: transparent;
        border: none;
        cursor: pointer;
        gap: 4px;
    }

    .mobile-menu-toggle span {
        display: block;
        width: 24px;
        height: 2px;
        background: var(--color-text-primary, #111827);
        transition: all 0.3s ease;
    }

    /* Animated hamburger */
    .mobile-menu-toggle.active span:nth-child(1) {
        transform: rotate(45deg) translate(5px, 5px);
    }

    .mobile-menu-toggle.active span:nth-child(2) {
        opacity: 0;
    }

    .mobile-menu-toggle.active span:nth-child(3) {
        transform: rotate(-45deg) translate(5px, -5px);
    }
}

/* ===== MAJOR FIX 7: Search/Filter Feedback ===== */
@media (max-width: 768px) {
    /* Loading overlay for filters */
    .filter-loading {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 10;
    }

    .filter-loading-spinner {
        width: 40px;
        height: 40px;
        border: 3px solid var(--color-primary-200, #a7f3d0);
        border-top-color: var(--color-primary-600, #002F85);
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

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

    /* Search input with loading state */
    .search-wrapper {
        position: relative;
    }

    .search-loading {
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        width: 20px;
        height: 20px;
    }

    .search-loading::after {
        content: '';
        display: block;
        width: 20px;
        height: 20px;
        border: 2px solid var(--color-primary-200, #a7f3d0);
        border-top-color: var(--color-primary-600, #002F85);
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

    /* Filter count badge */
    .filter-count-badge {
        position: absolute;
        top: -8px;
        right: -8px;
        background: var(--color-danger-500, #ef4444);
        color: white;
        font-size: 0.75rem;
        font-weight: 600;
        padding: 2px 6px;
        border-radius: 10px;
        min-width: 20px;
        text-align: center;
    }

    /* Active filter pills */
    .active-filters {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        padding: 12px;
        background: var(--color-bg-secondary, #f8fafc);
        border-radius: 8px;
        margin-bottom: 16px;
    }

    .filter-pill {
        display: flex;
        align-items: center;
        gap: 4px;
        background: white;
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 0.875rem;
        border: 1px solid var(--color-border-light, #e5e7eb);
    }

    .filter-pill-remove {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 20px;
        height: 20px;
        background: var(--color-bg-secondary, #f8fafc);
        border-radius: 50%;
        font-size: 12px;
        cursor: pointer;
        margin-left: 4px;
    }

    /* No results message */
    .no-results {
        text-align: center;
        padding: 48px 24px;
    }

    .no-results-icon {
        font-size: 3rem;
        margin-bottom: 16px;
        opacity: 0.5;
    }

    .no-results-title {
        font-size: 1.25rem;
        font-weight: 600;
        margin-bottom: 8px;
        color: var(--color-text-primary, #111827);
    }

    .no-results-message {
        font-size: 0.875rem;
        color: var(--color-text-secondary, #64748b);
        margin-bottom: 24px;
    }

    .no-results-action {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 12px 24px;
        background: var(--color-primary-600, #002F85);
        color: white;
        border: none;
        border-radius: 8px;
        font-weight: 500;
        cursor: pointer;
        text-decoration: none;
    }

    /* Filter success animation */
    @keyframes filterSuccess {
        0% { transform: scale(0.8); opacity: 0; }
        50% { transform: scale(1.1); }
        100% { transform: scale(1); opacity: 1; }
    }

    .filter-success-indicator {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: var(--color-primary-600, #002F85);
        color: white;
        padding: 16px 32px;
        border-radius: 8px;
        font-weight: 500;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        animation: filterSuccess 0.4s ease;
        z-index: var(--z-toast, 1080);
    }
}

/* ===== Additional Mobile Improvements ===== */
@media (max-width: 768px) {
    /* Ensure proper scrolling behavior */
    html, body {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        scroll-behavior: smooth !important;
        -webkit-overflow-scrolling: touch !important;
        /* Allow normal scrolling but prevent edge bounce */
        overscroll-behavior-x: none !important;
        overscroll-behavior-y: contain !important;
    }

    /* Container padding */
    .container {
        padding-left: 16px !important;
        padding-right: 16px !important;
    }

    /* Section spacing */
    section {
        padding: 40px 0 !important;
    }

    /* Heading sizes */
    h1 { font-size: 1.75rem !important; }
    h2 { font-size: 1.5rem !important; }
    h3 { font-size: 1.25rem !important; }
    h4 { font-size: 1.125rem !important; }

    /* Footer mobile layout */
    .footer-content {
        flex-direction: column !important;
        gap: 32px;
    }

    .footer-section {
        text-align: center;
    }

    /* Map container height - removed fixed height to allow Leaflet control */

    /* Modal improvements */
    .modal-content {
        margin: 16px !important;
        max-height: calc(100vh - 32px) !important;
        overflow-y: auto !important;
    }

    /* Table responsive scroll */
    .table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table {
        min-width: 600px;
    }

    /* Form layout */
    .form-group {
        margin-bottom: 16px !important;
    }

    .form-row {
        flex-direction: column !important;
        gap: 16px !important;
    }

    /* Button groups */
    .btn-group {
        display: flex;
        flex-direction: column;
        gap: 8px;
        width: 100%;
    }

    .btn-group .btn {
        width: 100%;
    }

    /* Breadcrumb scrolling */
    .breadcrumb {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        padding: 8px 0;
    }

    /* Tag cloud */
    .tag-cloud {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .tag {
        padding: 6px 12px;
        font-size: 0.875rem;
    }
}

/* ===== iOS Safari Specific Fixes ===== */
@supports (-webkit-touch-callout: none) {
    /* iOS momentum scrolling */
    .scrollable {
        -webkit-overflow-scrolling: touch;
    }

    /* iOS input zoom prevention */
    input[type="text"],
    input[type="search"],
    input[type="email"],
    input[type="tel"],
    textarea,
    select {
        font-size: 16px !important;
    }

    /* iOS tap highlight */
    a,
    button,
    input,
    textarea,
    select {
        -webkit-tap-highlight-color: transparent;
    }

    /* iOS safe area insets */
    .fixed-bottom {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .mobile-nav-content {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* ===== Landscape Mobile Adjustments ===== */
@media (max-width: 768px) and (orientation: landscape) {
    /* Reduce vertical spacing in landscape */
    .hero {
        padding: 40px 16px 24px !important;
    }

    .cookie-consent-banner {
        max-height: 50vh !important;
    }

    /* Side-by-side layout for CTAs */
    .hero-cta {
        flex-direction: row !important;
    }

    /* Adjust modal height */
    .modal-content {
        max-height: calc(100vh - 24px) !important;
    }
}

/* ===== Print Styles ===== */
@media print {
    /* Hide mobile-only elements */
    .mobile-filter-toggle,
    .mobile-nav-overlay,
    .filter-panel-backdrop,
    .cookie-consent-banner,
    .cookie-settings-btn {
        display: none !important;
    }
}