* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: "Chalkboard SE", "Comic Sans MS", "Comic Neue", "Marker Felt", system-ui, sans-serif;
  color: #2b2350;
  background: #ffe9a8;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body {
  background:
    radial-gradient(circle at 15% 20%, #ffd1e8 0 12%, transparent 13%),
    radial-gradient(circle at 85% 80%, #c9f2ff 0 14%, transparent 15%),
    radial-gradient(circle at 90% 12%, #d9ffd1 0 8%, transparent 9%),
    linear-gradient(160deg, #fff3c4, #ffd98a);
}
.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: flex; }

/* ---------- buttons ---------- */
.btn, .big-btn {
  font: inherit; border: 0; cursor: pointer; text-decoration: none; color: inherit;
  display: inline-flex; align-items: center; justify-content: center; gap: .4em;
  border-radius: 999px; position: relative;
  transition: transform .08s;
  touch-action: manipulation;
}
.btn:active, .big-btn:active { transform: translateY(3px) scale(.98); }
.big-btn {
  font-size: clamp(22px, 4.2vw, 36px); font-weight: bold;
  padding: .7em 1.3em; color: #fff;
  background: linear-gradient(#ff6b8b, #ff3d6e);
  box-shadow: 0 7px 0 #c41f4d, 0 12px 24px rgba(196,31,77,.35);
}
.big-btn.alt { background: linear-gradient(#5cc8ff, #2b8cff); box-shadow: 0 7px 0 #1b5fc2, 0 12px 24px rgba(27,95,194,.35); }
.btn.soft {
  font-size: clamp(16px, 2.6vw, 22px); padding: .6em 1.1em; background: #fff;
  box-shadow: 0 5px 0 #e0b44a, 0 8px 16px rgba(0,0,0,.12);
}
.btn.small {
  font-size: 20px; padding: .45em .8em; background: #fff;
  box-shadow: 0 4px 0 #d9a93a;
  min-width: 48px; min-height: 48px;
}
input[type=file] { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.row { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; }

/* ---------- home ---------- */
#home { overflow-y: auto; padding: max(16px, env(safe-area-inset-top)) 16px 16px; }
/* margin:auto centres without clipping the top when the page is taller than the screen */
.home-inner { margin: auto; text-align: center; max-width: 960px; width: 100%; display: flex; flex-direction: column; gap: 18px; align-items: center; }
.logo { font-size: clamp(54px, 12vw, 120px); margin: 0; line-height: 1; letter-spacing: 2px; }
.logo span { display: inline-block; text-shadow: 0 5px 0 rgba(0,0,0,.12); animation: bob 2.4s ease-in-out infinite; }
.logo span:nth-child(8n+1) { color: #ff3d6e; animation-delay: 0s; }
.logo span:nth-child(8n+2) { color: #ff8c1a; animation-delay: .1s; }
.logo span:nth-child(8n+3) { color: #e6b800; animation-delay: .2s; }
.logo span:nth-child(8n+4) { color: #2fbf4a; animation-delay: .3s; }
.logo span:nth-child(8n+5) { color: #2b8cff; animation-delay: .4s; }
.logo span:nth-child(8n+6) { color: #8a3fd1; animation-delay: .5s; }
.logo span:nth-child(8n+7) { color: #ff3d6e; animation-delay: .6s; }
.logo span:nth-child(8n+8) { color: #2fbf4a; animation-delay: .7s; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-8px) rotate(2deg); } }
.tagline { font-size: clamp(18px, 3.4vw, 28px); margin: 0; }
.photo-btn { font-size: clamp(24px, 5vw, 44px); padding: .8em 1.4em; }
.photo-btn .emoji { font-size: 1.3em; }
.legend { list-style: none; padding: 14px 18px; margin: 0; display: flex; flex-wrap: wrap; gap: 10px 22px; justify-content: center;
  background: rgba(255,255,255,.75); border-radius: 22px; font-size: clamp(16px, 2.4vw, 21px); }
.legend li { display: flex; align-items: center; gap: 8px; }
.sw { width: 30px; height: 30px; border-radius: 8px; display: inline-block; border: 3px solid rgba(0,0,0,.12); }
.sw.black { background: #1d1b22; } .sw.red { background: #e3262b; } .sw.green { background: #1fa83a; }
.sw.blue { background: #1f5fe0; } .sw.yellow { background: #ffd21a; }
.sw.purple { background: #8a3fd1; } .sw.orange { background: #ff7a1a; } .sw.lightblue { background: #19a7e0; }
.sw.dot { width: 18px; height: 18px; border-radius: 50%; margin: 6px; }
.tip { margin: 0; font-size: 15px; opacity: .75; max-width: 560px; }

/* ---------- loading ---------- */
#loading { align-items: center; justify-content: center; }
.loader { text-align: center; font-size: 30px; }
.pencil { font-size: 90px; animation: wiggle .5s ease-in-out infinite alternate; }
@keyframes wiggle { from { transform: rotate(-15deg) translateX(-10px); } to { transform: rotate(15deg) translateX(10px); } }

/* ---------- game ---------- */
#game { flex-direction: column; }
.bar {
  flex: none; display: flex; align-items: center; gap: 10px;
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
}
.score { margin-left: auto; margin-right: auto; font-size: 28px; font-weight: bold; background: #fff; border-radius: 999px; padding: 4px 18px;
  box-shadow: 0 4px 0 #d9a93a; white-space: nowrap; }
.score .timer { font-size: .7em; opacity: .75; margin-left: 6px; }
#score.bump { display: inline-block; animation: bump .35s; }
@keyframes bump { 50% { transform: scale(1.25); } }
#seeBtn.on { background: #2b2350; color: #fff; }
.stage { position: relative; flex: 1; min-height: 0; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }

.touch { position: absolute; inset: auto 0 0 0; display: none; justify-content: space-between; align-items: flex-end;
  padding: 0 max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  pointer-events: none; }
.touch.show { display: flex; }
.cluster { display: none; gap: 14px; align-items: flex-end; }
.touch[data-mode=platformer] .c1 { display: flex; flex: 1; }
.touch[data-mode=platformer] .c1 .jump { margin-left: auto; }
.touch[data-mode=race] .c1, .touch[data-mode=race] .c2 { display: flex; }
.touch[data-mode=race] .tbtn { width: clamp(58px, 9vw, 92px); height: clamp(58px, 9vw, 92px); }
.tbtn.p2 { background: rgba(255,122,26,.5); }
.tbtn.jump.p2 { background: rgba(255,122,26,.75); }
.touch[data-mode=tilt] .dpad, .touch[data-mode=maze] .dpad { display: grid; grid-template-columns: repeat(3, auto); grid-template-areas: ". u ." "l . r" ". d ."; gap: 4px; }
#dUp { grid-area: u; } #dLeft { grid-area: l; } #dRight { grid-area: r; } #dDown { grid-area: d; }
.dpad .tbtn { width: clamp(58px, 8.5vw, 88px); height: clamp(58px, 8.5vw, 88px); }
.tilt-btn { position: absolute; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); display: none; }
.tilt-btn.show { display: inline-flex; }
.tbtn {
  pointer-events: auto; touch-action: none;
  width: clamp(70px, 11vw, 104px); height: clamp(70px, 11vw, 104px); border-radius: 50%;
  border: 4px solid rgba(255,255,255,.9); background: rgba(43,35,80,.35); color: #fff;
  font-size: clamp(30px, 5vw, 46px); font-family: system-ui, sans-serif;
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.tbtn.jump { width: clamp(84px, 13vw, 124px); height: clamp(84px, 13vw, 124px); background: rgba(255,61,110,.55); }
.tbtn.down { background: rgba(255,210,26,.8); transform: scale(.94); }

.toast { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: #2b2350; color: #fff;
  padding: 10px 18px; border-radius: 16px; font-size: 18px; opacity: 0; transition: opacity .3s; pointer-events: none; max-width: 90%; text-align: center; }
.toast.show { opacity: .92; }

.win { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(43,35,80,.25); }
.win.show { display: flex; animation: pop .4s; }
.win-card { background: #fff; border-radius: 30px; padding: 26px 34px 34px; text-align: center; box-shadow: 0 10px 0 #d9a93a, 0 20px 40px rgba(0,0,0,.2); }
.win-card h2 { font-size: clamp(32px, 6vw, 56px); margin: 0 0 6px; color: #ff3d6e; }
.win-card p { font-size: 28px; margin: 0 0 24px; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }

@media (max-width: 640px), (max-height: 500px) {
  .bar .lbl { display: none; }
  .score { font-size: 22px; }
}

/* ---------- fix my photo ---------- */
#fix { flex-direction: column; background: #2b2350; color: #fff; }
.bar-title { margin: 0 auto; font-size: clamp(20px, 3.5vw, 30px); }
.fix-body { flex: 1; min-height: 0; display: flex; gap: 12px; padding: 0 max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
.fix-stage { position: relative; flex: 1; min-width: 0; touch-action: none; }
#fixCanvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.handle { position: absolute; left: -28px; top: -28px; width: 56px; height: 56px; border-radius: 50%; touch-action: none; cursor: grab;
  background: radial-gradient(circle, #ffd21a 0 9px, rgba(255,210,26,.35) 10px 100%); border: 3px solid #ffd21a; }
.handle.drag { background: radial-gradient(circle, #ff3d6e 0 9px, rgba(255,61,110,.35) 10px 100%); border-color: #ff3d6e; }
.fix-side { width: min(36%, 380px); display: flex; flex-direction: column; gap: 10px; align-items: center; overflow-y: auto; }
.fix-help { margin: 0; font-size: 17px; text-align: center; opacity: .9; }
.fix-preview { width: 100%; flex: 1; min-height: 120px; display: flex; align-items: center; justify-content: center; }
#fixPreview { border-radius: 10px; background: #fff; }
.fix-info { margin: 0; font-size: 22px; }
.fix-ink { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.fix-ink input { width: 150px; accent-color: #ffd21a; }
.fix-side .big-btn { font-size: clamp(20px, 3vw, 28px); }
@media (max-aspect-ratio: 1/1) {
  .fix-body { flex-direction: column; }
  .fix-side { width: 100%; max-height: 46%; }
  .fix-preview { min-height: 90px; }
}
#fix .btn { color: #2b2350; }
.fix-help, .fix-info, .fix-ink, .fix-side .row, .fix-side .big-btn { flex: none; }
.fix-preview { min-height: 0; overflow: hidden; }
#fixPreview { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- draw your hero ---------- */
.hero-row { align-items: center; }
.hero-pick { display: none; align-items: center; gap: 8px; font-size: 19px; background: rgba(255,255,255,.75); border-radius: 999px; padding: 6px 12px; }
.hero-pick.show { display: flex; }
.chip { padding: 4px; width: 60px; height: 60px; font-size: 32px; }
.chip.on { outline: 4px solid #ff3d6e; }
.chip canvas { width: 48px; height: 48px; }
#hero { align-items: center; justify-content: center; }
.hero-inner { display: flex; flex-direction: column; align-items: center; gap: 16px; width: min(900px, 94vw); height: min(720px, 92vh); }
.hero-inner h2 { margin: 0; font-size: clamp(30px, 6vw, 54px); color: #ff3d6e; }
.hero-stage { flex: 1; min-height: 0; width: 100%; border-radius: 26px;
  background: repeating-conic-gradient(#fff 0 25%, #f1ecff 0 50%) 0 0 / 40px 40px; box-shadow: inset 0 0 0 4px #d9a93a; }

/* ---------- mode picker ---------- */
#modes { flex-direction: column; }
.modes-body { flex: 1; min-height: 0; display: flex; gap: 18px; padding: 0 max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.mode-thumb { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; }
#modeThumb { border-radius: 16px; box-shadow: 0 8px 20px rgba(0,0,0,.2); background: #fff; }
.mode-grid { flex: 1.2; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-content: center; }
.mode-card { font: inherit; color: #2b2350; border: 0; cursor: pointer; border-radius: 26px; background: #fff; padding: 16px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; box-shadow: 0 6px 0 #d9a93a, 0 10px 20px rgba(0,0,0,.12);
  touch-action: manipulation; transition: transform .08s; }
.mode-card:active { transform: translateY(3px) scale(.98); }
.mode-card .emoji { font-size: clamp(40px, 7vw, 64px); line-height: 1; }
.mode-card b { font-size: clamp(20px, 3vw, 28px); }
.mode-card small { font-size: clamp(13px, 1.8vw, 16px); opacity: .75; }
.mode-card .best { font-size: 15px; color: #c41f4d; min-height: 1.2em; }
#modePlatformer { background: #ffe1ea; } #modeTilt { background: #e3f4ff; } #modeMaze { background: #e6ffe0; } #modeRace { background: #fff0d6; }
.win-more { margin: 18px 0 0; font-size: 18px; }
@media (max-aspect-ratio: 1/1) {
  .modes-body { flex-direction: column; }
  .mode-thumb { flex: .8; }
}
@media (max-height: 500px) {
  .mode-card { padding: 8px; }
  .mode-card small { display: none; }
}

/* ---------- my levels ---------- */
.count:not(:empty) { background: #ff3d6e; color: #fff; border-radius: 999px; padding: 0 .5em; font-size: .85em; }
#book { flex-direction: column; }
.book-body { flex: 1; min-height: 0; overflow-y: auto; padding: 0 max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); -webkit-overflow-scrolling: touch; }
.book-empty { display: none; text-align: center; font-size: 24px; margin-top: 10vh; }
.book-empty.show { display: block; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 18px; }
.book-card { background: #fff; border-radius: 22px; padding: 10px; box-shadow: 0 6px 0 #d9a93a, 0 10px 20px rgba(0,0,0,.12); display: flex; flex-direction: column; gap: 6px; }
.book-open { border: 0; padding: 0; background: #f4f0ff; border-radius: 14px; cursor: pointer; aspect-ratio: 4 / 3; display: flex; align-items: center; justify-content: center; overflow: hidden; touch-action: manipulation; }
.book-open img { max-width: 100%; max-height: 100%; display: block; }
.book-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 15px; }
.book-meta .date { opacity: .7; margin-right: auto; }
.badge { background: #fff3c4; border-radius: 999px; padding: 2px 8px; font-size: 14px; white-space: nowrap; }
.book-actions { display: flex; justify-content: flex-end; gap: 8px; }
.small-ic { padding: .3em .6em; font-size: 20px; min-width: 48px; min-height: 44px; }

.modal { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(43,35,80,.45); z-index: 10; padding: 16px; }
.modal.show { display: flex; }
.modal-card { background: #fff; border-radius: 26px; padding: 22px 26px; text-align: center; max-width: 520px; width: 100%; max-height: 100%; overflow-y: auto;
  box-shadow: 0 10px 0 #d9a93a, 0 20px 40px rgba(0,0,0,.25); }
.modal-card h3 { margin: 0 0 8px; font-size: 30px; }
.modal-card p { margin: 6px 0; font-size: 18px; }
.modal-card .row { margin-top: 14px; }
.modal-card .big-btn { font-size: 24px; }
.gate-thumb { max-width: 180px; max-height: 110px; border-radius: 10px; }
.gate-q { font-size: 34px !important; font-weight: bold; }
#gateIn { font: inherit; font-size: 30px; width: 140px; text-align: center; border: 3px solid #d9a93a; border-radius: 14px; padding: 4px; }
.shake { animation: shake .4s; }
@keyframes shake { 25% { transform: translateX(-10px); } 75% { transform: translateX(10px); } }
.share-qr { display: none; width: min(320px, 70vw); height: auto; image-rendering: pixelated; margin: 6px auto; border-radius: 8px; }
.share-qr.show { display: block; }
.share-msg { font-size: 17px !important; }

#musicBtn.off, #camBtn.page { opacity: .55; }
#musicBtn.off::after { content: ''; position: absolute; left: 22%; right: 22%; top: 50%; height: 3px; background: #c41f4d; transform: rotate(-35deg); border-radius: 2px; }
@media (max-width: 700px), (max-height: 500px) {
  .bar { gap: 6px; }
  .btn.small { min-width: 44px; padding: .4em .55em; font-size: 18px; }
  .score .timer { display: none; }
}

@media (max-height: 500px) {
  .fix-help { display: none; }
  .fix-side { gap: 6px; }
  .fix-preview { min-height: 110px; }
  .fix-side .btn.soft { font-size: 16px; padding: .45em .9em; }
  .fix-side .big-btn { font-size: 20px; padding: .5em 1em; }
}
