/* Same paper-on-sand look as the other reading pages (see cidr/style.css),
   with a narrower column because this page is mostly prose and code. */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;600&display=swap');

:root {
    --bg-outer: #d4c5a9;
    --bg-page: #faf6ee;
    --bg-card: #fdfbf7;
    --bg-insight: #fdf8ee;
    --bg-code: #f3ece0;
    --text: #362b1f;
    --text-heading: #2c2416;
    --text-muted: #6b5b47;
    --border: #e0d5c4;
    --accent: #8b6f47;
    --accent-border: #c4a882;

    --ptr: #2f6fb0;        /* main pointer */
    --ptr-soft: #dbe8f6;
    --ptr2: #c0573f;       /* second pointer: w, the caller's cursor */
    --ptr2-soft: #f6e0d9;
    --nul: #8a7f6e;        /* the '\0' cell */
    --good: #2e7d4f;
    --bad: #b2452f;

    --page-border: #e8dfd0;
    --h-sub: #3d2e1c;          /* h2 / h3 */
    --rule-strong: #5a4630;
    --code-comment: #8a7a62;
    --cell-bg: #fff;
    --cell-border: #cfc3ae;
    --cell-dead: #b9ad98;
    --idx: #a49882;
    --btn-text: #fff;
    --btn-hover: #6d5636;
    --ghost-hover: #f1e9da;
    --shadow-page: 0 1px 3px rgba(0, 0, 0, .08), 0 8px 24px rgba(0, 0, 0, .06);
    --grain: rgba(139, 115, 85, .08);
}

/* Dark reading mode: warm low-glare palette (same family as red-black-tree's dark theme).
   Text is off-white, not pure white, and pointer colours are lifted to stay readable. */
[data-theme="dark"] {
    color-scheme: dark;
    --bg-outer: #161412;
    --bg-page: #22201d;
    --bg-card: #2a2724;
    --bg-insight: #2d2822;
    --bg-code: #2b2723;
    --text: #e0d5c4;
    --text-heading: #f0e8dc;
    --text-muted: #a89880;
    --border: #3d3631;
    --accent: #c4a882;
    --accent-border: #8b6f47;

    --ptr: #7fb2e5;
    --ptr-soft: #1f3346;
    --ptr2: #e8846c;
    --ptr2-soft: #46261f;
    --nul: #9a8e7c;
    --good: #74c492;
    --bad: #e8846c;

    --page-border: #35302b;
    --h-sub: #e8dfd0;
    --rule-strong: #e0c9a6;
    --code-comment: #a8987f;
    --cell-bg: #1d1b18;
    --cell-border: #4a423a;
    --cell-dead: #5e554b;
    --idx: #9a8d79;
    --btn-text: #1a1612;
    --btn-hover: #d4b892;
    --ghost-hover: #332d26;
    --shadow-page: 0 1px 3px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .4);
    --grain: rgba(196, 168, 130, .04);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 1rem;
    line-height: 1.7;
    color: var(--text);
    background: var(--bg-outer);
    background-image:
        radial-gradient(ellipse at 20% 50%, var(--grain) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 20%, rgba(139, 115, 85, .05) 0%, transparent 50%);
    padding: 2.5rem;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

.container {
    max-width: 820px;
    margin: 0 auto;
    background: var(--bg-page);
    padding: 2.5rem 3rem 3rem;
    border-radius: 3px;
    box-shadow: var(--shadow-page);
    border: 1px solid var(--page-border);
    position: relative;
}

/* ---------- theme toggle (same as red-black-tree) ---------- */
.theme-toggle { position: absolute; top: 1.5rem; right: 1.5rem; display: flex; gap: .25rem; }
.theme-btn {
    font: .72rem 'JetBrains Mono', monospace;
    padding: .25rem .5rem;
    border: 1px solid var(--border);
    border-radius: 2px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}
.theme-btn:hover { color: var(--text); border-color: var(--accent); }
.theme-btn.active { background: var(--accent); color: var(--bg-page); border-color: var(--accent); }

.breadcrumb { font-size: .85rem; color: var(--text-muted); margin-bottom: 1rem; }
.breadcrumb a { color: var(--accent); text-decoration: none; }
.breadcrumb a:hover { text-decoration: underline; }

h1 {
    font-size: 2.1rem;
    font-weight: 700;
    color: var(--text-heading);
    text-align: center;
    letter-spacing: -.5px;
    line-height: 1.25;
}
.subtitle {
    text-align: center;
    color: var(--text-muted);
    margin: .4rem 0 2rem;
    padding-bottom: 1.1rem;
    border-bottom: 1px solid var(--accent-border);
}

h2 {
    font-size: 1.4rem;
    font-weight: 600;
    margin: 2.6rem 0 .8rem;
    color: var(--h-sub);
    letter-spacing: -.3px;
    scroll-margin-top: 1rem;
}
h2 .num { color: var(--accent); margin-right: .35rem; }
h3 { font-size: 1.08rem; font-weight: 600; margin: 1.6rem 0 .5rem; color: var(--h-sub); }

p, ul, ol, table { margin-bottom: .9rem; }
ul, ol { padding-left: 1.4rem; }
li { margin-bottom: .3rem; }
a { color: var(--accent); }

code, .mono { font-family: 'JetBrains Mono', monospace; font-size: .88em; }
:not(pre) > code { background: var(--bg-code); padding: .05rem .3rem; border-radius: 3px; }

pre {
    background: var(--bg-code);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: .8rem 1rem;
    margin-bottom: 1rem;
    overflow-x: auto;
    line-height: 1.55;
}
pre code { font-size: .86rem; }
pre .c { color: var(--code-comment); }  /* comments */

.rule {
    padding: .7rem 1.1rem;
    background: var(--bg-insight);
    border-left: 3px solid var(--accent-border);
    margin-bottom: 1rem;
}
.rule strong { color: var(--rule-strong); }

/* ---------- table of contents ---------- */
.toc {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 1rem 1.3rem .6rem;
    margin-bottom: 1rem;
}
.toc h2 { margin: 0 0 .4rem; font-size: 1.05rem; }
.toc ol { margin: 0 0 .5rem; }
.toc .group { font-size: .85rem; color: var(--text-muted); margin: .3rem 0 .1rem; font-weight: 600; }

/* ---------- tables ---------- */
table { border-collapse: collapse; width: 100%; font-size: .93rem; }
th, td { border: 1px solid var(--border); padding: .4rem .65rem; text-align: left; vertical-align: top; }
th { background: var(--bg-card); font-weight: 600; }

/* ---------- step-through diagrams ---------- */
.stepper {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 1rem 1.2rem 1.1rem;
    margin: 0 0 1.2rem;
}
.stepper-title { font-weight: 600; font-size: .92rem; margin-bottom: .6rem; }
.stepper-title code { font-weight: 600; }

.strip { display: flex; gap: 3px; overflow-x: auto; padding-bottom: .2rem; }
.slot { display: flex; flex-direction: column; align-items: center; min-width: 2.3rem; }
.cell {
    width: 2.3rem;
    height: 2.3rem;
    display: grid;
    place-items: center;
    font: 600 1.05rem 'JetBrains Mono', monospace;
    background: var(--cell-bg);
    border: 1px solid var(--cell-border);
    border-radius: 4px;
    transition: background .2s, border-color .2s;
}
.cell.nul { color: var(--nul); font-size: .85rem; }
.cell.past { background: transparent; border-style: dashed; color: var(--nul); font-size: .7rem; }
.cell.dead { color: var(--cell-dead); }                 /* old bytes after the new '\0' */
.cell.hi { background: var(--ptr-soft); border-color: var(--ptr); }
.cell.hi2 { background: var(--ptr2-soft); border-color: var(--ptr2); }
.idx { font: .7rem 'JetBrains Mono', monospace; color: var(--idx); margin-top: .15rem; }
.ptrs { display: flex; flex-direction: column; align-items: center; min-height: 2.6rem; }
.ptr { font: 600 .78rem 'JetBrains Mono', monospace; color: var(--ptr); line-height: 1.2; white-space: nowrap; }
.ptr::before { content: '\25B2'; display: block; text-align: center; font-size: .65rem; }
.ptr.p2 { color: var(--ptr2); }

.note {
    min-height: 3.2rem;
    margin: .5rem 0 .7rem;
    font-size: .93rem;
}
.note code { background: var(--bg-code); padding: .05rem .3rem; border-radius: 3px; }

.controls { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.btn {
    font: 500 .85rem 'Inter', sans-serif;
    padding: .32rem .85rem;
    border-radius: 4px;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: var(--btn-text);
    cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--btn-hover); border-color: var(--btn-hover); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.ghost:hover:not(:disabled) { background: var(--ghost-hover); }
.count { font-size: .82rem; color: var(--text-muted); margin-left: auto; }

.footer {
    margin-top: 3rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
    font-size: .88rem;
    color: var(--text-muted);
}

@media (max-width: 640px) {
    body { padding: .6rem; font-size: .97rem; }
    .container { padding: 1.4rem 1.1rem 2rem; }
    h1 { font-size: 1.6rem; margin-top: 1.2rem; }
    .theme-toggle { top: 1rem; right: 1rem; }
    .slot, .cell { min-width: 1.9rem; width: 1.9rem; }
    .cell { height: 1.9rem; font-size: .9rem; }
}

@media (prefers-reduced-motion: reduce) {
    .cell { transition: none; }
}
