/* Species Guide Mobile Enhanced CSS */
/* Addresses P0 mobile UX fixes from comprehensive review */

/* ========================================
   Fix #1: Collapsible Filter Enhancements
   ======================================== */
@media (max-width: 991px) {
    /* Collapsible filter container styling */
    #mobileFilterCollapse.collapsing,
    #mobileFilterCollapse.collapse.show {
        background-color: var(--bs-light, #f8f9fa);
        border: 1px solid var(--bs-gray-300, #dee2e6);
        border-radius: 8px;
        padding: 16px;
        margin-bottom: 16px;
    }

    /* Active filter count badge */
    #activeFilterCount {
        font-size: 0.85rem;
        background-color: var(--bs-warning, #ffc107) !important;
        color: var(--bs-dark, #212529) !important;
    }

    /* Hide count badge when zero */
    #activeFilterCount.d-none {
        display: none !important;
    }

    /* Chevron rotation animation */
    button[data-bs-toggle="collapse"] .fa-chevron-down {
        transition: transform 0.3s ease;
    }

    button[data-bs-toggle="collapse"][aria-expanded="true"] .fa-chevron-down {
        transform: rotate(180deg);
    }

    /* Filter toggle button styling */
    .filter-toggle-btn {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
    }

    .filter-toggle-btn .filter-label {
        display: flex;
        align-items: center;
        gap: 8px;
    }
}

/* ========================================
   Fix #3: Species Card Sizing for Mobile
   ======================================== */
@media (max-width: 767px) {
    /* Reduce card height for more visible cards */
    .species-card-item .card,
    .species-card {
        max-height: 280px !important;
        overflow: hidden;
    }

    /* Smaller image height */
    .species-card-item .card-img-top,
    .species-card .card-img-top {
        height: 140px !important;
        object-fit: cover;
    }

    /* Compact card body padding */
    .species-card-item .card-body,
    .species-card .card-body {
        padding: 12px !important;
    }

    /* Smaller title font */
    .species-card-item .card-title,
    .species-card .card-title {
        font-size: 1rem !important;
        margin-bottom: 8px;
        line-height: 1.2;
    }

    /* Smaller scientific name */
    .species-card-item .text-muted,
    .species-card .text-muted {
        font-size: 0.8rem !important;
        margin-bottom: 4px;
    }

    /* Smaller badges */
    .species-card-item .badge,
    .species-card .badge {
        font-size: 0.75rem !important;
        padding: 4px 8px;
    }

    /* Ensure 2 columns on mobile for better visibility */
    #speciesGrid.row-cols-1 {
        --bs-gutter-x: 0.5rem;
        --bs-gutter-y: 0.5rem;
    }

    /* Override Bootstrap columns to ensure 2 cards per row on mobile */
    #speciesGrid > .col {
        flex: 0 0 auto;
        width: 50%;
        padding-right: calc(var(--bs-gutter-x) * 0.5);
        padding-left: calc(var(--bs-gutter-x) * 0.5);
    }
}

/* Slightly larger for tablets */
@media (min-width: 576px) and (max-width: 767px) {
    .species-card-item .card,
    .species-card {
        max-height: 300px !important;
    }

    .species-card-item .card-img-top,
    .species-card .card-img-top {
        height: 160px !important;
    }
}

/* ========================================
   Fix #4: Modal Image Sizing for Mobile
   ======================================== */
@media (max-width: 767px) {
    /* Larger modal images on mobile */
    #speciesDetailModal .modal-body img,
    #modalSpeciesImage {
        max-height: 50vh !important;
        width: 100% !important;
        object-fit: contain;
        margin-bottom: 16px;
    }

    /* Better modal dialog sizing */
    #speciesDetailModal .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    /* Reduce modal header/footer padding */
    #speciesDetailModal .modal-header,
    #speciesDetailModal .modal-footer {
        padding: 12px 16px;
    }

    /* Ensure modal content is scrollable */
    #speciesDetailModal .modal-body {
        max-height: calc(100vh - 120px);
        overflow-y: auto;
        padding: 16px;
    }

    /* Placeholder sizing */
    #modalImagePlaceholder {
        min-height: 30vh !important;
        max-height: 50vh !important;
    }
}

/* ========================================
   Fix #5: Touch Target Improvements
   ======================================== */
@media (max-width: 767px) {
    /* Minimum touch target: 44x44px per Apple/Google guidelines */
    .filter-bar-sticky .btn,
    .filter-bar-sticky .form-select,
    .filter-bar-sticky .form-control,
    #mobileFilterCollapse .btn,
    #mobileFilterCollapse .form-select,
    #mobileFilterCollapse .form-control {
        min-height: 44px;
        padding: 10px 12px;
        font-size: 16px; /* Prevents zoom on iOS */
    }

    /* Family filter buttons */
    .filter-bar-sticky .btn-outline-info,
    .filter-bar-sticky .btn-info,
    #mobileFilterCollapse .btn-outline-info,
    #mobileFilterCollapse .btn-info {
        padding: 10px 16px;
        font-size: 0.95rem;
        min-height: 44px;
        margin: 2px;
    }

    /* Clear button enhancement */
    #clearFiltersButton {
        padding: 10px 20px;
        font-size: 1rem;
        min-height: 44px;
        font-weight: 500;
    }

    /* Tag buttons */
    .filter-bar-sticky .badge,
    #mobileFilterCollapse .badge {
        min-height: 36px;
        padding: 8px 12px;
        font-size: 0.9rem;
        display: inline-flex;
        align-items: center;
        cursor: pointer;
    }

    /* Range inputs need larger touch areas */
    input[type="range"] {
        min-height: 44px;
        padding: 10px 0;
    }

    /* Ensure clickable cards have visual feedback */
    .species-card {
        cursor: pointer;
        transition: transform 0.2s ease, box-shadow 0.2s ease;
    }

    .species-card:active {
        transform: scale(0.98);
    }
}

/* ========================================
   Additional Mobile Optimizations
   ======================================== */

/* Dark mode support for filter collapse */
@media (prefers-color-scheme: dark) {
    #mobileFilterCollapse.collapsing,
    #mobileFilterCollapse.collapse.show {
        background-color: var(--bs-dark, #212529);
        border-color: var(--bs-gray-700, #495057);
    }

    #activeFilterCount {
        background-color: var(--bs-warning, #ffc107) !important;
        color: var(--bs-dark, #212529) !important;
    }
}

/* Smooth transitions for filter collapse */
#mobileFilterCollapse {
    transition: none; /* Let Bootstrap handle the transition */
}

#mobileFilterCollapse.collapsing {
    transition: height 0.35s ease;
}

/* Better spacing for mobile filter sections */
@media (max-width: 991px) {
    #mobileFilterCollapse .filter-section {
        margin-bottom: 16px;
        padding-bottom: 16px;
        border-bottom: 1px solid var(--bs-gray-300, #dee2e6);
    }

    #mobileFilterCollapse .filter-section:last-child {
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
    }
}

/* Ensure filter stays sticky when appropriate */
@media (min-width: 992px) {
    .filter-bar-sticky {
        position: sticky;
        top: 60px; /* Adjust based on navbar height */
        z-index: 100;
        background-color: var(--bs-white, #ffffff);
    }
}

/* Loading state for filter updates */
.filter-loading {
    opacity: 0.6;
    pointer-events: none;
}

.filter-loading::after {
    content: " ";
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid transparent;
    border-top-color: var(--bs-primary, #0d6efd);
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
    margin-left: 8px;
    vertical-align: middle;
}

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

/* Accessibility improvements */
.species-card:focus-visible,
.filter-toggle-btn:focus-visible,
button:focus-visible,
a:focus-visible {
    outline: 2px solid var(--bs-primary, #0d6efd);
    outline-offset: 2px;
}

/* Prevent layout shift when loading */
.species-card-item {
    min-height: 280px;
}

@media (min-width: 768px) {
    .species-card-item {
        min-height: 320px;
    }
}