/* ===================================================
   Typing Replay Mode - Keystroke replay viewer
   旧プレイヤー(userscript_rtcombat)の配色を踏襲
   正解=#0099CC  ミス=#FF3554  ライン完了=#1eff52
   Theme-aware: uses CSS variables for UI chrome
   =================================================== */

/* --- Main Container --- */
.typing-replay {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 720px;
    height: 100%;
    margin: 0 auto;
    position: relative;
}

/* --- YouTube Player --- */
.typing-replay-player-wrap {
    flex-shrink: 0;
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #000;
    border-radius: 8px 8px 0 0;
    overflow: hidden;
}

.typing-replay-player-wrap iframe {
    width: 100%;
    height: 100%;
}

/* --- Controls Bar --- */
.typing-replay-controls {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 1rem;
    background: var(--player-surface);
    border-bottom: 1px solid var(--player-border);
    user-select: none;
    -webkit-user-select: none;
}

.typing-replay-controls-left {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.typing-replay-controls-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.typing-replay-ctrl-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-secondary);
    font-size: 1.1rem;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.typing-replay-ctrl-btn:hover {
    background: var(--player-hover);
    color: var(--text-primary);
}

.typing-replay-play-btn {
    width: 48px;
    height: 48px;
    font-size: 1.4rem;
    background: var(--player-surface);
}

.typing-replay-play-btn:hover {
    background: var(--player-surface-strong);
}

/* --- Speed Button --- */
.typing-replay-speed-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
    min-width: 42px;
}

.typing-replay-speed-btn:hover {
    border-color: rgba(var(--accent-rgb), 0.4);
    color: var(--text-primary);
}

.typing-replay-speed-btn.active {
    background: rgba(var(--accent-rgb), 0.08);
    border-color: rgba(var(--accent-rgb), 0.4);
    color: var(--accent);
}

.typing-replay-speed-btn.flash {
    background: rgba(var(--accent-rgb), 0.2);
    color: var(--accent);
    transform: scale(1.1);
}

/* --- Time Display --- */
.typing-replay-time {
    font-family: "Courier New", monospace;
    font-size: 0.7rem;
    color: var(--text-muted);
    letter-spacing: 0.02em;
}

/* --- Typing Link Button --- */
.typing-replay-typing-link {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.75rem;
    border: 1px solid rgba(var(--accent-rgb), 0.4);
    border-radius: 20px;
    background: rgba(var(--accent-rgb), 0.1);
    color: rgba(var(--accent-rgb), 0.9);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.typing-replay-typing-link i {
    font-size: 0.85rem;
}

.typing-replay-typing-link:hover {
    background: rgba(var(--accent-rgb), 0.2);
    border-color: rgba(var(--accent-rgb), 0.6);
    color: var(--accent);
}

/* --- Progress Bar --- */
.typing-replay-progress {
    flex-shrink: 0;
    height: 4px;
    background: var(--player-surface);
    cursor: pointer;
    position: relative;
    padding: 8px 0;
    margin: -8px 0;
    background-clip: content-box;
    -webkit-touch-callout: none;
}

.typing-replay-progress:hover .typing-replay-progress-fill {
    transform: scaleY(1.5);
}

.typing-replay-progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent, #e84898);
    border-radius: 0 2px 2px 0;
    transition: width 0.1s linear;
    transform-origin: bottom;
    pointer-events: none;
}

/* === Replay Info Bar: 旧システム準拠 2行×4列グリッド === */
.typing-replay-info {
    flex-shrink: 0;
    border-bottom: 1px solid var(--player-border);
}

.typing-replay-info-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 1rem 0.25rem;
    border-bottom: 1px solid var(--player-surface);
}

.typing-replay-info-name {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* Input Mode Badge */
.typing-replay-info-mode {
    font-size: 0.65rem;
    font-weight: 600;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    background: rgba(var(--accent-rgb), 0.15);
    color: rgba(var(--accent-rgb), 0.9);
    letter-spacing: 0.03em;
}

/* 2行×4列 Stats Grid */
.typing-replay-info-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
}

.typing-replay-info-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.25rem 0;
    border-right: 1px solid var(--player-surface);
}

.typing-replay-info-cell:nth-child(4n) {
    border-right: none;
}

/* Row 2 separator */
.typing-replay-info-cell:nth-child(n+5) {
    border-top: 1px solid var(--player-surface);
}

.typing-replay-info-cell-label {
    font-size: 0.55rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    line-height: 1;
}

.typing-replay-info-cell-value {
    font-family: "Courier New", monospace;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.4;
}

/* === Target Text: 旧システム準拠 3分割表示 === */
.typing-replay-typing-zone {
    flex-shrink: 0;
    border-bottom: 1px solid var(--player-border);
}

.typing-replay-target-text {
    min-height: 1.5rem;
    padding: 0.5rem 1rem 0.25rem;
    font-size: 1.0rem;
    letter-spacing: 0.08em;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
}

.typing-replay-target-text:empty {
    display: none;
}

/* 入力済み文字: 旧システム correct-word-color #0099CC */
.target-typed {
    color: #0099CC;
}

/* 次の入力文字: 太字で目立たせる（旧 next-character-color） */
.target-next {
    color: var(--text-primary);
    font-weight: 700;
    position: relative;
}

/* 残りの文字: 半透明（旧 word-color） */
.target-remaining {
    color: var(--text-muted);
}

/* === Input Replay Area === */
.typing-replay-input-area {
    min-height: 2.5rem;
    max-height: 5rem;
    padding: 0.375rem 1rem 0.5rem;
    font-family: "Courier New", monospace;
    font-size: 1.1rem;
    line-height: 1.6;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    transition: opacity 0.15s ease;
}

/* Line fade transition (G5) */
.typing-replay-input-area.line-fade {
    opacity: 0.3;
}

/* Miss flash effect (G4) */
.typing-replay-input-area.miss-flash {
    background: rgba(255, 53, 84, 0.12);
}

/* Line complete flash (GD-2: 旧システム #1eff52 緑) */
.typing-replay-typing-zone.line-complete-flash {
    animation: lineCompleteFlash 0.4s ease-out;
}

@keyframes lineCompleteFlash {
    0% { box-shadow: inset 3px 0 0 #1eff52; background: rgba(30, 255, 82, 0.08); }
    100% { box-shadow: inset 3px 0 0 transparent; background: transparent; }
}

/* Character coloring: 旧システム配色 */
.replay-char-correct {
    color: #0099CC;
}

.replay-char-miss {
    color: #FF3554;
    text-decoration: underline wavy #FF3554;
}

/* ライン完了文字 (将来の拡張用) */
.replay-char-complete {
    color: #1eff52;
}

/* === Replay Score Animation === */
.typing-replay-score-current {
    font-family: "Courier New", monospace;
    font-weight: 700;
}

.typing-replay-score-current.score-up {
    color: #0099CC;
}

.typing-replay-score-current.score-down {
    color: #FF3554;
}

/* Combo highlight */
.typing-replay-info-cell-value.combo-active {
    color: #0099CC;
    text-shadow: 0 0 8px rgba(0, 153, 204, 0.4);
}

/* === End Summary Overlay (G3) === */
.typing-replay-summary {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    animation: fadeIn 0.4s ease;
}

.typing-replay-summary-inner {
    text-align: center;
    padding: 2rem;
    max-width: 400px;
    width: 90%;
}

.typing-replay-summary-title {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.5);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: 0.75rem;
}

.typing-replay-summary-player {
    font-size: 1.2rem;
    font-weight: 700;
    color: #fff;
    margin-bottom: 0.25rem;
}

.typing-replay-summary-mode {
    font-size: 0.75rem;
    color: rgba(var(--accent-rgb), 0.8);
    margin-bottom: 1rem;
}

.typing-replay-summary-score {
    font-family: "Courier New", monospace;
    font-size: 2.5rem;
    font-weight: 700;
    color: #0099CC;
    margin-bottom: 1.5rem;
}

.typing-replay-summary-score small {
    font-size: 0.5em;
    color: rgba(255, 255, 255, 0.5);
}

.typing-replay-summary-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.typing-replay-summary-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

.typing-replay-summary-stat-value {
    font-family: "Courier New", monospace;
    font-size: 1.3rem;
    font-weight: 700;
    color: #fff;
}

.typing-replay-summary-stat-label {
    font-size: 0.65rem;
    color: rgba(255, 255, 255, 0.45);
}

.typing-replay-summary-typing-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    border: 1px solid rgba(var(--accent-rgb), 0.5);
    border-radius: 24px;
    background: rgba(var(--accent-rgb), 0.15);
    color: rgba(var(--accent-rgb), 1);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.typing-replay-summary-typing-btn:hover {
    background: rgba(var(--accent-rgb), 0.25);
    border-color: rgba(var(--accent-rgb), 0.7);
}

.typing-replay-summary-replay-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1.25rem;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 24px;
    background: transparent;
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    margin-top: 0.75rem;
}

.typing-replay-summary-replay-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.4);
    color: #fff;
}

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

/* --- 動画エラー表示 --- */
.typing-replay-video-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 1.5rem 1rem;
    text-align: center;
}

.typing-replay-video-error i {
    font-size: 2rem;
    color: #f0a030;
}

.typing-replay-video-error p {
    margin: 0;
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* --- Lyrics Area --- */
.typing-replay-lyrics {
    flex: 1;
    overflow-y: auto;
    padding: 0 1.5rem;
    scrollbar-width: thin;
    scrollbar-color: var(--player-scrollbar) transparent;
}

.typing-replay-lyrics::-webkit-scrollbar {
    width: 6px;
}

.typing-replay-lyrics::-webkit-scrollbar-track {
    background: transparent;
}

.typing-replay-lyrics::-webkit-scrollbar-thumb {
    background: var(--player-scrollbar);
    border-radius: 3px;
}

.typing-replay-lyrics::-webkit-scrollbar-thumb:hover {
    background: var(--player-scrollbar-hover);
}

/* Spacers allow first/last lines to be centered */
.typing-replay-spacer {
    height: 35vh;
}

/* --- Lyrics Lines --- */
.typing-replay-lyrics .lyrics-line {
    padding: 0.625rem 1rem;
    margin: 0.125rem 0;
    border-radius: 8px;
    border-left: 3px solid transparent;
    cursor: pointer;
    transition: opacity 0.4s ease, border-color 0.3s ease, background 0.3s ease,
                transform 0.3s ease, padding-left 0.3s ease;
    opacity: 0.75;
    outline: none;
}

.typing-replay-lyrics .lyrics-line:hover,
.typing-replay-lyrics .lyrics-line:focus-visible {
    background: var(--player-surface);
}

.typing-replay-lyrics .lyrics-line:focus-visible {
    outline: 2px solid rgba(0, 153, 204, 0.5);
    outline-offset: -2px;
}

/* Tap feedback */
.typing-replay-lyrics .lyrics-line:active {
    transform: scale(0.98);
    transition-duration: 0.05s;
}

/* Past lines */
.typing-replay-lyrics .lyrics-line.past {
    opacity: 0.3;
}

/* Current line */
.typing-replay-lyrics .lyrics-line.current {
    opacity: 1;
    border-left-color: #0099CC;
    background: linear-gradient(90deg, rgba(0, 153, 204, 0.1) 0%, transparent 60%);
    padding-left: 1.25rem;
    transform: scale(1.02);
}

.typing-replay-lyrics .lyrics-line.current .lyrics-line-text {
    font-weight: 700;
    font-size: 1.2em;
    color: var(--text-primary);
    text-shadow: 0 0 20px rgba(0, 153, 204, 0.3);
}

/* Upcoming lines */
.typing-replay-lyrics .lyrics-line.upcoming {
    opacity: 0.75;
}

/* Line text */
.typing-replay-lyrics .lyrics-line-text {
    font-size: 1.05rem;
    line-height: 1.5;
    color: var(--text-primary);
    transition: font-size 0.3s ease, font-weight 0.3s ease, color 0.3s ease;
}

/* Kana reading (furigana) */
.typing-replay-lyrics .lyrics-line-kana {
    font-size: 0.8rem;
    line-height: 1.3;
    color: var(--text-muted);
    margin-top: 0.1rem;
    transition: color 0.3s ease;
}

.typing-replay-lyrics .lyrics-line.current .lyrics-line-kana {
    color: rgba(0, 153, 204, 0.7);
}

.typing-replay-lyrics .lyrics-line.past .lyrics-line-kana {
    color: var(--text-muted);
}

/* --- Return to Current Button --- */
.typing-replay-return-btn {
    position: absolute;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.5rem 1.25rem;
    border: 1px solid var(--border-color);
    border-radius: 24px;
    background: var(--player-float-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: var(--text-primary);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    z-index: 10;
    transition: all 0.2s;
    box-shadow: var(--player-float-shadow);
}

.typing-replay-return-btn:hover {
    background: var(--player-surface-strong);
    border-color: rgba(var(--accent-rgb), 0.3);
    color: var(--text-primary);
}

/* --- Loading State --- */
.typing-replay-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
}

.typing-replay-loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--player-spinner-track);
    border-top-color: #0099CC;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* --- Empty State --- */
.typing-replay-empty {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* --- Screen Reader Only --- */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* === Responsive === */
@media (max-width: 640px) {
    .typing-replay {
        max-width: 100%;
    }

    .typing-replay-player-wrap {
        border-radius: 0;
    }

    .typing-replay-controls {
        padding: 0.25rem 0.5rem;
    }

    .typing-replay-ctrl-btn {
        width: 34px;
        height: 34px;
        font-size: 0.95rem;
    }

    .typing-replay-play-btn {
        width: 40px;
        height: 40px;
        font-size: 1.15rem;
    }

    .typing-replay-info-header {
        padding: 0.25rem 0.75rem 0.2rem;
    }

    .typing-replay-info-name {
        font-size: 0.75rem;
    }

    .typing-replay-info-mode {
        font-size: 0.6rem;
    }

    .typing-replay-info-cell {
        padding: 0.2rem 0;
    }

    .typing-replay-info-cell-label {
        font-size: 0.5rem;
    }

    .typing-replay-info-cell-value {
        font-size: 0.7rem;
    }

    .typing-replay-target-text {
        font-size: 0.85rem;
        padding: 0.375rem 0.75rem 0.2rem;
        min-height: 1.2rem;
    }

    .typing-replay-input-area {
        font-size: 0.95rem;
        min-height: 2rem;
        max-height: 3.5rem;
        padding: 0.25rem 0.75rem 0.375rem;
    }

    .typing-replay-lyrics {
        padding: 0 1rem;
    }

    .typing-replay-spacer {
        height: 20vh;
    }

    .typing-replay-lyrics .lyrics-line {
        padding: 0.5rem 0.75rem;
    }

    .typing-replay-lyrics .lyrics-line-text {
        font-size: 0.95rem;
    }

    .typing-replay-lyrics .lyrics-line-kana {
        font-size: 0.75rem;
    }

    .typing-replay-lyrics .lyrics-line.current .lyrics-line-text {
        font-size: 1.1em;
    }

    .typing-replay-return-btn {
        bottom: 1rem;
        font-size: 0.75rem;
        padding: 0.4rem 1rem;
    }

    .typing-replay-speed-btn {
        font-size: 0.7rem;
        min-width: 38px;
        padding: 0.2rem 0.4rem;
    }

    .typing-replay-time {
        font-size: 0.6rem;
    }

    .typing-replay-typing-link {
        font-size: 0.7rem;
        padding: 0.3rem 0.5rem;
        gap: 0.25rem;
    }

    .typing-replay-typing-link span {
        display: none;
    }

    /* Summary responsive */
    .typing-replay-summary-score {
        font-size: 1.8rem;
    }

    .typing-replay-summary-stats {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
    }

    .typing-replay-summary-stat-value {
        font-size: 1.1rem;
    }

    .typing-replay-summary-typing-btn {
        font-size: 0.8rem;
        padding: 0.6rem 1.2rem;
    }
}
