/* ============================================
   Arrow Escape - Style Sheet
   Dark, geometric, bold theme
   ============================================ */

/* ============================================
   CSS Custom Properties (Color Palette)
   ============================================ */
:root {
  /* Backgrounds */
  --bg:           #0e0f14;       /* page background */
  --board-bg:     #16181f;       /* board surface */
  --tile-empty:   #1e2028;       /* empty cell */
  --tile-border:  #2a2d38;       /* tile border */
  
  /* Arrow Colors */
  --arrow-up:     #4fc3f7;       /* cool blue */
  --arrow-right:  #ffb74d;       /* warm amber */
  --arrow-down:   #ef5350;       /* coral red */
  --arrow-left:   #66bb6a;       /* green */
  
  /* Text */
  --text-primary: #e8eaf6;
  --text-muted:   #5c6080;
  
  /* UI */
  --accent:       #7c4dff;       /* purple accent */
  --success:      #69f0ae;
  --endless-gold: #ffd54f;
  
  /* Typography */
  --font-title: 'Rajdhani', sans-serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;
  
  /* Tile Sizes by Grid */
  --tile-4x4: 80px;
  --tile-5x5: 72px;
  --tile-6x6: 64px;
  --tile-7x7: 56px;
  --tile-8x8: 50px;
  --tile-9x9: 44px;
  --tile-10x10: 40px;
  
  /* Gap Sizes */
  --gap-small: 4px;
  --gap-medium: 6px;
  
  /* Arrow Font Sizes */
  --arrow-4x4: 2rem;
  --arrow-5x5: 1.7rem;
  --arrow-6x6: 1.4rem;
  --arrow-7x7: 1.2rem;
  --arrow-8x8: 1rem;
  --arrow-9x9: 0.9rem;
  --arrow-10x10: 0.85rem;
}

/* ============================================
   Global Reset & Base Styles
   ============================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  width: 100%;
}

body {
  font-family: var(--font-title);
  background-color: var(--bg);
  color: var(--text-primary);
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  overflow: hidden;
}

/* ============================================
   Utility Classes
   ============================================ */
.hidden {
  display: none !important;
}

/* ============================================
   Loading Overlay
   ============================================ */
#loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: var(--bg);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.loading-content {
  text-align: center;
}

.loading-title {
  font-family: var(--font-title);
  font-size: 3rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2rem;
  letter-spacing: 0.1em;
}

.loading-status {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.loading-bar-container {
  width: 300px;
  height: 8px;
  background-color: var(--tile-border);
  border-radius: 4px;
  overflow: hidden;
  margin: 0 auto;
}

.loading-bar {
  height: 100%;
  width: 0%;
  background-color: var(--accent);
  transition: width 0.2s ease;
}

/* ============================================
   Game Container
   ============================================ */
#game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  width: 100%;
  max-width: 100%;
}

/* ============================================
   Game Header
   ============================================ */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 800px;
  margin-bottom: 16px;
  padding: 0 8px;
}

.game-title {
  font-family: var(--font-title);
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 0.08em;
  white-space: nowrap;
}

/* ============================================
   Stats Bar
   ============================================ */
.stats-bar {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 20px 30px;
  padding: 12px 20px;
  background-color: var(--board-bg);
  border-radius: 8px;
  margin-bottom: 20px;
  border: 1px solid var(--tile-border);
  width: 100%;
  max-width: 800px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-label {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text-muted);
}

.stat-value {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--text-primary);
  min-width: 45px;
  text-align: right;
}

/* ============================================
   Endless Badge
   ============================================ */
.endless-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background-color: var(--endless-gold);
  color: var(--bg);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  margin-left: 8px;
}

/* ============================================
   Board Container
   ============================================ */
.board-container {
  display: grid;
  gap: var(--gap-medium);
  background-color: var(--board-bg);
  padding: 16px;
  border-radius: 12px;
  border: 2px solid var(--tile-border);
  margin-bottom: 20px;
  transition: border-color 0.3s ease;
  /* Ensure board fits within viewport */
  max-width: calc(100vw - 40px);
  width: fit-content;
  margin-left: auto;
  margin-right: auto;
}

/* Endless mode pulsing border */
.board-container.endless-mode {
  animation: endless-pulse 3s ease-in-out infinite;
}

@keyframes endless-pulse {
  0%, 100% {
    border-color: var(--tile-border);
    box-shadow: 0 0 10px rgba(124, 77, 255, 0.1);
  }
  50% {
    border-color: var(--endless-gold);
    box-shadow: 0 0 20px rgba(255, 213, 79, 0.3);
  }
}

/* Responsive tile sizing using min() to ensure fit */
:root {
  /* Calculate max tile size that fits in viewport for each grid size */
  /* 4x4: (100vw - 80px padding) / 4 - gap */
  --tile-4x4-responsive: min(80px, calc((100vw - 80px) / 4 - 6px));
  --tile-5x5-responsive: min(72px, calc((100vw - 80px) / 5 - 6px));
  --tile-6x6-responsive: min(64px, calc((100vw - 80px) / 6 - 6px));
  --tile-7x7-responsive: min(56px, calc((100vw - 80px) / 7 - 4px));
  --tile-8x8-responsive: min(50px, calc((100vw - 80px) / 8 - 4px));
  --tile-9x9-responsive: min(44px, calc((100vw - 80px) / 9 - 4px));
  --tile-10x10-responsive: min(40px, calc((100vw - 80px) / 10 - 4px));
}

/* Grid size classes with responsive tiles */
.board-container.grid-4 {
  grid-template-columns: repeat(4, var(--tile-4x4-responsive));
  gap: var(--gap-medium);
}

.board-container.grid-5 {
  grid-template-columns: repeat(5, var(--tile-5x5-responsive));
  gap: var(--gap-medium);
}

.board-container.grid-6 {
  grid-template-columns: repeat(6, var(--tile-6x6-responsive));
  gap: var(--gap-medium);
}

.board-container.grid-7 {
  grid-template-columns: repeat(7, var(--tile-7x7-responsive));
  gap: var(--gap-small);
}

.board-container.grid-8 {
  grid-template-columns: repeat(8, var(--tile-8x8-responsive));
  gap: var(--gap-small);
}

.board-container.grid-9 {
  grid-template-columns: repeat(9, var(--tile-9x9-responsive));
  gap: var(--gap-small);
}

.board-container.grid-10 {
  grid-template-columns: repeat(10, var(--tile-10x10-responsive));
  gap: var(--gap-small);
}

/* ============================================
   Tile Styles
   ============================================ */
.tile {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  border: 2px solid var(--tile-border);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.15s ease;
  user-select: none;
}

.tile:hover {
  transform: scale(1.03);
}

.tile.empty {
  background-color: var(--tile-empty);
  border-color: var(--tile-border);
  cursor: default;
}

.tile.empty:hover {
  transform: none;
  box-shadow: none;
}

/* Arrow tile backgrounds (15% opacity) */
.tile.arrow-up {
  background-color: rgba(79, 195, 247, 0.15);
}

.tile.arrow-up:hover {
  box-shadow: 0 0 15px rgba(79, 195, 247, 0.6);
  border-color: var(--arrow-up);
}

.tile.arrow-right {
  background-color: rgba(255, 183, 77, 0.15);
}

.tile.arrow-right:hover {
  box-shadow: 0 0 15px rgba(255, 183, 77, 0.6);
  border-color: var(--arrow-right);
}

.tile.arrow-down {
  background-color: rgba(239, 83, 80, 0.15);
}

.tile.arrow-down:hover {
  box-shadow: 0 0 15px rgba(239, 83, 80, 0.6);
  border-color: var(--arrow-down);
}

.tile.arrow-left {
  background-color: rgba(102, 187, 106, 0.15);
}

.tile.arrow-left:hover {
  box-shadow: 0 0 15px rgba(102, 187, 106, 0.6);
  border-color: var(--arrow-left);
}

/* Arrow icons - responsive sizing */
.tile .arrow-icon {
  font-size: clamp(1rem, 2.5vw, var(--arrow-4x4));
  line-height: 1;
  pointer-events: none;
}

.grid-5 .tile .arrow-icon {
  font-size: clamp(0.9rem, 2.2vw, var(--arrow-5x5));
}

.grid-6 .tile .arrow-icon {
  font-size: clamp(0.8rem, 2vw, var(--arrow-6x6));
}

.grid-7 .tile .arrow-icon {
  font-size: clamp(0.75rem, 1.8vw, var(--arrow-7x7));
}

.grid-8 .tile .arrow-icon {
  font-size: clamp(0.7rem, 1.6vw, var(--arrow-8x8));
}

.grid-9 .tile .arrow-icon {
  font-size: clamp(0.65rem, 1.5vw, var(--arrow-9x9));
}

.grid-10 .tile .arrow-icon {
  font-size: clamp(0.6rem, 1.4vw, var(--arrow-10x10));
}

/* Direction-specific arrow colors */
.tile.arrow-up .arrow-icon {
  color: var(--arrow-up);
}

.tile.arrow-right .arrow-icon {
  color: var(--arrow-right);
}

.tile.arrow-down .arrow-icon {
  color: var(--arrow-down);
}

.tile.arrow-left .arrow-icon {
  color: var(--arrow-left);
}

/* ============================================
   Action Bar
   ============================================ */
.action-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

/* ============================================
   Button Styles
   ============================================ */
.btn {
  font-family: var(--font-title);
  font-size: 1rem;
  font-weight: 600;
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background-color: var(--tile-border);
  color: var(--text-primary);
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--text-muted);
  transform: translateY(-2px);
}

.btn-accent {
  background-color: var(--accent);
  color: var(--text-primary);
}

.btn-accent:hover:not(:disabled) {
  background-color: #8b6dff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(124, 77, 255, 0.4);
}

.btn-secondary {
  background-color: transparent;
  color: var(--text-primary);
  border: 2px solid var(--tile-border);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-2px);
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 2rem;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  transition: color 0.15s ease;
}

.btn-icon:hover {
  color: var(--text-primary);
}

/* ============================================
   Overlay Styles
   ============================================ */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(14, 15, 20, 0.95);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 500;
}

.overlay-content {
  background-color: var(--board-bg);
  border-radius: 12px;
  padding: 30px;
  max-width: 800px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  border: 2px solid var(--tile-border);
}

.overlay-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.overlay-header h2 {
  font-family: var(--font-title);
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* ============================================
   Level Select Grid
   ============================================ */
.level-section {
  margin-bottom: 24px;
}

.section-title {
  font-family: var(--font-title);
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 8px;
}

.level-btn {
  aspect-ratio: 1;
  background-color: var(--tile-empty);
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 2px;
}

.level-btn:hover {
  border-color: var(--text-muted);
  color: var(--text-primary);
  transform: translateY(-2px);
}

.level-btn.current {
  border-color: var(--accent);
  background-color: rgba(124, 77, 255, 0.15);
  color: var(--accent);
}

.level-btn.completed {
  border-color: var(--success);
  color: var(--success);
}

.level-btn.completed::after {
  content: '✓';
  font-size: 0.7rem;
}

.level-btn .stars {
  font-size: 0.6rem;
  color: var(--endless-gold);
}

.endless-grid {
  grid-template-columns: repeat(10, 1fr);
  overflow-x: auto;
  padding-bottom: 8px;
}

/* ============================================
   Level Complete Overlay
   ============================================ */
.level-complete-content {
  text-align: center;
  animation: slide-up 0.35s ease-out;
}

@keyframes slide-up {
  from {
    transform: translateY(50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.stage-clear-title {
  font-family: var(--font-title);
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--success);
  margin-bottom: 8px;
  letter-spacing: 0.1em;
}

.completed-stage {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.completion-stats {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-bottom: 20px;
}

.completion-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-icon {
  font-size: 1.5rem;
}

.completion-stat .stat-label {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.completion-stat .stat-value {
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--text-primary);
}

/* Star Rating */
.star-rating {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;
}

.star {
  font-size: 2rem;
  color: var(--tile-border);
  transition: color 0.2s ease;
}

.star.earned {
  color: var(--endless-gold);
}

.next-level-status {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.completion-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

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

/* Slide-out animations - extended distance for full board travel */
@keyframes slide-up {
  to {
    transform: translateY(-400%);
    opacity: 0;
  }
}

@keyframes slide-right {
  to {
    transform: translateX(400%);
    opacity: 0;
  }
}

@keyframes slide-down {
  to {
    transform: translateY(400%);
    opacity: 0;
  }
}

@keyframes slide-left {
  to {
    transform: translateX(-400%);
    opacity: 0;
  }
}

.tile.slide-up {
  animation: slide-up 0.24s ease-in forwards;
}

.tile.slide-right {
  animation: slide-right 0.24s ease-in forwards;
}

.tile.slide-down {
  animation: slide-down 0.24s ease-in forwards;
}

.tile.slide-left {
  animation: slide-left 0.24s ease-in forwards;
}

/* Shake animation for blocked moves */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.tile.shake {
  animation: shake 0.3s ease-in-out;
}

/* Hint pulse animation */
@keyframes hint-pulse {
  0%, 100% {
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.4);
    border-color: rgba(255, 255, 255, 0.8);
  }
  50% {
    box-shadow: 0 0 25px rgba(255, 255, 255, 0.8);
    border-color: rgba(255, 255, 255, 1);
  }
}

.tile.hint {
  animation: hint-pulse 0.5s ease-in-out infinite;
}

/* ============================================
   Background Progress Bar (Level Generation)
   ============================================ */
.bg-progress-container {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background-color: var(--board-bg);
  border-radius: 8px;
  border: 1px solid var(--tile-border);
  margin-top: 16px;
}

.bg-progress-label {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.bg-progress-bar-container {
  flex: 1;
  height: 6px;
  background-color: var(--tile-border);
  border-radius: 3px;
  overflow: hidden;
}

.bg-progress-bar {
  height: 100%;
  width: 5%;
  background-color: var(--accent);
  transition: width 0.2s ease;
}

.bg-progress-text {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 50px;
  text-align: right;
}

/* ============================================
   Explosion Effect
   ============================================ */
.explosion {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}

.explosion::before {
  content: '💥';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 2rem;
  animation: explode 0.4s ease-out forwards;
}

@keyframes explode {
  0% {
    transform: translate(-50%, -50%) scale(0.5);
    opacity: 1;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.2);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(1.5);
    opacity: 0;
  }
}

/* ============================================
   Responsive Adjustments
   ============================================ */
@media (max-width: 768px) {
  body {
    overflow-x: hidden;
  }

  #game-container {
    padding: 12px 8px;
  }

  .game-header {
    max-width: 100%;
    padding: 0 4px;
  }

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

  #level-select-btn {
    padding: 8px 12px;
    font-size: 0.85rem;
  }

  .stats-bar {
    gap: 15px 20px;
    padding: 10px 12px;
    max-width: 100%;
  }

  .stat-label {
    font-size: 0.75rem;
  }

  .stat-value {
    font-size: 0.95rem;
    min-width: 40px;
  }

  /* Board container responsive for mobile */
  .board-container {
    max-width: calc(100vw - 24px);
    padding: 12px;
  }

  .action-bar {
    gap: 8px;
    width: 100%;
    max-width: 400px;
  }

  .action-bar .btn {
    flex: 1 1 auto;
    min-width: 70px;
    padding: 8px 10px;
    font-size: 0.85rem;
  }

  .level-grid {
    grid-template-columns: repeat(5, 1fr);
  }

  .overlay-content {
    width: 95%;
    padding: 20px;
  }

  .completion-stats {
    gap: 15px;
  }

  .completion-stat .stat-label {
    font-size: 0.7rem;
  }

  .completion-stat .stat-value {
    font-size: 1rem;
  }
}

/* Extra small screens */
@media (max-width: 480px) {
  .game-title {
    font-size: 1.2rem;
  }

  .stats-bar {
    gap: 10px 15px;
    padding: 8px 10px;
  }

  .stat-label {
    font-size: 0.7rem;
  }

  .stat-value {
    font-size: 0.85rem;
    min-width: 35px;
  }

  /* Board container for very small screens */
  .board-container {
    max-width: calc(100vw - 20px);
    padding: 8px;
  }

  .level-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .action-bar {
    flex-wrap: wrap;
  }

  .action-bar .btn {
    flex: 1 1 40%;
    font-size: 0.8rem;
    padding: 8px 6px;
  }
}
