/* El Código (The Code) Styles */

/* =========================================
   Layout Inheritance (Matches Search/Sudoku)
   ========================================= */

/* Main Layout Stack */
.code-mode .memory-grid-layout {
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: flex-start !important;
  gap: 15px !important;
  width: 100% !important;
  max-width: 1000px !important;
  margin: 0 auto !important;
  padding-top: 1rem !important;

  --sudoku-grid-line: #000;
  --sudoku-inner-line: #bbb;
}

.dark-mode .code-mode .memory-grid-layout {
  --sudoku-grid-line: #fff;
  --sudoku-inner-line: #444;
}

/* Board Styling - Ensure it stays exactly like Search/Sudoku */
.code-mode .memory-board {
  background: var(--bg-secondary) !important;
  padding: 0 !important;
  gap: 0 !important;
  border: 4px solid var(--bg-secondary) !important;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 1/1 !important;
}

/* Remove internal rounding */
.code-mode .sudoku-chunk-slot,
.code-mode .mini-sudoku-grid {
  border-radius: 0 !important;
  box-sizing: border-box !important;
}

/* Internal Grid Borders */
.code-mode .sudoku-chunk-slot {
  border-right: 3px solid var(--sudoku-grid-line);
  border-bottom: 3px solid var(--sudoku-grid-line);
}

.code-mode .sudoku-chunk-slot:nth-child(3n) {
  border-right: none !important;
}

.code-mode .sudoku-chunk-slot:nth-child(n + 7) {
  border-bottom: none !important;
}

/* Hide Unused Elements (Jigsaw Panels, Cards) */
.code-mode .collected-zone,
.code-mode .collected-wrapper,
.code-mode .cards-area {
  display: none !important;
}

/* Center Board Wrapper */
.code-mode .board-wrapper {
  order: 1 !important;
  width: auto !important;
  margin: 0 !important;
  padding: 0 !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
}

/* Mobile Responsive Adjustments (Same as Search) */
@media (max-width: 768px) {
  .code-mode .memory-grid-layout {
    flex: 1 !important;
    min-height: 0;
    width: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: flex-start !important;
    align-items: center !important;
    gap: 8px !important;
    padding-bottom: 2px !important;
    max-height: calc(
      100dvh - var(--header-height) - var(--footer-height)
    ) !important;
    overflow: hidden !important;
  }

  .code-mode .memory-board {
    --v-budget: calc(100dvh - var(--header-height) - var(--footer-height) - 130px);
    width: min(95vw, var(--v-budget));
    max-height: var(--v-budget);
  }
}

@media (min-width: 769px) {
  .code-mode .memory-board {
    width: min(420px, 45vw) !important;
    height: auto !important;
    aspect-ratio: 1/1 !important;
  }
}

/* =========================================
   Interactive Elements
   ========================================= */

/* Active Cell Logic */

/* 1. Base disabled state for ALL cells in code mode */
.code-mode .mini-cell {
  background-color: transparent !important; /* Let board bg show through or dark */
  color: #555 !important;
  border-color: #333 !important;
  opacity: 0.6;
  pointer-events: none; /* Disable interaction on non-game cells */
}

.dark-mode .code-mode .mini-cell {
  color: #444 !important;
  border-color: #222 !important;
}

/* 2. Active Game Cells (Survivors) */
/* 2. Active Game Cells (Survivors) */
.code-mode .mini-cell.code-cell {
  background-color: var(--bg-secondary) !important;
  background: #e2e8f0 !important; /* Light neutral gray */
  color: #000000 !important; /* Light mode: Black text */
  text-shadow: none;
  font-weight: 800;
  opacity: 1 !important;
  pointer-events: auto !important;
  border: 2px solid #000000 !important; /* Black border instead of orange */
  z-index: 5;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.5) !important;
  transition: color 0.4s ease;
}

/* Return to theme color during win animation */
.code-mode .victory-final .code-cell {
  color: var(--text-main) !important;
}

.dark-mode .code-mode .mini-cell.code-cell {
  background: rgba(255, 255, 255, 0.15) !important;
  border-color: white !important;
  color: #ffffff !important; /* Dark mode: White text */
  text-shadow: 0 0 2px black;
}

/* Override previous game styles (Peaks/Valleys) to be colorless in Code Mode */
.code-mode .peak-found,
.code-mode .valley-found {
  background-color: transparent !important;
  background: transparent !important;
  color: #555 !important;
  border-color: #333 !important;
  box-shadow: none !important;
}

.dark-mode .code-mode .peak-found,
.dark-mode .code-mode .valley-found {
  color: #444 !important;
  border-color: #222 !important;
}

.code-cell {
  transition: all 0.2s ease;
  cursor: pointer;
  font-weight: bold;
}

.code-cell:active {
  transform: scale(0.9);
}

/* Active Blink State (Simon Says) */
/* High Specificity to override .code-cell !important rules */
.code-mode .mini-cell.simon-active {
  background-color: var(--accent-color) !important; /* Orange */
  color: white !important;
  box-shadow: 0 0 25px var(--accent-color) !important;
  transform: scale(1.15) !important;
  z-index: 20 !important;
  border-color: white !important;
  text-shadow: 0 0 4px black;
}

.dark-mode .code-mode .mini-cell.simon-active {
  background-color: var(--accent-color) !important;
  box-shadow: 0 0 30px var(--accent-color) !important;
  color: white !important;
}

/* Error State */
@keyframes shake-simon {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-5px);
  }
  75% {
    transform: translateX(5px);
  }
}

.simon-error {
  animation: shake-simon 0.4s ease-in-out;
  background-color: #ff3333 !important;
  box-shadow: 0 0 15px #ff0000;
  color: white !important;
}

/* Win State */
.simon-win {
  background-color: #ffd700 !important; /* Gold */
  color: #000 !important;
  box-shadow: 0 0 20px #ffd700;
  animation: pulse-gold 1s infinite alternate;
}

@keyframes pulse-gold {
  from {
    box-shadow: 0 0 10px #ffd700;
  }
  to {
    box-shadow: 0 0 25px #ffd700;
  }
}
/* =========================================
   Victory Animation (Disintegration + Glitch)
   ========================================= */

/* Disintegration Effect - Board & Non-Code Cells */
.disintegrate {
  animation: disintegrate 1.5s forwards ease-in;
  pointer-events: none;
}

@keyframes disintegrate {
  0% {
    opacity: 1;
    transform: scale(1) translate(0, 0);
    filter: blur(0);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.05);
    filter: blur(2px);
  }
  100% {
    opacity: 0;
    transform: scale(2);
    filter: blur(10px);
  }
}

/* Central Overlay for the Final Code */
.victory-code-container {
  position: absolute;
  top: 35%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  gap: 2px;
  z-index: 1000;
  pointer-events: none;
}

.victory-code-cell {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent-color); /* Orange default */
  background: transparent;
  text-shadow: 0 0 5px var(--accent-color); /* Reduced from 10px */
  font-family: "Courier New", monospace; /* Matrix style */
  /* Remove transform transition to avoid conflict with animation */
  transition:
    color 0.3s ease,
    text-shadow 0.3s ease;

  /* Hardware Acceleration Hints */
  will-change: transform; /* Removed text-shadow from will-change */
  backface-visibility: hidden;
  transform: translateZ(0);

  /* Text Rendering Optimization for Scaling */
  text-rendering: geometricPrecision;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Flash White on Lock-in */
/* Set specific locked state with strong shadow static */
.victory-code-cell.locked {
  color: var(--accent-color);
  /* Base static shadow (doesn't animate) */
  /* Base static shadow (doesn't animate) */
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.5),
    0 0 5px var(--accent-color); /* Dark drop shadow + smaller glow */
  position: relative; /* Ensure it can hold abs pseudo */

  animation:
    lock-flash 0.4s ease-out forwards,
    pulse-scale-only 2s infinite alternate 0.4s;
}

.dark-mode .victory-code-cell.locked {
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.3),
    0 0 5px var(--accent-color); /* More subtle definition */
}

/* The Pulsing Glow Layer - GPU Optimized */
.victory-code-cell.locked::before {
  content: attr(data-content);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;

  /* Center the Content (Glow Text) to match Parent */
  display: flex;
  justify-content: center;
  align-items: center;

  /* The Glow Itself */
  color: transparent;
  text-shadow:
    0 0 20px var(--accent-color),
    0 0 40px var(--accent-color); /* Slightly reduced from 30/50 */

  opacity: 0; /* Start invisible */
  pointer-events: none;
  z-index: -1; /* Behind or mixed? Text shadow usually on top for 'glow'. Let's try z-1 or top. */
  /* If z-1, it might be behind background if any. Here bg is transparent so it works. */

  will-change: opacity;
  animation: pulse-glow-opacity 2s infinite alternate 0.4s;
}

@keyframes lock-flash {
  0% {
    color: #fff;
    text-shadow: 0 0 20px #fff;
    transform: scale3d(1.5, 1.5, 1);
  }
  100% {
    color: var(--accent-color);
    /* Base Shadow Only */
    /* Base Shadow Only */
    text-shadow:
      0 1px 2px rgba(0, 0, 0, 0.5),
      0 0 5px var(--accent-color);
    transform: scale3d(1, 1, 1);
  }
}

/* Optimization: Opacity is cheap! Pulse the overlay */
@keyframes pulse-glow-opacity {
  from {
    opacity: 0.3;
  }
  to {
    opacity: 1;
  }
}

/* Scale text subtly too, via main element (cheap transform) */
@keyframes pulse-scale-only {
  from {
    transform: scale3d(1, 1, 1);
  }
  to {
    transform: scale3d(1.1, 1.1, 1);
  }
}

/* Glitch Effect */
.glitching {
  animation: glitch-anim 0.2s infinite;
  color: #0f0 !important; /* Matrix Green during glitch */
  text-shadow:
    2px 0 #f00,
    -2px 0 #00f;
}

@keyframes glitch-anim {
  0% {
    transform: translate(0);
  }
  20% {
    transform: translate(-2px, 2px);
  }
  40% {
    transform: translate(-2px, -2px);
  }
  60% {
    transform: translate(2px, 2px);
  }
  80% {
    transform: translate(2px, -2px);
  }
  100% {
    transform: translate(0);
  }
}

/* Spawning extra characters smoothly */
.spawn-in {
  animation: spawn-pop 0.5s ease-out forwards;
}

@keyframes spawn-pop {
  0% {
    transform: scale(0);
    opacity: 0;
    width: 0;
  }
  60% {
    transform: scale(1.2);
    opacity: 1;
    width: 60px;
  }
  100% {
    transform: scale(1);
    opacity: 1;
    width: 60px;
  }
}

/* Final Victory Text State */
/* Final Victory Text State */
.victory-final {
  /* Animation handled by .locked combined class now for better entrance */
  font-family: "Montserrat", sans-serif; /* Back to clean font */
}
