:root {
  color-scheme: light;
  --paper: #fffaf0;
  --ink: #30254d;
  --plum: #7452b8;
  --shadow: #39295e;
  --star: #ffd84d;
  --beech: #f3d6a8;
  --beech-deep: #d9a46a;
  --plaster: #f3dcc0;
  --font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
  --press: cubic-bezier(.2, .9, .25, 1.2);
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--plaster);
  color: var(--ink);
  font-family: var(--font);
  font-weight: 800;
  -webkit-user-select: none;
  user-select: none;
}

button,
a {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button:focus-visible,
a:focus-visible,
#game-canvas:focus-visible {
  outline: 4px solid var(--plum);
  outline-offset: 3px;
}

#game-canvas:focus-visible { outline-offset: -6px; }

#game-shell {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  isolation: isolate;
  background: var(--plaster);
}

#game-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: default;
}

#game-canvas.can-grab { cursor: grab; }
#game-canvas.dragging { cursor: grabbing; }

/* ---------- HUD: home slot | task panel | repeat, sound, fullscreen ---------- */

.hud {
  position: absolute;
  z-index: 30;
  inset: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(204px, 1fr) minmax(0, 520px) minmax(204px, 1fr);
  grid-template-areas: "home task actions";
  align-items: start;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 0 calc(10px + env(safe-area-inset-left));
  pointer-events: none;
}

.hud > * { pointer-events: auto; }
.hud > .tokenville-home-slot,
.hud > .home-button { grid-area: home; }

.task-panel {
  grid-area: task;
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: min(520px, 100%);
  min-height: 64px;
  padding: 8px 18px 8px 10px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow), 0 10px 22px rgba(57, 41, 94, .18);
  transition: transform 220ms var(--press);
}

.task-panel.is-hint { background: #fff3c4; }
.task-panel { position: relative; }

/* Mixed language mode: a small drawn tag on the panel corner names the level's language. */
.lang-badge {
  position: absolute;
  top: -11px;
  right: -8px;
  min-width: 38px;
  padding: 2px 7px;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  background: var(--star);
  color: var(--ink);
  font-size: 16px;
  font-weight: 950;
  line-height: 1.1;
  letter-spacing: .04em;
  text-align: center;
  box-shadow: 0 2px 0 var(--shadow);
  pointer-events: none;
}

.lang-badge[hidden] { display: none; }
.task-panel.has-badge { padding-right: 30px; }
.task-panel.is-flash { transform: scale(1.04); }

.goal-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--beech);
}

.goal-icon svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.25;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.goal-icon svg .fill { stroke: var(--ink); stroke-width: 1.6; }

.task-body { display: grid; gap: 6px; min-width: 0; }

#task-text {
  font-size: 20px;
  font-weight: 900;
  line-height: 1.1;
  overflow-wrap: break-word;
}

.progress-dots { display: flex; gap: 6px; }

.progress-dots i {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: #f1e6d2;
  transition: background 200ms ease-out, transform 250ms var(--press);
}

.progress-dots i.done { background: var(--star); transform: scale(1.12); }
.progress-dots svg { width: 14px; height: 14px; fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

.hud-actions { grid-area: actions; justify-self: end; display: flex; gap: 12px; }

.chrome-button {
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow), 0 8px 18px rgba(57, 41, 94, .18);
  cursor: pointer;
  transition: transform 80ms ease-out, box-shadow 80ms ease-out, background 160ms ease-out;
}

.chrome-button svg,
.action-button svg,
.play-button svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.25;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chrome-button:hover { background: #fff1cf; }
.chrome-button:active,
.action-button:active,
.play-button:active,
.station:active { transform: translateY(3px) scale(.94); box-shadow: 0 2px 0 var(--shadow); }

.chrome-button[hidden] { display: none; }
#sound-button .icon-sound-off { display: none; }
#sound-button[aria-pressed="false"] .icon-sound-on { display: none; }
#sound-button[aria-pressed="false"] .icon-sound-off { display: block; }
#sound-button[aria-pressed="false"] { background: #efe6d6; }

.map-button {
  position: absolute;
  z-index: 30;
  left: calc(14px + env(safe-area-inset-left));
  bottom: calc(14px + env(safe-area-inset-bottom));
  background: var(--beech);
}

/* ---------- keyboard tray: only after the first key press ---------- */

#keyboard-tray {
  position: absolute;
  z-index: 25;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: none;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow);
  font-size: 16px;
  white-space: nowrap;
}

body.keyboard-mode.playing #keyboard-tray { display: flex; }
.tray-item { display: grid; line-height: 1.1; }
.tray-item small { font-size: 16px; font-weight: 800; color: #6d6080; }
.tray-item strong { font-size: 18px; font-weight: 900; }
.tray-keys { color: var(--plum); font-weight: 900; }

/* ---------- start screen: the workshop map ---------- */

#start-screen {
  position: absolute;
  z-index: 100;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(124px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  background:
    radial-gradient(circle at 50% 30%, rgba(255, 250, 240, .9), rgba(243, 220, 192, .86) 60%),
    rgba(243, 220, 192, .7);
  overflow-y: auto;
  transition: opacity 240ms ease-out, visibility 240ms ease-out;
}

#start-screen.hidden { opacity: 0; visibility: hidden; pointer-events: none; }

.map-card {
  width: min(760px, 100%);
  padding: 22px 22px 26px;
  border: 3px solid var(--ink);
  border-radius: 30px;
  background: var(--paper);
  box-shadow: 0 8px 0 var(--shadow), 0 24px 48px rgba(57, 41, 94, .24);
  text-align: center;
}

.map-card h1 {
  margin: 0 0 14px;
  color: var(--plum);
  font-size: clamp(28px, 6vw, 44px);
  font-weight: 950;
  line-height: 1;
  letter-spacing: -.02em;
}

.map-route {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.route-line {
  position: absolute;
  left: 8%;
  right: 8%;
  top: 34%;
  width: 84%;
  height: 40px;
  pointer-events: none;
}

.route-line path { fill: none; stroke: var(--beech-deep); stroke-width: 4; stroke-dasharray: 2 11; stroke-linecap: round; }

.station {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  min-height: 64px;
  padding: 10px 6px 12px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: #fff4dd;
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow);
  cursor: pointer;
  transition: transform 80ms ease-out, box-shadow 80ms ease-out;
}

.station:hover { background: #ffecc2; }
.station-art { width: min(132px, 100%); height: auto; }
.station strong { font-size: 18px; font-weight: 950; line-height: 1.1; }

.station-dots { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; }
.station-dots i { width: 14px; height: 14px; border: 2.5px solid var(--ink); border-radius: 50%; background: #f1e6d2; }
.station-dots i.done { background: var(--star); }

.play-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-width: 220px;
  min-height: 64px;
  padding: 0 28px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--star);
  color: var(--ink);
  box-shadow: 0 6px 0 var(--shadow);
  font-size: 24px;
  font-weight: 950;
  cursor: pointer;
  transition: transform 80ms ease-out, box-shadow 80ms ease-out;
}

.play-button svg { fill: var(--ink); width: 28px; height: 28px; }

/* ---------- celebration ---------- */

.celebration {
  position: absolute;
  z-index: 200;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(72px + env(safe-area-inset-top)) 16px calc(120px + env(safe-area-inset-bottom));
  background: rgba(57, 41, 94, .22);
}

.celebration[hidden] { display: none; }

.celebration-card {
  display: grid;
  justify-items: center;
  gap: 10px;
  width: min(620px, 100%);
  padding: 18px 18px 22px;
  border: 3px solid var(--ink);
  border-radius: 30px;
  background: var(--paper);
  box-shadow: 0 8px 0 var(--shadow), 0 24px 48px rgba(57, 41, 94, .28);
  animation: card-in 320ms var(--press);
}

.big-star { width: clamp(96px, 22vh, 150px); height: auto; filter: drop-shadow(0 5px 0 #e9a916); animation: star-in 420ms var(--press); }
.celebration h2 { margin: 0; font-size: clamp(26px, 5vw, 32px); font-weight: 950; }

.celebration-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }

.action-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: #fff4dd;
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow);
  font-size: 20px;
  font-weight: 900;
  text-decoration: none;
  cursor: pointer;
  transition: transform 80ms ease-out, box-shadow 80ms ease-out;
}

.action-main { background: var(--star); }
.action-button[hidden] { display: none; }

@keyframes card-in { from { opacity: 0; transform: translateY(18px) scale(.94); } to { opacity: 1; transform: none; } }
@keyframes star-in { from { transform: scale(.4) rotate(-20deg); } 70% { transform: scale(1.08) rotate(4deg); } to { transform: none; } }

.error-card {
  position: absolute;
  z-index: 150;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: min(360px, calc(100% - 32px));
  padding: 24px;
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 6px 0 var(--shadow);
  text-align: center;
  font-size: 18px;
}

.error-card strong { display: block; font-size: 24px; }
.error-card span { display: block; margin-top: 8px; }

body:not(.playing) .task-panel,
body:not(.playing) #repeat-button { visibility: hidden; }

/* ---------- phones: task panel drops to its own row, 48 px chrome ---------- */

@media (max-width: 620px) {
  /* The platform language pill sits under the home control, so the task panel starts right of it. */
  .hud {
    grid-template-columns: 96px 1fr auto;
    grid-template-areas: "home . actions" ". task task";
    row-gap: 10px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }
  .chrome-button { width: 48px; height: 48px; border-radius: 16px; }
  .chrome-button svg { width: 24px; height: 24px; }
  .hud-actions { gap: 10px; }
  .task-panel { justify-self: stretch; min-height: 56px; padding: 6px 12px 6px 8px; gap: 10px; }
  .goal-icon { width: 38px; height: 38px; border-radius: 12px; }
  .goal-icon svg { width: 26px; height: 26px; }
  #task-text { font-size: 18px; }
  .progress-dots i { width: 18px; height: 18px; }
  .map-route { grid-template-columns: 1fr; }
  .station { grid-template-columns: 96px 1fr; justify-items: start; align-items: center; text-align: left; padding: 6px 12px; }
  .station .station-art { grid-row: 1 / 3; width: 96px; }
  .route-line { display: none; }
  #keyboard-tray { display: none !important; }
  .celebration-actions { flex-wrap: nowrap; gap: 10px; width: 100%; }
  .action-button { flex: 1 1 0; flex-direction: column; gap: 2px; min-width: 0; padding: 6px 4px; font-size: 17px; }
}

@media (max-height: 540px) and (orientation: landscape) {
  .map-card { padding: 12px 18px 16px; }
  .map-card h1 { margin-bottom: 8px; }
  .station-art { width: 92px; }
  .celebration { padding-top: 60px; }
}

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