/* Article page: in-page table of contents and reading progress.
 *
 * Loaded only by templates/articles/post.html. The markup it styles is
 * rendered server-side, so an article still reads correctly with this
 * stylesheet or its JavaScript missing -- the sidebar is then a plain,
 * working list of anchor links.
 */

:root {
  /* The navbar is .sticky-top, so anything that scrolls to the top of the
   * viewport has to clear it. One value, used by both the sticky sidebar and
   * the heading scroll-margin, so they can't drift apart.
   */
  --article-sticky-offset: 6rem;
}

/* ---------------------------------------------------------------- headings */

/* Without this an anchor jump parks the heading underneath the sticky navbar.
 * Applies to the body headings the TOC links to, not the page title.
 */
.blog-content h2,
.blog-content h3,
.blog-content h4 {
  scroll-margin-top: var(--article-sticky-offset);
}

/* Honour the system setting rather than forcing smooth scrolling on everyone. */
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

/* ------------------------------------------------------- table of contents */

.article-toc {
  top: var(--article-sticky-offset);
  /* Keep the list inside the viewport on a long article rather than letting
   * it run off the bottom of a short one.
   */
  max-height: calc(100vh - var(--article-sticky-offset) - 2rem);
  overflow-y: auto;
  font-size: 0.9rem;
}

.article-toc-heading {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #6c757d;
  margin-bottom: 0.75rem;
}

.article-toc ul {
  /* The rule the active marker sits against. */
  border-left: 2px solid #e9ecef;
}

.article-toc a {
  display: block;
  padding: 0.4rem 0 0.4rem 1rem;
  margin-left: -2px;
  border-left: 2px solid transparent;
  color: #6c757d;
  text-decoration: none;
  line-height: 1.35;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.article-toc a:hover,
.article-toc a:focus-visible {
  color: var(--primary-color);
}

.article-toc a.is-active {
  color: var(--primary-color);
  font-weight: 600;
  border-left-color: var(--primary-color);
}

/* --------------------------------------------------- reading progress bar */

/* The sidebar is desktop-only -- there is no room for it beside the text on a
 * phone -- so the progress bar carries "where am I" on small screens. It sits
 * directly under the sticky navbar.
 */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 100%;
  background: transparent;
  z-index: 1031; /* just above .sticky-top (1020) */
  pointer-events: none;
}

.reading-progress-bar {
  height: 100%;
  width: 0;
  background: var(--primary-color);
  transform-origin: left center;
}

@media (min-width: 992px) {
  /* On desktop the sidebar already answers the question; the bar would be
   * redundant chrome.
   */
  .reading-progress {
    display: none;
  }
}

/* ------------------------------------------------------- figure tables */
/* The owner-first article pattern (docs/company-policy.md section 3) puts a
   worked example at the centre of each piece -- a market day's numbers, a
   batch cost -- so tables are load-bearing here rather than incidental.
   Markdown emits a bare <table> with no classes, so the styling hangs off
   the element, and off <strong>, which is how a subtotal row marks itself
   in the source. */
.blog-content table {
  width: 100%;
  max-width: 34rem;
  margin: 2rem 0;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.blog-content th,
.blog-content td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

/* A two-column figure has no column headings worth printing, but the
   Markdown table syntax requires a header row, so the source carries an
   empty one. Collapse it. The :empty rule stands alone so the header still
   disappears in a browser without :has(). */
.blog-content th:empty {
  padding: 0;
  border: none;
}

.blog-content thead:has(th:empty) {
  display: none;
}

/* Subtotal and total lines: bolded in the Markdown, ruled off here, so the
   arithmetic reads the way it would on paper. */
.blog-content tbody tr:has(strong) td {
  border-top: 1px solid rgba(0, 0, 0, 0.35);
  border-bottom: none;
}

.blog-content tbody tr:last-child td {
  border-bottom: 2px solid rgba(0, 0, 0, 0.35);
}

/* Long line labels ("Cost of goods (96 bags @ $4.10)") crowd a phone. */
@media (max-width: 575.98px) {
  .blog-content table {
    font-size: 0.9375rem;
  }

  .blog-content th,
  .blog-content td {
    padding: 0.45rem 0.375rem;
  }
}
