/* ── Reset & Base ───────────────────────────────────────────── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    min-height: 100%;
    font-family: "SF Mono", "Fira Code", "Cascadia Code", "JetBrains Mono",
                 "Consolas", monospace;
    background: #111;
    color: #aaa;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-x: hidden;
}

/* ── Top bar ────────────────────────────────────────────────── */
#top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    max-width: 900px;
    padding: 1.5rem 2rem 0.5rem;
    user-select: none;
}

#logo {
    font-size: 1.4rem;
    font-weight: 700;
    color: #e2e2e2;
    letter-spacing: -0.02em;
}

#logo.clickable { cursor: pointer; }
#logo.clickable:hover { color: #fff; }

/* ── Timer mode buttons ──────────────────────────────── */
#timer-modes {
    display: flex;
    gap: 0.25rem;
    background: #181818;
    border: 1px solid #222;
    border-radius: 6px;
    padding: 0.2rem;
}

.timer-btn {
    font-family: inherit;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    padding: 0.3rem 0.7rem;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: #555;
    cursor: pointer;
    transition: color 0.12s, background 0.12s;
    -webkit-tap-highlight-color: transparent;
}

.timer-btn:hover { color: #aaa; }

.timer-btn.active {
    background: #e2e2e2;
    color: #111;
}

/* ── Mute button ──────────────────────────────────────── */
.mute-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: #aaa;
    cursor: pointer;
    transition: opacity 0.12s, color 0.12s;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    line-height: 0;
}

.mute-btn:hover { color: #e2e2e2; }

.mute-btn:hover { opacity: 0.7; }

#stats {
    display: flex;
    gap: 1.25rem;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    min-width: 3.5rem;
}

.stat-value {
    font-size: 1.1rem;
    font-weight: 600;
    color: #e2e2e2;
    font-variant-numeric: tabular-nums;
}

.stat-label {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #555;
}

/* ── Main ───────────────────────────────────────────────────── */
#main {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    width: 100%;
    max-width: 900px;
    padding: 1rem 2rem;
    gap: 1.25rem;
}

/* ── Word track (line of words) ─────────────────────────────── */
#word-track-container {
    width: 100%;
    overflow: hidden;
    min-height: 2.4rem;
}

#word-track {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 0.4rem;
    line-height: 1.6;
    font-size: 1.4rem;
    font-weight: 500;
    user-select: none;
}

.word {
    padding: 0.1em 0.2em;
    border-radius: 4px;
    white-space: nowrap;
    transition: color 0.1s;
}

.word.completed {
    color: #555;
}

.word.active {
    color: #e2e2e2;
    background: rgba(255, 255, 255, 0.04);
}

.word.pending {
    color: #333;
}

/* ── Character track (within current word) ─────────────────── */
#char-track-container {
    width: 100%;
    overflow: hidden;
    min-height: 2rem;
}

#char-track {
    display: flex;
    justify-content: center;
    gap: 0.6rem;
    line-height: 1.8;
    font-size: 1.2rem;
    font-weight: 500;
    user-select: none;
    letter-spacing: 0.05em;
}

.char-tile {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.4em;
    padding: 0 0.1em;
    border-radius: 3px;
    transition: color 0.08s, background 0.08s;
}

.char-tile.completed-correct {
    color: #4caf50;
}

.char-tile.completed-incorrect {
    color: #c62828;
}

.char-tile.active {
    color: #ffd200;
    background: rgba(255, 210, 0, 0.08);
}

.char-tile.pending {
    color: #444;
}

/* ── Input area (current character + morse) ─────────────────── */
#input-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6rem;
    width: 100%;
}

#current-character {
    display: flex;
    align-items: center;
    gap: 0.05em;
    font-size: 5rem;
    font-weight: 700;
    color: #e2e2e2;
    height: 1.3em;
    line-height: 1;
    user-select: none;
    letter-spacing: 0.02em;
}

#char-display {
    transition: transform 0.08s ease;
}

#char-display.pop {
    transform: scale(1.1);
}

/* ── Cursor ─────────────────────────────────────────────────── */
#cursor {
    font-weight: 300;
    color: #ffd200;
    animation: blink 1s step-end infinite;
    margin-left: 0.05em;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0; }
}

/* ── Morse output ───────────────────────────────────────────── */
#morse-output {
    min-height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

#morse-chars {
    font-size: 2rem;
    font-weight: 600;
    letter-spacing: 0.2em;
    color: #e2e2e2;
    min-height: 1.4em;
    transition: color 0.08s;
}

#morse-chars.dit-flash {
    color: #4fc3f7;
}

#morse-chars.dah-flash {
    color: #ffd200;
}

body.error #morse-chars {
    color: #c62828;
}

/* ── Mobile dit / dah buttons ──────────────────────────── */
#mobile-controls {
    display: none;
    gap: 0.75rem;
    width: 100%;
    max-width: 360px;
    padding: 0.5rem 0;
}

.morse-btn {
    flex: 1;
    font-family: inherit;
    font-size: 2.2rem;
    font-weight: 700;
    padding: 1rem 0;
    border: 2px solid #333;
    border-radius: 12px;
    background: #181818;
    color: #e2e2e2;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: manipulation;
    transition: background 0.08s, transform 0.06s, border-color 0.08s;
}

.morse-btn:active {
    transform: scale(0.95);
}

#btn-dit:active {
    background: rgba(79, 195, 247, 0.15);
    border-color: #4fc3f7;
}

#btn-dah:active {
    background: rgba(255, 210, 0, 0.15);
    border-color: #ffd200;
}

/* ── Hints ──────────────────────────────────────────────────── */
#hints {
    font-size: 0.75rem;
    color: #333;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.hint-key {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.6em;
    height: 1.6em;
    padding: 0 0.3em;
    border: 1px solid #222;
    border-radius: 3px;
    font-size: 0.7rem;
    font-weight: 600;
    color: #555;
    background: #181818;
}

/* ── Results overlay ────────────────────────────────────────── */
#results {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #111;
    z-index: 100;
    gap: 1.2rem;
    animation: fadeIn 0.25s ease;
    padding: 2rem;
    overflow-y: auto;
}

#results.hidden { display: none; }

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

#results-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem 2.5rem;
    text-align: center;
}

.result-stat {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.result-value {
    font-size: 2.2rem;
    font-weight: 700;
    color: #e2e2e2;
    font-variant-numeric: tabular-nums;
}

.result-label {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: #555;
}

#result-chart-container {
    width: 100%;
    max-width: 700px;
    height: 180px;
    position: relative;
}

#result-chart {
    width: 100%;
    height: 100%;
    border-radius: 6px;
    background: #141414;
}

#result-sentence {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 500;
    color: #555;
    max-width: 500px;
    line-height: 1.5;
}

#result-sentence .word-correct { color: #4caf50; }
#result-sentence .word-incorrect { color: #ef5350; }

#restart-btn {
    padding: 0.6rem 2rem;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    color: #e2e2e2;
    background: transparent;
    border: 1px solid #333;
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
    -webkit-tap-highlight-color: transparent;
}

#restart-btn:hover {
    border-color: #e2e2e2;
    color: #fff;
}

#restart-btn:active {
    transform: scale(0.97);
}

/* ── History bar ─────────────────────────────────────────────── */
#history-bar {
    width: 100%;
    max-width: 900px;
    padding: 0.5rem 2rem 1.5rem;
    user-select: none;
}

#history-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.history-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.2rem 0.5rem;
    background: #181818;
    border: 1px solid #222;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 500;
    color: #666;
    animation: chipIn 0.2s ease;
}

@keyframes chipIn {
    from { opacity: 0; transform: scale(0.9); }
    to   { opacity: 1; transform: scale(1); }
}

.history-chip .chip-char {
    font-size: 0.85rem;
    color: #aaa;
}

.history-chip .chip-morse {
    font-weight: 400;
    color: #555;
    letter-spacing: 0.08em;
}

/* ── Responsive ─────────────────────────────────────────────── */
@media (max-width: 640px) {
    #top {
        padding: 1rem 1rem 0.25rem;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    #timer-modes { order: 3; width: auto; margin-left: auto; }
    .mute-btn { order: 3; font-size: 1rem; }

    #stats { gap: 0.75rem; }

    .stat { min-width: 2.5rem; }

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

    #main {
        padding: 0.75rem 1rem;
        gap: 1rem;
    }

    #current-character { font-size: 3.5rem; }

    #morse-chars { font-size: 1.5rem; }

    #word-track { font-size: 1.1rem; gap: 0.35rem; }

    #char-track { font-size: 1rem; }

    #results-grid { gap: 0.5rem 1.5rem; }

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

    #result-chart-container { height: 130px; }

    #mobile-controls { display: flex; justify-content: center; }
}

@media (max-width: 400px) {
    #mobile-controls { max-width: 280px; }
    .morse-btn { font-size: 1.8rem; padding: 0.75rem 0; }
    #current-character { font-size: 2.8rem; }

    #morse-chars { font-size: 1.2rem; }

    #word-track { font-size: 0.95rem; }

    #char-track { font-size: 0.85rem; }

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