:root {
  --ink: #3a2a22;
  --paper: #fffaf0;
  --pink: #ff6fae;
  --purple: #a45ee5;
  --font: "Gochi Hand", "Comic Sans MS", "Chalkboard SE", cursive;
  /* wobbly hand-drawn borders */
  --wobble: 255px 15px 225px 15px / 15px 225px 15px 255px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #ecd2a2; color: var(--ink); font-family: var(--font);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: none; overscroll-behavior: none;
}
canvas#game { position: fixed; top: 0; left: 0; right: 0; bottom: 0; display: block; touch-action: none; }
button { font-family: inherit; color: inherit; cursor: pointer; }

.btn {
  width: 48px; height: 48px; font-size: 24px; line-height: 1;
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble);
  box-shadow: 2px 3px 0 rgba(58, 42, 34, 0.35);
  display: grid; place-items: center; padding: 0;
}
.btn:active { transform: translate(1px, 2px); box-shadow: 1px 1px 0 rgba(58, 42, 34, 0.35); }
.btn.off { opacity: 0.45; }
.btn.on { background: #9fdcff; transform: scale(1.12) rotate(-6deg); }
body.cleaning #tools { opacity: 0.6; }
.btn.bump { animation: bump 0.6s ease; }
@keyframes bump { 30% { transform: scale(1.3) rotate(-8deg); } 60% { transform: scale(0.95) rotate(4deg); } }

#top {
  position: fixed; left: 0; right: 0; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  pointer-events: none;
}
#top > * { pointer-events: auto; }
#top .right { display: flex; gap: 8px; }
#count {
  font-size: 20px; line-height: 1.05; padding: 3px 12px 4px; text-align: center;
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble);
  white-space: nowrap; display: grid; min-width: 0; overflow: hidden;
}
#count span { overflow: hidden; text-overflow: ellipsis;
}
#count .best { font-size: 14px; color: #b0602a; }
#count.record { animation: record 1.2s ease; }
@keyframes record { 25% { transform: scale(1.18) rotate(-3deg); background: #ffe14a; } 60% { background: #ffe14a; } }

/* The toolbar: centered when the tools fit, and swipes sideways when they don't (narrow phones). */
#tools {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 2;
  display: flex; gap: 6px;
  padding: 10px max(10px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 18px) max(10px, env(safe-area-inset-left));
  overflow-x: auto; overflow-y: hidden; touch-action: pan-x; scrollbar-width: none;
  pointer-events: none;
}
#tools::-webkit-scrollbar { display: none; }
#tools.scrolls { pointer-events: auto; }
.tool:first-child { margin-left: auto; }
.tool:last-child { margin-right: auto; }
.tool {
  pointer-events: auto;
  flex: 0 0 62px; min-width: 0; height: 78px; padding: 4px 2px 2px;
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble);
  box-shadow: 2px 3px 0 rgba(58, 42, 34, 0.35);
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  transition: transform 0.15s;
}
.tool canvas { width: 52px; height: 52px; }
.tool span { font-size: 14px; line-height: 1; white-space: nowrap; }
.tool.on { background: #ffe14a; transform: translateY(-8px) rotate(-3deg); }
/* uses left: a little bar that drains as you use the tool and slowly fills back up */
.tool { position: relative; }
.tool .meter { position: absolute; left: 6px; right: 6px; bottom: -7px; height: 7px; border: 2px solid var(--ink); border-radius: 4px; background: #fffaf0; overflow: hidden; }
.tool .meter b { display: block; height: 100%; width: 100%; background: #5cc25a; }
.tool.empty { filter: grayscale(1); opacity: 0.6; }
.tool.empty .meter b { background: #e8505a; }
.tool.shake { animation: shake 0.4s; }
@keyframes shake { 20%, 60% { transform: translateX(-5px) rotate(-4deg); } 40%, 80% { transform: translateX(5px) rotate(4deg); } }

/* Bug Hunt challenge card, under the top bar */
#challenge {
  position: fixed; z-index: 2; left: 50%; transform: translateX(-50%);
  top: calc(env(safe-area-inset-top) + 66px); width: min(360px, calc(100% - 32px));
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; column-gap: 8px; row-gap: 3px;
  padding: 4px 12px 6px; font-size: 17px; line-height: 1.15;
  background: rgba(255, 250, 240, 0.92); border: 3px solid var(--ink); border-radius: var(--wobble);
  pointer-events: none;
}
#challenge[hidden] { display: none; }
#challenge .label { font-size: 14px; color: var(--purple); white-space: nowrap; }
#challenge .count { font-variant-numeric: tabular-nums; }
#challenge .bar { grid-column: 1 / -1; height: 7px; border: 2px solid var(--ink); border-radius: 4px; overflow: hidden; background: #fff; }
#challenge .bar b { display: block; height: 100%; width: 0; background: var(--pink); transition: width 0.25s; }
#challenge.done { animation: challengeDone 1.2s ease; }
@keyframes challengeDone { 20% { transform: translateX(-50%) scale(1.12); background: #c8f5a0; } 60% { background: #c8f5a0; } }

/* A new sticker: a small badge that slides in just above the toolbar on the left, then fades */
#stickerPop {
  position: fixed; z-index: 4; left: max(12px, env(safe-area-inset-left)); bottom: calc(env(safe-area-inset-bottom) + 112px);
  display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; column-gap: 6px; align-items: center;
  padding: 3px 10px 3px 6px; max-width: 60vw;
  background: #fffaf0; border: 2px solid var(--ink); border-radius: 12px;
  pointer-events: none; opacity: 0; transform: translateX(-120%);
}
#stickerPop .icon { grid-row: 1 / 3; font-size: 26px; line-height: 1; }
#stickerPop .name { font-size: 15px; line-height: 1.1; }
#stickerPop .new { font-size: 12px; line-height: 1; color: var(--purple); grid-row: 1; grid-column: 2; }
#stickerPop .name { grid-row: 2; grid-column: 2; }
#stickerPop.show { animation: stickerPop 2.2s ease forwards; }
@keyframes stickerPop {
  0% { opacity: 0; transform: translateX(-120%); }
  12% { opacity: 1; transform: translateX(0); }
  80% { opacity: 1; transform: translateX(0); }
  100% { opacity: 0; transform: translateX(0); }
}

#toasts {
  position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); top: calc(env(safe-area-inset-top) + 124px); z-index: 3;
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none;
}
.toast {
  font-size: 18px; padding: 4px 14px; max-width: 100%; text-align: center;
  background: var(--paper); border: 3px solid var(--ink); border-radius: var(--wobble);
  animation: toast 2.6s ease forwards;
}
.toast.new { background: #c8f5a0; }
.toast.boss { background: #ffd0a0; font-size: 21px; }
.toast.rush { font-size: 28px; color: #fff; background: linear-gradient(90deg, var(--pink), var(--purple), #5ab8ff); -webkit-text-stroke: 1px var(--ink); }
@keyframes toast {
  0% { opacity: 0; transform: scale(0.4) rotate(-6deg); }
  12% { opacity: 1; transform: scale(1.1) rotate(2deg); }
  20% { transform: scale(1) rotate(0); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-20px); }
}
body.rush #count { background: linear-gradient(90deg, #ffd1ec, #e3c8ff); }

.overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 5;
  /* flex + margin:auto centers the card, but if the screen is too short it scrolls instead of cutting it off */
  display: flex; flex-direction: column; overflow-y: auto; padding: 16px; touch-action: pan-y;
  background: rgba(58, 42, 34, 0.35);
}
.overlay.hidden { display: none; }
.start-card {
  margin: auto; flex-shrink: 0;
  width: min(420px, 100%); text-align: center; padding: 20px 20px 24px;
  background: var(--paper); border: 4px solid var(--ink); border-radius: var(--wobble);
  box-shadow: 4px 6px 0 rgba(58, 42, 34, 0.4);
}
.start-card .logo { width: 120px; height: 120px; animation: wiggle 2s ease-in-out infinite; }
@keyframes wiggle { 50% { transform: rotate(-6deg) scale(1.05); } }
.start-card h1 { font-size: 52px; margin: 4px 0; color: var(--pink); -webkit-text-stroke: 1.5px var(--ink); letter-spacing: 1px; }
.start-card p { font-size: 21px; margin: 8px 0 18px; line-height: 1.3; }
#btnStart {
  font-size: 30px; padding: 10px 28px;
  background: #ffe14a; border: 4px solid var(--ink); border-radius: var(--wobble);
  box-shadow: 3px 4px 0 rgba(58, 42, 34, 0.4);
}
#btnStart:active { transform: translate(2px, 3px); box-shadow: 1px 1px 0 rgba(58, 42, 34, 0.4); }

.book-inner {
  margin: auto; min-height: 0;
  width: min(720px, 100%); max-height: 100%;
  display: flex; flex-direction: column;
  background: var(--paper); border: 4px solid var(--ink); border-radius: 18px;
  box-shadow: 4px 6px 0 rgba(58, 42, 34, 0.4);
  overflow: hidden;
}
.book-inner header {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px 8px 18px;
  border-bottom: 3px dashed #c9b48e;
}
.book-inner h2 { margin: 0; font-size: 32px; color: var(--purple); }
#bookCount { flex: 1; font-size: 18px; }
.book-inner .tabs { display: flex; gap: 8px; padding: 8px 12px 0; }
.book-inner .tabs button {
  font-size: 19px; padding: 4px 14px; min-height: 44px; background: #f1ece2;
  border: 3px solid var(--ink); border-radius: var(--wobble);
}
.book-inner .tabs button.on { background: #ffe14a; }
.sticker {
  border: 3px solid var(--ink); border-radius: 18px; padding: 8px; background: #fff; text-align: center;
}
.sticker .icon { font-size: 54px; line-height: 1.1; }
.sticker .name { font-size: 19px; line-height: 1.1; }
.sticker .info { font-size: 14px; color: #8a6a4a; margin-top: 3px; }
.sticker.locked { background: #f1ece2; }
.sticker.locked .icon { color: #b8a890; font-size: 44px; }
.sticker.locked .name { color: #8a7a6a; }
#bookGrid {
  overflow-y: auto; touch-action: pan-y; padding: 12px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px;
}
.card {
  border: 3px solid var(--ink); border-radius: var(--wobble); padding: 6px 8px 8px;
  background: #fff; text-align: center;
}
.card canvas { width: 84px; height: 84px; }
.card .name { font-size: 20px; line-height: 1.1; }
.card .info { font-size: 15px; line-height: 1.2; margin-top: 4px; }
.card .num { font-size: 15px; color: #8a6a4a; margin-top: 4px; }.card.unknown { background: #f1ece2; }
.card.unknown .name { color: #8a7a6a; }
.card.boss { background: #fff1de; border-width: 4px; }

.start-card .hint { font-size: 20px; color: var(--purple); margin: 0; animation: wiggle 1.5s ease-in-out infinite; }
.start-card .hint[hidden] { display: none; }

/* short screens (phones turned sideways, small phones): a smaller start card */
@media (max-height: 640px) {
  .start-card { padding: 12px 16px 16px; }
  .start-card .logo { width: 72px; height: 72px; }
  .start-card h1 { font-size: 38px; margin: 0; }
  .start-card p { font-size: 17px; margin: 4px 0 10px; }
  #btnStart { font-size: 24px; padding: 6px 22px; }
}
@media (max-height: 440px) {
  .start-card { width: min(560px, 100%); }
  .start-card .logo { width: 52px; height: 52px; }
  .start-card h1 { font-size: 30px; }
  .start-card p { font-size: 15px; }
  #challenge { top: calc(env(safe-area-inset-top) + 60px); }
}
/* narrow phones: "Rolling Pin" is too long for its button, so it shows "Pin" */
@media (max-width: 400px) {
  .tool span[data-short] { font-size: 0; }
  .tool span[data-short]::after { content: attr(data-short); font-size: 12px; }
}

@media (max-width: 380px) {
  .tool { height: 74px; }
  .tool span { font-size: 12px; }
  .tool canvas { width: 46px; height: 46px; }
  #count { font-size: 18px; padding: 4px 10px; }
  .btn { width: 42px; height: 42px; font-size: 20px; }
}
