:root {
  --c0: #4f7cff;
  --c1: #7c4fff;
  --c2: #ff4f7c;
  --c3: #4fffb0;
  --c4: #ffd24f;
  --bg: linear-gradient(135deg, #1e1e2f, #2a2a40);
  --panel: rgba(255, 255, 255, 0.06);
  --text: #f2f2f7;
  --muted: #a0a0b0;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

#toast {
  position: fixed;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  width: 100%;
  align-items: center;
}

.toast-item {
  background: rgba(20, 20, 30, 0.92);
  border: 1px solid rgba(255, 210, 79, 0.5);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  animation: fadein 0.2s ease-out;
}

@keyframes fadein {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

#topbar {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  font-weight: 600;
  font-size: 15px;
}

main#tabs { padding: 0 12px 80px; min-height: calc(100vh - 46px); }

.tab { display: none; }
.tab.active { display: block; }

#game-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; position: relative; touch-action: none; }

#score {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: 0.5px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

#score.pop { animation: scorePop 0.28s ease-out; }

@keyframes scorePop {
  0% { transform: scale(1); }
  40% { transform: scale(1.28); color: var(--c4); }
  100% { transform: scale(1); }
}

#board-area { position: relative; width: min(94vw, 420px); overflow: visible; }

#grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 4px;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: linear-gradient(160deg, #26263a, #14141e);
  border-radius: 14px;
  padding: 8px;
  box-shadow:
    inset 0 2px 6px rgba(0, 0, 0, 0.5),
    inset 0 -1px 0 rgba(255, 255, 255, 0.04),
    0 10px 30px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.cell {
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.25);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
  transition: box-shadow 0.15s;
}

.cell.filled {
  background: linear-gradient(
    155deg,
    color-mix(in srgb, var(--cell-color) 100%, white 35%),
    var(--cell-color) 45%,
    color-mix(in srgb, var(--cell-color) 100%, black 22%)
  );
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.45),
    inset 0 -3px 5px rgba(0, 0, 0, 0.35),
    0 2px 4px rgba(0, 0, 0, 0.3);
  animation: placeIn 0.14s ease-out;
}

.cell.clearing {
  animation: clearFlash 0.32s ease-out forwards;
}

@keyframes placeIn {
  from { transform: scale(0.55); }
  to { transform: scale(1); }
}

@keyframes clearFlash {
  0% { filter: brightness(1); transform: scale(1); }
  40% { filter: brightness(2.4); transform: scale(1.08); }
  100% { filter: brightness(2.4); transform: scale(0.2); opacity: 0; }
}

.cell.preview-ok { outline: 2px solid rgba(255, 255, 255, 0.6); outline-offset: -2px; }
.cell.preview-bad { outline: 2px solid rgba(255, 70, 70, 0.8); outline-offset: -2px; }

.cell.preview-clear {
  background: rgba(255, 210, 79, 0.22);
  animation: previewPulse 0.6s ease-in-out infinite;
}

@keyframes previewPulse {
  0%, 100% { box-shadow: inset 0 0 0 2px rgba(255, 210, 79, 0.9); }
  50% { box-shadow: inset 0 0 0 2px rgba(255, 210, 79, 0.35); }
}

.float-score {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, 0);
  font-size: 26px;
  font-weight: 800;
  color: var(--c4);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  animation: floatUp 0.8s ease-out forwards;
  z-index: 20;
}

@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, 10px) scale(0.8); }
  25% { opacity: 1; transform: translate(-50%, -6px) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -46px) scale(1); }
}

.float-score.perfect {
  font-size: 30px;
  white-space: nowrap;
  color: #fff;
  text-shadow: 0 0 16px var(--c4), 0 2px 8px rgba(0, 0, 0, 0.6);
  animation: floatUp 1.2s ease-out forwards;
}

#tray {
  display: flex;
  gap: 10px;
  width: min(94vw, 420px);
  justify-content: space-around;
  min-height: 96px;
  padding-top: 6px;
}

.piece {
  display: grid;
  gap: 3px;
  touch-action: none;
  cursor: grab;
  padding: 6px;
  border-radius: 10px;
  transition: opacity 0.15s;
}

.piece.dragging { opacity: 0.25; }

.piece-cell {
  width: 19px;
  height: 19px;
  border-radius: 5px;
  background: transparent;
}

.piece-cell:not(.empty) {
  background: linear-gradient(
    155deg,
    color-mix(in srgb, var(--cell-color) 100%, white 35%),
    var(--cell-color) 45%,
    color-mix(in srgb, var(--cell-color) 100%, black 22%)
  );
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.45),
    inset 0 -2px 4px rgba(0, 0, 0, 0.3),
    0 2px 3px rgba(0, 0, 0, 0.25);
}

#drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 50;
  display: grid;
  gap: 3px;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.5));
  transform: scale(1.15);
}

#gameover {
  position: absolute;
  inset: -12px;
  background: rgba(8, 8, 14, 0.88);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
}

#gameover.hidden { display: none; }

.panel {
  background: linear-gradient(160deg, #2a2a40, #1a1a28);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
  animation: panelIn 0.25s ease-out;
}

@keyframes panelIn {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.panel h2 { margin: 0 0 6px; font-size: 22px; }
.panel p { font-size: 17px; color: var(--muted); }
.panel p span { color: var(--c4); font-weight: 800; font-size: 20px; }

.panel button {
  margin-top: 14px;
  padding: 12px 28px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(160deg, var(--c0), color-mix(in srgb, var(--c0) 100%, black 20%));
  color: white;
  font-size: 16px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

#navbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: rgba(15, 15, 22, 0.95);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

#navbar button {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  padding: 10px 0;
  font-size: 12px;
}

#navbar button.active { color: var(--text); font-weight: 700; }

.shop-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 16px; }

.shop-item {
  background: var(--panel);
  border-radius: 12px;
  padding: 10px;
  text-align: center;
}

.shop-swatch { height: 44px; border-radius: 8px; margin-bottom: 8px; }
.shop-item button { width: 100%; padding: 6px; border-radius: 8px; border: none; font-weight: 600; }
.shop-item button.owned { background: #33aa66; color: white; }
.shop-item button.equipped { background: #555; color: white; }
.shop-item button.buy { background: var(--c4); color: #222; }

#buy-coins-row { display: flex; gap: 10px; margin-bottom: 16px; overflow-x: auto; }

.coin-pack {
  background: var(--panel);
  border-radius: 12px;
  padding: 12px;
  min-width: 110px;
  text-align: center;
  flex-shrink: 0;
}

.coin-pack button {
  margin-top: 8px;
  padding: 6px 10px;
  border: none;
  border-radius: 8px;
  background: #ffd24f;
  color: #222;
  font-weight: 700;
}

.lb-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 10px;
  background: var(--panel);
  border-radius: 10px;
  margin-bottom: 6px;
  font-size: 14px;
}

.lb-row.me { border: 1px solid var(--c4); }
.lb-rank { width: 28px; color: var(--muted); }

#profile-info { background: var(--panel); border-radius: 12px; padding: 14px; margin-bottom: 10px; }

.quest-row {
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.quest-row.done { opacity: 0.5; }
