/* ---- Color system --------------------------------------------------------
   Three surface levels give the page depth:
     --bg        page canvas (slightly tinted, never pure white)
     --surface   cards, header, footer (sits above the canvas)
     --surface-2 soft fills: chips, code, badges
   Theme resolution:
     no attribute        -> follow the OS (prefers-color-scheme)
     [data-theme=light]  -> force light
     [data-theme=dark]   -> force dark
--------------------------------------------------------------------------- */
:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eceff5;
  --fg: #161a22;
  --muted: #5a6472;
  --border: #dfe3ea;
  --border-strong: #cdd3dd;
  --brand: #4f46e5;
  --brand-strong: #4338ca;
  --link: #4f46e5;
  --accent: #ebe9fe;        /* soft brand wash */
  --accent-fg: #4338ca;
  --pin: #b45309;
  --pin-bg: #fdebc8;
  --hero-from: #eef0fe;
  --hero-to: #f4f6fa;
  --radius: 14px;
  --radius-sm: 9px;
  --maxw: 760px;
  --shadow: 0 1px 2px rgba(20, 26, 34, 0.05), 0 4px 16px rgba(20, 26, 34, 0.06);
  --shadow-hover: 0 2px 6px rgba(20, 26, 34, 0.08), 0 12px 28px rgba(79, 70, 229, 0.12);
  color-scheme: light;
}

/* Dark palette, shared by OS-dark (no override) and forced dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d12;
    --surface: #14171f;
    --surface-2: #1d2230;
    --fg: #e7eaf0;
    --muted: #9aa3b4;
    --border: #242a37;
    --border-strong: #323a4a;
    --brand: #8b9dff;
    --brand-strong: #a9b6ff;
    --link: #93a8ff;
    --accent: #1c2236;
    --accent-fg: #b9c4ff;
    --pin: #fbbf24;
    --pin-bg: #2e2710;
    --hero-from: #181d2e;
    --hero-to: #0b0d12;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 6px 20px rgba(0, 0, 0, 0.45);
    --shadow-hover: 0 2px 6px rgba(0, 0, 0, 0.55), 0 14px 32px rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d12;
  --surface: #14171f;
  --surface-2: #1d2230;
  --fg: #e7eaf0;
  --muted: #9aa3b4;
  --border: #242a37;
  --border-strong: #323a4a;
  --brand: #8b9dff;
  --brand-strong: #a9b6ff;
  --link: #93a8ff;
  --accent: #1c2236;
  --accent-fg: #b9c4ff;
  --pin: #fbbf24;
  --pin-bg: #2e2710;
  --hero-from: #181d2e;
  --hero-to: #0b0d12;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 6px 20px rgba(0, 0, 0, 0.45);
  --shadow-hover: 0 2px 6px rgba(0, 0, 0, 0.55), 0 14px 32px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

/* ---- Base ---------------------------------------------------------------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: 5rem; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.7;
  transition: background 0.2s ease, color 0.2s ease;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.page-title { margin: 0.4rem 0 0.6rem; letter-spacing: -0.01em; }

/* ---- Header -------------------------------------------------------------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.site-header-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0.8rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.site-title {
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  background: linear-gradient(120deg, var(--brand), var(--fg));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.site-nav { display: flex; align-items: center; gap: 0.3rem; }
.site-nav a {
  color: var(--muted);
  font-size: 0.92rem;
  padding: 0.32rem 0.72rem;
  border-radius: 999px;
  transition: background 0.15s, color 0.15s;
}
.site-nav a:hover { color: var(--fg); background: var(--surface-2); text-decoration: none; }
.site-nav a.active { color: var(--accent-fg); background: var(--accent); font-weight: 600; }

.theme-toggle {
  margin-left: 0.25rem;
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--fg);
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
}
.theme-toggle:hover { border-color: var(--brand); transform: rotate(-15deg); }

/* ---- Layout -------------------------------------------------------------- */
.content { max-width: var(--maxw); margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }
.site-footer { border-top: 1px solid var(--border); color: var(--muted); font-size: 0.85rem; background: var(--surface); }
.site-footer .site-header-inner { justify-content: flex-start; }

/* ---- Hero ---------------------------------------------------------------- */
.hero {
  position: relative;
  padding: 1.6rem 1.5rem;
  margin-bottom: 1.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(140deg, var(--hero-from), var(--hero-to));
  box-shadow: var(--shadow);
  overflow: hidden;
}
.hero::before {
  content: "";
  position: absolute;
  inset: 0 0 auto auto;
  width: 220px; height: 220px;
  background: radial-gradient(circle at 70% 30%, color-mix(in srgb, var(--brand) 28%, transparent), transparent 70%);
  pointer-events: none;
}
.hero-title { font-size: 2rem; margin: 0 0 0.3rem; letter-spacing: -0.02em; }
.hero-desc { color: var(--muted); margin: 0; font-size: 1.05rem; max-width: 46ch; }

/* ---- Search -------------------------------------------------------------- */
.search-bar { margin: 0 0 1.5rem; }
#search {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}
#search::placeholder { color: var(--muted); }
#search:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 4px var(--accent); }

/* ---- Feed of cards ------------------------------------------------------- */
.feed { display: flex; flex-direction: column; gap: 1rem; }
.card {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}
.card:hover { border-color: color-mix(in srgb, var(--brand) 50%, var(--border)); transform: translateY(-2px); box-shadow: var(--shadow-hover); }
.card-pinned {
  border-color: color-mix(in srgb, var(--pin) 45%, var(--border));
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--pin-bg) 55%, var(--surface)), var(--surface));
}
.card-pinned::before {
  content: "";
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 4px; border-radius: var(--radius) 0 0 var(--radius);
  background: linear-gradient(var(--pin), color-mix(in srgb, var(--pin) 40%, transparent));
}
.card-top { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.4rem; flex-wrap: wrap; }
.card-cat { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 600; }
.card-cat:hover { color: var(--brand); }
.card-title { font-size: 1.25rem; margin: 0 0 0.35rem; line-height: 1.3; }
.card-title a { color: var(--fg); }
.card-title a:hover { color: var(--brand); text-decoration: none; }
.card-excerpt { margin: 0 0 0.7rem; color: var(--muted); }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; flex-wrap: wrap; }
.card-tags { display: flex; flex-wrap: wrap; gap: 0.4rem; }

.pin {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--pin);
  background: var(--pin-bg);
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  letter-spacing: 0.02em;
}
.pin-dot { font-size: 0.8em; }

/* ---- Tags / chips -------------------------------------------------------- */
.tag {
  display: inline-flex;
  align-items: center;
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--accent-fg);
  background: var(--accent);
  border: 1px solid color-mix(in srgb, var(--brand) 22%, var(--border));
  padding: 0.12rem 0.5rem;
  border-radius: 7px;
  transition: background 0.15s, border-color 0.15s, transform 0.15s, color 0.15s;
}
.tag:hover {
  text-decoration: none;
  color: #fff;
  background: var(--brand);
  border-color: var(--brand);
  transform: translateY(-1px);
}
.post-meta { color: var(--muted); font-size: 0.82rem; }

.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.32rem 0.72rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.88rem;
  color: var(--fg);
  box-shadow: var(--shadow);
  transition: border-color 0.15s, color 0.15s, transform 0.15s;
}
.chip span { color: var(--muted); font-size: 0.78rem; }
.chip:hover { text-decoration: none; border-color: var(--brand); color: var(--brand); transform: translateY(-1px); }

/* ---- Contents (category tree) ------------------------------------------- */
#toc { margin-top: 1rem; }
.cat { margin: 1.25rem 0; }
.cat-d0 { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.4rem 1.1rem 0.6rem; box-shadow: var(--shadow); }
.cat-d0 > .cat-title { border-bottom: 2px solid var(--border); padding-bottom: 0.4rem; }
.cat-title { margin: 1.1rem 0 0.5rem; display: flex; align-items: baseline; gap: 0.5rem; }
.cat-count { font-size: 0.7rem; font-weight: 700; color: var(--accent-fg); background: var(--accent); border-radius: 999px; padding: 0.05rem 0.5rem; }
.cat .cat:not(.cat-d0) { margin-left: 0.5rem; border-left: 2px solid var(--border); padding-left: 1rem; }
.post-list { list-style: none; padding: 0; margin: 0; }
.post-item { padding: 0.6rem 0; border-bottom: 1px solid var(--border); }
.post-item:last-child { border-bottom: none; }
.post-link { font-size: 1.02rem; font-weight: 600; color: var(--fg); }
.post-link:hover { color: var(--brand); }
.post-excerpt { margin: 0.2rem 0 0.3rem; color: var(--muted); font-size: 0.92rem; }

/* ---- Single post --------------------------------------------------------- */
.crumbs { font-size: 0.85rem; color: var(--muted); margin-bottom: 1rem; }
.crumbs span { margin: 0 0.15rem; }
.post-title { margin: 0.2rem 0 0.5rem; line-height: 1.2; font-size: 2rem; letter-spacing: -0.02em; }
.post-tags { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.5rem 0 0; }
.post-lead { color: var(--muted); font-style: italic; margin: 1rem 0 1.5rem; padding-left: 1rem; border-left: 3px solid var(--brand); }
.post-body { margin-top: 1.5rem; }
.post-body img { max-width: 100%; height: auto; border-radius: 8px; }
.post-body pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  overflow-x: auto;
  font-size: 0.9rem;
}
.post-body code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; }
.post-body :not(pre) > code { background: var(--surface-2); padding: 0.1rem 0.35rem; border-radius: 5px; }
.post-body blockquote { margin: 1rem 0; padding: 0.3rem 1rem; border-left: 3px solid var(--border-strong); color: var(--muted); }
.post-body table { border-collapse: collapse; width: 100%; margin: 1rem 0; display: block; overflow-x: auto; }
.post-body th, .post-body td { border: 1px solid var(--border); padding: 0.4rem 0.7rem; }
.post-body th { background: var(--surface-2); }
.post-body h1, .post-body h2, .post-body h3 { line-height: 1.3; margin-top: 2rem; }
.post-body hr { border: none; border-top: 1px solid var(--border); margin: 2rem 0; }
mjx-container { overflow-x: auto; overflow-y: hidden; max-width: 100%; }

/* ---- Footnotes / citations ---------------------------------------------- */
/* Inline citation marker, e.g. text[^1] -> superscript [1] link. */
.post-body sup [data-footnote-ref] {
  display: inline-block;
  font-size: 0.78em;
  font-weight: 700;
  color: var(--accent-fg);
  background: var(--accent);
  border-radius: 5px;
  padding: 0 0.28em;
  margin: 0 0.05em;
  text-decoration: none;
  line-height: 1.2;
}
.post-body sup [data-footnote-ref]:hover { background: var(--brand); color: #fff; }
.post-body [data-footnote-ref]:target,
.post-body li:target { background: var(--accent); border-radius: 6px; }

/* The references list at the bottom of a post. */
.footnotes {
  margin-top: 2.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
  color: var(--muted);
}
.footnotes h2.sr-only {
  position: static;
  width: auto;
  height: auto;
  clip: auto;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 0.5rem;
}
.footnotes ol { margin: 0; padding-left: 1.4rem; }
.footnotes li { margin: 0.35rem 0; padding-left: 0.2rem; }
.footnotes li p { margin: 0.2rem 0; }
.footnotes a[data-footnote-backref] { text-decoration: none; margin-left: 0.25rem; opacity: 0.7; }
.footnotes a[data-footnote-backref]:hover { opacity: 1; }
/* Keep any other sr-only content hidden from sighted users. */
.sr-only:not(.footnotes *) {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (max-width: 600px) {
  body { font-size: 16px; }
  .hero-title, .post-title { font-size: 1.6rem; }
  .hero { padding: 1.3rem 1.1rem; }
}
