/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Colors — "paper & ink": night is default, light is the day/paper theme */
:root {
  --bg: #201f1d;
  --fg: #e6e4df;
  --muted: #97948b;
  --faint: #64615a;
  --link: #e6e4df;
  --codebg: #171614;
  --codefg: #e6e4df;
  --codeborder: #383632;
  --border: #383632;
  --grain-opacity: 0.35;
  --mono: "SF Mono", "Fira Mono", ui-monospace, Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", Charter, "Bitstream Charter", Georgia, serif;
  --reading: clamp(1.05rem, 0.98rem + 0.3vw, 1.17rem);
}

/* Light mode override (day / paper) */
:root[data-theme="light"] {
  --bg: #faf9f6;
  --fg: #353431;
  --muted: #87847b;
  --faint: #b5b2a8;
  --link: #353431;
  --codebg: #21201e;
  --codefg: #e6e4df;
  --codeborder: #21201e;
  --border: #e3e0d8;
  --grain-opacity: 0.5;
}

/* Base */
html { font-size: 16px; }
body {
  font-family: var(--serif);
  background:
    radial-gradient(ellipse 90% 120% at 50% 35%, rgba(0,0,0,0) 55%, rgba(0,0,0,0.05) 100%),
    var(--bg);
  color: var(--fg);
  line-height: 1.65;
  max-width: 680px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 4rem;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background 0.45s ease, color 0.45s ease;
}

/* Film grain over everything */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: calc(var(--grain-opacity) * 0.09);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* Links: underlined ink, not blue */
a { color: var(--link); text-decoration: underline; text-decoration-color: var(--faint); text-underline-offset: 3px; }
a:hover { color: var(--fg); text-decoration-color: var(--fg); }

/* Header — mono chrome */
.site-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  font-family: var(--mono);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 4rem;
}
.site-title {
  font-weight: 400;
  color: var(--fg);
  text-decoration: none;
}
.site-title:hover { text-decoration: underline; text-decoration-color: var(--faint); }
.site-nav { display: flex; gap: 1.4rem; align-items: baseline; }
.site-nav a {
  color: var(--muted);
  text-decoration: none;
}
.site-nav a:hover { color: var(--fg); text-decoration: none; }
.site-nav a.is-active {
  color: var(--fg);
  text-decoration: underline;
  text-decoration-color: var(--faint);
  text-underline-offset: 4px;
}

/* Arrival: one soft rise on page load */
@media (prefers-reduced-motion: no-preference) {
  main {
    animation: arrive 0.45s cubic-bezier(0.2, 0.6, 0.2, 1);
  }
  @keyframes arrive {
    from { opacity: 0; transform: translateY(8px); }
  }
}

/* Theme toggle */
.theme-toggle {
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}
.theme-toggle:hover { color: var(--fg); }

/* Footer */
.site-footer {
  margin-top: 5rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.footer-links { display: flex; gap: 1.1rem; }
.footer-links a { color: var(--muted); text-decoration: none; }
.footer-links a:hover { color: var(--fg); }

/* Email popover */
.email-wrap { position: relative; }
.email-toggle, .email-copy {
  font: inherit;
  letter-spacing: inherit;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}
.email-toggle:hover, .email-copy:hover { color: var(--fg); }
.email-pop {
  position: absolute;
  bottom: 1.8rem;
  right: 0;
  display: flex;
  align-items: baseline;
  gap: 0.8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.55rem 0.8rem;
  white-space: nowrap;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
  z-index: 10;
}
.email-pop[hidden] { display: none; }
.email-addr { color: var(--fg); user-select: all; }
.email-copy { text-decoration: underline; text-decoration-color: var(--faint); text-underline-offset: 3px; }

/* Home: intro + feed */
.home-intro {
  font-size: clamp(1.15rem, 1.02rem + 0.6vw, 1.4rem);
  line-height: 1.5;
  max-width: 46ch;
  margin-bottom: 3.5rem;
}
.section-label {
  display: flex;
  align-items: baseline;
  gap: 0.8rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 400;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 1.2rem;
}
.section-label::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--border);
  transform: translateY(-0.2em);
}

/* Post list (home) */
.post-list { list-style: none; }
.post-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.45rem 0;
}
.post-list a { text-decoration: none; font-size: 1.02rem; }
.post-list a:hover { text-decoration: underline; text-decoration-color: var(--faint); }
.post-list .post-date {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-left: 1rem;
}

/* Page/article header */
.post-header {
  margin-bottom: 2.4rem;
  padding-bottom: 1.2rem;
  border-bottom: 1px solid var(--border);
}
.post-title {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 0.5rem;
}
.post-meta {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
}
.post-tags { margin-top: 0.3rem; }
.post-tags a {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.72rem;
  margin-right: 0.5rem;
  text-decoration: none;
}
.post-tags a::before { content: "#"; }

/* Article layout with sidebar ToC */
.article-layout {
  display: flex;
  gap: 3rem;
}
.article-layout .content {
  flex: 1;
  min-width: 0;
}
.toc-sidebar {
  width: 280px;
  flex-shrink: 0;
}
.toc {
  position: sticky;
  top: 2rem;
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  scrollbar-width: none;
}
.toc::-webkit-scrollbar { display: none; }
.toc h2 {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.6rem;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.14em;
}
.toc ul {
  list-style: none;
  padding-left: 0;
}
.toc ul ul {
  padding-left: 0.8rem;
}
.toc li {
  margin-bottom: 0.35rem;
}
.toc a {
  font-family: var(--mono);
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.55;
  text-decoration: none;
}
.toc a:hover {
  color: var(--fg);
}

/* Content — serif reading text */
.content { font-size: var(--reading); }
.content h1 {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 1rem;
}
.content h2 {
  font-size: 1.35rem;
  margin-top: 2.6rem;
  margin-bottom: 0.9rem;
  font-weight: 700;
}
.content h3 {
  font-size: 1.12rem;
  margin-top: 2rem;
  margin-bottom: 0.6rem;
  font-weight: 700;
}
.content p { margin-bottom: 1.15rem; }
.content ul, .content ol {
  margin-bottom: 1.15rem;
  padding-left: 1.4rem;
}
.content li { margin-bottom: 0.35rem; }
.content li::marker { color: var(--faint); }
.content blockquote {
  border-left: 2px solid var(--faint);
  padding-left: 1.1rem;
  font-style: italic;
  color: var(--muted);
  margin-bottom: 1.15rem;
}
.content img {
  max-width: 100%;
  height: auto;
  border-radius: 3px;
  margin: 1.2rem 0;
}
.content strong { font-weight: 700; }
ol.footnotes {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  padding-left: 1.5rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.55;
}
ol.footnotes a { color: var(--fg); }
.closing {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
figure { margin: 1.4rem 0; }
figure img { max-width: 100%; height: auto; border-radius: 3px; }
figure img.invertible { filter: none; }
:root:not([data-theme="light"]) figure img.invertible { filter: invert(1) hue-rotate(180deg); }
figcaption {
  font-family: var(--mono);
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  margin-top: 0.5rem;
  line-height: 1.5;
}

/* Code — dark ink plates in both themes */
code {
  font-family: var(--mono);
  font-size: 0.82em;
  background: color-mix(in srgb, var(--fg) 8%, transparent);
  padding: 0.12em 0.35em;
  border-radius: 3px;
}
pre {
  background: var(--codebg);
  color: var(--codefg);
  border: 1px solid var(--codeborder);
  border-radius: 4px;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  margin-bottom: 1.3rem;
  line-height: 1.55;
}
pre code {
  background: none;
  padding: 0;
  font-size: 0.78em;
  color: inherit;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.3rem;
  font-size: 0.88em;
}
th, td {
  padding: 0.45rem 0.7rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
th {
  font-family: var(--mono);
  font-weight: 400;
  color: var(--muted);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* Archive page */
.archive-year {
  font-family: var(--mono);
  font-size: 0.85rem;
  font-weight: 400;
  margin-top: 2rem;
  margin-bottom: 0.5rem;
  color: var(--muted);
}

/* Tags page */
.tag-list { list-style: none; }
.tag-list li { display: inline-block; margin: 0.3rem 0.5rem 0.3rem 0; }
.tag-list a {
  font-family: var(--mono);
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: none;
}
.tag-list a::before { content: "#"; }

/* About page */
.about-content h2 { margin-top: 2rem; }

/* HN mode: ?hn=1 strips chrome */
body.hn-mode .site-header,
body.hn-mode .site-footer { display: none; }
body.hn-mode { max-width: 720px; padding-top: 1rem; }

/* Widen body when sidebar ToC is present */
body:has(.article-layout) {
  max-width: 1140px;
}

/* Mobile ToC toggle */
.toc-toggle, .toc-overlay { display: none; }

/* Responsive */
@media (max-width: 900px) {
  body:has(.article-layout) { max-width: 680px; }
  .article-layout {
    flex-direction: column;
    gap: 0;
  }
  .toc-toggle {
    display: block;
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1001;
    background: none;
    border: none;
    color: var(--muted);
    font-size: 1.8rem;
    padding: 0;
    cursor: pointer;
    line-height: 1;
    opacity: 0.6;
  }
  .toc-toggle:hover { opacity: 1; }
  .toc-sidebar {
    position: fixed;
    top: 0;
    right: 0;
    width: 240px;
    height: 100vh;
    background: var(--bg);
    border-left: 1px solid var(--border);
    padding: 3.5rem 1.5rem 2rem;
    z-index: 1000;
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }
  .toc-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }
  .toc-open .toc-sidebar { transform: translateX(0); }
  .toc-open .toc-overlay { opacity: 1; pointer-events: auto; }
  .toc-open .toc-toggle { opacity: 1; }
  .toc {
    position: static;
    max-height: none;
  }
}
@media (max-width: 600px) {
  body { padding: 1.6rem 1.1rem 3rem; }
  .site-header { flex-direction: column; gap: 0.6rem; margin-bottom: 3rem; }
  .post-title { font-size: 1.3rem; }
  .post-list li { flex-direction: column; gap: 0.1rem; }
  .post-list .post-date { margin-left: 0; }
}
