/* ═══════════════════════════════════════════════════════════
   PeptideSelector — Custom Searchable Dropdown Component
   Uses design tokens from variables.css for full theme compat.
   ═══════════════════════════════════════════════════════════ */

/* ─── Wrapper ─────────────────────────────────────────────── */
.ps-wrapper {
    position: relative;
    width: 100%;
}

/* ─── Trigger (looks like form-input) ─────────────────────── */
.ps-trigger {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    min-height: 42px;
    padding: 0 var(--space-3);
    font-size: var(--text-base);
    font-family: var(--font-sans);
    line-height: 1.5;
    color: var(--color-text-primary);
    background-color: var(--color-input-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: border-color var(--duration-fast) var(--ease-in-out),
                box-shadow var(--duration-fast) var(--ease-in-out);
    user-select: none;
    -webkit-user-select: none;
}

.ps-trigger:hover {
    border-color: var(--color-border-strong);
}

.ps-trigger.ps-open,
.ps-trigger:focus-within {
    border-color: var(--color-border-strong);
    outline: none;
}

.ps-trigger.is-invalid {
    border-color: var(--color-danger);
}

.ps-trigger-input {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    font-size: var(--text-base);
    font-family: var(--font-sans);
    color: var(--color-text-primary);
    line-height: 1.5;
    padding: var(--space-2) 0;
    cursor: pointer;
}

.ps-trigger-input:not([readonly]) {
    cursor: text;
}

.ps-trigger-input::placeholder {
    color: var(--color-text-tertiary);
}

.ps-trigger-chevron {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    color: var(--color-text-tertiary);
    transition: transform var(--duration-fast) var(--ease-out);
}

.ps-open .ps-trigger-chevron {
    transform: rotate(180deg);
}

.ps-trigger-clear {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: var(--radius-full);
    color: var(--color-text-tertiary);
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    transition: background-color var(--duration-fast) var(--ease-in-out),
                color var(--duration-fast) var(--ease-in-out);
}

.ps-trigger-clear:hover {
    background-color: var(--color-bg-tertiary);
    color: var(--color-text-primary);
}

/* ─── Dropdown Panel ──────────────────────────────────────── */
.ps-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    z-index: var(--z-dropdown);
    background-color: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    opacity: 0;
    transform: translateY(-4px);
    pointer-events: none;
    transition: opacity var(--duration-fast) var(--ease-out),
                transform var(--duration-fast) var(--ease-out);
}

.ps-dropdown.ps-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
    border-color: var(--color-border-strong);
}

/* ─── Search Input ────────────────────────────────────────── */
.ps-search-wrap {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: var(--space-2);
    background-color: var(--color-bg-elevated);
    border-bottom: 1px solid var(--color-border-light);
}

.ps-search {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background-color: var(--color-bg-tertiary);
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    transition: border-color var(--duration-fast) var(--ease-in-out),
                background-color var(--duration-fast) var(--ease-in-out);
}

.ps-search:focus-within {
    border-color: var(--color-primary);
    background-color: var(--color-input-bg);
}

.ps-search-icon {
    flex-shrink: 0;
    width: 15px;
    height: 15px;
    color: var(--color-text-tertiary);
}

.ps-search-input {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    color: var(--color-text-primary);
    line-height: 1.5;
    padding: 0;
}

.ps-search-input::placeholder {
    color: var(--color-text-tertiary);
}

/* ─── Options List ────────────────────────────────────────── */
.ps-options {
    max-height: 260px;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: var(--space-1) 0;
    scroll-behavior: smooth;
}

/* Custom scrollbar */
.ps-options::-webkit-scrollbar {
    width: 6px;
}
.ps-options::-webkit-scrollbar-track {
    background: transparent;
}
.ps-options::-webkit-scrollbar-thumb {
    background: var(--color-border-strong);
    border-radius: var(--radius-full);
}
.ps-options::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-tertiary);
}

/* ─── Category Header ─────────────────────────────────────── */
.ps-category {
    padding: var(--space-2) var(--space-3) var(--space-1);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    color: var(--color-text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    user-select: none;
    -webkit-user-select: none;
}

.ps-category:not(:first-child) {
    margin-top: var(--space-1);
    border-top: 1px solid var(--color-border-light);
    padding-top: var(--space-2);
}

/* ─── Option Item ─────────────────────────────────────────── */
.ps-option {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-base);
    color: var(--color-text-primary);
    cursor: pointer;
    transition: background-color var(--duration-fast) var(--ease-in-out);
    user-select: none;
    -webkit-user-select: none;
}

.ps-option:hover,
.ps-option.ps-highlighted {
    background-color: var(--color-bg-tertiary);
}

.ps-option.ps-selected {
    font-weight: var(--weight-medium);
}

.ps-option.ps-selected::after {
    content: '';
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-left: auto;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2310B981' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
}

.ps-option-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ─── Option Meta (stash icon + last dose) ────────────────── */
.ps-option-meta {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.ps-option-stash {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--color-success);
    opacity: 0.7;
}

.ps-option-lastdose {
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
    white-space: nowrap;
}

/* ─── Highlight Match ─────────────────────────────────────── */
.ps-match {
    font-weight: var(--weight-semibold);
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* ─── Divider ─────────────────────────────────────────────── */
.ps-divider {
    height: 1px;
    background-color: var(--color-border-light);
    margin: var(--space-1) 0;
}

/* ─── Custom Input Row ────────────────────────────────────── */
.ps-custom-option {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-base);
    color: var(--color-text-secondary);
    cursor: pointer;
    transition: background-color var(--duration-fast) var(--ease-in-out);
    border-top: 1px solid var(--color-border-light);
}

.ps-custom-option:hover,
.ps-custom-option.ps-highlighted {
    background-color: var(--color-bg-tertiary);
    color: var(--color-text-primary);
}

.ps-custom-option svg {
    flex-shrink: 0;
    width: 15px;
    height: 15px;
}

.ps-custom-option strong {
    color: var(--color-text-primary);
    font-weight: var(--weight-medium);
}

/* ─── Empty State ─────────────────────────────────────────── */
.ps-empty {
    padding: var(--space-6) var(--space-4);
    text-align: center;
    color: var(--color-text-tertiary);
    font-size: var(--text-sm);
}

/* ─── Mobile Optimizations ────────────────────────────────── */
@media (max-width: 768px) {
    .ps-dropdown {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: calc(var(--z-modal) + 10);
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
        border-bottom: none;
        max-height: 70vh;
        max-height: 70dvh;
        transform: translateY(100%);
        opacity: 0;
        visibility: hidden;
        /* On close: slide down + fade, then hide visibility at the end */
        transition: opacity var(--duration-fast) var(--ease-out),
                    transform var(--duration-fast) var(--ease-out),
                    visibility 0s linear var(--duration-fast);
        /* Safe area for iPhones with home indicator */
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    .ps-dropdown.ps-visible {
        transform: translateY(0);
        opacity: 1;
        visibility: visible;
        /* On open: visibility flips immediately, then slide up + fade in */
        transition: opacity var(--duration-fast) var(--ease-out),
                    transform var(--duration-fast) var(--ease-out),
                    visibility 0s;
    }

    .ps-options {
        max-height: calc(70vh - 110px);
        max-height: calc(70dvh - 110px);
        min-height: 260px;
    }

    /* Prevent iOS Safari auto-zoom: inputs must be ≥16px */
    .ps-search-input,
    .ps-trigger-input {
        font-size: 16px;
    }

    /* Backdrop overlay on mobile — covers everything including bottom nav */
    .ps-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        z-index: calc(var(--z-modal) + 9);
        background-color: var(--color-overlay);
        opacity: 0;
        transition: opacity var(--duration-normal) var(--ease-out);
    }

    .ps-backdrop.ps-visible {
        display: block;
        opacity: 1;
    }

    /* Mobile drag handle */
    .ps-mobile-handle {
        display: flex;
        justify-content: center;
        padding: var(--space-2) 0 0;
    }

    .ps-mobile-handle::after {
        content: '';
        width: 36px;
        height: 4px;
        border-radius: var(--radius-full);
        background-color: var(--color-border-strong);
    }

    /* Bigger touch targets on mobile */
    /* Bigger touch targets on mobile */
    .ps-option {
        padding: var(--space-3) var(--space-3);
        min-height: 48px;
    }

    .ps-custom-option {
        padding: var(--space-3) var(--space-3);
        padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
        min-height: 48px;
    }
}

@media (min-width: 769px) {
    .ps-backdrop {
        display: none !important;
    }

    .ps-mobile-handle {
        display: none !important;
    }

    /* On desktop, the trigger input is the search — hide the duplicate in dropdown */
    .ps-search-wrap {
        display: none !important;
    }

    /* Add top padding since search wrap is hidden */
    .ps-options {
        padding-top: var(--space-2);
    }

    /* Visual connection: trigger bottom merges with dropdown top */
    .ps-trigger.ps-open {
        border-bottom-left-radius: 0;
        border-bottom-right-radius: 0;
        border-bottom-color: transparent;
        box-shadow: none;
    }

    .ps-dropdown.ps-visible {
        border-top-left-radius: 0;
        border-top-right-radius: 0;
        margin-top: 0;
        top: 100%;
        border-top: none;
        box-shadow: var(--shadow-lg);
    }
}