/* Menu trái dạng cây, theo Figma "Map UI" (Docs / Tree item).
   HTML ở partials/nav.html và nav-item.html, hành vi ở assets/docs-tree.js.
   --d là số cấp thụt lề, đặt trên danh sách để mọi hàng bên trong thừa hưởng. */
:root,
[data-md-color-scheme="default"] {
  --vm-tree-icon: #718096;
  --vm-tree-guide: #dde5f0;
  --vm-tree-hover: #edf0f5;
  --vm-tree-focus-bg: #fff;
  --vm-tree-skeleton: #e6ebf2;
  --vm-tree-input-border: #d0d5dd;
}

/* Figma chưa có chế độ tối: màu dựng từ cùng bảng màu ở extra.css */
[data-md-color-scheme="slate"] {
  --vm-tree-icon: #8b9ab0;
  --vm-tree-guide: var(--vm-border);
  --vm-tree-hover: #1a2638;
  --vm-tree-focus-bg: var(--vm-bg);
  --vm-tree-skeleton: var(--vm-border);
  --vm-tree-input-border: var(--vm-border);
}

/* --- Khung: ô lọc và thẻ hỗ trợ đứng yên, chỉ vùng giữa cuộn ------------------ */
.md-sidebar--primary .md-sidebar__scrollwrap {
  margin: 0;
  overflow: hidden;
  scrollbar-gutter: auto;
  container-type: size;
}

.md-sidebar--primary .md-sidebar__inner {
  box-sizing: border-box;
  height: 100%;
}

.vm-tree {
  display: flex;
  flex-direction: column;
  gap: 18px;
  height: 100%;
  font-size: 14px;
  line-height: 20px;
}

.vm-tree__head,
.vm-tree__tabs {
  display: none;
}

/* Lề âm đưa thanh cuộn ra khoảng trống của cột; lề trong chừa chỗ cho viền focus */
.vm-tree__viewport {
  flex: 1 1 0;
  min-height: 0;
  margin: -2px -14px -2px -4px;
  padding: 2px 14px 2px 4px;
  overflow: hidden auto;
  overscroll-behavior: contain;
  scrollbar-color: var(--vm-tree-guide) transparent;
  scrollbar-width: thin;
}

.vm-tree__viewport:hover {
  scrollbar-color: var(--vm-tree-icon) transparent;
}

.vm-tree__list,
.vm-tree__root,
.vm-tree__group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.vm-tree__list {
  --d: 0;
}

.vm-tree__list[hidden],
.vm-tree__group[hidden] {
  display: none;
}

.vm-tree__section:not(:first-child) {
  margin-top: 14px;
}

.vm-tree__section + li:not(.vm-tree__section) {
  margin-top: 14px;
}

.vm-tree__label {
  display: flex;
  align-items: center;
  min-height: 26px;
  margin-bottom: 4px;
  color: var(--vm-muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 16px;
  text-transform: uppercase;
}

/* --- Hàng ------------------------------------------------------------------- */
.vm-tree__row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  box-sizing: border-box;
  min-height: 40px;
  padding: 10px 12px 10px calc(12px + var(--d) * 16px);
  border-radius: 6px;
  color: var(--vm-text-tree);
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.vm-tree__row:hover,
.vm-tree__row:focus {
  color: var(--vm-text-tree);
}

/* Màn cảm ứng giữ :hover sau khi chạm, hàng vừa bấm sẽ xám mãi */
@media (hover: hover) {
  .vm-tree__row:hover {
    background: var(--vm-tree-hover);
  }
}

.vm-tree__row[aria-busy] {
  cursor: progress;
}

/* Tên class dài như TwoFingersTouchZoomRotateHandler không có chỗ ngắt */
.vm-tree__text {
  min-width: 0;
  overflow-wrap: anywhere;
}

.vm-tree__icon {
  color: var(--vm-tree-icon);
}

.vm-tree__icon--page {
  --vm-icon: url("../assets/icons/page.svg");
}

.vm-tree__icon--branch {
  --vm-icon: url("../assets/icons/expand.svg");
  transform: rotate(-90deg);
  transition: transform .12s;
}

.vm-tree__row[aria-expanded="true"] > .vm-tree__icon--branch {
  transform: none;
}

/* Đường dẫn dọc đi qua tâm icon của hàng cha */
.vm-tree__group {
  position: relative;
}

.vm-tree__group::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(3px + var(--d) * 16px);
  z-index: 1;
  width: 1px;
  background: var(--vm-tree-guide);
  pointer-events: none;
}

.vm-tree__row[aria-current="page"],
.vm-tree__row[aria-current="page"]:hover {
  background: var(--vm-selected);
  color: var(--vm-primary);
  font-weight: 600;
}

.vm-tree__row[aria-current="page"] > .vm-tree__icon {
  color: var(--vm-primary);
}

.vm-tree__row[aria-current="page"]::before {
  content: "";
  position: absolute;
  top: 9px;
  bottom: 9px;
  left: 0;
  width: 3px;
  border-radius: 2px;
  background: var(--vm-primary);
}

/* Focus là viền xanh, tách với trang đang đọc là nền xanh */
.vm-tree__row:focus {
  outline: none;
}

.vm-tree__row:focus-visible,
.vm-tree__row--target {
  outline: 2px solid var(--vm-primary);
  outline-offset: -2px;
  background: var(--vm-tree-focus-bg);
}

/* --- Ô lọc và các trạng thái lọc ---------------------------------------------- */
.vm-tree__input {
  display: block;
  box-sizing: border-box;
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--vm-tree-input-border);
  border-radius: 8px;
  outline: none;
  background: var(--vm-bg);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .05);
  color: var(--vm-ink);
  font: inherit;
  appearance: none;
}

.vm-tree__input::placeholder {
  color: var(--vm-muted);
  opacity: 1;
}

.vm-tree__input::-webkit-search-cancel-button {
  display: none;
}

.vm-tree__input:focus {
  border-color: var(--vm-primary);
  box-shadow: 0 0 0 1px var(--vm-primary);
}

.vm-tree__empty,
.vm-tree__loading {
  padding: 8px 0;
}

.vm-tree__empty[hidden],
.vm-tree__loading[hidden] {
  display: none;
}

.vm-tree__empty p,
.vm-tree__loading p {
  margin: 0;
}

.vm-tree__empty-title {
  color: var(--vm-ink);
  font-size: 16px;
  font-weight: 600;
  line-height: 24px;
}

.vm-tree__clear {
  margin-top: 8px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--vm-primary);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.vm-tree__clear:hover {
  text-decoration: underline;
}

.vm-tree__loading {
  color: var(--vm-muted);
}

.vm-tree__loading span {
  display: block;
  height: 24px;
  margin-top: 12px;
  border-radius: 4px;
  background: var(--vm-tree-skeleton);
}

.vm-tree__loading span:nth-of-type(1) {
  width: 84%;
}

.vm-tree__loading span:nth-of-type(3) {
  width: 72%;
}

/* --- Thẻ hỗ trợ -------------------------------------------------------------- */
.vm-tree__support {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  box-sizing: border-box;
  padding: 8px 12px;
  border: 1px solid var(--vm-border);
  border-radius: 8px;
  background: var(--vm-bg);
  font-size: 12px;
  line-height: 18px;
}

.vm-tree__support-title {
  color: var(--vm-ink);
  font-weight: 700;
}

.vm-tree__support-link {
  color: var(--vm-primary);
  font-weight: 700;
  white-space: nowrap;
}

.vm-tree__support-link:hover {
  color: var(--vm-primary);
  text-decoration: underline;
}

/* Cột bị footer ép thấp hoặc màn hình nằm ngang: nhường chỗ cho cây */
@container (max-height: 400px) {
  .vm-tree__support {
    display: none;
  }
}

/* --- Bộ chọn tab: chỉ có trên drawer ------------------------------------------- */
.vm-tree__tabs {
  flex: none;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}

.vm-tree__tab {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 8px;
  border: 1px solid var(--vm-border);
  border-radius: 6px;
  background: var(--vm-bg);
  color: var(--vm-text-tree);
  font-weight: 600;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

/* Số tab lẻ thì tab cuối chiếm cả hàng, không để một ô trống */
.vm-tree__tab:last-child:nth-child(odd) {
  grid-column: 1 / -1;
}

.vm-tree__tab:hover,
.vm-tree__tab:focus {
  color: var(--vm-text-tree);
}

.vm-tree__tab[aria-current] {
  border-color: transparent;
  background: var(--vm-selected);
  color: var(--vm-primary);
}

/* --- Desktop: tab đã nằm ở hàng tab, cây chỉ cần phần của tab đang chọn -------- */
@media screen and (min-width: 76.25em) {
  /* Theme đặt chiều cao này sau lượt vẽ đầu; giá trị tạm tránh cây nhảy khi tải */
  .md-sidebar--primary .md-sidebar__scrollwrap {
    height: calc(100vh - var(--vm-header-h) - 56px);
  }

  /* Thẻ hỗ trợ dính đáy màn hình để vùng menu rộng ra khi hàng tab ẩn. Sticky cần cuộn
     theo cửa sổ nên khung ngoài không được overflow: hidden; vùng cuộn đã nằm ở
     .vm-tree__viewport. Bóng cùng màu nền che các hàng menu đi qua phía sau thẻ. */
  .md-sidebar--primary .md-sidebar__scrollwrap {
    overflow: visible;
  }

  .vm-tree__support {
    position: sticky;
    bottom: 20px;
    z-index: 1;
    box-shadow: 0 0 0 19px var(--vm-bg-subtle);
  }

  /* Hàng menu trôi vào sau thẻ thì mờ dần, không bị cắt ngang chữ */
  .vm-tree__support::before {
    position: absolute;
    right: -19px;
    bottom: calc(100% + 19px);
    left: -19px;
    height: 16px;
    background: linear-gradient(transparent, var(--vm-bg-subtle));
    content: "";
    pointer-events: none;
  }

  /* Chiều cao cột luôn bằng màn hình trừ header, nên lúc hàng tab còn hiện, đuôi vùng
     cuộn thò khỏi đáy màn hình đúng bằng hàng tab. Chừa sẵn quãng đó để mục cuối menu
     luôn kéo được lên trên thẻ. */
  .vm-tree__viewport {
    padding-bottom: calc(var(--vm-tabs-h) + 20px);
  }

  .vm-tabs-pinned .vm-tree__viewport {
    padding-bottom: 20px;
  }

  /* Trang ngắn không để footer ép cây thấp lại ngay từ đầu */
  .md-main__inner {
    min-height: calc(100vh - var(--vm-header-h) - 56px);
  }
}

/* --- Drawer dưới 1220px: cùng một cây, thêm bộ chọn tab ở trên ------------------ */
@media screen and (max-width: 76.1875em) {
  :root {
    --vm-drawer-w: min(320px, 86vw);
  }

  [dir] .md-sidebar--primary {
    left: calc(var(--vm-drawer-w) * -1);
    width: var(--vm-drawer-w);
    background: var(--vm-bg-subtle);
  }

  [data-md-toggle="drawer"]:checked ~ .md-container .md-sidebar--primary {
    transform: translateX(var(--vm-drawer-w));
  }

  [dir] .md-sidebar--primary .md-sidebar__inner {
    padding: 0 16px max(16px, env(safe-area-inset-bottom));
  }

  /* Trang sau lớp phủ không cuộn theo khi vuốt trong drawer */
  html:has(#__drawer:checked) {
    overflow: hidden;
  }

  /* Nút chat nổi trên mọi lớp, che hàng menu và thẻ hỗ trợ */
  html:has(#__drawer:checked) iframe[title="AI Agent Chat Widget"] {
    visibility: hidden;
  }

  .vm-tree {
    gap: 12px;
  }

  .vm-tree__head {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    margin: 0 -16px -12px;
    padding: 0 8px 0 16px;
  }

  .vm-tree__title {
    color: var(--vm-ink);
    font-size: 16px;
    font-weight: 700;
  }

  .vm-tree__close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--vm-text-tree);
    cursor: pointer;
  }

  .vm-tree__close svg {
    width: 24px;
    height: 24px;
    fill: currentcolor;
  }

  /* 16px để iOS không tự phóng to trang khi chạm vào ô nhập */
  .vm-tree__input {
    font-size: 16px;
  }

  .vm-tree__viewport {
    margin: -2px -12px -2px -4px;
    padding: 2px 12px 2px 4px;
  }

  .vm-tree__tabs {
    display: grid;
  }

  @container (max-height: 560px) {
    .vm-tree__support {
      display: none;
    }
  }
}

/* Cột phải bị ẩn dưới 960px; trên đó mục lục trong trang đã có chỗ riêng */
@media screen and (min-width: 60em) {
  .vm-tree__toc {
    display: none;
  }
}
