/* ============================================================================
   SIDEBAR FIT — left rail height/scroll + vertical balance with the logo.
   Loaded AFTER ds/v2-sidebar-cmu.css on purpose: the rails get rewritten often,
   this file is the single place for the fit rules and always wins (same
   specificity, later in the cascade + !important).
   Layout only — no menu item, URL or order is touched.
   ---------------------------------------------------------------------------
   Reference geometry (1440x900): logo box top 28px, right icon rail top = logo
   top (--rail-top), rail bottom = 100dvh, menu list = the only scroll region,
   search / TH|EN footer pinned to the bottom edge.
   ========================================================================== */

/* ---- left rail: capped to the real viewport, only the list scrolls -------- */
@media (min-width: 1200px) {
  html.v2 { --rail-top: 28px; }

  html.v2 .navbar.navbar-default.navbar-static-top {
    top: 0 !important; bottom: auto !important;
    height: 100vh !important;          /* fallback */
    height: 100dvh !important;         /* real viewport height */
    max-height: 100dvh !important;
    overflow: hidden !important;       /* the rail never bleeds past the window */
    overscroll-behavior: contain;
    display: flex !important; flex-direction: column !important;
  }
  html.v2 body .navbar.navbar-default.navbar-static-top > .container {
    display: flex !important; flex-direction: column !important;
    height: 100% !important; max-height: 100% !important; min-height: 0 !important;
    justify-content: flex-start !important; align-items: stretch !important;
    gap: 0 !important; overflow: hidden !important;
  }

  /* logo header: just enough air, pinned at the top of the rail */
  html.v2 .navbar-header { flex: 0 0 auto !important; padding: 28px 0 14px !important; }
  html.v2 .v2-group:first-child { margin-top: 0 !important; }
  /* the scroll region is a flex column: entries keep their height, the footer
     is pushed to the bottom edge (margin-top:auto) instead of a dead gap */
  html.v2 #navbar > .navbar-disable > .v2-group,
  html.v2 .navbar-collapse > .navbar-disable > .v2-group { flex: 0 0 auto; }

  /* the menu block starts right under the logo */
  html.v2 body .navbar > .container > .navbar-collapse,
  html.v2 body .navbar > .container > .navbar-collapse.collapse,
  html.v2 body .navbar > .container > .navbar-collapse.collapse.in {
    flex: 1 1 auto !important; min-height: 0 !important;
    display: flex !important; flex-direction: column !important;
    justify-content: flex-start !important;
    overflow: hidden !important; padding: 0 0 8px !important;
  }
  html.v2 #navbar > .navbar-disable,
  html.v2 .navbar-collapse > .navbar-disable {
    flex: 1 1 auto !important; min-height: 0; padding-bottom: 8px !important;
    display: flex !important; flex-direction: column !important; align-items: stretch !important;
    overflow-y: auto !important; overflow-x: hidden !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,.45) transparent;
  }

  /* search + TH|EN on the bottom edge, always visible */
  html.v2 body .navbar-collapse .navbar-disable > .v2-side-foot {
    position: static !important; z-index: 2; flex: 0 0 auto;
    margin-top: auto !important;
    padding: 14px 0 16px !important;
    background: var(--side-bg);
  }

  /* ---- right icon rail: level with the logo, capped to the viewport ------ */
  html.v2 body .navbar > .container > ul.nav.navbar-nav.navbar-right {
    top: var(--rail-top) !important;
    max-height: calc(100dvh - var(--rail-top) - 24px);
    overflow-y: auto; overflow-x: hidden;
    overscroll-behavior: contain;
  }
}

/* ---- short desktop windows (laptop 768 / 1366x768 with chrome) ----------- */
@media (min-width: 1200px) and (max-height: 800px) {
  html.v2 { --rail-top: 16px; }
  html.v2 .navbar-header { padding: 16px 0 8px !important; }
  html.v2 .navbar-collapse .v2-side-foot { padding: 8px 0 10px !important; }
  html.v2 .v2-langline { margin-top: 6px; }
  html.v2 body .navbar > .container > ul.nav.navbar-nav.navbar-right { gap: 6px !important; }
}
@media (min-width: 1200px) and (max-height: 640px) {
  html.v2 .navbar-header { padding: 10px 0 6px !important; }
  html.v2 .navbar-header .v2-logo,
  html.v2 .navbar-header .d-logo img { max-height: 46px !important; height: auto !important; }
  html.v2 .v2-group-toggle, html.v2 .v2-group-link { min-height: 40px !important; }
}

/* ---- drawer <=1199px: sheet under the 64px app bar, height from dvh ------ */
@media (max-width: 1199px) {
  html.v2 .navbar-collapse,
  html.v2 .navbar-collapse.collapse,
  html.v2 .navbar-collapse.collapse.in {
    top: 64px !important; bottom: 0 !important; left: 0 !important; right: 0 !important;
    height: auto !important;
    max-height: calc(100dvh - 64px) !important;
    overflow-y: auto !important; overflow-x: hidden !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}
