.main-header > .navbar-nav:has(> .top-menu) {
  min-width: 0;
}

.top-menu {
  min-width: 0;
  /* clip, not hidden: hidden is a scroll container and would scroll clipped items into view on focus */
  overflow: clip;
}

.top-menu-clipped {
  visibility: hidden;
}

/* Relative offset pulls the toggle against the last visible item without changing layout, which would
   otherwise feed back into the clip box's size and the visibility observer. */
[data-top-menu-overflow] {
  position: relative;
  left: calc(-1 * var(--top-menu-gap, 0px));
}

:root {
  /* popover-menu-fallback.js reads this too. */
  --popover-menu-edge: 8px;
  --popover-menu-padding: 0.5rem;
}

/* Keeps .dropdown-menu for theming (dark mode, text-sm) but drops its positioning.
   [popover] lifts specificity over AdminLTE's navbar rules. */
.dropdown-menu.popover-menu[popover] {
  position: fixed;
  inset: auto;
  margin: 0;
  float: none;
  padding-block: var(--popover-menu-padding);
  min-width: min(10rem, calc(100vw - 2 * var(--popover-menu-edge)));
  max-width: calc(100vw - 2 * var(--popover-menu-edge));
  overflow: auto;
  overscroll-behavior: contain;

  &:popover-open {
    display: block;
  }
}

/* Without support, popover-menu-fallback.js places the menus with the same rules. */
@supports (position-area: block-end) {
  /* An overflowing box is shifted back into the viewport by its margin box, so the end margins
     keep the edge free; position-try flips swap them along with the side. */
  .dropdown-menu.popover-menu[popover] {
    position-area: block-end span-inline-end;
    margin-block-end: var(--popover-menu-edge);
    margin-inline-end: var(--popover-menu-edge);
    max-height: calc(100% - var(--popover-menu-edge));
  }

  .popover-menu .dropdown-menu.popover-menu[popover] {
    position-area: inline-end span-block-end;
    position-try-fallbacks:
      flip-inline,
      flip-block,
      flip-block flip-inline;
    /* Cancels the padding, so the first entry is level with the row that opened it. */
    margin-block-start: calc(-1 * var(--popover-menu-padding));
    max-height: calc(100vh - 2 * var(--popover-menu-edge));
  }
}
