@layer components {
  /* Centered title with space for two buttons on either side */
  .header {
    --header-gap: 0.5ch;
    --btn-icon-size: 1rem;
    --header-btn-size: 2rem;
    --header-button-count: 0;
    --header-actions-width: calc((var(--header-btn-size) + var(--header-gap)) * var(--header-button-count));

    display: grid;
    grid-template-columns: var(--header-actions-width) 1fr var(--header-actions-width);
    grid-template-areas:
      "menu menu menu"
      "actions-start title actions-end"
      "subnav subnav subnav";
    max-inline-size: 100dvw;
    padding-block: calc(var(--block-space-half) + var(--custom-safe-inset-top)) var(--block-space-half);
    padding-inline: var(--main-padding);
    position: relative;
    z-index: var(--z-nav);

    /* Change the grid size depending on how many buttons are present */
    &:has(.header__actions > *:nth-child(1)) { --header-button-count: 1; }
    &:has(.header__actions > *:nth-child(2)) { --header-button-count: 2; }
    &:has(.header__actions > *:nth-child(3)) { --header-button-count: 3; }

    &:has(.app-shell__nav),
    &:has(.header__subnav > *) {
      row-gap: 0;
    }

    &:has(dialog[open]) {
      z-index: var(--z-nav-open);
    }

    &:has(~ #main .card-columns) {
      inline-size: 100dvw;
      margin-inline: auto;
      max-inline-size: var(--main-width);
    }

    .app-shell__nav {
      grid-area: menu;
      margin-inline: auto;
    }
  }

  .header__actions {
    display: flex;
    font-size: var(--text-x-small);
    gap: var(--header-gap);
    inline-size: var(--header-actions-width);
  }

  .header__actions--start {
    grid-area: actions-start;
    margin-inline-end: auto;
  }

  .header__actions--end {
    grid-area: actions-end;
    justify-content: flex-end;
    margin-inline-start: auto;
  }

  .header__title {
    color: inherit;
    font-size: var(--text-large);
    font-weight: 900;
    grid-area: title;
    margin: 0 auto;
    min-inline-size: 0;
    text-align: center;
  }

  .header__subnav {
    grid-area: subnav;
    margin-block-start: 0.375rem;
    margin-inline: auto;
    inline-size: min(100%, var(--main-width));
  }

  .header__subnav:has(.workspace-subnav) {
    margin-block-start: 0;
  }

  .header__skip-navigation {
    --left-offset: -999em;

    inset-block-start: 4rem;
    inset-inline-start: var(--left-offset);
    position: absolute;
    white-space: nowrap;
    z-index: 11;

    &:focus {
      --left-offset: var(--inline-space);
    }
  }

  .app-shell__nav {
    align-items: center;
    display: flex;
    gap: 0.5rem;
    inline-size: auto;
    justify-content: center;
    margin-inline: auto;
    max-inline-size: 100%;
  }

  .app-shell__nav-trigger {
    align-items: center;
    justify-content: center;
    min-inline-size: 0;
  }

  .app-shell__nav-trigger > span,
  .app-shell__nav-trigger > svg,
  .app-shell__nav-trigger > .icon {
    align-self: center;
    flex: 0 0 auto;
  }

  .app-shell__nav-trigger span :is(img, svg) {
    display: block;
    height: 100%;
    width: 100%;
  }

  .app-shell__nav-icon {
    overflow: visible;
  }

  .app-shell__nav-trigger .app-shell__nav-wordmark {
    display: block;
    height: 100%;
    margin-inline-start: 0.45em;
    margin-inline-end: 0.2em;
    max-height: 1em;
    overflow: visible;
    transform: translateY(0);
    width: auto;
  }

  .app-shell__nav-wordmark text {
    fill: currentColor;
    font-family: "Segoe UI Variable Fizzy", var(--font-sans);
    font-size: 30px;
    font-weight: 800;
    letter-spacing: -0.04em;
  }

  @media (max-width: 960px) {
    .header--flush-bottom {
      background: var(--color-canvas);
      padding-block-end: 0;
    }

    .header:has(.workspace-subnav) {
      background: color-mix(in srgb, var(--color-canvas) 92%, transparent);
      backdrop-filter: blur(16px);
    }

    .header__subnav:has(.workspace-subnav) {
      margin-block-start: 0.375rem;
    }

    .header:has(.workspace-subnav) .app-shell__nav {
      align-items: center;
      flex-direction: row;
      gap: 0.4rem;
    }
  }

  /* Optional class to stack header actions on small screens
  /* ------------------------------------------------------------------------ */

    .header--mobile-actions-stack {
      @media (max-width: 639px) {
        grid-template-areas:
          "actions-start menu actions-end"
          "title title title"
          "subnav subnav subnav";

        .header__title {
          margin-block-start: 0.25rem;
        }

        &:has(.workspace-subnav) .app-shell__nav {
          align-items: center;
          flex-direction: row;
        }
      }
    }
}
