:root {
    --font-main: 'Sarabun', sans-serif;
    
    /* Light Theme Variables */
    --bg-gradient: linear-gradient(135deg, #e0eafc 0%, #cfdef3 100%);
    --glass-bg: rgba(255, 255, 255, 0.85);
    --glass-border: rgba(255, 255, 255, 0.5);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --primary-color: #0ea5e9; 
    --primary-hover: #0284c7;
    --secondary-color: rgba(241, 245, 249, 0.8);
    --secondary-hover: rgba(226, 232, 240, 0.9);
    --correct-color: #10b981;
    --incorrect-color: #ef4444;
    --warning-color: #f59e0b;
    --warning-hover: #d97706;
    
    /* Input & Keyboard Light */
    --input-bg: #ffffff;
    --key-bg: #ffffff;
    --key-border: #cbd5e1;
    --key-shadow: #94a3b8;
    --key-pressed: #f1f5f9;
    
    /* Classic Grid Colors */
    --target-bg: #fffde7; 
    --grid-border: #cbd5e1;
    
    /* Summary Light */
    --sum-header-bg: #0284c7;
    --sum-panel-bg: linear-gradient(to bottom right, #0ea5e9, #1e3a8a);
    --sum-btn-bg: #ffffff;
    --sum-btn-text: #334155;
    
    /* Effects */
    --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.1);
    --shadow-glow: 0 0 15px rgba(14, 165, 233, 0.5);
    --transition-smooth: all 0.3s ease;
}

/* Dark Theme Variables */
[data-theme="dark"] {
    --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    --glass-bg: rgba(30, 41, 59, 0.85);
    --glass-border: rgba(255, 255, 255, 0.1);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --primary-color: #38bdf8; 
    --primary-hover: #7dd3fc;
    --secondary-color: rgba(51, 65, 85, 0.8);
    --secondary-hover: rgba(71, 85, 105, 0.9);
    
    /* Input & Keyboard Dark */
    --input-bg: #0f172a;
    --key-bg: #334155;
    --key-border: #475569;
    --key-shadow: #1e293b;
    --key-pressed: #475569;
    
    /* Classic Grid Colors Dark */
    --target-bg: #1e293b; 
    --grid-border: #475569;
    
    /* Summary Dark */
    --sum-header-bg: #0369a1;
    --sum-panel-bg: linear-gradient(to bottom right, #0284c7, #172554);
    --sum-btn-bg: #334155;
    --sum-btn-text: #f8fafc;
    
    --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.4);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: var(--font-main);
    background: var(--bg-gradient);
    color: var(--text-main);
    min-height: 100vh;
    overflow-x: hidden;
    transition: background 0.5s ease;
}

.theme-toggle {
    position: fixed; top: 20px; right: 20px;
    background: var(--glass-bg); border: 1px solid var(--glass-border);
    border-radius: 50%; width: 50px; height: 50px; font-size: 24px;
    cursor: pointer; z-index: 1000; box-shadow: var(--shadow-card);
    backdrop-filter: blur(10px); transition: var(--transition-smooth);
    display: flex; justify-content: center; align-items: center;
}
.theme-toggle:hover { transform: scale(1.1); }

.app-wrapper {
    width: 100vw; min-height: 100vh; display: flex;
    justify-content: center; align-items: center; padding: 2vh 2vw; 
}

.view-section {
    width: 100%; height: 100%; display: flex;
    justify-content: center; align-items: center; animation: fadeIn 0.4s ease;
}
.view-section.hidden { display: none !important; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.glass-card {
    background: var(--glass-bg); border: 1px solid var(--glass-border);
    border-radius: 20px; box-shadow: var(--shadow-card);
    backdrop-filter: blur(15px); -webkit-backdrop-filter: blur(15px);
}

/* --- Login, Home & Menu Cards --- */
.login-card, .home-card, .menu-card {
    padding: clamp(20px, 5vw, 40px); width: 100%; max-width: 500px; text-align: center;
}

.logo-large { font-size: clamp(50px, 8vw, 80px); margin-bottom: 10px; }
.app-title { font-size: clamp(28px, 5vw, 40px); font-weight: 700; color: var(--primary-color); }
.app-subtitle { font-size: clamp(14px, 3vw, 16px); color: var(--text-muted); margin-bottom: 25px; }

.login-form { display: flex; flex-direction: column; gap: 15px; text-align: left; }
.premium-input {
    width: 100%; font-family: var(--font-main); padding: 15px; border-radius: 10px;
    border: 1px solid var(--key-border); background-color: var(--input-bg);
    color: var(--text-main); font-size: 16px; outline: none; transition: var(--transition-smooth);
}
.premium-input:focus { border-color: var(--primary-color); box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.2); }
.login-hint { font-size: 12px; color: var(--text-muted); text-align: center; margin-top: -5px; }

.divider { display: flex; align-items: center; text-align: center; margin: 10px 0; }
.divider::before, .divider::after { content: ''; flex: 1; border-bottom: 1px solid var(--key-border); }
.divider span { padding: 0 10px; color: var(--text-muted); font-size: 14px; }

.user-info-box { background: var(--secondary-color); padding: 20px; border-radius: 12px; margin-bottom: 30px; }
.highlight-text { font-weight: 700; color: var(--primary-color); font-size: clamp(20px, 4vw, 26px); display: block; margin-top: 5px;}
.user-email { font-size: 14px; color: var(--text-muted); margin-top: 5px; }
.home-buttons { display: flex; flex-direction: column; gap: 15px; }

.menu-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 25px; padding-bottom: 15px; border-bottom: 2px solid var(--secondary-color);
}
.menu-header h2 { font-size: clamp(20px, 4vw, 26px); color: var(--text-main); }
.menu-content { text-align: left; }

.form-group { display: flex; flex-direction: column; gap: 8px; margin-bottom: 15px; }
.form-group label { font-size: 16px; font-weight: 600; color: var(--text-muted); }
.premium-select {
    font-family: var(--font-main); padding: 15px; border-radius: 10px;
    border: 1px solid var(--key-border); font-size: 16px; outline: none;
    background-color: var(--input-bg); color: var(--text-main);
    transition: var(--transition-smooth); cursor: pointer;
}
.premium-select:focus { border-color: var(--primary-color); box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.2); }

/* --- Typing Area --- */
.typing-container {
    width: 100%; max-width: 1600px; height: 95vh;
    display: flex; flex-direction: column; gap: 1.5vh;
}

.header-card {
    flex: 0 0 auto; padding: 1.5vh 2vw;
    display: flex; justify-content: space-between; align-items: center;
    flex-wrap: wrap; gap: 15px; border-left: 6px solid var(--primary-color);
}
.title-section h2 { font-size: clamp(18px, 3vw, 24px); color: var(--text-main); }
.badge {
    display: inline-block; padding: 6px 14px; border-radius: 20px;
    font-size: 14px; font-weight: 600; margin-top: 5px;
}
.mode-ref { background-color: rgba(14, 165, 233, 0.1); color: var(--primary-color); }
.mode-exam { background-color: rgba(239, 68, 68, 0.1); color: var(--incorrect-color); }

.stats-section { display: flex; gap: clamp(10px, 2vw, 20px); flex-wrap: wrap; }
.stat-box {
    display: flex; flex-direction: column; align-items: center;
    background: var(--secondary-color); padding: 10px clamp(15px, 2vw, 25px);
    border-radius: 10px; flex: 1; min-width: 120px;
}
.stat-label { font-size: clamp(12px, 2vw, 14px); color: var(--text-muted); font-weight: 600; white-space: nowrap; }
.stat-value { font-size: clamp(22px, 4vw, 28px); font-weight: 700; color: var(--primary-color); }
.incorrect-text { color: var(--incorrect-color) !important; }

.typing-card { flex: 1 1 auto; display: flex; flex-direction: column; padding: 2vh 2vw; }

/* =========================================================
   TYPING BOARD STYLES
   ========================================================= */
.typing-board {
    flex: 1 1 auto; display: flex; flex-direction: column;
    border: 2px solid var(--grid-border); border-radius: 12px;
    background-color: var(--input-bg);
    overflow: hidden;
}

/* 1. สไตล์สำหรับโหมดแบบฝึกหัด (Classic Grid Box) */
.ref-style .board-row { display: flex; flex: 1 1 50%; width: 100%; }
.ref-style .target-row { 
    background-color: var(--target-bg); 
    border-bottom: 2px solid var(--grid-border); 
}
.ref-style .input-row {
    background-color: var(--input-bg); 
}
.ref-style .char-box {
    flex: 1; display: flex; align-items: center; justify-content: center;
    font-size: clamp(24px, 4vw, 42px); 
    font-weight: 600; 
    border-right: 1px solid var(--grid-border);
    white-space: pre; color: var(--text-main); line-height: 1;
}
.ref-style .char-box:last-child { border-right: none; }

/* 2. สไตล์สำหรับโหมดแบบทดสอบ (Continuous Text Paragraph) */
.exam-style .board-row { 
    display: block; 
    width: 100%; 
    height: 50%; 
    padding: 30px 40px;
    overflow-y: auto; 
    text-align: left;
    line-height: 2; 
}
.exam-style .target-row { background-color: var(--secondary-color); border-bottom: 2px dashed var(--key-border); }
.exam-style .char-box {
    flex: 0 0 auto; 
    display: inline-flex; align-items: center; justify-content: center;
    font-size: clamp(22px, 4vw, 36px); font-weight: 500; 
    border-right: none; 
    padding: 0 1px; 
    white-space: pre-wrap; 
    color: var(--text-main); line-height: 1.5;
}

/* =========================================================
   SPACE CHARACTER FIX (แก้ปัญหาช่องว่างมองไม่เห็น)
   ========================================================= */
.char-box.is-space {
    min-width: clamp(20px, 3vw, 35px); /* บังคับให้ช่องว่างมีความกว้างเสมอ */
    display: inline-flex;
}

/* เพิ่มขีดล่างจางๆ ให้ช่องว่างในบรรทัดตัวอย่าง (Visual Cue) */
.target-row .char-box.is-space {
    position: relative;
}
.target-row .char-box.is-space::after {
    content: '';
    position: absolute;
    bottom: 15%;
    left: 15%;
    right: 15%;
    height: 3px;
    background-color: var(--key-border);
    border-radius: 2px;
    opacity: 0.8;
}

/* Common Box Styles */
.target-box { color: var(--text-muted); }
.input-box { font-weight: 600; transition: var(--transition-smooth); position: relative; }

.input-box.active { border-bottom: 4px solid var(--primary-color); background-color: rgba(14, 165, 233, 0.1); animation: blink 1s infinite; }
@keyframes blink { 0%, 100% { border-bottom-color: var(--primary-color); } 50% { border-bottom-color: transparent; } }

.input-box.correct { color: var(--correct-color); }
.input-box.incorrect { color: var(--incorrect-color); background-color: rgba(239, 68, 68, 0.2); border-radius: 4px; }
.input-box.disabled { background-color: transparent; }
.ref-style .input-box.disabled { background-color: var(--secondary-color); }

/* --- Keyboard --- */
.keyboard-card { flex: 0 0 auto; padding: 2vh 2vw; }
.keyboard { display: flex; flex-direction: column; gap: clamp(4px, 1vh, 8px); align-items: center; width: 100%; }
.keyboard-row { display: flex; gap: clamp(4px, 0.5vw, 8px); width: 100%; justify-content: center; }
.key {
    background-color: var(--key-bg); border: 1px solid var(--key-border);
    border-bottom: clamp(2px, 0.5vh, 6px) solid var(--key-shadow); border-radius: 8px;
    flex: 1 1 auto; min-width: 20px; max-width: 70px; height: clamp(40px, 7vh, 70px); 
    display: flex; flex-direction: column; justify-content: center; align-items: center; 
    font-size: clamp(12px, 2vw, 18px); color: var(--text-main); 
    transition: all 0.1s; user-select: none;
}
.key .shift-char { font-size: clamp(10px, 1.5vw, 14px); color: var(--text-muted); }
.key .main-char { font-weight: 600; }
.key.pressed { transform: translateY(4px); border-bottom: 0px solid transparent; background-color: var(--key-pressed); }
.key.glow { border-color: var(--primary-color); box-shadow: var(--shadow-glow); background-color: rgba(14, 165, 233, 0.1); }

.key.space { flex: 6 1 auto; max-width: 500px; } 
.key.backspace { flex: 1.5 1 auto; max-width: 100px; } 
.key.tab { flex: 1.2 1 auto; max-width: 80px; }
.key.caps { flex: 1.4 1 auto; max-width: 90px; } 
.key.enter { flex: 1.6 1 auto; max-width: 110px; } 
.key.shift { flex: 2 1 auto; max-width: 130px; }

/* --- Summary Screen --- */
.summary-card {
    width: 100%; max-width: 700px; padding: 0; overflow: hidden;
    border: 4px solid var(--glass-border);
}
.summary-header { background-color: var(--sum-header-bg); color: white; padding: 25px; text-align: right; }
.summary-header h2 { font-size: clamp(22px, 4vw, 28px); font-weight: 700; letter-spacing: 1px; }

.summary-body { display: flex; flex-wrap: wrap; background-color: var(--input-bg); }

.summary-buttons {
    width: 100%; flex: 1 1 30%; padding: 30px 20px; display: flex; flex-direction: column; gap: 20px;
    background-color: var(--secondary-color); border-right: 2px solid var(--key-border);
    justify-content: center;
}

.btn-summary-action {
    background: var(--sum-btn-bg); border: 2px solid var(--key-shadow); padding: 15px;
    font-family: var(--font-main); font-size: clamp(16px, 3vw, 18px); font-weight: 700;
    color: var(--sum-btn-text); border-radius: 8px; cursor: pointer;
    transition: var(--transition-smooth); box-shadow: 0 4px 0 var(--key-shadow);
}
.btn-summary-action:hover { transform: translateY(2px); box-shadow: 0 2px 0 var(--key-shadow); background: var(--secondary-hover); }
.btn-summary-action:active { transform: translateY(4px); box-shadow: none; }

.summary-stats-panel {
    width: 100%; flex: 1 1 70%; background: var(--sum-panel-bg);
    padding: 40px 30px; display: grid; grid-template-columns: 1fr; gap: 20px;
}

.summary-stat-row { display: grid; grid-template-columns: 1fr 120px; align-items: center; gap: 20px; }
.s-label { color: white; font-size: clamp(16px, 3vw, 18px); font-weight: 600; text-shadow: 1px 1px 2px rgba(0,0,0,0.3); text-align: right; }
.s-value-box {
    background: white; padding: 8px 10px; border-radius: 6px;
    font-size: clamp(20px, 4vw, 24px); font-weight: 700; color: #0f172a;
    text-align: center; box-shadow: inset 0 2px 4px rgba(0,0,0,0.2); width: 100%;
}

/* --- TABLES (Leaderboard & Personal Stats) --- */
.table-card {
    width: 100%; max-width: 1400px; min-height: 85vh; padding: clamp(20px, 4vw, 40px);
    display: flex; flex-direction: column; max-height: 95vh;
}
.table-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 20px; padding-bottom: 15px; border-bottom: 2px solid var(--secondary-color);
    flex: 0 0 auto;
}
.table-header h2 { font-size: clamp(20px, 4vw, 28px); color: var(--primary-color); font-weight: 700; }

.tab-container { display: flex; gap: 10px; justify-content: center; margin-bottom: 20px; flex-wrap: wrap; flex: 0 0 auto; }
.sub-tab { margin-top: -10px; transform: scale(0.9); margin-bottom: 15px; }
.tab-btn {
    background: var(--secondary-color); border: none; padding: 10px 20px;
    border-radius: 8px; cursor: pointer; font-family: var(--font-main);
    font-weight: 600; color: var(--text-muted); transition: var(--transition-smooth);
    font-size: clamp(14px, 2vw, 16px);
}
.tab-btn:hover { background: var(--secondary-hover); }
.tab-btn.active { background: var(--primary-color); color: white; box-shadow: 0 4px 10px rgba(14, 165, 233, 0.3); }

.table-responsive { 
    width: 100%; flex: 1; overflow-y: auto; border-radius: 10px; 
    border: 1px solid var(--key-border); background: var(--input-bg);
}
.premium-table { width: 100%; border-collapse: collapse; text-align: left; }
.premium-table th {
    background: var(--secondary-color); padding: 15px; font-weight: 700;
    color: var(--text-main); border-bottom: 2px solid var(--key-border);
    white-space: nowrap; position: sticky; top: 0; z-index: 10;
}
.premium-table td { padding: 15px; border-bottom: 1px solid var(--key-border); color: var(--text-main); white-space: nowrap; }
.premium-table tr:hover td { background: rgba(14, 165, 233, 0.05); }

.rank-1 td { background: rgba(250, 204, 21, 0.1); font-weight: bold; }
.rank-2 td { background: rgba(148, 163, 184, 0.1); font-weight: bold; }
.rank-3 td { background: rgba(180, 83, 9, 0.1); font-weight: bold; }
.highlight-row td { background: rgba(16, 185, 129, 0.1) !important; border-left: 4px solid var(--correct-color); }

.more-row td { text-align: center; color: var(--text-muted); font-style: italic; padding: 20px; font-weight: 600; }
.more-row:hover td { background: transparent !important; }

/* --- Buttons & Controls --- */
.controls { display: flex; justify-content: center; flex-wrap: wrap; gap: 15px; margin-top: 2vh; }
button { font-family: var(--font-main); cursor: pointer; transition: var(--transition-smooth); }
.btn-primary { background: var(--primary-color); color: white; border: none; padding: 15px 30px; border-radius: 10px; font-size: 18px; font-weight: 600; box-shadow: 0 4px 15px rgba(14, 165, 233, 0.3); }
.btn-primary:hover { background: var(--primary-hover); transform: translateY(-2px); }
.btn-secondary { background: var(--secondary-color); color: var(--text-main); border: none; padding: 12px 24px; border-radius: 8px; font-size: 16px; font-weight: 600; }
.btn-secondary:hover { background: var(--secondary-hover); }
.btn-warning { background: var(--warning-color); color: white; border: none; padding: 15px 30px; border-radius: 10px; font-size: 18px; font-weight: 600; box-shadow: 0 4px 15px rgba(245, 158, 11, 0.3); }
.btn-warning:hover { background: var(--warning-hover); transform: translateY(-2px); }
.btn-text { background: none; border: none; color: var(--text-muted); font-size: 16px; text-decoration: underline; margin-top: 10px;}
.btn-text:hover { color: var(--primary-color); }
.btn-icon { background: none; border: none; font-size: 18px; color: var(--text-muted); }
.btn-icon:hover { color: var(--primary-color); }
.btn-icon-light { background: none; border: none; color: var(--sidebar-muted); font-size: 18px; cursor: pointer; transition: color 0.2s; }
.btn-icon-light:hover { color: white; }
.w-100 { width: 100%; }

.hidden { display: none !important; }

/* SweetAlert Custom */
.swal2-popup { font-family: var(--font-main) !important; border-radius: 16px !important; background: var(--card-bg) !important; color: var(--text-main) !important; backdrop-filter: blur(10px); }
.swal2-input { font-family: var(--font-main) !important; text-align: center !important; font-size: 18px !important; background: var(--key-bg) !important; color: var(--text-main) !important; }
.swal2-title { color: var(--text-main) !important; }