/* termin.dev — main stylesheet
 *
 * Research-paper aesthetic. Typography-first. Light mode defaults; dark mode
 * via [data-theme="dark"] on <html>. No animations, no gradients, no flash.
 *
 * WCAG AA contrast verified on all text/background pairs.
 */

/* ── Theme tokens ─────────────────────────────────────────────────────── */

:root {
  /* Light mode */
  --bg: #fdfdfc;
  --fg: #14131a;
  --fg-muted: #55525e;
  --fg-subtle: #7a7683;
  --border: #e4e1dc;
  --border-strong: #c8c4bd;
  --accent: #2a5486;
  --accent-hover: #1a3a5f;
  --code-bg: #f4f2ed;
  --code-fg: #14131a;
  --callout-bg: #f8f5f0;
  --selection: #d7e4f2;
}

html[data-theme="dark"] {
  --bg: #14131a;
  --fg: #e7e4dc;
  --fg-muted: #a29c8f;
  --fg-subtle: #7a7683;
  --border: #2a2832;
  --border-strong: #403c47;
  --accent: #8fb1dc;
  --accent-hover: #b5cdee;
  --code-bg: #1d1c25;
  --code-fg: #e7e4dc;
  --callout-bg: #1a1922;
  --selection: #2a4066;
}

/* ── Base typography ──────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }

html {
  font-size: 17px;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: "Iowan Old Style", "Charter", "Georgia", "Times New Roman", serif;
  line-height: 1.65;
  font-feature-settings: "kern", "liga", "onum";
}

::selection { background: var(--selection); }

h1, h2, h3, h4, h5, h6 {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  line-height: 1.25;
  letter-spacing: -0.01em;
  color: var(--fg);
  margin-top: 2.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
}

h1 { font-size: 2.1rem; margin-top: 0; letter-spacing: -0.02em; }
h2 { font-size: 1.55rem; }
h3 { font-size: 1.2rem; }
h4 { font-size: 1rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--fg-muted); }

p, ul, ol, blockquote {
  margin-top: 0;
  margin-bottom: 1.2rem;
}

ul, ol { padding-left: 1.4rem; }
li { margin-bottom: 0.35rem; }

a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 0.06em;
  text-underline-offset: 0.15em;
  transition: color 0.08s ease;
}
a:hover, a:focus-visible { color: var(--accent-hover); }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.5rem 0;
}

/* ── Code ─────────────────────────────────────────────────────────────── */

code, pre, kbd, samp {
  font-family: "SF Mono", "Menlo", "Monaco", "Cascadia Mono", "Consolas", monospace;
  font-size: 0.9em;
}

:not(pre) > code {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 0.12em 0.35em;
  border-radius: 3px;
  font-size: 0.88em;
}

pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 1rem 1.2rem;
  border-radius: 4px;
  overflow-x: auto;
  line-height: 1.55;
  border: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

pre code {
  background: transparent;
  padding: 0;
  font-size: 0.92em;
}

/* ── Layout ───────────────────────────────────────────────────────────── */

.container {
  max-width: 44rem;
  margin: 0 auto;
  padding: 0 1.25rem;
}

main.container {
  padding-top: 2.5rem;
  padding-bottom: 4rem;
  min-height: calc(100vh - 9rem);
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
.skip-link:focus {
  position: static;
  width: auto;
  height: auto;
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: #fff;
}

/* ── Header ───────────────────────────────────────────────────────────── */

.site-header {
  border-bottom: 1px solid var(--border);
  padding: 1rem 0;
  background: var(--bg);
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.wordmark {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--fg);
  text-decoration: none;
  letter-spacing: -0.01em;
}
.wordmark:hover { color: var(--accent); }

.site-nav { flex: 1; }
.site-nav ul {
  display: flex;
  gap: 1.25rem;
  list-style: none;
  margin: 0;
  padding: 0;
  flex-wrap: wrap;
}
.site-nav a {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 0.9rem;
  color: var(--fg-muted);
  text-decoration: none;
}
.site-nav a:hover,
.site-nav a[aria-current="page"] { color: var(--fg); }
.site-nav a[aria-current="page"] { text-decoration: underline; text-decoration-thickness: 0.08em; text-underline-offset: 0.3em; }

.theme-toggle {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 0.85rem;
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--fg-muted);
  padding: 0.35rem 0.75rem;
  border-radius: 3px;
  cursor: pointer;
}
.theme-toggle:hover { color: var(--fg); border-color: var(--fg-muted); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Footer ───────────────────────────────────────────────────────────── */

.site-footer {
  border-top: 1px solid var(--border);
  padding: 1.75rem 0;
  margin-top: 4rem;
  color: var(--fg-muted);
  font-size: 0.9rem;
}

.footer-inner {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.footer-nav ul {
  display: flex;
  gap: 1rem;
  list-style: none;
  margin: 0;
  padding: 0;
  flex-wrap: wrap;
}
.footer-nav a {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color: var(--fg-muted);
  text-decoration: none;
}
.footer-nav a:hover { color: var(--fg); text-decoration: underline; }

.maintainer {
  margin: 0;
  font-size: 0.85rem;
  color: var(--fg-subtle);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

/* ── Heading anchors ──────────────────────────────────────────────────── */

/* markdown-it-anchor adds an invisible-by-default anchor link before each
   heading. Surface it on hover for "copy link to section" behavior. */
.heading-anchor {
  color: var(--fg-subtle);
  text-decoration: none;
  margin-right: 0.4rem;
  opacity: 0;
  transition: opacity 0.1s ease;
  font-weight: 400;
}
h2:hover .heading-anchor,
h3:hover .heading-anchor,
h4:hover .heading-anchor,
.heading-anchor:focus {
  opacity: 1;
}

/* Scroll offset for hash-jump: account for sticky header. */
h2[id], h3[id], h4[id] {
  scroll-margin-top: 5rem;
}

/* ── Table of contents (opt-in via `toc: true` in frontmatter) ─────────── */

.page-with-toc {
  display: grid;
  grid-template-columns: 1fr minmax(0, 44rem) 1fr;
  gap: 2rem;
  max-width: 80rem;
  margin: 0 auto;
  padding: 0 1.25rem;
}

.page-with-toc > main {
  grid-column: 2;
  padding-top: 2.5rem;
  padding-bottom: 4rem;
  min-width: 0; /* allows <pre> to overflow-x without breaking grid */
}

.toc-sidebar {
  grid-column: 1;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 0.85rem;
  padding-top: 2.5rem;
}

.toc-desktop {
  position: sticky;
  top: 5rem;
  max-height: calc(100vh - 7rem);
  overflow-y: auto;
  padding-right: 1rem;
}

.toc-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-subtle);
  margin: 0 0 0.75rem;
  font-weight: 600;
}

.toc-mobile-toggle { display: none; }

.toc ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.toc > ul > li > ul { padding-left: 0.9rem; }
.toc li { margin: 0.1rem 0; }
.toc a {
  display: block;
  color: var(--fg-muted);
  text-decoration: none;
  padding: 0.2rem 0.4rem;
  border-left: 2px solid transparent;
  line-height: 1.3;
}
.toc a:hover {
  color: var(--fg);
  background: var(--code-bg);
}
.toc a.toc-active {
  color: var(--fg);
  border-left-color: var(--accent);
  font-weight: 600;
  background: var(--code-bg);
}

/* ── Responsive ───────────────────────────────────────────────────────── */

@media (max-width: 64rem) {
  /* Tablet: drop the TOC sidebar and use the mobile collapsed form. */
  .page-with-toc {
    display: block;
    max-width: 44rem;
  }
  .page-with-toc > main { padding: 2.5rem 0 4rem; }
  .toc-sidebar { padding-top: 1.5rem; }
  .toc-desktop { display: none; }
  .toc-mobile-toggle {
    display: block;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.5rem 0.75rem;
    margin-bottom: 1.5rem;
    background: var(--bg);
  }
  .toc-mobile-toggle > summary {
    cursor: pointer;
    color: var(--fg-muted);
    font-size: 0.85rem;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    list-style: none;
  }
  .toc-mobile-toggle > summary::-webkit-details-marker { display: none; }
  .toc-mobile-toggle > summary::before {
    content: "▸ ";
    display: inline-block;
    transition: transform 0.1s ease;
  }
  .toc-mobile-toggle[open] > summary::before {
    transform: rotate(90deg);
  }
  .toc-mobile-toggle .toc { margin-top: 0.75rem; }
}

@media (max-width: 42rem) {
  html { font-size: 16px; }
  h1 { font-size: 1.75rem; }
  h2 { font-size: 1.35rem; }
  .header-inner { gap: 0.75rem; }
  .site-nav ul { gap: 0.85rem; }
  .theme-toggle { order: 99; }
}

/* ── Print ────────────────────────────────────────────────────────────── */

@media print {
  html { font-size: 11pt; }
  body { background: #fff; color: #000; }
  .site-nav, .theme-toggle, .skip-link, .toc-sidebar { display: none; }
  .page-with-toc { display: block; max-width: none; }
  a { color: inherit; text-decoration: underline; }
  a::after { content: " (" attr(href) ")"; font-size: 0.8em; color: #444; }
  pre, code { background: #f4f2ed; }
}
