/* Pop Sudoku. The colours start from the original's (ColourDefine.h): its sky blue, menu blues,
   name-plate blue and level card's red. The stage is phone-shaped, as the game was: as tall as
   the window and at most three quarters as wide. A header across the top shows the level, time
   and stars, with the buttons; under it, the play area. pop-sudoku.js sets --W, --H (the play
   area's size), --board, --sb (the margin under the fruit) and --ml (the board's left margin). */

:root {
  --sky: rgb(133, 207, 235);           /* BACK_COLOUR */
  --menu-back: rgb(26, 51, 128);
  --menu-button: rgb(51, 128, 204);
  --plate: rgb(51, 77, 204);           /* LEVEL_NAME_PLATE_BACK_COLOUR */
  --desc: rgb(179, 26, 51);            /* LEVEL_PREVIEW_DESC_BACK_COLOUR */
  --progress: rgb(240, 232, 170);
  --gold: #ffd648;
  --ink: #16213a;
  --round: "ui-rounded", "SF Pro Rounded", "Nunito", "Arial Rounded MT Bold", "Trebuchet MS", Arial, sans-serif;
  --pop: cubic-bezier(0.2, 1.4, 0.4, 1);
}

html, body { margin: 0; height: 100%; }
body {
  /* Beyond the stage on a wide screen: more sky */
  background:
    radial-gradient(ellipse at 20% 15%, rgba(255, 255, 255, 0.55), transparent 40%),
    radial-gradient(ellipse at 85% 70%, rgba(255, 255, 255, 0.4), transparent 45%),
    linear-gradient(#5db4ea, #a8def5);
  overflow: hidden;
  font-family: var(--round);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.stage {
  position: relative;
  width: min(100vw, 75vh);
  width: min(100vw, 75dvh);
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: var(--sky);
  box-shadow: 0 0 40px rgba(10, 40, 100, 0.35);
  display: flex; flex-direction: column;
  container-type: inline-size;
}
.play { position: relative; flex: 1; min-height: 0; }
#canvas { position: absolute; inset: 0; display: block; }

/* Loading: the title and a bouncing apple, fading away once the game's ready */
.loading {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: linear-gradient(#3a6fe0, #1f47b0); color: #fff;
  transition: opacity 0.4s, visibility 0.4s;
}
.loading.done { opacity: 0; visibility: hidden; }
.loading img { width: 84px; animation: hop 0.7s ease-in-out infinite alternate; filter: drop-shadow(0 10px 8px rgba(0, 0, 40, 0.3)); }
.loading h1 { margin: 18px 0 6px; font-size: 36px; letter-spacing: 0.02em; text-shadow: 0 3px 0 rgba(0, 0, 40, 0.3); }
.loading p { margin: 0; opacity: 0.8; }
@keyframes hop { from { transform: translateY(0) scale(1.04, 0.96); } to { transform: translateY(-22px) scale(0.97, 1.03); } }

button { font: inherit; cursor: pointer; }
button.icon { background: none; border: none; padding: 0; line-height: 0; }
button.icon img { width: 100%; height: 100%; object-fit: contain; transition: transform 0.15s var(--pop); filter: drop-shadow(0 3px 3px rgba(0, 0, 40, 0.3)); }
button.icon:hover img, button.icon:focus-visible img { transform: scale(1.12); }
button.icon:active img { transform: scale(0.92); }
button:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }

/* The header: the level (its fruit, name, number and difficulty), the time and stars left, and
   the buttons for the map and the menu */
.top {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; min-height: 60px; box-sizing: border-box;
  background: linear-gradient(#2a4aa8, #1c3889); color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 2px 10px rgba(0, 10, 50, 0.35); position: relative; z-index: 1;
}
.top-fruit { width: 40px; height: 44px; object-fit: contain; flex: none; filter: drop-shadow(0 3px 3px rgba(0, 0, 40, 0.4)); }
.top-level { flex: 1; min-width: 0; line-height: 1.2; }
.top-name { font-weight: bold; font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 2px rgba(0, 0, 40, 0.5); }
.top-sub { font-size: 13px; color: #c6d4ff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-stats {
  flex: none; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px 10px; border-radius: 12px; background: rgba(0, 10, 50, 0.3);
}
.top-stats[hidden] { display: none; }
.top .time { font-weight: bold; font-size: 17px; font-variant-numeric: tabular-nums; }
.top .lives { display: flex; gap: 2px; height: 16px; }
.top .lives:empty { display: none; }
.top .lives img { height: 100%; filter: drop-shadow(0 1px 1px rgba(0, 0, 40, 0.4)); transition: filter 0.3s, opacity 0.3s; }
.top .lives img.gone { filter: grayscale(1) brightness(1.6); opacity: 0.35; }
.top .lives img.losing { animation: lose-star 0.6s ease-out; }
@keyframes lose-star {
  0% { transform: scale(1); filter: none; opacity: 1; }
  30% { transform: scale(1.9) rotate(-20deg); filter: brightness(1.8); opacity: 1; }
  100% { transform: scale(1) rotate(0); }
}
.top-buttons { flex: none; display: flex; gap: 6px; }
.pill {
  display: flex; align-items: center; gap: 6px; height: 38px; padding: 0 12px 0 6px;
  border: none; border-radius: 999px; color: #fff; font-weight: bold; font-size: 14px;
  background: linear-gradient(#4a9be6, var(--menu-button));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35), 0 3px 0 #13336e;
  transition: transform 0.1s, filter 0.15s;
}
.pill img { width: 28px; height: 28px; object-fit: contain; }
.pill:hover, .pill:focus-visible { filter: brightness(1.1); }
.pill:active { transform: translateY(2px); }
/* Narrow: the buttons without their words, the stats smaller */
@container (max-width: 460px) {
  .top { gap: 8px; padding: 6px 8px; }
  .pill { padding: 0 5px; }
  .pill span { display: none; }
  .top-name { font-size: 16px; }
  .top-fruit { width: 32px; height: 36px; }
}

/* Windows over the game: rounded, white edged, springing in over a dimmed game */
.scrim {
  position: absolute; inset: 0; z-index: 1;
  background: radial-gradient(ellipse at center, rgba(10, 25, 70, 0.25), rgba(10, 25, 70, 0.55));
  animation: fade-in 0.2s ease-out;
}
.scrim[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; } }
.dialog {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  box-sizing: border-box; z-index: 2;
  animation: dialog-in 0.28s var(--pop);
  box-shadow: 0 14px 40px rgba(0, 10, 50, 0.45);
}
@keyframes dialog-in { from { transform: translate(-50%, -46%) scale(0.88); opacity: 0; } }
.dialog[hidden] { display: none; }
.plate {
  position: absolute; left: 50%; top: 0; transform: translate(-50%, -50%);
  background: linear-gradient(#5a77ea, var(--plate)); color: #fff; font-weight: bold;
  border: 2px solid #fff; border-radius: 999px; box-shadow: 0 4px 10px rgba(0, 10, 60, 0.35);
  padding: 0.3em 1.1em; white-space: nowrap; font-size: max(13px, calc(var(--W) * 0.035)); z-index: 1;
  text-shadow: 0 1px 1px rgba(0, 0, 40, 0.4);
}
.close {
  position: absolute; right: 0; top: 0; transform: translate(25%, -25%);
  width: calc(var(--W) * 0.1); height: calc(var(--W) * 0.1); min-width: 34px; min-height: 34px; max-width: 56px; max-height: 56px;
  background: none; border: none; padding: 0; z-index: 2; line-height: 0;
}
.close img { width: 100%; height: 100%; transition: transform 0.15s var(--pop); filter: drop-shadow(0 3px 4px rgba(0, 0, 40, 0.35)); }
.close:hover img { transform: scale(1.1) rotate(8deg); }

/* The menu */
.menu {
  width: min(calc(var(--W) * 0.72), 360px); padding: 14px 14px 12px;
  background: linear-gradient(#2a4aa8, var(--menu-back)); border: 4px solid #fff; border-radius: 28px;
}
.menu-title {
  margin: 2px 0 12px; text-align: center; color: #fff; font-size: max(20px, calc(var(--H) * 0.032));
  text-shadow: 0 3px 0 rgba(0, 0, 40, 0.35);
}
.menu-buttons { display: grid; gap: 8px; }
.menu-buttons button {
  height: max(44px, calc(var(--H) * 0.062)); border: none; border-radius: 14px;
  background: linear-gradient(#4a9be6, var(--menu-button)); color: #fff; font-weight: bold;
  font-size: max(16px, calc(var(--H) * 0.024));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.35), 0 4px 0 #1d4f8f;
  text-shadow: 0 1px 1px rgba(0, 0, 40, 0.35);
  transition: transform 0.1s, box-shadow 0.1s, filter 0.15s;
}
.menu-buttons button.primary { background: linear-gradient(#ffd75a, #f6a623); color: #5a3400; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5); box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.5), 0 4px 0 #b06d00; }
.menu-buttons button:hover, .menu-buttons button:focus-visible { filter: brightness(1.1); }
.menu-buttons button:active { transform: translateY(3px); box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.3), 0 1px 0 #1d4f8f; }
.menu-account, .menu-problem { margin: 12px 4px 0; color: #dfe8ff; font-size: 13px; line-height: 1.45; text-align: center; }
.menu-account a { color: #fff; font-weight: bold; }
.menu-problem { color: #ffc0b0; margin-top: 4px; }
.menu-problem:empty { display: none; }

/* The difficulty */
.difficulty {
  width: min(calc(var(--W) * 0.94), 520px); padding: 34px 16px 18px;
  background: linear-gradient(#f4f7ff, #d9e2f5); border: 4px solid #fff; border-radius: 22px;
}
.difficulty .choices { display: flex; gap: 6px; }
.difficulty .choices button {
  flex: 1; padding: 0.75em 0.1em; border: none; border-radius: 12px;
  background: #fff; color: #1a5bc8; font-weight: bold; font-size: max(13px, calc(var(--W) * 0.032));
  box-shadow: 0 3px 0 #b7c6e6; transition: transform 0.1s;
}
.difficulty .choices button:hover { transform: translateY(-2px); }
.difficulty .choices button[aria-pressed="true"] { background: linear-gradient(#4a9be6, #1a5bc8); color: #fff; box-shadow: 0 3px 0 #0f3a85; }

/* The map */
.map {
  width: calc(var(--W) * 0.86); height: calc(var(--H) * 0.82);
  border: 4px solid #fff; border-radius: calc(var(--W) * 0.08); background: #3a7a20;
}
.map canvas { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: calc(var(--W) * 0.075); touch-action: none; }
.map .close { width: calc(var(--W) * 0.12); height: calc(var(--W) * 0.12); }

/* A level's card */
.preview {
  width: min(calc(var(--W) * 0.8), 420px); height: min(calc(var(--H) * 0.62), 560px);
  border: 4px solid #fff; border-radius: 26px; overflow: visible;
  background: url(images/grass.jpg) center / cover;
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: calc(var(--H) * 0.04) 0 calc(var(--H) * 0.022);
  animation: card-in 0.4s var(--pop);
}
@keyframes card-in { from { transform: translate(-160%, -50%) rotate(-8deg); } }
.preview::before {     /* a soft light from above, so the grass isn't flat */
  content: ''; position: absolute; inset: 0; border-radius: 22px; pointer-events: none;
  background: radial-gradient(ellipse at 50% 15%, rgba(255, 255, 220, 0.35), transparent 60%), linear-gradient(transparent 60%, rgba(0, 30, 0, 0.3));
}
.preview > :not(.plate):not(.close) { position: relative; }
.preview .fruit {
  width: calc(min(var(--W) * 0.8, 420px) * 0.32 * var(--w, 1)); height: calc(min(var(--W) * 0.8, 420px) * 0.32 * var(--h, 1));
  flex: none; animation: float 2.6s ease-in-out infinite;
}
@keyframes float { 50% { transform: translateY(-8px) rotate(3deg); } }
.preview .fruit img { width: 100%; height: 100%; filter: drop-shadow(0 12px 8px rgba(0, 30, 0, 0.45)); }
.preview .fruit span {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: calc(min(var(--W) * 0.8, 420px) * 0.15); font-weight: bold; color: var(--ink);
  -webkit-text-stroke: 0; text-shadow: 0 0 6px rgba(255, 255, 255, 0.8);
}
.preview .name {
  align-self: stretch; margin: 0 -10px; padding: 0.4em 0.5em; text-align: center; font-weight: bold;
  background: linear-gradient(#d8323f, var(--desc)); color: #fff; font-size: max(17px, calc(var(--H) * 0.034));
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3); text-shadow: 0 2px 0 rgba(80, 0, 10, 0.45);
  clip-path: polygon(0 0, 100% 0, 97% 50%, 100% 100%, 0 100%, 3% 50%);
}
.preview .progress {
  width: 76%; padding: 3% 0 3.5%; text-align: center;
  background: linear-gradient(#fffbe0, var(--progress)); border: 3px solid #fff; border-radius: 16px;
  box-shadow: 0 6px 14px rgba(0, 30, 0, 0.35);
}
.preview .progress[hidden] { visibility: hidden; display: block; }
.preview .stars { display: flex; justify-content: center; gap: 4%; height: calc(var(--H) * 0.062); }
.preview .stars img { height: 100%; filter: drop-shadow(0 3px 2px rgba(80, 50, 0, 0.35)); }
.preview .stars img.gone { filter: grayscale(1); opacity: 0.25; }
.preview .best { font-size: max(14px, calc(var(--H) * 0.024)); color: #5a4a10; font-weight: bold; margin-top: 0.35em; }
.preview .buttons { display: flex; gap: calc(var(--W) * 0.08); }
.preview .buttons button { width: calc(var(--H) * 0.1); max-width: 84px; display: flex; flex-direction: column; align-items: center; gap: 4px; line-height: 1; }
.preview .buttons button img { height: calc(var(--H) * 0.1); max-height: 84px; width: auto; }
.preview .buttons button span { color: #fff; font-weight: bold; font-size: 13px; white-space: nowrap; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7); }

.sparkle { animation: sparkle 0.7s ease-out; }
@keyframes sparkle {
  0% { filter: drop-shadow(0 0 0 #fff8a0) brightness(1); transform: scale(1); }
  30% { filter: drop-shadow(0 0 16px #fff8a0) brightness(1.7); transform: scale(1.25); }
  100% { filter: drop-shadow(0 3px 2px rgba(80, 50, 0, 0.35)) brightness(1); transform: scale(1); }
}

/* The rules: a speech bubble over the fruit */
.instructions {
  top: auto; bottom: calc(var(--sb) * 0.5); transform: translateX(-50%);
  width: min(var(--W), calc(var(--board) * 1.05)); aspect-ratio: 687 / 536; max-height: calc(var(--H) - var(--board) * 0.5);
  background: url(images/dialogue-window-148172.png) center / 100% 100% no-repeat;
  box-shadow: none;
  padding: 5% 9% 0 14%; color: var(--ink);
  display: flex; flex-direction: column; align-items: flex-start;
  font-size: max(12px, calc(var(--board) * 0.033)); line-height: 1.3;
  animation: bubble-in 0.35s var(--pop);
  transform-origin: 85% 95%;
}
@keyframes bubble-in { from { transform: translateX(-50%) scale(0.6); opacity: 0; } }
.instructions p { margin: 0 0 0.4em; font-weight: bold; }
.instructions ol { margin: 0 0 0.4em; padding-left: 1.3em; }
.instructions .how { color: #3a4560; margin: 0; }
.instructions .ok {
  align-self: flex-end; margin-top: 0.4em;
  padding: 0.4em 1.6em; border: none; border-radius: 12px; font-weight: bold;
  background: linear-gradient(#ffd75a, #f6a623); color: #5a3400; box-shadow: 0 3px 0 #b06d00;
}
.instructions .ok:active { transform: translateY(2px); box-shadow: 0 1px 0 #b06d00; }

@media (prefers-reduced-motion: reduce) {
  *, *::before { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
