/* Animal Sounds — toddler tap-and-listen app.
   Palette "meadow morning". --sky is also hard-coded in index.html's
   theme-color meta and manifest.json — keep those in sync. */

:root {
  --sky: #DFF1F7;          /* page background */
  --ink: #3B3560;          /* text */
  --ink-shadow: rgba(59, 53, 96, 0.12);
  --night: #141227;        /* player backdrop */
  --cream: #FFF6DA;        /* text on night */
  --butter: #FFE6A7;
  --seafoam: #C9EDD3;
  --blossom: #FFD7DB;
  --lilac: #E3DCFA;
  --peach: #FFDFC2;
}

@font-face {
  font-family: "Fredoka";
  src: url("fonts/fredoka.woff2") format("woff2");
  font-weight: 300 700;
  font-display: swap;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--sky);
  color: var(--ink);
  font-family: "Fredoka", ui-rounded, "Arial Rounded MT Bold", "Comic Sans MS", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
  min-height: 100dvh;
}

/* Reduce perceived brightness uniformly without changing the tablet's
   hardware brightness or intercepting any touch input. */
body::after {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.25);
  content: "";
  pointer-events: none;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  padding: 10px 18px 2px;
}

.topbar h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  opacity: 0.55;
}

/* ---------- Swipeable play pages ---------- */

#page-viewport {
  overflow: hidden;
}

#pages {
  display: flex;
  width: 200vw;
}

/* The puzzle is a fixed play surface: no browser pan, pinch, or page scroll
   can steal a piece from the finger that is moving it. */
body[data-page="puzzle"] {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

body[data-page="puzzle"] #page-viewport {
  min-height: 0;
}

body[data-page="puzzle"] #pages {
  height: 100%;
  transform: translateX(-50%);
}

.app-screen {
  width: 100vw;
  flex: 0 0 100vw;
  min-width: 0;
}

#daily-time {
  display: flex;
  align-items: baseline;
  gap: 5px;
  font-variant-numeric: tabular-nums;
  opacity: 0.65;
}

#daily-time span {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

#daily-time-value {
  font-size: 1rem;
  font-weight: 600;
}

#page-tabs {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 8px;
}

#page-tabs button {
  min-width: min(38vw, 180px);
  min-height: 54px;
  border: 2px solid rgba(59, 53, 96, 0.14);
  border-radius: 999px;
  padding: 10px 22px;
  background: rgba(255, 255, 255, 0.5);
  color: var(--ink);
  font: 600 1.05rem/1 inherit;
}

#page-tabs button[aria-pressed="true"] {
  border-color: var(--ink);
  background: var(--cream);
}

/* ---------- Tile grid ---------- */

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  padding: 12px 14px 20px;
}

@media (min-width: 700px) {
  #grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 18px; }
}

.tile {
  appearance: none;
  border: none;
  border-radius: 28px;
  aspect-ratio: 1 / 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 10px 8px 12px;
  cursor: pointer;
  font-family: inherit;
  color: inherit;
  box-shadow: 0 4px 0 var(--ink-shadow);
  transition: transform 120ms ease;
}

.tile:focus-visible {
  outline: 4px solid var(--ink);
  outline-offset: 3px;
}

.tile:active {
  animation: boing 240ms ease;
  transform: scale(0.93);
}

@keyframes boing {
  0%   { transform: scale(1); }
  40%  { transform: scale(0.88, 0.96); }
  100% { transform: scale(0.93); }
}

.tile img {
  width: 72%;
  height: auto;
  max-height: 70%;
  pointer-events: none;
}

.tile .tile-name {
  font-size: clamp(1.05rem, 2.6vw, 1.5rem);
  font-weight: 600;
  line-height: 1.1;
}

.tile:nth-child(5n + 1) { background: var(--butter); }
.tile:nth-child(5n + 2) { background: var(--seafoam); }
.tile:nth-child(5n + 3) { background: var(--blossom); }
.tile:nth-child(5n + 4) { background: var(--lilac); }
.tile:nth-child(5n + 5) { background: var(--peach); }

.credits {
  margin: 0;
  padding: 0 18px 14px;
  font-size: 0.65rem;
  opacity: 0.45;
  pointer-events: none;
}

/* ---------- Animal puzzle ---------- */

#puzzle-screen {
  position: relative;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  padding: 4px 14px 24px;
  background: linear-gradient(155deg, #E3DCFA 0%, var(--sky) 48%, #C9EDD3 100%);
  touch-action: none;
}

.puzzle-heading {
  text-align: center;
}

.puzzle-heading p {
  margin: 3px 0 0;
  color: #635A91;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.puzzle-heading h2 {
  margin: 2px 0 0;
  font-size: clamp(1.45rem, 5vw, 2.2rem);
}

.puzzle-heading span {
  display: block;
  margin: 2px 0 9px;
  font-size: 0.9rem;
  opacity: 0.62;
}

#puzzle-board,
#puzzle-tray {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  width: min(80vw, 360px, calc((100dvh - 250px) / 2));
  margin-inline: auto;
}

.puzzle-section > h3 {
  width: min(80vw, 360px, calc((100dvh - 250px) / 2));
  margin: 10px auto 5px;
  color: #635A91;
  font-size: 0.76rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

#puzzle-board {
  padding: 7px;
  border-radius: 22px;
  background: #FFF6DA;
  box-shadow: 0 7px 0 rgba(59, 53, 96, 0.12);
}

.puzzle-slot,
.puzzle-piece {
  position: relative;
  aspect-ratio: 1;
  border-radius: 13px;
  background-repeat: no-repeat;
  background-size: 300% 300%;
}

.puzzle-slot {
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 3px dashed rgba(59, 53, 96, 0.25);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.38);
}

.puzzle-slot.filled {
  border-style: solid;
  border-color: rgba(59, 53, 96, 0.12);
}

#puzzle-tray {
  margin-top: 0;
  padding: 8px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.65);
}

.puzzle-piece {
  appearance: none;
  border: 3px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 3px 0 rgba(59, 53, 96, 0.2);
  cursor: grab;
  touch-action: none;
}

.puzzle-piece::after {
  position: absolute;
  inset: 5px;
  border: 1px solid rgba(255, 255, 255, 0.42);
  border-radius: 8px;
  content: "";
  pointer-events: none;
}

.puzzle-piece:focus-visible {
  outline: 4px solid var(--ink);
  outline-offset: 3px;
}

.puzzle-piece.dragging {
  position: fixed;
  z-index: 30;
  margin: 0;
  pointer-events: none;
  cursor: grabbing;
  opacity: 0.94;
  transform: scale(1.08) rotate(3deg);
  box-shadow: 0 12px 20px rgba(20, 18, 39, 0.28);
}

.puzzle-piece.drag-source {
  opacity: 0.24;
}

.puzzle-piece.dropped {
  position: absolute;
  z-index: 3;
  margin: 0;
}

.puzzle-piece.placed {
  box-shadow: none;
  cursor: default;
}

.puzzle-slot.filled > .puzzle-piece {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  aspect-ratio: auto;
  border-radius: 10px;
}

#puzzle-success {
  position: absolute;
  left: 50%;
  top: clamp(120px, 30vw, 190px);
  z-index: 50;
  width: min(86vw, 390px);
  height: min(86vw, 390px);
  pointer-events: none;
  transform: translateX(-50%);
}

#puzzle-success[hidden] { display: none; }

.success-message {
  position: absolute;
  left: 50%;
  bottom: 48%;
  width: max-content;
  color: var(--cream);
  font-size: clamp(2rem, 8vw, 3.5rem);
  line-height: 1;
  opacity: 0;
  text-shadow: 0 4px 0 #635A91, 0 9px 18px rgba(20, 18, 39, 0.32);
  transform: translate(-50%, 30px) scale(0.65);
}

#puzzle-success.playing .success-message {
  animation: success-message 3.5s ease-out forwards;
}

.success-star {
  position: absolute;
  left: 50%;
  bottom: 37%;
  color: #FFD24D;
  font-size: clamp(4.4rem, 20vw, 7rem);
  line-height: 1;
  opacity: 0;
  text-shadow: 0 4px 0 #E7A72A, 0 9px 18px rgba(59, 53, 96, 0.25);
}

#puzzle-success.playing .success-star {
  animation: star-rise 3.4s cubic-bezier(0.12, 0.7, 0.32, 1) forwards;
}

.success-particle {
  position: absolute;
  left: 50%;
  bottom: 43%;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--butter);
  opacity: 0;
}

.success-particle:nth-of-type(2n) { background: var(--blossom); }
.success-particle:nth-of-type(3n) { background: var(--seafoam); }
.success-particle:nth-of-type(4n) { background: var(--lilac); }
.success-particle:nth-of-type(2) { --x: -105px; --y: -120px; }
.success-particle:nth-of-type(3) { --x: -58px; --y: -180px; }
.success-particle:nth-of-type(4) { --x: 18px; --y: -150px; }
.success-particle:nth-of-type(5) { --x: 96px; --y: -124px; }
.success-particle:nth-of-type(6) { --x: 128px; --y: -66px; }
.success-particle:nth-of-type(7) { --x: 54px; --y: -82px; }
.success-particle:nth-of-type(8) { --x: -130px; --y: -62px; }
.success-particle:nth-of-type(9) { --x: -8px; --y: -210px; }

#puzzle-success.playing .success-particle {
  animation: sparkle-rise 1.5s ease-out forwards;
}

#puzzle-screen.puzzle-complete .puzzle-slot.filled {
  pointer-events: none;
  animation: puzzle-piece-celebrate 2.8s ease-in-out forwards;
}

#puzzle-screen.puzzle-complete .puzzle-slot.filled:nth-child(2n) {
  animation-delay: 80ms;
}

#puzzle-screen.puzzle-complete .puzzle-slot.filled:nth-child(3n) {
  animation-delay: 160ms;
}

@keyframes puzzle-piece-celebrate {
  0%, 8% { opacity: 1; transform: rotate(0) scale(1); }
  14% { transform: rotate(-7deg) scale(1.04); }
  20% { transform: rotate(7deg) scale(1.08); }
  26% { transform: rotate(-5deg) scale(1.05); }
  34% { transform: rotate(0) scale(1.12); }
  58% { opacity: 1; transform: translateY(-10px) rotate(4deg) scale(1.06); }
  100% { opacity: 0; transform: translateY(-55px) rotate(16deg) scale(0.25); }
}

@keyframes success-message {
  0% { opacity: 0; transform: translate(-50%, 30px) scale(0.65); }
  14%, 68% { opacity: 1; transform: translate(-50%, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -80px) scale(0.8); }
}

@keyframes star-rise {
  0% { opacity: 0; transform: translate(-50%, 22px) scale(0.45) rotate(-18deg); }
  16% { opacity: 1; transform: translate(-50%, 0) scale(1.12) rotate(9deg); }
  36% { transform: translate(-50%, -42px) scale(0.94) rotate(-6deg); }
  78% { opacity: 1; transform: translate(-50%, -150px) scale(1) rotate(9deg); }
  100% { opacity: 0; transform: translate(-50%, -205px) scale(0.75) rotate(15deg); }
}

@keyframes sparkle-rise {
  0% { opacity: 0; transform: translate(-50%, 0) scale(0.3); }
  18% { opacity: 1; }
  100% { opacity: 0; transform: translate(var(--x), var(--y)) scale(0.9); }
}

/* ---------- Player overlay ---------- */

#overlay {
  position: fixed;
  inset: 0;
  background: var(--night);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* These stay flex when visible, so the UA's [hidden] rule needs backup. */
#overlay[hidden],
#loading[hidden] { display: none; }

.player-frame {
  height: min(100dvh, 177.8vw);
  aspect-ratio: 9 / 16;
  max-width: 100vw;
}

/* The IFrame API replaces the #yt-slot div with an iframe of the same id. */
#yt-slot {
  width: 100%;
  height: 100%;
  border: 0;
}

/* Invisible shield: swallows every toddler tap on the video */
#shield {
  position: absolute;
  inset: 0;
  z-index: 11;
}

/* Loading state: the tapped animal bounces while the video buffers */
#loading {
  position: absolute;
  inset: 0;
  z-index: 12;
  background: var(--night);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
}

#loading-img {
  width: min(38vw, 260px);
  animation: bounce 900ms ease-in-out infinite;
}

#loading-name {
  color: var(--cream);
  font-size: clamp(1.6rem, 5vw, 2.6rem);
  font-weight: 600;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  50%      { transform: translateY(-16px) scale(1.04); }
}

/* Parent escape hatch: must be held down to trigger.
   transition-duration is set from app.js (HOLD_MS) so the visual fill
   always matches the hold time. */
#close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 13;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: rgba(255, 255, 255, 0.75);
  font-size: 1.1rem;
  font-family: inherit;
  cursor: pointer;
  transition-property: transform, background;
  transition-timing-function: linear;
}

#close-btn.holding {
  transform: scale(1.35);
  background: rgba(255, 255, 255, 0.55);
}

/* ---------- Screen-time break ---------- */

#time-limit {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--night);
  color: var(--cream);
  text-align: center;
}

#time-limit[hidden] { display: none; }

.time-limit-card {
  max-width: 420px;
}

.time-limit-icon {
  font-size: clamp(4rem, 18vw, 7rem);
}

#time-limit-title {
  margin: 14px 0 8px;
  font-size: clamp(2rem, 8vw, 3.4rem);
}

#time-limit-message {
  margin: 0;
  font-size: clamp(1.2rem, 4.5vw, 1.8rem);
  line-height: 1.35;
}

/* Idle time pauses screen-time accounting until a deliberate wake tap. */
#idle-dim {
  position: fixed;
  inset: 0;
  z-index: 19;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(20, 18, 39, 0.82);
  color: var(--cream);
  text-align: center;
}

#idle-dim[hidden] { display: none; }

.idle-dim-icon {
  font-size: clamp(3.5rem, 15vw, 6rem);
}

#idle-dim p {
  margin: 12px 0 0;
  font-size: clamp(1.25rem, 5vw, 2rem);
  font-weight: 600;
}

@media (prefers-reduced-motion: reduce) {
  .tile:active { animation: none; }
  #loading-img { animation: none; }
  #close-btn { transition: none; }
  #puzzle-screen.puzzle-complete .puzzle-slot.filled {
    animation: none;
    opacity: 0;
  }
}
