/* ============================================================
   Form controls

   Text inputs, selects, checkboxes, switches and the floating-label
   wrapper. Sizes and the focus ring are driven by custom properties so
   `.form-control-sm` / `.form-control-lg` only have to restate numbers.
   ============================================================ */

@layer components {
  .form-control,
  .form-select,
  .form-check-input {
    /* One focus treatment across every control */
    --input-focus-border-color: var(--color-primary);
    --input-focus-ring: 0 0 0 3px var(--primary-ring-strong);
  }

  .form-control,
  .form-select {
    --input-padding-y: 0.625rem;
    --input-padding-x: 1rem;
    --input-font-size: 1rem;
    --input-radius: var(--radius);
    --input-border-width: 1px;
    --input-border-color: #d1d5db;
    --input-placeholder-color: #9ca3af;
    --input-disabled-bg: #e9ecef;

    display: block;
    width: 100%;
    font-size: var(--input-font-size);
    font-weight: 400;
    line-height: var(--line-height);
    color: var(--color-body);
    /* Strips the platform chrome so the border-radius and the select
       chevron below are ours to draw. */
    appearance: none;
    background-color: var(--color-body-bg);
    border: var(--input-border-width) solid var(--input-border-color);
    border-radius: var(--input-radius);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;

    &:focus {
      color: var(--color-body);
      border-color: var(--input-focus-border-color);
      outline: none;
      box-shadow: var(--input-focus-ring);
    }

    &:disabled {
      background-color: var(--input-disabled-bg);
      opacity: 1;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .form-control,
    .form-select {
      transition: none;
    }
  }

  /* ---- Text inputs ---- */

  .form-control {
    padding: var(--input-padding-y) var(--input-padding-x);
    background-clip: padding-box;

    &::placeholder {
      color: var(--color-muted);
      opacity: 1;
    }

    &[type="file"] {
      overflow: hidden;
    }

    &[type="file"]:not(:disabled):not([readonly]) {
      cursor: pointer;
    }

    /* Chromium reserves a minimum width for date/time spinners */
    &::-webkit-date-and-time-value {
      min-width: 85px;
      height: 1.5em;
      margin: 0;
    }

    &::-webkit-datetime-edit {
      display: block;
      padding: 0;
    }

    /* The file button is pulled out to the control's own edges by the
       negative margin, so it reads as an attached segment. */
    &::file-selector-button {
      padding: var(--input-padding-y) var(--input-padding-x);
      margin: calc(-1 * var(--input-padding-y)) calc(-1 * var(--input-padding-x));
      margin-inline-end: var(--input-padding-x);
      color: var(--color-body);
      background-color: #f8f9fa;
      pointer-events: none;
      border-color: inherit;
      border-style: solid;
      border-width: 0;
      border-inline-end-width: var(--input-border-width);
      border-radius: 0;
      transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
        border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    }

    &:hover:not(:disabled):not([readonly])::file-selector-button {
      background-color: var(--input-disabled-bg);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .form-control::file-selector-button {
      transition: none;
    }
  }

  /* Outside a floating-label wrapper the placeholder is visible text and
     gets the app's lighter grey; inside one it is hidden (see below). */
  :not(.form-floating) > .form-control::placeholder,
  :not(.form-floating) > .form-select::placeholder {
    color: var(--input-placeholder-color);
  }

  textarea.form-control {
    min-height: calc(
      1.5em + var(--input-padding-y) * 2 + var(--input-border-width) * 2
    );
  }

  .form-control-sm {
    --input-padding-y: 0.25rem;
    --input-padding-x: 0.5rem;
    --input-font-size: 0.875rem;
    --input-radius: var(--radius-sm);

    min-height: calc(
      1.5em + var(--input-padding-y) * 2 + var(--input-border-width) * 2
    );
  }

  textarea.form-control-sm {
    min-height: calc(
      1.5em + var(--input-padding-y) * 2 + var(--input-border-width) * 2
    );
  }

  .form-control-lg {
    --input-padding-y: 0.5rem;
    --input-padding-x: 1rem;
    --input-font-size: 1.25rem;
    --input-radius: var(--radius-lg);

    min-height: calc(
      1.5em + var(--input-padding-y) * 2 + var(--input-border-width) * 2
    );
  }

  textarea.form-control-lg {
    min-height: calc(
      1.5em + var(--input-padding-y) * 2 + var(--input-border-width) * 2
    );
  }

  /* ---- Labels ---- */

  .form-label {
    margin-bottom: 0.375rem;
    font-weight: 500;
    color: var(--color-dark);
  }

  /* Marks a field the form will refuse to submit empty. */
  .form-label--required::after {
    content: " *";
  }

  /* Server-side validation marks a rejected field with .is-invalid, so the
     danger border and the warning glyph have to live here now that Bootstrap
     is not supplying them. */
  .form-control.is-invalid,
  .form-select.is-invalid {
    border-color: var(--color-danger);
  }

  .form-control.is-invalid {
    padding-right: calc(1.5em + 1.25rem);
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23ef4444'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23ef4444' stroke='none'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right calc(0.375em + 0.3125rem) center;
    background-size: calc(0.75em + 0.625rem) calc(0.75em + 0.625rem);
  }

  textarea.form-control.is-invalid {
    background-position: top calc(0.375em + 0.3125rem) right calc(0.375em + 0.3125rem);
  }

  .form-control.is-invalid:focus,
  .form-select.is-invalid:focus {
    border-color: var(--color-danger);
    box-shadow: 0 0 0 0.25rem color-mix(in srgb, var(--color-danger) 25%, transparent);
  }

  /* ---- Select -------------------------------------------------------
     `appearance: none` removes the native arrow, so the chevron is an
     inline SVG painted in the padding reserved on the inline-end side.
     `--form-select-bg-icon` is a second, optional layer that the
     validation states use for their tick / warning glyph.
     -------------------------------------------------------------------- */

  .form-select {
    --form-select-bg-img: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23343a40' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e");

    padding: var(--input-padding-y) 3rem var(--input-padding-y)
      var(--input-padding-x);
    background-image: var(--form-select-bg-img),
      var(--form-select-bg-icon, none);
    background-repeat: no-repeat;
    background-position: right var(--input-padding-x) center;
    background-size: 16px 12px;

    /* A list box has no collapsed arrow to draw */
    &[multiple],
    &[size]:not([size="1"]) {
      padding-right: var(--input-padding-x);
      background-image: none;
    }

    /* Firefox blanks the text colour on focus rings; paint it back with
       a shadow so the selected option stays readable. */
    &:-moz-focusring {
      color: transparent;
      text-shadow: 0 0 0 var(--color-body);
    }
  }

  /* ---- Checkboxes and radios ---------------------------------------
     The box is drawn by the element itself; the tick / dot is a
     background SVG swapped in through --form-check-bg-image when the
     input is checked.
     -------------------------------------------------------------------- */

  .form-check {
    display: block;
    min-height: 1.5rem;
    /* The indent that the negative margin on the input below sits in */
    padding-left: 1.5em;
    margin-bottom: 0.125rem;
  }

  .form-check .form-check-input {
    float: left;
    margin-left: -1.5em;
  }

  .form-check-input {
    --form-check-bg: var(--color-body-bg);

    flex-shrink: 0;
    width: 1em;
    height: 1em;
    margin-top: 0.25em;
    vertical-align: top;
    appearance: none;
    background-color: var(--form-check-bg);
    background-image: var(--form-check-bg-image);
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    border: 1px solid var(--color-border);
    /* Keeps the fill when the page is printed */
    print-color-adjust: exact;

    &[type="checkbox"] {
      border-radius: 0.25em;
    }

    &[type="radio"] {
      border-radius: 50%;
    }

    &:active {
      filter: brightness(90%);
    }

    &:focus {
      border-color: var(--input-focus-border-color);
      outline: 0;
      box-shadow: var(--input-focus-ring);
    }

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

    &:checked[type="checkbox"] {
      --form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
    }

    &:checked[type="radio"] {
      --form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='2' fill='%23fff'/%3e%3c/svg%3e");
    }

    &[type="checkbox"]:indeterminate {
      background-color: var(--color-primary);
      border-color: var(--color-primary);
      --form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10h8'/%3e%3c/svg%3e");
    }

    &:disabled {
      opacity: 0.5;
      pointer-events: none;
      filter: none;
    }
  }

  .form-check-input[disabled] ~ .form-check-label,
  .form-check-input:disabled ~ .form-check-label {
    cursor: default;
    opacity: 0.5;
  }

  /* ---- Switch -------------------------------------------------------
     The same checkbox, stretched to 2em and pill-rounded. The knob is a
     background circle that slides from `left center` to `right center`
     when checked; the SVG is re-declared per state because a data URI
     cannot read a custom property for its fill.
     -------------------------------------------------------------------- */

  .form-switch {
    padding-left: 2.5em;
  }

  .form-switch .form-check-input {
    --form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='rgba%280, 0, 0, 0.25%29'/%3e%3c/svg%3e");

    width: 2em;
    margin-left: -2.5em;
    background-image: var(--form-switch-bg);
    background-position: left center;
    border-radius: 2em;
    transition: background-position 0.15s ease-in-out;
  }

  @media (prefers-reduced-motion: reduce) {
    .form-switch .form-check-input {
      transition: none;
    }
  }

  /* Knob fill is --color-primary; a data URI cannot interpolate it. */
  .form-switch .form-check-input:focus {
    --form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%236366f1'/%3e%3c/svg%3e");
  }

  .form-switch .form-check-input:checked {
    --form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e");

    background-position: right center;
  }

  /* ---- Floating labels ----------------------------------------------
     The label is absolutely positioned over the control and shrinks up
     into the top of the box once the field is focused or has content.
     `:not(:placeholder-shown)` is what detects "has content", which is
     why every floating-label input must carry a placeholder attribute
     and why the placeholder itself is hidden here.

     `.focused` / `.filled` on the wrapper force the raised state for
     controls whose emptiness the selector cannot see.
     -------------------------------------------------------------------- */

  .form-floating {
    --fl-label-color: #9ca3af;
    --fl-label-color-active: color-mix(in srgb, var(--color-dark) 65%, transparent);
    --fl-label-raised: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
    /* Padding the control grows into once the label is raised */
    --fl-padding-top: 1.625rem;
    --fl-padding-bottom: 0.625rem;

    position: relative;

    > .form-control,
    > .form-select {
      height: calc(3.5rem + 2px);
      min-height: calc(3.5rem + 2px);
      line-height: 1.25;
    }

    > .form-control {
      padding: 1rem;
    }

    > .form-control::placeholder {
      color: transparent !important;
    }

    /* A select always shows its value, so its label is permanently raised
       and the padding is baked in. */
    > .form-select {
      padding: var(--fl-padding-top) var(--input-padding-x)
        var(--fl-padding-bottom);
    }

    > label {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
      width: 100%;
      max-width: 100%;
      height: 100%;
      padding: 1rem;
      overflow: hidden;
      color: var(--fl-label-color);
      font-weight: 400;
      text-align: start;
      text-overflow: ellipsis;
      white-space: nowrap;
      pointer-events: none;
      border: 1px solid transparent;
      transform-origin: 0 0;
      transform: none;
      transition: transform 0.15s ease-in-out, color 0.15s ease-in-out;
    }

    > .form-control:focus,
    > .form-control:not(:placeholder-shown),
    > .form-control:-webkit-autofill {
      padding-top: var(--fl-padding-top);
      padding-bottom: var(--fl-padding-bottom);
    }

    > .form-control:focus ~ label,
    > .form-control:not(:placeholder-shown) ~ label,
    > .form-control:-webkit-autofill ~ label,
    > .form-select ~ label {
      color: var(--fl-label-color-active);
      transform: var(--fl-label-raised);
    }

    /* A textarea scrolls its own content under the raised label, so the
       label needs an opaque strip behind it. */
    > textarea:focus ~ label::after,
    > textarea:not(:placeholder-shown) ~ label::after {
      position: absolute;
      inset: 1rem 0.5rem;
      z-index: -1;
      height: 1.5em;
      content: "";
      background-color: var(--color-body-bg);
      border-radius: var(--radius);
    }

    > textarea:disabled ~ label::after {
      background-color: #e9ecef;
    }

    > textarea.form-control {
      height: auto;
    }

    > :disabled ~ label,
    > .form-control:disabled ~ label {
      color: #6c757d;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .form-floating > label {
      transition: none;
    }
  }

  .form-floating.focused > .form-control,
  .form-floating.filled > .form-control {
    padding-top: var(--fl-padding-top);
    padding-bottom: var(--fl-padding-bottom);
  }

  .form-floating.focused > label,
  .form-floating.filled > label {
    color: var(--fl-label-color-active);
    transform: var(--fl-label-raised);
  }
}

@layer components {
  /* Dashed upload zone, shared by the post composer and the partner
     registration form. --stacked turns the compact row into a tall drop area. */
  .upload-dropzone {
    --dropzone-border-color: color-mix(in srgb, var(--color-dark) 20%, transparent);
    --dropzone-text-color: color-mix(in srgb, var(--color-dark) 72%, transparent);
    --dropzone-bg-color: rgba(255, 255, 255, 0.7);

    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.4rem;
    border: 1.5px dashed var(--dropzone-border-color);
    border-radius: 12px;
    color: var(--dropzone-text-color);
    background: var(--dropzone-bg-color);
    cursor: pointer;
    transition: border-color 0.2s ease, background-color 0.2s ease;

    &:hover {
      --dropzone-border-color: color-mix(in srgb, var(--color-primary) 80%, transparent);
      --dropzone-bg-color: color-mix(in srgb, var(--color-primary) 6%, transparent);
    }

    &.is-invalid {
      --dropzone-border-color: var(--color-danger);
      --dropzone-text-color: color-mix(in srgb, var(--color-danger) 90%, transparent);
      --dropzone-bg-color: color-mix(in srgb, var(--color-danger) 6%, transparent);
    }
  }

  .upload-dropzone__icon {
    font-size: 1.5rem;
    opacity: 0.6;
  }

  .upload-dropzone--stacked {
    flex-direction: column;
    padding: 1.5rem;
    text-align: center;
    cursor: default;
  }
}
