/* =============================================
   Typing Tutor — Premium Dark Theme
   ============================================= */

:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --bg-card: #1c2333;
    --fg-primary: #e6edf3;
    --fg-secondary: #8b949e;
    --fg-dim: #484f58;
    --accent-blue: #58a6ff;
    --accent-cyan: #79c0ff;
    --accent-green: #50fa7b;
    --accent-yellow: #f1fa8c;
    --accent-red: #ff5555;
    --accent-purple: #bd93f9;
    --accent-orange: #ffb86c;
    --glow-blue: rgba(88, 166, 255, 0.3);
    --glow-green: rgba(80, 250, 123, 0.3);
    --radius: 12px;
    --radius-sm: 8px;
    --font: 'Inter', 'Noto Sans Thai', -apple-system, sans-serif;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--fg-primary);
}

/* ═══ Screen System ═══ */
.screen {
    position: fixed;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
}

.screen.active {
    display: flex;
}

.screen.overlay {
    background: rgba(0, 0, 0, 0.85);
    z-index: 100;
    backdrop-filter: blur(8px);
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ═══ Buttons ═══ */
.btn {
    border: none;
    cursor: pointer;
    font-family: var(--font);
    font-weight: 600;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn:hover {
    transform: translateY(-2px);
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background: linear-gradient(135deg, #58a6ff, #3b82f6);
    color: white;
    padding: 14px 32px;
    font-size: 18px;
    box-shadow: 0 4px 20px var(--glow-blue);
}

.btn-primary:hover {
    box-shadow: 0 6px 30px var(--glow-blue);
}

.btn-lg {
    width: 100%;
    justify-content: center;
    font-size: 20px;
    padding: 16px;
}

.btn-ghost {
    background: transparent;
    color: var(--fg-secondary);
    padding: 10px 20px;
    font-size: 14px;
    border: 1px solid var(--bg-tertiary);
}

.btn-ghost:hover {
    color: var(--fg-primary);
    border-color: var(--fg-dim);
}

.btn-back {
    background: var(--bg-tertiary);
    color: var(--fg-secondary);
    padding: 10px 20px;
    font-size: 20px;
}

.btn-back:hover {
    color: var(--fg-primary);
}

.lesson-title-bar {
    color: var(--accent-cyan);
    font-size: 28px;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn-menu {
    background: var(--bg-card);
    color: var(--fg-primary);
    padding: 20px 24px;
    font-size: 18px;
    width: 100%;
    border-radius: var(--radius);
    border: 1px solid var(--bg-tertiary);
    justify-content: flex-start;
    position: relative;
}

.btn-menu:hover {
    border-color: var(--accent-blue);
    background: var(--bg-tertiary);
}

.btn-icon {
    font-size: 28px;
}

.btn-label {
    flex: 1;
    text-align: left;
}

.btn-badge {
    background: var(--bg-tertiary);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 13px;
    color: var(--accent-cyan);
}

.btn-survival {
    border-color: var(--accent-purple);
}

.btn-survival:hover {
    border-color: var(--accent-purple);
    box-shadow: 0 0 20px rgba(189, 147, 249, 0.2);
}

/* ═══ Login Screen ═══ */
.login-container {
    text-align: center;
    max-width: 420px;
    width: 90%;
}

.logo-glow {
    font-size: 72px;
    filter: drop-shadow(0 0 30px var(--glow-blue));
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {

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

    50% {
        transform: scale(1.05);
    }
}

.title-gradient {
    font-size: 48px;
    font-weight: 900;
    background: linear-gradient(135deg, #58a6ff, #bd93f9, #ff79c6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin: 16px 0 8px;
}

.subtitle {
    color: var(--fg-secondary);
    font-size: 18px;
    margin-bottom: 32px;
}

.login-box {
    background: var(--bg-secondary);
    padding: 32px;
    border-radius: var(--radius);
    border: 1px solid var(--bg-tertiary);
}

.login-box label {
    display: block;
    text-align: left;
    font-size: 14px;
    color: var(--fg-secondary);
    margin-bottom: 8px;
}

.login-box input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-primary);
    border: 2px solid var(--bg-tertiary);
    border-radius: var(--radius-sm);
    color: var(--fg-primary);
    font-size: 18px;
    font-family: var(--font);
    outline: none;
    margin-bottom: 16px;
    transition: var(--transition);
}

.login-box input:focus {
    border-color: var(--accent-blue);
    box-shadow: 0 0 0 3px var(--glow-blue);
}

.hint-text {
    color: var(--fg-dim);
    font-size: 13px;
    margin-top: 16px;
}

/* ═══ Menu Screen ═══ */
.menu-container {
    max-width: 480px;
    width: 90%;
}

.menu-header {
    text-align: center;
    margin-bottom: 24px;
}

.menu-header h1 {
    font-size: 32px;
}

.welcome-text {
    color: var(--accent-cyan);
    font-size: 18px;
    margin-top: 4px;
}

.lang-toggle {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
    background: var(--bg-secondary);
    padding: 4px;
    border-radius: var(--radius);
}

.btn-lang {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--fg-secondary);
    font-family: var(--font);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

.btn-lang.active {
    background: var(--accent-blue);
    color: white;
}

.btn-lang:not(.active):hover {
    color: var(--fg-primary);
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
}

/* ═══ Lesson Selection ═══ */
.lessons-container {
    max-width: 600px;
    width: 95%;
    height: 90vh;
    display: flex;
    flex-direction: column;
}

.lessons-header {
    text-align: center;
    padding: 16px 0;
    flex-shrink: 0;
}

.lessons-header h2 {
    font-size: 28px;
    margin: 8px 0;
}

.progress-text {
    color: var(--accent-cyan);
    font-size: 16px;
}

.lessons-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
    scrollbar-width: thin;
    scrollbar-color: var(--bg-tertiary) transparent;
}

.lesson-btn {
    width: 100%;
    padding: 16px 20px;
    background: var(--bg-card);
    border: 1px solid var(--bg-tertiary);
    border-radius: var(--radius-sm);
    color: var(--fg-primary);
    font-family: var(--font);
    font-size: 16px;
    text-align: left;
    cursor: pointer;
    margin-bottom: 8px;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 12px;
}

.lesson-btn:hover:not(:disabled) {
    border-color: var(--accent-blue);
    transform: translateX(4px);
}

.lesson-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.lesson-btn .icon {
    font-size: 20px;
    flex-shrink: 0;
}

.lesson-btn.completed {
    border-left: 3px solid var(--accent-green);
}

.lesson-btn.current {
    border-left: 3px solid var(--accent-blue);
    background: var(--bg-tertiary);
}

/* ═══ Practice Mode ═══ */
.practice-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    position: relative;
}

.practice-header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--bg-tertiary);
    flex-shrink: 0;
}

.practice-stats {
    display: flex;
    gap: 20px;
}

.stat {
    color: var(--fg-secondary);
    font-size: 24px;
    white-space: nowrap;
}

.stat b {
    color: var(--accent-cyan);
    font-size: 26px;
}

.stat-err {
    color: #f87171;
}

.stat-err b {
    color: #f87171;
}

/* Progress Bar */
.progress-bar-container {
    height: 6px;
    background: #3b4261;
    flex-shrink: 0;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #9ece6a, #50fa7b);
    transition: width 0.2s ease;
}

/* Text + Hands Row */
.text-hands-row {
    display: flex;
    align-items: stretch;
    padding: 4px 0;
    gap: 10px;
    flex-shrink: 0;
    max-height: 550px;
    max-width: 1650px;
    margin: 0 auto;
    position: relative;
}

.text-border-title {
    position: absolute;
    top: -10px;
    left: 200px;
    right: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    z-index: 5;
    background: var(--bg-secondary);
    padding: 4px 16px;
    transform: translateY(-100%);
}

.hand-container {
    position: relative;
    width: 180px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
}

.hand-container img {
    width: 100%;
    height: auto;
    max-height: 300px;
    object-fit: contain;
}

.hand-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.text-box-border {
    flex: 1;
    border: 3px solid #50fa7b;
    border-radius: 6px;
    background: #000;
    overflow: hidden;
    max-height: 435px;
}

.practice-text {
    padding: 10px 20px;
    line-height: 1.7;
    font-family: 'Sarabun', 'TH Sarabun New', sans-serif;
    letter-spacing: 0.3px;
    color: #fff;
    word-wrap: break-word;
}

.practice-text.lang-th {
    font-size: 35px;
    /* ขนาดตัวอักษรภาษาไทย (ปรับตรงนี้) */
}

.practice-text.lang-en {
    font-size: 35.7px;
    /* ขนาดตัวอักษรภาษาอังกฤษ (ปรับตรงนี้) */
}

.practice-text .char {
    transition: color 0.1s;
}

.practice-text .char.done {
    color: var(--accent-green);
}

.practice-text .char.wrong {
    color: var(--accent-red);
    text-decoration: underline;
}

.practice-text .char.current {
    background: #f1fa8c;
    color: #000;
    border-radius: 3px;
    padding: 0 2px;
    font-weight: bold;
    animation: cursorBlink 0.8s ease-in-out infinite;
}

.practice-text .char.pending {
    color: #ffffff;
}

@keyframes cursorBlink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

.practice-hint {
    text-align: center;
    padding: 4px;
    color: var(--accent-yellow);
    font-family: 'Sarabun', 'TH Sarabun New', sans-serif;
    font-size: 28px;
    min-height: 36px;
    flex-shrink: 0;
    margin-top: 16px;
}

.hint-char {
    display: inline-block;
    font-size: 34px;
    padding: 2px 10px;
    margin: 0 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    min-width: 40px;
    text-align: center;
}

.hint-blink {
    animation: hintBlink 0.8s step-end infinite;
}

@keyframes hintBlink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.3;
    }
}

.hint-warn {
    color: #ff6b6b;
    font-weight: bold;
    font-size: 22px;
}

/* Bullet Canvas */
.bullet-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
}

/* ═══ Virtual Keyboard ═══ */
.keyboard-container {
    padding: 2px 40px 8px;
    background: var(--bg-primary);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
    flex: 1;
}

.kb-row {
    display: flex;
    gap: 6px;
}

.kb-key {
    min-width: 90px;
    height: 76px;
    background: #111111;
    border: 2px solid #2a2a3a;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    font-family: 'Sarabun', 'TH Sarabun New', sans-serif;
    font-size: 26px;
    color: #fff;
    transition: all 0.12s ease;
    position: relative;
    gap: 2px;
}

.kb-key .sub {
    font-size: 16px;
    color: rgba(255, 255, 255, 0.6);
    line-height: 1;
}

.kb-key .main {
    font-size: 26px;
    line-height: 1.1;
    font-weight: 700;
}

.kb-key.shift-key {
    font-size: 14px;
    color: #aaa;
    background: #2a2a3a;
    border-color: #3b4261;
}

.kb-key.active {
    background: var(--accent-blue) !important;
    color: white !important;
    border-color: var(--accent-blue);
    transform: scale(1.06);
}

.kb-key.correct {
    background: #4ade80 !important;
    color: white !important;
    border-color: #4ade80;
    transform: scale(1.08);
}

.kb-key.wrong {
    background: #f87171 !important;
    color: white !important;
    border-color: #f87171;
    animation: shake 0.3s;
}

.kb-key.space {
    min-width: 500px;
    height: 64px;
    font-size: 15px;
    color: #aaa;
    background: #3b4261;
    border-color: #1a1b26;
}

.kb-key.next-key {
    --finger-glow: #ffffff;
    animation: pulseKey 1s ease-in-out infinite;
    z-index: 2;
}

.kb-key.space.next-key {
    animation: pulseKeySpace 1s ease-in-out infinite;
}

@keyframes pulseKey {

    0%,
    100% {
        box-shadow: none;
        border: 2px solid #1a1b26;
        transform: scale(1);
    }

    50% {
        box-shadow: 0 0 12px var(--finger-glow), 0 0 24px var(--finger-glow);
        border: 4px solid var(--finger-glow);
        transform: scale(1.03);
    }
}

@keyframes pulseKeySpace {

    0%,
    100% {
        box-shadow: none;
        border: 2px solid #1a1b26;
    }

    50% {
        box-shadow: 0 0 12px var(--finger-glow), 0 0 24px var(--finger-glow);
        border: 4px solid var(--finger-glow);
    }
}

@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-3px);
    }

    75% {
        transform: translateX(3px);
    }
}

/* ═══ Particle Effects ═══ */
.particle {
    position: fixed;
    pointer-events: none;
    border-radius: 50%;
    z-index: 200;
    animation: particleFly 0.6s ease-out forwards;
}

@keyframes particleFly {
    0% {
        opacity: 1;
        transform: translate(0, 0) scale(1);
    }

    100% {
        opacity: 0;
        transform: translate(var(--dx), var(--dy)) scale(0);
    }
}

/* ═══ Survival Game ═══ */
#screen-survival {
    background: #0d1117;
}

#survival-canvas {
    width: 100%;
    height: 100%;
}

.survival-ui {
    position: fixed;
    top: 12px;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 10;
    font-size: 20px;
    font-weight: 700;
}

#surv-hp {
    color: var(--accent-red);
}

#surv-score {
    color: var(--accent-yellow);
}

#surv-wpm {
    color: var(--accent-cyan);
}

.surv-input-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12px;
    text-align: center;
    background: var(--bg-secondary);
    color: var(--accent-blue);
    font-size: 18px;
    z-index: 10;
}

.surv-back {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 20;
    font-size: 20px;
}

/* ═══ Game Over ═══ */
.gameover-box,
.complete-box {
    background: var(--bg-card);
    padding: 40px;
    border-radius: var(--radius);
    text-align: center;
    border: 1px solid var(--bg-tertiary);
    min-width: 320px;
    animation: scaleIn 0.3s ease;
}

@keyframes scaleIn {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.gameover-box h2 {
    font-size: 36px;
    margin-bottom: 16px;
}

.gameover-stats p,
.complete-stats p {
    font-size: 20px;
    margin: 8px 0;
    color: var(--fg-secondary);
}

.gameover-stats b,
.complete-stats b {
    color: var(--accent-cyan);
    font-size: 28px;
}

.gameover-box .btn,
.complete-box .btn {
    margin-top: 16px;
    width: 100%;
    justify-content: center;
}

.unlock-msg {
    color: var(--accent-green);
    font-weight: 600;
}

.unlock-msg.fail {
    color: var(--accent-red);
}

/* ═══ Leaderboard ═══ */
.leaderboard-container {
    max-width: 700px;
    width: 95%;
    height: 90vh;
    display: flex;
    flex-direction: column;
}

.leaderboard-header {
    text-align: center;
    padding: 16px 0;
    flex-shrink: 0;
}

.leaderboard-header h2 {
    font-size: 28px;
    margin: 8px 0;
}

.lb-tabs,
.lb-filters {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin: 8px 0;
}

.lb-tab,
.lb-filter {
    padding: 8px 16px;
    border: 1px solid var(--bg-tertiary);
    background: transparent;
    color: var(--fg-secondary);
    border-radius: 20px;
    font-family: var(--font);
    font-size: 14px;
    cursor: pointer;
    transition: var(--transition);
}

.lb-tab.active,
.lb-filter.active {
    background: var(--accent-blue);
    color: white;
    border-color: var(--accent-blue);
}

.leaderboard-table {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.lb-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    margin-bottom: 6px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    border: 1px solid var(--bg-tertiary);
    transition: var(--transition);
}

.lb-row:hover {
    border-color: var(--accent-blue);
}

.lb-row.me {
    border-color: var(--accent-green);
    background: rgba(80, 250, 123, 0.08);
}

.lb-rank {
    font-size: 20px;
    font-weight: 900;
    min-width: 36px;
    text-align: center;
}

.lb-rank.gold {
    color: #ffd700;
}

.lb-rank.silver {
    color: #c0c0c0;
}

.lb-rank.bronze {
    color: #cd7f32;
}

.lb-name {
    flex: 1;
    font-weight: 600;
}

.lb-platform {
    font-size: 12px;
    color: var(--fg-dim);
}

.lb-score {
    font-weight: 700;
    color: var(--accent-cyan);
    font-size: 18px;
}

.lb-empty {
    text-align: center;
    padding: 40px;
    color: var(--fg-dim);
    font-size: 16px;
}

/* ═══ Scrollbar ═══ */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

/* ═══ Responsive ═══ */
@media (max-width: 768px) {
    .title-gradient {
        font-size: 36px;
    }

    .practice-text {
        font-size: 20px;
        padding: 20px;
    }

    .kb-key {
        min-width: 32px;
        height: 38px;
        font-size: 12px;
    }

    .kb-key.space {
        min-width: 180px;
    }

    .stat {
        font-size: 13px;
    }
}

/* 
 * ==========================================
 * ⚠️ WARNING: DO NOT REMOVE THIS SCALING ⚠️
 * ==========================================
 * Responsive Height Scaling: 
 * If the screen/iframe is shorter than 850px, 
 * scale the entire game down to 75% so it fits perfectly.
 * The user explicitly requested to NEVER remove this section.
 */
@media (max-height: 850px) {
    .practice-container {
        zoom: 0.75;
    }
}