:root {
  --ink: #3b2414;
  --chalk: #fdfbf2;
  --board: #2f5f3a;
  --wood: #b77a3c;
  --wood2: #8a5426;
  --cream: #fff8e6;
  --green: #4caf50;
  --orange: #ff9a1f;
  --blue: #2f8de0;
  --red: #e8473b;
  --font: "Trebuchet MS", "Arial Rounded MT Bold", "Nunito", "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fcf6e; }
body {
  font-family: var(--font); color: var(--ink);
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
button { font-family: inherit; cursor: pointer; touch-action: manipulation; }
.hidden { display: none !important; }

#app {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  background: url(img/bg.jpg) center / cover no-repeat, #8fcf6e;
}
#app::before { content: ""; position: absolute; inset: 0; background: rgba(255, 250, 230, .18); pointer-events: none; }

/* top bar */
#top {
  position: relative; z-index: 3; display: flex; align-items: center; gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px; min-height: 58px;
}
#title {
  flex: 1; margin: 0; font-size: clamp(18px, 3.2vw, 30px); line-height: 1.05; color: #fff;
  text-shadow: 0 2px 0 var(--wood2), 0 0 2px var(--wood2), 2px 0 0 var(--wood2), -2px 0 0 var(--wood2), 0 -2px 0 var(--wood2), 0 4px 6px rgba(0,0,0,.25);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.iconbtn {
  width: 46px; height: 46px; flex: none; border-radius: 50%; border: 3px solid var(--ink);
  background: var(--cream); color: var(--ink); display: grid; place-items: center; padding: 0;
  box-shadow: 0 4px 0 var(--wood2);
}
.iconbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--wood2); }
.iconbtn svg { width: 24px; height: 24px; }
.pill {
  display: flex; align-items: center; gap: 4px; height: 40px; padding: 0 14px; border-radius: 20px;
  background: var(--cream); border: 3px solid var(--ink); font-size: 20px; box-shadow: 0 4px 0 var(--wood2);
}
.st { color: #ffb400; text-shadow: 0 0 1px var(--ink), 0 1px 0 var(--ink); }

.screen { position: relative; z-index: 2; flex: 1; min-height: 0; }

/* hub */
#hub { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2vh; padding: 0 16px 16px; }
.hubHello { display: flex; align-items: flex-end; justify-content: center; gap: 8px; max-width: 900px; width: 100%; }
#hubAra { height: clamp(110px, 26vh, 230px); animation: bob 2.6s ease-in-out infinite; transform-origin: 50% 100%; }
.bubble {
  position: relative; background: #fff; border: 3px solid var(--ink); border-radius: 22px;
  padding: 10px 16px; font-size: clamp(16px, 2.4vw, 22px); font-weight: 700; box-shadow: 0 4px 0 rgba(59,36,20,.35);
  max-width: 420px;
}
.bubble.big { font-size: clamp(18px, 3vw, 28px); margin-bottom: 7vh; }
.bubble::before {
  content: ""; position: absolute; left: -14px; bottom: 18px; width: 20px; height: 20px; background: #fff;
  border-left: 3px solid var(--ink); border-bottom: 3px solid var(--ink); transform: skewY(30deg) rotate(25deg);
}
#cards {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 14px; width: 100%; max-width: 1100px;
}
.card {
  position: relative; border: 4px solid var(--ink); border-radius: 24px; padding: 10px 8px 12px;
  background: var(--c, #fff); color: var(--ink); display: flex; flex-direction: column; align-items: center; gap: 6px;
  box-shadow: 0 7px 0 var(--ink); transition: transform .12s;
}
.card:hover { transform: translateY(-3px) rotate(-1deg); }
.card:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--ink); }
.card .ic { width: 100%; height: clamp(70px, 18vh, 130px); display: grid; place-items: center; }
.card .ic img { height: 100%; width: 90%; object-fit: contain; min-height: 0; }
.card .ic svg { height: 100%; max-width: 100%; }
.card .nm { font-size: clamp(18px, 2.3vw, 26px); font-weight: 800; line-height: 1.05; text-align: center; }
.card .cs { font-size: 17px; letter-spacing: 1px; background: rgba(255,255,255,.75); border-radius: 12px; padding: 1px 10px; font-weight: 700; }
.tiles { display: flex; gap: 4px; }
.tile {
  width: clamp(30px, 4vw, 46px); aspect-ratio: 1; border-radius: 9px; border: 3px solid var(--ink); display: grid; place-items: center;
  font-size: clamp(20px, 3vw, 32px); font-weight: 900; background: #fff; box-shadow: 0 3px 0 var(--ink);
}

/* play */
#play { display: flex; flex-direction: column; align-items: center; padding: 0 12px 10px; gap: 10px; }
#bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; max-width: 980px; flex-wrap: wrap; }
#levels { display: flex; gap: 6px; }
.lvl {
  border: 3px solid var(--ink); border-radius: 16px; background: var(--cream); padding: 5px 12px; font-size: 16px; font-weight: 800;
  box-shadow: 0 3px 0 var(--wood2); color: var(--ink);
}
.lvl small { color: #e39b00; font-size: 13px; }
.lvl.on { background: #ffd23f; }
#dots { display: flex; gap: 6px; }
.dot { width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--ink); background: rgba(255,255,255,.7); }
.dot.done { background: #ffb400; }
.dot.now { background: #fff; transform: scale(1.25); }

#board {
  width: 100%; max-width: 980px; flex: 1; min-height: 0; display: flex; flex-direction: column;
  background: var(--board); border: 10px solid var(--wood); border-radius: 22px; outline: 3px solid var(--ink);
  box-shadow: inset 0 0 40px rgba(0,0,0,.35), 0 6px 0 var(--wood2); padding: 10px 14px; color: var(--chalk);
}
#qrow { display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 46px; }
#question { font-size: clamp(20px, 3.4vw, 34px); font-weight: 800; text-align: center; text-shadow: 0 2px 0 rgba(0,0,0,.3); }
.speak { width: 40px; height: 40px; background: #ffd23f; }
#stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; position: relative; }

/* letters */
.lwrap { display: flex; flex-direction: column; align-items: center; gap: 2vh; height: 100%; justify-content: center; width: 100%; }
.pic { height: clamp(90px, 26vh, 220px); max-width: 80%; object-fit: contain; filter: drop-shadow(0 6px 0 rgba(0,0,0,.25)); animation: pop .35s ease-out; }
.word { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.word .tile { width: clamp(34px, 6.4vw, 62px); font-size: clamp(22px, 4.6vw, 44px); color: var(--ink); background: var(--cream); }
.word .tile.miss { background: #ffd23f; animation: pulse 1.2s ease-in-out infinite; }
.word .tile.fill { background: #8fe388; animation: pop .35s ease-out; }

/* counting */
.items { display: flex; flex-wrap: wrap; justify-content: center; align-content: center; gap: 1.2vh 1.4vw; max-width: 760px; height: 100%; }
.items img { height: var(--s, 70px); animation: pop .35s ease-out backwards; filter: drop-shadow(0 4px 0 rgba(0,0,0,.25)); }
.sum { display: flex; align-items: center; justify-content: center; gap: 2vw; flex-wrap: wrap; height: 100%; }
.sum .grp { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; max-width: 40%; }
.sum .grp img { height: var(--s, 60px); animation: pop .35s ease-out backwards; }
.sum .op { font-size: clamp(40px, 7vw, 72px); font-weight: 900; color: #ffd23f; }

/* answers */
#answers { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; width: 100%; max-width: 980px; }
.ans {
  min-width: clamp(78px, 13vw, 130px); height: clamp(70px, 12vh, 104px); padding: 0 16px;
  border: 4px solid var(--ink); border-radius: 22px; background: var(--cream); color: var(--ink);
  font-size: clamp(30px, 5.5vw, 54px); font-weight: 900; box-shadow: 0 7px 0 var(--ink);
  transition: background .2s, opacity .25s, transform .1s;
}
.ans:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--ink); }
.ans.time { font-size: clamp(26px, 4.5vw, 44px); }
.ans.ok { background: #8fe388; animation: pop .35s ease-out; }
.ans.no { opacity: .35; animation: shake .4s; pointer-events: none; }

/* shapes */
.shapes { display: grid; gap: clamp(8px, 2vw, 22px); grid-template-columns: repeat(var(--cols, 4), 1fr); align-items: center; justify-items: center; width: 100%; max-width: 820px; }
.shp {
  width: 100%; max-width: 190px; aspect-ratio: 1; padding: 8%; border-radius: 22px; border: 3px dashed rgba(255,255,255,.25);
  background: rgba(255,255,255,.06); display: grid; place-items: center; transition: transform .1s, opacity .25s;
}
.shp svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 5px 0 rgba(0,0,0,.3)); }
.shp:hover { transform: scale(1.05); }
.shp.ok { background: rgba(143, 227, 136, .35); border-color: #8fe388; animation: pop .35s; }
.shp.no { opacity: .3; animation: shake .4s; pointer-events: none; }

/* memory */
.mem { display: grid; gap: var(--gap, 10px); grid-template-columns: repeat(var(--cols, 4), var(--cw, 100px)); grid-auto-rows: var(--ch, 120px); }
.mc { position: relative; perspective: 600px; border: none; background: none; padding: 0; min-width: 0; min-height: 0; }
.mc .in { position: absolute; inset: 0; transition: transform .35s; transform-style: preserve-3d; }
.mc.up .in, .mc.got .in { transform: rotateY(180deg); }
.mc .f, .mc .b {
  position: absolute; inset: 0; border-radius: 14px; border: 3px solid var(--ink); backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: 0 4px 0 rgba(0,0,0,.35);
}
.mc .b { background: #ffb21f radial-gradient(circle at 50% 50%, #ffd23f 0 30%, transparent 31%); }
.mc .b::after { content: "?"; font-size: clamp(22px, 4vw, 40px); font-weight: 900; color: var(--ink); }
.mc .f { background: var(--cream); transform: rotateY(180deg); padding: 6%; }
.mc .f img { flex: 1; min-height: 0; max-width: 100%; object-fit: contain; }
.mc .f span { color: var(--ink); font-weight: 800; font-size: clamp(11px, 1.6vw, 17px); }
.mc.got .f { background: #c9f5c2; }
.mc.got { animation: pop .4s; }

/* clock */
.clock { height: 100%; max-height: 330px; aspect-ratio: 1; }
.clock svg { width: 100%; height: 100%; filter: drop-shadow(0 6px 0 rgba(0,0,0,.3)); }

/* coach */
#coach {
  position: absolute; left: 8px; bottom: 8px; display: flex; align-items: flex-end; gap: 4px; pointer-events: none; z-index: 4;
}
#coachAra { height: clamp(80px, 22vh, 190px); transform-origin: 50% 100%; }
#coachAra.jump { animation: jump .6s ease-out; }
#coach .bubble { margin-bottom: 40%; opacity: 0; transform: scale(.6); transition: opacity .2s, transform .2s; max-width: 220px; }
#coach .bubble.show { opacity: 1; transform: none; }

/* end panel */
.overlay { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; background: rgba(40, 25, 10, .45); padding: 16px; }
.panel {
  position: relative; width: min(460px, 100%); background: var(--cream); border: 5px solid var(--ink); border-radius: 30px;
  padding: 70px 20px 22px; text-align: center; box-shadow: 0 10px 0 var(--ink); animation: pop .4s ease-out;
}
.endAra { position: absolute; left: 50%; top: -96px; height: 160px; transform: translateX(-50%); }
#endTitle { margin: 0 0 6px; font-size: 30px; }
#endStars { font-size: 64px; letter-spacing: 6px; line-height: 1; }
#endStars span { color: #d8cfb8; display: inline-block; text-shadow: 0 3px 0 var(--ink); }
#endStars span.lit { color: #ffb400; animation: starIn .5s ease-out backwards; }
#endMsg { font-size: 19px; font-weight: 700; margin: 8px 0 14px; }
.endBtns { display: flex; flex-direction: column; gap: 10px; }
.btn {
  border: 4px solid var(--ink); border-radius: 20px; padding: 12px 18px; font-size: 22px; font-weight: 900; color: #fff;
  text-shadow: 0 2px 0 rgba(0,0,0,.3); box-shadow: 0 6px 0 var(--ink);
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.btn.green { background: var(--green); } .btn.orange { background: var(--orange); } .btn.blue { background: var(--blue); }

#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

@keyframes bob { 0%,100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-8px) rotate(2deg); } }
@keyframes pop { 0% { transform: scale(.6); } 70% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-9px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(4px); } }
@keyframes jump { 0% { transform: translateY(0) scale(1); } 30% { transform: translateY(-26px) scale(1.04, .97); } 60% { transform: translateY(0) scale(1.05, .93); } 100% { transform: none; } }
@keyframes starIn { 0% { transform: scale(0) rotate(-90deg); } 70% { transform: scale(1.3) rotate(10deg); } 100% { transform: scale(1); } }

/* landscape: room for Ara on the left */
@media (min-aspect-ratio: 4/3) and (min-width: 800px) {
  #play { padding-left: clamp(150px, 19vw, 260px); }
  #coach { flex-direction: column; align-items: flex-start; }
  #coach .bubble { margin-bottom: 0; order: -1; }
  #coach .bubble::before { left: 30px; bottom: -14px; transform: rotate(-45deg) skew(10deg, 10deg); }
}
/* portrait / phone */
@media (max-width: 799px), (max-aspect-ratio: 4/3) {
  #top { min-height: 52px; padding-left: 10px; padding-right: 10px; gap: 6px; }
  .iconbtn { width: 42px; height: 42px; }
  .pill { height: 38px; padding: 0 10px; font-size: 18px; }
  #title { white-space: normal; font-size: 19px; }
  #cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; max-width: 520px; }
  #cards .card:last-child { grid-column: 1 / -1; }
  #cards .card:last-child .ic { height: clamp(56px, 10vh, 90px); }
  .card .ic { height: clamp(56px, 11vh, 110px); }
  .hubHello { align-items: center; }
  #hubAra { height: clamp(90px, 17vh, 170px); }
  .bubble.big { margin-bottom: 0; font-size: 18px; }
  #play { padding-bottom: clamp(86px, 13vh, 120px); }
  #coachAra { height: clamp(70px, 11vh, 110px); }
  #coach .bubble { margin-bottom: 14px; font-size: 16px; }
  #board { border-width: 7px; padding: 8px; }
}
@media (max-height: 560px) {
  #hubAra { height: 90px; }
  .bubble.big { margin-bottom: 0; }
  .card .ic { height: 60px; }
}
