/* Chapter pages: prose on the left, a docked Python REPL on the right. */

:root {
    --ink: #222;
    --muted: #666;
    --rule: #e2e2e2;
    --accent: #5a67d8;
    --code-bg: #f6f6f4;
    --screen-bg: #1e1f24;
    --screen-ink: #e8e8e8;
    --screen-err: #ff9b8f;
    --screen-note: #9aa3b5;
    --placeholder: #fff8e1;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
    --repl-width: min(46vw, 760px);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 18px;
    line-height: 1.6;
    color: var(--ink);
    background: #fff;
}

code,
pre,
textarea,
input {
    font-family: var(--mono);
    font-size: 15px;
}

/* ---- Layout ---------------------------------------------------------- */

.chapter-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--repl-width);
}

.chapter {
    /* An explicit width keeps long code lines scrolling inside their box
       instead of widening the whole column. */
    width: 100%;
    min-width: 0;
    max-width: 72ch;
    margin: 0 auto;
    padding: 32px 32px 80px;
}

.repl-panel {
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--rule);
    background: var(--screen-bg);
}

.repl-toggle {
    display: none;
}

@media (max-width: 1000px) {
    .chapter-layout {
        grid-template-columns: minmax(0, 1fr);
    }

    .chapter {
        padding: 24px 20px 120px;
    }

    .repl-panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: auto;
        height: 55vh;
        border-left: 0;
        border-top: 1px solid var(--rule);
        transform: translateY(100%);
        transition: transform 0.2s ease;
        z-index: 10;
    }

    .repl-open .repl-panel {
        transform: none;
    }

    .repl-toggle {
        display: block;
        position: fixed;
        right: 16px;
        bottom: 16px;
        z-index: 11;
        padding: 10px 18px;
        border: 0;
        border-radius: 999px;
        background: var(--accent);
        color: #fff;
        font-weight: 600;
        cursor: pointer;
    }

    .repl-open .repl-toggle {
        bottom: calc(55vh + 12px);
    }
}

/* ---- Prose ------------------------------------------------------------ */

h1 {
    font-size: 2.4em;
    line-height: 1.2;
    margin: 0.4em 0 0.6em;
}

h2 {
    margin-top: 2em;
}

.draft-label {
    font-size: 0.8em;
    color: var(--muted);
    border-bottom: 1px solid var(--rule);
    padding-bottom: 8px;
}

.header-block {
    border: 1px solid var(--rule);
    border-radius: 8px;
    padding: 4px 20px;
    font-size: 0.92em;
}

.header-block dt {
    font-weight: 600;
    margin-top: 12px;
}

.header-block dd {
    margin: 0 0 8px;
}

.placeholder {
    background: var(--placeholder);
    border-left: 4px solid #f0c040;
    padding: 10px 14px;
    margin: 1.2em 0;
    font-size: 0.92em;
    color: #5a4a10;
}

.placeholder > p {
    margin: 0;
}

.placeholder > p + p {
    margin-top: 0.5em;
}

.placeholder > :first-child::before {
    content: "Placeholder · ";
    font-weight: 600;
}

/* Author's notes from the plan and research; only in --all previews. */
.plan {
    background: #eef2ff;
    border-left: 4px solid var(--accent);
    padding: 10px 14px;
    margin: 1.2em 0;
    font-size: 0.9em;
}

.plan.research {
    background: #f1f8f1;
    border-left-color: #4a9a5a;
}

.plan > * {
    margin: 0.4em 0;
}

.plan-label {
    font-size: 0.8em;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}

.coming-from {
    border-left: 4px solid var(--muted);
    padding: 4px 14px;
    margin: 1.5em 0;
    font-size: 0.92em;
}

.chapter-nav {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin-top: 3em;
    padding-top: 1em;
    border-top: 1px solid var(--rule);
}

.chapter-nav a,
.draft-label a {
    color: var(--accent);
    text-decoration: none;
}

/* Chapters without a REPL (Part II on) use the full width. */
.no-repl .chapter-layout {
    grid-template-columns: minmax(0, 1fr);
}

.type-it {
    font-style: italic;
    color: var(--muted);
}

.knob {
    font-size: 0.95em;
}

code {
    background: var(--code-bg);
    padding: 1px 4px;
    border-radius: 3px;
}

pre.session,
pre.exercise-code {
    background: var(--code-bg);
    border-radius: 6px;
    padding: 14px 16px;
    overflow-x: auto;
    line-height: 1.45;
}

.under-the-hood,
.tutor,
.learned {
    border-top: 2px solid var(--ink);
    margin-top: 2.5em;
}

/* ---- Exercises -------------------------------------------------------- */

.exercise {
    border: 1px solid var(--rule);
    border-radius: 8px;
    padding: 4px 20px 16px;
    margin: 1.5em 0;
}

.exercise h3 {
    margin-bottom: 0.3em;
}

.exercise-editor,
.prediction {
    display: block;
    width: 100%;
    padding: 12px 14px;
    border: 1px solid #c9c9c9;
    border-radius: 6px;
    background: #fff;
    line-height: 1.45;
    resize: vertical;
    tab-size: 4;
}

.prediction-label {
    display: block;
    font-weight: 600;
    font-size: 0.95em;
    margin-top: 12px;
}

.prediction {
    margin-top: 6px;
}

.exercise-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 12px 0;
}

.exercise-actions button {
    padding: 8px 18px;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    font-size: 0.9em;
    font-weight: 600;
    cursor: pointer;
}

.exercise-actions button + button {
    background: #fff;
    color: var(--accent);
}

.exercise-actions button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.exercise-hint {
    font-size: 0.85em;
    color: var(--muted);
}

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

@media (max-width: 600px) {
    .exercise-result {
        grid-template-columns: minmax(0, 1fr);
    }
}

.exercise-result h4 {
    margin: 0 0 4px;
    font-size: 0.8em;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}

.exercise pre.actual,
.exercise pre.predicted {
    margin: 0;
    min-height: 3em;
    padding: 10px 12px;
    border-radius: 6px;
    white-space: pre-wrap;
    word-break: break-word;
}

.exercise pre.actual {
    background: var(--screen-bg);
    color: var(--screen-ink);
}

.exercise pre.predicted {
    background: var(--code-bg);
}

.exercise-knob {
    font-size: 0.9em;
    font-style: italic;
}

/* ---- REPL ------------------------------------------------------------- */

.repl-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    background: #2a2c33;
    color: var(--screen-ink);
    font-size: 13px;
}

.repl-status {
    flex: 1;
    color: var(--screen-note);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.repl-buttons {
    display: flex;
    gap: 6px;
}

.repl-buttons button {
    padding: 3px 10px;
    border: 1px solid #555;
    border-radius: 4px;
    background: transparent;
    color: var(--screen-ink);
    font-size: 12px;
    cursor: pointer;
}

.repl-buttons button:disabled {
    opacity: 0.35;
    cursor: default;
}

.repl-screen {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    color: var(--screen-ink);
    cursor: text;
}

.repl-output {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.45;
}

.repl-line {
    display: flex;
    line-height: 1.45;
}

.repl-prompt {
    font-family: var(--mono);
    font-size: 15px;
    white-space: pre;
    color: var(--screen-note);
}

.repl-input {
    flex: 1;
    padding: 0;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--screen-ink);
    caret-color: var(--screen-ink);
}

.out-stderr {
    color: var(--screen-err);
}

.out-note {
    color: var(--screen-note);
    font-style: italic;
}

.exercise .out-stderr {
    color: var(--screen-err);
}
