:root {
  --ink: #30254d;
  --ink-soft: #5b4d7a;
  --paper: #fffaf0;
  --plum: #7452b8;
  --star: #ffd84d;
  --shadow: #39295e;
  --chrome-fill: #fff3df;
  --tap-target: 56px;
  --dpad-button: 64px;
  --dpad-gap: 12px;
  --chrome-size: 56px;
  --edge: max(10px, env(safe-area-inset-top));
  --ease-pop: cubic-bezier(.2, .9, .25, 1.2);
  --font-round: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100dvh;
  overflow: hidden;
  background: #e7d8b2;
  color: var(--ink);
  font-family: var(--font-round);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

#game-shell {
  position: fixed;
  inset: 0;
  min-height: 100dvh;
  overflow: hidden;
}

#game-shell[data-district="quay"] { --chrome-fill: #eef2f6; }
#game-shell[data-district="square"] { --chrome-fill: #f1f5fd; }

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

#game-canvas:focus-visible {
  outline: 4px solid var(--plum);
  outline-offset: -6px;
}

/* ---------- HUD ---------- */

.hud {
  position: absolute;
  inset: 0 0 auto 0;
  z-index: 20;
  pointer-events: none;
}

.hud > * { pointer-events: auto; }

.hud .tokenville-home-slot {
  position: absolute;
  top: var(--edge);
  left: max(10px, env(safe-area-inset-left));
}

.hud-actions {
  position: absolute;
  top: var(--edge);
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  gap: 10px;
}

.chrome-button {
  display: grid;
  place-items: center;
  width: var(--chrome-size);
  height: var(--chrome-size);
  min-width: 0;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--chrome-fill);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--shadow);
  cursor: pointer;
  transition: transform 220ms var(--ease-pop), box-shadow 120ms ease;
}

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

.chrome-button:active,
.dpad button:active,
.dpad button.pressed,
.completion-button:active {
  transform: scale(.94);
  transition-duration: 80ms;
}

.chrome-button:focus-visible,
.dpad button:focus-visible,
.completion-button:focus-visible {
  outline: 4px solid var(--plum);
  outline-offset: 3px;
}

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

.task-panel {
  position: absolute;
  top: var(--edge);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: min(600px, calc(100vw - 2 * (10px + 3 * var(--chrome-size) + 20px + 16px)));
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  grid-template-areas: "icon head" "icon line";
  column-gap: 10px;
  row-gap: 2px;
  align-items: center;
  padding: 7px 14px 8px 8px;
  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);
}

.goal-icon {
  grid-area: icon;
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: var(--chrome-fill);
  border: 2px solid rgba(48, 37, 77, .2);
}

.goal-icon svg {
  width: 40px;
  height: 40px;
  stroke: var(--ink);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  overflow: visible;
}

/* In mixed mode a small tag on the goal icon names the language of this level. */
.language-badge {
  position: absolute;
  z-index: 1;
  top: -9px;
  left: -9px;
  min-width: 34px;
  padding: 2px 6px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--star);
  color: var(--ink);
  font-size: 16px;
  font-weight: 900;
  line-height: 1.1;
  text-align: center;
  box-shadow: 0 2px 0 var(--shadow);
  pointer-events: none;
}

.language-badge[hidden] { display: none; }
.language-badge[data-lang="en"] { background: #cfe6ff; }
.language-badge[data-lang="et"] { background: #c9f0d8; }

.goal-icon.pulse {
  box-shadow: 0 0 0 4px var(--star);
  animation: goal-pulse 900ms ease-in-out infinite;
}

@keyframes goal-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.task-head {
  grid-area: head;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.task-title {
  margin: 0;
  font-size: 16px;
  font-weight: 900;
  line-height: 1.1;
  color: var(--ink-soft);
  white-space: nowrap;
}

.task-line {
  grid-area: line;
  display: block;
  font-size: 20px;
  font-weight: 900;
  line-height: 1.15;
  color: var(--ink);
}

.cargo-pips {
  display: flex;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.cargo-pips li {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--paper);
}

.cargo-pips li.loaded { background: #dff0ff; }
.cargo-pips li.delivered { background: var(--star); }

.cargo-pips svg {
  width: 19px;
  height: 19px;
  stroke: var(--ink);
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.cargo-pips li:not(.delivered) svg { opacity: .9; }

.sun {
  display: block;
  width: 56px;
  height: 30px;
  margin-left: auto;
  flex: none;
}

.sun svg { width: 100%; height: 100%; overflow: visible; }
.sun-ground { stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; fill: none; }
.sun-arc { stroke: var(--ink-soft); stroke-width: 2; stroke-dasharray: 2 4; stroke-linecap: round; fill: none; }
.sun-disc { fill: #ffb36b; stroke: var(--ink); stroke-width: 2.2; transition: cx 400ms var(--ease-pop), cy 400ms var(--ease-pop); }
#game-shell[data-time="noon"] .sun-disc { fill: var(--star); }
#game-shell[data-time="evening"] .sun-disc { fill: #ff8a5c; }
#game-shell[data-time="evening"] .sun-arc { stroke: var(--plum); }

.message {
  position: absolute;
  z-index: 18;
  left: 50%;
  top: 120px;
  max-width: calc(100vw - 24px);
  padding: 8px 18px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  box-shadow: 0 4px 0 var(--shadow);
  color: var(--ink);
  font-size: 18px;
  font-weight: 900;
  line-height: 1.2;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -6px) scale(.96);
  transition: opacity 180ms ease, transform 260ms var(--ease-pop);
}

.message.show {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}

/* ---------- d-pad ---------- */

.dpad {
  position: absolute;
  z-index: 15;
  right: max(16px, calc(env(safe-area-inset-right) + 12px));
  bottom: max(16px, calc(env(safe-area-inset-bottom) + 12px));
  display: grid;
  grid-template-columns: repeat(3, var(--dpad-button));
  grid-template-rows: repeat(3, var(--dpad-button));
  gap: var(--dpad-gap);
  padding: var(--dpad-gap);
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: rgba(255, 250, 240, .9);
  box-shadow: 0 6px 0 var(--shadow), 0 14px 28px rgba(31, 24, 51, .22);
  touch-action: none;
}

#game-shell.is-complete .dpad { visibility: hidden; }

.dpad button {
  width: var(--dpad-button);
  height: var(--dpad-button);
  min-width: var(--tap-target);
  min-height: var(--tap-target);
  display: grid;
  place-items: center;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--chrome-fill);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--shadow);
  cursor: pointer;
  touch-action: none;
  transition: transform 220ms var(--ease-pop);
}

.dpad button svg {
  width: 32px;
  height: 32px;
  fill: none;
  stroke: currentColor;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

.dpad-center {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  place-items: center;
}

.dpad-center svg {
  width: 40px;
  height: 40px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dpad-center .bag { fill: #c8743c; }
.dpad-center .flap { fill: var(--paper); }

/* ---------- completion ---------- */

.completion {
  position: absolute;
  inset: 0;
  z-index: 900;
  display: grid;
  place-items: center;
  padding: 20px 16px 132px;
  background: rgba(48, 37, 77, .42);
}

.completion-card {
  width: min(480px, 100%);
  padding: 22px 20px 24px;
  border: 3px solid var(--ink);
  border-radius: 28px;
  background: var(--paper);
  box-shadow: 0 8px 0 var(--shadow), 0 24px 48px rgba(31, 24, 51, .3);
  text-align: center;
  animation: card-in 320ms var(--ease-pop) both;
}

.completion-star {
  width: 120px;
  height: 120px;
  fill: #ffd84d;
  stroke: #a86100;
  stroke-width: 1.3;
  stroke-linejoin: round;
}

.completion h2 {
  margin: 6px 0 18px;
  font-size: 30px;
  font-weight: 950;
  line-height: 1.1;
}

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

.completion-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 128px;
  min-height: 64px;
  padding: 8px 16px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow);
  font: 900 18px/1 var(--font-round);
  text-decoration: none;
  cursor: pointer;
  transition: transform 220ms var(--ease-pop);
}

.completion-button.primary { background: var(--star); }

@keyframes card-in {
  from { opacity: 0; transform: translateY(18px) scale(.94); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- responsive ---------- */

@media (max-width: 899px) {
  .task-panel {
    top: calc(var(--edge) + var(--chrome-size) + 10px);
    max-width: calc(100vw - 16px);
  }
}

@media (max-width: 560px), (max-height: 520px) {
  :root {
    --chrome-size: 48px;
    --dpad-button: 56px;
  }

  .hud-actions { gap: 8px; }
  .chrome-button { border-radius: 16px; }
  .chrome-button svg { width: 24px; height: 24px; }

  .task-panel {
    top: calc(var(--edge) + var(--chrome-size) + 10px);
    grid-template-columns: 44px minmax(0, 1fr);
    column-gap: 8px;
    padding: 6px 10px 7px 6px;
  }

  .goal-icon { width: 44px; height: 44px; border-radius: 14px; }
  .goal-icon svg { width: 34px; height: 34px; }
  .task-head { gap: 6px; }
  .task-line { font-size: 18px; }
  .cargo-pips { gap: 3px; }
  .cargo-pips li { width: 23px; height: 23px; }
  .cargo-pips svg { width: 16px; height: 16px; }
  .sun { width: 46px; height: 25px; }
  .message { font-size: 16px; padding: 7px 14px; }
  .completion-star { width: 96px; height: 96px; }
  .completion h2 { font-size: 26px; }
  .completion-button { min-width: 100px; padding: 8px 12px; }
  .completion { padding-bottom: 128px; }
}

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