/* Theme tokens: tp-* are this site's palette; pico-* map them onto Pico.
   Dark is the base. Light applies when the browser prefers light (no JS needed),
   or when the toggle sets data-theme="light". data-theme="dark" overrides a
   light browser preference. Pico follows the same rules on its own. */
:root {
    --tp-bg: #0f1419;
    --tp-bg-card: #1a2028;
    --tp-text: #c9d1d9;
    --tp-heading: #e6edf3;
    --tp-muted: #7d8590;
    --tp-border: #30363d;
    --tp-accent: #58a6ff;
    --tp-accent-hover: #79c0ff;
    --tp-accent-dim: #2d5a88;
    --tp-layer-data: #c0392b;
    --tp-layer-logic: #2c5aa0;
    --tp-layer-presentation: #27873b;
    --tp-layer-routing: #d4a517;
    --tp-logo-filter: invert(1);
    --tp-texture: url('parchment-speckle-dark.jpg');
}
@media (prefers-color-scheme: light) {
    html:not([data-theme="dark"]) {
        --tp-bg: #f5f0e8;
        --tp-bg-card: #ebe5d9;
        --tp-text: #3D372C;
        --tp-heading: #1a1816;
        --tp-muted: #7A756A;
        --tp-border: #d5cfc4;
        --tp-accent: #2c5aa0;
        --tp-accent-hover: #1a3a6a;
        --tp-accent-dim: #1a3a6a;
        --tp-logo-filter: none;
        --tp-texture: url('parchment-speckle-light.jpg');
    }
}
html[data-theme="light"] {
    --tp-bg: #f5f0e8;
    --tp-bg-card: #ebe5d9;
    --tp-text: #3D372C;
    --tp-heading: #1a1816;
    --tp-muted: #7A756A;
    --tp-border: #d5cfc4;
    --tp-accent: #2c5aa0;
    --tp-accent-hover: #1a3a6a;
    --tp-accent-dim: #1a3a6a;
    --tp-logo-filter: none;
    --tp-texture: url('parchment-speckle-light.jpg');
}
/* Pico's theme selectors reach (0,2,0) specificity (:root:not([data-theme])).
   :root:root:root is (0,3,0), so this palette wins in every theme state. */
:root:root:root {
    --pico-background-color: var(--tp-bg);
    --pico-color: var(--tp-text);
    --pico-h1-color: var(--tp-heading);
    --pico-h2-color: var(--tp-heading);
    --pico-h3-color: var(--tp-heading);
    --pico-muted-color: var(--tp-muted);
    --pico-primary: var(--tp-accent);
    --pico-primary-hover: var(--tp-accent-hover);
    --pico-blockquote-border-color: var(--tp-border);
}

/* Parchment texture. !important because Pico sets its own background on html/body. */
html, html body {
    background-color: var(--tp-bg) !important;
    background-image: var(--tp-texture) !important;
    background-repeat: repeat !important;
    background-size: 512px 512px !important;
}

/* Brand mark: inverted on the dark theme. */
.logo-mark { width: auto; opacity: 0.85; filter: var(--tp-logo-filter); }

/* Hero */
.hero { text-align: center; padding: 5rem 1rem 3rem; }
.hero h1 {
    display: inline-flex; align-items: center; gap: 0.5rem;
    font-size: clamp(2.4rem, 5vw, 3.4rem); margin-bottom: 0.5rem;
    font-weight: 700; letter-spacing: -0.02em;
}
.hero h1 .logo-mark { height: 0.9em; }
.hero .tagline { color: var(--tp-muted); font-size: 1.15rem; max-width: 32rem; margin: 0 auto; line-height: 1.6; }

/* Section divider: a plain <hr>, drawn as three dots. */
hr.divider { border: 0; height: auto; margin: 0; padding: 2rem 0; text-align: center; color: var(--tp-border); background: none; }
hr.divider::after { content: "\00B7  \00B7  \00B7"; white-space: pre; font-size: 1.2rem; letter-spacing: 0.5em; }

/* Sections */
.section { max-width: 42rem; margin: 0 auto; padding: 2.5rem 1rem; }
.section h2 { font-size: 1.4rem; font-weight: 700; margin-bottom: 1.5rem; }
.section h3 { font-size: 1.1rem; font-weight: 600; margin-bottom: 0.75rem; margin-top: 2rem; }
.section p { line-height: 1.85; margin-bottom: 1.25rem; }
.section a { color: var(--tp-accent); text-decoration: none; }
.section a:hover { color: var(--tp-accent-hover); }

/* The Two Rules: an ordered list, numbered "#1", "#2" by CSS counter. */
ol.rules { list-style: none; padding: 0; margin: 0 0 1.25rem; counter-reset: rule; }
ol.rules > li {
    counter-increment: rule; list-style: none;
    background: var(--tp-bg-card); border: 1px solid var(--tp-border); border-radius: 8px;
    padding: 1.75rem; margin-bottom: 1.25rem;
}
ol.rules > li::before {
    content: "#" counter(rule); float: left; margin-right: 1.25rem;
    font-family: Georgia, serif; font-size: 2.4rem; font-weight: 700; line-height: 1; color: var(--tp-muted);
}
ol.rules h3 { margin: 0 0 0.75rem; font-size: 1.2rem; font-weight: 600; color: var(--tp-heading); }
ol.rules p { color: var(--tp-text); line-height: 1.75; margin: 0; font-size: 0.95rem; }

/* Derived principles: an ordered list, numbered in roman numerals by CSS counter. */
ol.principles {
    list-style: none; padding: 0; margin: 1.5rem 0; counter-reset: principle;
    display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;
}
ol.principles > li {
    counter-increment: principle; list-style: none; margin: 0;
    background: var(--tp-bg-card); border: 1px solid var(--tp-border); border-radius: 6px; padding: 1.25rem;
}
ol.principles > li::before {
    content: counter(principle, upper-roman); display: block;
    font-size: 0.75rem; color: var(--tp-muted); letter-spacing: 0.1em; margin-bottom: 0.35rem;
}
ol.principles h3 { margin: 0 0 0.5rem; font-size: 0.95rem; font-weight: 600; color: var(--tp-heading); }
ol.principles p { font-size: 0.85rem; color: var(--tp-muted); line-height: 1.6; margin: 0; }
/* An odd count leaves the last card alone on its row: let it span the row. */
ol.principles > li:last-child:nth-child(odd) { grid-column: 1 / -1; }
@media (max-width: 600px) { ol.principles { grid-template-columns: 1fr; } }

/* DLPR: figure beside a definition list; each layer's letter is drawn from data-letter. */
.dlpr-layout { display: flex; gap: 2.5rem; align-items: flex-start; margin: 2rem 0; }
.dlpr-layout > figure { flex-shrink: 0; width: 200px; margin: 0; }
.dlpr-layout > figure img { width: 100%; height: auto; }
dl.dlpr-layers { flex: 1; margin: 1.5rem 0; }
dl.dlpr-layers > div {
    display: grid; grid-template-columns: 1.5rem 1fr; column-gap: 1rem;
    padding: 0.75rem 0; border-bottom: 1px solid var(--tp-border);
}
dl.dlpr-layers > div:last-child { border-bottom: none; }
dl.dlpr-layers > div::before {
    content: attr(data-letter); grid-row: span 2;
    font-family: Georgia, serif; font-size: 1.4rem; font-weight: 700; line-height: 1.2;
}
dl.dlpr-layers > div[data-letter="D"]::before { color: var(--tp-layer-data); }
dl.dlpr-layers > div[data-letter="L"]::before { color: var(--tp-layer-logic); }
dl.dlpr-layers > div[data-letter="P"]::before { color: var(--tp-layer-presentation); }
dl.dlpr-layers > div[data-letter="R"]::before { color: var(--tp-layer-routing); }
dl.dlpr-layers dt { grid-column: 2; margin: 0; font-weight: 700; color: var(--tp-heading); }
dl.dlpr-layers dd { grid-column: 2; margin: 0; font-size: 0.95rem; color: var(--tp-text); line-height: 1.6; }
dl.dlpr-layers .question { font-weight: 600; }
@media (max-width: 600px) {
    .dlpr-layout { flex-direction: column; align-items: center; }
    .dlpr-layout > figure { width: 160px; }
}

/* Settled decisions: each decision (dt) with its one-line reason (dd). */
dl.decisions { margin: 1.5rem 0; }
dl.decisions dt {
    margin: 0; padding-top: 0.9rem; border-top: 1px solid var(--tp-border);
    font-weight: 600; color: var(--tp-heading);
}
dl.decisions dt:first-of-type { border-top: none; padding-top: 0; }
dl.decisions dd { margin: 0.25rem 0 0.9rem; font-size: 0.95rem; color: var(--tp-text); line-height: 1.6; }

/* Closing quotation */
figure.quote { margin: 1.5rem 0; }
figure.quote blockquote { border-left: 3px solid var(--tp-border); padding: 0.75rem 1.25rem; margin: 0; font-style: italic; color: var(--tp-muted); }
figure.quote blockquote p { margin: 0; line-height: 1.85; }
figure.quote figcaption { padding-left: 1.25rem; margin-top: 0.5rem; font-size: 0.8rem; color: var(--tp-border); }
figure.quote cite { font-style: normal; }

/* Footer */
footer { text-align: center; padding: 3rem 1rem; font-size: 0.8rem; color: var(--tp-muted); border-top: 1px solid var(--tp-border); }
footer .brand-badge { margin-top: 1rem; }

/* About page: the mark shown large, above its explanation. */
.section p.mark { text-align: center; margin: 0.5rem 0 1.5rem; }
.section p.mark .logo-mark { height: 4rem; }

/* Theme toggle: hidden until theme.js reveals it, so a page without JS has no dead button. */
#theme-toggle {
    position: fixed; top: 1rem; right: 1rem; z-index: 999;
    width: auto; margin: 0; padding: 0.3rem 0.7rem;
    background: none; border: 1px solid var(--pico-muted-color); border-radius: 0.2rem;
    color: var(--pico-muted-color); font-size: 0.75rem; letter-spacing: 0.06em; text-transform: uppercase;
    cursor: pointer; opacity: 0.6; transition: opacity 0.2s;
}
#theme-toggle:hover, #theme-toggle:focus-visible { opacity: 1; }
#theme-toggle[hidden] { display: none; }

/* Compact header on inner pages: the brand links home; the page's own h1 is in main. */
.hero.compact { padding: 2rem 1rem 1rem; }
.hero .brand { margin: 0; font-size: 1.4rem; font-weight: 700; letter-spacing: -0.02em; }
.hero .brand a { display: inline-flex; align-items: center; gap: 0.4rem; color: var(--tp-heading); text-decoration: none; }
.hero .brand .logo-mark { height: 0.9em; }
.section h1 { font-size: 1.8rem; font-weight: 700; margin-bottom: 1.5rem; }

/* Series nav: one list, wraps on narrow screens; the current page is marked by aria-current. */
nav.series { display: block; max-width: 42rem; margin: 0 auto; padding: 0 1rem; }
nav.series ol {
    display: flex; flex-wrap: wrap; justify-content: center; gap: 0.25rem 1.25rem;
    list-style: none; margin: 0; padding: 0.75rem 0;
    border-top: 1px solid var(--tp-border); border-bottom: 1px solid var(--tp-border);
}
nav.series li { display: block; margin: 0; padding: 0; list-style: none; }
nav.series a { color: var(--tp-muted); text-decoration: none; font-size: 0.9rem; }
nav.series a:hover { color: var(--tp-accent-hover); }
nav.series a[aria-current="page"] { color: var(--tp-heading); font-weight: 600; }

/* Rule cards holding more than one paragraph. */
ol.rules p + p { margin-top: 0.85rem; }

/* Principles: cards may carry more than one paragraph. */
ol.principles p + p { margin-top: 0.6rem; }

/* Principle grid: scrolls inside its own box so the page never scrolls sideways. */
.grid-table { overflow-x: auto; margin: 1.5rem 0 0.75rem; }
.grid-table table { min-width: 40rem; margin: 0; font-size: 0.9rem; }
.grid-table caption { caption-side: bottom; padding-top: 0.5rem; font-size: 0.8rem; color: var(--tp-muted); text-align: left; }
.grid-table th, .grid-table td { background: transparent; vertical-align: top; }
.grid-table th[scope="row"] { color: var(--tp-heading); white-space: nowrap; }
.grid-table tr.everyone > * { border-top: 2px solid var(--tp-border); }
.grid-table td.spans { vertical-align: middle; border-left: 1px solid var(--tp-border); }

/* Practice examples: principle references above each description. */
dl.examples dd p { margin: 0 0 0.5rem; line-height: 1.6; }
dl.examples .principle-refs { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; }

/* Prev/next pager: either end of one row, stacked on narrow screens. */
nav.pager {
    display: flex; justify-content: space-between; gap: 1rem;
    max-width: 42rem; margin: 0 auto; padding: 1.5rem 1rem 2.5rem;
}
nav.pager a { color: var(--tp-accent); text-decoration: none; }
nav.pager a:hover { color: var(--tp-accent-hover); }
nav.pager .next { margin-left: auto; text-align: right; }
@media (max-width: 600px) {
    nav.pager { flex-direction: column; }
    nav.pager .next { margin-left: 0; text-align: left; }
}
