/* ===== ROOT VARIABLES ===== */
:root {
  --primary: #F2C94C;
  --primary-dark: #E0B429;
  --accent: #2563EB;
  --accent-light: #3B82F6;
  --bg: #0F172A;
  --bg-card: #1E293B;
  --bg-sidebar: #1A2332;
  --bg-input: #0F172A;
  --border: rgba(255,255,255,0.08);
  --text: #F1F5F9;
  --text-muted: #94A3B8;
  --text-faint: #64748B;
  --success: #10B981;
  --danger: #EF4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}
a { color: inherit; text-decoration: none; }
button { cursor: pointer; border: none; background: none; font-family: inherit; }
input, textarea, select {
  font-family: inherit;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--bg-input);
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ===== BUTTONS ===== */
.btn-primary {
  display: inline-block;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.95rem;
  background: var(--primary);
  color: #0F172A;
  transition: var(--transition);
  cursor: pointer;
  border: none;
  text-align: center;
}
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); }
.btn-done-active { background: #22c55e !important; color: #fff !important; }
.btn-done-active:hover { background: #16a34a !important; }

.btn-download {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  gap: 12px;
  padding: 12px 20px;
  border-radius: 12px;
  background: linear-gradient(135deg, #2563EB 0%, #7C3AED 100%);
  color: #fff;
  text-decoration: none;
  border: 2px solid rgba(255,255,255,0.2);
  cursor: pointer;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 18px rgba(37, 99, 235, 0.45);
  transition: box-shadow 0.2s ease, transform 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn-download::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, #1d4ed8 0%, #6d28d9 100%);
  opacity: 0;
  transition: opacity 0.2s ease;
}
.btn-download:hover {
  color: #fff;
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.6);
}
.btn-download:hover::before { opacity: 1; }
.btn-download:active { transform: translateY(0); }
.btn-download .btn-download-icon {
  font-size: 1.8rem;
  position: relative;
  z-index: 1;
  animation: bounce-down 1.6s ease infinite;
  line-height: 1;
}
.btn-download .btn-download-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  position: relative;
  z-index: 1;
}
.btn-download .btn-download-label {
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.2;
}
.btn-download .btn-download-sub {
  font-size: 0.75rem;
  font-weight: 400;
  opacity: 0.8;
  line-height: 1;
}
@keyframes bounce-down {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(4px); }
}

.chapter-actions-left {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

.btn-secondary {
  display: inline-block;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-weight: 600;
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  transition: var(--transition);
  cursor: pointer;
}
.btn-secondary:hover { background: #2D3F55; }

.btn-outline {
  display: inline-block;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  transition: var(--transition);
}
.btn-outline:hover { color: var(--text); border-color: var(--primary); }

.btn-sm {
  display: inline-block;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  background: var(--bg-card);
  color: var(--text-muted);
  border: 1px solid var(--border);
  transition: var(--transition);
}
.btn-sm:hover { color: var(--text); border-color: var(--primary); }

.btn-full { width: 100%; }

/* ===== NAVBAR ===== */
.navbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 40px;
  height: 60px;
  background: rgba(15,23,42,0.95);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 100;
}
.nav-brand a {
  font-weight: 700; font-size: 1.1rem; color: var(--primary);
}
.nav-links { display: flex; align-items: center; gap: 12px; }
.nav-links a {
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  color: var(--text-muted);
  transition: var(--transition);
}
.nav-links a:hover { color: var(--text); background: var(--border); }
.nav-user { font-size: 0.8rem !important; }

/* ===== MESSAGES / ALERTS ===== */
.messages { padding: 0 40px; margin: 12px 0; }
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin-bottom: 8px;
}
.alert-success { background: rgba(16,185,129,0.15); border: 1px solid rgba(16,185,129,0.3); color: var(--success); }
.alert-error, .alert-danger { background: rgba(239,68,68,0.15); border: 1px solid rgba(239,68,68,0.3); color: var(--danger); }
.alert-info { background: rgba(37,99,235,0.15); border: 1px solid rgba(37,99,235,0.3); color: var(--accent-light); }

/* ===== MAIN CONTENT ===== */
.main-content { min-height: calc(100vh - 120px); }

/* ===== FOOTER ===== */
.footer {
  text-align: center; padding: 24px;
  border-top: 1px solid var(--border);
  color: var(--text-faint); font-size: 0.85rem;
}
.footer a { color: var(--primary); }

/* ===== HERO ===== */
.hero {
  text-align: center;
  padding: 80px 40px 60px;
  max-width: 800px;
  margin: 0 auto;
}
.hero h1 { font-size: clamp(1.8rem, 4vw, 3rem); font-weight: 800; margin-bottom: 8px; }
.hero-sub { color: var(--text-muted); font-size: 1.1rem; margin-bottom: 16px; }
.hero-desc { color: var(--text-faint); max-width: 600px; margin: 0 auto 32px; }
.hero-actions { display: flex; gap: 16px; justify-content: center; flex-wrap: wrap; }

/* ===== CHAPTERS GRID ===== */
.chapters-grid { padding: 40px; }
.chapters-grid h2 { font-size: 1.5rem; font-weight: 700; margin-bottom: 24px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 20px;
}
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  transition: var(--transition);
}
.card:hover { border-color: var(--primary); transform: translateY(-3px); }
.card-emoji { font-size: 2.5rem; margin-bottom: 12px; }
.card h3 { font-size: 0.95rem; font-weight: 700; margin-bottom: 8px; }
.card p { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 16px; }

/* ===== READER LAYOUT ===== */
.reader-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - 60px);
  overflow: hidden;
}
.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 20px;
}
.sidebar h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 12px; }
.chapter-list { list-style: none; display: flex; flex-direction: column; gap: 4px; }
.chapter-list li a {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.83rem;
  color: var(--text-muted);
  transition: var(--transition);
}
.chapter-list li a:hover, .chapter-list li.active a { color: var(--text); background: var(--border); }
.chapter-list li.done a { color: var(--success); }
.check { margin-left: auto; font-size: 0.8rem; }
.progress-wrap { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 0.82rem; color: var(--text-muted); }
.progress-bar { height: 6px; background: var(--border); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 3px; transition: width 0.4s ease; }
.reader-main { overflow-y: auto; padding: 40px; }

/* ===== CHAPTER CONTENT ===== */
.chapter-content { max-width: 860px; margin: 0 auto; overflow-y: auto; padding: 40px; }
.chapter-header { margin-bottom: 32px; padding-bottom: 24px; border-bottom: 1px solid var(--border); }
.chapter-header-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.chapter-header-titles { display: flex; flex-direction: column; }
.chapter-emoji { font-size: 3rem; display: block; margin-bottom: 12px; }
.chapter-header h1 { font-size: 1.8rem; font-weight: 800; margin-bottom: 8px; }
.chapter-summary {
  color: var(--text-muted);
  font-size: 1.05rem;
  font-style: italic;
  background: rgba(242,201,76,0.08);
  border: 1px solid rgba(242,201,76,0.12);
  border-radius: var(--radius-sm);
  padding: 14px 18px;
  margin-top: 12px;
}
.chapter-section { margin-bottom: 32px; }
.chapter-section h2 { font-size: 1.2rem; font-weight: 700; margin-bottom: 16px; color: var(--primary); }
.section-content {
  background: linear-gradient(135deg, var(--bg-card) 0%, rgba(37,99,235,0.06) 100%);
  border: 1px solid rgba(242,201,76,0.18);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 28px 28px 28px 24px;
  color: var(--text);
  line-height: 1.8;
  font-size: 1.1rem;
  box-shadow: 0 2px 16px rgba(0,0,0,0.25), inset 0 1px 0 rgba(255,255,255,0.04);
  transition: var(--transition);
}
.section-content:hover {
  border-left-color: var(--accent-light);
  box-shadow: 0 4px 24px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.06);
  transform: translateY(-1px);
}
.section-content a {
  color: var(--accent-light);
  text-decoration: underline;
  text-underline-offset: 3px;
  transition: color var(--transition);
}
.section-content a:hover {
  color: var(--primary);
}
.block-heading {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
  margin: 28px 0 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--primary);
}
/* ===== CHAPTER & SECTION IMAGES ===== */
.chapter-image { margin-top: 20px; }
.chapter-image img,
.puzzle-img img {
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  object-fit: contain;
}
.puzzle-img { margin-top: 16px; }
.puzzle-img-25 { max-width: 25%; }
.puzzle-img-50 { max-width: 50%; }
.puzzle-img-60 { max-width: 60%; }
.puzzle-img-75 { max-width: 75%; }
.puzzle-img-xl { width: calc(100% + 80px); margin-left: -40px; max-width: none; }

.chapter-actions { margin-top: 40px; padding-top: 24px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 16px; }
.chapter-nav { display: flex; gap: 16px; margin-top: 16px; flex-wrap: wrap; }

/* ===== CHAT PAGE ===== */
.chat-layout { max-width: 860px; margin: 0 auto; padding: 32px; display: flex; flex-direction: column; height: calc(100vh - 60px); }
.chat-header { margin-bottom: 16px; }
.chat-header h1 { font-size: 1.5rem; font-weight: 700; }
.chat-header p { color: var(--text-muted); font-size: 0.9rem; }
.chat-messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 16px 0; }
.message { display: flex; }
.message.user { justify-content: flex-end; }
.bubble {
  max-width: 75%;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.6;
  white-space: pre-wrap;
}
.message.user .bubble { background: var(--primary); color: #0F172A; font-weight: 500; }
.message.assistant .bubble { background: var(--bg-card); border: 1px solid var(--border); }
.engine-badge { display: block; font-size: 0.7rem; color: var(--text-faint); margin-top: 6px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.suggestion-btn {
  padding: 6px 12px; border-radius: 20px;
  font-size: 0.78rem; cursor: pointer;
  background: rgba(37,99,235,0.15); border: 1px solid rgba(37,99,235,0.3);
  color: var(--accent-light); transition: var(--transition);
}
.suggestion-btn:hover { background: rgba(37,99,235,0.3); }
.chat-form { display: flex; gap: 10px; padding-top: 12px; border-top: 1px solid var(--border); }
#chat-input {
  flex: 1; padding: 12px 16px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--bg-card); color: var(--text); outline: none; transition: var(--transition);
}
#chat-input:focus { border-color: var(--primary); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== AUTH PAGE ===== */
.auth-page { display: flex; align-items: center; justify-content: center; min-height: calc(100vh - 120px); padding: 24px; }
.auth-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 40px; width: 100%; max-width: 420px;
}
.auth-card h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 24px; text-align: center; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 6px; }
.form-group input, .form-control {
  width: 100%; padding: 10px 14px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg-input); color: var(--text); outline: none;
  transition: var(--transition);
}
.form-group input:focus, .form-control:focus { border-color: var(--primary); }
.form-error { font-size: 0.8rem; color: var(--danger); margin-top: 4px; }
.auth-link { text-align: center; margin-top: 20px; font-size: 0.85rem; color: var(--text-muted); }
.auth-link a { color: var(--primary); }

/* ===== DASHBOARD ===== */
.dashboard { max-width: 1000px; margin: 0 auto; padding: 40px; }
.dashboard h1 { font-size: 1.8rem; font-weight: 700; margin-bottom: 32px; }
.dashboard-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px; }
.dash-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; }
.dash-card h3 { font-size: 1rem; font-weight: 700; margin-bottom: 16px; }
.dash-card p { font-size: 0.88rem; color: var(--text-muted); margin-bottom: 8px; }
.badge-green { background: rgba(16,185,129,0.15); border: 1px solid rgba(16,185,129,0.3); color: var(--success); padding: 2px 8px; border-radius: 4px; font-size: 0.8rem; }
.badge-red { background: rgba(239,68,68,0.15); border: 1px solid rgba(239,68,68,0.3); color: var(--danger); padding: 2px 8px; border-radius: 4px; font-size: 0.8rem; }
.progress-pct { font-size: 0.78rem; color: var(--text-faint); margin-top: 4px; }

/* ===== SEARCH PAGE ===== */
.search-page { max-width: 800px; margin: 0 auto; padding: 40px; }
.search-page h1 { font-size: 1.5rem; margin-bottom: 24px; }
.search-form { display: flex; gap: 10px; margin-bottom: 24px; }
.search-input { flex: 1; padding: 10px 16px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-card); color: var(--text); outline: none; }
.search-input:focus { border-color: var(--primary); }
.search-meta { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 16px; }
.search-results { list-style: none; display: flex; flex-direction: column; gap: 12px; }
.search-results li { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; transition: var(--transition); }
.search-results li:hover { border-color: var(--primary); }
.excerpt { font-size: 0.82rem; color: var(--text-faint); margin-top: 6px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .navbar { padding: 0 16px; }
  .reader-layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .messages { padding: 0 16px; }
  .chat-layout { padding: 16px; }
  .dashboard { padding: 20px 16px; }
  .chapters-grid { padding: 20px 16px; }
  .hero { padding: 40px 20px; }
}
