@layer layout {
  .app-skip-link {
    position: fixed;
    top: calc(var(--app-spacing) * 0.5);
    left: var(--app-spacing);
    padding: calc(var(--app-spacing) * 0.5) var(--app-spacing);
    border: 1px solid var(--app-primary);
    border-radius: var(--app-border-radius);
    background: var(--app-background-color);
    color: var(--app-primary);
    text-decoration: none;
    transform: translateY(-180%);
    transition: transform var(--app-transition);
    z-index: 40;
  }

  .app-skip-link:focus,
  .app-skip-link:focus-visible {
    transform: translateY(0);
  }

  .app-header {
    --app-topbar-item-padding: 8px;
    --app-topbar-list-gap: 8px;
    --app-topbar-divider: 1px solid var(--app-border);
    --app-topbar-muted: var(--app-form-element-placeholder-color);
    --app-topbar-bg: var(--app-background-color);
    padding-block: 0.375rem;
    padding-inline: var(--app-spacing);
    color: var(--app-contrast);
    border-bottom: var(--app-topbar-divider);
  }

  .app-header nav.app-topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: var(--app-spacing);
    align-items: center;
  }

  .app-header nav li {
    padding: 0;
  }

  .app-topbar-left,
  .app-topbar-right {
    display: flex;
    gap: var(--app-topbar-list-gap);
    align-items: center;
    color: var(--app-muted-color);
  }

  .app-topbar-right {
    justify-content: flex-end;
  }

  .app-topbar-center {
    min-height: 1px;
    display: flex;
    justify-content: center;
  }

  .app-topbar-search-trigger {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-width: min(22rem, 35vw);
    max-width: min(30rem, 50vw);
    width: 100%;
    padding: 0.3rem 0.65rem;
    border: var(--app-border-width) solid var(--app-border);
    border-radius: var(--app-border-radius);
    background: transparent;
    color: var(--app-muted-color);
    font-size: var(--text-sm);
    cursor: pointer;
    transition:
      border-color var(--app-transition),
      color var(--app-transition);
    appearance: none;
    -webkit-appearance: none;
    margin: 0;
  }

  .app-topbar-search-trigger:hover,
  .app-topbar-search-trigger:focus-visible {
    border-color: var(--app-primary);
  }

  .app-topbar-search-trigger:focus-visible {
    outline: 2px solid var(--app-primary);
    outline-offset: -2px;
  }

  .app-topbar-search-trigger .bi {
    font-size: var(--text-sm);
    flex-shrink: 0;
  }

  .app-topbar-search-trigger-text {
    flex: 1;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-topbar-search-trigger-shortcut {
    border: 1px solid var(--app-border);
    border-radius: 4px;
    background: var(--app-card-background-color);
    color: var(--app-muted-color);
    font-size: var(--text-xs);
    line-height: var(--leading-none);
    padding: 0.1rem 0.3rem;
    flex-shrink: 0;
  }

  /* Breadcrumb mobile-hide lives in components/app-breadcrumb.css — cross-layer
     overrides from the layout layer do not win against .app-breadcrumb's own
     display:flex in the components layer. */

  @media (max-width: 576px) {
    .app-topbar-search-trigger-text,
    .app-topbar-search-trigger-shortcut {
      display: none;
    }

    .app-topbar-search-trigger {
      width: auto;
      min-width: 2.25rem;
      min-height: 2.25rem;
      padding: var(--app-topbar-item-padding);
      border: none;
    }

    /* Dock the search icon alongside the right-column icons:
       swap grid \u2192 flex so left flex-grows and center+right cluster on the right. */
    .app-header nav.app-topbar {
      display: flex;
      grid-template-columns: unset;
      gap: var(--app-topbar-list-gap);
      align-items: center;
    }

    .app-topbar-left {
      flex: 1 1 0%;
      min-width: 0;
    }

    .app-topbar-center,
    .app-topbar-right {
      flex: 0 0 auto;
    }

    .app-topbar-center {
      justify-content: flex-end;
    }
  }

  /* xs — very narrow phones (iPhone SE 375, Pixel 4a portrait).
     Tighten topbar chrome so every item keeps its 2.25rem hit area without overflow. */
  @media (max-width: 400px) {
    .app-header {
      --app-topbar-list-gap: 2px;
      padding-inline: calc(var(--app-spacing) * 0.5);
    }

    .app-header nav.app-topbar {
      gap: calc(var(--app-spacing) * 0.5);
    }
  }

  .app-topbar-menu-item-detail-sidebar {
    display: none;
  }

  body:has(#app-details-aside-section) .app-topbar-menu-item-detail-sidebar {
    display: list-item;
  }

  .app-header details.dropdown > summary:not([role]) {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 2.25rem;
    min-height: 2.25rem;
    border: none;
    height: auto;
    padding: var(--app-topbar-item-padding);
    border-radius: var(--app-border-radius);
    background: transparent;
    color: var(--app-topbar-muted);
    cursor: pointer;
    transition:
      background var(--app-transition),
      color var(--app-transition);
  }

  .app-topbar-right > li > a,
  .app-topbar-right > li > button {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 2.25rem;
    min-height: 2.25rem;
    padding: var(--app-topbar-item-padding);
    margin: 0;
    border: none;
    border-radius: var(--app-border-radius);
    background-color: transparent;
    box-shadow: none;
    color: var(--app-topbar-muted);
    font-size: inherit;
    font-weight: var(--font-regular);
    line-height: var(--leading-none);
    cursor: pointer;
    transition:
      background-color var(--app-transition),
      color var(--app-transition);
    appearance: none;
    -webkit-appearance: none;
    /* Reset global button custom properties from app-shell.css */
    --app-background-color: transparent;
    --app-border-color: transparent;
    --app-color: var(--app-topbar-muted);
    --app-box-shadow: none;
  }

  .app-topbar-right > li > a:hover,
  .app-topbar-right > li > button:hover,
  .app-header details.dropdown > summary:not([role]):hover {
    background-color: var(--app-dropdown-hover-background-color);
    color: var(--app-color);
    --app-background-color: var(--app-dropdown-hover-background-color);
    --app-color: var(--app-color);
  }

  .app-topbar-right > li > a:active,
  .app-topbar-right > li > button:active,
  .app-header details.dropdown > summary:not([role]):active {
    background-color: color-mix(
      in srgb,
      var(--app-dropdown-hover-background-color) 70%,
      var(--app-contrast) 30%
    );
    --app-background-color: color-mix(
      in srgb,
      var(--app-dropdown-hover-background-color) 70%,
      var(--app-contrast) 30%
    );
  }

  .app-topbar-right > li > a:focus-visible,
  .app-topbar-right > li > button:focus-visible,
  .app-header details.dropdown > summary:not([role]):focus-visible {
    outline: 2px solid var(--app-primary);
    outline-offset: -2px;
  }

  /* Tenant-switcher uses the shared dropdown summary styling from .app-header details.dropdown > summary:not([role]) */

  .app-topbar-user-menu > summary:hover {
    color: var(--app-color);
  }

  .app-topbar-menu-list {
    min-width: min(17rem, calc(100vw - (var(--app-spacing) * 2)));
    max-width: min(24rem, calc(100vw - (var(--app-spacing) * 2)));
    max-height: min(32rem, calc(100vh - (var(--app-spacing) * 4)));
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
  }

  @supports (height: 100dvh) {
    .app-topbar-menu-list {
      max-height: min(32rem, calc(100dvh - (var(--app-spacing) * 4)));
    }
  }

  @media (min-width: 768px) {
    .app-topbar-menu-list {
      max-height: min(40rem, calc(100vh - (var(--app-spacing) * 4)));
    }
  }

  @supports (height: 100dvh) {
    @media (min-width: 768px) {
      .app-topbar-menu-list {
        max-height: min(40rem, calc(100dvh - (var(--app-spacing) * 4)));
      }
    }
  }

  .app-topbar-menu-list--right {
    right: 0;
    left: auto;
  }

  /* Override global dropdown default (left:0) without using dir=rtl hacks. */
  .app-header details.dropdown > summary + ul.app-topbar-menu-list--right {
    right: 0;
    left: auto;
    inset-inline-end: 0;
    inset-inline-start: auto;
  }

  .app-topbar-menu-list li > a,
  .app-topbar-menu-list li > button {
    display: block;
    width: 100%;
    margin: 0;
    padding: 0.375rem 0.5rem;
    overflow: hidden;
    border: 0;
    border-radius: var(--app-border-radius);
    background: transparent;
    color: var(--app-dropdown-color);
    font: inherit;
    line-height: inherit;
    text-align: left;
    text-decoration: none;
    text-overflow: ellipsis;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
  }

  .app-topbar-menu-list li {
    display: block;
  }

  .app-topbar-menu-list li > a:hover,
  .app-topbar-menu-list li > a:focus-visible,
  .app-topbar-menu-list li > button:hover,
  .app-topbar-menu-list li > button:focus-visible {
    background: color-mix(in srgb, var(--app-contrast) 8%, transparent);
  }

  .app-topbar-menu-list li > a.active {
    color: var(--app-primary);
    font-weight: var(--font-medium);
  }

  .app-topbar-menu-list li > button.app-topbar-menu-with-hotkey {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    column-gap: calc(var(--app-spacing) * 0.6);
  }

  .app-topbar-hotkey {
    color: var(--app-muted-color);
    font-size: var(--text-xs);
    white-space: nowrap;
  }

  .app-topbar-menu-list li.app-topbar-menu-heading {
    padding: calc(var(--app-spacing) * 0.5) 0.5rem
      calc(var(--app-spacing) * 0.35);
    color: var(--app-muted-color);
    font-weight: var(--font-medium);
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    pointer-events: none;
  }

  .app-topbar-menu-list li.app-topbar-menu-divider {
    margin: calc(var(--app-spacing) * 0.25) 0;
    padding: 0;
    height: 1px;
    background: var(--app-border);
    list-style: none;
  }

  .app-topbar-menu-list
    li.app-topbar-menu-divider
    + li.app-topbar-menu-heading {
    margin-top: calc(var(--app-spacing) * 0.15);
  }

  header.app-header {
    position: sticky;
    top: 0;
    z-index: var(--z-topbar);
    background: var(--app-topbar-bg);
  }

  /* ── Hamburger button (mobile-only) ── */
  .app-topbar-hamburger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.25rem;
    min-height: 2.25rem;
    padding: var(--app-topbar-item-padding);
    margin: 0;
    border: none;
    border-radius: var(--app-border-radius);
    background: transparent;
    color: var(--app-topbar-muted);
    font-size: var(--text-2xl);
    cursor: pointer;
    transition:
      background var(--app-transition),
      color var(--app-transition);
  }

  .app-topbar-hamburger:hover {
    background: var(--app-dropdown-hover-background-color);
    color: var(--app-color);
  }

  .app-topbar-hamburger:focus-visible {
    outline: 2px solid var(--app-primary);
    outline-offset: -2px;
  }

  @media (min-width: 768px) {
    .app-topbar-hamburger {
      display: none;
    }
  }

  /* ── Tenant branding in topbar ── */
  .app-topbar-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    color: var(--app-contrast);
  }

  .app-topbar-brand::after {
    content: "";
    display: block;
    width: 1px;
    height: 1.25rem;
    background: var(--app-border);
    flex-shrink: 0;
    margin-inline: calc(var(--app-spacing) * 0.5);
  }

  .app-topbar-brand img {
    display: block;
    width: 8rem;
    height: 2.5rem;
    object-fit: contain;
    object-position: left;
  }

  .app-topbar-brand-name {
    font-weight: var(--font-semibold);
    font-size: var(--text-sm);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 14rem;
  }

  @media (max-width: 575px) {
    .app-topbar-brand-name {
      display: none;
    }

    /* With brand-name hidden, the divider bar is pointless chrome — drop it. */
    .app-topbar-brand::after {
      display: none;
    }
  }

  .app-header details.dropdown[open] > summary::before {
    display: none;
  }
}
