/* ========================================
   CSS VARIABLES & THEME CONFIGURATION
   ======================================== */

:root {
  color-scheme: light;
  --background-color: #faf8f4;
  --text-color: #201e1a;
  /* 5.0:1 on the background, so dates and asides pass WCAG AA. */
  --text-secondary: #6f6a60;
  --border-color: #e7e2d9;
  --code-background-color: #efece4;
  --accent-color: #c0362a;
  --link-underline: #c0362a;
  --bg-gray-focus: rgba(0, 0, 0, 0.06);
  --selection-color: rgba(192, 54, 42, 0.18);
  --diagram-infra: #e6ecf3;
  --diagram-user: #fbf1cf;
  /* Shared monospace stack for code blocks and inline code. ui-monospace
     resolves to the platform's UI monospace (SF Mono on macOS). */
  --mono-font:
    ui-monospace, 'SF Mono', SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
}

[data-theme='dark'] {
  color-scheme: dark;
  --background-color: #161513;
  --text-color: #ece7dd;
  --text-secondary: #928b7f;
  --border-color: #2a2823;
  --code-background-color: #26241f;
  --accent-color: #ff7364;
  --link-underline: #b0564a;
  --bg-gray-focus: rgba(255, 255, 255, 0.08);
  --selection-color: rgba(255, 115, 100, 0.26);
  --diagram-infra: #1c2630;
  --diagram-user: #2e2814;
}

/* ========================================
   RESPONSIVE TYPOGRAPHY & BASE LAYOUT
   ======================================== */

/* Fluid root size: 18px on phones, 22px on large screens.
   rem inside root font-size resolves against the initial 16px. */
html {
  font-size: clamp(1.125rem, 0.958rem + 0.556vw, 1.375rem);
  /* Stop iOS Safari from inflating text in landscape. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* We style our own :active feedback. */
  -webkit-tap-highlight-color: transparent;
}

/* ========================================
   HTML & BODY RESETS
   ======================================== */

body {
  background-color: var(--background-color);
  color: var(--text-color);
  font-family: 'New York', Georgia, 'Noto Serif', 'Liberation Serif', 'Times New Roman', serif;
  padding: 0;
  margin: 0;
  text-rendering: optimizeLegibility;
  font-feature-settings:
    'kern' 1,
    'liga' 1;
}

@media (prefers-reduced-motion: no-preference) {
  @view-transition {
    navigation: auto;
  }

  html {
    scroll-behavior: smooth;
  }
}

* {
  margin: 0;
  padding: 0;
}

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

/* ========================================
   TYPOGRAPHY
   ======================================== */

p {
  margin-bottom: 1em;
  line-height: 1.6;
  max-width: 62ch;
  text-wrap: pretty;
}

p:has(img),
p:has(svg.diagram) {
  text-align: center;
}

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

/* Inline diagrams (SVG inlined at generation time).

   Each diagram SVG is self-contained: it ships its own <style> with light-mode
   defaults (geometry + colors) so it also renders correctly on its own and in
   the Atom feed. Here we override only the colors with the theme variables so
   the diagrams adapt to the light/dark theme, including the manual toggle.
   These rules win by specificity over the SVG's own bare-class rules. */
svg.diagram {
  display: block;
  width: 100%;
  height: auto;
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

svg.diagram text {
  fill: var(--text-color);
}

svg.diagram .d-call,
svg.diagram .d-arrow-call {
  stroke: var(--text-color);
  fill: var(--text-color);
}

svg.diagram .d-return,
svg.diagram .d-lifeline,
svg.diagram .d-frame,
svg.diagram .d-panel,
svg.diagram .d-frame-tab {
  stroke: var(--text-secondary);
}

svg.diagram .d-panel,
svg.diagram .d-frame-tab {
  fill: var(--code-background-color);
}

svg.diagram .d-return,
svg.diagram .d-arrow-return,
svg.diagram .d-muted,
svg.diagram .d-frame-label {
  fill: var(--text-secondary);
}

svg.diagram .d-box {
  fill: none;
  stroke: var(--text-secondary);
}

svg.diagram .d-infra {
  fill: var(--diagram-infra);
  stroke: var(--text-secondary);
}

svg.diagram .d-user {
  fill: var(--diagram-user);
  stroke: var(--text-secondary);
}

h1 {
  display: block;
  font-size: 170%;
  line-height: 1.1;
  letter-spacing: -0.01em;
  margin-top: 1rem;
  margin-bottom: 1.5rem;
  padding-top: 0.02em;
  hyphens: none;
  text-wrap: balance;
  scroll-margin-top: 1rem;
}

h2 {
  padding-top: 0.02em;
  margin-top: 0.7rem;
  margin-bottom: 1.1rem;
  text-wrap: balance;
  scroll-margin-top: 1rem;
}

h3 {
  padding-top: 0.02em;
  margin-top: 0.7rem;
  margin-bottom: 1.1rem;
  text-wrap: balance;
  scroll-margin-top: 1rem;
}

aside {
  font-size: 0.84rem;
  line-height: 1.4;
  color: var(--text-secondary);
}

aside em {
  font-size: 0.7rem;
}

/* ========================================
   LISTS & LINKS
   ======================================== */

ul,
ol {
  margin-bottom: 1em;
  padding-left: 1em;
}

li {
  line-height: 1.75;
}

a {
  color: inherit;
  text-decoration: underline;
  text-decoration-color: var(--link-underline);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  transition: text-decoration-thickness 0.15s ease;
}

a:visited {
  text-decoration-color: var(--text-secondary);
}

@media (hover: hover) {
  a:hover {
    text-decoration-thickness: 2.5px;
  }
}

:is(a, button, summary):focus-visible {
  outline: 2px solid var(--accent-color);
  outline-offset: 3px;
  border-radius: 2px;
}

/* ========================================
   CODE BLOCKS & INLINE CODE
   ======================================== */

pre {
  font-size: 0.8rem;
  font-family: var(--mono-font);
  line-height: 1.55;
  margin-bottom: 1em;
  background-color: var(--code-background-color);
  padding: 1em;
  border-radius: 5px;
  /* Scroll wide code instead of wrapping it mid-expression. */
  overflow-x: auto;
}

pre > code {
  white-space: pre;
}

:not(pre) > code {
  hyphens: none;
  border-radius: 5px;
  font-family: var(--mono-font);
  font-size: 0.85em;
  background-color: var(--code-background-color);
  color: inherit;
  padding: 0.12em 0.38em;
  white-space: pre-wrap;
}

/* ========================================
   MAIN LAYOUT & CONTAINER
   ======================================== */

.container {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 'header' 'content' 'footer';
  row-gap: 1em;
  margin-left: auto;
  margin-right: auto;
  max-width: 1094px;
  /* Pin the footer to the bottom on short pages. */
  min-height: 100vh;
  min-height: 100dvh;
  padding-inline: clamp(1.25rem, 3vw, 2rem);
}

.container > header {
  margin-top: 1.15rem;
}

.skip-link {
  position: fixed;
  z-index: 40;
  top: 0.4rem;
  left: 1rem;
  padding: 0.5rem 1rem;
  background: var(--background-color);
  transform: translateY(calc(-100% - 1rem));
}

.skip-link:focus {
  transform: none;
}

.container .content {
  grid-area: content;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  /* Keep rows packed at the top; without this, the extra space of
     the 1fr container row is spread between the rows. */
  align-content: start;
  row-gap: 0.6em;
  overflow-wrap: anywhere;
}

/* Let children shrink below their content minimum, so horizontally
   scrolling code blocks don't force the whole page wide. */
.container .content > * {
  min-width: 0;
}

.content.about-page {
  display: block;
  padding-block: clamp(1.25rem, 4.7vw, 3.2rem);
}

.about-page h1 {
  margin: 0 0 0.3em;
  padding: 0;
  font-size: clamp(2.45rem, 5.15vw, 3.5rem);
  line-height: 1.05;
  letter-spacing: -0.035em;
}

.about-page > p {
  font-size: clamp(1rem, 1.78vw, 1.21rem);
  line-height: 1.4;
  margin: 0 0 0.7em;
  max-width: 56ch;
}

.about-page h1 + p {
  font-size: clamp(1.2rem, 2.4vw, 1.64rem);
  margin-bottom: 0.86em;
}

.about-page h1 + p a {
  color: var(--accent-color);
}

.about-page > ul {
  font-size: clamp(1rem, 1.7vw, 1.18rem);
  display: flex;
  flex-wrap: wrap;
  column-gap: 2em;
  padding: 0;
  margin: 1.32em 0 0;
  list-style: none;
}

.about-page li a {
  display: inline-block;
  padding-block: 0.2em;
  text-decoration-color: currentColor;
}

.container > footer {
  grid-area: footer;
  border-top: 1px solid var(--border-color);
  padding-block: clamp(0.5rem, 1.6vw, 1.1rem) clamp(1rem, 3.8vw, 2.25rem);
  font-size: clamp(0.88rem, 1.7vw, 1.15rem);
}

.container > footer > ul {
  display: flex;
  flex-wrap: wrap;
  padding: 0;
  margin: 0;
  gap: 0 clamp(0.75rem, 2.45vw, 1.65rem);
  justify-content: center;
}

.container > footer > ul > li {
  list-style-type: none;
  line-height: normal;
}

.container > footer > ul > li > a {
  display: inline-block;
  padding-block: 0.5em;
  color: var(--text-color);
  text-decoration: none;
}

@media (hover: hover) {
  .container > footer > ul > li > a:hover {
    text-decoration: underline;
    text-decoration-color: var(--link-underline);
    text-underline-offset: 3px;
  }
}

.container > footer > ul > li > a:active {
  text-decoration: underline;
  text-decoration-color: var(--link-underline);
  text-underline-offset: 3px;
}

.container > footer a::after,
.nav-links a::after {
  content: '';
  display: inline-block;
  width: 0.17em;
  height: 0.17em;
  margin-left: 0.12em;
  vertical-align: super;
  border: max(1px, 0.08em) solid var(--accent-color);
  border-radius: 50%;
}

.icon-feed {
  width: 0.95em;
  height: 0.95em;
  vertical-align: -0.05em;
}

[data-theme='dark'] .icon-feed {
  filter: invert(1);
}

/* ========================================
   NAVIGATION & HAMBURGER MENU
   ======================================== */

.main-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding-bottom: 1.05rem;
  border-bottom: 1px solid var(--border-color);
  position: relative;
}

.nav-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  column-gap: 1.25em;
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: clamp(1rem, 1.9vw, 1.28rem);
}

.nav-links li {
  line-height: 1.3;
}

.nav-links a {
  display: block;
  padding-block: 0.25em;
  text-transform: lowercase;
  font-weight: bold;
  color: var(--text-color);
  text-decoration: none;
}

.nav-links a[aria-current],
.nav-links a:active {
  text-decoration: underline;
  text-decoration-color: var(--accent-color);
  text-underline-offset: 0.24em;
  text-decoration-thickness: 2px;
}

@media (hover: hover) {
  .nav-links a:hover {
    text-decoration: underline;
    text-decoration-color: var(--accent-color);
    text-underline-offset: 0.24em;
  }
}

#theme-toggle,
.hamburger {
  flex: none;
  width: 44px;
  height: 44px;
  place-items: center;
  color: var(--text-color);
  background: none;
  border: none;
  border-radius: 0.2rem;
  cursor: pointer;
}

#theme-toggle:not([hidden]) {
  display: grid;
}

#theme-toggle svg {
  width: clamp(1.35rem, 2.45vw, 1.65rem);
  height: auto;
}

#theme-toggle svg > * {
  display: none;
}

[data-theme-preference='auto'] #theme-toggle .theme-auto,
[data-theme-preference='light'] #theme-toggle .theme-light,
[data-theme-preference='dark'] #theme-toggle .theme-dark {
  display: initial;
}

:is(#theme-toggle, .hamburger):active {
  background: var(--bg-gray-focus);
}

@media (hover: hover) {
  :is(#theme-toggle, .hamburger):hover {
    background: var(--bg-gray-focus);
  }
}

.hamburger {
  display: none;
  align-content: center;
  row-gap: 4px;
}

.hamburger span {
  width: 20px;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
  transition:
    transform 0.18s,
    opacity 0.18s;
}

@media (max-width: 520px) {
  .container > header {
    position: sticky;
    top: 0;
    z-index: 30;
    margin-top: 0;
    background-color: var(--background-color);
  }

  :is(h1, h2, h3, h4, h5, h6, #main-content) {
    scroll-margin-top: 4rem;
  }

  .main-nav {
    padding-block: 0.25rem;
  }

  .nav-links {
    column-gap: 0.8em;
  }

  .main-nav[data-menu-ready] .hamburger {
    display: grid;
  }

  .main-nav[data-menu-ready] .nav-links {
    display: none;
    position: absolute;
    top: calc(100% + 0.35rem);
    inset-inline: 0;
    max-height: calc(100dvh - 4rem);
    overflow-y: auto;
    background: var(--background-color);
    border: 1px solid var(--border-color);
    border-radius: 0.4rem;
    box-shadow: 0 10px 24px rgb(0 0 0 / 0.15);
  }

  body.nav-open {
    overflow: hidden;
  }

  body.nav-open::before {
    content: '';
    position: fixed;
    inset: 0;
    z-index: 20;
    background: color-mix(in srgb, var(--text-color) 25%, transparent);
  }

  .nav-open .main-nav .nav-links {
    display: block;
    animation: nav-menu-in 0.18s ease-out;
  }

  .hamburger[aria-expanded='true'] span:first-child {
    transform: translateY(6px) rotate(45deg);
  }

  .hamburger[aria-expanded='true'] span:nth-child(2) {
    opacity: 0;
  }

  .hamburger[aria-expanded='true'] span:last-child {
    transform: translateY(-6px) rotate(-45deg);
  }

  .main-nav[data-menu-ready] .nav-links li + li {
    border-top: 1px solid var(--border-color);
  }

  .main-nav[data-menu-ready] .nav-links a {
    padding: 0.75rem 1rem;
    text-align: center;
  }
}

@keyframes nav-menu-in {
  from {
    opacity: 0;
    transform: translateY(-0.4rem);
  }
}

/* ========================================
   BLOG STYLES
   ======================================== */

.blog-year {
  max-width: 42rem;
}

.blog-year h2 {
  margin: 0 0 0.4em;
  font-size: 1.25rem;
}

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

.blog-year li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 0 1.5rem;
  border-top: 1px solid var(--border-color);
  padding-block: 0.55rem;
  line-height: 1.4;
}

.blog-year a {
  justify-self: start;
}

.blog-year time,
.article-header time {
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 580px) {
  .blog-year li {
    grid-template-columns: minmax(0, 1fr);
    row-gap: 0.15em;
  }
}

.article {
  max-width: 68ch;
}

.article-header {
  margin: 0.55rem 0 1.25rem;
}

.article-header h1 {
  margin: 0 0 0.25em;
}

.blog-toc {
  margin-bottom: 1.15rem;
  border-block: 1px solid var(--border-color);
  font-size: 0.9rem;
}

.blog-toc summary {
  width: fit-content;
  max-width: 100%;
  padding: 0.65em 0.15em;
  font-weight: bold;
  cursor: pointer;
}

.blog-toc nav {
  padding: 0 0.3em 0.8em;
}

.blog-toc ul {
  margin: 0;
}

.blog-toc a {
  color: var(--text-secondary);
}

/* ========================================
   ACCESSIBILITY & SMALL SCREENS
   ======================================== */

@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;
  }
}

/* Let dense diagrams scroll horizontally instead of shrinking
   their labels into illegibility. */
@media (max-width: 580px) {
  p:has(svg.diagram) {
    overflow-x: auto;
  }

  svg.diagram {
    min-width: 30rem;
  }
}
