:root {
  --bg: #070a12;
  --bg-2: #101827;
  --panel: rgba(12, 18, 32, 0.88);
  --panel-strong: rgba(19, 28, 48, 0.96);
  --line: rgba(148, 163, 184, 0.22);
  --text: #eef6ff;
  --muted: #93a4bb;
  --accent: #31e7ff;
  --accent-2: #ff4fd8;
  --accent-3: #a7ff55;
  --danger: #ff6a6a;
  --board-bg: #050711;
  --grid: rgba(124, 142, 180, 0.16);
  --ghost: rgba(221, 244, 255, 0.22);
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.42);
  --radius: 8px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --piece-i: #31e7ff;
  --piece-o: #ffd84d;
  --piece-t: #c77dff;
  --piece-s: #70ff87;
  --piece-z: #ff5f7a;
  --piece-j: #5e8cff;
  --piece-l: #ffad45;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  font-family: var(--font);
  background:
    linear-gradient(135deg, rgba(49, 231, 255, 0.09), transparent 34%),
    linear-gradient(225deg, rgba(255, 79, 216, 0.08), transparent 38%),
    var(--bg);
  overflow-x: hidden;
}

button,
summary {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(16px, 3vw, 32px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

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

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

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 5vw, 4.5rem);
  line-height: 0.9;
  letter-spacing: 0;
  text-shadow: 0 0 24px rgba(49, 231, 255, 0.35);
}

h2 {
  margin-bottom: 10px;
  font-size: 1rem;
  letter-spacing: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.primary-button,
.icon-button,
.control-button {
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--panel-strong);
  cursor: pointer;
  transition: transform 150ms ease, border-color 150ms ease, background 150ms ease, box-shadow 150ms ease;
}

.primary-button {
  min-height: 42px;
  padding: 0 18px;
  border-radius: var(--radius);
  font-weight: 800;
  background: linear-gradient(135deg, rgba(49, 231, 255, 0.95), rgba(255, 79, 216, 0.88));
  box-shadow: 0 12px 28px rgba(49, 231, 255, 0.18);
}

.primary-button.large {
  min-height: 50px;
  padding-inline: 26px;
}

.icon-button {
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  font-weight: 900;
}

.primary-button:hover,
.icon-button:hover,
.control-button:hover {
  transform: translateY(-1px);
  border-color: rgba(49, 231, 255, 0.55);
}

.primary-button:active,
.icon-button:active,
.control-button:active {
  transform: translateY(1px) scale(0.99);
}

.game-layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(180px, 0.78fr) minmax(260px, 420px) minmax(180px, 0.78fr);
  gap: clamp(12px, 2vw, 22px);
  align-items: start;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.stat-card,
.preview-card,
.help-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.stat-card {
  min-height: 82px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.stat-label,
.tiny-hint {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stat-value {
  font-size: clamp(1.25rem, 2vw, 1.8rem);
  line-height: 1;
  letter-spacing: 0;
}

.preview-card,
.help-card {
  padding: 14px;
}

.panel-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.panel-title-row h2 {
  margin: 0;
}

.preview-canvas,
.queue-canvas {
  width: 100%;
  display: block;
  border: 1px solid rgba(148, 163, 184, 0.14);
  border-radius: var(--radius);
  background: rgba(4, 7, 16, 0.76);
}

.preview-canvas {
  aspect-ratio: 1;
}

.queue-canvas {
  aspect-ratio: 7 / 18;
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.board-frame {
  position: relative;
  width: min(100%, 420px, calc((100vh - 176px) / 2));
  min-width: 260px;
  aspect-ratio: 1 / 2;
  border: 1px solid rgba(49, 231, 255, 0.34);
  border-radius: var(--radius);
  background: var(--board-bg);
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.04) inset,
    0 0 38px rgba(49, 231, 255, 0.16),
    var(--shadow);
  overflow: hidden;
  touch-action: none;
}

.board-frame.is-shaking {
  animation: board-shake 170ms ease;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--board-bg);
  touch-action: none;
}

.board-touch-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: 36% 28% 36%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board-touch-zone {
  display: block;
  min-width: 0;
  min-height: 0;
  touch-action: none;
}

.screen {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 22px;
  background: rgba(5, 7, 17, 0.72);
  backdrop-filter: blur(6px);
}

.screen.is-active {
  display: flex;
}

.screen-panel {
  width: min(280px, 100%);
  padding: 20px;
  border: 1px solid rgba(49, 231, 255, 0.32);
  border-radius: var(--radius);
  background: rgba(8, 13, 27, 0.92);
  text-align: center;
  box-shadow: 0 20px 46px rgba(0, 0, 0, 0.42);
}

.screen-panel h2 {
  margin-bottom: 8px;
  font-size: clamp(1.7rem, 6vw, 3rem);
  line-height: 0.95;
}

.screen-panel p:not(.eyebrow) {
  margin-bottom: 16px;
  color: var(--muted);
}

.touch-controls {
  width: min(100%, 460px);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  touch-action: none;
  user-select: none;
}

.control-button {
  min-height: 54px;
  padding: 0 8px;
  border-radius: var(--radius);
  color: var(--text);
  background: rgba(16, 24, 42, 0.96);
  font-size: clamp(0.82rem, 3.2vw, 0.95rem);
  font-weight: 900;
  letter-spacing: 0;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.24);
  touch-action: manipulation;
}

.control-button.accent {
  background: linear-gradient(135deg, rgba(49, 231, 255, 0.9), rgba(167, 255, 85, 0.74));
  color: #041016;
}

.control-button[data-action="left"],
.control-button[data-action="down"],
.control-button[data-action="right"] {
  min-height: 62px;
}

.help-card summary {
  cursor: pointer;
  color: var(--text);
  font-weight: 900;
  list-style-position: outside;
}

.help-copy {
  padding-top: 12px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.help-copy p {
  margin-bottom: 10px;
}

.help-copy p:last-child {
  margin-bottom: 0;
}

.credit {
  color: var(--muted);
  font-size: 0.88rem;
  text-align: center;
}

@keyframes board-shake {
  0%,
  100% {
    transform: translate3d(0, 0, 0);
  }
  25% {
    transform: translate3d(-5px, 2px, 0);
  }
  50% {
    transform: translate3d(4px, -2px, 0);
  }
  75% {
    transform: translate3d(-2px, 1px, 0);
  }
}

@media (max-width: 920px) {
  .app-shell {
    padding-inline: 14px;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .board-column {
    order: 1;
  }

  .left-panel {
    order: 2;
  }

  .right-panel {
    order: 3;
  }

  .panel {
    width: min(100%, 460px);
    margin-inline: auto;
  }

  .board-frame {
    width: min(92vw, 390px, calc((100vh - 250px) / 2));
  }
}

@media (max-width: 560px) {
  .app-shell {
    gap: 12px;
  }

  .app-header {
    align-items: flex-start;
  }

  .header-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .primary-button {
    padding-inline: 13px;
  }

  .board-frame {
    width: min(94vw, calc((100vh - 260px) / 2));
    min-width: 238px;
  }

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

  .control-button {
    min-height: 58px;
  }

  .control-button.accent {
    grid-column: span 3;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    scroll-behavior: auto !important;
    transition-duration: 1ms !important;
  }
}
