/* @group Layout */

/* @group Input */

/* @group Base */

#main .select_group,
#main .select_tag_group {
  display: flex;
  flex-flow: row nowrap;
  place-content: stretch flex-start;
  align-items: center;
}

.check_box_group,
.switch_group,
.switch_tag_group {
  display: flex;
  flex-flow: row nowrap;
  place-content: stretch flex-start;
  align-items: center;
}

.check_box_group:first-child {
  margin-inline-end: var(--ui-space-2);
}

::placeholder {
  color: var(--color-placeholder-text);
}

label {
  margin-inline-end: var(--ui-space-1);
  white-space: normal;
}

form[data-form-validation-issues='show'] input:invalid {
  border-color: var(--color-negative);
}

input::-moz-focus-inner {
  border-style: none;
}

/* Text-field chrome. Sliders, radio buttons and colour swatches are drawn
   in controls.css, and :where keeps this rule's weight at a plain element
   selector. */
input:where(:not([type='range'], [type='radio'], [type='color'])),
textarea,
select {
  box-sizing: border-box;
  margin-block-start: var(--ui-space-1);
  margin-block-end: var(--ui-space-2);
  padding: var(--ui-space-2);
  color: var(--color-input-text);
  font-family: var(--ui-font-family);
  font-size: var(--ui-font-size-field);
  font-weight: var(--ui-weight-regular);
  line-height: var(--ui-line-height-control);
  background-color: var(--color-input-background);
  border: 1px solid var(--color-input-border);
  border-radius: var(--ui-radius-control);
}

input[type='date'] {
  min-height: 26px;
}

input[type='checkbox'],
input[type='date'],
input[type='email'],
input[type='password'],
input[type='text'],
select,
textarea {
  appearance: none;
}

input[type='checkbox'] {
  width: 16px;
  height: 16px;
  margin: 0;
  background-color: var(--color-input-background);
  border: 1px solid var(--color-input-border);
}

input[type='checkbox']:checked {
  background-image: var(--checkbox-checkmark-icon);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 8px;
  border: 1px solid var(--color-input-border);
}

input[type='file'] {
  background-color: transparent;
  border-style: none;
  box-shadow: none;
}

input[readonly],
textarea[readonly] {
  color: var(--color-text-muted);
  font-family: var(--ui-font-family-code);
  background-color: var(--color-toolbar);
}

textarea {
  resize: vertical;
}

select:not([multiple='multiple']),
input[list] {
  padding-inline-end: var(--ui-space-6);
  padding-inline-start: var(--ui-space-2);
  background-color: var(--color-input-background);
  background-image: var(--select-icon-default);
  background-position: right 7px center;
  background-repeat: no-repeat;
  background-size: 9px 9px;
  border: 1px solid var(--color-input-border);
  box-shadow: none;
}

/* Keep the native suggestions target over the shared chevron. */
input[list] {
  padding-inline-end: var(--ui-space-2);
}

input[list]::-webkit-calendar-picker-indicator {
  width: var(--ui-space-4);
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

/* Enhanced select menus inherit --list-* surface tokens from their control.
   Unsupported browsers retain native menus. */
@supports (appearance: base-select) {
  select:not([multiple], [size]),
  select:not([multiple], [size])::picker(select) {
    appearance: base-select;
  }

  /* A toolbar's choice keeps one width whatever is chosen (the widest
     choice's, as a native select does). The controls after it then never
     move (the design toolbar's page, then Undo and Redo). */
  .options_bar_form select:not([multiple], [size]) {
    field-sizing: fixed;
  }

  /* A customizable select lays its value out as a flex row; centre it
     vertically, as the native control does. */
  select:not([multiple], [size]) {
    --list-surface: var(--color-input-background);
    --list-text: var(--color-input-text);
    --list-text-current: var(--list-text);
    --list-edge: var(--color-control-border);
    --list-highlight: var(--color-list-highlight);
    --list-highlight-text: var(--color-list-highlight-text);
    --list-shadow: var(--shadow-list);
    --list-offset: var(--ui-space-1);
    /* One row: the list's text line plus 4px above and below, or a touch
       target wherever a finger or Pencil can reach the list. */
    --list-row: calc(1em * 1.5 + 2 * var(--ui-space-1));

    align-items: center;
  }

  /* The select's own chevron is its background image. */
  select:not([multiple], [size])::picker-icon {
    display: none;
  }

  /* The list is set in its control's type, so it reads as the control
     opened, whatever size the control is where it sits. */
  select:not([multiple], [size])::picker(select) {
    box-sizing: border-box;
    min-width: anchor-size(width);
    max-height: min(60dvh, calc(var(--list-row) * 10.5 + 2 * var(--ui-space-1)));
    margin-block: var(--list-offset);
    padding: var(--ui-space-1) 0;
    color: var(--list-text);
    font: inherit;
    font-weight: var(--ui-weight-regular);
    background-color: var(--list-surface);
    border: 1px solid var(--list-edge);
    border-radius: var(--ui-radius-control);
    box-shadow: var(--list-shadow);
    overscroll-behavior: contain;
    /* The arrow bars below read whether the list scrolls. */
    container-type: scroll-state;
  }

  /* Rows leave a column for the check at the start and room at the end, so
     the longest name never touches the edge. */
  select:not([multiple], [size]) option {
    gap: var(--ui-space-2);
    box-sizing: border-box;
    min-height: var(--list-row);
    padding-block: 0;
    padding-inline: var(--ui-space-3) var(--ui-space-6);
    font: inherit;
    line-height: 1.5;
    white-space: nowrap;
    outline: none;
    transition: background-color var(--ui-duration-feedback) var(--ui-ease-feedback), color var(--ui-duration-feedback) var(--ui-ease-feedback);
  }

  /* One row is lit at a time: the keyboard's, or the pointer's when the
     pointer is over the list. */
  select:not([multiple], [size]) option:focus-visible,
  select:not([multiple], [size], :has(option:hover)) option:focus {
    color: var(--list-highlight-text);
    background-color: var(--list-highlight);
  }

  select:not([multiple], [size]) option:checked {
    color: var(--list-text-current);
    font-weight: var(--ui-weight-medium);
  }

  select:not([multiple], [size]) option:disabled {
    color: var(--color-text-inactive);
  }
}

/* A pointer that can hover lights the row under it; on a touch screen a lit
   row would stay behind after the tap. */
@media (any-hover: hover) {
  @supports (appearance: base-select) {
    select:not([multiple], [size]) option:not(:disabled):hover {
      color: var(--list-highlight-text);
      background-color: var(--list-highlight);
    }

    select:not([multiple], [size]):has(option:hover) option:focus-visible:not(:hover) {
      color: var(--list-text);
      background-color: transparent;
    }

    select:not([multiple], [size]):has(option:hover) option:checked:focus-visible:not(:hover) {
      color: var(--list-text-current);
    }
  }
}

@media (any-pointer: coarse) {
  @supports (appearance: base-select) {
    select:not([multiple], [size]) {
      --list-row: var(--ui-touch-target);
    }
  }
}

/* The chosen row's check, a light glyph in the start column. Its own rule:
   an engine without ::checkmark drops only this. */
@supports (appearance: base-select) and selector(::checkmark) {
  select:not([multiple], [size]) option::checkmark {
    flex: 0 0 1em;
    font: var(--ui-icon-weight) 0.9em/1 var(--ui-icon-font-family);
    text-align: center;
    content: var(--ui-icon-check) var(--ui-icon-alt,);
  }
}

/* Selects cannot draw picker pseudo-elements, so list_edges inserts the scroll indicators.
   Indicators overlay options without adding scroll height; ordering accommodates later options. */
@supports (appearance: base-select) and (container-type: scroll-state) {
  select:not([multiple], [size]):open::picker(select) {
    display: flex;
    flex-direction: column;
  }

  select:not([multiple], [size]) > .list_edge {
    position: sticky;
    z-index: 1;
    display: block;
    visibility: hidden;
    height: var(--list-row);
    color: var(--list-text);
    font: var(--ui-icon-weight) var(--ui-font-size-small)/var(--list-row) var(--ui-icon-font-family);
    text-align: center;
    background-color: var(--list-surface);
    pointer-events: none;
  }

  select:not([multiple], [size]) > .list_edge[data-edge='start'] {
    /* Stuck over the list's padding too, so no row shows past the bar. */
    inset-block-start: calc(-1 * var(--ui-space-1));
    order: -1;
    margin-block-end: calc(-1 * var(--list-row));
    border-block-end: 1px solid var(--list-edge);
  }

  select:not([multiple], [size]) > .list_edge[data-edge='end'] {
    inset-block-end: calc(-1 * var(--ui-space-1));
    order: 1;
    margin-block-start: calc(-1 * var(--list-row));
    border-block-start: 1px solid var(--list-edge);
  }

  select:not([multiple], [size]) > .list_edge[data-edge='start']::before {
    content: var(--ui-icon-chevron-up) var(--ui-icon-alt,);
  }

  select:not([multiple], [size]) > .list_edge[data-edge='end']::before {
    content: var(--ui-icon-chevron-down) var(--ui-icon-alt,);
  }

  @container scroll-state(scrollable: top) {
    select:not([multiple], [size]) > .list_edge[data-edge='start'] {
      visibility: visible;
    }
  }

  @container scroll-state(scrollable: bottom) {
    select:not([multiple], [size]) > .list_edge[data-edge='end'] {
      visibility: visible;
    }
  }
}

/* @end */

/* @group Focus */

/* Text fields show focus on the field itself; every other
   control shows a ring for keyboard focus (see focus.css). */
input:is(:not([type]), [type='text'], [type='email'], [type='password'], [type='search'], [type='url'], [type='tel'], [type='number'], [type='date'], [type='datetime-local']):focus,
textarea:focus {
  border-color: var(--color-input-focus-border);
  outline: 2px solid transparent;
  box-shadow: 0 0 8px var(--color-input-focus-shadow);
}

input[type]:not([type='text'], [type='email'], [type='password'], [type='search'], [type='url'], [type='tel'], [type='number'], [type='date'], [type='datetime-local']):focus-visible,
select:focus-visible {
  outline: 2px solid var(--color-input-focus-border);
  outline-offset: 2px;
}

input:is([type='text'], [type='email'], [type='password'], [type='search'], [type='url'], [type='tel'], [type='number'], [type='date'], [type='datetime-local']),
select {
  max-width: 100%;
  min-height: var(--ui-control-height);
}

@media (any-pointer: coarse) {
  input:is([type='text'], [type='email'], [type='password'], [type='search'], [type='url'], [type='tel'], [type='number'], [type='date'], [type='datetime-local']),
  select,
  textarea {
    min-height: var(--ui-touch-target);
    font-size: var(--ui-font-size-field);
  }

  .check_box_group {
    min-height: var(--ui-touch-target);
  }
}

/* @end */

a.link_button_small {
  padding: var(--ui-space-1) var(--ui-space-2);
  color: var(--color-text-inverse);
  text-align: center;
  border-radius: var(--ui-radius-control);
  cursor: pointer;
}

/* @end */

/* @group Date & Time Pickers */

input.date_picker,
input.time_picker {
  padding-inline-end: var(--ui-space-6);
  background-image: url("/assets/forms/calendar-1963f483.svg");
  background-position: right 6px center;
  background-repeat: no-repeat;
  background-size: 18px;
}

/* @end */

/* @end */

/* Shared Rails form rows: spacing is independent of field type and screen. */
#main form :is(.text_field_group, .select_group, .select_tag_group, .switch_group, .switch_tag_group),
#panels form :is(.text_field_group, .select_group, .select_tag_group, .switch_group, .switch_tag_group),
dialog form :is(.text_field_group, .select_group, .select_tag_group, .switch_group, .switch_tag_group) {
  margin-block-start: 0;
  margin-block-end: var(--ui-field-gap);
}

form :is(.text_field_group, .select_group, .select_tag_group) > :is(input, select, textarea) {
  margin-block-start: var(--ui-label-gap);
}

/* Every Rails field type participates in the same spacing contract. */
#main form .ui_field,
#panels form .ui_field,
dialog form .ui_field {
  margin-block-start: 0;
  margin-block-end: var(--ui-field-gap);
}

.ui_field {
  min-width: 0;
}

/* Form rows are divs (DpgFormBuilder::Row) and take the paragraph spacing
   (shared/typography.css p), at an element selector's weight. Every rule
   that spaces the rows then still wins. */
div:where(.ui_field) {
  margin-block-start: var(--ui-space-1);
  margin-block-end: var(--ui-space-1);
  line-height: var(--ui-line-height-body);
}

.ui_field > :is(input:not([type='checkbox'], [type='radio'], [type='hidden']), select, textarea) {
  min-width: 0;
  max-width: 100%;
}

/* Embedded code fields share the editor engine/theme; the form owns geometry. */
.cm-editor.ui_code_field {
  height: auto;
  min-height: 400px;
}

.cm-editor.ui_code_field[data-code-layout='html'] {
  height: 350px;
  min-height: 0;
}
