/**
 * Advanced Spare Parts Search - Frontend Styles
 * Stili per la barra di ricerca e il dropdown dei risultati
 */

/* Container principale */
.asps-search-wrapper {
    position: relative;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Input container */
.asps-search-container {
    position: relative;
    display: flex;
    align-items: center;
}

/* Input field */
.asps-search-input {
    width: 100%;
    padding: 14px 50px 14px 20px;
    font-size: 16px; /* IMPORTANTE: 16px minimo per evitare zoom su iOS Safari */
    line-height: 1.5;
    border: 2px solid #ddd;
    border-radius: 8px;
    outline: none;
    transition: all 0.3s ease;
    background: #fff;
    box-sizing: border-box;
    /* Previeni zoom su iOS */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

.asps-search-input:focus {
    border-color: #0073aa;
    box-shadow: 0 0 0 3px rgba(0, 115, 170, 0.1);
}

.asps-search-input::placeholder {
    color: #999;
}

/* Search icon */
.asps-search-icon,
.asps-search-loading {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #666;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Spinner animation */
.asps-spinner {
    animation: asps-spin 1s linear infinite;
}

@keyframes asps-spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Results dropdown */
.asps-search-results {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    max-height: 500px;
    overflow-y: auto;
    z-index: 1000;
}

/* Scrollbar personalizzata */
.asps-search-results::-webkit-scrollbar {
    width: 8px;
}

.asps-search-results::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 0 8px 8px 0;
}

.asps-search-results::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

.asps-search-results::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Results list */
.asps-results-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

/* Result item */
.asps-result-item {
    border-bottom: 1px solid #f0f0f0;
}

.asps-result-item:last-child {
    border-bottom: none;
}

.asps-result-link {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    text-decoration: none;
    color: inherit;
    transition: background-color 0.2s ease;
    gap: 12px;
}

.asps-result-link:hover,
.asps-result-item.asps-selected .asps-result-link {
    background-color: #f8f9fa;
}

/* Result image */
.asps-result-image,
.asps-result-image-placeholder {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
    border: 1px solid #e0e0e0;
}

.asps-result-image-placeholder {
    background: linear-gradient(135deg, #f5f5f5 0%, #e0e0e0 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #999;
    font-size: 12px;
}

.asps-result-image-placeholder::before {
    content: "📦";
    font-size: 24px;
}

/* Result content */
.asps-result-content {
    flex: 1;
    min-width: 0;
}

.asps-result-title {
    font-weight: 600;
    font-size: 14px;
    line-height: 1.4;
    margin-bottom: 4px;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.asps-result-title mark {
    background-color: #fff3cd;
    padding: 2px 4px;
    border-radius: 3px;
    font-weight: 700;
    color: #856404;
}

.asps-result-sku {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
    font-family: 'Courier New', monospace;
}

.asps-result-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    flex-wrap: wrap;
}

.asps-result-brand {
    color: #0073aa;
    font-weight: 500;
}

.asps-result-stock {
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.asps-result-stock.in-stock {
    background-color: #d4edda;
    color: #155724;
}

.asps-result-stock.out-of-stock {
    background-color: #f8d7da;
    color: #721c24;
}

/* Result price */
.asps-result-price {
    font-weight: 700;
    font-size: 16px;
    color: #0073aa;
    flex-shrink: 0;
    text-align: right;
}

.asps-result-price del {
    display: block;
    font-size: 12px;
    color: #999;
    font-weight: 400;
}

.asps-result-price ins {
    text-decoration: none;
    color: #d9534f;
}

/* View all link */
.asps-view-all {
    padding: 12px 16px;
    text-align: center;
    border-top: 1px solid #f0f0f0;
    background-color: #fafafa;
}

.asps-view-all a {
    color: #0073aa;
    text-decoration: none;
    font-weight: 600;
    font-size: 14px;
    transition: color 0.2s ease;
}

.asps-view-all a:hover {
    text-decoration: underline;
    color: #005177;
}

/* No results message */
.asps-no-results,
.asps-error {
    padding: 40px 20px;
    text-align: center;
    color: #666;
}

.asps-no-results svg,
.asps-error svg {
    margin-bottom: 12px;
    opacity: 0.5;
    stroke: currentColor;
    stroke-width: 2;
}

.asps-no-results p,
.asps-error p {
    margin: 0;
    font-size: 14px;
}

.asps-error {
    color: #d9534f;
}

/* Responsive */
@media (max-width: 768px) {
    .asps-search-wrapper {
        max-width: 100%;
        position: relative;
    }

    .asps-search-input {
        padding: 12px 45px 12px 16px;
        font-size: 16px; /* MANTIENI 16px anche su mobile per evitare zoom iOS! */
        /* Touch-friendly */
        -webkit-tap-highlight-color: rgba(0, 115, 170, 0.2);
    }

    /* Dropdown ottimizzato per mobile */
    .asps-search-results {
        /* Posizione fixed per occupare tutto lo schermo su mobile */
        position: fixed;
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        max-height: 70vh; /* 70% altezza viewport */
        border-radius: 16px 16px 0 0; /* Arrotonda solo sopra */
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.25);
        z-index: 9999;
        /* Smooth slide up animation */
        animation: slideUpMobile 0.3s ease-out;
        /* Permetti scroll su body bloccato */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scroll iOS */
    }

    @keyframes slideUpMobile {
        from {
            transform: translateY(100%);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }

    /* Aggiungi handle per chiudere */
    .asps-search-results::before {
        content: '';
        display: block;
        width: 40px;
        height: 4px;
        background: #ddd;
        border-radius: 2px;
        margin: 12px auto;
        flex-shrink: 0;
    }

    .asps-result-link {
        padding: 12px 16px; /* Più padding per touch */
        gap: 10px;
        min-height: 60px; /* Touch-friendly height */
    }

    .asps-result-image,
    .asps-result-image-placeholder {
        width: 50px;
        height: 50px;
    }

    .asps-result-title {
        font-size: 14px; /* Più grande su mobile */
    }

    .asps-result-price {
        font-size: 15px;
    }

    /* Backdrop scuro per mobile */
    .asps-mobile-backdrop {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9998;
        animation: fadeIn 0.3s ease-out;
        -webkit-tap-highlight-color: transparent;
    }

    @keyframes fadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }
}

/* Nascondi backdrop su desktop */
@media (min-width: 769px) {
    .asps-mobile-backdrop {
        display: none !important;
    }
}

@media (max-width: 480px) {
    .asps-result-link {
        flex-wrap: wrap;
    }

    .asps-result-price {
        width: 100%;
        text-align: left;
        margin-top: 8px;
        padding-top: 8px;
        border-top: 1px solid #f0f0f0;
    }
}

/* Accessibility */
.asps-search-input:focus-visible {
    outline: 2px solid #0073aa;
    outline-offset: 2px;
}

.asps-result-link:focus {
    outline: 2px solid #0073aa;
    outline-offset: -2px;
}

/* Loading state */
.asps-search-wrapper.loading .asps-search-input {
    padding-right: 50px;
}

/* Animation per comparsa dropdown */
@keyframes asps-fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.asps-search-results {
    animation: asps-fadeIn 0.2s ease-out;
}

/* Prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .asps-search-input {
        background: #2d2d2d;
        border-color: #444;
        color: #fff;
    }

    .asps-search-input:focus {
        border-color: #0096dd;
        box-shadow: 0 0 0 3px rgba(0, 150, 221, 0.2);
    }

    .asps-search-results {
        background: #2d2d2d;
        border-color: #444;
    }

    .asps-result-item {
        border-bottom-color: #444;
    }

    .asps-result-link:hover,
    .asps-result-item.asps-selected .asps-result-link {
        background-color: #3d3d3d;
    }

    .asps-result-title {
        color: #fff;
    }

    .asps-result-sku {
        color: #aaa;
    }

    .asps-view-all {
        background-color: #3d3d3d;
        border-top-color: #444;
    }

    .asps-no-results,
    .asps-error {
        color: #aaa;
    }
}
