:root {
  color-scheme: dark;
  --bg: #07080d;
  --panel: #111520;
  --panel-2: #080a10;
  --text: #f7f0d8;
  --muted: #aeb6a0;
  --line: rgba(247, 240, 216, 0.22);
  --accent: #f8d34a;
  --danger: #e24d3b;
  --good: #53d86a;
  --shadow: rgba(0, 0, 0, 0.7);
}

* { box-sizing: border-box; }

html {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  overscroll-behavior: none;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.04), transparent 18%),
    radial-gradient(circle at 50% 0%, rgba(214, 57, 43, 0.16), transparent 30%),
    var(--bg);
  color: var(--text);
  font-family: "Trebuchet MS", "Arial Black", system-ui, sans-serif;
}

button {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

.shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 18px;
}

.game-card {
  position: relative;
  width: min(1120px, 100%);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.05), transparent 8%),
    linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 4px solid #2e3546;
  border-radius: 0;
  padding: 14px;
  box-shadow:
    0 28px 90px var(--shadow),
    inset 0 0 0 2px #000,
    inset 0 0 0 6px rgba(255,255,255,0.05);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
  padding: 4px 4px 14px;
  text-transform: uppercase;
}

.kicker {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.12em;
}

h1 {
  margin: 0;
  color: #f7f0d8;
  font-size: clamp(1.7rem, 4vw, 3rem);
  letter-spacing: -0.05em;
  text-shadow: 3px 3px 0 #000, 5px 5px 0 #9b261f;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  color: var(--muted);
  font-size: 0.82rem;
}

.controls span {
  border: 2px solid #394254;
  border-radius: 0;
  padding: 6px 10px;
  background: #07080d;
  box-shadow: inset 0 0 0 2px #000;
}

.canvas-wrap {
  position: relative;
  overflow: hidden;
  touch-action: none;
  border-radius: 0;
  border: 4px solid #394254;
  background: #030407;
  box-shadow: inset 0 0 0 4px #000;
}

.canvas-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 100%, rgba(84, 209, 255, 0.08), transparent 42%),
    repeating-linear-gradient(
      180deg,
      rgba(255,255,255,0.035) 0,
      rgba(255,255,255,0.035) 1px,
      rgba(0,0,0,0.06) 2px,
      rgba(0,0,0,0.06) 5px
    );
  mix-blend-mode: screen;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100dvh - 190px);
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

.sound-toggle {
  min-width: 92px;
  min-height: 38px;
  border: 2px solid #f7f0d8;
  border-radius: 0;
  background: rgba(7, 8, 13, 0.92);
  color: var(--accent);
  cursor: pointer;
  font-size: 0.74rem;
  font-weight: 900;
  text-transform: uppercase;
  box-shadow: 4px 4px 0 #000, inset 0 0 0 2px #394254;
}

.sound-toggle:hover { filter: brightness(1.12); }

.sound-toggle:focus {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.72);
  z-index: 5;
}

.overlay.hidden { display: none; }

.overlay-panel {
  width: min(560px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  text-align: center;
  background: #111520;
  border: 4px solid #f7f0d8;
  border-radius: 0;
  padding: 26px;
  box-shadow: 10px 10px 0 #000, inset 0 0 0 4px #394254;
}

.overlay-panel h2 {
  margin: 0 0 12px;
  color: #f8d34a;
  font-size: clamp(1.6rem, 4vw, 2.7rem);
  letter-spacing: -0.05em;
  text-transform: uppercase;
  text-shadow: 3px 3px 0 #000;
}

.overlay-panel p {
  margin: 0 0 22px;
  color: var(--text);
  line-height: 1.45;
}

.player-field {
  display: grid;
  gap: 7px;
  margin-bottom: 18px;
  text-align: left;
}

.player-field label,
.difficulty-field legend,
.leaderboard h3 {
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.player-field input {
  width: 100%;
  min-height: 46px;
  border: 3px solid #394254;
  border-radius: 0;
  background: #07080d;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-weight: 900;
  padding: 10px 12px;
  box-shadow: inset 0 0 0 2px #000;
}

.player-field input:focus {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.difficulty-field {
  display: grid;
  gap: 8px;
  margin: 0 0 18px;
  padding: 0;
  border: 0;
  text-align: left;
}

.difficulty-field legend {
  padding: 0;
}

.difficulty-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.difficulty-options label {
  position: relative;
  min-width: 0;
}

.difficulty-options input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.difficulty-options span {
  display: grid;
  min-height: 44px;
  place-items: center;
  border: 3px solid #394254;
  background: #07080d;
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 900;
  text-transform: uppercase;
  box-shadow: inset 0 0 0 2px #000;
  cursor: pointer;
}

.difficulty-options input:checked + span {
  border-color: var(--accent);
  color: var(--accent);
  background: #211d0c;
}

.difficulty-options input:focus-visible + span {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.leaderboard {
  margin: 0 0 20px;
  text-align: left;
}

.leaderboard h3 {
  margin: 0 0 8px;
}

.leaderboard ol {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard li {
  display: grid;
  grid-template-columns: 2.5ch minmax(0, 1fr) auto;
  gap: 9px;
  align-items: center;
  min-height: 34px;
  border: 2px solid #394254;
  background: #07080d;
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 900;
  padding: 6px 8px;
  box-shadow: inset 0 0 0 2px #000;
}

.leaderboard .rank,
.leaderboard .score {
  color: var(--accent);
}

.leaderboard .name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard .leaderboard-message {
  grid-template-columns: 1fr;
  color: var(--muted);
  text-align: center;
}

.overlay-panel button,
.mobile-controls button {
  border: 3px solid #f7f0d8;
  border-radius: 0;
  padding: 12px 18px;
  background: #9b261f;
  color: #f7f0d8;
  font-weight: 900;
  cursor: pointer;
  text-transform: uppercase;
  box-shadow: 5px 5px 0 #000;
}

.overlay-panel button:hover,
.mobile-controls button:hover { filter: brightness(1.12); }

.mobile-controls {
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding-top: 14px;
  padding-bottom: env(safe-area-inset-bottom);
  touch-action: none;
}

.touch-hint { display: none; }

.mobile-controls button {
  min-height: 58px;
  font-size: clamp(1rem, 4.5vw, 1.4rem);
  touch-action: none;
}

.mobile-controls button:active {
  transform: translate(3px, 3px);
  box-shadow: 2px 2px 0 #000;
}

@media (max-width: 720px), (pointer: coarse) {
  .shell {
    padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    place-items: start center;
  }
  .game-card {
    padding: 8px;
    border-width: 3px;
  }
  .topbar {
    align-items: start;
    flex-direction: column;
    gap: 8px;
    padding-bottom: 8px;
  }
  h1 { font-size: clamp(1.35rem, 8vw, 2rem); }
  .kicker { font-size: 0.64rem; }
  .controls { justify-content: flex-start; font-size: 0.72rem; }
  .desktop-hint { display: none; }
  .touch-hint { display: inline-block; }
  .mobile-controls { display: grid; }
  .overlay { padding: 8px; align-items: end; }
  .overlay-panel {
    padding: 12px;
    max-height: calc(100dvh - 16px);
  }
  .overlay-panel h2 {
    font-size: clamp(1.15rem, 7vw, 1.7rem);
    margin-bottom: 8px;
  }
  .overlay-panel p {
    font-size: 0.86rem;
    line-height: 1.3;
    margin-bottom: 12px;
  }
  .player-field {
    margin-bottom: 12px;
  }
  .player-field input {
    min-height: 44px;
    padding: 8px 10px;
  }
  .leaderboard {
    margin-bottom: 12px;
  }
  .leaderboard h3,
  .difficulty-field legend,
  .player-field label {
    font-size: 0.68rem;
  }
  .difficulty-field {
    margin-bottom: 12px;
  }
  .difficulty-options {
    gap: 5px;
  }
  .difficulty-options span {
    min-height: 38px;
    border-width: 2px;
    font-size: 0.72rem;
  }
  .leaderboard ol {
    gap: 4px;
  }
  .leaderboard li {
    min-height: 30px;
    font-size: 0.78rem;
    padding: 4px 6px;
  }
  .overlay-panel button {
    width: 100%;
    min-height: 52px;
    padding: 10px 14px;
  }
}


@media (max-width: 720px) and (orientation: portrait), (pointer: coarse) and (orientation: portrait) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }
  .shell {
    min-height: 100dvh;
    height: 100dvh;
    display: block;
    padding: 0;
  }
  .game-card {
    width: 100vw;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
    border: 0;
    box-shadow: none;
  }
  .topbar {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 6;
    display: block;
    padding: 0;
  }
  .topbar > div:first-child,
  .topbar .controls span {
    display: none;
  }
  .topbar .controls {
    display: block;
  }
  .sound-toggle {
    min-width: 76px;
    min-height: 32px;
    font-size: 0.64rem;
  }
  .canvas-wrap {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    align-items: stretch;
    justify-content: center;
    border-width: 3px;
  }
  canvas {
    /* Mobile portrait must fill the available vertical space instead of
       falling back to a square-looking canvas. The JS sets the logical game
       arena to 390x720; this CSS scales that tall arena to the phone screen. */
    aspect-ratio: 390 / 720;
    height: 100%;
    width: auto;
    max-width: 100%;
    max-height: 100%;
    flex: 0 1 auto;
  }
  .mobile-controls {
    flex: 0 0 auto;
    padding: 8px 2px calc(6px + env(safe-area-inset-bottom));
    gap: 8px;
  }
  .mobile-controls button {
    min-height: 54px;
    font-size: 1.35rem;
  }
  .overlay {
    align-items: center;
  }
}

@media (max-width: 900px) and (orientation: landscape), (pointer: coarse) and (orientation: landscape) {
  .shell { padding: 4px; }
  .game-card { padding: 6px; }
  .topbar {
    flex-direction: row;
    align-items: center;
    padding-bottom: 4px;
  }
  .kicker { display: none; }
  h1 { font-size: 1.2rem; }
  .controls span { padding: 4px 7px; }
  canvas { max-height: calc(100dvh - 98px); }
  .mobile-controls { padding-top: 6px; }
  .mobile-controls button { min-height: 44px; padding: 8px 12px; }
  .overlay { align-items: center; }
  .overlay-panel { padding: 10px; }
  .overlay-panel h2 { font-size: 1.15rem; margin-bottom: 4px; }
  .overlay-panel p { font-size: 0.78rem; line-height: 1.22; margin-bottom: 8px; }
  .player-field { margin-bottom: 8px; }
  .player-field input { min-height: 38px; padding: 6px 8px; }
  .difficulty-field { margin-bottom: 8px; }
  .difficulty-options span { min-height: 34px; font-size: 0.66rem; }
  .leaderboard { margin-bottom: 8px; }
  .leaderboard li { min-height: 26px; font-size: 0.72rem; padding: 3px 6px; }
  .overlay-panel button { min-height: 42px; padding: 8px 10px; }
}
