.input {
  /* Base padding inside input container div */
  --margin-left-label: calc(var(--fx-m-20));
  --floating-top: 18px;
  /* For date inputs, the container div renders a ::after element stacked on top */
  /* of the input to mask the browser's calendar icon. To prevent our input's */
  /* icons from being hidden, we set them higher than the mask element. */
  --calendar-mask-width: 24px;
  --calendar-mask-index: 5;
  --calendar-icon-right-margin: var(--fx-p-20);
  --icon-index: 10;

  &.with-icon-left {
    /* Add 2 pixels for the border due to box-sizing: border-box */
    /* Add 8 pixels for the gap in the flex container for the icons and input */
    /* Add 24 pixels for the icon, which we set a standard size of 24px */
    --margin-left-label: calc(var(--fx-m-12) + 2px + 8px + 24px);

    > div {
      /* spacing is different if the input has an icon or not... */
      padding-left: var(--fx-p-12);
      padding-right: var(--fx-p-20);
    }
  }

  /* Based on if there's an icon right or an error or both, the mask hiding the */
  /* browser's calendar icon needs to be larger */
  &.with-icon-right,
  &.error {
    --calendar-mask-width: 54px;
    /* there's an icon on the right, add icon width, base margin and add in the gap */
    --calendar-icon-right-margin: calc(24px + var(--fx-p-20) + var(--fx-m-8));
  }

  &.with-icon-right.error {
    --calendar-mask-width: 88px;
    /* there's two icons on the right, double icon width, base margin add in twice the gap */
    --calendar-icon-right-margin: calc(
      (24px * 2) +
      var(--fx-p-20) +
      (var(--fx-m-8) * 2)
    );
  }

  /* Forcefully make the label float if a placeholder is defined */

  &.with-placeholder {
    label {
      top: var(--floating-top);
      color: var(--text-tertiary);
      font-family: var(--dm-sans), sans-serif;
      line-height: 16px;
      font-size: 12px;
      font-weight: var(--font-weight-semibold);
    }
  }

  /* div containing the input and label (a <Stack /> in the JSX) */
  > div {
    position: relative;
    height: 60px;
    background: var(--bg-page);
    border: 1px solid var(--border-default);
    border-radius: 12px;
    color: var(--text-utility);
    padding-inline: var(--fx-p-20);

    &:hover:not(
        :has(input:disabled),
        :has(select:disabled),
        :has(input:read-only)
      ) {
      border-color: var(--border-emphasis);
    }

    &:focus-within {
      border-color: var(--border-emphasis);
      box-shadow: inset 0 0 0 1px var(--border-emphasis);
    }

    /* When containing a date input, we add an element to visually hide the calendar icon and replace it with our own */
    &:has(input[type="date"])::after {
      content: "";
      width: var(--calendar-mask-width);
      height: 80%;
      position: absolute;
      right: var(--fx-p-20);
      pointer-events: none;
      background: var(--bg-page);
      z-index: var(--calendar-mask-index);
    }
  }

  /* The supporting text, under the <Stack /> */
  > p {
    color: var(--text-utility);
    margin: 0;
  }

  &:has(input:disabled),
  &:has(select:disabled),
  &:has(input:read-only) {
    cursor: not-allowed;

    /* The input container div */
    .stack {
      background-color: var(--bg-surface);
    }
  }

  input,
  select {
    width: 100%;
    height: 100%;
    outline: none;
    background: none;
    border: none;
    padding-top: var(--fx-p-16);
    color: var(--text-primary);
    font-family: var(--dm-sans), sans-serif;
    font-size: 16px;
    font-weight: var(--font-weight-medium);
    line-height: 24px;
    letter-spacing: -0.25px;

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

  input {
    &:read-only {
      cursor: not-allowed;
    }

    &[type="date"] {
      color: var(--text-utility);
      text-transform: uppercase;
    }

    &::placeholder {
      color: var(--text-utility);
      font-family: var(--dm-sans), sans-serif;
      font-size: 16px;
      font-weight: var(--font-weight-medium);
      line-height: 24px;
      letter-spacing: -0.25px;
    }

    &:focus ~ label,
    &:not(:placeholder-shown) ~ label,
    &:-webkit-autofill ~ label,
    &:autofill ~ label {
      /* Label, when input is focused */
      top: var(--floating-top);
      color: var(--text-tertiary);
      font-family: var(--dm-sans), sans-serif;
      line-height: 16px;
      font-size: 12px;
      font-weight: var(--font-weight-semibold);
    }
  }

  select {
    /* down arrow */
    background-image: url("/image/icon/v3/picker.svg");
    background-position: 100%;
    background-repeat: no-repeat;
    appearance: none;

    &:not(:has(option[disabled]:checked)):valid ~ label {
      top: var(--floating-top);
      color: var(--text-tertiary);
      font-family: var(--dm-sans), sans-serif;
      line-height: 16px;
      font-size: 12px;
      font-weight: var(--font-weight-semibold);
    }

    /* Placeholder styling */

    &:has(option[value=""]:checked) {
      color: var(--text-utility);
    }
  }

  label {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: var(--margin-left-label);
    transition-duration: 300ms;
    font-family: var(--dm-sans), sans-serif;
    font-size: 16px;
    font-weight: var(--font-weight-medium);
    line-height: 24px;
    letter-spacing: -0.25px;
    pointer-events: none;
  }

  svg {
    color: var(--icon-strong);
    overflow: visible;
    z-index: var(--icon-index);
  }

  .calendar-icon {
    position: absolute;
    right: var(--calendar-icon-right-margin);
    pointer-events: none;
  }

  /* Error state styles. Must come after the base `> div` rules above: */
  /* `.error > div:hover` and `> div:hover:not(...)` tie on specificity, */
  /* so source order decides the winner. */
  &.error,
  &:has(input:user-invalid),
  &:has(select:user-invalid) {
    > div {
      border: 1px solid var(--border-status-error);
      color: var(--text-status-error);

      label {
        color: var(--text-status-error);
      }

      &:hover {
        border-color: var(--border-status-error);
      }

      &:focus-within {
        border-color: var(--border-status-error);
        /* Grow the border, but without causing a content shift */
        box-shadow: inset 0 0 0 1px var(--border-status-error);
      }
    }

    > p {
      color: var(--text-status-error);
    }

    input {
      &:focus ~ label,
      &:not(:placeholder-shown) ~ label,
      &:-webkit-autofill ~ label,
      &:autofill ~ label {
        color: var(--text-status-error);
      }
    }
  }
}
