/* =============================================================================
   Warp form fields
   A field is a label, a control and a message line for help and errors. The
   label is always visible; a placeholder never replaces it. Heights follow
   --warp-control-height, the density token.
   ============================================================================= */

.warp-field {
    display: grid;
    /* Alone: about the label width, wider only for a long word. */
    grid-template-columns: minmax(min-content, var(--warp-field-label-width, 140px)) minmax(0, 1fr);
    column-gap: var(--warp-spacing-3);
    margin-bottom: var(--warp-field-gap);

    /* The first line beside the control; a long label wraps with normal spacing. */
    & > .warp-field-label {
        line-height: var(--warp-line-height-normal);
        padding-block: calc((var(--warp-control-height) - 1lh) / 2);
        /* Beside the control and its message, so that a long label does not push the message down. */
        grid-row: span 2;
    }

    & > .warp-field-control,
    & > .warp-field-message {
        grid-column: 2;
    }

    &[hidden] {
        display: none;
    }
}

/*
   The fields of a group on a record — its panel, a section — share one
   label column, as wide as the group's longest label up to
   --warp-field-label-max, and never narrower than its longest word. A
   wrapper inside the group with fields of its own takes part. The panel's
   group is a div inside its fieldset, which is not a grid in every browser.
   Margins do not collapse in a grid: a heading after a field, a divider or
   the title keeps the gap it has in normal flow.
   ------------------------------------------------------------------------- */
.warp-record-layout :is(.warp-record-group, section):has(> .warp-field:not(.warp-field--above), > * > .warp-field:not(.warp-field--above)) {
    display: grid;
    grid-template-columns: fit-content(var(--warp-field-label-max, 180px)) minmax(0, 1fr);
    column-gap: var(--warp-spacing-3);
    align-content: start;

    & > * {
        grid-column: 1 / -1;
    }

    & > :is(a, button, .warp-btn) {
        justify-self: start;
    }

    & > :not(.warp-field):has(> .warp-field:not(.warp-field--above)) {
        display: grid;
        grid-template-columns: subgrid;

        & > * {
            grid-column: 1 / -1;
        }

        /* A wrapper hidden by warp--shown-when */
        &[hidden] {
            display: none;
        }

        /* A wrapper shown only in view mode, or only while editing, as in _records.css */
        body.warp-editing &.warp-record-view-only,
        body:not(.warp-editing) &.warp-record-edit-only {
            display: none;
        }
    }

    & > .warp-field:not(.warp-field--above),
    & > * > .warp-field:not(.warp-field--above) {
        grid-column: 1 / -1;
        grid-template-columns: subgrid;
        column-gap: inherit;
    }

    & > :is(.warp-field, :has(> .warp-field:last-child)) + .warp-record-heading {
        margin-top: max(0px, calc(var(--warp-spacing-5) - var(--warp-field-gap)));
    }

    & > :is(.warp-record-divider, .warp-record-title) + .warp-record-heading {
        margin-top: max(0px, calc(var(--warp-spacing-5) - var(--warp-spacing-4)));
    }

    & > :is(.warp-field, :has(> .warp-field:last-child)) + .warp-record-divider {
        margin-top: max(0px, calc(var(--warp-spacing-4) - var(--warp-field-gap)));
    }
}

/* Label above the control: narrow places, modals, the login page */
.warp-field--above {
    grid-template-columns: minmax(0, 1fr);

    & > .warp-field-label {
        line-height: var(--warp-line-height-normal);
        margin-bottom: var(--warp-spacing-0-5);
        grid-row: auto;
    }

    & > .warp-field-control,
    & > .warp-field-message {
        grid-column: 1;
    }
}

.warp-field-label {
    /* Wraps at spaces only; the column is never narrower than the longest word. */
    overflow-wrap: normal;
    hyphens: manual;
    font-size: var(--warp-font-size-sm);
    font-weight: var(--warp-font-weight-medium);
    color: var(--warp-color-gray-700);
}

.warp-field-required {
    color: var(--warp-color-danger-700);
    margin-left: var(--warp-spacing-0-5);
}

.warp-field-message {
    font-size: var(--warp-font-size-xs);
    color: var(--warp-color-gray-500);

    &:not(:empty) {
        padding-top: var(--warp-spacing-0-5);
    }
}

.warp-field-error {
    display: flex;
    align-items: center;
    gap: var(--warp-spacing-1);
    color: var(--warp-color-danger-700);

    &[hidden] {
        display: none;
    }
}

/* A warning at a field that does not stop the save, as an invalid VAT number */
.warp-field-warning {
    color: var(--warp-color-warning-700);
}

/*
   The value, in view mode: the same border and inner margin as a control,
   so that the text stays where it is between view and edit mode.
*/
.warp-field-value {
    display: flex;
    align-items: center;
    min-height: var(--warp-control-height);
    padding-inline: var(--warp-spacing-2);
    color: var(--warp-color-gray-900);
    /* Coloured in a record's view mode; transparent so that nothing moves. */
    border: var(--warp-border-width-default) solid transparent;
    border-radius: var(--warp-radius-default) var(--warp-radius-default) 0 0;
}

.warp-field-empty {
    color: var(--warp-color-gray-500);
}

/* A list of values, one per line */
.warp-field-values {
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    margin: 0;
    padding-block: 0;
    list-style: none;
}

/* =============================================================================
   Controls
   ============================================================================= */

.warp-input,
.warp-select,
.warp-textarea {
    width: 100%;
    max-width: var(--warp-control-width, 16rem);
    height: var(--warp-control-height);
    padding: 0 var(--warp-spacing-2);
    font-family: var(--warp-font-family-sans);
    font-size: var(--warp-font-size-sm);
    color: var(--warp-color-gray-900);
    background-color: var(--warp-color-white);
    border: 1px solid var(--warp-color-control-border);
    border-radius: var(--warp-radius-default);
    transition: border-color var(--warp-transition-fast), box-shadow var(--warp-transition-fast);

    &:hover {
        border-color: var(--warp-color-gray-600);
    }

    &:focus {
        outline: none;
        border-color: var(--warp-color-primary-600);
        box-shadow: 0 0 0 3px color-mix(in oklch, var(--warp-color-primary-600) 25%, transparent);
    }

    &:disabled,
    &[readonly] {
        background-color: var(--warp-color-gray-100);
        color: var(--warp-color-gray-600);
        cursor: not-allowed;
    }

    .warp-field--error & {
        border-color: var(--warp-color-danger-500);

        &:focus {
            box-shadow: 0 0 0 3px color-mix(in oklch, var(--warp-color-danger-500) 25%, transparent);
        }
    }
}

/* Widths, the same in every form */
.warp-control--sm { --warp-control-width: 8rem; }
.warp-control--md { --warp-control-width: 16rem; }
.warp-control--lg { --warp-control-width: 28rem; }
.warp-control--full { --warp-control-width: 100%; }

/* A unit after a field, such as an amount's currency or a percent sign */
.warp-input-unit {
    display: flex;
    align-items: center;
    gap: var(--warp-spacing-2);
}

.warp-input-unit-text {
    font-size: var(--warp-font-size-sm);
    color: var(--warp-color-gray-600);
    white-space: nowrap;
}

.warp-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%23676767' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    /* Optically centred: a chevron's weight is at its top, so it sits a little below the middle */
    background-position: right var(--warp-spacing-2) top calc(50% + 0.0625rem);
    background-repeat: no-repeat;
    background-size: 1.25em 1.25em;
    padding-right: var(--warp-spacing-8);
}

.warp-textarea {
    height: auto;
    min-height: calc(var(--warp-control-height) * 3);
    padding: var(--warp-spacing-1) var(--warp-spacing-2);
    resize: vertical;
}

/* A translated text: an input per language, each followed by the language's name when there are several */
.warp-translated {
    display: flex;
    flex-direction: column;
    gap: var(--warp-spacing-1);
}

.warp-translated-row {
    display: flex;
    /* On the first line of the text, also for a textarea. */
    align-items: baseline;
    gap: var(--warp-spacing-2);

    &[hidden] {
        display: none;
    }
}

/* One language at a time, with Warp:LanguageSwitch: the switch names it. */
.warp-translated[data-single] .warp-translated-lang {
    display: none;
}

/* One width for every language's name, so that the fields end in line */
.warp-translated-lang {
    flex: none;
    width: 6rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--warp-font-size-xs);
    color: var(--warp-color-gray-600);
}

/* The language a form's translated texts are edited in: a segmented control */
.warp-language-switch {
    display: inline-flex;
    margin-bottom: var(--warp-spacing-3);
    border: 1px solid var(--warp-color-control-border);
    border-radius: var(--warp-radius-default);
    overflow: hidden;

    &[hidden] {
        display: none;
    }
}

.warp-language-switch-option {
    display: inline-flex;
    align-items: center;
    gap: var(--warp-spacing-1);
    padding: var(--warp-spacing-1) var(--warp-spacing-3);
    border: 0;
    background: white;
    color: var(--warp-color-gray-700);
    font: inherit;
    font-size: var(--warp-font-size-sm);
    cursor: pointer;

    & + & {
        border-left: 1px solid var(--warp-color-control-border);
    }

    &:hover {
        background: var(--warp-color-gray-50);
    }

    &[aria-pressed="true"] {
        background: var(--warp-color-brand);
        color: white;
    }

    &:focus-visible {
        outline: 2px solid var(--warp-color-primary-500);
        outline-offset: -2px;
    }
}

.warp-language-switch-missing {
    width: 6px;
    height: 6px;
    border-radius: var(--warp-radius-full);
    background: var(--warp-color-warning-500);

    &[hidden] {
        display: none;
    }
}

/* Checkboxes or radio buttons, one per line; data-depth indents a tree */
.warp-choice-list {
    display: flex;
    flex-direction: column;
    padding-block: var(--warp-spacing-1);
}

/* A long list of checkboxes with a filter (warp--choice-filter): in columns that fill the field */
.warp-choice-filter {
    margin-block-end: var(--warp-spacing-2);
}

.warp-choice-list--columns {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
    column-gap: var(--warp-spacing-4);

    & > .warp-choice-item[hidden] {
        display: none;
    }
}

/* In view mode only the checked ones, without the filter. */
.warp-record-fields:disabled .warp-choice-filtered {
    & > .warp-choice-filter {
        display: none;
    }

    & .warp-choice-item:not(:has(input:checked)) {
        display: none;
    }
}

.warp-choice-item {
    display: flex;
    align-items: center;
    gap: var(--warp-spacing-2);

    & .warp-checkbox {
        min-height: calc(var(--warp-control-height) * 0.8);
    }

    &[data-depth="1"] { padding-left: var(--warp-spacing-6); }
    &[data-depth="2"] { padding-left: calc(var(--warp-spacing-6) * 2); }
    &[data-depth="3"] { padding-left: calc(var(--warp-spacing-6) * 3); }
    &[data-depth="4"] { padding-left: calc(var(--warp-spacing-6) * 4); }
}

.warp-choice-label {
    cursor: pointer;
}

/* A checkbox with its text and an explanation under: Form\CheckboxGroupType */
.warp-choice-item--described {
    align-items: flex-start;
    padding-block: var(--warp-spacing-1);

    & .warp-checkbox {
        min-height: 0;
        /* On the text's first line. */
        height: 1lh;
        font-size: var(--warp-font-size-sm);
        line-height: var(--warp-line-height-normal);
    }
}

.warp-choice-text {
    font-size: var(--warp-font-size-sm);
    line-height: var(--warp-line-height-normal);
    color: var(--warp-color-gray-900);
}

.warp-choice-help {
    font-size: var(--warp-font-size-xs);
    color: var(--warp-color-gray-500);
}

.warp-checkbox {
    display: inline-flex;
    align-items: center;
    gap: var(--warp-spacing-2);
    min-height: var(--warp-control-height);
    cursor: pointer;

    & input[type="checkbox"],
    & input[type="radio"] {
        width: 16px;
        height: 16px;
        margin: 0;
        accent-color: var(--warp-color-primary-600);
        cursor: pointer;
    }

    /* Drawn by Warp, so that a checked box reads as checked also when disabled, in view mode. */
    & input[type="checkbox"] {
        appearance: none;
        display: inline-grid;
        place-content: center;
        flex: none;
        border: 1px solid var(--warp-color-control-border);
        border-radius: var(--warp-radius-sm);
        background-color: white;

        &::before {
            content: "";
            width: 10px;
            height: 10px;
            background-color: white;
            clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
            transform: scale(0);
        }

        &:checked {
            background-color: var(--warp-color-primary-600);
            border-color: var(--warp-color-primary-600);

            &::before {
                transform: scale(1);
            }
        }

        &:focus-visible {
            outline: 2px solid var(--warp-color-primary-600);
            outline-offset: 2px;
        }

        &:disabled {
            cursor: not-allowed;
            opacity: 0.6;
        }
    }

    /* Drawn by Warp as the checkbox: a disabled native radio's dot is grey and reads as empty in view mode. */
    & input[type="radio"] {
        appearance: none;
        display: inline-grid;
        place-content: center;
        flex: none;
        border: 1px solid var(--warp-color-control-border);
        border-radius: 50%;
        background-color: white;

        &::before {
            content: "";
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background-color: var(--warp-color-primary-600);
            transform: scale(0);
        }

        &:checked {
            border-color: var(--warp-color-primary-600);

            &::before {
                transform: scale(1);
            }
        }

        &:focus-visible {
            outline: 2px solid var(--warp-color-primary-600);
            outline-offset: 2px;
        }

        &:disabled {
            cursor: not-allowed;
            opacity: 0.6;
        }
    }
}

/* =============================================================================
   Combobox (docs/krav/combobox.md)
   The text field with its arrow; in view mode, or fixed, the chosen value's
   text. One list of suggestions per page, under the field that has focus.
   ============================================================================= */

.warp-combobox {
    position: relative;
    display: block;
    width: 100%;
    max-width: var(--warp-control-width, 16rem);
}

.warp-combobox-view {
    display: none;
}

.warp-combobox-edit {
    position: relative;
    display: flex;
    align-items: center;
}

.warp-combobox--fixed .warp-combobox-view,
.warp-record-fields:disabled .warp-combobox-view {
    display: flex;
}

.warp-record-fields:disabled .warp-combobox-edit {
    display: none;
}

.warp-combobox-input.warp-input {
    max-width: none;
    padding-right: calc(var(--warp-control-height) + var(--warp-spacing-1));
}

.warp-combobox-toggle {
    position: absolute;
    inset-block: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--warp-control-height);
    min-width: var(--warp-touch-target, 0);
    min-height: var(--warp-touch-target, 0);
    padding: 0;
    color: var(--warp-color-gray-600);
    background: none;
    border: 0;
    cursor: pointer;

    & .warp-icon {
        width: 0.75rem;
        height: 0.75rem;
        /* Optically centred, as the select's chevron */
        translate: 0 0.0625rem;
    }
}

/* Shown at once when a character is typed, until the answer comes */
.warp-combobox-busy {
    position: absolute;
    right: calc(var(--warp-control-height) + var(--warp-spacing-0-5));
    width: 0.75rem;
    height: 0.75rem;
    border: 2px solid var(--warp-color-gray-300);
    border-top-color: var(--warp-color-primary-600);
    border-radius: 50%;
    animation: warp-spin 0.8s linear infinite;

    &[hidden] {
        display: none;
    }
}

.warp-combobox-mark {
    flex: none;
    margin-left: var(--warp-spacing-2);
    padding: 0 var(--warp-spacing-1);
    font-size: var(--warp-font-size-xs);
    color: var(--warp-color-gray-700);
    background-color: var(--warp-color-gray-100);
    border-radius: var(--warp-radius-default);
}

.warp-combobox-edit > .warp-combobox-mark {
    position: absolute;
    right: calc(var(--warp-control-height) + var(--warp-spacing-1));
}

/* In the top layer, fixed at its field by warp--combobox#place: no scroll box, dialog or panel clips it */
.warp-combobox-popup {
    position: fixed;
    inset: auto;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    overflow-y: auto;
    color: inherit;
    z-index: var(--warp-z-dropdown);
    width: 22rem;
    max-width: min(36rem, 100vw - 2 * var(--warp-spacing-4));
    background-color: var(--warp-color-white);
    border: 1px solid var(--warp-color-control-border);
    border-radius: var(--warp-radius-default);
    box-shadow: var(--warp-shadow-lg);

    &[hidden] {
        display: none;
    }
}

/* A small register's loaded choices, copied into the list */
.warp-combobox-choices {
    display: none;
}

/* The list's search box for a small register on a touch screen */
.warp-combobox-search.warp-input {
    display: block;
    width: calc(100% - 2 * var(--warp-spacing-2));
    max-width: none;
    margin: var(--warp-spacing-2);

    &[hidden] {
        display: none;
    }
}

.warp-combobox-list {
    max-height: 20rem;
    margin: 0;
    padding: var(--warp-spacing-0-5) 0;
    overflow-y: auto;
    list-style: none;

    &:empty {
        display: none;
    }
}

/* Every column shows without hover; long names wrap instead of being cut */
.warp-combobox-option {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: var(--warp-spacing-3);
    min-height: var(--warp-touch-target, var(--warp-control-height));
    padding: var(--warp-spacing-1) var(--warp-spacing-2);
    font-size: var(--warp-font-size-sm);
    color: var(--warp-color-gray-900);
    cursor: pointer;

    &:hover {
        background-color: var(--warp-color-gray-50);
    }

    &[aria-selected="true"] {
        background-color: var(--warp-color-primary-50);
        box-shadow: inset 3px 0 0 var(--warp-color-primary-600);
    }

    /* A new record from the field, last, in a dialog */
    &.warp-combobox-option--new {
        font-style: italic;
        color: var(--warp-color-primary-700);
        border-top: 1px solid var(--warp-color-gray-200);
    }

    /* A tree's depth, as in .warp-choice-item */
    &[data-depth="1"] { padding-left: calc(var(--warp-spacing-2) + var(--warp-spacing-6)); }
    &[data-depth="2"] { padding-left: calc(var(--warp-spacing-2) + var(--warp-spacing-6) * 2); }
    &[data-depth="3"] { padding-left: calc(var(--warp-spacing-2) + var(--warp-spacing-6) * 3); }
    &[data-depth="4"] { padding-left: calc(var(--warp-spacing-2) + var(--warp-spacing-6) * 4); }
}

.warp-combobox-cell {
    overflow-wrap: anywhere;

    &:first-child {
        font-weight: var(--warp-font-weight-medium);
    }

    &:not(:first-child) {
        color: var(--warp-color-gray-700);
    }
}

.warp-combobox-note {
    margin: 0;
    padding: var(--warp-spacing-1) var(--warp-spacing-2);
    font-size: var(--warp-font-size-xs);
    color: var(--warp-color-gray-600);
    border-top: 1px solid var(--warp-color-gray-200);

    .warp-combobox-list:empty + & {
        border-top: 0;
    }

    &[hidden] {
        display: none;
    }
}

/* =============================================================================
   A read-only field with a reason (readonly_reason): drawn as in view mode,
   with the reason in the message line instead of the help. A script that
   makes a field with a reason read-only changes the look and the reason with
   it; a field drawn read-only stays drawn so.
   ============================================================================= */

:is(.warp-field--readonly, .warp-field:has(> .warp-field-message > .warp-field-reason):has(> .warp-field-control :is(input, select, textarea):is([readonly], :disabled))) {
    & > .warp-field-control {
        & .warp-input,
        & .warp-select,
        & .warp-textarea {
            background-color: var(--warp-color-readonly-bg);
            border-color: transparent transparent var(--warp-color-field-line);
            border-radius: var(--warp-radius-default) var(--warp-radius-default) 0 0;
            box-shadow: none;
            color: var(--warp-color-gray-900);
            -webkit-text-fill-color: var(--warp-color-gray-900);
            cursor: default;
            opacity: 1;
        }

        & .warp-select {
            background-image: none;
        }

        /* An empty date is empty, not the browser's placeholder. */
        & input[type="date"]:is(:not([value]), [value=""]) {
            color: transparent;
            -webkit-text-fill-color: transparent;
        }

        & .warp-checkbox,
        & .warp-checkbox input,
        & .warp-choice-label {
            cursor: default;
            opacity: 1;
        }
    }

    /* Hidden, with its place kept, as in view mode (_records.css) */
    & > .warp-field-label :is(.warp-field-required, .warp-field-required + .warp-sr-only) {
        visibility: hidden;
    }
}

/* An empty text field is "–" only in a record's view mode; elsewhere the dash is not seen. */
.warp-input--dash::placeholder {
    color: transparent;
    -webkit-text-fill-color: transparent;
}

.warp-record-fields:disabled .warp-field .warp-input--dash::placeholder {
    color: var(--warp-color-gray-900);
    -webkit-text-fill-color: var(--warp-color-gray-900);
    opacity: 1;
}

/* A checkbox disabled with a reason outside a field, as a record's flags: as in view mode. */
.warp-checkbox--readonly {
    cursor: default;

    & input[type="checkbox"]:disabled {
        cursor: default;
        opacity: 1;
    }
}

/* The reason shows, and the help goes, while the control is read-only. */
:where(.warp-field:has(> .warp-field-message > .warp-field-reason):has(> .warp-field-control :is(input, select, textarea):is([readonly], :disabled))) > .warp-field-message {
    & > .warp-field-reason[hidden] {
        display: inline;
    }

    & > .warp-field-help {
        display: none;
    }
}

/* In a record's view mode the help and the reason are not shown; an error and a notice are. */
.warp-record-fields:disabled :is(.warp-field-message > .warp-field-reason, .warp-field-message > .warp-field-help, .warp-choice-help, .warp-row-table-reason--table) {
    display: none;
}

.warp-record-fields:disabled .warp-field-message:not(:has(> :not(.warp-field-reason, .warp-field-help))) {
    padding-top: 0;
}
