/*
 * Shared responsive guardrails for every chapter and research note.
 * Keep this file loaded after each page's inline styles so these rules win
 * over older page-specific desktop declarations.
 */

html,
body {
  max-width: 100%;
  overflow-x: hidden;
  overflow-x: clip;
}

:root {
  --topbar-height: 71px;
}

body {
  padding-top: var(--topbar-height);
}

header.site {
  position: fixed;
  inset: 0 0 auto;
  width: 100%;
}

.site-inner,
.shell,
.lockup,
.header-right,
article,
article > *,
.pc > *,
.receipts > *,
.nextrow > *,
.meta-card,
.fig,
.loop,
.tablewrap,
.codeblock,
.out {
  min-width: 0;
}

.lockup {
  flex: 1 1 auto;
  overflow: hidden;
}

.book-tag {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-right {
  flex: 0 0 auto;
}

article,
article > *,
img,
video,
iframe,
svg {
  max-width: 100%;
}

article a,
.refs li,
dd,
td,
th {
  overflow-wrap: anywhere;
}

pre {
  max-width: 100%;
}

.tablewrap,
.codeblock pre,
.out pre,
.loop pre,
.fig .mermaid,
.source-image-link {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-inline: contain;
  scrollbar-width: thin;
}

/* Keep the reading column centered while the contents drawer floats above it. */
.shell,
.shell.toc-hidden {
  grid-template-columns: minmax(0, 1fr) !important;
}

aside.toc {
  position: fixed;
  top: var(--topbar-height);
  bottom: 0;
  left: 0;
  z-index: 45;
  width: min(21rem, 86vw);
  height: auto;
  max-height: none;
  padding: 28px 18px 32px;
  overflow-y: auto;
  background: var(--canvas);
  border-right: 1px solid var(--hairline);
  box-shadow: 12px 0 36px rgba(0, 8, 27, 0.12);
  transition: transform 220ms ease;
}

.shell.toc-hidden aside.toc {
  display: block;
}

.toc-scrim {
  top: var(--topbar-height);
}

article h2,
article h3 {
  scroll-margin-top: calc(var(--topbar-height) + 24px);
}

/* Fixed, section-aware page navigation in the space beside the article. */
.page-nav {
  position: fixed;
  top: calc(var(--topbar-height) + 32px);
  right: max(18px, calc((100vw - 1280px) / 2 + 18px));
  z-index: 35;
  width: 17rem;
  max-height: calc(100dvh - var(--topbar-height) - 64px);
  padding: 18px 14px;
  overflow-y: auto;
  color: var(--ink-muted);
  background: var(--header-bg);
  border: 1px solid var(--hairline);
  border-radius: var(--r-md);
  box-shadow: var(--card-shadow);
  backdrop-filter: blur(12px);
  scrollbar-width: thin;
}

.page-nav__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 6px 12px;
  font-family: "Geist Mono", monospace;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}

.page-nav__progress {
  color: var(--accent-ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}

.page-nav ol {
  position: relative;
  margin: 0;
  padding: 0 0 0 12px;
  list-style: none;
}

.page-nav ol::before {
  content: "";
  position: absolute;
  top: 5px;
  bottom: 5px;
  left: 2px;
  width: 1px;
  background: var(--hairline);
}

.page-nav li {
  position: relative;
}

.page-nav a {
  display: block;
  padding: 6px 8px;
  overflow: hidden;
  color: var(--ink-muted);
  font-family: "Geist", system-ui, sans-serif;
  font-size: 13px;
  line-height: 1.35;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-radius: 9px;
  transition: color 160ms ease, background 160ms ease;
}

.page-nav a::before {
  content: "";
  position: absolute;
  top: 50%;
  left: -13px;
  width: 7px;
  height: 7px;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 50%;
  transform: translateY(-50%);
  transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.page-nav a:hover {
  color: var(--ink);
  background: var(--nav-hover);
  text-decoration: none;
}

.page-nav a.active {
  color: var(--ink);
  background: var(--nav-hover);
  font-weight: 500;
}

.page-nav a.active::before {
  background: var(--accent);
  border-color: var(--accent);
  transform: translateY(-50%) scale(1.2);
}

@media (min-width: 981px) {
  aside.toc {
    transform: translateX(0);
  }

  .shell.toc-hidden aside.toc {
    transform: translateX(-100%);
  }
}

@media (max-width: 980px) {
  :root {
    --topbar-height: 61px;
  }

  .shell,
  .shell.toc-hidden {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  #toc {
    top: var(--topbar-height);
    height: calc(100dvh - var(--topbar-height));
    padding-top: 24px;
    transform: translateX(-100%);
  }

  #toc.open {
    transform: translateX(0);
  }

  article {
    width: 100%;
    max-width: none;
  }

  .shell.toc-hidden aside.toc {
    display: block;
  }

  .pc,
  .receipts,
  .nextrow {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .nextrow .spacer {
    display: none;
  }
}

@media (max-width: 1240px) {
  .page-nav {
    display: none;
  }
}

@media (max-width: 600px) {
  .site-inner {
    padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
    gap: 8px;
  }

  .header-right {
    gap: 6px;
  }

  .book-tag {
    font-size: 12px;
    letter-spacing: 0;
  }

  .iconbtn {
    width: 40px;
    height: 40px;
  }

  #toc-toggle {
    min-height: 40px;
    padding: 7px 12px;
    font-size: 13px;
  }

  .shell {
    gap: 20px;
    padding: 24px max(16px, env(safe-area-inset-right)) 56px max(16px, env(safe-area-inset-left));
  }

  article h1 {
    font-size: clamp(29px, 8.5vw, 36px);
    line-height: 1.1;
  }

  article h2 {
    font-size: clamp(23px, 6.5vw, 28px);
    margin-top: 48px;
  }

  article h3 {
    font-size: 19px;
  }

  .standfirst,
  article p.lede {
    font-size: 17px;
    line-height: 1.6;
  }

  article p,
  article ul.body li,
  article ol.body li {
    font-size: 16px;
    line-height: 1.65;
  }

  .byline {
    margin-bottom: 36px;
  }

  .fig,
  .loop,
  .tablewrap,
  .meta-card,
  .pc .col,
  .receipt,
  .recap {
    padding: 18px;
    border-radius: var(--r-md);
  }

  .fig,
  .loop,
  .tablewrap,
  .pc,
  .receipts {
    margin-top: 32px;
    margin-bottom: 32px;
  }

  .loop .panel-eyebrow,
  .fig .panel-eyebrow {
    padding-right: 64px;
  }

  .codeblock pre,
  .out pre,
  .loop pre {
    font-size: 12.5px;
    line-height: 1.6;
  }

  .nextrow {
    margin-top: 48px;
  }

  .next,
  .nextrow a.next {
    align-items: flex-start;
    flex-wrap: wrap;
    padding: 20px;
  }

  .next .title,
  .nextrow .title {
    font-size: 17px;
  }

  .foot-inner {
    padding: 24px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left));
  }
}

@media (max-width: 380px) {
  :root {
    --topbar-height: 59px;
  }

  .site-inner {
    gap: 6px;
  }

  .header-right {
    gap: 4px;
  }

  .iconbtn {
    width: 38px;
    height: 38px;
  }

  #toc-toggle {
    min-height: 38px;
    padding-inline: 10px;
  }

  .shell {
    padding-inline: 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  aside.toc,
  .page-nav a,
  .page-nav a::before {
    transition: none;
  }
}
