@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

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

:root {
  --bg: #0f0f1a;
  --bg2: #1a1a2e;
  --grid-line: #2a2a4a;
  --cell-size: 64px;
  --grid-cols: 8;
  --grid-rows: 8;
  --accent: #7c5cfc;
  --accent-glow: #a78bfa;
  --text: #e0def4;
  --text-dim: #6e6a86;
  --panel-bg: #16162a;
  --panel-border: #2a2a4a;
  --success: #50fa7b;
  --danger: #ff5555;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  user-select: none;
}

/* ===== Layout ===== */
#app {
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

/* ===== Toolbar Panel ===== */
#toolbar {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-width: 80px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

#toolbar h2 {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 4px;
}

.tool-btn {
  width: 56px;
  height: 56px;
  border: 2px solid var(--panel-border);
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  position: relative;
}

.tool-btn:hover {
  border-color: var(--accent);
  background: rgba(124, 92, 252, 0.1);
  transform: translateY(-2px);
}

.tool-btn.active {
  border-color: var(--accent);
  background: rgba(124, 92, 252, 0.2);
  box-shadow: 0 0 16px rgba(124, 92, 252, 0.3);
}

.tool-btn canvas {
  width: 36px;
  height: 36px;
}

.tool-btn .tool-label {
  position: absolute;
  bottom: -18px;
  font-size: 8px;
  color: var(--text-dim);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.2s;
}

.tool-btn:hover .tool-label { opacity: 1; }

/* ===== Game Container ===== */
#game-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
}

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: calc(var(--cell-size) * var(--grid-cols) + 2px);
}

#header h1 {
  font-size: 20px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-glow), #f472b6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#level-info {
  font-size: 13px;
  color: var(--text-dim);
}

/* ===== Grid Area ===== */
#grid-area {
  position: relative;
  width: calc(var(--cell-size) * var(--grid-cols) + 2px);
  height: calc(var(--cell-size) * var(--grid-rows) + 2px);
  border: 1px solid var(--grid-line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg2);
  box-shadow: 0 0 60px rgba(124, 92, 252, 0.08), 0 8px 32px rgba(0,0,0,0.5);
}

#grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols), var(--cell-size));
  grid-template-rows: repeat(var(--grid-rows), var(--cell-size));
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
}

.cell {
  border: 1px solid var(--grid-line);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}

.cell:hover {
  background: rgba(124, 92, 252, 0.06);
}

.cell.has-element {
  cursor: pointer;
}

.cell.fixed {
  cursor: default;
}

/* ===== Element Icons ===== */
.cell-icon {
  width: 48px;
  height: 48px;
  pointer-events: none;
}

/* ===== Source (lamp) ===== */
.source-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: radial-gradient(circle, #fff 20%, #fbbf24 60%, transparent 100%);
  box-shadow: 0 0 20px #fbbf24, 0 0 40px rgba(251,191,36,0.4);
  animation: pulse-source 2s ease-in-out infinite;
}

@keyframes pulse-source {
  0%, 100% { box-shadow: 0 0 20px #fbbf24, 0 0 40px rgba(251,191,36,0.4); }
  50% { box-shadow: 0 0 30px #fbbf24, 0 0 60px rgba(251,191,36,0.6); }
}

/* ===== Target (receiver) ===== */
.target-icon {
  width: 36px;
  height: 36px;
  border: 3px solid #555;
  border-radius: 50%;
  position: relative;
  transition: all 0.4s ease;
}

.target-icon::after {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #333;
  transition: all 0.4s ease;
}

.target-icon.activated {
  border-color: var(--success);
  box-shadow: 0 0 20px var(--success), 0 0 40px rgba(80, 250, 123, 0.3);
  animation: target-glow 1s ease-in-out infinite;
}

.target-icon.activated::after {
  background: var(--success);
  box-shadow: 0 0 10px var(--success);
}

.target-icon.target-red { border-color: #ef4444; }
.target-icon.target-red::after { background: #ef4444; }
.target-icon.target-red.activated { border-color: #ef4444; box-shadow: 0 0 20px #ef4444, 0 0 40px rgba(239,68,68,0.3); }
.target-icon.target-red.activated::after { background: #ef4444; box-shadow: 0 0 10px #ef4444; }

.target-icon.target-green { border-color: #22c55e; }
.target-icon.target-green::after { background: #22c55e; }
.target-icon.target-green.activated { border-color: #22c55e; box-shadow: 0 0 20px #22c55e, 0 0 40px rgba(34,197,94,0.3); }
.target-icon.target-green.activated::after { background: #22c55e; box-shadow: 0 0 10px #22c55e; }

.target-icon.target-blue { border-color: #3b82f6; }
.target-icon.target-blue::after { background: #3b82f6; }
.target-icon.target-blue.activated { border-color: #3b82f6; box-shadow: 0 0 20px #3b82f6, 0 0 40px rgba(59,130,246,0.3); }
.target-icon.target-blue.activated::after { background: #3b82f6; box-shadow: 0 0 10px #3b82f6; }

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

/* ===== Canvas overlay for beams ===== */
#beam-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 10;
}

/* ===== Buttons ===== */
#controls {
  display: flex;
  gap: 12px;
  width: calc(var(--cell-size) * var(--grid-cols) + 2px);
}

.btn {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: var(--panel-bg);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:hover {
  border-color: var(--accent);
  background: rgba(124, 92, 252, 0.15);
  transform: translateY(-1px);
}

.btn-success {
  border-color: var(--success);
  color: var(--success);
  box-shadow: 0 0 16px rgba(80,250,123,0.15);
  animation: btn-appear 0.4s ease;
}

.btn-success:hover {
  background: rgba(80,250,123,0.15);
  border-color: var(--success);
}

@keyframes btn-appear {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: rgba(255,85,85,0.15); border-color: var(--danger); }

#next-btn { display: none; }

/* ===== Level Complete Overlay ===== */
#win-overlay {
  display: none;
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(15, 15, 26, 0.7);
  backdrop-filter: blur(4px);
  z-index: 20;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
}

#win-overlay.show {
  display: flex;
}

#win-text {
  font-size: 28px;
  font-weight: 700;
  color: var(--success);
  text-shadow: 0 0 30px var(--success);
  animation: win-pulse 1.2s ease-in-out infinite;
}

@keyframes win-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.85; }
}

/* ===== Level Description ===== */
#level-desc {
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  max-width: calc(var(--cell-size) * var(--grid-cols) + 2px);
  line-height: 1.5;
}

/* ===== Responsive ===== */
@media (max-width: 700px) {
  :root { --cell-size: 48px; }
  body { align-items: flex-start; padding: 12px 0; }
  #app { flex-direction: column; align-items: center; gap: 12px; }
  #toolbar { flex-direction: row; width: auto; padding: 10px 16px; }
  .tool-btn { width: 44px; height: 44px; }
  #controls { flex-wrap: wrap; }
  #controls .btn { flex: 1 1 calc(50% - 6px); min-width: 0; }
  .welcome-hints { flex-direction: column; }
  .level-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ===== Экран приветствия ===== */
#welcome-screen {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  position: fixed;
  top: 0; left: 0;
  z-index: 100;
  background: var(--bg);
}

.welcome-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: fadeInUp 0.6s ease;
}

.welcome-logo {
  font-size: 72px;
  background: linear-gradient(135deg, var(--accent-glow), #f472b6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: pulse-logo 3s ease-in-out infinite;
  filter: drop-shadow(0 0 30px rgba(124, 92, 252, 0.4));
}

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

.welcome-title {
  font-size: 48px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-glow), #f472b6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-subtitle {
  font-size: 16px;
  color: var(--text-dim);
  letter-spacing: 4px;
  text-transform: uppercase;
}

.welcome-instructions {
  margin-top: 16px;
  padding: 20px 28px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  max-width: 420px;
}

.welcome-instructions p {
  font-size: 14px;
  color: var(--text);
  margin-bottom: 12px;
}

.welcome-hints {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.welcome-hints span {
  font-size: 11px;
  color: var(--text-dim);
  background: rgba(124, 92, 252, 0.08);
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
}

.welcome-play-btn {
  margin-top: 12px;
  padding: 16px 48px !important;
  font-size: 18px !important;
  border-color: var(--accent) !important;
  color: var(--accent-glow) !important;
  box-shadow: 0 0 24px rgba(124, 92, 252, 0.25);
  transition: all 0.3s ease !important;
}

.welcome-play-btn:hover {
  box-shadow: 0 0 40px rgba(124, 92, 252, 0.4) !important;
  transform: translateY(-3px) !important;
}

/* ===== Экран выбора уровней ===== */
#level-select-screen {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  position: fixed;
  top: 0; left: 0;
  z-index: 100;
  background: var(--bg);
}

.level-select-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  animation: fadeInUp 0.4s ease;
}

.level-select-title {
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-glow), #f472b6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.level-select-subtitle {
  font-size: 13px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 2px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  max-width: 380px;
}

.level-btn {
  width: 64px;
  height: 64px;
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel-bg);
  color: var(--text);
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  position: relative;
}

.level-btn:hover:not(.locked) {
  border-color: var(--accent);
  background: rgba(124, 92, 252, 0.15);
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(124, 92, 252, 0.2);
}

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

.level-btn.completed::after {
  content: '✓';
  position: absolute;
  top: 2px;
  right: 5px;
  font-size: 10px;
  color: var(--success);
}

.level-btn.locked {
  opacity: 0.3;
  cursor: not-allowed;
}

.level-btn.current {
  border-color: var(--accent);
  box-shadow: 0 0 16px rgba(124, 92, 252, 0.3);
  animation: level-pulse 2s ease-in-out infinite;
}

@keyframes level-pulse {
  0%, 100% { box-shadow: 0 0 16px rgba(124, 92, 252, 0.3); }
  50% { box-shadow: 0 0 28px rgba(124, 92, 252, 0.5); }
}

/* ===== Финальный экран ===== */
#final-screen {
  display: none;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  position: fixed;
  top: 0; left: 0;
  z-index: 100;
  background: var(--bg);
}

.final-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: fadeInUp 0.8s ease;
}

.final-star {
  font-size: 80px;
  animation: spin-star 3s ease-in-out infinite;
}

@keyframes spin-star {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(1.15) rotate(10deg); }
}

.final-title {
  font-size: 40px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--success), #a3f7bf);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.final-subtitle {
  font-size: 18px;
  color: var(--text);
}

.final-desc {
  font-size: 14px;
  color: var(--text-dim);
}

.final-buttons {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

/* ===== Анимации ===== */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
