/* ————— Toy Box design system ————— */
:root {
  --sky-top: #59c3ff;
  --sky-bottom: #a8efff;
  --ink: #3a2e39;
  --muted: #7a7078;
  --paper: #fffdf7;
  --strawberry: #ff5d5d;
  --mango: #ffa83b;
  --banana: #ffd93d;
  --apple: #6bcb77;
  --blueberry: #4d96ff;
  --grape: #9b5de5;
  --mint: #35d0ba;
  --shadow-ink: rgba(58, 46, 57, 0.18);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  display: grid;
  place-items: center;
  font-family: "Baloo 2", "Comic Sans MS", "Chalkboard SE", system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(120% 90% at 50% 110%, rgba(255, 255, 255, 0.55) 0%, rgba(255,255,255,0) 60%),
    linear-gradient(180deg, var(--sky-top), var(--sky-bottom));
  user-select: none;
  -webkit-user-select: none;
  -webkit-font-smoothing: antialiased;
}

#app {
  width: 100%;
  height: min(100dvh, 900px);
  max-width: 520px;
  max-height: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px calc(env(safe-area-inset-bottom) + 12px);
  position: relative;
}

.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.hidden { display: none !important; }

button,
input { -webkit-tap-highlight-color: transparent; }

button { touch-action: manipulation; }

button:focus-visible,
input:focus-visible {
  outline: 4px solid var(--paper);
  outline-offset: 3px;
  box-shadow: 0 0 0 7px var(--ink);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

/* ————— top bar (shared) ————— */
.topbar { display: flex; justify-content: space-between; align-items: center; }

.left-group { display: flex; gap: 8px; align-items: center; min-width: 0; }

.star-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--paper);
  border-radius: 999px;
  padding: 5px 14px 5px 10px;
  font-size: 22px;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--shadow-ink);
  flex: none;
}
.icon.star { width: 22px; height: 22px; fill: var(--banana); stroke: var(--ink); stroke-width: 1.6; }

.name-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: none;
  background: var(--paper);
  border-radius: 999px;
  padding: 5px 12px 5px 8px;
  font-family: inherit;
  font-size: 17px;
  font-weight: 800;
  color: var(--ink);
  min-width: 0;
  box-shadow: 0 4px 0 var(--shadow-ink);
  cursor: pointer;
}
.name-chip:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow-ink); }
#nameAvatar { font-size: 20px; }
#nameLabel { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.topbtns { display: flex; gap: 10px; }

.round-btn.small {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: none;
  background: var(--paper);
  box-shadow: 0 4px 0 var(--shadow-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.round-btn.small:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow-ink); }
.round-btn.small .icon { width: 24px; height: 24px; stroke: var(--ink); stroke-width: 2.2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.round-btn.small.muted .sound-on { display: none; }

/* ————— HOME ————— */
#screen-home { justify-content: center; }

.mascot-big {
  width: 92px;
  height: 92px;
  margin: 2px auto 0;
  background: var(--paper);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 54px;
  box-shadow: 0 6px 0 var(--shadow-ink);
}
#homeFace { display: inline-block; animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-3deg); } 50% { transform: translateY(-7px) rotate(3deg); } }

.home-title { text-align: center; font-size: 40px; font-weight: 800; line-height: 1; }
.home-title span { display: inline-block; animation: hop 1.5s ease-in-out infinite; }
.home-title span:nth-child(odd) { color: var(--grape); }
.home-title span:nth-child(even) { color: var(--strawberry); }
.home-title span:nth-child(3n) { color: var(--blueberry); }
.home-title span:nth-child(4n) { color: var(--apple); }
@keyframes hop { 0%, 100% { transform: translateY(0); } 35% { transform: translateY(-8px); } }

.player-bar {
  background: var(--paper);
  border-radius: 22px;
  padding: 12px;
  box-shadow: 0 4px 0 var(--shadow-ink);
}
.players { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 10px; }
.players:empty { margin-bottom: 0; }

.player-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 3px solid transparent;
  background: #f1ece4;
  border-radius: 999px;
  padding: 6px 14px 6px 10px;
  font-family: inherit;
  font-size: 18px;
  font-weight: 800;
  color: var(--ink);
  box-shadow: 0 3px 0 var(--shadow-ink);
  cursor: pointer;
}
.player-chip .avatar { font-size: 20px; }
.player-chip.active { background: var(--banana); border-color: var(--ink); transform: scale(1.06); }
.player-chip:active { transform: translateY(2px); }

.new-player { display: flex; gap: 8px; }
.new-player input {
  font-family: inherit;
  font-size: 19px;
  font-weight: 700;
  color: var(--ink);
  padding: 9px 16px;
  border-radius: 999px;
  border: 3px solid #e5ddd0;
  background: #fff;
  width: 100%;
  min-width: 0;
}
.new-player input:focus { border-color: var(--mango); }
.new-player input:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; box-shadow: none; }
.new-player input::placeholder { color: #766d67; }
.new-player button {
  flex: none;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--apple);
  color: var(--ink);
  font-family: inherit;
  font-size: 28px;
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 4px 0 var(--shadow-ink), inset 0 3px 0 rgba(255,255,255,0.4);
  cursor: pointer;
}
.new-player button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow-ink); }

.game-cards {
  flex: 0 1 min(38dvh, 320px);
  min-height: 150px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.game-card {
  border: none;
  border-radius: 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 7px 0 var(--shadow-ink), inset 0 3px 0 rgba(255,255,255,0.5);
  transition: transform 120ms cubic-bezier(0.2, 0, 0, 1), box-shadow 120ms cubic-bezier(0.2, 0, 0, 1), filter 120ms ease-out;
}
.game-card:hover { filter: brightness(1.04); transform: translateY(-2px); }
.game-card:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--shadow-ink); }
.card-art { font-size: clamp(38px, 11vw, 56px); filter: drop-shadow(0 3px 0 rgba(58,46,57,0.2)); }
.card-name { font-size: clamp(16px, 4vw, 20px); font-weight: 800; color: var(--ink); }
.game-card.banana { background: var(--banana); }
.game-card.mint { background: #9be8dc; }
.game-card.blueberry { background: #a9cdff; }

/* ————— BLOCKS ————— */
.blocks-head {
  display: flex;
  align-items: center;
  gap: 10px;
  align-self: flex-end;
  background: var(--paper);
  border-radius: 18px;
  padding: 6px 8px 6px 14px;
  box-shadow: 0 4px 0 var(--shadow-ink);
}
.next-label { font-size: 16px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
#next { width: 84px; height: 56px; display: block; }

.board-wrap { flex: 1; min-height: 0; display: grid; place-items: center; }

.board-frame {
  position: relative;
  background: var(--paper);
  padding: 9px;
  border-radius: 30px;
  box-shadow: 0 8px 0 var(--shadow-ink);
  max-height: 100%;
}
.board-frame.bump { animation: bump 0.22s ease; }
@keyframes bump { 40% { transform: scale(1.015); } }

#board { display: block; border-radius: 20px; background: #fdf7ec; }

.controls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }

.ctl {
  aspect-ratio: 1 / 1;
  border: none;
  border-radius: 26px;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 7px 0 var(--shadow-ink), inset 0 -4px 0 rgba(0, 0, 0, 0.08), inset 0 3px 0 rgba(255, 255, 255, 0.55);
  transition: transform 0.06s ease, box-shadow 0.06s ease;
}
.ctl .icon { width: 46%; max-width: 44px; stroke: var(--ink); stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.ctl:active, .ctl.pressed { transform: translateY(5px); box-shadow: 0 2px 0 var(--shadow-ink); }
.ctl.pink   { background: #ffb3c6; }
.ctl.yellow { background: var(--banana); }
.ctl.green  { background: #a7e8b0; }
.ctl.blue   { background: #a9cdff; }

/* ————— overlay (start / pause / done) ————— */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(58, 46, 57, 0.45);
  backdrop-filter: blur(3px);
  z-index: 10;
  padding: 20px;
}
.overlay.hidden { display: none; }

.overlay-card {
  background: var(--paper);
  border-radius: 16px;
  padding: 30px 26px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: 0 10px 0 rgba(58, 46, 57, 0.25);
}

.title {
  font-size: clamp(32px, 10vw, 42px);
  font-weight: 800;
  line-height: 1.05;
}
.title span { display: inline-block; animation: hop 1.4s ease-in-out infinite; }
.title span:nth-child(odd) { color: var(--grape); }
.title span:nth-child(even) { color: var(--strawberry); }
.title span:nth-child(3n) { color: var(--blueberry); }
.title span:nth-child(4n) { color: var(--apple); }

.msg { margin: 10px 0 20px; font-size: 21px; font-weight: 600; color: var(--muted); }

.play-btn {
  font-family: inherit;
  font-size: 28px;
  font-weight: 800;
  color: var(--ink);
  background: var(--banana);
  border: none;
  border-radius: 22px;
  padding: 13px 52px;
  cursor: pointer;
  box-shadow: 0 7px 0 var(--shadow-ink), inset 0 3px 0 rgba(255, 255, 255, 0.6);
}
.play-btn:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--shadow-ink); }

/* ————— shared bits for noughts & drop-four ————— */
.turn-banner {
  background: var(--paper);
  border-radius: 999px;
  padding: 8px 20px;
  align-self: center;
  font-size: 23px;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--shadow-ink);
}

.game-actions { display: flex; gap: 12px; justify-content: center; }

.pill-btn {
  font-family: inherit;
  font-size: 20px;
  font-weight: 800;
  color: var(--ink);
  background: var(--paper);
  border: none;
  border-radius: 999px;
  padding: 10px 22px;
  box-shadow: 0 4px 0 var(--shadow-ink);
  cursor: pointer;
}
.pill-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow-ink); }

.pill-btn:hover { filter: brightness(1.04); }
.pill-btn:disabled { filter: grayscale(0.2); }
.pill-btn.again { background: var(--apple); color: var(--ink); }

/* noughts & crosses board */
.ttt-board {
  flex: 1;
  min-height: 0;
  aspect-ratio: 1 / 1;
  max-width: min(100%, calc(100dvh - 260px));
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  background: var(--paper);
  padding: 12px;
  border-radius: 28px;
  box-shadow: 0 8px 0 var(--shadow-ink);
}
.ttt-cell {
  border: none;
  border-radius: 20px;
  background: #f4eee3;
  font-family: inherit;
  font-weight: 800;
  font-size: clamp(44px, 14vw, 72px);
  line-height: 1;
  cursor: pointer;
}
.ttt-cell.x { color: var(--blueberry); }
.ttt-cell.o { color: var(--strawberry); }
.ttt-cell.win { background: var(--banana); animation: winpop 0.5s ease; }
.ttt-cell:empty:active { background: #eae2d2; }
.ttt-cell:empty:hover { background: #eee6d8; }
.ttt-cell:disabled { opacity: 1; cursor: default; }
@keyframes winpop { 30% { transform: scale(1.15) rotate(-4deg); } }

/* connect four board */
.c4-board {
  flex: 1;
  min-height: 0;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  background: var(--blueberry);
  padding: 10px;
  border-radius: 24px;
  box-shadow: 0 8px 0 var(--shadow-ink);
  aspect-ratio: 7 / 6;
  max-height: calc(100dvh - 240px);
}
.c4-hole {
  position: relative;
  border: none;
  padding: 0;
  background: #fdf7ec;
  border-radius: 50%;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
  transition: background-color 120ms ease-out, transform 120ms cubic-bezier(0.2, 0, 0, 1);
}
.c4-hole:not(:disabled):hover { background: #fff; transform: scale(0.94); }
.c4-hole:disabled { opacity: 1; cursor: default; }
.c4-disc {
  position: absolute;
  inset: 7%;
  border-radius: 50%;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18), inset 0 4px 0 rgba(255, 255, 255, 0.45);
  animation: plop 0.3s cubic-bezier(0.55, 0, 0.85, 0.4);
}
.c4-disc.red { background: var(--strawberry); }
.c4-disc.yellow { background: var(--banana); }
.c4-disc.winring::after {
  content: "⭐";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 60%;
}
@keyframes plop { 0% { transform: translateY(-300%); } 75% { transform: translateY(0); } 90% { transform: translateY(-12%); } 100% { transform: translateY(0); } }

@media (max-width: 380px), (max-height: 650px) {
  #app { gap: 8px; padding-inline: 10px; }
  .mascot-big { width: 70px; height: 70px; font-size: 42px; }
  .home-title { font-size: 32px; }
  .player-bar { padding: 9px; border-radius: 16px; }
  .game-cards { gap: 8px; }
  .game-card, .ctl { border-radius: 16px; }
  .game-actions { gap: 8px; }
  .pill-btn { min-height: 44px; padding: 8px 14px; font-size: 17px; }
}

@media (hover: none) {
  .game-card:hover { filter: none; transform: none; }
}

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