:root {
  --sky-top: #8fd3ff;
  --sky-bottom: #fdf7c8;
  --panel: rgba(255, 255, 255, 0.84);
  --panel-strong: rgba(255, 255, 255, 0.94);
  --ink: #22304a;
  --accent: #ff7a59;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "Trebuchet MS", "Gill Sans", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.7), transparent 35%),
    linear-gradient(180deg, var(--sky-top), var(--sky-bottom));
  display: grid;
  place-items: center;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

.game-shell {
  width: min(100%, 1024px);
  padding: 16px;
}

.hud,
.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 16px 20px;
  background: var(--panel);
  border-radius: 20px;
  box-shadow: 0 16px 50px rgba(34, 48, 74, 0.16);
}

.hud {
  margin-bottom: 12px;
}

.hud h1 {
  margin: 0 0 8px;
  font-size: clamp(1.8rem, 4vw, 3rem);
}

.hud p,
.controls p {
  margin: 0;
}

.stats {
  display: grid;
  gap: 8px;
  text-align: right;
  font-weight: 700;
}

.controls-panel {
  margin-bottom: 12px;
}

.controls-details {
  background: var(--panel);
  border-radius: 18px;
  box-shadow: 0 16px 50px rgba(34, 48, 74, 0.16);
  overflow: hidden;
}

.controls-details summary {
  padding: 14px 18px;
  cursor: pointer;
  font-weight: 700;
  list-style: none;
  background: rgba(255, 255, 255, 0.2);
}

.controls-details summary::-webkit-details-marker {
  display: none;
}

.controls-details summary::after {
  content: "▾";
  float: right;
}

.controls-details[open] summary::after {
  content: "▴";
}

.controls {
  flex-wrap: wrap;
  justify-content: flex-start;
  padding-top: 0;
  border-radius: 0 0 18px 18px;
  box-shadow: none;
  background: transparent;
}

.mode-toggle,
.easy-mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
}

.mode-toggle select {
  font: inherit;
  border-radius: 999px;
  border: 0;
  padding: 10px 14px;
  color: var(--ink);
  background: white;
}

.easy-mode-toggle input {
  inline-size: 18px;
  block-size: 18px;
}

.density-toggle {
  min-width: 220px;
}

.density-toggle input[type="range"] {
  width: 120px;
}

body.pinball-mode #score {
  background: linear-gradient(90deg, #ff595e, #ffca3a, #8ac926, #4cc9f0, #c77dff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.22);
}

body.pinball-mode #level {
  color: #7a2cff;
}

.game-stage {
  position: relative;
}

canvas {
  width: 100%;
  height: auto;
  display: block;
  margin: 0;
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(34, 48, 74, 0.22);
  background: linear-gradient(180deg, #6ec2ff 0%, #dff7ff 58%, #82d173 58%, #5faf54 100%);
  touch-action: none;
}

button {
  border: 0;
  border-radius: 999px;
  padding: 12px 20px;
  font: inherit;
  font-weight: 700;
  color: white;
  background: var(--accent);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:hover {
  filter: brightness(1.05);
}

button:active,
.controls-details summary:active {
  transform: translateY(1px) scale(0.985);
}

.touch-overlay {
  display: none;
}

body.immersive-mode {
  background: #09111d;
}

body.immersive-mode .game-shell {
  position: fixed;
  inset: 0;
  width: 100vw;
  max-width: none;
  padding: 0;
  z-index: 100;
}

body.immersive-mode .hud,
body.immersive-mode .controls-panel {
  display: none;
}

body.immersive-mode .game-stage {
  width: 100vw;
  height: 100dvh;
  display: grid;
  place-items: center;
  background: #09111d;
}

body.immersive-mode canvas {
  width: 100vw;
  height: 100dvh;
  max-height: none;
  border-radius: 0;
  box-shadow: none;
  object-fit: contain;
}

@media (hover: none), (pointer: coarse) {
  body {
    align-items: start;
  }

  .game-shell {
    width: 100%;
    max-width: 100%;
    padding: 10px;
  }

  .hud {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 14px;
    margin-bottom: 10px;
  }

  .stats {
    text-align: left;
  }

  .controls-panel {
    margin-bottom: 10px;
  }

  .controls-details:not([open]) + * {
    margin-top: 0;
  }

  .controls-details summary {
    padding: 12px 14px;
  }

  .controls {
    gap: 12px;
    padding: 0 14px 14px;
  }

  .game-stage {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  canvas {
    border-radius: 18px;
    max-height: calc(100dvh - 205px);
    object-fit: contain;
  }

  .touch-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 14px;
    pointer-events: none;
  }

  .touch-move,
  .touch-actions {
    pointer-events: auto;
  }

  .touch-move {
    display: grid;
    grid-template-columns: repeat(2, 68px);
    gap: 12px;
  }

  .touch-actions {
    display: grid;
    gap: 12px;
    justify-items: end;
  }

  .touch-icon {
    width: 68px;
    height: 68px;
    padding: 0;
    display: grid;
    place-items: center;
    font-size: 1.8rem;
    border-radius: 50%;
    background: rgba(34, 48, 74, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.55);
    backdrop-filter: blur(6px);
    box-shadow: 0 10px 24px rgba(12, 18, 28, 0.2);
  }

  .touch-icon-move {
    background: rgba(34, 48, 74, 0.34);
  }

  .touch-icon-jump {
    background: rgba(43, 127, 255, 0.42);
  }

  .touch-icon-swat {
    background: rgba(255, 122, 89, 0.42);
  }

  .touch-icon-expand {
    background: rgba(34, 48, 74, 0.38);
    font-size: 1.5rem;
  }

  body.immersive-mode .touch-overlay {
    padding:
      max(12px, env(safe-area-inset-top))
      max(12px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom))
      max(12px, env(safe-area-inset-left));
  }

  .controls-details summary {
    min-height: 48px;
    display: flex;
    align-items: center;
  }
}

@media (hover: none) and (orientation: landscape), (pointer: coarse) and (orientation: landscape) {
  .game-shell {
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  }

  .hud {
    margin-bottom: 8px;
    padding: 10px 12px;
  }

  .hud h1 {
    font-size: clamp(1.2rem, 3vw, 1.8rem);
  }

  .controls-panel {
    margin-bottom: 8px;
  }

  canvas {
    max-height: calc(100dvh - 150px);
  }

  .touch-move {
    grid-template-columns: repeat(2, 60px);
  }

  .touch-icon {
    width: 60px;
    height: 60px;
  }
}
