/* Pickering.ae — Gwern-inspired CSS
 * Monochrome, typographically elegant, minimal
 * ============================================ */

/* ========== CSS Variables ========== */
:root {
  /* Colors - Light Mode */
  --bg-color: #fff;
  --text-color: #000;
  --text-muted: #666;
  --text-faint: #888;
  --border-color: #ccc;
  --link-color: #000;
  --link-underline: #888;
  --code-bg: #f5f5f5;
  --highlight-bg: #fffbdd;
  
  /* Typography */
  --font-body: "Source Serif Pro", "ET Book", Georgia, serif;
  --font-sans: "Source Sans Pro", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "Fira Code", "JetBrains Mono", Consolas, monospace;
  
  /* Sizing */
  --font-size-base: 19px;
  --line-height: 1.7;
  --content-width: 65ch;
  --sidenote-width: 280px;
  
  /* Spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 2rem;
  --spacing-xl: 4rem;
}

/* Dark Mode */
[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --text-color: #e0e0e0;
  --text-muted: #999;
  --text-faint: #666;
  --border-color: #444;
  --link-color: #e0e0e0;
  --link-underline: #666;
  --code-bg: #2d2d2d;
  --highlight-bg: #3d3d00;
}

/* ========== Base Styles ========== */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: var(--font-size-base);
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: var(--line-height);
  color: var(--text-color);
  background-color: var(--bg-color);
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* ========== Layout ========== */
.container {
  max-width: calc(var(--content-width) + var(--sidenote-width) + var(--spacing-xl));
  margin: 0 auto;
  padding: var(--spacing-lg);
}

main {
  max-width: var(--content-width);
}

@media (max-width: 1200px) {
  main {
    max-width: 100%;
  }
}

/* ========== Typography ========== */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-body);
  font-weight: 400;
  line-height: 1.3;
  margin-top: var(--spacing-xl);
  margin-bottom: var(--spacing-md);
}

h1 {
  font-size: 2.5rem;
  margin-top: 0;
}

h2 {
  font-size: 1.75rem;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: var(--spacing-sm);
}

h3 {
  font-size: 1.35rem;
}

h4 {
  font-size: 1.15rem;
  font-style: italic;
}

p {
  margin: var(--spacing-md) 0;
  text-align: justify;
  hyphens: auto;
}

/* First paragraph after heading - no top margin */
h1 + p, h2 + p, h3 + p, h4 + p {
  margin-top: 0;
}

/* ========== Links ========== */
a {
  color: var(--link-color);
  text-decoration: none;
  border-bottom: 1px solid var(--link-underline);
  transition: border-color 0.2s;
}

a:hover {
  border-color: var(--text-color);
}

a:visited {
  color: var(--text-muted);
}

/* External link indicator */
a[href^="http"]:not([href*="pickering.ae"])::after {
  content: "↗";
  font-size: 0.7em;
  vertical-align: super;
  margin-left: 0.1em;
  opacity: 0.5;
}

/* ========== Dropcaps ========== */
article > p:first-of-type::first-letter,
.dropcap::first-letter {
  float: left;
  font-size: 3.5em;
  line-height: 0.8;
  padding-right: 0.1em;
  margin-top: 0.05em;
  font-weight: 400;
  color: var(--text-color);
}

/* ========== Small Caps ========== */
.smallcaps {
  font-variant: small-caps;
  letter-spacing: 0.05em;
}

abbr, acronym {
  font-variant: small-caps;
  letter-spacing: 0.05em;
  text-decoration: none;
  border-bottom: 1px dotted var(--text-muted);
  cursor: help;
}

/* ========== Blockquotes / Epigraphs ========== */
blockquote {
  margin: var(--spacing-lg) 0;
  padding-left: var(--spacing-lg);
  border-left: 3px solid var(--border-color);
  font-style: italic;
  color: var(--text-muted);
}

blockquote cite,
blockquote footer {
  display: block;
  margin-top: var(--spacing-sm);
  font-style: normal;
  font-size: 0.9em;
}

blockquote cite::before,
blockquote footer::before {
  content: "— ";
}

.epigraph {
  margin: var(--spacing-xl) 0;
  padding: var(--spacing-lg);
  border: none;
  font-size: 0.95em;
}

.epigraph blockquote {
  border: none;
  padding: 0;
  margin: 0;
}

/* ========== Code ========== */
code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--code-bg);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}

pre {
  background: var(--code-bg);
  padding: var(--spacing-md);
  overflow-x: auto;
  border-radius: 3px;
  line-height: 1.4;
}

pre code {
  background: none;
  padding: 0;
}

/* ========== Sidenotes (Desktop) ========== */
.sidenote,
.marginnote {
  float: right;
  clear: right;
  margin-right: calc(-1 * var(--sidenote-width) - var(--spacing-lg));
  width: var(--sidenote-width);
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text-muted);
  position: relative;
}

.sidenote-number {
  counter-increment: sidenote-counter;
}

.sidenote-number::after,
.sidenote::before {
  font-size: 0.7rem;
  vertical-align: super;
  font-weight: bold;
}

.sidenote-number::after {
  content: counter(sidenote-counter);
}

.sidenote::before {
  content: counter(sidenote-counter) " ";
}

/* Mobile: inline footnotes */
@media (max-width: 1000px) {
  .sidenote,
  .marginnote {
    float: none;
    clear: both;
    display: block;
    margin: var(--spacing-md) 0;
    width: 100%;
    padding: var(--spacing-sm);
    background: var(--code-bg);
    border-radius: 3px;
  }
}

/* ========== Lists ========== */
ul, ol {
  padding-left: 1.5em;
  margin: var(--spacing-md) 0;
}

li {
  margin: var(--spacing-xs) 0;
}

/* Definition lists */
dt {
  font-weight: 600;
  margin-top: var(--spacing-md);
}

dd {
  margin-left: var(--spacing-lg);
  margin-bottom: var(--spacing-sm);
}

/* ========== Tables ========== */
table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--spacing-lg) 0;
  font-size: 0.9rem;
}

th, td {
  padding: var(--spacing-sm);
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}

th {
  font-weight: 600;
  border-bottom-width: 2px;
}

tr:hover {
  background: var(--code-bg);
}

/* ========== Images ========== */
img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: var(--spacing-lg) auto;
}

figure {
  margin: var(--spacing-lg) 0;
}

figcaption {
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
  margin-top: var(--spacing-sm);
}

/* ========== Horizontal Rule ========== */
hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: var(--spacing-xl) 0;
}

/* Section break */
hr.section {
  width: 30%;
  margin: var(--spacing-xl) auto;
}

/* ========== Header ========== */
header.site-header {
  margin-bottom: var(--spacing-xl);
  padding-bottom: var(--spacing-lg);
  border-bottom: 1px solid var(--border-color);
}

.site-title {
  font-size: 1.5rem;
  font-weight: 400;
  margin: 0;
}

.site-title a {
  border: none;
}

nav.main-nav {
  margin-top: var(--spacing-md);
}

nav.main-nav a {
  margin-right: var(--spacing-md);
  font-size: 0.9rem;
  color: var(--text-muted);
}

nav.main-nav a:hover {
  color: var(--text-color);
}

/* ========== Article Meta ========== */
.article-meta {
  margin-bottom: var(--spacing-lg);
  padding-bottom: var(--spacing-md);
  border-bottom: 1px solid var(--border-color);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.article-meta time {
  display: block;
}

.article-meta .status,
.article-meta .confidence {
  display: inline-block;
  padding: 0.1em 0.5em;
  background: var(--code-bg);
  border-radius: 3px;
  margin-right: var(--spacing-sm);
  font-size: 0.8em;
}

/* ========== Table of Contents ========== */
.toc {
  background: var(--code-bg);
  padding: var(--spacing-md);
  margin: var(--spacing-lg) 0;
  border-radius: 3px;
}

.toc-title {
  font-weight: 600;
  margin-bottom: var(--spacing-sm);
}

.toc ul {
  margin: 0;
  padding-left: 1em;
}

.toc li {
  margin: var(--spacing-xs) 0;
}

.toc a {
  border: none;
  color: var(--text-muted);
}

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

/* ========== Footer ========== */
footer.site-footer {
  margin-top: var(--spacing-xl);
  padding-top: var(--spacing-lg);
  border-top: 1px solid var(--border-color);
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* ========== Theme Toggle ========== */
.theme-toggle {
  position: fixed;
  top: var(--spacing-md);
  right: var(--spacing-md);
  z-index: 1000;
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  padding: var(--spacing-sm);
  cursor: pointer;
  font-size: 1.2rem;
}

.theme-toggle:hover {
  background: var(--code-bg);
}

/* ========== Abstract ========== */
.abstract {
  background: var(--code-bg);
  padding: var(--spacing-lg);
  margin: var(--spacing-lg) 0;
  border-radius: 3px;
  border-left: 4px solid var(--border-color);
}

.abstract p:first-child {
  margin-top: 0;
}

.abstract p:last-child {
  margin-bottom: 0;
}

/* ========== Collapsible Sections ========== */
details {
  margin: var(--spacing-md) 0;
}

summary {
  cursor: pointer;
  font-weight: 600;
  padding: var(--spacing-sm);
  background: var(--code-bg);
  border-radius: 3px;
}

summary:hover {
  background: var(--highlight-bg);
}

details[open] summary {
  margin-bottom: var(--spacing-sm);
}

/* ========== Print Styles ========== */
@media print {
  body {
    font-size: 12pt;
    line-height: 1.5;
  }
  
  .theme-toggle,
  nav,
  .toc {
    display: none;
  }
  
  a {
    color: inherit;
    border: none;
  }
  
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
  }
}

/* ========== Selection ========== */
::selection {
  background: var(--text-color);
  color: var(--bg-color);
}

/* ========== Focus States ========== */
a:focus,
button:focus,
input:focus {
  outline: 2px solid var(--link-color);
  outline-offset: 2px;
}

/* ========== Utility Classes ========== */
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-muted { color: var(--text-muted); }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ========== Link Popups (Gwern-style) ========== */
.link-popup {
  position: absolute;
  z-index: 9999;
  width: 400px;
  max-height: 300px;
  overflow: hidden;
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s;
  pointer-events: none;
}

.link-popup.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.link-popup-content {
  padding: var(--spacing-md);
}

.popup-title {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: var(--spacing-sm);
  line-height: 1.3;
}

.popup-excerpt {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.popup-error {
  color: var(--text-faint);
  font-style: italic;
}

@media (max-width: 600px) {
  .link-popup {
    display: none !important;
  }
}

/* ========== Sidenote Counter (CSS) ========== */
body {
  counter-reset: sidenote-counter;
}

.sidenote-number {
  counter-increment: sidenote-counter;
  cursor: pointer;
}

.sidenote-number::after {
  content: counter(sidenote-counter);
  font-size: 0.6em;
  vertical-align: super;
  color: var(--link-color);
  margin-left: 0.05em;
}

.sidenote::before {
  content: counter(sidenote-counter) ". ";
  font-size: 0.85em;
  font-weight: 600;
}

/* ========== Article Enhancements ========== */
article {
  position: relative;
}

.article-header {
  margin-bottom: var(--spacing-xl);
}

.article-header h1 {
  margin-bottom: var(--spacing-sm);
}

.article-subtitle {
  font-size: 1.3rem;
  color: var(--text-muted);
  font-style: italic;
  margin-bottom: var(--spacing-md);
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-sm);
  align-items: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  padding-bottom: var(--spacing-md);
  border-bottom: 1px solid var(--border-color);
  margin-bottom: var(--spacing-lg);
}

.meta-item {
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
}

.tag {
  display: inline-block;
  padding: 0.1em 0.5em;
  background: var(--code-bg);
  border-radius: 3px;
  font-size: 0.8em;
  color: var(--text-muted);
  text-decoration: none;
  border: none;
}

.tag:hover {
  background: var(--border-color);
  border: none;
}

/* Status badges */
.status-badge {
  display: inline-block;
  padding: 0.15em 0.6em;
  border-radius: 3px;
  font-size: 0.75em;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.status-finished { background: #d4edda; color: #155724; }
.status-in-progress { background: #fff3cd; color: #856404; }
.status-draft { background: #f8d7da; color: #721c24; }

[data-theme="dark"] .status-finished { background: #1e3a2f; color: #8fd19e; }
[data-theme="dark"] .status-in-progress { background: #3d3321; color: #ffc107; }
[data-theme="dark"] .status-draft { background: #3d2124; color: #f5c6cb; }

/* ========== Math Blocks ========== */
.katex-display {
  margin: var(--spacing-lg) 0;
  overflow-x: auto;
  overflow-y: hidden;
}

/* ========== Essay List ========== */
.essay-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.essay-item {
  margin-bottom: var(--spacing-lg);
  padding-bottom: var(--spacing-lg);
  border-bottom: 1px solid var(--border-color);
}

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

.essay-title {
  font-size: 1.3rem;
  margin-bottom: var(--spacing-xs);
}

.essay-title a {
  border: none;
}

.essay-title a:hover {
  text-decoration: underline;
}

.essay-meta {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: var(--spacing-sm);
}

.essay-abstract {
  color: var(--text-muted);
  font-size: 0.95rem;
}
