:root {
  --bg: #10141f;
  --panel: #181e2b;
  --panel-2: #202838;
  --line: #334055;
  --text: #f4f7fb;
  --muted: #aab5c4;
  --green: #59d487;
  --yellow: #ffd166;
  --red: #ef476f;
  --blue: #4cc9f0;
  --cell: #1c2432;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--text);
  background:
    linear-gradient(135deg, rgba(89, 212, 135, 0.13), transparent 34%),
    linear-gradient(315deg, rgba(239, 71, 111, 0.11), transparent 38%),
    var(--bg);
  font-family: Inter, "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

button,
input {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel-2);
  cursor: pointer;
}

button:hover {
  border-color: #71829d;
  background: #273144;
}

.shell {
  display: grid;
  grid-template-columns: minmax(0, 820px);
  width: min(860px, calc(100vw - 20px));
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(10px, 2vw, 22px) 0;
  align-items: center;
  justify-content: center;
}

.playfield,
.panel {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  background: rgba(24, 30, 43, 0.86);
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.32);
}

.playfield {
  padding: 18px;
  min-width: 0;
}

.topbar {
  margin-bottom: 8px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--green);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.45rem, 3vw, 2.3rem);
  line-height: 1.1;
}

.scoreboard,
.stat-grid {
  display: grid;
  gap: 8px;
}

.scoreboard {
  min-width: 0;
  grid-template-columns: repeat(4, minmax(0, 1fr)) minmax(104px, 0.85fr);
}

.stat-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.scoreboard div,
.stat-grid div,
.keys div {
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(16, 20, 31, 0.72);
}

.stat-grid .highlight-stat {
  border-color: rgba(255, 209, 102, 0.72);
  background: rgba(255, 209, 102, 0.1);
}

.scoreboard span,
.stat-grid span,
.keys span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
}

.scoreboard strong,
.stat-grid strong {
  display: block;
  margin-top: 2px;
  font-size: 1.08rem;
}

.board-start {
  grid-column: auto;
  border-color: var(--yellow);
  color: #231b02;
  background: var(--yellow);
  font-weight: 800;
}

.scoreboard .board-start {
  min-height: auto;
  padding: 0 12px;
}

.board-controls {
  display: grid;
  grid-template-columns: 1.1fr 1.45fr 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.arena-wrap {
  position: relative;
  width: 100%;
  max-width: min(100%, calc(100dvh - 210px));
  margin: 0 auto;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cell);
  touch-action: none;
  user-select: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  text-align: center;
  background: rgba(16, 20, 31, 0.68);
  transition: opacity 140ms ease;
}

.overlay.hidden {
  pointer-events: none;
  opacity: 0;
}

.overlay strong {
  font-size: clamp(1.5rem, 4vw, 3rem);
}

.overlay span {
  color: var(--muted);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.4vw, 16px);
  padding: clamp(10px, 1.5vw, 16px);
  align-self: center;
}

.mode-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.difficulty-group {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.mode.active,
.difficulty.active {
  border-color: var(--green);
  color: #07130d;
  background: var(--green);
  font-weight: 800;
}

.control-row {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 0.78rem;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--green);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  min-width: 142px;
}

.board-controls button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 0.86rem;
}

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

.keys strong {
  display: block;
  margin-bottom: 4px;
}

.touch-controls {
  display: none;
}

.touch-controls,
.panel-pad {
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(2, 48px);
  justify-content: center;
  gap: 8px;
}

.playfield > .stat-grid {
  margin-top: 8px;
}

.touch-controls {
  margin-top: 14px;
}

.panel-pad {
  display: grid;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(16, 20, 31, 0.72);
}

.dir {
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  touch-action: manipulation;
  user-select: none;
}

.dir.up {
  grid-column: 2;
}

.dir.left {
  grid-column: 1;
  grid-row: 2;
}

.dir.down {
  grid-column: 2;
  grid-row: 2;
}

.dir.right {
  grid-column: 3;
  grid-row: 2;
}

.record-popup {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(8, 11, 17, 0.68);
}

.record-popup.hidden {
  display: none;
}

.record-card {
  display: grid;
  gap: 12px;
  width: min(360px, 100%);
  padding: 20px;
  border: 1px solid rgba(255, 209, 102, 0.72);
  border-radius: 8px;
  color: var(--text);
  text-align: center;
  background: #181e2b;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.46);
}

.record-card strong {
  font-size: 1.55rem;
}

.record-card span {
  color: var(--muted);
  line-height: 1.45;
}

.record-card button {
  border-color: var(--yellow);
  color: #231b02;
  background: var(--yellow);
  font-weight: 800;
}

@media (max-width: 900px) {
  .shell {
    grid-template-columns: 1fr;
    align-items: start;
    width: min(760px, calc(100vw - 12px));
  }

  .board-controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .touch-controls {
    display: grid;
  }

  .arena-wrap {
    max-width: min(100%, calc(100dvh - 260px));
  }
}

@media (max-width: 560px) {
  .shell {
    width: calc(100vw - 12px);
    padding: 6px 0;
    gap: 8px;
  }

  .playfield,
  .panel {
    padding: 10px;
  }

  .playfield {
    padding: 8px;
  }

  .scoreboard,
  .stat-grid {
    gap: 6px;
  }

  .scoreboard {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .board-start {
    grid-column: auto;
  }

  .scoreboard div,
  .stat-grid div,
  .keys div {
    padding: 7px 8px;
  }

  .scoreboard strong,
  .stat-grid strong {
    font-size: 0.92rem;
  }

  .board-controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mode-group,
  .difficulty-group {
    grid-column: 1 / -1;
  }

  .difficulty-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .board-controls button {
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.82rem;
  }

  .arena-wrap {
    max-width: min(100%, calc(100dvh - 330px));
  }

  .touch-controls,
  .panel-pad {
    grid-template-columns: repeat(3, 52px);
    grid-template-rows: repeat(2, 44px);
  }

  .keys {
    display: none;
  }
}

@media (orientation: landscape) and (max-height: 820px) and (min-width: 700px) {
  .shell {
    grid-template-columns: minmax(0, 820px);
    align-items: center;
  }

  .arena-wrap {
    max-width: min(100%, calc(100dvh - 185px));
  }

  .scoreboard div,
  .stat-grid div,
  .keys div {
    padding: 8px;
  }
}

@media (max-height: 760px) {
  h1 {
    font-size: clamp(1.2rem, 2.4vw, 1.7rem);
  }

  .topbar {
    margin-bottom: 8px;
  }

  .touch-controls {
    margin-top: 8px;
  }
}
