/* === Reset & Variables === */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #0f1117;
    --bg-secondary: #1a1d27;
    --bg-tertiary: #242837;
    --text-primary: #e2e8f0;
    --text-secondary: #8892a8;
    --text-muted: #4a5068;
    --accent: #6c5ce7;
    --accent-glow: rgba(108, 92, 231, 0.3);
    --green: #00e676;
    --green-dim: rgba(0, 230, 118, 0.15);
    --red: #ff5252;
    --red-dim: rgba(255, 82, 82, 0.15);
    --yellow: #ffd740;
    --border: #2a2e3e;
    --radius: 12px;
    --radius-sm: 8px;
    --font-mono: 'JetBrains Mono', monospace;
    --font-sans: 'Inter', system-ui, sans-serif;
}

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

#app {
    max-width: 860px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}

/* === Header === */
header {
    text-align: center;
    margin-bottom: 2rem;
}

header h1 {
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), #a78bfa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-top: 0.25rem;
}

/* === Settings Bar === */
.settings-bar {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.difficulty-selector,
.timer-selector {
    display: flex;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    padding: 4px;
    gap: 2px;
}

.diff-btn,
.timer-btn {
    font-family: var(--font-sans);
    font-size: 0.85rem;
    font-weight: 500;
    padding: 0.5rem 1.2rem;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
    text-transform: capitalize;
}

.diff-btn:hover,
.timer-btn:hover {
    color: var(--text-primary);
}

.diff-btn.active,
.timer-btn.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 2px 8px var(--accent-glow);
}

/* === Stats Bar === */
.stats-bar {
    display: flex;
    justify-content: center;
    gap: 2rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.stat {
    text-align: center;
}

.stat-value {
    display: block;
    font-size: 1.75rem;
    font-weight: 700;
    font-family: var(--font-mono);
    color: var(--text-primary);
    transition: color 0.3s;
}

.stat-value.urgent {
    color: var(--red);
    animation: pulse 1s ease-in-out infinite;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* === Typing Area === */
.typing-area {
    position: relative;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    margin-bottom: 2rem;
    cursor: text;
    min-height: 180px;
    transition: border-color 0.2s;
}

.typing-area:hover {
    border-color: var(--accent);
}

.hidden-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: text;
    z-index: 2;
}

.text-display {
    font-family: var(--font-mono);
    font-size: 1.2rem;
    line-height: 2;
    letter-spacing: 0.02em;
    position: relative;
    z-index: 1;
    pointer-events: none;
    user-select: none;
}

.text-display span {
    transition: color 0.1s, background 0.1s;
    border-radius: 2px;
}

.char-default {
    color: var(--text-muted);
}

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

.char-incorrect {
    color: var(--red);
    background: var(--red-dim);
}

.char-current {
    color: var(--text-primary);
    border-left: 2px solid var(--accent);
    animation: blink 1s step-end infinite;
    margin-left: -1px;
    padding-left: 1px;
}

@keyframes blink {
    0%, 100% { border-left-color: var(--accent); }
    50% { border-left-color: transparent; }
}

.hint {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-secondary);
    font-size: 0.95rem;
    pointer-events: none;
    z-index: 1;
}

/* === Results Screen === */
.results-screen {
    text-align: center;
    margin-bottom: 2rem;
}

.results-screen h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--text-primary);
}

.results-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 2rem;
}

.result-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.result-card.primary {
    border-color: var(--accent);
    box-shadow: 0 0 20px var(--accent-glow);
}

.result-value {
    font-size: 1.75rem;
    font-weight: 700;
    font-family: var(--font-mono);
}

.result-card.primary .result-value {
    color: var(--accent);
    font-size: 2.5rem;
}

.result-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.difficulty-badge {
    text-transform: capitalize;
    font-size: 1.25rem !important;
}

.difficulty-badge.easy, .history-diff.easy { color: var(--green); }
.difficulty-badge.medium, .history-diff.medium { color: var(--yellow); }
.difficulty-badge.hard, .history-diff.hard { color: var(--red); }

.retry-btn {
    font-family: var(--font-sans);
    font-size: 1rem;
    font-weight: 600;
    padding: 0.85rem 2.5rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 12px var(--accent-glow);
}

.retry-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--accent-glow);
}

/* === History === */
.history-section {
    margin-bottom: 2rem;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.history-header h3 {
    font-size: 1rem;
    color: var(--text-secondary);
}

.clear-btn {
    font-family: var(--font-sans);
    font-size: 0.8rem;
    padding: 0.35rem 0.85rem;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
}

.clear-btn:hover {
    border-color: var(--red);
    color: var(--red);
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 1.25rem;
    padding: 0.75rem 1rem;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
}

.history-wpm {
    font-family: var(--font-mono);
    font-weight: 600;
    color: var(--accent);
    min-width: 90px;
}

.history-acc {
    color: var(--text-secondary);
    min-width: 55px;
}

.history-diff {
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    min-width: 65px;
}

.history-date {
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-left: auto;
}

/* === Footer === */
footer {
    text-align: center;
    padding-top: 1rem;
    color: var(--text-muted);
    font-size: 0.85rem;
}

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

footer a:hover {
    text-decoration: underline;
}

/* === Responsive === */
@media (max-width: 640px) {
    #app {
        padding: 1.25rem 1rem;
    }

    header h1 {
        font-size: 1.5rem;
    }

    .settings-bar {
        flex-direction: column;
        align-items: center;
    }

    .stats-bar {
        gap: 1rem;
    }

    .stat-value {
        font-size: 1.25rem;
    }

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

    .typing-area {
        padding: 1.25rem;
    }

    .results-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .history-item {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .history-date {
        width: 100%;
        margin-left: 0;
    }
}
