/* ============================================================
   CSS CUSTOM PROPERTIES
   ============================================================ */
:root {
  --font-heading: 'Fraunces', Georgia, serif;
  --font-body: 'JetBrains Mono', 'Fira Code', monospace;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
  --font-hand: 'Caveat', cursive;

  --bg: #f0ebe3;
  --surface: #ffffff;
  --text: #1c1917;
  --text-muted: #8a8078;
  --heading: #5c2018;
  --accent: #8b4049;
  --accent-dim: rgba(139, 64, 73, 0.08);
  --border: #e7e5e4;
  --border-strong: #d6d3d1;

  --code-bg: #1e1e2e;
  --code-text: #cdd6f4;
  --code-kw: #cba6f7;
  --code-str: #a6e3a1;
  --code-num: #fab387;
  --code-cmt: #6c7086;
  --code-reg: #89b4fa;
  --code-fn: #89dceb;

  /* pencil / sketch palette */
  --pencil: #3d3d3d;
  --pencil-light: #888;
  --pencil-faint: #bbb;
  --paper: #fefcf5;
  --paper-line: #e8e0d0;
  --sketch-blue: #4a7ab5;
  --sketch-red: #c0544e;
  --sketch-green: #5a9e6f;
  --sketch-orange: #d4943a;
  --sketch-purple: #8b6aad;
  --sketch-pink: #c46a8a;
  --sketch-teal: #4a9e9e;

  --callout-bg: rgba(139, 64, 73, 0.08);
  --callout-bad-bg: rgba(239, 68, 68, 0.06);
  --accent-border: rgba(139, 64, 73, 0.15);
  --nav-bg: rgba(240, 235, 227, 0.85);
  --nav-border: #e7e5e4;
}

[data-theme="dark"] {
  --bg: #1a1614;
  --surface: #201e1a;
  --text: #e7e0d8;
  --text-muted: #9a918a;
  --heading: #d4a490;
  --accent: #d4808a;
  --accent-dim: rgba(212, 128, 138, 0.12);
  --border: #33302e;
  --border-strong: #44403c;
  --pencil: #c8c0b8;
  --pencil-light: #998f85;
  --pencil-faint: #5a5450;
  --paper: #201e1a;
  --paper-line: #302c28;
  --sketch-blue: #6a9ad5;
  --sketch-red: #e0746e;
  --sketch-green: #7abe8f;
  --sketch-orange: #e4b45a;
  --sketch-purple: #ab8acd;
  --sketch-pink: #e48aaa;
  --sketch-teal: #6abebe;
  --callout-bg: rgba(212, 128, 138, 0.12);
  --callout-bad-bg: rgba(239, 100, 100, 0.12);
  --accent-border: rgba(212, 128, 138, 0.2);
  --nav-bg: rgba(26, 22, 20, 0.85);
  --nav-border: #33302e;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1614;
    --surface: #201e1a;
    --text: #e7e0d8;
    --text-muted: #9a918a;
    --heading: #d4a490;
    --accent: #d4808a;
    --accent-dim: rgba(212, 128, 138, 0.12);
    --border: #33302e;
    --border-strong: #44403c;
    --pencil: #c8c0b8;
    --pencil-light: #998f85;
    --pencil-faint: #5a5450;
    --paper: #201e1a;
    --paper-line: #302c28;
    --sketch-blue: #6a9ad5;
    --sketch-red: #e0746e;
    --sketch-green: #7abe8f;
    --sketch-orange: #e4b45a;
    --sketch-purple: #ab8acd;
    --sketch-pink: #e48aaa;
    --sketch-teal: #6abebe;
    --callout-bg: rgba(212, 128, 138, 0.12);
    --callout-bad-bg: rgba(239, 100, 100, 0.12);
    --accent-border: rgba(212, 128, 138, 0.2);
    --nav-bg: rgba(26, 22, 20, 0.85);
    --nav-border: #33302e;
  }
}

/* ============================================================
   TRANSITION
   ============================================================ */
html.theme-transition,
html.theme-transition *,
html.theme-transition *::before,
html.theme-transition *::after {
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease !important;
}

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 18px;
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
  line-height: 1.65;
  font-size: 0.92rem;
}

/* ============================================================
   TYPOGRAPHY
   ============================================================ */
h1, h2, h3, h4 { font-family: var(--font-mono); color: var(--heading); line-height: 1.3; letter-spacing: -0.01em; }

h1 { font-size: 1.6rem; font-weight: 700; }
h2 { font-size: 1.3rem; font-weight: 600; margin-top: 0; }
h3 { font-size: 1.05rem; font-weight: 600; margin-top: 1.75rem; margin-bottom: 0.5rem; }

h1::before { content: '# '; color: var(--text-muted); font-weight: 400; }
h2::before { content: '## '; color: var(--text-muted); font-weight: 400; }
h3::before { content: '### '; color: var(--text-muted); font-weight: 400; }

p { margin-bottom: 0.9rem; }
strong { font-weight: 600; color: var(--heading); }

a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-weight: 500;
}

ul, ol { margin-bottom: 1.5rem; padding-left: 1.25rem; }
li { margin-bottom: 0.25rem; }

/* ============================================================
   LAYOUT
   ============================================================ */
.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.wide {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

section { padding: 1.5rem 0 1rem; }

/* ============================================================
   HERO
   ============================================================ */
.hero { padding: 2.5rem 0 0.5rem; }

.hero h1 {
  margin-bottom: 1.5rem;
}

.hero .lead {
  color: var(--text-muted);
  max-width: 540px;
  line-height: 1.7;
  margin-bottom: 2rem;
}

/* ============================================================
   CODE BLOCKS
   ============================================================ */
pre {
  background: var(--code-bg);
  color: var(--code-text);
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.7;
  padding: 1.5rem;
  border-radius: 10px;
  overflow-x: auto;
  margin: 1rem 0 1.25rem;
}

pre .kw { color: var(--code-kw); }
pre .str { color: var(--code-str); }
pre .num { color: var(--code-num); }
pre .cmt { color: var(--code-cmt); font-style: italic; }
pre .reg { color: var(--code-reg); }
pre .fn { color: var(--code-fn); }

/* ============================================================
   CALLOUT
   ============================================================ */
.callout {
  background: var(--callout-bg);
  border-left: 3px solid var(--accent);
  padding: 1rem 1.25rem;
  border-radius: 0 8px 8px 0;
  margin: 1rem 0 1.25rem;
  font-size: 0.95rem;
}

.callout p:last-child { margin-bottom: 0; }

.callout.bad {
  background: var(--callout-bad-bg);
  border-left-color: var(--sketch-red);
}

/* ============================================================
   SKETCH DIAGRAMS
   ============================================================ */
.sketch {
  background: var(--paper);
  border: 1.5px solid var(--paper-line);
  border-radius: 6px;
  padding: 1.5rem;
  margin: 1.25rem 0;
  overflow-x: auto;
  /* subtle notebook-paper feel */
  background-image:
    repeating-linear-gradient(
      var(--paper) 0px, var(--paper) 31px,
      var(--paper-line) 31px, var(--paper-line) 32px
    );
  background-size: 100% 32px;
  background-position: 0 1rem;
}

.sketch svg {
  display: block;
  margin: 0 auto;
  width: 100%;
  height: auto;
  overflow: visible;
}

.sketch-caption {
  text-align: center;
  font-family: var(--font-hand);
  font-size: 1.05rem;
  color: var(--pencil-light);
  margin-top: 0.75rem;
  font-weight: 500;
}

/* ============================================================
   PROS / CONS
   ============================================================ */
.tradeoff {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin: 1rem 0 1.25rem;
}

.tradeoff-col {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
}

.tradeoff-col h4 {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.75rem;
}

.tradeoff-col.pros h4 { color: var(--sketch-green); }
.tradeoff-col.cons h4 { color: var(--sketch-red); }

.tradeoff-col ul {
  list-style: none;
  padding: 0;
}

.tradeoff-col ul li {
  font-size: 0.9rem;
  padding: 0.3rem 0;
  padding-left: 1.2rem;
  position: relative;
}

.tradeoff-col.pros ul li::before {
  content: '+';
  position: absolute;
  left: 0;
  color: var(--sketch-green);
  font-weight: 700;
  font-family: var(--font-mono);
}

.tradeoff-col.cons ul li::before {
  content: '\2013';
  position: absolute;
  left: 0;
  color: var(--sketch-red);
  font-weight: 700;
  font-family: var(--font-mono);
}

/* ============================================================
   COMPARISON TABLE
   ============================================================ */
.compare-table {
  width: 100%;
  border-collapse: collapse;
  margin: 2rem 0;
  font-size: 0.88rem;
}

.compare-table th {
  font-family: var(--font-mono);
  font-weight: 600;
  text-align: left;
  padding: 0.75rem 1rem;
  border-bottom: 2px solid var(--border-strong);
  color: var(--heading);
}

.compare-table td {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.compare-table tr:last-child td { border-bottom: none; }
.compare-table .col-stack { color: var(--sketch-red); font-weight: 500; }
.compare-table .col-heap { color: var(--sketch-orange); font-weight: 500; }
.compare-table .label-col { font-weight: 600; color: var(--text-muted); font-size: 0.82rem; white-space: nowrap; }

/* ============================================================
   CLOSING
   ============================================================ */
.closing {
  border-top: 1px solid var(--border);
  padding-top: 2rem;
}

.next-post {
  background: var(--callout-bg);
  border: 1px solid var(--accent-border);
  border-radius: 12px;
  padding: 1.5rem 2rem;
  margin: 2rem 0;
}

.next-post p:last-child { margin-bottom: 0; }

/* ============================================================
   NAV & THEME TOGGLE
   ============================================================ */
.site-nav {
  position: sticky;
  top: 0;
  background: var(--nav-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--nav-border);
  z-index: 100;
}

.site-nav .nav-inner {
  max-width: 720px;
  margin: 0 auto;
  padding: 0.85rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.back-link {
  color: var(--accent);
  text-decoration: none;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 500;
}

.theme-toggle {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.3rem 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}

.theme-toggle:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 768px) {
  html { font-size: 16px; }
  h1 { font-size: 2.2rem; }
  h2 { font-size: 1.6rem; }
  h3 { font-size: 1.15rem; }
  section { padding: 1.25rem 0 0.75rem; }
  .tradeoff { grid-template-columns: 1fr; }
  .compare-table { font-size: 0.8rem; }
  .compare-table th, .compare-table td { padding: 0.5rem 0.6rem; }
}
