:root {
  --green: #00a321;
  --green-dark: #008f1d;
  --mint: #c5ffd7;
  --text: #142019;
  --board-width: min(480px, calc((100dvh - 86px) * 0.8), calc(100vw - 28px));
  --cell: calc(var(--board-width) / 12);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

*::before,
*::after {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

img,
svg,
canvas,
button {
  touch-action: manipulation;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  color: var(--text);
  font-family: "SB Sans Display", Arial, Helvetica, sans-serif;
  background: #ece2d8;
}

button {
  font: inherit;
}

.game {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: radial-gradient(50% 50% at 50% 50%, rgba(236, 226, 216, 0.5) 21.15%, #ece2d8 100%);
}

.game::before {
  content: "";
  position: absolute;
  inset: -26px;
  z-index: 0;
  background-image:
    radial-gradient(50% 50% at 50% 50%, rgba(236, 226, 216, 0.5) 21.15%, #ece2d8 100%),
    url("assets/bg_layer.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  filter: blur(2px);
  opacity: 1;
  transform: scale(1.05);
}

.scene {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: min-content min-content;
  justify-items: center;
  align-content: center;
  gap: clamp(10px, 2.1vh, 18px);
  padding: clamp(12px, 2vw, 24px);
}

.maze-board {
  position: relative;
  width: var(--board-width);
  height: calc(var(--cell) * 15);
  overflow: hidden;
  touch-action: none;
  border-radius: calc(var(--cell) * 0.34);
  background-image: url("assets/bg_desk.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  box-shadow: 0 18px 55px rgba(28, 20, 13, 0.28);
}

.maze-board.is-denied {
  animation: boardDeny 180ms ease;
}

.car {
  position: absolute;
  left: var(--car-left, 12.5%);
  top: var(--car-top, 23.333%);
  z-index: 5;
  width: var(--cell);
  height: var(--cell);
  transform:
    translate(-50%, -50%)
    rotate(var(--car-rotation, 180deg));
  transform-origin: 50% 50%;
  backface-visibility: hidden;
  filter: drop-shadow(0 4px 5px rgba(36, 23, 12, 0.28));
  transition:
    transform 180ms cubic-bezier(0.2, 0.82, 0.2, 1),
    filter 180ms ease;
  will-change: left, top, transform;
}

.car img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  transform: scale(-1, -1);
  transform-origin: 50% 50%;
  backface-visibility: hidden;
}

.car.is-moving {
  filter: drop-shadow(0 4px 5px rgba(36, 23, 12, 0.28));
}

.car.is-denied {
  animation: carDeny 180ms ease;
}

.finish-spark {
  position: absolute;
  left: 87.5%;
  top: 90%;
  z-index: 4;
  width: calc(var(--cell) * 1.1);
  height: calc(var(--cell) * 1.1);
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.4);
}

.finish-spark.is-active {
  animation: finishSpark 620ms ease both;
}

.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.control-button {
  --button-size: 40px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.88);
  border-radius: 10px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(245, 248, 244, 0.96));
  box-shadow:
    0 4px 10px rgba(38, 26, 16, 0.12),
    inset 0 -1px 0 rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: transform 130ms ease, opacity 130ms ease, box-shadow 130ms ease, background 130ms ease;
}

.control-button::before {
  content: "";
  width: 22%;
  height: 22%;
  border-right: max(2px, calc(var(--button-size, 40px) * 0.052)) solid var(--arrow-color, var(--green));
  border-bottom: max(2px, calc(var(--button-size, 40px) * 0.052)) solid var(--arrow-color, var(--green));
  border-radius: 1.5px;
  transform: translate(var(--arrow-shift-x, 0), var(--arrow-shift-y, 0)) rotate(var(--arrow-rotation, -45deg));
  transform-origin: 50% 50%;
  transition: transform 130ms ease;
}

.control-button[data-direction="up"] {
  --arrow-rotation: -135deg;
  --arrow-shift-y: 7%;
}

.control-button[data-direction="right"] {
  --arrow-rotation: -45deg;
  --arrow-shift-x: -7%;
}

.control-button[data-direction="down"] {
  --arrow-rotation: 45deg;
  --arrow-shift-y: -7%;
}

.control-button[data-direction="left"] {
  --arrow-rotation: 135deg;
  --arrow-shift-x: 7%;
}

.control-button:disabled {
  cursor: default;
  opacity: 0.52;
}

.control-button:not(:disabled):active,
.control-button.is-active {
  --arrow-color: #ffffff;
  border-color: rgba(0, 143, 29, 0.94);
  transform: translateY(1px) scale(0.95);
  background:
    linear-gradient(180deg, #08b63a, var(--green-dark));
  box-shadow:
    0 2px 6px rgba(38, 26, 16, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.24);
}

.control-button:focus-visible {
  outline: 3px solid rgba(0, 163, 33, 0.42);
  outline-offset: 3px;
}

.final-modal {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(238, 228, 218, 0.64);
  backdrop-filter: blur(5px);
  animation: modalIn 240ms ease both;
}

.final-modal[hidden] {
  display: none;
}

.final-card {
  position: relative;
  width: min(807px, calc(100vw - 36px));
  height: 244px;
  display: grid;
  justify-items: center;
  align-content: center;
  padding: 58px 64px 32px;
  border: 1px solid rgba(255, 255, 255, 0.86);
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(197, 255, 215, 0.94), rgba(255, 255, 255, 0.96));
  text-align: center;
  box-shadow: 0 24px 80px rgba(43, 28, 17, 0.16);
  animation: cardIn 260ms cubic-bezier(0.22, 0.8, 0.22, 1) both;
}

.final-icon {
  position: absolute;
  left: 50%;
  top: 0;
  width: 86px;
  height: 86px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #ffffff;
  transform: translate(-50%, -50%);
}

.car-sequence {
  position: relative;
  width: 86px;
  height: 86px;
  display: block;
  overflow: hidden;
  border-radius: 50%;
  transform: translateZ(0);
}

.car-sequence img {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 100%;
  height: 100%;
  object-fit: contain;
  transform-origin: 50% 50%;
  backface-visibility: hidden;
  pointer-events: none;
  will-change: opacity;
}

.car-sequence-base {
  opacity: 1;
  transform: translate(-50%, -50%);
}

.car-sequence-light {
  opacity: 0;
  transform: translate(-50%, -50%);
  animation: carLightFrame 1600ms steps(1, end) infinite;
}

.final-card h1 {
  max-width: 720px;
  margin: 0;
  font-size: 24px;
  line-height: 1.14;
  letter-spacing: 0;
  font-weight: 500;
}

.primary-button {
  min-width: 278px;
  height: 56px;
  margin-top: 28px;
  padding: 0 28px;
  border: 0;
  border-radius: 8px;
  background: var(--green);
  color: #ffffff;
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
}

.primary-button:hover,
.primary-button:focus-visible {
  background: var(--green-dark);
  outline: none;
}

@keyframes boardDeny {
  0%,
  100% {
    transform: translateX(0);
  }
  34% {
    transform: translateX(-3px);
  }
  68% {
    transform: translateX(3px);
  }
}

@keyframes carDeny {
  0%,
  100% {
    transform: translate(-50%, -50%) rotate(var(--car-rotation, 180deg));
  }
  50% {
    transform: translate(-50%, -50%) rotate(var(--car-rotation, 180deg)) translateX(-2px);
  }
}

@keyframes finishSpark {
  0% {
    opacity: 0.9;
    box-shadow: 0 0 0 0 rgba(0, 163, 33, 0.34);
    transform: translate(-50%, -50%) scale(0.4);
  }
  100% {
    opacity: 0;
    box-shadow: 0 0 0 calc(var(--cell) * 1.6) rgba(0, 163, 33, 0);
    transform: translate(-50%, -50%) scale(1);
  }
}

@keyframes modalIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes cardIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes carLightFrame {
  0%,
  42%,
  72.01%,
  100% {
    opacity: 0;
  }
  42.01%,
  72% {
    opacity: 1;
  }
}

@media (max-width: 1200px) and (min-width: 561px) {
  :root {
    --board-width: min(480px, calc(100vw - 48px), calc((100dvh - 112px) * 0.8));
  }

  .scene {
    gap: clamp(12px, 2vh, 20px);
    padding: 20px;
  }

  .control-button {
    --button-size: 46px;
    width: 46px;
    height: 46px;
    border-radius: 12px;
  }

  .controls {
    gap: 12px;
  }
}

@media (max-width: 560px) {
  :root {
    --board-width: min(360px, calc(100vw - 28px), calc((100dvh - 84px) * 0.8));
  }

  .game::before {
    background-image:
      radial-gradient(50% 50% at 50% 50%, rgba(236, 226, 216, 0.5) 21.15%, #ece2d8 100%),
      url("assets/bg_layer.png");
    opacity: 1;
  }

  .scene {
    gap: 12px;
    padding: 8px 0 16px;
  }

  .maze-board {
    background-image: url("assets/bg_mob.png");
    border-radius: calc(var(--cell) * 0.32);
  }

  .car {
    width: var(--cell);
    height: var(--cell);
  }

  .controls {
    gap: 9px;
  }

  .control-button {
    --button-size: 50px;
    width: 50px;
    height: 50px;
    border-radius: 12px;
  }

  .final-card {
    width: min(333px, calc(100vw - 42px));
    height: 244px;
    padding: 62px 28px 26px;
    border-radius: 32px;
  }

  .final-icon {
    width: 65px;
    height: 65px;
  }

  .final-icon .car-sequence {
    width: 65px;
    height: 65px;
  }

  .final-card h1 {
    max-width: 278px;
    font-size: 16px;
    line-height: 1.16;
    font-weight: 500;
  }

  .primary-button {
    min-width: 200px;
    height: 42px;
    margin-top: 24px;
    font-size: 16px;
  }
}
