/* attobop.net -- consolidated blog stylesheet */

/* ---- Font faces ---- */
@font-face {
  font-family: "Berkeley Mono";
  src: url("/static/fonts/BerkeleyMonoVariable-Regular.woff2") format("woff2"),
       url("/static/fonts/BerkeleyMonoVariable-Regular.woff") format("woff");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Berkeley Mono";
  src: url("/static/fonts/BerkeleyMonoVariable-Italic.woff2") format("woff2"),
       url("/static/fonts/BerkeleyMonoVariable-Italic.woff") format("woff");
  font-weight: 100 900;
  font-style: italic;
  font-display: swap;
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; }
*:not(dialog) { margin: 0; }
img, picture, video, canvas, svg { display: block; max-width: 100%; }
input, button, textarea, select { font: inherit; }

/* ---- Custom properties ---- */
:root {
  --color-fg: #111;
  --color-fg-muted: #555;
  --color-fg-accent: #a22;
  --color-bg: #f7f7f5;
  --color-bg-card: #fff;
  --color-bg-code: #f0efe9;
  --color-border: #222;
  --color-border-muted: #ccc;

  --font-body: Charter, "Bitstream Charter", "Sitka Text", Cambria, serif;
  --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --font-mono: "Berkeley Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  --measure: 42rem;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-fg: #d4d4d4;
    --color-fg-muted: #888;
    --color-fg-accent: #e07070;
    --color-bg: #161616;
    --color-bg-card: #1e1e1e;
    --color-bg-code: rgba(255,255,255,0.10);
    --color-border: #555;
    --color-border-muted: #444;
  }
}

/* Manual dark override */
:root[data-theme="dark"] {
  --color-fg: #d4d4d4;
  --color-fg-muted: #888;
  --color-fg-accent: #e07070;
  --color-bg: #161616;
  --color-bg-card: #1e1e1e;
  --color-bg-code: rgba(255,255,255,0.10);
  --color-border: #555;
  --color-border-muted: #444;
}

/* Manual light override */
:root[data-theme="light"] {
  --color-fg: #111;
  --color-fg-muted: #555;
  --color-fg-accent: #a22;
  --color-bg: #f7f7f5;
  --color-bg-card: #fff;
  --color-bg-code: #f0efe9;
  --color-border: #222;
  --color-border-muted: #ccc;
}

/* ---- Body ---- */
body {
  font-family: var(--font-body);
  font-size: 1.125rem;
  line-height: 1.6;
  color: var(--color-fg);
  background-color: var(--color-bg);
  padding: 1.5rem 0.75rem;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-kerning: normal;
  font-variant-numeric: oldstyle-nums;
  overflow-wrap: break-word;
}

@media (min-width: 768px) {
  body { padding: 2rem; }
}

/* ---- Theme toggle ---- */
#theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 100;
  background: var(--color-bg);
  border: 1px solid var(--color-border-muted);
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.15rem 0.5rem;
  color: var(--color-fg-muted);
  line-height: 1;
}

#theme-toggle:hover {
  color: var(--color-fg);
  border-color: var(--color-border);
}

/* ---- Scroll to top ---- */
#scroll-top {
  position: fixed;
  bottom: 1.5rem;
  right: 1rem;
  z-index: 100;
  background: var(--color-bg);
  border: 1px solid var(--color-border-muted);
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.15rem 0.5rem;
  color: var(--color-fg-muted);
  line-height: 1;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, color 0.15s, border-color 0.15s;
}
#scroll-top.visible {
  opacity: 1;
  pointer-events: auto;
}
#scroll-top:hover {
  color: var(--color-fg);
  border-color: var(--color-border);
}

/* ---- Layout ---- */
.container, main {
  max-width: var(--measure);
  margin-inline: auto;
}

/* ---- Header / Nav ---- */
header {
  border-bottom: 1px dotted var(--color-border-muted);
  padding-bottom: 0.75rem;
  margin-bottom: 1.5rem;
}

nav {
  display: flex;
  gap: 1.25rem;
  flex-wrap: wrap;
  align-items: center;
  max-width: var(--measure);
  margin-inline: auto;
}

header nav {
  font-family: var(--font-mono);
  font-size: 0.84rem;
  letter-spacing: 0.04em;
  text-transform: lowercase;
}

nav a {
  color: var(--color-fg);
  text-decoration: none;
  font-weight: 400;
}

nav a:hover {
  color: var(--color-fg-accent);
}

/* ---- Headings ---- */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-sans);
  font-weight: 700;
  line-height: 1.25;
  text-wrap: balance;
}

/* Heading anchor links: terra cotta # in left margin on hover.
   linkInsideHeader with placement:"before" puts <a> as first child of <hN>.
   position:absolute keeps it out of the text flow. */
h2, h3, h4 { position: relative; }
.anchor-link {
  position: absolute;
  right: 100%;
  padding-right: 0.4em;
  opacity: 0;
  text-decoration: none;
  color: #c07a5a;
  font-weight: 400;
  font-size: 0.9em;
  transition: opacity 0.15s ease;
}
h1:hover .anchor-link,
h2:hover .anchor-link,
h3:hover .anchor-link,
h4:hover .anchor-link,
.anchor-link:hover {
  opacity: 0.8;
}

h1 {
  font-size: 2em;
  margin-top: 0;
  margin-bottom: 0.4em;
  border-bottom: 1px dotted var(--color-border-muted);
  padding-bottom: 0.3em;
}

h2 {
  font-size: 1.35em;
  margin-top: 1.8em;
  margin-bottom: 0.5em;
  position: sticky;
  top: 0;
  background: var(--color-bg);
  padding-top: 0.3em;
  padding-bottom: 0.3em;
  z-index: 10;
  border-bottom: 1px solid var(--color-border-muted);
  padding-bottom: 0.25em;
}

h3 {
  font-size: 1em;
  margin-top: 1.6em;
  margin-bottom: 0.35em;
}

/* ---- Body text ---- */
p {
  margin-bottom: 1em;
  text-wrap: pretty;
}

a {
  color: var(--color-fg-accent);
  text-decoration: underline;
}

a:hover {
  text-decoration: none;
}

strong { font-weight: 700; }

/* ---- Lists ---- */
ul, ol {
  margin-bottom: 0.75em;
  padding-left: 1.5em;
}

li { margin-bottom: 0.35em; }

/* ---- Code ---- */
code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  font-weight: 300;
  font-variant-numeric: lining-nums;
  background: var(--color-bg-code);
  padding: 0.1em 0.3em;
  border-radius: 2px;
}

pre {
  background: var(--color-bg-code);
  border-left: 2px solid var(--color-border-muted);
  padding: 0.85em 1.1em;
  margin: 1.25em 0;
  overflow-x: auto;
  line-height: 1.55;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  font-weight: 300;
}

pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: inherit;
}

/* ---- Prism syntax highlighting ---- */
/* Acme-inspired: minimal color, differentiation via weight + style.
   One warm accent (strings), one muted tone (comments), rest is weight. */
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata { color: var(--color-code-comment); font-style: italic; font-weight: 250; }
.token.punctuation { color: var(--color-code-punct); }
.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol { color: var(--color-code-fg); font-weight: 300; }
.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin { color: var(--color-code-string); }
.token.operator,
.token.entity,
.token.url,
.token.variable { color: var(--color-code-fg); }
.token.atrule,
.token.attr-value,
.token.function,
.token.class-name { color: var(--color-code-fg); font-weight: 300; }
.token.keyword { color: var(--color-code-fg); font-weight: 450; }
.token.regex,
.token.important { color: var(--color-code-string); font-weight: 300; }

/* Light code tokens */
:root {
  --color-code-fg: #1a1a1a;
  --color-code-comment: #8b8b8b;
  --color-code-punct: #666;
  --color-code-string: #6b4f17;
}

/* Dark code tokens -- manual toggle */
:root[data-theme="dark"] {
  --color-code-fg: #c8c8c8;
  --color-code-comment: #6a6a6a;
  --color-code-punct: #888;
  --color-code-string: #d4b87c;
}

/* Dark code tokens -- system preference */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-code-fg: #c8c8c8;
    --color-code-comment: #808080;
    --color-code-punct: #888;
    --color-code-string: #d4b87c;
  }
}

/* Light code tokens -- manual toggle */
:root[data-theme="light"] {
  --color-code-fg: #1a1a1a;
  --color-code-comment: #8b8b8b;
  --color-code-punct: #666;
  --color-code-string: #6b4f17;
}

/* ---- Blockquotes ---- */
blockquote {
  padding: 0 0.75em;
  color: var(--color-fg-muted);
  border-left: 2px solid var(--color-border);
  margin-bottom: 0.75em;
}

/* ---- Tables ---- */
table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.25em 0;
  font-size: 0.9em;
  font-variant-numeric: lining-nums tabular-nums;
}

th, td {
  border-bottom: 1px solid var(--color-border-muted);
  padding: 0.35em 0.6em;
  text-align: left;
}

th {
  font-weight: 600;
  font-size: 0.875em;
  border-bottom: 2px solid var(--color-border);
}

/* ---- Images ---- */
img {
  height: auto;
}

/* Figures: white card in light mode, dimmed in dark mode */
figure {
  background: #fff;
  border-radius: 6px;
  padding: 1em 1.25em 0.5em;
  border: 1px solid var(--color-border-muted);
}

/* Dark figures: dim the whole card so white-background PNGs
   don't glare. brightness(0.82) takes the white down to ~#d1d1d1
   without inverting colors in the diagram. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) figure {
    background: #d0cdc8;
    border-color: #444;
    filter: brightness(0.82);
  }
  :root:not([data-theme="light"]) figure figcaption { color: #c07a5a; }
}

:root[data-theme="dark"] figure {
  background: #d0cdc8;
  border-color: #444;
  filter: brightness(0.82);
}
:root[data-theme="dark"] figure figcaption { color: #c07a5a; }

/* Caption on white card needs a fixed dark color in light mode */
figure figcaption { color: #555; }

/* ---- Horizontal rule ---- */
hr {
  height: 1px;
  padding: 0;
  margin: 1.5em 0;
  background: var(--color-border);
  border: 0;
}

/* ---- Details/summary ---- */
details {
  margin: 0.75em 0;
  padding: 0.5em 0.75em;
  background: var(--color-bg-card);
  border: 1px solid var(--color-border);
}

summary {
  cursor: pointer;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.875em;
}

/* ---- KaTeX math ---- */
.katex {
  font-size: 1.05em;
}

.katex-display {
  margin: 1em 0;
  overflow-x: auto;
  overflow-y: hidden;
  text-align: center;
}

.katex-display > .katex {
  text-align: center;
}

p .katex {
  font-size: 1em;
  margin: 0 0.05em;
}

/* ---- Figures / columns ---- */
figure {
  margin: 1.5em 0;
  text-align: center;
}

/* Wide figures: break out of text column symmetrically for readability.
   width: fit-content shrink-wraps the card around the image. */
figure {
  width: fit-content;
  max-width: min(100vw - 2rem, 64rem);
  margin-inline: auto;
}

figure img {
  display: block;
  max-width: min(100vw - 4rem, 62rem);
  height: auto;
  margin-inline: auto;
}

figcaption {
  font-size: 0.875em;
  font-style: italic;
  color: var(--color-fg-muted);
  margin-top: 0.5em;
  text-align: left;
  max-width: 85%;
  margin-inline: auto;
}

.columns {
  display: flex;
  gap: 1em;
  margin: 1em 0;
}

.column { flex: 1; }

@media (max-width: 600px) {
  .columns { flex-direction: column; }
  figcaption { max-width: 100%; }
}

/* ---- Footnotes ---- */
.footnotes {
  margin-top: 2em;
  border-top: 1px solid var(--color-border);
  padding-top: 0.75em;
  font-size: 0.8125em;
  color: var(--color-fg-muted);
}

/* markdown-it-footnote renders an <hr> before the list; hide it since
   we already draw the border-top on .footnotes itself */
.footnotes > hr { display: none; }

.footnotes ol {
  padding-left: 1.5em;
  margin-bottom: 0;
}

.footnotes li {
  margin-bottom: 0.4em;
}

.footnotes li p {
  margin: 0;
}

/* Backref arrow */
.footnote-backref {
  font-style: normal;
  margin-left: 0.25em;
  text-decoration: none;
  color: var(--color-fg-muted);
}

.footnote-backref:hover {
  color: var(--color-fg-accent);
}

/* Highlight flash on backlink navigation */
@keyframes highlight-flash {
  0%, 45% { background-color: var(--color-highlight-flash); }
  100% { background-color: transparent; }
}

.highlight-flash {
  --color-highlight-flash: rgba(162, 34, 34, 0.15);
  animation: highlight-flash 2.5s ease;
  border-radius: 3px;
}

/* Highlight the [N] ref link itself on backref navigation */
@keyframes highlight-ref {
  0%, 45% { color: #fff; background-color: var(--color-sidenote); }
  100% { color: var(--color-sidenote); background-color: transparent; }
}
.highlight-ref {
  animation: highlight-ref 2.5s ease;
  border-radius: 2px;
  padding: 0 0.15em;
}

/* Hide bottom footnotes when promoted to sidenotes (wide screens) */
@media (min-width: 1101px) {
  .footnotes.sidenotes-active { display: none; }
}

/* Inline superscript refs -- use sidenote color (annotation pointer, not navigation) */
.footnote-ref a {
  text-decoration: none;
  color: var(--color-sidenote);
  font-size: 0.75em;
  vertical-align: super;
  line-height: 0;
  transition: color 0.15s;
}
.footnote-ref a:hover {
  color: var(--color-fg-accent);
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}

/* Sidenote ref number hover (the superscript counter in body text) */
.sidenote-ref:hover::after, .marginnote-ref:hover::after {
  color: var(--color-fg-accent);
  font-weight: 700;
}

/* Reference citation links [N] and ref definition spans -- terra cotta */
a[href^="#ref"], span[id^="ref"] {
  color: var(--color-sidenote);
  text-decoration: none;
  transition: color 0.15s;
}
a[href^="#ref"]:hover {
  color: var(--color-fg-accent);
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}

/* Links inside sidenotes: inherit body link color but underline for contrast
   against the sidenote text color */
.sidenote a, .marginnote a {
  color: var(--color-fg-accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

/* ---- Margin notes ---- */
/* Right margin: annotations */
.sidenote {
  float: right;
  clear: right;
  margin-right: -17rem;
  width: 15rem;
  margin-top: 0.15rem;
  margin-bottom: 0.75rem;
  font-size: 0.92rem;
  line-height: 1.45;
  color: var(--color-sidenote, #8b5c3a);
  hyphens: auto;
  overflow-wrap: break-word;
  text-wrap: pretty;
  user-select: contain;
  -webkit-user-select: contain;
}

:root {
  --color-sidenote: #b5533e;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-sidenote: #e0957e;
  }
}

:root[data-theme="dark"] {
  --color-sidenote: #e0957e;
}

:root[data-theme="light"] {
  --color-sidenote: #b5533e;
}

/* Left margin: editorial commentary */
.marginnote {
  float: left;
  clear: left;
  margin-left: -17rem;
  width: 15rem;
  margin-top: 0.15rem;
  margin-bottom: 0.75rem;
  font-size: 0.92rem;
  line-height: 1.45;
  color: var(--color-sidenote);
  text-align: right;
  hyphens: auto;
  overflow-wrap: break-word;
  text-wrap: pretty;
  user-select: contain;
  -webkit-user-select: contain;
}

/* Keep text selection isolated between main content and margins */
main > * { user-select: contain; -webkit-user-select: contain; }
main > * * { user-select: text; -webkit-user-select: text; }

main { counter-reset: sidenote-counter; }

.sidenote-ref, .marginnote-ref {
  counter-increment: sidenote-counter;
}

.sidenote-ref::after, .marginnote-ref::after {
  content: counter(sidenote-counter);
  color: var(--color-sidenote);
  font-size: 0.75em;
  vertical-align: super;
  line-height: 0;
  margin-left: 0.1em;
  cursor: pointer;
}

.sidenote::before {
  content: counter(sidenote-counter) ". ";
  color: var(--color-sidenote);
  font-style: normal;
  font-size: 0.85em;
}

@media (max-width: 1100px) {
  .sidenote, .marginnote {
    float: none;
    display: none;
    margin: 0.5rem 0 0.5rem 1rem;
    margin-right: 0;
    padding-left: 0.75rem;
    border-left: 2px solid var(--color-border-muted);
    width: auto;
    text-align: left;
  }

  .sidenote.expanded, .marginnote.expanded {
    display: block;
  }

  /* Hide redundant "N. " prefix -- the tap target already shows the number */
  .sidenote::before { display: none; }

  .sidenote-ref, .marginnote-ref {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .sidenote-ref::after, .marginnote-ref::after {
    text-decoration: underline;
    text-decoration-style: dotted;
    text-underline-offset: 2px;
  }
}

/* ---- Landing header ---- */
.landing-header {
  margin-bottom: 2.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.landing-header h1 {
  font-size: 2.4rem;
  margin-bottom: 0.25rem;
  letter-spacing: -0.02em;
}

.landing-tagline {
  font-size: 1rem;
  color: var(--color-fg-muted);
  margin: 0;
}

/* ---- Table of contents (auto-generated) ---- */
.toc {
  position: fixed;
  left: 0.25rem;
  top: 50%;
  transform: translateY(-50%);
  z-index: 90;
  width: 1.25rem;
  max-height: calc(100vh - 6rem);
  overflow: hidden;
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.6;
  transition: width 0.25s ease, background 0.2s ease, padding 0.2s ease, box-shadow 0.2s ease;
  padding: 0.4rem 0;
  border-radius: 4px;
}

/* Mini-indicator: tiny dashes visible by default */
.toc .toc-label { display: none; }

.toc ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc li {
  margin-bottom: 0;
}

.toc .toc-h3 {
  padding-left: 0;
}

.toc a {
  font-family: var(--font-sans);
  font-size: 0.82rem;
  color: transparent;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.15rem 0.4rem;
  white-space: nowrap;
  transition: color 0.15s;
  overflow: hidden;
}

/* Mini-indicator marks */
.toc a::before {
  content: '';
  flex-shrink: 0;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--color-border-muted);
  transition: background 0.15s, width 0.15s, height 0.15s;
}

.toc a.active::before {
  background: var(--color-sidenote);
  width: 9px;
  height: 9px;
}

/* Expand on hover: show labels */
.toc:hover {
  width: auto;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--color-bg);
  box-shadow: 0 1px 8px rgba(0,0,0,0.1);
  padding: 0.6rem 0;
}

.toc:hover a {
  color: var(--color-fg-muted);
  padding: 0.2rem 0.85rem;
  font-size: 0.85rem;
}

.toc:hover a::before {
  width: 7px;
  height: 7px;
}

.toc:hover a.active::before {
  width: 9px;
  height: 9px;
}

.toc:hover a:hover {
  color: var(--color-fg);
}

.toc:hover a.active {
  color: var(--color-sidenote);
}

.toc:hover .toc-h3 a {
  padding-left: 1.5rem;
}

/* Mobile: inline TOC at top */
@media (max-width: 1100px) {
  .toc {
    position: static;
    transform: none;
    width: auto;
    max-height: none;
    margin-bottom: 1.5rem;
    padding: 0.75rem 1rem;
    border: 1px solid var(--color-border-muted);
    border-radius: 4px;
    background: transparent;
    box-shadow: none;
  }
  .toc a { color: var(--color-fg-muted); white-space: normal; }
  .toc a::before { display: none; }
  .toc a.active { color: var(--color-sidenote); }
  .toc a:hover { color: var(--color-fg); }
  .toc .toc-h3 a { padding-left: 1rem; }
  .toc ul { display: flex; flex-wrap: wrap; gap: 0.15rem 0.75rem; }
}

/* ---- Post date ---- */
.post-date {
  font-family: var(--font-mono);
  font-size: 0.75em;
  color: var(--color-fg-muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  margin-bottom: 1em;
}

/* ---- Post list ---- */
.post-list {
  list-style: none;
  padding-left: 0;
}

.post-item {
  margin-bottom: 0.75em;
  padding-bottom: 0.75em;
  border-bottom: 1px solid var(--color-border-muted);
}

.post-item:last-child { border-bottom: none; }

.post-item .date {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.75em;
  color: var(--color-fg-muted);
  letter-spacing: 0.02em;
}

/* ---- Post navigation (prev/next) ---- */
.post-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3em;
  padding-top: 1em;
  border-top: 1px solid var(--color-border-muted);
  font-size: 0.85em;
}

.post-nav a {
  max-width: 45%;
  color: var(--color-fg-muted);
  text-decoration: none;
}

.post-nav a:hover {
  color: var(--color-fg-accent);
}

.post-nav-next {
  margin-left: auto;
  text-align: right;
}

/* ---- Footer ---- */
footer {
  margin-top: 3em;
  padding-top: 1em;
  border-top: 1px solid var(--color-border);
  text-align: center;
  font-family: var(--font-mono);
  color: var(--color-fg-muted);
  font-size: 0.75em;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ---- Mobile ---- */
@media (max-width: 767px) {
  nav {
    gap: 0.75em;
  }

  h1 { font-size: 1.5em; }
}
