/* ---------------------------------------------------------------------
   Phase 2 — Mobile navigation, touch targets, focus return, and header
------------------------------------------------------------------------ */

@media (max-width: 820px) {
  .site-header {
    z-index: 100;
  }

  /* The TOC is moved inside the article by the router on mobile.
     Keep it explicitly visible after dynamic page injection. */
  .toc-col {
    display: block !important;
    position: static;
    width: 100%;
    height: auto;
    max-height: none;
    overflow: visible;
    padding: 0;
    margin: 0 0 1.5rem;
    order: initial;
  }

  .toc-col h2 {
    margin-top: 0;
    padding-top: 0.75rem;
  }

  .toc-col ul {
    margin-bottom: 0;
  }

  .nav-toggle,
  .header-actions .icon-btn,
  .password-toggle {
    min-width: 44px;
    min-height: 44px;
  }

  .nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
  }

  .sidebar {
    top: var(--header-h);
    height: calc(100dvh - var(--header-h));
    width: min(88vw, 340px);
    max-width: 340px;
    padding: 1rem 0.75rem 2rem;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar-scrim {
    top: var(--header-h);
    inset-block-end: 0;
    touch-action: manipulation;
  }

  .tree-row {
    min-height: 44px;
    gap: 0.35rem;
  }

  .tree-toggle,
  .tree-toggle-spacer {
    width: 44px;
    min-width: 44px;
    height: 44px;
  }

  .tree-toggle {
    border-radius: var(--radius);
  }

  .tree-toggle svg {
    width: 14px;
    height: 14px;
  }

  .tree-link,
  .tree-label-only {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0.55rem 0.65rem;
  }

  .tree-link {
    white-space: normal;
  }

  .sidebar-tree ul {
    margin-left: 0.5rem;
    padding-left: 0.5rem;
  }

  .site-header .brand-logo {
    width: 92px;
    height: 38px;
  }

  .header-actions {
    gap: 0.2rem;
  }

  .header-actions .icon-btn {
    padding: 0.4rem;
  }

  .header-actions #install-app span {
    display: none;
  }

  .header-search {
    min-height: 44px;
  }

  .search-input {
    min-height: 44px;
  }

  .page-tool {
    min-height: 44px;
  }

  .back-to-top {
    min-width: 44px;
    min-height: 44px;
    margin: 0.5rem;
  }
}

@media (max-width: 640px) {
  .site-header {
    grid-template-columns: 44px minmax(0, 1fr) auto;
    grid-template-rows: 44px 44px;
    min-height: 96px;
    padding: 0.4rem 0.6rem;
    gap: 0.35rem 0.45rem;
  }

  .nav-toggle {
    grid-column: 1;
    grid-row: 1;
  }

  .brand {
    grid-column: 2;
    grid-row: 1;
    justify-content: center;
  }

  .header-actions {
    grid-column: 3;
    grid-row: 1;
  }

  .header-search {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .brand-logo {
    width: 88px;
    height: 36px;
  }
}

@media (max-width: 420px) {
  .site-header {
    grid-template-columns: 44px minmax(0, 1fr) 44px;
    padding-inline: 0.5rem;
  }

  .header-actions #install-app {
    display: none;
  }

  .site-header .brand-logo {
    width: 84px;
    height: 34px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar {
    transition: none;
  }
}
