/* One set of existing controls: inline on desktop, a disclosure on small screens. */
.mobile-tools-toggle,
.mobile-utility-label {
  display: none;
}

@media (max-width: 992px) {
  .topbar,
  body.section-dashboard .topbar {
    flex-wrap: nowrap;
    gap: 8px;
  }

  .topbar-start {
    flex: 1 0 auto;
    flex-wrap: nowrap;
    min-width: 0;
  }

  /* Cancel the old <=600px full-width rule even before the small script loads. */
  .topbar-actions {
    width: auto;
    flex-wrap: nowrap;
    min-width: 0;
  }

  .topbar:not(.mobile-tools-ready) .topbar-actions {
    flex: 0 1 auto;
    overflow-x: auto;
  }

  .topbar .mobile-sidebar-toggle {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
  }

  .topbar.mobile-tools-ready .mobile-tools-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    gap: 7px;
    width: auto;
    min-width: 76px;
    height: 44px;
    padding: 0 12px;
    border-radius: 12px;
    font-size: .86rem;
    font-weight: 700;
    color: #f7d36a;
  }

  .topbar.mobile-tools-ready #headerToolsPanel {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 12px;
    width: min(320px, calc(100vw - 24px));
    max-height: calc(100vh - var(--app-header-height, 60px) - 24px);
    max-height: calc(100dvh - var(--app-header-height, 60px) - 24px - env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 10px;
    border: 1px solid rgba(242, 193, 78, .38);
    border-radius: 16px;
    background: var(--sidebar, #0f172a);
    color: var(--sidebar-text, #f8fafc);
    box-shadow: 0 14px 32px rgba(2, 6, 23, .26);
  }

  .topbar.mobile-tools-ready.mobile-tools-open #headerToolsPanel {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  /* Translation must remain available, including below the old 390px cutoff. */
  .topbar.mobile-tools-ready #headerToolsPanel .translate-menu,
  .topbar.mobile-tools-ready #headerToolsPanel .user-profile-chip {
    display: block;
    width: 100%;
  }

  .topbar.mobile-tools-ready #headerToolsPanel .header-utility-btn,
  .topbar.mobile-tools-ready #headerToolsPanel .translate-icon-btn,
  .topbar.mobile-tools-ready #headerToolsPanel .profile-menu-trigger,
  .topbar.mobile-tools-ready #headerToolsPanel .user-logout-btn {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 12px;
    width: 100%;
    min-width: 0;
    min-height: 44px;
    height: auto;
    padding: 10px 12px;
    border-radius: 10px;
    font: 600 .92rem/1.4 Inter, sans-serif;
    color: var(--sidebar-text, #f8fafc);
    text-align: left;
    transform: none;
  }

  .topbar.mobile-tools-ready .mobile-utility-label {
    display: inline;
  }

  .topbar.mobile-tools-ready #headerToolsPanel .notification-count {
    position: static;
    flex: 0 0 auto;
    margin-left: auto;
    min-width: 18px;
    height: 18px;
    font-size: .68rem;
  }

  /* Expand existing Bootstrap dropdowns inside the scrollable disclosure.
     Override only their mobile positioning, never duplicate their contents. */
  .topbar.mobile-tools-ready #headerToolsPanel .dropdown-menu {
    position: static !important;
    inset: auto !important;
    transform: none !important;
    min-width: 0;
    width: 100%;
    max-width: 100%;
    margin: 6px 0 0 !important;
    box-shadow: none;
  }

  .topbar.mobile-tools-ready #headerToolsPanel .dropdown-item {
    white-space: normal;
    overflow-wrap: anywhere;
    min-height: 44px;
  }

  .topbar.mobile-tools-ready :is(button, a):focus-visible {
    outline: 2px solid #f7d36a;
    outline-offset: 2px;
  }
}
