/* Trading Basics: book-style reader, light and dark themes from one set of tokens */
:root {
    --bg: #fbfaf7;
    --paper: #ffffff;
    --ink: #1d2433;
    --ink-soft: #4b5568;
    --ink-faint: #8a93a5;
    --line: #e6e3dc;
    --accent: #2f6fde;
    --accent-soft: #e8f0ff;
    --up: #16a34a;
    --down: #dc2626;
    --amber: #d97706;
    --purple: #7c3aed;
    --teal: #0d9488;
    --grid: #ece9e2;
    --note: #fff8e6;
    --note-line: #f2d48a;
    --good: #e9f8ef;
    --bad: #fdecec;
    --shadow: 0 1px 2px rgba(20, 24, 40, 0.06), 0 6px 24px rgba(20, 24, 40, 0.06);
    --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
    --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
    --mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #12151c;
        --paper: #181c25;
        --ink: #e7e9ee;
        --ink-soft: #b4bac6;
        --ink-faint: #7d8596;
        --line: #2a303d;
        --accent: #6c9cff;
        --accent-soft: #1d2840;
        --up: #22c55e;
        --down: #f05252;
        --amber: #f5a524;
        --purple: #a78bfa;
        --teal: #2dd4bf;
        --grid: #252a36;
        --note: #2a2414;
        --note-line: #6b5520;
        --good: #13281c;
        --bad: #301818;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 24px rgba(0, 0, 0, 0.25);
    }
}

:root[data-theme="dark"] {
    --bg: #12151c;
    --paper: #181c25;
    --ink: #e7e9ee;
    --ink-soft: #b4bac6;
    --ink-faint: #7d8596;
    --line: #2a303d;
    --accent: #6c9cff;
    --accent-soft: #1d2840;
    --up: #22c55e;
    --down: #f05252;
    --amber: #f5a524;
    --purple: #a78bfa;
    --teal: #2dd4bf;
    --grid: #252a36;
    --note: #2a2414;
    --note-line: #6b5520;
    --good: #13281c;
    --bad: #301818;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 24px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
    background: var(--bg);
    color: var(--ink);
    font-family: var(--sans);
    font-size: 17px;
    line-height: 1.65;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

/* ---- Layout: sidebar contents + reading column ---- */
.shell {
    display: grid;
    grid-template-columns: 290px 1fr;
    min-height: 100vh;
}

.toc {
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    padding: 22px 18px 40px;
    border-right: 1px solid var(--line);
    background: var(--paper);
}

.toc a.brand {
    display: block;
    padding: 0;
    background: none;
    margin-bottom: 4px;
    line-height: 1.2;
    color: var(--ink);
    font-family: var(--serif);
    font-size: 1.6rem;
    font-weight: 700;
    text-decoration: none;
}

.brand-sub {
    margin: 0 0 18px;
    color: var(--ink-faint);
    font-size: 0.82rem;
}

.progress {
    height: 5px;
    margin-bottom: 20px;
    border-radius: 999px;
    background: var(--grid);
    overflow: hidden;
}

.progress span {
    display: block;
    height: 100%;
    width: 0;
    background: var(--accent);
    transition: width 0.3s ease;
}

.toc-part {
    margin: 18px 0 6px;
    color: var(--ink-faint);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

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

.toc a {
    display: flex;
    align-items: baseline;
    gap: 9px;
    padding: 6px 9px;
    border-radius: 8px;
    color: var(--ink-soft);
    font-size: 0.9rem;
    line-height: 1.35;
    text-decoration: none;
}

.toc a:hover { background: var(--accent-soft); color: var(--ink); }
.toc a.brand:hover { background: none; }

.toc a.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
}

.toc .num {
    flex: 0 0 30px;
    color: var(--ink-faint);
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* The drawer backdrop only exists on small screens */
.scrim { display: none; }

/* A read chapter gets a small filled dot */
.toc a.read .num::after {
    content: "";
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-left: 4px;
    border-radius: 50%;
    background: var(--up);
    vertical-align: middle;
}

.toc-tools {
    display: flex;
    gap: 8px;
    margin-top: 24px;
}

.toc-tools button, .btn {
    padding: 7px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--paper);
    color: var(--ink-soft);
    font: inherit;
    font-size: 0.82rem;
    cursor: pointer;
}

.toc-tools button:hover, .btn:hover { border-color: var(--accent); color: var(--accent); }

main {
    min-width: 0;
    padding: 36px 40px 80px;
}

.topbar {
    display: none;
}

article {
    max-width: 760px;
    margin: 0 auto;
}

.chapter { display: none; }
.chapter.show { display: block; animation: fade 0.25s ease; }

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

.kicker {
    margin: 0 0 6px;
    color: var(--accent);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

h1 {
    margin: 0 0 8px;
    font-family: var(--serif);
    font-size: 2.3rem;
    line-height: 1.15;
}

.lede {
    margin: 0 0 26px;
    color: var(--ink-soft);
    font-size: 1.12rem;
}

h2 {
    margin: 34px 0 10px;
    font-family: var(--serif);
    font-size: 1.45rem;
    line-height: 1.25;
}

h3 {
    margin: 24px 0 6px;
    font-size: 1.05rem;
}

p { margin: 0 0 14px; }

ul, ol.steps { margin: 0 0 16px; padding-left: 22px; }
li { margin-bottom: 6px; }

strong { color: var(--ink); }

code, .mono {
    padding: 1px 6px;
    border-radius: 5px;
    background: var(--accent-soft);
    font-family: var(--mono);
    font-size: 0.86em;
}

/* ---- Callouts ---- */
.note, .tip, .warn, .example {
    margin: 20px 0;
    padding: 14px 18px;
    border-left: 4px solid;
    border-radius: 0 10px 10px 0;
}

.note { background: var(--note); border-color: var(--note-line); }
.tip { background: var(--good); border-color: var(--up); }
.warn { background: var(--bad); border-color: var(--down); }
.example { background: var(--accent-soft); border-color: var(--accent); }

.note p:last-child, .tip p:last-child, .warn p:last-child, .example p:last-child { margin-bottom: 0; }

.label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.74rem;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    opacity: 0.8;
}

.formula {
    display: block;
    margin: 14px 0;
    padding: 12px 16px;
    border: 1px dashed var(--line);
    border-radius: 10px;
    background: var(--paper);
    font-family: var(--mono);
    font-size: 0.92rem;
    text-align: center;
    overflow-x: auto;
}

/* ---- Figures and charts ---- */
figure {
    margin: 24px 0;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--paper);
    box-shadow: var(--shadow);
}

figure svg { display: block; width: 100%; height: auto; }

/* Charts with dense labels keep a readable width on phones and scroll sideways */
.wide-chart { overflow-x: auto; }
@media (max-width: 600px) { .wide-chart svg { min-width: 600px; } }

figcaption {
    margin-top: 10px;
    color: var(--ink-faint);
    font-size: 0.84rem;
    line-height: 1.5;
}

.chart-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 16px;
    margin-bottom: 12px;
    font-size: 0.88rem;
}

.chart-controls label { display: flex; align-items: center; gap: 8px; }
.chart-controls input[type="range"] { width: 160px; accent-color: var(--accent); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button {
    padding: 6px 13px;
    border: none;
    border-right: 1px solid var(--line);
    background: transparent;
    color: var(--ink-soft);
    font: inherit;
    font-size: 0.84rem;
    cursor: pointer;
}
.seg button:last-child { border-right: none; }
.seg button.on { background: var(--accent); color: #fff; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 8px; font-size: 0.82rem; color: var(--ink-soft); }
.legend i { display: inline-block; width: 14px; height: 3px; margin-right: 6px; border-radius: 2px; vertical-align: middle; }

/* ---- HTML flow diagrams ---- */
.flow {
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    justify-content: center;
    gap: 0;
}

.flow .node {
    flex: 1 1 0;
    min-width: 110px;
    padding: 12px 10px;
    border: 1.5px solid var(--line);
    border-radius: 12px;
    background: var(--bg);
    text-align: center;
    font-size: 0.86rem;
    line-height: 1.35;
}

.flow .node b { display: block; margin-bottom: 3px; font-size: 0.92rem; }
.flow .node small { color: var(--ink-faint); }
.flow .node.hi { border-color: var(--accent); background: var(--accent-soft); }

.flow .arrow {
    position: relative;
    flex: 0 0 34px;
    align-self: center;
    height: 2px;
    background: var(--ink-faint);
}

.flow .arrow::after {
    content: "";
    position: absolute;
    right: -1px;
    top: -5px;
    border: 6px solid transparent;
    border-left: 8px solid var(--ink-faint);
    border-right: none;
}

/* ---- Timelines ---- */
.timeline {
    position: relative;
    display: grid;
    grid-template-columns: repeat(var(--cols, 4), 1fr);
    gap: 10px;
    padding-top: 26px;
}

.timeline::before {
    content: "";
    position: absolute;
    top: 9px;
    left: 4%;
    right: 4%;
    height: 3px;
    border-radius: 3px;
    background: var(--grid);
}

.timeline .stop { position: relative; text-align: center; font-size: 0.84rem; line-height: 1.4; }

.timeline .stop::before {
    content: "";
    position: absolute;
    top: -23px;
    left: 50%;
    width: 14px;
    height: 14px;
    margin-left: -7px;
    border: 3px solid var(--paper);
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 0 1px var(--accent);
}

.timeline .stop.key::before { background: var(--down); box-shadow: 0 0 0 1px var(--down); }
.timeline .stop b { display: block; font-size: 0.9rem; }
.timeline .stop small { color: var(--ink-faint); }

/* ---- Tables and metric cards ---- */
table {
    width: 100%;
    margin: 16px 0 20px;
    border-collapse: collapse;
    font-size: 0.9rem;
}

th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--ink-faint); font-size: 0.74rem; letter-spacing: 0.06em; text-transform: uppercase; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; }

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 10px;
    margin: 18px 0;
}

.card {
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--paper);
}

.card .k { color: var(--ink-faint); font-size: 0.74rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; }
.card .v { font-family: var(--serif); font-size: 1.35rem; font-weight: 700; }
.card .d { color: var(--ink-soft); font-size: 0.8rem; line-height: 1.4; }

/* ---- Quiz ---- */
details.check {
    margin: 26px 0 8px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--paper);
}

details.check summary {
    padding: 13px 16px;
    font-weight: 600;
    cursor: pointer;
}

details.check summary::marker { color: var(--accent); }
details.check .answer { padding: 0 16px 14px; color: var(--ink-soft); }

/* ---- Calculator ---- */
.calc {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 16px;
}

.calc label { display: flex; flex-direction: column; gap: 4px; font-size: 0.82rem; color: var(--ink-soft); }

.calc input {
    padding: 9px 11px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--bg);
    color: var(--ink);
    font: inherit;
    font-size: 0.95rem;
}

.calc-out {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
    margin-top: 16px;
}

/* ---- Chapter navigation ---- */
.pager {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    margin-top: 44px;
    padding-top: 22px;
    border-top: 1px solid var(--line);
}

.pager a {
    flex: 1;
    max-width: 48%;
    padding: 12px 16px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--paper);
    color: var(--ink);
    text-decoration: none;
}

.pager a:hover { border-color: var(--accent); }
.pager a.next { margin-left: auto; text-align: right; }
.pager small { display: block; color: var(--ink-faint); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; }
.pager span { font-weight: 600; }

.disclaimer {
    margin-top: 30px;
    color: var(--ink-faint);
    font-size: 0.8rem;
    text-align: center;
}

/* ---- Small screens: the contents become a drawer ---- */
@media (max-width: 900px) {
    .shell { grid-template-columns: 1fr; }

    .toc {
        position: fixed;
        z-index: 40;
        left: 0;
        top: 0;
        width: min(320px, 86vw);
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: var(--shadow);
    }

    body.drawer .toc { transform: none; }

    .scrim {
        position: fixed;
        inset: 0;
        z-index: 30;
        display: none;
        background: rgba(10, 12, 18, 0.45);
    }

    body.drawer .scrim { display: block; }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 20;
        display: flex;
        align-items: center;
        gap: 12px;
        margin: -36px -40px 22px;
        padding: 10px 16px;
        border-bottom: 1px solid var(--line);
        background: var(--paper);
        font-size: 0.9rem;
    }

    .topbar .where { flex: 1; min-width: 0; overflow: hidden; color: var(--ink-soft); text-overflow: ellipsis; white-space: nowrap; }

    main { padding: 36px 40px 80px; }
}

@media (max-width: 600px) {
    body { font-size: 16px; }
    main { padding: 36px 16px 70px; }
    .topbar { margin: -36px -16px 18px; }
    h1 { font-size: 1.8rem; }
    h2 { font-size: 1.25rem; }
    figure { padding: 10px; margin: 20px -4px; }
    .flow { flex-direction: column; }
    .flow .node { min-width: 0; }
    .flow .arrow { flex: 0 0 22px; width: 2px; height: 22px; align-self: center; }
    .flow .arrow::after { right: -5px; top: auto; bottom: -1px; border: 6px solid transparent; border-top: 8px solid var(--ink-faint); border-bottom: none; }
    .timeline { grid-template-columns: repeat(2, 1fr); row-gap: 34px; }
    .timeline::before { display: none; }
    .calc { grid-template-columns: 1fr; }
    .pager { flex-direction: column; }
    .pager a { max-width: none; }
    .pager a.next { margin-left: 0; }
    .chart-controls input[type="range"] { width: 120px; }
}

/* ---- Reading width: text stays narrow, figures marked .wide use the whole content area ---- */
article { max-width: 1280px; }
article .chapter > *, .pager, article > .disclaimer { max-width: 780px; margin-left: auto; margin-right: auto; }
article .chapter > .wide { max-width: none; }

/* ---- Option simulator ---- */
.sim { container-type: inline-size; }

.sim-controls {
    margin-bottom: 18px;
    padding: 16px 18px;
    border-radius: 12px;
    background: var(--bg);
}

.ctl-row {
    display: flex;
    flex-wrap: wrap;
    gap: 14px 28px;
    margin-bottom: 16px;
}

.ctl-sliders {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px 24px;
}

.ctl { display: flex; flex-direction: column; gap: 6px; min-width: 0; font-size: 0.86rem; }
.ctl-label { color: var(--ink-soft); font-weight: 600; white-space: nowrap; }
.ctl-label b { color: var(--accent); margin-left: 4px; }
.ctl small { color: var(--ink-faint); font-size: 0.78rem; line-height: 1.35; }
.ctl input[type="range"] { width: 100%; accent-color: var(--accent); }
.ctl .seg { flex-wrap: wrap; align-self: flex-start; }

.sim-main { display: flex; flex-direction: column; gap: 6px; }
.sim-fan-col { min-width: 0; }

.sim-headline {
    margin: 0 0 14px;
    padding: 14px 16px;
    border-left: 4px solid var(--accent);
    border-radius: 0 10px 10px 0;
    background: var(--accent-soft);
    font-size: 1rem;
    line-height: 1.55;
}

.sim-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 6px 0 18px;
}

.sim-key { color: var(--ink-faint); font-size: 0.8rem; }
.sim-key i { display: inline-block; width: 18px; height: 3px; margin: 0 6px 0 12px; border-radius: 2px; vertical-align: middle; }
.sim-key i.win { background: var(--up); }
.sim-key i.lose { background: var(--ink-faint); opacity: 0.5; }

.eq { display: flex; align-items: stretch; gap: 10px; margin-bottom: 8px; }

.eq-tile {
    flex: 1;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--bg);
}

.eq-tile .k { color: var(--ink-faint); font-size: 0.72rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; }
.eq-tile .v { font-family: var(--serif); font-size: 1.6rem; font-weight: 700; }
.eq-tile .d { color: var(--ink-soft); font-size: 0.78rem; line-height: 1.35; }
.eq-tile.total { border-color: var(--accent); background: var(--accent-soft); }

.eq-op { align-self: center; color: var(--ink-faint); font-family: var(--serif); font-size: 1.8rem; font-weight: 700; }

.sim-block { margin-top: 10px; }
.sim h3 { margin-top: 22px; }
.sim-sub { margin: 0 0 10px; color: var(--ink-soft); font-size: 0.9rem; }

.minis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

.mini {
    padding: 8px 10px 4px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--bg);
}

/* Wide screens: sliders in one line, chart beside the explanation, four small charts across */
@container (min-width: 880px) {
    .ctl-row { flex-wrap: nowrap; }
    .ctl-sliders { grid-template-columns: repeat(4, minmax(0, 1fr)); }
    .sim-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr); gap: 22px; align-items: start; }
    .sim-side { position: sticky; top: 16px; }
    .sim-side .eq { flex-direction: column; gap: 4px; }
    .sim-side .sim-headline { font-size: 0.94rem; }
    .sim-side .eq-tile { padding: 9px 13px; }
    .sim-side .eq-tile .v { font-size: 1.4rem; line-height: 1.25; }
    .sim-side .eq-op { font-size: 1.3rem; line-height: 1; }
    .minis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* Futures draw themselves from left to right */
path.grow {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: grow 1.1s ease-out forwards;
}

@keyframes grow { to { stroke-dashoffset: 0; } }

@media (prefers-reduced-motion: reduce) {
    path.grow { animation: none; stroke-dashoffset: 0; }
}

@media (max-width: 600px) {
    .sim-controls { padding: 12px; }
    .ctl-sliders { grid-template-columns: 1fr; }
    .ctl-label { white-space: normal; }
    .eq { flex-direction: column; }
    .eq-op { align-self: center; font-size: 1.4rem; line-height: 1; }
    .minis { grid-template-columns: 1fr; }
}
