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

/* ============================================
   CSS Variables - Synced with GameConfig.CSS
   ============================================ */
:root {
  /* Font Families */
  --font-main: sans-serif;
  --font-display: 'Ravenhell', serif;

  /* Tile dimensions - responsive scaling that respects both width and height */
  --tile-size-v: clamp(35px, 8.5vh, 55px);
  --tile-size-h: calc((100vw - 2 * var(--board-padding)) / var(--grid-size));
  --tile-width: min(var(--tile-size-v), var(--tile-size-h));
  --tile-height: var(--tile-width);
  /* Strictly preserve square shape */
  --tile-gap: 0px;
  --grid-size: 8;
  /* Fallback grid size */

  /* Board styling */
  --board-bg: #18724A;
  --board-surface: rgba(28, 128, 84, 0.4);
  --board-padding: clamp(10px, 2vw, 20px);
  --board-radius: 10px;

  /* Table ambiance */
  --felt-light: #1e8a56;
  --felt-dark: #145a38;
  --vignette-strength: 0.2;

  /* 3D perspective (sync with GameConfig.CSS) */
  --perspective: 1200px;
  --board-rotation: 10deg;
  --level-height: 10px;

  /* Interaction lifts */
  --hover-lift: 8px;
  --select-lift: 25px;

  /* Mobile pan/camera system */
  --board-pan-x: 0px;
  --board-pan-y: 0px;
  --pan-transition: transform 0.1s ease-out;

  /* Z-index layers (sync with GameConfig.Z_INDEX) */
  --z-selected: 999999;
  --z-hover: 999998;
  --z-match-animation: 1000000;
  --z-auto-finish: 5000000;
  --z-overlay: 9000000;
  --z-shuffle-overlay: 9100000;

  /* Animation easing */
  --ease-bounce: cubic-bezier(0.18, 0.89, 0.32, 1.28);
  --ease-smooth: cubic-bezier(0.55, 0, 0.1, 1);

  /* Colors - ivory theme */
  --ivory: #fffff8;
  --ivory-side: #c8c8c0;
}

/* ============================================
   Fonts
   ============================================ */
@font-face {
  font-family: 'Ravenhell';
  src: url('../webfonts/ravenhell.woff') format('woff');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* ============================================
   Base Styles
   ============================================ */
html {
  background-color: var(--board-bg);
  height: 100%;
  overflow: hidden;
  /* Felt texture - layered gradients simulating fabric weave */
  background-image:
    /* Subtle light spot (table center lighting) */
    radial-gradient(ellipse 60% 50% at 50% 45%, rgba(40, 160, 100, 0.25) 0%, transparent 70%),
    /* Fine horizontal weave */
    repeating-linear-gradient(0deg,
      transparent,
      transparent 2px,
      rgba(0, 0, 0, 0.03) 2px,
      rgba(0, 0, 0, 0.03) 4px),
    /* Fine vertical weave */
    repeating-linear-gradient(90deg,
      transparent,
      transparent 2px,
      rgba(0, 0, 0, 0.02) 2px,
      rgba(0, 0, 0, 0.02) 4px),
    /* Subtle color variation */
    radial-gradient(circle at 30% 70%, var(--felt-light) 0%, transparent 50%),
    radial-gradient(circle at 70% 30%, var(--felt-light) 0%, transparent 50%);
}

body {
  font-family: var(--font-main);
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  height: 100%;
  width: 100%;
  /* Mobile Optimizations */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: var(--font-display);
}

/* Vignette overlay */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse 70% 60% at 50% 50%,
      transparent 40%,
      rgba(0, 0, 0, var(--vignette-strength)) 100%);
  pointer-events: none;
  z-index: 0;
}

/* Ensure app content is above the vignette */
#app {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
}

.section {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  touch-action: none;
}

.container.panning {
  cursor: grabbing;
}

/* Mobile only: enable grab cursor */
@media (max-width: 768px) {
  .container {
    cursor: grab;
  }
}



/* ============================================
   Board Styles
   ============================================ */
.board {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  perspective: var(--perspective);
  perspective-origin: 50% 50%;
  padding-bottom: 80px;
  /* Space for fixed auto-finish button */
}

.board-surface {
  position: relative;
  width: calc(var(--grid-size) * (var(--tile-width) + var(--tile-gap)));
  height: calc(var(--grid-size) * (var(--tile-height) + var(--tile-gap)));
  margin: 0 auto;
  transform: translate(var(--board-pan-x), var(--board-pan-y)) rotateX(var(--board-rotation));
  transform-style: preserve-3d;
  transition: var(--pan-transition);
  will-change: transform;
}

/* ============================================
   Header Styles (Top Bar)
   ============================================ */
.game-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: rgba(0, 0, 0, 0.15);
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.game-title {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 900;
  color: #FFFFFF;
  letter-spacing: 1px;
  line-height: 1;
}

.level-badge {
  font-family: var(--font-main);
  font-size: 0.8rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.8);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.header-right {
  display: flex;
  gap: 10px;
}

.header-icon-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1.3rem;
  color: #061B43;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.header-icon-btn:hover {
  transform: scale(1.08);
  filter: brightness(1.1);
}

.header-icon-btn:active {
  transform: scale(0.95);
}

.trophy-btn {
  background-color: #FF9900;
}

.user-btn {
  background-color: #5588EE;
}

/* ============================================
   Tile Base Styles
   ============================================ */
.tile {
  position: absolute;
  width: var(--tile-width);
  height: var(--tile-height);
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  cursor: pointer;
  user-select: none;
  overflow: visible;
  touch-action: manipulation;
  border-radius: 8px;
  /* Use CSS variable for base z-index */
  z-index: var(--base-z-index);
  /* Use smooth easing for exit to prevent dipping */
  transition: transform 0.25s var(--ease-smooth), z-index 0s 0.25s;
  transform-style: preserve-3d;
  transform: translateZ(calc(var(--level) * var(--level-height)));
  will-change: transform, opacity;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* Tile Face (::before pseudo-element) */
.tile::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: var(--tile-image);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  background-color: var(--ivory);
  border-radius: 6px;
  transform: translateZ(0);
  box-shadow:
    0 1px 0 var(--ivory-side),
    0 2px 0 var(--ivory-side),
    0 3px 0 var(--ivory-side),
    0 4px 0 var(--ivory-side),
    0 5px 0 var(--ivory-side),
    0 7px 10px rgba(0, 0, 0, 0.25);
  filter: brightness(calc(1 - var(--depth-darkness, 0) * 1.2));
  /* Use smooth easing for exit to prevent dipping */
  transition: all 0.25s var(--ease-smooth);
  pointer-events: none;
  z-index: 0;
}

/* Tile Bevel and Reflection (::after pseudo-element) */
.tile::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 6px;
  transform: translateZ(0.1px);
  background: radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.4) 0%, transparent 60%);
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.8),
    inset 0 -2px 4px rgba(0, 0, 0, 0.1);
  pointer-events: none;
  transition: all 0.25s var(--ease-smooth);
}

/* ============================================
   Tile States
   ============================================ */

/* Uncovered tile - enhanced depth */
.tile:not(.covered)::before {
  box-shadow:
    0 1px 0 var(--ivory-side),
    0 2px 0 var(--ivory-side),
    0 3px 0 var(--ivory-side),
    0 4px 0 var(--ivory-side),
    0 5px 0 var(--ivory-side),
    0 8px 12px rgba(0, 0, 0, 0.2);
}

/* Tile Front Layer (Illustration) */
.tile .tile-front {
  position: absolute;
  inset: 0;
  background-image: var(--tile-front-image, none);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  pointer-events: none;
  z-index: 5;
  /* Above base ::before (0) */
  border-radius: 6px;
  /* Ensure it moves with the tile 3D transforms */
  transform: translateZ(0.1px);
}

/* Covered tile */
.tile.covered {
  cursor: not-allowed;
  /* Apply filter here so it affects BOTH base and front layers */
  filter: grayscale(1) brightness(0.8);
}

.tile.covered::before {
  background-color: var(--ivory-side);
  box-shadow:
    0 1px 0 #a8a8a0,
    0 2px 0 #a8a8a0,
    0 3px 0 #a8a8a0,
    0 4px 0 #a8a8a0,
    0 5px 0 #a8a8a0,
    0 6px 8px rgba(0, 0, 0, 0.35);
}

.tile.selected {
  z-index: var(--z-selected);
  transform: translateZ(calc(var(--level) * var(--level-height) + var(--select-lift)));
  transition: transform 0.25s var(--ease-bounce), z-index 0s;
}

.tile.selected::before {
  box-shadow:
    0 1px 0 var(--ivory-side),
    0 2px 0 var(--ivory-side),
    0 3px 0 var(--ivory-side),
    0 4px 0 var(--ivory-side),
    0 5px 0 var(--ivory-side),
    0 25px 40px rgba(0, 0, 0, 0.3);
  filter: brightness(1.1);
}

.tile.selected::after {
  background: radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.6) 0%, transparent 60%);
}

/* Hover state (uncovered tiles only) */
.tile:not(.covered):hover {
  z-index: var(--z-hover);
  transform: translateZ(calc(var(--level) * var(--level-height) + var(--hover-lift)));
  transition: transform 0.25s var(--ease-bounce), z-index 0s;
}

.tile:not(.covered):hover::before {
  transition: all 0.25s var(--ease-bounce);
  box-shadow:
    0 1px 0 var(--ivory-side),
    0 2px 0 var(--ivory-side),
    0 3px 0 var(--ivory-side),
    0 4px 0 var(--ivory-side),
    0 5px 0 var(--ivory-side),
    0 14px 22px rgba(0, 0, 0, 0.2);
  filter: brightness(1.05);
}

.tile:not(.covered):hover::after {
  transition: all 0.25s var(--ease-bounce);
}

/* Active state for better touch feedback */
.tile:not(.covered):active {
  transform: translateZ(calc(var(--level) * var(--level-height) + var(--hover-lift) - 2px)) scale(0.96);
  transition: transform 0.05s ease-out;
}

/* ============================================
   Tile Animations
   ============================================ */

/* Match Success Animation */
@keyframes match-success {
  0% {
    transform: translateZ(calc(var(--level) * var(--level-height))) scale(1);
    opacity: 1;
  }

  30% {
    transform: translateZ(calc(var(--level) * var(--level-height) + 60px)) scale(1.1);
    opacity: 1;
  }

  100% {
    transform: translateZ(calc(var(--level) * var(--level-height) + 200px)) scale(0.8);
    opacity: 0;
    visibility: hidden;
  }
}

.tile.match-success {
  animation: match-success 0.5s cubic-bezier(0.45, 0.05, 0.55, 0.95) forwards;
  z-index: 1000000;
}

/* Faster animation for auto-finish */
.auto-finishing .tile.match-success {
  animation-duration: 0.15s;
}

/* Match Failure Animation */
@keyframes match-failure {

  0%,
  100% {
    transform: translateZ(calc(var(--level) * var(--level-height))) translateX(0);
  }

  20%,
  60% {
    transform: translateZ(calc(var(--level) * var(--level-height))) translateX(-10px);
  }

  40%,
  80% {
    transform: translateZ(calc(var(--level) * var(--level-height))) translateX(10px);
  }
}

.tile.match-failure {
  animation: match-failure 0.4s ease-in-out;
}

/* Shake Animation for Locked Tiles */
@keyframes shake {

  0%,
  100% {
    transform: translateZ(calc(var(--level) * var(--level-height))) rotate(0deg);
  }

  25% {
    transform: translateZ(calc(var(--level) * var(--level-height))) rotate(-3deg);
  }

  75% {
    transform: translateZ(calc(var(--level) * var(--level-height))) rotate(3deg);
  }
}

.tile.locked-shake {
  animation: shake 0.3s ease-in-out;
  cursor: not-allowed;
}

/* Tile Leave Transition */
.tile-leave-active {
  transition: all 0.4s cubic-bezier(0.55, 0, 0.1, 1);
}

.tile-leave-to {
  opacity: 0;
  transform: scale(0.5) translateY(-20px);
}

/* ============================================
   Shuffle Animations
   ============================================ */
@keyframes shuffle-out {
  0% {
    transform: translateZ(0) scale(1) rotate(0deg);
    opacity: 1;
  }

  50% {
    transform: translateZ(100px) scale(0.5) rotate(180deg);
    opacity: 0.5;
  }

  100% {
    transform: translateZ(200px) scale(0) rotate(360deg);
    opacity: 0;
  }
}

@keyframes shuffle-in {
  0% {
    transform: translateZ(200px) scale(0) rotate(-360deg);
    opacity: 0;
  }

  50% {
    transform: translateZ(100px) scale(0.5) rotate(-180deg);
    opacity: 0.5;
  }

  100% {
    transform: translateZ(0) scale(1) rotate(0deg);
    opacity: 1;
  }
}

.tile.shuffling-out {
  animation: shuffle-out 0.4s ease-in forwards;
}

.tile.shuffling-in {
  animation: shuffle-in 0.4s ease-out forwards;
}

.shuffle-overlay {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(50, 115, 220, 0.9);
  color: white;
  padding: 0.5rem 1.5rem;
  border-radius: 2rem;
  font-weight: bold;
  z-index: var(--z-shuffle-overlay);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  font-family: var(--font-display);
}

/* ============================================
   Game Overlays
   ============================================ */
.game-overlay {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: var(--z-overlay);
}

/* ============================================
   Modal Shared Styles
   ============================================ */
.game-modal {
  position: relative;
  width: 90%;
  max-width: 420px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  animation: modal-in 0.35s var(--ease-bounce);
}

@keyframes modal-in {
  0% {
    opacity: 0;
    transform: scale(0.85) translateY(30px);
  }

  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.modal-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2.5rem 2rem 2rem;
  gap: 12px;
}

.modal-top h1 {
  font-family: var(--font-display);
  font-size: 2.2rem;
  font-weight: 900;
  color: #061B43;
  letter-spacing: 1px;
}

.modal-icon-circle {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: #FFFFFF;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
}

.modal-bottom {
  background: #FFFFFF;
  padding: 1.5rem 2rem 2rem;
  text-align: center;
}

.modal-bottom p {
  font-size: 1.05rem;
  color: #555;
  margin-bottom: 1.5rem;
  font-weight: 500;
}

.modal-action-btn {
  display: block;
  width: 100%;
  padding: 16px 24px;
  background-color: #061B43;
  color: #FFFFFF;
  border: none;
  border-radius: 10px;
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: 1px;
  cursor: pointer;
  transition: transform 0.15s ease, background-color 0.15s ease;
}

.modal-action-btn:hover {
  background-color: #0a2a5e;
  transform: translateY(-2px);
}

.modal-action-btn:active {
  transform: translateY(0);
}

/* Victory Modal */
.victory-top {
  background: linear-gradient(135deg, #22AA66, #33cc77);
}

.victory-top .modal-icon-circle {
  color: #22AA66;
}

/* Defeat Modal */
.defeat-top {
  background: linear-gradient(135deg, #EE4433, #f47066);
}

.defeat-top .modal-icon-circle {
  color: #EE4433;
}

/* ============================================
   Auto-Finish Button
   ============================================ */
.button-auto-finish {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-auto-finish);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 42px;
  padding: 0 18px;
  background-color: #FF9900;
  color: #061B43;
  border: none;
  border-radius: 5px;
  font-size: 0.9rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: transform 0.2s ease, background-color 0.2s ease;
  font-family: var(--font-display);
}

.button-auto-finish i {
  font-size: 1.1rem;
}

.button-auto-finish:hover {
  transform: translateX(-50%) translateY(-2px);
  background-color: #ffaa33;
}

.button-auto-finish:active {
  transform: translateX(-50%) translateY(0);
}

/* ============================================
   Game Over / Victory overlay styles
   ============================================ */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* ============================================
   Responsive Styles
   ============================================ */

/* Tablet - 768px */
@media (max-width: 768px) {
  :root {
    --tile-width: 45px;
    --tile-height: 45px;
    --level-height: 8px;
    --hover-lift: 6px;
    --select-lift: 20px;
  }

  .game-header {
    padding: 10px 16px;
  }

  .game-title {
    font-size: 1.3rem;
  }
}

/* Mobile - 600px */
@media (max-width: 600px) {
  :root {
    --tile-width: 38px;
    --tile-height: 38px;
    --level-height: 6px;
    --hover-lift: 4px;
    --select-lift: 15px;
    --board-rotation: 15deg;
  }

  .board {
    gap: 8px;
  }


  .board {
    margin-top: 60px;
    /* Space for fixed header elements */
    margin-bottom: 20px;
  }

  .game-modal {
    width: 92%;
  }

  .modal-top h1 {
    font-size: 1.8rem;
  }

  .modal-bottom p {
    font-size: 0.95rem;
  }
}

/* Small Mobile - 480px */
@media (max-width: 480px) {
  :root {
    --tile-width: 36px;
    --tile-height: 36px;
    --level-height: 6px;
    --hover-lift: 4px;
    --select-lift: 12px;
    --perspective: 800px;
  }

  .board {
    margin-top: 50px;
    gap: 4px;
  }


  .shuffle-overlay {
    font-size: 0.9rem;
    padding: 0.4rem 1rem;
  }
}

/* Reduce animations for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {

  .tile,
  .tile::before,
  .tile::after {
    transition: none;
  }

  .tile.match-success,
  .tile.match-failure,
  .tile.shuffling-out,
  .tile.shuffling-in {
    animation: none;
  }
}