/* ===== ตัวแปรสี โหมดกลางวัน/กลางคืน ถนอมสายตา ===== */
:root {
    /* Light Mode - สีอ่อน ถนอมสายตา */
    --bg-primary: #f7f5f0;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-header: #e8f5e9;
    --text-primary: #2c3e2d;
    --text-secondary: #5a6a5b;
    --text-muted: #7a8a7b;
    --accent: #2e7d32;
    --accent-light: #81c784;
    --accent-dark: #1b5e20;
    --border: #d4e4d5;
    --shadow: rgba(46, 125, 50, 0.08);
    --success: #43a047;
    --danger: #e53935;
    --warning: #fb8c00;
    --info: #039be5;
    --quiz-correct: #e8f5e9;
    --quiz-wrong: #ffebee;
    --sidebar-bg: #ffffff;
    --overlay: rgba(0,0,0,0.4);
}

body.dark-mode {
    --bg-primary: #1a1f1a;
    --bg-secondary: #242a24;
    --bg-card: #2a322a;
    --bg-header: #1e2a1e;
    --text-primary: #e0e8e0;
    --text-secondary: #b0c0b0;
    --text-muted: #809080;
    --accent: #66bb6a;
    --accent-light: #a5d6a7;
    --accent-dark: #43a047;
    --border: #3a4a3a;
    --shadow: rgba(0,0,0,0.3);
    --success: #66bb6a;
    --danger: #ef5350;
    --warning: #ffa726;
    --info: #29b6f6;
    --quiz-correct: #1b3a1b;
    --quiz-wrong: #3a1b1b;
    --sidebar-bg: #1e2a1e;
    --overlay: rgba(0,0,0,0.6);
}

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

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: 'Sarabun', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.7;
    min-height: 100vh;
    transition: background 0.3s, color 0.3s;
}

/* ===== Header ===== */
.header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 2px 8px var(--shadow);
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    max-width: 1200px;
    margin: 0 auto;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--accent-dark);
}

.logo i { color: var(--accent); }

.menu-toggle, .theme-btn, .stats-btn {
    background: none;
    border: none;
    font-size: 1.3rem;
    color: var(--text-primary);
    cursor: pointer;
    padding: 0.4rem;
    border-radius: 8px;
    transition: background 0.2s;
}

.menu-toggle:hover, .theme-btn:hover, .stats-btn:hover {
    background: var(--border);
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

.user-bar {
    background: var(--bg-secondary);
    padding: 0.4rem 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    border-top: 1px solid var(--border);
}

.btn-sm {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    font-size: 0.9rem;
}

/* ===== Sidebar ===== */
.sidebar {
    position: fixed;
    top: 0;
    left: -280px;
    width: 280px;
    height: 100%;
    background: var(--sidebar-bg);
    z-index: 200;
    transition: left 0.3s ease;
    overflow-y: auto;
    box-shadow: 4px 0 20px var(--shadow);
}

.sidebar.open { left: 0; }

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-header);
}

.sidebar-header h3 {
    font-size: 1.1rem;
    color: var(--accent-dark);
}

.close-sidebar {
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--text-primary);
    cursor: pointer;
}

.nav-menu {
    list-style: none;
    padding: 0.5rem 0;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1.25rem;
    color: var(--text-primary);
    text-decoration: none;
    transition: background 0.2s;
    font-size: 0.95rem;
}

.nav-link:hover, .nav-link.active {
    background: var(--accent-light);
    color: var(--accent-dark);
}

.nav-link i { width: 22px; text-align: center; }

.has-submenu .submenu {
    list-style: none;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s;
    background: var(--bg-primary);
}

.has-submenu.open .submenu { max-height: 500px; }

.has-submenu.open .submenu-toggle .fa-chevron-down {
    transform: rotate(180deg);
}

.submenu .nav-link {
    padding-left: 2.5rem;
    font-size: 0.9rem;
}

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--overlay);
    z-index: 150;
    opacity: 0;
    visibility: hidden;
    transition: 0.3s;
}

.overlay.show {
    opacity: 1;
    visibility: visible;
}

/* ===== Main Content ===== */
.main-content {
    max-width: 900px;
    margin: 0 auto;
    padding: 1.25rem 1rem 3rem;
    min-height: calc(100vh - 160px);
}

/* ===== Cards & Components ===== */
.card {
    background: var(--bg-card);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 1.25rem;
    box-shadow: 0 4px 12px var(--shadow);
    border: 1px solid var(--border);
}

.card-title {
    font-size: 1.3rem;
    font-weight: 600;
    color: var(--accent-dark);
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.7rem 1.4rem;
    border: none;
    border-radius: 10px;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}

.btn-primary {
    background: var(--accent);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-dark);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--border);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background: var(--text-muted);
    color: white;
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-block {
    width: 100%;
    margin-top: 0.75rem;
}

/* ===== Home ===== */
.hero {
    text-align: center;
    padding: 2rem 1rem;
    background: linear-gradient(135deg, var(--bg-header), var(--bg-card));
    border-radius: 20px;
    margin-bottom: 1.5rem;
    border: 1px solid var(--border);
}

.hero h1 {
    font-size: 1.6rem;
    color: var(--accent-dark);
    margin-bottom: 0.5rem;
}

.hero p {
    color: var(--text-secondary);
    margin-bottom: 1.25rem;
}

.level-grid, .topic-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
}

.level-card, .topic-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.25rem 1rem;
    text-align: center;
    text-decoration: none;
    color: var(--text-primary);
    transition: all 0.25s;
    box-shadow: 0 2px 8px var(--shadow);
}

.level-card:hover, .topic-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px var(--shadow);
    border-color: var(--accent);
}

.level-card i, .topic-card i {
    font-size: 2rem;
    color: var(--accent);
    margin-bottom: 0.5rem;
    display: block;
}

.level-card h3, .topic-card h3 {
    font-size: 1rem;
    margin-bottom: 0.3rem;
}

.topic-card small {
    color: var(--text-muted);
    font-size: 0.8rem;
}

/* ===== Lesson ===== */
.lesson-header {
    background: var(--bg-header);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 1.25rem;
    border: 1px solid var(--border);
}

.lesson-header h1 {
    font-size: 1.5rem;
    color: var(--accent-dark);
    margin-bottom: 0.5rem;
}

.lesson-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.lesson-meta span {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.lesson-content h2 {
    font-size: 1.25rem;
    color: var(--accent-dark);
    margin: 1.5rem 0 0.75rem;
    padding-bottom: 0.4rem;
    border-bottom: 2px solid var(--accent-light);
}

.lesson-content h3 {
    font-size: 1.1rem;
    color: var(--accent);
    margin: 1.25rem 0 0.5rem;
}

.lesson-content p {
    margin-bottom: 0.9rem;
    text-align: justify;
}

.lesson-content ul, .lesson-content ol {
    margin: 0.75rem 0 1rem 1.5rem;
}

.lesson-content li {
    margin-bottom: 0.4rem;
}

.example-box {
    background: var(--bg-primary);
    border-left: 4px solid var(--accent);
    padding: 1rem 1.25rem;
    margin: 1rem 0;
    border-radius: 0 12px 12px 0;
}

.example-box h4 {
    color: var(--accent-dark);
    margin-bottom: 0.4rem;
    font-size: 1rem;
}

.img-container {
    text-align: center;
    margin: 1.25rem 0;
}

.img-container img {
    max-width: 100%;
    border-radius: 12px;
    box-shadow: 0 4px 12px var(--shadow);
    border: 1px solid var(--border);
}

.img-container figcaption {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.4rem;
}

.highlight-box {
    background: linear-gradient(135deg, var(--accent-light), transparent);
    padding: 1rem 1.25rem;
    border-radius: 12px;
    margin: 1rem 0;
    border: 1px solid var(--accent);
}

/* ===== Quiz ===== */
.quiz-section {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 2px dashed var(--border);
}

.quiz-question {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.quiz-question h4 {
    margin-bottom: 0.75rem;
    font-size: 1.05rem;
}

.quiz-options {
    list-style: none;
}

.quiz-options li {
    margin-bottom: 0.5rem;
}

.quiz-options label {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    padding: 0.7rem 1rem;
    background: var(--bg-primary);
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.2s;
    border: 1px solid transparent;
}

.quiz-options label:hover {
    background: var(--border);
}

.quiz-options input {
    margin-top: 0.25rem;
    accent-color: var(--accent);
}

.quiz-options label.selected {
    border-color: var(--accent);
    background: var(--accent-light);
}

.quiz-options label.correct {
    background: var(--quiz-correct);
    border-color: var(--success);
}

.quiz-options label.wrong {
    background: var(--quiz-wrong);
    border-color: var(--danger);
}

.explanation {
    margin-top: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--bg-primary);
    border-radius: 8px;
    font-size: 0.9rem;
    display: none;
}

.explanation.show { display: block; }

.explanation.correct-exp { border-left: 3px solid var(--success); }
.explanation.wrong-exp { border-left: 3px solid var(--danger); }

.quiz-result {
    text-align: center;
    padding: 1.5rem;
}

.quiz-result .score {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--accent);
}

/* ===== Games ===== */
.game-card {
    text-align: center;
}

.game-area {
    min-height: 200px;
    padding: 1rem;
}

.match-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
}

.match-item {
    padding: 1rem;
    background: var(--bg-primary);
    border: 2px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.95rem;
}

.match-item.selected {
    border-color: var(--accent);
    background: var(--accent-light);
}

.match-item.matched {
    opacity: 0.5;
    pointer-events: none;
    background: var(--quiz-correct);
}

/* ===== Progress ===== */
.progress-bar-container {
    background: var(--border);
    border-radius: 10px;
    height: 12px;
    overflow: hidden;
    margin: 0.5rem 0;
}

.progress-bar {
    height: 100%;
    background: var(--accent);
    border-radius: 10px;
    transition: width 0.5s;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

.stat-item {
    text-align: center;
    padding: 1rem;
    background: var(--bg-primary);
    border-radius: 12px;
}

.stat-item .num {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--accent);
}

/* ===== Certificate ===== */
.certificate-preview {
    background: linear-gradient(135deg, #f8f5e8, #fff);
    border: 8px double #2e7d32;
    border-radius: 8px;
    padding: 2rem 1.5rem;
    text-align: center;
    max-width: 600px;
    margin: 0 auto;
    color: #1a1a1a;
}

.certificate-preview h2 {
    font-size: 1.6rem;
    color: #1b5e20;
    margin-bottom: 0.5rem;
}

.certificate-preview .name {
    font-size: 1.4rem;
    font-weight: 700;
    color: #2e7d32;
    margin: 1rem 0;
    border-bottom: 2px solid #81c784;
    display: inline-block;
    padding: 0 1rem 0.3rem;
}

/* ===== Modal ===== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--overlay);
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: 0.3s;
    padding: 1rem;
}

.modal.show {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: var(--bg-card);
    border-radius: 16px;
    padding: 1.75rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

.modal-content h3 {
    margin-bottom: 0.75rem;
    color: var(--accent-dark);
}

.modal-content input {
    width: 100%;
    padding: 0.8rem 1rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-family: inherit;
    font-size: 1rem;
    margin: 1rem 0;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
}

.modal-actions .btn { flex: 1; }

/* ===== Footer ===== */
.footer {
    text-align: center;
    padding: 1.5rem 1rem;
    background: var(--bg-header);
    border-top: 1px solid var(--border);
    font-size: 0.85rem;
    color: var(--text-muted);
}

.footer .small { font-size: 0.75rem; margin-top: 0.3rem; }

/* ===== Responsive ===== */
@media (min-width: 768px) {
    .hero h1 { font-size: 2rem; }
    .level-grid { grid-template-columns: repeat(2, 1fr); }
    .topic-grid { grid-template-columns: repeat(3, 1fr); }
    .stat-grid { grid-template-columns: repeat(4, 1fr); }
    .match-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 480px) {
    html { font-size: 15px; }
    .card { padding: 1.1rem; }
    .lesson-header h1 { font-size: 1.3rem; }
}

/* Print for certificate */
@media print {
    body * { visibility: hidden; }
    .certificate-preview, .certificate-preview * { visibility: visible; }
    .certificate-preview {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        border: 8px double #2e7d32;
    }
}
