* { box-sizing: border-box; }
:root {
  --ink: #3b2616;
  --paper: #fffaec;
  --paper2: #f6ead0;
  --mango: #ffb020;
  --lime: #6cc644;
  --dragon: #ff4fa3;
  --river: #2f7f86;
  --font: "Baloo 2", "Nunito", system-ui, "Noto Sans", sans-serif;
}
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #173a3c; color: var(--ink);
  font-family: var(--font);
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
}
#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; touch-action: none; display: block; }
.hidden { display: none !important; }

/* ---------- overlays & modals */
.overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: rgba(30, 40, 30, 0.55); animation: fade .2s ease;
}
.overlay.clear { background: linear-gradient(90deg, rgba(20,40,40,.55) 0%, rgba(20,40,40,0) 32%, rgba(20,40,40,0) 68%, rgba(20,40,40,.55) 100%); }
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
@keyframes pop { 0% { transform: scale(.7) rotate(-2deg); opacity: 0 } 70% { transform: scale(1.04) rotate(.5deg); opacity: 1 } 100% { transform: scale(1) } }
@keyframes wobble { 0%,100% { transform: rotate(-2deg) } 50% { transform: rotate(2deg) } }
@keyframes bob { 0%,100% { transform: translateY(0) } 50% { transform: translateY(-6px) } }

.modal {
  background: var(--paper); border: 4px solid var(--ink); border-radius: 26px; padding: 22px 34px 24px;
  min-width: 440px; max-width: 980px; max-height: 690px; overflow: auto; text-align: center;
  box-shadow: 0 10px 0 rgba(59,38,22,.35), 0 30px 60px rgba(0,0,0,.4); animation: pop .32s cubic-bezier(.2,.9,.3,1.3) both;
  background-image: radial-gradient(circle at 20% 10%, rgba(255,210,120,.25), transparent 40%), radial-gradient(circle at 90% 90%, rgba(120,200,180,.2), transparent 45%);
}
.modal.wide { width: 920px; }
.modal h2 { margin: 0 0 4px; font-size: 42px; line-height: 1.05; color: #c2410c; text-shadow: 2px 2px 0 #ffe3a3; }
.modal h3 { margin: 10px 0 6px; font-size: 22px; text-align: left; }
.modal .sub { margin: 4px 0 10px; color: #7a5a3a; font-size: 18px; }
.actions { display: flex; gap: 14px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; margin: 12px auto; width: 320px; }
.icon-row { display: flex; gap: 10px; justify-content: center; }

/* ---------- buttons */
.btn {
  font: inherit; font-weight: 800; font-size: 22px; color: #fff; border: 3px solid var(--ink); cursor: pointer;
  padding: 8px 26px 10px; border-radius: 18px; position: relative; line-height: 1.15;
  box-shadow: 0 5px 0 var(--ink); transition: transform .08s, filter .15s;
  text-shadow: 0 2px 0 rgba(0,0,0,.25);
}
.btn:hover { filter: brightness(1.08); transform: translateY(-2px) rotate(-.5deg); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.btn:disabled { filter: grayscale(1) opacity(.6); cursor: not-allowed; }
.btn.mango { background: linear-gradient(#ffcf4a, #f29a12); }
.btn.lime { background: linear-gradient(#9fe35a, #3f9a2a); }
.btn.dragon { background: linear-gradient(#ff7ac0, #d6246f); }
.btn.river { background: linear-gradient(#5fc1c8, #2f7f86); }
.btn.paper { background: var(--paper); color: var(--ink); text-shadow: none; }
.btn.big { font-size: 40px; padding: 10px 40px 14px; border-radius: 24px; animation: wobble 2.4s ease-in-out infinite; }
.btn.icon { padding: 6px 14px 8px; font-size: 22px; }
.btn.off { opacity: .45; }
.btn small { font-size: 16px; opacity: .9; }
.badge { position: absolute; top: -8px; right: -8px; min-width: 24px; height: 24px; border-radius: 12px; background: var(--dragon); border: 2px solid var(--ink); color: #fff; font-style: normal; font-size: 14px; line-height: 20px; padding: 0 5px; }

/* ---------- menu */
#menu { flex-direction: row; justify-content: space-between; align-items: stretch; padding: 26px 30px; }
.menu-left, .menu-right { width: 380px; display: flex; flex-direction: column; gap: 14px; }
.menu-left { align-items: center; justify-content: center; padding-bottom: 130px; }
.menu-right { justify-content: center; }
.logo { display: flex; flex-direction: column; align-items: center; line-height: .88; animation: bob 3s ease-in-out infinite; text-align: center; }
.logo .l1 { font-size: 38px; font-weight: 800; letter-spacing: 10px; color: #fff; -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; text-shadow: 0 4px 0 var(--ink); }
.logo .l2 { font-size: 66px; font-weight: 800; color: var(--mango); -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill; text-shadow: 0 6px 0 var(--ink); max-width: 380px; overflow-wrap: anywhere; }
.tagline { margin: 0; color: #fff; font-weight: 700; font-size: 19px; text-shadow: 0 2px 0 var(--ink), 0 0 8px rgba(0,0,0,.4); text-align: center; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.chip { background: var(--paper); border: 3px solid var(--ink); border-radius: 20px; padding: 2px 14px; font-weight: 800; font-size: 20px; }
.goal { margin: 0; color: #fff; font-weight: 700; font-size: 17px; text-align: center; text-shadow: 0 2px 0 var(--ink); max-width: 360px; }
.card { background: var(--paper); border: 4px solid var(--ink); border-radius: 22px; padding: 12px 16px 14px; box-shadow: 0 6px 0 rgba(59,38,22,.5); }
.daily { text-align: left; display: flex; flex-direction: column; gap: 4px; }
.dc-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.dc-title { font-size: 26px; font-weight: 800; color: #c2410c; }
.dc-time { font-size: 14px; color: #7a5a3a; }
.dc-rule { font-size: 21px; font-weight: 800; color: var(--river); }
.dc-desc { font-size: 16px; color: #6b4a2a; line-height: 1.2; }
.dc-row { display: flex; justify-content: space-between; gap: 8px; font-weight: 700; font-size: 16px; }
.dc-reward { font-size: 15px; color: #2f8f3a; font-weight: 700; }
.daily .btn { margin-top: 6px; font-size: 20px; }
.grid-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-btns .btn { font-size: 18px; padding: 8px 8px 10px; display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 52px; }
.grid-btns .btn .e { font-size: 22px; }
.grid-btns .wide { grid-column: span 2; }

/* ---------- in-game HUD buttons */
#hud { position: absolute; inset: 0; pointer-events: none; }
.hbtn { pointer-events: auto; position: absolute; font: inherit; font-weight: 800; border: 3px solid var(--ink); border-radius: 16px; background: var(--paper); color: var(--ink); cursor: pointer; box-shadow: 0 4px 0 var(--ink); }
.hbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
#btn-pause { right: 22px; top: 18px; width: 58px; height: 54px; font-size: 20px; }
#btn-swap { left: 1010px; top: 196px; transform: translateX(-50%); height: 50px; padding: 0 16px; font-size: 20px; white-space: nowrap; max-width: 250px; overflow: hidden; }
#btn-swap:active { transform: translateX(-50%) translateY(3px); }
#btn-swap b { background: var(--mango); border: 2px solid var(--ink); border-radius: 10px; padding: 0 7px; margin-left: 4px; }
#btn-swap:disabled { opacity: .45; }

/* ---------- toast */
.toast { position: absolute; left: 50%; top: 14px; transform: translateX(-50%); background: var(--paper); border: 3px solid var(--ink); border-radius: 18px; padding: 6px 20px; font-size: 20px; font-weight: 800; box-shadow: 0 4px 0 var(--ink); animation: pop .3s ease both; z-index: 5; max-width: 700px; text-align: center; }

/* ---------- loading */
.loader { text-align: center; font-size: 24px; color: #fff; font-weight: 800; text-shadow: 0 2px 0 var(--ink); }
.spin { width: 64px; height: 64px; margin: 0 auto 10px; border-radius: 50%; border: 8px solid rgba(255,255,255,.3); border-top-color: var(--mango); animation: rot 1s linear infinite; }
@keyframes rot { to { transform: rotate(360deg) } }
.loader .bar { width: 260px; height: 14px; border: 3px solid var(--ink); border-radius: 10px; background: var(--paper); overflow: hidden; margin: 8px auto 0; }
.loader .bar i { display: block; height: 100%; width: 0; background: var(--mango); transition: width .2s; }

/* ---------- book */
.book-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; text-align: left; }
.fcard { background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 6px 10px 8px; display: grid; grid-template-columns: 64px 1fr; gap: 2px 8px; align-items: center; min-height: 118px; }
.fcard canvas { width: 64px; height: 64px; grid-row: span 2; }
.fcard .nm { font-weight: 800; font-size: 18px; line-height: 1.05; }
.fcard .tier { font-size: 13px; color: #9a7a5a; }
.fcard .bio { grid-column: span 2; font-size: 13.5px; line-height: 1.2; color: #6b4a2a; }
.fcard .made { grid-column: span 2; font-size: 13px; font-weight: 700; color: var(--river); }
.fcard.locked { background: #eee6d6; }

/* ---------- shop */
.wallet { display: inline-block; font-size: 24px; font-weight: 800; background: #fff; border: 3px solid var(--ink); border-radius: 20px; padding: 0 16px; }
.shop-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
#shop .modal { padding-top: 14px; padding-bottom: 16px; }
#shop .modal h3 { margin: 4px 0 4px; }
#shop .actions { margin-top: 10px; }
.skin { background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 6px; min-width: 0; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.skin canvas { width: 190px; height: 100px; flex: none; border-radius: 10px; border: 2px solid var(--ink); }
.skin .nm { font-weight: 800; font-size: 17px; line-height: 1.1; min-height: 38px; display: flex; align-items: center; text-align: center; }
.skin .btn { font-size: 17px; padding: 4px 14px 6px; }
.skin.on { background: #fff5d6; box-shadow: 0 0 0 4px var(--mango); }

/* ---------- missions */
.mission-list { display: flex; flex-direction: column; gap: 10px; min-width: 520px; text-align: left; }
.mrow { background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 8px 14px; display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.mrow .nm { font-weight: 800; font-size: 19px; }
.mrow .rw { font-weight: 800; color: #c2410c; }
.mrow .bar { grid-column: span 2; height: 12px; border-radius: 6px; background: #eadcc0; overflow: hidden; }
.mrow .bar i { display: block; height: 100%; background: var(--mango); }
.mrow.done { background: #eaf8e2; }
.mrow.done .bar i { background: var(--lime); }
.streak-line { font-weight: 800; color: #c2410c; margin: 12px 0 0; }

/* ---------- settings */
.set-list { display: flex; flex-direction: column; gap: 12px; min-width: 460px; margin: 12px 0; }
.set-row { display: flex; justify-content: space-between; align-items: center; gap: 20px; font-size: 22px; font-weight: 700; background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 6px 14px; }
.toggle { font: inherit; font-weight: 800; font-size: 18px; min-width: 90px; border: 3px solid var(--ink); border-radius: 14px; padding: 2px 12px; cursor: pointer; background: var(--lime); color: #fff; }
.toggle.off { background: #cbbfa8; color: var(--ink); }
select { font: inherit; font-size: 18px; font-weight: 700; border: 3px solid var(--ink); border-radius: 12px; padding: 4px 8px; background: var(--paper); color: var(--ink); max-width: 260px; }

/* ---------- howto */
.howto { text-align: left; font-size: 20px; line-height: 1.4; margin: 8px 0; padding-left: 28px; }
.howto li { margin: 5px 0; }
.keys { color: #7a5a3a; font-size: 17px; }

/* ---------- result */
.modal.result { width: 640px; }
.joke { margin: 0 0 6px; font-size: 19px; color: #7a5a3a; font-style: italic; }
.big-score { display: flex; flex-direction: column; align-items: center; margin: 4px 0 6px; }
.big-score small { font-size: 18px; font-weight: 800; color: #9a5d1a; text-transform: uppercase; }
.big-score b { font-size: 66px; line-height: 1; color: var(--ink); text-shadow: 3px 3px 0 #ffd36a; }
.newbest { background: var(--dragon); color: #fff; border: 3px solid var(--ink); border-radius: 14px; padding: 0 12px; font-weight: 800; animation: wobble .6s ease-in-out infinite; margin-top: 6px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 0; }
.stats div { background: #fff; border: 3px solid var(--ink); border-radius: 14px; padding: 4px 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 76px; }
.stats b { font-size: 22px; line-height: 1.1; }
.stats small { color: #7a5a3a; font-size: 13px; line-height: 1.1; }
.stats canvas { width: 40px; height: 40px; }
.reward { font-size: 28px; font-weight: 800; color: #c2410c; }
.result-extra { font-size: 17px; font-weight: 700; color: var(--river); display: flex; flex-direction: column; gap: 2px; }

#rotate { display: none; position: fixed; inset: auto 0 0 0; padding: 10px; text-align: center; background: var(--paper); color: var(--ink); font-weight: 800; border-top: 3px solid var(--ink); z-index: 10; }
@media (orientation: portrait) and (max-width: 900px) and (pointer: coarse) { #rotate { display: block; } }
