:root {
  --sky: #bcdcf5;
  --sky-deep: #9cc9ee;
  --sea: #6f9fd2;
  --navy: #2c4a6e;
  --navy-soft: #4c6a8e;
  --orange: #d38b3c;
  --orange-deep: #b9732a;
  --white: #ffffff;
  --ice: #f3f8fd;
  --radius: 22px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", system-ui, -apple-system, "Segoe UI", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--navy);
  background: linear-gradient(180deg, var(--sky) 0%, var(--sky-deep) 100%);
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
#app { min-height: 100%; min-height: 100dvh; display: flex; flex-direction: column; }
.loading { margin: auto; font-size: 1.4rem; }

button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: 1rem; }

/* ---------- Teacher screens ---------- */
.screen {
  flex: 1; display: flex; flex-direction: column;
  padding: calc(16px + var(--safe-top)) 18px calc(20px + var(--safe-bottom));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.topbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.topbar h1 { font-size: 1.35rem; margin: 0; flex: 1; }
.back-btn {
  border: none; background: rgba(255,255,255,.7); color: var(--navy);
  border-radius: 999px; padding: 8px 14px; font-size: 1rem; font-weight: 600;
}
.hero { border-radius: var(--radius); width: 100%; display: block; box-shadow: 0 8px 24px rgba(44,74,110,.18); }
.student-chip {
  margin: 16px 0 6px; padding: 12px 16px; background: var(--white); border-radius: 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 1.05rem;
}
.student-chip strong { font-size: 1.2rem; }
.menu { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.menu button {
  border: none; border-radius: 18px; padding: 18px 14px; text-align: left;
  background: var(--white); color: var(--navy); box-shadow: 0 3px 0 rgba(44,74,110,.15);
  font-size: 1.1rem; font-weight: 700; min-height: 92px;
}
.menu button span { display: block; font-size: .85rem; font-weight: 500; color: var(--navy-soft); margin-top: 4px; }
.menu button.primary { background: var(--orange); color: var(--white); grid-column: 1 / -1; }
.menu button.primary span { color: #fff3e3; }

.panel { background: var(--white); border-radius: 18px; padding: 16px; margin-bottom: 14px; }
.panel h2 { font-size: 1.05rem; margin: 0 0 10px; }
.panel p.hint { font-size: .88rem; color: var(--navy-soft); margin: 6px 0 0; line-height: 1.35; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row + .row { margin-top: 8px; }
.field {
  border: 2px solid #d6e6f5; border-radius: 12px; padding: 10px 12px; color: var(--navy);
  background: var(--ice); flex: 1; min-width: 0;
}
.btn {
  border: none; border-radius: 12px; padding: 10px 16px; font-size: 1rem; font-weight: 700;
  background: var(--navy); color: var(--white);
}
.btn.orange { background: var(--orange); }
.btn.light { background: #e6f0fa; color: var(--navy); }
.btn.small { padding: 7px 12px; font-size: .9rem; }
.btn.danger { background: #f6dede; color: #9b3434; }
.btn:disabled { opacity: .45; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 2px solid #cfe2f4; background: var(--ice); color: var(--navy);
  border-radius: 999px; padding: 8px 14px; font-weight: 600; font-size: .95rem;
}
.chip.on { background: var(--navy); border-color: var(--navy); color: var(--white); }

.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid #e8f0f8;
}
.list li:last-child { border-bottom: none; }
.list .grow { flex: 1; min-width: 0; }
.list .muted { color: var(--navy-soft); font-size: .85rem; }
.list li.hidden-card .grow { opacity: .45; text-decoration: line-through; }
.cat-tag {
  font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em;
  background: #e6f0fa; color: var(--navy-soft); padding: 3px 8px; border-radius: 999px; white-space: nowrap;
}

.slot {
  display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: 8px; align-items: start;
  padding: 10px 0; border-bottom: 1px solid #e8f0f8;
}
.slot:last-child { border-bottom: none; }
.slot .num {
  width: 28px; height: 28px; border-radius: 50%; background: var(--navy); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: .9rem; margin-top: 6px;
}
.slot .num.empty { background: #d6e6f5; color: var(--navy-soft); }
.slot .opts { min-width: 0; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.slot select, .slot input[type=number] { border: 2px solid #d6e6f5; border-radius: 10px; padding: 8px; background: var(--ice); color: var(--navy); }
.slot input[type=number] { width: 70px; }
.slot select[data-card] { width: 100%; max-width: 100%; }
.slot label.check { font-size: .9rem; display: flex; gap: 6px; align-items: center; }

/* ---------- Student stage ---------- */
.stage {
  position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: calc(56px + var(--safe-top)) 16px calc(110px + var(--safe-bottom));
  min-height: 100dvh; overflow: hidden;
}
.exit-btn {
  position: absolute; top: calc(10px + var(--safe-top)); left: 12px; z-index: 20;
  border: none; background: rgba(255,255,255,.35); color: var(--navy-soft);
  width: 40px; height: 40px; border-radius: 50%; font-size: 1.1rem; opacity: .7;
}
.progress-dots { position: absolute; top: calc(22px + var(--safe-top)); left: 0; right: 0; display: flex; justify-content: center; gap: 8px; pointer-events: none; }
.progress-dots i { width: 10px; height: 10px; border-radius: 50%; background: rgba(255,255,255,.6); }
.progress-dots i.done { background: var(--navy); }
.progress-dots i.now { background: var(--orange); transform: scale(1.3); }

/* Card + flip */
.card-wrap {
  width: min(94vw, 760px, calc((100dvh - 200px) * 1.72));
  aspect-ratio: 2127 / 1236; perspective: 1600px;
  animation: deal .55s cubic-bezier(.2,1.2,.4,1) both;
}
.card {
  position: relative; width: 100%; height: 100%; transform-style: preserve-3d;
  transition: transform .8s cubic-bezier(.3,1.4,.5,1);
}
.card.flipped { transform: rotateY(180deg); }
.card-face {
  position: absolute; inset: 0; border-radius: var(--radius); overflow: hidden;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  box-shadow: 0 14px 34px rgba(44,74,110,.28);
}
.card-face.art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-face.art::after {
  content: "Tap to flip!"; position: absolute; bottom: 10px; right: 14px;
  background: rgba(255,255,255,.85); color: var(--navy); font-weight: 800; font-size: .9rem;
  padding: 5px 12px; border-radius: 999px; animation: nudge 1.6s ease-in-out infinite;
}
.card-face.text {
  transform: rotateY(180deg);
  background: radial-gradient(circle at 20% 15%, #ffffff 0%, var(--ice) 55%, #e2effa 100%);
  border: 6px solid var(--white);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 6% 7%; text-align: center;
}
.card-face.text .cat-tag { position: absolute; top: 14px; left: 16px; font-size: clamp(.65rem, 1.6vw, .85rem); }
.card-face.text .prompt {
  font-size: clamp(1.25rem, 4.6vw, 2.6rem); line-height: 1.25; font-weight: 800; color: var(--navy);
  text-wrap: balance;
}
.card-face.text .prompt.long { font-size: clamp(1.05rem, 3.6vw, 2.05rem); }
.card-face.text .touch-hint {
  position: absolute; bottom: 12px; font-size: .85rem; font-weight: 700; color: var(--orange);
  animation: nudge 1.6s ease-in-out infinite;
}

/* Bottom action bar */
.actionbar {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 10;
  padding: 16px 16px calc(22px + var(--safe-bottom));
  display: flex; justify-content: center;
  transform: translateY(140%); transition: transform .45s cubic-bezier(.2,1.3,.4,1);
}
.actionbar.show { transform: translateY(0); }
.next-btn {
  border: none; border-radius: 999px; background: var(--orange); color: #fff;
  font-size: 1.45rem; font-weight: 800; padding: 16px 52px;
  box-shadow: 0 6px 0 var(--orange-deep), 0 12px 24px rgba(0,0,0,.15);
}
.next-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--orange-deep); }

/* Poly speech screen */
.poly-say { display: flex; flex-direction: column; align-items: center; gap: 18px; width: min(92vw, 640px); }
.poly-say .poly-pic {
  width: min(70vw, 360px); aspect-ratio: 1050/760; border-radius: 50% 50% 46% 54% / 55% 55% 45% 45%;
  overflow: hidden; box-shadow: 0 10px 26px rgba(44,74,110,.22); background: var(--sky);
  animation: hop 1.4s ease-in-out infinite;
}
.poly-say .poly-pic img { width: 100%; height: 100%; object-fit: cover; display: block; }
.bubble {
  position: relative; background: var(--white); border-radius: 26px; padding: 22px 26px;
  font-size: clamp(1.25rem, 4.4vw, 2rem); font-weight: 800; text-align: center; line-height: 1.3;
  box-shadow: 0 8px 20px rgba(44,74,110,.15); animation: pop .5s cubic-bezier(.2,1.5,.4,1) both;
}
.bubble::before {
  content: ""; position: absolute; top: -14px; left: 50%; transform: translateX(-50%);
  border: 14px solid transparent; border-top: 0; border-bottom-color: var(--white);
}
.bubble small { display: block; font-size: .55em; font-weight: 700; color: var(--navy-soft); margin-top: 8px; }

/* Story */
.story { width: min(94vw, 720px); display: flex; flex-direction: column; align-items: center; gap: 14px; }
.story-panel {
  width: 100%; border-radius: var(--radius); overflow: hidden; background: var(--sky);
  box-shadow: 0 12px 30px rgba(44,74,110,.25); animation: slidein .5s cubic-bezier(.2,1.2,.4,1) both;
}
.story-panel img { width: 100%; max-height: calc(100dvh - 260px); object-fit: contain; display: block; background: #a7cdee; }
.story-panel .story-text {
  background: var(--white); padding: 18px 22px; font-size: clamp(1.15rem, 4vw, 1.7rem); font-weight: 800; text-align: center; line-height: 1.35;
}
.story-panel.text-only { background: var(--white); }
.story-panel.text-only .story-text { padding: 34px 26px; }
.story-tap { font-weight: 800; color: var(--navy-soft); animation: nudge 1.6s ease-in-out infinite; }
.story-dots { display: flex; gap: 6px; }
.story-dots i { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,.65); }
.story-dots i.on { background: var(--navy); }

/* Activity */
.activity-title { font-size: clamp(1.6rem, 6vw, 2.6rem); font-weight: 900; margin: 0; text-align: center; }
.timer {
  font-size: clamp(3rem, 16vw, 6rem); font-weight: 900; font-variant-numeric: tabular-nums;
  color: var(--navy); background: var(--white); border-radius: 28px; padding: 6px 30px;
  box-shadow: 0 8px 20px rgba(44,74,110,.15);
}
.timer-bar { width: min(80vw, 480px); height: 14px; border-radius: 999px; background: rgba(255,255,255,.6); overflow: hidden; }
.timer-bar div { height: 100%; background: var(--orange); width: 0; transition: width 1s linear; }

/* Confetti */
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 5; }
.confetti i {
  position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px;
  animation: fall 2.6s ease-in forwards;
}

/* PIN pad */
.modal-back {
  position: fixed; inset: 0; background: rgba(28,48,72,.55); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  animation: fade .2s both;
}
.modal {
  background: var(--white); border-radius: 24px; padding: 22px; width: min(92vw, 360px); text-align: center;
  animation: pop .35s cubic-bezier(.2,1.4,.4,1) both;
}
.modal h3 { margin: 0 0 6px; font-size: 1.2rem; }
.modal p { margin: 0 0 12px; color: var(--navy-soft); font-size: .92rem; }
.pin-dots { display: flex; justify-content: center; gap: 14px; margin: 12px 0 18px; }
.pin-dots i { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--navy); }
.pin-dots i.on { background: var(--navy); }
.pin-dots.shake { animation: shake .4s; }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.pinpad button {
  border: none; background: #e9f2fb; color: var(--navy); border-radius: 16px;
  font-size: 1.5rem; font-weight: 800; padding: 14px 0;
}
.pinpad button.fn { background: transparent; font-size: 1rem; font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translate(-50%, 250%); visibility: hidden;
  background: var(--navy); color: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 700;
  transition: transform .35s cubic-bezier(.2,1.3,.4,1), visibility .35s; z-index: 200; max-width: 90vw; text-align: center;
}
.toast.show { transform: translate(-50%, 0); visibility: visible; }

/* Animations */
@keyframes deal { from { transform: translateY(40px) scale(.85) rotate(-4deg); opacity: 0; } to { opacity: 1; } }
@keyframes nudge { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes hop { 0%,100% { transform: translateY(0) rotate(-1deg); } 50% { transform: translateY(-10px) rotate(1.5deg); } }
@keyframes pop { from { transform: scale(.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes slidein { from { transform: translateX(60px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } }
@keyframes shake { 20%,60% { transform: translateX(-8px); } 40%,80% { transform: translateX(8px); } }
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01s !important; transition-duration: .01s !important; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .stage { padding-top: calc(20px + var(--safe-top)); padding-bottom: calc(86px + var(--safe-bottom)); }
  .card-wrap { width: min(94vw, calc((100dvh - 120px) * 1.72)); }
  .poly-say { flex-direction: row; width: min(94vw, 820px); }
  .poly-say .poly-pic { width: min(36vw, 300px); }
  .next-btn { padding: 12px 44px; font-size: 1.25rem; }
  .actionbar { padding-bottom: calc(10px + var(--safe-bottom)); }
}
