// UTILITY PANEL
// Modifier for .sub-nav__content. All sub-nav panels in the site header (.nav__utilities) use it:
// the notifications, the messages, the profile menu and the shortcut menu. Thus they have the same
// size, padding and position. Below the medium breakpoint the panel is a layer over the page,
// below the site header.
//
// Parts:
// .sub-nav__content--utility  the panel
// --sub-nav-padding           the padding of the panel. Content in the panel gets it automatically.
//                             A scroll area uses the utility-panel-scroll-area mixin to reach the
//                             edges of the panel, so that its scroll bar is on the edge. The content
//                             inside the scroll area uses var(--sub-nav-padding) as its own padding.

$utility-panel-width: 375px;
$utility-panel-max-height: calc(100vh - 80px); // a first value from the designer, to be tuned
$utility-panel-glyph-offset: 0.56em; // the glyph is centred in its button, so its right edge is this far in
$utility-panel-top-medium: 1.94em; // sub-nav__content has 1.7em here, but the nav has two rows and the icons sit higher
$utility-panel-top-wide: 2.7em;
$utility-layer-min-height: 15rem; // keeps the content usable in a very short viewport, e.g. a phone in landscape

// A scroll area reaches the left, right and bottom edges of the panel, so its scroll bar is on the
// edge of the panel. The padding must be inside the element that scrolls, so the scroll area does
// not get it here: its content uses var(--sub-nav-padding). With $from-top, the scroll area also
// reaches the top edge, for a scroll area that holds all of the panel.
@mixin utility-panel-scroll-area($from-top: false) {
  $edge: calc(-1 * var(--sub-nav-padding));

  flex: 1 1 auto;
  min-height: 0;

  // .sub-nav__content (content-container) removes the top margin of its first child and the bottom
  // margin of its last child. This selector is more specific, so the margins stay.
  .sub-nav > .sub-nav__content.sub-nav__content--utility & {
    margin: if($from-top, $edge, 0) $edge $edge;
  }
}

.sub-nav__content--utility {
  --sub-nav-padding: #{spacing(-1)};

  width: $utility-panel-width;
  max-width: calc(100vw - 2rem);
  max-height: $utility-panel-max-height;
  right: $utility-panel-glyph-offset;
  padding: var(--sub-nav-padding);
  box-sizing: border-box;

  // Mobile: a layer directly under the site header, as wide as the page and as tall as the rest of
  // the first screen. It stays in place when the page scrolls. The app sets --sub-nav-layer-top to
  // the bottom edge of the header; .page is its containing block (see below). The layer grows with
  // its content, so the content does not scroll inside it. The padding is the side padding of the
  // page, so the text lines up with the page content.
  @include breakpoint("medium", $mobile-last: true) {
    --sub-nav-padding: #{spacing("side")};

    top: var(--sub-nav-layer-top, 0);
    right: 0;
    left: 0;
    width: auto;
    max-width: none;
    min-height: max(#{$utility-layer-min-height}, calc(100dvh - var(--sub-nav-layer-top, 0px)));
    max-height: none;
    box-shadow: none;
    // A scroll area with negative margins can round to a fraction of a pixel past the edge of the
    // screen, which gives the page a horizontal scroll bar. "clip" does not make the layer a scroll
    // container, so position: sticky in the layer still works.
    overflow-x: clip;
  }

  @include breakpoint("medium") {
    top: $utility-panel-top-medium;
  }

  @include breakpoint("wide") {
    right: $utility-panel-glyph-offset;
    top: $utility-panel-top-wide;
  }
}

// From the wide breakpoint, a trigger with its icon on the right shows its label (e.g. the profile
// menu shows the first name). The icon is then at the right edge of the button and not centred in
// it, so the panel is in line with the right edge of the button.
@include breakpoint("wide") {
  .sub-nav:has(> .nav__item--icon--right:not(.nav__item--icon--hide-label)) > .sub-nav__content--utility {
    right: 0;
  }
}

// MOBILE SIZES
// The content of the panel uses the text and spacing sizes of the smallest breakpoint at all screen
// widths. The size variables (--font-size-*, --spacing-*) are set on :root and get larger at wider
// breakpoints, so the panel sets them again with the values of the default scale. The mixins that
// make the variables also change the global size maps, so the maps are put back after them.
.sub-nav__content--utility {
  $scale: map-get($scales, default);
  $saved-font-sizes: $sizes;
  $saved-spacings: $values;

  @include font-sizes(map-get($scale, font-ratio), map-get($scale, font-base));
  @include spacings(
    map-get($scale, spacing-ratio),
    map-get($scale, font-base) * power(map-get($scale, font-ratio), $spacing-font-size-origin)
  );

  $sizes: $saved-font-sizes !global;
  $values: $saved-spacings !global;
}

// Text without its own size inherits the size of the site header, which is already a fixed length.
// Thus the children get the default text size again, with the single line height of the header. The
// rule is on the children and not on the panel, because the position of the panel is in em of its
// own font size. :where() gives the rule no specificity, so each element's own size still applies.
:where(.sub-nav__content--utility) > * {
  font-size: var(--font-size-0);
  line-height: calc(#{$line-height-scaleable-part} + #{$line-height-single-fixed-part});
}

// .sub-nav--toggled sets display: block on the same element with higher specificity.
// A column, so a scroll area can fill the rest of the panel.
.sub-nav--toggled > .sub-nav__content--utility {
  display: flex;
  flex-direction: column;
}

// Mobile: the page holds the layer, so it is as wide as the page. The header can be wider than the
// page when the nav overflows. The rules apply only while a panel is open, because a panel can stay
// in the DOM while it is closed.
@include breakpoint("medium", $mobile-last: true) {
  .page:has(.sub-nav--toggled > .sub-nav__content--utility) {
    position: relative;
  }

  // The layer is out of the page's flow, so the page content stands in for it: it is hidden and as
  // tall as the layer (the app sets --sub-nav-layer-height), which puts the footer under the layer.
  .page:has(.sub-nav--toggled > .sub-nav__content--utility) > .page__content {
    height: var(--sub-nav-layer-height, auto);
    overflow: hidden;
    visibility: hidden;
  }

  // The trigger's own wrapper is not the containing block on mobile.
  .sub-nav:has(> .sub-nav__content--utility) {
    position: static;
  }
}

// TITLE
// The title at the top of a panel. It has no rule under it: a list under the title has a rule at
// its top (e.g. .notice-feed__inner), and a panel without a list has no rule.
.sub-nav__header {
  margin: 0;
  padding: 0 0 spacing(-1);
}

.sub-nav__title {
  margin: 0;
}

// The padding of the title gives the space under it. Thus the content after the title has no top
// margin, also not in its first element and that element's first child. Otherwise e.g. the name of
// a source in the messages feed (an h3 in a section in the list) gets both spaces.
.sub-nav__header + *,
.sub-nav__header + * > :first-child,
.sub-nav__header + * > :first-child > :first-child {
  margin-top: 0;
}

// USER
// A line of text in .sub-nav__user, e.g. the user's name. It has the same style as the copy buttons
// in the user block (AU-ID, student number). .sub-nav__user-name is a heading, so it is not used here.
.sub-nav__user-text {
  @include set-font(-1, "single");
  margin: 0;
  color: color("foreground-secondary");
}
