/* =============================================
   Polestar: Drift-Aware Cache Calibration
   Interactive companion · stylesheet
   ============================================= */

:root {
    --bg: #fbfaf7;
    --bg-elevated: #ffffff;
    --ink: #15212e;
    --ink-soft: #3a4a5c;
    --ink-muted: #6b7785;
    --ink-faint: #a4adb8;
    --rule: #e3e0d8;
    --rule-strong: #c9c4b6;

    --upstream: #1f3a52;
    --upstream-soft: #2c537a;
    --midmarket: #4a7396;
    --downstream: #7aa6cc;
    --downstream-soft: #a4c2dc;
    --feedback: #16a085;
    --feedback-soft: #2bbfa1;

    --accent: #c8392c;
    --accent-soft: #e25646;
    --accent-glow: rgba(200, 57, 44, 0.12);
    --warn: #d18b1f;
    --good: #16a085;

    --drift: #6b46c1;
    --drift-soft: #9b7fd4;
    --cache: #1f3a52;
    --commit: #c8392c;
    --system: #16a085;

    --serif: 'Newsreader', 'Source Serif Pro', Georgia, 'Times New Roman', serif;
    --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --mono: 'JetBrains Mono', 'SF Mono', Menlo, monospace;

    --max-width: 920px;
    --shadow-sm: 0 1px 2px rgba(21, 33, 46, 0.04), 0 2px 6px rgba(21, 33, 46, 0.04);
    --shadow-md: 0 2px 4px rgba(21, 33, 46, 0.06), 0 8px 24px rgba(21, 33, 46, 0.06);
    --shadow-lg: 0 8px 16px rgba(21, 33, 46, 0.08), 0 24px 60px rgba(21, 33, 46, 0.10);
}

* { box-sizing: border-box; }
html, body {
    margin: 0; padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--serif);
    font-size: 18px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}
body { overflow-x: hidden; }

h1, h2, h3, h4 { font-family: var(--serif); font-weight: 600; line-height: 1.2; letter-spacing: -0.01em; }
h2 { font-size: 2.6rem; margin: 0 0 0.8rem 0; }
h3 { font-size: 1.5rem; margin: 2.4rem 0 0.8rem 0; }
h4 { font-size: 1.15rem; margin: 0 0 0.6rem 0; font-weight: 600; }

p { margin: 0 0 1.2rem 0; }
.muted { color: var(--ink-muted); font-size: 0.92em; }
em { font-style: italic; color: inherit; }
b, strong { font-weight: 600; color: var(--ink); }
code { font-family: var(--mono); font-size: 0.88em; background: rgba(31,58,82,0.06); padding: 1px 5px; border-radius: 3px; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ========== NAV BAR ========== */
.nav-bar {
    position: fixed; top: 0; left: 0; right: 0;
    background: rgba(251, 250, 247, 0.92);
    backdrop-filter: saturate(1.4) blur(10px);
    -webkit-backdrop-filter: saturate(1.4) blur(10px);
    border-bottom: 1px solid var(--rule);
    z-index: 100;
    height: 56px;
}
.nav-inner {
    max-width: 1280px; margin: 0 auto; padding: 0 28px;
    height: 100%;
    display: flex; align-items: center; justify-content: space-between;
    position: relative;
}
.nav-title { display: flex; flex-direction: column; line-height: 1.15; }
.nav-paper { font-family: var(--serif); font-weight: 600; font-size: 0.96rem; color: var(--ink); }
.nav-sub { font-family: var(--sans); font-size: 0.72rem; color: var(--ink-muted); letter-spacing: 0.02em; text-transform: uppercase; }
.nav-progress { position: absolute; bottom: -1px; left: 0; right: 0; height: 2px; background: transparent; }
.nav-progress-fill {
    height: 100%; width: 0%;
    background: linear-gradient(90deg, var(--upstream), var(--accent));
    transition: width 0.15s linear;
}

/* ========== CHAPTER RAIL ========== */
.chapter-rail { position: fixed; left: 28px; top: 50%; transform: translateY(-50%); z-index: 50; }
.chapter-rail ol { list-style: none; margin: 0; padding: 0; }
.chapter-rail li {
    display: flex; align-items: center; gap: 0.7rem;
    padding: 0.4rem 0; cursor: pointer;
    color: var(--ink-faint); font-family: var(--sans);
    font-size: 0.78rem; font-weight: 500; letter-spacing: 0.01em;
    transition: color 0.2s;
}
.chapter-rail li:hover { color: var(--ink); }
.chapter-rail li.active { color: var(--accent); font-weight: 600; }
.chapter-rail .dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--ink-faint); transition: all 0.2s;
}
.chapter-rail li.active .dot {
    background: var(--accent); box-shadow: 0 0 0 4px var(--accent-glow); transform: scale(1.2);
}
.chapter-rail .label {
    opacity: 0; transform: translateX(-8px);
    transition: opacity 0.18s, transform 0.18s;
    pointer-events: none; white-space: nowrap;
}
.chapter-rail:hover .label,
.chapter-rail li.active .label { opacity: 1; transform: translateX(0); }

@media (max-width: 1100px) { .chapter-rail { display: none; } }

/* ========== CHAPTER LAYOUT ========== */
.chapter {
    max-width: var(--max-width); margin: 0 auto;
    padding: 7rem 32px 6rem 32px;
    border-bottom: 1px solid var(--rule);
}
.chapter:last-of-type { border-bottom: none; }
.chapter-head { margin-bottom: 3rem; }
.chapter-num {
    font-family: var(--sans); font-size: 0.74rem; font-weight: 600;
    letter-spacing: 0.18em; text-transform: uppercase;
    color: var(--accent); margin-bottom: 0.6rem;
}
.lede { font-size: 1.18rem; line-height: 1.55; color: var(--ink-soft); max-width: 56ch; margin-top: 1rem; }
.prose { max-width: 64ch; }
.prose ul, .prose ol { margin: 1.2rem 0; padding-left: 1.4rem; }
.prose li { margin-bottom: 0.7rem; }

.flow-list { counter-reset: flow; list-style: none; padding-left: 0; }
.flow-list li {
    counter-increment: flow; position: relative;
    padding-left: 2.4rem; margin-bottom: 0.85rem;
}
.flow-list li::before {
    content: counter(flow); position: absolute;
    left: 0; top: 0.05em;
    width: 1.7rem; height: 1.7rem; border-radius: 50%;
    background: var(--upstream); color: white;
    font-family: var(--sans); font-weight: 600; font-size: 0.82rem;
    display: flex; align-items: center; justify-content: center;
}

/* ========== COVER ========== */
.cover {
    min-height: 100vh; max-width: none; padding: 0;
    border-bottom: 1px solid var(--rule);
    position: relative; display: flex;
    align-items: center; justify-content: center; overflow: hidden;
}
.cover-inner { max-width: var(--max-width); padding: 6rem 32px; position: relative; z-index: 2; }
.cover-bg {
    position: absolute; inset: 0;
    background:
        radial-gradient(ellipse at 20% 20%, rgba(107, 70, 193, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 70%, rgba(31, 58, 82, 0.10) 0%, transparent 55%),
        radial-gradient(ellipse at 50% 100%, rgba(22, 160, 133, 0.06) 0%, transparent 50%);
    z-index: 1;
}
.cover-eyebrow {
    font-family: var(--sans); font-size: 0.78rem; font-weight: 500;
    letter-spacing: 0.22em; text-transform: uppercase;
    color: var(--accent); margin-bottom: 1.2rem;
}
.cover-title {
    font-size: clamp(2.2rem, 5.5vw, 4.2rem);
    font-weight: 600; line-height: 1.08;
    margin: 0 0 1.2rem 0; letter-spacing: -0.025em;
}
.cover-title em { font-style: italic; color: var(--accent); }
.cover-subtitle { font-size: 1.3rem; color: var(--ink-soft); font-style: italic; margin-bottom: 2.4rem; max-width: 40ch; }
.cover-byline { font-family: var(--sans); font-size: 0.95rem; color: var(--ink-soft); margin-bottom: 2.6rem; }

.cover-abstract {
    display: grid; grid-template-columns: repeat(3, 1fr);
    align-items: start; column-gap: 2.4rem; row-gap: 1.2rem;
    margin: 0 0 3.6rem 0; padding-top: 1.6rem;
    border-top: 1px solid var(--rule);
}
.cover-abstract-label {
    grid-column: 1 / -1;
    font-family: var(--sans); font-size: 0.72rem; font-weight: 600;
    letter-spacing: 0.22em; text-transform: uppercase;
    color: var(--accent); margin: 0;
}
.cover-abstract p {
    font-family: var(--serif); font-size: 1.02rem; line-height: 1.65;
    color: var(--ink-soft); margin: 0;
    padding-top: 1rem; border-top: 1px solid var(--rule); position: relative;
}
.cover-abstract p::before {
    content: counter(abstract-step, upper-roman);
    counter-increment: abstract-step;
    position: absolute; top: 0.85rem; right: 0;
    font-family: var(--serif); font-style: italic; font-size: 0.95rem;
    color: var(--accent); background: var(--bg); padding: 0 6px;
    transform: translateY(-50%);
}
.cover-abstract { counter-reset: abstract-step; }
@media (max-width: 820px) { .cover-abstract { grid-template-columns: 1fr; } }
.cover-abstract b, .cover-abstract strong { color: var(--ink); font-weight: 600; }
.cover-abstract em { color: var(--accent); font-style: italic; }

.cover-pillars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.6rem; margin-bottom: 3rem; }
.pillar {
    padding: 1.6rem; background: var(--bg-elevated);
    border: 1px solid var(--rule); border-radius: 6px;
    box-shadow: var(--shadow-sm); position: relative;
    transition: transform 0.25s, box-shadow 0.25s;
}
.pillar:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.pillar-num {
    position: absolute; top: -10px; right: 16px;
    font-family: var(--serif); font-style: italic; font-size: 1.1rem;
    color: var(--accent); background: var(--bg-elevated); padding: 0 6px;
}
.pillar-title { font-family: var(--serif); font-weight: 600; font-size: 1.15rem; margin-bottom: 0.6rem; color: var(--ink); }
.pillar-text { font-size: 0.95rem; line-height: 1.55; color: var(--ink-soft); }
.pillar-text em { color: var(--accent); font-style: italic; }
@media (max-width: 720px) { .cover-pillars { grid-template-columns: 1fr; } }

.cover-cta { display: flex; flex-direction: column; align-items: flex-start; gap: 1rem; }
.cover-meta { font-family: var(--sans); font-size: 0.85rem; }

/* ========== BUTTONS ========== */
.primary-btn {
    background: var(--ink); color: white; border: none;
    padding: 0.85rem 1.6rem; font-family: var(--sans);
    font-size: 0.94rem; font-weight: 500; letter-spacing: 0.01em;
    border-radius: 4px; cursor: pointer;
    transition: background 0.2s, transform 0.15s; box-shadow: var(--shadow-sm);
}
.primary-btn:hover { background: var(--accent); transform: translateY(-1px); }
.primary-btn:active { transform: translateY(0); }
.ghost-btn {
    background: transparent; color: var(--ink-soft);
    border: 1px solid var(--rule-strong);
    padding: 0.8rem 1.4rem; font-family: var(--sans);
    font-size: 0.9rem; font-weight: 500; border-radius: 4px;
    cursor: pointer; transition: all 0.2s;
}
.ghost-btn:hover { color: var(--ink); border-color: var(--ink-soft); background: rgba(0,0,0,0.02); }
.btn-row { display: flex; gap: 0.7rem; flex-wrap: wrap; margin-top: 1rem; }

/* ========== CALLOUTS ========== */
.callout {
    margin: 3rem 0; padding: 1.6rem 1.8rem;
    background: linear-gradient(180deg, rgba(200, 57, 44, 0.04), rgba(200, 57, 44, 0.02));
    border-left: 3px solid var(--accent); border-radius: 0 6px 6px 0;
}
.callout.subtle {
    background: rgba(31, 58, 82, 0.04); border-left-color: var(--upstream);
}
.callout-label {
    font-family: var(--sans); font-size: 0.75rem; font-weight: 600;
    letter-spacing: 0.16em; text-transform: uppercase;
    color: var(--accent); margin-bottom: 0.6rem;
}
.callout.subtle .callout-label { color: var(--upstream); }
.callout-body { font-size: 1rem; line-height: 1.6; color: var(--ink-soft); }
.callout blockquote {
    margin: 0.8rem 0; padding-left: 1rem;
    border-left: 2px solid var(--accent);
    font-style: italic; color: var(--ink); font-size: 1.08rem;
}

/* "In plain English" cards */
.plain-card {
    margin: 2rem 0 2.4rem 0;
    padding: 1.6rem 1.8rem 1.4rem 1.8rem;
    background: linear-gradient(180deg, #fff8ec 0%, #fffaf0 100%);
    border: 1px solid #f0e4c9; border-left: 4px solid #d4a849;
    border-radius: 6px; box-shadow: 0 1px 3px rgba(212, 168, 73, 0.08);
}
.plain-label {
    font-family: var(--sans); font-size: 0.72rem; font-weight: 700;
    letter-spacing: 0.18em; text-transform: uppercase;
    color: #a07719; margin-bottom: 0.9rem;
    display: flex; align-items: center; gap: 0.5rem;
}
.plain-label::before { content: "\2726"; font-size: 0.95rem; color: #d4a849; letter-spacing: 0; transform: translateY(-1px); }
.plain-card p { margin: 0 0 0.85rem 0; color: #2a2618; font-family: var(--serif); font-size: 1.04rem; line-height: 1.65; }
.plain-card p:last-child { margin-bottom: 0; }
.plain-card b, .plain-card strong { color: #15212e; }
.plain-card em { color: #a07719; font-style: italic; }

/* ========== EQUATION BLOCKS ========== */
.equation-block {
    background: var(--bg-elevated); border: 1px solid var(--rule);
    border-radius: 6px; padding: 1.4rem; margin: 1.8rem 0;
    overflow-x: auto; box-shadow: var(--shadow-sm); text-align: center;
}
.equation-block.large { padding: 2rem; }
.equation-block .katex { font-size: 1.05em; }
.equation-block.large .katex-display { margin: 0; }
.equation-caption {
    font-family: var(--sans); font-size: 0.82rem; color: var(--ink-muted);
    margin-top: 0.8rem; text-align: center;
}

/* ========== INTERACTIVE SECTIONS ========== */
.interactive-section {
    background: var(--bg-elevated); border: 1px solid var(--rule);
    border-radius: 8px; padding: 2rem; margin: 3rem 0; box-shadow: var(--shadow-md);
}
.interactive-section h3 {
    margin-top: 0; color: var(--upstream);
    display: flex; align-items: center; gap: 0.6rem;
}
.interactive-section h3::before { content: '\25B6'; font-size: 0.65em; color: var(--accent); }

.viz-container {
    width: 100%; min-height: 280px;
    border-radius: 4px; background: var(--bg);
    border: 1px solid var(--rule); margin: 1.4rem 0;
    position: relative; overflow: hidden;
}
.viz-container.tall { min-height: 420px; }
.viz-container.short { min-height: 240px; }

/* ========== CONTROLS ========== */
.ctrl, .ctrl-row { margin-bottom: 1rem; }
.ctrl label, .ctrl-row label {
    display: block; font-family: var(--sans);
    font-size: 0.86rem; font-weight: 500;
    color: var(--ink-soft); margin-bottom: 0.5rem;
}
.ctrl label span, .ctrl-row label span {
    color: var(--accent); font-family: var(--mono);
    font-weight: 600; margin-left: 0.4rem;
}

input[type="range"] {
    -webkit-appearance: none; width: 100%;
    height: 4px; background: var(--rule); border-radius: 2px;
    outline: none; cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; width: 18px; height: 18px;
    border-radius: 50%; background: var(--accent); cursor: pointer;
    box-shadow: 0 1px 4px rgba(200, 57, 44, 0.4);
    transition: transform 0.12s, box-shadow 0.12s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.15); }
input[type="range"]::-moz-range-thumb {
    width: 18px; height: 18px; border: none; border-radius: 50%;
    background: var(--accent); cursor: pointer;
}

.ctrl-tick {
    display: flex; justify-content: space-between;
    font-family: var(--sans); font-size: 0.72rem;
    color: var(--ink-muted); margin-top: 0.3rem;
}

.viz-controls {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1.4rem; margin: 1.5rem 0; padding: 1.4rem;
    background: var(--bg); border-radius: 6px; border: 1px solid var(--rule);
}

/* ========== READOUTS ========== */
.readout-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem; margin-top: 1.4rem;
}
.readout {
    background: var(--bg); border: 1px solid var(--rule);
    border-radius: 6px; padding: 1rem 1.2rem;
}
.readout-label {
    font-family: var(--sans); font-size: 0.74rem; font-weight: 500;
    text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--ink-muted); margin-bottom: 0.4rem;
}
.readout-val {
    font-family: var(--mono); font-size: 1.35rem;
    font-weight: 600; color: var(--ink);
}
.readout.highlight { background: linear-gradient(135deg, var(--accent-glow), transparent); border-color: var(--accent-soft); }
.readout-val.accent { color: var(--accent); }

/* ========== DUAL COLUMN ========== */
.dual-col { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin: 2.4rem 0; }
.dual-col h4 { color: var(--upstream); font-size: 1.1rem; margin-bottom: 0.6rem; padding-bottom: 0.4rem; border-bottom: 1px solid var(--rule); }
@media (max-width: 720px) { .dual-col { grid-template-columns: 1fr; } }

/* ========== CHART GRID ========== */
.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin: 1rem 0; }
@media (max-width: 900px) { .chart-grid { grid-template-columns: 1fr; } }

/* ========== STRATEGY CARDS ========== */
.method-cards {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem; margin: 1.4rem 0;
}
.method-card {
    padding: 1.3rem; background: var(--bg);
    border: 2px solid var(--rule); border-radius: 6px;
    cursor: pointer; transition: all 0.2s;
}
.method-card:hover { border-color: var(--ink-soft); transform: translateY(-2px); }
.method-card.selected {
    border-color: var(--accent);
    background: linear-gradient(135deg, rgba(200, 57, 44, 0.05), transparent);
    box-shadow: var(--shadow-md);
}
.method-name { font-family: var(--serif); font-weight: 600; font-size: 1.05rem; color: var(--ink); margin-bottom: 0.4rem; }
.method-meta { font-family: var(--mono); font-size: 0.76rem; color: var(--ink-muted); margin-bottom: 0.6rem; }
.method-desc { font-size: 0.88rem; color: var(--ink-soft); line-height: 1.45; }

/* ========== CHAPTER FOOT LINK ========== */
.chapter-foot {
    display: inline-flex; align-items: center; gap: 0.5rem;
    margin-top: 2.5rem; padding: 0.8rem 1.4rem;
    font-family: var(--sans); font-size: 0.9rem; font-weight: 500;
    color: var(--ink-soft); background: var(--bg-elevated);
    border: 1px solid var(--rule); border-radius: 6px;
    transition: all 0.2s; text-decoration: none;
}
.chapter-foot:hover {
    color: var(--accent); border-color: var(--accent-soft);
    text-decoration: none;
}
.chapter-foot .arrow { transition: transform 0.2s; }
.chapter-foot:hover .arrow { transform: translateX(4px); }

/* ========== CLOSING ========== */
.closing { margin-top: 4rem; padding-top: 2rem; border-top: 1px solid var(--rule); }
.closing-statement { font-size: 1.15rem; font-style: italic; color: var(--ink-soft); line-height: 1.6; }

/* ========== HELPER ========== */
.hint-text {
    font-family: var(--sans); font-size: 0.82rem; color: var(--ink-muted);
    font-style: italic; margin-top: -0.5rem; margin-bottom: 1rem;
}
.figure-caption {
    font-family: var(--sans); font-size: 0.85rem; color: var(--ink-muted);
    text-align: center; margin-top: 0.5rem; line-height: 1.5;
}
