@font-face {
  font-family: 'Pixelify';
  src: url('fonts/PixelifySans.ttf') format('truetype');
  font-weight: 400 700;
  font-display: block;
}

:root {
  --sky: #bfe6ff;
  --sky-deep: #a6dcff;
  --paper: #ffffff;
  --board: #f7f5fc;
  --ink: #3b3355;
  --ink-soft: #8a84a3;
  --grey: #ebe9f1;
  --hot: #c7bde6;
  --coral: #ff7a59;
  --coral-deep: #e85f3f;
  --gold: #ffc21a;
  --num: ui-rounded, 'SF Pro Rounded', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

/* iOS ignores user-scalable=no, so page zoom, double-tap zoom, rubber-banding,
   text selection and the long-press callout are all switched off here. The
   library re-enables vertical panning for its own scroller. */
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  background: var(--sky);
  color: var(--ink);
  font-family: var(--num);
}
body { position: fixed; inset: 0; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  padding: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

[hidden] { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* The pixel-checker sky behind every screen. */
.sky {
  background-color: var(--sky);
  background-image:
    linear-gradient(45deg, var(--sky-deep) 25%, transparent 25%, transparent 75%, var(--sky-deep) 75%),
    linear-gradient(45deg, var(--sky-deep) 25%, transparent 25%, transparent 75%, var(--sky-deep) 75%);
  background-size: 48px 48px;
  background-position: 0 0, 24px 24px;
}

/* ---- logo: chunky pixel letters with a white outline and a soft drop ---- */
.logo {
  margin: 0;
  font-family: 'Pixelify', var(--num);
  font-weight: 700;
  font-size: clamp(56px, 13vmin, 120px);
  line-height: 1;
  letter-spacing: 0.02em;
  color: #6a5cff;
  -webkit-text-stroke: 0.12em #fff;
  paint-order: stroke fill;
  text-shadow: 0 0.09em 0 #4b3fcc;
  white-space: nowrap;
}
.logo span:last-child { color: var(--coral); }
.logo.small { font-size: clamp(34px, 6vmin, 52px); }

/* ---- title screen ---- */
#title {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4vmin;
}
#title-art { display: flex; gap: 5vmin; align-items: flex-end; }
#title-art canvas { width: min(18vmin, 150px); height: min(18vmin, 150px); animation: bob 3s ease-in-out infinite; }
#title-art canvas:nth-child(2) { width: min(26vmin, 210px); height: min(22vmin, 180px); animation-delay: -1s; }
#title-art canvas:nth-child(3) { animation-delay: -2s; }
@keyframes bob { 50% { transform: translateY(-8px); } }
.play {
  width: clamp(120px, 22vmin, 180px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--coral);
  box-shadow: 0 10px 0 var(--coral-deep), 0 14px 24px rgba(59, 51, 85, 0.25);
  border: 6px solid #fff;
  animation: breathe 2.4s ease-in-out infinite;
}
.play svg { width: 62%; fill: #fff; margin-left: 6%; }
.play:active { transform: translateY(6px); box-shadow: 0 4px 0 var(--coral-deep); animation: none; }
@keyframes breathe { 50% { transform: scale(1.05); } }

/* ---- library ---- */
#library { display: flex; flex-direction: column; }
.lib-head { flex: none; display: flex; justify-content: center; padding: 14px 16px 6px; }
#shelves {
  flex: 1;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding: 8px max(16px, 3vw) 40px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: flex-start;
  gap: 22px 36px;
}
/* Short shelves sit side by side; a long one wraps its tiles across the width. */
.shelf { max-width: 100%; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 6px 20px 6px 8px;
  border-radius: 999px;
  border: 4px solid #fff;
  font-family: 'Pixelify', var(--num);
  font-weight: 700;
  font-size: 26px;
  box-shadow: 0 4px 0 rgba(59, 51, 85, 0.18);
  margin: 0 0 12px 4px;
}
.pill canvas { width: 40px; height: 40px; }
.tiles { display: flex; flex-wrap: wrap; gap: 16px; }
.tile {
  position: relative;
  width: clamp(118px, 19vmin, 170px);
  aspect-ratio: 1;
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 6px 0 rgba(59, 51, 85, 0.16);
  display: grid;
  place-items: center;
}
.tile:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(59, 51, 85, 0.16); }
.tile canvas { width: 80%; height: 80%; }
.tile .star {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 44px;
  height: 44px;
}

.ribbon {
  position: absolute;
  top: 10px;
  left: -8px;
  padding: 3px 10px 4px;
  border-radius: 8px;
  background: var(--coral);
  color: #fff;
  font-family: 'Pixelify', var(--num);
  font-weight: 700;
  font-size: 17px;
  transform: rotate(-12deg);
  box-shadow: 0 3px 0 var(--coral-deep);
}

/* ---- My Gallery ---- */
#gallery {
  flex: 1;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding: 12px max(16px, 3vw) 40px;
}
#gallery .frames, #gallery .tiles { justify-content: center; margin-bottom: 26px; }
.frames { display: flex; flex-wrap: wrap; gap: 26px; }
.tile.big {
  width: clamp(170px, 30vmin, 250px);
  border: 10px solid #ffe6a8;
  outline: 4px solid #e8b94a;
  border-radius: 14px;
}
#gallery .empty { display: grid; place-items: center; height: 70%; }
#gallery .empty svg { width: 40vmin; fill: none; stroke: rgba(59, 51, 85, 0.25); stroke-width: 2.5; stroke-dasharray: 4 3; }

/* ---- tab bar under the library ---- */
.tabbar {
  flex: none;
  display: flex;
  justify-content: center;
  gap: 28px;
  padding: 8px 0 14px;
}
.tab {
  width: 84px;
  height: 64px;
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 5px 0 rgba(59, 51, 85, 0.16);
  display: grid;
  place-items: center;
  opacity: 0.75;
}
.tab svg { width: 44px; fill: none; stroke: var(--ink); stroke-width: 3.5; stroke-linejoin: round; stroke-linecap: round; }
.tab.on { opacity: 1; background: var(--coral); box-shadow: 0 5px 0 var(--coral-deep); }
.tab.on svg { stroke: #fff; }
.tab:active { transform: translateY(3px); }

/* ---- board screen ---- */
#board-screen {
  display: grid;
  /* minmax(0, ...) so the canvas's pixel width can never widen the column */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: 64px minmax(0, 1fr) auto;
  grid-template-areas: 'top' 'board' 'chips';
}
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px 0;
}
.topbar .spacer { flex: 1; }
.round-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 4px 0 rgba(59, 51, 85, 0.18);
  display: grid;
  place-items: center;
}
.round-btn svg { width: 60%; fill: none; stroke: var(--ink); stroke-width: 4.5; stroke-linecap: round; stroke-linejoin: round; }
.round-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(59, 51, 85, 0.18); }
.round-btn.big { width: 92px; height: 92px; }
.round-btn.coral { background: var(--coral); box-shadow: 0 5px 0 var(--coral-deep); }
.round-btn.coral svg { stroke: #fff; }
#hint.pulse { animation: hint-glow 0.8s ease-in-out 2; }
@keyframes hint-glow { 50% { box-shadow: 0 0 0 8px rgba(255, 122, 89, 0.45); } }

#board-area { grid-area: board; position: relative; min-height: 0; min-width: 0; overflow: hidden; }
#board, #fx { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
#fx { pointer-events: none; }

#chips {
  grid-area: chips;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--gap, 10px);
  padding: 10px 12px 20px;
}
.chip {
  position: relative;
  width: var(--chip, 64px);
  height: var(--chip, 64px);
  flex: none;
  border-radius: 50%;
  background: var(--c);
  border: 4px solid var(--paper);
  box-shadow: 0 0 0 2px var(--ink), 0 5px 0 2px rgba(59, 51, 85, 0.22);
  display: grid;
  place-items: center;
  transition: transform 0.15s;
}
.chip .num {
  font-family: var(--num);
  font-weight: 800;
  font-size: calc(var(--chip, 64px) * 0.48);
  line-height: 1;
  color: var(--fg);
  -webkit-text-stroke: var(--stroke, 0) var(--ink);
  paint-order: stroke fill;
}
.chip svg { display: none; width: 62%; fill: none; stroke: var(--fg); stroke-width: 7; stroke-linecap: round; stroke-linejoin: round; }
.chip.done .num { display: none; }
.chip.done svg { display: block; }
.chip.sel { transform: scale(1.15); box-shadow: 0 0 0 2px var(--ink), 0 0 0 7px var(--coral), 0 6px 0 6px rgba(59, 51, 85, 0.18); z-index: 1; }
.chip.wiggle { animation: wiggle 0.5s; }
@keyframes wiggle {
  20% { transform: rotate(-14deg) scale(1.1); }
  40% { transform: rotate(12deg) scale(1.1); }
  60% { transform: rotate(-8deg) scale(1.05); }
  80% { transform: rotate(5deg); }
}

#finish {
  grid-area: chips;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 48px;
  padding: 6px 12px 20px;
  background: transparent;
  animation: rise 0.4s ease-out;
}
#board-screen.finished #chips, #board-screen.finished #hint, #board-screen.finished #restart { visibility: hidden; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }

/* Landscape: chips move to a column on the right so the board keeps the height. */
@media (min-aspect-ratio: 1/1) {
  #board-screen {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 64px minmax(0, 1fr);
    grid-template-areas: 'top top' 'board chips';
  }
  #chips { flex-direction: column; padding: 10px 24px 20px 12px; }
  #finish { flex-direction: column; gap: 32px; padding: 0 20px 20px 8px; }
}

/* The eraser sits after the colours, a white chip with a pink and blue eraser. */
.chip.eraser { background: #fff1cc; margin-left: calc(var(--gap, 10px) * 0.6); }
.chip .eraser-ico { display: grid; place-items: center; width: 72%; }
.chip .eraser-ico svg { display: block; width: 100%; stroke: var(--ink); stroke-width: 3; stroke-linejoin: round; }
@media (min-aspect-ratio: 1/1) { .chip.eraser { margin-left: 0; margin-top: calc(var(--gap, 10px) * 0.6); } }

/* ---- pop-ups: start over, what's new ---- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  background: rgba(59, 51, 85, 0.45);
}
.modal .card {
  background: var(--paper);
  border-radius: 30px;
  padding: 26px 26px 28px;
  box-shadow: 0 10px 0 rgba(59, 51, 85, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  animation: pop-in 0.35s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop-in { from { transform: scale(0.6); opacity: 0; } }
.modal .row { display: flex; gap: 48px; }
#confirm-pic { width: min(220px, 50vmin); height: min(220px, 50vmin); }
.news-title { font-size: clamp(40px, 8vmin, 60px); }
#news-items { display: flex; flex-direction: column; gap: 12px; width: 100%; }
.news-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px 10px 10px;
  border-radius: 22px;
  background: #f3f0fb;
  text-align: left;
  font-weight: 700;
  font-size: 19px;
  line-height: 1.25;
  transition: background 0.2s;
}
.news-item.on { background: #ffe3d8; box-shadow: inset 0 0 0 4px var(--coral); }
.news-item .ico { flex: none; width: 76px; height: 76px; display: grid; place-items: center; border-radius: 18px; background: var(--paper); }
.news-item .ico canvas { width: 64px; height: 64px; }
.news-item .ico svg { width: 56px; fill: none; stroke: var(--ink); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }

#stamp {
  position: fixed;
  right: calc(8px + var(--safe-r));
  bottom: calc(4px + var(--safe-b));
  font-size: 11px;
  color: rgba(59, 51, 85, 0.35);
  pointer-events: none;
}
