/* ============================================================
   Components CSS: Buttons, cards, labs, modals, tables
   ============================================================ */

/* ============================================================
   Channel diagram SVG
   ============================================================ */
#lab-channel svg .node {
    fill: none;
    stroke: var(--stroke);
    stroke-width: 1.5;
}

#lab-channel svg .label {
    fill: var(--fg);
    font-family: var(--sans);
    font-size: 13px;
}

#lab-channel svg .edge {
    stroke: var(--fg);
    stroke-width: 1.2;
    stroke-opacity: 0.35;
    fill: none;
}

#lab-channel svg .node-accent {
    stroke: var(--accent);
    stroke-opacity: 0.4;
}

#lab-channel svg .node-warn {
    stroke: var(--warn);
    stroke-opacity: 0.5;
}

#lab-channel svg .node-accent3 {
    stroke: var(--accent3);
    stroke-opacity: 0.4;
}

#lab-channel svg .label-warn {
    fill: var(--warn);
}

#lab-channel svg .label-accent3 {
    fill: var(--accent3);
}

#lab-channel svg .edge-accent3 {
    stroke: var(--accent3);
    stroke-opacity: 0.4;
}

/* ============================================================
   Comparison table
   ============================================================ */
.cmp-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    margin: 32px 0;
}

.cmp-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 14px 18px;
    background: var(--bg1);
    color: var(--muted);
    font-weight: 700;
    font-family: var(--sans);
    text-align: left;
    border-bottom: 2px solid var(--stroke);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.12em;
}

.cmp-table td {
    padding: 14px 18px;
    border-bottom: 1px solid var(--stroke);
    line-height: 1.72;
}

.cmp-table td:first-child {
    font-weight: 600;
    color: var(--text);
}

.cmp-table tr:last-child td {
    border-bottom: none;
}

.cmp-table tbody tr {
    transition: background 0.2s var(--ease);
}

.cmp-table tbody tr:hover {
    background: rgba(99, 167, 255, 0.06);
}

[data-theme="light"] .cmp-table tbody tr:hover {
    background: rgba(47, 111, 178, 0.06);
}

/* ============================================================
   Permalink anchors
   ============================================================ */
.heading-link {
    opacity: 0;
    margin-inline-start: 0.4em;
    font-size: 0.7em;
    color: var(--accent);
    text-decoration: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    cursor: pointer;
    user-select: none;
    display: inline-block;
}

h2:hover .heading-link,
h3:hover .heading-link,
.heading-link:focus-visible {
    opacity: 0.8;
}

.heading-link:hover {
    transform: scale(1.1);
    opacity: 1;
}

.permalink-toast {
    position: fixed;
    bottom: 28px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--card3);
    color: var(--fg);
    border: 1px solid var(--stroke2);
    padding: 12px 24px;
    border-radius: var(--radius);
    font-size: 14px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s var(--ease-out);
    z-index: 9999;
    backdrop-filter: blur(12px);
    box-shadow: var(--shadow2);
}

.permalink-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ============================================================
   Lab lazy fade-in
   ============================================================ */
.lab {
    opacity: 0;
    transform: translateY(16px);
    transition: opacity 0.5s var(--ease-out), transform 0.5s var(--ease-out);
}

.lab.lab-visible {
    opacity: 1;
    transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
    .lab {
        opacity: 1;
        transform: none;
        transition: none;
    }
}

/* ============================================================
   Canonical diagram — failure overlays + zoom
   ============================================================ */
.sys-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

.sys-controls button {
    font-size: 12px;
    padding: 6px 12px;
    border-radius: var(--radius-xs);
    border: 1px solid var(--stroke);
    background: var(--card);
    color: var(--fg);
    cursor: pointer;
    transition: all 0.2s var(--ease);
    font-weight: 500;
}

.sys-controls button.danger {
    border-color: var(--danger);
    color: var(--danger);
}

.sys-controls button.active {
    background: rgba(255, 95, 109, 0.12);
    border-color: rgba(255, 95, 109, 0.5);
    color: var(--danger);
}

.sys-controls button:hover {
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

#sysMap .failure-surface {
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

#sysMap.show-failures .failure-surface {
    opacity: 1;
}

#sysMap {
    display: block;
    width: 100%;
    height: auto;
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    background: linear-gradient(180deg, rgba(8, 14, 20, 0.34), rgba(8, 14, 20, 0.12));
}

#sysMap .edge {
    fill: none;
    stroke: var(--faint);
    stroke-width: 1.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-opacity: 0.55;
    transition: stroke 0.2s ease, stroke-opacity 0.2s ease;
}

#sysMap .node {
    fill: var(--card2);
    stroke: var(--stroke2);
    stroke-width: 1.5;
    transition: stroke 0.2s ease, fill 0.2s ease, stroke-width 0.2s ease;
}

#sysMap .label {
    fill: var(--text);
    font-family: var(--sans);
    font-size: 13px;
    font-weight: 600;
}

#sysMap .label-sub {
    fill: var(--muted);
    font-size: 11px;
    font-weight: 500;
}

#sysMap .node-hot {
    stroke: var(--accent);
    stroke-width: 2;
    fill: rgba(99, 167, 255, 0.12);
}

#sysMap .edge-hot {
    stroke: var(--accent);
    stroke-opacity: 0.95;
}

[data-theme="light"] #sysMap {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(248, 244, 237, 0.96));
    border-color: rgba(145, 127, 102, 0.38);
}

[data-theme="light"] #sysMap .edge {
    stroke: rgba(81, 98, 109, 0.72);
}

[data-theme="light"] #sysMap .node {
    fill: rgba(255, 255, 255, 0.92);
    stroke: rgba(145, 127, 102, 0.45);
}

[data-theme="light"] #sysMap .node-hot {
    fill: rgba(47, 111, 178, 0.12);
}

.sys-zoom-wrap {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius);
}

.sys-zoom-wrap svg {
    transition: transform 0.3s var(--ease);
    transform-origin: center center;
}

/* ============================================================
   Threat model cards
   ============================================================ */
.threat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 20px;
    margin-top: 24px;
}

.threat-card {
    border: 1px solid var(--stroke);
    border-left: 3px solid rgba(255, 95, 109, 0.3);
    border-radius: var(--radius);
    padding: 26px 28px;
    background: var(--card);
    cursor: pointer;
    transition: all 0.3s var(--ease);
}

.threat-card:hover {
    border-color: rgba(255, 95, 109, 0.4);
    box-shadow: 0 12px 32px rgba(255, 95, 109, 0.08);
    transform: translateY(-3px);
}

[data-theme="light"] .threat-card {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(0, 0, 0, 0.12);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

[data-theme="light"] .threat-card:hover {
    box-shadow: 0 8px 24px rgba(255, 95, 109, 0.12);
}

.threat-card .threat-name {
    font-weight: 700;
    font-size: 16px;
    margin-bottom: 12px;
    color: var(--fg);
}

.threat-card .threat-row {
    font-size: 14px;
    line-height: 1.7;
    color: var(--muted);
}

.threat-card .threat-row strong {
    color: var(--fg);
    font-weight: 600;
}

/* ============================================================
   Schema code block
   ============================================================ */
.schema-block {
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    padding: 28px 32px;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 14px;
    line-height: 1.8;
    color: var(--fg);
    margin: 32px 0;
    position: relative;
}

.schema-block.warn-left {
    border-left: 3px solid var(--warn);
}

.schema-block.danger-left {
    border-left: 3px solid var(--danger);
}

.schema-block.ok-left {
    border-left: 3px solid var(--success);
}

/* Schema-block variant for quoted prose (not code) */
.schema-quote p {
    font-family: var(--font-body, inherit);
    font-size: 15px;
    line-height: 1.7;
    color: var(--text);
    font-style: italic;
    margin: 0;
    letter-spacing: 0.01em;
}

[data-theme="light"] .schema-block {
    background: rgba(248, 249, 250, 0.95);
    border-color: rgba(0, 0, 0, 0.15);
}

.schema-block::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, var(--accent), var(--accent2), transparent);
    opacity: 0.3;
}

.schema-block .sk {
    color: var(--accent);
}

.schema-block .sv {
    color: var(--accent3);
}

.schema-block .sc {
    color: var(--muted);
    font-style: italic;
}

.schema-block .sd {
    color: var(--accent2);
}

.schema-block .se {
    color: var(--warn);
}

/* ============================================================
   Scale scenario slider
   ============================================================ */
.scale-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
    margin-top: 24px;
}

.scale-card {
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    padding: 20px;
    text-align: center;
    transition: all 0.3s var(--ease);
    background: var(--card);
}

.scale-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

[data-theme="light"] .scale-card {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(0, 0, 0, 0.12);
}

.scale-card.active {
    border-color: var(--accent);
    background: rgba(94, 231, 223, 0.06);
    box-shadow: 0 0 30px var(--accent-glow);
}

.scale-card .sc-val {
    font-size: 26px;
    font-weight: 800;
    color: var(--fg);
    letter-spacing: -0.02em;
}

.scale-card .sc-label {
    font-size: 12px;
    color: var(--muted);
    margin-top: 4px;
    font-weight: 500;
}

/* ============================================================
   Chunk visualiser
   ============================================================ */
.chunk-viz {
    font-family: var(--mono);
    font-size: 14px;
    line-height: 1.9;
    padding: 24px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    background: var(--card);
    overflow-x: auto;
    margin-top: 20px;
}

.chunk-viz .chunk-span {
    border-bottom: 2px solid var(--accent);
    padding-bottom: 2px;
    transition: background 0.2s ease;
}

.chunk-viz .chunk-span:nth-child(even) {
    border-color: var(--accent2);
}

.chunk-viz .chunk-break {
    display: inline-block;
    width: 3px;
    height: 1.3em;
    background: var(--warn);
    margin: 0 2px;
    vertical-align: middle;
    opacity: 0.8;
    border-radius: 2px;
}

.chunk-viz .chunk-fracture {
    background: rgba(255, 95, 109, 0.15);
    border-radius: 4px;
    padding: 0 4px;
}

[data-theme="light"] .chunk-viz {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(0, 0, 0, 0.12);
}

[data-theme="light"] .chunk-viz .chunk-fracture {
    background: rgba(220, 38, 38, 0.12);
}

/* ============================================================
   Memory hierarchy matrix
   ============================================================ */
.mem-matrix {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    margin-top: 24px;
}

.mem-matrix th {
    padding: 14px 16px;
    font-weight: 700;
    text-align: center;
    color: var(--muted);
    border-bottom: 2px solid var(--stroke);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.mem-matrix th:first-child {
    text-align: left;
}

.mem-matrix td {
    padding: 12px 16px;
    text-align: center;
    border-bottom: 1px solid var(--stroke);
    transition: background 0.2s ease;
}

.mem-matrix tbody tr:hover td {
    background: rgba(94, 231, 223, 0.03);
}

[data-theme="light"] .mem-matrix tbody tr:hover td {
    background: rgba(8, 126, 164, 0.06);
}

.mem-matrix td:first-child {
    text-align: left;
    font-weight: 600;
}

.mem-matrix .dot-high {
    color: var(--accent3);
}

.mem-matrix .dot-med {
    color: var(--warn);
}

.mem-matrix .dot-low {
    color: var(--danger);
}

/* ============================================================
   Info theory equations
   ============================================================ */
/* ── Formal definition cards ── */
.eq-block {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    padding: 28px 32px 24px;
    overflow-x: auto;
    margin: 20px 0;
    position: relative;
}

[data-theme="light"] .eq-block {
    background: rgba(248, 249, 250, 0.95);
    border-color: rgba(0, 0, 0, 0.15);
}

.eq-block::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: linear-gradient(180deg, var(--accent), var(--accent2));
    border-radius: var(--radius) 0 0 var(--radius);
}

.eq-block .eq-label {
    color: var(--text);
    font-size: 14px;
    font-family: var(--sans);
    font-weight: 700;
    display: block;
    margin-bottom: 16px;
    letter-spacing: 0.02em;
}

.eq-block .eq-formula {
    font-family: var(--mono);
    font-size: 17px;
    line-height: 1.7;
    color: var(--text);
    padding: 16px 20px;
    background: rgba(0, 0, 0, 0.25);
    border-radius: var(--radius-sm);
    border: 1px solid rgba(255, 255, 255, 0.04);
    margin-bottom: 14px;
}

[data-theme="light"] .eq-block .eq-formula {
    background: rgba(0, 0, 0, 0.04);
    border-color: rgba(0, 0, 0, 0.08);
}

.eq-block .eq-where {
    font-family: var(--serif);
    font-size: 14px;
    line-height: 1.8;
    color: var(--muted);
    padding-left: 20px;
    border-left: 1px solid var(--stroke);
}

.eq-block .eq-note {
    font-family: var(--serif);
    font-size: 13.5px;
    line-height: 1.7;
    color: var(--muted);
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--stroke);
}

.eq-block .eq-note strong {
    color: var(--text);
    font-weight: 600;
}

.eq-block .eq-var {
    color: var(--accent);
    font-style: italic;
    font-family: var(--mono);
}

.eq-block .eq-where .eq-var,
.eq-block .eq-note .eq-var {
    font-size: 13px;
}

/* ============================================================
   TOC - Editorial rail
   ============================================================ */
.toc {
    align-self: auto;
    padding: 22px;
    border-radius: 0;
    border: 1px solid var(--stroke);
    background: var(--card2);
    box-shadow: var(--shadow);
}

[data-theme="light"] .toc {
    background: rgba(255, 255, 255, 0.86);
    border-color: rgba(145, 127, 102, 0.34);
}

.toc-close {
    cursor: pointer;
    border: 1px solid var(--stroke);
    background: var(--card);
    color: var(--muted);
    padding: 7px 10px;
    border-radius: var(--radius-xs);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.toc h4 {
    margin: 0 0 16px;
    font-family: var(--sans);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.16em;
    color: var(--muted);
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.toc .mini {
    font-family: var(--mono);
    font-size: 10px;
    color: var(--faint);
    border: 1px solid var(--stroke);
    padding: 4px 8px;
    border-radius: 999px;
    background: var(--card);
    white-space: nowrap;
}

.toc a {
    display: block;
    padding: 10px 12px;
    border-radius: var(--radius-xs);
    font-size: 13px;
    color: var(--muted);
    border-left: 2px solid transparent;
    line-height: 1.4;
    transition: background 0.2s var(--ease), color 0.2s var(--ease), border-color 0.2s var(--ease);
    margin: 3px 0;
}

.toc a:hover {
    background: var(--card);
    color: var(--text);
    border-left-color: var(--stroke2);
}

.toc a.active {
    background: rgba(99, 167, 255, 0.08);
    color: var(--text);
    border-left-color: var(--accent);
    font-weight: 600;
}

.toc a.is-read:not(.active) {
    color: var(--faint);
    border-left-color: rgba(120, 181, 135, 0.45);
}

[data-theme="light"] .toc a.active {
    background: rgba(47, 111, 178, 0.08);
}

.toc .divider {
    height: 1px;
    background: var(--stroke);
    margin: 16px 0;
}

.toc .toc-foot {
    font-size: 12px;
    color: var(--faint);
    line-height: 1.65;
    padding: 2px 2px 0;
}

.toc .toc-foot .row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
}

/* ============================================================
   Chapters / prose - Enhanced spacing
   ============================================================ */
article {
    min-width: 0;
    font-size: 18px;
}

/* Consistent chapter rails: narrative on 2/3, dense data wider, labs widest. */
section.chapter {
    --rail-narrative: min(100%, 1040px);
    --rail-data: min(100%, 1280px);
    --rail-lab: min(100%, 1320px);
}

section.chapter>.chapter-head,
section.chapter>p,
section.chapter>.subhead,
section.chapter>h3,
section.chapter>h4,
section.chapter>ul,
section.chapter>ol,
section.chapter>.callout,
section.chapter>.ref-list,
section.chapter>.hr {
    max-width: var(--rail-narrative);
    margin-left: auto;
    margin-right: auto;
}

/* Data-dense components: tables, code, formulas, splits — use more of the available width */
section.chapter>.cmp-table,
section.chapter>.mem-matrix,
section.chapter>.threat-grid,
section.chapter>.scroll-x,
section.chapter>.schema-block,
section.chapter>.eq-block,
section.chapter>.split,
section.chapter>.editor-quote {
    max-width: var(--rail-data);
    margin-left: auto;
    margin-right: auto;
}

section.chapter>.lab {
    max-width: var(--rail-narrative);
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 8px;
    transition: opacity 0.5s var(--ease-out), transform 0.5s var(--ease-out), max-width 0.35s var(--ease);
}

section.chapter>.lab.lab-open {
    max-width: var(--rail-lab);
}

section.chapter>.lab+h3,
section.chapter>.lab+p,
section.chapter>.lab+.callout {
    margin-top: 48px;
}

section.chapter>.lab-pair+h3,
section.chapter>.lab-pair+p,
section.chapter>.lab-pair+.callout {
    margin-top: 48px;
}

section.chapter>.lab-pair {
    max-width: var(--rail-narrative);
    margin-left: auto;
    margin-right: auto;
    transition: opacity 0.5s var(--ease-out), transform 0.5s var(--ease-out), max-width 0.35s var(--ease);
}

section.chapter>.lab-pair:has(.lab-open) {
    max-width: var(--rail-lab);
}

section.chapter>.callout {
    margin-top: 32px;
    margin-bottom: 32px;
}

section.chapter>.callout+.callout {
    margin-top: 20px;
}

section.chapter>.hint {
    margin-top: 20px;
    max-width: var(--rail-narrative);
    margin-left: auto;
    margin-right: auto;
}

/* Reference list styling */
.ref-list p {
    padding: 16px 0;
    border-bottom: 1px solid var(--stroke);
    font-size: 14.5px;
    line-height: 1.72;
}

.ref-list p:first-child {
    padding-top: 0;
}

.ref-list p:last-child {
    border-bottom: none;
}

.ref-list p strong {
    color: var(--text);
}

.ref-list p em {
    color: var(--accent);
    font-style: normal;
    font-size: 13.5px;
}

.lab-pair {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px;
    align-items: start;
    margin-top: 36px;
}

.lab-pair>.lab {
    margin-top: 0;
    max-width: none;
}

.lab-pair .lab-grid {
    grid-template-columns: 1fr;
}

.lab-pair .controls {
    border-right: none;
    border-bottom: 1px solid var(--stroke);
}

section.chapter {
    margin: 0;
    padding: 64px 0 56px;
    border-radius: 0;
    border: none;
    background: transparent;
    box-shadow: none;
    position: relative;
    overflow: visible;
}

section.chapter:first-of-type {
    padding-top: 38px;
}

@media (max-width: 1024px) {
    section.chapter {
        padding: 52px 0 44px;
    }

    section.chapter:first-of-type {
        padding-top: 26px;
    }

    .lab-pair {
        grid-template-columns: 1fr;
        gap: 26px;
    }
}

@media (max-width: 768px) {
    section.chapter {
        padding: 46px 0 38px;
    }
}

[data-theme="light"] section.chapter {
    background: transparent;
}

/* Chapter separators */
section.chapter+section.chapter::after {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    left: calc(50% - 60px);
    width: 120px;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--accent), transparent);
    opacity: 0.35;
}

section.chapter:first-of-type::after {
    display: none;
}

section.chapter:last-of-type::after {
    display: block;
}

section.chapter.in-view {
    transform: translateY(0);
    opacity: 1;
}

section.chapter::before {
    content: "";
    position: absolute;
    inset: -1px;
    background: none;
    opacity: 0;
    pointer-events: none;
    z-index: 0;
}

section.chapter>* {
    position: relative;
    z-index: 1;
}

.chapter-head {
    display: block;
    position: relative;
    margin-bottom: 40px;
    padding-left: 0;
}

@media (max-width: 768px) {
    .chapter-head {
        margin-bottom: 34px;
    }
}

.chap-num {
    font-family: var(--mono);
    font-size: clamp(66px, 8vw, 108px);
    font-weight: 900;
    letter-spacing: -0.04em;
    color: var(--accent);
    opacity: 0.18;
    position: absolute;
    top: -34px;
    left: -8px;
    line-height: 1;
    border: none;
    background: none;
    padding: 0;
    user-select: none;
    pointer-events: none;
    z-index: 0;
    transition: transform 0.8s var(--ease-out), opacity 0.8s ease;
}

.chap-num.chap-num-ref {
    font-size: clamp(56px, 7vw, 80px);
    opacity: 0.04;
}

[data-theme="light"] .chap-num {
    background: none;
    opacity: 0.04;
}

.chapter h2 {
    margin: 0;
    font-family: var(--display);
    letter-spacing: -0.015em;
    line-height: 1.05;
    font-size: clamp(30px, 3vw, 46px);
    position: relative;
    z-index: 1;
    font-weight: 760;
}

.subhead {
    margin: 20px 0 0;
    color: var(--muted);
    font-size: 1.05rem;
    max-width: 80ch;
    line-height: 1.76;
    position: relative;
    z-index: 1;
}

@media (max-width: 768px) {
    .subhead {
        margin: 20px 0 0;
        font-size: 16px;
    }
}

/* Section subheadings within chapters */
.chapter h3 {
    font-family: var(--display);
    font-size: clamp(22px, 1.8vw, 30px);
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.3;
    margin: 64px 0 28px;
    color: var(--text);
}

.chapter h3.mt-28 {
    margin-top: 34px;
}

.chapter h3.mt-32 {
    margin-top: 42px;
}

.chapter h3:first-of-type {
    margin-top: 0;
}

@media (max-width: 768px) {
    .chapter h3 {
        margin: 42px 0 20px;
    }
}

/* Editorial callout styles */
.callout {
    border: 1px solid var(--stroke);
    background: var(--card);
    border-radius: var(--radius);
    border-left-width: 4px;
    border-left-color: var(--accent5);
    padding: 24px 26px 22px;
    margin: 34px 0;
    box-shadow: 0 8px 18px rgba(2, 8, 14, 0.18);
    position: relative;
    overflow: hidden;
}

@media (max-width: 768px) {
    .callout {
        padding: 20px 20px 18px;
        margin: 28px 0;
    }
}

.callout.mt-22 {
    margin-top: 28px;
}

.callout.mt-16 {
    margin-top: 20px;
}

.callout.mt-18 {
    margin-top: 24px;
}

.callout.mb-22 {
    margin-bottom: 28px;
}

.callout.mb-16 {
    margin-bottom: 20px;
}

[data-theme="light"] .callout {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(0, 0, 0, 0.12);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.callout .tag {
    font-family: var(--sans);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.16em;
    color: var(--faint);
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
}

.callout .tag .dot {
    width: 7px;
    height: 7px;
    border-radius: 99px;
    background: var(--accent5);
    flex: 0 0 auto;
}

.callout p {
    margin: 0;
    color: var(--muted);
    font-size: 0.97rem;
    line-height: 1.74;
}

.callout p.mt-16 {
    margin-top: 20px;
}

.editor-quote {
    font-family: var(--serif);
    font-size: 22px;
    color: var(--text);
    line-height: 1.75;
    margin: 52px 0;
    padding: 36px 40px;
    border-left: 3px solid var(--accent);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    opacity: 0.95;
    background: linear-gradient(135deg, rgba(94, 231, 223, 0.06), rgba(94, 231, 223, 0.02) 40%, transparent);
}

@media (max-width: 768px) {
    .editor-quote {
        font-size: 19px;
        margin: 36px 0;
        padding: 24px 28px;
    }
}

.callout.small {
    padding: 14px 16px;
}

.callout.danger {
    border-color: rgba(216, 107, 107, 0.45);
    border-left-color: var(--danger);
    background: rgba(216, 107, 107, 0.08);
}

.callout.warn {
    border-color: rgba(217, 142, 95, 0.42);
    border-left-color: var(--warn);
    background: rgba(217, 142, 95, 0.08);
}

.callout.ok {
    border-color: rgba(120, 181, 135, 0.4);
    border-left-color: var(--success);
    background: rgba(120, 181, 135, 0.07);
}

.callout.accent2 {
    border-color: rgba(180, 144, 202, 0.35);
}

.dot-accent2 {
    background: var(--accent2);
}

/* Split layouts */
.split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 40px;
    margin-top: 40px;
}

.split.split-tight {
    margin-top: 32px;
}

@media (max-width: 900px) {
    .split {
        grid-template-columns: 1fr;
        gap: 28px;
        margin-top: 32px;
    }
}

@media (max-width: 768px) {
    .split {
        gap: 24px;
        margin-top: 28px;
    }
}

.hr {
    height: 1px;
    background: var(--stroke);
    margin: 48px 0;
    position: relative;
    overflow: hidden;
}

@media (max-width: 768px) {
    .hr {
        margin: 36px 0;
    }
}

.hr::after {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 60%;
    height: 100%;
    background: linear-gradient(90deg, transparent, var(--accent), var(--accent2), transparent);
    opacity: 0;
    transition: opacity 0.4s ease;
}

.hr.hr-animate::after {
    opacity: 0.6;
    animation: hrStream 2.5s var(--ease) forwards;
}

@keyframes hrStream {
    0% {
        left: -60%;
    }

    100% {
        left: 100%;
    }
}

/* ============================================================
   Lab blocks - Systems console
   ============================================================ */
.lab {
    --lab-shell-max: min(calc(100svh - 88px), 1200px);
    --lab-head-height: 88px;
    margin-top: 42px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    background: linear-gradient(180deg, var(--bg1), var(--bg2));
    box-shadow: var(--shadow);
    overflow: hidden;
    position: relative;
    display: flex;
    flex-direction: column;
    max-height: var(--lab-shell-max);
}

@media (max-width: 768px) {
    .lab {
        margin-top: 34px;
    }
}

.lab::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, var(--stroke2), transparent);
    opacity: 0.8;
}

[data-theme="light"] .lab {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(252, 249, 244, 0.96));
    border-color: rgba(145, 127, 102, 0.34);
}

.lab-head {
    padding: 24px 28px 20px;
    border-bottom: 1px solid var(--stroke);
    background: var(--card2);
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 52px;
    transition: background 0.2s var(--ease);
    flex: 0 0 auto;
}

@media (max-width: 768px) {
    .lab-head {
        padding: 18px 18px 14px;
        padding-right: 42px;
    }
}

.lab-head:hover {
    background: var(--card3);
}

/* Chevron indicator */
.lab-head::after {
    content: "";
    position: absolute;
    right: 18px;
    top: 50%;
    width: 8px;
    height: 8px;
    border-right: 1.5px solid var(--faint);
    border-bottom: 1.5px solid var(--faint);
    transform: translateY(-60%) rotate(-45deg);
    transition: transform 0.4s var(--ease-out), border-color 0.2s ease;
}

.lab-head:hover::after {
    border-color: var(--accent);
}

.lab.lab-open .lab-head::after {
    transform: translateY(-30%) rotate(45deg);
}

.lab.lab-open .lab-head {
    border-bottom-color: var(--stroke2);
}

/* Collapsed/open model without max-height animation artifacts. */
.lab.lab-ready>*:not(.lab-head) {
    display: none;
    pointer-events: none;
}

.lab.lab-ready.lab-open>*:not(.lab-head) {
    display: block;
    pointer-events: auto;
    flex: 0 1 auto;
    min-height: 0;
    max-height: calc(var(--lab-shell-max) - var(--lab-head-height));
    overflow: auto;
}

/* Preserve grid display for containers that need it */
.lab.lab-ready.lab-open>.failuregrid {
    display: grid;
}

.lab.lab-ready.lab-open>.lab-grid {
    display: grid;
    overflow: hidden;
}

/* Let each column scroll independently so controls-heavy labs
   don't push the viz off-screen when the user scrolls. */
.lab.lab-ready.lab-open>.lab-grid>.controls,
.lab.lab-ready.lab-open>.lab-grid>.viz {
    max-height: calc(var(--lab-shell-max) - var(--lab-head-height));
    overflow-y: auto;
}

.lab .viz>.callout,
.lab .viz>.schema-block,
.lab .viz>.hint {
    max-width: min(100%, 940px);
}

.lab .controls>.callout,
.lab .viz>.callout {
    margin-top: 24px;
    margin-bottom: 8px;
}

.lab .controls>.callout:first-child {
    margin-top: 0;
}

/* Metrics grid → callout transition: tighten the gap */
.lab .controls>.metrics+.callout,
.lab .viz>.metrics+.callout {
    margin-top: 18px;
}

.lab-head h3 {
    margin: 0 0 8px;
    font-family: var(--sans);
    font-size: 12px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text);
    font-weight: 700;
}

.lab-head p {
    margin: 0;
    color: var(--muted);
    font-size: 14.5px;
    max-width: 80ch;
    line-height: 1.75;
}

.lab-grid {
    display: grid;
    grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
    gap: 0;
    align-items: start;
    min-height: 0;
    height: auto;
    overflow: hidden;
}

@media (max-width: 980px) {
    .lab-grid {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;
        height: auto;
        overflow: hidden;
    }
}

@media (max-width: 768px) {
    .lab {
        --lab-shell-max: min(calc(100svh - 80px), 940px);
        --lab-head-height: 76px;
    }
}

.controls {
    padding: 30px 28px;
    border-right: 1px solid var(--stroke);
    border-bottom: none;
    background: var(--card);
    min-height: 0;
    overflow: auto;
}

@media (max-width: 980px) {
    .controls {
        border-right: none;
        border-bottom: 1px solid var(--stroke);
        padding: 20px;
        min-height: 0;
    }
}

@media (max-width: 768px) {
    .controls {
        padding: 24px;
    }
}

.viz {
    padding: 30px 32px;
    min-width: 0;
    min-height: 0;
    overflow: auto;
}

@media (max-width: 768px) {
    .viz {
        padding: 18px;
    }
}

.field {
    margin: 0 0 22px;
}

.field:last-child {
    margin-bottom: 0;
}

.field label {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-family: var(--sans);
    font-size: 13.5px;
    color: var(--muted);
    font-weight: 600;
    line-height: 1.3;
}

.field input[type="range"] {
    width: 100%;
    margin-top: 4px;
}

.field input[type="text"],
.field input[type="number"],
.field textarea,
.field select {
    padding: 9px 12px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-xs);
    background: var(--card2);
    color: var(--text);
    font-size: 13px;
    transition: all 0.2s ease;
}

.field input[type="text"]:focus,
.field input[type="number"]:focus,
.field textarea:focus,
.field select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(94, 231, 223, 0.1);
    outline: none;
}

[data-theme="light"] .field input[type="text"]:focus,
[data-theme="light"] .field input[type="number"]:focus,
[data-theme="light"] .field textarea:focus,
[data-theme="light"] .field select:focus {
    box-shadow: 0 0 0 3px rgba(8, 126, 164, 0.2);
}

.metrics {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
    margin-top: 22px;
}

.metrics:has(.metric:nth-child(4):last-child) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.metrics:has(.metric:nth-child(2):last-child) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (max-width: 768px) {
    .metrics {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 12px;
    }
}

@media (max-width: 560px) {
    .metrics {
        grid-template-columns: 1fr;
    }
}

.metric {
    padding: 20px 18px;
    background: var(--card);
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    text-align: center;
    transition: border-color 0.2s ease;
}

.metric:hover {
    border-color: var(--stroke2);
}

.metric .label {
    font-size: 11.5px;
    color: var(--faint);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    margin-bottom: 10px;
    font-weight: 600;
    line-height: 1.4;
}

.metric .value {
    font-family: var(--mono);
    font-size: 17px;
    font-weight: 700;
    color: var(--text);
    word-break: break-word;
    overflow-wrap: break-word;
}

/* ============================================================
   Lab utilities and layout helpers
   ============================================================ */
.checklist {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px 16px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
}

.checklist label {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    font-size: 13.5px;
    color: var(--muted);
    cursor: pointer;
    line-height: 1.6;
}

.checklist input[type="checkbox"] {
    width: 14px;
    height: 14px;
    margin: 2px 0 0;
    flex-shrink: 0;
    accent-color: var(--accent);
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 9px;
    border-radius: 999px;
    background: rgba(120, 181, 135, 0.14);
    color: var(--accent3);
    border: 1px solid rgba(120, 181, 135, 0.34);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 700;
    font-family: var(--sans);
}

[data-theme="light"] .badge {
    background: rgba(62, 122, 78, 0.12);
    border-color: rgba(62, 122, 78, 0.32);
}

.cost-row {
    display: grid;
    gap: 14px;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin: 20px 0;
}

.cost-label {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    margin-top: 4px;
}

.cost-val {
    font-size: 17px;
    line-height: 1.2;
    font-family: var(--mono);
    font-weight: 700;
    color: var(--text);
}

.export-btn {
    margin-top: 16px;
    margin-right: 6px;
}

.export-btn.copied {
    border-color: var(--accent3);
    color: var(--accent3);
}

.copied {
    color: var(--accent3);
}

.stackbar {
    height: 28px;
    border-radius: 999px;
    overflow: hidden;
    border: 1px solid var(--stroke);
    display: flex;
    background: var(--card);
}

.stacklegend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 12px;
    padding-bottom: 4px;
}

.leg {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 11.5px;
    color: var(--muted);
    white-space: nowrap;
}

.swatch {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    display: inline-block;
    flex: 0 0 auto;
    opacity: 0.95;
}

.stacklist {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 220px;
}

.stackitem {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-xs);
    background: var(--card2);
    transition: border-color 0.2s ease, transform 0.2s ease, background 0.2s ease;
}

.stackitem:hover {
    border-color: var(--stroke2);
    transform: translateX(2px);
}

.stackitem.dragging {
    opacity: 0.6;
    cursor: grabbing;
}

.stackitem.dropzone {
    border-color: var(--accent);
    background: rgba(99, 167, 255, 0.1);
}

.stackitem .handle {
    color: var(--faint);
    font-size: 13px;
    margin-right: 10px;
    user-select: none;
}

.stackitem .lhs {
    display: flex;
    align-items: center;
    min-width: 0;
    flex: 1;
}

.stackitem .name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    line-height: 1.4;
}

.morph-stage {
    display: flex;
    gap: 6px;
    margin: 12px 0;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    min-height: 24px;
    padding: 6px;
    background: var(--card);
    overflow: hidden;
}

.morph-bar {
    height: 100%;
    border-radius: 999px;
    min-height: 10px;
    transition: width 0.3s var(--ease), opacity 0.2s ease;
}

.compare-grid {
    margin-top: 16px;
    display: grid;
    gap: 14px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.compare-col {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 18px;
    background: var(--card);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.compare-col h5 {
    margin: 0;
    font-size: 12px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--faint);
}

.compare-delta {
    margin-top: 12px;
    border-left: 3px solid var(--accent);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    background: var(--card);
    padding: 12px 14px;
    border-top: 1px solid var(--stroke);
    border-right: 1px solid var(--stroke);
    border-bottom: 1px solid var(--stroke);
    color: var(--muted);
    font-size: 14px;
}

.compare-delta strong {
    color: var(--text);
}

.rag-results {
    min-height: 220px;
    margin-top: 4px;
    display: grid;
    gap: 10px;
}

.ragcard {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    padding: 14px;
    transition: border-color 0.2s ease, transform 0.2s ease;
}

.ragcard .top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 8px;
}

.ragcard .title {
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    font-family: var(--sans);
}

.ragcard .score {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--accent);
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.ragcard .body {
    margin: 0 0 10px;
    color: var(--muted);
    line-height: 1.7;
    font-size: 13px;
}

.ragcard .meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.ragcard .pill {
    display: inline-flex;
    border-radius: 999px;
    padding: 2px 8px;
    background: rgba(99, 167, 255, 0.10);
    border: 1px solid rgba(99, 167, 255, 0.28);
    color: var(--accent);
    font-size: 11px;
    font-family: var(--mono);
    line-height: 1.4;
}

.ragcard .pill + .pill {
    margin-left: 0;
}

.ragcard:hover {
    border-color: var(--stroke2);
}

.rag-bottom {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 0 24px 24px;
}

.rag-metrics-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
}

.rag-prompt {
    background: var(--card);
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 14px;
}

.rag-prompt h4 {
    margin: 0 0 8px;
    font-family: var(--sans);
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

#ragPrompt {
    margin: 0;
    white-space: pre-wrap;
    background: var(--card2);
    border: 1px solid var(--stroke);
    border-radius: var(--radius-xs);
    padding: 16px;
    font-size: 11.5px;
    line-height: 1.6;
    max-height: 320px;
    overflow: auto;
}

.rd-zone-legend {
    font-size: 11px;
    color: var(--muted);
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

/* ============================================================
   Timeline: vertical accordion with left rail
   ============================================================ */
.tl-vertical {
    position: relative;
    max-width: 860px;
    margin: 0 auto;
    padding: 32px 36px 28px 36px;
}

/* Track line — the visual spine running behind phase circles */
.tl-vertical::before {
    content: "";
    position: absolute;
    left: 56px;
    top: 52px;
    bottom: 46px;
    width: 2px;
    background: linear-gradient(180deg, var(--accent), var(--stroke) 40%, var(--stroke) 80%, var(--faint));
    opacity: 0.6;
}

/* ── Each phase item ── */
.tl-item {
    position: relative;
    padding-left: 56px;
    margin-bottom: 0;
}

/* Subtle separator between items */
.tl-item + .tl-item {
    border-top: 1px solid rgba(255, 255, 255, 0.03);
}

/* ── Phase header (always visible) ── */
.tl-header {
    display: flex;
    align-items: center;
    gap: 16px;
    width: 100%;
    padding: 14px 18px 14px 0;
    background: none;
    border: none;
    cursor: pointer;
    text-align: left;
    color: var(--muted);
    transition: color 0.2s, background 0.2s;
    border-radius: var(--radius-sm);
}

.tl-header:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.02);
}

.tl-header:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Phase number — sits over the track line */
.tl-num {
    position: absolute;
    left: 0;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid var(--stroke);
    background: var(--bg1);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--mono);
    font-size: 11px;
    font-weight: 700;
    color: var(--faint);
    transition: all 0.3s var(--ease);
    z-index: 1;
    flex-shrink: 0;
}

/* Phase title */
.tl-title {
    font-family: var(--sans);
    font-weight: 600;
    font-size: 15px;
    line-height: 1.4;
    flex: 1;
    transition: color 0.2s, font-size 0.25s var(--ease);
}

/* Expand arrow */
.tl-arrow {
    font-size: 15px;
    color: var(--faint);
    transition: transform 0.3s var(--ease), color 0.2s;
    flex-shrink: 0;
}

/* ── Detail body (collapsed by default) ── */
.tl-body {
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transition: max-height 0.4s var(--ease), opacity 0.3s ease, padding 0.3s ease;
    padding: 0 0 0 0;
}

.tl-body .tl-why {
    font-family: var(--serif);
    font-size: 14.5px;
    line-height: 1.7;
    color: var(--text);
    margin: 0 0 14px;
    font-style: italic;
    opacity: 0.85;
}

.tl-body ul {
    margin: 0;
    padding-left: 18px;
}

.tl-body li {
    font-family: var(--serif);
    font-size: 13.5px;
    line-height: 1.7;
    color: var(--muted);
    margin-bottom: 6px;
}

.tl-body li strong {
    color: var(--text);
    font-weight: 600;
}

.tl-body li em {
    font-style: italic;
}

/* ── Visited state — completed phases ── */
.tl-item.visited .tl-num {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg);
}

.tl-item.visited .tl-title {
    color: var(--muted);
}

.tl-item.visited .tl-header:hover .tl-title {
    color: var(--text);
}

/* ── Active state — current phase ── */
.tl-item.active .tl-num {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg);
    box-shadow: 0 0 16px rgba(94, 231, 223, 0.3), 0 0 4px rgba(94, 231, 223, 0.15);
    width: 32px;
    height: 32px;
    left: -2px;
    font-size: 12px;
}

.tl-item.active .tl-header {
    color: var(--text);
}

.tl-item.active .tl-title {
    color: var(--text);
    font-size: 16px;
}

.tl-item.active .tl-arrow {
    transform: rotate(90deg);
    color: var(--accent);
}

.tl-item.active .tl-body {
    max-height: 400px;
    opacity: 1;
    padding: 4px 0 20px 0;
    background: rgba(0, 0, 0, 0.15);
    border-radius: var(--radius-sm);
    padding: 14px 20px 18px;
    margin-top: 4px;
    margin-bottom: 8px;
    border-left: 2px solid rgba(94, 231, 223, 0.2);
}

[data-theme="light"] .tl-item.active .tl-body {
    background: rgba(0, 0, 0, 0.03);
    border-left-color: rgba(47, 111, 178, 0.2);
}

[data-theme="light"] .tl-item + .tl-item {
    border-top-color: rgba(0, 0, 0, 0.04);
}

.fail {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    text-align: left;
    padding: 18px 20px;
    color: var(--text);
    width: 100%;
    transition: border-color 0.2s ease, transform 0.2s ease;
}

.fail .t {
    font-weight: 700;
    font-size: 14px;
    color: var(--text);
    margin-bottom: 6px;
    line-height: 1.35;
}

.fail .s {
    font-size: 11px;
    color: var(--faint);
    margin-bottom: 6px;
    font-family: var(--mono);
}

.fail .d {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
    font-size: 13px;
}

.val {
    font-family: var(--mono);
    color: var(--accent);
    font-weight: 600;
    letter-spacing: 0.01em;
}

/* Timeline detail container (hidden — accordion renders inline) */
.timelinedetail {
    display: none !important;
}

.pm-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    margin-bottom: 8px;
}

.pm-track {
    margin-top: 18px;
    display: grid;
    gap: 10px;
}

.pm-step {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 20px 22px;
    background: var(--card);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.pm-step + .pm-step {
    margin-top: 12px;
}

.pm-step.pm-active {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px rgba(99, 167, 255, 0.2);
}

.pm-label {
    font-size: 11px;
    font-family: var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--faint);
    margin-bottom: 4px;
}

.pm-title {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 8px;
    color: var(--text);
    line-height: 1.4;
}

.pm-body {
    font-size: 14px;
    color: var(--muted);
    margin-bottom: 12px;
    line-height: 1.7;
}

.pm-link {
    color: var(--accent);
    font-size: 13px;
    text-decoration: underline;
    text-underline-offset: 3px;
    opacity: 0.85;
    transition: opacity 0.15s ease;
}

.pm-link:hover {
    opacity: 1;
}

.injection-result {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 16px;
    background: var(--card);
    min-height: 130px;
    max-height: 260px;
    overflow: auto;
    font-size: 12.5px;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
}

.injection-verdict {
    margin: 12px 0;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    font-size: 13px;
    font-weight: 600;
}

.injection-verdict.blocked {
    border-color: rgba(120, 181, 135, 0.5);
    color: var(--accent3);
    background: rgba(120, 181, 135, 0.06);
}

.injection-verdict.breached {
    border-color: rgba(216, 107, 107, 0.5);
    color: var(--danger);
    background: rgba(216, 107, 107, 0.06);
}

.inj-system {
    display: inline;
    color: var(--faint);
}

.inj-hostile {
    color: var(--danger);
    font-weight: 700;
}

.inj-blocked {
    text-decoration: line-through;
    opacity: 0.8;
}

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

.maturity-questions label {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    color: var(--text);
    cursor: pointer;
    font-size: 13.5px;
    line-height: 1.6;
}

.maturity-questions label select {
    flex-shrink: 0;
}

.maturity-tier {
    margin-top: 20px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    padding: 20px;
}

.tier-name {
    color: var(--accent3);
    font-size: 18px;
    font-family: var(--sans);
    margin-bottom: 6px;
}

.tier-desc {
    color: var(--muted);
    font-size: 12px;
    line-height: 1.5;
}

.pre-plain {
    display: block;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    padding: 18px;
    font-size: 12px;
    line-height: 1.7;
    overflow-x: auto;
    color: var(--text);
}

[data-theme="light"] .pre-plain {
    background: rgba(255, 255, 255, 0.95);
}

.sysmap-title {
    font-family: var(--sans);
    font-weight: 700;
    font-size: 15px;
    color: var(--text);
}

.panel {
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    padding: 16px;
    background: var(--card);
    margin-bottom: 14px;
}

@media (max-width: 900px) {
    .cost-row {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .compare-grid {
        grid-template-columns: 1fr;
    }

    .tl-vertical {
        padding: 20px 16px 20px 16px;
    }

    .tl-vertical::before {
        left: 37px;
    }

    .tl-item {
        padding-left: 44px;
    }

    .tl-item.active .tl-num {
        left: -1px;
        width: 28px;
        height: 28px;
    }

    .rag-bottom {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 640px) {
    .cost-row {
        grid-template-columns: 1fr;
    }

    .stacklist {
        min-height: auto;
    }
}

/* Bar for budget visualisation */
.bar {
    display: flex;
    height: 28px;
    border-radius: 999px;
    overflow: hidden;
    border: 1px solid var(--stroke);
    background: var(--card);
    margin-top: 12px;
    position: relative;
}

.seg {
    height: 100%;
    transition: width 0.4s var(--ease);
    position: relative;
}

.seg.system {
    background: var(--c-system);
}

.seg.conv {
    background: var(--c-conv);
}

.seg.retr {
    background: var(--c-retr);
}

.seg.tools {
    background: var(--c-tools);
}

.seg.user {
    background: var(--c-user);
}

.seg.resp {
    background: var(--c-resp);
}

/* ============================================================
   Modal - Product shell
   ============================================================ */
.modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 13vh;
    background: rgba(8, 12, 16, 0.74);
    backdrop-filter: blur(6px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

[data-theme="light"] .modal {
    background: rgba(237, 231, 221, 0.8);
}

.modal.open {
    opacity: 1;
    pointer-events: auto;
}

.modal-card {
    background: linear-gradient(180deg, var(--bg1), var(--bg2));
    border: 1px solid var(--stroke);
    border-radius: var(--radius);
    width: min(640px, 92vw);
    max-height: 74vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow2);
    transform: translateY(16px) scale(0.98);
    transition: transform 0.35s var(--ease-out);
}

.modal.open .modal-card {
    transform: translateY(0) scale(1);
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 20px;
    border-bottom: 1px solid var(--stroke);
}

.modal-title {
    font-family: var(--sans);
    font-weight: 700;
    font-size: 15px;
    letter-spacing: 0.02em;
}

.modal-head .close {
    background: var(--card);
    border: 1px solid var(--stroke);
    color: var(--muted);
    cursor: pointer;
    font-size: 11px;
    font-family: var(--sans);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 6px 10px;
    border-radius: var(--radius-xs);
    transition: all 0.2s ease;
}

.modal-head .close:hover {
    background: var(--card);
    color: var(--text);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
}

.palette {
    padding: 16px 20px;
    border-bottom: 1px solid var(--stroke);
}

.palette input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card2);
    color: var(--text);
    font-size: 14px;
    transition: all 0.2s ease;
}

.palette input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(94, 231, 223, 0.1);
    outline: none;
}

[data-theme="light"] .palette input:focus {
    box-shadow: 0 0 0 3px rgba(8, 126, 164, 0.2);
}

.results {
    overflow-y: auto;
    max-height: 360px;
    padding: 10px;
}

.res {
    padding: 12px 14px;
    border-radius: var(--radius-xs);
    cursor: pointer;
    border: 1px solid var(--stroke);
    background: var(--card);
    transition: all 0.2s ease;
    margin-bottom: 6px;
}

.res:hover,
.res.active {
    background: var(--card2);
    border-color: var(--accent);
}

.res .t {
    font-weight: 600;
    font-size: 13px;
    color: var(--text);
    margin-bottom: 4px;
}

.res .m {
    font-size: 11px;
    color: var(--faint);
}

/* ============================================================
   Drag list
   ============================================================ */
.draglist {
    list-style: none;
    margin: 0;
    padding: 0;
}

.draglist li {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    margin-bottom: 10px;
    cursor: grab;
    transition: all 0.2s ease;
    user-select: none;
}

.draglist li:hover {
    border-color: var(--stroke2);
    transform: translateX(4px);
}

.draglist li.dragging {
    opacity: 0.5;
    cursor: grabbing;
}

.draglist li .handle {
    color: var(--faint);
    font-size: 16px;
}

/* ============================================================
   Token chips
   ============================================================ */
.tokenout {
    margin-bottom: 18px;
    padding: 14px 16px;
    border: 1px solid var(--stroke);
    border-radius: var(--radius-sm);
    background: var(--card);
    min-height: 104px;
    max-height: clamp(180px, 34vh, 320px);
    overflow-y: auto;
    overflow-x: hidden;
    font-family: var(--mono);
    font-size: 12px;
    line-height: 1.65;
    color: var(--text);
    word-break: break-word;
}

#lab-tokeniser .metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (max-width: 560px) {
    #lab-tokeniser .metrics {
        grid-template-columns: 1fr;
    }

    .tokenout {
        max-height: 240px;
    }
}

.tokchip {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    background: var(--card2);
    border: 1px solid var(--stroke);
    border-radius: 999px;
    font-family: var(--mono);
    font-size: 12px;
    margin: 3px;
    transition: all 0.2s ease;
}

.tokchip:hover {
    border-color: var(--accent);
    box-shadow: 0 0 12px var(--accent-glow);
    transform: translateY(-1px);
}

/* ============================================================
   Lab-specific overrides — visual polish pass
   ============================================================ */

/* Dense labs: raise the height ceiling so content breathes */
#lab-budget,
#lab-maturity,
#lab-benchmark {
    --lab-shell-max: min(calc(100svh - 72px), 1400px);
}

/* RAG lab: content-driven height, no excessive void */
#lab-rag {
    --lab-shell-max: min(calc(100svh - 72px), 1100px);
}

#lab-chunks,
#lab-middle,
#lab-stack,
#lab-injection {
    --lab-shell-max: min(calc(100svh - 80px), 1280px);
}

/* Budget lab: tighter field spacing since it has 12+ controls */
#lab-budget .controls .field {
    margin-bottom: 18px;
}

#lab-budget .cost-row {
    gap: 12px;
    margin: 20px 0 16px;
}

/* Chunks lab: the text panel needs better readability */
#lab-chunks .viz {
    font-size: 15px;
    line-height: 1.76;
}

/* Embeddings lab: prevent card text truncation */
#lab-embeddings .metric .value {
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    overflow-wrap: anywhere;
}

/* Tokeniser lab: the "tax note" metric is a sentence, not a number */
#lab-tokeniser #tokNote {
    font-family: var(--serif);
    font-size: 14px;
    font-weight: 500;
    line-height: 1.55;
}

/* RAG lab: the insight cards grid needs more room */
#lab-rag .viz {
    padding: 28px 24px;
}

/* RAG lab: metric labels are longer phrases — allow wrapping */
#lab-rag .metric .label {
    font-size: 10.5px;
    letter-spacing: 0.04em;
    word-break: break-word;
    overflow-wrap: break-word;
}

/* Timeline lab — no lab-grid, standalone .tl-vertical layout */
#lab-timeline {
    --lab-shell-max: min(calc(100svh - 72px), 1000px);
}

/* Maturity dimension scores: soften the empty-state colour */
#lab-maturity .metric .value {
    font-size: 15px;
    font-weight: 700;
}

/* Maturity diagnostic: give the radar chart more room */
#lab-maturity .viz {
    padding: 24px 28px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Injection lab: wider controls for two textareas */
#lab-injection .lab-grid {
    grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
}

@media (max-width: 980px) {
    #lab-injection .lab-grid {
        grid-template-columns: 1fr;
    }
}

/* Postmortem walkthrough: step cards need internal breathing room */
#lab-postmortem .viz {
    padding: 24px 28px;
}

/* Failure grid: proper grid layout for the failure mode cards */
.failuregrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 18px;
    padding: 28px 30px;
}

.failuregrid .fail {
    border: 1px solid var(--stroke);
    border-left: 3px solid var(--faint);
    border-radius: var(--radius-sm);
    background: var(--card);
    padding: 20px 22px;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    color: var(--text);
    transition: border-color 0.2s ease, transform 0.15s ease;
}

.failuregrid .fail:hover {
    border-left-color: var(--accent);
    transform: translateY(-1px);
}

.failuregrid .fail .t {
    font-weight: 700;
    font-size: 15px;
    margin-bottom: 6px;
    line-height: 1.35;
}

.failuregrid .fail .s {
    font-size: 11.5px;
    color: var(--faint);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 10px;
    font-weight: 600;
    font-family: var(--mono);
}

.failuregrid .fail .d {
    font-size: 13.5px;
    color: var(--muted);
    line-height: 1.6;
    margin: 0;
}

/* Budget lab: two-column layout with dense controls grid */
#lab-budget .lab-grid {
    grid-template-columns: minmax(320px, 440px) minmax(0, 1fr);
}

#lab-budget .controls {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px 24px;
    padding: 24px 22px;
}

#lab-budget .controls .field:nth-child(-n+2) {
    /* Window size + custom field stay in first column */
}

#lab-budget .controls .checklist {
    grid-column: 1 / -1;
}

#lab-budget .controls .metrics {
    grid-column: 1 / -1;
}

#lab-budget .controls .cost-row {
    grid-column: 1 / -1;
}

#lab-budget .controls .callout {
    grid-column: 1 / -1;
}

/* Buttons: sit side-by-side, natural width */
#lab-budget .controls .btn,
#lab-budget .controls .export-btn {
    justify-self: start;
    align-self: center;
}

@media (max-width: 980px) {
    #lab-budget .lab-grid {
        grid-template-columns: 1fr;
    }
    #lab-budget .controls {
        border-right: none;
        border-bottom: 1px solid var(--stroke);
    }
}

@media (max-width: 640px) {
    #lab-budget .controls {
        grid-template-columns: 1fr;
    }
}

/* General: ensure canvases in labs display well */
.lab canvas {
    display: block;
    max-width: 100%;
    border-radius: var(--radius-sm);
    background: transparent;
}

/* Better visual weight for the "Run" / action buttons in labs */
.lab .btn {
    margin-top: 8px;
    margin-bottom: 4px;
}

/* Lab pairs (side-by-side mini labs): increase gap */
.lab-pair {
    gap: 24px;
}

/* ═══════ AUDIT FIXES ═══════ */

/* 1. Governance lab: collapse empty timeline, align content top */
#lab-governance .viz {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 24px 28px;
    align-items: stretch;
}

#lab-governance .gov-timeline:empty {
    min-height: 58px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#lab-governance .gov-timeline:empty::before {
    content: "Trigger an event to see the governance timeline.";
    display: block;
    padding: 14px 20px;
    border: 1px dashed var(--stroke);
    border-radius: var(--radius-sm);
    color: var(--faint);
    font-size: 13px;
    text-align: center;
    font-style: italic;
}

/* 2. Mobile RAG: prevent hint text from being oversized */
@media (max-width: 768px) {
    #lab-rag .controls .hint {
        font-size: 12px;
        line-height: 1.5;
    }

    #lab-rag .viz {
        padding: 16px 14px;
    }

    .rag-results {
        min-height: 120px;
    }

    /* Ensure select doesn't overlap with result cards */
    #lab-rag .controls select {
        position: relative;
        z-index: 2;
    }

    .rag-bottom {
        padding: 0 14px 18px;
    }
}

/* 3. Failure grid: minmax floor on smaller screens */
@media (max-width: 768px) {
    .failuregrid {
        grid-template-columns: 1fr;
        padding: 18px 16px;
        gap: 12px;
    }
}

/* 4. Postmortem cards: tighten step card spacing */
#lab-postmortem .pm-step + .pm-step {
    margin-top: 8px;
}

#lab-postmortem .pm-controls {
    gap: 8px;
    margin-bottom: 4px;
}

/* 5. Injection lab: better empty-state styling for metric cards */
#lab-injection .metric .value {
    min-height: 1.2em;
}

#lab-injection .viz {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 24px 28px;
}

/* Assembled prompt placeholder styling */
#injAssembled:empty::after,
#injAssembled[data-empty]::after {
    content: "Run the simulation to see the assembled prompt.";
    color: var(--faint);
    font-style: italic;
    font-size: 13px;
}

/* 6. Replay lab pair: ensure viz section has clear separation */
.lab-pair #lab-replay .viz {
    padding: 20px 22px;
}

.lab-pair #lab-replay .replay-verdict {
    font-size: 13px;
    color: var(--faint);
    font-style: italic;
    margin-bottom: 8px;
}

/* 7. Stacked mobile layout: the lab-grid max-height (from *:not(.lab-head))
   constrains grid rows, making controls overflow their cell into viz.
   Fix: remove the max-height on the lab-grid itself so rows size to content,
   and let the lab-grid scroll as one unit. */
@media (max-width: 980px) {
    .lab.lab-ready.lab-open > .lab-grid {
        max-height: calc(var(--lab-shell-max) - var(--lab-head-height)) !important;
        overflow-y: auto !important;
        grid-template-rows: min-content min-content !important;
    }

    /* Remove per-section independent scroll — stacked layout scrolls as one unit */
    .lab.lab-ready.lab-open > .lab-grid > .controls,
    .lab.lab-ready.lab-open > .lab-grid > .viz {
        max-height: none !important;
        overflow-y: visible !important;
        overflow-x: hidden !important;
    }
}

/* 8. Governance lab: compact button layout on mobile */
@media (max-width: 768px) {
    #lab-governance .gov-controls {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    #lab-governance .gov-controls .btn {
        flex: 0 0 auto;
        font-size: 11px;
        padding: 6px 10px;
    }
}
