:root {
  --primary: #58cc02;
  --primary-border: #46a302;
  --accent: #1cb0f6;
  --accent-border: #1899d6;
  --bg: #f7f9fa;
  --paper: #ffffff;
  --text: #2c3437;
  --text-sub: #77838f;
  --border: #e2e8f0;
  --card-shadow: rgba(0, 0, 0, 0.06);
  --word-dash: #a0aec0;
  --option-bg: #f8fafc;
}

body.dark-mode {
  --primary: #58cc02;
  --primary-border: #3b8001;
  --accent: #38bdf8;
  --accent-border: #0284c7;
  --bg: #0f172a;
  --paper: #1e293b;
  --text: #f1f5f9;
  --text-sub: #94a3b8;
  --border: #334155;
  --card-shadow: rgba(0, 0, 0, 0.35);
  --word-dash: #64748b;
  --option-bg: #27354a;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Nunito', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  background-color: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  min-height: 100vh;
  padding: 12px;
  user-select: none;
  transition: background-color 0.25s ease, color 0.2s ease;
}

/* Wadah Utama Aplikasi (Responsif Desktop & Mobile) */
.app-container {
  width: 100%;
  max-width: 540px; /* Lebar default untuk ponsel/tablet portrait */
  display: flex;
  flex-direction: column;
}

/* Optimasi Khusus Layar Komputer / Desktop */
@media (min-width: 768px) {
  body {
    padding: 24px;
    align-items: center;
  }
  .app-container {
    max-width: 660px; /* Lebih lebar dan proporsional di monitor desktop */
  }
  .story-card-item:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 0 var(--border);
  }
}

.view-screen {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ================= 1. BERANDA (RAK BUKU) ================= */
.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.header-title h1 {
  font-size: 1.45rem;
  font-weight: 800;
  color: var(--text);
}

.header-title p {
  font-size: 0.9rem;
  color: var(--text-sub);
  margin-top: 2px;
}

.stories-grid {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.story-card-item {
  background: var(--paper);
  border: 2px solid var(--border);
  border-radius: 20px;
  box-shadow: 0 5px 0 var(--border);
  padding: 14px;
  display: flex;
  gap: 14px;
  align-items: center;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.story-card-item:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--border);
}

.story-cover-thumb {
  width: 85px;
  height: 110px;
  border-radius: 14px;
  object-fit: cover;
  background-color: var(--border);
  flex-shrink: 0;
}

.story-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.story-badge {
  align-self: flex-start;
  background: rgba(28, 176, 246, 0.15);
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 8px;
  text-transform: uppercase;
}

.story-title {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--text);
}

.story-desc {
  font-size: 0.85rem;
  color: var(--text-sub);
  line-height: 1.35;
}

/* ================= 2. RUANG BACA (READER) ================= */
.top-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-tool {
  background: var(--paper);
  border: 2px solid var(--border);
  box-shadow: 0 3px 0 var(--border);
  border-radius: 14px;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  cursor: pointer;
  flex-shrink: 0;
}

.btn-tool:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--border);
}

.progress-track {
  flex: 1;
  height: 14px;
  background-color: var(--border);
  border-radius: 20px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 5%;
  background-color: var(--primary);
  border-radius: 20px;
  transition: width 0.35s ease;
}

.page-indicator {
  font-weight: 800;
  color: var(--text-sub);
  font-size: 0.9rem;
  min-width: 46px;
  text-align: right;
}

/* Area 3D Stage */
.book-stage {
  perspective: 1600px;
  width: 100%;
  min-height: 490px;
  display: flex;
  position: relative;
  touch-action: pan-y;
}

.page-sheet {
  width: 100%;
  background: var(--paper);
  border: 2px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 8px 0 var(--card-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  transform-origin: center center;
  position: relative;
}

/* ================= ANIMASI 3D BALIK HALAMAN SINKRON ================= */
/* Fase Keluar (0 deg -> 90 deg) */
.flip-out-left {
  animation: flipOutLeft 0.18s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}
.flip-out-right {
  animation: flipOutRight 0.18s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}

/* Fase Masuk (90 deg -> 0 deg) */
.flip-in-right {
  animation: flipInRight 0.20s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}
.flip-in-left {
  animation: flipInLeft 0.20s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes flipOutLeft {
  0% { transform: rotateY(0deg) scale(1); opacity: 1; }
  100% { transform: rotateY(-90deg) scale(0.96); opacity: 0.3; }
}
@keyframes flipInRight {
  0% { transform: rotateY(90deg) scale(0.96); opacity: 0.3; }
  100% { transform: rotateY(0deg) scale(1); opacity: 1; }
}
@keyframes flipOutRight {
  0% { transform: rotateY(0deg) scale(1); opacity: 1; }
  100% { transform: rotateY(90deg) scale(0.96); opacity: 0.3; }
}
@keyframes flipInLeft {
  0% { transform: rotateY(-90deg) scale(0.96); opacity: 0.3; }
  100% { transform: rotateY(0deg) scale(1); opacity: 1; }
}

.page-sheet::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0; left: 0;
  width: 14px;
  background: linear-gradient(to right, rgba(0,0,0,0.06), transparent);
  pointer-events: none;
  z-index: 5;
}

.image-frame {
  width: 100%;
  height: clamp(240px, 42vh, 340px); /* Skalabilitas tinggi otomatis untuk desktop dan ponsel */
  background-color: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.page-body {
  padding: 18px 20px;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  flex: 1;
}

.btn-speaker {
  background: var(--accent);
  border: none;
  border-radius: 16px;
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--accent-border);
  flex-shrink: 0;
  transition: transform 0.1s;
}

.btn-speaker:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-border); }
.btn-speaker svg { width: 26px; height: 26px; fill: #ffffff; }

.btn-speaker.playing {
  background: #ff9600;
  box-shadow: 0 4px 0 #cc7800;
  animation: pulseBtn 0.9s infinite alternate;
}

@keyframes pulseBtn {
  from { transform: scale(1); }
  to { transform: scale(1.05); }
}

.text-flow {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.6;
}

.clickable-word {
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 8px;
  border-bottom: 2px dashed var(--word-dash);
  transition: all 0.12s ease;
}

.clickable-word:hover {
  background-color: rgba(28, 176, 246, 0.15);
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.clickable-word:active, .clickable-word.speaking {
  background-color: rgba(28, 176, 246, 0.3);
  color: var(--accent);
  border-bottom-style: solid;
  transform: scale(0.94);
}

.action-controls {
  display: flex;
  gap: 12px;
  padding: 4px 0;
}

.btn-action {
  flex: 1;
  padding: 16px;
  font-size: 1rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  border-radius: 18px;
  border: none;
  cursor: pointer;
}

.btn-action-prev {
  background-color: var(--paper);
  color: var(--text-sub);
  border: 2px solid var(--border);
  box-shadow: 0 4px 0 var(--border);
}

.btn-action-next {
  background-color: var(--primary);
  color: #ffffff;
  box-shadow: 0 4px 0 var(--primary-border);
}

.btn-action:active:not(:disabled) { transform: translateY(3px); }
.btn-action-prev:active:not(:disabled) { box-shadow: 0 1px 0 var(--border); }
.btn-action-next:active:not(:disabled) { box-shadow: 0 1px 0 var(--primary-border); }
.btn-action:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; transform: none; }

/* ================= 3. KUIS PEMAHAMAN ================= */
.quiz-view {
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--paper);
  border: 2px solid var(--border);
  border-radius: 24px;
  padding: 20px;
  box-shadow: 0 8px 0 var(--card-shadow);
}

.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid var(--border);
  padding-bottom: 12px;
}

.quiz-header h2 { font-size: 1.25rem; font-weight: 800; color: var(--primary); }
.quiz-step { font-weight: 800; color: var(--text-sub); font-size: 0.9rem; }
.quiz-question { font-size: 1.15rem; font-weight: 700; line-height: 1.5; margin: 14px 0; }

.quiz-options { display: flex; flex-direction: column; gap: 10px; }

.quiz-opt-btn {
  background: var(--option-bg);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 14px 16px;
  text-align: left;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 3px 0 var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.quiz-opt-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--border); }
.quiz-opt-btn.correct { background: #dcfce7 !important; border-color: #22c55e !important; color: #15803d !important; }
.quiz-opt-btn.wrong { background: #fee2e2 !important; border-color: #ef4444 !important; color: #b91c1c !important; }

.quiz-result {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 14px;
  padding: 10px 0;
}

.stars-box { font-size: 3rem; letter-spacing: 4px; }
.result-score { font-size: 2.2rem; font-weight: 800; color: var(--primary); }
.result-msg { font-size: 1.05rem; font-weight: 700; color: var(--text-sub); line-height: 1.5; }
.quiz-result-actions { display: flex; gap: 10px; width: 100%; margin-top: 6px; }

/* ================= OVERLAY KONFETI ================= */
#confettiCanvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 99999;
  display: none;
}

/* ================= LENCANA RIWAYAT KUIS DI BERANDA ================= */
.story-history-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fef08a;
  color: #854d0e;
  border: 1.5px solid #facc15;
  border-radius: 10px;
  padding: 3px 8px;
  font-size: 0.8rem;
  font-weight: 800;
  margin-top: 4px;
  width: fit-content;
}

body.dark-mode .story-history-badge {
  background: #362c0d;
  color: #fde047;
  border-color: #854d0e;
}

.story-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
