/* Header-only overrides: keep disclosures out of the old scrolling nav. */
.topbar {
  flex-wrap: wrap;
  column-gap: 20px;
  row-gap: 8px;
}

.topbar nav {
  align-items: center;
  flex: 1 1 40rem;
  flex-wrap: wrap;
  gap: 4px;
  overflow: visible;
}

.topbar nav > a,
.topbar-settings > summary {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 7px 9px;
  border-radius: 6px;
  white-space: nowrap;
}

.topbar nav > a:hover,
.topbar-settings > summary:hover,
.topbar-settings[open] > summary {
  background: #374151;
}

.topbar a:focus-visible,
.topbar-settings > summary:focus-visible {
  outline: 2px solid #93c5fd;
  outline-offset: 2px;
}

.topbar-settings {
  position: relative;
  flex: 0 0 auto;
}

.topbar-settings > summary {
  gap: 9px;
  cursor: pointer;
  list-style: none;
}

.topbar-settings > summary::-webkit-details-marker {
  display: none;
}

.topbar-settings > summary::after {
  content: '';
  width: 7px;
  height: 7px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateY(-2px) rotate(45deg);
}

.topbar-settings[open] > summary::after {
  transform: translateY(2px) rotate(225deg);
}

.topbar-settings-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 1;
  display: grid;
  gap: 2px;
  width: 16rem;
  max-width: calc(100vw - 24px);
  max-height: min(26rem, calc(100vh - 12rem));
  max-height: min(26rem, calc(100dvh - 12rem));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}

.topbar-settings:not([open]) > .topbar-settings-panel {
  display: none;
}

.topbar .topbar-settings-panel a {
  display: block;
  padding: 10px 12px;
  border-radius: 5px;
  color: #1f2937;
  white-space: normal;
  overflow-wrap: anywhere;
}

.topbar .topbar-settings-panel a:hover,
.topbar .topbar-settings-panel a:focus-visible {
  background: #eff6ff;
  color: #1d4ed8;
}

.topbar .topbar-settings-panel a:focus-visible {
  outline-offset: -2px;
}

.topbar .user-menu {
  min-width: 0;
  max-width: 100%;
  margin-left: auto;
}

.topbar .user-menu > span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.topbar .user-menu form {
  flex: 0 0 auto;
}

@media (max-width: 1100px) {
  .topbar nav {
    position: relative;
    order: 3;
    flex-basis: 100%;
    width: 100%;
    overflow: visible;
  }

  /* Anchor to the entire nav so a wrapped Settings item cannot open offscreen. */
  .topbar-settings {
    position: static;
  }

  .topbar-settings-panel {
    left: 0;
    right: 0;
    width: auto;
    max-width: none;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .topbar nav {
    gap: 2px;
  }

  .topbar nav > a,
  .topbar-settings > summary {
    min-height: 44px;
    padding: 8px;
  }

  .topbar-settings-panel {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* Very short windows must be able to scroll past the header itself. */
@media (max-height: 400px) {
  .topbar {
    position: relative;
  }

  .topbar-settings-panel {
    max-height: 50vh;
  }
}
