/**
 * Food Store Search - Styles
 * Version: 1.0.0
 */

/* Search button styles */
.search-button {
    position: fixed;
    top: 3px;
    left: 275px;
    z-index: 1000;
    color: black;
    padding: 6px 12px;
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 32px;
    font-family: Arial, sans-serif;
    font-size: 14px;
    background-color: transparent;
    transition: left 0.15s ease-out;
}

.search-button:hover {
    opacity: 0.85;
}

.search-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
}

.search-icon svg {
    width: 32px;
    height: 32px;
    color: var(--wfs-primary-color, #267dc9);
    fill: var(--wfs-primary-color, #267dc9);
}

.search-icon i {
    color: black;
}

/* ==========================================================================
   iOS-SAFE SEARCH MODAL
   Uses 100dvh with fallbacks, safe-area-insets, and top-anchored design
   to prevent layout jumping when iOS keyboard opens.
   ========================================================================== */

/* Full-screen overlay - uses dvh for iOS keyboard safety */
.search-dialog {
    display: none; /* Hidden by default, JS shows it */
    position: fixed;
    z-index: 9999;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    /* Height fallbacks: 100% -> -webkit-fill-available -> 100dvh */
    height: 100%;
    height: -webkit-fill-available;
    height: 100dvh;
    background-color: rgba(0,0,0,0.85);
    overflow: hidden; /* Overlay itself doesn't scroll */
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    transition: opacity 0.2s ease-out;
    /* Flex container for content layout */
    flex-direction: column;
}

.search-dialog.visible {
    opacity: 1;
}

/* Prevent body scroll when modal is open - NO position:fixed to avoid scroll jump */
html.search-dialog-open,
html.search-dialog-open body {
    overflow: hidden !important;
    overscroll-behavior: none !important;
}

/* Search header - FIXED at top for iOS stability (survives app-switching) */
.search-dialog-content {
    background-color: #ffffff;
    /* Respect iOS notch/dynamic island */
    padding: 20px 0;
    padding-top: calc(20px + env(safe-area-inset-top, 0px));
    border: none;
    border-radius: 0 0 16px 16px;
    width: 100%;
    /* Fixed positioning - always anchored to viewport top, survives iOS keyboard/app-switch */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10000;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    animation: slideDown 0.25s ease-out;
    box-sizing: border-box;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-100%);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.search-close {
    color: #fff;
    font-size: 38px;
    font-weight: 300;
    line-height: 1;
    cursor: pointer;
    background-color: var(--wfs-primary-color, #267dc9);
    border: none;
    border-radius: 50%;
    width: 52px;
    height: 52px;
    min-width: 52px;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Adjust X position to be perfectly centered */
    padding-bottom: 4px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    flex-shrink: 0;
}

/* Search Form Styles */
.search-form {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 20px; /* Equal left and right padding */
}

/* Search Input Wrapper - contains input and clear button */
.search-input-wrapper {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
}

#product-search-input {
    flex: 1;
    width: 100%;
    padding: 14px 48px 14px 20px; /* Extra right padding for clear button */
    border: 1px solid #eaeaea;
    border-radius: 50px;
    font-size: 16px;
    height: 52px;
    box-sizing: border-box;
    caret-color: var(--wfs-primary-color, #267dc9);
    transition: all 0.3s ease;
    background-color: #f9f9f9;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

#product-search-input:focus {
    outline: none;
    border-color: var(--wfs-primary-color, #267dc9);
    box-shadow: 0 0 0 3px rgba(var(--wfs-primary-color-rgb, 38, 125, 201), 0.15);
    background-color: #ffffff;
}

/* Clear button inside search input - outlined circle style */
.search-input-clear {
    position: absolute;
    right: 14px;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    border: 2px solid #a0a0a0;
    background-color: transparent;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease, border-color 0.2s ease;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

.search-input-clear svg {
    width: 12px;
    height: 12px;
    color: #a0a0a0;
    stroke: #a0a0a0;
    stroke-width: 2.5;
    flex-shrink: 0;
}

.search-input-clear:hover {
    border-color: #707070;
}

.search-input-clear:hover svg {
    color: #707070;
    stroke: #707070;
}

.search-input-clear:active {
    transform: translateY(-50%) scale(0.92);
}

/* Show clear button when input has value */
.search-input-wrapper.has-value .search-input-clear {
    opacity: 1;
    visibility: visible;
}

#search-submit {
    display: none;
}

#search-submit svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

/* Search Results - Scrollable container with iOS-safe bottom padding */
.search-results {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch; /* Smooth iOS scrolling */
    margin-top: 15px;
    border-top: 1px solid #eaeaea;
    padding: 10px 20px;
    /* iOS safe area at bottom for home indicator */
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    /* Fixed height for scrolling to work - dvh for iOS keyboard safety */
    max-height: calc(70vh - 100px);
    max-height: calc(70dvh - 100px);
    /* Allow vertical scrolling on touch */
    touch-action: pan-y;
    overscroll-behavior: contain;
}

.search-results-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.search-result-item {
    display: flex;
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: none;
    margin-bottom: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    background-color: #eff1f4;
    border-left: 3px solid #7A8698;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.search-result-item:focus,
.search-result-item:active {
    outline: none;
    background-color: #eff1f4;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

@media (hover: hover) and (pointer: fine) {
    .search-result-item {
        transition: all 0.3s ease;
    }
    .search-result-item:hover,
    .search-result-item:focus {
        background-color: #f9f9f9;
        box-shadow: 0 3px 8px rgba(0,0,0,0.12);
        transform: translateY(-2px);
        border-left: 3px solid var(--wfs-primary-color, #267dc9);
    }
}

.search-result-image {
    width: 70px;
    min-width: 70px;
    margin-right: 15px;
    border-radius: 6px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #eff1f4;
}

.search-result-image img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 70px;
    object-fit: contain;
}

.search-result-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.search-result-info h4 {
    margin: 0 0 8px 0;
    font-size: 16px;
    font-weight: 600;
    color: #333;
    line-height: 1.3;
}

.search-result-price {
    margin: 0;
    color: var(--wfs-primary-color, #267dc9);
    font-weight: 700;
    font-size: 15px;
    display: inline-block;
}

.searching, .no-results, .error {
    padding: 20px;
    text-align: center;
    border-radius: 8px;
    margin-top: 15px;
    font-weight: 500;
}

.searching {
    color: var(--wfs-primary-color, #267dc9);
    background-color: rgba(var(--wfs-primary-color-rgb, 38, 125, 201), 0.05);
}

.no-results {
    color: #666;
    background-color: #eff1f4;
}

.error {
    color: #d9534f;
    background-color: rgba(217, 83, 79, 0.05);
}

/* ============================================
   Floating Button Style
   ============================================ */

.search-button-floating {
    position: fixed !important;
    bottom: 20px !important;
    left: 20px !important;
    top: auto !important;
    right: auto !important;
    width: 56px !important;
    height: 56px !important;
    min-width: 56px;
    min-height: 56px;
    background: var(--wfs-primary-color, #267dc9) !important;
    border-radius: 50% !important;
    padding: 0 !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 999 !important;
}

/* Only enable hover transitions after preload is removed */
.search-button-floating:not(.fss-preload) {
    transition: transform 0.3s ease, box-shadow 0.3s ease !important;
}

.search-button-floating:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.search-button-floating:active {
    transform: scale(0.98);
}

.search-button-floating .search-icon {
    font-size: 24px;
}

.search-button-floating .search-icon svg {
    width: 24px !important;
    height: 24px !important;
    color: #ffffff !important;
    fill: #ffffff !important;
}

/* Floating button responsive - consistent on all screen sizes */
@media screen and (max-width: 768px) {
    .search-button-floating {
        width: 56px !important;
        height: 56px !important;
        bottom: 20px !important;
        left: 20px !important;
    }
    
    .search-button-floating .search-icon svg {
        width: 24px !important;
        height: 24px !important;
    }
}

@media screen and (max-width: 480px) {
    .search-button-floating {
        width: 56px !important;
        height: 56px !important;
        bottom: 20px !important;
        left: 20px !important;
    }
    
    .search-button-floating .search-icon svg {
        width: 24px !important;
        height: 24px !important;
    }
}

/* ============================================
   Floating Button - Bottom Right Position
   ============================================ */

.search-button-floating.search-button-floating-right {
    left: auto !important;
    right: 20px !important;
}

@media screen and (max-width: 768px) {
    .search-button-floating.search-button-floating-right {
        left: auto !important;
        right: 20px !important;
    }
}

@media screen and (max-width: 480px) {
    .search-button-floating.search-button-floating-right {
        left: auto !important;
        right: 20px !important;
    }
}

/* ============================================
   Device Visibility
   ============================================ */

/* Mobile only - hide on desktop (≥600px) */
.fss-mobile-only {
    display: flex !important;
}

@media screen and (min-width: 600px) {
    .fss-mobile-only {
        display: none !important;
    }
}

/* Desktop only - hide on mobile (<600px) */
.fss-desktop-only {
    display: flex !important;
}

@media screen and (max-width: 599px) {
    .fss-desktop-only {
        display: none !important;
    }
}

/* ============================================
   Scroll Behavior
   ============================================ */

/* Preload state ALWAYS wins - prevents any flash on page load */
.search-button-floating.fss-preload,
.search-button-floating.fss-preload.fss-visible,
.search-button-floating.fss-preload.fss-hidden {
    transition: none !important;
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
    visibility: visible !important;
    pointer-events: auto !important;
}

/* Visible state - only applies after preload removed */
.search-button-floating:not(.fss-preload).fss-visible {
    opacity: 1 !important;
    pointer-events: auto !important;
    transform: translateY(0) scale(1) !important;
}

/* Only enable transitions after page is ready */
.search-button-floating.fss-ready:not(.fss-preload).fss-visible {
    transition: opacity 0.3s ease, transform 0.3s ease !important;
}

/* Hidden state for scroll/inactivity hide - only after preload removed */
.search-button-floating:not(.fss-preload).fss-hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: translateY(20px) scale(0.9) !important;
    transition: opacity 0.3s ease, transform 0.3s ease !important;
}

/* Desktop/Tablet: Position is set dynamically via JS relative to navigation */
@media screen and (min-width: 600px) {
    .search-button:not(.search-button-floating) {
        /* Default fallback, JS will override */
        left: 275px;
    }
}

/* Tablet-specific styles */
@media screen and (min-width: 600px) and (max-width: 768px) {
    .search-button:not(.search-button-floating) {
        padding: 0 12px 2px;
        height: 42px;
    }
    
    .search-icon {
        font-size: 22px;
    }
    
    #product-search-input {
        padding: 14px 16px;
    }
}

/* Mobile styles - iOS keyboard safe */
@media screen and (max-width: 599px) {
    .search-button:not(.search-button-floating) {
        padding: 0 12px;
        left: 60px;
        height: 44px;
    }
    
    .search-icon {
        font-size: 24px;
    }
    
    /* Mobile header with safe area support */
    .search-dialog-content {
        padding: 15px 0;
        padding-top: calc(15px + env(safe-area-inset-top, 0px));
        border-radius: 0 0 12px 12px;
    }
    
    .search-form {
        padding: 0 15px; /* Equal left/right on mobile */
        gap: 10px;
    }
    
    #product-search-input {
        padding: 12px 15px;
        font-size: 16px; /* 16px prevents iOS zoom on focus */
        height: 46px;
    }
    
    .search-close {
        width: 46px;
        height: 46px;
        min-width: 46px;
        font-size: 34px; /* Bigger X on mobile */
    }
    
    /* Results scrollable on mobile */
    .search-results {
        padding: 10px 15px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
        max-height: calc(60vh - 80px);
        max-height: calc(60dvh - 80px);
        touch-action: pan-y;
    }
}
