/* GachaKit header capsule, copied from thayn.me (styles.css, "Capsule: header")
   so the nav behaves the same: sliding pill on hover, popover menu on phones */

.nav-pill-wrap {
  display: inline-flex;
  align-items: stretch;
  gap: var(--spacing-sm);
}

.nav-pill__theme {
  display: flex;
  align-items: stretch;

  & .theme-toggle {
    height: 100%;
    margin: 0;
  }
}

.nav-pill {
  background:
    linear-gradient(135deg, var(--glass-grad-start), var(--glass-grad-end)),
    var(--bg-main);
  backdrop-filter: var(--bg-blur) var(--bg-saturation);
  border-radius: var(--radius-full);
  box-shadow:
    var(--shadow-soft),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
  padding: calc(var(--spacing-xs) + 2px) 8px;
  display: inline-block;
  border: 1px solid var(--glass-border);
  position: relative;
}

.nav-pill__list {
  /* A popover only below 720px; up here it is the plain inline list */
  position: static;
  inset: auto;
  width: auto;
  height: auto;
  overflow: visible;
  border: 0;
  background: none;
  color: inherit;
  list-style: none;
  display: flex;
  --nav-pill-gap: calc(var(--spacing-sm) + 2px);
  gap: var(--nav-pill-gap);
  margin: 0;
  padding: 0;
  align-items: center;
  flex-wrap: nowrap;
}

.nav-pill__item {
  display: block;
  flex: 0 0 auto;
}

.nav-pill__toggle {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  color: var(--text);
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-full);
  position: relative;
  z-index: 1;
  transition: color var(--transition-normal);
  overflow: hidden;
  text-align: center;
  background: transparent;
  border: none;
  cursor: pointer;
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

.nav-pill__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.nav-pill__link {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  color: var(--text);
  text-decoration: none;
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-full);
  position: relative;
  z-index: 1;
  transition: color var(--transition-normal);
  overflow: hidden;
  text-align: center;
  min-width: var(--nav-item-width);
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;

  & i {
    font-size: 1.25rem;
  }
}

.nav-pill__link::before,
.nav-pill__toggle::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--nav-highlight);
  background-blend-mode: normal;
  border-radius: var(--radius-full);
  z-index: -1;
  opacity: 0;
  transition: opacity var(--transition-normal);
}

:is(.nav-pill__link, .nav-pill__toggle):is(:hover, :focus-visible, :active) {
  color: var(--text);
}

:is(.nav-pill__link, .nav-pill__toggle):is(
    :hover,
    :focus-visible,
    :active
  )::before,
.nav-pill__link.is-active::before,
.nav-pill:has(.nav-pill__list:popover-open) .nav-pill__toggle::before {
  opacity: 1;
}

:is(.nav-pill__link, .nav-pill__toggle):is(
    :hover,
    :focus-visible,
    :active
  )::after,
.nav-pill__link.is-active::after,
.nav-pill:has(.nav-pill__list:popover-open) .nav-pill__toggle::after {
  opacity: 1;
}

:is(.nav-pill__link, .nav-pill__toggle):active::before,
:is(.nav-pill__link, .nav-pill__toggle):active::after {
  transition-duration: 0s;
}

.nav-pill__link::after,
.nav-pill__toggle::after {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: var(--radius-full);
  border: 1px solid var(--nav-highlight-border);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
  opacity: 0;
  transition: opacity var(--transition-normal);
  pointer-events: none;
}

.nav-pill__text {
  font-size: 0.7rem;
  white-space: nowrap;
  line-height: 1;
}

/* The pill slides to the hovered or focused link and rests on the current
   one. Links share one column width, so the pill moves by whole columns;
   the index rules cover up to eight links. */

@supports selector(:has(a)) {
  @media (width > 720px) {
    .nav-pill__list {
      position: relative;
      display: grid;
      grid-auto-flow: column;
      grid-auto-columns: 1fr;
    }

    .nav-pill__list:has(> :nth-child(1) > .is-active) {
      --nav-pill-index: 0;
    }
    .nav-pill__list:has(> :nth-child(2) > .is-active) {
      --nav-pill-index: 1;
    }
    .nav-pill__list:has(> :nth-child(3) > .is-active) {
      --nav-pill-index: 2;
    }
    .nav-pill__list:has(> :nth-child(4) > .is-active) {
      --nav-pill-index: 3;
    }
    .nav-pill__list:has(> :nth-child(5) > .is-active) {
      --nav-pill-index: 4;
    }
    .nav-pill__list:has(> :nth-child(6) > .is-active) {
      --nav-pill-index: 5;
    }
    .nav-pill__list:has(> :nth-child(7) > .is-active) {
      --nav-pill-index: 6;
    }
    .nav-pill__list:has(> :nth-child(8) > .is-active) {
      --nav-pill-index: 7;
    }

    /* Later and of equal weight, so a hovered link beats the current one */
    .nav-pill__list:has(> :nth-child(1) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 0;
    }
    .nav-pill__list:has(> :nth-child(2) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 1;
    }
    .nav-pill__list:has(> :nth-child(3) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 2;
    }
    .nav-pill__list:has(> :nth-child(4) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 3;
    }
    .nav-pill__list:has(> :nth-child(5) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 4;
    }
    .nav-pill__list:has(> :nth-child(6) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 5;
    }
    .nav-pill__list:has(> :nth-child(7) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 6;
    }
    .nav-pill__list:has(> :nth-child(8) > :is(:hover, :focus-visible)) {
      --nav-pill-index: 7;
    }

    .nav-pill__list::before,
    .nav-pill__list::after {
      content: "";
      position: absolute;
      grid-area: 1 / 1 / 2 / 2;
      inset: 0;
      border-radius: var(--radius-full);
      pointer-events: none;
      translate: calc(var(--nav-pill-index, 0) * (100% + var(--nav-pill-gap))) 0;
      transition:
        translate 0.35s cubic-bezier(0.22, 1, 0.36, 1),
        opacity 0.2s ease;
    }

    .nav-pill__list::before {
      backdrop-filter: blur(8px) saturate(120%);
      -webkit-backdrop-filter: blur(8px) saturate(120%);
      background: linear-gradient(
        to bottom,
        var(--glass-highlight),
        var(--nav-highlight)
      );
      box-shadow:
        inset 0 1px 0 var(--glass-highlight),
        0 1px 4px rgba(0, 0, 0, 0.05);
    }

    .nav-pill__list::after {
      border: 1px solid transparent;
      background: linear-gradient(
          to bottom,
          var(--glass-highlight),
          transparent 40%,
          transparent 65%,
          var(--glass-edge)
        )
        border-box;
      -webkit-mask:
        linear-gradient(#fff 0 0) padding-box,
        linear-gradient(#fff 0 0);
      mask:
        linear-gradient(#fff 0 0) padding-box,
        linear-gradient(#fff 0 0);
      -webkit-mask-composite: xor;
      mask-composite: exclude;
    }

    .nav-pill__list:not(
        :has(> li > :is(.is-active, :hover, :focus-visible))
      )::before,
    .nav-pill__list:not(
        :has(> li > :is(.is-active, :hover, :focus-visible))
      )::after {
      opacity: 0;
    }

    .nav-pill__link.is-active,
    .nav-pill__link:is(:hover, :focus-visible) {
      color: var(--accent-secondary);
    }

    .nav-pill__list:has(> li > :is(:hover, :focus-visible))
      .nav-pill__link.is-active:not(:hover, :focus-visible) {
      color: var(--text);
    }

    .nav-pill__link::before,
    .nav-pill__link::after {
      display: none;
    }
  }
}

@media (width <= 720px) {
  .nav-pill__link.is-active {
    color: var(--accent-secondary);
  }

  .nav-pill-wrap {
    gap: var(--spacing-xs);
    width: 100%;
    justify-content: space-between;
  }

  .nav-pill {
    --nav-toggle-icon-size: 1.4rem;
    --nav-toggle-size: 62px;
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
    backdrop-filter: none;
    z-index: 50;
    order: 2;
    display: inline-flex;
    align-items: center;
  }

  .nav-pill__theme {
    order: 1;
  }

  .nav-pill__toggle {
    display: grid;
    place-items: center;
    height: var(--nav-toggle-size);
    width: var(--nav-toggle-size);
    padding: 0;
    border-radius: 9999px;
    box-sizing: border-box;
    line-height: 1;
    background:
      linear-gradient(135deg, var(--glass-grad-start), var(--glass-grad-end)),
      var(--bg-main);
    border: 1px solid var(--glass-border);
    box-shadow:
      var(--shadow-soft),
      inset 0 1px 0 rgba(255, 255, 255, 0.4);
    backdrop-filter: var(--bg-blur) var(--bg-saturation);
    -webkit-backdrop-filter: var(--bg-blur) var(--bg-saturation);

    & i {
      display: block;
      font-size: var(--nav-toggle-icon-size);
      line-height: 1;
      transform: translateY(0.02em);
    }
  }

  .nav-pill__toggle {
    anchor-name: --nav-pill-toggle;
  }

  /* An open popover sits in the top layer, out of the nav's box, so it is
     placed against the toggle rather than the nav */
  .nav-pill__list {
    position: absolute;
    position-anchor: --nav-pill-toggle;
    inset: auto;
    top: calc(anchor(bottom) + var(--spacing-sm));
    right: anchor(right);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--spacing-xs);
    margin: 0;
    padding: var(--spacing-xs);
    background:
      linear-gradient(
        135deg,
        color-mix(
          in srgb,
          var(--glass-grad-start) 82%,
          var(--glass-highlight) 18%
        ),
        color-mix(
          in srgb,
          var(--glass-grad-end) 80%,
          var(--glass-highlight) 20%
        )
      ),
      color-mix(in srgb, var(--bg-main) 82%, var(--glass-highlight) 18%);
    backdrop-filter: blur(18px) saturate(180%);
    -webkit-backdrop-filter: blur(18px) saturate(180%);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    box-shadow:
      var(--shadow-soft),
      inset 0 1px 0 rgba(255, 255, 255, 0.35);
    min-width: 220px;
    max-width: min(320px, calc(100lvw - 2 * var(--spacing-md)));
    width: max-content;
    opacity: 0;
    transform: translateY(-8px) scale(0.97);
    transform-origin: top right;
    /* Also hidden where :popover-open is unknown and the rules below drop */
    visibility: hidden;
    pointer-events: none;
    transition:
      opacity var(--transition-normal),
      transform var(--transition-normal),
      visibility var(--transition-normal) allow-discrete,
      overlay var(--transition-normal) allow-discrete,
      display var(--transition-normal) allow-discrete;
  }

  .nav-pill__list.is-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    visibility: visible;
    pointer-events: auto;
  }

  .nav-pill__list:not(:popover-open) {
    display: none;
  }

  .nav-pill__list:popover-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    visibility: visible;
    pointer-events: auto;
  }

  @starting-style {
    .nav-pill__list:popover-open {
      opacity: 0;
      transform: translateY(-8px) scale(0.97);
    }
  }

  @supports not (anchor-name: --a) {
    .nav-pill__list {
      position: fixed;
      top: calc(
        env(safe-area-inset-top) + var(--header-gap) +
          var(--container-padding) + var(--nav-toggle-size) + var(--spacing-sm)
      );
      right: calc(env(safe-area-inset-right) + var(--container-padding));
    }
  }

  .nav-pill__toggle .nav-pill__icon-close,
  .nav-pill:has(.nav-pill__list:is(:popover-open, .is-open))
    .nav-pill__toggle
    .nav-pill__icon-menu {
    display: none;
  }

  .nav-pill:has(.nav-pill__list:is(:popover-open, .is-open))
    .nav-pill__toggle
    .nav-pill__icon-close {
    display: block;
  }

  .nav-pill__link {
    width: 100%;
    flex-direction: row;
    justify-content: flex-start;
    text-align: left;
    gap: var(--spacing-sm);
    padding: 0.5rem 0.9rem;

    & i {
      font-size: 1.15rem;
    }
  }

  .nav-pill__text {
    font-size: 0.9rem;
    white-space: normal;
  }

  .theme-toggle__button,
  .theme-toggle[data-compact="true"] .theme-toggle__button {
    min-width: var(--nav-item-width-sm);
  }
}

@media (forced-colors: active) {
  /* The default inset of -1px sits outside the link's overflow clip */
  .nav-pill__link::after {
    inset: 0;
  }
}

@media (forced-colors: active) and (width <= 720px) {
  .nav-pill__list {
    background: Canvas;
  }
}

/* === Theme toggle (embedded in header) === */

.theme-toggle {
  background:
    linear-gradient(135deg, var(--glass-grad-start), var(--glass-grad-end)),
    var(--bg-main);
  border: 1px solid var(--glass-border);
  position: relative;
  border-radius: var(--radius-full);
  padding: 6px 8px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  box-shadow:
    var(--shadow-soft),
    inset 0 1px 0 var(--glass-highlight);
  transition:
    transform var(--transition-fast),
    box-shadow var(--transition-fast);
  backdrop-filter: var(--bg-blur) var(--bg-saturation);
  -webkit-backdrop-filter: var(--bg-blur) var(--bg-saturation);

  &::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    border: 1px solid transparent;
    background: linear-gradient(
        135deg,
        var(--glass-highlight),
        transparent 45%,
        transparent 60%,
        var(--glass-edge)
      )
      border-box;
    -webkit-mask:
      linear-gradient(#fff 0 0) padding-box,
      linear-gradient(#fff 0 0);
    mask:
      linear-gradient(#fff 0 0) padding-box,
      linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
    opacity: 0.8;
  }

  &:hover {
    transform: none;
    box-shadow:
      var(--shadow-soft),
      inset 0 1px 0 var(--glass-highlight);
  }
}

.theme-toggle__button {
  height: 100%;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text);
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  padding: 0 0.9rem;
  position: relative;
  overflow: hidden;
  min-width: var(--nav-item-width);
  box-sizing: border-box;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--transition-fast);

  &::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: linear-gradient(
      to bottom,
      var(--glass-highlight),
      var(--nav-highlight)
    );
    box-shadow:
      inset 0 1px 0 var(--glass-highlight),
      0 1px 4px rgba(0, 0, 0, 0.05);
    opacity: 0;
    transition: opacity var(--transition-normal);
    z-index: -1;
  }

  &::after {
    content: "";
    position: absolute;
    inset: -1px;
    border-radius: inherit;
    border: 1px solid var(--nav-highlight-border);
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
    opacity: 0;
    transition: opacity var(--transition-normal);
    pointer-events: none;
    z-index: -1;
  }

  &:is(:hover, :focus-visible, :active) {
    color: var(--accent-secondary);
  }

  &:is(:hover, :focus-visible, :active)::before,
  &:is(:hover, :focus-visible, :active)::after {
    opacity: 1;
  }

  &:active::before,
  &:active::after {
    transition-duration: 0s;
  }
}

.theme-toggle__icon {
  font-size: 1.2rem;
  position: relative;
  z-index: 1;
}

.theme-toggle__text {
  font-weight: 600;
  font-size: 0.65rem;
  line-height: 1;
  position: relative;
  z-index: 1;
}

.theme-toggle[data-compact="true"] {
  & .theme-toggle__button {
    min-width: var(--nav-item-width);
    padding: 0 0.9rem;
  }

  & .theme-toggle__icon {
    font-size: 1.3rem;
  }
}
