/* === Article Detail Layout ================================================ */

.article-toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.article-meta {
  font-size: 0.78rem;
  color: var(--color-muted);
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-bg-sel);

  & .badge {
    font-size: 0.85em;
    padding: 0.15em 0.6em;
  }
}

.article-meta-tags {
  margin-top: 0.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.article-changelog {
  & summary {
    cursor: pointer;
    color: var(--color-fg-dim);
    font-size: 0.78rem;
    margin-top: 0.75rem;
  }

  & ul {
    list-style: none;
    margin-top: 0.5rem;
    padding-left: 1rem;
  }

  & li {
    font-size: 0.75rem;
    color: var(--color-muted);
    padding: 0.15rem 0;
  }

  & li::before {
    content: '› ';
    color: var(--color-accent);
  }
}

/* === Article Page Layout ================================================== */

.page-article {
  --article-width: 62rem;
  max-width: none;
  position: relative;
}

.page-article > .article-toolbar,
.page-article > .article-meta {
  max-width: var(--article-width);
  margin-inline: auto;
}

/* === Article Body ========================================================= */

.article-body {
  max-width: var(--article-width);
  margin-inline: auto;
  position: relative;
  line-height: 1.75;

  /* --- Table of Contents -------------------------------------------------- */

  & > nav[role="doc-toc"] {
    --toc-pad-x: 1.2rem;
    position: sticky;
    top: calc(var(--nav-height) + 1.5rem);
    float: left;
    width: 20rem;
    margin-left: -22rem;
    max-height: calc(100vh - var(--nav-height) - 3rem);
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--color-bg-sel) transparent;
    background: var(--color-bg-alt);
    border: 1px solid var(--color-bg-sel);
    border-left: 3px solid var(--color-accent);
    border-radius: var(--radius);
    padding: 1rem var(--toc-pad-x);

    & h2 {
      font-size: 0.7rem;
      text-transform: uppercase;
      letter-spacing: 0.1em;
      color: var(--color-accent);
      margin: 0;
    }

    & ol { list-style: none; }
    & ol ol { padding-left: 0.9rem; margin-top: 0.1rem; }
    & li { margin: 0.2rem 0; font-size: 0.78rem; }
    & a { text-decoration: none; line-height: 1.4; display: block; }
    & span { text-decoration: none !important; }

    & .prefix {
      color: var(--color-muted);
      margin-right: 0.3em;
      font-variant-numeric: tabular-nums;
    }
  }

  /* --- Headings ----------------------------------------------------------- */

  & h1, & h2, & h3, & h4 {
    color: var(--color-fg-max);
    margin-top: 2rem;
    margin-bottom: 0.75rem;
    line-height: 1.3;
  }

  & > h2:first-of-type { margin-top: 0.5rem; }
  & h1 { font-size: 1.5rem; }
  & h2 { font-size: 1.25rem; }
  & h3 { font-size: 1.05rem; }

  /* --- Text --------------------------------------------------------------- */

  & p { margin-bottom: 1rem; }

  & img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
    margin: 1rem 0;
  }

  & code {
    background: var(--color-bg-alt);
    padding: 0.15em 0.4em;
    border-radius: var(--radius);
  }

  /* --- Links -------------------------------------------------------------- */

  & a {
    color: var(--color-fg-dim);
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: var(--color-bg-sel);
    transition: color var(--fast), text-decoration-color var(--fast);
  }

  & a:hover {
    color: var(--color-fg-max);
    text-decoration-color: var(--color-accent);
  }

  /* --- Lists -------------------------------------------------------------- */

  & ul, & ol { margin: 0.5rem 0 1rem; }
  & ul { padding-left: 1.5rem; list-style-type: disc; }
  & ol { padding-left: 2rem; list-style-type: decimal; }
  & li { margin: 0.2rem 0; }
  & li > p { margin: 0; }
  & li > p + ul, & li > p + ol { margin-top: 0.2rem; }

  /* --- Figures ------------------------------------------------------------ */

  & figure { margin: 1.75rem auto; text-align: center; }
  & figure > table { margin: 0 auto 0.5rem; }

  & figcaption {
    font-size: 0.8rem;
    color: var(--color-muted);
    margin-top: 0.5rem;
    font-style: italic;
  }

  & figure svg.typst-frame {
    filter: invert(0.78);
    display: block;
    margin-inline: auto;
    max-width: 100%;
    height: auto;
  }

  /* --- Tables ------------------------------------------------------------- */

  & table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.88em;
  }

  & thead tr {
    background: var(--color-bg-sel);
    border-bottom: 2px solid var(--color-accent);
  }

  & th {
    color: var(--color-accent);
    padding: 0.5rem 1rem;
    text-align: center;
    font-weight: 600;
    letter-spacing: 0.03em;
  }

  & td {
    padding: 0.45rem 1rem;
    border-bottom: 1px solid var(--color-bg-sel);
  }

  & tr:last-child td { border-bottom: none; }
  & tbody tr:hover td { background: color-mix(in srgb, var(--color-bg-sel) 40%, transparent); }

  /* --- Bibliography ------------------------------------------------------- */

  & section[role="doc-bibliography"] ul { list-style: none; }

  & section[role="doc-bibliography"] li {
    font-size: 0.83rem;
    color: var(--color-fg-dim);
    padding: 0.25rem 0;
  }

  & .prefix {
    color: var(--color-muted);
    margin-right: 0.3em;
  }
}

/* === Typst HTML Export Styles ============================================= */

.typst-doc { color: var(--color-fg); fill: var(--color-fg); }

.typst-inline-math svg,
.typst-display-math svg { filter: invert(0.78); }

.typst-display-math {
  margin: 1.25em auto;
  text-align: center;
  overflow-x: auto;
}

.typst-inline-math { display: inline-block; }

.typst-display-math > svg,
.typst-inline-math > svg { display: inline-block; }

/* --- Blockquotes --------------------------------------------------------- */

.typst-quote {
  border-left: 3px solid var(--color-accent);
  background: color-mix(in srgb, var(--color-accent) 6%, transparent);
  padding: 0.6rem 1rem 0.5rem;
  margin: 1.25rem 0;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--color-fg-dim);
  font-style: italic;

  & > p { margin: 0; }
}

.typst-quote-attr {
  display: block;
  font-size: 0.8rem;
  color: var(--color-muted);
  font-style: normal;
  margin-top: 0.4rem;
}

/* --- Styled blocks (clues) ----------------------------------------------- */

.typst-clue {
  --_clue: var(--color-muted);
  border-left: 3px solid var(--_clue);
  background: color-mix(in srgb, var(--_clue) 7%, var(--color-bg-alt));
  padding: 0.75rem 1rem;
  margin: 1rem 0;
  border-radius: 0 var(--radius) var(--radius) 0;
  line-height: 1.6;
  color: var(--color-fg);

  & p:last-child { margin-bottom: 0; }
  & i { margin-right: 0.4em; opacity: 0.6; }
}

.typst-clue-label { font-weight: bold; }
.typst-clue-sublabel { font-style: italic; }

.typst-clue-theorem { --_clue: var(--base09); }
.typst-clue-proof { --_clue: var(--color-muted); color: var(--color-fg-dim); }
.typst-clue-definition, .typst-clue-note, .typst-clue-info { --_clue: var(--color-link); }
.typst-clue-tip { --_clue: var(--color-ok); }
.typst-clue-warning { --_clue: var(--color-warn); }
.typst-clue-danger { --_clue: var(--color-danger); }

/* --- Code blocks --------------------------------------------------------- */

.typst-code-block,
.typst-pseudocode {
  position: relative;
  background: var(--color-bg-alt);
  border: 1px solid var(--color-bg-sel);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  margin: 1.25rem 0;
  overflow-x: auto;
}

.typst-pseudocode {
  font-size: 1.1rem;
}

.typst-code-block {
  font-size: 0.95rem;

  & pre, & code { background: none; }
  & code { padding: 0; counter-reset: line-number; }
}

.code-header {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  z-index: 1;
}

.code-lang-badge {
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  background: var(--color-accent);
  color: var(--color-bg);
}

.typst-pseudocode svg { max-width: 100%; height: auto; filter: invert(0.78); }

.code-line {
  display: block;
  counter-increment: line-number;
  line-height: 1.4;

  &::before {
    content: counter(line-number);
    display: inline-block;
    width: 2.5em;
    text-align: right;
    margin-right: 1.2em;
    color: var(--color-muted);
    user-select: none;
    font-style: normal;
  }
}

/* === TOC Mobile Controls =================================================== */

.toc-toggle-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}

.toc-fab,
.toc-close,
.toc-scrim { display: none; }

/* === Responsive ============================================================ */

@media (max-width: 900px) {
  .page-article {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .page-article > .article-toolbar,
  .page-article > .article-meta {
    max-width: 100%;
  }

  /* Replace sticky sidebar with hidden off-screen drawer */
  .article-body > nav[role="doc-toc"] {
    position: fixed;
    top: 0;
    right: 0;
    width: min(20rem, 85vw);
    height: 100dvh;
    float: none;
    margin-left: 0;
    max-height: none;
    border-radius: 0;
    border: none;
    border-left: 3px solid var(--color-accent);
    z-index: 200;
    transform: translateX(110%);
    transition: transform var(--med);
    padding-top: 0;
  }

  /* Sticky header block inside the drawer — keeps close button readable while list scrolls under it */
  .article-body > nav[role="doc-toc"]::before {
    content: '';
    display: block;
    position: sticky;
    top: 0;
    height: calc(var(--nav-height) + 1.5rem);
    background: var(--color-bg-alt);
    margin: 0 calc(-1 * var(--toc-pad-x));
    z-index: 1;
  }

  .page-article:has(#toc-toggle:checked) .article-body > nav[role="doc-toc"] {
    transform: translateX(0);
  }

  /* Scrim backdrop, shown when drawer is open */
  .page-article:has(#toc-toggle:checked) .toc-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 199;
    cursor: pointer;
  }

  /* Floating action button to open the TOC drawer */
  .toc-fab {
    display: flex;
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    z-index: 198;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    background: var(--color-bg-alt);
    border: 1px solid var(--color-accent);
    border-radius: 2rem;
    color: var(--color-fg);
    font-family: var(--font);
    font-size: 0.85rem;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
    transition: background var(--fast);
    user-select: none;
  }

  .toc-fab:hover { background: var(--color-bg-sel); }

  /* Close icon — sits over the sticky header block, always visible */
  .page-article:has(#toc-toggle:checked) .toc-close {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    /* Vertically centered in the header block (nav-height + 1.5rem tall) */
    top: calc(var(--nav-height) - 0.15rem);
    right: 0.75rem;
    z-index: 202;
    width: 1.8rem;
    height: 1.8rem;
    color: var(--color-fg-dim);
    font-size: 1rem;
    cursor: pointer;
    transition: color var(--fast);
  }

  .page-article:has(#toc-toggle:checked) .toc-close:hover {
    color: var(--color-fg-max);
  }

  /* Make tables horizontally scrollable on narrow viewports */
  .article-body table {
    display: block;
    overflow-x: auto;
  }
}

/* --- highlight.js theme (base16) ----------------------------------------- */

.hljs { color: var(--base05); background: none; }
.hljs-comment, .hljs-quote { color: var(--base03); }
.hljs-keyword, .hljs-selector-tag, .hljs-literal { color: var(--base0E); }
.hljs-string, .hljs-addition { color: var(--base0B); }
.hljs-number, .hljs-built_in, .hljs-doctag, .hljs-attr, .hljs-attribute { color: var(--base09); }
.hljs-title, .hljs-section, .hljs-name, .hljs-function > .hljs-title { color: var(--base0D); }
.hljs-variable, .hljs-template-variable, .hljs-tag, .hljs-deletion, .hljs-meta, .hljs-regexp { color: var(--base08); }
.hljs-type, .hljs-class .hljs-title { color: var(--base0A); }
.hljs-symbol, .hljs-bullet, .hljs-link { color: var(--base0C); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: bold; }
