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

:root {
  --bg: #1a0a2e;
  --grid-bg: #2d1b4e;
  --grid-line: #3d2b5e;
  --cell-empty: #352358;
  --text-primary: #ffffff;
  --text-gold: #ffd366;
  --accent-magenta: #e91e8c;
  --accent-teal: #00d4aa;
  --accent-purple: #b44aff;
  --accent-pink: #ff6bb5;
  --accent-cyan: #00c8ff;
  --font-display: 'Russo One', sans-serif;
  --font-body: 'Inter', sans-serif;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--font-body);
  color: var(--text-primary);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#game-container {
  width: 100%;
  height: 100%;
  max-width: 540px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 12px env(safe-area-inset-bottom, 8px) 12px;
  position: relative;
}

/* --- Header --- */

#header {
  width: 100%;
  flex-shrink: 0;
  padding: 4px 0 6px;
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-row.sub {
  font-size: 0.8rem;
  margin-top: 2px;
  opacity: 0.85;
}

#logo {
  font-family: var(--font-display);
  font-size: 1.5rem;
  letter-spacing: 3px;
  text-transform: uppercase;
  background: linear-gradient(180deg, #ffffff 0%, #c8b8ff 40%, var(--accent-cyan) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4));
}

.score-display {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1.35rem;
  display: flex;
  align-items: center;
  gap: 6px;
}

#score {
  transition: transform 0.15s ease;
}

#score.pop {
  transform: scale(1.25);
}

.star-icon {
  color: var(--text-gold);
  font-size: 1.1rem;
}

#streak-display {
  color: var(--accent-teal);
  font-weight: 700;
  font-size: 0.82rem;
}

#best-display {
  color: var(--text-gold);
  font-weight: 700;
  font-size: 0.82rem;
}

#mute-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 8px;
  opacity: 0.7;
  transition: opacity 0.2s;
}

#mute-btn:hover { opacity: 1; }

/* --- Board --- */

#board-wrapper {
  position: relative;
  width: 100%;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#game-canvas {
  display: block;
  border-radius: 10px;
  box-shadow:
    0 0 30px rgba(180, 74, 255, 0.15),
    0 0 60px rgba(0, 200, 255, 0.06),
    inset 0 1px 0 rgba(255,255,255,0.04);
}

#particle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* --- Tray --- */

#tray {
  width: 100%;
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 12px;
  padding: 10px 8px;
  min-height: 60px;
  background: linear-gradient(180deg, transparent 0%, rgba(45, 27, 78, 0.4) 100%);
  border-radius: 12px 12px 0 0;
}

.tray-piece {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: grab;
  transition: transform 0.15s ease, opacity 0.15s ease;
  touch-action: none;
  animation: trayEnter 0.25s ease backwards;
}

.tray-piece:nth-child(1) { animation-delay: 0.0s; }
.tray-piece:nth-child(2) { animation-delay: 0.08s; }
.tray-piece:nth-child(3) { animation-delay: 0.16s; }

@keyframes trayEnter {
  from { opacity: 0; transform: scale(0.6) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.tray-piece:active { cursor: grabbing; }

.tray-piece.placed {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
}

.tray-piece canvas {
  pointer-events: none;
}

/* --- Overlays --- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 4, 20, 0.92);
  z-index: 100;
  transition: opacity 0.3s ease;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

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

.overlay-content {
  text-align: center;
  padding: 32px 24px;
}

.splash-star {
  font-family: 'Segoe UI Symbol', 'Noto Sans Symbols', 'Apple Symbols', sans-serif;
  font-size: 3rem;
  color: var(--text-gold);
  margin-bottom: 16px;
  animation: spinStar 8s linear infinite;
  filter: drop-shadow(0 0 14px rgba(255, 211, 102, 0.5));
  line-height: 1;
}

@keyframes spinStar {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.splash-title {
  font-family: var(--font-display);
  font-size: 2.8rem;
  letter-spacing: 5px;
  text-transform: uppercase;
  margin-bottom: 6px;
  position: relative;
  background: linear-gradient(
    180deg,
    #ffffff 0%,
    #f0e0ff 25%,
    var(--accent-pink) 50%,
    var(--accent-purple) 75%,
    var(--accent-cyan) 100%
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  filter: drop-shadow(0 2px 8px rgba(180, 74, 255, 0.5));
}

.splash-subtitle {
  font-family: var(--font-body);
  font-weight: 500;
  font-size: 1rem;
  color: var(--text-gold);
  margin-bottom: 36px;
  letter-spacing: 2.5px;
  text-transform: uppercase;
  opacity: 0.9;
}

.gameover-title {
  font-family: var(--font-display);
  font-size: 2rem;
  text-transform: uppercase;
  letter-spacing: 3px;
  margin-bottom: 14px;
  background: linear-gradient(180deg, #ffffff 0%, var(--accent-pink) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  filter: drop-shadow(0 2px 6px rgba(255, 107, 181, 0.4));
}

.gameover-score {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1.5rem;
  margin-bottom: 6px;
}

.gameover-best {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text-gold);
  margin-bottom: 16px;
  animation: pulse 0.8s ease infinite alternate;
}

@keyframes pulse {
  from { opacity: 0.7; transform: scale(1); }
  to { opacity: 1; transform: scale(1.08); }
}

#high-score-list {
  margin: 16px auto;
  max-width: 260px;
  text-align: left;
  font-size: 0.85rem;
  font-weight: 500;
}

#high-score-list .hs-entry {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

#high-score-list .hs-entry.current {
  color: var(--text-gold);
  font-weight: 700;
}

.btn-primary {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1.05rem;
  padding: 14px 44px;
  border: none;
  border-radius: 50px;
  color: white;
  cursor: pointer;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  position: relative;
  background: linear-gradient(180deg, #d946a8 0%, var(--accent-purple) 100%);
  box-shadow:
    0 4px 20px rgba(180, 74, 255, 0.4),
    inset 0 1px 0 rgba(255,255,255,0.25);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.btn-primary:hover {
  transform: scale(1.04);
  box-shadow:
    0 6px 28px rgba(180, 74, 255, 0.55),
    inset 0 1px 0 rgba(255,255,255,0.3);
}

.btn-primary:active {
  transform: scale(0.97);
}

/* --- Drag ghost --- */

#drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 200;
  transition: none;
}

/* --- Screen shake --- */

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px) translateY(2px); }
  40% { transform: translateX(3px) translateY(-2px); }
  60% { transform: translateX(-2px) translateY(1px); }
  80% { transform: translateX(2px) translateY(-1px); }
}

#game-container.shake {
  animation: shake 0.3s ease;
}

/* --- Landscape for narrow-height screens --- */

@media (orientation: landscape) and (max-height: 500px) {
  #game-container {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 4px 8px;
    max-width: 100%;
  }
  #header { order: 0; width: 100%; padding: 2px 0; }
  #board-wrapper { order: 1; flex: 1 1 auto; max-height: calc(100vh - 40px); }
  #tray {
    order: 2;
    flex-direction: column;
    width: auto;
    min-height: unset;
    padding: 4px;
    gap: 6px;
  }
}
