/* ============================================
   สังคมศึกษา ม.ต้น - Eye-friendly Learning UI
   Light / Dark Mode + Mobile First
   ============================================ */

:root {
    /* Light Mode - Soft eye-friendly palette */
    --bg-primary: #f7f5f0;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-soft: #f0ebe3;
    --text-primary: #2c2a26;
    --text-secondary: #5a564e;
    --text-muted: #8a857a;
    --accent: #2a6f6f;
    --accent-hover: #1e5555;
    --accent-soft: #e0f0f0;
    --success: #3d7a4a;
    --success-bg: #e8f5e9;
    --warning: #b8860b;
    --warning-bg: #fff8e6;
    --error: #a33a3a;
    --error-bg: #fdecea;
    --border: #e0dcd3;
    --shadow: 0 2px 12px rgba(44, 42, 38, 0.08);
    --shadow-lg: 0 8px 30px rgba(44, 42, 38, 0.12);
    --radius: 12px;
    --radius-sm: 8px;
    --header-h: 56px;
    --sidebar-w: 280px;
    --font: 'Sarabun', system-ui, -apple-system, sans-serif;
    --transition: 0.25s ease;
}

[data-theme="dark"] {
    --bg-primary: #1a1c1e;
    --bg-secondary: #242628;
    --bg-card: #2a2d30;
    --bg-soft: #323538;
    --text-primary: #e8e6e1;
    --text-secondary: #b0aca4;
    --text-muted: #7a766e;
    --accent: #5bb5b5;
    --accent-hover: #7acccc;
    --accent-soft: #1e3535;
    --success: #6bb87a;
    --success-bg: #1e2e22;
    --warning: #d4a84b;
    --warning-bg: #2e2a1a;
    --error: #d46b6b;
    --error-bg: #2e1e1e;
    --border: #3a3d40;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.4);
}

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

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

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.7;
    min-height: 100vh;
    transition: background var(--transition), color var(--transition);
    -webkit-font-smoothing: antialiased;
}

/* ========== Header ========== */
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-h);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    z-index: 1000;
    box-shadow: var(--shadow);
}

.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    padding: 0 16px;
    max-width: 1400px;
    margin: 0 auto;
}

.menu-toggle {
    display: flex;
    flex-direction: column;
    gap: 5px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
}

.menu-toggle span {
    display: block;
    width: 22px;
    height: 2px;
    background: var(--text-primary);
    border-radius: 2px;
    transition: var(--transition);
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 1.1rem;
}

.logo-icon { font-size: 1.4rem; }

.header-actions {
    display: flex;
    gap: 8px;
}

.theme-toggle, .user-btn {
    background: var(--bg-soft);
    border: 1px solid var(--border);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.theme-toggle:hover, .user-btn:hover {
    background: var(--accent-soft);
    border-color: var(--accent);
}

/* ========== Sidebar ========== */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-w);
    height: 100vh;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    z-index: 1100;
    transform: translateX(-100%);
    transition: transform var(--transition);
    overflow-y: auto;
    padding-bottom: 40px;
}

.sidebar.open {
    transform: translateX(0);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    height: var(--header-h);
}

.sidebar-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.close-sidebar {
    background: none;
    border: none;
    font-size: 1.8rem;
    cursor: pointer;
    color: var(--text-secondary);
    line-height: 1;
}

.nav-menu {
    list-style: none;
    padding: 12px 0;
}

.nav-menu > li > a,
.nav-menu .submenu a {
    display: block;
    padding: 12px 20px;
    color: var(--text-primary);
    text-decoration: none;
    font-size: 0.95rem;
    transition: var(--transition);
    border-left: 3px solid transparent;
}

.nav-menu > li > a:hover,
.nav-menu .submenu a:hover,
.nav-menu > li > a.active {
    background: var(--accent-soft);
    border-left-color: var(--accent);
    color: var(--accent);
}

.has-submenu > a::after {
    content: '▾';
    float: right;
    font-size: 0.8rem;
    opacity: 0.6;
}

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

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

.submenu a {
    padding-left: 36px;
    font-size: 0.9rem;
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1050;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition);
}

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

/* ========== Main Content ========== */
.main-content {
    margin-top: var(--header-h);
    padding: 20px 16px 60px;
    max-width: 900px;
    margin-left: auto;
    margin-right: auto;
    min-height: calc(100vh - var(--header-h));
}

/* ========== Cards & Components ========== */
.card {
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
    transition: var(--transition);
}

.card:hover {
    box-shadow: var(--shadow-lg);
}

.card-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
}

@media (min-width: 600px) {
    .card-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 900px) {
    .card-grid.three {
        grid-template-columns: repeat(3, 1fr);
    }
}

.strand-card {
    text-align: center;
    cursor: pointer;
    padding: 28px 16px;
}

.strand-card .icon {
    font-size: 2.5rem;
    margin-bottom: 12px;
}

.strand-card h3 {
    font-size: 1.05rem;
    margin-bottom: 8px;
    color: var(--accent);
}

.strand-card p {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

/* ========== Typography ========== */
h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 12px; line-height: 1.3; }
h2 { font-size: 1.3rem; font-weight: 600; margin-bottom: 10px; color: var(--accent); }
h3 { font-size: 1.1rem; font-weight: 600; margin-bottom: 8px; }
h4 { font-size: 1rem; font-weight: 600; margin-bottom: 6px; }

p { margin-bottom: 12px; }
ul, ol { margin: 0 0 16px 20px; }
li { margin-bottom: 6px; }

.lead {
    font-size: 1.05rem;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

/* ========== Lesson ========== */
.lesson-header {
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 2px solid var(--accent-soft);
}

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

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    background: var(--accent-soft);
    color: var(--accent);
}

.badge.success { background: var(--success-bg); color: var(--success); }
.badge.warning { background: var(--warning-bg); color: var(--warning); }

.lesson-section {
    margin-bottom: 28px;
}

.lesson-section h2 {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 14px;
}

.example-box {
    background: var(--bg-soft);
    border-left: 4px solid var(--accent);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    padding: 16px;
    margin: 16px 0;
}

.example-box h4 {
    color: var(--accent);
    margin-bottom: 8px;
}

.example-box .ex-num {
    font-weight: 600;
    color: var(--accent);
}

.img-figure {
    margin: 20px 0;
    text-align: center;
}

.img-figure img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

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

/* ========== Quiz ========== */
.quiz-container {
    margin-top: 32px;
    padding-top: 24px;
    border-top: 2px dashed var(--border);
}

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

.quiz-question h4 {
    margin-bottom: 12px;
    display: flex;
    gap: 8px;
}

.quiz-question .q-num {
    background: var(--accent);
    color: #fff;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.quiz-options {
    list-style: none;
    margin: 0;
    padding: 0;
}

.quiz-options li {
    margin-bottom: 8px;
}

.quiz-options label {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    background: var(--bg-primary);
}

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

.quiz-options input[type="radio"],
.quiz-options input[type="checkbox"] {
    margin-top: 3px;
    accent-color: var(--accent);
}

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

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

.explanation {
    margin-top: 10px;
    padding: 12px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    display: none;
}

.explanation.show {
    display: block;
}

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

.explanation.wrong-exp {
    background: var(--error-bg);
    border-left: 3px solid var(--error);
}

/* ========== Buttons ========== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.95rem;
    font-weight: 500;
    border: none;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
}

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

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

.btn-secondary {
    background: var(--bg-soft);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

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

.btn-block {
    width: 100%;
}

.btn-group {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 20px;
}

/* ========== Progress ========== */
.progress-bar-wrap {
    background: var(--bg-soft);
    border-radius: 20px;
    height: 12px;
    overflow: hidden;
    margin: 8px 0 16px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), #4a9a9a);
    border-radius: 20px;
    transition: width 0.5s ease;
}

.stat-card {
    text-align: center;
    padding: 20px;
}

.stat-card .stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent);
}

.stat-card .stat-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* ========== Games ========== */
.game-card {
    cursor: pointer;
    text-align: center;
    padding: 24px;
}

.game-card .game-icon {
    font-size: 2.8rem;
    margin-bottom: 10px;
}

.memory-board {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    max-width: 400px;
    margin: 20px auto;
}

.memory-card {
    aspect-ratio: 1;
    background: var(--accent);
    color: #fff;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    cursor: pointer;
    transition: var(--transition);
    user-select: none;
}

.memory-card.flipped, .memory-card.matched {
    background: var(--bg-card);
    color: var(--text-primary);
    border: 2px solid var(--accent);
}

.memory-card.matched {
    opacity: 0.6;
    cursor: default;
}

/* ========== Modal ========== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition);
}

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

.modal-content {
    background: var(--bg-secondary);
    border-radius: var(--radius);
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    transform: translateY(20px);
    transition: transform var(--transition);
}

.modal.show .modal-content {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 { margin: 0; }

.modal-close {
    background: none;
    border: none;
    font-size: 1.6rem;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
}

.modal-body {
    padding: 20px;
}

.modal-body label {
    display: block;
    font-weight: 500;
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.modal-body input[type="text"],
.modal-body select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 1rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    margin-bottom: 16px;
}

.modal-body input:focus,
.modal-body select:focus {
    outline: none;
    border-color: var(--accent);
}

.modal-body .note {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.modal-footer {
    padding: 12px 20px 20px;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* ========== Certificate Preview ========== */
.cert-preview {
    background: linear-gradient(135deg, #f8f4e8, #e8f0f0);
    border: 3px double var(--accent);
    border-radius: var(--radius);
    padding: 30px 20px;
    text-align: center;
    margin: 20px 0;
    color: #2c2a26;
}

.cert-preview h2 {
    color: #2a6f6f;
    font-size: 1.4rem;
}

/* ========== Responsive ========== */
@media (min-width: 768px) {
    .main-content {
        padding: 28px 24px 80px;
    }
    h1 { font-size: 1.9rem; }
}

@media (min-width: 1024px) {
    .sidebar {
        transform: translateX(0);
    }
    .sidebar-overlay { display: none; }
    .menu-toggle { display: none; }
    .close-sidebar { display: none; }
    .main-content {
        margin-left: var(--sidebar-w);
        max-width: calc(100% - var(--sidebar-w));
        padding-left: 32px;
        padding-right: 32px;
    }
    .header-inner {
        padding-left: calc(var(--sidebar-w) + 16px);
    }
}

/* ========== Utility ========== */
.text-center { text-align: center; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }
.mb-2 { margin-bottom: 16px; }
.hidden { display: none !important; }
.fade-in {
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.back-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--accent);
    text-decoration: none;
    font-size: 0.9rem;
    margin-bottom: 16px;
    cursor: pointer;
}

.back-link:hover { text-decoration: underline; }

/* Loading */
.loading {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}
