: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, min(72vh, 760px)) minmax(292px, 320px);
  gap: clamp(12px, 2vw, 22px);
  width: min(1180px, calc(100vw - 28px));
  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 {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: end;
  margin-bottom: 14px;
}

.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;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.scoreboard {
  min-width: min(320px, 100%);
}

.scoreboard div,
.stat-grid div,
.keys div {
  min-width: 96px;
  padding: 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.76rem;
}

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

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

.arena-wrap {
  position: relative;
  width: 100%;
  max-width: min(100%, calc(100dvh - 150px));
  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: 8px;
}

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

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

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

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

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.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;
}

.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 - 18px));
  }

  .panel {
    order: 0;
    display: grid;
    grid-template-columns: 164px minmax(0, 1fr) minmax(0, 1fr);
    align-items: center;
  }

  .panel-pad {
    grid-row: span 2;
  }

  .mode-group {
    grid-column: 2;
  }

  .difficulty-group {
    grid-column: 3;
  }

  .stat-grid {
    grid-column: 2 / 4;
  }

  .control-row {
    grid-column: 1 / 3;
  }

  .actions {
    grid-column: 3;
  }

  .keys {
    grid-column: 1 / 4;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stat-grid,
  .difficulty-group,
  .actions,
  .keys,
  .control-row {
    min-width: 0;
  }

  .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;
  }

  .topbar {
    display: grid;
    align-items: start;
  }

  .panel {
    grid-template-columns: 1fr;
  }

  .panel-pad {
    grid-row: auto;
  }

  .mode-group,
  .difficulty-group,
  .stat-grid,
  .control-row,
  .actions,
  .keys {
    grid-column: auto;
  }

  .scoreboard,
  .stat-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .board-start {
    grid-column: 1 / -1;
  }

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

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

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

  .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, calc(100dvh - 32px)) minmax(280px, 320px);
    align-items: center;
  }

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

  .panel {
    max-height: calc(100dvh - 20px);
    overflow: auto;
  }

  .panel-pad {
    grid-template-columns: repeat(3, 48px);
    grid-template-rows: repeat(2, 40px);
  }

  .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;
  }
}
