/*
 * Reading layout for blog articles, reaction guides and molecule guides.
 *
 * Article on the left, "On this page" on the right. The list sticks under the
 * site top bar and scrolls on its own when it is longer than the screen.
 * Pages opt in with <body class="md-read">; /js/article-toc.js fills the right
 * column. Layout only: every page keeps its own text, headings, links, images,
 * embeds and metadata exactly as written.
 */

body.md-read {
  --rd-col: 740px;
  --rd-rail: 228px;
  --rd-gap: 56px;
  --rd-top: 45px; /* sticky site top bar (site-topbar.css): 44px + border */
  --rd-ink: #0f172a;
  --rd-text: #334155;
  --rd-muted: #64748b;
  --rd-line: #e5e7eb;
  --rd-brand: #0f766e;
  background: #fff;
}

@media (prefers-reduced-motion: no-preference) {
  html:has(> body.md-read) {
    scroll-behavior: smooth;
  }
}

/* Jump targets land below the sticky top bar. */
body.md-read main [id] {
  scroll-margin-top: calc(var(--rd-top) + 20px);
}

/* ── Page shell: one reading column (phones, tablets) ───────────────────── */

body.md-read > main {
  width: min(calc(100% - 32px), var(--rd-col));
  max-width: none;
  margin: 0 auto;
  padding: 24px 0 64px;
  display: block;
  background: none;
}

/* ── Wide screens: article left, contents right ─────────────────────────── */

@media (min-width: 1080px) {
  body.md-read > main {
    width: min(calc(100% - 48px), calc(var(--rd-col) + var(--rd-gap) + var(--rd-rail)));
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--rd-rail);
    column-gap: var(--rd-gap);
    row-gap: 0;
    align-items: start;
    padding-top: 32px;
  }

  body.md-read > main > * {
    grid-column: 1;
    min-width: 0;
  }

  /* The right column spans every row so it can stick for the whole article. */
  body.md-read > main > :is(.md-rail, aside.outline, aside.toc) {
    grid-column: 2;
    grid-row: 1 / span 40;
    align-self: start;
    position: sticky;
    top: calc(var(--rd-top) + 28px);
    max-height: calc(100vh - var(--rd-top) - 52px);
    max-height: calc(100dvh - var(--rd-top) - 52px);
  }

  body.md-read .md-rail {
    display: flex;
    flex-direction: column;
    gap: 24px;
    min-height: 0;
  }

  /* Inside the column the list shrinks and scrolls; the column itself sticks. */
  body.md-read .md-rail > .md-toc {
    position: static;
    top: auto;
    max-height: none;
  }
}

/* ── "On this page" list ─────────────────────────────────────────────────── */

body.md-read main :is(.md-toc, aside.outline, aside.toc) {
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}

body.md-read main :is(.md-toc, aside.outline, aside.toc) > :is(h2, .md-toc__title) {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0;
  padding: 0 0 10px;
  background: #fff;
  color: var(--rd-muted);
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1.4;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

body.md-read main :is(.md-toc, aside.outline, aside.toc) a {
  display: block;
  margin: 0;
  padding: 6px 4px 6px 14px;
  border: 0;
  border-left: 1px solid var(--rd-line);
  border-radius: 0;
  background: none;
  color: var(--rd-muted);
  font-size: 13px;
  font-weight: 450;
  line-height: 1.45;
  text-decoration: none;
  transition: color 0.15s ease, border-color 0.15s ease;
}

body.md-read main :is(.md-toc, aside.outline, aside.toc) a:hover {
  background: none;
  color: var(--rd-ink);
}

body.md-read main .md-toc a.is-active {
  padding-left: 13px;
  border-left: 2px solid var(--rd-brand);
  color: var(--rd-ink);
  font-weight: 600;
}

body.md-read main :is(.md-toc, aside.outline, aside.toc) a:focus-visible {
  outline: 2px solid var(--rd-brand);
  outline-offset: 1px;
}

/* Sign-up card under the list (placed by site-topbar.js). */
body.md-read .md-rail .site-signup-cta {
  position: static;
  flex: none;
  flex-direction: column;
  align-items: flex-start;
  width: auto;
  margin: 0;
  padding: 14px;
  gap: 10px;
  border-radius: 14px;
}

body.md-read .md-rail .site-signup-cta-actions {
  width: 100%;
  flex-direction: column;
  align-items: stretch;
}

body.md-read .md-rail .site-signup-cta-btn {
  width: 100%;
  min-height: 34px;
}

/* ── Phones and tablets: a Contents button opens the same list ──────────── */

.md-toc-fab {
  display: none;
}

@media (max-width: 1079.98px) {
  body.md-read .md-rail {
    display: contents;
  }

  body.md-read main :is(.md-toc, aside.outline, aside.toc) {
    display: none;
  }

  body.md-read.md-toc-open main .md-toc {
    display: block;
    position: fixed;
    z-index: 1002;
    top: auto;
    left: 12px;
    right: 12px;
    bottom: 72px;
    max-width: 520px;
    max-height: min(70vh, 560px);
    margin: 0 0 0 auto;
    padding: 0 14px 12px;
    border: 1px solid var(--rd-line);
    border-radius: 16px;
    background: #fff;
    box-shadow: 0 18px 50px rgba(15, 23, 42, 0.18), 0 2px 6px rgba(15, 23, 42, 0.06);
  }

  body.md-read.md-toc-open main .md-toc > :is(h2, .md-toc__title) {
    padding-top: 16px;
  }

  body.md-read.md-toc-open main .md-toc a {
    padding-top: 9px;
    padding-bottom: 9px;
    font-size: 15px;
  }

  body.md-read .md-toc-fab {
    position: fixed;
    z-index: 1002;
    right: 16px;
    bottom: 16px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 16px;
    border: 1px solid var(--rd-line);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.96);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
    color: var(--rd-ink);
    font: 600 14px/1 'Libre Franklin', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    cursor: pointer;
  }

  body.md-read .md-toc-fab svg {
    width: 18px;
    height: 18px;
    flex: none;
  }

  body.md-read.md-toc-open .md-toc-fab {
    border-color: var(--rd-brand);
    color: var(--rd-brand);
  }
}

/* ── Article column ──────────────────────────────────────────────────────── */

/* Cards become a plain page; text gets a comfortable size and measure. */
body.md-read > main > :is(article.article, article.card) {
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
  font-size: 17px;
  line-height: 1.75;
}

body.md-read > main h1 {
  margin-bottom: 12px;
  color: var(--rd-ink);
  font-size: clamp(28px, 2vw + 18px, 42px);
  line-height: 1.14;
  letter-spacing: -0.022em;
}

body.md-read > main :is(article.article, article.card) > h2,
body.md-read > main :is(article.article, article.card) > section > h2,
body.md-read > main .panel > h2,
body.md-read > main.molecule-layout > .related-links > h2 {
  margin: 48px 0 12px;
  color: var(--rd-ink);
  font-size: clamp(22px, 0.6vw + 17px, 26px);
  line-height: 1.25;
  letter-spacing: -0.012em;
}

body.md-read > main :is(article.article, article.card) > h3,
body.md-read > main :is(article.article, article.card) > section > h3 {
  margin: 28px 0 8px;
  color: var(--rd-ink);
  font-size: 19px;
  line-height: 1.35;
}

body.md-read > main :is(article.article, article.card) :is(figure, img, video, iframe, table, pre) {
  max-width: 100%;
}

body.md-read > main > :is(.md-related, .seo-related, .md-updated, .related-nav) {
  margin-top: 40px;
}

/* ── Reaction and molecule guides: one column of sections, no card grid ── */

body.md-read > main:is(.reaction-layout, .molecule-layout) :is(.grid, .content-grid) {
  display: block;
}

body.md-read > main:is(.reaction-layout, .molecule-layout) :is(.hero, .panel, .related-links) {
  margin: 0 0 8px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
}

body.md-read > main:is(.reaction-layout, .molecule-layout) .hero {
  margin-bottom: 4px;
}

body.md-read > main:is(.reaction-layout, .molecule-layout) .hero .summary {
  max-width: none;
  color: #475569;
  font-size: 18px;
  line-height: 1.65;
}

body.md-read > main:is(.reaction-layout, .molecule-layout) :is(.panel, .related-links) {
  font-size: 17px;
  line-height: 1.75;
}

body.md-read > main:is(.reaction-layout, .molecule-layout) .facts {
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
}

/* Wide reaction schemes scroll inside their own row instead of widening the page. */
body.md-read > main.reaction-layout .examples {
  grid-template-columns: minmax(0, 1fr);
}

body.md-read > main:is(.reaction-layout, .molecule-layout) :is(.panel, .example, .related-links) {
  min-width: 0;
}

body.md-read > main.molecule-layout .facts {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (max-width: 560px) {
  body.md-read > main.molecule-layout .facts {
    grid-template-columns: 1fr;
  }
}
