* {
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  /* Mirrors the default theme's chrome roles; an e2e test fails on drift. */
  --outline: #160720;
  --slate: #5a1b33;
  --line: #4a1b3d;
  --border: #6b2547;
  --mute: #9d6b8f;
  --hint: #c9a0b8;
  --text-soft: #e6cfe0;
  --text: #f8eef5;
  --accent: #fb923c;
  --accent-bright: #fdba74;
  --accent-ink: #431407;
  --amber: #fcd34d;
  --star: #fde68a;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh; /* mobile chrome: vh overshoots the visible viewport */
  display: flex;
  /* auto margins on .shell centre it while it fits and let the page scroll from the top when it does not */
  padding: 16px 12px calc(32px + env(safe-area-inset-bottom, 0px));
  background: radial-gradient(circle at 50% 0%, var(--slate), var(--outline) 70%);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.shell {
  /* board height: fits the viewport height, a 660px cap, or the width left after padding */
  --board-height: min(74vh, 660px, calc((100vw - 56px) * 1.5));
  --board-height: min(74dvh, 660px, calc((100vw - 56px) * 1.5));
  width: min(100%, calc(var(--board-height) * 2 / 3 + 32px));
  margin: auto;
  padding: 20px 16px 28px;
  display: grid;
  gap: 12px;
  justify-items: center;
}

.masthead {
  width: 100%;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.masthead h1 {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent-bright);
}

.masthead .back {
  font-size: 0.85rem;
  color: var(--hint);
  text-decoration: none;
}

.masthead .back:hover,
.masthead .back:focus-visible {
  color: var(--accent-bright);
}

.hud {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}

.stat strong {
  color: var(--accent);
  font-size: 1.15rem;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 24px 60px -24px #000, 0 0 0 1px var(--line);
  touch-action: none; /* steering surface, not a scroll surface */
  overscroll-behavior: contain;
}

/* Portrait game: in landscape, ask for a rotate instead of resizing the board. */
@media (orientation: landscape) and (max-height: 560px) {
  .board::after {
    content: 'rotate your device';
    position: absolute;
    inset: 0;
    display: grid;
    place-content: center;
    background: rgba(2, 6, 23, 0.86);
    color: var(--hint);
    font-size: 1rem;
    letter-spacing: 0.06em;
  }
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.timer {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 4px 14px;
  border: 1px solid var(--mute);
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  color: var(--text);
  font-size: 0.9rem;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}

.bonus {
  position: absolute;
  top: 12px;
  left: 12px;
  margin: 0;
  padding: 4px 12px;
  border: 1px solid var(--mute);
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  color: var(--star);
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}

/* Level-change indicator: leaving `hidden` restarts both animations, `animationend` re-hides. */
.flash {
  position: absolute;
  inset: 0;
  background: var(--accent);
  opacity: 0;
  pointer-events: none;
  animation: flash 0.6s ease-out;
}

.banner {
  position: absolute;
  top: 26%;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 18px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  color: var(--accent);
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  animation: banner 1.8s ease-out;
}

@keyframes flash {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 0;
  }
}

@keyframes banner {
  0% {
    opacity: 0;
    transform: translate(-50%, 12px) scale(0.9);
  }
  15%,
  75% {
    opacity: 1;
    transform: translate(-50%, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -8px) scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .banner {
    animation: banner-fade 1.8s ease-out;
  }
  .flash {
    animation: flash 0.25s ease-out;
  }
}

@keyframes banner-fade {
  0% {
    opacity: 0;
  }
  15%,
  75% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.boost {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 56px;
  height: 56px;
  padding: 0;
  font-size: 1.5rem;
  line-height: 1;
  border-color: var(--star);
  background: rgba(2, 6, 23, 0.72);
  color: var(--star);
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(3px);
}

.overlay[hidden] {
  display: none;
}

.splash {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  padding: 16px 12px;
  text-align: center;
  background: rgba(2, 6, 23, 0.92);
  backdrop-filter: blur(3px);
  cursor: pointer;
  overflow: hidden;
}

.splash[hidden] {
  display: none;
}

.splash-title {
  margin: 0;
  font-size: 1.7rem;
  letter-spacing: 0.04em;
  color: var(--accent-bright);
}

.splash-tag {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-soft);
}

.splash-hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--hint);
}

.overlay h2 {
  margin: 0;
  font-size: 1.6rem;
  color: var(--amber);
}

.about {
  width: 100%;
  margin-top: 4px;
  display: grid;
  gap: 10px;
  text-align: left;
  color: var(--hint);
  font-size: 0.82rem;
  line-height: 1.5;
}

.about h2 {
  margin: 0;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-soft);
}

.about p {
  margin: 0;
}

.legend {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 8px;
}

.legend li {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 10px;
  align-items: center;
}

.legend canvas {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
}

.legend strong {
  color: var(--text-soft);
}

.overlay p {
  margin: 0;
  color: var(--text-soft);
}

button {
  padding: 10px 22px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--slate);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}

button:hover {
  background: var(--line);
  border-color: var(--mute);
}

button:active {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

button.primary:hover {
  background: var(--accent-bright);
}

.hint {
  margin: 0;
  color: var(--hint);
  font-size: 0.8rem;
  text-align: center;
}
