/* CSS Reset and Base Styles */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #64748b;
    --success-color: #22c55e;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    --bg-color: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border-color: #475569;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.4);
    --radius: 12px;
    --radius-sm: 8px;
    --transition: all 0.2s ease;
}

html {
    font-size: 16px;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, var(--bg-color) 0%, #1a1a2e 100%);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
}

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

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

header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary-color), #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1.1rem;
}

/* Main Content */
main {
    flex: 1;
}

/* Settings Panel */
.settings-panel {
    display: flex;
    gap: 1.5rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 2rem;
    padding: 1.25rem;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
}

.setting-group {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.setting-group label {
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 0.9rem;
}

.setting-group select {
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 0.9rem;
    cursor: pointer;
    transition: var(--transition);
}

.setting-group select:hover {
    border-color: var(--primary-color);
}

.setting-group select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

/* Stats Display */
.stats-display {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-box {
    background: var(--bg-secondary);
    padding: 1.25rem 1rem;
    border-radius: var(--radius);
    text-align: center;
    border: 1px solid var(--border-color);
    transition: var(--transition);
}

.stat-box:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.stat-value {
    display: block;
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Text Display */
.text-display-container {
    margin-bottom: 1.5rem;
}

.text-display {
    background: var(--bg-secondary);
    padding: 1.5rem;
    border-radius: var(--radius);
    border: 2px solid var(--border-color);
    font-size: 1.25rem;
    line-height: 2;
    min-height: 150px;
    max-height: 200px;
    overflow-y: auto;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    letter-spacing: 0.5px;
    transition: var(--transition);
}

.text-display.active {
    border-color: var(--primary-color);
}

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

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

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

.text-display .char.current {
    background: rgba(99, 102, 241, 0.3);
    border-radius: 2px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% { background: rgba(99, 102, 241, 0.3); }
    51%, 100% { background: rgba(99, 102, 241, 0.1); }
}

/* Input Area */
.input-container {
    margin-bottom: 1.5rem;
}

#typing-input {
    width: 100%;
    padding: 1.25rem;
    border-radius: var(--radius);
    border: 2px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 1.1rem;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    resize: none;
    min-height: 100px;
    transition: var(--transition);
}

#typing-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

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

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

/* Controls */
.controls {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 3rem;
}

/* Buttons */
.btn {
    padding: 0.875rem 2rem;
    border-radius: var(--radius-sm);
    border: none;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), #8b5cf6);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--border-color);
    transform: translateY(-2px);
}

.btn-danger {
    background: var(--error-color);
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

.btn-small {
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 2.5rem;
    border-radius: var(--radius);
    max-width: 500px;
    width: 100%;
    text-align: center;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    animation: modalSlide 0.3s ease;
}

@keyframes modalSlide {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-content h2 {
    margin-bottom: 1.5rem;
    font-size: 1.75rem;
    color: var(--text-primary);
}

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

.result-item {
    background: var(--bg-tertiary);
    padding: 1.25rem;
    border-radius: var(--radius-sm);
}

.result-value {
    display: block;
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.result-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.modal-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

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

.history-section h2 {
    margin-bottom: 1rem;
    font-size: 1.5rem;
    color: var(--text-primary);
}

.history-controls {
    margin-bottom: 1rem;
    display: flex;
    justify-content: flex-end;
}

.history-table-container {
    overflow-x: auto;
    margin-bottom: 1.5rem;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    overflow: hidden;
}

.history-table th,
.history-table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.history-table th {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.history-table td {
    color: var(--text-primary);
}

.history-table tbody tr:hover {
    background: rgba(99, 102, 241, 0.05);
}

.history-table tbody tr:last-child td {
    border-bottom: none;
}

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

.history-stats {
    display: flex;
    gap: 2rem;
    flex-wrap: wrap;
    padding: 1.25rem;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
}

.history-stat {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.history-stat-label {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.history-stat-value {
    font-weight: 700;
    color: var(--primary-color);
    font-size: 1.1rem;
}

/* Footer */
footer {
    text-align: center;
    padding: 2rem 0;
    margin-top: auto;
    border-top: 1px solid var(--border-color);
}

footer p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

footer a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
    transition: var(--transition);
}

footer a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

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

    header h1 {
        font-size: 2rem;
    }

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

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

    .text-display {
        font-size: 1.1rem;
        padding: 1.25rem;
        min-height: 120px;
    }

    .settings-panel {
        flex-direction: column;
        align-items: stretch;
    }

    .setting-group {
        justify-content: space-between;
    }

    .results-grid {
        grid-template-columns: 1fr;
    }

    .modal-content {
        padding: 1.5rem;
    }

    .history-stats {
        flex-direction: column;
        gap: 1rem;
    }

    .controls {
        flex-direction: column;
    }

    .btn {
        width: 100%;
        justify-content: center;
    }
}

@media (max-width: 480px) {
    header h1 {
        font-size: 1.75rem;
    }

    .subtitle {
        font-size: 1rem;
    }

    .stat-box {
        padding: 1rem 0.75rem;
    }

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

    .stat-label {
        font-size: 0.7rem;
    }

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

    #typing-input {
        font-size: 1rem;
        padding: 1rem;
    }

    .history-table th,
    .history-table td {
        padding: 0.75rem 0.5rem;
        font-size: 0.85rem;
    }
}

/* Focus visible for accessibility */
.btn:focus-visible,
select:focus-visible,
#typing-input:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
