/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f7fafc;
  --bg-card: #ffffff;
  --text: #1a202c;
  --text-muted: #4a5568;
  --primary: #2b6cb0;
  --primary-dark: #1a365d;
  --accent: #ed8936;
  --border: #e2e8f0;
  --success: #38a169;
  --danger: #e53e3e;
  --shadow: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -1px rgba(0,0,0,.06);
  --radius: 12px;
  --header-h: 56px;
}
body.dark {
  --bg: #1a202c;
  --bg-card: #2d3748;
  --text: #f7fafc;
  --text-muted: #a0aec0;
  --primary: #63b3ed;
  --primary-dark: #2b6cb0;
  --border: #4a5568;
  --shadow: 0 4px 6px -1px rgba(0,0,0,.4);
}
html { font-size: 16px; scroll-behavior: smooth; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans Thai", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  padding-bottom: 80px;
  transition: background .3s, color .3s;
}
a { color: var(--primary); text-decoration: none; }
img { max-width: 100%; }
.hidden { display: none !important; }

/* ===== Header ===== */
.header {
  position: sticky; top: 0; z-index: 100;
  background: var(--primary-dark);
  color: #fff;
  height: var(--header-h);
  box-shadow: var(--shadow);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  height: 100%; padding: 0 12px; max-width: 800px; margin: 0 auto;
}
.logo { font-size: 1.25rem; font-weight: 700; letter-spacing: .5px; }
.menu-btn, .icon-btn {
  background: none; border: none; color: #fff;
  font-size: 1.4rem; padding: 8px; cursor: pointer;
  border-radius: 8px;
}
.menu-btn:hover, .icon-btn:hover { background: rgba(255,255,255,.15); }
.header-actions { display: flex; gap: 4px; }

/* ===== Sidebar ===== */
.sidebar {
  position: fixed; top: 0; left: -280px; width: 280px; height: 100%;
  background: var(--bg-card); z-index: 200;
  transition: left .3s ease; box-shadow: 4px 0 20px rgba(0,0,0,.15);
  overflow-y: auto;
}
.sidebar.open { left: 0; }
.sidebar-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; background: var(--primary-dark); color: #fff;
}
.close-btn { background: none; border: none; color: #fff; font-size: 1.3rem; cursor: pointer; }
.nav-list { list-style: none; padding: 12px 0; }
.nav-link {
  display: block; padding: 14px 20px; color: var(--text);
  font-size: 1rem; border-left: 4px solid transparent;
  transition: background .2s;
}
.nav-link:hover, .nav-link.active {
  background: rgba(43,108,176,.1); border-left-color: var(--primary);
  color: var(--primary);
}
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  z-index: 150; opacity: 0; pointer-events: none; transition: opacity .3s;
}
.overlay.show { opacity: 1; pointer-events: auto; }

/* ===== Main ===== */
.main { max-width: 800px; margin: 0 auto; padding: 16px 12px; }
.page { display: none; animation: fadeIn .3s ease; }
.page.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.page-title { font-size: 1.4rem; margin-bottom: 16px; color: var(--primary-dark); }
body.dark .page-title { color: var(--primary); }

/* ===== Hero ===== */
.hero {
  text-align: center; padding: 24px 12px;
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff; border-radius: var(--radius); margin-bottom: 20px;
}
.hero h2 { font-size: 1.5rem; line-height: 1.3; margin-bottom: 8px; }
.subtitle { opacity: .9; font-size: .95rem; margin-bottom: 12px; }
.desc { font-size: .9rem; opacity: .85; max-width: 400px; margin: 0 auto; }

/* ===== Cards ===== */
.card-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px; margin-bottom: 24px;
}
.card {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 20px 14px; text-align: center; box-shadow: var(--shadow);
  border: 1px solid var(--border); transition: transform .2s, box-shadow .2s;
  color: var(--text);
}
.card:hover { transform: translateY(-3px); box-shadow: 0 8px 20px rgba(0,0,0,.12); }
.card-icon { font-size: 2rem; display: block; margin-bottom: 8px; }
.card h3 { font-size: 1rem; margin-bottom: 4px; }
.card p { font-size: .8rem; color: var(--text-muted); }

/* ===== Info Box ===== */
.info-box {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 16px; border: 1px solid var(--border); margin-bottom: 20px;
}
.info-box h3 { font-size: 1.05rem; margin-bottom: 10px; color: var(--primary); }
.info-box ul { padding-left: 20px; font-size: .9rem; }
.info-box li { margin-bottom: 4px; }

/* ===== Unit List ===== */
.unit-list { display: flex; flex-direction: column; gap: 10px; }
.unit-item {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 14px 16px; border: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; cursor: pointer;
  transition: background .2s;
}
.unit-item:hover { background: rgba(43,108,176,.08); }
.unit-num {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; flex-shrink: 0;
}
.unit-title { font-weight: 600; font-size: .95rem; }
.unit-sub { font-size: .8rem; color: var(--text-muted); }

/* ===== Unit Content ===== */
.unit-content {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 20px 16px; border: 1px solid var(--border); margin-top: 16px;
}
.unit-content h3 { color: var(--primary); margin: 16px 0 8px; font-size: 1.1rem; }
.unit-content h3:first-child { margin-top: 0; }
.unit-content p, .unit-content li { font-size: .92rem; margin-bottom: 8px; }
.unit-content ul, .unit-content ol { padding-left: 20px; margin-bottom: 12px; }
.back-btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: none; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; color: var(--text); margin-bottom: 12px;
  font-size: .9rem;
}
.grammar-box, .culture-box, .ref-box {
  background: rgba(43,108,176,.06); border-left: 4px solid var(--primary);
  padding: 12px; border-radius: 0 8px 8px 0; margin: 12px 0;
  font-size: .9rem;
}
body.dark .grammar-box, body.dark .culture-box { background: rgba(99,179,237,.1); }
.speak-btn {
  background: var(--primary); color: #fff; border: none;
  border-radius: 6px; padding: 4px 10px; font-size: .8rem;
  cursor: pointer; margin-left: 6px;
}

/* ===== Vocab ===== */
.filter-bar {
  display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap;
}
.filter-bar select, .filter-bar input {
  flex: 1; min-width: 120px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg-card); color: var(--text); font-size: .95rem;
}
.vocab-list { display: flex; flex-direction: column; gap: 8px; }
.vocab-item {
  background: var(--bg-card); border-radius: 10px;
  padding: 12px 14px; border: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: flex-start;
}
.vocab-word { font-weight: 700; color: var(--primary); font-size: 1.05rem; }
.vocab-pos { font-size: .75rem; color: var(--text-muted); margin-left: 6px; }
.vocab-meaning { font-size: .9rem; margin-top: 2px; }
.vocab-example { font-size: .8rem; color: var(--text-muted); font-style: italic; margin-top: 4px; }

/* ===== Notes ===== */
.notes-content .note-card {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 16px; border: 1px solid var(--border); margin-bottom: 12px;
}
.note-card h3 { color: var(--accent); font-size: 1.05rem; margin-bottom: 8px; }
.note-card ul { padding-left: 18px; font-size: .9rem; }
.note-card li { margin-bottom: 6px; }

/* ===== Quiz / Exam ===== */
.quiz-select, .exam-options {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  margin-bottom: 20px;
}
.quiz-select select {
  padding: 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg-card); color: var(--text);
}
.btn {
  padding: 10px 18px; border: none; border-radius: 8px;
  font-size: .95rem; cursor: pointer; font-weight: 600;
  background: var(--border); color: var(--text);
  transition: background .2s, transform .1s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.success { background: var(--success); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.exam-btn { flex: 1; min-width: 120px; }

.quiz-area { margin-top: 12px; }
.question-card {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 16px; border: 1px solid var(--border); margin-bottom: 14px;
}
.q-num { font-size: .8rem; color: var(--text-muted); margin-bottom: 6px; }
.q-text { font-weight: 600; margin-bottom: 12px; font-size: .95rem; }
.option {
  display: block; width: 100%; text-align: left;
  padding: 10px 12px; margin-bottom: 6px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); cursor: pointer;
  font-size: .9rem; transition: background .2s;
}
.option:hover { background: rgba(43,108,176,.1); }
.option.selected { border-color: var(--primary); background: rgba(43,108,176,.15); }
.option.correct { border-color: var(--success); background: rgba(56,161,105,.15); }
.option.wrong { border-color: var(--danger); background: rgba(229,62,62,.12); }
.quiz-result {
  text-align: center; padding: 24px;
  background: var(--bg-card); border-radius: var(--radius);
  border: 1px solid var(--border);
}
.quiz-result .score { font-size: 2.5rem; font-weight: 700; color: var(--primary); }
.progress-bar {
  height: 6px; background: var(--border); border-radius: 3px;
  margin: 12px 0; overflow: hidden;
}
.progress-fill { height: 100%; background: var(--primary); transition: width .3s; }

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  z-index: 300; display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal-content {
  background: var(--bg-card); border-radius: 16px;
  padding: 24px 20px; max-width: 360px; width: 100%;
  text-align: center; box-shadow: 0 20px 40px rgba(0,0,0,.3);
}
.rest-modal h3 { margin-bottom: 12px; color: var(--primary); }
.dua { font-size: 1.3rem; direction: rtl; margin: 12px 0 4px; }
.dua-th { font-size: .85rem; color: var(--text-muted); margin-bottom: 12px; }
.rest-msg { font-size: .95rem; margin-bottom: 12px; }
.health-tip {
  font-size: .8rem; background: rgba(237,137,54,.1);
  border-radius: 8px; padding: 10px; margin-bottom: 16px; text-align: left;
}

/* ===== Print ===== */
@media print {
  .header, .sidebar, .overlay, .menu-btn, .icon-btn,
  .filter-bar, .quiz-select, .exam-options, .btn, .speak-btn,
  #restModal { display: none !important; }
  body { background: #fff; color: #000; padding: 0; }
  .main { max-width: 100%; padding: 0; }
  .page { display: block !important; }
  .unit-content, .vocab-item, .note-card, .question-card {
    break-inside: avoid; border: 1px solid #ccc; box-shadow: none;
  }
}

/* ===== Responsive ===== */
@media (min-width: 600px) {
  .hero h2 { font-size: 1.8rem; }
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}
