/*
 * Framework SmartPicker Component
 * Project-agnostic styles using standard base CSS variables with safe fallbacks.
 */

.smart-picker-container {
    --smp-surface: rgb(var(--bg-color, 255, 255, 255));
    --smp-surface-muted: rgba(var(--text-color, 15, 23, 42), 0.04);
    --smp-ink: rgb(var(--text-color, 15, 23, 42));
    --smp-muted: rgba(var(--text-color, 15, 23, 42), 0.62);
    --smp-border: rgba(var(--text-color, 15, 23, 42), 0.16);
    --smp-border-soft: rgba(var(--text-color, 15, 23, 42), 0.09);
    --smp-primary: rgb(var(--primary-color, 37, 99, 235));
    --smp-primary-soft: rgba(var(--primary-color, 37, 99, 235), 0.1);
    --smp-primary-line: rgba(var(--primary-color, 37, 99, 235), 0.35);
    --smp-danger: rgb(var(--primary-red, 220, 38, 38));
    --smp-danger-soft: rgba(var(--primary-red, 220, 38, 38), 0.1);
    --smp-radius: 0.75em;
    --smp-radius-sm: 0.5em;
    --smp-shadow-pop: 0 12px 32px rgba(15, 23, 42, 0.14), 0 2px 8px rgba(15, 23, 42, 0.08);

    display: flex;
    align-items: flex-start;
    gap: 0.5em;
    width: 100%;
    min-width: 0;
}

.smart-picker-container.is-disabled,
.smart-picker-container.is-readonly {
    opacity: 0.72;
    pointer-events: none;
}

.smart-picker {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.35em;
    flex: 1 1 auto;
    min-width: 0;
}

.smart-picker__field[hidden],
.smart-picker__selected[hidden],
.smart-picker__clear[hidden],
.smart-picker__popover[hidden],
.smart-picker__tags[hidden],
.smart-picker__filters[hidden],
.smart-picker__footer[hidden] {
    display: none !important;
}

/* ------------------------------------------------------------- input row */

.smart-picker__field {
    display: flex;
    align-items: center;
    gap: 0.5em;
    min-height: 2.7em;
    padding: 0.35em 0.6em;
    border-radius: var(--smp-radius-sm);
    background: var(--smp-surface);
    outline: 1px solid var(--smp-border);
    transition: outline-color 0.16s ease, box-shadow 0.16s ease, background-color 0.16s ease;
}

.smart-picker__field:hover {
    outline-color: var(--smp-primary-line);
}

.smart-picker.is-open .smart-picker__field,
.smart-picker__field:focus-within {
    outline: 1.5px solid var(--smp-primary);
    background: var(--smp-surface);
    box-shadow: 0 0 0 0.18em var(--smp-primary-soft);
}

.smart-picker.is-invalid .smart-picker__field {
    outline-color: var(--smp-danger);
}

.smart-picker__icon {
    display: flex;
    flex: 0 0 auto;
    color: var(--smp-muted);
}

.smart-picker.is-open .smart-picker__icon {
    color: var(--smp-primary);
}

.smart-picker__input {
    flex: 1 1 auto;
    min-width: 0;
    padding: 0.2em 0;
    border: 0;
    background: transparent;
    color: var(--smp-ink);
    font: inherit;
    font-size: 0.9em;
    line-height: 1.35;
}

.smart-picker__input:focus {
    outline: none;
}

.smart-picker__input::placeholder {
    color: var(--smp-muted);
    opacity: 0.85;
}

.smart-picker__clear {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    width: 1.6em;
    height: 1.6em;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--smp-surface-muted);
    color: var(--smp-muted);
    font-size: 0.85em;
    line-height: 1;
    cursor: pointer;
}

.smart-picker__clear:hover,
.smart-picker__clear:focus-visible {
    background: var(--smp-danger-soft);
    color: var(--smp-danger);
}

.smart-picker__clear:focus-visible {
    outline: 2px solid var(--smp-danger);
    outline-offset: 1px;
}

/* ----------------------------------------------------- single selected card */

.smart-picker__selected {
    display: flex;
    align-items: center;
    gap: 0.55em;
    min-width: 0;
    padding: 0.45em 0.55em 0.45em 0.65em;
    border-radius: var(--smp-radius-sm);
    border: 1px solid var(--smp-primary-line);
    background: linear-gradient(90deg, var(--smp-primary-soft), var(--smp-surface) 70%);
}

.smart-picker__selected-icon {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    width: 2em;
    height: 2em;
    border-radius: 0.5em;
    background: var(--smp-surface);
    color: var(--smp-primary);
    border: 1px solid var(--smp-primary-line);
}

.smart-picker__selected-body {
    display: flex;
    flex-direction: column;
    gap: 0.05em;
    min-width: 0;
    flex: 1 1 auto;
}

.smart-picker__selected-name {
    font-size: 0.88em;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    color: var(--smp-ink);
    overflow-wrap: anywhere;
}

.smart-picker__selected-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35em;
    font-size: 0.72em;
    line-height: 1.3;
    color: var(--smp-muted);
}

.smart-picker__selected-meta code {
    padding: 0.05em 0.3em;
    border-radius: 0.28em;
    background: var(--smp-surface);
    border: 1px solid var(--smp-border-soft);
    font-size: 0.95em;
    color: var(--smp-ink);
}

.smart-picker__change {
    flex: 0 0 auto;
    padding: 0.3em 0.55em;
    border: 1px solid var(--smp-border);
    border-radius: 0.4em;
    background: var(--smp-surface);
    color: var(--smp-ink);
    font-size: 0.7em;
    font-weight: var(--semi-bold, 600);
    cursor: pointer;
}

.smart-picker__change:hover,
.smart-picker__change:focus-visible {
    border-color: var(--smp-primary-line);
    background: var(--smp-primary-soft);
    color: var(--smp-primary);
}

.smart-picker__change:focus-visible {
    outline: 2px solid var(--smp-primary);
    outline-offset: 1px;
}

/* ----------------------------------------------------- multi-select tags */

.smart-picker__tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35em;
    padding-top: 0.1em;
}

.smart-picker__tag {
    display: inline-flex;
    align-items: center;
    gap: 0.35em;
    max-width: 100%;
    padding: 0.22em 0.45em 0.22em 0.55em;
    border: 1px solid var(--smp-primary-line);
    border-radius: 2em;
    background: var(--smp-primary-soft);
    color: var(--smp-ink);
    font-size: 0.78em;
    line-height: 1.3;
}

.smart-picker__tag-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.smart-picker__tag-code {
    padding: 0.02em 0.28em;
    border-radius: 0.25em;
    background: var(--smp-surface);
    font-size: 0.9em;
    color: var(--smp-muted);
}

.smart-picker__tag-remove {
    display: grid;
    place-items: center;
    width: 1.35em;
    height: 1.35em;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--smp-muted);
    font-size: 0.95em;
    line-height: 1;
    cursor: pointer;
}

.smart-picker__tag-remove:hover,
.smart-picker__tag-remove:focus-visible {
    background: var(--smp-danger-soft);
    color: var(--smp-danger);
}

/* -------------------------------------------------------- picker popover */

.smart-picker__popover {
    position: fixed;
    z-index: 950;
    display: flex;
    flex-direction: column;
    max-height: min(26em, 70vh);
    overflow: hidden;
    border: 1px solid var(--smp-border);
    border-radius: var(--smp-radius);
    background: var(--smp-surface);
    box-shadow: var(--smp-shadow-pop);
}

.smart-picker__filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3em;
    padding: 0.55em 0.6em 0.5em;
    border-bottom: 1px solid var(--smp-border-soft);
    background: var(--smp-surface);
}

.smart-picker__chip {
    display: inline-flex;
    align-items: center;
    gap: 0.3em;
    padding: 0.28em 0.55em;
    border: 1px solid var(--smp-border);
    border-radius: 2em;
    background: var(--smp-surface);
    color: var(--smp-muted);
    font-size: 0.7em;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    cursor: pointer;
    transition: border-color 0.16s ease, background-color 0.16s ease, color 0.16s ease;
}

.smart-picker__chip:hover,
.smart-picker__chip:focus-visible {
    border-color: var(--smp-primary-line);
    color: var(--smp-ink);
}

.smart-picker__chip:focus-visible {
    outline: 2px solid var(--smp-primary);
    outline-offset: 1px;
}

.smart-picker__chip.is-active {
    border-color: var(--smp-primary);
    background: var(--smp-primary-soft);
    color: var(--smp-primary);
}

.smart-picker__body {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0.35em;
    scrollbar-width: thin;
}

.smart-picker__group {
    display: flex;
    align-items: center;
    gap: 0.4em;
    padding: 0.4em 0.4em 0.2em;
    font-size: 0.66em;
    font-weight: var(--bold, 700);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--smp-muted);
}

.smart-picker__group::after {
    content: "";
    flex: 1 1 auto;
    border-top: 1px solid var(--smp-border-soft);
}

/* -------------------------------------------------------- picker options */

.smart-picker__option {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.3em 0.7em;
    width: 100%;
    padding: 0.5em 0.55em;
    border: 1px solid transparent;
    border-radius: 0.55em;
    background: transparent;
    color: var(--smp-ink);
    text-align: left;
    font: inherit;
    cursor: pointer;
}

.smart-picker__option + .smart-picker__option {
    margin-top: 0.1em;
}

.smart-picker__option.is-active,
.smart-picker__option:hover {
    background: var(--smp-primary-soft);
    border-color: var(--smp-primary-line);
}

/* Keyboard cursor: DOM focus never leaves the combobox, so `.is-active` (driven
   by arrow keys + aria-activedescendant on the input) is the single visible
   keyboard indicator — options are tabindex="-1" and are never focused. */
.smart-picker__option.is-active {
    box-shadow: inset 2px 0 0 var(--smp-primary);
}

.smart-picker__option.is-selected {
    background: rgba(var(--primary-color, 37, 99, 235), 0.06);
}

.smart-picker__option[aria-disabled="true"] {
    cursor: not-allowed;
    background: var(--smp-surface-muted);
    border-color: transparent;
}

.smart-picker__option[aria-disabled="true"] .smart-picker__option-name {
    color: var(--smp-muted);
}

.smart-picker__option-main {
    display: flex;
    flex-direction: column;
    gap: 0.1em;
    min-width: 0;
}

.smart-picker__option-name {
    font-size: 0.86em;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    overflow-wrap: anywhere;
}

.smart-picker__option-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.3em;
    font-size: 0.7em;
    line-height: 1.3;
    color: var(--smp-muted);
}

.smart-picker__option-meta code {
    padding: 0.02em 0.28em;
    border-radius: 0.25em;
    background: var(--smp-surface-muted);
    font-size: 0.98em;
    color: var(--smp-ink);
}

.smart-picker__option-dot {
    opacity: 0.5;
}

.smart-picker__option-side {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.2em;
    text-align: right;
}

.smart-picker__option-check {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.35em;
    height: 1.35em;
    border-radius: 0.3em;
    border: 1px solid var(--smp-border);
    background: var(--smp-surface);
    color: transparent;
    font-size: 0.75em;
}

.smart-picker__option.is-selected .smart-picker__option-check {
    border-color: var(--smp-primary);
    background: var(--smp-primary);
    color: #fff;
}

.smart-picker__option-reason {
    grid-column: 1 / -1;
    font-size: 0.7em;
    line-height: 1.35;
    color: var(--smp-danger);
}

/* ----------------------------------------------------------- status chip */

.smart-picker__status {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    padding: 0.2em 0.6em 0.2em 0.5em;
    border: 1px solid rgba(var(--tone, var(--dark-gray, 100, 116, 139)), 0.34);
    border-radius: 2em;
    background: rgba(var(--tone, var(--dark-gray, 100, 116, 139)), 0.1);
    color: var(--smp-ink);
    font-size: 0.66em;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    white-space: nowrap;
}

.smart-picker__status.is-safe,
.smart-picker__status.is-success {
    --tone: var(--accent-light-green, 16, 185, 129);
}

.smart-picker__status.is-low,
.smart-picker__status.is-warning {
    --tone: var(--primary-orange, 245, 158, 11);
}

.smart-picker__status.is-empty,
.smart-picker__status.is-danger {
    --tone: var(--primary-red, 239, 68, 68);
}

.smart-picker__status.is-info {
    --tone: var(--primary-color, 37, 99, 235);
}

.smart-picker__status.is-inactive {
    --tone: var(--dark-gray, 100, 116, 139);
}

.smart-picker__status-meter {
    display: inline-flex;
    align-items: flex-end;
    gap: 0.15em;
    flex: 0 0 auto;
    height: 1.15em;
}

.smart-picker__status-meter i {
    width: 0.22em;
    border-radius: 99px;
    background: rgba(var(--tone, var(--dark-gray, 100, 116, 139)), 0.24);
}

.smart-picker__status-meter i:nth-child(1) { height: 0.45em; }
.smart-picker__status-meter i:nth-child(2) { height: 0.8em; }
.smart-picker__status-meter i:nth-child(3) { height: 1.15em; }

.smart-picker__status-meter i.is-on {
    background: rgb(var(--tone, var(--dark-gray, 100, 116, 139)));
}

.smart-picker__status-meter i.is-off {
    background: transparent;
    box-shadow: inset 0 0 0 1px rgba(var(--tone, var(--dark-gray, 100, 116, 139)), 0.55);
}

/* ------------------------------------------- loading / empty / error / footer */

.smart-picker__notice {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3em;
    padding: 1.4em 1em;
    text-align: center;
}

.smart-picker__notice-title {
    font-size: 0.84em;
    font-weight: var(--semi-bold, 600);
    color: var(--smp-ink);
}

.smart-picker__notice-text {
    max-width: 34ch;
    font-size: 0.74em;
    line-height: 1.45;
    color: var(--smp-muted);
}

.smart-picker__notice .button {
    margin-top: 0.3em;
}

.smart-picker__skeleton {
    display: flex;
    flex-direction: column;
    gap: 0.35em;
    padding: 0.4em;
}

.smart-picker__skeleton-row {
    height: 2.4em;
    border-radius: 0.5em;
    background: linear-gradient(
        90deg,
        var(--smp-surface-muted) 25%,
        rgba(var(--text-color, 15, 23, 42), 0.04) 37%,
        var(--smp-surface-muted) 63%
    );
    background-size: 400% 100%;
    animation: smart-picker-skeleton 1.2s ease-in-out infinite;
}

@keyframes smart-picker-skeleton {
    0% { background-position: 100% 50%; }
    100% { background-position: 0 50%; }
}

.smart-picker__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.4em;
    padding: 0.45em 0.65em;
    border-top: 1px solid var(--smp-border-soft);
    background: var(--smp-surface-muted);
    font-size: 0.66em;
    color: var(--smp-muted);
}

/* Keyboard hints share one style: the footer row and the "select all matching"
   action's own Ctrl+A hint (its keyboard path, since Tab never reaches it). */
.smart-picker__footer kbd,
.smart-picker__bulk kbd {
    display: inline-block;
    min-width: 1.4em;
    padding: 0.05em 0.3em;
    border: 1px solid var(--smp-border);
    border-bottom-width: 2px;
    border-radius: 0.3em;
    background: var(--smp-surface);
    font-family: inherit;
    font-size: 0.95em;
    text-align: center;
}

/* ------------------------------------------- selected icon (sprite) badges */

.smart-picker__selected-icon-slot {
    display: contents;
}

/* The configured badge is a sprite <svg>; the fallback keeps the legacy span
   wrapper around the inline box glyph. Both share .smart-picker__selected-icon. */
svg.smart-picker__selected-icon {
    display: block;
    box-sizing: border-box;
    padding: 0.34em;
}

.smart-picker__selected.is-icon-only {
    gap: 0.35em;
    padding: 0.35em 0.45em;
}

.smart-picker__selected.is-icon-only .smart-picker__selected-body {
    display: none;
}

.smart-picker__tag-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    color: var(--smp-primary);
}

.smart-picker__tag-icon .smart-picker__selected-icon {
    display: block;
    width: 1.15em;
    height: 1.15em;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: inherit;
}

.smart-picker__tag.is-icon-only {
    padding: 0.22em 0.35em;
}

.smart-picker__tag.is-icon-only .smart-picker__tag-label,
.smart-picker__tag.is-icon-only .smart-picker__tag-code {
    display: none;
}

/* --------------------------------------------------------- undo affordance */

.smart-picker__undo {
    display: flex;
    align-items: center;
    gap: 0.5em;
    padding: 0.35em 0.5em;
    border: 1px solid var(--smp-border-soft);
    border-radius: var(--smp-radius-sm);
    background: var(--smp-surface-muted);
    color: var(--smp-muted);
    font-size: 0.72em;
    line-height: 1.35;
}

.smart-picker__undo[hidden] {
    display: none !important;
}

.smart-picker__undo-text {
    flex: 1 1 auto;
    min-width: 0;
}

.smart-picker__undo-btn,
.smart-picker__undo-dismiss {
    flex: 0 0 auto;
    padding: 0.2em 0.5em;
    border: 1px solid var(--smp-border);
    border-radius: var(--smp-radius-sm);
    background: var(--smp-surface);
    color: var(--smp-ink);
    font: inherit;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    cursor: pointer;
}

.smart-picker__undo-btn:hover,
.smart-picker__undo-dismiss:hover {
    border-color: var(--smp-primary-line);
    background: var(--smp-primary-soft);
    color: var(--smp-primary);
}

.smart-picker__undo-btn:focus-visible,
.smart-picker__undo-dismiss:focus-visible {
    outline: 2px solid var(--smp-primary);
    outline-offset: 1px;
}

/* ---------------------------------------------- "select all matching" action */

.smart-picker__bulk {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5em;
    padding: 0.45em 0.65em;
    border-top: 1px solid var(--smp-border-soft);
    background: var(--smp-surface);
    font-size: 0.74em;
    line-height: 1.35;
}

.smart-picker__bulk[hidden] {
    display: none !important;
}

.smart-picker__select-all {
    padding: 0.3em 0.7em;
    border: 1px solid var(--smp-primary-line);
    border-radius: 2em;
    background: var(--smp-primary-soft);
    color: var(--smp-primary);
    font: inherit;
    font-weight: var(--semi-bold, 600);
    line-height: 1.3;
    cursor: pointer;
}

.smart-picker__select-all:hover,
.smart-picker__select-all:focus-visible {
    border-color: var(--smp-primary);
}

.smart-picker__select-all:focus-visible {
    outline: 2px solid var(--smp-primary);
    outline-offset: 1px;
}

.smart-picker__select-all[disabled] {
    opacity: 0.72;
    cursor: progress;
}

/* ------------------------------------------------------------ misc helpers */

.smart-picker__sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

@media (prefers-reduced-motion: reduce) {
    .smart-picker__field,
    .smart-picker__chip,
    .smart-picker__option,
    .smart-picker__skeleton-row,
    .smart-picker__undo-btn,
    .smart-picker__select-all {
        transition: none;
        animation: none;
    }
}
