@import url('https://fonts.googleapis.com/css?family=Arvo');

/* Цветовая палитра */
:root {
  --grid-bg: #9a9a95;
  --color-1: #FFC43D;
  --color-2: #EF476F;
  --color-3: #1B9AAA;
  --color-4: #06D6A0;
  --placeholder-tile: #F8FFE5;
  --font-color: #555;
  --border-width: 7px;
}

* {
  box-sizing: border-box;
}

a {
  color: var(--color-3);
  text-decoration: none;
  border-bottom: 1px solid currentColor;
}

a:hover {
  color: #0d7a8a;
}

a:focus,
a:active {
  color: #0a5f6b;
}

body,
html {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  font-family: "Arvo", Helvetica, sans-serif;
  font-size: 12px;
  color: var(--font-color);
  background: var(--placeholder-tile);
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  touch-action: manipulation;
}

/* Отступ для мобильных устройств с монобровью */
@media (max-width: 768px) {
  body {
    padding-bottom: max(env(safe-area-inset-bottom), 20px);
    padding-left: max(env(safe-area-inset-left), 10px);
    padding-right: max(env(safe-area-inset-right), 10px);
  }
}

strong {
  font-weight: bold;
}

p {
  line-height: 1.6;
}

.inspired {
  margin-top: 1em;
  font-size: 0.9rem;
  color: var(--grid-bg);
}

header {
  color: var(--placeholder-tile);
  text-align: center;
}

header span {
  display: inline-block;
  box-sizing: border-box;
  width: 4rem;
  height: 4rem;
  line-height: 4rem;
  margin: 0 0.4rem;
  background: var(--color-1);
  border-radius: 3px;
  font-weight: bold;
  font-size: 1.5rem;
}

header span:nth-of-type(2) {
  background: var(--color-2);
}

header span:nth-of-type(3) {
  background: var(--color-3);
}

header span:nth-of-type(4) {
  background: var(--color-4);
}

h1 {
  font-size: 2.2rem;
  margin: 0;
}

.directions {
  padding: 2rem;
  border-top: 1px solid var(--grid-bg);
  border-bottom: 1px solid var(--grid-bg);
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  margin: 1rem 0;
}

.container {
  margin: 0 auto;
  padding-bottom: 3.5rem;
  flex: 1;
  width: 100%;
  max-width: 550px;
  text-align: center;
}

.container header {
  padding: 2rem 4rem;
  max-width: 900px;
}

.scores {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin: 0.5rem 0;
  padding: 0 1rem;
}

.score-container {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.9rem;
  color: var(--font-color);
  background: rgba(255, 255, 255, 0.1);
  padding: 0.3rem 0.6rem;
  border-radius: 4px;
  min-width: 60px;
}

.score-container.current-score {
  color: var(--accent, #0a84ff);
}

.score-container.best-score {
  color: var(--grid-bg);
}

.score-label {
  font-size: 1.4rem;
  opacity: 0.8;
}

.score-value {
  font-weight: bold;
  font-size: 2rem;
}

.game {
  position: relative;
  margin: 0 auto;
  background: var(--grid-bg);
  padding: var(--border-width);
  display: inline-block;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.tile-container {
  border-radius: 6px;
  position: relative;
  width: 400px;
  height: 400px;
}

.tile, .background {
  display: block;
  color: var(--placeholder-tile);
  position: absolute;
  width: 100px;
  height: 100px;
  box-sizing: border-box;
  text-align: center;
}

.background {
  z-index: 1;
  text-align: center;
  border: var(--border-width) solid var(--grid-bg);
  background-color: var(--placeholder-tile);
  border-radius: 3px;
}

.tile {
  opacity: 1;
  z-index: 2;
  background: var(--color-1);
  color: #333;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: bold;
  transition: all 150ms ease-in-out;
  border-radius: 3px;
  border: var(--border-width) solid var(--grid-bg);
  box-sizing: border-box;
}

/* Плитка перед анимацией появления */
.tile--new {
  opacity: 0;
  transform: scale(0.5);
}

.tile--2 {
  background: var(--color-1);
  color: #333;
}

.tile--4 {
  background: var(--color-2);
  color: white;
}

.tile--8 {
  background: var(--color-3);
  color: white;
}

.tile--16 {
  background: var(--color-4);
  color: white;
}

.tile--32 {
  background: #ff6b6b;
  color: white;
}

.tile--64 {
  background: #4ecdc4;
  color: white;
}

.tile--128 {
  background: #45b7d1;
  color: white;
}

.tile--256 {
  background: #f9ca24;
  color: #333;
}

.tile--512 {
  background: #f0932b;
  color: white;
}

.tile--1024 {
  background: #eb4d4b;
  color: white;
}

.tile--2048 {
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  color: white;
  box-shadow: 0 0 20px rgba(255, 107, 107, 0.5);
}

.tile--pop {
  animation: pop 0.3s ease-in;
  animation-fill-mode: forwards;
  opacity: 1;
  transform: scale(1);
}

.tile--shrink {
  animation: shrink 0.5s ease-in-out;
  animation-fill-mode: forwards;
}

.tile--delete {
  animation: deleteTile 0.3s ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes deleteTile {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.8;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

.delete-particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 1000;
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.8);
}

.tile--upgrade {
  animation: upgradeTile 0.2s ease-in-out;
  animation-fill-mode: forwards;
}

.tile--upgraded {
  animation: upgradedTile 0.3s ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes upgradeTile {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes upgradedTile {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 rgba(249, 202, 36, 0);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 0 20px rgba(249, 202, 36, 0.8);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 rgba(249, 202, 36, 0);
  }
}

.upgrade-particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 1000;
  animation: upgradeParticleGlow 1s ease-out;
}

.shuffle-particle {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 1000;
  animation: shuffleParticleGlow 1.2s ease-out;
}

@keyframes upgradeParticleGlow {
  0% {
    transform: scale(0.5);
    opacity: 0.8;
  }
  50% {
    transform: scale(1.3);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

@keyframes shuffleParticleGlow {
  0% {
    transform: scale(0.3);
    opacity: 0.6;
  }
  30% {
    transform: scale(1.5);
    opacity: 1;
  }
  70% {
    transform: scale(1.2);
    opacity: 0.8;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

.add {
  position: absolute;
  opacity: 0;
  left: 120%;
  top: 0;
  font-size: 1rem;
  color: var(--color-3);
  font-weight: bold;
}

.add.active {  
  animation: add 0.8s ease-in-out;
}

@keyframes add {
  0% {
    opacity: 1;
    top: 0;
  }
  100% {
    opacity: 0;
    top: -100%;
  }
}

@keyframes pop {
  0% {
    transform: scale(0.5);
    opacity: 0;
  }
  90% {
    transform: scale(1.1);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes shrink {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0.9);
    opacity: 0.9;
  }
}

.end {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(85, 85, 85, 0.9);
  color: white;
  font-size: 2rem;
  transition: opacity 0.3s ease-in-out;
  border-radius: 6px;
}

.end.active {
  opacity: 1;
  z-index: 1000;
}

.monkey {
  font-size: 3rem;
  margin: 1rem 0;
}

.btn {
  font-family: inherit;
  font-size: 1rem;
  border: none;
  background: var(--color-3);
  letter-spacing: 1px;
  color: white;
  font-weight: 300;
  padding: 0.9em 1.5em;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  margin: 0.5rem;
}

.btn:hover {
  background-color: #0d7a8a;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.btn:active {
  background-color: #0a5f6b;
  transform: translateY(0);
}

.btn:focus {
  box-shadow: 0 0 10px rgba(27, 154, 170, 0.3);
  outline: none;
}

.not-recommended {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 3rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.not-recommended__item + .not-recommended__annotation:before {
  font-size: 30px;
  content: "😐";
}

.not-recommended__item:hover + .not-recommended__annotation:before {
  content: "😟";
}

.not-recommended__item:focus + .not-recommended__annotation:before {
  content: "😄";
}

.not-recommended__item:active + .not-recommended__annotation:before {
  content: "😨";
}

/* Универсальный UI */
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1rem;
  background: var(--card, #1a1d24);
  border-bottom: 1px solid var(--border, #26262a);
}

.back-btn, .restart-btn-header, .leaderboard-btn-header {
  background: var(--color-3);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.5rem;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-btn:hover, .restart-btn-header:hover, .leaderboard-btn-header:hover {
  background: #0d7a8a;
  transform: translateY(-1px);
}

.back-btn:active, .restart-btn-header:active, .leaderboard-btn-header:active {
  background: #0a5f6b;
  transform: translateY(0);
}

.header-buttons {
  display: flex;
  gap: 0.5rem;
}

.back-btn {
  padding: 0.5rem 1rem;
  font-size: 14px;
  min-width: auto;
}

/* Game wrapper для позиционирования стрелок */
.game-wrapper {
  position: relative;
  display: inline-block;
  margin: 0 auto;
}

/* Виртуальные стрелки для мобильных */
.mobile-controls {
  display: none;
  position: relative;
  width: 300px;
  height: 300px;
  margin: 10px auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 4px;
}

.control-btn {
  width: 100%;
  height: 100%;
  background: var(--color-3);
  color: white;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
  user-select: none;
  transition: all 0.2s ease;
  border: 4px solid transparent;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

/* Позиционирование кнопок в крестовине */
#up-btn {
  grid-column: 2;
  grid-row: 1;
}

#left-btn {
  grid-column: 1;
  grid-row: 2;
}

#down-btn {
  grid-column: 2;
  grid-row: 3;
}

#right-btn {
  grid-column: 3;
  grid-row: 2;
}

.control-btn:hover {
  background: #0d7a8a;
}

.control-btn:active {
  background: #0a5f6b;
}

/* Базовые респонсивные стили */
.container {
  max-width: 100%;
  padding: 0.5rem;
  box-sizing: border-box;
}

.game-wrapper {
  position: relative;
  display: inline-block;
  margin: 0 auto;
  max-width: 100%;
}

/* Адаптивность для разных размеров экрана */
@media (max-width: 768px) {
  .tile-container {
    width: min(90vw, 300px);
    height: min(90vw, 300px);
  }
  
  .tile, .background {
    width: calc(min(90vw, 300px) / 4);
    height: calc(min(90vw, 300px) / 4);
  }
  
  .tile {
    font-size: clamp(0.8rem, 4vw, 1.2rem);
  }
  
  .game-header {
    padding: 0.6rem 0.8rem;
  }
  
  .back-btn {
    padding: 0.4rem 0.8rem;
    font-size: 12px;
  }
  
  .restart-btn-header, .leaderboard-btn-header {
    font-size: 14px;
    padding: 0.4rem;
    min-width: 35px;
    height: 35px;
  }
  
  .scores {
    padding: 0 0.5rem;
    margin: 0.3rem 0;
  }
  
  .score-container {
    padding: 0.3rem 0.5rem;
    font-size: 1.4rem;
    min-width: 50px;
  }
  
  .score-value {
    font-size: 1.8rem;
  }
  
  /* Показываем виртуальные стрелки на мобильных */
  .mobile-controls {
    display: grid;
    width: 240px;
    height: 240px;
  }
  
  .control-btn {
    font-size: 36px;
  }
}

@media (max-width: 480px) {
  .tile-container {
    width: min(85vw, 280px);
    height: min(85vw, 280px);
  }
  
  .tile, .background {
    width: calc(min(85vw, 280px) / 4);
    height: calc(min(85vw, 280px) / 4);
  }
  
  .tile {
    font-size: clamp(0.7rem, 3.5vw, 1rem);
  }
  
  .game-header {
    padding: 0.5rem 0.6rem;
  }
  
  .back-btn {
    padding: 0.3rem 0.6rem;
    font-size: 11px;
  }
  
  .restart-btn-header, .leaderboard-btn-header {
    font-size: 12px;
    padding: 0.3rem;
    min-width: 32px;
    height: 32px;
  }
  
  .scores {
    padding: 0 0.3rem;
    margin: 0.2rem 0;
  }
  
  .score-container {
    padding: 0.2rem 0.4rem;
    font-size: 1.4rem;
    min-width: 45px;
  }
  
  .score-value {
    font-size: 1.6rem;
  }
  
  .mobile-controls {
    width: 200px;
    height: 200px;
  }
  
  .control-btn {
    font-size: 32px;
  }
}

@media (max-width: 360px) {
  .tile-container {
    width: min(80vw, 260px);
    height: min(80vw, 260px);
  }
  
  .tile, .background {
    width: calc(min(80vw, 260px) / 4);
    height: calc(min(80vw, 260px) / 4);
  }
  
  .tile {
    font-size: clamp(0.6rem, 3vw, 0.9rem);
  }
  
  .game-header {
    padding: 0.4rem 0.5rem;
  }
  
  .back-btn {
    padding: 0.2rem 0.5rem;
    font-size: 10px;
  }
  
  .restart-btn-header, .leaderboard-btn-header {
    font-size: 11px;
    padding: 0.2rem;
    min-width: 28px;
    height: 28px;
  }
  
  .scores {
    padding: 0 0.2rem;
    margin: 0.1rem 0;
  }
  
  .score-container {
    padding: 0.1rem 0.3rem;
    font-size: 1.1rem;
    min-width: 40px;
  }
  
  .score-value {
    font-size: 1.4rem;
  }
  
  .mobile-controls {
    width: 160px;
    height: 160px;
  }
  
  .control-btn {
    font-size: 28px;
  }
  
  .bonuses-container {
    gap: 15px;
    margin: 15px 0;
    padding: 0 15px;
  }
  
  .bonus-item {
    width: 60px;
    height: 60px;
  }
  
  .bonus-icon {
    font-size: 24px;
  }
  
  .bonus-count {
    width: 20px;
    height: 20px;
    font-size: 10px;
  }
}

/* Бонусы */
.bonuses-container {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 20px 0;
  padding: 0 20px;
}

.bonus-item {
  position: relative;
  width: 80px;
  height: 80px;
  background: var(--color-3);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  border: 3px solid transparent;
}

.bonus-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
}

.bonus-item:active {
  transform: translateY(0);
}

.bonus-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: #ccc;
}

.bonus-item.disabled:hover {
  transform: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.bonus-icon {
  font-size: 32px;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.bonus-count {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--color-2);
  color: white;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  border: 2px solid white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.bonus-item.purchase-mode .bonus-count {
  background: var(--color-4);
}

/* Показываем "+" только когда бонусов 0 */
.bonus-item.purchase-mode .bonus-count[data-count="0"] {
  font-size: 0 !important;
  color: transparent !important;
}

.bonus-item.purchase-mode .bonus-count[data-count="0"]::after {
  content: "+";
  font-size: 16px !important;
  color: #fff !important;
  font-weight: bold !important;
}

/* Убираем "+" для всех остальных случаев */
.bonus-item.purchase-mode .bonus-count:not([data-count="0"])::after {
  content: "";
}

/* Режим выбора плитки */
.bonus-mode-active {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  pointer-events: auto;
}

.bonus-mode-message {
  background: var(--color-3);
  color: white;
  padding: 15px 20px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  max-width: 300px;
  margin: 0 auto;
}

.bonus-mode-message h3 {
  margin: 0 0 10px 0;
  color: white;
  font-size: 16px;
}

.bonus-mode-message p {
  margin: 0 0 15px 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.bonus-mode-cancel {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s ease;
}

.bonus-mode-cancel:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Анимации для улучшения UX */
.game-container {
  animation: fadeIn 0.5s ease-in-out;
  touch-action: manipulation;
  overflow-x: hidden;
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height for mobile */
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Эффект для кнопок */
.btn {
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.btn:hover::before {
  left: 100%;
}
