/* Phones, tablets and other touch screens. Loaded last so it can adjust any component.
   - Touch: bigger tap targets, press feedback instead of sticky hover, 16px fields (no iOS zoom on focus).
   - Small screens: drawers, bottom-sheet dialogs, a full-screen search, full-width menus and toasts.
   - Notched phones: content stays clear of the rounded corners and the home indicator (viewport-fit=cover). */

/* ---------- Everywhere ---------- */
html {
  -webkit-tap-highlight-color: transparent;
}
a,
button,
summary,
label,
select,
[role='button'] {
  touch-action: manipulation;
}
/* The page behind an open drawer, sheet or dialog doesn't scroll (main.js LockScroll). */
:root.scroll-locked,
:root.scroll-locked body {
  overflow: hidden;
}
/* Scrolling to the end of a panel doesn't carry on into the page behind it. */
.sidebar-scroll,
.modal-body,
.search-results,
.dropdown-menu,
.admin-nav-scroll,
.toc,
.toc-mobile ul {
  overscroll-behavior: contain;
}
.touch-only {
  display: none;
}

/* Safe areas: landscape iPhones put the notch at the side; the home indicator sits at the bottom. */
.header-inner {
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
}
.footer-inner {
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  padding-bottom: calc(28px + env(safe-area-inset-bottom));
}
.toasts {
  bottom: calc(20px + env(safe-area-inset-bottom));
  right: max(20px, env(safe-area-inset-right));
}
.admin-nav-foot {
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

/* WebKit draws its own search-field chrome; the dialog's input row already provides it. */
.search-input-row input {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
}
.search-input-row input::-webkit-search-decoration {
  -webkit-appearance: none;
}

/* ---------- Drawer contents (site menu and the docs sidebar's copy of the header links) ---------- */
.site-menu-title {
  display: flex;
  align-items: center;
  min-width: 0;
  padding-left: 4px;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drawer-links .nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 550;
  color: var(--text);
}
.drawer-links .nav-link svg {
  color: var(--text-subtle);
}
.sidebar-site-links {
  margin: 0 0 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
@media (min-width: 961px) {
  .sidebar.site-menu,
  .sidebar-site-links {
    display: none;
  }
}

/* ---------- "On this page" for screens without the side table of contents ---------- */
.toc-mobile {
  display: none;
}
@media (max-width: 1200px) {
  html:has(.toc-mobile) {
    scroll-padding-top: calc(var(--header-h) + 76px);
  }
  .toc-mobile {
    position: sticky;
    top: calc(var(--header-h) + 8px);
    z-index: 20;
    display: block;
    margin: 0 0 28px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: saturate(180%) blur(14px);
    box-shadow: var(--shadow-sm);
  }
  .toc-mobile summary {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 14px;
    list-style: none;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    user-select: none;
  }
  .toc-mobile summary::-webkit-details-marker {
    display: none;
  }
  .toc-mobile summary svg {
    width: 16px;
    height: 16px;
    flex: none;
    color: var(--text-subtle);
  }
  .toc-mobile summary .grow {
    flex: none;
  }
  .toc-mobile-current {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: right;
    font-weight: 500;
    color: var(--text-subtle);
  }
  .toc-mobile[open] .toc-mobile-current {
    visibility: hidden;
  }
  .toc-mobile .chev {
    transition: transform 180ms var(--ease);
  }
  .toc-mobile[open] .chev {
    transform: rotate(180deg);
  }
  .toc-mobile ul {
    max-height: min(52vh, 420px);
    margin: 0;
    padding: 6px;
    overflow-y: auto;
    list-style: none;
    border-top: 1px solid var(--border);
  }
  .toc-mobile a {
    display: block;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    line-height: 1.4;
    color: var(--text-muted);
  }
  .toc-mobile a.active {
    color: var(--brand-text);
    background: var(--brand-soft);
    font-weight: 550;
  }
  .toc-mobile .lvl-3 a {
    padding-left: 22px;
  }
  .toc-mobile .lvl-4 a {
    padding-left: 34px;
  }
}

/* ---------- Tablets and phones (drawer navigation) ---------- */
@media (max-width: 960px) {
  .sidebar {
    padding-left: env(safe-area-inset-left);
    padding-bottom: env(safe-area-inset-bottom);
    touch-action: pan-y;
  }
  .sidebar:not(.open) {
    /* Off-screen drawers stay out of the tab order and away from screen readers. */
    visibility: hidden !important;
    transition: transform 220ms var(--ease), visibility 0s 220ms;
  }
  .sidebar.open {
    transition: transform 220ms var(--ease), visibility 0s;
  }
  .sidebar-top {
    align-items: center;
  }
}
@media (max-width: 900px) {
  .admin-nav {
    padding-left: env(safe-area-inset-left);
    touch-action: pan-y;
  }
  .admin-nav:not(.open) {
    visibility: hidden;
    transition: transform 220ms var(--ease), visibility 0s 220ms;
  }
  .admin-nav.open {
    transition: transform 220ms var(--ease), visibility 0s;
  }
  .admin-topbar {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }
  .admin-content {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }
}

/* ---------- Touch screens ---------- */
@media (pointer: coarse) {
  .touch-only {
    display: inline;
  }
  .hide-touch,
  .search-trigger .keys,
  .search-footer,
  .hero-search kbd {
    display: none !important;
  }

  /* Tap targets of at least 40px (44px for primary rows). */
  .btn {
    height: 40px;
  }
  .btn-sm {
    height: 36px;
  }
  .btn-lg {
    height: 46px;
  }
  .btn-icon {
    width: 40px;
  }
  .btn-icon.btn-sm {
    width: 36px;
  }
  .header-actions .btn-icon {
    width: 40px;
    height: 40px;
  }
  .search-trigger {
    height: 40px;
  }
  .version-select {
    height: 36px;
  }
  .nav-link {
    padding: 10px 12px;
  }
  .nav-category {
    min-height: 42px;
    padding: 10px 8px;
  }
  .dropdown-item {
    min-height: 44px;
    padding: 10px 12px;
  }
  .admin-nav a {
    min-height: 42px;
  }
  .toc-actions a,
  .toc-actions button {
    padding: 8px 0;
  }
  .segmented button,
  .segmented a {
    height: 34px;
  }
  .tab {
    padding: 12px 14px;
  }
  .copy-button {
    height: 32px;
    padding: 0 10px;
  }
  .hero-suggestions button {
    padding: 7px 14px;
  }
  .footer-links {
    gap: 6px 4px;
  }
  .footer-links a {
    padding: 8px 10px;
    margin: 0 -2px;
  }
  .breadcrumbs a {
    padding: 4px 0;
  }

  /* 16px text in fields stops iOS zooming the page when one is focused. */
  .input,
  .select,
  .textarea,
  .input-sm,
  .select.input-sm,
  .search-input-row input,
  .composer-input,
  .editor-textarea,
  .tag-input input {
    font-size: 16px;
  }
  /* Unstyled fields too. Element-only specificity: it overrides base.css's `font: inherit` but any
     class with a larger designed size (the editor title) still wins. */
  input,
  textarea,
  select {
    font-size: 16px;
  }
  .input,
  .select {
    height: 42px;
  }
  .input-sm,
  .select.input-sm {
    height: 38px;
  }

  /* Press feedback in place of hover effects, which stick after a tap on touch screens. */
  .category-card:hover,
  .pager a:hover,
  .btn:active:not(:disabled) {
    transform: none;
  }
  .btn:active:not(:disabled) {
    opacity: 0.75;
  }
  .table tbody tr:hover {
    background: transparent;
    font-weight: inherit;
  }
  .nav-link:active,
  .nav-category:active,
  .dropdown-item:active,
  .search-result:active,
  .page-list a:active,
  .toc-mobile a:active,
  .admin-nav a:active,
  .table tbody tr:active {
    background: var(--bg-hover);
  }
  .category-card:active,
  .related-card:active,
  .pager a:active {
    border-color: var(--brand-border);
    background: var(--brand-softer);
  }
  /* Heading anchors only appear on hover with a mouse; on touch they'd never show. */
  .prose .heading-anchor {
    display: none;
  }
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
  /* Search fills the screen, so results have room above the keyboard. */
  .modal-backdrop[data-search] {
    align-items: stretch;
    padding: 0;
    backdrop-filter: none;
  }
  .search-modal {
    max-width: none;
    max-height: none;
    height: 100%;
    border: 0;
    border-radius: 0;
    padding-top: env(safe-area-inset-top);
    animation: fade-in 160ms var(--ease);
  }
  .search-input-row {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
  }
  .search-results {
    flex: 1;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  .search-result {
    padding: 12px;
  }
}

@media (max-width: 560px) {
  .hide-sm {
    display: none !important;
  }
  .header-inner {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .header-actions {
    gap: 0;
  }

  /* Header menus open full-width under the header instead of hanging off its right edge. */
  .header .dropdown {
    position: static;
  }
  .header .dropdown-menu {
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    top: calc(100% + 6px);
    min-width: 0;
    max-height: calc(100dvh - var(--header-h) - 24px);
    overflow-y: auto;
  }
  .header .notif-menu {
    width: auto;
  }

  /* Dialogs become bottom sheets: anchored to the bottom edge, pulled down by the header to close. */
  .modal-backdrop:not([data-search]) {
    align-items: flex-end;
    padding: env(safe-area-inset-top) 0 0;
  }
  .modal-backdrop:not([data-search]) > .modal {
    max-width: none;
    max-height: 92vh;
    max-height: 92dvh;
    border-bottom: 0;
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
    animation: sheet-up 280ms var(--ease);
  }
  .modal-backdrop:not([data-search]) .modal-header {
    position: relative;
    padding-top: 24px;
    touch-action: none;
  }
  .modal-backdrop:not([data-search]) .modal-header::before {
    content: '';
    position: absolute;
    top: 9px;
    left: 50%;
    width: 38px;
    height: 5px;
    border-radius: 3px;
    background: var(--border-strong);
    transform: translateX(-50%);
  }
  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 16px;
    padding-right: 16px;
  }
  .modal-footer {
    flex-wrap: wrap;
  }
  .modal-footer > .btn,
  .modal-footer > form > .btn {
    flex: 1;
  }

  /* Toasts span the bottom of the screen. */
  .toasts {
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(12px + env(safe-area-inset-bottom));
    max-width: none;
  }

  /* Reading: code blocks run edge to edge so more of each line fits. */
  .prose > .code-frame,
  .prose > pre.code-block,
  .prose > pre.mermaid {
    margin-left: -20px;
    margin-right: -20px;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
  }
  .prose pre {
    font-size: 13px;
  }
  .content {
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
  }
  .feedback {
    flex-direction: column;
    align-items: stretch;
  }
  .feedback > .row {
    flex-wrap: wrap;
  }
  .page-meta {
    gap: 8px 14px;
  }
  .composer-guest .input {
    width: 100%;
  }
  .narrow-page {
    padding-top: 32px;
  }
  .page-title {
    font-size: 26px;
  }
  .status-page .code {
    font-size: 56px;
  }
  .home-body {
    padding-top: 40px;
  }

  /* Page editor: the title gets its own row and the formatting toolbar scrolls sideways. */
  .editor-top {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .editor-title {
    order: 1;
    flex-basis: 100%;
    min-width: 0;
  }
  .editor-top .save-state {
    order: 2;
    flex-basis: 100%;
  }
  .editor-top > :not(.editor-title):not(.save-state) {
    order: 3;
  }
  .editor-top > :first-child {
    order: 0;
  }
  .editor-toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .editor-toolbar::-webkit-scrollbar {
    display: none;
  }
  .editor-toolbar .hint {
    display: none;
  }
  .editor-textarea {
    padding: 16px;
  }
}

@keyframes sheet-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: none;
  }
}
