/* ─── CSS Variables & Global Tokens ─── */
:root {
    --bg-primary: #0a0a12;
    --bg-secondary: #12121e;
    --bg-tertiary: #1a1a2e;
    --bg-key: #1e1e30;
    --bg-key-hover: #2a2a42;
    --bg-key-active: #6c63ff;
    --bg-glass: rgba(255, 255, 255, 0.04);
    --border: rgba(255, 255, 255, 0.08);
    --border-hover: rgba(108, 99, 255, 0.3);
    --text-primary: #e8e8f0;
    --text-secondary: #8888a0;
    --text-muted: #555570;
    --accent: #6c63ff;
    --accent-glow: rgba(108, 99, 255, 0.25);
    --accent-2: #ff6b9d;
    --accent-3: #00d4aa;
    --danger: #ff4757;
    --gradient-1: linear-gradient(135deg, #6c63ff 0%, #a855f7 100%);
    --gradient-2: linear-gradient(135deg, #ff6b9d 0%, #ff8e53 100%);
    --heatmap-0: #1a1a2e;
    --heatmap-1: #1e3a5f;
    --heatmap-2: #2c6b9e;
    --heatmap-3: #459bd5;
    --heatmap-4: #6dd5fa;
    --heatmap-5: #a8e063;
    --heatmap-6: #f7d154;
    --heatmap-7: #f0932b;
    --heatmap-8: #eb4d4b;
    --heatmap-9: #ff3838;
    --radius: 10px;
    --radius-sm: 6px;
    --radius-lg: 16px;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
    --shadow-key: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-glow: 0 0 20px var(--accent-glow);
    --transition: 0.2s ease;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
}

/* ─── Reset & Base ─── */
*,
*::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);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    background-image:
        radial-gradient(ellipse at 20% 50%, rgba(108, 99, 255, 0.06) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 50%, rgba(255, 107, 157, 0.04) 0%, transparent 50%);
}

/* ─── App Layout ─── */
.app {
    max-width: 1100px;
    margin: 0 auto;
    padding: 24px 20px;
}

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

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo {
    font-size: 1.4rem;
    font-weight: 700;
    background: var(--gradient-1);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.badge {
    font-size: 0.65rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 99px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.header-right {
    display: flex;
    gap: 8px;
}

/* ─── Buttons ─── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border: none;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
}

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

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

.btn-primary {
    background: var(--gradient-1);
    color: #fff;
}

.btn-primary:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: var(--shadow-glow);
}

/* ─── Controls ─── */
.controls {
    display: flex;
    align-items: center;
    gap: 24px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

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

select {
    padding: 8px 32px 8px 12px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.85rem;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238888a0'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    transition: border-color var(--transition);
}

select:hover,
select:focus {
    border-color: var(--accent);
    outline: none;
}

/* Toggle Group */
.toggle-group {
    display: flex;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    overflow: hidden;
}

.toggle-btn {
    padding: 7px 14px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
}

.toggle-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.04);
}

.toggle-btn.active {
    background: var(--accent);
    color: #fff;
}

/* ─── Stats Bar ─── */
.stats-bar {
    display: flex;
    gap: 2px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 4px;
    margin-bottom: 20px;
    overflow-x: auto;
}

.stat {
    flex: 1;
    min-width: 80px;
    text-align: center;
    padding: 10px 8px;
    border-radius: var(--radius-sm);
    transition: background var(--transition);
}

.stat:hover {
    background: var(--bg-glass);
}

.stat-label {
    display: block;
    font-size: 0.65rem;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 4px;
}

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

/* ─── Keyboard ─── */
.keyboard-wrapper {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    padding: 20px;
    box-shadow: var(--shadow);
    margin-bottom: 20px;
    overflow-x: auto;
}

.keyboard {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 700px;
}

.keyboard-row {
    display: flex;
    gap: 5px;
    justify-content: center;
}

/* ─── Keys ─── */
.key {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    background: var(--bg-key);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.12s ease;
    box-shadow: var(--shadow-key);
    user-select: none;
}

.key:hover {
    background: var(--bg-key-hover);
    border-color: var(--border-hover);
    transform: translateY(-1px);
}

.key.active {
    background: var(--bg-key-active);
    border-color: var(--accent);
    transform: translateY(2px);
    box-shadow: var(--shadow-glow), inset 0 2px 4px rgba(0, 0, 0, 0.2);
}

.key .key-label {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-primary);
    font-family: var(--font-mono);
    line-height: 1;
}

.key .key-sub {
    font-size: 0.52rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.key .key-count {
    position: absolute;
    top: 3px;
    right: 4px;
    font-size: 0.48rem;
    font-weight: 700;
    color: var(--accent);
    font-family: var(--font-mono);
}

/* Special key sizes */
.key-backspace {
    width: 100px;
}

.key-tab {
    width: 78px;
}

.key-caps {
    width: 90px;
}

.key-enter {
    width: 110px;
}

.key-shift-left {
    width: 116px;
}

.key-shift-right {
    width: 140px;
}

.key-ctrl {
    width: 68px;
}

.key-alt {
    width: 58px;
}

.key-meta {
    width: 58px;
}

.key-space {
    width: 320px;
}

.key-fn {
    width: 48px;
}

/* Modifier key styling */
.key-modifier {
    background: var(--bg-tertiary);
}

.key-modifier:hover {
    background: var(--bg-key-hover);
}

.key-modifier.active {
    background: var(--accent-2);
    border-color: var(--accent-2);
}

/* ─── Heatmap Styles ─── */
.key.heatmap-0 {
    background: var(--heatmap-0);
}

.key.heatmap-1 {
    background: var(--heatmap-1);
}

.key.heatmap-2 {
    background: var(--heatmap-2);
}

.key.heatmap-3 {
    background: var(--heatmap-3);
    color: #000;
}

.key.heatmap-4 {
    background: var(--heatmap-4);
    color: #000;
}

.key.heatmap-5 {
    background: var(--heatmap-5);
    color: #000;
}

.key.heatmap-6 {
    background: var(--heatmap-6);
    color: #000;
}

.key.heatmap-7 {
    background: var(--heatmap-7);
    color: #fff;
}

.key.heatmap-8 {
    background: var(--heatmap-8);
    color: #fff;
}

.key.heatmap-9 {
    background: var(--heatmap-9);
    color: #fff;
}

.key.heatmap-3 .key-label,
.key.heatmap-4 .key-label,
.key.heatmap-5 .key-label,
.key.heatmap-6 .key-label {
    color: #000;
}

.key.heatmap-7 .key-label,
.key.heatmap-8 .key-label,
.key.heatmap-9 .key-label {
    color: #fff;
}

.key.heatmap-3 .key-count,
.key.heatmap-4 .key-count,
.key.heatmap-5 .key-count,
.key.heatmap-6 .key-count {
    color: rgba(0, 0, 0, 0.7);
}

.key.heatmap-7 .key-count,
.key.heatmap-8 .key-count,
.key.heatmap-9 .key-count {
    color: rgba(255, 255, 255, 0.8);
}

/* Heatmap Legend */
.heatmap-legend {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    margin-bottom: 16px;
}

.heatmap-legend.hidden {
    display: none;
}

.legend-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.legend-gradient {
    width: 200px;
    height: 12px;
    border-radius: 6px;
    background: linear-gradient(90deg, var(--heatmap-0), var(--heatmap-2), var(--heatmap-4), var(--heatmap-6), var(--heatmap-8), var(--heatmap-9));
}

/* ─── NKRO indicator ─── */
.key.nkro-pressed {
    background: var(--accent-3) !important;
    border-color: var(--accent-3) !important;
    box-shadow: 0 0 12px rgba(0, 212, 170, 0.4);
}

.key.nkro-pressed .key-label {
    color: #000 !important;
}

/* ─── Custom Editor ─── */
.custom-editor {
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 20px;
    margin-bottom: 20px;
}

.custom-editor.hidden {
    display: none;
}

.custom-editor h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 8px;
}

.editor-hint {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.editor-actions {
    display: flex;
    gap: 10px;
}

/* Selected key in editor mode */
.key.editor-selected {
    border: 2px solid var(--accent-2) !important;
    box-shadow: 0 0 12px rgba(255, 107, 157, 0.3);
}

/* ─── Typing Area ─── */
.typing-area {
    margin-bottom: 20px;
}

.typing-area label {
    display: block;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

textarea {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: var(--font-mono);
    font-size: 0.9rem;
    line-height: 1.6;
    resize: vertical;
    transition: border-color var(--transition), box-shadow var(--transition);
}

textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

textarea::placeholder {
    color: var(--text-muted);
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
    .app {
        padding: 16px 12px;
    }

    .header {
        flex-direction: column;
        gap: 12px;
        align-items: flex-start;
    }

    .controls {
        flex-direction: column;
        align-items: flex-start;
    }

    .stats-bar {
        flex-wrap: wrap;
    }

    .stat {
        min-width: 60px;
    }

    .key {
        width: 38px;
        height: 38px;
    }

    .key .key-label {
        font-size: 0.55rem;
    }

    .key-backspace {
        width: 68px;
    }

    .key-tab {
        width: 54px;
    }

    .key-caps {
        width: 62px;
    }

    .key-enter {
        width: 76px;
    }

    .key-shift-left {
        width: 80px;
    }

    .key-shift-right {
        width: 96px;
    }

    .key-ctrl {
        width: 48px;
    }

    .key-alt {
        width: 42px;
    }

    .key-meta {
        width: 42px;
    }

    .key-space {
        width: 200px;
    }

    .keyboard {
        min-width: 500px;
    }
}

@media (max-width: 480px) {
    .stats-bar {
        gap: 1px;
        padding: 2px;
    }

    .stat {
        padding: 8px 4px;
    }

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

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

    .toggle-btn {
        padding: 6px 10px;
        font-size: 0.7rem;
    }
}

/* ─── Animations ─── */
@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.6;
    }
}

@keyframes keyPress {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(0.92);
    }

    100% {
        transform: scale(1);
    }
}

.key.just-pressed {
    animation: keyPress 0.15s ease;
}