:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #070914;
  --panel: rgba(18, 22, 42, 0.78);
  --line: rgba(255, 255, 255, 0.1);
  --text: #f4f6ff;
  --muted: #838ba8;
  --cyan: #52f7ff;
  --purple: #aa7cff;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 15% 10%, rgba(91, 73, 255, 0.2), transparent 30rem),
    radial-gradient(circle at 85% 90%, rgba(0, 226, 255, 0.14), transparent 32rem),
    var(--bg);
  color: var(--text);
}

body {
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

button {
  border: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.app {
  width: min(100%, 720px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--cyan);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.22em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.7rem, 6vw, 2.5rem);
  letter-spacing: -0.05em;
}

.icon-button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--cyan);
  font-size: 1.2rem;
}

.icon-button.muted {
  color: var(--muted);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 300px) minmax(120px, 170px);
  gap: 16px;
  justify-content: center;
}

.board-shell {
  position: relative;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 1 / 2;
  border: 1px solid rgba(82, 247, 255, 0.22);
  border-radius: 20px;
  background: #090c1c;
  box-shadow:
    0 24px 80px rgba(0, 0, 0, 0.45),
    0 0 35px rgba(82, 247, 255, 0.08);
  touch-action: none;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(5, 7, 18, 0.76);
  backdrop-filter: blur(7px);
  transition: opacity 180ms ease;
}

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

.overlay-card {
  width: 100%;
  padding: 26px 18px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  background: rgba(17, 21, 41, 0.88);
  text-align: center;
}

.overlay-card h2 {
  margin-bottom: 8px;
  font-size: 1.65rem;
}

.overlay-card > p:not(.eyebrow) {
  margin-bottom: 22px;
  color: var(--muted);
  font-size: 0.9rem;
}

.primary-button,
.secondary-button {
  width: 100%;
  border-radius: 13px;
  font-weight: 800;
}

.primary-button {
  padding: 13px 18px;
  background: linear-gradient(135deg, var(--cyan), #8f8cff);
  color: #071018;
  box-shadow: 0 8px 30px rgba(82, 247, 255, 0.2);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stat-card {
  min-width: 0;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: var(--panel);
  backdrop-filter: blur(14px);
}

.stat-card span {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.stat-card strong {
  font-size: clamp(1.2rem, 5vw, 1.8rem);
}

.score-card strong {
  color: var(--cyan);
}

.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.next-card {
  display: grid;
  place-items: center;
  min-height: 150px;
}

.next-card span {
  justify-self: start;
}

#nextCanvas {
  width: 100%;
  max-width: 110px;
  height: auto;
}

.secondary-button {
  margin-top: auto;
  padding: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
}

.secondary-button:disabled {
  cursor: default;
  opacity: 0.42;
}

.touch-controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr) 1.4fr;
  gap: 9px;
  margin-top: 16px;
}

.touch-controls button {
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: rgba(22, 27, 49, 0.9);
  font-size: 1.35rem;
  font-weight: 800;
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.05);
}

.touch-controls button:active {
  transform: translateY(2px);
  background: rgba(82, 247, 255, 0.16);
}

.touch-controls .drop-button {
  background: linear-gradient(135deg, rgba(82, 247, 255, 0.2), rgba(170, 124, 255, 0.2));
  color: var(--cyan);
  font-size: 0.74rem;
  letter-spacing: 0.08em;
}

.gesture-help {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.7rem;
  text-align: center;
}

.gesture-help div {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.gesture-help i {
  color: var(--purple);
  font-size: 1rem;
  font-style: normal;
}

@media (max-width: 520px) {
  body {
    display: block;
  }

  .app {
    margin: 0 auto;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr) 104px;
    gap: 10px;
  }

  .sidebar {
    gap: 8px;
  }

  .stat-card {
    padding: 11px;
    border-radius: 14px;
  }

  .stats-row {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .next-card {
    min-height: 110px;
  }

  .touch-controls {
    position: sticky;
    bottom: max(8px, env(safe-area-inset-bottom));
    z-index: 5;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: rgba(7, 9, 20, 0.82);
    backdrop-filter: blur(18px);
  }

  .touch-controls button {
    min-height: 50px;
  }

  .gesture-help {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-height: 740px) and (max-width: 520px) {
  .topbar {
    margin-bottom: 10px;
  }

  h1 {
    font-size: 1.45rem;
  }

  .board-shell {
    max-height: 54vh;
    width: auto;
    margin-left: auto;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr) 100px;
  }

  .gesture-help {
    display: none;
  }
}
