/*===================================*\
  Writing — the article index and the article template
  Builds on base.css.
\*===================================*/

:root {
  --measure: 50rem;
  --toc-w: 14rem;
  --toc-gap: clamp(40px, 4vw, 64px);
  --shell: var(--container);
  --step-article: clamp(1.125rem, 1.05rem + 0.3vw, 1.25rem);
  --step-title: clamp(2.25rem, 1.5rem + 3vw, 3.75rem);
  --neg: #b42318;
  --pos: #1a7f4b;
}
[data-theme="dark"] {
  --neg: #ff8a7a;
  --pos: #6fd3a0;
}

/* Reading progress (the bar is created by script.js) */
#global-scroll-progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 120;
  width: 0;
  height: 3px;
  background: var(--accent);
  pointer-events: none;
}

.meta-line {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: var(--step-small);
  color: var(--muted);
}
.meta-line span + span::before {
  content: "\00B7";
  margin-right: 10px;
}

/* ===================================
   WRITING INDEX
   =================================== */
.writing-hero { padding: 72px 0 56px; }
.writing-hero h1 { font-size: var(--step-title); line-height: 1.05; letter-spacing: -0.03em; }
.writing-hero .lead {
  margin-top: 20px;
  max-width: 36em;
  font-size: var(--step-lead);
  line-height: 1.5;
}

.rg-panel {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
  gap: clamp(24px, 4vw, 56px);
  margin-top: 48px;
  padding: clamp(24px, 3vw, 36px);
  border-radius: var(--radius);
  background: var(--surface);
}
.rg-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px 24px;
}
.rg-stats dd {
  font-size: clamp(1.5rem, 1.2rem + 1vw, 2rem);
  line-height: 1.1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.rg-stats dd strong { font-weight: 500; }
.rg-stats dt { margin-top: 4px; font-size: var(--step-small); color: var(--muted); overflow-wrap: anywhere; }
.rg-stats div { display: flex; flex-direction: column-reverse; justify-content: flex-end; }
.rg-note { font-size: var(--step-small); color: var(--muted); }
.rg-note .arrow-link { margin-top: 12px; color: var(--ink); }

.filter {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 40px;
}
.filter button {
  padding: 9px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  font: 500 var(--step-small)/1.2 var(--font);
  cursor: pointer;
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease), color 0.2s var(--ease);
}
.filter button:hover { border-color: var(--muted); }
.filter button[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}

.post-feature {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  gap: clamp(28px, 4vw, 56px);
  align-items: center;
  margin-bottom: 72px;
  text-decoration: none;
}
.post-feature img {
  width: 100%;
  aspect-ratio: 3 / 2;
  object-fit: cover;
  border-radius: var(--radius);
}
.post-feature h2 { margin-top: 12px; font-size: var(--step-head); }
.post-feature p.excerpt { margin-top: 16px; }
.post-feature .arrow-link { margin-top: 22px; }
.post-feature:hover h2 { text-decoration: underline; text-decoration-thickness: 1px; }

.post-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr));
  gap: 48px 32px;
}
.post-card { display: flex; flex-direction: column; text-decoration: none; }
.post-card .thumb { overflow: hidden; border-radius: var(--radius); }
.post-card img {
  width: 100%;
  aspect-ratio: 3 / 2;
  object-fit: cover;
  transition: transform 0.6s var(--ease);
}
.post-card:hover img { transform: scale(1.03); }
.post-card .meta-line { margin-top: 16px; }
.post-card h3 { margin-top: 8px; font-size: var(--step-lead); }
.post-card p { margin-top: 10px; font-size: var(--step-small); color: var(--muted); }
.post-card:hover h3 { text-decoration: underline; text-decoration-thickness: 1px; }
[hidden] { display: none !important; }

/* ===================================
   ARTICLE
   =================================== */
.article-head {
  max-width: var(--shell);
  margin: 0 auto;
  padding: 56px var(--gutter) 40px calc(var(--gutter) + var(--toc-w) + var(--toc-gap));
}
.article-head > * { max-width: var(--measure); }
.article-head .back { font-size: var(--step-small); }
.article-head .meta-line { margin-top: 36px; }
.article-title {
  margin-top: 14px;
  font-size: var(--step-title);
  line-height: 1.08;
  letter-spacing: -0.025em;
}
.article-deck {
  margin-top: 20px;
  font-size: var(--step-lead);
  line-height: 1.5;
  color: var(--muted);
}
.byline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}
.byline-who { display: flex; align-items: center; gap: 12px; }
.byline-who img {
  width: 44px; height: 44px;
  border-radius: 50%;
  object-fit: cover;
  object-position: center 20%;
}
.byline-who strong { display: block; font-weight: 700; }
.byline-who span { display: block; font-size: var(--step-small); color: var(--muted); }
.byline-views { font-size: var(--step-small); color: var(--muted); }

.article-hero {
  max-width: var(--shell);
  margin: 0 auto;
  padding: 0 var(--gutter);
}
.article-hero img {
  width: 100%;
  max-height: 36rem;
  object-fit: cover;
  border-radius: var(--radius);
}
.article-hero figcaption,
.article-figure figcaption {
  margin-top: 10px;
  font-size: var(--step-small);
  color: var(--muted);
}

.article-layout {
  display: grid;
  grid-template-columns: var(--toc-w) minmax(0, var(--measure));
  gap: 0 var(--toc-gap);
  max-width: var(--shell);
  margin: 0 auto;
  padding: 56px var(--gutter) 0;
}
.toc {
  position: sticky;
  top: calc(var(--nav-h) + 32px);
  align-self: start;
  font-size: var(--step-small);
}
.toc p { font-weight: 700; margin-bottom: 12px; }
.toc ol { display: grid; gap: 10px; counter-reset: toc; }
.toc li { counter-increment: toc; display: grid; grid-template-columns: 1.6em 1fr; color: var(--muted); }
.toc li::before { content: counter(toc); font-variant-numeric: tabular-nums; }
.toc a { text-decoration: none; line-height: 1.4; transition: color 0.2s var(--ease); }
.toc a:hover { color: var(--ink); }
.toc a.is-current { color: var(--ink); text-decoration: underline; }
.toc-mobile { display: none; }

/* ——— Article body ——— */
.article-body {
  font-size: var(--step-article);
  line-height: 1.7;
}
.article-body > * + * { margin-top: 1.15em; }
.article-body h2 {
  margin-top: 2.2em;
  font-size: clamp(1.5rem, 1.25rem + 1vw, 2rem);
  line-height: 1.2;
  scroll-margin-top: calc(var(--nav-h) + 24px);
}
.article-body h3 {
  margin-top: 1.8em;
  font-size: clamp(1.2rem, 1.1rem + 0.4vw, 1.375rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.3;
}
.article-body h2 + *, .article-body h3 + * { margin-top: 0.6em; }
.article-body strong { font-weight: 700; }
.article-body a {
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 0.18em;
}
.article-body ul, .article-body ol:not(.agenda) { padding-left: 1.3em; }
.article-body ul li { list-style: disc; }
.article-body ol:not(.agenda) li { list-style: decimal; }
.article-body li + li { margin-top: 0.45em; }
.article-body li::marker { color: var(--muted); }

.article-body blockquote.pull {
  margin: 1.8em 0;
  padding-left: 24px;
  border-left: 3px solid var(--accent);
  font-size: var(--step-lead);
  font-style: italic;
  line-height: 1.45;
}
.article-body blockquote.pull p { margin: 0; }

.callout {
  padding: 24px 28px;
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 1.0625rem;
  line-height: 1.6;
}
.callout-key { box-shadow: inset 3px 0 0 var(--accent); }
.callout-title { font-weight: 700; }
.callout > * + * { margin-top: 0.7em; }
.callout ul { padding-left: 1.2em; }

.article-figure img { width: 100%; border-radius: var(--radius); }
.article-body .article-figure { margin: 2em 0; }

.table-wrap {
  overflow-x: auto;
  margin: 1.8em 0;
  -webkit-overflow-scrolling: touch;
}
.table-wrap table {
  width: 100%;
  min-width: 34rem;
  border-collapse: collapse;
  font-size: 0.95rem;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
}
.table-wrap th {
  padding: 10px 12px 10px 0;
  border-bottom: 2px solid var(--ink);
  text-align: left;
  font-weight: 600;
  vertical-align: bottom;
}
.table-wrap td {
  padding: 10px 12px 10px 0;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.table-wrap td:first-child { font-weight: 500; }
.table-wrap td.neg { color: var(--neg); }
.table-wrap td.pos { color: var(--pos); }
.table-note { font-size: var(--step-small); color: var(--muted); }
.table-wrap + .table-note { margin-top: -0.8em; }

.agenda { counter-reset: agenda; }
.agenda li {
  counter-increment: agenda;
  display: grid;
  grid-template-columns: 2.4rem minmax(0, 1fr);
  gap: 0 8px;
  padding: 18px 0;
  border-top: 1px solid var(--line);
}
.agenda li::before {
  content: counter(agenda);
  font-size: 1.5rem;
  line-height: 1.2;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.agenda li:last-child { border-bottom: 1px solid var(--line); }

.references { margin-top: 3em; padding-top: 2em; border-top: 1px solid var(--line); }
.references h2 { margin-top: 0; font-size: 1.375rem; }
.references ol { font-size: var(--step-small); color: var(--muted); }
.references li + li { margin-top: 0.6em; }

.note {
  padding: 18px 22px;
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--step-small);
  color: var(--muted);
  line-height: 1.6;
}
.note strong { color: var(--ink); }

/* ——— After the article ——— */
.article-end {
  max-width: var(--shell);
  margin: 0 auto;
  padding: 56px var(--gutter) 96px calc(var(--gutter) + var(--toc-w) + var(--toc-gap));
}
.article-end > * { max-width: var(--measure); }
.topics { font-size: var(--step-small); color: var(--muted); }
.author-box {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 20px;
  margin-top: 32px;
  padding-top: 32px;
  border-top: 1px solid var(--line);
}
.author-box img {
  width: 72px; height: 72px;
  border-radius: 50%;
  object-fit: cover;
  object-position: center 20%;
}
.author-box strong { font-weight: 700; }
.author-box p { margin-top: 6px; font-size: 1.0625rem; }
.author-links { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-top: 12px; font-size: var(--step-small); }

.more-writing { padding: 88px 0 104px; background: var(--surface); }
.more-writing .section-head { margin-bottom: 40px; }

/* ===================================
   RESPONSIVE
   =================================== */
@media (max-width: 1180px) {
  .article-layout { grid-template-columns: minmax(0, var(--measure)); justify-content: center; }
  .article-head, .article-end { max-width: calc(var(--measure) + 2 * var(--gutter)); padding-left: var(--gutter); }
  .toc { display: none; }
  .toc-mobile {
    display: block;
    margin-bottom: 8px;
    padding: 14px 18px;
    border-radius: var(--radius);
    background: var(--surface);
    font-size: var(--step-small);
  }
  .toc-mobile summary { cursor: pointer; font-weight: 600; }
  .toc-mobile ol { margin-top: 12px; padding-left: 1.3em; display: grid; gap: 8px; }
  .toc-mobile li { list-style: decimal; }
  .toc-mobile a { text-decoration: none; }
}

@media (max-width: 960px) {
  .rg-panel { grid-template-columns: 1fr; }
  .post-feature { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .writing-hero { padding: 40px 0 40px; }
  .rg-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .article-head { padding-top: 32px; }
  .article-layout { padding-top: 40px; }
  .callout { padding: 20px; }
  .author-box { grid-template-columns: 56px minmax(0, 1fr); }
  .author-box img { width: 56px; height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  .post-card img { transition: none; }
}

/* Long titles step down a size so they stay within three or four lines */
.article-title.is-long { font-size: clamp(2rem, 1.4rem + 2.2vw, 3rem); line-height: 1.12; }

/* Display equations */
.article-body p.equation {
  overflow-x: auto;
  padding: 18px 20px;
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
  font-style: italic;
  white-space: nowrap;
}
/* Charts sit on white in both themes */
.article-figure img[src$=".png"] { background: #ffffff; }
