/* ---------- Tokens ---------- */
:root {
  --color-ivory-light: #faf9f5;
  --color-ivory-medium: #f0eee6;
  --color-ivory-dark: #e8e6dc;
  --color-cloud-light: #d1cfc5;
  --color-cloud-medium: #b0aea5;
  --color-cloud-dark: #87867f;
  --color-slate-light: #5e5d59;
  --color-slate-medium: #3d3d3a;
  --color-slate-dark: #1f1e1d;

  --theme-background: var(--color-ivory-medium);
  --theme-text: var(--color-slate-dark);
  --theme-text-secondary: var(--color-slate-light);
  --theme-text-link-hover: var(--color-slate-light);
  --theme-border: var(--color-cloud-light);
  --theme-footnote-background: #00000014;
  --theme-footnote-background-hover: #00000026;
  --theme-footnote-tooltip-background: var(--color-ivory-light);

  --font-text: 'Newsreader', georgia, serif;
  --font-weight-regular: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  --font-size-body: 1.25rem;
  --font-size-h1: 3.1em;
  --font-size-h2: 1.6em;
  --font-size-h3: 1.2em;
  --font-size-paragraph-lg: 1.1em;
  --font-size-paragraph-sm: 0.9em;
  --line-height-h1: 1.1;
  --line-height-heading: 1.2;
  --line-height-body: 1.6;
  --max-width-narrow: 38.75rem;
  --gap-md: 1.25rem;
  --margin-sm: 1.3em;
  --margin-md: 2em;
  --margin-lg: 3em;
}

:root[data-color-mode='dark'] {
  --theme-background: var(--color-slate-dark);
  --theme-text: var(--color-ivory-medium);
  --theme-text-secondary: var(--color-cloud-medium);
  --theme-text-link-hover: var(--color-cloud-medium);
  --theme-border: var(--color-slate-light);
  --theme-footnote-background: #ffffff26;
  --theme-footnote-background-hover: #fff6;
  --theme-footnote-tooltip-background: var(--color-slate-medium);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-color-mode='light']) {
    --theme-background: var(--color-slate-dark);
    --theme-text: var(--color-ivory-medium);
    --theme-text-secondary: var(--color-cloud-medium);
    --theme-text-link-hover: var(--color-cloud-medium);
    --theme-border: var(--color-slate-light);
    --theme-footnote-background: #ffffff26;
    --theme-footnote-background-hover: #fff6;
    --theme-footnote-tooltip-background: var(--color-slate-medium);
  }
}

@media (max-width: 767px) {
  :root { --font-size-h1: 2.4em; }
}

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

html { text-size-adjust: 100%; -webkit-text-size-adjust: 100%; height: 100%; }

body {
  background-color: var(--theme-background);
  color: var(--theme-text);
  font-family: var(--font-text);
  font-size: var(--font-size-body);
  font-weight: var(--font-weight-regular);
  font-variation-settings: 'opsz' 24;
  line-height: var(--line-height-body);
  min-height: 100%;
  margin: 0;
  transition: color 0.2s linear, background-color 0.2s linear;
}

::selection { color: inherit; background-color: #0064ff42; }

h1 { font-size: var(--font-size-h1); font-weight: var(--font-weight-bold); line-height: var(--line-height-h1); margin: 0 0 0.2em; }
h2 { font-size: var(--font-size-h2); font-weight: var(--font-weight-semibold); line-height: var(--line-height-heading); margin: 0 0 0.5em; }
h3 { font-size: var(--font-size-h3); font-weight: var(--font-weight-semibold); line-height: var(--line-height-heading); margin: 0 0 0.7em; }
p { margin: 0 0 1em; }
ul, ol { margin: 0 0 1em; padding-left: 2.2em; }
li { margin-bottom: 0.3em; }

a {
  color: var(--theme-text);
  text-decoration: underline;
  text-decoration-thickness: 0.08em;
  text-underline-offset: 0.1em;
  transition: color 0.2s linear;
}
a:hover { color: var(--theme-text-link-hover); }

b, strong { font-weight: var(--font-weight-semibold); }
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
img { max-width: 100%; height: auto; }

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link { position: absolute; left: -9999px; top: 0; z-index: 10; padding: 0.5em 1em; background: var(--theme-background); }
.skip-link:focus { left: 1em; top: 1em; }

/* ---------- Page shell ---------- */
.wrapper {
  background-color: var(--theme-background);
  color: var(--theme-text);
  display: flex;
  flex-flow: column;
  min-height: 100vh;
  overflow: clip;
  position: relative;
  transition: color 0.2s, background-color 0.2s;
}

.main { display: flex; flex: 1; flex-flow: column; }

.container,
.bar,
.contents-inner {
  width: 100%;
  max-width: var(--max-width-narrow);
  margin-inline: auto;
  padding-inline: var(--gap-md);
}

@media (max-width: 991px) {
  .container, .bar, .contents-inner { width: 80%; max-width: none; padding-inline: 10px; }
}
@media (max-width: 479px) {
  .container, .bar, .contents-inner { width: 95%; }
}

.footer {
  margin: var(--margin-sm) 0;
  text-align: center;
  font-size: var(--font-size-paragraph-sm);
  color: var(--theme-text-secondary);
}
.footer a { color: inherit; }

.back-to-top { margin: var(--margin-sm) 0 var(--margin-lg); text-align: center; }

/* ---------- Header ---------- */
.site-header {
  background-color: var(--theme-background);
  margin: 15px 0 25px;
  padding: 15px 0 10px;
  position: relative;
  z-index: 5;
  transition: transform 0.3s cubic-bezier(0.165, 0.84, 0.44, 1), background-color 0.2s;
}

@media (max-width: 991px) {
  .site-header { position: sticky; top: 0; }
  .site-header[data-hidden] { transform: translateY(-5rem); }
}

.bar { display: flex; align-items: center; justify-content: space-between; }

.name {
  display: inline-flex;
  align-items: flex-end;
  font-size: 1.44em;
  font-weight: var(--font-weight-semibold);
  line-height: 1;
  text-decoration: none;
}
.name:hover { color: var(--theme-text); }

/* Dark-mode switch (only shown once JS has set a mode) */
.switch { display: none; position: relative; width: 40px; height: 20px; transform: translateY(-2px); }
:root[data-color-mode] .switch { display: inline-block; }
.switch input { appearance: none; opacity: 0; width: 0; height: 0; margin: 0; }
.switch label {
  position: absolute; inset: 0;
  background-color: var(--color-ivory-light);
  border: 1px solid var(--color-slate-dark);
  border-radius: 2rem;
  cursor: pointer;
}
.switch label::before {
  content: '';
  position: absolute; left: 2px; bottom: 2px;
  width: 14px; height: 14px;
  background-color: var(--color-slate-dark);
  border-radius: 50%;
  transition: transform 0.2s;
}
:root[data-color-mode='dark'] .switch label::before { transform: translateX(20px); }
.switch input:focus-visible + label { outline: 2px solid var(--theme-text); outline-offset: 2px; }

/* ---------- Contents sidebar (posts with sections) ---------- */
.contents-button {
  display: none;
  position: fixed; top: 22px; left: 12px;
  z-index: 6;
  width: 44px; height: 44px;
  align-items: center; justify-content: center;
  padding: 0; border: 0;
  background: transparent; color: inherit;
  cursor: pointer;
}
:root[data-color-mode] .contents-button { display: flex; }
.contents-button:focus-visible { outline: 2px solid var(--theme-text); outline-offset: 2px; }
.contents-button svg {
  width: 24px; height: 24px;
  fill: none; stroke: currentColor; stroke-width: 2px;
  stroke-linecap: round; stroke-linejoin: round;
}
.contents-button .icon-menu { display: none; }
.contents-button[data-open='false'] .icon-close { display: none; }
.contents-button[data-open='false'] .icon-menu { display: block; }

.contents {
  position: fixed; inset: 0 auto 0 0;
  z-index: 4;
  width: 22%; max-width: 23rem;
  padding: 130px 25px 25px;
  overflow: auto;
  scrollbar-width: none;
  background-color: var(--theme-background);
  transition: color 0.2s, background-color 0.2s;
}
.contents::-webkit-scrollbar { display: none; }
.contents[data-open='false'] { display: none; }
.contents[data-open='true'] { display: block; }

@media (min-width: 992px) and (max-width: 1330px) {
  .contents { width: 20%; }
}

@media (max-width: 991px) {
  .contents-button { position: absolute; top: 6px; left: auto; right: calc(10% + 60px); }
  .contents { width: 100%; max-width: none; padding-inline: 0; display: none; }
  body:has(.contents[data-open='true']) { overflow: hidden; }
}
@media (max-width: 479px) {
  .contents-button { right: calc(2.5% + 60px); }
}

.contents-inner { position: relative; }
@media (min-width: 992px) {
  .contents-inner { width: 100%; max-width: none; padding-inline: 0; }
}

.contents-heading {
  font-size: var(--font-size-h3);
  font-weight: var(--font-weight-semibold);
  line-height: var(--line-height-heading);
  margin: 0 0 0.7em;
}
.contents-list { list-style: none; margin-top: 30px; padding-left: 0; }
.contents-link {
  display: inline-block;
  margin-bottom: 0.2em;
  color: var(--theme-text-secondary);
  text-decoration: none;
}
.contents-link:hover { color: var(--theme-text); }
.contents-link[aria-current] { color: var(--theme-text); text-decoration: underline; text-underline-offset: 0.2em; }

@media (max-width: 991px) {
  .contents-link, .contents-link[aria-current] { color: var(--theme-text); text-decoration: none; }
  .contents-link:hover { color: var(--theme-text-link-hover); }
}

/* ---------- Home ---------- */
.lead { font-size: inherit; font-weight: var(--font-weight-regular); line-height: inherit; margin: 0; text-wrap: pretty; }
.intro .lead + .biography { margin-top: 1em; }
.ellipsis, .more { display: none; }
.biography { overflow: hidden; transition: opacity 0.3s; }

@media (max-width: 767px) {
  :root[data-color-mode] .intro:not([data-expanded]) .full-stop { display: none; }
  :root[data-color-mode] .intro:not([data-expanded]) .biography { opacity: 0; height: 0; margin: 0; }
  :root[data-color-mode] .intro:not([data-expanded]) .ellipsis,
  :root[data-color-mode] .intro:not([data-expanded]) .more { display: inline; }
}

.link-section { margin-top: var(--margin-sm); }
.link-section:last-child { margin-bottom: var(--margin-lg); }
.link-heading {
  font-size: var(--font-size-h3);
  font-weight: var(--font-weight-semibold);
  line-height: var(--line-height-heading);
  margin: 0 0 0.7em;
}
.link-list { display: flex; flex-flow: column; gap: 0.8em; list-style: none; margin: 0; padding: 0; }
.link-item { margin: 0; padding-left: 1em; }
.link-item::before { content: '•'; display: inline-block; margin-left: -0.7em; margin-right: 0.3em; }
.link-item .link { margin-right: 0.25em; }
.link-note { color: var(--theme-text-secondary); }

/* ---------- Article ---------- */
.article-title { text-wrap: balance; margin: 0.6em 0 0.1em; }
.article-subtitle { font-size: var(--font-size-paragraph-lg); font-style: italic; margin: 0; }
.article-date { color: var(--theme-text-secondary); font-size: var(--font-size-paragraph-sm); margin: 2em 0 1em; }

.rich-text { text-wrap: pretty; }
.rich-text > :first-child { margin-top: 0; }
.rich-text > :last-child,
.rich-text ol li:last-child,
.rich-text ul li:last-child { margin-bottom: 0; }
.rich-text h2, .rich-text h3, .rich-text h4 { text-wrap: balance; margin-top: 1em; }
.rich-text h2[id] { margin-top: 0; padding-top: 1em; scroll-margin-top: calc(130px - 1em); }
.rich-text ol, .rich-text ul { padding-left: 2em; }
@media (max-width: 767px) {
  .rich-text ol { padding-left: 1.5em; }
  .rich-text ul { padding-left: 1.25em; }
}

.rich-text blockquote {
  border-left: 1px solid var(--theme-border);
  font-size: 1em;
  font-style: italic;
  line-height: 1.5em;
  margin: 2em 0;
  padding: 0.25em 0 0.1em 1.25em;
}
@media (max-width: 991px) { .rich-text blockquote { font-size: 1.25rem; } }
@media (max-width: 767px) { .rich-text blockquote { font-size: 1.1rem; } }
.rich-text blockquote p:last-child { margin-bottom: 0; }

.rich-text hr { border: 0; height: auto; box-sizing: content-box; }
.rich-text hr::after {
  content: '⁂';
  display: block;
  margin: 1em auto;
  font-family: serif;
  font-size: 1.5em;
  font-weight: 200;
  text-align: center;
}

.rich-text img { display: block; margin: 2em auto; }
.rich-text figure { margin: 2em 0; }
.rich-text figcaption { color: var(--theme-text-secondary); font-size: var(--font-size-paragraph-sm); margin-top: 0.5em; }

.rich-text table { border-collapse: collapse; width: 100%; margin: 0 0 1em; font-size: 0.9em; }
.rich-text th, .rich-text td { border-bottom: 1px solid var(--theme-border); padding: 0.4em 0.6em; text-align: left; }

.rich-text code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.85em; }

/* Footnotes */
a[data-footnote-ref] {
  position: relative;
  z-index: 1;
  margin: 0 2px;
  padding: 3px 3px 1px;
  border-radius: 4px;
  background-color: var(--theme-footnote-background);
  font-size: 0.75rem;
  font-weight: var(--font-weight-medium);
  text-decoration: none;
}
a[data-footnote-ref]:hover { background-color: var(--theme-footnote-background-hover); }

.footnote-tooltip {
  visibility: hidden;
  position: absolute;
  top: calc(100% + 0.5em);
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  width: 36vw;
  max-width: 350px;
  padding: 16px 20px;
  background-color: var(--theme-footnote-tooltip-background);
  box-shadow: 0 2px 8px #00000026;
  font-size: 1rem;
  font-weight: var(--font-weight-regular);
  line-height: 1.5;
}
.footnote-tooltip p { margin: 0; }
@media (min-width: 768px) {
  a[data-footnote-ref]:hover + .footnote-tooltip,
  a[data-footnote-ref]:focus-visible + .footnote-tooltip { visibility: visible; }
}

.footnotes { margin: var(--margin-md) 0 var(--margin-sm); }
.rich-text .footnotes h2[id] {
  font-size: var(--font-size-h3);
  font-weight: var(--font-weight-semibold);
  line-height: var(--line-height-heading);
  margin: 0 0 var(--margin-sm);
  padding-top: 0;
}
.footnotes ol { padding-left: 1.5em; font-size: 0.9em; }
.footnotes li { margin-bottom: 1em; scroll-margin-top: 130px; }
.footnotes li p { margin: 0; }
.footnotes a[data-footnote-backref] { margin-left: 0.3em; font-size: 0.75em; text-decoration: none; }
.footnotes a[data-footnote-backref]:hover { text-decoration: underline; }

/* ---------- 404 ---------- */
.not-found h1 { font-size: var(--font-size-h2); font-weight: var(--font-weight-semibold); line-height: var(--line-height-heading); margin: 0 0 0.5em; }
