/* 2048 CUPCAKE - Gameplay Interface & Grid Styling (Responsive Flexbox Centering) */

#screen-game {
  width: 100%;
  height: 100%;
  max-height: 100%;
  max-width: 560px;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: clamp(6px, 1.5vh, 14px) clamp(8px, 2.5vw, 16px);
  gap: clamp(6px, 1.2vh, 12px);
  margin: 0 auto;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
}

#screen-game.active {
  display: flex;
}

/* 1. Game Top Header: Navigation, Mode & Stat Boxes */
.game-header {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: clamp(4px, 0.8vh, 12px);
  flex-shrink: 0;
  box-sizing: border-box;
}

.game-top-bar {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: clamp(4px, 1.2vw, 10px);
  flex-wrap: nowrap;
  box-sizing: border-box;
  min-width: 0;
}

.game-brand-group {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: clamp(6px, 1.5vw, 12px);
  flex-shrink: 1;
  flex-wrap: nowrap;
  min-width: 0;
}

.game-nav-actions {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: clamp(4px, 1vw, 8px);
  flex-shrink: 0;
}

.game-nav-actions .btn-icon {
  width: clamp(38px, 9vw, 44px);
  height: clamp(38px, 9vw, 44px);
  font-size: clamp(1.15rem, 2.8vw, 1.35rem);
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  border-radius: var(--radius-md);
}

.mode-tag {
  font-size: 0.62rem;
  font-weight: 800;
  background: var(--bg-card);
  color: var(--text-medium);
  padding: 2px 6px;
  border-radius: var(--radius-full);
  border: 1px solid rgba(74, 46, 24, 0.1);
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
  min-width: 0;
  max-width: 78px;
}

.game-stats-bar {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: clamp(3px, 1vw, 8px);
  flex-shrink: 1;
  flex-wrap: nowrap;
  min-width: 0;
  overflow: hidden;
}

.stat-box {
  background: var(--bg-card);
  padding: clamp(2px, 0.5vh, 4px) clamp(4px, 1vw, 8px);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(74, 46, 24, 0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: clamp(30px, 9.5vw, 60px);
  max-width: 100%;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  flex: 1 1 auto;
  flex-shrink: 1;
}

.stat-box.highlight {
  background: linear-gradient(135deg, #fff7ed 0%, #faede0 100%);
  border-color: rgba(242, 106, 141, 0.4);
}

.stat-label {
  font-size: clamp(0.5rem, 1.6vw, 0.62rem);
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  line-height: 1;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  display: block;
  text-align: center;
}

.stat-value {
  font-family: var(--font-display);
  font-size: clamp(0.85rem, 2.4vw, 1.15rem);
  font-weight: 800;
  color: var(--text-dark);
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  display: block;
  text-align: center;
}

/* Floating Added Points */
.score-addition {
  position: absolute;
  top: -10px;
  right: 4px;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 0.90rem;
  color: var(--primary);
  opacity: 0;
  pointer-events: none;
  animation: floatScore 0.8s ease-out forwards;
}

@keyframes floatScore {
  0% { opacity: 0; transform: translateY(0) scale(0.8); }
  20% { opacity: 1; transform: translateY(-6px) scale(1.15); }
  100% { opacity: 0; transform: translateY(-20px) scale(0.9); }
}

/* Combo Banner */
.combo-banner-container {
  min-height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.combo-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 12px;
  background: linear-gradient(135deg, #f5b041 0%, #f26a8d 100%);
  color: #fff;
  border-radius: var(--radius-full);
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 0.80rem;
  letter-spacing: 0.5px;
  box-shadow: 0 3px 10px rgba(242, 106, 141, 0.35);
  animation: popIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.combo-meter {
  width: 44px;
  height: 4px;
  background: rgba(255, 255, 255, 0.4);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.combo-meter-fill {
  height: 100%;
  width: 100%;
  background: #fff;
  transition: width linear;
}

/* 2. Responsive Center Play Area & Perfectly Centered Board Wrapper */
.game-center-area {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(4px, 1vh, 8px);
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

.game-board-wrapper {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  box-sizing: border-box;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  padding: 0;
  margin: 0 auto;
}

/* Strict Square 1:1 Aspect Ratio 4x4 Grid */
.game-board {
  --grid-size: 4;
  --grid-gap: clamp(6px, 1.8vw, 10px);
  --grid-padding: clamp(6px, 1.8vw, 10px);
  
  /* Fits dynamically into available flexbox area without overflow or clipping */
  width: min(calc(100vw - 28px), calc(100dvh - 210px), 440px);
  height: min(calc(100vw - 28px), calc(100dvh - 210px), 440px);
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 1;

  background: var(--bg-board);
  border-radius: var(--radius-xl);
  padding: var(--grid-padding);
  position: relative;
  box-shadow: inset 0 3px 6px rgba(74, 46, 24, 0.15), var(--shadow-md);
  border: 4px solid #debfa7;
  box-sizing: border-box;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid-background {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--grid-gap);
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

.grid-cell {
  background: var(--bg-cell);
  border-radius: var(--radius-md);
  box-shadow: inset 0 2px 4px rgba(74, 46, 24, 0.1);
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

.tile-container {
  position: absolute;
  top: var(--grid-padding);
  left: var(--grid-padding);
  width: calc(100% - (var(--grid-padding) * 2));
  height: calc(100% - (var(--grid-padding) * 2));
  pointer-events: none;
  box-sizing: border-box;
}

/* Tile styling */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - (var(--grid-gap) * 3)) / 4);
  height: calc((100% - (var(--grid-gap) * 3)) / 4);
  transform: translate(
    calc(var(--col, 0) * (100% + var(--grid-gap))),
    calc(var(--row, 0) * (100% + var(--grid-gap)))
  );
  transition: transform 110ms cubic-bezier(0.2, 0.8, 0.2, 1);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(74, 46, 24, 0.14);
  pointer-events: auto;
  z-index: 10;
  box-sizing: border-box;
}

/* Explicit positional classes for guaranteed placement (Row - Col) */
.tile.tile-pos-0-0 { transform: translate(0, 0); }
.tile.tile-pos-0-1 { transform: translate(calc(100% + var(--grid-gap)), 0); }
.tile.tile-pos-0-2 { transform: translate(calc(200% + (var(--grid-gap) * 2)), 0); }
.tile.tile-pos-0-3 { transform: translate(calc(300% + (var(--grid-gap) * 3)), 0); }

.tile.tile-pos-1-0 { transform: translate(0, calc(100% + var(--grid-gap))); }
.tile.tile-pos-1-1 { transform: translate(calc(100% + var(--grid-gap)), calc(100% + var(--grid-gap))); }
.tile.tile-pos-1-2 { transform: translate(calc(200% + (var(--grid-gap) * 2)), calc(100% + var(--grid-gap))); }
.tile.tile-pos-1-3 { transform: translate(calc(300% + (var(--grid-gap) * 3)), calc(100% + var(--grid-gap))); }

.tile.tile-pos-2-0 { transform: translate(0, calc(200% + (var(--grid-gap) * 2))); }
.tile.tile-pos-2-1 { transform: translate(calc(100% + var(--grid-gap)), calc(200% + (var(--grid-gap) * 2))); }
.tile.tile-pos-2-2 { transform: translate(calc(200% + (var(--grid-gap) * 2)), calc(200% + (var(--grid-gap) * 2))); }
.tile.tile-pos-2-3 { transform: translate(calc(300% + (var(--grid-gap) * 3)), calc(200% + (var(--grid-gap) * 2))); }

.tile.tile-pos-3-0 { transform: translate(0, calc(300% + (var(--grid-gap) * 3))); }
.tile.tile-pos-3-1 { transform: translate(calc(100% + var(--grid-gap)), calc(300% + (var(--grid-gap) * 3))); }
.tile.tile-pos-3-2 { transform: translate(calc(200% + (var(--grid-gap) * 2)), calc(300% + (var(--grid-gap) * 3))); }
.tile.tile-pos-3-3 { transform: translate(calc(300% + (var(--grid-gap) * 3)), calc(300% + (var(--grid-gap) * 3))); }

.tile-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  background: var(--bg-card);
  padding: 3px;
  box-sizing: border-box;
  border: 1.5px solid rgba(156, 102, 68, 0.14);
  box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.95);
}

.tile-svg-wrap {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.tile-svg-wrap svg {
  width: 95%;
  height: 95%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 3px 6px rgba(74, 46, 24, 0.16));
}

/* Animations on .tile-inner to preserve transform translations */
.tile.tile-new .tile-inner {
  animation: tileAppear 180ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.tile.tile-merged .tile-inner {
  animation: tilePop 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 20;
}

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

@keyframes tilePop {
  0% { transform: scale(0.85); }
  50% { transform: scale(1.16); }
  100% { transform: scale(1); }
}

/* Golden Cupcake Special Tile */
.tile.tile-golden {
  box-shadow: 0 0 15px rgba(245, 176, 65, 0.8), 0 0 30px rgba(245, 176, 65, 0.4);
  animation: goldenPulse 2s ease-in-out infinite;
}

.tile.tile-golden .tile-inner {
  background: linear-gradient(135deg, #fff9db 0%, #ffe066 50%, #f59f00 100%);
  border: 1.5px solid #fff;
}

/* 3. Quick Direction Controls / Mobile D-Pad */
.game-dpad-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 3vw, 22px);
  width: 100%;
  margin-top: clamp(10px, 2.4vh, 16px);
  margin-bottom: clamp(22px, 4.5vh, 32px);
  flex-shrink: 0;
  box-sizing: border-box;
}

.dpad-btn {
  background: var(--bg-card);
  border: 2px solid #debfa7;
  color: var(--text-dark);
  width: clamp(54px, 14.5vw, 68px);
  height: clamp(46px, 8vh, 56px);
  border-radius: var(--radius-lg);
  font-size: clamp(1.4rem, 3.8vw, 1.65rem);
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 3px 6px rgba(74, 46, 24, 0.12), var(--shadow-sm);
  transition: all 0.12s ease;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.dpad-btn:active, .dpad-btn:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary-dark);
  transform: scale(0.95);
}

/* 4. In-Game Level Progress Footer */
.game-bottom-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 2px 4px calc(2px + var(--safe-bottom, 0px)) 4px;
  flex-shrink: 0;
  box-sizing: border-box;
}

.currency-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  background: var(--bg-card);
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(74, 46, 24, 0.08);
  font-family: var(--font-display);
  font-size: 0.85rem;
  font-weight: 800;
  color: #b08968;
}

.game-xp-track {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.game-xp-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-muted);
}

.game-xp-bar {
  width: 100%;
  height: 5px;
  background: var(--bg-cell);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.game-xp-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #52b788, #4ea8de);
  border-radius: var(--radius-full);
  transition: width 0.3s ease;
}
