:root {
    --bg: #1a1a1a;
    --bg-surface: #242424;
    --bg-hover: #2e2e2e;
    --text: #e2e2e2;
    --text-muted: #555;
    --text-dim: #777;
    --accent: #e8956a;
    --accent-glow: #e8956a40;
    --correct: #d1d0c5;
    --error: #ca4754;
    --error-bg: #ca475420;
    --caret: #e8956a;
    --border: #333;
    --nav-bg: #151515;
    --font-mono: 'Roboto Mono', 'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, 'Liberation Mono', Menlo, monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

[data-theme="light"] {
    --bg: #f5f5f5;
    --bg-surface: #ffffff;
    --bg-hover: #eee;
    --text: #333;
    --text-muted: #aaa;
    --text-dim: #888;
    --correct: #333;
    --error: #d32f2f;
    --error-bg: #d32f2f15;
    --caret: #d97a4a;
    --border: #ddd;
    --nav-bg: #e8e8e8;
    --accent: #d97a4a;
    --accent-glow: #d97a4a30;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.6;
}

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

a:hover {
    text-decoration: underline;
}

/* ─── Navigation ─── */
.nav {
    background: var(--nav-bg);
    border-bottom: 1px solid var(--border);
    padding: 0 2rem;
    display: flex;
    align-items: center;
    height: 56px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-logo {
    font-family: var(--font-mono);
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--accent);
    margin-right: 2rem;
    letter-spacing: -0.5px;
}

.nav-logo:hover {
    text-decoration: none;
}

.nav-links {
    display: flex;
    gap: 0.25rem;
    list-style: none;
    flex: 1;
}

.nav-links a {
    color: var(--text-dim);
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: color 0.15s, background 0.15s;
}

.nav-links a:hover,
.nav-links a.active {
    color: var(--text);
    background: var(--bg-hover);
    text-decoration: none;
}

.nav-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: color 0.15s, border-color 0.15s;
}

.theme-toggle:hover {
    color: var(--text);
    border-color: var(--text-dim);
}

/* ─── Layout ─── */
.container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 2rem;
}

.page-title {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    color: var(--text);
}

/* ─── Typing area ─── */
.typing-container {
    position: relative;
    outline: none;
    user-select: none;
    -webkit-user-select: none;
}

.typing-stats {
    display: flex;
    gap: 2rem;
    margin-bottom: 1.5rem;
    font-family: var(--font-mono);
    font-size: 1.8rem;
    color: var(--text-muted);
}

.typing-stats .stat-value {
    color: var(--accent);
    font-weight: 600;
}

.typing-stats .stat-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    display: block;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.text-display {
    font-family: var(--font-mono);
    font-size: 1.5rem;
    line-height: 2;
    position: relative;
    overflow: hidden;
    min-height: 120px;
    max-height: calc(1.5rem * 2 * 3 + 1rem);
    transition: max-height 0.3s;
    padding: 0.5rem 0;
}

.text-display.focused {
    outline: none;
}

.text-display .word {
    display: inline-block;
    margin: 0 0.35em 0 0;
    position: relative;
    border-bottom: 2px solid transparent;
}

.text-display .word.current {
    border-bottom-color: var(--accent-glow);
}

.text-display .char {
    position: relative;
    display: inline-block;
    color: var(--text-muted);
    transition: color 0.05s;
}

.text-display .char.correct {
    color: var(--correct);
}

.text-display .char.incorrect {
    color: var(--error);
    background: var(--error-bg);
    border-radius: 2px;
}

.text-display .char.extra {
    color: var(--error);
    opacity: 0.7;
}

/* ─── Caret ─── */
.caret {
    position: absolute;
    width: 2.5px;
    height: 1.8rem;
    background: var(--caret);
    border-radius: 2px;
    pointer-events: none;
    z-index: 10;
    will-change: transform;
    transition: none;
}

.caret.blink {
    animation: caret-blink 1s step-end infinite;
}

@keyframes caret-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* ─── Focus overlay ─── */
.focus-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    opacity: 0.85;
    z-index: 5;
    cursor: pointer;
    border-radius: 8px;
    font-family: var(--font-mono);
    font-size: 1rem;
    color: var(--text-muted);
    transition: opacity 0.2s;
}

.focus-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

/* ─── Settings bar ─── */
.settings-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem 0.75rem;
    margin-bottom: 2rem;
    flex-wrap: wrap;
}

.settings-bar .separator {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 0.25rem;
}

.setting-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    padding: 0.4rem 0.7rem;
    border-radius: 6px;
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.setting-btn:hover {
    color: var(--text);
    background: var(--bg-hover);
}

.setting-btn.active {
    color: var(--accent);
    background: var(--accent-glow);
}

/* ─── Results screen ─── */
.results-screen {
    display: none;
    text-align: center;
    padding: 2rem 0;
}

.results-screen.visible {
    display: block;
}

.results-main-stat {
    font-family: var(--font-mono);
    font-size: 4rem;
    font-weight: 700;
    color: var(--accent);
    line-height: 1;
}

.results-main-label {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 2rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 1rem;
    margin: 2rem 0;
}

.result-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.25rem;
}

.result-card .value {
    font-family: var(--font-mono);
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text);
}

.result-card .label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 0.25rem;
}

.results-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
    margin-top: 2rem;
}

.btn {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    padding: 0.7rem 1.5rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: all 0.15s;
}

.btn-primary {
    background: var(--accent);
    color: #1a1a1a;
    border-color: var(--accent);
    font-weight: 600;
}

.btn-primary:hover {
    filter: brightness(1.1);
}

.btn-secondary {
    background: var(--bg-surface);
    color: var(--text);
}

.btn-secondary:hover {
    background: var(--bg-hover);
}

/* ─── Lessons grid ─── */
.lessons-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1rem;
}

.lesson-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.25rem;
    cursor: pointer;
    transition: border-color 0.15s, transform 0.15s;
    text-decoration: none;
    color: var(--text);
    display: block;
}

.lesson-card:hover {
    border-color: var(--accent);
    transform: translateY(-1px);
    text-decoration: none;
}

.lesson-card.locked {
    opacity: 0.4;
    pointer-events: none;
}

.lesson-card.passed {
    border-color: #4caf50;
}

.lesson-card .lesson-number {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.lesson-card .lesson-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0.25rem 0;
}

.lesson-card .lesson-meta {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.lesson-card .lesson-check {
    color: #4caf50;
    float: right;
    font-size: 1.2rem;
}

/* ─── Ghost mode ─── */
.ghost-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 2rem;
}

.ghost-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem 1.25rem;
    cursor: pointer;
    transition: border-color 0.15s;
}

.ghost-item:hover {
    border-color: var(--accent);
}

.ghost-progress-container {
    margin-bottom: 2rem;
}

.ghost-bar {
    height: 6px;
    border-radius: 3px;
    background: var(--bg-surface);
    margin-bottom: 0.5rem;
    position: relative;
    overflow: hidden;
}

.ghost-bar-label {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.ghost-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.1s linear;
    width: 0%;
}

.ghost-bar-fill.user {
    background: var(--accent);
}

.ghost-bar-fill.ghost {
    background: var(--text-muted);
}

.ghost-bar-fill.ahead {
    box-shadow: 0 0 8px var(--accent-glow);
}

/* ─── Analysis ─── */
.analysis-section {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.analysis-section h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text);
}

.chart-container {
    width: 100%;
    overflow-x: auto;
}

.chart-container svg {
    display: block;
}

.keyboard-heatmap .key {
    rx: 4;
    ry: 4;
    stroke: var(--border);
    stroke-width: 1;
    cursor: pointer;
    transition: stroke 0.15s;
}

.keyboard-heatmap .key:hover {
    stroke: var(--text);
    stroke-width: 2;
}

.keyboard-heatmap .key-label {
    fill: var(--text);
    font-family: var(--font-mono);
    font-size: 11px;
    text-anchor: middle;
    dominant-baseline: central;
    pointer-events: none;
}

.tooltip {
    position: fixed;
    background: var(--nav-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    font-family: var(--font-mono);
    font-size: 0.75rem;
    color: var(--text);
    pointer-events: none;
    z-index: 1000;
    display: none;
}

.bigram-table, .missed-keys-table {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--font-mono);
    font-size: 0.85rem;
}

.bigram-table th, .missed-keys-table th {
    text-align: left;
    color: var(--text-muted);
    font-weight: 500;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.bigram-table td, .missed-keys-table td {
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-state h2 {
    font-size: 1.2rem;
    margin-bottom: 0.5rem;
    color: var(--text-dim);
}

/* ─── Article layout ─── */
.article-content {
    max-width: 700px;
    margin: 0 auto;
}

.article-content h1 {
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.article-content h2 {
    font-size: 1.3rem;
    margin-top: 2rem;
    margin-bottom: 0.75rem;
}

.article-content p {
    margin-bottom: 1rem;
    color: var(--text-dim);
    line-height: 1.8;
}

.article-content .todo {
    background: var(--accent-glow);
    border: 1px dashed var(--accent);
    border-radius: 6px;
    padding: 1rem;
    color: var(--accent);
    font-style: italic;
}

/* ─── Index / Landing ─── */
.hero {
    text-align: center;
    padding: 4rem 0 3rem;
}

.hero h1 {
    font-family: var(--font-mono);
    font-size: 3rem;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 0.5rem;
}

.hero p {
    font-size: 1.1rem;
    color: var(--text-dim);
    max-width: 500px;
    margin: 0 auto 2rem;
}

.mode-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
    margin-bottom: 3rem;
}

.mode-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    text-decoration: none;
    color: var(--text);
    transition: border-color 0.15s, transform 0.15s;
    display: block;
}

.mode-card:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
    text-decoration: none;
}

.mode-card h3 {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.mode-card p {
    font-size: 0.85rem;
    color: var(--text-dim);
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
    .nav {
        padding: 0 1rem;
    }

    .nav-links {
        gap: 0;
    }

    .nav-links a {
        padding: 0.4rem 0.5rem;
        font-size: 0.8rem;
    }

    .container {
        padding: 1rem;
    }

    .typing-stats {
        font-size: 1.3rem;
        gap: 1rem;
    }

    .text-display {
        font-size: 1.1rem;
    }

    .settings-bar {
        font-size: 0.75rem;
    }

    .hero h1 {
        font-size: 2rem;
    }
}

@media (max-width: 480px) {
    .nav-links a {
        padding: 0.3rem 0.4rem;
        font-size: 0.7rem;
    }

    .text-display {
        font-size: 1rem;
    }

    .results-main-stat {
        font-size: 3rem;
    }
}
