/* V36.7.41 — one source of truth for responsive header, landscape sidebar and touch scrolling. */
:root {
  --v40-header-gap: 8px;
  --v40-sidebar-width: 188px;
  --v40-sidebar-top: 150px;
}

/* Keep the display-name trigger and its popover above all page decorations. */
.app-header .user-pill {
  position: relative !important;
  z-index: 1605 !important;
  overflow: visible !important;
  isolation: isolate !important;
  pointer-events: auto !important;
}
.app-header #user-name-display {
  position: relative !important;
  z-index: 1607 !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  min-width: 0 !important;
  max-width: 100% !important;
  pointer-events: auto !important;
  touch-action: manipulation !important;
  cursor: pointer !important;
}
.app-header #header-actions-menu {
  position: absolute !important;
  top: calc(100% + 8px) !important;
  right: 0 !important;
  z-index: 1610 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: flex-end !important;
  gap: 8px !important;
  width: max-content !important;
  max-width: calc(100vw - 20px) !important;
  padding: 8px !important;
  pointer-events: auto !important;
}
.app-header #header-actions-menu[hidden] { display: none !important; }
.app-header #header-actions-menu button {
  pointer-events: auto !important;
  touch-action: manipulation !important;
}
.app-header #header-actions-menu #theme-gallery-toggle > span { display: none !important; }

/* Small screens and short landscape screens use a deliberate two-row header.
   Do not flatten the user-pill: it is the positioning anchor for the action list. */
@media (max-width: 980px), (orientation: landscape) and (max-height: 820px) {
  html body header.app-header {
    position: relative !important; /* must scroll away; sticky header breaks sidebar tracking */
    top: auto !important;
    z-index: 1600 !important;
    isolation: isolate !important;
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto auto !important;
    grid-template-rows: auto auto !important;
    grid-template-areas: "brand mode user" "menu menu menu" !important;
    align-items: center !important;
    justify-content: stretch !important;
    column-gap: 8px !important;
    row-gap: 8px !important;
    min-height: 0 !important;
    padding: 8px 12px 10px !important;
    overflow: visible !important;
    box-sizing: border-box !important;
  }
  .app-header > .header-left,
  .app-header > .header-controls,
  .app-header .brand-stack { display: contents !important; }
  .app-header .brand {
    grid-area: brand !important;
    display: flex !important;
    align-items: center !important;
    justify-self: start !important;
    min-width: 0 !important;
    max-width: 100% !important;
    gap: 7px !important;
    position: relative !important;
    z-index: 1602 !important;
  }
  .app-header .brand-icon,
  .app-header .brand-icon img {
    width: 38px !important;
    height: 38px !important;
    max-width: 38px !important;
    flex: 0 0 38px !important;
    object-fit: contain !important;
  }
  .app-header .brand-title {
    font-size: clamp(13px, 3.8vw, 17px) !important;
    line-height: 1.15 !important;
    white-space: nowrap !important;
  }
  .app-header .brand-sub { display: none !important; }
  .app-header .user-pill {
    grid-area: user !important;
    justify-self: end !important;
    align-self: center !important;
    display: flex !important;
    min-width: 0 !important;
    max-width: 30vw !important;
    padding: 7px 9px !important;
    white-space: nowrap !important;
  }
  .app-header .user-avatar { display: none !important; }
  .app-header #user-name-display {
    max-width: 26vw !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
    font-size: 12px !important;
  }
  .app-header #btn-auth-trigger,
  .app-header #btn-logout { display: none !important; }
  .app-header #default-mode-switch {
    grid-area: mode !important;
    justify-self: end !important;
    align-self: center !important;
    display: flex !important;
    width: max-content !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 3px !important;
    gap: 3px !important;
    white-space: nowrap !important;
  }
  .app-header #default-mode-switch.hidden { display: none !important; }
  .app-header #default-mode-switch button { min-width: 0 !important; padding: 6px 8px !important; font-size: 11px !important; }
  .app-header #theme-picker { display: none !important; }
  .app-header #mobile-menu-toggle {
    grid-area: menu !important;
    justify-self: start !important;
    align-self: center !important;
    position: relative !important;
    z-index: 1603 !important;
    display: inline-flex !important;
    margin: 0 !important;
    padding: 8px 11px !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    white-space: nowrap !important;
  }
}

/* Landscape split layout: the sidebar is a fixed panel below the live header edge. */
@media (orientation: landscape) and (max-height: 820px) {
  :root { --v40-sidebar-width: 188px; }
  html body #app-sidebar.app-sidebar {
    position: fixed !important;
    top: var(--v40-sidebar-top, 150px) !important;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    left: 8px !important;
    right: auto !important;
    width: min(188px, 24vw) !important;
    min-width: 154px !important;
    max-width: 188px !important;
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    overscroll-behavior: contain !important;
    z-index: 1400 !important;
    pointer-events: auto !important;
    box-sizing: border-box !important;
    transition: transform .2s ease !important;
  }
  html body #app-sidebar #main-tabs-nav,
  html body #app-sidebar #main-tabs-nav.tabs-nav {
    position: static !important;
    display: flex !important;
    flex: 1 1 auto !important;
    flex-direction: column !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: contain !important;
    touch-action: pan-y !important;
    scrollbar-width: thin !important;
    pointer-events: auto !important;
  }
  html body #app-sidebar #main-tabs-nav .tab-btn {
    flex: 0 0 auto !important;
    width: 100% !important;
    min-height: 46px !important;
    touch-action: pan-y !important;
    pointer-events: auto !important;
  }
  html body #app-sidebar .sidebar-heading,
  html body #app-sidebar .sidebar-foot { flex: 0 0 auto !important; }
  html body.sidebar-collapsed #app-sidebar.app-sidebar {
    transform: translateX(calc(-100% - 20px)) !important;
    pointer-events: none !important;
  }
  html body.sidebar-expanded #app-sidebar.app-sidebar {
    transform: translateX(0) !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }
  html body .main-container,
  html body.dashboard-home .main-container,
  html body.dashboard-tab-mode .main-container {
    width: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    margin: 0 10px 0 calc(var(--v40-sidebar-width) + 24px) !important;
    padding: 12px 10px 24px !important;
    box-sizing: border-box !important;
  }
  html body.sidebar-collapsed .main-container,
  html body.dashboard-home.sidebar-collapsed .main-container,
  html body.dashboard-tab-mode.sidebar-collapsed .main-container {
    margin-left: 10px !important;
  }
  #sidebar-backdrop, .sidebar-backdrop { display: none !important; pointer-events: none !important; }
}

/* Portrait drawer keeps its established width, but uses the same reliable touch scroll. */
@media (max-width: 600px) and (orientation: portrait) {
  #app-sidebar #main-tabs-nav {
    min-height: 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: contain !important;
    touch-action: pan-y !important;
  }
}


/* V36.7.41 — final correction: one scroll surface for the complete landscape sidebar. */
@media (orientation: landscape) and (max-height: 820px) {
  html body #app-sidebar.app-sidebar {
    display: block !important;
    position: fixed !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: contain !important;
    touch-action: pan-y !important;
    scrollbar-width: thin !important;
    padding: 12px 10px 12px !important;
    box-sizing: border-box !important;
    contain: layout paint style !important;
  }
  html body #app-sidebar #main-tabs-nav,
  html body #app-sidebar #main-tabs-nav.tabs-nav {
    display: flex !important;
    position: static !important;
    flex: 0 0 auto !important;
    flex-direction: column !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    gap: 6px !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 6px 1px 8px !important;
    overflow: visible !important;
    overscroll-behavior: auto !important;
    touch-action: auto !important;
    transform: none !important;
  }
  html body #app-sidebar .sidebar-heading,
  html body #app-sidebar .sidebar-foot,
  html body #app-sidebar #main-tabs-nav .tab-btn {
    position: relative !important;
    flex: 0 0 auto !important;
    touch-action: pan-y !important;
  }
  html body #app-sidebar #main-tabs-nav .tab-btn {
    min-height: 46px !important;
    width: 100% !important;
    pointer-events: auto !important;
  }
}

/* The display name is a button inside a pill; remove the browser's inner square button chrome. */
.app-header .user-pill #user-name-display,
.app-header .user-pill #user-name-display:hover,
.app-header .user-pill #user-name-display:active,
.app-header .user-pill #user-name-display:focus,
.app-header .user-pill #user-name-display:focus-visible {
  appearance: none !important;
  -webkit-appearance: none !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  padding: 0 !important;
  margin: 0 !important;
  border: 0 !important;
  outline: 0 !important;
  border-radius: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  font: inherit !important;
  line-height: inherit !important;
  color: inherit !important;
}
.app-header .user-pill:focus-within {
  outline: 2px solid rgba(91, 151, 224, .42) !important;
  outline-offset: 2px !important;
}
