/**
 * Bottom Sheet Component
 * Mobile-friendly selection UI with search and add-new capability
 * Uses native <dialog> element for accessibility
 */

/* Only apply bottom sheet styles on touch devices */
@media (pointer: coarse) {
    /* Hide the desktop select elements on touch devices */
    .bottomsheet-field select.form-select {
        display: none !important;
    }

    /* Show the mobile trigger input on touch devices */
    .bottomsheet-field .bottomsheet-trigger {
        display: block !important;
    }
}

/* Hide mobile trigger by default (desktop) */
.bottomsheet-trigger {
    display: none;
    cursor: pointer;
    background-color: #fff;
    caret-color: transparent;
}

.bottomsheet-trigger:focus {
    border-color: #F16523;
    box-shadow: 0 0 0 0.2rem rgba(241, 101, 35, 0.25);
}

/* Visual indicator when field has a value */
.bottomsheet-trigger--has-value {
    border-color: #27AE60;
    background-color: #f0fff4;
}

/* The dialog element */
.bottomsheet-dialog {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: none;
    background: transparent;
    overflow: hidden;
}

.bottomsheet-dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    transition: opacity 0.2s ease-out;
}

.bottomsheet-dialog[open]::backdrop {
    opacity: 1;
}

/* Sheet container that slides up */
.bottomsheet-container {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 85vh; /* fallback for older iOS */
    max-height: 85dvh;
    background: #fff;
    border-radius: 16px 16px 0 0;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
}

.bottomsheet-dialog[open] .bottomsheet-container {
    transform: translateY(0);
}

/* When keyboard is open, shrink to fit above it */
.bottomsheet-dialog.bottomsheet-keyboard-open .bottomsheet-container {
    max-height: var(--bottomsheet-vh, 85dvh);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1),
                max-height 0.15s ease-out;
}

/* Drag handle */
.bottomsheet-handle {
    display: flex;
    justify-content: center;
    padding: 12px;
    cursor: grab;
    touch-action: none;
}

.bottomsheet-handle::before {
    content: '';
    width: 40px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
}

/* Header with title and close button */
.bottomsheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px 12px;
    border-bottom: 1px solid #e5e7eb;
}

.bottomsheet-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: #111827;
    margin: 0;
}

.bottomsheet-close {
    width: 32px;
    height: 32px;
    border: none;
    background: #f3f4f6;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #6b7280;
    font-size: 1.25rem;
    line-height: 1;
    transition: background-color 0.15s;
}

.bottomsheet-close:hover,
.bottomsheet-close:focus {
    background: #e5e7eb;
    outline: none;
}

/* Search input */
.bottomsheet-search {
    padding: 12px 16px;
}

.bottomsheet-search-input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.bottomsheet-search-input:focus {
    border-color: #F16523;
    box-shadow: 0 0 0 3px rgba(241, 101, 35, 0.15);
}

.bottomsheet-search-input::placeholder {
    color: #9ca3af;
}

/* Options list */
.bottomsheet-options {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-bottom: env(safe-area-inset-bottom, 16px);
}

.bottomsheet-section {
    padding: 8px 0;
}

.bottomsheet-section-title {
    font-size: 0.75rem;
    font-weight: 600;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 8px 16px 4px;
}

/* Individual option */
.bottomsheet-option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 48px;
    padding: 12px 16px;
    cursor: pointer;
    transition: background-color 0.1s;
    border: none;
    background: transparent;
    width: 100%;
    text-align: left;
    font-size: 1rem;
    color: #111827;
}

.bottomsheet-option:hover,
.bottomsheet-option:focus {
    background-color: #f9fafb;
    outline: none;
}

.bottomsheet-option:active {
    background-color: #f3f4f6;
}

.bottomsheet-option--selected {
    color: #F16523;
    font-weight: 500;
}

.bottomsheet-option--selected::after {
    content: '\2713';
    color: #F16523;
    font-weight: bold;
}

/* Add new option (shown inline as a normal selectable item) */

/* N/A option for optional fields */
.bottomsheet-option--na {
    color: #6b7280;
    font-style: italic;
}

/* Empty state */
.bottomsheet-empty {
    padding: 24px 16px;
    text-align: center;
    color: #6b7280;
}

/* Unavailable option (pre-filled value no longer in list) */
.bottomsheet-option--unavailable {
    color: #9ca3af;
}

.bottomsheet-option--unavailable::after {
    content: '(not available)';
    font-size: 0.75rem;
    margin-left: 8px;
    font-weight: normal;
}

