/* Consolidated Search CSS - Mobile-First Design with Scottish Heritage Theme */
/* Combines mobile-search-advanced.css and mobile-search-fix.css */
/* Z-index hierarchy: Search (1080) > Filters (1040-1050) > FAB (1001-1002) */

/* ==================== SCOTTISH HERITAGE COLOR VARIABLES ==================== */
:root {
    --scottish-primary: #2E5BBA;    /* Tartan blue */
    --scottish-secondary: #8B5A96;  /* Heather purple */
    --scottish-accent: #4A7C59;     /* Highland green */
    --scottish-light: #E8F2FF;      /* Light blue tint */
    --scottish-text: #2c3e50;       /* Dark blue text */
    --scottish-success: #4A7C59;    /* Highland green for success states */
    --scottish-warning: #D4A574;    /* Scottish gold for warnings */
}

/* ==================== SEARCH TOGGLE BUTTON ==================== */
.search-toggle {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    color: #666;
    transition: color 0.2s ease;
    z-index: 2;
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.search-toggle:hover {
    color: #333;
    background-color: #f0f0f0;
}

.search-toggle:active {
    transform: translateY(-50%) scale(0.95);
}

.search-toggle .search-icon {
    display: block;
    width: 20px;
    height: 20px;
}

/* ==================== SEARCH CONTAINER ==================== */
.search-container {
    position: relative;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.search-container #search-input {
    padding-right: 40px; /* Make room for toggle button */
    box-sizing: border-box;
    width: 100%;
}

/* ==================== SEARCH OVERLAY SYSTEM ==================== */
.search-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1080; /* Above filter system (1040-1050) and FAB (1001-1002) */
    display: none;
    background: linear-gradient(135deg, rgba(46,91,186,0.15) 0%, rgba(139,90,150,0.15) 100%), 
                rgba(0, 0, 0, 0.45);
    opacity: 0;
    transition: opacity 0.2s ease;
}

/* Progressive enhancement: Add backdrop-filter only on capable devices */
@supports (backdrop-filter: blur(1px)) {
    .search-overlay {
        backdrop-filter: blur(4px);
    }
}

@supports (-webkit-backdrop-filter: blur(1px)) {
    .search-overlay {
        -webkit-backdrop-filter: blur(4px);
    }
}

.search-overlay.active {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 1rem;
    padding-top: 2rem;
    opacity: 1;
}

.search-overlay-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1079;
}

.search-overlay-content {
    position: relative;
    z-index: 1081;
    width: 100%;
    max-width: 600px;
    max-height: 80vh;
    background: white;
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(46, 91, 186, 0.2);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transform: translateY(-20px);
    transition: transform 0.2s ease;
}

.search-overlay.active .search-overlay-content {
    transform: translateY(0);
}

/* ==================== SEARCH OVERLAY HEADER ==================== */
.search-overlay-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e0e0e0;
    background: linear-gradient(135deg, var(--scottish-light) 0%, #f9f9f9 100%);
    flex-shrink: 0;
}

.search-overlay-header h2 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--scottish-primary);
}

.search-overlay-close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    color: #666;
    transition: all 0.2s ease;
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.search-overlay-close:hover {
    background-color: #e0e0e0;
    color: #333;
}

.search-overlay-close:active {
    transform: scale(0.95);
}

/* ==================== SEARCH OVERLAY BODY ==================== */
.search-overlay-body {
    flex: 1;
    padding: 1.5rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.search-overlay-input-container {
    position: relative;
    margin-bottom: 1.5rem;
}

#search-overlay-input {
    width: 100%;
    padding: 12px 16px;
    font-size: 16px; /* Prevent iOS zoom */
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    background: white;
    outline: none;
    transition: all 0.2s ease;
    box-sizing: border-box;
    -webkit-appearance: none; /* iOS reset */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
}

#search-overlay-input:focus {
    border-color: var(--scottish-primary);
    box-shadow: 0 2px 8px rgba(46, 91, 186, 0.15);
    transform: translateY(-1px);
}

/* ==================== SEARCH RESULTS ==================== */
.search-results {
    display: none;
}

.search-results.active {
    display: block;
}

.search-result-item {
    padding: 12px 16px;
    border: 1px solid #e8e8e8;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 8px;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 44px; /* WCAG touch target */
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.search-result-item:hover {
    background-color: var(--scottish-light);
    border-color: var(--scottish-primary);
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(46, 91, 186, 0.1);
}

.search-result-item:active {
    background-color: #e9ecef;
    transform: scale(0.98);
}

.search-result-item.highlighted {
    background-color: #e7f3ff;
    border-color: #4a90e2;
}

.search-result-image {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    object-fit: cover;
    flex-shrink: 0;
}

.search-result-content {
    flex: 1;
    min-width: 0;
}

.search-result-name {
    font-weight: 700;
    color: var(--scottish-primary);
    margin-bottom: 2px;
    font-size: 1rem;
}

.search-result-category {
    font-size: 0.85rem;
    color: var(--scottish-secondary);
    margin-bottom: 2px;
    font-weight: 500;
}

.search-result-description {
    font-size: 0.8rem;
    color: var(--scottish-text);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* ==================== SEARCH PLACEHOLDER ==================== */
.search-placeholder {
    text-align: center;
    padding: 2rem 1rem;
    color: #666;
}

.search-placeholder.hidden {
    display: none;
}

.search-placeholder-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

.search-placeholder p {
    margin: 0 0 1.5rem 0;
    font-size: 1.1rem;
    line-height: 1.5;
}

.search-placeholder-tips {
    background: #f8f9fa;
    padding: 1rem;
    border-radius: 8px;
    text-align: left;
    max-width: 300px;
    margin: 0 auto;
}

.search-placeholder-tips p {
    margin: 0 0 0.5rem 0;
    font-weight: 600;
    color: #333;
}

.search-placeholder-tips ul {
    margin: 0;
    padding-left: 1.2rem;
}

.search-placeholder-tips li {
    margin-bottom: 0.3rem;
    color: #666;
    font-size: 0.9rem;
}

/* ==================== RECENT SEARCHES ==================== */
.search-recent {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #e0e0e0;
}

.search-recent h3 {
    margin: 0 0 0.5rem 0;
    font-size: 1rem;
    color: #333;
}

.search-recent-items {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.search-recent-item {
    background: #f0f0f0;
    padding: 6px 12px;
    border-radius: 16px;
    font-size: 0.9rem;
    color: #666;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 32px;
    display: flex;
    align-items: center;
}

.search-recent-item:hover {
    background: #e0e0e0;
    color: #333;
}

.search-recent-item:active {
    transform: scale(0.95);
}

/* ==================== MOBILE OPTIMIZATIONS ==================== */
@media (max-width: 768px) {
    .search-overlay {
        padding: 0.5rem;
        padding-top: 1rem;
    }
    
    .search-overlay-content {
        max-height: 90vh;
        border-radius: 8px;
    }
    
    .search-overlay-header {
        padding: 0.75rem 1rem;
    }
    
    .search-overlay-header h2 {
        font-size: 1.1rem;
    }
    
    .search-overlay-body {
        padding: 1rem;
    }
    
    .search-placeholder {
        padding: 1.5rem 0.5rem;
    }
    
    .search-placeholder-icon {
        font-size: 2.5rem;
    }
    
    .search-placeholder p {
        font-size: 1rem;
    }
    
    .search-placeholder-tips {
        padding: 0.75rem;
    }
    
    .search-result-item {
        padding: 10px 16px;
        min-height: 44px;
    }
}

/* ==================== MOBILE INPUT FIXES ==================== */
@media (max-width: 640px) {
    /* Ensure search inputs are properly positioned */
    #search-input,
    .search-input,
    input[type="search"],
    input[type="text"][id*="search"],
    input[type="text"][placeholder*="Search"],
    input[type="text"][placeholder*="search"] {
        position: relative;
        left: 0;
        right: auto;
        top: auto;
        bottom: auto;
        transform: none;
        margin: 0.5rem 1rem;
        padding: 0.75rem;
        width: calc(100% - 2rem);
        max-width: calc(100% - 2rem);
        min-width: 0;
        display: block;
        visibility: visible;
        opacity: 1;
        z-index: 10;
        font-size: 16px; /* Prevent iOS zoom */
        -webkit-appearance: none;
        line-height: 1.5;
        height: auto;
        min-height: 44px; /* WCAG touch target minimum */
        box-sizing: border-box;
        float: none;
        flex: none;
        grid-area: auto;
        background-color: white;
        border: 1px solid #ccc;
    }
    
    /* Ensure parent containers support search properly */
    .search-container,
    .search-wrapper,
    .filter-panel,
    .filters-container,
    .filter-section,
    .mobile-filter-panel,
    .search-section {
        position: relative;
        overflow: visible;
        z-index: 1;
        display: block;
        width: 100%;
        max-width: 100%;
    }
    
    /* Prevent horizontal scroll */
    html, body {
        overflow-x: hidden;
        max-width: 100vw;
        box-sizing: border-box;
    }
    
    * {
        max-width: 100vw;
        box-sizing: border-box;
    }
}

/* ==================== iOS SAFARI SPECIFIC ==================== */
@supports (-webkit-touch-callout: none) {
    .search-overlay {
        /* Account for iOS safe areas */
        padding-top: env(safe-area-inset-top, 1rem);
        padding-bottom: env(safe-area-inset-bottom, 1rem);
    }
    
    #search-overlay-input {
        /* Prevent iOS zoom and styling issues */
        font-size: 16px;
        -webkit-appearance: none;
        border-radius: 8px;
    }
    
    .search-overlay-body {
        /* Smooth scrolling on iOS */
        -webkit-overflow-scrolling: touch;
        /* Prevent bounce */
        overscroll-behavior: contain;
    }
}

/* ==================== TOUCH OPTIMIZATIONS ==================== */
@media (hover: none) and (pointer: coarse) {
    .search-toggle {
        min-width: 44px;
        min-height: 44px;
    }
    
    .search-overlay-close {
        min-width: 44px;
        min-height: 44px;
    }
    
    .search-result-item {
        padding: 16px;
        min-height: 44px;
    }
    
    .search-recent-item {
        min-height: 32px;
        padding: 8px 16px;
    }
}

/* ==================== HIGH DPI DISPLAYS ==================== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx) {
    /* Enhanced blur for high DPI displays with progressive enhancement */
    @supports (backdrop-filter: blur(1px)) {
        .search-overlay {
            backdrop-filter: blur(6px);
        }
    }
    
    @supports (-webkit-backdrop-filter: blur(1px)) {
        .search-overlay {
            -webkit-backdrop-filter: blur(6px);
        }
    }
}

/* ==================== ACCESSIBILITY ==================== */
.search-overlay[aria-hidden="true"] {
    display: none;
}

.search-overlay:not([aria-hidden="true"]) {
    display: flex;
}

/* Focus Management */
.search-overlay-content:focus-within {
    outline: none;
}

#search-overlay-input:focus {
    outline: none;
    border-color: #4a90e2;
    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}

/* ==================== Z-INDEX HIERARCHY ENFORCEMENT ==================== */
/* Ensure proper layering above filter system */
body.mobile-filters-open .search-overlay {
    z-index: 1080; /* Above filter backdrop (1040) and filter panel (1050) */
}

/* Preserve essential positioned elements */
.mobile-filter-fab,
.fab {
    z-index: 1002; /* Below search overlay */
}

.dropdown,
.modal:not(.search-overlay),
.tooltip,
.popover,
[role="tooltip"],
[role="dialog"]:not(.search-overlay) {
    z-index: 1070; /* Below search overlay but above filters */
}

/* ==================== LOADING AND ERROR STATES ==================== */
.search-overlay.loading .search-overlay-content {
    opacity: 0.7;
    pointer-events: none;
}

.search-overlay.loading .search-overlay-body::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #4a90e2;
    border-top-color: transparent;
    border-radius: 50%;
    animation: search-spin 1s linear infinite;
}

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

.search-error {
    color: #d32f2f;
    font-size: 0.9rem;
    text-align: center;
    padding: 1rem;
    background: #ffebee;
    border-radius: 4px;
    margin: 1rem 0;
}

/* ==================== HIGHLIGHTED SEARCH TERMS ==================== */
.search-highlight {
    background-color: var(--scottish-warning);
    color: #5a3e1a;
    font-weight: 600;
    padding: 2px 4px;
    border-radius: 3px;
}