/**
 * Reading-experience enhancements for single Notes articles only.
 * Loaded after css/system.css, on is_singular( 'post' ) requests.
 *
 * .article-page becomes a 3-column CSS Grid: [left rail] [820px content]
 * [right rail]. The rails are `position: sticky`, not `fixed` — sticky is
 * bounded by its own grid row, and the row's height is the article's own
 * height (the tallest column). That means the rails can float alongside
 * the text while scrolling, but they physically cannot escape past the end
 * of the article into .akava-footer, because their containing track ends
 * exactly where the article does. `fixed` (the previous approach) is
 * anchored to the viewport instead of the article, which is exactly why it
 * kept floating over the footer — a different property was the fix, not a
 * tweak to the numbers.
 *
 * Visibility is still decided by JS (initRailVisibility(), in
 * js/article-reading.js), but the rails now have fixed known widths, so JS
 * only compares that constant against the real available margin — no
 * layout measurement, no chicken-and-egg with opacity. Below 900px
 * (existing breakpoint, reused) they're removed from the grid entirely.
 */

.article-shell {
  --ak-article-rail-toc-width: 34px;
  --ak-article-rail-side-width: 300px;
  --ak-article-rail-edge-gap: 64px;
}

/* ---- Progress bar ---- */

.article-progress {
  position: fixed;
  inset-block-start: 0;
  inset-inline-start: 0;
  z-index: var(--ak-z-nav);
  inline-size: 100%;
  block-size: 3px;
  background: transparent;
}

.article-progress__bar {
  inline-size: 0%;
  block-size: 100%;
  background: var(--ak-primary);
  transition: inline-size 100ms linear;
  will-change: inline-size;
}

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

/* ---- Grid: content column + two rail columns ---- */

.article-page {
  /* .article-page in system.css also styles page.php (generic WP Pages)
     with a simple 820px centered block — but that template doesn't load
     this stylesheet (only is_singular('post') does, see inc/setup.php),
     so this grid override only ever applies here, on Notes. The 820px cap
     itself moves to .article-page__main (grid column 2) below. */
  inline-size: 100%;
  margin-inline: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) min(820px, 100%) minmax(0, 1fr);
  align-items: start;
}

.article-page__main {
  grid-column: 2;
  inline-size: 100%;
}

.article-rail {
  position: sticky;
  inset-block-start: 132px;
  z-index: 2;
  /* Collapsed to zero width and clipped until JS confirms it actually
     fits — this is what stops a rail from ever forcing horizontal
     overflow on medium-width screens where its grid track is narrower
     than the rail itself. */
  inline-size: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.article-rail--toc {
  grid-column: 3;
  justify-self: start;
  margin-inline-start: var(--ak-article-rail-edge-gap);
}

.article-rail--side {
  grid-column: 1;
  justify-self: end;
  margin-inline-end: var(--ak-article-rail-edge-gap);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.article-rail.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.article-rail--toc.is-visible {
  inline-size: var(--ak-article-rail-toc-width);
  /* Visible: allow the hover tooltip (below) to escape this box to the
     left again now that it has an actual size. */
  overflow: visible;
}

.article-rail--side.is-visible {
  inline-size: var(--ak-article-rail-side-width);
  /* No max-block-size / overflow-y here anymore — that's what forced an
     ugly internal scrollbar once "You're reading" + "More like this" +
     the game added up to more than one short viewport. Sticky already
     behaves correctly without it: if the stack is taller than the
     viewport, it simply extends past the fold like any other sticky
     sidebar and reveals the rest as the page scrolls — same one scrollbar
     the whole page already has, not a second nested one. */
  overflow-x: hidden;
}

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

/* ---- Right rail: dot-nav TOC ---- */

.article-dotnav {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 0;
  padding: 14px 0;
  list-style: none;
  border-radius: 999px;
  border: 1px solid var(--ak-article-border);
  background: color-mix(in srgb, var(--ak-article-bg) 90%, #000 10%);
}

.article-dotnav__item a {
  position: relative;
  display: flex;
  min-inline-size: 22px;
  min-block-size: 22px;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.article-dotnav__dot {
  inline-size: 6px;
  block-size: 6px;
  border-radius: 50%;
  background: var(--ak-article-border);
  transition: background 140ms ease, transform 140ms ease;
}

.article-dotnav__item a:hover .article-dotnav__dot,
.article-dotnav__item a:focus-visible .article-dotnav__dot {
  background: var(--ak-article-link-strong);
  transform: scale(1.3);
}

.article-dotnav__item.is-active .article-dotnav__dot {
  background: var(--ak-primary-deep);
  transform: scale(1.3);
}

.article-dotnav__label {
  position: absolute;
  inset-inline-end: calc(100% + 12px);
  inset-block-start: 50%;
  transform: translateY(-50%) translateX(6px);
  inline-size: max-content;
  max-inline-size: 220px;
  padding: 6px 10px;
  border: 1px solid var(--ak-article-border);
  border-radius: var(--ak-radius-sm);
  background: var(--ak-article-bg);
  color: var(--ak-article-heading);
  font: 650 0.78rem/1.3 var(--ak-font-sans);
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease, transform 140ms ease;
}

.article-dotnav__item a:hover .article-dotnav__label,
.article-dotnav__item a:focus-visible .article-dotnav__label {
  opacity: 1;
  transform: translateY(-50%) translateX(0);
}

@media (prefers-reduced-motion: reduce) {
  .article-dotnav__dot,
  .article-dotnav__label {
    transition: none;
  }
}

/* ---- Left rail: related notes + game ----
   One visually continuous column with thin dividers between sections,
   not three separate bordered cards stacked on top of each other — the
   "card soup" look was a big part of what read as cramped/busy next to
   body text. Only the game keeps its own frame, since that's a genuinely
   distinct interactive area, not another list of links. */

.article-context,
.article-related {
  padding-block-end: 18px;
  border-block-end: 1px solid var(--ak-article-border);
}

.article-game {
  padding-block-start: 2px;
}

.article-context__title {
  margin: 0;
  color: var(--ak-article-heading);
  font: 700 0.95rem/1.35 var(--ak-font-sans);
}

.article-context__topic {
  margin: 6px 0 0;
  color: var(--ak-article-muted);
  font: 600 0.76rem/1.4 var(--ak-font-mono);
}

.article-context__links {
  display: grid;
  gap: 8px;
  margin-block-start: 14px;
  padding-block-start: 14px;
  border-block-start: 1px solid var(--ak-article-border);
}

.article-context__links a {
  color: var(--ak-article-link);
  font: 650 0.82rem/1.3 var(--ak-font-sans);
  text-decoration: none;
}

.article-context__links a:hover,
.article-context__links a:focus-visible {
  color: var(--ak-article-link-strong);
  text-decoration: underline;
}

.article-rail__heading {
  margin: 0 0 12px;
  color: var(--ak-article-heading);
  font: 750 0.72rem/1.2 var(--ak-font-mono);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.article-related__list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.article-related__list a {
  display: block;
  color: var(--ak-article-deck);
  font: 600 0.85rem/1.4 var(--ak-font-sans);
  text-decoration: none;
}

.article-related__list a:hover,
.article-related__list a:focus-visible {
  color: var(--ak-article-link-strong);
  text-decoration: underline;
}

.article-game__frame {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--ak-border);
  border-radius: var(--ak-radius-sm);
  background: var(--ak-bg-black);
}

.article-game__canvas {
  display: block;
  inline-size: 100%;
  block-size: auto;
}

.article-game__start {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  min-block-size: 44px;
  border: 0;
  background: rgba(2, 6, 4, 0.82);
  color: var(--ak-term-green);
  font: 750 0.82rem/1 var(--ak-font-mono);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
}

.article-game__start:hover,
.article-game__start:focus-visible {
  background: rgba(2, 6, 4, 0.92);
}

.article-game__start[hidden] {
  display: none;
}

.article-game__hint {
  margin: 8px 0 0;
  color: var(--ak-article-muted);
  font: 600 0.7rem/1.3 var(--ak-font-mono);
  text-align: center;
}

/* ---- Code block: capped height + copy button + expand toggle ---- */

.article-content pre {
  max-block-size: 420px;
  padding-inline-end: 56px;
  overflow: auto;
}

.article-content pre.is-expanded {
  max-block-size: none;
}

.article-code-copy {
  position: absolute;
  inset-block-start: 10px;
  inset-inline-end: 10px;
  min-inline-size: 44px;
  min-block-size: 32px;
  padding: 6px 10px;
  border: 1px solid var(--ak-article-pre-border);
  border-radius: var(--ak-radius-sm);
  background: var(--ak-surface-2);
  color: var(--ak-article-pre-text);
  font: 650 0.7rem/1 var(--ak-font-mono);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.article-code-copy:hover,
.article-code-copy:focus-visible {
  border-color: var(--ak-primary);
  color: var(--ak-primary);
}

.article-code-copy[data-copied='true'] {
  border-color: var(--ak-primary);
  color: var(--ak-primary);
}

.article-code-toggle {
  display: block;
  inline-size: 100%;
  min-block-size: 44px;
  margin-block-start: -1.55em;
  margin-block-end: 1.55em;
  border: 1px solid var(--ak-article-pre-border);
  border-block-start: 0;
  border-radius: 0 0 var(--ak-radius-sm) var(--ak-radius-sm);
  background: var(--ak-surface-2);
  color: var(--ak-article-muted);
  font: 650 0.72rem/1 var(--ak-font-mono);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
}

.article-code-toggle:hover,
.article-code-toggle:focus-visible {
  color: var(--ak-primary);
}

/* ---- FAQ accordion ---- */

.article-faq {
  margin: 20px 0 0;
  border-block-start: 1px solid var(--ak-article-border);
}

.article-faq__item {
  border-block-end: 1px solid var(--ak-article-border);
}

.article-faq__question {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-block-size: 44px;
  padding: 14px 4px;
  color: var(--ak-article-heading);
  font-weight: 700;
  font-size: 1.05em;
  cursor: pointer;
  list-style: none;
}

.article-faq__question::-webkit-details-marker {
  display: none;
}

.article-faq__question::after {
  content: '+';
  flex: 0 0 auto;
  color: var(--ak-article-muted);
  font-size: 1.3em;
  line-height: 1;
}

.article-faq__question code {
  display: contents;
}

.article-faq__item[open] .article-faq__question::after {
  content: '−';
}

.article-faq__answer {
  padding: 0 4px 18px;
  color: var(--ak-article-deck);
}

.article-faq__answer > :first-child {
  margin-block-start: 0;
}

.article-faq__answer > :last-child {
  margin-block-end: 0;
}

/* ---- Heading anchor links ---- */

.article-anchor {
  display: inline-block;
  margin-inline-start: 0.35em;
  color: var(--ak-article-border);
  font-weight: 400;
  text-decoration: none;
  opacity: 0;
}

.article-content h2:hover .article-anchor,
.article-content h3:hover .article-anchor,
.article-anchor:focus-visible {
  opacity: 1;
}

.article-anchor:hover,
.article-anchor:focus-visible {
  color: var(--ak-article-link-strong);
}

.article-anchor[data-copied='true'] {
  opacity: 1;
  color: var(--ak-primary-deep);
}

/* ---- Hide the whole reading-experience layer where it doesn't belong ---- */

@media (max-width: 900px) {
  .article-rail {
    display: none;
  }
}

@media print {
  .article-rail,
  .article-progress,
  .article-code-copy,
  .article-code-toggle,
  .article-anchor {
    display: none !important;
  }

  .article-content pre {
    max-block-size: none;
    overflow: visible;
  }

  /* Closed <details> hide their content even in print by default — force
     every FAQ answer open so a printed/PDF'd article isn't missing text. */
  .article-faq__item {
    display: block !important;
  }

  .article-faq__answer {
    display: block !important;
  }
}
