@layer reset, tokens, theme, base, components, utilities;

@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    scrollbar-gutter: stable;
  }

  :where(body, h1, h2, h3, p, ul, fieldset) {
    margin: 0;
  }

  :where(ul) {
    padding: 0;
    list-style: none;
  }
}

@layer tokens {
  :root {
    color-scheme: light dark;
    --background-color-page: light-dark(oklch(98.5% 0 none), oklch(21% 0.006 285.885));
    --background-color-surface: light-dark(oklch(100% 0 none), oklch(27.4% 0.006 286.033));
    --text-color: light-dark(oklch(27.4% 0.006 286.033), oklch(100% 0 none));
    --text-color-muted: light-dark(oklch(55.2% 0.016 285.938), oklch(70.5% 0.015 286.067));
    --border-color: light-dark(oklch(92% 0.004 286.32), oklch(37% 0.013 285.805));
    --color-accent: light-dark(oklch(27.4% 0.006 286.033), oklch(100% 0 none));
    --text-color-on-accent: light-dark(oklch(100% 0 none), oklch(27.4% 0.006 286.033));
    --color-danger: light-dark(oklch(57.7% 0.245 27.325), oklch(50.5% 0.213 27.518));
    --text-color-danger: light-dark(oklch(57.7% 0.245 27.325), oklch(70.4% 0.191 22.216));
    --focus-outline-color: light-dark(oklch(54.6% 0.245 262.881), oklch(70.7% 0.165 254.624));
    --border-radius: 0.5rem;
    --control-height: 2.5rem;
    --font-family: system-ui, sans-serif;
  }
}

@layer base {
  html {
    background: var(--background-color-page);
  }

  body {
    padding-inline: min(2rem, 5vw);
    color: var(--text-color);
    font-family: var(--font-family);
    line-height: 1.5;
    overflow-wrap: break-word;
  }

  a {
    color: inherit;
    text-underline-offset: 0.2em;
  }

  :focus-visible {
    outline: 2px solid var(--focus-outline-color);
    outline-offset: 2px;
  }

  input:is([type="checkbox"], [type="radio"]) {
    accent-color: var(--color-accent);
  }

  @media (forced-colors: active) {
    :root {
      --background-color-page: Canvas;
      --background-color-surface: Canvas;
      --text-color: CanvasText;
      --text-color-muted: CanvasText;
      --text-color-danger: CanvasText;
      --border-color: CanvasText;
      --color-accent: ButtonFace;
      --color-danger: ButtonFace;
      --text-color-on-accent: ButtonText;
      --focus-outline-color: Highlight;
    }

    input:is([type="checkbox"], [type="radio"]) {
      accent-color: auto;
    }

    :any-link {
      color: LinkText;
    }
  }
}

@layer components {
  .page {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 2rem;
    max-inline-size: 40rem;
    margin-inline: auto;
    padding-block: 3rem;
  }

  .page__title {
    font-size: 1.5rem;
    line-height: 2rem;
    font-weight: 500;
    text-wrap: balance;
  }

  .page__loading {
    color: var(--text-color-muted);
  }

  .section {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
    padding: min(1.5rem, 6%);
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius) * 1.5);
    background: var(--background-color-surface);
  }

  .section__heading {
    font-size: 1rem;
    font-weight: 500;
    text-wrap: balance;
  }

  .section__description {
    color: var(--text-color-muted);
    font-size: 0.875rem;
    text-wrap: pretty;
  }

  .empty-state {
    padding-block: 1.5rem;
    color: var(--text-color-muted);
    text-align: center;
  }

  .empty-state__title {
    font-weight: 500;
  }

  .empty-state__description {
    font-size: 0.875rem;
    text-wrap: pretty;
  }

  :where(.button) {
    justify-self: start;
  }

  .button {
    position: relative;
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    justify-content: center;
    min-block-size: var(--control-height);
    padding-inline: 1rem;
    border: 1px solid transparent;
    border-radius: var(--border-radius);
    color: var(--button-color);
    font-family: inherit;
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    transition-property: background-color, color, opacity;
    transition-duration: 150ms;
    transition-timing-function: ease-out;

    &:disabled {
      cursor: default;
      opacity: 0.5;
      transition-delay: 150ms;
    }
  }

  .button--primary {
    --button-color: var(--text-color-on-accent);
    background: var(--color-accent);

    @media (hover: hover) {
      &:hover:not(:disabled) {
        background: color-mix(in oklch, var(--color-accent) 90%, transparent);
      }
    }
  }

  .button--quiet {
    --button-color: var(--text-color);
    background: transparent;

    @media (hover: hover) {
      &:hover:not(:disabled) {
        background: color-mix(in oklch, var(--text-color) 5%, transparent);
      }
    }
  }

  .button--danger {
    --button-color: white;
    background: var(--color-danger);

    @media (hover: hover) {
      &:hover:not(:disabled) {
        background: color-mix(in oklch, var(--color-danger) 90%, transparent);
      }
    }
  }

  .button[aria-busy="true"],
  .form[aria-busy="true"] .button[type="submit"] {
    color: transparent;
    cursor: progress;
    transition-delay: 150ms;

    &::after {
      position: absolute;
      inset: 0;
      inline-size: 1rem;
      block-size: 1rem;
      margin: auto;
      border: 2px solid var(--button-color);
      border-block-start-color: transparent;
      border-radius: 50%;
      content: "";
      animation:
        reveal 150ms ease-out 150ms backwards,
        button-spin 0.6s linear infinite;

      @media (prefers-reduced-motion: reduce) {
        animation: reveal 0s 150ms backwards;
      }

      @media (forced-colors: active) {
        display: none;
      }
    }
  }

  .actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .input {
    min-inline-size: 0;
    min-block-size: var(--control-height);
    padding-block: calc(0.5rem - 1px);
    padding-inline: 0.75rem;
    border: 1px solid var(--text-color-muted);
    border-radius: var(--border-radius);
    background: var(--background-color-surface);
    color: var(--text-color);
    font: inherit;
    font-size: 1rem;

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

      @media (forced-colors: active) {
        color: GrayText;
      }
    }

    &[aria-invalid="true"] {
      border-color: var(--text-color-danger);
    }

    &:disabled {
      opacity: 0.5;
    }
  }

  .field {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.75rem;

    &:has(:is(input, select, textarea):disabled) .field__label {
      opacity: 0.5;
    }
  }

  .field__label {
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
  }

  .field__description {
    margin-block-start: -0.25rem;
    color: var(--text-color-muted);
    font-size: 0.875rem;
    line-height: 1.25rem;
  }

  .field__controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
  }

  .field__controls > .input {
    flex: 1 1 12rem;
  }

  .field__error {
    color: var(--text-color-danger);
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
  }

  .form {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
  }

  .list {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
  }

  .row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
    justify-content: space-between;
    padding-block: 0.75rem;

    & + & {
      border-block-start: 1px solid var(--border-color);
    }
  }

  .row__main {
    min-inline-size: 0;
  }

  .row__title {
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
  }

  .row__meta {
    color: var(--text-color-muted);
    font-size: 0.875rem;
  }

  .row__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  @keyframes button-spin {
    to {
      rotate: 1turn;
    }
  }
}

@layer utilities {
  .visually-hidden {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    border: 0;
    white-space: nowrap;
  }

  .delayed-reveal {
    animation: reveal 200ms ease-out 300ms backwards;

    @media (prefers-reduced-motion: reduce) {
      animation-duration: 0s;
    }
  }

  @keyframes reveal {
    from {
      opacity: 0;
    }
  }
}
