/* Тетрис — адаптивная тёмная тема */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0b0b12;
  --panel: #16161f;
  --accent: #7c5cff;
  --text: #e8e8f0;
  --muted: #9a9ab0;
  --line: #2a2a3a;
}

html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 900px;
  margin: 0 auto;
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
  gap: 8px;
}

.game-area {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 12px;
}

.board-wrap {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-inner {
  position: relative;
  line-height: 0;
}

#board {
  border-radius: 8px;
  box-shadow: 0 0 0 2px var(--line), 0 10px 40px rgba(0, 0, 0, 0.6);
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(10, 10, 18, 0.85);
  border-radius: 8px;
  text-align: center;
  padding: 16px;
  line-height: 1.4;
}

.overlay[hidden] {
  display: none;
}

.overlay h1 {
  font-size: clamp(20px, 5vw, 40px);
  letter-spacing: 0.08em;
  max-width: 100%;
}

.overlay p {
  color: var(--muted);
  max-width: 100%;
  font-size: 14px;
  padding: 0 8px;
}

#overlay-btn {
  font: inherit;
  font-size: 18px;
  font-weight: 600;
  max-width: calc(100% - 12px);
  padding: 10px 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  touch-action: manipulation;
}

#overlay-btn:active {
  transform: scale(0.96);
}

.panel {
  width: 170px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}

.stat,
.next-box {
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.label {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.next-box canvas {
  align-self: center;
  margin-top: 4px;
}

.help {
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--muted);
}

.help summary {
  cursor: pointer;
  color: var(--text);
}

.help ul {
  margin: 8px 0 8px 18px;
}

.help p {
  margin-top: 8px;
}

.touch-controls {
  display: none;
  gap: 8px;
  touch-action: none;
  grid-template-columns: 1.25fr 1.25fr 1fr 1fr;
  grid-template-areas:
    "left right rotate drop"
    "left right soft pause";
}

.touch-controls button {
  font: inherit;
  color: var(--text);
  border: 1px solid #2e2e40;
  border-radius: 16px;
  background: #1a1a26;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.35);
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 62px;
  padding: 6px 4px;
}

.touch-controls .ico {
  font-size: 26px;
  line-height: 1;
}

.touch-controls .cap {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.02em;
  color: var(--muted);
}

.touch-controls button:active {
  background: var(--accent);
  border-color: transparent;
  transform: translateY(1px);
  box-shadow: none;
}

.touch-controls button:active .cap {
  color: #fff;
}

#btn-left { grid-area: left; }
#btn-right { grid-area: right; }
#btn-rotate { grid-area: rotate; background: #2a2152; }
#btn-drop { grid-area: drop; background: #34286a; }
#btn-down { grid-area: soft; }
#btn-pause { grid-area: pause; background: #14141c; }

#btn-rotate .cap,
#btn-drop .cap {
  color: #d9d0ff;
}

@media (pointer: coarse), (hover: none), (max-width: 700px) {
  .touch-controls {
    display: grid;
  }

  .help {
    display: none;
  }
}

@media (max-width: 700px) {
  .app {
    gap: 6px;
  }

  .game-area {
    flex-direction: column;
    gap: 6px;
  }

  .panel {
    order: -1;
    width: 100%;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 6px;
    overflow: hidden;
  }

  .stat {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 8px;
    gap: 1px;
    justify-content: center;
  }

  .label {
    font-size: 10px;
    letter-spacing: 0.04em;
    white-space: nowrap;
  }

  .value {
    font-size: 16px;
  }

  .next-box {
    flex: 0 0 auto;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 0;
    padding: 4px 8px;
  }

  .next-box {
    flex-direction: column;
    gap: 2px;
  }

  .next-box .label {
    display: block;
    font-size: 9px;
    letter-spacing: 0.03em;
  }

  .next-box canvas {
    margin-top: 0;
  }

  .help {
    display: none;
  }
}

/* Телефон горизонтально: поле на всю высоту, кнопки сбоку */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    flex-direction: row;
    align-items: stretch;
    justify-content: flex-start;
    gap: 8px;
  }

  .game-area {
    flex: 1 1 auto;
    flex-direction: row;
    min-width: 0;
    min-height: 0;
  }

  .board-wrap {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    justify-content: center;
  }

  .help {
    display: none;
  }

  .panel {
    order: 0;
    width: 108px;
    flex: 0 0 108px;
    flex-direction: column;
    flex-wrap: nowrap;
    overflow: hidden;
    gap: 6px;
  }

  .stat {
    flex: 1 1 0;
    min-height: 0;
    padding: 4px 8px;
  }

  .value {
    font-size: 15px;
  }

  .next-box {
    flex: 0 0 auto;
    justify-content: center;
  }

  .next-box .label {
    font-size: 9px;
  }

  .touch-controls {
    width: min(42vw, 230px);
    flex: 0 0 auto;
    align-self: stretch;
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "left rotate"
      "right drop"
      "soft pause";
  }

  .touch-controls button {
    min-height: 0;
  }

  .overlay h1 {
    font-size: 18px;
    letter-spacing: 0.04em;
  }

  .overlay p {
    font-size: 12px;
  }

  #overlay-btn {
    font-size: 14px;
    padding: 8px 12px;
  }
}
