/* ============================================================
   Word Wizards — a crossword for 8-year-olds.
   Big targets, loud colours, and something moving at all times.
   ============================================================ */

:root {
  --grape: #6c3fd1;
  --grape-dark: #4a239c;
  --bubblegum: #ff5fa2;
  --sunshine: #ffc93c;
  --mint: #35d6a4;
  --sky-blue: #46bdf5;
  --tangerine: #ff8c42;
  --ink: #2c1b57;
  --paper: #fffdf7;

  /* Starting guess only — game.js measures the leftover space after the
     bar, clue banner, buttons and keyboard, then overrides --cell. */
  --cell: 42px;
  --gap: 3px;
  --grid-pad: 10px;
  --radius: 18px;
  --shadow-pop: 0 6px 0 rgba(0, 0, 0, 0.16);

  --font: "Baloo 2", "Comic Sans MS", "Chalkboard SE", "Comic Neue",
    "Trebuchet MS", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(170deg, #b9e8ff 0%, #dcd0ff 45%, #ffe1f0 100%);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- drifting background shapes ---------- */
.sky {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(circle at 12% 20%, rgba(255, 255, 255, 0.75) 0 42px, transparent 43px),
    radial-gradient(circle at 84% 30%, rgba(255, 255, 255, 0.6) 0 32px, transparent 33px),
    radial-gradient(circle at 68% 78%, rgba(255, 255, 255, 0.55) 0 52px, transparent 53px),
    radial-gradient(circle at 26% 88%, rgba(255, 255, 255, 0.5) 0 26px, transparent 27px);
  animation: drift 26s ease-in-out infinite alternate;
}
@keyframes drift {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to   { transform: translate3d(-28px, 22px, 0) scale(1.07); }
}

/* ---------- screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  padding: clamp(8px, 2vh, 20px);
  overflow: auto;
}
.screen.is-active { display: flex; }

/* ---------- home ---------- */
.logo {
  margin: clamp(6px, 3vh, 30px) 0 0;
  text-align: center;
  font-size: clamp(2.2rem, 8vw, 4.6rem);
  line-height: 1.05;
  letter-spacing: 2px;
}
.logo__word { display: block; }
.logo__letter {
  display: inline-block;
  color: var(--paper);
  text-shadow:
    0 3px 0 var(--grape-dark),
    0 7px 12px rgba(0, 0, 0, 0.25);
  animation: letterHop 2.4s ease-in-out infinite;
  animation-delay: calc(var(--i) * 0.09s);
}
.logo__word--alt .logo__letter { color: var(--sunshine); }
@keyframes letterHop {
  0%, 62%, 100% { transform: translateY(0) rotate(0deg); }
  72%           { transform: translateY(-16px) rotate(-6deg); }
  84%           { transform: translateY(0) rotate(3deg); }
}

.tagline {
  text-align: center;
  font-size: clamp(1rem, 3vw, 1.4rem);
  color: var(--grape-dark);
  margin: 10px 0 clamp(10px, 3vh, 26px);
}

.puzzle-list {
  width: min(940px, 100%);
  margin: 0 auto;
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
}

/* ---------- difficulty tiers ---------- */
.tier + .tier { margin-top: clamp(24px, 5vh, 44px); }

.tier__title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 2px;
  font-size: clamp(1.2rem, 3.6vw, 1.7rem);
  color: var(--grape-dark);
}
.tier__stars {
  font-size: 0.9em;
  animation: wobble 3.6s ease-in-out infinite;
}
.tier__count {
  margin-left: auto;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  font-size: 0.72em;
  color: var(--grape);
}
.tier__hint {
  margin: 0 0 12px;
  font-size: 0.95rem;
  color: #6f5fa0;
}

.puzzle-card {
  position: relative;
  border: 0;
  font: inherit;
  cursor: pointer;
  text-align: left;
  padding: 18px 18px 16px;
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow-pop);
  transition: transform 0.16s ease, box-shadow 0.16s ease;
  animation: cardIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  animation-delay: calc(var(--i) * 0.07s);
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(22px) scale(0.9); }
  to   { opacity: 1; transform: none; }
}
.puzzle-card:hover  { transform: translateY(-4px) rotate(-1deg); box-shadow: 0 12px 0 rgba(0,0,0,0.14); }
.puzzle-card:active { transform: translateY(2px); box-shadow: 0 3px 0 rgba(0,0,0,0.18); }

.puzzle-card__emoji {
  display: block;
  font-size: 2.6rem;
  animation: wobble 3.4s ease-in-out infinite;
  animation-delay: calc(var(--i) * 0.3s);
}
@keyframes wobble {
  0%, 100% { transform: rotate(-7deg); }
  50%      { transform: rotate(7deg) scale(1.08); }
}
.puzzle-card__name { display: block; font-size: 1.35rem; margin: 4px 0 2px; }
.puzzle-card__meta { display: block; font-size: 0.95rem; color: #7a6ba3; }
.puzzle-card__stars { font-size: 1.2rem; letter-spacing: 2px; }
.puzzle-card.is-done { background: linear-gradient(160deg, #fff6d8, #ffe9a8); }
.puzzle-card.is-done::after {
  content: "✓";
  position: absolute;
  top: 10px; right: 12px;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--mint);
  color: #fff;
  font-size: 1.1rem;
  box-shadow: 0 3px 0 #1c9d74;
}

.text-btn {
  margin: 22px auto clamp(10px, 4vh, 30px);
  border: 0;
  background: none;
  font: inherit;
  font-size: 1rem;
  color: var(--grape-dark);
  opacity: 0.7;
  text-decoration: underline;
  cursor: pointer;
}

/* ---------- game top bar ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}
.bar__title {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: clamp(1.1rem, 3.4vw, 1.7rem);
  color: var(--grape-dark);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar__emoji { font-size: 1.5em; animation: wobble 3s ease-in-out infinite; }
.bar__right { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  border: 0;
  cursor: pointer;
  font-size: 1.4rem;
  width: 46px; height: 46px;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
  transition: transform 0.12s ease;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.15); }

.pill {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--grape);
  color: #fff;
  font-size: 1rem;
  white-space: nowrap;
}
.pill.is-bump { animation: bump 0.45s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes bump {
  50% { transform: scale(1.35) rotate(-5deg); background: var(--mint); }
}

/* ---------- current clue ---------- */
.clue-banner {
  flex: 0 0 auto;
  margin: 10px 0;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow-pop);
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
}
.clue-banner.is-new { animation: clueIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes clueIn {
  from { transform: scale(0.94) translateY(-6px); opacity: 0.4; }
}
.clue-banner__tag {
  flex: 0 0 auto;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--sunshine);
  color: var(--ink);
  font-size: 0.95rem;
  white-space: nowrap;
}
.clue-banner__text { font-size: clamp(1rem, 3.2vw, 1.45rem); }

/* ---------- play area ---------- */
.play {
  flex: 1 1 auto;
  display: flex;
  gap: 18px;
  align-items: flex-start;
  justify-content: center;
  min-height: 0;
  overflow: auto;
}
.play__left  { display: flex; flex-direction: column; align-items: center; gap: 12px; min-height: 0; }
.play__right { display: flex; flex-direction: column; gap: 10px; max-width: 340px; flex: 1 1 260px; }

.grid-wrap { position: relative; }

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
  padding: var(--grid-pad);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.45);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.7);
}

.cell { position: relative; }
.cell--blank { background: transparent; }

.cell--play {
  border-radius: 10px;
  background: var(--paper);
  border: 2px solid #d8cdf0;
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.56);
  line-height: 1;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.12s ease;
}
.cell--play.in-word { background: #e8f7ff; border-color: var(--sky-blue); }
.cell--play.is-current {
  background: var(--sunshine);
  border-color: var(--tangerine);
  transform: scale(1.06);
  z-index: 2;
  animation: focusPulse 1.3s ease-in-out infinite;
}
@keyframes focusPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 140, 66, 0.7); }
  50%      { box-shadow: 0 0 0 8px rgba(255, 140, 66, 0); }
}

.cell__num {
  position: absolute;
  top: 1px; left: 3px;
  font-size: calc(var(--cell) * 0.26);
  color: #9b8bc4;
  pointer-events: none;
}

.cell__letter { display: block; }
.cell__letter.is-pop { animation: letterPop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes letterPop {
  0%   { transform: scale(0.2) rotate(-25deg); opacity: 0; }
  60%  { transform: scale(1.3) rotate(8deg); }
  100% { transform: scale(1) rotate(0); }
}

/* a whole word landing correctly: a rainbow flip that rolls left to right */
.cell--play.is-solved {
  cursor: default;
  border-color: #1c9d74;
  background: var(--mint);
  color: #fff;
}
.cell--play.just-solved { animation: solveFlip 0.6s ease both; animation-delay: calc(var(--i) * 0.08s); }
@keyframes solveFlip {
  0%   { transform: rotateY(0) scale(1);   background: var(--sunshine); }
  40%  { transform: rotateY(90deg) scale(1.25); background: var(--bubblegum); }
  60%  { transform: rotateY(180deg) scale(1.25); background: var(--sky-blue); }
  100% { transform: rotateY(360deg) scale(1); background: var(--mint); }
}

.cell--play.is-wrong { animation: shake 0.45s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-7px) rotate(-4deg); background: #ffd9d9; }
  40%      { transform: translateX(7px) rotate(4deg);  background: #ffd9d9; }
  60%      { transform: translateX(-5px) rotate(-3deg); }
  80%      { transform: translateX(5px) rotate(2deg); }
}

.cell--play.is-hinted { animation: hintGlow 0.9s ease; }
@keyframes hintGlow {
  0%   { background: #fff; box-shadow: 0 0 0 0 var(--sunshine); }
  40%  { background: var(--sunshine); box-shadow: 0 0 0 14px rgba(255, 201, 60, 0); transform: scale(1.25); }
  100% { background: var(--paper); transform: scale(1); }
}

/* ---------- sparkle burst layer ---------- */
.sparkles { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.spark {
  position: absolute;
  font-size: 20px;
  animation: sparkFly 0.9s ease-out forwards;
}
@keyframes sparkFly {
  0%   { transform: translate(0, 0) scale(0.3) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.4) rotate(var(--rot)); opacity: 0; }
}

/* ---------- controls ---------- */
.controls {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  justify-content: center;
  padding-top: 10px;
}
.big-btn {
  border: 0;
  font: inherit;
  cursor: pointer;
  font-size: 1.15rem;
  padding: 12px 22px;
  border-radius: 999px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.18);
  transition: transform 0.12s ease;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0,0,0,0.18); }
.big-btn--hint { background: var(--sunshine); }
.big-btn--go   { background: var(--mint); color: #fff; }
.big-btn--nav  { padding: 12px 18px; }
.big-btn--clues { display: none; background: var(--sky-blue); color: #fff; }

.sheet-close {
  display: none;
  position: absolute;
  top: 8px; right: 10px;
  border: 0;
  font: inherit;
  font-size: 1.2rem;
  width: 38px; height: 38px;
  border-radius: 50%;
  background: #ffd9e6;
  cursor: pointer;
}

/* ---------- mascot ---------- */
.mascot { position: relative; align-self: center; width: 118px; flex: 0 0 auto; }
.mascot__svg { width: 100%; overflow: visible; }
.m-shadow { fill: rgba(76, 42, 130, 0.18); }
.m-body   { transform-origin: 60px 120px; animation: bob 2.6s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50%      { transform: translateY(-7px) rotate(1.5deg); }
}
.m-belly-bg { fill: var(--grape); }
.m-belly    { fill: #f5ebff; }
.m-wing     { fill: var(--grape-dark); transform-origin: 60px 66px; }
.m-tuft     { fill: var(--grape-dark); }
.m-eye      { fill: #fff; stroke: var(--grape-dark); stroke-width: 3; animation: blink 5s infinite; transform-origin: center; transform-box: fill-box; }
@keyframes blink {
  0%, 92%, 100% { transform: scaleY(1); }
  95%           { transform: scaleY(0.08); }
}
.m-pupil { fill: var(--ink); }
.m-glint { fill: #fff; }
.m-beak  { fill: var(--sunshine); stroke: var(--tangerine); stroke-width: 2; }
.m-foot  { stroke: var(--tangerine); stroke-width: 4; stroke-linecap: round; fill: none; }

.mascot[data-mood="cheer"] .m-body { animation: cheer 0.6s ease-in-out 2; }
@keyframes cheer {
  0%, 100% { transform: translateY(0) rotate(0); }
  25%      { transform: translateY(-22px) rotate(-10deg) scale(1.06); }
  75%      { transform: translateY(-14px) rotate(10deg) scale(1.04); }
}
.mascot[data-mood="cheer"] .m-wing--l { animation: flapL 0.3s ease-in-out 4; }
.mascot[data-mood="cheer"] .m-wing--r { animation: flapR 0.3s ease-in-out 4; }
@keyframes flapL { 50% { transform: rotate(-40deg) translateY(-6px); } }
@keyframes flapR { 50% { transform: rotate(40deg) translateY(-6px); } }

.mascot[data-mood="think"] .m-body { animation: think 0.9s ease-in-out 2; }
@keyframes think {
  0%, 100% { transform: rotate(0); }
  50%      { transform: rotate(-9deg) translateX(-5px); }
}

.speech {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translate(-50%, -6px) scale(0.4);
  opacity: 0;
  padding: 8px 14px;
  border-radius: 16px;
  background: var(--paper);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.14);
  font-size: 1rem;
  white-space: nowrap;
  pointer-events: none;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
}
.speech.is-on { opacity: 1; transform: translate(-50%, -10px) scale(1); }
.speech::after {
  content: "";
  position: absolute;
  top: 100%; left: 50%;
  margin-left: -7px;
  border: 7px solid transparent;
  border-top-color: var(--paper);
}

/* ---------- clue lists ---------- */
.clue-lists { display: flex; gap: 12px; overflow: auto; min-height: 0; }
.clue-col { flex: 1 1 0; min-width: 0; }
.clue-col h2 { margin: 0 0 6px; font-size: 1.05rem; color: var(--grape-dark); }
.clue-col ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.clue-item {
  padding: 7px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.72);
  font-size: 0.92rem;
  cursor: pointer;
  display: flex;
  gap: 7px;
  transition: background 0.15s ease, transform 0.15s ease;
}
.clue-item:hover { transform: translateX(3px); }
.clue-item b { color: var(--grape); flex: 0 0 auto; }
.clue-item.is-current { background: var(--sunshine); }
.clue-item.is-solved {
  background: rgba(53, 214, 164, 0.28);
  text-decoration: line-through;
  color: #4b6f63;
}
.clue-item.is-solved::after { content: "⭐"; margin-left: auto; animation: starStamp 0.5s cubic-bezier(0.34,1.56,0.64,1); }
@keyframes starStamp {
  0%   { transform: scale(2.6) rotate(-40deg); opacity: 0; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

/* ---------- keyboard ---------- */
.keyboard {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  padding-top: 10px;
}
.kb-row { display: flex; gap: 5px; }
.key {
  border: 0;
  font: inherit;
  cursor: pointer;
  min-width: clamp(26px, 7.4vw, 46px);
  height: clamp(32px, min(8vw, 7vh), 52px);
  padding: 0 4px;
  border-radius: 12px;
  font-size: clamp(0.95rem, 3vw, 1.3rem);
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(76, 42, 130, 0.35);
  transition: transform 0.08s ease, background 0.08s ease;
}
.key:active, .key.is-down {
  transform: translateY(4px);
  box-shadow: 0 0 0 rgba(0,0,0,0.2);
  background: var(--sky-blue);
  color: #fff;
}
.key--wide { min-width: clamp(46px, 12vw, 74px); background: #ffd9e6; }

/* ---------- win overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(44, 27, 87, 0.55);
  z-index: 50;
}
.overlay[hidden] { display: none; }
.confetti { position: absolute; inset: 0; width: 100%; height: 100%; }

.trophy-card {
  position: relative;
  text-align: center;
  padding: 26px 34px 30px;
  border-radius: 26px;
  background: var(--paper);
  box-shadow: 0 14px 0 rgba(0, 0, 0, 0.22);
  animation: popIn 0.55s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes popIn {
  from { transform: scale(0.3) rotate(-12deg); opacity: 0; }
}
.trophy { font-size: 4.6rem; animation: trophySpin 2.6s ease-in-out infinite; }
@keyframes trophySpin {
  0%, 100% { transform: rotate(-9deg) scale(1); }
  50%      { transform: rotate(9deg) scale(1.14); }
}
.trophy-card h2 { margin: 4px 0; font-size: 2.1rem; color: var(--grape-dark); }
.trophy-card p  { margin: 0 0 10px; color: #6c5b96; }
.win-stars { font-size: 2.4rem; letter-spacing: 6px; margin-bottom: 16px; }
.win-stars span { display: inline-block; animation: starIn 0.5s cubic-bezier(0.34,1.56,0.64,1) both; animation-delay: calc(var(--i) * 0.22s + 0.3s); }
@keyframes starIn {
  from { transform: scale(3) rotate(70deg); opacity: 0; }
}
.trophy-card__btns { display: flex; gap: 10px; justify-content: center; }

/* ---------- narrow screens: stack the clue lists under the grid ---------- */
/* ---------- phones and small tablets ----------
   The board is the whole point on a small screen, so the clue columns move
   into a slide-up sheet and the owl floats over a corner instead of taking
   a slice of the layout. Both are out of normal flow, which hands the grid
   every pixel fitGrid() can find. */
@media (max-width: 820px) {
  .play {
    position: relative;
    flex-direction: column;
    align-items: center;
    /* Centre the board in the leftover space; `safe` keeps the top reachable
       if a puzzle ever ends up taller than the area. */
    justify-content: center;
    justify-content: safe center;
  }
  /* flex-basis applies to height once the axis flips, so the (empty) clue
     column would otherwise swallow all the spare space and defeat centring. */
  .play__right { max-width: none; width: 100%; flex: 0 0 auto; }

  .big-btn--clues { display: block; }
  .sheet-close { display: block; }

  .mascot {
    position: absolute;
    right: 2px;
    bottom: 2px;
    width: 68px;
    z-index: 6;
  }
  .speech { font-size: 0.85rem; }

  .clue-lists {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    max-height: 66vh;
    padding: 44px 14px 18px;
    border-radius: 26px 26px 0 0;
    background: var(--paper);
    box-shadow: 0 -8px 26px rgba(44, 27, 87, 0.3);
    transform: translateY(102%);
    transition: transform 0.3s cubic-bezier(0.34, 1.4, 0.64, 1);
  }
  #game.show-clues .clue-lists { transform: translateY(0); }
  .clue-item { font-size: 1rem; padding: 10px 12px; }
}

/* ---------- respect reduced-motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
