/*
    TYPING SPEED TEST

    Markup: src/includes/typing-test.njk, behaviour: typing-test.N.js.
    Uses the --pf-* tokens of the site-wide "modern shell" in index.NN.css.
    The deploy caches this file for a year: bump N in its name (and in the
    include) whenever it changes.
*/

.typing-test {
    --tt-ink: var(--text-color-light);
    --tt-typed: rgba(49, 48, 48, 0.42);
    --tt-muted: #77746f;
    --tt-error: #d92b2b;
    --tt-error-bg: rgba(255, 41, 41, 0.12);
    --tt-button-bg: rgba(255, 255, 255, 0.75);
    --tt-button-bg-hover: #fff;
    --tt-line: var(--pf-line);

    max-width: 760px;
    margin: 30px auto 0;
    font-family: var(--pf-font);
    font-weight: 400;
    letter-spacing: normal;
    color: var(--tt-ink);
}

body.dark .typing-test {
    --tt-ink: var(--text-color-dark);
    --tt-typed: rgba(255, 255, 255, 0.42);
    --tt-muted: rgba(255, 255, 255, 0.62);
    --tt-error: #ff7b7b;
    --tt-error-bg: rgba(255, 123, 123, 0.16);
    --tt-button-bg: rgba(255, 255, 255, 0.08);
    --tt-button-bg-hover: rgba(255, 255, 255, 0.16);
    --tt-line: var(--pf-line-dark);
}

/* card */

.typing-test__card {
    position: relative;
    overflow: hidden;
    padding: 22px 26px;
    border: 1px solid var(--tt-line);
    border-radius: 18px;
    background: var(--pf-surface);
    box-shadow: var(--pf-shadow-card), var(--pf-inset);
}

/* shrinks from full width to nothing over the test length (set from JS) */
.typing-test__progress {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: var(--pf-primary-deep);
    transform-origin: left;
    transition: none;
}

.is-running .typing-test__progress {
    transform: scaleX(0);
    transition-property: transform;
    transition-timing-function: linear;
}

.is-finished .typing-test__progress,
.is-finished .typing-test__stats {
    display: none;
}

/* top bar: test length + live stats */

.typing-test__bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 16px 24px;
}

.typing-test__durations {
    display: flex;
    gap: 8px;
}

.typing-test__chip {
    padding: 0.35rem 0.95rem;
    border: 1.5px solid var(--tt-line);
    border-radius: 2rem;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: 15px;
    cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s;
}

.typing-test__chip:hover {
    border-color: var(--pf-primary-deep);
    background-color: rgba(255, 212, 101, 0.15);
}

.typing-test__chip[aria-pressed="true"] {
    border-color: var(--pf-primary-deep);
    background-color: var(--pf-primary);
    color: var(--pf-ink);
    font-weight: 500;
}

.typing-test__stats {
    display: flex;
    gap: 28px;
    margin: 0;
}

.typing-test__stat {
    display: flex;
    flex-direction: column-reverse;
    min-width: 54px;
    text-align: right;
}

.typing-test__stat dt,
.typing-test__result-stats dt {
    color: var(--tt-muted);
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.typing-test__stat dd {
    margin: 0;
    font-size: 26px;
    font-weight: 600;
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
}

/* the passage */

.typing-test__box {
    position: relative;
    height: calc(3 * 1.7em);
    margin-top: 22px;
    overflow: hidden;
    font-size: 24px;
    cursor: text;
}

.typing-test__text {
    position: relative;
    line-height: 1.7em;
    letter-spacing: 0.01em;
    transition: transform 0.2s var(--pf-ease-spring), opacity 0.2s;
}

.typing-test__word .is-correct {
    color: var(--tt-typed);
}

.typing-test__word .is-wrong {
    border-radius: 3px;
    background: var(--tt-error-bg);
    color: var(--tt-error);
}

.typing-test__word .is-extra {
    color: var(--tt-error);
    opacity: 0.75;
}

.typing-test__word.has-error {
    text-decoration: underline 2px var(--tt-error);
    text-underline-offset: 6px;
}

.typing-test__word .is-missed {
    color: var(--tt-typed);
}

.typing-test__caret {
    position: absolute;
    top: 0;
    left: 0;
    width: 3px;
    height: 1.2em;
    margin-left: -1px;
    border-radius: 2px;
    background: var(--pf-primary-deep);
    opacity: 0;
    transition: transform 0.1s ease-out;
}

.is-focused .typing-test__caret {
    opacity: 1;
    animation: typing-test-blink 1s steps(1) infinite;
}

.is-running.is-focused .typing-test__caret {
    animation: none;
}

@keyframes typing-test-blink {
    50% {
        opacity: 0;
    }
}

/* invisible, covers the passage so a click or tap focuses it and opens the
   phone keyboard; 16px stops iOS from zooming in on focus */
.typing-test__input {
    position: absolute;
    inset: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: transparent;
    caret-color: transparent;
    font-size: 16px;
    opacity: 0;
    cursor: text;
}

.typing-test__hint {
    position: absolute;
    top: 50%;
    left: 50%;
    padding: 8px 18px;
    border-radius: 999px;
    background: var(--pf-ink);
    color: #fff;
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    box-shadow: var(--pf-shadow-card);
    transform: translate(-50%, -50%);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

body.dark .typing-test__hint {
    background: var(--pf-primary);
    color: var(--pf-ink);
}

.is-ready:not(.is-focused) .typing-test__hint {
    opacity: 1;
}

.is-ready:not(.is-focused) .typing-test__text {
    opacity: 0.45;
}

/* source + buttons */

.typing-test__footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px 16px;
    margin-top: 18px;
}

.typing-test__source {
    color: var(--tt-muted);
    font-size: 14px;
}

.typing-test a {
    color: inherit;
    text-decoration: none;
    background: linear-gradient(to bottom, transparent 55%, rgba(255, 212, 101, 0.8) 55%);
    transition: background 0.1s ease-in-out;
}

.typing-test a:hover {
    background: linear-gradient(to bottom, transparent 55%, #ffd465 55%);
}

.typing-test__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.typing-test__button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 15px;
    border: 1px solid var(--tt-line);
    border-radius: 999px;
    background: var(--tt-button-bg);
    color: inherit;
    font: inherit;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: transform 0.2s var(--pf-ease-spring), background-color 0.2s, box-shadow 0.2s;
}

.typing-test__button:hover {
    background: var(--tt-button-bg-hover);
    transform: translateY(-1px);
}

.typing-test__button--primary,
.typing-test__button--primary:hover {
    border-color: transparent;
    background: var(--pf-primary);
    color: var(--pf-ink);
    font-weight: 600;
    box-shadow: 0 10px 22px -10px rgba(254, 190, 22, 0.7);
}

/* while the clock runs, everything but the passage and the stats steps back */
.is-running .typing-test__durations,
.is-running .typing-test__footer {
    opacity: 0.35;
    transition: opacity 0.3s;
}

.is-running .typing-test__durations:hover,
.is-running .typing-test__footer:hover,
.is-running .typing-test__footer:focus-within {
    opacity: 1;
}

/* result */

.typing-test__result {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 0 4px;
    text-align: center;
}

.typing-test__result[hidden] {
    display: none;
}

.typing-test__result:focus {
    outline: none;
}

.typing-test__result-title {
    color: var(--tt-muted);
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.typing-test__result-wpm {
    margin-top: 6px;
    font-size: 76px;
    font-weight: 600;
    line-height: 1;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.typing-test__result-unit {
    color: var(--tt-muted);
    font-size: 24px;
    letter-spacing: 0;
}

.typing-test__result-stats {
    display: flex;
    justify-content: center;
    gap: 36px;
    margin: 20px 0 0;
}

.typing-test__result-stats div {
    display: flex;
    flex-direction: column-reverse;
}

.typing-test__result-stats dd {
    margin: 0;
    font-size: 22px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.typing-test__result-verdict {
    max-width: 500px;
    margin-top: 20px;
    font-size: 17px;
    line-height: 1.5;
}

.typing-test__result-best {
    margin-top: 6px;
    color: var(--tt-muted);
    font-size: 15px;
}

.typing-test__result-best:empty {
    display: none;
}

.typing-test__actions--result {
    justify-content: center;
    margin-top: 22px;
}

.typing-test__result-more {
    margin-top: 18px;
    font-size: 15px;
}

/* under the card */

.typing-test__tip {
    display: block;
    margin-top: 12px;
    color: var(--tt-muted);
    font-size: 14px;
    text-align: center;
}

.typing-test kbd {
    padding: 1px 6px;
    border: 1px solid var(--tt-line);
    border-radius: 5px;
    background: var(--tt-button-bg);
    font-family: inherit;
    font-size: 12px;
}

.typing-test__visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (hover: none) and (pointer: coarse) {
    .typing-test__keyboard-only {
        display: none;
    }
}

@media (max-width: 680px) {
    .typing-test {
        margin-top: 24px;
    }

    .typing-test__card {
        padding: 18px 16px;
    }

    .typing-test__bar {
        flex-direction: column-reverse;
        align-items: stretch;
    }

    .typing-test__stats {
        justify-content: space-between;
    }

    .typing-test__stat {
        text-align: left;
    }

    .typing-test__stat dd {
        font-size: 24px;
    }

    .typing-test__box {
        margin-top: 18px;
        font-size: 20px;
    }

    .typing-test__result-wpm {
        font-size: 64px;
    }

    .typing-test__result-stats {
        gap: 24px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .typing-test__text,
    .typing-test__caret,
    .typing-test__button {
        transition: none;
    }

    .is-focused .typing-test__caret {
        animation: none;
    }
}
