.skip-link {
  position: absolute;
  top: -100px;
  left: 1rem;
  z-index: 1000;
  padding: 0.5rem 1rem;
  background: var(--color-primary);
  color: #fff;
  font-family: var(--font-family-ui);
  text-decoration: none;
  border-radius: var(--radius-sm);
}

.skip-link:focus {
  top: 1rem;
}

:where(
  a,
  button,
  summary,
  input,
  select,
  textarea,
  .font-controls__btn,
  .header-settings__toggle,
  .theme-toggle,
  .nav-toggle
):focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.site-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--header-height);
  background: var(--color-header-bg);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
}

.site-header__inner {
  display: flex;
  align-items: stretch;
  width: 100%;
  height: 100%;
  padding: 0;
}

.site-brand {
  position: relative;
  flex: 0 0 var(--sidebar-width);
  display: flex;
  align-items: center;
  gap: 0.625rem;
  width: var(--sidebar-width);
  padding: 0.5rem 0.75rem;
  background: var(--color-bg);
  color: var(--color-primary);
  text-decoration: none;
  min-width: 0;
}

.site-brand__icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.0625rem;
  height: 2.0625rem;
}

.site-brand__icon .scales-icon {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

:root .site-brand__icon .scales-icon,
[data-theme="light"] .site-brand__icon .scales-icon {
  filter: invert(21%) sepia(98%) saturate(2200%) hue-rotate(205deg) brightness(0.78) contrast(1.05);
}

[data-theme="dark"] .site-brand__icon .scales-icon {
  filter: invert(72%) sepia(18%) saturate(650%) hue-rotate(185deg) brightness(0.95);
}

.site-brand__title {
  font-family: var(--font-family-ui);
  font-size: calc(1.2375rem * var(--font-scale));
  font-weight: 600;
  line-height: 1.2;
  color: var(--color-text);
}

.site-brand:hover .site-brand__title {
  color: var(--color-primary);
}

.site-header__controls {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0 1.25rem;
  min-width: 0;
}

.site-shell {
  min-height: calc(100vh - var(--header-height));
  padding-left: var(--sidebar-width);
}

.sidebar {
  position: fixed;
  top: var(--header-height);
  left: 0;
  width: var(--sidebar-width);
  height: calc(100vh - var(--header-height));
  overflow-y: auto;
  background: var(--color-bg);
  border-right: 1px solid var(--color-border);
  padding: 1rem 0.75rem 2rem;
  z-index: 90;
}

.sidebar-backdrop {
  display: none;
}

.sidebar-backdrop[hidden] {
  display: none !important;
}

.site-main {
  padding: 1.5rem var(--site-main-padding-x) 3rem;
  min-width: 0;
  max-width: 100%;
  min-height: calc(100vh - var(--header-height));
  background: var(--color-surface);
}

.main-content {
  max-width: min(var(--content-max-width), calc(100vw - var(--content-inline-inset)));
  width: 100%;
}

.main-content > .page-title {
  margin: 0 0 1rem;
  font-family: var(--font-family-ui);
  font-size: var(--font-size-h1);
  font-weight: 600;
  line-height: 1.25;
  color: var(--color-primary);
}

.site-footer {
  margin-left: var(--sidebar-width);
  padding: 1.5rem;
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
  font-family: var(--font-family-ui);
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.site-footer p {
  margin: 0;
  max-width: none;
}

.font-controls {
  display: flex;
  align-items: center;
  gap: 0.125rem;
  font-family: var(--font-family-ui);
}

.font-controls__btn {
  min-width: 2rem;
  padding: 0.25rem 0.375rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: var(--font-size-sm);
  cursor: pointer;
}

.font-controls__btn:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.header-settings {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-settings__toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
}

.header-settings__toggle:hover {
  border-color: var(--color-primary);
}

.header-settings__icon {
  display: block;
}

.header-settings__panel {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.theme-toggle,
.nav-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
}

.theme-toggle:hover,
.nav-toggle:hover {
  border-color: var(--color-primary);
}

.nav-toggle {
  display: none;
  flex-direction: column;
  gap: 4px;
  padding: 0.4rem;
}

.nav-toggle__bar {
  display: block;
  width: 18px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
}

.nav-tree {
  list-style: none;
  margin: 0;
  padding: 0;
  --nav-tree-gutter: 0.75rem;
  --nav-tree-pad-x: 0.5rem;
  --nav-tree-pad-y: 0.2rem;
}

.nav-tree__details {
  margin: 0;
}

.nav-tree__summary {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0;
  list-style: none;
  cursor: pointer;
  padding: 0;
}

.nav-tree__summary::-webkit-details-marker {
  display: none;
}

.nav-tree__summary::marker {
  display: none;
}

.nav-tree__summary::before {
  content: "▸";
  flex-shrink: 0;
  display: inline-block;
  width: var(--nav-tree-gutter);
  font-size: 0.7rem;
  line-height: 1;
  color: var(--color-text-secondary);
  transition: transform 0.15s ease;
}

.nav-tree__details[open] > .nav-tree__summary::before {
  transform: rotate(90deg);
}

.nav-tree__link,
.nav-tree__label {
  display: block;
  padding: var(--nav-tree-pad-y) var(--nav-tree-pad-x);
  font-family: var(--font-family-ui);
  font-size: var(--font-size-nav);
  line-height: 1.45;
  border-radius: var(--radius-sm);
}

/* Leaf rows: reserve the same gutter as expandable chevrons */
.nav-tree__item > .nav-tree__link,
.nav-tree__item > .nav-tree__label {
  padding-left: calc(var(--nav-tree-gutter) + var(--nav-tree-pad-x));
  font-weight: 500;
}

.nav-tree__summary .nav-tree__link,
.nav-tree__summary .nav-tree__label {
  display: block;
  flex: 1;
  min-width: 0;
  margin-top: 0;
  text-transform: none;
  font-size: var(--font-size-nav);
  font-weight: 500;
  letter-spacing: normal;
  padding: var(--nav-tree-pad-y) var(--nav-tree-pad-x);
}

/* Expand-only parents (no URL) must match sibling link styling, not section headers */
.nav-tree__summary > .nav-tree__label {
  color: var(--color-text);
}

.nav-tree .nav-tree {
  padding-left: 0.75rem;
  margin-top: 0.125rem;
  border-left: 1px solid var(--color-border);
}

.nav-tree__item {
  margin-bottom: 0.125rem;
}

.nav-tree__link {
  color: var(--color-text);
  text-decoration: none;
}

.nav-tree__link:visited {
  color: var(--color-text);
}

.nav-tree__link:hover {
  color: var(--color-primary);
  background: var(--color-primary-subtle);
}

.nav-tree__link--active {
  color: var(--color-primary);
  font-weight: 600;
  background: var(--color-primary-subtle);
}

.nav-tree__link--active:visited {
  color: var(--color-primary);
}

.nav-tree__label {
  color: var(--color-text);
  font-weight: 600;
  font-size: calc(0.75rem * var(--font-scale));
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.75rem;
}

.breadcrumbs {
  margin-bottom: 1rem;
  max-width: min(var(--content-max-width), calc(100vw - var(--content-inline-inset)));
}

.breadcrumbs__list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.5rem;
  list-style: none;
  margin: 0;
  padding: 0;
  font-family: var(--font-family-ui);
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.breadcrumbs__item:not(:last-child)::after {
  content: "›";
  margin-left: 0.5rem;
  opacity: 0.6;
}

.breadcrumbs__item a {
  color: var(--color-text-secondary);
  text-decoration: none;
}

.breadcrumbs__item a:hover {
  color: var(--color-primary);
}

.chapter-nav {
  margin: 0.75rem 0 1.25rem;
  padding: 0.75rem 0;
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  max-width: min(var(--content-max-width), calc(100vw - var(--content-inline-inset)));
}

.main-content + .chapter-nav {
  margin-top: 2rem;
  border-top: 1px solid var(--color-border);
}

.chapter-nav__inner {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
}

.chapter-nav__link {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
  max-width: 45%;
  font-family: var(--font-family-ui);
  font-size: var(--font-size-sm);
  text-decoration: none;
  color: var(--color-text-secondary);
}

.chapter-nav__link:hover {
  color: var(--color-primary);
}

.chapter-nav__link--next {
  text-align: right;
  margin-left: auto;
}

.chapter-nav__dir {
  font-size: calc(0.75rem * var(--font-scale));
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.chapter-nav__title {
  color: var(--color-text);
  line-height: 1.35;
}

.chapter-nav__link:hover .chapter-nav__title {
  color: var(--color-primary);
}

.chapter-nav__spacer {
  flex: 1;
}

@media (max-width: 960px) {
  .site-header {
    background: var(--color-bg);
  }

  .site-brand {
    flex: 1 1 auto;
    width: auto;
    max-width: none;
    border-right: none;
    min-width: 0;
  }

  .site-brand__title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .site-header__controls {
    flex: 0 0 auto;
    padding: 0 0.75rem;
    gap: 0.375rem;
  }

  .header-settings {
    position: relative;
  }

  .header-settings__toggle {
    display: inline-flex;
  }

  .header-settings__panel {
    display: none;
    position: absolute;
    top: calc(100% + 0.375rem);
    right: 0;
    z-index: 110;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
  }

  .header-settings.is-open .header-settings__panel {
    display: flex;
  }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-sm);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-backdrop.is-visible {
    display: block;
    position: fixed;
    inset: var(--header-height) 0 0 0;
    background: rgb(0 0 0 / 45%);
    z-index: 80;
  }

  .site-main,
  .site-footer {
    /* shell padding removed on mobile — sidebar overlays */
  }

  .site-shell {
    padding-left: 0;
  }

  /* Sidebar overlays on mobile — don't subtract its width from content */
  :root {
    --content-inline-inset: calc(2 * var(--site-main-padding-x));
  }

  .page-content--decorated::before {
    --page-decor-left: var(--site-main-padding-x);
  }

  .site-footer {
    margin-left: 0;
  }

  .nav-toggle {
    display: inline-flex;
  }
}

@media (min-width: 1440px) {
  :root {
    --content-max-width: 68rem;
  }
}

@media (min-width: 1920px) {
  :root {
    /* Keep a hard cap — do not expand to full viewport (that edge-bleeds heroes). */
    --content-max-width: 72rem;
    --site-main-padding-x: 2rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
