:root {
  --ink: #10251d;
  --muted: #64756f;
  --panel: #f8fbf8;
  --line: #cddbd4;
  --accent: #e43d30;
  --gold: #f2bd24;
  --green: #15945f;
  --deep: #0b3b2e;
  --blue: #2467c8;
  --shadow: 0 18px 55px rgba(5, 31, 23, .2);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    linear-gradient(90deg, rgba(255,255,255,.32) 1px, transparent 1px) 0 0 / 34px 34px,
    linear-gradient(0deg, rgba(255,255,255,.24) 1px, transparent 1px) 0 0 / 34px 34px,
    radial-gradient(circle at 18% 12%, rgba(21, 148, 95, .16), transparent 28%),
    linear-gradient(135deg, #e8f0ed, #d8e3df 45%, #eef2f0);
}

button {
  border: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.game-shell {
  display: grid;
  grid-template-rows: auto 1fr auto;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(12px, env(safe-area-inset-left));
  gap: 10px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 46px;
  padding: 7px 12px;
  background: rgba(248, 251, 248, .92);
  border: 1px solid rgba(11, 59, 46, .14);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(5, 31, 23, .1);
  backdrop-filter: blur(12px);
}

.topbar > div:first-child {
  min-width: 0;
}

.topbar strong {
  display: block;
  color: var(--deep);
  line-height: 1.1;
}

.topbar span,
.topbar #taskText {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  max-width: min(56vw, 640px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
}

.stats {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
}

.stats span,
.sound-toggle {
  min-width: 62px;
  padding: 6px 8px;
  text-align: center;
  color: var(--deep);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.sound-toggle {
  min-width: 72px;
  color: #fff;
  background: var(--deep);
  font-size: 12px;
  font-weight: 900;
}

.sound-toggle.is-muted {
  color: var(--deep);
  background: #eef5f1;
}

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(11, 59, 46, .22);
  border-radius: 8px;
  background: #83908d;
  box-shadow: var(--shadow);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 22px;
  background:
    linear-gradient(rgba(238, 245, 241, .9), rgba(238, 245, 241, .86)),
    radial-gradient(circle at 50% 38%, rgba(21, 148, 95, .18), transparent 42%);
  backdrop-filter: blur(8px);
  text-align: center;
}

.hidden {
  display: none;
}

.brand-mark {
  margin-bottom: 10px;
  padding: 7px 14px;
  border-radius: 7px;
  color: #fff;
  background: var(--deep);
  font-size: 18px;
  font-weight: 900;
  letter-spacing: 0;
}

.menu h1 {
  margin: 0 0 8px;
  font-size: clamp(30px, 5vw, 56px);
  letter-spacing: 0;
}

.menu p {
  max-width: 600px;
  margin: 0 0 20px;
  color: var(--muted);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(128px, 1fr));
  width: min(780px, 100%);
  gap: 10px;
}

.level-grid button,
.result-actions button {
  min-height: 48px;
  padding: 12px 14px;
  border-radius: 8px;
  color: #fff;
  background: var(--deep);
  font-weight: 900;
  box-shadow: 0 8px 18px rgba(5, 31, 23, .14);
}

.level-grid button:nth-child(2n) {
  background: var(--green);
}

.level-grid button:nth-child(3n) {
  background: var(--blue);
}

.level-grid button:last-child {
  background: var(--accent);
}

.result-title {
  margin-bottom: 8px;
  font-size: clamp(30px, 6vw, 58px);
  font-weight: 900;
}

.stars {
  min-height: 48px;
  color: var(--gold);
  font-size: 42px;
  letter-spacing: 0;
}

.result p {
  max-width: 600px;
  min-height: 24px;
  margin: 8px 0 18px;
  color: var(--muted);
}

.result-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.result-actions button:first-child {
  background: var(--accent);
}

.result-actions button:nth-child(2) {
  background: var(--green);
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1.35fr;
  gap: 12px;
  min-height: 78px;
}

.pad {
  display: grid;
  gap: 8px;
}

.steer-pad {
  grid-template-columns: repeat(2, 1fr);
}

.drive-pad {
  grid-template-columns: 1fr 1fr .85fr;
}

.controls button {
  min-height: 66px;
  border: 1px solid rgba(16, 37, 29, .14);
  border-radius: 8px;
  color: #fff;
  background: #263d36;
  font-size: 16px;
  font-weight: 900;
  box-shadow: 0 8px 18px rgba(5, 31, 23, .16);
  user-select: none;
}

.controls button:active,
.controls button.is-down {
  transform: translateY(1px);
  filter: brightness(1.08);
}

.drive-pad button:first-child {
  background: var(--green);
}

.drive-pad button:nth-child(2) {
  background: var(--accent);
}

.drive-pad button:nth-child(3) {
  color: #392900;
  background: var(--gold);
}

@media (max-width: 760px) {
  .game-shell {
    gap: 8px;
    padding-left: 8px;
    padding-right: 8px;
  }

  .topbar {
    min-height: 42px;
    padding: 6px 9px;
  }

  .stats span {
    min-width: 54px;
    padding: 5px 6px;
  }

  .level-grid {
    grid-template-columns: 1fr;
    max-width: 340px;
  }

  .controls {
    min-height: 82px;
    grid-template-columns: 1fr 1.2fr;
  }

  .controls button {
    min-height: 68px;
    font-size: 16px;
  }
}

  @media (orientation: landscape) and (max-height: 540px) {
  .game-shell {
    grid-template-columns: 1fr 230px;
    grid-template-rows: auto 1fr;
  }

  .topbar {
    grid-column: 1 / -1;
  }

  .controls {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .steer-pad,
  .drive-pad {
    grid-template-columns: 1fr;
  }

  .controls button {
    min-height: 44px;
  }
}
