/* Notes — technical writing.
   -------------------------------------------------------------------------
   A calm, single-column reading environment. It borrows the site's existing
   conventions rather than introducing a second visual system: Helvetica Neue
   for UI/metadata, Georgia for display type (as on the CV), the same #111 /
   #666 / #e5e5e5 ink-and-rule palette, and the ~740px reading measure already
   established by .cv-shell.

   Layout model
   ------------
   .writing-shell  two columns on desktop: a sticky TOC rail + the article.
   .writing-article is itself a grid. Every direct child lands in the centered
   text column; anything marked .breakout spans the full width instead. That
   keeps wide figures escaping the measure without negative-margin guesswork
   and without ever colliding with the rail, which lives in its own column. */

.writing-page {
  --writing-ink: #111;
  --writing-ink-soft: #444;
  --writing-ink-muted: #6b6b6b;
  --writing-rule: #e5e5e5;
  --writing-rule-soft: #efefef;
  --writing-accent: #111;

  --writing-text-col: 720px; /* reading measure, within the 650–760 target */
  --writing-rail-col: 190px;
  --writing-rail-gap: 56px;
  --writing-page-pad: 24px;

  --writing-serif: Georgia, "Times New Roman", serif;
  --writing-sans: "Helvetica Neue", Helvetica, sans-serif;
  --writing-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;

  background-color: #fff;
  color: var(--writing-ink);
  font-family: var(--writing-sans);
}

.writing-page .writing-main {
  /* .main-content in main.css is a centered flex box for the homepage hero;
     articles need a plain block flow, so opt out of it here. */
  display: block;
  flex: 1 0 auto;
  text-align: left;
  background-color: #fff;
}

/* ----------------------------------------------------------------- header */

/* The header shares the article's inner grid so the title, dek, and metadata
   align exactly with the prose column rather than with the page centre. */
.writing-header {
  grid-column: 2;
  grid-row: 1;
  display: grid;
  grid-template-columns:
    [full-start] minmax(0, 1fr)
    [text-start] minmax(0, var(--writing-text-col)) [text-end]
    minmax(0, 1fr) [full-end];
  margin-bottom: 52px;
}

.writing-header-inner {
  grid-column: text;
}

/* Topics sit above the title as a quiet eyebrow — not pills. */
.writing-topics {
  margin: 0 0 18px;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--writing-ink-muted);
}

.writing-topic-sep {
  margin: 0 0.45em;
  color: #c4c4c4;
}

.writing-title {
  font-family: var(--writing-serif);
  font-size: clamp(1.9rem, 4vw, 2.75rem);
  font-weight: 400;
  line-height: 1.15;
  letter-spacing: -0.02em;
  color: #222;
  margin: 0 0 20px;
  max-width: 20em;
  text-wrap: balance;
}

.writing-dek {
  font-size: 1.125rem;
  line-height: 1.55;
  color: var(--writing-ink-soft);
  margin: 0 0 26px;
  max-width: 34em;
}

/* Metadata is deliberately smaller and greyer than the dek so it never
   competes with the title. */
.writing-meta {
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
  margin: 0;
}

.writing-meta-sep {
  margin: 0 0.5em;
  color: #c4c4c4;
}

.writing-origin {
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
  margin: 6px 0 0;
}

.writing-origin a {
  color: var(--writing-ink-soft);
  border-bottom: 1px solid var(--writing-rule);
}

.writing-origin a:hover {
  color: var(--writing-ink);
  border-bottom-color: #b9b9b9;
}

/* ----------------------------------------------------------------- shell */

.writing-shell {
  display: grid;
  grid-template-columns: var(--writing-rail-col) minmax(0, 1fr);
  column-gap: var(--writing-rail-gap);
  max-width: calc(
    var(--writing-rail-col) + var(--writing-rail-gap) + 994px
  );
  margin: 0 auto;
  padding: 132px var(--writing-page-pad) 96px;
}

/* The rail and the article share row 2, so the rail's grid area is as tall as
   the article — which is what gives position: sticky room to travel. */
.writing-rail {
  grid-column: 1;
  grid-row: 2;
  position: relative;
}

.writing-article {
  grid-column: 2;
  grid-row: 2;
  min-width: 0;
  display: grid;
  grid-template-columns:
    [full-start] minmax(0, 1fr)
    [text-start] minmax(0, var(--writing-text-col)) [text-end]
    minmax(0, 1fr) [full-end];
}

.writing-article > * {
  grid-column: text;
}

.writing-article > .breakout,
.writing-article > .writing-figure.breakout {
  grid-column: full;
}

/* --------------------------------------------------------------------- TOC */

.writing-toc {
  position: sticky;
  top: 104px;
  font-size: 0.8125rem;
  line-height: 1.45;
}

.writing-toc[hidden] {
  display: none;
}

.writing-toc-summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--writing-ink-muted);
  font-size: 0.6875rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding-bottom: 10px;
}

.writing-toc-summary::-webkit-details-marker {
  display: none;
}

.writing-toc-caret {
  display: none;
}

.writing-toc-nav {
  max-height: calc(100vh - 200px);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.writing-toc-list,
.writing-toc-list ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.writing-toc-list li {
  margin: 0;
}

.writing-toc-list a {
  display: block;
  padding: 4px 0 4px 11px;
  margin-left: -1px;
  border-left: 1px solid var(--writing-rule);
  color: var(--writing-ink-muted);
  transition: color 140ms ease, border-color 140ms ease;
}

.writing-toc-list a:hover {
  color: var(--writing-ink);
  border-left-color: #b9b9b9;
}

/* Sub-headings (h3) indent one step. */
.writing-toc-list .writing-toc-h3 a {
  padding-left: 22px;
  font-size: 0.78rem;
}

/* Scroll-spy state, kept to ink weight rather than a colour accent. */
.writing-toc-list a[aria-current="true"] {
  color: var(--writing-ink);
  border-left-color: var(--writing-accent);
}

/* ------------------------------------------------------------------ prose */

.writing-article {
  font-size: 1.0625rem;
  line-height: 1.72;
  color: var(--writing-ink);
}

.writing-article > p,
.writing-aside-body p {
  margin: 0 0 1.35em;
}

.writing-article > p:first-of-type {
  margin-top: 0;
}

.writing-article h2,
.writing-article h3,
.writing-article h4 {
  font-family: var(--writing-serif);
  font-weight: 400;
  color: #1a1a1a;
  letter-spacing: -0.01em;
  scroll-margin-top: 104px; /* clears the fixed nav on anchor jumps */
}

.writing-article h2 {
  font-size: 1.6rem;
  line-height: 1.25;
  margin: 2.6em 0 0.7em;
  padding-top: 0.9em;
  border-top: 1px solid var(--writing-rule-soft);
}

.writing-article > h2:first-child {
  margin-top: 0.6em;
}

.writing-article h3 {
  font-size: 1.2rem;
  line-height: 1.35;
  margin: 2em 0 0.55em;
}

.writing-article h4 {
  font-size: 1.0625rem;
  font-family: var(--writing-sans);
  font-weight: 600;
  margin: 1.8em 0 0.5em;
}

/* Inline links: underline via border so the rule sits clear of descenders. */
.writing-article > p a,
.writing-article li a,
.writing-aside-body a,
.writing-figcaption a {
  color: var(--writing-ink);
  border-bottom: 1px solid #c9c9c9;
  transition: border-color 140ms ease, color 140ms ease;
}

.writing-article > p a:hover,
.writing-article li a:hover,
.writing-aside-body a:hover,
.writing-figcaption a:hover {
  color: #000;
  border-bottom-color: var(--writing-ink);
}

.writing-article ul,
.writing-article ol {
  margin: 0 0 1.35em;
  padding-left: 1.35em;
}

.writing-article li {
  margin: 0 0 0.5em;
}

.writing-article li > ul,
.writing-article li > ol {
  margin: 0.5em 0 0;
}

.writing-article strong {
  font-weight: 600;
}

.writing-article blockquote {
  margin: 1.8em 0;
  padding: 0 0 0 20px;
  border-left: 2px solid var(--writing-rule);
  color: var(--writing-ink-soft);
  font-style: italic;
}

.writing-article blockquote p {
  margin: 0 0 0.8em;
}

.writing-article hr {
  border: 0;
  border-top: 1px solid var(--writing-rule);
  margin: 3em 0;
}

/* --------------------------------------------------------------- figures */

.writing-figure {
  margin: 2.4em 0;
}

.writing-figure img {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 auto;
}

.writing-figure.breakout img {
  max-width: 100%;
}

.writing-figcaption {
  margin-top: 12px;
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--writing-ink-muted);
}

/* On breakout figures the caption stays at the reading measure so it doesn't
   stretch into an uncomfortably long line. */
.writing-figure.breakout .writing-figcaption {
  max-width: var(--writing-text-col);
  margin-left: auto;
  margin-right: auto;
}

.writing-figcaption-source {
  display: block;
  margin-top: 3px;
  color: #8a8a8a;
  font-size: 0.75rem;
}

.writing-figure-grid-items {
  display: grid;
  grid-template-columns: repeat(var(--figure-grid-columns, 2), minmax(0, 1fr));
  gap: 18px;
  align-items: start;
}

/* ---------------------------------------------------------------- asides */

.writing-aside {
  margin: 2.2em 0;
  padding: 18px 22px;
  background: #fafafa;
  border-left: 2px solid #d8d8d8;
  font-size: 1rem;
  line-height: 1.65;
  color: var(--writing-ink-soft);
}

.writing-aside-caveat {
  border-left-color: #b98a3e;
}

.writing-aside-label {
  margin: 0 0 6px;
  font-size: 0.6875rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--writing-ink-muted);
}

.writing-aside-body > :last-child {
  margin-bottom: 0;
}

/* ------------------------------------------------------------- equations */

.writing-equation {
  margin: 1.9em 0;
  text-align: center;
  overflow-x: auto;
  overflow-y: hidden;
}

/* kramdown wraps a standalone $$…$$ block in div.kdmath. Bare display math in
   Markdown should look identical to the <Equation /> component. */
.writing-article > .kdmath {
  margin: 1.9em 0;
  text-align: center;
  overflow-x: auto;
  overflow-y: hidden;
}

.writing-equation-image img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

.writing-equation-caption {
  margin: 10px 0 0;
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
}

/* Display math can be wider than the measure on small screens; scroll it
   rather than letting the page overflow horizontally. */
.writing-article .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
}

/* ------------------------------------------------------------------- code */

.writing-article pre,
.writing-article .highlight {
  margin: 0;
}

.writing-article div.highlighter-rouge,
.writing-article figure.highlight,
.writing-code-body {
  margin: 1.9em 0;
  background: #fafafa;
  border: 1px solid var(--writing-rule);
  border-radius: 3px;
  overflow: hidden;
}

.writing-code-body > div.highlighter-rouge {
  margin: 0;
  border: 0;
  border-radius: 0;
}

.writing-article pre {
  padding: 16px 18px;
  overflow-x: auto;
  font-family: var(--writing-mono);
  font-size: 0.8125rem;
  line-height: 1.65;
  color: #24292e;
  -webkit-text-size-adjust: 100%;
}

.writing-article code {
  font-family: var(--writing-mono);
  font-size: 0.875em;
}

/* Inline code — a tint, no border, so prose stays quiet. */
.writing-article > p code,
.writing-article li code,
.writing-aside-body code {
  background: #f3f3f3;
  padding: 0.12em 0.34em;
  border-radius: 3px;
  color: #1a1a1a;
}

.writing-code {
  margin: 1.9em 0;
}

.writing-code-filename {
  margin: 0 0 -1.6em;
  font-family: var(--writing-mono);
  font-size: 0.75rem;
  color: var(--writing-ink-muted);
}

.writing-code-caption {
  margin: -1.4em 0 0;
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
}

/* Rouge syntax highlighting — understated on purpose: comments recede,
   keywords and strings get just enough differentiation to scan. */
.writing-article .highlight .c,
.writing-article .highlight .c1,
.writing-article .highlight .cm,
.writing-article .highlight .cs {
  color: #969896;
  font-style: italic;
}

.writing-article .highlight .k,
.writing-article .highlight .kn,
.writing-article .highlight .kc,
.writing-article .highlight .kd,
.writing-article .highlight .kp,
.writing-article .highlight .kr,
.writing-article .highlight .ow {
  color: #a6317d;
}

.writing-article .highlight .kt,
.writing-article .highlight .nc,
.writing-article .highlight .nn {
  color: #4a5a9c;
}

.writing-article .highlight .s,
.writing-article .highlight .s1,
.writing-article .highlight .s2,
.writing-article .highlight .se,
.writing-article .highlight .sb,
.writing-article .highlight .sd {
  color: #2f7a52;
}

.writing-article .highlight .m,
.writing-article .highlight .mi,
.writing-article .highlight .mf,
.writing-article .highlight .il {
  color: #b06020;
}

.writing-article .highlight .nf,
.writing-article .highlight .nd {
  color: #3f5fa8;
}

.writing-article .highlight .nb,
.writing-article .highlight .bp {
  color: #6a5acd;
}

.writing-article .highlight .o {
  color: #333;
}

/* --------------------------------------------------------------- tables */

.writing-article table {
  width: 100%;
  border-collapse: collapse;
  margin: 2em 0;
  font-size: 0.9375rem;
  line-height: 1.5;
}

.writing-article thead th {
  text-align: left;
  font-family: var(--writing-sans);
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--writing-ink-muted);
  padding: 0 14px 8px 0;
  border-bottom: 1px solid #d5d5d5;
}

.writing-article tbody td {
  padding: 12px 14px 12px 0;
  border-bottom: 1px solid var(--writing-rule-soft);
  vertical-align: top;
  color: var(--writing-ink-soft);
}

.writing-article tbody td:first-child {
  color: var(--writing-ink);
}

/* A wide comparison table can break out and scroll rather than squeeze. */
.writing-article > .breakout table {
  margin: 0;
}

.writing-table-scroll {
  overflow-x: auto;
  margin: 2em 0;
}

.writing-table-scroll table {
  margin: 0;
  min-width: 560px;
}

/* ---------------------------------------------------- references + footer */

.writing-references {
  scroll-margin-top: 104px;
  margin: 3.6em 0 0;
  padding-top: 1.6em;
  border-top: 1px solid var(--writing-rule);
}

/* Inline citation markers: small, unobtrusive, and clearly clickable. */
.writing-cite {
  font-size: 0.7em;
  line-height: 1;
  margin-left: 0.1em;
}

.writing-cite a {
  color: var(--writing-ink-muted);
  padding: 0 0.1em;
  border-bottom: 0;
}

.writing-cite a:hover {
  color: var(--writing-ink);
}

.writing-cite-missing {
  color: #c0392b;
}

.writing-reference-missing {
  color: #c0392b;
}

/* Give a cited reference a moment of emphasis when jumped to. */
.writing-reference:target {
  background: #fbfbf4;
  box-shadow: -8px 0 0 #fbfbf4, 8px 0 0 #fbfbf4;
}

.writing-references-title {
  font-family: var(--writing-serif);
  font-size: 1.2rem;
  font-weight: 400;
  margin: 0 0 1em;
}

.writing-references-list {
  list-style: decimal;
  margin: 0;
  padding-left: 1.5em;
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--writing-ink-soft);
}

.writing-reference {
  margin-bottom: 0.85em;
}

.writing-reference-link {
  white-space: nowrap;
  color: var(--writing-ink-muted);
  font-size: 0.8125rem;
}

.writing-reference-link:hover {
  color: var(--writing-ink);
}

.writing-footer {
  margin-top: 3.2em;
  padding-top: 1.4em;
  border-top: 1px solid var(--writing-rule-soft);
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
}

.writing-footer p {
  margin: 0 0 6px;
}

.writing-footer a {
  color: var(--writing-ink-soft);
  border-bottom: 1px solid var(--writing-rule);
}

.writing-footer a:hover {
  color: var(--writing-ink);
  border-bottom-color: #b9b9b9;
}

/* =========================================================== the archive */

.writings-index-main {
  display: block;
  flex: 1 0 auto;
  text-align: left;
  background: #fff;
  padding: 132px var(--writing-page-pad) 96px;
}

.writings-index-shell {
  max-width: 820px;
  margin: 0 auto;
}

.writings-index-title {
  font-family: var(--writing-serif);
  font-size: clamp(2rem, 5vw, 2.75rem);
  font-weight: 400;
  letter-spacing: -0.02em;
  color: #222;
  margin: 0 0 16px;
}

.writings-index-intro {
  font-size: 1.0625rem;
  line-height: 1.6;
  color: var(--writing-ink-soft);
  max-width: none;
  margin: 0 0 72px;
}

.writings-year {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  gap: 0 24px;
  padding-top: 22px;
  border-top: 1px solid var(--writing-rule);
  margin-bottom: 44px;
}

.writings-year-label {
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
  margin: 0;
  position: sticky;
  top: 104px;
  align-self: start;
}

.writings-year-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.writings-entry {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 0 24px;
  padding: 0 0 34px;
}

.writings-entry:last-child {
  padding-bottom: 4px;
}

.writings-entry-date {
  font-size: 0.8125rem;
  color: var(--writing-ink-muted);
  padding-top: 0.42em;
  font-variant-numeric: tabular-nums;
}

.writings-entry-title {
  font-family: var(--writing-serif);
  font-size: 1.25rem;
  font-weight: 400;
  line-height: 1.3;
  letter-spacing: -0.01em;
  margin: 0 0 8px;
  max-width: 30em;
}

.writings-entry-title a {
  color: #1a1a1a;
  transition: color 140ms ease;
}

.writings-entry-title a:hover {
  color: #6b6b6b;
}

.writings-entry-desc {
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--writing-ink-soft);
  margin: 0 0 8px;
  max-width: 34em;
}

.writings-entry-topics {
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  color: #8a8a8a;
  margin: 0;
}

.writings-entry-topics .writing-topic-sep {
  margin: 0 0.4em;
}

.writings-entry-origin {
  color: #a0a0a0;
}

.writings-index-empty {
  color: var(--writing-ink-muted);
  font-size: 0.9375rem;
}

/* ============================================================== responsive */

/* Below the point where a rail plus a full measure fits, drop to one column
   and turn the TOC into a compact collapsible control under the header. */
@media (max-width: 1023px) {
  .writing-page {
    --writing-page-pad: 20px;
  }

  .writing-shell {
    grid-template-columns: minmax(0, 1fr);
    row-gap: 0;
    padding-top: 96px;
  }

  /* Single column: header, then the collapsed TOC, then the article. */
  .writing-header {
    grid-column: 1;
    grid-row: 1;
    grid-template-columns: minmax(0, 1fr);
    margin-bottom: 34px;
  }

  .writing-header-inner {
    grid-column: 1;
  }

  .writing-rail {
    grid-column: 1;
    grid-row: 2;
    margin-bottom: 30px;
  }

  .writing-article {
    grid-column: 1;
    grid-row: 3;
    /* Figures go full width of the single column. */
    grid-template-columns: [full-start] [text-start] minmax(0, 1fr) [text-end] [full-end];
  }

  .writing-toc {
    position: static;
    border: 1px solid var(--writing-rule);
    border-radius: 4px;
    padding: 10px 14px;
  }

  .writing-toc-summary {
    padding-bottom: 0;
    justify-content: space-between;
  }

  /* A rotating chevron replaces the desktop label-only summary. */
  .writing-toc-caret {
    display: block;
    width: 7px;
    height: 7px;
    border-right: 1px solid var(--writing-ink-muted);
    border-bottom: 1px solid var(--writing-ink-muted);
    transform: rotate(45deg) translateY(-2px);
    transition: transform 160ms ease;
  }

  .writing-toc[open] .writing-toc-caret {
    transform: rotate(-135deg) translateY(-1px);
  }

  .writing-toc[open] .writing-toc-nav {
    padding-top: 10px;
    max-height: 48vh;
  }

  /* No sticky highlight to chase on mobile — the outline is a jump menu. */
  .writing-toc-list a[aria-current="true"] {
    border-left-color: var(--writing-rule);
    color: var(--writing-ink-muted);
  }

  .writing-figure.breakout .writing-figcaption {
    max-width: none;
  }

  .writings-index-main {
    padding-top: 96px;
  }
}

@media (max-width: 600px) {
  .writing-page {
    --writing-page-pad: 18px;
  }

  .writing-article {
    font-size: 1rem;
    line-height: 1.68;
  }

  .writing-shell {
    padding-top: 84px;
  }

  .writing-dek {
    font-size: 1.0625rem;
  }

  /* Stack the metadata separators out of the way on narrow screens. */
  .writing-meta,
  .writing-origin {
    font-size: 0.78125rem;
  }

  .writing-article pre {
    font-size: 0.75rem;
    padding: 14px;
  }

  .writing-figure-grid-items {
    grid-template-columns: minmax(0, 1fr);
  }

  .writing-aside {
    padding: 14px 16px;
  }

  /* Archive: date above the entry instead of a narrow gutter column. */
  .writings-year {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }

  .writings-year-label {
    position: static;
    margin-bottom: 18px;
  }

  .writings-entry {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }

  .writings-entry-date {
    padding-top: 0;
    margin-bottom: 6px;
  }

  .writings-entry-title {
    font-size: 1.125rem;
  }

  .writings-index-intro {
    margin-bottom: 52px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .writing-toc-caret,
  .writing-toc-list a,
  .writings-entry-title a {
    transition: none;
  }
}
