@import url("reset.css");
@import url("color.css");
@import url("md-syntax.css");

/* New Computer Modern Font Faces — served via jsDelivr (woff2) */
@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20Regular.woff2")
    format("woff2");
  font-weight: 400;
  font-style: normal;
}

@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20Bold.woff2")
    format("woff2");
  font-weight: 700;
  font-style: normal;
}

@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20Book.woff2")
    format("woff2");
  font-weight: 300;
  font-style: normal;
}

@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20Italic.woff2")
    format("woff2");
  font-weight: 400;
  font-style: italic;
}

@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20BoldOblique.woff2")
    format("woff2");
  font-weight: 700;
  font-style: italic;
}

@font-face {
  font-family: "New CM";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Mono%2010%20BookItalic.woff2")
    format("woff2");
  font-weight: 300;
  font-style: italic;
}

/* Math variant — has the OpenType MATH table the browser needs to size
   radicals, fractions, big operators with consistent stroke weight. */
@font-face {
  font-family: "New CM Math";
  src: url("https://cdn.jsdelivr.net/npm/web-computer-modern@1.1.0-new-cm-7-0-2/woff2/WebCM%20Math%20Serif%20Regular.woff2")
    format("woff2");
  font-weight: 400;
  font-style: normal;
}

html,
body {
  padding: 0;
  margin: 0;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  height: 100%;
}

video {
  width: 100%;
  height: auto;
}

main {
  max-width: 60em;
  margin: 0 auto;
  padding: 3em 4em;
  font-family: "Recursive";
  font-variation-settings: "CASL" 1;
  color: var(--text-color);
}

.post:not(.essay) {
  max-width: 54em;
}

h1 {
  font-size: 2rem;
}
h2 {
  font-size: 1.5rem;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  margin-top: 0;
  margin-bottom: 0.5em;
  scroll-margin-top: 0.25em;
  position: relative;
}

/* Section links: a "#" marker appears to the left of any heading on hover. */
.heading-anchor {
  color: inherit;
  text-decoration: none;
  font: inherit;
}

.heading-anchor::before {
  content: "#";
  position: absolute;
  left: -0.9em;
  opacity: 0;
  color: var(--text-secondary);
  font-variation-settings:
    "wght" 400,
    "CASL" 0;
  transition: opacity 0.15s;
  pointer-events: none;
}

:is(h1, h2, h3, h4):hover > .heading-anchor::before {
  opacity: 1;
}

header {
  margin-bottom: 2em;
  font-size: 1.5rem;
  font-variation-settings: "CASL" 1;
}

i,
em {
  font-variation-settings:
    "slnt" -15,
    "CRSV" 1,
    "wght" 350;
  font-style: italic;
}

pre > code {
  width: 100%;
  padding: 1em;
  display: block;
  white-space: pre-wrap;
  word-wrap: break-word;
}

code {
  background-color: light-dark(#f6f8fa, #151b23);
  width: 100%;
  color: var(--text-color);

  padding: 0.1em 0.3em;
  border-radius: 4px;
  box-sizing: border-box;
  font-family: "Recursive";
  font-variation-settings: "MONO" 1;
  font-weight: 400;
  border: 1px solid light-dark(rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0.1));
}

b,
strong {
  font-variation-settings: "wght" 600;
}

blockquote {
  margin: 1em 0;
  padding: 1em;
  background-color: var(--blockquote-bg);
  border-radius: 4px;
  & p {
    font-variation-settings: "CASL" 1;
    margin: 0;
  }
}

p {
  margin-top: 0;
  margin-bottom: 1.5em;
  font-size: 1.05rem;
  font-variation-settings: "wght" 350;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  margin-bottom: 1em;
  font-variation-settings: "casl" 0;
  th,
  td {
    padding: 0.5em;
    border: 1px solid var(--table-border);
  }

  th {
    background-color: var(--table-header-bg);
    font-weight: bold;
  }

  tr:nth-child(even) {
    background-color: var(--table-stripe-bg);
  }
}

a {
  font-variation-settings:
    "CASL" 0,
    "wght" 400;
  transition: font-variation-settings 0.2s ease;

  &:hover {
    font-variation-settings:
      "CASL" 1,
      "wght" 600;
  }
}

.dinkus {
  display: block;
  text-align: center;
  font-size: 1.1rem;
  margin-top: 2em;
  margin-bottom: 0em;
}

ol,
ul {
  padding-left: 0;
  margin-top: 0;
  font-size: 1rem;
  & li::marker {
    color: var(--text-secondary);
  }
  & a {
    text-decoration: none;
    &:hover {
      text-decoration: underline;
    }
  }
}

img {
  display: block;
  margin: 0 auto;
}

@media (max-width: 600px) {
  main {
    padding: 1.5em;
  }
  header {
    margin-bottom: 1em;
  }
  ol,
  ul {
    list-style-position: inside;
    /* Add hanging indent for wrapped text */
    text-indent: -1.2em;
    padding-left: 1.2em;
  }
}

/* ── Table of Contents ──────────────────────────────────────────────────── */

/* Shared panel styles */
.toc-panel {
  font-family: "Recursive", sans-serif;
  font-variation-settings:
    "CASL" 0.5,
    "wght" 350;
  font-size: 0.78rem;
  line-height: 1.4;
}

.toc-heading {
  font-variation-settings:
    "wght" 600,
    "CASL" 0;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-size: 0.65rem;
  color: var(--text-secondary);
  margin: 0 0 0.6em 0;
}

.toc-panel ol {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 1em;
}

.toc-panel li {
  padding: 0;
  text-indent: 0;
}

/* Sub-list for h3 entries */
.toc-panel ol ol {
  padding-left: 0.75em;
  margin-top: 0;
  border-left: 1px solid light-dark(rgba(0, 0, 0, 0.08), rgba(255, 255, 255, 0.08));
  margin-left: 0.4em;
  margin-bottom: 0.15em;
}

.toc-panel a {
  display: block;
  padding: 0.2em 0.4em;
  border-radius: 3px;
  text-decoration: none;
  color: var(--text-secondary);
  transition:
    color 0.15s,
    background 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toc-panel a:hover {
  color: var(--text-color);
  background: light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.06));
}

.toc-panel a[aria-current="true"] {
  color: var(--text-color);
  font-variation-settings:
    "wght" 500,
    "CASL" 0.5;
}

/* ── Wide: fixed sidebar + buttons row to the right of the content column ── */
@media (min-width: 70em) {
  .toc-toggle {
    display: none;
  }

  /* Three phases as viewport widens:
     1. Left-anchored 1.5em from text, width grows to fill right space (min wins).
     2. Width caps at 20em; centering takes over — equal gap on both sides (preferred).
        Transition at ~92em where 50%+24.5em = 75%+1.5em.
     3. Gap capped at 8em — TOC stops drifting (max wins).
        Transition at ~118em where 75%+1.5em = 50%+31em. */
  .page-chrome,
  .toc {
    --toc-left: clamp(calc(50% + 24.5em), calc(75% + 1.5em), calc(50% + 31em));
    --toc-w: clamp(10em, calc(50vw - 25em), 20em);
  }

  /* Buttons: horizontal row at the top of the column, right-aligned. */
  .page-chrome {
    position: fixed;
    left: var(--toc-left);
    top: 2rem;
    width: var(--toc-w);
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    gap: 0;
    z-index: 1001;
  }

  /* Override shadow-DOM :host { position: fixed } — document author styles
     win over :host rules, so this repositions the buttons into the flex row. */
  .page-chrome > dark-mode-toggle,
  .page-chrome > github-link,
  .page-chrome > docs-link {
    position: static;
  }

  .toc {
    position: fixed;
    left: var(--toc-left);
    top: calc(2rem + 40px + 0.75rem);
    width: var(--toc-w);
  }

  /* Panel visibility at wide is controlled by JS (toc.open = true) — no override needed. */
  .toc-panel {
    max-height: calc(100vh - 2rem - 40px - 0.75rem - 1rem);
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: light-dark(rgba(0, 0, 0, 0.15), rgba(255, 255, 255, 0.1)) transparent;
  }
}

/* ── Narrow (tablet): stack page buttons with centers 2em from the right edge.
     right = 2em − half_button_width = 2em − 1.25em = 0.75em. */
@media (min-width: 38em) and (max-width: 69.99em) {
  .page-chrome {
    position: fixed;
    right: 0.75em;
    top: 2rem;
    width: 2.5em;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    z-index: 1000;
  }

  .page-chrome > dark-mode-toggle,
  .page-chrome > github-link,
  .page-chrome > docs-link {
    position: static;
  }
}

/* ── Narrow: floating toggle button + overlay panel; buttons restore naturally ── */
@media (max-width: 69.99em) {
  .toc {
    position: fixed;
    bottom: 1.5em;
    right: 0.75em;
    z-index: 100;
  }

  .toc-panel {
    display: none;
    position: absolute;
    bottom: calc(100% + 0.75em);
    right: 0;
    width: min(18em, calc(100vw - 3em));
    max-height: min(22em, 60vh);
    overflow-y: auto;
    background: light-dark(#fff, #1e222a);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 8px 24px light-dark(rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.45));
    padding: 0.9em 0.9em;
    scrollbar-width: thin;
    scrollbar-color: light-dark(rgba(0, 0, 0, 0.15), rgba(255, 255, 255, 0.1)) transparent;
  }

  details.toc[open] .toc-panel {
    display: block;
  }

  .toc-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.6em;
    height: 2.6em;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: light-dark(#fff, #1e222a);
    box-shadow: 0 2px 8px light-dark(rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.4));
    cursor: pointer;
    color: var(--text-secondary);
    transition:
      color 0.15s,
      box-shadow 0.15s;
    list-style: none;
  }

  /* Hide the browser's built-in disclosure triangle on <summary>. */
  .toc-toggle::-webkit-details-marker {
    display: none;
  }

  .toc-toggle:hover,
  details.toc[open] .toc-toggle {
    color: var(--text-color);
    box-shadow: 0 4px 14px light-dark(rgba(0, 0, 0, 0.16), rgba(0, 0, 0, 0.5));
  }

  .toc-toggle-icon {
    font-size: 1.1rem;
    font-family: "Recursive";
    font-variation-settings: "CASL" 1;
    line-height: 1;
  }
}

/* ── Some conditional spacing ── */

table:not(:has(+ p)) {
  margin-bottom: 2em;
}

p:has(+ ul, + ol) {
  margin-bottom: 0.5em;
}
p:has(+ md-syntax) {
  margin-bottom: 0em;
}

/* Post media styling */
.post img,
.post video {
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  display: block;
  max-width: 100%;
}

/* Prose math / marker elements */
md-tex {
  display: inline-block;
  /* New CM Math has a smaller x-height than Recursive; nudge up slightly
     so the math baseline aligns with the surrounding text baseline. */
  vertical-align: -0.15em;
}

md-tex math {
  /* Counteract any browser UA math-font-size scaling so inline symbols
     sit at the same visual size as the surrounding text. */
  font-size: 1.05em;
}

md-marker {
  display: inline;
  padding: 0 2px;
  border-radius: 2px;
}

/* Markdown / Marked */
[data-footnote-ref] {
  font-variation-settings: "CASL" 1;
  text-decoration: none;
}
[data-footnote-ref]::before {
  content: "[";
}

[data-footnote-ref]::after {
  content: "]";
}
