/* Princess Mathematica — responsive layout
   Phone portrait/landscape + iPad sizes + safe areas
   Loaded after inline base styles in index.php */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

/* ===== Safe areas for fixed chrome ===== */
.spot-nav {
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
}
.spot-nav-right {
  left: auto;
  right: calc(12px + var(--safe-right));
}
.settings-gear {
  bottom: calc(16px + var(--safe-bottom));
  left: calc(16px + var(--safe-left));
}
.home-back-btn {
  bottom: calc(16px + var(--safe-bottom));
  right: calc(16px + var(--safe-right));
}
.story-skip {
  bottom: calc(20px + var(--safe-bottom));
  right: calc(20px + var(--safe-right));
}

/* Keep celebratory rain below interactive chrome on play screens */
body.on-playroom #gem-rain,
body.on-spot #gem-rain {
  z-index: 5;
}
.play-particle-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 5;
}
.play-particle {
  position: absolute !important;
  z-index: 1 !important;
  pointer-events: none;
}
.sand-build-toolbar {
  position: relative;
  z-index: 40 !important;
  isolation: isolate;
}
.sand-build-buckets {
  position: relative;
  z-index: 41;
}

/* ===== Playroom treasure shelf: never clip the 5th column ===== */
.playroom-wall-section {
  overflow: hidden; /* scrolling lives on .playroom-wall-scroll */
  padding-left: clamp(6px, 2vw, 12px);
  padding-right: clamp(6px, 2vw, 12px);
  box-sizing: border-box;
  max-height: min(42vh, 320px);
}
.playroom-wall-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.playroom-wall {
  width: 100%;
  box-sizing: border-box;
  gap: clamp(4px, 1.2vw, 8px);
  grid-template-columns: repeat(5, minmax(0, 1fr)); /* minmax(0) allows shrink */
}
.playroom-wall-slot {
  min-width: 0; /* critical: default min-width:auto refuses to shrink below 64px icon */
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  min-height: 0;
  padding: 5px; /* keep rings inside the cell */
  box-sizing: border-box;
  overflow: hidden;
  border-radius: 10px;
}
.playroom-wall-slot .mo-item {
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
  max-height: 100%;
  box-sizing: border-box;
}
.playroom-wall-slot .mo-item-ring {
  inset: 0 !important; /* was -5px — that alone overflowed phones */
  border-width: 2px;
  box-shadow: none !important;
}
.playroom-wall-slot .mo-item svg,
.playroom-wall-slot .mo-item .scaled-object,
.playroom-wall-slot .mo-item .scaled-object svg {
  max-width: 100% !important;
  max-height: 100% !important;
  width: 100% !important;
  height: auto !important;
}
#screen-playroom {
  padding:
    calc(8px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(12px + var(--safe-left));
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  box-sizing: border-box;
  min-height: 100%;
  min-height: 100dvh;
}
.playroom-topbar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 100%;
  max-width: 720px;
  margin: 0 auto 8px;
  text-align: center;
  position: relative;
  z-index: 5;
}
.playroom-back-btn {
  align-self: flex-start;
  font-family: 'Quicksand', sans-serif;
  font-size: .85rem;
  font-weight: 700;
  padding: 10px 16px;
  min-height: 44px;
  border: 2px solid rgba(123, 45, 142, .35);
  background: rgba(255, 255, 255, .92);
  color: #5a1a6b;
  border-radius: 20px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 2px 8px rgba(123, 45, 142, .15);
}
.playroom-back-btn:active {
  transform: scale(.97);
}
.playroom-topbar .playroom-title {
  margin-bottom: 0;
  font-size: clamp(1.05rem, 4.2vw, 1.4rem);
}
.playroom-topbar .playroom-hint {
  margin-bottom: 0;
  font-size: clamp(.72rem, 3.2vw, .85rem);
  padding: 0 8px;
  line-height: 1.35;
}
#screen-playroom .playroom-room {
  flex: 1 1 auto;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  min-height: 0; /* override base 520px so short phones can flex */
  display: flex;
  flex-direction: column;
}
#screen-playroom .playroom-floor {
  min-height: 0;
  flex: 1 1 auto;
}
#screen-playroom .playroom-nav {
  display: none; /* replaced by topbar back button */
}

/* ===== Phone portrait (narrow) ===== */
@media (max-width: 480px) and (orientation: portrait) {
  #splash {
    padding:
      calc(12px + var(--safe-top))
      calc(12px + var(--safe-right))
      calc(16px + var(--safe-bottom))
      calc(12px + var(--safe-left));
  }
  .splash-scene {
    width: min(92vw, 340px);
    height: min(42vh, 280px);
  }
  .splash-title {
    font-size: clamp(1.45rem, 7vw, 1.9rem);
  }
  .splash-sub {
    font-size: .95rem;
  }

  #screen-design {
    padding:
      calc(8px + var(--safe-top))
      calc(10px + var(--safe-right))
      0
      calc(10px + var(--safe-left));
    overflow: hidden;
  }
  .design-footer {
    padding-left: calc(10px + env(safe-area-inset-left, 0px));
    padding-right: calc(10px + env(safe-area-inset-right, 0px));
  }
  .design-preview {
    width: min(92vw, 320px);
    height: auto;
    aspect-ratio: 340 / 210;
  }
  .name-input {
    width: min(88vw, 260px);
    font-size: 1.05rem;
  }
  .dtab {
    width: 44px;
    height: 44px;
    font-size: 1.15rem;
  }

  #screen-map {
    padding:
      calc(6px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(10px + var(--safe-bottom))
      calc(8px + var(--safe-left));
  }
  .map-hotspot {
    width: clamp(44px, 9%, 52px);
    min-width: 44px;
    min-height: 44px;
    font-size: clamp(.55rem, 2.2vw, .85rem);
  }
  .map-hotspot-num {
    width: 14px;
    height: 14px;
    font-size: .4rem;
    top: -4px;
    right: -4px;
    border-width: 1px;
  }
  .map-hotspot::before {
    inset: -8px;
  }
  .map-scene-wrap.map-compact .map-hotspot {
    width: var(--map-pin, 48px) !important;
    height: var(--map-pin, 48px) !important;
    min-width: 44px;
    min-height: 44px;
  }
  .playroom-door {
    height: min(24vh, 180px);
    min-height: 140px;
    max-height: 200px;
  }

  #screen-playroom .playroom-room {
    min-height: 0;
    max-height: none;
    flex: 1 1 auto;
  }
  .playroom-wall {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    min-height: 0;
  }
  .playroom-wall-slot {
    min-height: 0;
    padding: 3px;
    border-radius: 8px;
  }
  .playroom-floor {
    min-height: 240px;
    flex: 1 1 auto;
  }
  .playroom-princess {
    width: min(52vw, 200px);
    height: min(48vw, 220px);
    bottom: 12px;
  }
  .sand-build-bucket {
    width: 52px;
    height: 58px;
  }
  .sand-build-bucket svg {
    width: 34px;
    height: 34px;
  }
  .sand-build-bucket span {
    font-size: .52rem;
  }

  #screen-game {
    padding:
      calc(6px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(56px + var(--safe-bottom)) /* leave room for gear + home */
      calc(8px + var(--safe-left));
    height: 100%;
    min-height: 100dvh;
    box-sizing: border-box;
    justify-content: stretch;
    align-items: stretch;
  }
  .game-wrapper {
    max-width: 100%;
    width: 100%;
    height: 100%;
    min-height: 0;
    flex: 1 1 auto;
    gap: clamp(4px, 1vh, 10px);
    padding-top: 2px;
    padding-bottom: 4px;
    justify-content: flex-start;
    box-sizing: border-box;
  }
  .game-header-bar {
    gap: 8px;
    padding: 5px 8px;
  }
  .game-header-title {
    font-size: clamp(.4rem, 2.8vw, .5rem);
    letter-spacing: .2px;
    line-height: 1.18;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  .game-princess-sm {
    width: 44px;
    height: 44px;
    flex: none;
  }
  .problem-section,
  .answer-section {
    width: 100%;
    max-width: 420px;
  }
  .problem-display {
    font-size: clamp(1.8rem, 9vw, 2.4rem);
    min-height: clamp(52px, 8vh, 72px);
    padding: 8px 12px;
  }
  .answer-box {
    font-size: clamp(1.6rem, 6vw, 2rem);
    min-height: clamp(44px, 6.5vh, 56px);
  }
  .keypad-section {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: min(360px, 92vw);
    min-height: 0;
    margin-top: auto; /* pin keypad toward bottom of free space */
  }
  .keypad {
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
    min-height: 0;
    gap: clamp(6px, 1.1vh, 10px);
    grid-auto-rows: 1fr;
  }
  .key-gem {
    min-height: clamp(48px, 9.5vh, 76px);
    height: 100%;
    font-size: clamp(1.15rem, 4.5vw, 1.55rem);
    border-radius: 16px;
  }
  .feedback-section {
    min-height: 20px;
    flex: 0 0 auto;
  }
  .hint-btn {
    top: calc(8px + var(--safe-top));
    right: calc(8px + var(--safe-right));
  }

  .spot-content {
    padding: calc(52px + var(--safe-top)) 0 calc(12px + var(--safe-bottom));
  }
  .spot-scene-stage {
    min-height: 280px;
  }
  .spot-princess {
    max-width: min(46vw, 220px);
    height: min(48vh, 300px);
  }
}

/* Extra-short phones (iPhone SE-ish height) */
@media (max-width: 480px) and (max-height: 700px) and (orientation: portrait) {
  .playroom-topbar .playroom-hint {
    display: none;
  }
  .playroom-wall-section {
    padding: 8px 8px 0;
  }
  .playroom-wall-slot {
    min-height: 0;
    padding: 2px;
  }
  .playroom-floor {
    min-height: 200px;
  }
  .splash-scene {
    height: min(36vh, 220px);
  }
}

/* ===== Phone / short landscape — fully playable reflow ===== */
@media (orientation: landscape) and (max-height: 560px) {
  #splash {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px 24px;
    padding:
      calc(8px + var(--safe-top))
      calc(16px + var(--safe-right))
      calc(8px + var(--safe-bottom))
      calc(16px + var(--safe-left));
  }
  .splash-scene {
    width: min(42vw, 360px);
    height: min(78vh, 260px);
    margin-bottom: 0;
  }
  .splash-title {
    font-size: clamp(1.2rem, 4vw, 1.8rem);
    text-align: left;
  }
  .splash-sub {
    margin-top: 6px;
    font-size: .9rem;
  }

  #screen-design {
    padding:
      calc(6px + var(--safe-top))
      calc(12px + var(--safe-right))
      0
      calc(12px + var(--safe-left));
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  .design-scroll {
    flex: 1 1 auto;
    min-height: 0;
    max-width: 920px;
    display: grid;
    grid-template-columns: minmax(200px, 1fr) minmax(240px, 1.1fr);
    grid-template-rows: auto auto auto 1fr;
    column-gap: 16px;
    row-gap: 6px;
    align-items: start;
    overflow-y: auto;
  }
  .design-title {
    grid-column: 1 / -1;
    margin-bottom: 0;
    font-size: 1.1rem;
  }
  .design-names {
    grid-column: 1;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 0;
  }
  .design-preview {
    grid-column: 2;
    grid-row: 2 / 5;
    width: 100%;
    max-width: none;
    height: min(70vh, 280px);
    margin: 0;
  }
  .design-tabs,
  .design-panel {
    grid-column: 1;
  }
  .design-footer {
    max-width: 920px;
    margin: 0 auto;
  }

  #screen-map {
    padding:
      calc(4px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(6px + var(--safe-bottom))
      calc(8px + var(--safe-left));
    overflow: hidden;
  }
  .map-hub {
    display: grid !important;
    grid-template-columns: minmax(0, 1.4fr) minmax(180px, 0.85fr);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
    gap: 10px;
    height: calc(100dvh - var(--safe-top) - var(--safe-bottom) - 12px);
    min-height: 0;
    flex-wrap: nowrap !important;
  }
  .map-hub-map {
    display: flex !important;
    flex-direction: column !important;
    flex: unset !important;
    width: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    min-height: 0 !important;
    overflow: hidden;
  }
  .map-hub-side {
    display: flex !important;
    flex-direction: column !important;
    flex: unset !important;
    width: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    min-height: 0 !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
  }
  .map-hub-map .btn-all-kingdoms {
    flex: 0 0 auto;
    margin-bottom: 4px;
    padding: 6px 10px;
    min-height: 36px;
    font-size: .75rem;
  }
  .map-scene-wrap {
    flex: 0 1 auto !important;
    width: auto;
    max-width: 100%;
    min-height: 0 !important;
    height: min(calc(100dvh - 72px), 320px) !important;
    max-height: min(calc(100dvh - 72px), 320px) !important;
    aspect-ratio: 600 / 500;
    align-self: center;
  }
  .playroom-door {
    height: auto !important;
    flex: 0 0 auto !important;
    min-height: 64px !important;
    max-height: 88px !important;
  }
  .playroom-door-princess,
  .playroom-door-toy {
    display: none;
  }
  .map-hub-controls {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 8px !important;
  }
  .map-hub-controls .btn-magic,
  .map-hub-controls .btn-edit-princess {
    min-height: 40px;
    padding: 8px 12px;
    font-size: .8rem;
  }
  .map-hub-controls .intro-go {
    margin-top: 6px !important;
  }
  .map-hub-controls .intro-reset {
    padding-top: 6px !important;
  }

  /* Playroom: shelf | play area */
  #screen-playroom {
    padding:
      calc(4px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(6px + var(--safe-bottom))
      calc(8px + var(--safe-left));
    overflow: hidden;
  }
  .playroom-topbar {
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 12px;
    margin-bottom: 4px;
    max-width: none;
    text-align: left;
  }
  .playroom-topbar .playroom-hint {
    display: none;
  }
  .playroom-topbar .playroom-title {
    font-size: 1rem;
  }
  .playroom-back-btn {
    padding: 8px 14px;
    min-height: 40px;
    font-size: .8rem;
  }
  #screen-playroom .playroom-room {
    flex-direction: row;
    max-width: none;
    min-height: 0;
    height: calc(100dvh - var(--safe-top) - var(--safe-bottom) - 56px);
    overflow: hidden;
  }
  .playroom-wall-section {
    width: 38%;
    max-width: 320px;
    flex: 0 0 38%;
    max-height: none;
    height: 100%;
    overflow: hidden;
    padding: 8px 8px 0;
  }
  .playroom-wall {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    min-height: 0;
  }
  .playroom-wall-slot {
    min-height: 0;
  }
  .playroom-floor {
    flex: 1 1 auto;
    min-height: 0;
    width: auto;
  }
  .playroom-princess {
    width: min(28vw, 180px);
    height: min(55vh, 200px);
  }
  .sand-build-toolbar {
    padding: 4px 6px 6px;
  }
  .sand-build-buckets {
    flex-wrap: nowrap;
    overflow-x: auto;
    justify-content: flex-start;
    gap: 4px;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .sand-build-bucket {
    width: 48px;
    height: 54px;
  }

  /* Spot stays in-bounds on short landscape */
  #screen-spot {
    overflow: hidden !important;
  }
  #screen-spot .spot-content {
    flex-direction: column !important;
    align-items: stretch;
    padding:
      calc(44px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(8px + var(--safe-bottom))
      calc(8px + var(--safe-left)) !important;
    min-height: 0 !important;
    height: 100% !important;
    max-height: 100dvh;
    box-sizing: border-box;
    overflow: hidden;
  }
  #screen-spot .spot-header {
    position: relative !important;
    top: auto !important;
    padding: 0 8px 4px !important;
    flex: 0 0 auto;
    pointer-events: none;
  }
  #screen-spot .spot-place-name {
    font-size: clamp(.9rem, 4.5vh, 1.2rem) !important;
  }
  #screen-spot .spot-treasure-name,
  #screen-spot .spot-play-hint {
    font-size: .75rem !important;
    margin-top: 2px !important;
  }
  #screen-spot .spot-scene-stage {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: none !important;
    padding: 0 !important;
    overflow: hidden !important;
  }
  #screen-spot .spot-actions {
    position: relative !important;
    left: auto !important;
    bottom: auto !important;
    transform: none !important;
    flex: 0 0 auto;
    padding: 4px 8px !important;
    z-index: 8;
  }
  #screen-spot .btn-spot-play,
  #screen-spot .btn-spot-playroom {
    padding: 6px 12px !important;
    font-size: .78rem !important;
  }
  #screen-spot .scooter-ride-track-wrap,
  #spot-scene-stage .scooter-ride-track-wrap {
    width: min(70vh, 52vw, 420px) !important;
    max-height: min(68%, 220px) !important;
    top: 48% !important;
  }
  #screen-spot .scooter-ride-standings {
    top: 36px !important;
    right: 4px !important;
    max-width: 42% !important;
    font-size: .55rem !important;
  }
  #screen-spot .scooter-ride-place {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #screen-spot .scooter-ride-hint {
    bottom: 4px !important;
    font-size: .68rem !important;
    padding: 4px 10px !important;
  }
  #screen-spot #spot-scene-stage.scooter-ride-active,
  #screen-spot #spot-scene-stage.horse-ride-active,
  #screen-spot #spot-scene-stage.carriage-ride-active,
  #screen-spot #spot-scene-stage.carpet-ride-active,
  #screen-spot #spot-scene-stage.sand-build-active,
  #screen-spot #spot-scene-stage.ball-play-active {
    min-height: 0 !important;
  }

  #screen-game {
    padding:
      calc(4px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(4px + var(--safe-bottom))
      calc(8px + var(--safe-left));
  }
  .game-wrapper {
    max-width: 100%;
    height: calc(100dvh - var(--safe-top) - var(--safe-bottom) - 8px);
    flex-direction: row;
    flex-wrap: wrap;
    align-content: flex-start;
    justify-content: center;
    gap: 6px 16px;
    padding-top: 0;
  }
  .game-header-bar,
  .progress-section {
    width: 100%;
  }
  .problem-section,
  .answer-section {
    flex: 1 1 38%;
    max-width: 42%;
  }
  .keypad-section {
    flex: 1 1 48%;
    max-width: 52%;
  }
  .problem-display {
    font-size: 1.7rem;
    min-height: 48px;
    padding: 6px 10px;
  }
  .answer-box {
    font-size: 1.5rem;
    min-height: 40px;
  }
}

/* Mid-height landscape (phablets / folded) */
@media (orientation: landscape) and (min-height: 501px) and (max-height: 700px) and (max-width: 1024px) {
  #screen-playroom .playroom-room {
    flex-direction: row;
    max-width: none;
    height: auto;
    min-height: calc(100dvh - 100px);
  }
  .playroom-wall-section {
    width: 36%;
    flex: 0 0 36%;
    max-height: none;
    height: auto;
    align-self: stretch;
    overflow: hidden;
  }
  .playroom-wall {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .playroom-floor {
    flex: 1;
    min-height: 280px;
  }
}

/* ===== iPad (tablet) ===== */
@media (min-width: 768px) {
  #screen-playroom {
    padding:
      calc(16px + var(--safe-top))
      calc(24px + var(--safe-right))
      calc(20px + var(--safe-bottom))
      calc(24px + var(--safe-left));
  }
  .playroom-topbar {
    max-width: 820px;
    margin-bottom: 14px;
  }
  .playroom-topbar .playroom-title {
    font-size: 1.85rem;
  }
  .playroom-topbar .playroom-hint {
    font-size: .95rem;
  }
  .playroom-back-btn {
    font-size: .95rem;
    padding: 12px 22px;
  }
  #screen-playroom .playroom-room {
    max-width: 820px;
    min-height: min(72vh, 720px);
  }
  .playroom-wall {
    gap: 12px;
    min-height: 160px;
  }
  .playroom-wall-slot {
    min-height: 0;
  }
  .playroom-floor {
    min-height: 420px;
  }
  .playroom-princess {
    width: 300px;
    height: 320px;
  }
  .sand-build-bucket {
    width: 72px;
    height: 78px;
  }
  .sand-build-bucket svg {
    width: 46px;
    height: 46px;
  }
  .sand-build-bucket span {
    font-size: .68rem;
  }

  .spot-nav {
    top: calc(18px + var(--safe-top));
    left: calc(18px + var(--safe-left));
    font-size: 1rem;
    padding: 12px 22px;
  }
  .spot-nav-right {
    right: calc(18px + var(--safe-right));
  }
  .settings-gear,
  .home-back-btn {
    width: 48px;
    height: 48px;
  }
  .settings-gear svg {
    width: 24px;
    height: 24px;
  }
}

/* iPad landscape: roomy side-by-side playroom */
@media (min-width: 768px) and (orientation: landscape) {
  #screen-playroom .playroom-room {
    flex-direction: row;
    max-width: 1100px;
    min-height: calc(100dvh - 120px);
  }
  .playroom-wall-section {
    width: 34%;
    flex: 0 0 34%;
    max-height: none;
    align-self: stretch;
    overflow: hidden;
  }
  .playroom-wall {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .playroom-floor {
    flex: 1;
    min-height: 0;
  }
  .map-hub {
    gap: 18px;
  }
}

/* ===== Large iPad / desktop-width tablets ===== */
@media (min-width: 1024px) {
  .playroom-topbar,
  #screen-playroom .playroom-room {
    max-width: 960px;
  }
  .playroom-wall {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 14px;
  }
  .playroom-wall-slot {
    min-height: 0;
  }
  #screen-playroom .playroom-room {
    min-height: min(74vh, 780px);
  }
}

@media (min-width: 1024px) and (orientation: landscape) {
  #screen-playroom .playroom-room {
    max-width: 1200px;
  }
  .playroom-wall-section {
    width: 32%;
    flex: 0 0 32%;
  }
  .playroom-wall {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* Touch comfort: larger hit targets on coarse pointers */
@media (pointer: coarse) {
  .playroom-wall-slot {
    /* keep grid shrinkable; don't force a min height that overflows columns */
    min-height: 0;
  }
  .sand-build-bucket {
    min-width: 48px;
  }
  .map-hotspot {
    /* keep relative size; expand hit area via ::before instead of growing the pin */
    width: clamp(44px, 8%, 52px);
    min-width: 44px;
    min-height: 44px;
  }
  .map-hotspot::before {
    inset: -10px;
  }
}

/* ============================================================
   MATH GAME — fill the full viewport on every device
   ============================================================ */
#screen-game {
  padding:
    calc(8px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(56px + var(--safe-bottom))
    calc(12px + var(--safe-left)) !important;
  justify-content: stretch !important;
  align-items: stretch !important;
  box-sizing: border-box;
  height: 100% !important;
  height: 100dvh !important;
  max-height: 100dvh !important;
  min-height: 100dvh !important;
}
#screen-game .game-wrapper {
  width: 100% !important;
  max-width: min(640px, 100%) !important;
  height: 100% !important;
  max-height: 100% !important;
  min-height: 0 !important;
  flex: 1 1 auto !important;
  margin: 0 auto;
  display: grid !important;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  grid-template-areas:
    "hdr"
    "prog"
    "prob"
    "ans"
    "pad"
    "fb";
  justify-items: center;
  align-content: stretch;
  gap: clamp(6px, 1.2vh, 14px);
  padding: 0 !important;
  box-sizing: border-box;
}
#screen-game .game-header-bar { grid-area: hdr; width: 100%; max-width: 100%; }
#screen-game .progress-section { grid-area: prog; width: 100%; max-width: 100%; }
#screen-game .problem-section { grid-area: prob; width: 100%; max-width: 100%; }
#screen-game .answer-section { grid-area: ans; width: 100%; max-width: 100%; }
#screen-game .keypad-section {
  grid-area: pad;
  width: 100% !important;
  max-width: min(520px, 100%) !important;
  height: 100% !important;
  min-height: 0 !important;
  margin-top: 0 !important;
  display: flex !important;
  flex-direction: column;
  align-self: stretch;
  justify-self: center;
}
#screen-game .feedback-section {
  grid-area: fb;
  width: 100%;
  min-height: 18px;
}
#screen-game .keypad {
  flex: 1 1 auto !important;
  width: 100% !important;
  height: 100% !important;
  min-height: 0 !important;
  gap: clamp(8px, 1.4vh, 14px);
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(4, minmax(0, 1fr));
}
#screen-game .key-gem {
  min-height: 0 !important;
  height: 100% !important;
  font-size: clamp(1.15rem, 2.6vh + 0.6vw, 2.2rem);
  border-radius: clamp(12px, 2vh, 20px);
  padding: clamp(4px, 1.2vh, 16px) !important;
}
#screen-game .problem-display {
  font-size: clamp(1.7rem, 3.8vh + 1vw, 3.4rem);
  min-height: clamp(52px, 8vh, 96px);
  padding: clamp(8px, 1.4vh, 18px) clamp(12px, 2vw, 24px);
}
#screen-game .answer-box {
  font-size: clamp(1.45rem, 3vh + 0.6vw, 2.6rem);
  min-height: clamp(44px, 6.5vh, 72px);
}
#screen-game .hint-btn {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 12;
}

/* Short landscape phones: problem left, keypad right */
@media (orientation: landscape) and (max-height: 500px) {
  #screen-game {
    padding:
      calc(4px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(8px + var(--safe-bottom))
      calc(8px + var(--safe-left)) !important;
  }
  #screen-game .game-wrapper {
    max-width: 100% !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "hdr hdr"
      "prog prog"
      "prob pad"
      "ans  pad";
    column-gap: 14px;
    row-gap: 4px;
  }
  #screen-game .feedback-section {
    position: absolute;
    left: 50%;
    bottom: 2px;
    transform: translateX(-50%);
    min-height: 0;
    pointer-events: none;
    grid-area: unset;
  }
  #screen-game .problem-display {
    font-size: clamp(1.3rem, 6vh, 1.8rem);
    min-height: clamp(40px, 14vh, 56px);
  }
  #screen-game .answer-box {
    font-size: clamp(1.2rem, 5vh, 1.6rem);
    min-height: clamp(34px, 12vh, 44px);
  }
  #screen-game .keypad-section {
    max-width: none !important;
  }
}

/* Tablets portrait — bigger canvas, keys still fill leftover height */
@media (min-width: 768px) and (orientation: portrait) {
  #screen-game {
    padding:
      calc(14px + var(--safe-top))
      calc(20px + var(--safe-right))
      calc(28px + var(--safe-bottom))
      calc(20px + var(--safe-left)) !important;
  }
  #screen-game .game-wrapper {
    max-width: min(760px, 92vw) !important;
  }
  #screen-game .keypad-section {
    max-width: min(560px, 100%) !important;
  }
  #screen-game .game-princess-sm {
    width: 80px;
    height: 80px;
  }
  #screen-game .game-header-title {
    font-size: .8rem;
  }
  #screen-game .progress-track {
    height: 30px;
  }
}

@media (min-width: 1024px) and (orientation: portrait) {
  #screen-game .game-wrapper {
    max-width: min(880px, 88vw) !important;
  }
  #screen-game .keypad-section {
    max-width: min(640px, 100%) !important;
  }
}

/* iPad / tablet landscape: wide two-column fill */
@media (min-width: 768px) and (orientation: landscape) {
  #screen-game {
    padding:
      calc(12px + var(--safe-top))
      calc(18px + var(--safe-right))
      calc(18px + var(--safe-bottom))
      calc(18px + var(--safe-left)) !important;
  }
  #screen-game .game-wrapper {
    max-width: min(1180px, 96vw) !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "hdr hdr"
      "prog prog"
      "prob pad"
      "ans  pad";
    column-gap: 28px;
    row-gap: 12px;
  }
  #screen-game .keypad-section {
    max-width: none !important;
  }
  #screen-game .feedback-section {
    grid-column: 1 / -1;
    grid-area: unset;
  }
  #screen-game .game-princess-sm {
    width: 72px;
    height: 72px;
  }
}


/* ============================================================
   SPOT SCENE — never spill outside the viewport
   ============================================================ */
#screen-spot {
  overflow: hidden !important;
  height: 100% !important;
  height: 100dvh !important;
  max-height: 100dvh !important;
  box-sizing: border-box;
}
#screen-spot .spot-content {
  width: 100%;
  max-width: 100%;
  height: 100%;
  min-height: 0 !important;
  max-height: 100%;
  box-sizing: border-box;
  overflow: hidden;
  padding:
    calc(52px + var(--safe-top))
    calc(10px + var(--safe-right))
    calc(10px + var(--safe-bottom))
    calc(10px + var(--safe-left));
  display: flex;
  flex-direction: column;
}
#screen-spot .spot-header {
  flex: 0 0 auto;
  max-width: 100%;
}
#screen-spot .spot-scene-stage {
  flex: 1 1 auto;
  min-height: 0 !important;
  width: 100%;
  max-width: 100%;
  overflow: hidden !important;
  position: relative;
}
#screen-spot .spot-actions {
  flex: 0 0 auto;
  max-width: 100%;
  flex-wrap: wrap;
  justify-content: center;
}
#screen-spot .spot-nav {
  max-width: calc(50% - 16px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Force ride/minigame stages to honor the flex slot (kill fixed 420px mins) */
#screen-spot #spot-scene-stage.scooter-ride-active,
#screen-spot #spot-scene-stage.horse-ride-active,
#screen-spot #spot-scene-stage.carriage-ride-active,
#screen-spot #spot-scene-stage.carpet-ride-active,
#screen-spot #spot-scene-stage.sand-build-active,
#screen-spot #spot-scene-stage.ball-play-active,
#screen-spot #spot-scene-stage.flower-garden-active,
#screen-spot #spot-scene-stage.magic-play-active {
  min-height: 0 !important;
  height: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
}
#screen-spot .play-scene-scooter-ride,
#screen-spot .play-scene-horse-ride,
#screen-spot .play-scene-carriage-ride,
#screen-spot .play-scene-carpet-ride,
#screen-spot .play-scene-sand-build,
#screen-spot .play-scene-flower-garden,
#screen-spot .play-scene-ball-play {
  max-width: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
}

/* Scooter oval: size by available short side so it can’t run off-screen */
#screen-spot .scooter-ride-track-wrap {
  width: min(92%, 560px, calc((100cqh - 24px) * 1.55), calc(100% - 16px)) !important;
  max-width: 100% !important;
  max-height: min(78%, calc(100% - 56px)) !important;
  height: auto !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  aspect-ratio: 1.55;
  container-type: size;
}
#spot-scene-stage {
  container-type: size;
}
#screen-spot .scooter-ride-standings {
  max-width: min(46%, 180px);
  right: max(4px, var(--safe-right));
  box-sizing: border-box;
}
#screen-spot .scooter-ride-place {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}
#screen-spot .scooter-ride-hud,
#screen-spot .scooter-ride-progress {
  left: max(6px, var(--safe-left));
  right: max(6px, var(--safe-right));
}

/* Portrait: fill height without leaving huge empty footer band */
@media (orientation: portrait) {
  #screen-spot .spot-scene-stage {
    flex: 1 1 auto;
  }
  #screen-spot .scooter-ride-track-wrap {
    width: min(94%, 560px) !important;
    max-height: min(70%, 420px) !important;
  }
}

/* Mid landscape tablets/phones taller than 500px still need clip */
@media (orientation: landscape) and (max-height: 820px) {
  #screen-spot .spot-play-hint {
    display: none;
  }
  #screen-spot .scooter-ride-track-wrap {
    width: min(58vw, 70vh, 480px) !important;
    max-height: min(72%, 280px) !important;
  }
  #screen-spot .scooter-ride-standings {
    font-size: .58rem;
    max-width: min(38%, 160px);
  }
}


/* ===== Kingdom picker ===== */
#screen-kingdoms {
  padding-top: calc(12px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
}
.kingdoms-grid {
  gap: clamp(10px, 2.5vw, 16px);
  max-width: min(720px, 100%);
}
.kingdom-card {
  min-height: clamp(140px, 28vh, 200px);
}
.btn-all-kingdoms {
  margin-left: calc(2px + var(--safe-left));
}

@media (max-width: 380px) {
  .kingdoms-grid {
    grid-template-columns: 1fr;
  }
  .kingdom-card {
    min-height: 120px;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .kingdoms-title { font-size: 1.1rem; margin-top: 2px; }
  .kingdoms-sub { margin-bottom: 8px; font-size: .75rem; }
  .kingdoms-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
  }
  .kingdom-card { min-height: 110px; }
  .kingdom-card-name { font-size: .78rem; }
  .kingdom-card-emoji { font-size: 1.4rem; top: 6px; right: 6px; }
}

@media (min-width: 900px) {
  .kingdoms-grid {
    max-width: 880px;
    gap: 18px;
  }
  .kingdom-card { min-height: 210px; }
}

/* ===== Map: hide SVG labels when pins are deconflicted ===== */
.map-scene-wrap.map-compact .map-spot-labels {
  display: none;
}

/* ===== CTA above the fold on short phones / tablets ===== */
@media (max-width: 900px) and (max-height: 720px) {
  .playroom-door {
    height: min(18vh, 120px) !important;
    min-height: 88px !important;
    max-height: 130px !important;
  }
  .map-scene-wrap {
    max-height: none;
    aspect-ratio: 600 / 500;
    width: 100%;
    height: auto;
  }
  #screen-map .map-hub-controls .intro-go {
    position: sticky;
    bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    z-index: 6;
    background: linear-gradient(180deg, transparent, rgba(8, 40, 70, 0.96) 30%);
    padding-top: 8px;
  }
}

@media (max-height: 700px) {
  #screen-design .design-preview {
    height: min(22vh, 150px);
  }
  #screen-design .design-footer {
    padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  }
  #screen-design .design-footer .btn-magic {
    min-height: 48px;
  }
  #screen-victory .v-princess {
    width: 88px;
    height: 88px;
  }
  .kingdom-card {
    min-height: clamp(100px, 20vh, 140px);
  }
}

@media (min-width: 700px) and (max-width: 900px) and (min-height: 900px) {
  /* iPad portrait 768×1024 — keep Start reachable without long scroll */
  .playroom-door {
    height: 150px !important;
    min-height: 120px !important;
    max-height: 170px !important;
  }
  .map-scene-wrap {
    max-height: none;
    aspect-ratio: 600 / 500;
  }
}

/* ===== Playroom shelf shrinks when few treasures ===== */
.playroom-wall-section.shelf-sparse {
  max-height: fit-content;
  max-height: 26vh;
}
.playroom-wall-section.shelf-empty {
  max-height: 56px;
  min-height: 48px;
}
.playroom-wall-section.shelf-empty .playroom-wall {
  display: none;
}

@media (orientation: landscape) {
  .playroom-wall-section.shelf-sparse {
    flex: 0 0 auto !important;
    width: auto !important;
    max-width: min(200px, 24%) !important;
    min-width: 96px;
  }
  .playroom-wall-section.shelf-sparse .playroom-wall {
    grid-template-columns: repeat(2, minmax(56px, 1fr));
  }
  .playroom-wall-section.shelf-mid {
    flex: 0 0 26% !important;
    width: 26% !important;
    max-width: 240px !important;
  }
  .playroom-wall-section.shelf-empty {
    flex: 0 0 64px !important;
    width: 64px !important;
    max-width: 64px !important;
  }
}

@media (min-width: 768px) and (orientation: landscape) {
  .playroom-wall-section.shelf-sparse {
    max-width: min(180px, 20%) !important;
  }
  .playroom-wall-section:not(.shelf-sparse):not(.shelf-empty):not(.shelf-mid) {
    /* keep full shelf for 9+ items — existing % rules apply */
  }
}

@media (min-width: 1024px) and (orientation: landscape) {
  .playroom-wall-section.shelf-sparse {
    max-width: min(170px, 18%) !important;
  }
}
