@layer ui-reset, ui;

/* loading */
@layer ui {
  :root, :host {
    --ui-loading: url("data:image/svg+xml,%3Csvg fill='none' height='24' width='24' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cstyle%3E g %7B animation: rotate 2s linear infinite; transform-origin: center center; %7D circle %7B stroke-dasharray: 75,100; stroke-dashoffset: -5; animation: dash 1.5s ease-in-out infinite; stroke-linecap: round; %7D @keyframes rotate %7B 0%25 %7B transform: rotate(0deg); %7D 100%25 %7B transform: rotate(360deg); %7D %7D @keyframes dash %7B 0%25 %7B stroke-dasharray: 1,100; stroke-dashoffset: 0; %7D 50%25 %7B stroke-dasharray: 44.5,100; stroke-dashoffset: -17.5; %7D 100%25 %7B stroke-dasharray: 44.5,100; stroke-dashoffset: -62; %7D %7D %3C/style%3E%3Cg%3E%3Ccircle cx='12' cy='12' r='10' fill='none' stroke='white' stroke-width='4' /%3E%3C/g%3E%3C/svg%3E");
  }

  :where(:not(button)[aria-busy]:not([aria-busy="false"])):before {
    content: "";
    mask-image: var(--ui-loading);
    aspect-ratio: 1;
    background-color: currentColor;
    width: 1lh;
    display: inline-block;
    mask-size: 1lh;
    mask-repeat: no-repeat;
  }
}

/* text-field */
@layer ui {
  input[type="text"], input[type="email"], input[type="search"], input[type="password"], input[type="number"], input[type="tel"], input[type="url"], input[type="date"], input[type="time"], input[type="datetime-local"], input[type="month"], input[type="week"], input:not([type]) {
    appearance: none;
    background-color: var(--bg, transparent);
    box-shadow: 0 0 0 1px var(--border-color, var(--ui-neutral-300)) inset;
    padding-block: var(--ui-spacing-2);
    padding-inline: calc(var(--ui-spacing-3) * clamp(1, var(--ui-rounded-scale) * .75, 1.3));
    border-radius: var(--ui-rounded-3);
    font: inherit;
    transition: padding-inline-start var(--ease-glide-2);
    border: none;
    line-height: 1.14286rem;

    @media (width < 600px) {
      font-size: 16px;
    }

    &:focus-visible {
      outline: none;
      box-shadow: inset 0 0 0 1px;
    }
  }

  label:has([data-suffix]) input[type="search"]::-webkit-search-cancel-button {
    display: none;
  }

  input[type="search"]::-webkit-search-cancel-button {
    appearance: none;
    cursor: pointer;
    background-color: var(--ui-neutral-400);
    width: 1em;
    height: 1em;
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M18 6l-12 12'/%3E%3Cpath d='M6 6l12 12'/%3E%3C/svg%3E");
    mask-position: center;
    mask-size: contain;
    mask-repeat: no-repeat;
  }

  label:not(.toggle):has( > [data-prefix],  > [data-suffix]) {
    cursor: text;
    grid-template-columns: auto 1fr auto auto;
    align-items: center;
    display: grid;

    & > input:is([type="text"], [type="email"], [type="search"], [type="password"], [type="number"], [type="tel"], [type="url"], :not([type])) {
      grid-area: 1 / 1 / auto / -1;
    }

    & > [data-prefix], & > [data-suffix] {
      z-index: 1;
      grid-row: 1;
      position: relative;
    }

    & > [data-prefix]:not(button), & > [data-suffix]:not(button) {
      color: var(--ui-neutral-600);
    }

    & > [data-prefix] {
      margin-inline: var(--ui-spacing-3) var(--ui-spacing-2);
      transition: opacity var(--ease-glide-2);
      grid-column: 1;
    }

    & > [data-suffix]:last-child {
      margin-inline: var(--ui-spacing-2) var(--ui-spacing-3);
      grid-column: 4;
    }

    & > [data-suffix]:not(:last-child) {
      margin-inline: var(--ui-spacing-2) 0;
      grid-column: 3;
    }

    & > button[data-suffix] {
      padding: var(--ui-spacing-2) var(--ui-spacing-2);
      border-radius: calc(var(--ui-rounded-3) - 3px);
      height: calc(100% - 8px);
      font-size: smaller;

      &:last-child {
        margin-inline-end: 4px;
      }
    }

    &:before {
      content: "";
      z-index: 2;
      background-color: var(--ui-neutral-400);
      width: 1em;
      height: 1em;
      mask-image: var(--ui-loading);
      margin-inline: var(--ui-spacing-3) var(--ui-spacing-2);
      opacity: 0;
      transition: opacity var(--ease-glide-2);
      grid-area: 1 / 1;
      display: block;
      position: relative;
      mask-size: contain;
      mask-repeat: no-repeat;
    }

    &:has( > input[aria-busy]:not([aria-busy="false"])) {
      & > [data-prefix] {
        opacity: 0;
      }

      &:before {
        opacity: 1;
      }
    }
  }

  label:not(.toggle):not(:has( > [data-prefix],  > [data-suffix])):has( > input[aria-busy]:not([aria-busy="false"])) {
    &:before {
      opacity: 1;
    }
  }

  label:not(.toggle):not(:has( > [data-prefix],  > [data-suffix])):has( > input[aria-busy]:not([aria-busy="false"])) {
    cursor: text;
    grid-template-columns: auto 1fr;
    align-items: center;
    display: grid;

    &:before {
      content: "";
      z-index: 1;
      background-color: var(--ui-neutral-400);
      width: 1em;
      height: 1em;
      mask-image: var(--ui-loading);
      margin-inline: var(--ui-spacing-3) var(--ui-spacing-2);
      transition: opacity var(--ease-glide-2);
      grid-area: 1 / 1;
      display: block;
      position: relative;
      mask-size: contain;
      mask-repeat: no-repeat;

      @starting-style {
        opacity: 0;
      }
    }

    & > input:is([type="text"], [type="email"], [type="search"], [type="password"], [type="number"], [type="tel"], [type="url"], :not([type])) {
      grid-area: 1 / 1 / auto / -1;
      padding-inline-start: calc(var(--ui-spacing-3) + 1rem + var(--ui-spacing-2));
    }
  }

  label:not(.toggle):has( > [data-prefix]) > input:is([type="text"], [type="email"], [type="search"], [type="password"], [type="number"], [type="tel"], [type="url"], :not([type])) {
    padding-inline-start: calc(var(--ui-spacing-3) + 1lh + var(--ui-spacing-2));
  }

  label:not(.toggle):has( > [data-suffix]) > input:is([type="text"], [type="email"], [type="search"], [type="password"], [type="number"], [type="tel"], [type="url"], :not([type])) {
    padding-inline-end: calc(var(--ui-spacing-3) + 1lh + var(--ui-spacing-2));
  }

  label:not(.toggle):has( > [data-suffix]:not(:last-child)) > input:is([type="text"], [type="email"], [type="search"], [type="password"], [type="number"], [type="tel"], [type="url"], :not([type])) {
    padding-inline-end: calc(var(--ui-spacing-3) + 2lh + var(--ui-spacing-2));
  }
}

/* input-otp */
@layer ui {
  .otp {
    --otp-length: 6;
    --otp-cell-size: 2.75rem;
    vertical-align: middle;
    width: calc((var(--otp-length) + 0) * var(--otp-cell-size));
    height: var(--otp-cell-size);
    display: inline-block;
    overflow: hidden;

    &:has( > input[maxlength="1"]) {
      --otp-length: 1;
    }

    &:has( > input[maxlength="2"]) {
      --otp-length: 2;
    }

    &:has( > input[maxlength="3"]) {
      --otp-length: 3;
    }

    &:has( > input[maxlength="4"]) {
      --otp-length: 4;
    }

    &:has( > input[maxlength="5"]) {
      --otp-length: 5;
    }

    &:has( > input[maxlength="6"]) {
      --otp-length: 6;
    }

    &:has( > input[maxlength="7"]) {
      --otp-length: 7;
    }

    &:has( > input[maxlength="8"]) {
      --otp-length: 8;
    }

    &:has( > input[maxlength="9"]) {
      --otp-length: 9;
    }

    &:has( > input[maxlength="10"]) {
      --otp-length: 10;
    }

    text-overflow: clip;

    &.otp > input {
      width: calc((var(--otp-length) + 2) * var(--otp-cell-size));
      height: var(--otp-cell-size);
      background-color: var(--ui-neutral-100);
      font-variant-numeric: tabular-nums;
      letter-spacing: calc(var(--otp-cell-size) - 1ch);
      font-size: 1.25rem;
      font-weight: 600;
      transition: outline-color .1s;

      &:focus-visible {
        outline: 2px solid var(--ui-primary);
        outline-offset: 2px;
      }

      &:disabled {
        opacity: .4;
      }

      &::placeholder {
        color: var(--ui-neutral-300);
        letter-spacing: inherit;
      }

      &::selection {
        background-color: var(--ui-primary-200);
      }
    }
  }
}
