:root {
  --ink: #4a2412;
  --cream: #fff6e4;
  --saffron: #f28a1c;
  --pink: #e2357f;
  --green: #4f9e1d;
  --panel: rgba(255, 246, 228, 0.94);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #f6c27a; }
body {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink); touch-action: none; user-select: none; -webkit-user-select: none;
}
#bg {
  position: fixed; inset: 0; background: #f6c27a url(img/bg_wide.webp) center bottom / cover no-repeat;
}
@media (max-aspect-ratio: 1/1) { #bg { background-image: url(img/bg_tall.webp); } }
#cv { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
button svg { width: 22px; height: 22px; fill: currentColor; }

#top {
  position: fixed; top: 0; left: 0; right: 0; height: 64px; padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  display: flex; align-items: center; justify-content: space-between; gap: 8px; pointer-events: none;
}
#top > * { pointer-events: auto; }
.pill, .round {
  background: var(--panel); border: 2.5px solid var(--ink); box-shadow: 0 3px 0 var(--ink);
  border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-weight: 800; transition: transform .08s;
}
.pill { height: 42px; padding: 0 14px 0 11px; }
.round { width: 42px; height: 42px; }
.pill:active, .round:active, .tool:active, .btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
#title { text-align: center; line-height: 1.05; flex: 1; min-width: 0; }
#title h1 {
  margin: 0; font-size: 21px; font-weight: 900; letter-spacing: .3px; color: #fff;
  text-shadow: -2px -2px 0 var(--ink), 2px -2px 0 var(--ink), -2px 2px 0 var(--ink), 2px 2px 0 var(--ink), 0 3px 0 var(--ink);
  white-space: nowrap;
}
#title h1 em { font-style: normal; color: #ffd23f; }
#lvl {
  display: inline-block; margin-top: 4px; background: var(--ink); color: #ffe7b8; font-weight: 800; font-size: 14px;
  padding: 2px 12px; border-radius: 999px;
}
#right { display: flex; align-items: center; gap: 7px; }
#hearts { display: flex; gap: 2px; margin-right: 4px; }
#hearts svg { width: 24px; height: 24px; transition: transform .25s, opacity .25s; }
#hearts svg.lost { opacity: .35; transform: scale(.8); }
#hearts svg.pop { animation: pop .45s; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.5) rotate(-12deg); } 100% { transform: scale(.8); } }

#tools {
  position: fixed; left: 0; right: 0; bottom: 0; height: 84px; padding: 10px 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  display: flex; justify-content: center; align-items: center; gap: 12px; pointer-events: none;
}
.tool {
  pointer-events: auto; min-width: 92px; height: 54px; padding: 0 14px; border-radius: 18px;
  background: var(--panel); border: 2.5px solid var(--ink); box-shadow: 0 4px 0 var(--ink);
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-weight: 800; font-size: 16px;
}
.tool svg { width: 24px; height: 24px; }
#btnHint { background: #ffd23f; }
.tool[disabled] { opacity: .5; }

#toast {
  position: fixed; left: 50%; top: 82px; transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  background: var(--ink); color: #fff3d6; font-weight: 800; padding: 8px 16px; border-radius: 999px; font-size: 15px;
  transition: opacity .25s, transform .25s; white-space: nowrap; max-width: calc(100vw - 24px); overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: #b3261e; }

.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: rgba(74, 36, 18, 0.35); z-index: 5; animation: fade .25s;
}
.overlay.hidden { display: none; }
@keyframes fade { from { opacity: 0; } }
.card {
  position: relative; width: min(380px, 100%); background: var(--cream); border: 3px solid var(--ink);
  box-shadow: 0 6px 0 var(--ink); border-radius: 24px; padding: 34px 20px 20px; text-align: center;
  animation: rise .35s cubic-bezier(.2, 1.4, .4, 1);
}
.card.wide { width: min(560px, 100%); max-height: calc(100% - 16px); display: flex; flex-direction: column; padding-top: 20px; }
@keyframes rise { from { transform: translateY(30px) scale(.9); opacity: 0; } }
.card h2 { margin: 0 0 6px; font-size: 26px; font-weight: 900; }
.card p { margin: 6px 0 14px; font-size: 15px; line-height: 1.4; }
.ribbon {
  position: absolute; top: -20px; left: 50%; transform: translateX(-50%) rotate(-3deg);
  background: var(--saffron); color: #fff; font-weight: 900; font-size: 20px; padding: 6px 22px; border-radius: 12px;
  border: 3px solid var(--ink); box-shadow: 0 3px 0 var(--ink); white-space: nowrap;
}
.ribbon.sad { background: #7b6cc9; }
.stars { display: flex; justify-content: center; gap: 8px; margin: 8px 0 4px; }
.stars i { width: 56px; height: 56px; display: block; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.5l2.9 6 6.6.8-4.9 4.6 1.3 6.5L12 17.2l-5.9 3.2 1.3-6.5L2.5 9.3l6.6-.8z' fill='%23e8d6b8' stroke='%234a2412' stroke-width='1.6' stroke-linejoin='round'/%3E%3C/svg%3E") center/contain no-repeat; }
.stars i:nth-child(2) { transform: translateY(-8px) scale(1.12); }
.stars i.on { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.5l2.9 6 6.6.8-4.9 4.6 1.3 6.5L12 17.2l-5.9 3.2 1.3-6.5L2.5 9.3l6.6-.8z' fill='%23ffc61a' stroke='%234a2412' stroke-width='1.6' stroke-linejoin='round'/%3E%3C/svg%3E"); animation: starIn .4s cubic-bezier(.2, 1.6, .4, 1) both; }
.stars i.on:nth-child(2) { animation-name: starIn2; }
@keyframes starIn { from { transform: scale(0) rotate(-40deg); } }
@keyframes starIn2 { from { transform: translateY(-8px) scale(0) rotate(-40deg); } }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 6px; }
.btn {
  min-height: 50px; padding: 0 20px; border-radius: 16px; font-weight: 900; font-size: 17px; color: #fff;
  background: var(--green); border: 3px solid var(--ink); box-shadow: 0 4px 0 var(--ink);
}
.btn.ghost { background: #fff; color: var(--ink); }

#starTotal { margin: 0 0 10px; font-weight: 800; }
#grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 9px; overflow-y: auto;
  padding: 4px 4px 10px; touch-action: pan-y; flex: 1; min-height: 0;
}
.lv {
  position: relative; height: 66px; border-radius: 14px; border: 2.5px solid var(--ink); background: #ffe2a8;
  box-shadow: 0 3px 0 var(--ink); font-weight: 900; font-size: 20px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
}
.lv.done { background: #ffd23f; }
.lv.cur { background: var(--saffron); color: #fff; }
.lv.locked { background: #d9c6a6; color: #8a6d55; box-shadow: none; transform: translateY(3px); cursor: default; }
.lv small { font-size: 11px; letter-spacing: 1px; color: var(--ink); }
.lv.cur small { color: #fff; }
.lv svg { width: 18px; height: 18px; fill: #8a6d55; }

@media (max-width: 480px) {
  #title h1 { font-size: 16px; }
  #lvl { font-size: 12px; }
  .pill span { display: none; }
  .pill { width: 42px; padding: 0; }
  #hearts svg { width: 20px; height: 20px; }
  #right { gap: 5px; }
  .round { width: 38px; height: 38px; }
  .tool { min-width: 0; flex: 1; max-width: 120px; font-size: 15px; }
}
@media (max-height: 520px) {
  #tools { height: 70px; }
  .tool { height: 46px; }
}
