/* ========================================
   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;
}

/* Same-origin cross-document view transitions (progressive enhancement). */
@view-transition {
  navigation: auto;
}

/* ========================================
   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-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 1200px;
  text-rendering: optimizeLegibility;
  font-feature-settings:
    'kern' 1,
    'liga' 1;
}

@media (prefers-reduced-motion: no-preference) {
  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;
  }
}

a: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: auto;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 'header' 'content' 'footer';
  row-gap: 1em;
  margin-left: auto;
  margin-right: auto;
  /* Pin the footer to the bottom on short pages. */
  min-height: 100vh;
  min-height: 100dvh;
  padding-left: 0.7em;
  padding-right: 0.7em;
}

@media (max-width: 580px) {
  .container {
    padding-left: 1.25rem;
    padding-right: 1.25rem;
  }
}

.container > header {
  margin-top: 1em;
}

@media (max-width: 520px) {
  .container > header {
    margin-top: 0;
  }
}

.container .content {
  grid-area: content;
  display: grid;
  grid-auto-flow: row;
  /* 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;
}

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

@media (max-width: 520px) {
  .content {
    padding: 0;
  }
}

.container > footer {
  grid-area: footer;
  padding-bottom: 0.4rem;
}

.container > footer > ul {
  display: grid;
  grid-auto-flow: column;
  padding: 0;
  margin: 0;
  gap: 10px;
  justify-content: center;
}

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

.container > footer > ul > li > a {
  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 > ul > li > a::after {
  content: '\FEFF°';
  margin-left: 0.1em;
  font-size: 80%;
  vertical-align: 0.35em;
  color: var(--accent-color);
}

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

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

.main-nav {
  display: grid;
  grid-template-columns: max-content auto;
  padding: 0 0 0.6em 0;
  border-bottom: 1px solid var(--border-color);
  align-items: center;
  /* Anchor for the mobile menu overlay. */
  position: relative;
}

/* Theme toggle button positioned after nav links on desktop */
.main-nav > #theme-toggle {
  order: 1;
}

.nav-links {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  column-gap: 1.3em;
  margin: 0;
  align-items: center;
  padding: 0;
}

/* Theme toggle button styling */
#theme-toggle {
  background: none;
  border: none;
  /* 44px square for a comfortable touch target. */
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1.1em;
  transition:
    background 0.18s,
    color 0.18s;
  justify-self: end;
}

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

@media (hover: hover) {
  #theme-toggle:hover {
    background: var(--bg-gray-focus);
    color: var(--accent-color);
  }
}

#theme-toggle:focus-visible {
  outline: 2px solid var(--accent-color);
  outline-offset: 2px;
}

.theme-icon {
  display: block;
  font-size: 1em;
}

.nav-links > li {
  list-style-type: none;
  line-height: normal;
}

.nav-links > li > a {
  max-width: fit-content;
  text-transform: lowercase;
  font-weight: bold;
  color: var(--text-color);
  text-decoration: none;
}

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

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

.nav-links > li > a::after {
  content: '\FEFF°';
  margin-left: 0.1em;
  font-size: 80%;
  vertical-align: 0.35em;
  color: var(--accent-color);
}

/* Hamburger menu button.
   The bars are 20x25px; the padding brings the button to a
   44px touch target without changing how the bars animate.
   content-box overrides the UA's border-box default for buttons,
   so the padding adds to the 20x25px content instead of squeezing it. */
.hamburger {
  display: none;
  grid-auto-flow: row;
  box-sizing: content-box;
  width: 20px;
  height: 25px;
  padding: 9.5px 12px;
  justify-self: end;
  background: none;
  border: none;
  cursor: pointer;
}

.hamburger:active {
  background: var(--bg-gray-focus);
}

.hamburger:focus-visible {
  outline: 2px solid var(--accent-color);
  outline-offset: 2px;
}

.hamburger span {
  display: block;
  height: 3px;
  width: 100%;
  background-color: var(--text-color);
  border-radius: 5px;
  transition: all 0.3s ease-in-out;
}

/* Mobile navigation */
@media (max-width: 520px) {
  /* The header stays reachable while scrolling, so the menu button
     and its dropdown work at any scroll position. */
  .container > header {
    position: sticky;
    top: 0;
    z-index: 30;
    background-color: var(--background-color);
  }

  /* Anchored headings must clear the sticky header. */
  :is(h1, h2, h3) {
    scroll-margin-top: 4rem;
  }

  .hamburger {
    display: grid;
  }

  .main-nav > #theme-toggle {
    order: -1;
    justify-self: start;
  }

  /* Dim the page and freeze scrolling while the menu is open. */
  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);
    animation: nav-menu-in 0.18s ease-out;
  }

  .nav-links {
    display: none;
    /* Anchored to the nav itself, so the menu stays under the
       button no matter where the page is scrolled. */
    position: absolute;
    top: calc(100% + 0.5rem);
    left: 0;
    right: 0;
    z-index: 25;
    padding: 0.25rem 0;
    background-color: var(--background-color);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
    grid-template-columns: auto;
    grid-auto-flow: row;
  }

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

  .main-nav.nav-open .hamburger span:nth-child(1) {
    transform: rotate(45deg) translate(8px, 4px);
  }

  .main-nav.nav-open .hamburger span:nth-child(2) {
    opacity: 0;
  }

  .main-nav.nav-open .hamburger span:nth-child(3) {
    transform: rotate(-45deg) translate(8px, -4px);
  }

  .nav-links > li {
    display: flex;
    padding: 1em;
    border-bottom: 1px solid var(--border-color);
    justify-content: center;
    align-content: center;
  }

  .nav-links > li:last-child {
    border-bottom: 0;
  }
}

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

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

/* Blog index page */
.blog-month > ul {
  padding: 0;
}

.blog-month > ul > li {
  list-style-type: none;
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 1em;
  align-items: baseline;
}

.blog-month > ul > li > a {
  grid-column: 1;
  max-width: fit-content;
}

.blog-month > ul > li > span {
  grid-column: 2;
  justify-self: end;
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Blog entry page */
.article-header {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas: 'title date';
  align-items: center;
}

.article-header > h1 {
  grid-area: title;
  margin: 0;
  padding: 0;
}

.article-header > h2 {
  grid-area: date;
  align-self: center;
  justify-self: right;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin: 0;
  padding: 0;
}

@media (max-width: 840px) {
  .article-header {
    grid-template-rows: auto auto;
    grid-template-areas:
      'title'
      'date';
  }
  .article-header > h2 {
    justify-self: left;
    margin-top: 0.7em;
  }
}

/* Blog table of contents: visually secondary so it doesn't compete
   with the article itself. */
nav.blog-toc {
  font-size: 0.8rem;
  color: var(--text-secondary);
  border-left: 2px solid var(--border-color);
  padding-left: 1em;
}

nav.blog-toc ul {
  margin-bottom: 0;
}

nav.blog-toc a {
  color: inherit;
  text-decoration: none;
}

nav.blog-toc a:hover {
  text-decoration: underline;
  text-decoration-color: var(--link-underline);
  text-underline-offset: 2px;
}

/* ========================================
   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;
  }
}
