/* Tufte-inspired CSS for QED Explorer */

/* Tufte-style font stack: ET Book if self-hosted, otherwise Palatino family */
:root {
    --body-font: 'ET Book', Palatino, 'Palatino Linotype', 'Palatino LT STD',
                 'Book Antiqua', Georgia, serif;
    --code-font: Consolas, 'Liberation Mono', Menlo, Courier, monospace;
    --bg: #fffff8;
    --fg: #111;
    --fg-light: #555;
    --fg-lighter: #888;
    --accent: #a00000;
    --link: #a00000;
    --border: #ccc;
    --max-width: 80rem;
    --sidenote-width: 17rem;
}

* { box-sizing: border-box; }

html {
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
}

body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: var(--body-font);
    line-height: 1.6;
    counter-reset: sidenote-counter;
    overflow-x: hidden;
}

/* --- Layout --- */

article, .container {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 2rem 4rem;
}

/* --- Typography --- */

h1 {
    font-weight: 400;
    font-size: 2.6rem;
    line-height: 1.1;
    margin-top: 2rem;
    margin-bottom: 1.5rem;
}

h2 {
    font-weight: 400;
    font-style: italic;
    font-size: 1.6rem;
    line-height: 1.2;
    margin-top: 2.5rem;
    margin-bottom: 0.8rem;
}

h3 {
    font-weight: 400;
    font-style: italic;
    font-size: 1.2rem;
    margin-top: 2rem;
    margin-bottom: 0.5rem;
}

p, ol, ul {
    font-size: 1.05rem;
    line-height: 1.7;
    margin-bottom: 1.2rem;
}

p {
    width: 85%;
}

/* Full-width paragraphs in certain contexts */
.full-width p,
.card p,
.diagram-meta p,
table p {
    width: 100%;
}

blockquote {
    font-size: 1rem;
    margin: 1.5rem 0;
    padding-left: 1.5rem;
    border-left: 3px solid var(--border);
    color: var(--fg-light);
}

blockquote p { width: 100%; }

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

a:hover {
    text-decoration: underline;
}

/* --- Sidenotes (Tufte signature) --- */

.sidenote, .marginnote {
    float: right;
    clear: right;
    margin-right: -40%;
    width: 35%;
    font-size: 0.85rem;
    line-height: 1.4;
    color: var(--fg-light);
    margin-bottom: 1rem;
}

.sidenote-number {
    counter-increment: sidenote-counter;
}

.sidenote-number::after {
    content: counter(sidenote-counter);
    font-size: 0.7rem;
    vertical-align: super;
    color: var(--accent);
}

.sidenote::before {
    content: counter(sidenote-counter) ". ";
    font-size: 0.7rem;
    vertical-align: super;
    color: var(--accent);
}

/* --- Navigation --- */

nav {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 1rem 4rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 2rem;
    align-items: baseline;
}

nav .site-title {
    font-size: 1.2rem;
    font-weight: 400;
    color: var(--fg);
    text-decoration: none;
}

nav .site-title:hover { text-decoration: none; }

nav a {
    font-size: 0.9rem;
    color: var(--fg-light);
    text-decoration: none;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

nav a:hover { color: var(--fg); }

nav a.nav-active {
    color: var(--fg);
    border-bottom: 2px solid var(--accent);
    padding-bottom: 0.15rem;
}

/* --- Breadcrumbs --- */

.breadcrumbs {
    font-size: 0.82rem;
    color: var(--fg-lighter);
    margin-bottom: 0.5rem;
    margin-top: -0.5rem;
}

.breadcrumbs a {
    color: var(--fg-light);
}

.breadcrumbs .sep {
    margin: 0 0.4rem;
    color: var(--border);
}

/* --- Tables --- */

table {
    border-collapse: collapse;
    font-size: 0.95rem;
    width: auto;
    margin: 1.5rem 0;
}

thead th {
    border-bottom: 2px solid var(--fg);
    padding: 0.4rem 1rem 0.4rem 0;
    text-align: left;
    font-weight: 400;
    font-style: italic;
}

tbody td {
    padding: 0.35rem 1rem 0.35rem 0;
    border-bottom: 1px solid #e8e8e8;
    vertical-align: top;
}

/* --- Cards for process/diagram grids --- */

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin: 1.5rem 0;
    width: 85%;
}

.card {
    border: 1px solid var(--border);
    padding: 0;
    background: white;
    transition: border-color 0.15s;
    display: flex;
    flex-direction: column;
}

.card:hover {
    border-color: var(--fg-light);
}

.card a {
    text-decoration: none;
    color: inherit;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.card-preview {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 150px;
    background: #fafaf5;
    border-bottom: 1px solid #eee;
    padding: 0.5rem;
    flex-shrink: 0;
}

.card-preview svg {
    max-width: 100%;
    max-height: 100%;
    height: auto;
    width: auto;
}

.card-body {
    padding: 0.9rem 1.2rem;
    flex: 1;
}

.card-body h3 {
    margin-top: 0;
    margin-bottom: 0.3rem;
    font-style: normal;
    font-size: 1.05rem;
}

.card-body .subtitle {
    font-size: 0.85rem;
    color: var(--fg-light);
    margin-bottom: 0.5rem;
}

.card-body p {
    font-size: 0.88rem;
    margin-bottom: 0.5rem;
}

/* Tag container inside cards */
.card-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem;
    margin-bottom: 0.4rem;
}

.card-tags .tag {
    margin: 0;
}

/* --- Tags / badges --- */

.tag {
    display: inline-block;
    font-size: 0.72rem;
    padding: 0.15rem 0.5rem;
    border: 1px solid var(--border);
    color: var(--fg-light);
    letter-spacing: 0.03em;
    text-transform: uppercase;
    margin-right: 0.3rem;
    margin-bottom: 0.3rem;
}

a.tag {
    text-decoration: none;
    cursor: pointer;
}

a.tag:hover {
    border-color: var(--fg);
    color: var(--fg);
}

/* --- Tooltips --- */

.has-tooltip {
    position: relative;
    border-bottom: 1px dotted var(--fg-lighter);
    cursor: help;
}

.has-tooltip .tooltip {
    display: none;
    position: absolute;
    left: 0;
    top: 1.6em;
    width: 300px;
    padding: 0.7rem 0.9rem;
    background: white;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    font-size: 0.82rem;
    line-height: 1.5;
    color: var(--fg-light);
    z-index: 10;
    font-style: normal;
    font-weight: normal;
}

.has-tooltip:hover .tooltip {
    display: block;
}

.tag.tree { border-color: #4a9; color: #2a7a5a; }
.tag.oneloop { border-color: #c90; color: #a80; }
.tag.twoloop { border-color: #c6e; color: #93b; }

/* Divergence badges */
.tag.uv { border-color: #48e; color: #26c; }
.tag.ir { border-color: #e44; color: #c22; }
.tag.finite { border-color: #4a9; color: #2a7a5a; }

/* --- Diagram detail page --- */

.diagram-header {
    display: flex;
    gap: 2rem;
    align-items: flex-start;
    margin-bottom: 2rem;
}

.diagram-svg {
    flex: 0 0 280px;
    min-height: 180px;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    padding: 1rem;
}

.diagram-svg svg {
    max-width: 100%;
    height: auto;
}

.diagram-meta {
    flex: 1;
}

/* --- Chain steps (the core pedagogical feature) --- */

.chain {
    margin: 2rem 0;
    width: 85%;
}

.chain-step {
    border-left: 3px solid var(--border);
    padding: 0.8rem 0 0.8rem 1.5rem;
    margin-bottom: 0;
    position: relative;
}

.chain-step::before {
    content: attr(data-step);
    position: absolute;
    left: -0.9rem;
    top: 0.7rem;
    background: var(--bg);
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    border: 2px solid var(--border);
    text-align: center;
    line-height: 1.5rem;
    font-size: 0.7rem;
    color: var(--fg-light);
}

.chain-step .step-title {
    font-weight: 600;
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--fg-light);
    margin-bottom: 0.4rem;
}

.chain-step .step-math {
    margin: 0.5rem 0;
    overflow-x: auto;
}

.chain-step .step-explanation {
    font-size: 0.92rem;
    color: var(--fg-light);
    line-height: 1.5;
}

/* --- Process detail: diagram listing --- */

.process-diagrams {
    width: 85%;
}

.process-diagram-row {
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
    padding: 1rem 0;
    border-bottom: 1px solid #eee;
}

.process-diagram-row .mini-svg {
    flex: 0 0 120px;
    height: 80px;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    font-size: 0.75rem;
    color: var(--fg-lighter);
}

.process-diagram-row .info { flex: 1; }
.process-diagram-row .info h3 { margin: 0 0 0.3rem 0; font-size: 1rem; }

/* --- KaTeX overrides --- */

.katex-display {
    margin: 1rem 0;
    overflow-x: auto;
    overflow-y: hidden;
}

/* --- Topic body --- */

.topic-body {
    width: 85%;
    line-height: 1.8;
}

.topic-body p {
    width: 100%;
    margin-bottom: 1.4rem;
}

.topic-body h2 {
    margin-top: 2.5rem;
}

.topic-body h3 {
    margin-top: 2rem;
}

/* --- Table of contents (Learn page) --- */

.toc {
    width: 70%;
}

.toc-part {
    margin-top: 2rem;
    margin-bottom: 0.6rem;
    font-size: 1.15rem;
}

.toc-chapter {
    margin-bottom: 0.15rem;
}

.toc-chapter-link {
    display: inline;
    text-decoration: none;
    color: inherit;
}

.toc-chapter-link:hover .toc-title {
    color: var(--link);
}

.toc-chapter-link .toc-number {
    display: inline-block;
    min-width: 2rem;
    color: var(--fg-lighter);
    font-size: 0.95rem;
}

.toc-chapter-link .toc-title {
    font-size: 1.05rem;
}

.toc-sections {
    margin-left: 2rem;
    margin-bottom: 0.4rem;
}

.toc-section-link {
    display: block;
    text-decoration: none;
    color: var(--fg-light);
    padding: 0.05rem 0;
    font-size: 0.9rem;
}

.toc-section-link:hover .toc-title {
    color: var(--link);
}

.toc-section-link .toc-number {
    display: inline-block;
    min-width: 2.5rem;
    color: var(--fg-lighter);
    font-size: 0.85rem;
}

/* --- Chapter detail --- */

.chapter-header .chapter-label {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--fg-lighter);
}

/* Section table of contents within a chapter */
.section-toc {
    width: 55%;
    margin: 1.5rem 0 2rem 0;
    padding: 1rem 1.5rem;
    border: 1px solid #eee;
    background: #fafaf5;
}

.section-toc a {
    display: block;
    color: var(--fg-light);
    text-decoration: none;
    font-size: 0.95rem;
    padding: 0.25rem 0;
}

.section-toc a:hover {
    color: var(--fg);
}

/* Section dividers */
.chapter-section {
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid #eee;
}

.chapter-section h2 {
    font-size: 1.5rem;
    margin-top: 0;
}

.chapter-body {
    width: 55%;
    line-height: 1.8;
    position: relative;
}

.chapter-body p {
    width: 100%;
    margin-bottom: 1.4rem;
}

.chapter-body h2 {
    margin-top: 2.5rem;
}

.chapter-body .sidenote,
.chapter-body .marginnote {
    float: right;
    margin-right: -75%;
    width: 60%;
    font-size: 0.85rem;
    line-height: 1.4;
    color: var(--fg-light);
    margin-bottom: 0.5rem;
}

.chapter-body .marginnote {
    font-style: italic;
}

/* Figures in the margin — wider, no italic, no clear so they
   appear at the right vertical position */
.chapter-body .margin-figure {
    float: right;
    clear: none;
    margin-right: -75%;
    width: 60%;
    margin-bottom: 1rem;
    font-style: normal;
    font-size: 0.82rem;
    color: var(--fg-light);
    line-height: 1.4;
}

.chapter-body .margin-figure img {
    width: 100%;
    display: block;
    margin-bottom: 0.3rem;
}

/* Full-width figures within the text column */
.chapter-body .full-figure {
    width: 100%;
    margin: 1.5rem 0;
    text-align: center;
}

.chapter-body .full-figure img {
    max-width: 100%;
}

.chapter-body .full-figure .caption {
    font-size: 0.85rem;
    color: var(--fg-light);
    font-style: italic;
    margin-top: 0.4rem;
}

.chapter-nav {
    display: flex;
    justify-content: space-between;
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
    width: 85%;
}

.chapter-nav a {
    color: var(--link);
    text-decoration: none;
    font-size: 0.95rem;
}

.chapter-nav a:hover {
    text-decoration: underline;
}

.chapter-prev { text-align: left; }
.chapter-next { text-align: right; }

/* --- Responsive --- */

@media (max-width: 900px) {
    html { overflow-x: hidden; }
    article, .container { padding: 1rem 1rem; }
    p { width: 100%; }
    .card-grid { width: 100%; }
    .chain { width: 100%; }
    .process-diagrams { width: 100%; }

    nav {
        padding: 0.8rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem 1rem;
    }
    nav .site-title {
        width: 100%;
        margin-bottom: 0.2rem;
    }
    nav a {
        font-size: 0.78rem;
        letter-spacing: 0.02em;
    }

    .sidenote, .marginnote {
        display: block;
        float: none;
        margin: 0.5rem 0 1rem 1rem;
        width: auto;
    }
    .diagram-header { flex-direction: column; }
    .diagram-svg { flex: none; width: 100%; max-width: 320px; }

    .toc { width: 100%; }
    .section-toc { width: 100%; }
    .topic-body, .chapter-body { width: 100%; }
    .chapter-nav { width: 100%; }

    .breadcrumbs { font-size: 0.75rem; }

    table { font-size: 0.85rem; display: block; overflow-x: auto; }
}
