@use '../../sass/variables' as *;

select.browser-default {
  opacity: 1;
  color: var(--md-sys-color-on-background);
}

// TODO: Change to ".select" and not use tag name directly
// Keep selects useable without javascript

select {
  background-color: var(--md-sys-color-surface);
  width: 100%;
  padding: 5px;
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: 2px;
  height: 3rem;
}

.select-wrapper:focus-within {
  outline: 2px solid var(--md-sys-color-primary);
  border-radius: 4px;
}

.select-wrapper {
  position: relative;

  .caret {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto 0;
    z-index: 0;
    fill: var(--md-sys-color-on-background);
    pointer-events: none;
  }

  // Hide select with overflow hidden instead of using display none
  // (this prevents form validation errors with hidden form elements)
  .hide-select {
    width: 0;
    height: 0;
    overflow: hidden;
    position: absolute;
    top: 0;
    z-index: -1;
  }
}

select:disabled {
  color: var(--md-sys-color-on-surface);
}

.select-wrapper.disabled {
  + label {
    color: var(--md-sys-color-on-surface);
  }
  .caret {
    fill: var(--md-sys-color-on-surface);
  }
}

.select-wrapper input.select-dropdown:disabled {
  color: var(--md-sys-color-on-surface);
  cursor: default;
  user-select: none;
}

.select-wrapper i {
  color: var(--md-sys-color-on-surface);
}

/// FormSelect inherits its surface, selection, and focus states from Dropdown.
/// Keep its icon and checkbox rows aligned with the shared 48px menu items.
.dropdown-content.select-dropdown > li {
  display: flex;
  align-items: center;

  > span { flex: 1; }

  > img {
    flex: 0 0 20px;
    width: 20px;
    height: 20px;
    margin: 0;
    margin-inline-end: 12px;
    object-fit: cover;
    float: none;
    order: 1;

    &.left {
      order: -1;
      margin-inline-start: 12px;
      margin-inline-end: -4px;
    }
  }

  /// Prevent floating field labels from changing multi-select option labels.
  > span > label {
    position: static;
    display: flex;
    align-items: center;
    gap: 8px;
    height: auto;
    min-height: 24px;
    color: inherit;
    font-size: inherit;
    line-height: inherit;
    transform: none;

    > input[type="checkbox"] { flex-shrink: 0; }

    > span { color: inherit; }
  }
}

// Keep enhanced FormSelect labels in sync with TomSelectField.
// FormSelect moves the original label inside its generated .select-wrapper.
.input-field:has(> .select-wrapper) {
  margin-top: 16px;
}

// State 1: a genuinely empty select with no visible placeholder.
.input-field > .select-wrapper > label {
  position: absolute !important;
  top: 16px !important;
  left: 16px !important;
  z-index: 2;
  margin: 0 !important;
  padding: 0 !important;
  color: var(--md-sys-color-on-surface-variant);
  font-size: 16px !important;
  line-height: 24px !important;
  text-align: left !important;
  background: transparent;
  transform: none !important;
  transform-origin: top left !important;
  transition: top 0.2s ease-out, transform 0.2s ease-out, color 0.2s ease-out;
}

// State 2: the generated input displays a placeholder/selection, the native
// select declares a placeholder, or the component currently has focus.
.input-field > .select-wrapper:has(> input.select-dropdown:not(:placeholder-shown)) > label,
.input-field > .select-wrapper:has(.hide-select > select[placeholder]:not([placeholder=""])) > label,
.input-field > .select-wrapper:focus-within > label {
  position: absolute !important;
  top: 4px !important;
  left: 16px !important;
  padding: 0 !important;
  font-size: 16px !important;
  background: transparent;
  transform: scale(0.75) !important;
}

.input-field > .select-wrapper:focus-within > label {
  color: var(--input-color);
}

// Outlined FormSelect uses the same state logic with the label in its notch.
.input-field.outlined > .select-wrapper:has(> input.select-dropdown:not(:placeholder-shown)) > label,
.input-field.outlined > .select-wrapper:has(.hide-select > select[placeholder]:not([placeholder=""])) > label,
.input-field.outlined > .select-wrapper:focus-within > label {
  top: -8px !important;
  left: 12px !important;
  padding: 0 4px !important;
  line-height: 16px !important;
  background: var(--md-sys-color-surface);
  transform: scale(0.75) !important;
}

/// Group headings use the same typography and section separators as peer menus.
.dropdown-content.select-dropdown > li.optgroup {
  min-height: 44px;
  border-radius: 0;
  cursor: default;
  color: var(--md-sys-color-on-surface-variant);

  &:not(:first-child) {
    margin-top: 4px;
    border-top: 1px solid var(--md-sys-color-outline-variant);
  }

  > span {
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
  }

  + li {
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
  }
}

.dropdown-content.select-dropdown > li:has(+ .optgroup) {
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}

// Generated select inputs are readonly for typing, but remain interactive.
// They must not inherit the filled input's extra top padding or disabled outline.
.input-field.outlined > .select-wrapper {
  &:focus-within { outline: none; }
  > input.select-dropdown,
  > input.select-dropdown:not([type=checkbox]):not([type=radio]):not(:focus):not(:placeholder-shown) {
    padding-top: 0;
    padding-bottom: 0;
    padding-right: 40px;
  }
  &.has-outlined-notch {
    > input.select-dropdown + label { background: transparent; }
    > .input-outline { border-color: var(--md-sys-color-on-surface-variant); }
    &:focus-within > .input-outline {
      border-color: var(--input-color);
      border-width: 2px;
      padding-inline: 11px;
      > legend { max-width: 100%; }
    }
    &.disabled > .input-outline {
      border-color: color-mix(in srgb, transparent, var(--md-sys-color-on-surface) 12%);
    }
  }
}

// Filled selects share the text field's in-field floating label layout.
.input-field:not(.outlined) > .select-wrapper:has(> label) {
  > input.select-dropdown:not([type=checkbox]):not([type=radio]):not(:placeholder-shown),
  &:focus-within > input.select-dropdown:not([type=checkbox]):not([type=radio]) {
    padding-top: 12px;
    padding-right: 40px;
  }
}
