:root {
  --ink: #161410;
  --gold: #ffc83d;
  --amber: #ff9f1c;
  --cream: #fff8e6;
  --red: #ff4d5f;
  --cyan: #3bd6ff;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  touch-action: none;
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 200, 61, 0.28), transparent 30%),
    linear-gradient(145deg, #1d1a18 0%, #36261a 54%, #151515 100%);
  color: var(--cream);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

.back-to-portal {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: max(8px, env(safe-area-inset-left));
  z-index: 10;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 2px solid rgba(255, 200, 61, 0.65);
  border-radius: 8px;
  background: rgba(22, 20, 16, 0.78);
  color: #fff8e6;
  font-size: 20px;
  font-weight: 900;
  text-decoration: none;
}

.game-shell {
  width: 100vw;
  height: 100svh;
  min-height: 480px;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  justify-items: center;
}

.mission {
  width: min(100%, 520px);
  min-height: 24px;
  display: grid;
  place-items: center;
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(255, 248, 230, 0.12);
  color: #fff8e6;
  font-size: clamp(12px, 3.2vw, 15px);
  line-height: 1.2;
  text-align: center;
}

.hud {
  width: min(100%, 520px);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 2px solid rgba(255, 200, 61, 0.5);
  border-radius: 8px;
  background: rgba(22, 20, 16, 0.72);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.26);
}

.hud h1 {
  margin: 0;
  color: var(--gold);
  font-size: clamp(18px, 4.6vw, 28px);
  line-height: 1;
  white-space: nowrap;
}

.hud div:last-child {
  text-align: right;
}

.label {
  display: block;
  color: rgba(255, 248, 230, 0.72);
  font-size: 10px;
  letter-spacing: 0;
}

.hud strong {
  color: #fff;
  font-size: clamp(18px, 5vw, 28px);
  line-height: 1;
}

.stage-wrap {
  position: relative;
  width: min(96vw, calc(100svh - 178px), 520px);
  aspect-ratio: 1;
  min-width: 280px;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 8px;
  background: #120f13;
  box-shadow:
    0 0 0 2px rgba(255, 200, 61, 0.5),
    0 18px 44px rgba(0, 0, 0, 0.42);
}

.message {
  position: absolute;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: min(78%, 310px);
  padding: 14px 16px;
  border-radius: 8px;
  border: 2px solid rgba(255, 200, 61, 0.78);
  background: rgba(15, 13, 14, 0.9);
  text-align: center;
  pointer-events: none;
}

.message.is-hidden {
  display: none;
}

.message b {
  display: block;
  color: var(--gold);
  font-size: clamp(24px, 8vw, 42px);
  line-height: 1;
}

.message span {
  display: block;
  margin-top: 6px;
  font-size: clamp(12px, 3.4vw, 15px);
}

.controls {
  width: min(88vw, 340px);
  height: 92px;
  display: grid;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(2, 42px);
  gap: 6px;
  justify-content: center;
  align-content: center;
}

.controls button {
  border: 0;
  border-radius: 8px;
  background: linear-gradient(#fff4a6, var(--gold));
  color: var(--ink);
  font-weight: 900;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.22), 0 6px 12px rgba(0, 0, 0, 0.26);
}

.controls button:active {
  transform: translateY(2px);
}

.controls [data-dir="up"] { grid-column: 2; grid-row: 1; }
.controls [data-dir="left"] { grid-column: 1; grid-row: 2; }
.controls [data-dir="down"] { grid-column: 2; grid-row: 2; }
.controls [data-dir="right"] { grid-column: 3; grid-row: 2; }

@media (min-width: 740px) and (min-height: 700px) {
  .controls {
    height: 54px;
    grid-template-columns: repeat(4, 54px);
    grid-template-rows: 44px;
  }

  .controls [data-dir] {
    grid-row: 1;
  }

  .controls [data-dir="left"] { grid-column: 1; }
  .controls [data-dir="up"] { grid-column: 2; }
  .controls [data-dir="down"] { grid-column: 3; }
  .controls [data-dir="right"] { grid-column: 4; }
}

@media (max-height: 560px) {
  .game-shell {
    min-height: 0;
    gap: 5px;
  }

  .hud {
    padding: 5px 8px;
  }

  .mission {
    min-height: 20px;
    padding: 2px 8px;
    font-size: 11px;
  }

  .controls {
    height: 58px;
    grid-template-columns: repeat(4, 50px);
    grid-template-rows: 42px;
  }

  .controls [data-dir] {
    grid-row: 1;
  }

  .controls [data-dir="left"] { grid-column: 1; }
  .controls [data-dir="up"] { grid-column: 2; }
  .controls [data-dir="down"] { grid-column: 3; }
  .controls [data-dir="right"] { grid-column: 4; }
}
