/* Stepper: ol[data-stepper] with aria-current="step" on the current li (or its link) */

:root,
:host,
[data-theme] {
  --pico-stepper-marker-size: 2rem;
  --pico-stepper-line-width: 0.125rem;
  --pico-stepper-gap: 0.5rem;
  --pico-stepper-ring-width: var(--pico-outline-width);
  --pico-stepper-line-color: var(--pico-muted-border-color);
  --pico-stepper-label-color: var(--pico-muted-color);
  --pico-stepper-complete-label-color: var(--pico-color);
  --pico-stepper-current-color: var(--pico-color);
  --pico-stepper-current-ring-color: var(--pico-primary-background);
  --pico-stepper-complete-background: var(--pico-primary-background);
  --pico-stepper-complete-color: var(--pico-primary-inverse);
  --pico-stepper-complete-line-color: var(--pico-primary-background);
}

ol[data-stepper] {
  display: flex;
  inline-size: 100%;
  margin: 0 0 var(--pico-spacing);
  padding: 0;
  list-style: none;

  /* The nav module's list spacing does not apply */
  nav > & {
    margin-bottom: 0;
  }

  & > li {
    /* State lives in these, so the marker, label and line rules are written once */
    --_marker-background: var(--pico-background-color);
    --_marker-color: var(--pico-stepper-label-color);
    --_marker-ring: inset 0 0 0 var(--pico-stepper-line-width) var(--pico-form-element-border-color);
    --_marker-icon: none;
    --_label-color: var(--pico-stepper-label-color);
    --_label-weight: inherit;
    --_line-color: var(--pico-stepper-line-color);

    position: relative;
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    gap: var(--pico-stepper-gap);
    align-items: center;
    min-inline-size: 0;
    margin: 0;
    padding: 0;
    color: var(--_label-color);
    font-weight: var(--_label-weight);
    font-size: 0.875em;
    line-height: 1.25;
    text-align: center;
    overflow-wrap: break-word;

    /* Keeps list semantics in VoiceOver with list-style: none, as the nav module does */
    &::before {
      position: absolute;
      float: none;
      content: "\200B";
    }

    & > a {
      display: flex;
      flex-direction: column;
      gap: var(--pico-stepper-gap);
      align-items: center;
      min-inline-size: 0;
      margin: 0;
      padding: 0;
      border-radius: var(--pico-border-radius);
      color: inherit;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
        text-underline-offset: var(--pico-text-underline-offset);
      }

      &:focus-visible {
        outline: var(--pico-outline-width) solid var(--pico-primary-focus);
        outline-offset: 0.25rem;
      }

      &[aria-disabled="true"] {
        opacity: var(--pico-form-element-disabled-opacity);
        pointer-events: none;
      }
    }

    /* Marker */
    & > [aria-hidden="true"],
    & > a > [aria-hidden="true"] {
      z-index: 1;
      display: grid;
      flex: none;
      place-items: center;
      inline-size: var(--pico-stepper-marker-size);
      block-size: var(--pico-stepper-marker-size);
      border-radius: 50%;
      background: var(--_marker-icon) center / 0.875rem no-repeat var(--_marker-background);
      box-shadow: var(--_marker-ring);
      color: var(--_marker-color);
      font-weight: 600;
      font-size: 0.875rem;
      font-variant-numeric: tabular-nums;
      line-height: 1;
      transition:
        background-color var(--pico-transition),
        box-shadow var(--pico-transition);
    }

    /* Line to the next step, from marker edge to marker edge */
    &:not(:last-child)::after {
      position: absolute;
      inset-block-start: calc(
        (var(--pico-stepper-marker-size) - var(--pico-stepper-line-width)) / 2
      );
      inset-inline: calc(50% + var(--pico-stepper-marker-size) / 2 + var(--pico-stepper-gap))
        calc(var(--pico-stepper-marker-size) / 2 + var(--pico-stepper-gap) - 50%);
      block-size: var(--pico-stepper-line-width);
      border-radius: var(--pico-stepper-line-width);
      background-color: var(--_line-color);
      content: "";
    }

    /* Completed: a step before the current one */
    /* Completed: a check mark instead of the number */
    &:has(~ li[aria-current="step"], ~ li > [aria-current="step"]) {
      --_marker-background: var(--pico-stepper-complete-background);
      --_marker-color: transparent;
      --_marker-ring: 0 0 0 0 transparent;
      --_marker-icon: var(--pico-icon-checkbox);
      --_label-color: var(--pico-stepper-complete-label-color);
      --_line-color: var(--pico-stepper-complete-line-color);
    }

    /* Current */
    &[aria-current="step"],
    &:has(> [aria-current="step"]) {
      --_marker-color: var(--pico-primary);
      --_marker-ring:
        inset 0 0 0 var(--pico-stepper-ring-width) var(--pico-stepper-current-ring-color),
        0 0 0 0.25rem color-mix(in oklab, var(--pico-stepper-current-ring-color) 20%, transparent);
      --_label-color: var(--pico-stepper-current-color);
      --_label-weight: 600;
    }
  }

  /* Vertical: marker at the inline start, line runs down the marker's center */
  &[data-stepper="vertical"] {
    flex-direction: column;
  }
}

/* Vertical layout, written once for "vertical" and again inside the responsive query */
ol[data-stepper="vertical"] > li {
  flex: none;
  flex-direction: row;
  align-items: flex-start;
  padding-block-end: max(1.5rem, calc(var(--pico-spacing) * 1.75));
  line-height: var(--pico-stepper-marker-size);
  text-align: start;

  &:last-child {
    padding-block-end: 0;
  }

  & > a {
    flex-direction: row;
    align-items: flex-start;
    align-self: auto;
  }

  &:not(:last-child)::after {
    inset-block: calc(var(--pico-stepper-marker-size) + var(--pico-stepper-gap) / 2)
      calc(var(--pico-stepper-gap) / 2);
    inset-inline: calc((var(--pico-stepper-marker-size) - var(--pico-stepper-line-width)) / 2) auto;
    block-size: auto;
    inline-size: var(--pico-stepper-line-width);
  }
}

@media (width < 576px) {
  ol[data-stepper="responsive"] {
    flex-direction: column;

    & > li {
      flex: none;
      flex-direction: row;
      align-items: flex-start;
      padding-block-end: max(1.5rem, calc(var(--pico-spacing) * 1.75));
      line-height: var(--pico-stepper-marker-size);
      text-align: start;

      &:last-child {
        padding-block-end: 0;
      }

      & > a {
        flex-direction: row;
        align-items: flex-start;
        align-self: auto;
      }

      &:not(:last-child)::after {
        inset-block: calc(var(--pico-stepper-marker-size) + var(--pico-stepper-gap) / 2)
          calc(var(--pico-stepper-gap) / 2);
        inset-inline: calc((var(--pico-stepper-marker-size) - var(--pico-stepper-line-width)) / 2)
          auto;
        block-size: auto;
        inline-size: var(--pico-stepper-line-width);
      }
    }
  }
}
