body[data-page="map"] {
  --map-control-right: max(.7rem, env(safe-area-inset-right));
  --map-settings-size: 44px;
  --map-control-gap: .45rem;
}

.map-page {
  position: relative;
  isolation: isolate;
  min-height: 100dvh;
  overflow: hidden;
  display: grid;
  align-items: center;
  justify-items: center;
  padding: clamp(0px, 1.5vw, 20px);
  background: radial-gradient(ellipse at 50% 32%, #ffebbb, #e5b87c);
}

.map {
  --map-effect-cycle: 5.2s;
  position: relative;
  width: min(100%, 1440px, calc((100dvh - 2.5rem) * 1.5));
  aspect-ratio: 3 / 2;
  isolation: isolate;
  background: #ffe9bc;
  box-shadow: 0 9px 32px #5e2e1c42;
}

.map-art, .map-sprite, .map-stage-art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.map-layer, .map-fallback {
  pointer-events: none;
  user-select: none;
}

.map-fallback[hidden] { display: none; }
.map:not([data-art="ready"]) .map-layer { visibility: hidden; }

.map-placement {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--w);
  height: var(--h);
}

.map-clouds {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.map-clouds--middle { z-index: 1; }
.map-clouds--front { z-index: 5; }

.map-cloud-track {
  position: absolute;
  left: 0;
  top: var(--y);
  width: 100%;
  height: var(--h);
}

.map-cloud-track .map-sprite {
  left: var(--x);
  right: auto;
  width: var(--w);
}

.map-object-title, .map-object-compass, .map-object-compass-labels { z-index: 3; }

.map-object-title {
  transform-origin: 50% 0;
  transform: scale(.94);
}

.map-title-leaf, .map-title-letter { display: none; }

.map[data-ambient-paused], .is-entering-stage .map { --map-idle-state: paused; }

@media (prefers-reduced-motion: no-preference) {
  .map[data-art="ready"] .map-cloud-track {
    animation: map-cloud-drift var(--cloud-duration) var(--cloud-delay) linear infinite;
  }

  /* The cuts follow the joins between the leaves and the pencils in the cutout. */
  .map .map-title-banner {
    clip-path: polygon(
      12.35% 0, 87.65% 0, 87.65% 38%, 87.15% 46%, 87.35% 49.6%, 88.25% 52.6%, 90.5% 57.4%, 92% 62%,
      100% 62%, 100% 100%, 0 100%, 0 62%, 8% 62%, 9.5% 57.4%, 11.75% 52.6%, 12.65% 49.6%, 12.85% 46%, 12.35% 38%
    );
  }

  .map .map-title-leaf-left {
    --leaf-delay: -1s;
    transform-origin: 11.7% 52%;
    clip-path: polygon(0 0, 12.4% 0, 12.4% 38%, 12.9% 46%, 12.7% 49.6%, 11.8% 52.6%, 9.55% 57.4%, 8.05% 62%, 0 62%);
  }

  .map .map-title-leaf-right {
    --leaf-delay: -2.7s;
    transform-origin: 88.3% 52%;
    clip-path: polygon(100% 0, 87.6% 0, 87.6% 38%, 87.1% 46%, 87.3% 49.6%, 88.2% 52.6%, 90.45% 57.4%, 91.95% 62%, 100% 62%);
  }

  .map[data-art="ready"] .map-title-leaf {
    display: block;
    animation: map-leaf-sway 4.8s var(--leaf-delay) ease-in-out infinite;
  }

  .map[data-art="ready"] .map-title-letter {
    display: block;
    position: absolute;
    background: url("/assets/map/title-letters.png") no-repeat;
    opacity: 0;
    animation: map-title-color-wave 3.7s var(--letter-delay) ease-in-out infinite;
  }

  .map[data-art="ready"] .map-stage-open .map-stage-art {
    animation: map-island-light var(--map-effect-cycle) ease-in-out infinite;
  }

  .map[data-art="ready"] .map-rocket-art {
    transform-origin: 50% 82%;
    animation: map-rocket-hover 3.1s -.8s ease-in-out infinite;
  }

  .map[data-art="ready"] .map-stage:not([data-unlock]) .map-stage-idle {
    animation: var(--stage-idle-animation) 4.2s var(--stage-idle-delay) ease-in-out infinite;
    animation-play-state: var(--map-idle-state, running);
  }

  .map-stage-idle--sway {
    --stage-idle-animation: map-stage-sway;
    transform-origin: 50% 87%;
  }

  .map-stage-idle--float { --stage-idle-animation: map-stage-float; }

  .map[data-art="ready"] :is(.map-cloud-track, .map-title-leaf, .map-title-letter, .map-stage-open .map-stage-art, .map-rocket-art) {
    animation-play-state: var(--map-idle-state, running);
  }
}

@keyframes map-leaf-sway {
  0%, 100% { transform: rotate(-4deg); }
  27% { transform: rotate(6deg) skewY(1deg); }
  55% { transform: rotate(1deg); }
  78% { transform: rotate(-6deg) skewY(-1deg); }
}

@keyframes map-title-color-wave {
  0% { opacity: 0; filter: hue-rotate(0deg) brightness(1); }
  6% { opacity: 1; filter: hue-rotate(70deg) brightness(1.12); }
  15% { opacity: 1; filter: hue-rotate(180deg) brightness(1.15); }
  23% { opacity: .94; filter: hue-rotate(270deg) brightness(1.1); }
  31%, 100% { opacity: 0; filter: hue-rotate(360deg) brightness(1); }
}

@keyframes map-cloud-drift {
  from { transform: translateX(calc(100% - var(--x))); }
  to { transform: translateX(calc(0% - var(--x) - var(--w))); }
}

@keyframes map-stage-sway {
  0%, 60%, 100% { transform: none; }
  19% { transform: rotate(2deg); }
  41% { transform: rotate(-1.5deg); }
}

@keyframes map-stage-float {
  0%, 62%, 100% { transform: none; }
  26% { transform: translateY(-4%) rotate(1.5deg); }
}

.map-stage {
  z-index: 2;
  pointer-events: none;
}

.map-stage:not(.map-stage-open) .map-stage-art { filter: saturate(.15) brightness(.9); }
.map-stage-open .map-stage-art {
  transform: scale(.94);
  filter: drop-shadow(0 2px 4px #f0aa49);
}

.island {
  position: absolute;
  inset: 2.5% 1.4%;
  border: 0;
  border-radius: 42%;
  background: transparent;
  pointer-events: auto;
}

.map[data-art="fallback"] .island-locked {
  background: #342b4d29;
  backdrop-filter: saturate(.12) brightness(.87);
}

.map-lock {
  position: absolute;
  top: 50%;
  left: 50%;
  width: clamp(23px, 20%, 52px);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border: 2px solid #fff1bf;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 25%, #a588cd, #634883 70%);
  box-shadow: 0 0 0 1px #49345b, 0 3px 8px #33223c69, inset 0 0 0 1px #efe0ff50;
}

.map-lock::before, .map-lock::after {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  content: "";
}

.map-lock::before {
  top: 22%;
  width: 32%;
  height: 31%;
  border: clamp(1px, .18vw, 2.5px) solid #ffe3a0;
  border-bottom: 0;
  border-radius: 55% 55% 0 0;
}

.map-lock::after {
  top: 44%;
  width: 49%;
  height: 34%;
  border-radius: 23%;
  background:
    radial-gradient(circle at 50% 42%, #6b4679 0 11%, transparent 14%),
    linear-gradient(#6b4679, #6b4679) center 68% / 8% 23% no-repeat,
    linear-gradient(#fff3b7, #ecc36b);
  box-shadow: 0 1px 2px #33223c70;
}

.map[data-art="loading"] :is(.map-lock, .map-route) { visibility: hidden; }

.map-stage[data-unlock] { z-index: 4; }
.map-stage[data-unlock] .map-stage-art { filter: drop-shadow(0 2px 9px #ffc464); }
.map-stage[data-unlock] .island .map-lock { visibility: hidden; }
.map[data-art="fallback"] [data-unlock] .island-locked { background: transparent; backdrop-filter: none; }

.map-unlock-burst {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.map-unlock-ring {
  position: absolute;
  inset: 10% 6%;
  border: 2px solid #fff6c2;
  border-radius: 50%;
  box-shadow: 0 0 9px #ffbf4d, inset 0 0 12px #ffd06e;
}

.map-unlock-lock, .map-unlock-star { display: none; }

@media (prefers-reduced-motion: no-preference) {
  .map[data-art="ready"] .map-stage[data-unlock] .map-stage-art {
    animation: map-unlock-color var(--unlock-duration) ease-out both;
  }
  .map-unlock-ring { animation: map-unlock-ring var(--unlock-duration) ease-out both; }
  .map-unlock-lock {
    display: block;
    animation: map-unlock-lock var(--unlock-duration) ease-out both;
  }
  .map-unlock-lock::before {
    transform-origin: 0 100%;
    animation: map-unlock-shackle var(--unlock-duration) ease-out both;
  }
  .map-unlock-star {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: #fff2a6;
    font-size: clamp(1rem, 2.1vw, 2rem);
    text-shadow: 0 0 6px #ffac39, 0 1px 2px #a05329;
    animation: map-unlock-star var(--unlock-duration) ease-out both;
  }
  .map-unlock-star:nth-child(even) { color: #f2d4ff; }
}

@keyframes map-unlock-color {
  0%, 17% { filter: saturate(.15) brightness(.9); }
  39% { transform: translateY(-4%) scale(1.035); filter: brightness(1.14) drop-shadow(0 0 12px #ffd06c); }
  64%, 92% { filter: drop-shadow(0 2px 6px #ffc464); }
  100% { transform: none; filter: none; }
}

@keyframes map-unlock-shackle {
  0%, 18% { transform: translateX(-50%); }
  30%, 100% { transform: translate(-55%, -15%) rotate(-30deg); }
}

@keyframes map-unlock-lock {
  0%, 17% { opacity: 1; transform: translate(-50%, -50%); }
  30% { opacity: 1; transform: translate(-50%, -63%) scale(1.16); }
  55%, 100% { opacity: 0; transform: translate(-50%, -145%) rotate(-13deg) scale(.5); }
}

@keyframes map-unlock-ring {
  0%, 20% { opacity: 0; transform: scale(.22); }
  32% { opacity: 1; }
  72%, 100% { opacity: 0; transform: scale(1.18); }
}

@keyframes map-unlock-star {
  0%, 24% { opacity: 0; transform: scale(.25); }
  35%, 47% { opacity: 1; }
  76%, 100% { opacity: 0; transform: translate(var(--star-x), var(--star-y)) rotate(30deg) scale(.8); }
}

.island-open { transition: box-shadow .18s, background-color .18s; }
.map[data-art="fallback"] .island-open { box-shadow: 0 0 0 2px #fff4bdbb, 0 0 16px #ffcb64; }

.island-open:hover, .island-open:focus-visible {
  background: #fff9b71c;
  box-shadow: 0 0 0 3px #fff388, 0 0 20px #ffed8b;
}

.map-effects {
  position: absolute;
  z-index: 2;
  top: max(.55rem, env(safe-area-inset-top));
  left: max(.65rem, env(safe-area-inset-left));
}
.map-effects .button { min-height: 42px; }
.effects-picker {
  position: relative;
  display: flex;
  align-items: center;
  gap: .38rem;
  min-height: 42px;
  padding-inline-start: .65rem;
  border: 1px solid #efc97b;
  border-radius: .63rem;
  background: #fffcf1;
  color: #593982;
  box-shadow: 0 2px 8px #6b462635;
  font-size: .87rem;
  font-weight: 750;
}
.effects-picker::after {
  position: absolute;
  right: .65rem;
  width: .43rem;
  height: .43rem;
  border-right: 2px solid;
  border-bottom: 2px solid;
  transform: translateY(-2px) rotate(45deg);
  pointer-events: none;
  content: "";
}
.effects-picker select {
  appearance: none;
  min-height: 40px;
  padding: .3rem 1.6rem .3rem .42rem;
  border: 0;
  border-left: 1px solid #ecdab4;
  border-radius: 0 .6rem .6rem 0;
  color: inherit;
  background: transparent;
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
}

.effects-preview-open {
  gap: .35rem;
  padding: .35rem .7rem;
  border: 1px solid #efc97b;
  background: #fffcf1;
  color: #593982;
  box-shadow: 0 2px 8px #6b462635;
  white-space: nowrap;
}
.effects-preview-open > span:first-child { color: #b37413; }
.preview-action, .preview-action-short { font-size: .82em; font-weight: 650; }
.preview-action-short { display: none; }
.effects-preview { width: min(35rem, calc(100vw - 1.5rem)); max-height: calc(100dvh - 1.5rem); }
.effects-preview h2 { margin: 0 0 .65rem; }
.effects-preview-board { position: relative; overflow: hidden; border-radius: .85rem; background: #fff; }
.effects-preview-board img { display: block; width: 100%; height: auto; }
.effects-preview-actions { display: flex; flex-wrap: wrap; gap: .45rem; margin-top: .8rem; }
.effects-preview-actions form { margin-inline-start: auto; }

.map-route {
  height: clamp(4px, .65vw, 9px);
  background: radial-gradient(ellipse, #fff7c1 6%, #ffcc58 38%, #b8782d 53%, transparent 62%)
    left center / clamp(6px, 1vw, 14px) 100% repeat-x;
  transform: translateY(-50%) rotate(var(--route-angle));
  transform-origin: left center;
  pointer-events: none;
  user-select: none;
}

.map-route-arrow {
  position: absolute;
  right: 0;
  top: 50%;
  width: clamp(12px, 2.2vw, 27px);
  height: auto;
  transform: translate(45%, -50%);
}

.map-route-light {
  position: absolute;
  top: 50%;
  width: clamp(9px, 1.45vw, 22px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle, #fff 0 14%, #fff1af 25%, #ffb546 40%, transparent 70%);
  opacity: 0;
  transform: translate(-50%, -50%);
}

.map[data-ambient] .map-route-light {
  animation: map-trail var(--map-effect-cycle) ease-in-out infinite;
}

.map[data-ambient] .map-object-compass {
  transform-origin: 46% 50%;
  animation: map-compass-turn var(--map-effect-cycle) .25s ease-in-out infinite;
}

.map[data-ambient] .map-object-earth {
  animation: map-earth-light var(--map-effect-cycle) ease-out infinite;
}

.map[data-ambient] .map-sparkle {
  animation: map-twinkle var(--map-effect-cycle) var(--sparkle-delay) ease-out infinite;
}

.map[data-ambient-paused] :is(.map-route-light, .map-object-compass, .map-object-earth, .map-sparkle) {
  animation-play-state: paused;
}

@keyframes map-twinkle {
  0%, 24%, 100% { transform: scale(1); }
  10% { transform: scale(1.27); filter: drop-shadow(0 0 5px #ffd343); }
}

@keyframes map-trail {
  0%, 6% { left: 0; opacity: 0; }
  10% { opacity: 1; }
  24% { opacity: .95; }
  29%, 100% { left: 100%; opacity: 0; }
}

@keyframes map-compass-turn {
  0%, 53%, 100% { transform: rotate(0deg); }
  22% { transform: rotate(20deg); }
}

@keyframes map-earth-light {
  0%, 25%, 100% { filter: none; }
  9% { filter: drop-shadow(0 0 8px #a2edff); }
}

@keyframes map-island-light {
  0%, 100% { transform: rotate(-.9deg) scale(.94); filter: drop-shadow(0 2px 4px #f0aa49); }
  32% { transform: translateY(-5.8%) rotate(1.3deg) scale(.954); filter: drop-shadow(0 4px 11px #ffbe55); }
  68% { transform: translateY(-1.7%) rotate(.35deg) scale(.946); filter: drop-shadow(0 3px 7px #ffd77c); }
}

@keyframes map-rocket-hover {
  0%, 100% { transform: translate(-2%, -1%) rotate(-4deg); }
  42% { transform: translate(4%, -14%) rotate(6deg); }
  73% { transform: translate(1%, -5%) rotate(1deg); }
}



.map-entry {
  display: none;
  position: absolute;
  z-index: 3;
  inset: 0;
  place-items: center;
  background: radial-gradient(ellipse at center, #52558a, #292752);
  user-select: none;
}

.is-entering-stage .map-entry { display: grid; }
body:has(.is-entering-stage) .offline-status { visibility: hidden; }

.map-entry-art {
  position: relative;
  width: min(100%, calc(100dvh * 1.5));
  aspect-ratio: 3 / 2;
}

.map-entry-stars {
  filter: brightness(1.23) saturate(1.12) drop-shadow(0 0 1px #ffb425) drop-shadow(0 0 3px #fff18d);
  animation: map-entry-stars var(--entry-duration) steps(1, end) both;
}

@keyframes map-entry-stars {
  0% { clip-path: inset(0 100% 0 0); }
  25% { clip-path: inset(0 calc(8 * var(--entry-star-width)) 0 0); }
  40% { clip-path: inset(0 calc(7 * var(--entry-star-width)) 0 0); }
  52% { clip-path: inset(0 calc(6 * var(--entry-star-width)) 0 0); }
  62% { clip-path: inset(0 calc(5 * var(--entry-star-width)) 0 0); }
  70% { clip-path: inset(0 calc(4 * var(--entry-star-width)) 0 0); }
  77% { clip-path: inset(0 calc(3 * var(--entry-star-width)) 0 0); }
  83% { clip-path: inset(0 calc(2 * var(--entry-star-width)) 0 0); }
  88% { clip-path: inset(0 var(--entry-star-width) 0 0); }
  92%, 100% { clip-path: inset(0); }
}

.island-return::after {
  position: absolute;
  inset: -3px;
  border: 3px solid #fff4a1;
  border-radius: inherit;
  box-shadow: 0 0 0 3px #f8c86890, 0 0 22px #ffdf72;
  pointer-events: none;
  content: "";
  animation: unlock-glow .8s ease-out both;
}

@media (max-width: 480px) {
  .map-effects { top: calc(max(.55rem, env(safe-area-inset-top)) + var(--map-settings-size) + var(--map-control-gap)); }
}
@media (max-width: 420px) {
  .preview-action { display: none; }
  .preview-action-short { display: inline; }
  .effects-preview-open { font-size: .89rem; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .effects-preview-board img { max-height: 55dvh; object-fit: contain; }
}

@media (max-width: 720px) {
  .map { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .map[data-ambient] :is(.map-route-light, .map-object-compass, .map-object-earth, .map-sparkle) {
    animation: none;
  }
  .island-open { box-shadow: 0 0 0 2px #fff4bb, 0 0 16px #ffc35a; }
  .is-entering-stage .map-entry { display: none; }
}
