:root {
  --ink: #2b2350;
  --paper: #fffaf0;
  --main: #7c5cff;
  --soft: #e9e4ff;
  --gold: #ffd24a;
  --good: #35c46a;
  --bad: #ff5d5d;
  --chapter: #7c5cff;
  --round: ui-rounded, "SF Pro Rounded", "Nunito", "Arial Rounded MT Bold", system-ui, sans-serif;
}
/* touch-action is not inherited, so set it on everything: no double-tap zoom anywhere. */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #efe9ff; }
body {
  font-family: var(--round); color: var(--ink);
  -webkit-user-select: none; user-select: none; touch-action: manipulation;
}
button { font-family: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app {
  height: 100dvh; display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background:
    radial-gradient(circle at 12% 8%, #fff6 0 90px, transparent 91px),
    radial-gradient(circle at 88% 92%, #fff5 0 130px, transparent 131px),
    linear-gradient(160deg, #f3edff, #e3f4ff 60%, #fff1e0);
}

/* Top bar */
#top { display: flex; align-items: center; gap: 12px; padding: 8px 14px; flex: none; }
.icon-btn {
  width: 48px; height: 48px; border-radius: 16px; border: 3px solid var(--ink); background: #fff;
  font-size: 24px; line-height: 1; box-shadow: 0 4px 0 var(--ink); flex: none;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
#crumb { font-weight: 800; font-size: 18px; white-space: nowrap; }
#chapDots { flex: 1; display: flex; align-items: center; justify-content: center; gap: clamp(4px, 1.2vw, 14px); min-width: 0; }
.chap-dot {
  width: 38px; height: 38px; border-radius: 50%; border: 3px solid var(--ink); background: #fff; font-size: 17px; font-weight: 800;
  box-shadow: 0 3px 0 var(--ink); flex: none; padding: 0; position: relative;
}
.chap-dot.done { background: var(--c); color: #fff; text-shadow: 0 1px 0 #0005; }
.chap-dot.now { transform: scale(1.18); box-shadow: 0 3px 0 var(--ink), 0 0 0 4px var(--gold); }
.chap-dot:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
#chapPop {
  position: fixed; z-index: 30; width: min(300px, calc(100vw - 20px)); padding: 12px 14px 14px; background: #fff; border: 4px solid var(--ink);
  border-radius: 20px; box-shadow: 0 6px 0 var(--ink); border-top: 12px solid var(--c);
}
#chapPopName { font-size: 20px; font-weight: 800; line-height: 1.2; }
#chapPopState { font-size: 17px; font-weight: 700; margin: 4px 0 8px; opacity: .75; }
#chapPopState.done { color: #17803f; opacity: 1; }
#foot { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-top: 22px; }
#foot .small-btn { align-self: center; font-size: 14px; }
#version { text-align: center; font-size: 13px; font-weight: 600; opacity: .5; }
#studioLink { position: fixed; left: 8px; bottom: 8px; z-index: 60; font: 12px/1.3 system-ui, sans-serif; color: #5b3cc4; background: #fffc; border: 1px solid #5b3cc455; border-radius: 8px; padding: 3px 8px; text-decoration: none; max-width: 60vw; }
body.reviewing #top { background: repeating-linear-gradient(135deg, #5b3cc422 0 12px, transparent 12px 24px); }

.view { flex: 1; min-height: 0; }

/* Home */
#home { overflow-y: auto; padding: 6px 18px 28px; -webkit-overflow-scrolling: touch; }
#hero { display: flex; align-items: center; gap: 10px; max-width: 980px; margin: 0 auto; }
#heroPipa { width: 240px; height: 240px; flex: none; }
#heroTitle { font-size: clamp(34px, 6vw, 64px); margin: 0; color: var(--main); letter-spacing: -1px; line-height: 1; }
#heroSub { font-size: clamp(18px, 2.4vw, 24px); font-weight: 600; margin: 10px 0 16px; }
.big-btn {
  font-size: 24px; font-weight: 800; padding: 14px 28px; border-radius: 22px; border: 4px solid var(--ink);
  background: var(--gold); box-shadow: 0 6px 0 var(--ink);
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
#chapters {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px;
  max-width: 980px; margin: 18px auto 0;
}
.chapter {
  position: relative; text-align: left; padding: 14px 14px 12px; border-radius: 24px; border: 4px solid var(--ink);
  background: #fff; box-shadow: 0 6px 0 var(--ink); overflow: hidden;
}
.chapter:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.chapter::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 10px; background: var(--c); }
.chapter .num { font-size: 14px; font-weight: 800; opacity: .6; margin-top: 6px; }
.chapter .emo { font-size: 44px; line-height: 1.1; }
.chapter .name { font-size: 20px; font-weight: 800; line-height: 1.15; }
.chapter .state { font-size: 16px; font-weight: 800; margin-top: 6px; min-height: 22px; color: #17803f; }
.chapter.done { background: #f3fff6; }

/* Book */
#book { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(280px, 1fr); gap: 14px; padding: 4px 14px 6px; }
#stageWrap { display: flex; flex-direction: column; min-height: 0; min-width: 0; gap: 8px; }
#stage {
  flex: 1; min-height: 0; background: var(--paper); border: 4px solid var(--ink); border-radius: 28px;
  box-shadow: 0 6px 0 var(--ink); overflow: hidden; position: relative;
}
#stage::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 10px; background: var(--chapter); z-index: 1; }
.scene-svg { width: 100%; height: 100%; display: block; font-family: var(--round); }
.scene-svg .tappable { cursor: pointer; }
.scene-svg text { pointer-events: none; }
#controls { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; min-height: 0; flex: none; }
.scene-btn, .small-btn {
  font-size: 20px; font-weight: 800; padding: 10px 20px; border-radius: 18px; border: 3px solid var(--ink);
  background: #fff; box-shadow: 0 4px 0 var(--ink);
}
.scene-btn { background: var(--gold); animation: nudge 1.6s ease-in-out infinite; }
.scene-btn:active, .small-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.scene-btn:disabled { opacity: .45; animation: none; }
@keyframes nudge { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }

#side { display: flex; flex-direction: column; min-height: 0; overflow-y: auto; padding-right: 2px; -webkit-overflow-scrolling: touch; }
#pipaRow { position: relative; display: flex; align-items: flex-end; min-height: 150px; flex: none; }
#pipa { width: 170px; height: 170px; flex: none; }
#bubble {
  position: relative; margin: 0 0 46px 2px; padding: 10px 14px; background: #fff; border: 3px solid var(--ink);
  border-radius: 20px; font-size: 19px; font-weight: 700; line-height: 1.2; animation: bubble .35s cubic-bezier(.34,1.56,.64,1);
}
#bubble::before {
  content: ""; position: absolute; left: -13px; bottom: 12px; width: 20px; height: 20px; background: #fff;
  border-left: 3px solid var(--ink); border-bottom: 3px solid var(--ink); transform: rotate(45deg);
}
@keyframes bubble { from { transform: scale(.3); opacity: 0; } }
#pageTitle { font-size: clamp(26px, 3.2vw, 38px); margin: 2px 0 8px; color: var(--chapter); line-height: 1.05; }
#pageText p {
  font-size: clamp(21px, 2.5vw, 29px); font-weight: 650; line-height: 1.25; margin: 0 0 10px; border-radius: 12px; padding: 2px 6px;
  margin-left: -6px; animation: lineIn .45s both;
}
#pageText p:nth-child(2) { animation-delay: .25s; }
#pageText p:nth-child(3) { animation-delay: .5s; }
#pageText p.speaking, #pageTitle.speaking { background: #fff8d6; }
.w { border-radius: 7px; padding: 0 2px; margin: 0 -2px; transition: background .08s; }
.w.on { background: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
.w.code { font-family: ui-monospace, Menlo, monospace; font-size: .9em; background: var(--soft); }
.w.code.on { background: var(--gold); }
#pageTitle { border-radius: 12px; }
@keyframes lineIn { from { opacity: 0; transform: translateY(10px); } }
.small-btn { align-self: flex-start; font-size: 16px; padding: 7px 14px; margin-top: 4px; background: var(--soft); flex: none; }
/* More info opens as a sheet over the page so it can never be cut off. */
#moreSheet { position: fixed; inset: 0; z-index: 40; background: #2b235066; display: grid; place-items: center; padding: 20px; }
#moreCard {
  position: relative; width: min(720px, 100%); max-height: min(86dvh, 100%); overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: #fff; border: 4px solid var(--ink); border-radius: 26px; box-shadow: 0 8px 0 var(--ink); padding: 18px 22px 24px;
  border-top: 14px solid var(--chapter); animation: bubble .3s cubic-bezier(.34,1.56,.64,1);
}
#moreCard h2 { margin: 0 56px 10px 0; font-size: 26px; color: var(--chapter); }
#moreClose { position: absolute; top: 12px; right: 14px; }
#moreText { font-size: clamp(20px, 2.3vw, 25px); line-height: 1.5; font-weight: 550; }

/* Quiz */
#choices { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.choice {
  text-align: left; font-size: clamp(19px, 2.2vw, 24px); font-weight: 750; padding: 13px 16px; border-radius: 18px;
  border: 3px solid var(--ink); background: #fff; box-shadow: 0 4px 0 var(--ink); line-height: 1.2;
}
.choice:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.choice.right { background: #c9f7d8; border-color: #17803f; }
.choice.wrong { background: #ffd6d6; opacity: .7; animation: shake .4s; }
.choice:disabled { box-shadow: 0 2px 0 var(--ink); }
@keyframes shake { 20% { transform: translateX(-8px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(5px); } }
#why { cursor: pointer; margin-top: 12px; padding: 12px 14px; background: #f3fff6; border: 3px solid #17803f; border-radius: 16px; font-size: 19px; font-weight: 650; line-height: 1.3; }

/* Bottom nav */
#nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 16px 10px; flex: none; }
.nav-btn {
  width: 84px; height: 60px; border-radius: 22px; border: 4px solid var(--ink); background: #fff; font-size: 26px;
  box-shadow: 0 5px 0 var(--ink); flex: none;
}
.nav-btn.next { background: var(--gold); width: 120px; }
.nav-btn.next.ready { animation: nudge 1.2s ease-in-out infinite; }
.nav-btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.nav-btn:disabled { opacity: .35; animation: none; }
#dots { display: flex; gap: 7px; flex-wrap: wrap; justify-content: center; }
.dot { width: 14px; height: 14px; border-radius: 50%; border: 2.5px solid var(--ink); background: #fff; }
.dot.q { border-radius: 4px; }
.dot.seen { background: var(--chapter); }
.dot.now { transform: scale(1.45); background: var(--gold); }

/* Pipa fallback (when the 3D model cannot load) */
.pipa-fallback { width: 100%; height: 100%; display: grid; place-items: center; font-size: 110px; animation: bobble 2s ease-in-out infinite; }
.pipa-fallback.act { animation: hop .6s; }
@keyframes bobble { 50% { transform: translateY(-8px) rotate(-3deg); } }
@keyframes hop { 40% { transform: translateY(-40px) scale(1.1) rotate(8deg); } }
#pipa canvas, #heroPipa canvas { width: 100% !important; height: 100% !important; display: block; touch-action: none; }

/* Phones and portrait tablets: stack */
@media (max-width: 820px), (orientation: portrait) {
  #book { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); gap: 8px; padding: 2px 10px 4px; }
  #stage { flex: none; aspect-ratio: 8 / 5; border-radius: 22px; }
  #pipaRow { min-height: 104px; }
  #pipa { width: 112px; height: 112px; }
  #bubble { font-size: 16px; margin-bottom: 30px; }
  #hero { flex-direction: column; text-align: center; }
  #heroPipa { width: 190px; height: 190px; }
  #crumb { display: none; }
  #top { gap: 6px; padding: 6px 8px; }
  #top .icon-btn { width: 40px; height: 40px; font-size: 20px; border-radius: 13px; }
  #chapDots { gap: 3px; }
  .chap-dot { width: clamp(22px, 7vw, 34px); height: clamp(22px, 7vw, 34px); font-size: 13px; border-width: 2px; box-shadow: 0 2px 0 var(--ink); }
  .nav-btn { height: 52px; }
  .scene-btn { font-size: 17px; padding: 8px 14px; }
}
