:root {
  --ink: #30254d;
  --paper: #fffaf0;
  --plum: #7452b8;
  --star: #ffd84d;
  --shadow: #39295e;
  --grass: #cfe3ad;
  --pop: cubic-bezier(.2, .9, .25, 1.2);
  --control: 56px;
  --gap: 12px;
  --edge-top: max(12px, env(safe-area-inset-top));
  --edge-right: max(12px, env(safe-area-inset-right));
  --edge-bottom: max(12px, env(safe-area-inset-bottom));
  --edge-left: max(12px, env(safe-area-inset-left));
  font-family: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
  font-weight: 900;
  color: var(--ink);
  background: var(--grass);
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible { outline: 4px solid #fff; outline-offset: 2px; box-shadow: 0 0 0 8px var(--plum); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ui-icon { width: 30px; height: 30px; display: block; }

#game-shell { position: fixed; inset: 0; }
#game-container { position: absolute; inset: 0; }
#game-container canvas { display: block; touch-action: none; }

/* Press feedback shared by every game control: 0.94 in 80 ms, release with a 1.04 overshoot. */
.top-controls button, .dpad button, .district-button, .celebration-button {
  transition: transform 220ms var(--pop), background-color 120ms ease;
}
.top-controls button:active, .dpad button:active, .dpad button.pressed, .district-button:active, .celebration-button:active {
  transform: scale(.94);
  transition-duration: 80ms;
}

/* Platform chrome: sound then fullscreen, top right, at every breakpoint. */
.top-controls {
  position: absolute;
  top: var(--edge-top);
  right: var(--edge-right);
  display: flex;
  justify-content: flex-end;
  gap: var(--gap);
  z-index: 30;
}
.top-controls button {
  display: grid;
  place-items: center;
  width: var(--control);
  height: var(--control);
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow), 0 8px 18px rgba(31, 24, 51, .18);
}

/* Task panel, top centre: drawn goal chips, one short line and progress dots. */
.task-stack {
  position: absolute;
  top: var(--edge-top);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: min(580px, calc(100vw - 300px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 25;
  pointer-events: none;
}
.task-panel {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 12px;
  row-gap: 6px;
  padding: 8px 16px 8px 10px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow), 0 10px 22px rgba(31, 24, 51, .18);
}
.task-goal { grid-row: 1 / 3; display: flex; align-items: center; gap: 4px; }
.chip { display: grid; place-items: center; width: 46px; height: 46px; border-radius: 15px; background: #f3ead6; }
.chip.carried { background: var(--star); }
.chip .art-icon, .chip .chip-star { width: 38px; height: 38px; }
.chip-arrow { width: 22px; height: 22px; color: var(--plum); }
#task-text { margin: 0; font-size: clamp(18px, 2vw, 22px); line-height: 1.15; }
.task-dots { display: flex; gap: 6px; margin: 0; padding: 0; list-style: none; }
.task-dots li { width: 20px; height: 20px; border: 2.5px solid var(--ink); border-radius: 50%; background: var(--paper); }
.task-dots li.done { background: var(--star); }
.task-dots svg { display: block; width: 100%; height: 100%; }
.cargo-slots { display: flex; gap: var(--gap); }
.cargo-slots[hidden] { display: none; }
.slot { display: grid; place-items: center; width: 52px; height: 52px; border: 3px dashed #8a7fa6; border-radius: 16px; background: rgba(255, 250, 240, .9); }
.slot.full { border-style: solid; border-color: var(--ink); background: var(--paper); box-shadow: 0 4px 0 var(--shadow); }
.slot .art-icon { width: 38px; height: 38px; }
.slot-empty { width: 26px; height: 26px; color: #8a7fa6; }

/* District title card shown for a moment when a district opens. */
.district-banner {
  position: absolute;
  left: 50%;
  top: 38%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px 14px 14px;
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 6px 0 var(--shadow);
  transform: translate(-50%, -50%) scale(.9);
  opacity: 0;
  pointer-events: none;
  transition: opacity 260ms ease, transform 320ms var(--pop);
  z-index: 26;
}
.district-banner.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.banner-icon { display: grid; place-items: center; width: 56px; height: 56px; border-radius: 18px; background: var(--star); }
.district-banner strong { display: block; font-size: 26px; }
.district-banner small { display: block; margin-top: 4px; font-size: 18px; font-weight: 800; color: #4b3f6b; }
.lang-badge {
  display: inline-block; margin-left: 8px; padding: 1px 6px; vertical-align: middle;
  border: 2px solid #30254d; border-radius: 8px; background: #fffaf0; color: #30254d;
  font-size: 13px; font-weight: 900; line-height: 1.2; letter-spacing: .04em;
}

/* Bottom-left: minimap card with the three-dot district strip; it opens the district chooser. */
.map-stack {
  position: absolute;
  left: var(--edge-left);
  bottom: var(--edge-bottom);
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 20;
}
.map-card {
  display: grid;
  gap: 6px;
  width: 186px;
  padding: 6px 6px 8px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow);
  transition: transform 220ms var(--pop);
}
.map-card:active { transform: scale(.96); transition-duration: 80ms; }
.map-card canvas { display: block; width: 100%; height: auto; border-radius: 11px; }
.district-dots { display: flex; justify-content: center; gap: 10px; }
.district-dots span { width: 16px; height: 16px; border: 2.5px solid var(--ink); border-radius: 50%; background: #e6dcc6; }
.district-dots span.done { background: var(--star); }
.district-dots span.current { box-shadow: 0 0 0 3px var(--paper), 0 0 0 5px var(--plum); }

.district-chooser {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(48, 37, 77, .38);
  z-index: 45;
}
.district-chooser[hidden] { display: none; }
.chooser-card {
  position: relative;
  padding: 26px 22px 22px;
  border: 3px solid var(--ink);
  border-radius: 28px;
  background: var(--paper);
  box-shadow: 0 7px 0 var(--shadow);
}
.district-strip { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--gap); }
.district-button {
  position: relative;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 8px;
  width: 150px;
  min-height: 132px;
  padding: 12px 8px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow);
}
.district-art { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 20px; background: #f3ead6; }
.district-art .ui-icon { width: 40px; height: 40px; }
.district-name { font-size: 18px; line-height: 1.1; text-align: center; }
.district-button.current .district-art { background: var(--star); }
.district-button:disabled { opacity: .45; box-shadow: none; cursor: default; }
.district-star { position: absolute; right: -10px; top: -10px; width: 34px; height: 34px; }
.chooser-close {
  position: absolute;
  top: -20px;
  right: -20px;
  display: grid;
  place-items: center;
  width: var(--control);
  height: var(--control);
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 4px 0 var(--shadow);
}

/* Arrival bubble above the reached building; never over the robot or the toast zone. */
.arrival-bubble {
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(380px, 88vw);
  padding: 8px 16px 8px 8px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow);
  font-size: 19px;
  line-height: 1.15;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
  z-index: 24;
}
.arrival-bubble.show { opacity: 1; }
.bubble-icons { display: flex; gap: 4px; }
.bubble-icons .art-icon, .bubble-icons .ui-icon { width: 40px; height: 40px; }

/* Edge pointer toward an off-screen goal. */
.goal-pointer {
  position: fixed;
  left: 0;
  top: 0;
  width: 64px;
  height: 64px;
  display: none;
  pointer-events: none;
  z-index: 22;
}
.goal-pointer.show { display: block; }
.goal-pointer-icon {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 4px 0 var(--shadow);
}
.goal-pointer-icon .art-icon { width: 42px; height: 42px; }
.goal-pointer-arrow { position: absolute; inset: -16px; transform: rotate(var(--angle, 0rad)); }
.goal-pointer-arrow::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  margin-top: -11px;
  border-left: 16px solid var(--ink);
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
}

/* D-pad: bottom right, 56 px buttons with 12 px gaps, drawn chevrons around the robot face. */
.dpad {
  position: absolute;
  right: var(--edge-right);
  bottom: var(--edge-bottom);
  display: grid;
  grid-template-columns: repeat(3, var(--control));
  grid-template-rows: repeat(3, var(--control));
  gap: var(--gap);
  padding: 8px;
  border-radius: 30px;
  background: rgba(255, 250, 240, .55);
  touch-action: none;
  z-index: 20;
}
.dpad button {
  display: grid;
  place-items: center;
  width: var(--control);
  height: var(--control);
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  box-shadow: 0 4px 0 var(--shadow);
  touch-action: none;
}
.dpad button.pressed { background: var(--star); }
.dpad [data-direction=up] { grid-column: 2; grid-row: 1; }
.dpad [data-direction=left] { grid-column: 1; grid-row: 2; }
.dpad [data-direction=right] { grid-column: 3; grid-row: 2; }
.dpad [data-direction=down] { grid-column: 2; grid-row: 3; }
.dpad-face { grid-column: 2; grid-row: 2; display: grid; place-items: center; }
.dpad-face .art-icon { width: 46px; height: 46px; }

/* Platform celebration: drawn star, one phrase, Ещё раз / Дальше / Домой. Bottom stays free for the toast. */
.celebration {
  position: fixed;
  inset: 0;
  display: grid;
  justify-items: center;
  align-content: start;
  padding: max(96px, calc(env(safe-area-inset-top) + 84px)) 16px 140px;
  background: linear-gradient(to bottom, rgba(48, 37, 77, .34) calc(100% - 190px), rgba(48, 37, 77, 0) calc(100% - 130px));
  z-index: 40;
}
.celebration[hidden] { display: none; }
.celebration-card {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 18px 22px 22px;
  border: 3px solid var(--ink);
  border-radius: 28px;
  background: var(--paper);
  box-shadow: 0 7px 0 var(--shadow), 0 20px 40px rgba(31, 24, 51, .3);
  animation: card-in 360ms var(--pop) both;
}
.celebration-star .big-star { width: 132px; height: 132px; display: block; }
.celebration h2 { margin: 0; font-size: 30px; text-align: center; }
.celebration-actions { display: flex; gap: var(--gap); flex-wrap: wrap; justify-content: center; }
.celebration-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 64px;
  min-height: 64px;
  padding: 0 18px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  color: var(--ink);
  font-size: 20px;
  text-decoration: none;
  box-shadow: 0 5px 0 var(--shadow);
}
.celebration-button[hidden] { display: none; }
.celebration-button.primary { background: var(--star); }
.celebration-button .ui-icon { width: 28px; height: 28px; }
.is-celebrating .dpad, .is-celebrating .goal-pointer { visibility: hidden; }
@keyframes card-in { from { opacity: 0; transform: scale(.85); } to { opacity: 1; transform: scale(1); } }

/* Loading card: drawn robot, a loader-driven bar, one line. */
#loading { position: absolute; inset: 0; display: grid; place-items: center; background: var(--grass); z-index: 50; }
#loading[hidden] { display: none; }
#loading.failed { cursor: pointer; }
#loading.failed .loading-bar { display: none; }
.loading-card {
  display: grid;
  justify-items: center;
  gap: 12px;
  padding: 22px 28px;
  border: 3px solid var(--ink);
  border-radius: 26px;
  background: var(--paper);
  box-shadow: 0 6px 0 var(--shadow);
}
.loading-robot .art-icon { width: 96px; height: 96px; }
#loading-text { margin: 0; font-size: 22px; }
.loading-bar { width: 200px; height: 16px; border: 3px solid var(--ink); border-radius: 10px; overflow: hidden; background: #f3ead6; }
.loading-bar span { display: block; width: 4%; height: 100%; background: var(--star); transition: width 120ms linear; }

/* Narrow screens: the task panel moves under the top row, clear of home and sound. */
@media (max-width: 600px) {
  .task-stack {
    top: calc(var(--edge-top) + 60px);
    left: calc(var(--tokenville-corner-right, 86px) + 6px);
    right: var(--edge-right);
    transform: none;
    width: auto;
    max-width: none;
    align-items: stretch;
  }
  .task-panel { grid-template-columns: auto minmax(0, 1fr); padding: 6px 10px 8px 6px; column-gap: 8px; }
  .task-goal { grid-row: 1; }
  .task-dots { grid-column: 2; grid-row: 1; justify-self: end; }
  #task-text { grid-column: 1 / 3; grid-row: 2; font-size: 18px; padding-left: 4px; }
  .chip { width: 40px; height: 40px; }
  .chip .art-icon { width: 32px; height: 32px; }
  .chip-arrow { width: 16px; height: 16px; }
  .cargo-slots { justify-content: center; }
  .map-card { width: 140px; padding: 5px 5px 7px; }
  .district-strip { flex-direction: column; }
  .district-button { width: 230px; min-height: 76px; grid-template-columns: 64px 1fr; justify-items: start; text-align: left; }
  .district-name { text-align: left; }
  .district-banner { top: 42%; width: max-content; max-width: 90vw; }
  .district-banner strong { font-size: 22px; }
  .celebration h2 { font-size: 26px; }
  .celebration-button { padding: 0 12px; font-size: 18px; }
  .celebration-button .ui-icon { width: 24px; height: 24px; }
}
@media (max-width: 480px) {
  :root { --control: 48px; }
  .top-controls button { border-radius: 16px; }
  .top-controls .ui-icon { width: 26px; height: 26px; }
  .district-button { border-radius: 15px; }
  .dpad { padding: 6px; border-radius: 26px; }
  .dpad button { border-radius: 15px; }
  .dpad-face .art-icon { width: 40px; height: 40px; }
}
/* Phone landscape: short screens keep the panel top centre and shrink the corners. */
@media (max-height: 500px) {
  :root { --control: 48px; }
  .task-stack { flex-direction: row; align-items: flex-start; max-width: calc(100vw - 300px); }
  .task-panel { padding: 4px 12px 4px 6px; }
  .chip { width: 38px; height: 38px; }
  .chip .art-icon { width: 30px; height: 30px; }
  #task-text { font-size: 18px; }
  .slot { width: 46px; height: 46px; }
  .map-card { width: 140px; padding: 4px 4px 6px; gap: 4px; }
  .district-dots span { width: 14px; height: 14px; }
  .district-button { width: 132px; min-height: 112px; }
  .dpad { padding: 6px; }
  .celebration { padding-top: 12px; padding-bottom: 90px; }
  .celebration-card { grid-template-columns: auto auto; column-gap: 18px; padding: 12px 18px; }
  .celebration-star { grid-row: 1 / 3; }
  .celebration-star .big-star { width: 96px; height: 96px; }
  .celebration h2 { font-size: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
