/* Compact app bar: on desktop the logo, navigation and user menu share one
   slim sticky row instead of a tall header with a second navigation row.
   Opt in per page with <body class="app-compact">. Phones keep the header
   with the menu button; the block at the end only slims it down. */
@media (min-width: 769px) {
  body.app-compact {
    /* Side inset that lines the bar up with the centred 1280px content column */
    --bar-inset: max(16px, calc((100vw - 1280px) / 2 + 16px));
  }

  body.app-compact #app {
    display: grid;
    grid-template-columns: var(--bar-inset) auto minmax(0, 1fr) auto var(--bar-inset);
    grid-template-rows: auto 1fr;
  }

  body.app-compact #app > * {
    grid-column: 1 / -1;
  }

  /* The bar's background; the three pieces below sit on top of it */
  body.app-compact #app::before {
    content: "";
    grid-column: 1 / -1;
    grid-row: 1;
    position: sticky;
    top: 0;
    z-index: 1000;
    background: var(--surface, #fff);
    border-bottom: 1px solid var(--border, #e5e7eb);
  }

  /* Let the header's children take part in the bar's grid directly */
  body.app-compact .app-header,
  body.app-compact .app-header .header-content {
    display: contents;
  }

  body.app-compact .app-header .app-title,
  body.app-compact #app > .main-nav,
  body.app-compact .app-header .user-menu {
    grid-row: 1;
    position: sticky;
    top: 0;
    z-index: 1001;
    min-height: 52px;
  }

  body.app-compact .app-header .app-title {
    grid-column: 2;
    display: flex;
    align-items: center;
    flex: none;
    padding: 8px 8px 8px 0;
  }

  body.app-compact .app-title img {
    height: 36px !important;
  }

  body.app-compact .app-title h1 {
    margin: 0;
    font-size: 1.125rem;
    line-height: 1.2;
    white-space: nowrap;
  }

  body.app-compact #app > .main-nav {
    grid-column: 3;
    display: flex;
    align-items: stretch;
    min-width: 0;
    padding: 0;
    background: none;
    border: 0;
    box-shadow: none;
    overflow-x: auto;
  }

  /* Some pages wrap the links in an extra element */
  body.app-compact .main-nav .desktop-nav {
    display: flex;
    flex: 1 1 auto;
    min-width: 0;
  }

  /* Links sit on the right and spread out a little when there is room */
  body.app-compact .main-nav .nav-content {
    flex: 1 1 auto;
    max-width: 1000px;
    margin: 0 0 0 auto;
    padding: 0;
    gap: 0;
    justify-content: space-between;
  }

  body.app-compact .main-nav .nav-item {
    padding: 8px 6px;
    font-size: 0.9375rem;
  }

  /* Text-only links: nine labelled links plus icons do not fit beside the logo */
  body.app-compact .main-nav .nav-icon {
    display: none;
  }

  body.app-compact .app-header .user-menu {
    grid-column: 4;
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 12px;
    white-space: nowrap;
  }

  body.app-compact .app-header .user-menu .button {
    padding: 6px 12px;
    font-size: 0.9375rem;
  }

  body.app-compact #app > .main-content {
    grid-row: 2;
    width: 100%;
  }
}

/* The bar has no room for the "Welcome, name" greeting; Logout stays */
@media (min-width: 769px) {
  body.app-compact .app-header .user-menu #user-name {
    display: none;
  }
}

/* As the window narrows: drop the wordmark, then shrink the links, so they fit */
@media (min-width: 769px) and (max-width: 1320px) {
  body.app-compact .app-title h1 {
    display: none;
  }
}

@media (min-width: 769px) and (max-width: 1140px) {
  body.app-compact .main-nav .nav-item {
    padding: 8px 5px;
    font-size: 0.875rem;
  }
}

/* Phones: one slim header row at the very top (the shared mobile rules left an
   empty band above it and let the title wrap onto two lines) */
@media (max-width: 768px) {
  body.app-compact {
    padding-top: 0;
  }

  body.app-compact .app-header {
    position: sticky !important;
    top: 0;
    padding: 4px 12px;
  }

  body.app-compact .app-header .header-content {
    padding: 0;
  }

  body.app-compact .app-header .app-title {
    flex: 1;
    min-width: 0;
  }

  body.app-compact .app-header .app-title img {
    height: 32px !important;
  }

  body.app-compact .app-header .app-title h1 {
    margin: 0;
    font-size: 1.125rem;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
