:root {
  --bg-color: var(--tg-theme-bg-color, #07162a);
  --bg-gradient-start: var(--tg-theme-secondary-bg-color, #0a1f38);
  --bg-gradient-end: #061322;
  --surface-color: rgba(255, 255, 255, 0.04);
  --surface-border: rgba(255, 255, 255, 0.08);
  --text-color: var(--tg-theme-text-color, #e9f2ff);
  --muted-text-color: rgba(233, 242, 255, 0.72);
  --grid-color: #163252;
  --grid-cell-glow: rgba(138, 200, 255, 0.12);
  --spawn-bg: rgba(255, 255, 255, 0.03);
  --spawn-border: rgba(255, 255, 255, 0.09);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text-color);
  background:
    radial-gradient(circle at top, rgba(91, 153, 242, 0.15), transparent 42%),
    linear-gradient(180deg, var(--bg-gradient-start) 0%, var(--bg-gradient-end) 100%);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.app {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: clamp(12px, 2.2vh, 18px);
  padding:
    max(12px, var(--safe-top))
    max(14px, var(--safe-right))
    max(14px, var(--safe-bottom))
    max(14px, var(--safe-left));
}

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
}

.title {
  text-align: center;
  font-size: clamp(15px, 2.6vw, 19px);
  font-weight: 700;
  letter-spacing: 0.3px;
  color: var(--text-color);
  opacity: 0.95;
}

.user-greeting {
  text-align: center;
  color: var(--muted-text-color);
  font-size: 14px;
  margin-top: -4px;
}

.hud-card {
  min-width: 78px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--surface-border);
  background: var(--surface-color);
  backdrop-filter: blur(6px);
}

.hud-card:first-child {
  justify-self: start;
}

.hud-card:last-child {
  justify-self: end;
}

.hud-label {
  display: block;
  font-size: 11px;
  color: var(--muted-text-color);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.hud-value {
  font-size: 22px;
  line-height: 1;
  font-weight: 700;
  color: var(--text-color);
}

.board-section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 0;
}

.board-canvas {
  width: min(92vw, calc(100vh - 260px));
  max-width: 520px;
  aspect-ratio: 1 / 1;
  border-radius: 20px;
  border: 1px solid var(--surface-border);
  background: linear-gradient(180deg, rgba(14, 35, 61, 0.95), rgba(10, 24, 42, 0.96));
  box-shadow:
    0 10px 28px rgba(1, 6, 13, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.spawn-zone {
  width: min(92vw, 520px);
  justify-self: center;
  border-radius: 18px;
  border: 1px dashed var(--spawn-border);
  background: var(--spawn-bg);
  padding: clamp(14px, 2.4vh, 20px);
}

.spawn-zone__inner {
  min-height: clamp(84px, 14vh, 116px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0.01));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(8px, 2.6vw, 14px);
  padding: clamp(8px, 2vw, 12px);
}

.spawn-shape-slot {
  flex: 1;
  min-width: 0;
  min-height: clamp(58px, 9vh, 74px);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: visible;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(7, 20, 36, 0.46);
}

.spawn-shape-item {
  --spawn-shape-scale: 0.56;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate3d(-50%, -50%, 0) scale(var(--spawn-shape-scale));
  transform-origin: center center;
  will-change: transform;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  transition: transform 0.2s ease;
}

.spawn-shape-item.is-dragging {
  cursor: grabbing;
  z-index: 9999;
  transition: none !important;
}

.spawn-shape-item.is-returning {
  transition: transform 0.3s ease;
}

.spawn-shape-canvas {
  display: block;
  max-width: 100%;
  height: auto;
  pointer-events: none;
  -webkit-user-drag: none;
}

.game-over-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(4, 10, 20, 0.62);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 10000;
}

.game-over-modal.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.game-over-card {
  width: min(88vw, 340px);
  border-radius: 18px;
  border: 1px solid var(--surface-border);
  background: linear-gradient(180deg, rgba(13, 31, 52, 0.96), rgba(8, 20, 35, 0.98));
  box-shadow: 0 16px 38px rgba(0, 5, 14, 0.5);
  padding: 20px 18px 18px;
  text-align: center;
}

.game-over-title {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 12px;
  letter-spacing: 0.3px;
}

.game-over-stats {
  display: grid;
  gap: 8px;
  margin-bottom: 18px;
}

.game-over-stat {
  font-size: 17px;
  color: var(--muted-text-color);
}

.game-over-stat strong {
  color: var(--text-color);
}

.restart-button {
  width: 100%;
  border: none;
  border-radius: 14px;
  padding: 13px 14px;
  font-size: 17px;
  font-weight: 700;
  color: #032133;
  background: linear-gradient(180deg, #8ad8ff 0%, #79f2cf 100%);
  box-shadow: 0 8px 20px rgba(77, 200, 255, 0.35);
}

@media (max-height: 660px) {
  .app {
    gap: 10px;
    padding-top: max(10px, var(--safe-top));
  }

  .hud-card {
    padding: 8px 10px;
  }

  .hud-value {
    font-size: 19px;
  }

  .spawn-zone__inner {
    min-height: 72px;
  }
}