* { box-sizing: border-box; }
:root {
  --paper: #fdfdf6; --ink: #2f3e46; --blue: #2b59c3; --red: #e63946; --green: #06a77d; --gold: #ffb400;
  --orange: #f77f00; --grid: rgba(43, 89, 195, .12);
  --font: "Baloo 2", "Nunito", system-ui, "Noto Sans", sans-serif;
  --wobble: 255px 15px 225px 15px / 15px 225px 15px 255px;
}
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #9fd3c7; color: var(--ink); font-family: var(--font);
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: none;
}
#viewport { position: fixed; inset: 0; }
#stage { position: absolute; left: 0; top: 0; width: 1280px; height: 720px; transform-origin: 0 0; overflow: hidden; }
#game { position: absolute; inset: 0; width: 1280px; height: 720px; display: block; touch-action: none; }
button, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- overlays */
.overlay { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 10; }
.overlay.dim { background: rgba(47, 62, 70, .35); z-index: 20; }
#loading { background: var(--paper) url(assets/menu_bg.webp) center / cover; }
.loader { text-align: center; font-size: 28px; font-weight: 800; }
.roller-spin {
  width: 90px; height: 34px; margin: 0 auto 10px; border: 4px solid var(--ink); border-radius: 18px;
  background: repeating-linear-gradient(90deg, var(--blue) 0 14px, #6f93e8 14px 22px); animation: roll .6s linear infinite;
}
@keyframes roll { to { background-position: 44px 0; } }

.modal {
  position: relative; background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble);
  padding: 18px 26px 20px; width: 520px; max-width: 94%; max-height: 92%; overflow: auto; text-align: center;
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px; box-shadow: 6px 7px 0 rgba(47, 62, 70, .35); animation: pop .35s cubic-bezier(.3, 1.6, .5, 1);
}
.modal.wide { width: 860px; }
@keyframes pop { from { transform: scale(.6) rotate(-3deg); opacity: 0; } to { transform: none; opacity: 1; } }
.modal h2 { margin: 0 0 4px; font-size: 40px; line-height: 1.1; color: var(--blue); text-shadow: 2px 2px 0 #fff; }
.sub { margin: 4px 0 12px; font-size: 18px; color: #51606a; font-weight: 600; }
.actions { display: flex; gap: 12px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 10px; align-items: stretch; margin: 10px auto; width: 300px; max-width: 100%; }

/* ---------------------------------------------------------------- buttons */
.btn {
  position: relative; cursor: pointer; border: 3px solid var(--ink); border-radius: var(--wobble);
  padding: 8px 20px; font-size: 22px; font-weight: 800; line-height: 1.15; background: #fff; color: var(--ink);
  box-shadow: 3px 4px 0 var(--ink); transition: transform .08s, box-shadow .08s; min-height: 48px;
  overflow-wrap: anywhere;
}
.btn:hover { transform: translate(-1px, -2px) rotate(-.6deg); box-shadow: 4px 6px 0 var(--ink); }
.btn:active { transform: translate(2px, 3px); box-shadow: 1px 1px 0 var(--ink); }
.btn small { display: block; font-size: 15px; font-weight: 700; opacity: .85; }
.btn.green { background: #3fcf8e; }
.btn.blue { background: #7fa6ff; }
.btn.orange { background: #ffb259; }
.btn.paper { background: var(--paper); }
.btn.big { font-size: 34px; padding: 10px 30px; }
.btn.icon { min-width: 52px; padding: 6px 10px; font-size: 24px; }
.btn.toggle { min-width: 110px; font-size: 18px; }
.btn.toggle.on { background: #3fcf8e; }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.badge {
  position: absolute; top: -10px; right: -10px; min-width: 26px; height: 26px; border-radius: 13px; padding: 0 6px;
  background: var(--red); color: #fff; font-size: 16px; line-height: 22px; border: 2px solid #fff; animation: wig 1.2s infinite;
}
@keyframes wig { 0%, 80%, 100% { transform: rotate(0); } 85% { transform: rotate(-12deg) scale(1.1); } 92% { transform: rotate(10deg) scale(1.1); } }

/* ---------------------------------------------------------------- menu */
#menu { justify-content: flex-start; background: linear-gradient(rgba(253, 253, 246, .25), rgba(253, 253, 246, .1)); }
.top-bar { width: 100%; display: flex; justify-content: space-between; align-items: center; padding: 14px 20px 0; }
.rank-box {
  display: flex; align-items: center; gap: 10px; background: var(--paper); border: 3px solid var(--ink);
  border-radius: var(--wobble); padding: 6px 16px 6px 8px; box-shadow: 3px 4px 0 rgba(47, 62, 70, .5); max-width: 380px;
}
.rank-badge {
  width: 48px; height: 48px; border-radius: 50%; background: var(--gold); border: 3px solid var(--ink);
  display: grid; place-items: center; font-weight: 800; font-size: 24px; flex: none;
}
.rank-info { min-width: 0; }
.rank-info b { display: block; font-size: 20px; line-height: 1.1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rank-info small { font-size: 14px; font-weight: 700; color: #51606a; }
.bar { height: 10px; width: 200px; max-width: 100%; border: 2px solid var(--ink); border-radius: 6px; background: #fff; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: repeating-linear-gradient(45deg, var(--green) 0 8px, #3fcf8e 8px 16px); transition: width .6s; }
.top-right { display: flex; align-items: center; gap: 10px; }
.streak, .best {
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble); padding: 6px 14px;
  font-size: 18px; font-weight: 800; box-shadow: 3px 4px 0 rgba(47, 62, 70, .5); white-space: nowrap;
}
.logo { display: flex; flex-direction: column; align-items: center; margin-top: -6px; transform: rotate(-2deg); }
.logo .l1, .logo .l2 {
  font-weight: 800; line-height: .9; letter-spacing: 2px; -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  text-shadow: 4px 5px 0 var(--ink);
}
.logo .l1 { font-size: 64px; color: #fff; }
.logo .l2 { font-size: 80px; color: var(--blue); margin-top: 2px; animation: wobbleT 2.4s ease-in-out infinite; }
.logo .tag { font-size: 21px; font-weight: 800; background: var(--gold); border: 3px solid var(--ink); border-radius: var(--wobble); padding: 0 14px; margin-top: 8px; transform: rotate(2deg); }
@keyframes wobbleT { 50% { transform: rotate(2deg) scale(1.03); } }
.menu-main { display: flex; gap: 30px; align-items: center; margin-top: 14px; }
.preview-card {
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble); padding: 4px 4px 6px; box-shadow: 5px 6px 0 rgba(47, 62, 70, .45);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px); background-size: 18px 18px;
  cursor: pointer; transform: rotate(-1.5deg); width: 320px;
}
#preview { width: 310px; height: 232px; display: block; }
.preview-name { font-size: 22px; font-weight: 800; text-align: center; margin-top: -6px; }
.menu-actions { display: flex; flex-direction: column; gap: 12px; width: 420px; }
.menu-actions .btn.big { font-size: 44px; animation: breathe 1.8s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.04) rotate(-1deg); } }
.menu-row { display: flex; gap: 10px; }
.menu-row .btn { flex: 1; font-size: 19px; padding: 6px 10px; }
.menu-row .btn.icon { flex: none; }
.menu-row.center { justify-content: center; margin-top: 8px; }
.menu-row.center .btn { flex: none; }
.next-goal {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); max-width: 900px; text-align: center;
  background: #fff7d6; border: 3px dashed var(--ink); border-radius: 14px; padding: 6px 18px; font-size: 18px; font-weight: 700;
}

/* ---------------------------------------------------------------- skins */
.skin-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.skin {
  position: relative; border: 3px solid var(--ink); border-radius: 14px; background: #fff; padding: 4px 4px 6px; cursor: pointer;
  box-shadow: 2px 3px 0 var(--ink); transition: transform .1s; min-width: 0;
}
.skin:hover { transform: translateY(-2px) rotate(-1deg); }
.skin canvas { width: 100%; height: 70px; display: block; object-fit: contain; }
.skin b { display: block; font-size: 15px; line-height: 1.1; overflow-wrap: anywhere; }
.skin small { display: block; font-size: 12px; color: #51606a; font-weight: 700; line-height: 1.15; }
.skin.locked canvas { filter: grayscale(1) brightness(.35); }
.skin.locked { background: #eee; }
.skin.on { background: #d9ffe9; outline: 4px solid var(--green); }
.skin .mini-bar { height: 7px; border: 1.5px solid var(--ink); border-radius: 4px; margin-top: 3px; background: #fff; overflow: hidden; }
.skin .mini-bar i { display: block; height: 100%; background: var(--gold); }

/* ---------------------------------------------------------------- missions */
.quest-list { display: flex; flex-direction: column; gap: 10px; }
.quest { display: flex; align-items: center; gap: 12px; background: #fff; border: 3px solid var(--ink); border-radius: 14px; padding: 8px 12px; text-align: left; }
.quest .q-body { flex: 1; min-width: 0; }
.quest b { font-size: 18px; display: block; }
.quest .bar { width: 100%; margin-top: 4px; }
.quest .btn { font-size: 16px; padding: 4px 12px; min-height: 40px; white-space: nowrap; }
.quest.done { background: #f1fff6; }

/* ---------------------------------------------------------------- settings / howto */
.set-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 8px 4px; font-size: 20px; font-weight: 700; border-bottom: 2px dashed rgba(47, 62, 70, .25); }
.set-row select { font-size: 18px; padding: 6px 10px; border: 3px solid var(--ink); border-radius: 10px; background: #fff; max-width: 60%; }
.howto { text-align: left; font-size: 19px; font-weight: 600; line-height: 1.35; margin: 6px 0; padding-left: 28px; }
.howto li { margin: 5px 0; }
.keys { font-size: 16px; color: #51606a; font-weight: 700; }

/* ---------------------------------------------------------------- result */
.modal.result { width: 560px; }
.modal.result h2 { font-size: 52px; color: var(--red); transform: rotate(-2deg); }
.res-big { display: flex; align-items: center; justify-content: center; gap: 12px; }
.res-big b { font-size: 70px; line-height: 1; color: var(--blue); -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; }
.res-newbest { background: var(--gold); border: 3px solid var(--ink); border-radius: 10px; padding: 2px 10px; font-weight: 800; font-size: 18px; transform: rotate(6deg); animation: wig 1s infinite; }
.res-stats { display: flex; justify-content: center; gap: 12px; margin: 10px 0; }
.res-stats div { background: #fff; border: 3px solid var(--ink); border-radius: 12px; padding: 4px 16px; min-width: 120px; }
.res-stats small { display: block; font-size: 14px; font-weight: 700; color: #51606a; }
.res-stats b { font-size: 28px; }
.res-xp { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.res-xp b { font-size: 26px; color: var(--green); }
.res-xp .bar { width: 320px; }
.res-xp small { font-weight: 700; color: #51606a; }
.res-extra { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.res-extra .line { background: #fff7d6; border: 2px solid var(--ink); border-radius: 10px; padding: 4px 10px; font-weight: 700; font-size: 17px; animation: pop .4s both; }
.res-extra .unlock { background: #d9ffe9; display: flex; align-items: center; gap: 10px; justify-content: center; }
.res-extra .unlock canvas { width: 70px; height: 52px; }
.res-extra .unlock .btn { font-size: 16px; min-height: 36px; padding: 2px 10px; }

/* ---------------------------------------------------------------- HUD bits */
#hud-btns { position: absolute; left: 12px; top: 12px; z-index: 5; }
.hbtn { width: 60px; height: 60px; border: 3px solid var(--ink); border-radius: 50%; background: var(--paper); font-size: 20px; font-weight: 800; cursor: pointer; box-shadow: 2px 3px 0 var(--ink); }
.hbtn:active { transform: translate(1px, 2px); box-shadow: none; }
.toast {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); z-index: 40; background: #fff7d6; border: 3px solid var(--ink);
  border-radius: var(--wobble); padding: 8px 20px; font-size: 20px; font-weight: 800; box-shadow: 3px 4px 0 var(--ink); animation: pop .3s; max-width: 80%; text-align: center;
}
#rotate { display: none; }
@media (orientation: portrait) and (max-width: 700px) and (pointer: coarse) {
  #rotate {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 99; background: var(--ink); color: #fff; text-align: center;
    padding: 12px; font-size: 16px; font-weight: 700;
  }
}
@media (prefers-reduced-motion: reduce) {
  .menu-actions .btn.big, .logo .l2, .badge, .res-newbest { animation: none; }
}
