/* ============================================================
   1. TOKENS — single source of truth
   ============================================================ */
:root {
  /* Colour — light mode (warm paper) */
  --color-bg:             #f6efe1;
  --color-bg-subtle:      rgba(92, 110, 62, 0.06);
  --color-text:           #1f1b15;
  --color-text-secondary: #5f574a;
  --color-accent:         #5c6e3e;
  --color-border:         #e2dac4;
  --color-selection:      rgba(92, 110, 62, 0.18);

  /* Type families */
  --font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-sans:  "Geist Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono:  Consolas, Monaco, monospace;

  /* Type scale */
  --text-xs:   0.75rem;
  --text-sm:   0.875rem;
  --text-base: 1rem;
  --text-md:   1.125rem;
  --text-lg:   1.5rem;
  --text-xl:   2rem;
  --text-2xl:  2.5rem;

  /* Spacing */
  --space-xs:  0.5rem;
  --space-sm:  1rem;
  --space-md:  2rem;
  --space-lg:  4rem;

  /* Layout */
  --container-width: 1000px;
  --content-width:   65ch;
  --margin-width:    30%;

  /* Radii */
  --radius-pill: 12px;

  /* Motion */
  --motion-fast:    150ms;
  --motion-base:    300ms;
  --motion-slow:    600ms;
  --motion-ease:    cubic-bezier(0.16, 1, 0.3, 1);
  --motion-ease-io: cubic-bezier(0.65, 0, 0.35, 1);
}

[data-theme="dark"] {
  --color-bg:             #1a1815;
  --color-bg-subtle:      rgba(154, 176, 122, 0.08);
  --color-text:           #ebe4d4;
  --color-text-secondary: #a39d8d;
  --color-accent:         #9ab07a;
  --color-border:         #2a261f;
  --color-selection:      rgba(154, 176, 122, 0.25);
}

/* ============================================================
   2. @FONT-FACE — self-hosted variable fonts, latin subset
   ============================================================ */
@font-face {
  font-family: "Source Serif 4";
  src: url("fonts/source-serif-4-latin.woff2") format("woff2-variations");
  font-weight: 200 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Source Serif 4";
  src: url("fonts/source-serif-4-latin-italic.woff2") format("woff2-variations");
  font-weight: 200 900;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: "Geist Sans";
  src: url("fonts/geist-sans-latin.woff2") format("woff2-variations");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

/* ============================================================
   3. RESET & BASE
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
}

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-serif);
  font-size: var(--text-md);
  line-height: 1.7;
  font-variation-settings: "opsz" auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background var(--motion-base) var(--motion-ease),
              color var(--motion-base) var(--motion-ease);
}

h1, h2, h3, h4, h5, h6 {
  margin: 0 0 var(--space-sm) 0;
  font-family: var(--font-serif);
  font-weight: 400;
  line-height: 1.2;
  color: var(--color-text);
}

h1 { font-size: var(--text-2xl); font-weight: 600; margin-bottom: var(--space-md); }
h2 { font-size: var(--text-xl);  font-style: italic;  margin: var(--space-lg) 0 var(--space-md); }
h3 { font-size: var(--text-lg);  font-weight: 500;    margin: var(--space-md) 0 var(--space-sm); }

p {
  margin: var(--space-sm) 0;
  max-width: var(--content-width);
  line-height: 1.7;
  hyphens: auto;
}

.subtitle {
  font-size: var(--text-lg);
  font-style: italic;
  color: var(--color-accent);
  margin-bottom: var(--space-lg);
}

a {
  color: var(--color-accent);
  text-decoration: none;
  border-bottom: 1px solid var(--color-accent);
  transition: border-bottom-width var(--motion-fast) var(--motion-ease);
}

a:hover {
  border-bottom-width: 2px;
}

ul, ol {
  padding-left: var(--space-md);
  max-width: var(--content-width);
}

li { margin-bottom: var(--space-xs); }

code, pre {
  font-family: var(--font-mono);
  font-size: 0.92em;
}

code {
  background: var(--color-bg-subtle);
  padding: 0.1em 0.35em;
  border-radius: 3px;
}

pre {
  background: var(--color-bg-subtle);
  padding: var(--space-sm);
  border-radius: 4px;
  overflow-x: auto;
  max-width: var(--content-width);
}

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

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

img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* ============================================================
   4. LAYOUT
   ============================================================ */
.container {
  width: 100%;
  max-width: var(--container-width);
  margin: 0 auto;
  padding: var(--space-md);
}

/* No special top padding — the static .site-header provides its own spacing */

.section {
  margin: var(--space-lg) 0;
  padding: var(--space-md) 0;
  display: flow-root;
}

/* ============================================================
   5. HEADER & NAV — static, stacked (brand row + nav row)
   ============================================================ */
.site-header {
  border-bottom: 1px solid var(--color-border);
  margin-bottom: var(--space-md);
}

.header-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-sm);
  max-width: var(--container-width);
  margin: 0 auto;
  padding: var(--space-md) var(--space-md) var(--space-xs);
}

/* Header variant for pages without a brand (homepage) — push actions to the right edge. */
.header-row.actions-only { justify-content: flex-end; }

.brand {
  font-family: var(--font-serif);
  font-size: var(--text-xl);
  font-weight: 600;
  color: var(--color-text);
  border-bottom: none;
  line-height: 1.2;
}

.brand:hover { border-bottom: none; color: var(--color-accent); }

.nav-menu {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-md);
  max-width: var(--container-width);
  margin: 0 auto;
  padding: 0 var(--space-md) var(--space-md);
  font-family: var(--font-sans);
}

.nav-menu a {
  color: var(--color-text-secondary);
  border-bottom: 1px solid transparent;
  padding: 0.25rem 0;
  font-weight: 500;
  font-size: var(--text-base);
}

.nav-menu a:hover,
.nav-menu a:focus-visible,
.nav-menu a.active {
  border-bottom-color: var(--color-accent);
  color: var(--color-accent);
  outline: none;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  padding: var(--space-xs);
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color var(--motion-fast) var(--motion-ease);
}

.theme-toggle:hover { color: var(--color-accent); }

.theme-toggle:focus-visible {
  color: var(--color-accent);
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.theme-toggle svg { width: 20px; height: 20px; stroke-width: 2px; }
.theme-toggle .moon-icon { display: none; }
[data-theme="dark"] .sun-icon  { display: none; }
[data-theme="dark"] .moon-icon { display: block; }

/* ============================================================
   7. SIDENOTE + MARGINNOTE
   ============================================================ */
body { counter-reset: sidenote-counter; }

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

.sidenote-number::after {
  content: counter(sidenote-counter);
  font-size: 0.75em;
  vertical-align: super;
  color: var(--color-accent);
  margin-left: 0.15em;
  font-family: var(--font-sans);
  font-weight: 500;
}

.sidenote-number:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.sidenote-toggle[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: -1px;
  clip-path: inset(50%);
  white-space: nowrap;
  overflow: hidden;
}

/* Contain the marginnote/sidenote float to its parent paragraph so a
   long note can't bleed past its anchor into the next sibling block. */
p:has(> .marginnote),
p:has(> .sidenote) {
  display: flow-root;
}

.sidenote,
.marginnote {
  float: right;
  clear: right;
  width: var(--margin-width);
  margin-right: calc(-1 * var(--margin-width) - var(--space-md));
  margin-top: 0.3rem;
  margin-bottom: var(--space-sm);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--color-text-secondary);
  position: relative;
  padding-left: var(--space-sm);
  border-left: 2px solid var(--color-accent);
}

.marginnote { font-style: italic; }

@media (max-width: 1100px) {
  .sidenote,
  .marginnote {
    display: block;
    float: none;
    width: auto;
    margin: var(--space-sm) 0;
    padding: var(--space-sm) var(--space-md);
    background: var(--color-bg-subtle);
    border-left: 2px solid var(--color-accent);
    font-size: var(--text-base);
    line-height: 1.5;
    color: var(--color-text-secondary);
  }

  /* Hide sidenote by default on narrow screens; tap the number to reveal */
  .sidenote { display: none; }
  .sidenote-toggle:checked + .sidenote { display: block; }
}

/* ============================================================
   8. UTILITIES — focus, prefers-reduced-motion
   ============================================================ */
:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Card fade-in (one-time, on first scroll into view) */
.fade-ready {
  opacity: 0;
  transform: translateY(12px);
  transition: opacity var(--motion-slow) var(--motion-ease),
              transform var(--motion-slow) var(--motion-ease);
}

.fade-ready.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* ============================================================
   9. RESPONSIVE
   ============================================================ */
@media (max-width: 1200px) {
  :root { --container-width: 95%; }
}

@media (max-width: 768px) {
  :root {
    --space-lg: 2rem;
    --space-md: 1.5rem;
    --text-2xl: 2rem;
    --text-xl:  1.6rem;
    --text-lg:  1.3rem;
  }

  body { font-size: var(--text-base); }

  /* Long publication titles / author lists can break to avoid overflow */
  .publications-list li {
    overflow-wrap: anywhere;
  }
}

@media (max-width: 480px) {
  :root {
    --space-lg: 1.5rem;
    --space-md: 1rem;
    --text-2xl: 1.75rem;
  }

  .nav-menu { gap: var(--space-sm); }
}

/* Ultra-narrow phones (e.g., 320 px iPhone SE) */
@media (max-width: 360px) {
  :root {
    --text-2xl: 1.5rem;
    --text-xl:  1.4rem;
  }
}

/* ============================================================
   10. PRINT
   ============================================================ */
@media print {
  .nav-menu,
  .theme-toggle,
  .search-btn,
  .search-container { display: none !important; }

  main.container { padding-top: 0; }

  body {
    background: white;
    color: black;
    font-size: 11pt;
  }

  a { color: black; text-decoration: underline; }

  .sidenote,
  .marginnote {
    float: none;
    width: 100%;
    margin: 0.5rem 0;
    padding: 0.25rem 0.5rem;
    background: none;
    color: #444;
    font-size: 10pt;
  }

  .sidenote { display: block !important; }
}

/* 404 page */
.error-page {
  max-width: 600px;
  margin: 0 auto;
  padding: var(--space-lg) var(--space-md);
  text-align: center;
}

.error-page h1 {
  font-size: 5rem;
  color: var(--color-accent);
  margin: var(--space-sm) 0;
  line-height: 1;
}

.error-subtitle {
  font-size: var(--text-lg);
  font-style: italic;
  color: var(--color-text-secondary);
  margin-bottom: var(--space-md);
}

.error-content {
  background: var(--color-bg-subtle);
  padding: var(--space-md);
  border-radius: 4px;
  margin: var(--space-md) 0;
  text-align: left;
}

.troubleshooting { margin: var(--space-md) 0; }

.troubleshooting p {
  font-weight: 500;
  color: var(--color-accent);
  margin-bottom: var(--space-sm);
}

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

.troubleshooting li {
  padding-left: var(--space-md);
  position: relative;
  margin-bottom: var(--space-xs);
}

.troubleshooting li::before {
  content: "•";
  position: absolute;
  left: 0;
  color: var(--color-accent);
}

.back-home {
  display: inline-block;
  margin-top: var(--space-sm);
  padding: 0.6rem 1.2rem;
  font-family: var(--font-sans);
  font-weight: 500;
  border: 1px solid var(--color-accent);
  border-radius: 4px;
  color: var(--color-accent);
  transition: background var(--motion-fast) var(--motion-ease),
              color var(--motion-fast) var(--motion-ease);
}

.back-home:hover {
  background: var(--color-accent);
  color: var(--color-bg);
  border-bottom: 1px solid var(--color-accent);
}

.lab-equipment { color: var(--color-accent); margin-bottom: var(--space-md); }

/* Blog landing — post list */
.post-list {
  list-style: none;
  padding: 0;
  margin: var(--space-lg) 0;
  max-width: var(--content-width);
}

.post-list .post-entry {
  margin: 0 0 var(--space-lg) 0;
  padding-bottom: var(--space-lg);
  border-bottom: 1px solid var(--color-border);
}

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

.post-list .post-entry h2 {
  margin: var(--space-xs) 0;
  font-style: normal;
  font-size: var(--text-lg);
}

.post-list .post-entry h2 a {
  color: var(--color-text);
  border-bottom: 1px solid transparent;
}

.post-list .post-entry h2 a:hover {
  color: var(--color-accent);
  border-bottom-color: var(--color-accent);
  border-bottom-width: 1px;
}

.post-list .post-meta {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  display: flex;
  gap: var(--space-md);
  margin: 0;
}

.post-list .post-excerpt {
  margin: var(--space-xs) 0 var(--space-sm);
  color: var(--color-text-secondary);
}

.blog-empty-state {
  max-width: var(--content-width);
  margin: var(--space-lg) 0;
  color: var(--color-text-secondary);
  font-style: italic;
}

/* Post-meta separator (between date and read-time) — shared by landing + posts */
.post-read-time::before {
  content: "·";
  margin-right: var(--space-sm);
  color: var(--color-border);
}

/* Post tags (pill list) — shared by landing entries and post headers */
.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  list-style: none;
  padding: 0;
  margin: var(--space-sm) 0 0;
}

.post-tags li {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: 0.2rem 0.65rem;
  background: var(--color-bg-subtle);
  color: var(--color-accent);
  border-radius: var(--radius-pill);
  margin: 0;
}

/* Publications — academic citation list */
.publications-list {
  list-style: none;
  counter-reset: pub-counter;
  padding: 0;
  margin: var(--space-md) 0;
  max-width: var(--content-width);
}

.publications-list li {
  counter-increment: pub-counter;
  margin-bottom: var(--space-md);
  padding-left: var(--space-md);
  position: relative;
  line-height: 1.6;
}

.publications-list li::before {
  content: counter(pub-counter) ".";
  position: absolute;
  left: 0;
  top: 0;
  font-family: var(--font-sans);
  font-weight: 500;
  color: var(--color-accent);
}

.publications-list .pub-title {
  display: block;
  margin: 0.25rem 0;
  color: var(--color-text);
}

.publications-list cite {
  font-style: italic;
}

.publications-list a {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
}

/* ============================================================
   11. REVAMP-V2 ADDITIONS
   ============================================================ */

/* Homepage directory list — arrowed vertical list */
.home-directory {
  list-style: none;
  padding: 0;
  margin: var(--space-md) 0 0;
  max-width: var(--content-width);
  font-family: var(--font-sans);
}

.home-directory li {
  margin: var(--space-xs) 0;
}

.home-directory li::before {
  content: "→";
  margin-right: var(--space-xs);
  color: var(--color-accent);
}

.home-directory a {
  font-size: var(--text-md);
  border-bottom: 1px solid transparent;
  color: var(--color-text);
}

.home-directory a:hover,
.home-directory a:focus-visible {
  border-bottom-color: var(--color-accent);
  color: var(--color-accent);
  outline: none;
}

/* Site footer — every page */
.site-footer {
  border-top: 1px solid var(--color-border);
  margin-top: var(--space-lg);
  padding: var(--space-md) 0;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

.site-footer p {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-sm);
  max-width: var(--container-width);
  margin: 0 auto;
  padding: 0 var(--space-md);
}

.site-footer .footer-meta {
  margin-left: auto;
}

/* Projects list — definition list layout used on /projects/ */
.project-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
  column-gap: var(--space-md);
  row-gap: var(--space-sm);
  margin: var(--space-md) 0;
  max-width: var(--content-width);
}

.project-list dt {
  font-family: var(--font-sans);
  font-weight: 500;
}

.project-list dd {
  margin: 0;
  color: var(--color-text-secondary);
}

@media (max-width: 600px) {
  .project-list {
    grid-template-columns: 1fr;
  }
  .project-list dd {
    margin-bottom: var(--space-sm);
  }
}

/* Reading list — book log used on /reading/ */
.reading-list {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-md);
}

.reading-list li {
  margin-bottom: var(--space-md);
}

.reading-list li:last-child {
  margin-bottom: 0;
}

.reading-list cite {
  font-style: italic;
}

.reading-note {
  margin: 0.25rem 0 0;
  color: var(--color-text-secondary);
}

/* ============================================================
   SEARCH — modal + cross-page index UI
   Loaded on every page; modal is injected by js/search.js.
   ============================================================ */

/* Search button (in the page header) */
.search-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 0.35rem var(--space-sm);
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  color: var(--color-text-secondary);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: color var(--motion-fast) var(--motion-ease),
              border-color var(--motion-fast) var(--motion-ease);
}

.search-btn:hover,
.search-btn:focus-visible {
  color: var(--color-accent);
  border-color: var(--color-accent);
  outline: none;
}

.search-btn svg {
  width: 16px;
  height: 16px;
}

.search-shortcut {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
}

/* Modal container — full-viewport overlay */
.search-container {
  position: fixed;
  inset: 0;
  z-index: 1000;
}

.search-container[hidden] { display: none; }

.search-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

[data-theme="dark"] .search-overlay {
  background: rgba(0, 0, 0, 0.55);
}

/* Modal panel */
.search-modal {
  position: relative;
  width: min(90%, 640px);
  max-height: 80vh;
  margin: 6vh auto 0;
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
  overflow: hidden;
}

/* Header — input + close button */
.search-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--color-border);
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  flex: 1;
}

.search-input-wrapper .search-icon {
  color: var(--color-text-secondary);
  flex-shrink: 0;
}

#search-input {
  flex: 1;
  padding: 0.35rem 0;
  font-family: var(--font-serif);
  font-size: var(--text-md);
  background: transparent;
  border: none;
  color: var(--color-text);
}

#search-input:focus { outline: none; }

#search-input::placeholder {
  color: var(--color-text-secondary);
  font-style: italic;
}

.search-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--color-text-secondary);
  padding: 0 var(--space-xs);
  transition: color var(--motion-fast) var(--motion-ease);
}

.search-close:hover,
.search-close:focus-visible {
  color: var(--color-accent);
  outline: none;
}

/* Results list */
.search-results {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-xs) 0;
}

.search-state {
  padding: var(--space-md);
  text-align: center;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-style: italic;
  color: var(--color-text-secondary);
}

.search-result {
  display: block;
  padding: var(--space-sm) var(--space-md);
  border-left: 2px solid transparent;
  color: var(--color-text);
  text-decoration: none;
  transition: background var(--motion-fast) var(--motion-ease),
              border-color var(--motion-fast) var(--motion-ease);
}

.search-result:hover,
.search-result.is-active,
.search-result:focus-visible {
  background: var(--color-bg-subtle);
  border-left-color: var(--color-accent);
  outline: none;
}

.search-result-title {
  font-family: var(--font-sans);
  font-weight: 500;
  font-size: var(--text-base);
  color: var(--color-text);
  margin-bottom: 0.25rem;
}

.search-result-snippet {
  font-family: var(--font-serif);
  font-size: var(--text-sm);
  line-height: 1.5;
  color: var(--color-text-secondary);
}

.search-result mark {
  background: transparent;
  color: var(--color-accent);
  font-weight: 600;
}

/* Footer — keyboard hints */
.search-footer {
  padding: var(--space-xs) var(--space-md);
  border-top: 1px solid var(--color-border);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
  text-align: right;
}

.search-footer kbd {
  display: inline-block;
  padding: 0 0.35rem;
  margin: 0 0.1rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: 3px;
}

/* Responsive */
@media (max-width: 768px) {
  .search-modal {
    width: 95%;
    margin: 4vh auto 0;
    max-height: 88vh;
  }

  .search-shortcut { display: none; }

  .search-btn {
    padding: 0.35rem;
  }

  .search-footer {
    text-align: center;
  }
}

