:root {
  --ink: #1c1b33;
  --paper: #fff8ea;
  --yellow: #ffd23f;
  --water: #cdeefa;
  --stand: #2b2547;
  --stand-line: #463e72;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--stand);
  color: var(--ink);
  font-family: 'Fredoka', ui-rounded, 'SF Pro Rounded', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.noscript {
  position: absolute;
  top: 40%;
  left: 50%;
  z-index: 3;
  width: min(420px, calc(100vw - 32px));
  transform: translate(-50%, -50%);
  margin: 0;
  padding: 16px 20px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 4px 0 var(--ink);
  font-size: 17px;
  font-weight: 600;
  text-align: center;
}

/* ---------- tank ---------- */

#tank {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #3d93d6;
  overflow: hidden;
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: default;
}
body[data-mode="feed"] #scene { cursor: crosshair; }
body[data-mode="tap"] #scene { cursor: pointer; }

#scene.shake { animation: shake 0.28s linear; }
@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-4px, 1px); }
  40% { transform: translate(3px, -1px); }
  60% { transform: translate(-2px, 1px); }
  80% { transform: translate(1px, 0); }
}

.ray { animation: ray 9s ease-in-out infinite alternate; }
@keyframes ray { from { opacity: 0.3; } to { opacity: 1; } }

#scene text { font-family: 'Fredoka', ui-rounded, system-ui, sans-serif; font-weight: 700; }

/* ---------- toast ---------- */

#toast {
  position: absolute;
  top: 22px;
  left: 50%;
  max-width: calc(100vw - 32px);
  transform: translate(-50%, -12px);
  opacity: 0;
  pointer-events: none;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 999px;
  box-shadow: 0 3px 0 var(--ink);
  padding: 7px 16px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  transition: opacity 0.25s, transform 0.25s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- stand / toolbar ---------- */

#stand {
  position: relative;
  flex: none;
  height: calc(80px + env(safe-area-inset-bottom));
  padding: 0 16px env(safe-area-inset-bottom);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--stand);
  border-top: 6px solid var(--ink);
  z-index: 2;
}

.plaque {
  position: absolute;
  left: 22px;
  top: 50%;
  transform: translateY(-50%);
  margin-top: calc(env(safe-area-inset-bottom) / -2);
  padding: 4px 14px 5px;
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: 10px;
  box-shadow: 0 3px 0 var(--ink);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* The maker's mark: stamped under the plaque on wide screens; on narrow ones,
   where the plaque is hidden, along the foot of the stand */
.maker {
  position: absolute;
  left: 22px;
  bottom: calc(2px + env(safe-area-inset-bottom));
  padding: 6px 0;
  font: 600 11px/1 'Fredoka', ui-rounded, system-ui, sans-serif;
  letter-spacing: 0.02em;
  color: #c4bdf0;
  text-decoration: none;
  white-space: nowrap;
}
.maker:hover { text-decoration: underline; }
.maker:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
@media (min-width: 761px) { .plaque { margin-top: calc(-10px - env(safe-area-inset-bottom) / 2); } }
@media (max-width: 760px) {
  #stand { height: calc(100px + env(safe-area-inset-bottom)); padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
  .maker { left: 50%; transform: translateX(-50%); bottom: calc(0px + env(safe-area-inset-bottom)); padding: 4px 0; }
}
@media (max-width: 760px) { .plaque { display: none; } }

/* Quit mirrors the plaque on the right; on narrow screens, where the plaque is
   hidden, it joins the end of the toolbar */
#btn-quit {
  position: absolute;
  right: 22px;
  top: 50%;
  margin-top: calc(-28px - env(safe-area-inset-bottom) / 2);
  text-decoration: none;
}
.sep-quit { display: none; }
@media (max-width: 760px) {
  #btn-quit { position: static; margin-top: 0; }
  .sep-quit { display: block; }
}

.tools { display: flex; align-items: center; gap: 8px; }

.sep {
  width: 3px;
  height: 40px;
  margin: 0 4px;
  border-radius: 2px;
  background: var(--stand-line);
}

.tool {
  appearance: none;
  -webkit-appearance: none;
  width: 60px;
  height: 56px;
  padding: 3px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  font: 600 12px/1 'Fredoka', ui-rounded, system-ui, sans-serif;
  box-shadow: 0 4px 0 var(--ink);
  transform: translateY(-2px);
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s, background-color 0.15s;
}
.tool:active { transform: translateY(2px); box-shadow: 0 0 0 var(--ink); }
.tool:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }
.tool[aria-pressed="true"], .tool[aria-expanded="true"] { background: var(--yellow); }
.tool svg {
  width: 30px;
  height: 30px;
  stroke: var(--ink);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#btn-lamp .ic-moon, #btn-lamp[aria-pressed="true"] .ic-sun { display: none; }
#btn-lamp[aria-pressed="true"] .ic-moon { display: inline; }
#btn-sound .ic-on, #btn-sound[aria-pressed="true"] .ic-off { display: none; }
#btn-sound[aria-pressed="true"] .ic-on { display: inline; }

@media (max-width: 440px) {
  .tools { gap: 5px; }
  .tool { width: 48px; }
  .sep { margin: 0 1px; }
}

/* ---------- palette ---------- */

#palette {
  position: absolute;
  bottom: calc(100% + 14px);
  left: 50%;
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 140px);
  overflow-y: auto;
  padding: 12px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 5px 0 var(--ink);
}
#palette[hidden] { display: none; }

.palette-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 2px 10px;
  font-size: 16px;
}
.palette-head .close {
  width: 30px;
  height: 30px;
  border: 2.5px solid var(--ink);
  border-radius: 50%;
  background: #fff;
  font: 700 18px/1 'Fredoka', system-ui, sans-serif;
  color: var(--ink);
  cursor: pointer;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
@media (max-width: 520px) { .palette-grid { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 360px) { .palette-grid { grid-template-columns: repeat(3, 1fr); } }

.critter {
  position: relative;
  appearance: none;
  padding: 4px 2px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: 2.5px solid var(--ink);
  border-radius: 12px;
  background: var(--water);
  color: var(--ink);
  font: 600 12px/1.1 'Fredoka', ui-rounded, system-ui, sans-serif;
  cursor: pointer;
  transition: transform 0.08s, background-color 0.15s;
}
.critter:hover { background: #b5e4f7; }
.critter:active { transform: scale(0.95); }
.critter:focus-visible { outline: 3px solid var(--yellow); outline-offset: 2px; }
.critter:disabled { opacity: 0.4; cursor: not-allowed; }
.critter svg { width: 100%; height: 46px; overflow: visible; }
.critter .count {
  position: absolute;
  top: -7px;
  right: -7px;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--yellow);
  font-size: 11px;
  font-weight: 700;
}
.critter .count:empty { display: none; }

@media (prefers-reduced-motion: reduce) {
  #scene.shake { animation: none; }
  .ray { animation: none; }
}

/* ---------- a packet in the treasure chest ---------- */

.packet {
  position: absolute;
  z-index: 3;
  width: 34px;
  height: 24px;
  margin: -12px 0 0 -17px;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 4px;
  background: linear-gradient(135deg, #fff1c9 0 50%, #e8cf8a 50%);
  box-shadow: 0 3px 0 var(--ink);
  cursor: pointer;
  animation: packet-bob 1.6s ease-in-out infinite alternate;
}
.packet::after { content: ''; position: absolute; inset: -10px; }
.packet:focus-visible { outline: 3px solid var(--yellow); outline-offset: 4px; }
@keyframes packet-bob { to { transform: translateY(-6px) rotate(-6deg); } }
.packet-note {
  max-width: min(92vw, 440px);
  padding: 20px 20px 16px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--ink);
  font: 500 16px/1.45 'Fredoka', ui-rounded, system-ui, sans-serif;
}
.packet-note::backdrop { background: rgba(28, 27, 51, 0.55); }
.packet-note h2 { margin: 0 0 8px; font-size: 20px; }
.packet-note p { margin: 0 0 10px; }
.packet-numbers {
  font-family: 'Courier New', monospace;
  font-size: 15px;
  line-height: 1.6;
  word-spacing: 0.3em;
}
.tool-text {
  padding: 8px 16px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: var(--yellow);
  color: var(--ink);
  font: 600 15px 'Fredoka', ui-rounded, system-ui, sans-serif;
  box-shadow: 0 3px 0 var(--ink);
  cursor: pointer;
}
@media (prefers-reduced-motion: reduce) {
  .packet { animation: none; }
}
