@layer base, typography, layout, components;

@layer base {
  :root {
    color-scheme: dark;
    --background: #111111;
    --surface: #1b1b1b;
    --text: #e0e0e0;
    --muted: #999999;
    --accent: #c4c4c4;
    --border: #333333;
    --code-background: #1a1a1a;
    --code-border: #303030;
    --code-text: #dedede;
    --code-comment: #969696;
    --code-string: #b5c68b;
    --code-keyword: #f29448;
    --code-number: #ddab73;
    --code-type: #7fb8cd;
    --code-function: #dedede;
    --scrollbar-thumb: #666666;
    --scrollbar-thumb-hover: #8a8a8a;
    --font-body: Georgia, 'Times New Roman', serif;
    --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-code: 'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
    --reading-width: 680px;
  }
  * { box-sizing: border-box; }
  html { scroll-behavior: smooth; scroll-padding-top: 32px; }
  body { margin: 0; padding-bottom: 64px; background: var(--background); color: var(--text); font-family: var(--font-body); -webkit-font-smoothing: antialiased; }
  a { color: inherit; text-decoration-thickness: 1px; text-underline-offset: .22em; }
  a:hover { color: var(--accent); }
  a:focus-visible, button:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 5px; }
  ::selection { background: var(--accent); color: var(--background); }
  img, svg, video { max-width: 100%; }
  img { height: auto; border-radius: 4px; }
  button, input, summary { font: inherit; }
  button { cursor: pointer; }
  [hidden] { display: none !important; }
  @media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
}

@layer components {
  :where(.prose, .prose *, .contents-rail nav) {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) transparent;
  }
  :where(.prose, .prose *, .contents-rail nav):where(:hover, :focus-within) {
    scrollbar-color: var(--scrollbar-thumb-hover) transparent;
  }
  /* Use an exact, rounded 6px scrollbar where the native pseudo-elements exist. */
  @supports selector(::-webkit-scrollbar) {
    @media (forced-colors: none) {
      :where(.prose, .prose *, .contents-rail nav) {
        scrollbar-width: auto;
        scrollbar-color: auto;
      }
      :where(.prose, .prose *, .contents-rail nav)::-webkit-scrollbar {
        width: 6px;
        height: 6px;
      }
      :where(.prose, .prose *, .contents-rail nav)::-webkit-scrollbar-track,
      :where(.prose, .prose *, .contents-rail nav)::-webkit-scrollbar-corner {
        background: transparent;
      }
      :where(.prose, .prose *, .contents-rail nav)::-webkit-scrollbar-thumb {
        background: var(--scrollbar-thumb);
        border-radius: 999px;
      }
      :where(.prose, .prose *, .contents-rail nav):where(:hover, :focus-within)::-webkit-scrollbar-thumb {
        background: var(--scrollbar-thumb-hover);
      }
      :where(.prose, .prose *, .contents-rail nav)::-webkit-scrollbar-button {
        display: none;
      }
    }
  }
  @media (forced-colors: active) {
    :where(.prose, .prose *, .contents-rail nav) {
      scrollbar-width: auto;
      scrollbar-color: auto;
    }
  }
}

@layer typography {
  h1, h2, h3, h4 { font-weight: 400; line-height: 1.16; text-wrap: balance; }
  h1 { font-size: clamp(33px, 3.9vw, 52px); letter-spacing: -.035em; }
  .prose { font-size: 17.5px; line-height: 1.55; overflow-wrap: break-word; }
  .article .prose { font-size: 17px; }
  .prose p { margin: .45em 0; }
  .prose h2 { font-size: 27px; letter-spacing: -.025em; margin: 1.45em 0 .35em; }
  .prose h3 { font-size: 23px; margin: 1.2em 0 .35em; }
  .prose h4 { font-size: 20px; margin: 1.15em 0 .35em; }
  .prose a { text-decoration-color: var(--muted); }
  .prose li { padding-left: .2em; margin-block: .2em; }
  .prose ul, .prose ol { padding-left: 1.5em; margin-block: .5em; }
  .prose li::marker { color: var(--muted); font-variant-numeric: tabular-nums; }
  .prose li > :is(ul, ol) { margin-block: .25em; }
  .prose li > p { margin-block: .3em; }
  .prose code { font: .78em/1.6 var(--font-code); }
  .prose :not(pre) > code { font-size: .8em; line-height: inherit; background: var(--surface); padding: .1em .28em; border-radius: 3px; overflow-wrap: anywhere; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
  .prose pre { max-width: 100%; overflow-x: auto; border: 1px solid var(--code-border); padding: 16px 18px; background: var(--code-background); color: var(--code-text); font: 13.25px/1.45 var(--font-code); margin: .85em 0; tab-size: 4; }
  .prose pre code { display: block; font: inherit; }
  .syntax-highlighting .comment { color: var(--code-comment); }
  .syntax-highlighting .string { color: var(--code-string); }
  .syntax-highlighting :is(.keyword, .attribute) { color: var(--code-keyword); }
  .syntax-highlighting :is(.constant, .number) { color: var(--code-number); }
  .syntax-highlighting .type { color: var(--code-type); }
  .syntax-highlighting .function { color: var(--code-function); }
  .prose blockquote { color: var(--muted); border-left: 2px solid var(--border); padding-left: 18px; margin: .85em 0; }
  .prose blockquote > :first-child { margin-top: 0; }
  .prose blockquote > :last-child { margin-bottom: 0; }
  .prose blockquote cite { display: block; margin-top: .6em; font-size: .85em; }
  .prose figure { margin-block: 24px; }
  .prose figcaption { color: var(--muted); font-size: 14px; line-height: 1.6; margin-top: 14px; }
  .prose table { display: block; width: 100%; max-width: 100%; overflow-x: auto; border-collapse: collapse; border-block: 1px solid var(--border); font: 15px/1.5 var(--font-ui); font-variant-numeric: tabular-nums; margin: .85em 0; }
  .prose th, .prose td { border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; padding: 9px 18px 9px 0; }
  .prose th:last-child, .prose td:last-child { padding-right: 0; }
  .prose th { font-weight: 600; color: var(--text); }
  .prose tbody tr:last-child td { border-bottom: 0; }
  .prose caption { text-align: left; color: var(--muted); font-size: 14px; padding-bottom: 10px; }
  .prose hr { width: 100%; border: 0; border-top: 1px solid var(--border); margin-block: 32px; }
  .heading-anchor { opacity: 0; font-size: .6em; padding: .4em; text-decoration: none; color: var(--muted); }
  :is(h2,h3,h4):hover .heading-anchor, .heading-anchor:focus-visible { opacity: 1; }
  .footnotes { font-size: 14px; line-height: 1.5; margin-top: 30px; border-top: 1px solid var(--border); padding-top: 14px; color: var(--muted); }
  .footnotes ol { margin-block: 0; }
  .footnotes li { margin-block: .5em; padding-left: .3em; }
  .footnotes p { margin-block: .25em; }
  .footnote-ref { font: .75em/0 var(--font-code); vertical-align: super; }
  .footnote-ref a { color: var(--accent); padding-inline: .15em; text-decoration: none; }
  .footnote-backref { color: var(--muted); padding: .25em; text-decoration: none; white-space: nowrap; }
  .footnote-backref:hover { color: var(--text); }
  .markdown-alert { border-left: 2px solid var(--accent); padding: 4px 22px; margin: 20px 0; color: var(--muted); }
  .markdown-alert-title { color: var(--accent); font: 13px var(--font-ui); }
  .markdown-alert-title svg { width: 14px; height: 14px; fill: currentColor; margin-right: 8px; }
}

@layer layout {
  .site-header { margin-inline: auto; width: calc(100% - 96px); max-width: 1360px; display: flex; align-items: center; justify-content: space-between; }
  .site-header { padding-block: 24px; }
  .wordmark { font-size: 22px; letter-spacing: -.025em; text-decoration: none; }
  .site-header nav { display: flex; gap: 30px; font: 15px var(--font-body); }
  .site-header nav a { text-decoration: none; }
  .home { max-width: 780px; padding-inline: 20px; margin: 24px auto 0; min-height: calc(100vh - 320px); }
  .home-intro h1 { margin: 0; font-size: clamp(31px, 3.5vw, 39px); line-height: 1.1; }
  .fractal-banner { position: relative; height: clamp(220px, 45vw, 410px); margin-bottom: 28px; }
  .fractal-art { position: absolute; inset: 0; isolation: isolate; }
  .fractal-layer { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; border-radius: 0; pointer-events: none; }
  .fractal-cool { opacity: 1; }
  .fractal-warm { opacity: 0; }
  .fractal-banner.is-animated .fractal-warm { animation: fractal-warm-cycle 16s cubic-bezier(.4, 0, .6, 1) infinite; will-change: opacity; }
  .fractal-banner.is-animated.is-paused .fractal-layer { animation-play-state: paused; }
  .fractal-toggle { position: absolute; right: 0; bottom: 0; width: 44px; height: 44px; display: grid; place-items: center; border: 0; padding: 0; background: transparent; color: var(--muted); opacity: 0; pointer-events: none; transition: opacity 160ms ease; }
  .fractal-banner.controls-visible .fractal-toggle, .fractal-banner .fractal-toggle:focus-visible { opacity: .65; pointer-events: auto; }
  .fractal-banner .fractal-toggle:focus-visible { opacity: 1; color: var(--text); }
  @media (hover: hover) {
    .fractal-banner:hover .fractal-toggle { opacity: .65; pointer-events: auto; }
    .fractal-banner .fractal-toggle:hover { opacity: 1; color: var(--text); }
  }
  .fractal-toggle svg { display: block; fill: currentColor; }
  .fractal-play-icon, .fractal-toggle[aria-pressed="true"] .fractal-pause-icon { display: none; }
  .fractal-toggle[aria-pressed="true"] .fractal-play-icon { display: block; }
  @keyframes fractal-warm-cycle { 0%, 6.25%, 87.5%, 100% { opacity: 0; } 37.5%, 56.25% { opacity: 1; } }
  @keyframes fractal-cool-cycle { 0%, 6.25%, 87.5%, 100% { opacity: 1; } 37.5%, 56.25% { opacity: 0; } }
  @supports (mix-blend-mode: plus-lighter) {
    .fractal-layer { mix-blend-mode: plus-lighter; }
    .fractal-banner.is-animated .fractal-cool { animation: fractal-cool-cycle 16s cubic-bezier(.4, 0, .6, 1) infinite; will-change: opacity; }
  }
  @media (prefers-reduced-motion: reduce) {
    .fractal-banner.is-animated .fractal-layer { animation: none; will-change: auto; }
    .fractal-banner .fractal-cool { opacity: 1; }
    .fractal-banner .fractal-warm { opacity: 0; }
    .fractal-toggle { display: none; }
  }
  .home .writing { margin-top: 36px; }
  .writing { margin-top: 52px; }
  .writing h2 { border-bottom: 1px solid var(--border); padding-bottom: 18px; margin: 0 0 22px; font-size: 27px; letter-spacing: -.025em; }
  .writing-row { display: grid; grid-template-columns: 65px minmax(0,1fr); gap: 25px; }
  .writing-year { font-size: 18px; color: var(--accent); padding-top: 4px; }
  .writing-entry { padding-bottom: 20px; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
  .writing-row:last-child .writing-entry { border: none; margin: 0; }
  .writing-title { display: flex; flex-wrap: wrap; gap: 10px 15px; align-items: baseline; }
  .writing-title h3 { width: 100%; }
  .writing-title h3 { font-size: 22px; letter-spacing: -.015em; margin: 0; }
  .writing-title h3 a { text-decoration: none; }
  .writing-title time { margin-left: 0; flex-shrink: 0; font-size: 15px; color: var(--muted); }
  .writing-entry p { margin: 10px 0 0; color: var(--muted); font-size: 16.5px; line-height: 1.5; }
  .article-layout { width: calc(100% - 80px); max-width: 1280px; margin: 40px auto 0; position: relative; }
  .article { width: 100%; max-width: 860px; margin-inline: auto; }
  .article-header { max-width: 800px; margin: 0 auto 24px; text-align: center; }
  .article-header h1 { font-size: clamp(33px, 3vw, 41px); font-weight: 400; letter-spacing: -.025em; }
  .article-author { font-size: 16.5px; margin: 0 0 8px; }
  .article-header h1 { margin: 0 0 16px; }
  .article-meta { justify-content: center; display: flex; gap: 20px; align-items: baseline; color: var(--muted); font-size: 15px; }
  .draft-label { font-style: italic; color: var(--accent); font-size: 15px; white-space: nowrap; }
  .contents-rail { position: absolute; top: 250px; bottom: 0; left: 0; width: 150px; }
  .contents-rail nav { position: sticky; top: 48px; max-height: calc(100vh - 96px); overflow-y: auto; }
  .contents-rail ol, .mobile-contents ol { list-style: none; padding: 0; margin: 0; }
  .contents-rail li { margin: 0 0 13px; }
  .contents-rail a { font-size: 14px; line-height: 1.4; color: var(--muted); text-decoration: none; display: block; }
  .contents-rail a:hover, .contents-rail a[aria-current] { color: var(--accent); }
  .contents-rail .subsection { padding-left: 12px; }
  .mobile-contents { display: none; }
  .prose { display: grid; grid-template-columns: [wide-start] minmax(0,1fr) [text-start] minmax(0,var(--reading-width)) [text-end] minmax(0,1fr) [wide-end]; }
  .prose > * { grid-column: text; min-width: 0; }
  .prose > :is(.wide, figure), .prose > p:has(picture), .prose > p:has(> img:only-child) { grid-column: wide; }
  .prose > p:has(picture) { margin-block: 22px; text-align: center; }
  .prose picture { display: block; }
  .prose picture img { display: block; margin: auto; max-height: none; }
  .prose > :first-child { margin-top: 0; }
  .article-end { max-width: var(--reading-width); margin: 44px auto 0; padding-top: 20px; border-top: 1px solid var(--border); font-size: 16px; }
  .article-end a { color: var(--muted); }
  .standalone { max-width: 720px; margin: 64px auto 0; padding-inline: 20px; min-height: 50vh; }
  .standalone h1 { margin-bottom: 36px; }
  .social-links { display: flex; align-items: center; gap: 8px; margin-top: 20px; }
  .social-links a { display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; opacity: .75; }
  .social-links a:hover, .social-links a:focus-visible { opacity: 1; }
  .social-links img { display: block; border-radius: 0; filter: brightness(0) invert(1); }
  .skip-link { position: fixed; top: -100px; left: 20px; z-index: 20; background: var(--background); padding: 12px; }
  .skip-link:focus { top: 12px; }
  @media (max-width: 1200px) {
    .contents-rail { display: none; }
    .article { width: 100%; max-width: 900px; margin-inline: auto; }
    .mobile-contents { display: block; max-width: var(--reading-width); margin: -8px auto 24px; border-block: 1px solid var(--border); padding-block: 10px; font: 14px/1.7 var(--font-ui); color: var(--muted); }
    .mobile-contents summary { cursor: pointer; padding-block: 4px; }
    .mobile-contents nav { padding: 14px 0 6px; }
    .mobile-contents li { margin-bottom: 8px; }
    .mobile-contents .subsection { padding-left: 16px; }
  }
  @media (max-width: 760px) {
    body { padding-bottom: 40px; }
    .site-header { width: calc(100% - 40px); }
    .site-header { padding-block: 24px; }
    .wordmark { font-size: 21px; }
    .site-header nav { gap: 20px; font-size: 13px; }
    .home { margin-top: 8px; min-height: calc(100vh - 270px); }
    .home-intro h1 { max-width: 580px; }
    .fractal-banner { width: calc(100% + 40px); margin-inline: -20px; margin-bottom: 20px; }
    .home .writing { margin-top: 28px; }
    .writing { margin-top: 38px; }
    .writing h2 { font-size: 27px; margin-bottom: 20px; }
    .writing-row { grid-template-columns: 1fr; gap: 12px; }
    .writing-row > span:empty { display: none; }
    .writing-year { font-size: 15px; }
    .writing-title { flex-wrap: wrap; gap: 8px 12px; }
    .writing-title h3 { width: 100%; font-size: 22px; line-height: 1.3; }
    .writing-title time { margin-left: 0; font-size: 14px; }
    .writing-title .draft-label { order: 1; font-size: 14px; }
    .writing-entry p { font-size: 15.5px; }
    .article-layout { width: calc(100% - 40px); margin-top: 30px; }
    .article-header { margin-bottom: 24px; }
    .article-header h1 { font-size: clamp(31px, 6.3vw, 44px); line-height: 1.14; }
    .article-meta { font-size: 14px; }
    .prose { font-size: 16.5px; line-height: 1.55; }
    .article .prose { font-size: 16px; }
    .prose h2 { font-size: 26px; margin-top: 1.3em; }
    .prose h3 { font-size: 22px; }
    .prose pre { padding: 14px; font-size: 12.75px; }
    .prose table { font-size: 14px; }
    .prose th, .prose td { padding-right: 14px; }
    .prose > p:has(picture) { margin-block: 18px; }
    .standalone { margin-top: 40px; }
  }
}
