* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
    --bg: #fff;
    --surface: #ffffff;
    --text: #171b21;
    --text-2: #4b5563;
    --text-3: #9aa3af;
    --border: #d5dae0;
    --key-face: #ffffff;
    --key-edge: #c3cad3;
    --accent: #f59e0b;
    --accent-ink: #7c3400;
    --accent-soft: rgba(245, 158, 11, 0.16);
    --ok: #3b6d11;
    --bad: #dc2626;
    --mono: "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
    --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --word-size: clamp(1.3rem, 5vw, 2.2rem);
    --word-h: calc(var(--word-size) * 1.7);
    /* script.js keeps this equal to the real header height */
    --header-h: 50px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12151a;
        --surface: #1b2027;
        --text: #eef1f4;
        --text-2: #b6bec9;
        --text-3: #6b7480;
        --border: #2c333c;
        --key-face: #272d36;
        --key-edge: #0b0d10;
        --ok: #7bc043;
        --bad: #f87171;
    }
}

body {
    min-height: 100vh;
    padding-top: calc(var(--header-h) + 6px);
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
}

/* ───────── header: logo | controls | keycaps ───────── */
.site-nav {
    position: fixed;
    inset: 0 0 auto 0;
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    padding: 4px 16px;
    background: var(--bg);
}
.brand { flex: none; }
.logo-img { display: block; width: 100px; height: auto; }
.site-nav nav { flex: none; }

.controls {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 8px 10px;
}
.controls .pill { padding: 8px 14px; font-size: 12.5px; }

.keys { display: flex; gap: 8px; list-style: none; }

.keycap {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    border-radius: 10px;
    background: var(--key-face);
    color: var(--text);
    font: 600 13px/1.2 var(--sans);
    text-decoration: none;
    white-space: nowrap;
}
.keycap svg {
    width: 18px; height: 18px; flex: none;
    fill: none; stroke: currentColor; stroke-width: 1.8;
    stroke-linecap: round; stroke-linejoin: round;
}
.keycap:hover { color: blueviolet; }
.keycap:active,
.keycap[aria-current="page"],
.keycap[aria-current="page"]:hover {
    transform: translateY(3px);
    border-bottom-width: 2px;
}
.keycap[aria-current="page"] {
    background: var(--accent);
    color: var(--accent-ink);
    border-color: #d97706;
    border-bottom-color: #b45309;
}
.keycap:focus-visible,
.pill:focus-visible,
.modal-x:focus-visible,
.test-cta:focus-visible,
.test-list a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* pills */
.pill {
    font: 600 13px/1 var(--sans);
    padding: 10px 18px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
}
.pill:hover { transform: scale(1.04); }
.pill:disabled { opacity: 0.45; cursor: default; transform: none; box-shadow: none; }
.pill[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.pill.primary {
    color: #fff;
    border-color: transparent;
    background: linear-gradient(135deg, firebrick, darkred);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* timer + result button (always visible, so the header never jumps) */
.session-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 3px 3px 3px 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
}
.timer {
    font: 600 16px/1 var(--mono);
    font-variant-numeric: tabular-nums;
    min-width: 4.5ch;
}

/* ───────── searchable language select ───────── */
.lang { position: relative; }
.lang-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: 200px;
}
.lang-btn svg {
    width: 16px; height: 16px; flex: none;
    fill: none; stroke: currentColor; stroke-width: 1.8;
    stroke-linecap: round; stroke-linejoin: round;
}
.lang-btn .chev { width: 12px; height: 12px; transition: transform 0.15s; }
.lang-btn[aria-expanded="true"] .chev { transform: rotate(180deg); }
#lang-current { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.lang-panel {
    position: absolute;
    top: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    width: 280px;
    z-index: 30;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
    font-family: var(--sans);
}
.lang-search {
    display: block;
    width: 100%;
    padding: 12px 14px;
    border: 0;
    border-bottom: 1px solid var(--border);
    background: transparent;
    color: var(--text);
    font: 500 14px var(--sans);
    outline: none;
}
.lang-search:focus { box-shadow: inset 0 -2px 0 var(--accent); }
.lang-list { list-style: none; max-height: 260px; overflow-y: auto; padding: 4px; }
.lang-opt {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    padding: 9px 10px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
}
.lang-opt.active { background: var(--accent-soft); }
.lang-opt[aria-selected="true"] .lo-name { font-weight: 700; }
.lang-opt[aria-selected="true"] .lo-name::after { content: "  ✓"; color: var(--ok); }
.lo-native { color: var(--text-3); font-size: 12.5px; }
.lang-empty { padding: 14px; font-size: 13px; color: var(--text-2); text-align: center; }

/* ───────── main app ───────── */
#app {
    padding: 0.4rem 0.75rem 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    font-family: var(--mono);
    width: 100%;
    max-width: 100%;
    overflow: hidden;
}

/* word mode */
#word-display {
    font-size: var(--word-size);
    height: var(--word-h);
    font-weight: 500;
    letter-spacing: 0.18em;
    display: flex;
    gap: 0.04em;
    align-items: center;
    flex-wrap: wrap;
    justify-content: center;
}

.ch {
    transition: color 0.12s, border-bottom-color 0.12s;
    border-bottom: 3px solid transparent;
    padding-bottom: 2px;
    color: var(--text-3);
}
.ch.done    { color: var(--ok); }
.ch.current { color: var(--text); border-bottom-color: var(--accent); }
.ch.wrong   { color: var(--bad); animation: nudge 0.18s; }
@keyframes nudge { 25% { transform: translateX(-2px); } 75% { transform: translateX(2px); } }

/* sentence mode: single line, scrolls horizontally */
.sentence-view {
    width: 100%;
    max-width: 900px;
    height: var(--word-h);
    overflow: hidden;
    -webkit-mask-image: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
            mask-image: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
}
.track {
    position: relative;
    display: flex;
    align-items: center;
    gap: 1.1em;
    width: max-content;
    height: 100%;
    white-space: nowrap;
    font-size: var(--word-size);
    line-height: 1.2;
    font-weight: 500;
    letter-spacing: 0.18em;
    transition: transform 0.12s ease-out;
}
.w { display: inline-flex; }
.w.past { opacity: 0.4; }

#hint { font-size: 11px; color: var(--text-3); font-family: var(--sans); letter-spacing: 0.04em; }

.accent-opt {
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 92%;
    font: 12px/1.3 var(--sans);
    color: var(--text-2);
    cursor: pointer;
}
.accent-opt input { accent-color: var(--accent); flex: none; }

.hands-wrap {
    display: flex;
    gap: clamp(4px, 2vw, 24px);
    justify-content: center;
    width: 100%;
    max-width: 250px;
}
.hand-svg { width: 48%; max-width: 200px; height: auto; }

#finger-label {
    font-size: 11px;
    color: var(--text-2);
    font-family: var(--sans);
    text-align: center;
    min-height: 1.2em;
}

/* keyboard */
#keyboard-wrap { width: 100%; max-width: 560px; padding: 0 2px; }
.kb-row {
    display: flex;
    justify-content: center;
    gap: clamp(1px, 0.5vw, 4px);
    margin-bottom: clamp(1px, 0.5vw, 4px);
}
.key {
    flex: 1;
    max-width: clamp(22px, 8vw, 42px);
    aspect-ratio: 1 / 1;
    border-radius: 4px;
    background: var(--surface);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--sans);
    font-size: clamp(7px, 1.8vw, 13px);
    font-weight: 500;
    color: var(--text-2);
    transition: background 0.12s, color 0.12s, border-color 0.12s, transform 0.08s;
    user-select: none;
    position: relative;
    overflow: hidden;
}
.key.space {
    flex: 5;
    max-width: clamp(100px, 36vw, 200px);
    aspect-ratio: unset;
    height: clamp(22px, 6vw, 38px);
}
.key.active {
    background: var(--accent);
    color: var(--accent-ink);
    border-color: #d97706;
    transform: scale(1.08);
    box-shadow: 0 0 8px #f59e0b88;
    z-index: 2;
}
.key.shift {
    flex: 1.8;
    max-width: calc(clamp(22px, 8vw, 42px) * 1.8);
    aspect-ratio: 1.8 / 1;
    font-size: clamp(6px, 1.5vw, 11px);
}
.key.left-hand  { border-bottom: 2px solid #60a5fa55; }
.key.right-hand { border-bottom: 2px solid #f472b655; }

/* ───────── side panels / ads ───────── */
.ad-side { position: fixed; top: 100px; width: 20%; height: auto; }
.ad-right { right: 5px; }
.ad-left  { left: 5px; }

.test-cta {
    display: block;
    margin-bottom: 14px;
    padding: 12px 14px;
    border-radius: 12px;
    background: linear-gradient(135deg, firebrick, darkred);
    color: #fff;
    font: 700 14px/1.3 var(--sans);
    text-align: center;
    text-decoration: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
    transition: transform 0.15s;
}
.test-cta:hover { transform: scale(1.03); }

.test-title {
    margin: 0 0 8px;
    font: 700 11px var(--sans);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-3);
}
.test-list { list-style: none; display: grid; gap: 6px; margin-bottom: 14px; }
.test-list a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--key-face);
    color: var(--text);
    font: 600 13px var(--sans);
    text-decoration: none;
}
.test-list a:hover { color: blueviolet; border-color: blueviolet; }

/* ───────── result modal ───────── */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(10, 12, 16, 0.62);
}
.modal-card {
    position: relative;
    width: min(580px, 100%);
    max-height: 90vh;
    overflow-y: auto;
    padding: 24px;
    border-radius: 16px;
    background: var(--surface);
    color: var(--text);
    font-family: var(--sans);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.modal-card h2 { font-size: 20px; margin-bottom: 16px; }
.modal-card h3 { font-size: 14px; margin: 20px 0 10px; }
.modal-x {
    position: absolute;
    top: 12px; right: 14px;
    width: 34px; height: 34px;
    border: none; border-radius: 50%;
    background: transparent;
    color: var(--text-2);
    font-size: 26px; line-height: 1;
    cursor: pointer;
}
.modal-x:hover { background: var(--bg); }
.modal-actions { display: flex; justify-content: flex-end; margin-top: 22px; }

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: 10px;
}
.stat { padding: 12px; border-radius: 10px; background: var(--bg); }
.stat b { display: block; font: 700 24px/1.1 var(--mono); }
.stat small { color: var(--text-2); font-size: 12px; }
.sub { margin-top: 10px; font-size: 13px; color: var(--text-2); }

.verdict {
    margin-top: 18px;
    padding: 12px 14px;
    border-left: 4px solid var(--accent);
    border-radius: 6px;
    background: var(--bg);
    font-size: 14px;
    line-height: 1.5;
}
.verdict.clean { border-left-color: var(--ok); }

.rows { list-style: none; display: grid; gap: 8px; }
.row {
    display: grid;
    grid-template-columns: 112px 1fr 76px;
    align-items: center;
    gap: 10px;
    font-size: 13px;
}
.row .val { text-align: right; color: var(--text-2); font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 99px; background: var(--border); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.row.worst .bar i { background: firebrick; }
.row.worst .label { font-weight: 700; }

kbd {
    display: inline-block;
    min-width: 1.8em;
    padding: 1px 6px;
    margin-right: 6px;
    text-align: center;
    border-radius: 5px;
    border: 1px solid var(--border);
    border-bottom-width: 3px;
    background: var(--key-face);
    font: 600 12px var(--mono);
}

/* ───────── responsive ───────── */

/* header too narrow for one row: logo + keycaps on top, controls underneath */
@media (max-width: 1180px) {
    .brand { order: 1; }
    .site-nav nav { order: 2; margin-left: auto; }
    .controls { order: 3; flex: 1 1 100%; }
}

/* side panels hidden → page is a single column; leave room for bottom (anchor) ads so the keyboard is never covered */
@media (max-width: 1100px) {
    .ad-side { display: none; }
    #app { padding-bottom: calc(1.5rem + 110px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 640px) {
    .brand { display: none; }
    .site-nav { padding: 4px 10px; }
    .site-nav nav { order: 1; width: 100%; margin-left: 0; }
    .controls { order: 2; }
    .keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
    .keycap {
        flex-direction: column;
        justify-content: center;
        gap: 3px;
        padding: 7px 4px;
        font-size: 11px;
        text-align: center;
        white-space: normal;
    }
    .controls .pill { padding: 7px 11px; font-size: 12px; }
    .timer { font-size: 15px; }
    .row { grid-template-columns: 92px 1fr 64px; }

    /* dropdown spans the screen instead of hanging off the button */
    .lang { position: static; }
    .lang-panel {
        position: fixed;
        top: var(--header-h);
        left: 10px; right: 10px;
        width: auto;
        margin-top: 4px;
        transform: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .track, .keycap, .pill, .ch, .key, .test-cta, .chev { transition: none; }
    .ch.wrong { animation: none; }
}
