/*
    Tom Select, skinned onto this project's own field vocabulary.

    The same decision `mint-calendar.css` makes, for the same reason and with
    the same consequence: `resources/3rdparty/` is vendored as-is, but upstream's
    stylesheet is ANOTHER PRODUCT'S LOOK, and matching this app through it would
    mean `!important` on nearly every property -- unlayered third-party CSS beats
    every layer here. So `tom-select.default.min.css` is deliberately NOT
    vendored: only `tom-select.complete.min.js` is, and this file is the whole
    of its appearance.

    What that costs, stated so nobody rediscovers it: upstream's stylesheet
    carries STRUCTURAL rules as well as decorative ones, and a few of them are
    load-bearing rather than taste -- `.ts-wrapper { position: relative }`, the
    absolutely-positioned dropdown, `.ts-hidden-accessible` hiding the real
    `<select>`, and the `!important` reset on `.ts-control > input`, which the
    library's own markup needs because that input lives inside a box styled like
    a field. Those are reproduced below and marked. Everything else is ours.

    Only the SINGLE select is styled, because that is the only kind this project
    creates -- see `SearchableSelect` in common.js. The multi-select, the tag
    plugins and the drag-and-drop plugin all ship in the vendored JS and none of
    them has a look here; giving one a screen means giving it a block in this
    file first.

    The control is `.field-input` in every respect that a broker can see, and
    that is the point: a picker that searches must not read as a different KIND
    of control from the select beside it. /dev/forms shows the two together.
*/
@layer components {

    /* STRUCTURAL (upstream): the dropdown is absolutely positioned against
       this. */
    .ts-wrapper {
        position: relative;
        display: block;
        width: 100%;
    }

    /* Tom Select copies the original <select>'s classes onto its wrapper, so
       the wrapper arrives as `.field-input` -- a bordered, padded, 40px box --
       with `.ts-control`, a second one, inside it: a box in a box, the caret
       inset, the text off-centre. The CONTROL is the field; the wrapper is
       only the positioning context for the dropdown, so it gives all of that
       back. Two classes, so this out-ranks `.field-input` whatever the order. */
    .ts-wrapper.field-input {
        height: auto;
        padding: 0;
        border: 0;
        border-radius: 0;
        background: none;
        box-shadow: none;
    }

    /* And the error border belongs on the box that is drawn. */
    .field-has-error .ts-control {
        border-color: var(--status-error-accent);
    }

    /* STRUCTURAL (upstream): the real <select> stays in the DOM and in the form
       -- it is what WooForm binds to and what posts -- so it is hidden the way
       an accessible name is hidden rather than with `display: none`, which
       would take it out of the form's own validity reporting. Deliberately not
       `.sr-only`: that class is ours and a third-party library renaming its
       hook must not silently un-hide a field. */
    .ts-hidden-accessible {
        position: absolute !important;
        width: 1px !important;
        overflow: hidden !important;
        clip-path: inset(50%) !important;
        white-space: nowrap !important;
        border: 0 !important;
        padding: 0 !important;
    }

    /* `.field-input`, repeated rather than shared, because the element is a
       <div> the library builds and no class of ours is on it (the wrapper
       gets the select's classes; this does not). Keep the two in
       step by hand; /dev/forms renders them side by side so a drift shows. */
    .ts-control {
        box-sizing: border-box;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        width: 100%;
        min-height: 40px;
        padding: 0 var(--spacing-s3);
        /* Room for the caret drawn below, the same gap `select.field-input`
           leaves for `--field-select-arrow`. */
        padding-inline-end: var(--spacing-s8);
        border: 1px solid var(--border-colour);
        border-radius: var(--radius-m);
        background-color: var(--surface-colour);
        color: var(--text-colour-regular);
        font-size: var(--font-size-r);
        cursor: pointer;
        transition: border-color var(--transition-duration) var(--transition-timing),
                    box-shadow var(--transition-duration) var(--transition-timing);
    }

    /* The control is a <div>, so it never matches `:focus-visible` itself --
       Tom Select puts `.focus` on the wrapper instead. Same ring as
       `.field-input:focus-visible`, and it is drawn on keyboard and mouse alike
       here because the control IS a text box once it has focus: hiding the ring
       after a click would leave a typing cursor with nothing around it. */
    .ts-wrapper.focus .ts-control {
        border-color: var(--border-colour-brand);
        box-shadow: var(--focus-ring);
    }

    /* A search box's clear button (`data-search-box`, SearchableSelect),
       just inside the caret, and only once something is chosen. */
    .ts-wrapper .clear-button {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        inset-inline-end: var(--spacing-s8);
        align-items: center;
        padding-inline: var(--spacing-s1);
        color: var(--text-colour-light);
        font-size: var(--font-size-l);
        line-height: 1;
        cursor: pointer;
    }

    .ts-wrapper.has-items .clear-button {
        display: flex;
    }

    .ts-wrapper .clear-button:hover {
        color: var(--text-colour-regular);
    }

    .ts-wrapper.disabled .ts-control {
        background-color: var(--surface-colour-raised);
        color: var(--text-colour-faint);
        cursor: default;
    }

    /* The caret. `--field-select-arrow` itself rather than a third drawing of
       it -- that token is already the data: URI every <select> in the app
       paints, so this control and a native one cannot drift, and nothing new
       goes in docs/theming.md's register of hand-copied colours. A
       background-image rather than the icon partial because this element is
       built by JavaScript and there is no template to include from. */
    .ts-wrapper.single .ts-control::after {
        content: "";
        position: absolute;
        inset-inline-end: var(--spacing-s3);
        top: 50%;
        width: 16px;
        height: 16px;
        margin-top: -8px;
        background-image: var(--field-select-arrow);
        background-repeat: no-repeat;
        background-size: 16px;
        pointer-events: none;
        transition: transform var(--transition-duration) var(--transition-timing);
    }

    /* Open reads as pressed, the same way `.filter-trigger` and `.owner-face`
       do. */
    .ts-wrapper.single.dropdown-active .ts-control::after {
        transform: rotate(180deg);
    }

    /* STRUCTURAL (upstream): the typing cursor. It sits inside a box that is
       already styled as a field, so every inherited input style has to come
       off -- and upstream uses `!important` for it because a host stylesheet is
       exactly what it is defending against. Ours would win on layer order
       alone, but the declarations are kept as upstream wrote them: this is the
       rule most likely to need re-checking against a new version. */
    .ts-control > input {
        flex: 1 1 auto;
        min-width: 7rem;
        max-width: 100%;
        margin: 0 !important;
        padding: 0 !important;
        border: 0 !important;
        background: none !important;
        box-shadow: none !important;
        color: inherit;
        font-family: inherit;
        font-size: inherit;
        line-height: inherit !important;
    }

    .ts-control > input:focus {
        outline: none !important;
    }

    .ts-control > input::placeholder {
        color: var(--text-colour-faint);
    }

    /* The chosen option, shown when the box is not being typed in. */
    .ts-control > .item {
        color: var(--text-colour-regular);
    }

    /* STRUCTURAL (upstream): `left`/`top`/`width` are what put the list under
       the control; the rest is ours and is `.menu-list`'s look without being
       that class -- RecordMenus owns `.menu-list`'s positioning in JavaScript,
       and sharing a class whose geometry another module writes is how two
       components come to share a bug. The same reasoning `.choice-list`
       carries. */
    .ts-dropdown {
        position: absolute;
        top: 100%;
        inset-inline-start: 0;
        width: 100%;
        margin-block-start: var(--spacing-s1);
        padding: var(--spacing-s1);
        border: 1px solid var(--border-colour);
        border-radius: var(--radius-m);
        background-color: var(--surface-colour);
        box-shadow: var(--elevation-3);
        color: var(--text-colour-regular);
        font-size: var(--font-size-r);
        /* Above the sidebar (40) and the header (50), below the toasts (60) --
           the values `.menu-list` reasons out, copied rather than re-picked.
           One higher than `.flyout-popover`'s 55, because every one of these in
           the product today is INSIDE a flyout. */
        z-index: 56;
    }

    /* STRUCTURAL (upstream): the scrolling box. Capped for the same reason
       `.label-picker-list` is -- a house's contact list has no ceiling, and a
       dropdown taller than the viewport puts its last option somewhere
       unreachable. */
    .ts-dropdown-content {
        max-height: 240px;
        overflow-y: auto;
        scroll-behavior: smooth;
    }

    .ts-dropdown .option {
        padding: var(--spacing-s2) var(--spacing-s3);
        border-radius: var(--radius-s);
        cursor: pointer;
    }

    /* `.active` is the option under the keyboard, `.selected` is the one
       already chosen. They are different facts and the keyboard's is the one
       that has to be obvious, so it takes the fill and the other takes weight
       only. */
    .ts-dropdown .option.selected {
        font-weight: var(--font-weight-semibold);
    }

    .ts-dropdown .active {
        background-color: var(--color-ui-100);
    }

    /* The second line a searched option carries -- job title and company for
       a person, type and website for a company -- so two people of one name
       are told apart. Only remote pickers render it (`SearchableSelect.remote`
       in common.js); an in-page option is its name alone. */
    .ts-dropdown .ts-option-detail {
        display: block;
        font-size: var(--font-size-s);
        font-weight: var(--font-weight-regular);
        color: var(--text-colour-light);
    }

    /* What the typed text matched, inside the option. Tom Select wraps it for
       us; upstream's own colour is a blue wash that reads as a link here, and a
       link inside an option is a second thing to click. Weight rather than a
       fill, because the match sits inside an option that may already be filled
       by `.active` and two backgrounds one inside the other is mush. */
    .ts-dropdown .highlight {
        font-weight: var(--font-weight-semibold);
        color: var(--text-colour-brand);
    }

    /* Said out loud rather than shown as an empty box -- the same rule
       `.label-picker-empty` follows, and for the same reason: a filter that
       matches nothing looks identical to a house with no contacts. */
    .ts-dropdown .no-results {
        padding: var(--spacing-s2) var(--spacing-s3);
        color: var(--text-colour-light);
    }

    .ts-dropdown .optgroup-header {
        padding: var(--spacing-s2) var(--spacing-s3) var(--spacing-s1);
        color: var(--text-colour-light);
        font-size: var(--font-size-2xs);
        font-weight: var(--font-weight-semibold);
        letter-spacing: 0.04em;
        text-transform: uppercase;
    }

    .ts-dropdown .optgroup + .optgroup {
        margin-block-start: var(--spacing-s1);
        border-block-start: 1px solid var(--border-colour);
        padding-block-start: var(--spacing-s1);
    }

}
