* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }

body[data-theme="cosmos"] {
  --bg1: #3a2d5b; --bg2: #2b2140; --bg3: #1c1530;
  --ink: #fff; --line: rgba(255,255,255,.55);
  --glass: rgba(255,255,255,.045); --btnbg: rgba(255,255,255,.12); --btnline: rgba(255,255,255,.25);
  --bubble: rgba(255,255,255,.06);
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at 50% 18%, var(--bg1) 0%, var(--bg2) 55%, var(--bg3) 100%);
  color: var(--ink);
  touch-action: none;
  user-select: none;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Пузырьки на фоне для глубины */
#bubbles { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
#bubbles span {
  position: absolute; bottom: -12vmin;
  border-radius: 50%;
  background: radial-gradient(circle at 32% 30%, var(--bubble), transparent 70%);
  animation: rise linear infinite;
}
@keyframes rise { to { transform: translateY(-125vh); } }

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 12px 16px 6px;
  position: relative; z-index: 2;
}
#coinbox {
  font-size: 17px; font-weight: 700;
  background: var(--btnbg);
  border: 2px solid var(--btnline);
  border-radius: 999px;
  padding: 5px 14px;
}
.left { display: flex; align-items: center; gap: 10px; }
a.home {
  display: inline-flex; align-items: center; justify-content: center;
  width: 50px; height: 50px;
  font-size: 22px;
  color: var(--ink); text-decoration: none;
  background: var(--btnbg);
  border: 2px solid var(--btnline);
  border-radius: 14px;
  box-shadow: 0 4px 10px rgba(0,0,0,.18);
}
a.home:active { transform: translateY(2px); }
.scores { text-align: center; }
#score {
  font-size: clamp(30px, 7vmin, 44px);
  font-weight: 800;
  line-height: 1.05;
  text-shadow: 0 3px 10px rgba(0,0,0,.4);
}
#best { font-size: 14px; color: rgba(255,255,255,.65); font-weight: 700; }
.btns { display: flex; gap: 8px; }
.btns button, footer button {
  position: relative;
  width: 50px; height: 50px;
  font-size: 22px;
  color: var(--ink);
  background: var(--btnbg);
  border: 2px solid var(--btnline);
  border-radius: 14px;
  box-shadow: 0 4px 10px rgba(0,0,0,.18);
}
.btns button:active, footer button:active { transform: translateY(2px); }
footer button sup {
  position: absolute; right: -6px; top: -8px;
  font-size: 10px; font-weight: 700;
  background: #ffb300; color: #4b3200;
  border-radius: 999px;
  padding: 2px 5px;
  box-shadow: 0 2px 5px rgba(0,0,0,.3);
}
footer button.armed { border-color: #ffd54f; box-shadow: 0 0 14px 3px rgba(255,213,79,.6); }

/* Плашка комбо */
#combo {
  min-height: 26px;
  text-align: center;
  font-size: 18px; font-weight: 800;
  color: #ffd54f;
  text-shadow: 0 2px 6px rgba(0,0,0,.5);
  position: relative; z-index: 2;
  opacity: 0;
  transition: opacity .25s;
}
#combo.show { opacity: 1; animation: comboPop .45s cubic-bezier(.3,1.6,.5,1); }
@keyframes comboPop { from { transform: scale(.4); } to { transform: scale(1); } }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  position: relative; z-index: 1;
  padding: 4px 8px;
}

/* ---- Поле 8×8 ---- */
#grid {
  --cell: min(10.4vw, 5.9vh, 52px);
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  grid-template-rows: repeat(8, var(--cell));
  gap: 4px;
  padding: 8px;
  border-radius: 18px;
  background: rgba(0,0,0,.22);
  border: 2px solid rgba(255,255,255,.14);
  box-shadow: inset 0 4px 14px rgba(0,0,0,.35), 0 14px 24px -10px rgba(0,0,0,.5);
}
.cell {
  border-radius: 22%;
  background: var(--glass);
  box-shadow: inset 0 1px 2px rgba(255,255,255,.07);
  position: relative;
}
/* Призрак фигуры под пальцем */
.cell.ghost { background: rgba(255,255,255,.22); box-shadow: inset 0 0 0 2px rgba(255,255,255,.4); }
.cell.ghost.bad { background: rgba(255,80,80,.16); box-shadow: inset 0 0 0 2px rgba(255,80,80,.35); }
/* Линия, которая соберётся этим ходом */
.cell.will { animation: willGlow .8s ease-in-out infinite alternate; }
@keyframes willGlow {
  from { box-shadow: inset 0 0 0 2px rgba(255,213,79,.35), 0 0 4px 1px rgba(255,213,79,.25); }
  to   { box-shadow: inset 0 0 0 2px rgba(255,213,79,.8), 0 0 12px 3px rgba(255,213,79,.5); }
}

/* ---- Самоцветы ---- */
.gem, .cell.fill::after { content: ''; }
.cell.fill::after {
  position: absolute; inset: 0;
  border-radius: 22%;
  background:
    radial-gradient(ellipse at 30% 22%, rgba(255,255,255,.75), rgba(255,255,255,.12) 42%, transparent 60%),
    linear-gradient(160deg, var(--c1) 0%, var(--c2) 100%);
  box-shadow: inset 0 -3px 5px rgba(0,0,0,.32), inset 0 2px 3px rgba(255,255,255,.35), 0 2px 5px rgba(0,0,0,.35);
}
.cell.fill.pop::after { animation: pop .3s forwards; }
@keyframes pop {
  40% { transform: scale(1.18); filter: brightness(1.8); }
  100% { transform: scale(0); filter: brightness(2.2); }
}
.cell.fill.land::after { animation: land .22s cubic-bezier(.3,1.5,.5,1); }
@keyframes land { from { transform: scale(1.25); } to { transform: scale(1); } }
/* Падение блока после схлопывания: прилетает сверху на своё место */
.cell.fill.fall::after { animation: fallIn .3s cubic-bezier(.35,1.25,.5,1); }
@keyframes fallIn {
  from { transform: translateY(calc((var(--cell) + 4px) * var(--fd, 1) * -1)); }
  to   { transform: translateY(0); }
}
.cell.boom { animation: boomcell .35s; }
@keyframes boomcell { 50% { background: rgba(255,170,60,.5); } }

/* Цвета самоцветов */
.k1 { --c1: #ff6b81; --c2: #c62b46; }
.k2 { --c1: #58c7ff; --c2: #1f6fbf; }
.k3 { --c1: #7ee787; --c2: #2c9440; }
.k4 { --c1: #ffd54f; --c2: #d0910c; }
.k5 { --c1: #c99bff; --c2: #7b3fd4; }
.k6 { --c1: #6ee7d8; --c2: #1f9c8b; }
.k7 { --c1: #ffa94d; --c2: #d06a10; }

/* ---- Лоток фигур ---- */
#tray {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 5vmin;
  min-height: calc(var(--tcell, 24px) * 3 + 58px);
  padding: 4px 0 2px;
}
.slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  min-width: calc(var(--tcell, 24px) * 2);
}
.slot.used { visibility: hidden; }
/* Стрелки поворота над фигурой */
.rots { display: flex; gap: 12px; min-height: 30px; }
.rbtn {
  width: 30px; height: 30px;
  font-size: 17px; line-height: 1;
  color: var(--ink);
  background: var(--btnbg);
  border: 2px solid var(--btnline);
  border-radius: 50%;
  box-shadow: 0 2px 6px rgba(0,0,0,.25);
  touch-action: none;
}
.rbtn:active { transform: scale(.88); }
/* Поворот выручит: фигура так не влезает, но в другом положении — да */
.rots.hintrot .rbtn { animation: rhint 1s ease-in-out infinite alternate; }
@keyframes rhint {
  from { border-color: var(--btnline); box-shadow: 0 2px 6px rgba(0,0,0,.25); }
  to   { border-color: #ffd54f; box-shadow: 0 0 10px 2px rgba(255,213,79,.55); }
}
.piece.rotp { animation: protate .26s cubic-bezier(.3,1.3,.5,1); }
@keyframes protate { from { transform: rotate(var(--ra, -90deg)) scale(.82); } to { transform: rotate(0deg) scale(1); } }
.piece {
  display: grid;
  gap: 3px;
  touch-action: none;
  transition: opacity .2s, transform .2s;
}
.piece .pc {
  width: var(--tcell, 24px); height: var(--tcell, 24px);
  border-radius: 22%;
  background:
    radial-gradient(ellipse at 30% 22%, rgba(255,255,255,.75), rgba(255,255,255,.12) 42%, transparent 60%),
    linear-gradient(160deg, var(--c1) 0%, var(--c2) 100%);
  box-shadow: inset 0 -2px 4px rgba(0,0,0,.32), inset 0 1px 2px rgba(255,255,255,.35), 0 2px 4px rgba(0,0,0,.3);
}
.piece .pc.empty { background: none; box-shadow: none; }
.piece.used { visibility: hidden; }
.piece.stuck { opacity: .35; filter: grayscale(.7); }
.piece.spawnp { animation: pspawn .35s cubic-bezier(.3,1.5,.5,1); }
@keyframes pspawn { from { transform: scale(.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* Фигура, которую тащат */
#drag {
  position: fixed;
  display: grid;
  gap: 4px;
  pointer-events: none;
  z-index: 30;
  filter: drop-shadow(0 14px 12px rgba(0,0,0,.45));
}
#drag .pc {
  width: var(--cell); height: var(--cell);
  border-radius: 22%;
  background:
    radial-gradient(ellipse at 30% 22%, rgba(255,255,255,.75), rgba(255,255,255,.12) 42%, transparent 60%),
    linear-gradient(160deg, var(--c1) 0%, var(--c2) 100%);
  box-shadow: inset 0 -3px 5px rgba(0,0,0,.32), inset 0 2px 3px rgba(255,255,255,.35);
}
#drag .pc.empty { background: none; box-shadow: none; }

footer {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 8px 0 18px;
  position: relative; z-index: 2;
}

/* ---- Оверлей конца игры ---- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(20,14,40,.75);
  display: flex; align-items: center; justify-content: center;
  z-index: 40;
  backdrop-filter: blur(4px);
}
.overlay.hidden { display: none; }
.card {
  background: linear-gradient(165deg, #453768, #2b2140);
  border: 2px solid rgba(255,255,255,.2);
  border-radius: 24px;
  padding: 34px 40px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 24px 48px rgba(0,0,0,.5);
  animation: cardIn .4s cubic-bezier(.3,1.4,.5,1);
}
@keyframes cardIn { from { transform: scale(.6); opacity: 0; } }
.big { font-size: 26px; font-weight: 800; }
#overScore { font-size: 18px; line-height: 1.5; color: rgba(255,255,255,.85); }
.card button {
  font-size: 19px; font-weight: 700;
  color: #4b3200;
  background: linear-gradient(180deg, #ffd54f, #ffb300);
  border: none;
  border-radius: 999px;
  padding: 13px 34px;
  box-shadow: 0 5px 12px rgba(0,0,0,.35), inset 0 2px 2px rgba(255,255,255,.5);
}
.card button:active { transform: translateY(2px); }

#toast {
  position: fixed;
  left: 50%; bottom: 15%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.75);
  color: #fff;
  font-size: 16px; font-weight: 600;
  border-radius: 999px;
  padding: 10px 22px;
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
  z-index: 50;
  white-space: nowrap;
}
#toast.show { opacity: 1; }

/* Летящие очки */
.fly {
  position: fixed;
  font-weight: 800;
  color: #ffd54f;
  text-shadow: 0 2px 6px rgba(0,0,0,.6);
  pointer-events: none;
  z-index: 35;
  animation: flyup 1s ease-out forwards;
}
@keyframes flyup { to { transform: translateY(-70px); opacity: 0; } }

#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 45; overflow: hidden; }
#confetti i {
  position: absolute;
  width: 10px; height: 14px;
  top: -20px;
  animation: cfall linear forwards;
}
@keyframes cfall { to { transform: translateY(115vh) rotate(720deg); } }
