/* How far the bar has ridden up into the strip's row: 0 at the top of the page, 1 once it has scrolled. */
@property --bar-merge {
  inherits: true;
  initial-value: 0;
  syntax: "<number>";
}

@keyframes bar-merge {
  to { --bar-merge: 1; }
}

@layer components {
  /* The top bar: the mark at the start, the jump field in the middle, messages at the end. The sides share what the field leaves, so it stays centred. */
  .site-header {
    align-items: center;
    column-gap: var(--header-gap, var(--inline-space-half));
    display: grid;
    grid-template-areas: "mark jump messages";
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  }

  .header:has(> .site-header) {
    --header-btn-size: 2.75rem;

    align-items: center;
    row-gap: 0;

    /* header.css sizes the action columns for icon circles; from 640 up ours carry a label, so the sides take their content and stay equal while there is room. */
    @media (width >= 640px) {
      --header-actions-width: auto;

      column-gap: var(--header-gap);
      grid-template-columns: 1fr fit-content(50%) 1fr;
    }
  }

  /* In the app the bar is 72px tall from 800 up and 48px on a phone, and stays at the top of the window. On a phone the strip scrolls away under it, so it keeps the ground as its fill and takes the strip's hairline once the page has scrolled. */
  body:not(.public) .header:has(> .site-header) {
    --top-bar-padding: 0.5rem; /* 8px */

    min-block-size: calc(var(--top-bar-height) + var(--custom-safe-inset-top));
    padding-block: var(--custom-safe-inset-top) 0;
    padding-inline:
      calc(var(--top-bar-padding) + var(--custom-safe-inset-left))
      calc(var(--top-bar-padding) + var(--custom-safe-inset-right));

    /* Over the strip, which comes after it, and still over it while one of the strip's menus raises it. */
    &:not(:has(dialog[open])) {
      z-index: calc(var(--z-nav) + 1);

      body:has(.strip dialog[open]) & {
        z-index: calc(var(--z-nav-open) + 1);
      }
    }

    @media (width < 800px) {
      background-color: var(--color-page);
      box-shadow: 0 1px 0 color-mix(in oklch, var(--color-ink-lighter) calc(var(--bar-merge) * 100%), transparent);
      inset-block-start: 0;
      position: sticky;
    }

    @media (width >= 800px) {
      --top-bar-padding: 0.42rem;
    }
  }

  /* From 800 up the bar rides up until its row is the strip's and stays there: the mark and messages in the gutters, the field in the strip's middle, the two one band. Only where the browser can tell how far the page has scrolled; elsewhere the bar scrolls away. */
  @supports (animation-timeline: scroll()) {
    body:not(.public) :is(.header:has(> .site-header), .sheet > .strip) {
      animation: bar-merge linear both;
      animation-range: 0 1.25rem;
      animation-timeline: scroll(root);
    }

    /* The pointer reaches the strip between the bar's three pieces. */
    @media (width >= 800px) {
      body:not(.public) .header:has(> .site-header) {
        inset-block-start: calc((var(--strip-height) - var(--top-bar-height)) / 2);
        pointer-events: none;
        position: sticky;

        .header__skip-navigation,
        .site-header > * {
          pointer-events: auto;
        }
      }
    }

    /* Beside the open messages sidebar a window under 1200 leaves the strip no room for the field, so there the bar scrolls away with the page. */
    @media (800px <= width < 1200px) {
      html[data-sidebar-open] body:has(> .messages-sidebar) {
        .header:has(> .site-header),
        .sheet > .strip,
        .sheet::before {
          animation: none;
        }

        .header:has(> .site-header) {
          inset-block-start: 0;
          position: relative;
        }
      }
    }
  }

  @media (width >= 640px) {
    .header__actions {
      font-size: var(--text-normal);
    }

    .header__actions > .popup-anchor > .btn--circle {
      --btn-size: var(--header-btn-size);
    }

    /* buttons.css makes a circle of any button with an aria-label and an icon, the label stacked on the icon. */
    :is(.header__actions, .site-header) .btn--circle-mobile[aria-label] {
      --btn-gap: var(--inline-space);
      --btn-padding: 0 1.1em;
      --icon-size: var(--btn-icon-size, 1.3em);

      aspect-ratio: auto;
      block-size: auto;
      display: inline-flex;
      inline-size: auto;
      justify-content: center;
      min-block-size: var(--header-btn-size);
      white-space: nowrap;
    }
  }

  /* The circle the header's columns are sized for. */
  @media (width < 640px) {
    :is(.header__actions, .site-header) .btn--circle-mobile {
      --btn-size: var(--header-btn-size);
    }
  }

  .header .header__title {
    font-size: var(--text-x-large);
    font-weight: 800;
    letter-spacing: -0.024em;
    line-height: 1.15;
  }

  /* With the title and actions in the strip, the header keeps the top bar alone. */
  .header:has(> .site-header):not(:has(.header__title, .header__actions)) {
    grid-template-areas: "menu menu menu";
  }

  /* Spanning the header's columns, its content would hold their width against the title's. */
  .header > .site-header {
    contain: inline-size;
    grid-area: menu;
  }

  .site-header__brand {
    align-items: center;
    display: inline-flex;
    gap: var(--inline-space-half);
    grid-area: brand;
    justify-content: center;
    min-inline-size: 0;
  }

  /* As tall as a small button: one line of its small text and its padding. */
  .site-header__logo,
  .site-header__account-logo {
    block-size: calc(1lh + 1em);
    font-size: var(--text-small);
    border-radius: var(--radius-lg);
    display: block;
    inline-size: auto;
  }

  .site-header__account-logo {
    border-radius: var(--radius-md);
    flex-shrink: 1;
    min-inline-size: 0;
    object-fit: contain;
  }

  .public .site-header__logo {
    border-radius: var(--radius-md);
  }

  /* Without a signed-in user the account's mark leads, and a course taker's one section follows at the end; the mark gives way first. */
  .site-header:has(> .site-header__brand) {
    grid-template-areas: "brand messages";
    grid-template-columns: minmax(0, 1fr) auto;

    .site-header__brand {
      justify-self: start;
    }
  }

  .header .site-header__nav {
    grid-area: messages;
    justify-self: end;
    margin-inline: 0;
  }

  .site-header__list {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .site-header__link {
    --btn-background: transparent;
    --btn-border-size: 0;
    --btn-hover-amount: 8%;
    --btn-padding: 0.5em 0.75em;

    white-space: nowrap;

    &[aria-current="page"],
    &[aria-expanded="true"] {
      --btn-background: var(--color-ink-lightest);
    }
  }

  /* The mark goes home: the icon and the name, 32px tall. Where the bar leaves the name no room beside the icon,
     on a phone or beside the open messages, it wraps under the icon and out of sight, still the link's name. */
  .site-header__mark {
    align-content: flex-start;
    align-items: center;
    block-size: var(--control-size);
    border-radius: var(--radius-lg);
    color: var(--color-ink);
    display: inline-flex;
    flex-wrap: wrap;
    font-size: var(--text-section);
    font-weight: 800;
    gap: 0.4em;
    grid-area: mark;
    justify-self: start;
    letter-spacing: -0.02em;
    max-inline-size: 100%;
    min-inline-size: 0;
    overflow: hidden;
    text-decoration: none;
    white-space: nowrap;

    .site-header__logo {
      block-size: var(--control-size);
      flex: none;
    }

    /* The name gives way as the bar merges: the icon alone fits the gutter beside the sheet. */
    @media (width >= 800px) {
      .site-header__wordmark {
        max-inline-size: calc((1 - var(--bar-merge)) * 8em);
        opacity: calc(1 - 2 * var(--bar-merge));
        overflow-x: clip;
      }
    }
  }

  /* Beside the open messages a window under 960 leaves the bar a phone's room, so the field is the phone's. */
  @media (640px <= width < 960px) {
    html[data-sidebar-open] body:has(> .messages-sidebar) {
      --jump-field-size: 11.5rem;   /* 185px */
    }
  }

  @media (800px <= width < 960px) {
    html[data-sidebar-open] body:has(> .messages-sidebar) {
      --jump-field-size: 9.7rem;    /* 185px */
    }
  }

  /* The jump field: 300×40 from 960, 245×40 from 800, 245×32 from 640, up to 185×32 on a phone. Its width sits on the root, where the strip reads it to keep clear of the field. */
  :root {
    --jump-field-size: 11.5rem;               /* 185px */

    @media (width >= 640px) {
      --jump-field-size: 15.3rem;             /* 245px */
    }

    @media (width >= 800px) {
      --jump-field-size: 12.9rem;             /* 245px */
    }

    @media (width >= 960px) {
      --jump-field-size: 15.8rem;             /* 300px */
    }
  }

  .site-jump {
    --jump-field-height: var(--control-size); /* 32px */
    --jump-field-top: calc((var(--top-bar-height) - var(--jump-field-height)) / 2); /* 8px */
    --jump-panel-size: 33.75rem;              /* 540px */
    --jump-space: 0.5rem;                     /* 8px */

    grid-area: jump;
    min-inline-size: 0;
    position: relative;

    /* 16px from the top of the window, and 4px once the bar has ridden up into the strip's row. */
    @media (width >= 800px) {
      --jump-field-height: 2.1rem;            /* 40px */
      --jump-field-top: calc((var(--top-bar-height) - var(--jump-field-height)) / 2 - var(--bar-merge) * (var(--top-bar-height) - var(--strip-height)) / 2);
      --jump-panel-size: 28.4rem;             /* 540px */
      --jump-space: 0.42rem;                  /* 8px */
    }
  }

  .site-jump__field {
    --btn-background: color-mix(in oklch, var(--color-canvas) 70%, transparent);
    --btn-border-color: var(--color-ink-light);
    --btn-color: var(--color-ink-medium);
    --btn-gap: var(--jump-space);
    --btn-padding: 0 0.85em;
    --btn-hover-amount: 0%;

    block-size: var(--jump-field-height);
    font-size: var(--text-control);
    inline-size: var(--jump-field-size);
    justify-content: space-between;
    max-inline-size: 100%;

    @media (any-hover: hover) {
      &:hover {
        --btn-border-color: var(--color-ink-medium);
      }
    }

    kbd {
      flex: none;
    }

    .icon {
      --icon-size: 0.85em;

      flex: none;
    }

    /* The hint when wide, a chevron on a phone. */
    @media (width < 640px) {
      kbd {
        display: none;
      }
    }

    @media (width >= 640px) {
      .icon {
        display: none;
      }
    }
  }

  .site-jump__placeholder {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The panel comes down from the top of the window, its field where the bar's was, and dims the page around it. */
  .site-jump__panel[open] {
    --popup-item-padding-inline: calc(1.5 * var(--jump-space)); /* 12px */

    background-color: var(--color-canvas);
    border: 0;
    border-radius: 0 0 var(--radius-xl) var(--radius-xl);
    box-shadow:
      0 0 0 100vmax color-mix(in oklch, var(--color-always-black) 20%, transparent),
      var(--shadow-modal);
    color: var(--color-ink);
    display: flex;
    flex-direction: column;
    inline-size: min(var(--jump-panel-size), 100vw - 2 * var(--jump-space));
    inset: calc(-1 * var(--jump-field-top) - var(--custom-safe-inset-top)) auto auto 50%;
    margin: 0;
    max-block-size: calc(100dvh - var(--footer-height) - var(--custom-safe-inset-bottom) - var(--jump-space));
    max-inline-size: none;
    overflow: hidden;
    padding: 0;
    position: absolute;
    translate: -50%;
    z-index: var(--z-popup);

    /* On a phone, a sheet as wide as the window. */
    @media (width < 640px) {
      inline-size: 100%;
      inset: 0 0 auto;
      position: fixed;
      translate: none;
    }
  }

  /* The field and the sections stand on the ground's colour, over the list. */
  .site-jump__top {
    background-color: var(--color-page);
    box-shadow: 0 1px 0 var(--color-ink-lighter);
    flex: none;
    padding: calc(var(--jump-field-top) + var(--custom-safe-inset-top)) var(--popup-item-padding-inline) var(--popup-item-padding-inline);
    position: relative;

    @media (width < 640px) {
      padding-inline: var(--jump-space);
    }
  }

  /* The filter box of a list, as tall as the bar's field and in its 16px. A blue line and a soft glow say where the typing goes. */
  .site-jump__input:is(.input) {
    --focus-ring-size: 0;
    --input-border-color: var(--color-ink-light);
    --input-padding: 0 calc(7 * var(--jump-space)) 0 var(--popup-item-padding-inline); /* 0 56px 0 12px */

    block-size: var(--jump-field-height);
    display: block;
    font-size: max(16px, var(--text-control));

    &:focus {
      border-color: var(--color-link);
      box-shadow: 0 0 2px var(--color-link);
    }

    /* The bar's own 14px where its field is 32px tall and no finger would make the page zoom. */
    @media (640px <= width < 800px) and (any-hover: hover) {
      font-size: var(--text-control);
    }
  }

  /* The sections as quick tiles across the panel: a row of tiles as tall as the field, stacked tiles on a phone. Past header.css, which centres any nav in the header. */
  .site-jump__panel .site-jump__tiles {
    display: flex;
    gap: var(--jump-space);
    margin: var(--jump-space) 0 0;

    &:not(:has(.site-jump__tile:not([hidden]))) {
      display: none;
    }

    @media (width >= 800px) {
      margin-block-start: calc(2 * var(--jump-space)); /* 16px */
    }

    @media (width < 640px) {
      gap: calc(var(--jump-space) / 2);
    }
  }

  /* The list picks its first item as the panel opens, which is no one's doing: the pick rests on nothing until an arrow
     moves it, and while something is typed the first match takes it. */
  [data-filtering] .site-jump__rest {
    display: none;
  }

  /* Its fill is its own, so the tile of the section the page is in looks like the others. */
  .site-jump__tile {
    --btn-border-color: var(--color-ink-lighter);
    --btn-gap: 0.45em;
    --btn-padding: 0 0.85em;
    --icon-size: 1.15em;

    background-color: var(--tile-background, var(--color-canvas));
    flex: 1 1 auto;
    font-size: var(--text-control);
    font-weight: 500;
    min-block-size: var(--jump-field-height);
    min-inline-size: 0; /* gives way and cuts its name before it leaves the row */
    white-space: nowrap;

    @media (any-hover: hover) {
      &:hover {
        --tile-background: var(--color-ink-lightest);
      }
    }

    &[aria-selected] {
      --tile-background: var(--color-selected);
    }

    kbd {
      opacity: 0.66;
    }

    /* On a phone, big buttons: the icon over a 16px name. */
    @media (width < 640px) {
      --btn-gap: 0.3em;
      --btn-padding: 0 calc(var(--jump-space) / 2);
      --icon-size: 1.25rem; /* 20px */

      flex-direction: column;
      font-size: 1rem;
      font-weight: 400;
      min-block-size: 3.75rem; /* 60px */

      .overflow-ellipsis {
        max-inline-size: 100%;
      }
    }
  }

  /* Home, its icon alone in a square as tall as the others, and its name back once it is all that matches. Its key
     waits for the name: the square has no room for both. */
  .site-jump__panel:not([data-filtering]) .site-jump__tile--icon {
    flex: none;
    inline-size: var(--jump-field-height);

    @media (width < 640px) {
      inline-size: 3rem; /* 48px */
    }

    .overflow-ellipsis {
      block-size: 1px;
      clip-path: inset(50%);
      inline-size: 1px;
      position: absolute;
    }

    kbd {
      display: none;
    }
  }

  /* The results take the menu's place, under the field alone, as plain rows. */
  .site-jump__results[open] {
    --row-padding-inline: var(--popup-item-padding-inline);
    --rows-border-size: 0;
    --rows-margin: 0;
    --rows-shadow: none;

    background: none;
    border: 0;
    color: inherit;
    display: block;
    font-size: calc(var(--text-control) * 8 / 7); /* 16px */
    inline-size: auto;
    margin: 0;
    max-inline-size: none;
    min-block-size: 0;
    overflow: auto;
    overscroll-behavior: contain;
    padding: var(--jump-space) var(--popup-item-padding-inline) var(--popup-item-padding-inline);
    position: static;

    &:has([data-search-redirect]) {
      display: none;
    }

    .search {
      padding: 0;
    }

    .rows > .row {
      border-radius: var(--radius-lg);
    }
  }

  .site-jump__panel:has(.site-jump__results[open]) :is(.site-jump__tiles, .site-jump__menu) {
    display: none;
  }

  /* A group's name over its rows, and after it the way to the whole list. */
  .site-jump__heading {
    display: flex;
    font-size: var(--text-control);
    font-weight: 600;
    gap: 0.3em;
    line-height: 1.5;
    margin: 0 0 calc(var(--jump-space) / 2);
    padding: 0 var(--popup-item-padding-inline);

    a {
      color: inherit;
      font-weight: 400;
      text-decoration: none;

      &::before {
        content: "– ";
      }

      @media (any-hover: hover) {
        &:hover {
          text-decoration: underline;
        }
      }
    }
  }

  /* The menu under the tiles: each group under its name, each row a 36px line with its icon on a round badge. */
  .site-jump__menu {
    font-size: calc(var(--text-control) * 8 / 7); /* 16px */
    min-block-size: 0;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 0 var(--popup-item-padding-inline) calc(2.5 * var(--jump-space));

    .popup__section {
      padding-block-start: var(--popup-item-padding-inline);
    }

    .popup__list {
      row-gap: 0;
    }

    .popup__item {
      border-radius: var(--radius-lg);
    }

    .popup__btn {
      --btn-background: transparent;
      --btn-border-radius: var(--radius-lg);
      --btn-gap: var(--jump-space);
      --btn-hover-amount: 0%;

      min-block-size: calc(4.5 * var(--jump-space)); /* 36px */

      .overflow-ellipsis {
        min-inline-size: 0;
      }

      @media (any-pointer: coarse) {
        min-block-size: 2.75rem;
      }
    }
  }

  /* A menu that did not load says nothing: the tiles and what was visited still lead on. */
  .site-jump__menu .turbo-frame-error {
    display: none;
  }

  /* With no row to show, as on a page handed to a worker, the panel ends at its tiles. */
  .site-jump__panel:not([data-filtering]) .site-jump__menu:not(:has(.popup__section .popup__item)) {
    display: none;
  }

  /* 16px on a 28px round badge. */
  .site-jump__badge {
    --icon-size: calc(2 * var(--jump-space));

    background-color: var(--color-ink-lightest);
    block-size: calc(3.5 * var(--jump-space));
    border-radius: 50%;
    display: grid;
    flex: none;
    inline-size: calc(3.5 * var(--jump-space));
    place-items: center;
  }

  /* Where a visited page sits, in gray after its name. */
  .site-jump__parent {
    color: var(--color-text-subtle);
    font-size: var(--text-control);

    &:not(:empty)::before {
      content: "· ";
    }
  }

  /* People are too many to list under the menu, so they wait for something to be typed. */
  .site-jump__panel:not([data-filtering]) .site-jump__section--secret {
    display: none;
  }

  /* The way to the search closes the list once something is typed. */
  .site-jump__menu .site-jump__search {
    display: none;
    margin-block-start: var(--jump-space);

    [data-filtering] & {
      display: flex;
    }
  }

  /* Said under the tiles where nothing typed matches and there is no search to fall back on. */
  .site-jump__panel > .filter__empty {
    color: var(--color-text-subtle);
    font-size: var(--text-control);
    margin: 0;
    padding: var(--popup-item-padding-inline) calc(2 * var(--popup-item-padding-inline)) calc(2.5 * var(--jump-space));
  }

  /* Pings and what is new, as a 32px pill in 14/500. While something is unread it is pale apricot and says "Pings + ● New for you",
     "● New" under 1280 and the dot alone under 960. Otherwise it has no fill, and says "Pings + Notifications" after a bell,
     "Notifications" under 960 and the bell alone under 768. From 800 up it closes to the square as the bar merges. */
  .site-header__messages {
    --messages-padding: 0.85em; /* 12px */

    display: flex;
    font-size: var(--text-control);
    grid-area: messages;
    justify-self: end;
    position: relative;

    .site-header__link {
      --btn-background: transparent;
      --btn-font-weight: 500;
      --btn-hover-amount: 10%;
      --btn-icon-size: 1.15em; /* 16px */
      --btn-padding: 0 var(--messages-padding);

      block-size: var(--control-size);
      font-size: inherit;

      @media (width >= 800px) {
        --btn-padding: 0 calc((1 - var(--bar-merge)) * var(--messages-padding));

        justify-content: start;
        max-inline-size: calc(var(--control-size) + (1 - var(--bar-merge)) * 14em);
        min-inline-size: var(--control-size);
        overflow: clip;

        /* The bell slides to the square's middle as the words go. */
        .icon {
          margin-inline-start: calc(var(--bar-merge) * (var(--control-size) - var(--icon-size)) / 2);
        }

        .site-header__label,
        .site-header__prefix,
        .site-header__dot {
          opacity: calc(1 - 2 * var(--bar-merge));
        }

        /* While Shift is held the square shows its key's cap in the bell's or the dot's place. */
        @media (any-hover: hover) {
          [data-key-hints] & {
            justify-content: center;

            &::before {
              margin-inline-end: calc((1 - var(--bar-merge)) * 0.3em);
            }

            .icon {
              opacity: calc(1 - var(--bar-merge));
            }

            :is(.site-header__unreads, .site-header__empty) {
              max-inline-size: calc((1 - var(--bar-merge)) * 14em);
              overflow: clip;
            }
          }
        }
      }

      @media (width < 768px) {
        html:not([data-sidebar-open]) & {
          --btn-padding: 0;

          inline-size: var(--control-size);

          .site-header__dot {
            display: none;
          }
        }
      }
    }

    :is(.site-header__unreads, .site-header__empty) {
      align-items: center;
      display: inline-flex;
      gap: 0.4em;
      white-space: nowrap;
    }

    /* The words the pill drops are still said to a screen reader, as the bell and the dot say nothing, and are shown again in
       the open sidebar. */
    html:not([data-sidebar-open]) & {
      @media (width < 1280px) {
        .site-header__unreads .site-header__prefix,
        .site-header__suffix {
          block-size: 1px;
          clip-path: inset(50%);
          inline-size: 1px;
          overflow: hidden;
          position: absolute;
        }
      }

      @media (width < 960px) {
        .site-header__unreads .site-header__label,
        .site-header__empty .site-header__prefix {
          block-size: 1px;
          clip-path: inset(50%);
          inline-size: 1px;
          overflow: hidden;
          position: absolute;
        }
      }

      @media (width < 768px) {
        .site-header__empty .site-header__label {
          block-size: 1px;
          clip-path: inset(50%);
          inline-size: 1px;
          overflow: hidden;
          position: absolute;
        }
      }
    }

    .site-header__dot {
      background-color: var(--color-marker);
      block-size: 12px;
      border-radius: 50%;
      flex: none;
      inline-size: 12px;
    }

    /* The dot the square keeps, and what a screen reader hears beside the link. */
    .unread-dot {
      background-color: var(--color-marker);
      block-size: 12px;
      inline-size: 12px;
      inset: 50% auto auto 50%;
      margin: 0;
      opacity: 0;
      pointer-events: none;
      position: absolute;
      translate: -50% -50%;

      @media (width >= 800px) {
        opacity: var(--bar-merge);
      }

      @media (width < 768px) {
        html:not([data-sidebar-open]) & {
          opacity: 1;
        }
      }

      @media (any-hover: hover) {
        [data-key-hints] & {
          opacity: 0;
        }
      }
    }

    &:not(.site-header__messages--unread) {
      .site-header__unreads,
      .unread-dot {
        display: none;
      }
    }

    &.site-header__messages--unread {
      .site-header__empty {
        display: none;
      }

      .site-header__link {
        --btn-background: var(--color-pings);
        --btn-hover-amount: 100%;
        --btn-hover-color: var(--color-pings-hover);
      }
    }
  }
}
