/* ========== tokens (light, warm) ========== */
:root{
  --bg:        #faf9f5;
  --paper:     #ffffff;
  --ink:       #14140f;
  --ink-2:     #2a2a23;
  --muted:     #6b6b60;
  --muted-2:   #9a9a8e;
  --rule:      #e5e2d6;
  --rule-2:    #ecead0;

  --accent:    #4d7a2e;     /* darker lime for light bg legibility */
  --accent-2:  #a87a1c;     /* amber */
  --highlight: #f3ecb4;     /* highlighter yellow */

  --code-bg:   #1a1a1a;
  --code-fg:   #e6e6dc;

  --font-display: "Instrument Serif", ui-serif, Georgia, "Times New Roman", serif;
  --font-body:    "Newsreader", ui-serif, Georgia, serif;
  --font-mono:    "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;

  --maxw: 760px;
  --maxw-wide: 1120px;
}

*,*::before,*::after{ box-sizing:border-box; }
html, body { margin:0; padding:0; }
html{ scroll-behavior:smooth; }
body{
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 18px;
  line-height: 1.7;
  font-variation-settings: "opsz" 16;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
::selection{ background: var(--highlight); color: var(--ink); }
a{ color: inherit; }
a:hover{ color: var(--accent); }

/* ========== sticky nav ========== */
.nav{
  position:sticky; top:0; z-index:60;
  background: rgba(250,249,245,0.88);
  backdrop-filter: blur(10px) saturate(120%);
  -webkit-backdrop-filter: blur(10px) saturate(120%);
  border-bottom: 1px solid var(--rule);
}
.nav-inner{
  max-width: var(--maxw-wide);
  margin: 0 auto;
  padding: 14px 28px;
  display:flex; align-items:center; justify-content:space-between;
  gap: 24px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--muted);
}
.crumbs{
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.crumbs a{ color: var(--muted); text-decoration: none; }
.crumbs a:hover{ color: var(--ink); }
.crumbs .sep{ color: var(--muted-2); margin: 0 4px; }
.crumbs .here{ color: var(--ink); }
.nav-right{
  flex-shrink: 0;
  display:flex; align-items:center; gap: 18px;
  white-space: nowrap;
}
.nav-right .dot{
  display:inline-block; width:6px; height:6px; border-radius:50%;
  background: var(--accent); margin-right: 6px; vertical-align: middle;
}
@media (max-width: 760px){
  .nav-right > span:not(.lang-toggle){ display:none; }
}
@media (max-width: 498px){
  .nav-inner{ flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 16px; }
  .nav-right{ align-self: flex-end; }
}
@media (min-width: 374px) and (max-width: 498px){
  .crumbs{ display:flex; align-items:center; }
  .crumbs::after{
    content:""; flex:1; height:1.5px;
    background: var(--accent);
    margin-left: 10px;
  }
}

/* ========== layout ========== */
.container{
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 28px;
}
.container-wide{
  max-width: var(--maxw-wide);
  margin: 0 auto;
  padding: 0 28px;
}

/* ========== hero ========== */
.hero{
  padding: 80px 0 48px;
  border-bottom: 1px solid var(--rule);
}
.hero .kicker{
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 28px;
  display:flex; align-items:center; gap: 12px;
}
.hero .kicker::before{
  content:""; width: 28px; height: 1.5px; background: var(--accent);
}
.hero h1{
  font-family: var(--font-display);
  font-weight: 400;
  font-style: italic;
  font-size: clamp(48px, 7.5vw, 88px);
  line-height: 0.98;
  letter-spacing: -0.012em;
  margin: 0 0 28px;
  color: var(--ink);
  text-wrap: balance;
}
.hero h1 em{
  font-style: normal;
  color: var(--accent);
}
.hero .standfirst{
  font-family: var(--font-body);
  font-size: 20px;
  line-height: 1.55;
  color: var(--ink-2);
  max-width: 60ch;
  margin: 0 0 36px;
  font-variation-settings: "opsz" 20;
}
.meta-row{
  display:flex; gap: 28px; flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.04em;
  padding-bottom: 8px;
}
.meta-row b{ color: var(--ink); font-weight: 500; }
.meta-row .key{ color: var(--muted-2); text-transform: uppercase; letter-spacing: 0.14em; margin-right: 6px; }

/* ============================================================
   didactic memory block diagrams
   ============================================================ */
.fig{
  margin: 40px 0;
  padding: 36px 28px 28px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 4px;
  position: relative;
}
.fig-title{
  position: absolute;
  top: -10px; left: 24px;
  background: var(--paper);
  padding: 0 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
.fig-title b{ color: var(--ink); font-weight: 500; }
.fig + figcaption,
figcaption.fig-cap{
  margin-top: -28px;
  margin-bottom: 32px;
  padding-top: 12px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
  max-width: 65ch;
}
figcaption.fig-cap b{ color: var(--ink); font-weight: 500; }

/* state label (Pre Malloc / Post Malloc / Before free / After) */
.fig .state{
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.06em;
  margin: 0 0 14px;
  text-align: center;
}
.fig .state b{ color: var(--ink); font-weight: 500; }

/* row of blocks with arrows between */
.row-blk{
  display:flex;
  align-items: stretch;
  gap: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding: 4px 0 12px;
}
.row-blk + .state{ margin-top: 28px; }
.row-blk .b{
  flex-shrink: 0;
  min-width: 84px;
  min-height: 64px;
  padding: 10px 14px;
  border: 1.5px solid var(--ink);
  border-radius: 4px;
  display:flex; align-items:center; justify-content:center;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink);
  text-align:center;
  position: relative;
}
.row-blk .b.used{
  background: #f6cfcf;
  border-color: #c97777;
}
.row-blk .b.free{
  background: #cfe8cf;
  border-color: #6fa66f;
}
.row-blk .b.request{
  background: #cfdbf6;
  border-color: #6f8ac9;
  min-width: 48px;
}
.row-blk .b.ghost{
  background: var(--paper);
  border-style: dashed;
  border-color: var(--muted-2);
  color: var(--muted-2);
}
.row-blk .b small{
  display:block;
  font-size: 10px;
  color: rgba(0,0,0,0.55);
  margin-top: 4px;
  letter-spacing: 0.04em;
}
.row-blk .b.used small{ color: #6b3636; }
.row-blk .b.free small{ color: #2f5b2f; }
.row-blk .arr{
  flex-shrink: 0;
  align-self: center;
  width: 32px;
  display:flex; align-items:center; justify-content:center;
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 18px;
  line-height: 1;
}
.row-blk .arr::before{ content: "→"; }
.row-blk .arr.down::before{ content: "↓"; }
.row-blk .arr.small{ width: 18px; font-size: 14px; color: var(--muted); }
.row-blk .gap{
  flex-shrink: 0;
  width: 14px;
}

/* request line (the blue "malloc with this size") */
.req-line{
  display:flex; align-items:center; gap: 14px;
  margin: 18px 0;
  padding: 14px 16px;
  background: var(--bg);
  border: 1px dashed var(--rule);
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-2);
}
.req-line .swatch{
  width: 36px; height: 52px;
  background: #cfdbf6;
  border: 1.5px solid #6f8ac9;
  border-radius: 4px;
  flex-shrink: 0;
}
.req-line b{ color: var(--ink); font-weight: 500; }

/* arrow separator between Pre/Post sections inside a figure */
.fig .divider{
  display:flex; align-items:center; gap: 14px;
  margin: 22px 0 14px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.fig .divider::before,
.fig .divider::after{
  content: "";
  flex: 1; height: 1px;
  background: var(--rule);
}

/* split / merge bracket */
.row-blk .bracket{
  align-self: stretch;
  display:flex; flex-direction:column;
  align-items:center; justify-content:center;
  gap: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0 4px;
}
.row-blk .bracket .line{
  flex: 1; width: 1.5px;
  background: var(--ink);
}
.row-blk .bracket.dashed .line{
  background: repeating-linear-gradient(180deg, var(--muted) 0 4px, transparent 4px 8px);
}

/* legend for figures */
.fig-legend{
  margin-top: 18px;
  display:flex; gap: 22px; flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
  padding-top: 16px;
  border-top: 1px dashed var(--rule);
}
.fig-legend .sw{ display:inline-flex; align-items:center; gap: 8px; }
.fig-legend .sw i{
  display:inline-block; width: 16px; height: 12px;
  border: 1.5px solid var(--ink);
  border-radius: 2px;
  flex-shrink: 0;
}
.fig-legend .sw.used i{ background: #f6cfcf; border-color: #c97777; }
.fig-legend .sw.free i{ background: #cfe8cf; border-color: #6fa66f; }
.fig-legend .sw.req  i{ background: #cfdbf6; border-color: #6f8ac9; }
.fig-legend .sw.ghost i{ background: var(--paper); border-style: dashed; border-color: var(--muted-2); }

@media (max-width: 720px){
  .fig{ padding: 32px 18px 22px; }
  .row-blk{ padding-bottom: 8px; }
  .row-blk .b{ min-width: 68px; font-size: 12px; padding: 8px 10px; }
  .row-blk .arr{ width: 24px; font-size: 16px; }
}
figcaption{
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
  padding-top: 10px;
  max-width: 65ch;
}
figcaption b{ color: var(--ink); font-weight: 500; }

/* ========== quick answer ========== */
.quick{
  margin: 56px 0 40px;
  padding: 28px 32px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 4px;
  position: relative;
}
.quick .tag{
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 12px;
  display: block;
}
.quick p{
  font-family: var(--font-display);
  font-style: italic;
  font-size: 24px;
  line-height: 1.35;
  color: var(--ink);
  margin: 0;
  font-weight: 400;
  letter-spacing: -0.005em;
}

/* ========== TOC ========== */
.toc{
  margin: 40px 0 64px;
  padding: 28px 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
}
.toc h2{
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 16px;
  font-weight: 500;
}
.toc ol{
  list-style: none;
  padding: 0;
  margin: 0;
  counter-reset: toc;
  column-count: 2;
  column-gap: 32px;
}
.toc li{
  counter-increment: toc;
  padding: 4px 0;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  break-inside: avoid;
}
.toc li a{
  color: var(--ink-2);
  text-decoration: none;
  display: flex;
  gap: 14px;
  padding: 2px 0;
}
.toc li a:hover{ color: var(--accent); }
.toc li a::before{
  content: counter(toc, decimal-leading-zero);
  color: var(--muted-2);
  font-size: 11px;
  letter-spacing: 0.04em;
  flex-shrink: 0;
  width: 22px;
}
@media (max-width: 640px){
  .toc ol{ column-count: 1; }
}

/* ========== article body ========== */
article p{
  margin: 0 0 22px;
  max-width: 68ch;
}
article p b{ font-weight: 600; color: var(--ink); }
article p i, article p em{ font-style: italic; color: var(--ink-2); }
article p .hl{
  background: linear-gradient(180deg, transparent 62%, var(--highlight) 62%);
  padding: 0 2px;
}
article p .term{
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: rgba(0,0,0,0.04);
  padding: 1px 6px;
  border-radius: 3px;
  color: var(--ink);
}

section.chapter{
  padding: 56px 0 16px;
  scroll-margin-top: 80px;
}
section.chapter + section.chapter{
  border-top: 1px solid var(--rule);
}
section.chapter > .container > .num{
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  color: var(--accent);
  text-transform: uppercase;
  margin-bottom: 14px;
  display: block;
}
section.chapter h2{
  font-family: var(--font-display);
  font-weight: 400;
  font-style: italic;
  font-size: clamp(34px, 4.6vw, 52px);
  line-height: 1.05;
  letter-spacing: -0.005em;
  margin: 0 0 18px;
  color: var(--ink);
  text-wrap: balance;
}
section.chapter h2 code,
section.chapter h3 code,
.hero h1 code{
  font-family: var(--font-mono);
  font-style: normal;
  color: var(--accent);
  background: transparent;
  border: 0;
  padding: 0;
  font-size: 0.78em;
  font-weight: 500;
  letter-spacing: -0.01em;
}
section.chapter h3{
  font-family: var(--font-display);
  font-weight: 400;
  font-style: normal;
  font-size: 26px;
  line-height: 1.25;
  letter-spacing: -0.005em;
  margin: 40px 0 12px;
  color: var(--ink);
}
section.chapter .summary{
  font-family: var(--font-body);
  font-size: 17px;
  font-style: italic;
  color: var(--muted);
  margin: 0 0 32px;
  padding-bottom: 24px;
  border-bottom: 1px dashed var(--rule);
  max-width: 65ch;
  line-height: 1.6;
}

article ul, article ol{
  padding-left: 24px;
  margin: 0 0 22px;
}
article ul li, article ol li{
  margin-bottom: 8px;
  max-width: 64ch;
}
article ul li::marker{ color: var(--accent); }
article ol li::marker{ color: var(--muted-2); font-family: var(--font-mono); font-size: 0.9em; }

/* ========== code blocks ========== */
.code-block{
  position: relative;
  margin: 28px 0 32px;
}
.code-block .code-head{
  display:flex; align-items:center; justify-content:space-between;
  padding: 8px 14px;
  background: #111;
  border-radius: 6px 6px 0 0;
  border-bottom: 1px solid #2a2a2a;
  font-family: var(--font-mono);
  font-size: 11px;
  color: #888;
  letter-spacing: 0.04em;
}
.code-block .code-head .file{
  display:flex; align-items:center; gap: 10px;
  font-weight: 500;
  color: #b8b8a8;
}
.code-block .copy{
  background: transparent;
  border: 1.5px solid #3a3a3a;
  color: #ffffff;
  padding: 5px 14px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: color .15s, border-color .15s, background .15s, transform .1s;
}
.code-block .copy:hover{
  color: #0e0e0e;
  background: #c8f080;
  border-color: #c8f080;
}
.code-block .copy:active{ transform: translateY(1px); }
.code-block .copy.copied{
  color: #0e0e0e;
  background: #c8f080;
  border-color: #c8f080;
}

.code-block pre{
  margin: 0;
  padding: 18px 18px 20px;
  background: var(--code-bg) !important;
  color: var(--code-fg);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.65;
  border-radius: 0 0 6px 6px;
  overflow-x: auto;
}
/* override hljs comment color — bright neon green */
.code-block pre code .hljs-comment,
.code-block pre code .hljs-quote{
  color: #c8f080 !important;
  font-style: italic;
}

/* ========== language toggle ========== */
.lang-toggle{
  display:flex;
  align-items:center;
  gap: 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 4px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--paper);
}
.lang-toggle button{
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.lang-toggle button:hover{ color: var(--ink); }
.lang-toggle button.active{
  background: var(--ink);
  color: var(--paper);
}
/* hide the inactive language */
html[lang="en"] [lang="es"],
html[lang="es"] [lang="en"]{ display: none !important; }
.code-block pre code.hljs{
  padding: 0;
  background: transparent !important;
  font-family: inherit;
  font-size: inherit;
}

/* inline code */
p code, li code{
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: rgba(0,0,0,0.05);
  padding: 1.5px 6px;
  border-radius: 3px;
  color: var(--ink);
  border: 1px solid var(--rule);
}

/* ========== callouts (zine aside) ========== */
.callout{
  margin: 40px 0;
  padding: 24px 0 4px;
  background: transparent;
  display: block;
  position: relative;
  border-top: 1.5px solid var(--accent);
}
.callout > .lbl{
  position: absolute;
  top: -9px;
  left: 0;
  background: var(--bg);
  padding: 0 12px 0 0;
  font-family: var(--font-mono);
  font-size: 10.5px;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--ink);
  font-weight: 500;
}
.callout.warn > .lbl{ color: var(--accent-2); }
.callout p{
  margin: 0 !important;
  font-family: var(--font-display);
  font-style: italic;
  font-size: 21px;
  line-height: 1.45;
  color: var(--ink);
  max-width: 56ch;
  letter-spacing: -0.005em;
  font-weight: 400;
}
.callout p + p{ margin-top: 14px !important; }
.callout p b{
  font-style: normal;
  font-family: var(--font-body);
  font-weight: 600;
  color: var(--ink);
  font-size: 0.88em;
}
.callout p code{
  font-style: normal;
  font-family: var(--font-mono);
  font-size: 0.78em;
  background: rgba(0,0,0,0.05);
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid var(--rule);
  vertical-align: 2px;
}
.callout p i, .callout p em{
  font-style: normal;
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 1.5px;
  text-underline-offset: 4px;
}
@media (max-width: 640px){
  .callout p{ font-size: 18px; }
}

/* ========== legacy ascii diagram (kept minimal) ========== */
.diagram{
  margin: 32px 0;
  padding: 24px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-2);
  overflow-x: auto;
}
.diagram pre{
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: 1.6;
  color: inherit;
  white-space: pre;
}

/* ========== FAQ ========== */
.faq{
  padding: 64px 0 32px;
  border-top: 1px solid var(--rule);
}
.faq h2{
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 400;
  font-size: 42px;
  margin: 0 0 28px;
  color: var(--ink);
  letter-spacing: -0.005em;
}
.faq details{
  border-bottom: 1px solid var(--rule);
  padding: 18px 0;
}
.faq details summary{
  cursor: pointer;
  list-style: none;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 22px;
  color: var(--ink);
  display:flex; justify-content:space-between; align-items: center;
  gap: 16px;
  padding: 6px 0;
}
.faq details summary::-webkit-details-marker{ display: none; }
.faq details summary::after{
  content: "+";
  font-family: var(--font-mono);
  font-weight: 400;
  font-size: 22px;
  color: var(--accent);
  transition: transform .25s;
  flex-shrink: 0;
}
.faq details[open] summary::after{ transform: rotate(45deg); }
.faq details p{
  margin: 12px 0 6px;
  color: var(--ink-2);
  font-size: 16px;
  line-height: 1.75;
  max-width: 68ch;
}

/* ========== footer ========== */
footer.site{
  margin-top: 80px;
  padding: 56px 0 40px;
  border-top: 1px solid var(--rule);
  background: var(--paper);
}
footer.site .ft{
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 28px;
  display:flex; justify-content:space-between; gap: 24px; flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--muted);
}
footer.site .ft a{
  color: var(--ink);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color .2s, color .2s;
}
footer.site .ft a:hover{ color: var(--accent); border-color: var(--accent); }
footer.site .ft .col{ display:flex; flex-direction:column; gap: 6px; }
footer.site .ft .col .k{ color: var(--muted-2); text-transform: uppercase; letter-spacing: 0.14em; font-size: 11px; }

/* reading progress bar */
.progress{
  position: fixed; left: 0; top: 0; height: 2px;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  z-index: 70;
  transition: width .1s linear;
}

/* mobile spacing tweaks */
@media (max-width: 640px){
  body{ font-size: 16px; line-height: 1.8; }
  .hero{ padding: 56px 0 40px; }
  section.chapter{ padding: 44px 0 8px; }
  .quick{ padding: 22px 20px; }
  .quick p{ font-size: 18px; }
}

/* print */
@media print{
  .nav, .progress, footer.site, .code-block .copy{ display: none !important; }
  body{ background: white; color: black; }
  .code-block pre{ background: #f5f5f5 !important; color: #111; border:1px solid #ccc; }
}
