/* ---------------------------------------------------------------- tokens */
:root {
    color-scheme: light;
    --brand: #4f46e5;
    --brand-soft: #eef2ff;
    --brand-ink: #ffffff;
    --bg: #ffffff;
    --bg-sunken: #f7f8fa;
    --bg-raised: #ffffff;
    --bg-code: #f6f7f9;
    --bg-hover: rgba(15, 23, 42, 0.05);
    --bg-active: #eef2ff;
    --text: #15181d;
    --text-soft: #4b5563;
    --text-faint: #8b93a1;
    --border: #e6e8ec;
    --border-soft: #eff0f3;
    --shadow-lg: 0 24px 60px -12px rgba(15, 23, 42, 0.22), 0 0 0 1px rgba(15, 23, 42, 0.06);
    --mark: #fde68a;
    --sidebar-w: 300px;
    --outline-w: 232px;
    --topbar-h: 60px;
    --measure: 74ch;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, 'SF Mono', 'Cascadia Mono', Consolas, monospace;
}

:root[data-theme='dark'] {
    color-scheme: dark;
    --brand: #818cf8;
    --brand-soft: rgba(129, 140, 248, 0.14);
    --brand-ink: #10121a;
    --bg: #0c0e13;
    --bg-sunken: #090a0e;
    --bg-raised: #14171f;
    --bg-code: #12151c;
    --bg-hover: rgba(255, 255, 255, 0.06);
    --bg-active: rgba(129, 140, 248, 0.14);
    --text: #e8eaf0;
    --text-soft: #a8afbd;
    --text-faint: #6b7385;
    --border: #232733;
    --border-soft: #1a1e27;
    --shadow-lg: 0 24px 60px -12px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(255, 255, 255, 0.07);
    --mark: rgba(250, 204, 21, 0.3);
}

/* ------------------------------------------------------------------ base */
* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.68;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

a {
    color: var(--brand);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
    text-underline-offset: 2px;
}

svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

kbd {
    font-family: var(--font);
    font-size: 11px;
    font-weight: 600;
    line-height: 1;
    padding: 4px 6px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--bg-raised);
    color: var(--text-faint);
    white-space: nowrap;
}

:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
    border-radius: 4px;
}

/* -------------------------------------------------------------- progress */
.progress {
    position: fixed;
    inset: 0 0 auto 0;
    height: 2px;
    z-index: 60;
    pointer-events: none;
}

.progress span {
    display: block;
    height: 100%;
    width: 0;
    background: var(--brand);
    transition: width 0.12s linear;
}

/* ---------------------------------------------------------------- topbar */
.topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-bottom: 1px solid var(--border-soft);
}

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 36px;
    height: 36px;
    flex: none;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--text-soft);
    cursor: pointer;
    transition:
        background 0.14s,
        color 0.14s;
}

.icon-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.menu-btn {
    display: none;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-left: auto;
    flex: none;
}

:root[data-theme='dark'] .icon-sun {
    display: none;
}

:root:not([data-theme='dark']) .icon-moon {
    display: none;
}

.search-trigger {
    display: flex;
    align-items: center;
    gap: 9px;
    height: 36px;
    width: min(340px, 100%);
    margin-left: 24px;
    padding: 0 8px 0 11px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-sunken);
    color: var(--text-faint);
    font: inherit;
    font-size: 13.5px;
    cursor: pointer;
    transition:
        border-color 0.14s,
        background 0.14s;
}

.search-trigger:hover {
    border-color: var(--brand);
    background: var(--bg-raised);
}

.search-trigger svg {
    width: 16px;
    height: 16px;
    flex: none;
}

.search-trigger span {
    flex: 1;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---------------------------------------------------------------- layout */
.layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--outline-w);
    max-width: 1560px;
    margin: 0 auto;
    align-items: start;
}

/* --------------------------------------------------------------- sidebar */
.sidebar {
    position: sticky;
    top: var(--topbar-h);
    height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: 1px solid var(--border-soft);
    scrollbar-width: thin;
}

.sidebar-inner {
    display: flex;
    flex-direction: column;
    min-height: 100%;
    padding: 24px 14px 20px 22px;
}

.sidebar-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    padding: 0 10px 10px;
}

.sidebar-title {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--text-faint);
}

.sidebar-meta {
    font-size: 11px;
    color: var(--text-faint);
}

.chapter-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.chapter-link {
    display: flex;
    align-items: baseline;
    gap: 10px;
    padding: 7px 10px;
    border-radius: 8px;
    color: var(--text-soft);
    font-size: 14px;
    line-height: 1.45;
    transition:
        background 0.13s,
        color 0.13s;
}

.chapter-link:hover {
    background: var(--bg-hover);
    color: var(--text);
    text-decoration: none;
}

.chapter-link.active {
    background: var(--bg-active);
    color: var(--brand);
    font-weight: 600;
}

.chapter-num {
    font-variant-numeric: tabular-nums;
    font-size: 12px;
    color: var(--text-faint);
    flex: none;
    min-width: 14px;
}

.chapter-link.active .chapter-num {
    color: var(--brand);
}

/* headings of the open chapter, nested under its sidebar entry */
.chapter-subs {
    list-style: none;
    margin: 2px 0 8px;
    padding: 0 0 0 24px;
    border-left: 1px solid var(--border);
}

.chapter-subs a {
    display: block;
    padding: 4px 10px;
    border-radius: 6px;
    color: var(--text-faint);
    font-size: 13px;
    line-height: 1.4;
}

.chapter-subs a:hover {
    background: var(--bg-hover);
    color: var(--text);
    text-decoration: none;
}

.chapter-subs a.current {
    color: var(--brand);
    font-weight: 550;
}

.chapter-subs .lvl-3 a {
    padding-left: 22px;
    font-size: 12.5px;
}

.sidebar-foot {
    margin-top: auto;
    padding: 18px 10px 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 12.5px;
}

.sidebar-foot a {
    color: var(--text-faint);
}

.sidebar-foot a:hover {
    color: var(--brand);
}

/* ------------------------------------------------------------------ main */
.main {
    min-width: 0;
    padding: 40px 56px 96px;
}

.doc {
    max-width: var(--measure);
    /* long urls and identifiers must never widen the page on a phone */
    overflow-wrap: break-word;
}

.doc a {
    overflow-wrap: anywhere;
}

.skeleton {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.skeleton span {
    height: 15px;
    border-radius: 6px;
    background: linear-gradient(90deg, var(--bg-hover) 25%, var(--border-soft) 50%, var(--bg-hover) 75%);
    background-size: 400% 100%;
    animation: shimmer 1.4s ease-in-out infinite;
}

.skeleton span:first-child {
    height: 34px;
    margin-bottom: 10px;
}

@keyframes shimmer {
    to {
        background-position: -135% 0;
    }
}

.doc.fade-in {
    animation: fade 0.24s ease-out;
}

@keyframes fade {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

/* ----------------------------------------------------------------- prose */
.doc > :first-child {
    margin-top: 0;
}

.doc h1 {
    margin: 0 0 24px;
    font-size: clamp(30px, 4.2vw, 40px);
    line-height: 1.16;
    font-weight: 700;
    letter-spacing: -0.028em;
}

.doc h2 {
    margin: 52px 0 16px;
    padding-top: 18px;
    border-top: 1px solid var(--border-soft);
    font-size: 25px;
    line-height: 1.28;
    font-weight: 660;
    letter-spacing: -0.02em;
}

.doc h3 {
    margin: 36px 0 12px;
    font-size: 19px;
    font-weight: 650;
    letter-spacing: -0.012em;
}

.doc h4 {
    margin: 28px 0 10px;
    font-size: 16.5px;
    font-weight: 650;
}

.heading-anchor {
    position: relative;
    scroll-margin-top: calc(var(--topbar-h) + 22px);
}

.heading-anchor .anchor {
    position: absolute;
    left: -26px;
    top: 50%;
    translate: 0 -50%;
    padding: 0 6px;
    color: var(--text-faint);
    opacity: 0;
    font-weight: 400;
    transition: opacity 0.14s;
}

.heading-anchor:hover .anchor {
    opacity: 1;
}

.heading-anchor .anchor:hover {
    color: var(--brand);
    text-decoration: none;
}

.doc p {
    margin: 0 0 18px;
}

.doc strong {
    font-weight: 650;
    color: var(--text);
}

.doc ul,
.doc ol {
    margin: 0 0 18px;
    padding-left: 26px;
}

.doc li {
    margin: 6px 0;
}

.doc li > ul,
.doc li > ol {
    margin: 6px 0;
}

.doc blockquote {
    margin: 24px 0;
    padding: 2px 20px;
    border-left: 3px solid var(--brand);
    background: var(--brand-soft);
    border-radius: 0 10px 10px 0;
    color: var(--text-soft);
}

.doc blockquote p:first-child {
    margin-top: 14px;
}

.doc blockquote p:last-child {
    margin-bottom: 14px;
}

.doc hr {
    margin: 44px 0;
    border: 0;
    border-top: 1px solid var(--border);
}

.doc table {
    width: 100%;
    margin: 24px 0;
    border-collapse: collapse;
    font-size: 14.5px;
    display: block;
    overflow-x: auto;
}

.doc th,
.doc td {
    padding: 9px 14px;
    border: 1px solid var(--border);
    text-align: left;
}

.doc th {
    background: var(--bg-sunken);
    font-weight: 650;
}

.doc img {
    max-width: 100%;
    border-radius: 10px;
}

/* inline code */
.doc :not(pre) > code {
    padding: 0.14em 0.4em;
    border: 1px solid var(--border-soft);
    border-radius: 5px;
    background: var(--bg-code);
    font-family: var(--mono);
    font-size: 0.86em;
    color: var(--text);
    overflow-wrap: break-word;
}

/* ------------------------------------------------------------ code blocks */
.code-block {
    position: relative;
    margin: 24px 0;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-code);
    overflow: hidden;
}

.code-block > pre {
    margin: 0;
    padding: 16px 18px;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 13.2px;
    line-height: 1.62;
    tab-size: 4;
    scrollbar-width: thin;
}

.code-block > pre code {
    font-family: inherit;
    background: none;
    padding: 0;
    border: 0;
}

/* ASCII diagrams (indented blocks with no language) read as figures */
.code-block.is-diagram {
    background: var(--bg-sunken);
    border-style: dashed;
}

.code-block.is-diagram > pre {
    font-size: 12.6px;
    line-height: 1.5;
    letter-spacing: 0.01em;
    color: var(--text-soft);
}

.code-lang {
    position: absolute;
    top: 8px;
    right: 44px;
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-faint);
    pointer-events: none;
}

.copy-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border: 1px solid transparent;
    border-radius: 7px;
    background: color-mix(in srgb, var(--bg-raised) 80%, transparent);
    color: var(--text-faint);
    cursor: pointer;
    opacity: 0;
    transition:
        opacity 0.14s,
        color 0.14s,
        border-color 0.14s;
}

.code-block:hover .copy-btn,
.copy-btn:focus-visible {
    opacity: 1;
}

.copy-btn:hover {
    color: var(--text);
    border-color: var(--border);
}

.copy-btn svg {
    width: 15px;
    height: 15px;
}

.copy-btn.done {
    color: #10b981;
    opacity: 1;
}

.copy-btn .check {
    display: none;
}

.copy-btn.done .check {
    display: block;
}

.copy-btn.done .clip {
    display: none;
}

/* ----------------------------------------------------------- highlighting */
.hljs-comment,
.hljs-quote {
    color: var(--text-faint);
    font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-type {
    color: #c026d3;
}

.hljs-string,
.hljs-regexp,
.hljs-addition {
    color: #0d9488;
}

.hljs-number,
.hljs-symbol {
    color: #c2410c;
}

.hljs-title,
.hljs-title.function_,
.hljs-section {
    color: #2563eb;
}

.hljs-attr,
.hljs-variable,
.hljs-template-variable,
.hljs-property {
    color: #b45309;
}

.hljs-built_in,
.hljs-class .hljs-title {
    color: #7c3aed;
}

.hljs-params {
    color: var(--text);
}

:root[data-theme='dark'] .hljs-keyword,
:root[data-theme='dark'] .hljs-selector-tag,
:root[data-theme='dark'] .hljs-literal,
:root[data-theme='dark'] .hljs-type {
    color: #f0abfc;
}

:root[data-theme='dark'] .hljs-string,
:root[data-theme='dark'] .hljs-regexp,
:root[data-theme='dark'] .hljs-addition {
    color: #5eead4;
}

:root[data-theme='dark'] .hljs-number,
:root[data-theme='dark'] .hljs-symbol {
    color: #fdba74;
}

:root[data-theme='dark'] .hljs-title,
:root[data-theme='dark'] .hljs-title.function_,
:root[data-theme='dark'] .hljs-section {
    color: #93c5fd;
}

:root[data-theme='dark'] .hljs-attr,
:root[data-theme='dark'] .hljs-variable,
:root[data-theme='dark'] .hljs-template-variable,
:root[data-theme='dark'] .hljs-property {
    color: #fcd34d;
}

:root[data-theme='dark'] .hljs-built_in,
:root[data-theme='dark'] .hljs-class .hljs-title {
    color: #c4b5fd;
}

/* ----------------------------------------------------------------- pager */
.pager {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    max-width: var(--measure);
    margin-top: 64px;
}

.pager a {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 14px 18px;
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    transition:
        border-color 0.14s,
        background 0.14s,
        transform 0.14s;
}

.pager a:hover {
    border-color: var(--brand);
    background: var(--bg-sunken);
    text-decoration: none;
    transform: translateY(-1px);
}

.pager .dir {
    font-size: 11.5px;
    font-weight: 650;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-faint);
}

.pager .name {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.pager .next {
    grid-column: 2;
    text-align: right;
    align-items: flex-end;
}

.doc-foot {
    max-width: var(--measure);
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid var(--border-soft);
    font-size: 12.5px;
    color: var(--text-faint);
}

/* --------------------------------------------------------------- outline */
.outline {
    position: sticky;
    top: var(--topbar-h);
    max-height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
}

.outline-inner {
    padding: 40px 24px 40px 8px;
}

.outline-title {
    display: block;
    margin-bottom: 10px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--text-faint);
}

.outline ul {
    list-style: none;
    margin: 0;
    padding: 0;
    border-left: 1px solid var(--border);
}

.outline li a {
    display: block;
    padding: 5px 12px;
    margin-left: -1px;
    border-left: 2px solid transparent;
    color: var(--text-faint);
    font-size: 13px;
    line-height: 1.45;
    transition:
        color 0.13s,
        border-color 0.13s;
}

.outline li a:hover {
    color: var(--text);
    text-decoration: none;
}

.outline li a.current {
    color: var(--brand);
    border-left-color: var(--brand);
    font-weight: 550;
}

.outline li.lvl-3 a {
    padding-left: 26px;
    font-size: 12.5px;
}

/* --------------------------------------------------------- search palette */
.palette {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: start center;
    padding: max(8vh, 40px) 16px 16px;
}

/* the class sets display, so it has to opt back out of [hidden] itself */
.palette[hidden] {
    display: none;
}

.palette-scrim {
    position: absolute;
    inset: 0;
    background: rgba(8, 10, 15, 0.5);
    backdrop-filter: blur(3px);
    animation: fade 0.16s ease-out;
}

.palette-box {
    position: relative;
    width: min(680px, 100%);
    max-height: min(620px, 78vh);
    display: flex;
    flex-direction: column;
    background: var(--bg-raised);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    animation: pop 0.18s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes pop {
    from {
        opacity: 0;
        transform: translateY(-10px) scale(0.985);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

.palette-input {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 0 14px;
    border-bottom: 1px solid var(--border-soft);
    flex: none;
}

.palette-input svg {
    width: 18px;
    height: 18px;
    color: var(--text-faint);
    flex: none;
}

.palette-input input {
    flex: 1;
    height: 56px;
    border: 0;
    background: none;
    color: var(--text);
    font: inherit;
    font-size: 16px;
    outline: none;
}

.palette-input input::-webkit-search-cancel-button {
    display: none;
}

.palette-results {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    scrollbar-width: thin;
}

.palette-empty {
    padding: 40px 20px;
    text-align: center;
    color: var(--text-faint);
    font-size: 14px;
}

.result {
    display: block;
    padding: 10px 12px;
    border-radius: 9px;
    color: var(--text);
    cursor: pointer;
}

.result:hover,
.result.sel {
    background: var(--bg-hover);
    text-decoration: none;
}

.result.sel {
    background: var(--bg-active);
}

.result-top {
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.result-title {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.result-where {
    margin-left: auto;
    font-size: 11.5px;
    color: var(--text-faint);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 45%;
}

.result-snip {
    margin-top: 3px;
    font-size: 12.8px;
    line-height: 1.5;
    color: var(--text-soft);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.result mark {
    background: var(--mark);
    color: inherit;
    border-radius: 3px;
    padding: 0 1px;
}

.palette-foot {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 9px 14px;
    border-top: 1px solid var(--border-soft);
    font-size: 11.5px;
    color: var(--text-faint);
    flex: none;
}

.palette-foot kbd {
    margin-right: 3px;
}

.palette-foot span:last-child {
    margin-left: auto;
}

/* ----------------------------------------------------------------- scrim */
.scrim {
    position: fixed;
    inset: var(--topbar-h) 0 0;
    z-index: 39;
    background: rgba(8, 10, 15, 0.45);
}

/* ------------------------------------------------------------ responsive */
/* wide enough for the right-hand outline: don't repeat it inside the sidebar */
@media (min-width: 1241px) {
    .chapter-subs {
        display: none;
    }
}

@media (max-width: 1240px) {
    .layout {
        grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    }
    .outline {
        display: none;
    }
    .main {
        padding: 40px 44px 96px;
    }
}

@media (max-width: 900px) {
    .layout {
        grid-template-columns: minmax(0, 1fr);
    }
    .menu-btn {
        display: inline-grid;
    }
    .search-trigger {
        width: 36px;
        margin-left: 4px;
        padding: 0;
        justify-content: center;
    }
    .search-trigger span,
    .search-trigger kbd {
        display: none;
    }

    .sidebar {
        position: fixed;
        z-index: 40;
        top: var(--topbar-h);
        left: 0;
        width: min(320px, 86vw);
        background: var(--bg);
        border-right: 1px solid var(--border);
        translate: -102% 0;
        transition: translate 0.24s cubic-bezier(0.32, 0.72, 0, 1);
        box-shadow: var(--shadow-lg);
    }
    .sidebar.open {
        translate: 0 0;
    }

    .main {
        padding: 28px 20px 80px;
    }
    .doc h2 {
        margin-top: 40px;
        font-size: 22px;
    }
    .pager {
        grid-template-columns: 1fr;
    }
    .pager .next {
        grid-column: 1;
        text-align: left;
        align-items: flex-start;
    }
    .heading-anchor .anchor {
        display: none;
    }
    .palette {
        padding: 0;
    }
    .palette-box {
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }
}

@media print {
    .topbar,
    .sidebar,
    .outline,
    .pager,
    .progress,
    .palette,
    .copy-btn {
        display: none !important;
    }
    .layout {
        display: block;
    }
    .main {
        padding: 0;
    }
    .doc {
        max-width: none;
    }
}
