/* ===================================================================
   Shared styles — theme system, typography, layout primitives.
   Every visualization page links to this file.
   =================================================================== */

/* Custom properties — dark theme (default) */
:root, [data-theme="dark"] {
    --bg: #101010;
    --bg-surface: #181818;
    --border: #282828;
    --text: #d4d4d4;
    --text-dim: #888;
    --text-heading: #eee;
    --text-faint: #666;
    --accent: #c8a26a;
    --hud-bg: rgba(16, 16, 16, 0.75);
    --hud-border: rgba(255, 255, 255, 0.06);
    --subtle-bg: rgba(255, 255, 255, 0.08);
    --plotly-bg: #101010;
    --plotly-grid: #1a1a1a;
    --plotly-zeroline: #222;
    --font-body: 'Cambria', 'Georgia', 'Times New Roman', serif;
    --font-ui: 'Segoe UI', system-ui, sans-serif;
    --font-mono: 'Cascadia Code', 'Consolas', monospace;
}

/* Light theme */
[data-theme="light"] {
    --bg: #f5f3ef;
    --bg-surface: #ffffff;
    --border: #ddd;
    --text: #333;
    --text-dim: #777;
    --text-heading: #222;
    --text-faint: #999;
    --accent: #9a7b4f;
    --hud-bg: rgba(255, 255, 255, 0.85);
    --hud-border: rgba(0, 0, 0, 0.08);
    --subtle-bg: rgba(0, 0, 0, 0.06);
    --plotly-bg: #f5f3ef;
    --plotly-grid: #e0ddd8;
    --plotly-zeroline: #ccc;
}

/* Global reset — consistent box model and smooth anchor scrolling */
* { margin: 0; padding: 0; box-sizing: border-box; }
html { scroll-behavior: smooth; overflow-x: hidden; }

/* Base body styles */
body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    font-size: 1.05rem;
    line-height: 1.8;
    overflow-x: hidden;
}

/* =================================================================
   Accessibility utilities
   ================================================================= */

/* Screen-reader only content */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Focus indicators — visible only on keyboard navigation */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Remove default outline for mouse users, keep for keyboard */
:focus:not(:focus-visible) {
    outline: none;
}

/* Site navigation bar — sticky on mobile */
.site-nav {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg);
    max-width: 960px;
    margin: 0 auto;
    padding: 16px 28px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
}
.nav-home {
    font-family: var(--font-body);
    font-size: 1rem;
    color: var(--accent);
    text-decoration: none;
    white-space: nowrap;
}
.nav-links {
    display: flex;
    gap: 20px;
    font-family: var(--font-ui);
    font-size: 0.82rem;
    align-items: center;
}
.nav-links a {
    color: var(--text-dim);
    text-decoration: none;
    transition: color 0.15s;
    padding: 2px 0;
}
.nav-links a:hover,
.nav-links a.active,
.nav-links a[aria-selected="true"] {
    color: var(--accent);
}
/* Active tab underline indicator */
.nav-links a.active,
.nav-links a[aria-selected="true"] {
    box-shadow: 0 2px 0 0 var(--accent);
}

/* Theme toggle button */
.theme-toggle,
.kbd-shortcut-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-size: 1rem;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s, border-color 0.15s;
    flex-shrink: 0;
}
.theme-toggle:hover,
.kbd-shortcut-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* Centered content column, max 760px for comfortable reading width */
.container {
    max-width: 760px;
    margin: 0 auto;
    padding: 0 28px;
}

/* Page header — generous vertical padding, bottom border separator */
header {
    padding: 32px 0 28px;
    border-bottom: 1px solid var(--border);
    text-align: center;
}
header h1 {
    font-family: var(--font-body);
    font-size: 2.4rem;
    font-weight: 400;
    letter-spacing: 0.5px;
    color: var(--text-heading);
    margin-bottom: 8px;
}
header .tagline {
    font-size: 1rem;
    color: var(--text-dim);
    font-style: italic;
}

/* Content sections — separated by subtle bottom borders */
.section {
    padding: 52px 0;
    border-bottom: 1px solid var(--border);
}
.section:last-of-type {
    border-bottom: none;
}
/* Uppercase label above each section heading */
.section-label {
    font-family: var(--font-ui);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--accent);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.section-label::before {
    content: '';
    display: inline-block;
    width: 24px;
    height: 2px;
    background: var(--accent);
    flex-shrink: 0;
}
h2 {
    font-family: var(--font-body);
    font-size: 1.6rem;
    font-weight: 400;
    color: var(--text-heading);
    margin-bottom: 20px;
}
p {
    margin-bottom: 16px;
    color: var(--text);
}
p:last-child { margin-bottom: 0; }

/* Equation display blocks — dark surface with accent left border */
.equation-block {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    padding: 24px 28px;
    margin: 24px 0;
    overflow-x: auto;
    max-width: 100%;
}
/* Prevent MathJax containers from stretching the page */
.equation-block mjx-container {
    display: block !important;
    overflow-x: auto;
    max-width: 100%;
}
.equation-block .mjx-math {
    font-size: 1.15rem;
}
/* Explanation text below each equation */
.equation-block p {
    font-size: 0.95rem;
    color: var(--text-dim);
    margin-top: 14px;
    margin-bottom: 0;
    padding-top: 14px;
    border-top: 1px solid var(--border);
}

/* Parameter table — shows values and descriptions */
.param-table {
    width: 100%;
    border-collapse: collapse;
    margin: 24px 0;
    font-size: 0.95rem;
}
.param-table th {
    text-align: left;
    font-family: var(--font-ui);
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-dim);
    padding: 10px 16px;
    border-bottom: 2px solid var(--border);
}
.param-table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.param-table .symbol {
    font-family: var(--font-body);
    font-style: italic;
    font-size: 1.1rem;
    color: var(--accent);
    width: 60px;
}
.param-table .value {
    font-family: var(--font-mono);
    color: var(--text-heading);
    width: 60px;
}

/* Application / item lists — borderless list items with bold titles */
.app-list {
    list-style: none;
    margin: 20px 0 0;
}
.app-list li {
    padding: 14px 0;
    border-bottom: 1px solid var(--border);
}
.app-list li:last-child { border-bottom: none; }
.app-list .app-title {
    font-weight: 700;
    color: var(--text-heading);
}

/* Footnote — small italic text for supplementary notes */
.note {
    font-size: 0.88rem;
    color: var(--text-dim);
    font-style: italic;
    margin-top: 12px;
}

/* Footer — centered links with hover underline */
footer {
    padding: 40px 0;
    text-align: center;
}
footer a {
    font-family: var(--font-ui);
    font-size: 0.85rem;
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.15s;
}
footer a:hover { border-bottom-color: var(--accent); }

/* =================================================================
   Tab panels — SPA tab switching with fade transitions
   ================================================================= */
.tab-panel {
    display: none;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.3s ease, transform 0.3s ease;
}
.tab-panel.active {
    display: block;
}
.tab-panel.visible {
    opacity: 1;
    transform: translateY(0);
}

/* =================================================================
   Consolidated visualization styles — shared across all viz panels.
   Extracted from per-page inline <style> blocks.
   ================================================================= */

/* Plot / canvas wrappers — full-width container for the visualization */
.plot-wrapper {
    position: relative;
    max-width: 960px;
    margin: 0 auto;
    padding: 12px 16px 0;
    border-bottom: 1px solid var(--border);
}
.canvas-wrapper {
    position: relative;
    max-width: 960px;
    margin: 0 auto;
    padding: 12px 16px 0;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

/* Plotly plot container */
.viz-plot {
    width: 100%;
    height: 560px;
}

/* Canvas element */
.viz-canvas {
    max-width: 100%;
    height: 560px;
    display: block;
    margin: 0 auto;
    background: var(--bg);
}
.viz-canvas.crosshair {
    cursor: crosshair;
}

/* Status HUD overlay — glassmorphic panel */
.viz-hud {
    position: absolute;
    bottom: 52px;
    left: 24px;
    background: var(--hud-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-radius: 6px;
    padding: 10px 16px 10px 14px;
    min-width: 150px;
    z-index: 10;
    pointer-events: none;
    border: 1px solid var(--hud-border);
}
.viz-hud-phase {
    font-family: var(--font-ui);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--accent);
    margin-bottom: 6px;
    transition: color 0.3s;
}
.viz-hud-phase.paused {
    color: #d4915e;
}
.viz-hud-progress {
    width: 100%;
    height: 3px;
    background: var(--subtle-bg);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 6px;
}
.viz-hud-progress-fill {
    height: 100%;
    width: 100%;
    background: var(--accent);
    border-radius: 2px;
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.15s linear;
}
.viz-hud-detail {
    font-family: var(--font-ui);
    font-size: 0.72rem;
    color: var(--text-dim);
    white-space: nowrap;
}
.viz-hud-coords {
    font-family: var(--font-mono);
    font-size: 0.68rem;
    color: var(--text-dim);
    margin-top: 4px;
}

/* Animation control buttons — glassmorphic button group */
.viz-controls {
    position: absolute;
    bottom: 52px;
    right: 24px;
    background: var(--hud-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-radius: 6px;
    padding: 6px 8px;
    z-index: 10;
    pointer-events: auto;
    border: 1px solid var(--hud-border);
    display: flex;
    gap: 4px;
}
.viz-controls button {
    background: transparent;
    border: none;
    color: var(--accent);
    font-family: var(--font-ui);
    font-size: 1rem;
    padding: 4px 8px;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
    line-height: 1;
}
.viz-controls button:hover {
    background: var(--subtle-bg);
    color: var(--text-heading);
}

/* Educational caption below visualization */
.viz-caption {
    text-align: center;
    font-family: var(--font-ui);
    font-size: 0.9rem;
    color: var(--text-dim);
    font-style: italic;
    padding: 8px 0 16px;
    min-height: 40px;
    transition: opacity 0.6s;
}

/* Slider controls — row of parameter adjusters */
.viz-sliders {
    display: flex;
    gap: 24px;
    justify-content: center;
    padding: 12px 16px;
    flex-wrap: wrap;
    max-width: 960px;
    margin: 0 auto;
}
.slider-group {
    display: flex;
    align-items: center;
    gap: 10px;
    font-family: var(--font-ui);
    font-size: 0.8rem;
    color: var(--text-dim);
}
.slider-group label {
    min-width: 100px;
    white-space: nowrap;
}
.slider-group label span {
    font-family: var(--font-mono);
    color: var(--accent);
}
.slider-group input[type="range"] {
    width: 120px;
    height: 6px;
    accent-color: var(--accent);
    -webkit-appearance: none;
    appearance: none;
    background: var(--subtle-bg);
    border-radius: 3px;
}
.slider-group input[type="range"]:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}
/* Custom thumb for iOS and cross-browser consistency */
.slider-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid var(--bg);
    cursor: pointer;
}
.slider-group input[type="range"]::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid var(--bg);
    cursor: pointer;
}

/* Dark-themed select dropdown */
.viz-select {
    background: var(--bg-surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px 8px;
    font-family: var(--font-ui);
    font-size: 0.8rem;
    cursor: pointer;
}
.viz-select:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* Position variant for canvas-only panels (Sierpinski — no flow content in wrapper) */
.viz-hud.hud-low { bottom: 12px; }
.viz-controls.controls-low { bottom: 12px; }

/* =================================================================
   Keyboard shortcuts help overlay
   ================================================================= */
.keyboard-help {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.6);
    align-items: center;
    justify-content: center;
}
.keyboard-help.visible {
    display: flex;
}
.keyboard-help-panel {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 28px 32px;
    max-width: 420px;
    width: 90%;
    position: relative;
}
.keyboard-help-panel h2 {
    font-family: var(--font-ui);
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-heading);
    margin-bottom: 16px;
}
.keyboard-help-panel .close-btn {
    position: absolute;
    top: 12px; right: 16px;
    background: transparent;
    border: none;
    color: var(--text-dim);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px;
}
.keyboard-help-panel .close-btn:hover {
    color: var(--text-heading);
}
.shortcut-list {
    list-style: none;
    font-family: var(--font-ui);
    font-size: 0.85rem;
}
.shortcut-list li {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    color: var(--text);
}
.shortcut-list li:last-child { border-bottom: none; }
.shortcut-list kbd {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    background: var(--subtle-bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 6px;
    color: var(--accent);
    min-width: 28px;
    text-align: center;
}

/* =================================================================
   Loading spinner — CSS-only pulse while viz initializes
   ================================================================= */
.viz-plot.loading::before,
.viz-canvas.loading::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: viz-spinner 0.8s linear infinite;
    z-index: 5;
}
.viz-plot.loading,
.viz-canvas.loading {
    position: relative;
}
@keyframes viz-spinner {
    to { transform: rotate(360deg); }
}

/* =================================================================
   Scroll-triggered section reveal
   ================================================================= */
.section {
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.5s ease, transform 0.5s ease;
}
.section.revealed {
    opacity: 1;
    transform: translateY(0);
}

/* =================================================================
   Nav scroll indicator — gradient fade on mobile
   ================================================================= */
@media (max-width: 600px) {
    .nav-links {
        position: relative;
        mask-image: linear-gradient(to right, black 85%, transparent 100%);
        -webkit-mask-image: linear-gradient(to right, black 85%, transparent 100%);
    }
}

/* =================================================================
   Touch-friendly cards — active feedback for touch devices
   ================================================================= */
@media (hover: none) {
    .viz-card:active {
        transform: scale(0.98);
    }
}

/* =================================================================
   Tablet breakpoint — single column, reduced viz height
   ================================================================= */
@media (max-width: 900px) {
    .viz-plot { height: 440px; }
    .viz-canvas { height: 440px; }
}

/* =================================================================
   CSS performance — will-change and contain
   ================================================================= */
.tab-panel {
    will-change: opacity;
}
.plot-wrapper,
.canvas-wrapper {
    contain: content;
}
.viz-hud-progress-fill {
    will-change: transform;
}

/* =================================================================
   Reduced motion — disable animations for users who prefer it
   ================================================================= */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    html { scroll-behavior: auto; }
}

/* Responsive — tighter spacing on narrow screens */
@media (max-width: 600px) {
    header { padding: 52px 0 36px; }
    header h1 { font-size: 1.8rem; }
    .section { padding: 36px 0; }

    .site-nav {
        flex-direction: column;
        gap: 10px;
        padding: 12px 20px;
    }
    .nav-links {
        gap: 14px;
        overflow-x: auto;
        flex-wrap: nowrap;
        justify-content: flex-start;
        -ms-overflow-style: none;
        scrollbar-width: none;
    }
    .nav-links::-webkit-scrollbar { display: none; }

    .equation-block {
        padding: 16px 12px;
    }
    .equation-block mjx-container {
        font-size: 0.85rem;
    }

    .viz-plot { height: 360px; }
    .viz-canvas { height: 360px; }

    /* HUD/controls — consistent positioning inside the shorter plot */
    .viz-hud {
        bottom: 16px;
        left: 12px;
        min-width: 120px;
        padding: 8px 12px 8px 10px;
    }
    .viz-controls {
        bottom: 16px;
        right: 12px;
        padding: 4px 6px;
    }
    .viz-controls button {
        padding: 8px 10px;
        font-size: 1.1rem;
    }

    /* Sliders — stacked vertically with touch-friendly sizing */
    .viz-sliders {
        flex-direction: column;
        gap: 14px;
        align-items: stretch;
        padding: 16px 24px;
    }
    .slider-group {
        gap: 12px;
        min-height: 44px;
    }
    .slider-group label {
        min-width: 110px;
        font-size: 0.85rem;
    }
    .slider-group input[type="range"] {
        width: 100%;
        flex: 1;
        height: 8px;
        padding: 16px 0;
        margin: -16px 0;
    }
    .slider-group input[type="range"]::-webkit-slider-thumb {
        width: 28px;
        height: 28px;
    }
    .slider-group input[type="range"]::-moz-range-thumb {
        width: 28px;
        height: 28px;
    }
    .viz-select {
        padding: 8px 12px;
        font-size: 0.85rem;
        min-height: 44px;
    }

    /* Caption — more padding on mobile */
    .viz-caption {
        padding: 10px 20px 16px;
        font-size: 0.82rem;
    }
}
