/* ========================================
   CSS Variables & Theme
   ======================================== */
:root {
    /* Light Theme (Default) */
    --bg-primary: #f8f9fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e9ecef;
    --text-primary: #212529;
    --text-secondary: #6c757d;
    --text-muted: #adb5bd;
    --border-color: #dee2e6;
    --accent: #4a9eff;
    --accent-hover: #3a8eef;
    --correct: #22c55e;
    --incorrect: #ef4444;
    --current: #4a9eff;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
}

[data-theme="dark"] {
    --bg-primary: #121212;
    --bg-secondary: #1e1e1e;
    --bg-tertiary: #2d2d2d;
    --text-primary: #e4e4e4;
    --text-secondary: #a0a0a0;
    --text-muted: #6b6b6b;
    --border-color: #3d3d3d;
    --accent: #4a9eff;
    --accent-hover: #6ab0ff;
    --correct: #22c55e;
    --incorrect: #ef4444;
    --current: #4a9eff;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
}

/* ========================================
   Reset & Base Styles
   ======================================== */
*, *::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: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* ========================================
   Layout
   ======================================== */
.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ========================================
   Header
   ======================================== */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.title {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

/* Language Toggle */
.lang-toggle {
    display: flex;
    gap: 0.25rem;
    background: var(--bg-tertiary);
    padding: 0.25rem;
    border-radius: 0.5rem;
}

.lang-btn {
    padding: 0.4rem 0.75rem;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 0.375rem;
    transition: all 0.2s ease;
}

.lang-btn:hover {
    color: var(--text-primary);
}

.lang-btn.active {
    background: var(--bg-secondary);
    color: var(--text-primary);
    box-shadow: var(--shadow);
}

/* Theme Toggle */
.theme-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: var(--bg-tertiary);
    border-radius: 0.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    transition: all 0.2s ease;
    color: var(--text-secondary);
}

.theme-btn:hover {
    background: var(--border-color);
    color: var(--text-primary);
}

.icon-sun, .icon-moon {
    position: absolute;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.icon-moon {
    opacity: 0;
    transform: rotate(-90deg);
}

[data-theme="dark"] .icon-sun {
    opacity: 0;
    transform: rotate(90deg);
}

[data-theme="dark"] .icon-moon {
    opacity: 1;
    transform: rotate(0);
}

/* ========================================
   Main Content
   ======================================== */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* Text Display */
.text-display-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 0.75rem;
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

.text-display {
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 1.25rem;
    line-height: 1.8;
    color: var(--text-muted);
    word-wrap: break-word;
    min-height: 80px;
}

.text-display .char {
    transition: color 0.1s ease;
}

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

.text-display .char.incorrect {
    color: var(--incorrect);
    text-decoration: underline;
    text-decoration-color: var(--incorrect);
}

.text-display .char.current {
    background: var(--accent);
    color: white;
    border-radius: 2px;
    padding: 0 1px;
}

/* Input Area */
.input-container {
    position: relative;
}

.typing-input {
    width: 100%;
    padding: 1rem 1.25rem;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 1.1rem;
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 0.75rem;
    color: var(--text-primary);
    resize: none;
    min-height: 60px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.typing-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.15);
}

.typing-input::placeholder {
    color: var(--text-muted);
}

.typing-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Stats */
.stats {
    display: flex;
    justify-content: center;
    gap: 2.5rem;
    padding: 1rem 0;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

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

.stat-value {
    font-family: 'JetBrains Mono', monospace;
    font-size: 1.75rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* Controls */
.controls {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 0.5rem;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    font-size: 0.95rem;
    font-weight: 500;
    border: none;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
}

.btn-primary {
    background: var(--accent);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

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

.btn-secondary:hover {
    background: var(--border-color);
    color: var(--text-primary);
}

/* ========================================
   Modal
   ======================================== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
    opacity: 1;
    transition: opacity 0.3s ease;
}

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

.modal-content {
    background: var(--bg-secondary);
    padding: 2.5rem;
    border-radius: 1rem;
    text-align: center;
    max-width: 400px;
    width: 90%;
    box-shadow: var(--shadow-lg);
    transform: scale(1);
    transition: transform 0.3s ease;
}

.modal.hidden .modal-content {
    transform: scale(0.95);
}

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

.result-stats {
    display: flex;
    justify-content: center;
    gap: 2rem;
    margin-bottom: 2rem;
}

.result-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

.result-value {
    font-family: 'JetBrains Mono', monospace;
    font-size: 2rem;
    font-weight: 600;
    color: var(--accent);
}

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

/* ========================================
   History Section
   ======================================== */
.history-section {
    margin-top: 2.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
}

.history-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 1rem;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 200px;
    overflow-y: auto;
}

.history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    font-size: 0.9rem;
}

.history-rank {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-tertiary);
    border-radius: 50%;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.history-stats {
    display: flex;
    gap: 1.5rem;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.85rem;
    color: var(--text-primary);
}

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

.no-history {
    color: var(--text-muted);
    font-size: 0.9rem;
    text-align: center;
    padding: 1rem;
}

#clear-history {
    margin-top: 1rem;
    font-size: 0.85rem;
    padding: 0.5rem 1rem;
}

/* ========================================
   Utilities
   ======================================== */
.hidden {
    display: none !important;
}

/* ========================================
   Responsive Design
   ======================================== */
@media (max-width: 600px) {
    .container {
        padding: 1.5rem 1rem;
    }

    .header {
        flex-wrap: wrap;
        gap: 1rem;
    }

    .title {
        order: -1;
        width: 100%;
        text-align: center;
    }

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

    .stats {
        gap: 1.5rem;
    }

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

    .result-stats {
        gap: 1rem;
    }

    .result-value {
        font-size: 1.5rem;
    }

    .history-stats {
        gap: 1rem;
        font-size: 0.8rem;
    }
}

/* ========================================
   Animations
   ======================================== */
@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

.typing-input:focus + .cursor {
    animation: pulse 1s infinite;
}
