:root {
  --bg: #f8fafc; /* Clean off-white */
  --surface: #ffffff;
  --glass-bg: rgba(255, 255, 255, 0.7);
  --border-light: rgba(0, 0, 0, 0.08);
  --accent-gradient: linear-gradient(135deg, #6366f1 0%, #6366f1 100%);
  --primary-blue: #3b82f6;
  --primary-purple: #8b5cf6;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --editor-bg: #1a1b26; /* Deep dark for editor */
  --terminal-bg: rgba(255, 255, 255, 0.02);
  --grid-cell-border: rgba(0, 0, 0, 0.05);
  --cell-size: 60px;
  --grid-rows: 8;
  --grid-cols: 8;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: "Outfit", sans-serif;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(139, 92, 246, 0.2);
  border-radius: 20px;
  border: 2px solid transparent;
  background-clip: content-box;
  transition: all 0.3s ease;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(139, 92, 246, 0.6);
  background-clip: content-box;
}

body {
  background-color: var(--bg);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 20px;
}

#app {
  width: 100%;
  max-width: 1200px;
  height: 90vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.glass {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: 20px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.05);
}

.shared-header {
  height: 64px;
  padding: 0 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-link {
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.95rem;
  font-weight: 600;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;

  & :hover {
    color: var(--primary-purple);
  }
}

.logo {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -1.2px;

  & .grid {
    color: var(--text-main);
  }

  & .x {
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}

.game-layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 420px;
  gap: 12px;
  min-height: 0;
  position: relative;
}

/* --- Game Grid --- */
#game-panel {
  display: flex;
  flex-direction: column;
  background: #fff;
  overflow: hidden;
  padding: 24px;
}

.game-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  width: 100%;
  padding: 20px 0;
  height: auto;
  border-bottom: 1px solid var(--border-light);
}

.game-panel-header .header-left .level-title {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 6px;
}

.game-panel-header .header-left .level-desc {
  font-size: 0.95rem;
  color: var(--text-muted);
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols), var(--cell-size));
  grid-template-rows: repeat(var(--grid-rows), var(--cell-size));
  border: 1px solid var(--border-light);
  background-image:
    linear-gradient(var(--grid-cell-border) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-cell-border) 1px, transparent 1px);
  background-size: var(--cell-size) var(--cell-size);
  position: relative;
  flex-shrink: 0;
  margin: auto; /* Center in the column layout */
}

#player {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 10;
}

#player img {
  width: calc(var(--cell-size) * 0.7);
  height: calc(var(--cell-size) * 0.7);
  object-fit: contain;
}

/* --- Code Panel --- */
#code-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--editor-bg);
  border: none;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.editor-header {
  background: rgba(255, 255, 255, 0.05);
  padding: 14px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.editor-header h3 {
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.5);
  font-family: "Fira Code", monospace;
  letter-spacing: 1px;
}

.status-dot {
  display: flex;
  gap: 6px;
  padding: 4px;
  cursor: pointer;
}

.status-dot .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.status-dot .dot:nth-child(1) {
  background: #ff5f57;
}

.status-dot .dot:nth-child(2) {
  background: #febc2e;
}

.status-dot .dot:nth-child(3) {
  background: #28c840;
}

.editor-container {
  flex: 1;
  display: flex;
  background: var(--editor-bg);
  min-height: 0;
  overflow: hidden;
}

/* --- CodeMirror GridX Custom Theme --- */
.cm-s-gridx.CodeMirror {
  flex: 1;
  height: 100%;
  font-family: "Fira Code", "Courier New", monospace !important;
  font-size: 15px !important;
  line-height: 1.5 !important;
  background: transparent !important;
  color: #f8fafc;
}

.cm-s-gridx .CodeMirror-gutters {
  background: transparent !important;
  border-right: 1px solid rgba(255, 255, 255, 0.05) !important;
}

.cm-s-gridx .CodeMirror-linenumber {
  color: #4b5563 !important;
  padding: 0 10px !important;
}

/* Enhanced Syntax Highlighting Colors */
.cm-s-gridx .cm-keyword {
  color: #c792ea;
  font-weight: 600;
} /* Soft Purple */
.cm-s-gridx .cm-string {
  color: #c3e88d;
} /* Mint Green */
.cm-s-gridx .cm-number {
  color: #f78c6c;
} /* Peach/Orange */
.cm-s-gridx .cm-def {
  color: #82aaff;
} /* Soft Blue */
.cm-s-gridx .cm-variable {
  color: #f8fafc;
} /* White/Grey */
.cm-s-gridx .cm-variable-2 {
  color: #f07178;
} /* Soft Red */
.cm-s-gridx .cm-property {
  color: #89ddff;
} /* Cyan */
.cm-s-gridx .cm-operator {
  color: #89ddff;
} /* Cyan */
.cm-s-gridx .cm-comment {
  color: #64748b;
} /* Slate Gray */
.cm-s-gridx .cm-punctuation {
  color: #89ddff;
}

/* Editor State */
.cm-s-gridx .CodeMirror-cursor {
  border-left: 2px solid #00d2ff !important;
}
.cm-s-gridx .CodeMirror-selected {
  background: rgba(0, 210, 255, 0.2) !important;
}
.cm-s-gridx .CodeMirror-line::selection,
.cm-s-gridx .CodeMirror-line > span::selection,
.cm-s-gridx .CodeMirror-line > span > span::selection {
  background: rgba(0, 210, 255, 0.2);
}
.cm-s-gridx .CodeMirror-focused {
  outline: none;
}

/* --- Button System --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
  font-family: inherit;
  gap: 8px;
}

.btn-primary {
  background: var(--accent-gradient);
  color: #fff;
  border: none;
}

.btn-primary:hover {
  opacity: 0.9;
}

.btn-danger {
  background: #ef4444;
  color: #fff;
  border: none;
}

.btn-danger:hover {
  background: #dc2626;
}

.btn-outline {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-main);
}

.btn-outline:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

/* --- Panel Controls --- */
.panel-controls {
  display: flex;
  gap: 12px;
  padding: 22px 20px;
  background: rgba(255, 255, 255, 0.02);
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.panel-controls .btn-primary {
  flex: 0 0 auto;
}

.panel-controls .btn-outline {
  flex: 0 0 auto;
}

.output-log {
  height: 140px;
  background: var(--terminal-bg);
  padding: 20px;
  font-family: "Fira Code", monospace;
  font-size: 13px;
  overflow-y: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  transition: all 0.3s ease;
}

.output-log.hidden {
  height: 0;
  padding-block: 0;
  border-top: none;
  overflow: hidden;
}

/* Custom Webkit Scrollbar for Terminal Areas */
.output-log::-webkit-scrollbar,
#code-input::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.output-log::-webkit-scrollbar-track,
#code-input::-webkit-scrollbar-track {
  background: transparent;
}

.output-log::-webkit-scrollbar-thumb,
#code-input::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  border: 2px solid transparent;
  background-clip: content-box;
}

.output-log::-webkit-scrollbar-thumb:hover,
#code-input::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
  background-clip: content-box;
}

.log-info {
  color: #94a3b8;
}
.log-success {
  color: #34d399;
}
.log-error {
  color: #f87171;
}

/* --- Documentation Styles moved to commands.css --- */

/* --- Responsiveness --- */
@media (max-width: 1024px) {
  .game-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 480px;
  }
  #app {
    height: auto;
  }
  :root {
    --cell-size: 52px;
  }
}

@media (max-width: 768px) {
  :root {
    --cell-size: 42px;
  }
  .game-layout {
    grid-template-rows: auto 420px;
  }
}

/* --- Hamburger Menu Logic --- */
.menu-toggle {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 24px;
  height: 18px;
  background: transparent;
  border: none;
  cursor: pointer;
  z-index: 100;
}

.menu-toggle .bar {
  height: 2px;
  width: 100%;
  background-color: var(--text-main);
  border-radius: 4px;
  transition:
    transform 0.3s ease,
    opacity 0.3s ease;
}

@media (max-width: 640px) {
  header {
    height: 80px;
    padding: 0 20px;
    flex-direction: row;
    position: relative;
    z-index: 90;
  }

  :root {
    --cell-size: clamp(28px, 10vw, 40px);
  }
  #game-panel {
    justify-content: center;
    align-items: center;
  }

  .menu-toggle {
    display: flex;
  }

  .header-controls {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    background: var(--surface);
    flex-direction: column;
    padding: 20px;
    gap: 15px;
    align-items: center;
    border: 1px solid var(--border-light);
    border-radius:20px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);

    /* Animation State */
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s ease;
    z-index: 9999;
  }

  .header-controls.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
  }

  /* Hamburger Animation X */
  .menu-toggle.active .bar:nth-child(1) {
    transform: translateY(8px) rotate(45deg);
  }
  .menu-toggle.active .bar:nth-child(2) {
    opacity: 0;
  }
  .menu-toggle.active .bar:nth-child(3) {
    transform: translateY(-8px) rotate(-45deg);
  }
}

/* --- Fullscreen Code Panel (Large Screens Only) --- */
@media (min-width: 1025px) {
  #code-panel.fullscreen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 50;
    background: rgba(26, 27, 38, 0.96); /* Dark base, slightly transparent */
    backdrop-filter: blur(
      8px
    ); /* Optional: blurs the grids behind it to look very nice */
  }

  #code-panel.fullscreen .editor-container {
    background: transparent;
  }
}

footer {
  text-align: center;
  padding: 60px 40px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.footer-link {
  color: var(--primary-purple);
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.footer-link:hover {
  color: var(--primary-blue);
}

/* =============================================
   LEVEL SYSTEM STYLES
   ============================================= */

/* --- Level Info Bar --- */
.level-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  border-radius: 16px;
}

.level-info {
  flex: 1;
  min-width: 0;
}

.level-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.level-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-muted);
}

.level-number {
  font-size: 0.75rem;
  font-weight: 800;
  color: var(--primary-purple);
  background: rgba(139, 92, 246, 0.1);
  padding: 2px 8px;
  border-radius: 6px;
}

.level-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
}

.level-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* --- Level Navigation Buttons --- */
.level-nav-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border-light);
  background: var(--surface);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.level-nav-btn:hover {
  background: #f1f5f9;
  color: var(--primary-purple);
  border-color: rgba(139, 92, 246, 0.3);
}

/* --- Level Dots --- */
.level-dots {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

.level-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--border-light);
  background: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 0;
}

.level-dot:hover {
  border-color: var(--primary-purple);
  transform: scale(1.3);
}

.level-dot.active {
  background: var(--primary-purple);
  border-color: var(--primary-purple);
  transform: scale(1.2);
  box-shadow: 0 0 8px rgba(139, 92, 246, 0.4);
}

.level-dot.completed {
  background: #34d399;
  border-color: #34d399;
}

/* --- Level Actions --- */
.level-actions {
  flex-shrink: 0;
}

.btn-hint {
  background: rgba(139, 92, 246, 0.08);
  color: var(--primary-purple);
  border: 1px solid rgba(139, 92, 246, 0.15);
  font-size: 0.8rem;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-family: inherit;
  transition: all 0.2s ease;
}

.btn-hint:hover {
  background: rgba(139, 92, 246, 0.15);
  border-color: rgba(139, 92, 246, 0.3);
}

/* --- Grid Elements: Walls, Goals, Items, Boxes --- */
.grid-wall,
.grid-goal,
.grid-item,
.grid-box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: var(--cell-size);
  height: var(--cell-size);
  position: relative;
}

.grid-wall img,
.grid-goal img,
.grid-item img,
.grid-box img {
  width: calc(var(--cell-size) * 0.75);
  height: calc(var(--cell-size) * 0.75);
  object-fit: contain;
}

.grid-wall {
  background: repeating-linear-gradient(
    45deg,
    #f1f5f9,
    #f1f5f9 10px,
    #e2e8f0 10px,
    #e2e8f0 20px
  );
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.2);
}

.grid-box {
  transition:
    opacity 0.5s ease,
    transform 0.5s cubic-bezier(0.4, 0, 0.2, 1),
    grid-column 0.5s cubic-bezier(0.4, 0, 0.2, 1),
    grid-row 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  background: rgba(139, 92, 246, 0.05); /* optional subtle styling */
}

.grid-goal {
  animation: goalPulse 2s ease-in-out infinite;
}

.grid-goal.locked {
  animation: none;
  opacity: 0.7;
}

.grid-item {
  transition:
    opacity 0.4s ease,
    transform 0.4s ease;
  animation: itemFloat 2.5s ease-in-out infinite;
}

@keyframes goalPulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

@keyframes itemFloat {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-4px);
  }
}

/* --- Win Overlay --- */
.win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.4s ease;
}

.win-overlay.active {
  opacity: 1;
  visibility: visible;
}

.win-card {
  text-align: center;
  padding: 48px 56px;
  border-radius: 24px;
  max-width: 400px;
  width: 90%;
  transform: scale(0.8) translateY(30px);
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.win-overlay.active .win-card {
  transform: scale(1) translateY(0);
}

.win-emoji {
  font-size: 4rem;
  margin-bottom: 16px;
  animation: bounceIn 0.6s ease;
}

.win-card h2 {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text-main);
  margin-bottom: 8px;
}

.win-card p {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 0.95rem;
}

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

/* --- Level Bar Responsive --- */
@media (max-width: 768px) {
  .level-bar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 14px;
  }
  .level-dots {
    order: 10;
    width: 100%;
    justify-content: center;
  }
  .level-title {
    font-size: 0.95rem;
  }
  .level-desc {
    font-size: 0.75rem;
  }
  .level-actions {
    display: none;
  }
}

@media (max-width: 640px) {
  .level-bar {
    padding: 8px 12px;
    border-radius: 12px;
  }
  .level-nav-btn {
    width: 30px;
    height: 30px;
    border-radius: 8px;
  }
  .level-dot {
    width: 8px;
    height: 8px;
  }
  .win-card {
    padding: 32px 24px;
  }
  .win-emoji {
    font-size: 3rem;
  }
}
