/**
 * Custom Selects Styling for Tom Select
 *
 * This stylesheet contains the styling for the Tom Select
 * enhanced dropdowns in the Moth Heatmap plugin.
 *
 * @package MothHeatmap
 */

/* ===================================================================
   FOUC (Flash of Unstyled Content) Prevention
   Hide native selects until Tom Select is initialized
   =================================================================== */

/* Hide native selects initially to prevent FOUC */
.moth-heatmap-species,
.moth-heatmap-counties {
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.1s ease-in-out, visibility 0.1s ease-in-out; /* Faster, smoother transition */
}

/* Show selects when Tom Select is ready */
.moth-heatmap-selects-ready .moth-heatmap-species,
.moth-heatmap-selects-ready .moth-heatmap-counties {
    opacity: 1;
    visibility: visible;
}

/* Keep native selects hidden during loading/reinitialization */
.moth-heatmap-select-loading .moth-heatmap-species,
.moth-heatmap-select-loading .moth-heatmap-counties {
    opacity: 0 !important;
    visibility: hidden !important;
}

/* Loading state for selects - make it less disruptive */
.moth-heatmap-select-loading {
    position: relative;
}

.moth-heatmap-select-loading::after {
    content: ""; /* Remove loading text to reduce visual disruption */
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 4px;
    z-index: 10;
    pointer-events: none;
    opacity: 0.3; /* Make it more subtle */
    transition: opacity 0.1s ease-in-out;
}

/* Container for Tom Select */
.ts-wrapper {
    width: 100%;
    position: relative;
    border-radius: 4px;
}

/* Input control */
.ts-control {
    border: 1px solid #d1d5db;
    padding: 8px 12px;
    width: 100%;
    overflow: hidden;
    position: relative;
    z-index: 1;
    box-sizing: border-box;
    background-color: #fff;
    cursor: text;
    border-radius: 4px;
    min-height: 44px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    font-size: 16px; /* Prevents iOS zoom - must be 16px or larger */
}

/* Focus state */
.ts-wrapper.focus .ts-control {
    border-color: #4f46e5;
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.2);
    outline: none;
}

/* Keyboard focus for accessibility */
.ts-wrapper:focus-within .ts-control {
    border-color: #4f46e5;
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.2);
    outline: 2px solid transparent; /* For high contrast mode */
}

/* Focus for dropdown options */
.ts-dropdown .option:focus {
    background-color: #4f46e5;
    color: #ffffff;
    outline: 2px solid transparent; /* For high contrast mode */
}

/* Dropdown */
.ts-dropdown {
    position: absolute;
    z-index: 10;
    border: 1px solid #d1d5db;
    background: #fff;
    margin: 2px 0 0;
    border-radius: 4px;
    box-shadow:
        0 4px 6px -1px rgba(0, 0, 0, 0.1),
        0 2px 4px -1px rgba(0, 0, 0, 0.06);
    box-sizing: border-box;
    left: 0;
    right: 0;
    overflow: hidden;
    width: 100%;
    min-width: 200px;
}

/* Dropdown content */
.ts-dropdown-content {
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 280px;
    scrollbar-width: thin;
}

/* Options */
.ts-dropdown .option {
    padding: 10px 12px;
    cursor: pointer;
    font-size: 16px; /* Prevents iOS zoom - must be 16px or larger */
    line-height: 1.5;
    min-height: 44px; /* Improved touch target size for accessibility */
    display: flex;
    align-items: center;
}

.ts-dropdown .option.active {
    background-color: #f3f4f6;
    color: #111827;
}

.ts-dropdown .option:hover {
    background-color: #eff6ff;
    color: #1e40af;
}

/* Selected item tag styling */
.ts-control .item {
    background-color: #e5e7eb;
    color: #374151;
    border-radius: 3px;
    padding: 4px 10px;
    margin: 2px 4px 2px 0;
    display: inline-flex;
    align-items: center;
    cursor: default;
    font-size: 16px;
    min-height: 30px;
}

/* Remove button for items */
.ts-control .item .remove {
    margin-left: 6px;
    cursor: pointer;
    display: inline-block;
    padding: 0 2px;
    font-size: 16px;
    color: #6b7280;
}

.ts-control .item .remove:hover {
    color: #ef4444;
}

/* Placeholder */
.ts-control input::placeholder {
    color: #6b7280; /* Improved contrast ratio for accessibility */
    opacity: 1;
    font-size: 16px; /* Prevents iOS zoom - must be 16px or larger */
}

/* Ensure all input elements have proper font-size */
.ts-control input {
    font-size: 16px; /* Prevents iOS zoom - must be 16px or larger */
    border: none;
    outline: none;
    background: transparent;
    color: inherit;
}

/* Mobile optimizations */
@media (max-width: 768px) {
    .ts-wrapper {
        font-size: 16px; /* Prevents iOS zoom on focus */
        width: 100%;
    }

    .ts-control {
        min-height: 50px; /* Larger touch target */
        padding: 12px 14px; /* Increased padding for better touch experience */
        font-size: 16px; /* Ensures no zoom on iOS */
    }

    .ts-control input {
        font-size: 16px; /* Critical: prevents iOS zoom */
    }

    .ts-dropdown .option {
        padding: 16px 14px; /* Larger touch targets for options */
        font-size: 16px; /* Prevents iOS zoom */
        min-height: 48px; /* Improved accessibility - larger touch targets */
    }

    .ts-dropdown-content {
        max-height: 320px; /* More space on mobile */
    }

    /* Improve touch targets */
    .ts-control .item {
        padding: 6px 12px; /* Larger padding for better touch */
        margin: 3px 5px 3px 0;
        font-size: 16px; /* Consistent font-size */
        min-height: 36px; /* Better touch target */
    }

    .ts-control .item .remove {
        padding: 6px; /* Larger touch area */
        margin-left: 8px;
        font-size: 16px; /* Consistent font-size */
        min-width: 24px; /* Minimum touch target size */
        min-height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* Dark mode support if needed */
@media (prefers-color-scheme: dark) {
    body.supports-dark-theme .ts-control {
        background-color: #1f2937;
        border-color: #4b5563;
        color: #f9fafb;
    }

    body.supports-dark-theme .ts-dropdown {
        background-color: #1f2937;
        border-color: #4b5563;
    }

    body.supports-dark-theme .ts-dropdown .option {
        color: #f9fafb;
    }

    body.supports-dark-theme .ts-dropdown .option.active {
        background-color: #374151;
        color: #f9fafb;
    }

    body.supports-dark-theme .ts-dropdown .option:hover {
        background-color: #4b5563; /* Improved contrast */
        color: #f9fafb;
    }

    body.supports-dark-theme .ts-dropdown .option:focus {
        background-color: #6366f1; /* Better contrast for focus */
        color: #ffffff;
    }

    body.supports-dark-theme .ts-control .item {
        background-color: #374151;
        color: #f9fafb;
    }

    body.supports-dark-theme .ts-control input::placeholder {
        color: #9ca3af; /* Better contrast in dark mode */
    }

    body.supports-dark-theme .ts-wrapper.focus .ts-control,
    body.supports-dark-theme .ts-wrapper:focus-within .ts-control {
        border-color: #6366f1;
        box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.3);
    }
}

/* Animations */
.ts-dropdown.ts-hidden {
    opacity: 0;
    transform: translateY(-5px);
    transition:
        opacity 150ms,
        transform 150ms;
}

.ts-dropdown {
    opacity: 1;
    transform: translateY(0);
    transition:
        opacity 150ms,
        transform 150ms;
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    .ts-dropdown.ts-hidden,
    .ts-dropdown {
        transition: none;
        transform: none;
    }
    
    .moth-heatmap-species,
    .moth-heatmap-counties {
        transition: none;
    }
}
