/* --- CẤU HÌNH MÀU SẮC & BIẾN (THEME) --- */
:root {
    /* Màu chủ đạo - Tươi hơn một chút */
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --success: #10b981;
    --success-bg: #ecfdf5;
    --warning: #f59e0b;
    --warning-hover: #d97706;
    --error: #ef4444;
    --error-bg: #fef2f2;
    --border-radius: 16px;

    /* Light Mode */
    --bg-body: #f8fafc;
    --bg-card: #ffffff;
    --text-main: #1e293b;
    --text-sub: #64748b;
    --bg-surface: #f1f5f9;
    --border-color: #e2e8f0;
    --input-bg: #ffffff;
    
    /* Shadow chuyên nghiệp */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --focus-ring: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

/* DARK MODE - Tối ưu độ tương phản */
[data-theme="dark"] {
    --primary: #60a5fa;
    --primary-hover: #93c5fd;
    --bg-body: #0f172a;
    --bg-card: #1e293b;
    --text-main: #f8fafc;
    --text-sub: #cbd5e1;
    --bg-surface: #334155;
    --border-color: #475569;
    --input-bg: #1e293b;
    --success-bg: rgba(16, 185, 129, 0.1);
    --error-bg: rgba(239, 68, 68, 0.1);
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
    --focus-ring: 0 0 0 3px rgba(96, 165, 250, 0.3);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-body);
    color: var(--text-main);
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: flex-start; /* Better for mobile scrolling */
    min-height: 100vh;
    transition: background 0.3s ease, color 0.3s ease;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* --- LAYOUT CHÍNH --- */
.box {
    width: 100%;
    max-width: 800px;
    background: var(--bg-card);
    padding: 40px;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
    transition: background 0.3s ease, box-shadow 0.3s ease;
    position: relative;
    border: 1px solid var(--border-color);
    box-sizing: border-box; /* Ensure padding doesn't break width */
}

/* Header, Nav & Theme Toggle */
.header-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 30px;
}

.nav-action-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 15px;
}

.main-nav {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
}

.nav-link {
    text-decoration: none;
    color: var(--text-sub);
    font-weight: 500;
    padding: 8px 16px;
    border-radius: 8px;
    transition: all 0.2s;
    font-size: 14px;
}

.nav-link:hover {
    background-color: var(--bg-surface);
    color: var(--text-main);
}

.nav-link.active {
    background-color: var(--primary);
    color: white;
    box-shadow: var(--shadow-sm);
}
.volume-input {
    width: 50px !important;
    margin: 0 !important;
    padding: 4px 8px !important;
    text-align: center;
    font-size: 13px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background: var(--input-bg);
    color: var(--text-main);
}
.volume-control {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    max-width: 250px;
    margin: 0 auto;
}
.player-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin-bottom: 15px;
}

/* Trạng thái active cho nút Play/Pause và Loop */
.player-controls button.playing,
.player-controls button.active {
    background-color: var(--success);
    border-color: var(--success);
}
.player-controls button.playing:hover,
.player-controls button.active:hover {
    background-color: #059669; /* Darker success */
    border-color: #059669;
}

.volume-input::-webkit-outer-spin-button,
.volume-input::-webkit-inner-spin-button {
    opacity: 1;
}

h2 {
    color: var(--primary);
    margin: 0;
    font-weight: 800;
    font-size: 28px;
    letter-spacing: -0.5px;
    text-align: center;
    line-height: 1.2;
}

h3 {
    margin-bottom: 20px;
    font-weight: 600;
    line-height: 1.6;
    color: var(--text-main);
    font-size: 18px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.theme-toggle {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    cursor: pointer;
    width: 40px;
    height: 40px;
    color: var(--text-sub);
    border-radius: 50%;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    box-shadow: var(--shadow-sm);
}
.theme-toggle:hover {
    color: var(--text-main);
    background-color: var(--border-color);
    transform: rotate(30deg);
}

.login-btn {
    font-size: 14px;
    font-weight: 600;
    background-color: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-main);
    border-radius: 8px;
    padding: 8px 16px;
    transition: all 0.2s;
}
.login-btn:hover {
    background-color: var(--bg-surface);
    border-color: var(--text-sub);
}
.login-btn.logged-in {
    background-color: var(--success-bg);
    color: var(--success);
    border-color: var(--success);
}

/* User Menu & Dropdown */
.user-menu-container {
    position: relative;
}

.dropdown-menu {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    width: 180px;
    display: flex;
    flex-direction: column;
    padding: 8px 0;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
}

.dropdown-menu.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.dropdown-menu a {
    padding: 10px 16px;
    color: var(--text-main);
    text-decoration: none;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s;
    font-weight: 500;
}

.dropdown-menu a:hover {
    background: var(--bg-surface);
}

/* --- FORM ELEMENTS --- */
input[type="number"], input[type="text"], input[type="password"], textarea, select {
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background-color: var(--input-bg);
    color: var(--text-main);
    box-sizing: border-box;
    font-size: 15px;
    transition: all 0.2s ease;
    width: 100%;
    margin-bottom: 15px;
    font-family: inherit;
}

/* Hiệu ứng Focus chuyên nghiệp */
input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: var(--focus-ring);
}

textarea {
    height: 100px;
    font-family: 'JetBrains Mono', Consolas, monospace;
    font-size: 14px;
    resize: vertical;
    line-height: 1.5;
}

.input-group-3 {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}
.input-group-3 input {
    flex: 1 1 calc(33.333% - 12px); /* Flexible width */
    min-width: 120px;
    margin-bottom: 0; /* Override default margin */
}

.file-upload-wrapper {
    margin-bottom: 20px;
    background: var(--bg-surface);
    padding: 25px 15px;
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    text-align: center;
    transition: all 0.2s;
    color: var(--text-sub);
    cursor: pointer;
    box-sizing: border-box;
}
.file-upload-wrapper:hover {
    border-color: var(--primary);
    background: rgba(59, 130, 246, 0.05);
    color: var(--primary);
}

/* --- BUTTONS --- */
button {
    background-color: var(--primary);
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    margin: 6px 0;
    box-shadow: var(--shadow-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-sizing: border-box;
}

button:hover {
    background-color: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
button:active {
    transform: translateY(0);
    box-shadow: none;
}
button:disabled {
    background-color: var(--border-color);
    color: var(--text-sub);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Các loại nút */
button.btn-retry { background-color: var(--warning); color: #fff; }
button.btn-retry:hover { background-color: var(--warning-hover); }

button.btn-reset { background-color: var(--bg-surface); color: var(--text-main); border: 1px solid var(--border-color); }
button.btn-reset:hover { background-color: var(--border-color); color: var(--text-main); }

button.btn-danger { background-color: var(--error); }
button.btn-danger:hover { background-color: #dc2626; }

button.btn-success { background-color: var(--success); }
button.btn-success:hover { background-color: #059669; }

button#endBtn { 
    background-color: transparent; 
    border: 1px solid var(--border-color);
    color: var(--text-sub);
    font-size: 14px; 
    padding: 8px 16px; 
    float: right; 
    box-shadow: none;
}
button#endBtn:hover {
    background-color: var(--error-bg);
    color: var(--error);
    border-color: var(--error);
}

/* --- QUIZ & STATUS --- */
.status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
    gap: 10px;
}
.timer { font-weight: 700; color: var(--error); font-size: 1.2em; font-variant-numeric: tabular-nums; }
.progress { font-weight: 700; color: var(--primary); font-size: 1.1em; }

.progress-bar-fill {
    height: 100%;
    background: var(--primary);
    transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 5px;
}

.option-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Card đáp án */
.option-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    padding: 16px 20px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    font-size: 16px;
    line-height: 1.5;
    position: relative;
    color: var(--text-main);
    display: flex;
    align-items: center;
    word-break: break-word; /* Prevent text overflow */
}

.option-card:hover {
    background: var(--bg-card);
    border-color: var(--primary);
    box-shadow: var(--shadow-md);
    transform: translateX(4px);
}

/* Trạng thái đúng/sai */
.option-card.correct {
    background-color: var(--success-bg);
    border-color: var(--success);
    color: #065f46;
    font-weight: 600;
}
[data-theme="dark"] .option-card.correct { color: #34d399; }
.option-card.correct::after {
    content: "✓";
    margin-left: auto;
    font-weight: bold;
    font-size: 1.2em;
    padding-left: 10px;
}

.option-card.wrong {
    background-color: var(--error-bg);
    border-color: var(--error);
    color: #991b1b;
    font-weight: 600;
}
[data-theme="dark"] .option-card.wrong { color: #f87171; }
.option-card.wrong::after {
    content: "✕";
    margin-left: auto;
    font-weight: bold;
    font-size: 1.2em;
    padding-left: 10px;
}

.disabled-click { pointer-events: none; opacity: 0.7; }

/* --- RESULT & REVIEW --- */
.result-summary {
    text-align: center;
    padding: 30px 20px;
    background: var(--bg-surface);
    border-radius: 16px;
    margin-bottom: 30px;
    border: 1px solid var(--border-color);
}
.result-summary h3 { font-size: 22px; color: var(--primary); margin-top: 0; }

.review-item {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: var(--shadow-sm);
    word-break: break-word;
}

.review-option {
    padding: 10px 14px;
    margin-top: 8px;
    border-radius: 8px;
    background: var(--bg-surface);
    font-size: 15px;
    color: var(--text-main);
    border: 1px solid transparent;
}
.review-option.correct-mark {
    background: var(--success-bg);
    border-color: var(--success);
    color: #065f46;
}
[data-theme="dark"] .review-option.correct-mark { color: #34d399; }

.review-option.wrong-mark {
    background: var(--error-bg);
    border-color: var(--error);
    color: #991b1b;
}
[data-theme="dark"] .review-option.wrong-mark { color: #f87171; }

hr { border: 0; border-top: 1px solid var(--border-color); margin: 30px 0; }

.hidden { display: none !important; }
.fade-in { animation: fadeIn 0.4s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Grid setup & Import log */
.start-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 15px;
    margin-bottom: 25px;
}

.start-grid button {
    width: 100%;
    margin: 0;
}

.custom-setup {
    background: var(--bg-surface);
    padding: 20px;
    border-radius: 12px;
    margin-top: 15px;
    border: 1px solid var(--border-color);
    box-sizing: border-box;
}

.import-log {
    font-size: 14px;
    color: var(--text-sub);
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    padding: 10px 15px;
    border-radius: 8px;
    margin-top: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
    word-break: break-all;
}

.btn-delete-item {
    background-color: var(--error-bg);
    color: var(--error);
    border: none;
    width: 28px;
    height: 28px;
    min-width: 28px; /* Prevent shrinking */
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 14px;
    margin: 0; 
    padding: 0;
    transition: all 0.2s;
    box-shadow: none;
}
.btn-delete-item:hover { background-color: var(--error); color: white; transform: scale(1.1); }

.playlist-item {
    border-left: none !important;
    border-right: none !important;
    border-top: none !important;
    border-bottom: 1px solid var(--border-color) !important;
    margin-bottom: 0 !important;
    border-radius: 0 !important;
}
.playlist-item:last-child {
    border-bottom: none !important;
}

/* Stat Buttons (Filter) */
.stat-btn {
    display: inline-block;
    padding: 10px 20px;
    margin: 5px;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--bg-card);
    color: var(--text-sub);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.stat-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    color: var(--text-main);
}

.stat-btn.correct-stat { border-color: var(--success); color: var(--success); }
.stat-btn.correct-stat:hover { background: var(--success-bg); }
.stat-btn.active-correct { background: var(--success); color: white; border-color: var(--success); }

.stat-btn.wrong-stat { border-color: var(--error); color: var(--error); }
.stat-btn.wrong-stat:hover { background: var(--error-bg); }
.stat-btn.active-wrong { background: var(--error); color: white; border-color: var(--error); }

#detailArea { margin-top: 30px; text-align: left; }

/* --- LOGIN MODAL --- */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0; visibility: hidden;
    transition: all 0.3s ease;
    padding: 20px; /* Add padding so it doesn't touch edges on small screens */
    box-sizing: border-box;
}
.modal-overlay.active { opacity: 1; visibility: visible; }

.modal-box {
    background: var(--bg-card);
    padding: 30px;
    border-radius: 20px;
    width: 100%;
    max-width: 360px; /* Adjusted from fixed 320px to max-width */
    box-shadow: var(--shadow-lg);
    transform: scale(0.9);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: center;
    border: 1px solid var(--border-color);
    position: relative;
    box-sizing: border-box;
}
.modal-overlay.active .modal-box { transform: scale(1); }

.modal-close {
    position: absolute; top: 15px; right: 15px;
    background: none; border: none; font-size: 24px; cursor: pointer;
    color: var(--text-sub);
    transition: color 0.2s;
    line-height: 1;
    box-shadow: none;
    padding: 5px;
    margin: 0;
}
.modal-close:hover { color: var(--error); transform: none; box-shadow: none; }

/* --- RESPONSIVE MEDIA QUERIES --- */
@media (max-width: 768px) {
    .box {
        padding: 30px 20px;
        border-radius: 12px;
    }
    
    h2 {
        font-size: 24px;
    }
    
    .header-container {
        margin-bottom: 20px;
    }
}

@media (max-width: 480px) {
    body {
        padding: 10px;
    }

    .box {
        padding: 20px 15px;
        border-radius: 10px;
        border: none;
        box-shadow: none; /* Cleaner look on small devices */
    }

    h2 {
        font-size: 22px;
    }

    .login-btn {
        flex-grow: 1;
        justify-content: center;
    }

    .nav-action-row {
        justify-content: center;
        flex-direction: column-reverse; /* Đẩy thanh Nav xuống dưới nút Đăng nhập khi ở mobile */
    }
    
    .main-nav {
        justify-content: center;
        width: 100%;
    }

    .input-group-3 {
        flex-direction: column;
        gap: 10px;
    }
    
    .input-group-3 input {
        width: 100%;
    }

    .start-grid {
        grid-template-columns: 1fr 1fr; /* 2 columns on very small screens */
    }

    button {
        width: 100%;
        margin: 5px 0;
    }

    button#endBtn {
        width: auto;
        float: none;
        display: block;
        margin: 10px auto;
    }
    
    .stat-btn {
        width: 100%;
        margin: 5px 0;
        box-sizing: border-box;
    }

    .option-card {
        padding: 14px 16px;
        font-size: 15px;
    }
    
    .status-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 5px;
    }
    
    .timer {
        align-self: flex-end;
        margin-top: -25px; /* Pull timer up to align with progress on next line visually if needed or keep it stacked */
    }

    #dataManager h3, #gameMode h3 {
        font-size: 16px;
    }

    .custom-setup {
        padding: 15px;
    }
}
