/* ===== CSS Variables & Reset ===== */
:root {
  --primary: #c41e3a;
  --primary-dark: #9a1830;
  --secondary: #1a365d;
  --accent: #e53e3e;
  --bg: #f7fafc;
  --bg-card: #ffffff;
  --text: #1a202c;
  --text-muted: #4a5568;
  --border: #e2e8f0;
  --success: #38a169;
  --danger: #e53e3e;
  --warning: #d69e2e;
  --radius: 12px;
  --shadow: 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);
  --header-h: 56px;
  --font: 'Sarabun', 'Noto Sans Thai', 'Hiragino Sans', 'Yu Gothic', sans-serif;
}

[data-theme="dark"] {
  --primary: #fc8181;
  --primary-dark: #e53e3e;
  --secondary: #90cdf4;
  --bg: #1a202c;
  --bg-card: #2d3748;
  --text: #f7fafc;
  --text-muted: #a0aec0;
  --border: #4a5568;
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.3);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.4);
}

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

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  min-height: 100vh;
  padding-top: var(--header-h);
  transition: background 0.3s, color 0.3s;
}

/* ===== Header ===== */
.header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--primary);
  color: #fff;
  z-index: 1000;
  box-shadow: var(--shadow);
}

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

.logo { font-size: 1.25rem; font-weight: 700; }

.menu-btn, .icon-btn, .close-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: background 0.2s;
}
.menu-btn:hover, .icon-btn:hover { background: rgba(255,255,255,0.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: 1100;
  transition: left 0.3s ease;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.sidebar.open { left: 0; }

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
}
.close-btn { color: #fff; font-size: 1.2rem; }

.nav-list { list-style: none; padding: 8px 0; }
.nav-section {
  padding: 12px 16px 4px;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.05em;
}
.nav-link {
  display: block;
  padding: 10px 16px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.95rem;
  transition: background 0.2s, color 0.2s;
  border-left: 3px solid transparent;
}
.nav-link:hover, .nav-link.active {
  background: rgba(196,30,58,0.08);
  color: var(--primary);
  border-left-color: var(--primary);
}
[data-theme="dark"] .nav-link:hover,
[data-theme="dark"] .nav-link.active {
  background: rgba(252,129,129,0.15);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1050;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s;
}
.overlay.show { opacity: 1; visibility: visible; }

/* ===== Main ===== */
.main {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 16px 80px;
}

.page { display: none; }
.page.active { display: block; animation: fadeIn 0.3s ease; }

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

/* ===== Hero ===== */
.hero {
  text-align: center;
  padding: 32px 16px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  border-radius: var(--radius);
  margin-bottom: 24px;
}
.hero h2 { font-size: 1.75rem; margin-bottom: 8px; }
.subtitle { opacity: 0.9; font-size: 0.95rem; margin-bottom: 12px; }
.desc { opacity: 0.85; font-size: 0.9rem; max-width: 500px; margin: 0 auto; }

/* ===== Cards ===== */
.card-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
}
@media (max-width: 480px) {
  .card-grid { grid-template-columns: 1fr; }
}

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px 16px;
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s;
  border: 1px solid var(--border);
}
.card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
}
.card-icon { font-size: 2rem; display: block; margin-bottom: 8px; }
.card h3 { font-size: 1.05rem; margin-bottom: 6px; color: var(--primary); }
.card p { font-size: 0.85rem; color: var(--text-muted); }

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

/* ===== Lesson Styles ===== */
.lesson-header {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  border-left: 4px solid var(--primary);
  box-shadow: var(--shadow);
}
.lesson-header h2 { color: var(--primary); font-size: 1.4rem; margin-bottom: 8px; }
.lesson-header .lesson-num { font-size: 0.85rem; color: var(--text-muted); }

.section-block {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.section-block h3 {
  color: var(--secondary);
  font-size: 1.15rem;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--border);
}
.section-block h4 {
  color: var(--primary);
  font-size: 1rem;
  margin: 16px 0 8px;
}

.example-box {
  background: rgba(196,30,58,0.05);
  border-radius: 8px;
  padding: 12px 16px;
  margin: 10px 0;
  border-left: 3px solid var(--primary);
}
[data-theme="dark"] .example-box {
  background: rgba(252,129,129,0.1);
}
.example-jp {
  font-size: 1.15rem;
  font-weight: 600;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.example-romaji {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}
.example-th {
  font-size: 0.95rem;
  margin-top: 4px;
}

.speak-btn {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.15s, background 0.2s;
}
.speak-btn:hover { transform: scale(1.1); background: var(--primary-dark); }
.speak-btn.playing { animation: pulse 0.8s infinite; }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

.vocab-table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 0.95rem;
}
.vocab-table th, .vocab-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.vocab-table th {
  background: rgba(196,30,58,0.08);
  color: var(--primary);
  font-weight: 600;
}
[data-theme="dark"] .vocab-table th {
  background: rgba(252,129,129,0.15);
}
.vocab-table tr:hover { background: rgba(0,0,0,0.02); }
[data-theme="dark"] .vocab-table tr:hover { background: rgba(255,255,255,0.03); }

.note {
  background: rgba(214,158,46,0.1);
  border-left: 3px solid var(--warning);
  padding: 10px 14px;
  border-radius: 0 8px 8px 0;
  margin: 12px 0;
  font-size: 0.9rem;
}

.grammar-point {
  margin: 16px 0;
  padding: 14px;
  background: rgba(26,54,93,0.05);
  border-radius: 8px;
}
[data-theme="dark"] .grammar-point {
  background: rgba(144,205,244,0.08);
}
.grammar-point .formula {
  font-family: monospace;
  background: var(--bg);
  padding: 8px 12px;
  border-radius: 6px;
  margin: 8px 0;
  font-size: 0.95rem;
  overflow-x: auto;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
  font-family: inherit;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.secondary {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}
.btn.secondary:hover { background: rgba(196,30,58,0.08); }
.btn.success { background: var(--success); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { width: 100%; margin-top: 12px; }

/* ===== Quiz ===== */
.quiz-select, .exam-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin-bottom: 20px;
  background: var(--bg-card);
  padding: 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.quiz-select label { font-weight: 600; }
.quiz-select select {
  flex: 1;
  min-width: 200px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
}

.quiz-area { margin-top: 16px; }
.hidden { display: none !important; }

.quiz-progress {
  background: var(--bg-card);
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  border: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  flex: 1;
  min-width: 100px;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.3s;
  border-radius: 3px;
}

.question-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.question-num {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.question-text {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 16px;
  line-height: 1.6;
}
.options { list-style: none; }
.option-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  margin-bottom: 8px;
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.option-item:hover { border-color: var(--primary); background: rgba(196,30,58,0.04); }
.option-item.selected { border-color: var(--primary); background: rgba(196,30,58,0.1); }
.option-item.correct { border-color: var(--success); background: rgba(56,161,105,0.12); }
.option-item.wrong { border-color: var(--danger); background: rgba(229,62,62,0.12); }
.option-item input { margin-top: 3px; accent-color: var(--primary); }

.quiz-result {
  text-align: center;
  padding: 32px 20px;
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.quiz-result .score {
  font-size: 3rem;
  font-weight: 700;
  color: var(--primary);
  margin: 12px 0;
}
.quiz-result .score-label { color: var(--text-muted); margin-bottom: 20px; }

/* ===== Vocab List ===== */
.vocab-list { display: flex; flex-direction: column; gap: 8px; }
.vocab-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.vocab-item .jp { font-size: 1.1rem; font-weight: 600; min-width: 120px; }
.vocab-item .romaji { font-size: 0.8rem; color: var(--text-muted); font-style: italic; }
.vocab-item .th { flex: 1; font-size: 0.95rem; }
.vocab-item .chapter-tag {
  font-size: 0.7rem;
  background: rgba(196,30,58,0.1);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

/* ===== Footer & FAB ===== */
.footer {
  text-align: center;
  padding: 20px;
  font-size: 0.8rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

.fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, transform 0.2s;
  z-index: 900;
}
.fab.show { opacity: 1; visibility: visible; }
.fab:hover { transform: scale(1.1); }

/* ===== Print Styles ===== */
@media print {
  .header, .sidebar, .overlay, .fab, .menu-btn, .icon-btn,
  .speak-btn, .quiz-select, .exam-controls, .footer, .nav-list {
    display: none !important;
  }
  body { padding-top: 0; background: #fff; color: #000; }
  .main { max-width: 100%; padding: 0; }
  .page { display: block !important; }
  .section-block, .lesson-header, .info-box, .example-box {
    box-shadow: none;
    border: 1px solid #ccc;
    break-inside: avoid;
  }
  .card-grid { display: none; }
}

/* ===== Page Desc ===== */
.page-desc {
  color: var(--text-muted);
  margin-bottom: 16px;
  font-size: 0.95rem;
}

/* ===== Responsive ===== */
@media (min-width: 768px) {
  .main { padding: 28px 24px 80px; }
  .hero h2 { font-size: 2rem; }
  .sidebar { width: 300px; left: -300px; }
}

/* ===== Important Notes ===== */
.note-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  box-shadow: var(--shadow);
}
.note-card h3 {
  color: var(--primary);
  font-size: 1.05rem;
  margin-bottom: 10px;
}
.note-card ul {
  padding-left: 20px;
  margin: 0;
}
.note-card li {
  margin-bottom: 8px;
  font-size: 0.95rem;
  line-height: 1.6;
}
.note-card .tag {
  display: inline-block;
  font-size: 0.7rem;
  background: rgba(196,30,58,0.12);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 6px;
  font-weight: 600;
}
.note-highlight {
  background: rgba(214,158,46,0.15);
  border-left: 3px solid var(--warning);
  padding: 8px 12px;
  border-radius: 0 6px 6px 0;
  margin: 8px 0;
  font-size: 0.9rem;
}

/* ===== Break Modal ===== */
.break-modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.break-modal.hidden { display: none !important; }
.break-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
}
.break-modal-content {
  position: relative;
  background: var(--bg-card);
  color: var(--text);
  border-radius: 16px;
  padding: 24px 20px;
  max-width: 420px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  animation: fadeIn 0.3s ease;
}
.break-dhikr {
  text-align: center;
  background: rgba(196,30,58,0.08);
  border-radius: 12px;
  padding: 16px 12px;
  margin-bottom: 16px;
}
[data-theme="dark"] .break-dhikr {
  background: rgba(252,129,129,0.12);
}
.dhikr-ar {
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.8;
  margin-bottom: 6px;
  color: var(--primary);
}
.dhikr-th {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 4px;
}
.dhikr-meaning {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.break-modal-content h3 {
  text-align: center;
  margin-bottom: 8px;
  color: var(--primary);
}
.break-msg {
  text-align: center;
  margin-bottom: 14px;
  font-size: 0.95rem;
}
.break-tips {
  padding-left: 18px;
  margin-bottom: 12px;
  font-size: 0.88rem;
  line-height: 1.55;
}
.break-tips li { margin-bottom: 8px; }
.break-ref {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.4;
}
.break-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.break-actions .btn { width: 100%; }
