/* Retro Form - Win9x Style Only */

@use "../variables" as *;
@use "../mixins" as *;

/**
 * @name Retro Form
 * @group components
 * @description
 * Form components including inputs, textareas, selects, and checkboxes.
 * @example scss
 *   <form class="retro-form">
 *     <div class="retro-form-group">
 *       <label for="username">Username</label>
 *       <input type="text" id="username" class="retro-input">
 *     </div>
 *   </form>
 */

.retro-form {
  &-group {
    margin-bottom: $retro-spacing-md;
  }

  &-group > label {
    display: block;
    margin-bottom: $retro-spacing-xs;
    font-weight: var(--retro-font-weight-normal);
    color: var(--retro-input-text);
  }
}

.retro-input,
.retro-textarea,
.retro-select {
  width: 100%;
  padding: $retro-spacing-sm;
  background: var(--retro-input-bg);
  border: 2px solid var(--retro-border-dark);
  box-shadow: inset 1px 1px 0 var(--retro-border-light), inset -1px -1px 0 var(--retro-border-medium);
  color: var(--retro-input-text);
  font-family: var(--retro-font);
  font-size: var(--retro-font-size);
  border-radius: var(--retro-border-radius);
  transition: none;

  // Border and inset shadow stay on plain :focus -- clicking into a text field
  // is a deliberate act and the field should show it. Only the outline is
  // gated behind :focus-visible, via the mixin below.
  &:focus {
    border-color: var(--retro-primary);
    box-shadow: inset 1px 1px 0 var(--retro-border-light), inset -1px -1px 0 var(--retro-border-medium);
  }

  @include retro-focus($offset: -4px);
}

.retro-textarea {
  min-height: 100px;
  resize: vertical;
}

.retro-select {
  appearance: none;
  background-image: none;
  padding-inline-end: $retro-spacing-md;
}

.retro-form {
  &-inline {
    display: flex;
    align-items: center;
    gap: var(--retro-spacing-md);

    .retro-form-group {
      margin-bottom: 0;
    }
  }

  &-horizontal {
    .retro-form-group {
      display: flex;
      align-items: center;
      gap: var(--retro-spacing-md);

      label {
        width: 200px;
        margin-bottom: 0;
        text-align: end;
      }
    }
  }
}

// Form validation states
.retro-input,
.retro-textarea,
.retro-select {
  &.is-valid {
    border-color: var(--retro-color-success);
    
    &:focus {
      box-shadow: 
        inset -1px -1px 0 var(--retro-border-dark),
        inset 1px 1px 0 var(--retro-shadow-light),
        0 0 0 2px rgba(var(--retro-color-success-rgb), 0.2);
    }
  }

  &.is-invalid {
    border-color: var(--retro-color-danger);
    
    &:focus {
      box-shadow: 
        inset -1px -1px 0 var(--retro-border-dark),
        inset 1px 1px 0 var(--retro-shadow-light),
        0 0 0 2px rgba(var(--retro-color-danger-rgb), 0.2);
    }
  }
}

.retro-form-text {
  font-size: var(--retro-font-size-sm);
  color: var(--retro-text-muted);
  margin-top: var(--retro-spacing-xs);
}

.retro-form-feedback {
  font-size: var(--retro-font-size-sm);
  margin-top: var(--retro-spacing-xs);

  &.is-valid {
    color: var(--retro-color-success);
  }

  &.is-invalid {
    color: var(--retro-color-danger);
  }
}

// NOTE: this block used to re-declare `border: 1px solid #b0b0b0` and
// `border-radius: 4px` over the tokenized 2px bevel above, flattening every
// input and freezing its border at a light-mode hex. Padding kept, border
// dropped so the bevel and the theme both survive.
.retro-input,
.retro-select,
textarea {
  padding: 0.5em 0.75em;
}

// Custom styles for additional input types
input[type="color"].retro-input {
  width: 48px;
  height: 36px;
  padding: 2px;
  border-radius: var(--retro-border-radius);
  border: 2px solid var(--retro-border-dark);
  background: var(--retro-input-bg);
  box-shadow: 1px 1px 0 var(--retro-border-light);
}

input[type="range"].retro-input {
  width: 100%;
  margin: 8px 0;
  background: transparent;
  appearance: none;
  height: 6px;
  border-radius: var(--retro-border-radius);
  outline: none;
  border: 2px solid var(--retro-border-dark);
  box-shadow: 1px 1px 0 var(--retro-border-light);
}
input[type="range"].retro-input::-webkit-slider-thumb {
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: var(--retro-border-radius);
  background: var(--retro-primary);
  border: 2px solid var(--retro-border-dark);
  box-shadow: 1px 1px 0 var(--retro-border-light);
  cursor: pointer;
}
input[type="range"].retro-input::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: var(--retro-border-radius);
  background: var(--retro-primary);
  border: 2px solid var(--retro-border-dark);
  box-shadow: 1px 1px 0 var(--retro-border-light);
  cursor: pointer;
}
input[type="range"].retro-input::-ms-thumb {
  width: 18px;
  height: 18px;
  border-radius: var(--retro-border-radius);
  background: var(--retro-primary);
  border: 2px solid var(--retro-border-dark);
  box-shadow: 1px 1px 0 var(--retro-border-light);
  cursor: pointer;
}
input[type="range"].retro-input::-webkit-slider-runnable-track {
  height: 6px;
  background: var(--retro-input-bg);
  border-radius: var(--retro-border-radius);
}
input[type="range"].retro-input::-ms-fill-lower,
input[type="range"].retro-input::-ms-fill-upper {
  background: var(--retro-input-bg);
}
input[type="file"].retro-input {
  padding: 4px;
  background: var(--retro-input-bg);
  border: 2px solid var(--retro-border-dark);
  box-shadow: 1px 1px 0 var(--retro-border-light);
  color: var(--retro-input-text);
  font-family: var(--retro-font);
  font-size: var(--retro-font-size);
}

// Disabled input styles
.retro-input:disabled,
.retro-select:disabled,
.retro-textarea:disabled {
  // Was #888 on #c0c0c0 -- 1.95:1, unreadable. Muted text on the light surface
  // holds ~4.2:1 while still reading as clearly dimmer than enabled text.
  background: var(--retro-light);
  color: var(--retro-text-muted);
  border-color: var(--retro-border-medium);
  cursor: not-allowed;
  opacity: 1;
}

.retro-label-text {
  display: inline-block;
  font-weight: normal;
  margin-bottom: 0;
  vertical-align: middle;
  line-height: 1.2;
}

// Modern custom checkbox and radio (Win9x style)
input[type="checkbox"].retro-input,
input[type="radio"].retro-input {
  appearance: none;
  -webkit-appearance: none;
  background: var(--retro-input-bg);
  border: 2px solid var(--retro-border-medium);
  width: 16px;
  height: 16px;
  margin-block: 0 0;
  margin-inline: 0 8px;
  vertical-align: middle;
  display: inline-block;
  position: relative;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

input[type="checkbox"].retro-input {
  border-radius: var(--retro-border-radius);
}
input[type="checkbox"].retro-input:checked::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 0px;
  width: 5px;
  height: 10px;
  border: solid var(--retro-primary);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
  background: none;
  display: block;
}

input[type="radio"].retro-input {
  border-radius: 50%;
}
input[type="radio"].retro-input:checked::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 4px;
  width: 6px;
  height: 6px;
  background: var(--retro-primary);
  border-radius: 50%;
  display: block;
}

input[type="checkbox"].retro-input,
input[type="radio"].retro-input {
  @include retro-focus-ring;
}

.retro-label {
  font-family: var(--retro-font);
  font-size: var(--retro-font-size);
  color: var(--retro-text);
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  margin-inline-end: 16px;
}

/**
 * @name Form Shell
 * @group components
 * @description
 * Wrapper and a few pieces the demo pages referenced but the framework never
 * shipped: .retro-form, .retro-input-icon, .retro-file-input, .retro-listbox.
 */
.retro-form {
  display: flex;
  flex-direction: column;
  gap: var(--retro-spacing-md);
  width: 100%;

  // A flex column stretches its children across the cross axis, which is what
  // fields want and buttons do not: every submit button in a .retro-form
  // silently spanned the whole form. Fields keep the stretch; a direct-child
  // button sits at its natural width unless it asks for more (.retro-w-full
  // still wins, since width beats the alignment).
  > button,
  > .retro-btn {
    align-self: flex-start;
  }
}

/// An input with a leading glyph.
///
/// A block, not a flex row: the glyph is positioned against the field, so any
/// extra content in the wrapper (a hint, a validation checklist) flows below
/// the input instead of being laid out beside it.
.retro-input-icon {
  position: relative;
  display: block;
  width: 100%;

  > .retro-input,
  > input,
  > .retro-select,
  > select {
    width: 100%;
    padding-inline-start: 2.2em;
  }

  // The glyph: any non-field first child. Absolutely positioned and centred,
  // so it tracks the field's height at any font size. Keep the wrapper to just
  // the glyph and the field — hints and checklists go after it, not inside.
  > :first-child:not(input):not(select):not(.retro-input):not(.retro-select) {
    position: absolute;
    top: 50%;
    inset-inline-start: 0.7em;
    transform: translateY(-50%);
    line-height: 1;
    pointer-events: none;
    color: var(--retro-text-muted);
    z-index: 1;
  }
}

.retro-file-input {
  display: block;
  width: 100%;
  padding: var(--retro-spacing-xs);
  background: var(--retro-input-bg);
  color: var(--retro-input-text);
  border: var(--retro-border-width-thick) solid var(--retro-border-dark);
  box-shadow: var(--retro-border-sunken);
  font-family: var(--retro-font);
  font-size: var(--retro-font-size);
}

/// Multi-select list, styled to match .retro-select.
.retro-listbox {
  width: 100%;
  padding: 0;
  background: var(--retro-input-bg);
  color: var(--retro-input-text);
  border: var(--retro-border-width-thick) solid var(--retro-border-dark);
  box-shadow: var(--retro-border-sunken);
  font-family: var(--retro-font);
  font-size: var(--retro-font-size);

  // Works as a <select multiple> and as a plain <ul>. Only `option` was styled
  // before, so a <ul class="retro-listbox"> fell through to the base list rules
  // and rendered with disc markers and no padding.
  margin: 0;
  list-style: none;
  overflow-y: auto;

  option,
  li {
    display: block;
    padding: var(--retro-spacing-xs) var(--retro-spacing-sm);
    list-style: none;
  }

  li + li {
    border-top: 1px solid var(--retro-border-light);
  }

  option:checked,
  li[aria-selected="true"],
  li.active {
    background: var(--retro-primary);
    color: var(--retro-primary-fg);
  }
}
