/* Monty's Treasure Sort — Design System */

/* ─── Variables ─────────────────────────────────────────────── */
:root {
  --primary: #0f766e;
  --primary-rgb: 15, 118, 110;
  --primary-container: #f6cf74;
  --on-primary: #f4fffd;
  --on-primary-container: #5a3d00;

  --surface: #fdf8ee;
  --surface-container-low: #f7f0e1;
  --surface-container-lowest: #fffdf8;
  --surface-container-high: #ece2cf;
  --surface-container-highest: #e3d7c0;
  --secondary-container: #c7ece8;
  --on-secondary-container: #0b4f4a;
  --tertiary-container: #ffe1a8;
  --on-tertiary-container: #5a3d00;

  --on-surface: #2b2620;
  --on-surface-variant: #5f574c;
  --outline-variant: #a99c88;

  /* Google Fonts arrive non-blocking (js/fonts.js) and the offline-first
     first run may never see them at all, so the fallback list names real
     system faces instead of a bare sans-serif — matching legal/legal.css. */
  --font-headline: 'Plus Jakarta Sans', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-body: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;

  --radius-md: 0.75rem;
  --radius-lg: 1.5rem;
  --radius-xl: 2rem;
  --radius-full: 9999px;

  --shadow-card: 0 4px 12px rgba(43, 38, 32, 0.07);
  --shadow-modal: 0 20px 40px rgba(43, 38, 32, 0.14);
}

/* ─── Reset ──────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  /* touch-action does NOT inherit — the universal selector is the only
     repo-wide choke point. manipulation kills the double-tap-zoom beat,
     so a toddler's rapid second tap (Back twice for "Stop playing?")
     lands as two taps, not a pinch gesture; panning and pinch-zoom stay.
     Drag zones override with touch-action: none. */
  touch-action: manipulation;
}

html, body {
  height: 100vh; /* Fallback for browsers that do not support dynamic viewport units */
  height: 100dvh;
  font-family: var(--font-body);
  background: var(--surface) url("../assets/backgrounds/treasure-cave.webp?v=6a3ec2c5b9") center / cover no-repeat;
  color: var(--on-surface);
  line-height: 1.5;
  overflow: hidden; /* The game is a contained surface, not a scrolling page. */
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  padding: 0;
}

/* ─── Typography ─────────────────────────────────────────────── */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-headline);
  letter-spacing: -0.02em;
  color: var(--on-surface);
}
h1 { font-size: clamp(1.25rem, 4vw, 2rem);    font-weight: 700; }
h2 { font-size: clamp(1.25rem, 3vw, 1.75rem); font-weight: 600; }
h3 { font-size: clamp(1rem, 2.5vw, 1.25rem);  font-weight: 600; }

/* ─── Loading Overlay ────────────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--primary-container);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20000;
  transition: opacity 0.5s ease-out, visibility 0.5s;
}
.loading-overlay.hidden {
  opacity: 0;
  visibility: hidden;
}

.loader-content {
  text-align: center;
  color: var(--on-primary-container);
  padding: 2rem;
}

.loader-title {
  font-size: clamp(1.5rem, 5vw, 2.5rem);
  margin-bottom: 2rem;
  color: var(--on-primary-container);
}

.noscript-note {
  font-size: clamp(1.1rem, 4vw, 1.5rem);
  font-weight: 600;
  color: var(--on-primary-container);
}

.spinner {
  font-size: clamp(2.5rem, 8vw, 4rem);
  margin-bottom: 1rem;
  color: var(--primary);
  animation: rotateScale 1.5s ease-in-out infinite;
  display: inline-block;
}

.spinner svg {
  width: 1em;
  height: 1em;
  display: block;
}

@keyframes rotateScale {
  0%   { transform: scale(1)   rotate(0deg); }
  50%  { transform: scale(1.2) rotate(180deg); }
  100% { transform: scale(1)   rotate(360deg); }
}

.loader-content p {
  font-family: var(--font-headline);
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-size: clamp(0.75rem, 2vw, 1rem);
}

/* ─── Main Container ─────────────────────────────────────────── */
#game-container {
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  padding: calc(clamp(0.5rem, 1.5vh, 1.25rem) + var(--reserved-top, 0px))
           calc(clamp(1rem, 3vw, 2rem) + env(safe-area-inset-right))
           calc(clamp(0.5rem, 1.5vh, 1.25rem) + var(--reserved-bottom, 0px))
           calc(clamp(1rem, 3vw, 2rem) + env(safe-area-inset-left));
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

body[data-screen="game"] #game-container {
  max-width: 100vw;
  padding: calc(0.25rem + var(--reserved-top, 0px))
           calc(0.5rem + env(safe-area-inset-right))
           calc(0.5rem + var(--reserved-bottom, 0px) + env(safe-area-inset-bottom))
           calc(0.5rem + env(safe-area-inset-left));
  overflow: hidden;
}

body { --reserved-bottom: 0px; --reserved-top: 0px; }

/* ─── Screens ────────────────────────────────────────────────── */
.screen {
  display: none;
  animation: fadeIn 0.3s ease-out;
  height: 100%;
  min-height: 0;
}
.screen.active {
  display: flex;
  flex-direction: column;
}
#home-screen.active,
#difficulty-screen.active,
#settings-screen.active {
  justify-content: center;
}

#victory-screen.active {
  justify-content: flex-start;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ─── Home Screen ────────────────────────────────────────────── */
.welcome-content {
  text-align: center;
  padding: clamp(0.5rem, 3vh, 2rem) clamp(1rem, 5vw, 3rem);
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.hero-brand {
  margin-bottom: clamp(0.75rem, 2.5vh, 1.5rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  animation: fadeInDown 0.8s cubic-bezier(0.23, 1, 0.32, 1);
}

.hero-logo {
  width: clamp(2rem, 6vw, 3.5rem);
  height: clamp(2rem, 6vw, 3.5rem);
  color: var(--primary);
  filter: drop-shadow(0 4px 12px rgba(var(--primary-rgb), 0.22));
}

.hero-title {
  font-size: clamp(1.75rem, 6vw, 3rem);
  font-weight: 800;
  line-height: 1.2;
  background: linear-gradient(135deg, var(--on-primary-container), var(--primary));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: var(--primary); /* fallback when background-clip is unsupported */
  text-shadow: 0 10px 30px rgba(var(--primary-rgb), 0.12);
  padding-bottom: 0.1em;
}

@keyframes fadeInDown {
  from { opacity: 0; transform: translateY(-20px); }
  to   { opacity: 1; transform: translateY(0); }
}

.mascot-area {
  margin-bottom: clamp(0.5rem, 2.5vh, 1.5rem);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
}

.mascot-image-wrapper {
  width: clamp(140px, 35vw, 280px);
  height: clamp(140px, 35vw, 280px);
  max-width: 35vh;
  max-height: 35vh;
  position: relative;
  animation: float 4s ease-in-out infinite;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  overflow: visible;
}

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50%      { transform: translateY(-6px) rotate(-1.5deg); }
}

.mascot-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
  filter: drop-shadow(0 20px 40px rgba(43, 38, 32, 0.12));
  position: relative;
  z-index: 2;
}

.mascot-glow {
  position: absolute;
  width: 120%;
  height: 120%;
  background: radial-gradient(circle, var(--primary-container) 0%, transparent 70%);
  opacity: 0.6;
  filter: blur(40px);
  z-index: 1;
  animation: pulseGlow 4s ease-in-out infinite;
  pointer-events: none;
}

@keyframes pulseGlow {
  0%, 100% { transform: scale(1);   opacity: 0.4; }
  50%      { transform: scale(1.1); opacity: 0.7; }
}

.hero-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.hero-tagline {
  color: var(--on-surface-variant);
  margin-bottom: clamp(0.75rem, 3vh, 1.75rem);
  font-weight: 500;
  font-size: clamp(1rem, 3vw, 1.25rem);
  opacity: 0.9;
}

/* ─── Buttons ────────────────────────────────────────────────── */
.btn-primary {
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: var(--radius-full);
  padding: 0.875rem 2rem;
  font-size: clamp(0.9rem, 2.5vw, 1.125rem);
  font-weight: 600;
  font-family: var(--font-headline);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-primary:hover  { background: var(--on-primary-container); transform: translateY(-2px); }
.btn-primary:active { transform: translateY(0); }

.btn-secondary {
  background: var(--surface-container-high);
  color: var(--on-surface);
  border: none;
  border-radius: var(--radius-full);
  padding: 0.75rem 1.5rem;
  font-size: clamp(0.875rem, 2vw, 1rem);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-secondary:hover { background: var(--surface-container-highest); }
.btn-secondary:active { transform: scale(0.97); }

/* Shared inline icon sizing */
.btn-primary svg,
.btn-secondary svg,
.btn-back svg,
.btn-support svg {
  width: 1.25em;
  height: 1.25em;
  vertical-align: middle;
  margin-right: 0.45em;
}

/* ─── Touch Target Enforcement ───────────────────────────────── */
button,
[role="button"],
input[type="checkbox"],
.btn-primary,
.btn-secondary,
.btn-difficulty,
.btn-back {
  min-height: 56px;
  min-width: 56px;
}

.btn-link {
  min-height: 44px;
  min-width: 44px;
}

.btn-large {
  padding: 1rem 2.5rem;
  font-size: clamp(1rem, 3vw, 1.25rem);
}

.btn-settings {
  margin-top: 0.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Continue chip: appears once the child has played, and replays their last
   mode with the difficulty screen's own pictogram — getting back to the
   game is one tap and no reading. */
.btn-resume {
  margin-top: 0.75rem;
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 3.5rem;
  padding: 0.4rem 1.25rem 0.4rem 0.5rem;
  border-radius: var(--radius-full);
}

.btn-resume[hidden] { display: none; }

.btn-resume .difficulty-icon {
  width: 2.6rem;
  height: 2.6rem;
  flex-shrink: 0;
}

.btn-resume .difficulty-icon img {
  width: 1rem;
  height: 1rem;
}

/* ─── Difficulty Screen ──────────────────────────────────────── */
/* Panels sit over the illustrated cave as translucent glass so the artwork
   keeps company with every screen instead of a wall of white. */
.difficulty-content {
  text-align: center;
  padding: clamp(1rem, 3vw, 2rem);
  background: rgba(255, 253, 248, 0.9);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-card);
  max-width: min(560px, 100%);
  margin: 0 auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
}

.screen-title {
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-size: clamp(1.25rem, 3vw, 1.75rem);
  margin: 0;
}

.screen-title svg {
  width: 1.15em;
  height: 1.15em;
}

.screen-subtitle {
  color: var(--on-surface-variant);
  font-size: clamp(0.9rem, 2vw, 1rem);
  margin-bottom: clamp(0.5rem, 1.5vh, 1rem);
}

.difficulty-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 100%;
  max-width: 420px;
  margin: 0 auto clamp(0.75rem, 2vh, 1.25rem);
}

.btn-difficulty {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 0.85rem;
  align-items: center;
  text-align: left;
  background: var(--secondary-container);
  color: var(--on-secondary-container);
  border: none;
  border-radius: var(--radius-lg);
  padding: 0.9rem 1.25rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.btn-difficulty:hover {
  background: var(--tertiary-container);
  color: var(--on-tertiary-container);
}
.btn-difficulty:active { transform: scale(0.98); }

/* Rule pictograms, not decoration: easy = the same gem in three colours,
   medium = three treasures sharing one colour, hard = three different
   treasures with a cycle arrow for "the rule rotates". A child who cannot
   read yet can still pick the mode they can actually play. Built from
   existing precached treasure art, so no new assets. */
.difficulty-icon {
  grid-row: 1 / span 2;
  position: relative;
  width: 3rem;
  height: 3rem;
  border-radius: var(--radius-full);
  background: var(--surface-container-lowest);
  color: var(--primary);
}

.difficulty-icon img {
  position: absolute;
  width: 1.15rem;
  height: 1.15rem;
  display: block;
  object-fit: contain;
}

.difficulty-icon img:nth-child(1) { left: 0.3rem; top: 1rem; transform: rotate(-10deg); }
.difficulty-icon img:nth-child(2) { left: 0.92rem; top: 0.45rem; }
.difficulty-icon img:nth-child(3) { right: 0.3rem; top: 1.05rem; transform: rotate(10deg); }

.difficulty-cycle {
  position: absolute;
  right: 0.25rem;
  bottom: 0.2rem;
  width: 0.95rem;
  height: 0.95rem;
}

.difficulty-name {
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: clamp(1rem, 2.5vw, 1.2rem);
  line-height: 1.2;
}

.difficulty-meta {
  font-size: clamp(0.8rem, 2vw, 0.9rem);
  opacity: 0.9;
}

/* ─── Game Screen ────────────────────────────────────────────── */
.game-header-bar {
  background: rgba(255, 253, 248, 0.86);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  padding: clamp(0.5rem, 1.5vw, 1rem) clamp(0.75rem, 2vw, 1.5rem);
  border-radius: var(--radius-lg);
  margin-bottom: clamp(0.5rem, 2vw, 1rem);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  box-shadow: var(--shadow-card);
  gap: 1rem;
  flex-shrink: 0;
}

.game-header-bar .screen-title {
  font-size: clamp(1rem, 2.5vw, 1.35rem);
  white-space: nowrap;
}

.btn-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  justify-self: start;
  background: var(--surface-container-high);
  color: var(--on-surface);
  border: none;
  border-radius: var(--radius-full);
  padding: 0.4rem 1rem;
  font-size: clamp(0.8rem, 2vw, 1rem);
  cursor: pointer;
  transition: background 0.2s;
}
.btn-back:hover { background: var(--surface-container-highest); }

.header-right {
  justify-self: end;
}

/* The hoard, baskets and items are built by the game controller. */
.treasure-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
.game-stage {
  --basket-size: min(28vw, 120px);
  --item-size: clamp(112px, 34vw, 190px);
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: clamp(0.5rem, 1.5vh, 1rem);
  touch-action: manipulation;
}

.item-slot {
  flex: 1;
  min-height: 0;
  position: relative;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 50%, rgba(246, 207, 116, 0.16), transparent 62%);
}

.basket-tray {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: clamp(0.5rem, 2vw, 1rem);
  padding: 0.25rem 0 0.5rem;
  /* A soft ground shadow so the chests sit on the illustrated sand rather
     than float over it. */
  background: radial-gradient(ellipse 84% 66% at 50% 90%, rgba(90, 61, 0, 0.14), transparent 72%);
}

.round-progress {
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: clamp(0.8rem, 2vw, 0.95rem);
  color: var(--on-surface-variant);
  background: var(--surface-container-low);
  border-radius: var(--radius-full);
  padding: 0.25rem 0.7rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* The sibling pill bar: a child who cannot read "7 / 12" still sees how
   much of the hoard is sorted and how much is left. */
.round-progress-bar {
  position: relative;
  display: block;
  width: clamp(3rem, 12vw, 5.5rem);
  height: 0.5rem;
  border-radius: var(--radius-full);
  background: var(--surface-container-low);
  box-shadow: inset 0 1px 2px rgba(43, 38, 32, 0.18);
  overflow: hidden;
}

/* Hard mode is three 8-item chapters; two hairline ticks at the thirds
   turn the pill into a chapter map the child can feel progress through. */
.round-progress-bar.is-segmented::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg, transparent calc(33.33% - 1px), rgba(43, 38, 32, 0.35) calc(33.33% - 1px) calc(33.33% + 1px), transparent calc(33.33% + 1px)),
    linear-gradient(90deg, transparent calc(66.66% - 1px), rgba(43, 38, 32, 0.35) calc(66.66% - 1px) calc(66.66% + 1px), transparent calc(66.66% + 1px));
}

.round-progress-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #f6cf74, #eba93f);
  transition: width 0.3s ease-out;
}

/* ─── Baskets: treasure chests ───────────────────────────────────
   The chest artwork is the shared #chest-art sprite drawn by Basket;
   the button itself stays a plain, full-size hit target so the drop
   area never shrinks with the art. */
.basket {
  position: relative;
  width: var(--basket-size);
  height: calc(var(--basket-size) * 0.92);
  min-width: 72px;
  min-height: 64px;
  border: none;
  padding: 0;
  background: transparent;
  border-radius: 14px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.18s;
  touch-action: manipulation;
}

.basket[hidden] { display: none; }

.basket-icon {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: block;
}

/* The size rule now reads three ways: 2x treasure widths, 34 px vs
   16 px gold marks on the chests, and the chest bodies themselves —
   the small chest's art is drawn smaller, the large one's bigger, so
   the pair side by side looks like the rule. Transform on the art
   wrapper only: the button box — the touch target and the drop zone —
   never moves, and no chest animation touches .basket-icon. */
.basket[data-basket-id=size-small] .basket-icon { transform: scale(0.82); }
.basket[data-basket-id=size-large] .basket-icon { transform: scale(1.12); }

.basket-svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
  /* Composed 128px pixel chests, exported at 2x: keep the grid square. */
  image-rendering: pixelated;
  filter: drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22));
  transition: filter 0.18s;
}

.basket:hover { transform: translateY(-2px); }

.basket:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 3px;
}

.basket.is-target {
  transform: translateY(-6px) scale(1.05);
}

/* A tap-held treasure invites the chests: each one bobs once in a
   position-staggered wave saying "a tap here counts", never revealing
   which chest is right. The global reduce-motion rule stops it after a
   single pass. */
body.has-selected-item .basket:not(.is-target) {
  animation: basketInvite 1.4s ease-in-out infinite;
  animation-delay: var(--invite-delay, 0ms);
}

@keyframes basketInvite {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

.basket.is-target .basket-svg {
  filter: drop-shadow(0 0 10px var(--primary-container))
          drop-shadow(0 12px 18px rgba(43, 38, 32, 0.2));
}

.basket.is-correct {
  animation: basketPop 0.45s ease-out;
}

.basket.is-correct .basket-svg {
  filter: drop-shadow(0 0 14px rgba(255, 220, 120, 0.95))
          drop-shadow(0 8px 12px rgba(43, 38, 32, 0.2));
}

.basket.is-wrong {
  animation: basketWobble 0.4s ease-in-out;
}

.basket.is-wrong .basket-svg {
  filter: grayscale(0.45) brightness(0.9)
          drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22));
}

.basket.is-hint .basket-svg {
  animation: basketHint 0.75s ease-in-out 2;
}

.basket.is-relabelling .basket-svg {
  animation: basketRelabel 0.3s ease-out var(--swap-delay, 0ms);
}

@keyframes basketPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.08) translateY(-4px); }
  100% { transform: scale(1); }
}

@keyframes basketWobble {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px) rotate(-1deg); }
  75% { transform: translateX(5px) rotate(1deg); }
}

@keyframes basketHint {
  0%, 100% { filter: drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22)); }
  50% { filter: drop-shadow(0 0 13px rgba(246, 207, 116, 0.9))
                 drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22)); }
}

@keyframes basketRelabel {
  from { opacity: 0; transform: scale(0.8) rotate(-5deg); }
  to { opacity: 1; transform: scale(1) rotate(0); }
}

/* ─── Treasure items ─────────────────────────────────────────── */
.treasure-item {
  position: relative;
  z-index: 5;
  width: var(--item-size);
  aspect-ratio: 1;
  padding: 0;
  border: none;
  background: transparent;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  filter: drop-shadow(0 0 10px rgba(246, 207, 116, 0.45))
          drop-shadow(0 12px 18px rgba(43, 38, 32, 0.18));
  transition: filter 0.2s, transform 0.2s;
}

/* The size RULE is how big the treasure looks; the touch floor is a
   separate contract and it WINS — every button is at least 56 px, so a
   small button box used to clamp straight back to 56 px and the child
   saw two identical sizes (the 2x only survived on screens whose
   --item-size exceeded 112 px). The hit box therefore stays one
   uniform, always-tappable square and the ART inside it carries the
   rule: small art is half the box, large fills it — the same 0.5 the
   SIZE_SCALES promise, and it now holds at every viewport. */
.treasure-item--small .treasure-img { width: 50%; height: 50%; }
.treasure-item--large { width: var(--item-size); }

.treasure-item:hover {
  filter: drop-shadow(0 16px 24px rgba(43, 38, 32, 0.22));
  transform: translateY(-2px) scale(1.02);
}

.treasure-item:focus-visible {
  outline: 4px solid var(--primary);
  outline-offset: 8px;
  border-radius: 28px;
}

.treasure-item.is-selected {
  outline: 4px solid var(--primary-container);
  outline-offset: 7px;
  border-radius: 28px;
  filter: drop-shadow(0 0 18px rgba(246, 207, 116, 0.8));
  transform: scale(1.05);
}

.treasure-item.is-pressed { cursor: grabbing; }

.treasure-item.is-dragging {
  transition: none;
  z-index: 40;
  cursor: grabbing;
  filter: drop-shadow(0 22px 26px rgba(43, 38, 32, 0.24));
}

.treasure-item.is-returning {
  transition: transform 0.34s cubic-bezier(0.2, 0.8, 0.3, 1), filter 0.2s;
}

.treasure-item.is-snapping {
  z-index: 30;
  pointer-events: none;
}

.treasure-item.is-fading {
  transition: opacity 0.16s ease-out;
}

/* Arrival cue: a new treasure pops into view so young eyes catch where the
   next one lands. No forwards fill, and any grab, return or snap cancels
   the pop outright — the child's own transform always wins. Reduce-motion
   collapses it to a blink, like everything else. */
.treasure-item.is-arriving {
  animation: treasureArrive 0.45s cubic-bezier(0.2, 0.8, 0.3, 1.2) backwards;
}

.treasure-item.is-arriving.is-dragging,
.treasure-item.is-arriving.is-selected,
.treasure-item.is-arriving.is-returning,
.treasure-item.is-arriving.is-snapping {
  animation: none;
}

@keyframes treasureArrive {
  0% { transform: scale(0.3); opacity: 0; }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

.treasure-img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  /* Native 128px PixelEngine sprites: nearest-neighbour keeps the pixel grid
     square instead of resampling the art into mush at tablet sizes. */
  image-rendering: pixelated;
  user-select: none;
  -webkit-user-drag: none;
}

body.is-dragging-item {
  touch-action: none;
  overflow: hidden;
  cursor: grabbing;
}

/* ─── Sparkles ───────────────────────────────────────────────── */
.sparkle-layer {
  position: absolute;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

.sparkle {
  position: absolute;
  width: 10px;
  height: 10px;
  margin: -5px 0 0 -5px;
  border-radius: 50%;
  background: var(--primary-container);
  box-shadow: 0 0 8px #fff3c4;
  animation: sparkleOut 0.62s ease-out forwards;
}

@keyframes sparkleOut {
  from { opacity: 1; transform: translate(0, 0) scale(1); }
  to { opacity: 0; transform: translate(var(--dx), var(--dy)) scale(0.2); }
}

/* ─── Victory screen ─────────────────────────────────────────── */
.victory-panel {
  width: min(560px, 100%);
  padding: clamp(1rem, 3vw, 2rem);
  border-radius: var(--radius-xl);
  background: rgba(255, 253, 248, 0.9);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.victory-monty {
  width: clamp(110px, 22vh, 200px);
  height: auto;
  object-fit: contain;
  image-rendering: pixelated;
}

.victory-title {
  color: var(--primary);
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  margin: 0;
}

/* Focused programmatically so screen readers announce the result; it is a
   heading, not a control, so no visible ring is needed. */
.victory-title:focus { outline: none; }

.victory-difficulty {
  color: var(--on-surface-variant);
  font-weight: 600;
  margin: 0;
}

.victory-stars {
  display: flex;
  justify-content: center;
  gap: 0.35rem;
  margin: 0.25rem 0;
}

.victory-star {
  width: clamp(2.4rem, 8vw, 3.4rem);
  height: clamp(2.4rem, 8vw, 3.4rem);
  fill: none;
  stroke: var(--outline-variant);
  stroke-width: 1.5;
  opacity: 0.55;
}

.victory-star.is-earned {
  fill: var(--primary-container);
  stroke: #5a3d00;
  opacity: 1;
  animation: starPop 0.5s cubic-bezier(0.2, 0.8, 0.3, 1.25) both;
  animation-delay: var(--star-delay, 0ms);
}

.victory-star.is-locked {
  fill: var(--surface-container-high);
  stroke: var(--outline-variant);
}

@keyframes starPop {
  0% { transform: scale(0.4) rotate(-16deg); opacity: 0; }
  70% { transform: scale(1.12) rotate(4deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

.victory-accuracy {
  margin: 0;
  color: var(--on-surface);
  font-weight: 600;
  /* The first-try count rolls up on arrival; tabular figures stop jitter. */
  font-variant-numeric: tabular-nums;
}

.victory-next-star {
  margin: 0;
  color: var(--on-surface-variant);
  font-size: 0.95rem;
}

.victory-time {
  margin: 0;
  color: var(--on-surface-variant);
  font-size: 0.95rem;
  /* The time counts up on arrival; tabular figures keep it from jittering. */
  font-variant-numeric: tabular-nums;
}

.victory-best {
  margin: 0;
  padding: 0.25rem 0.75rem;
  border-radius: var(--radius-full);
  background: var(--primary-container);
  color: var(--on-primary-container);
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.9rem;
}

.victory-badges { width: 100%; }

.victory-badges[hidden] { display: none; }

.victory-badges-title {
  margin: 0.25rem 0 0.4rem;
  text-align: center;
  color: var(--on-secondary-container);
  font-family: var(--font-headline);
  font-weight: 700;
}

.victory-badges-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(78px, 1fr));
  gap: 0.5rem;
  width: 100%;
}

.victory-badges-list .badge-tile {
  padding: 0.5rem 0.35rem;
  background: var(--secondary-container);
  animation: badgeReveal 0.5s cubic-bezier(0.2, 0.8, 0.3, 1.25) both;
  animation-delay: var(--badge-delay, 0ms);
}

@keyframes badgeReveal {
  0% { transform: scale(0.4) translateY(10px); opacity: 0; }
  70% { transform: scale(1.1) translateY(0); opacity: 1; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}

.victory-badges-list .badge-medallion {
  width: clamp(38px, 10vw, 54px);
}

.victory-badges-list .badge-blurb { display: none; }

.victory-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

/* Pictograms first, like the difficulty buttons: the victory choice
   ("do that again" vs "go home") must be readable without reading. The
   words stay for grown-ups and screen readers; the svgs are aria-hidden
   because the button label already says it. One selector per rule on
   purpose: the validator reads a comma group as one compound and reads
   its inflated specificity against the hidden-element guards, and
   .btn-secondary is shared with the hidden Continue chip. */
.victory-buttons .btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.victory-buttons .btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.victory-buttons button svg {
  width: 1.35rem;
  height: 1.35rem;
  flex-shrink: 0;
}

/* ─── Best scores (difficulty screen) ────────────────────────── */
.best-scores {
  width: 100%;
  max-width: 420px;
  margin: 0 auto clamp(0.5rem, 1.5vh, 0.9rem);
}

.best-scores-title {
  margin: 0 0 0.35rem;
  color: var(--on-surface-variant);
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.best-scores-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.best-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.6rem;
  border-radius: var(--radius-md);
  background: var(--surface-container-low);
  font-size: 0.875rem;
}

.best-name {
  min-width: 4.5rem;
  text-align: left;
  font-weight: 600;
  color: var(--on-surface);
}

.best-stars {
  color: #8a5a00;
  letter-spacing: 0.12em;
}

.best-time {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  color: var(--on-surface-variant);
}

.best-row.is-empty .best-time {
  font-size: 0.78rem;
  opacity: 0.75;
}

/* ─── Monty on the stage ─────────────────────────────────────
   Monty sits in the top-left corner of the stage for the whole round.
   Rules and encouragement pop out of him as a speech bubble: the
   bubble fades, he never leaves. The full masthead sticker keeps him
   instantly recognisable at any size. */
.monty-companion {
  position: absolute;
  top: 0.25rem;
  left: clamp(0.5rem, 2vw, 1.25rem);
  z-index: 18;
  width: clamp(52px, min(14vw, 16vh), 120px);
  pointer-events: none;
}

.monty-companion-img {
  width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
  user-select: none;
  -webkit-user-drag: none;
  filter: drop-shadow(0 3px 6px rgba(90, 61, 0, 0.28));
}

/* A pre-reader's instinctive way to "pick up" a treasure is to press
   and HOLD it — and on iOS a long press on any <img> pops the system
   callout (Copy / Add to Photos / Look Up), which steals the whole
   round mid-grab. user-select: none does NOT stop it; only
   -webkit-touch-callout does. Every art zone on the child's path:
   home mascot, Monty sticker, treasure art, chests, the victory
   celebration, and the difficulty card art. */
.mascot-image,
.monty-companion-img,
.treasure-img,
.basket-svg,
.victory-monty,
.difficulty-icon img {
  -webkit-touch-callout: none;
}

/* A little celebratory hop every time a treasure lands correctly.
   Only the sticker hops; the speech bubble stays put. */
.monty-companion-img.is-cheering {
  animation: montyCheer 620ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes montyCheer {
  0% { transform: translateY(0) scale(1) rotate(0deg); }
  30% { transform: translateY(-9%) scale(1.07) rotate(-4deg); }
  55% { transform: translateY(0) scale(0.98) rotate(2deg); }
  75% { transform: translateY(-4%) scale(1.03) rotate(0deg); }
  100% { transform: translateY(0) scale(1) rotate(0deg); }
}

.monty-bubble {
  position: absolute;
  left: calc(100% - 0.5rem);
  top: 8%;
  z-index: 19;
  display: flex;
  align-items: center;
  /* Monty's box is narrow; the bubble sizes to its words up to a cap. */
  width: max-content;
  max-width: min(60vw, 300px);
  padding: 0.4rem 0.85rem;
  border-radius: var(--radius-full);
  background: var(--surface-container-lowest);
  box-shadow: var(--shadow-card);
  opacity: 0;
  transform: translateX(-6px);
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
  pointer-events: none;
}

.monty-bubble[hidden] { display: none; }

.monty-bubble.is-visible {
  opacity: 1;
  transform: translateX(0);
}

/* The tail points back at Monty's face. */
.monty-bubble::before {
  content: "";
  position: absolute;
  left: -0.25rem;
  bottom: 0.85rem;
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 2px;
  background: var(--surface-container-lowest);
  transform: rotate(45deg);
}

.monty-bubble-text {
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--on-surface);
}

body[data-orientation="landscape"] .monty-companion {
  top: 0.5rem;
  left: 0.75rem;
}

body[data-orientation="landscape"] .monty-bubble {
  max-width: min(38vw, 300px);
}

/* ─── Badges ─────────────────────────────────────────────────── */
.settings-badges {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--surface-container-high);
}

.settings-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.settings-section-head h3 {
  margin: 0;
  color: var(--primary);
  font-size: 1rem;
}

.badge-count {
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--on-primary-container);
  background: var(--primary-container);
  padding: 0.15rem 0.55rem;
  border-radius: var(--radius-full);
}

.badges-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(118px, 1fr));
  gap: 0.5rem;
  width: 100%;
}

.badge-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.55rem 0.4rem;
  border-radius: var(--radius-md);
  background: var(--surface-container-low);
  text-align: center;
  min-width: 0;
}

.badge-medallion {
  width: clamp(42px, 12vw, 58px);
  aspect-ratio: 1;
  display: block;
}

.badge-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.badge-name {
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--primary);
  line-height: 1.2;
}

.badge-blurb {
  font-size: 0.68rem;
  line-height: 1.3;
  color: var(--on-surface-variant);
}

.badge-tile.locked .badge-svg {
  filter: grayscale(1);
  opacity: 0.55;
}

.badge-tile.locked .badge-name { color: var(--on-surface-variant); }

.settings-status {
  min-height: 1.2em;
  margin: 0.5rem 0 0;
  text-align: center;
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 600;
}

/* ─── Reset control ──────────────────────────────────────────── */
.btn-reset {
  color: var(--primary);
  font-weight: 600;
  flex-shrink: 0;
}

/* ─── In-app reduced motion ──────────────────────────────────── */
body.reduce-motion *,
body.reduce-motion *::before,
body.reduce-motion *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
  scroll-behavior: auto !important;
}

body.reduce-motion .sparkle { display: none; }

body.reduce-motion .basket.is-hint .basket-svg {
  filter: drop-shadow(0 0 9px var(--primary-container))
          drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22));
}

body.reduce-motion .victory-star.is-earned {
  animation: none;
  opacity: 1;
}

body.reduce-motion .victory-badges-list .badge-tile {
  animation: none;
  opacity: 1;
}

/* ─── Landscape game layout ──────────────────────────────────── */
body[data-orientation="landscape"] .game-stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr);
  gap: clamp(0.5rem, 1.5vw, 1.25rem);
}

body[data-orientation="landscape"] .item-slot {
  grid-column: 1;
  grid-row: 1;
}

body[data-orientation="landscape"] .basket-tray {
  grid-column: 2;
  grid-row: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 0.25rem;
}

/* Short viewports: the header gives back vertical room and Monty shrinks
   with it, so the stage never feels crowded. */
@media (max-height: 700px) {
  .game-header-bar {
    padding: 0.35rem 0.6rem;
    margin-bottom: 0.35rem;
  }
  .game-stage { gap: 0.35rem; }
  .basket-tray { padding-bottom: 0.25rem; gap: 0.4rem; }
  .monty-companion { width: clamp(44px, min(13vw, 13vh), 96px); }
  .best-scores { margin-bottom: 0.35rem; }
  .best-scores-title { font-size: 0.7rem; margin-bottom: 0.2rem; }
  .best-row { padding: 0.15rem 0.45rem; font-size: 0.78rem; }
  .settings-badges { margin-top: 0.75rem; padding-top: 0.6rem; }
  /* Short screens must show both victory choices without the child
     hunting for scroll at the exact moment they are too busy celebrating.
     Monty shrinks rather than disappears; the shelf tiles tighten too. */
  .victory-content { gap: 0.2rem; padding: 0.5rem 0.75rem; }
  .victory-monty { width: clamp(76px, 13vh, 120px); }
  .victory-stars { margin: 0; }
  .victory-star {
    width: clamp(1.9rem, 6.5vh, 2.6rem);
    height: clamp(1.9rem, 6.5vh, 2.6rem);
  }
  .victory-badges-list { gap: 0.3rem; }
  .victory-badges-list .badge-tile { padding: 0.3rem 0.25rem; }
  .victory-badges-list .badge-medallion { width: clamp(30px, 7vh, 40px); }
  .victory-buttons { margin-top: 0.25rem; }
}

/* The very shortest phones (568px class) get one more squeeze so both
   victory buttons land fully on-screen; text stays text, Monty stays
   Monty, just smaller. */
@media (max-height: 600px) {
  /* Short screens: Monty's words wrap tall under big type and the
     glass lands ON the treasure (320x568 at 200%: bubble bottom 280
     over slot top 231). 24 px binds only ABOVE 150% — at 100% the
     base 0.95rem is 15.2 px, at 150% it is 22.8 px and the glass
     already clears by 43 px — so scaling users ride free to 150%,
     and 200% wears the 150% size to keep the board visible.
     Sideways overrides this at 15 px (later rule, element-scoped
     selector). */
  .monty-bubble-text { font-size: 19px; }
  .victory-title { margin: 0; }
  .victory-monty { width: clamp(56px, 11vh, 96px); }
  .victory-star {
    width: clamp(1.5rem, 5.5vh, 2.1rem);
    height: clamp(1.5rem, 5.5vh, 2.1rem);
  }
  .victory-badges-title { margin: 0; font-size: 0.85rem; }
  .victory-badges-list .badge-tile { padding: 0.2rem; }
  .victory-badges-list .badge-medallion { width: clamp(24px, 5.5vh, 34px); }
  .victory-badges-list .badge-name { font-size: 0.72rem; }
  .badge-tile { padding: 0.4rem 0.3rem; }
}

/* The blanket reduced-motion rule above shortens every animation; these
   remove the two decorative effects that do not have a useful static form. */
@media (prefers-reduced-motion: reduce) {
  .sparkle { display: none; }
  .treasure-item.is-selected,
  .treasure-item.is-returning { transition: none; }
  /* The pulse is an animation, so the blanket rule would erase the only
     persistent hint cue. Keep a static glow for its 1.6s lifetime. */
  .basket.is-hint .basket-svg {
    filter: drop-shadow(0 0 9px var(--primary-container))
            drop-shadow(0 7px 12px rgba(43, 38, 32, 0.22));
  }
  .victory-star.is-earned {
    animation: none;
    opacity: 1;
  }
}

@media (forced-colors: active) {
  .basket,
  .treasure-item {
    border: 2px solid currentColor;
  }

  /* The pill toggle keeps working without colors: the track gains a real
     border and the knob becomes a solid system-colored dot; the checked
     state is still readable because the knob translates. */
  .setting-item input[type="checkbox"]::before {
    background: Field;
    border: 1px solid CanvasText;
  }

  .setting-item input[type="checkbox"]::after {
    background: CanvasText;
    box-shadow: none;
  }
}

/* ─── Victory Screen ─────────────────────────────────────────── */
.victory-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: clamp(0.5rem, 2vh, 1rem);
  text-align: center;
  padding: clamp(0.75rem, 2vh, 1.25rem);
  overflow-y: auto;
}

/* ─── Settings Screen ────────────────────────────────────────── */
.settings-content {
  text-align: left;
  background: rgba(255, 253, 248, 0.92);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  padding: clamp(1.25rem, 3vw, 2rem);
  width: min(460px, 100%);
  max-height: 100%;
  overflow-y: auto;
  margin: 0 auto;
}

/* Settings has one exit, and on short screens the badge collection pushes
   it below the fold — grown-ups should never have to scroll to leave. Back
   sticks to the bottom lip of the scrolling panel, wide as the panel, over
   a fade of the panel's own surface so scrolled content slips under it. */
.settings-content .modal-actions {
  position: sticky;
  bottom: 0;
  margin-left: calc(clamp(1.25rem, 3vw, 2rem) * -1);
  margin-right: calc(clamp(1.25rem, 3vw, 2rem) * -1);
  padding-top: 1.25rem;
  padding-bottom: calc(clamp(1.25rem, 3vw, 2rem));
  background: linear-gradient(to bottom, rgba(255, 253, 248, 0), rgba(255, 253, 248, 0.97) 45%);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.settings-content .screen-title {
  justify-content: flex-start;
  margin-bottom: 1rem;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.875rem 0;
  border-bottom: 1px solid var(--surface-container-high);
}
.setting-item:last-of-type { border-bottom: none; }
.setting-item label { font-weight: 500; color: var(--on-surface); }

.setting-text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}

.setting-item small {
  display: block;
  color: var(--on-surface-variant);
  font-size: 0.75rem;
  line-height: 1.4;
}

/* Grown-up toggles, but still honest touch targets: a 54×44 hit area
   (above the 44px floor) rendered as a chunky pill with a sliding knob.
   Native checkbox semantics (label for=, keyboard space) are untouched. */
.setting-item input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  width: 54px;
  height: 44px;
  flex-shrink: 0;
  margin: 0;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.setting-item input[type="checkbox"]::before {
  content: "";
  position: absolute;
  top: 11px;
  left: 0;
  right: 0;
  height: 22px;
  border-radius: var(--radius-full);
  background: var(--surface-container-high);
  transition: background-color 0.2s ease;
}

.setting-item input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 13px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface-container-lowest);
  box-shadow: 0 1px 3px rgba(43, 38, 32, 0.35);
  transition: transform 0.2s ease;
}

.setting-item input[type="checkbox"]:checked::before {
  background: var(--primary-container);
}

.setting-item input[type="checkbox"]:checked::after {
  transform: translateX(28px);
}

/* The analytics switch while the parental gate is deciding (app.js adds
   is-pending and aria-busy): the knob parks mid-pill and pulses gently,
   because consent only lands if the grown-up passes — promising the click
   already worked would be a lie, snapping it back is a flinch. Listed
   after the :checked rules so it wins the specificity tie. */
.setting-item.is-pending input[type="checkbox"]::after {
  transform: translateX(14px);
  animation: knobPending 0.9s ease-in-out infinite;
}

@keyframes knobPending {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

body.reduce-motion .setting-item.is-pending input[type="checkbox"]::after {
  animation: none;
}

.setting-item input[type="checkbox"]:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-full);
}

.modal-actions { margin-top: 1.5rem; text-align: center; }

.legal-links {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--surface-container-high);
  display: flex;
  gap: 1rem;
  justify-content: center;
  font-size: 0.875rem;
  flex-wrap: wrap;
}
.legal-links a { color: var(--on-surface-variant); text-decoration: none; }
.legal-links a:hover { color: var(--primary); }

/* ─── Footer ─────────────────────────────────────────────────── */
.game-footer {
  margin-top: auto;
  padding: clamp(0.5rem, 1.5vh, 1.25rem) clamp(1rem, 3vw, 2rem);
  background: rgba(247, 240, 225, 0.82);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  border-top-left-radius: var(--radius-xl);
  border-top-right-radius: var(--radius-xl);
  flex-shrink: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

body[data-screen="game"] .game-footer,
body[data-screen="victory"] .game-footer,
body[data-screen="settings"] .game-footer {
  display: none;
}

.footer-content {
  max-width: 800px;
  margin: 0 auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.footer-top {
  display: flex;
  justify-content: center;
  align-items: center;
}
.footer-top[hidden] { display: none; }

.footer-links {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.footer-links a,
.footer-links .btn-link {
  color: var(--on-surface-variant);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  transition: color 0.2s;
}

/* Grown-up exit links sit in the first screen a parent sees: they clear
   the same 44 px touch floor the game itself obeys (live census; the
   bare anchors measured 42x21 / 52x21 before this). */
.footer-links a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
}


.footer-links a:hover,
.footer-links .btn-link:hover { color: var(--primary); }
.footer-links span { color: var(--outline-variant); user-select: none; }

.footer-copyright p {
  color: var(--on-surface-variant);
  font-size: 0.75rem;
  font-weight: 500;
  opacity: 0.8;
}
.footer-copyright[hidden] { display: none; }

/* ─── Legal / Support Buttons ────────────────────────────────── */
.btn-support {
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: var(--radius-full);
  padding: 0.6rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-support:hover {
  background: var(--on-primary-container);
  transform: translateY(-2px);
}

.btn-link {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-link:hover { color: var(--primary); }

/* ─── Install Prompt ─────────────────────────────────────────── */
.install-prompt {
  position: fixed;
  bottom: 1.5rem;
  /* At 200% type with the iOS steps the card outgrew the whole screen
     and lost 426 px of text above the top edge — unreachable, no
     sideways scroll. Same contract as the Privacy Chest: cap the card
     and let IT scroll. */
  /* vh first: an engine without dvh DROPS the dvh line wholesale and
     silently loses the cap+scroller contract (the 426 px overflow
     would return). Same rule as body height at the top of this file. */
  max-height: calc(100vh - 3rem);
  max-height: calc(100dvh - 3rem);
  overflow-y: auto;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-container-lowest);
  padding: 0.875rem 1.25rem;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  z-index: 9999;
  width: min(420px, 92vw);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  animation: slideUp 0.3s ease-out;
}

/* Longest unbreakable content sets a floor on every flex child unless
   each says otherwise: at 200% type the step chips pushed the "Not
   Now" button 114 px past the screen. Every card child may shrink to
   its column. */
.install-prompt > * { min-width: 0; }

/* The button pair wraps too — the update banner's lesson: at 200%
   type the two buttons together outgrew even the whole card. */
.install-actions { flex-wrap: wrap; }

/* The prompt belongs on the home screen only. */
body:not([data-screen="home"]) .install-prompt { display: none; }

/* An author display rule beats the UA rule behind the hidden attribute. */
.install-prompt[hidden] { display: none; }

.install-steps {
  margin-top: 0.5rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.625rem;
  background: var(--surface-container-low);
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  color: var(--on-surface-variant);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.install-steps[hidden] { display: none; }
.install-step { display: flex; align-items: center; gap: 0.4rem; }
.install-step-sep { color: var(--outline-variant); }
.install-step-num {
  width: 1.25rem;
  height: 1.25rem;
  border-radius: var(--radius-full);
  background: var(--primary-container);
  color: var(--on-primary-container);
  font-family: var(--font-headline);
  font-weight: 700;
  font-size: 0.75rem;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.install-content h3 {
  color: var(--primary);
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.install-content h3 svg { width: 1.1em; height: 1.1em; }
.install-content p { color: var(--on-surface-variant); font-size: 0.875rem; margin-top: 0.25rem; }

.install-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

/* ─── Offline Banner ─────────────────────────────────────────── */
.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--on-surface);
  color: var(--surface-container-lowest);
  padding: 0.75rem 1rem;
  text-align: center;
  font-size: 0.875rem;
  font-weight: 500;
  z-index: 10000;
  display: none;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}
.offline-banner:not([hidden]) { display: flex; }
.offline-banner svg { width: 1.25em; height: 1.25em; }

/* Fallback reserve for the frame before js/offline-status.js measures the
   banner inline (it measures synchronously on show, and a ResizeObserver
   follows reflow). This approximation only applies if that measurement
   fails, so it just needs to be roughly the banner's height. */
body.offline-banner-visible { --reserved-top: 4.25rem; }

/* ─── Update Banner ──────────────────────────────────────────── */
.update-banner {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary-container);
  color: var(--on-primary-container);
  padding: 1rem 1.5rem;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  z-index: 9999;
  width: min(400px, 90vw);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  animation: slideUp 0.3s ease-out;
}

/* Big grown-up type pushes the actions off the RIGHT of the card — and
   off the screen: at 150/200% on a 320 px phone Later/Reload measured
   right edges 463/612 px, unreachable with no sideways scroll. The
   wrap puts the buttons on their own centred row whenever the words
   need the room; 100% type never wraps. */
.update-banner > .update-actions {
  margin-inline: auto;
}
.update-banner[hidden] { display: none; }
.update-banner svg { width: 1.25em; height: 1.25em; flex-shrink: 0; }

.update-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.btn-small {
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
}

/* ─── Accessibility ──────────────────────────────────────────── */
button:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

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

@media (prefers-contrast: high), (prefers-contrast: more) {
  :root { --primary: #000; --on-primary: #fff; }
  .basket,
  .treasure-item { border: 3px solid #000; }
  .best-stars { color: #000; }
  .victory-star.is-earned { fill: #000; stroke: #000; }
}

/* High contrast mode overrides */
@media (forced-colors: active) {
  :root {
    --primary: #006400;
    --on-primary: #ffffff;
    --surface: #ffffff;
    --on-surface: #000000;
  }
  /* The illustration is decoration; drop it and the glass so panels keep
     the platform's own colours. */
  html, body { background-image: none; }
  .monty-companion-img { display: none; }
  .monty-bubble {
    background: Canvas;
    color: CanvasText;
    box-shadow: none;
    border: 2px solid currentColor;
  }
  .monty-bubble::before { display: none; }
  .difficulty-content,
  .settings-content,
  .victory-panel,
  .game-header-bar,
  .game-footer {
    background: Canvas;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .btn-primary,
  .btn-secondary,
  .btn-difficulty,
  .btn-back,
  .btn-support {
    border: 2px solid currentColor;
  }
  .install-steps {
    border: 1px solid currentColor;
  }
  /* The sticky settings Back lip paints a translucent gradient; in forced
     colors it becomes an opaque system-surface bar. This override lives in
     the LATER forced-colors block on purpose: the base
     `.settings-content .modal-actions` gradient rule comes after the first
     forced block in the file, and at equal specificity order decides — an
     override placed there loses to the gradient (caught live by
     responsive-check.mjs's forced pass: `actionsOpaque: false`). */
  .settings-content .modal-actions {
    background: Canvas;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

/* A child asking for less transparency — or for more contrast — keeps the
   warm palette; the glass just stops being see-through: solid panels, no
   blur. The cave art and Monty stay; these settings are about the glass,
   not the illustration. */
@media (prefers-reduced-transparency: reduce), (prefers-contrast: more) {
  .difficulty-content,
  .game-header-bar,
  .victory-panel,
  .settings-content,
  .settings-content .modal-actions {
    background: var(--surface-container-lowest);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .game-footer {
    background: var(--surface-container-low);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
}

/* Visually hidden but accessible to screen readers */
.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;
}

/* ─── Short viewports ────────────────────────────────────────── */
@media (max-height: 700px) {
  .welcome-content { padding: 0.5rem 1rem; }
  .hero-logo { width: 1.75rem; height: 1.75rem; }
  .hero-brand { margin-bottom: 0.35rem; gap: 0.35rem; }
  .hero-title { font-size: clamp(1rem, 4vh, 1.4rem); }
  .mascot-image-wrapper {
    width: clamp(72px, 18vh, 140px);
    height: clamp(72px, 18vh, 140px);
    max-width: 20vh;
    max-height: 20vh;
    margin-bottom: 0.3rem;
  }
  .mascot-area { margin-bottom: 0.25rem; }
  .hero-tagline { display: none; }
  .btn-large { padding: 0.75rem 2rem; }
  .btn-settings { margin-top: 0.4rem; }
  .game-footer { padding: 0.35rem 1rem; }
  .footer-content { gap: 0.35rem; }
  .difficulty-content { padding: 0.75rem 1rem; }
  .difficulty-buttons { gap: 0.5rem; margin-bottom: 0.5rem; }
  .btn-difficulty { padding: 0.55rem 0.9rem; }
  .settings-content { padding: 1rem; }
  .setting-item { padding: 0.5rem 0; }
  .modal-actions { margin-top: 0.75rem; }
  .legal-links { margin-top: 0.75rem; font-size: 0.8rem; }
  .victory-content { padding: 0.5rem; gap: 0.5rem; }
}

/* Mid-height desktops: the full 35vh mascot plus footer is a few pixels
   taller than the container, which leaves a stray scrollbar on the home
   screen. Trim the mascot instead of letting the page scroll. */
@media (min-height: 701px) and (max-height: 900px) {
  .mascot-image-wrapper {
    width: clamp(130px, 28vh, 240px);
    height: clamp(130px, 28vh, 240px);
    max-width: 28vh;
    max-height: 28vh;
  }
  .hero-brand { margin-bottom: 0.5rem; }
  .hero-tagline { margin-bottom: 0.5rem; }
}

/* ─── Landscape welcome layout ───────────────────────────────── */
/* Short landscape phones only. A desktop window is also landscape, but it has
   the height for the centred stack and reads better that way. */
@media (orientation: landscape) and (max-height: 600px) {
  .welcome-content {
    display: grid;
    grid-template-columns: minmax(110px, 220px) 1fr;
    grid-template-rows: auto 1fr;
    gap: 0.4rem 1rem;
    align-items: center;
    justify-items: center;
    padding-top: 0.25rem;
    padding-bottom: 0.25rem;
  }
  .hero-brand { grid-column: 1 / -1; margin-bottom: 0.25rem; }
  .mascot-area { grid-column: 1; grid-row: 2; margin-bottom: 0; }
  .hero-actions { grid-column: 2; grid-row: 2; }
  .mascot-image-wrapper {
    width: clamp(70px, 16vh, 150px);
    height: clamp(70px, 16vh, 150px);
    max-width: 18vh;
    max-height: 18vh;
  }
  .hero-tagline { margin-bottom: 0.35rem; display: block; }
  .btn-settings { margin-top: 0.4rem; }
}

@media (max-height: 480px) {
  body[data-screen="home"] #game-container,
  body[data-screen="difficulty"] #game-container {
    overflow-y: auto;
  }
  /* Keep the legal links reachable without letting the footer crowd the
     buttons out of a 375px-tall landscape viewport. */
  .footer-copyright { display: none; }
  .game-footer { padding: 0.3rem 1rem; }
  .footer-content { gap: 0.2rem; }
  .btn-support { min-height: 44px; padding: 0.4rem 1rem; }
}

/* Sideways on a short phone: the celebration must show BOTH choices
   without the child hunting for scroll mid-cheer. Width is plentiful
   here and height is scarce, so the panel wraps its parts into wide
   rows — Monty and the headline share one, the status line shares the
   next, and the badge shelf keeps a full row for itself. */
@media (orientation: landscape) and (max-height: 500px) {
  /* px clamps and px type: at scaled system type (≥150%) rem minima
     like 1.4rem become the binding size and the shelf walks itself off
     a 375 px-tall screen — at 200% the buttons landed at y 527–741.
     Pixels do not scale with the OS font, so the shelf cannot creep;
     the buttons keep their 44 px touch floor by construction. */
  .victory-panel {
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 2px 8px;
    padding: 3px 10px;
  }
  .victory-monty { width: clamp(40px, 10vh, 56px); }
  .victory-title { font-size: clamp(17px, 4.5vh, 20px); margin: 0; }
  .victory-difficulty { font-size: 14px; }
  .victory-stars { margin: 0; }
  .victory-star {
    width: clamp(18px, 5vh, 20px);
    height: clamp(18px, 5vh, 20px);
  }
  .victory-accuracy,
  .victory-time,
  .victory-next-star,
  .victory-best { font-size: 12px; }
  .victory-best { padding: 2px 8px; }
  .victory-badges { width: 100%; }
  .victory-badges-list .badge-medallion { width: clamp(28px, 7vh, 36px); }
  .victory-buttons { margin-top: 0; }
  .victory-buttons .btn-primary,
  .victory-buttons .btn-secondary {
    min-height: 44px;
    padding: 4px 12px;
    gap: 4px;
    font-size: 13px;
    line-height: 1.2;
  }
  .victory-buttons .btn-primary svg,
  .victory-buttons .btn-secondary svg { width: 18px; height: 18px; }

  /* Monty's glass must never cover the treasure he is talking about.
     At 200% the bubble grows into a two-line slab that lands right on
     the slot (bubble bottom 243 vs treasure top 204). The type cap is
     px, not rem — short words, glass clear of the board; the audio
     speaks the same line whatever the size. 15 px ≈ the base 0.95rem,
     so unscaled users see no change. */
  body[data-orientation="landscape"] .monty-bubble-text { font-size: 15px; }

  /* Same squeeze for the grown-up panel: every switch AND the Reset
     button AND the sticky Back must be on screen the moment settings
     opens — a parent in landscape must not scroll to mute the game.
     Controls keep their size; only air is cut. */
  /* px, not rem: at scaled system type rem air scales WITH the text and
     the column spills again at 200%. Fixed pixels stay fixed, and the
     rows bottom out at their 56 px touch floor whatever the OS font. */
  .settings-content { padding: 4px 16px; }
  .settings-content .screen-title { font-size: 18px; margin-bottom: 3px; }
  .setting-item { padding: 2px 0; }
  /* Two columns of switches: width is plentiful sideways, height is not.
     One column of seven rows (Narration joined the set) needed 420+ px of
     56 px rows at 200% type and the Reset button walked to y 405 on a
     375 px-tall screen. Half as many rows fits every control above the
     fold at every text tier. */
  .settings-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    column-gap: 1.25rem;
    align-content: start;
  }
  .settings-content .screen-title,
  .settings-content .settings-status,
  .settings-content .settings-badges,
  .settings-content .legal-links,
  .settings-content .modal-actions { grid-column: 1 / -1; }
  .setting-text small {
    /* At scaled system type the explainers are what spill the controls
       off a 375 px-tall screen; the switches stay labelled, and the
       scroll reaches the words when a grown-up wants them. */
    display: none;
  }
  .settings-badges { margin-top: 0.5rem; }
  .settings-badges { padding-top: 0.4rem; }
}


