/* ============================================
   游戏核心样式 game.css
   包含：三栏布局、积木工具箱、编程画布、
   游戏网格、角色定位（修复版）、运行动画
   ============================================ */

/* ---------- 游戏三栏布局 ---------- */
.game-layout {
  display: flex;
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 56px);
  box-sizing: border-box;
}

/* 左侧积木工具箱 */
.game-toolbox {
  width: 200px;
  flex-shrink: 0;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.toolbox-header {
  padding: 12px;
  background: linear-gradient(135deg, var(--color-primary), #FF8C42);
  color: #fff;
  font-weight: bold;
  font-size: 15px;
  text-align: center;
}
.toolbox-categories {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.toolbox-category {
  margin-bottom: 8px;
}
.toolbox-category-title {
  font-size: 13px;
  font-weight: bold;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}
.toolbox-category-title:hover { background: #f5f5f5; }
.toolbox-category-items {
  padding: 4px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.toolbox-category-items.collapsed { display: none; }

/* ---------- 积木样式 ---------- */
.block {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: bold;
  color: #fff;
  cursor: grab;
  user-select: none;
  min-height: 36px;
  position: relative;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  transition: transform 0.1s, box-shadow 0.1s;
  white-space: nowrap;
}
.block:active { cursor: grabbing; }
.block:hover { transform: scale(1.02); box-shadow: 0 4px 8px rgba(0,0,0,0.2); }
.block.dragging {
  opacity: 0.8;
  transform: scale(1.05) rotate(2deg);
  z-index: 100;
  position: fixed;
  pointer-events: none;
}

/* 积木类型颜色 */
.block-event { background: var(--block-event); color: #5D4037; }
.block-motion { background: var(--block-motion); }
.block-loop { background: var(--block-loop); }
.block-condition { background: var(--block-condition); }
.block-variable { background: var(--block-variable); }
.block-function { background: var(--block-function); }

.block-icon { margin-right: 6px; font-size: 16px; }
.block-text { flex: 1; }
.block-input {
  width: 40px;
  padding: 2px 4px;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  font-weight: bold;
  text-align: center;
  background: rgba(255,255,255,0.9);
  color: var(--color-text);
  margin: 0 4px;
}
.block-delete {
  margin-left: 8px;
  cursor: pointer;
  font-size: 18px;
  font-weight: bold;
  opacity: 0.75;
  min-width: 30px;
  min-height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 8px;
  background: rgba(255,255,255,0.55);
  flex-shrink: 0;
}
.block-delete:hover { opacity: 1; background: rgba(255,120,90,0.25); }

/* 事件积木（入口帽子块） */
.block-event-wrapper {
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin: 4px 0;
}
.block-event-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--block-event);
  color: #5D4037;
  font-weight: bold;
  font-size: 14px;
  cursor: grab;
  min-height: 36px;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.block-event-body {
  background: rgba(255,193,7,0.12);
  border-left: 3px solid var(--block-event);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  padding: 8px 6px 8px 12px;
  min-height: 64px;
}
.block-event-body:empty::after {
  content: "拖入积木";
  color: #999;
  font-size: 12px;
  display: block;
  text-align: center;
  padding: 8px 0;
}

/* 循环积木（带内部容器） */
.block-loop-wrapper {
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin: 4px 0;
}
.block-loop-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--block-loop);
  color: #fff;
  font-weight: bold;
  font-size: 14px;
  cursor: grab;
  min-height: 36px;
}
.block-loop-body {
  background: rgba(255,171,25,0.15);
  border-left: 3px solid var(--block-loop);
  padding: 8px 6px 8px 12px;
  min-height: 64px;
}
.block-loop-body:empty::after {
  content: "拖入积木";
  color: #999;
  font-size: 12px;
  display: block;
  text-align: center;
  padding: 8px 0;
}

/* 条件积木 */
.block-condition-wrapper {
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin: 4px 0;
}
.block-condition-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--block-condition);
  color: #fff;
  font-weight: bold;
  font-size: 14px;
  cursor: grab;
  min-height: 36px;
}
.block-condition-body {
  background: rgba(92,184,92,0.12);
  border-left: 3px solid var(--block-condition);
  padding: 8px 6px 8px 12px;
  min-height: 64px;
}
.block-condition-body:empty::after {
  content: "拖入积木";
  color: #999;
  font-size: 12px;
  display: block;
  text-align: center;
  padding: 8px 0;
}

/* ---------- 中间编程画布 ---------- */
.game-canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  min-width: 0;
}
.canvas-header {
  padding: 10px 14px;
  background: #f8f9fa;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.canvas-title { font-weight: bold; font-size: 15px; }
.canvas-actions { display: flex; gap: 6px; }

.canvas-workspace {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  position: relative;
  background: repeating-linear-gradient(
    45deg,
    #fafafa,
    #fafafa 10px,
    #f5f5f5 10px,
    #f5f5f5 20px
  );
}
.canvas-placeholder {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #ccc;
  font-size: 18px;
  pointer-events: none;
  text-align: center;
}
.canvas-placeholder.hidden { display: none; }

.block-stack {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 100%;
}
.block-stack .block,
.block-stack .block-loop-wrapper,
.block-stack .block-condition-wrapper {
  width: fit-content;
  min-width: 180px;
}

/* 拖拽放置指示线 */
.drop-indicator {
  height: 4px;
  background: var(--color-primary);
  border-radius: 2px;
  margin: 2px 0;
  animation: pulse 0.5s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.canvas-controls {
  padding: 10px 14px;
  border-top: 1px solid var(--color-border);
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------- 右侧游戏预览 ---------- */
.game-preview {
  width: 340px;
  flex-shrink: 0;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.preview-header {
  padding: 10px 14px;
  background: linear-gradient(135deg, var(--color-secondary), #44A08D);
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.preview-title { font-weight: bold; font-size: 15px; }
.level-name { font-size: 13px; opacity: 0.9; }

.task-panel {
  padding: 10px 14px;
  background: #FFF8E1;
  border-bottom: 1px solid #FFE082;
}
.task-title {
  font-weight: bold;
  font-size: 14px;
  color: var(--color-primary);
  margin-bottom: 4px;
}
.task-desc {
  font-size: 13px;
  color: var(--color-text);
  line-height: 1.5;
  margin-bottom: 4px;
}
.task-progress {
  font-size: 12px;
  color: var(--color-success);
  font-weight: bold;
}

/* ===== 游戏网格（核心！修复版） ===== */
.game-grid-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: linear-gradient(135deg, #E8F5E9, #C8E6C9);
  overflow: auto;
}

/* 关键修复：position: relative 必须从一开始就有 */
/* --cell-size 统一控制格子/角色/敌人尺寸，engine.js 动态读取，任何屏幕都不出格 */
.game-grid {
  position: relative;
  display: grid;
  --cell-size: 44px;
  gap: 2px;
  background: #A5D6A7;
  padding: 4px;
  border-radius: var(--radius-md);
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.1);
}

/* 格子 */
.grid-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: #C8E6C9;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  position: relative;
  transition: background 0.3s;
}
.grid-cell.start { background: #81C784; }
.grid-cell.goal {
  background: #64B5F6;
  animation: goalGlow 1.5s infinite;
}
@keyframes goalGlow {
  0%, 100% { box-shadow: 0 0 4px #2196F3; }
  50% { box-shadow: 0 0 16px #2196F3; }
}
.grid-cell.crystal-cell { background: #FFF9C4; }
.grid-cell.obstacle { background: #8D6E63; }
.grid-cell.enemy-cell { background: #FFCDD2; }
.grid-cell.visited { background: #A5D6A7; }

/* ===== 角色元素（关键修复！） ===== */
/* flex 居中 + 小号字体，不出格，不依赖 emoji 自然宽度 */
.game-character {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: left 0.4s ease-in-out, top 0.4s ease-in-out;
  pointer-events: none;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}
/* SVG 卡通头像：铺满整个格子，绝对居中，无 emoji 字形偏移 */
.game-character svg,
.grid-enemy svg {
  width: 100%;
  height: 100%;
  display: block;
}
.game-character.moving {
  animation: characterBounce 0.4s ease-in-out;
}
@keyframes characterBounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08) translateY(-2px); }
}

/* 水晶收集动画 */
.crystal-collect {
  position: absolute;
  font-size: 20px;
  z-index: 20;
  animation: crystalFly 0.6s ease-out forwards;
  pointer-events: none;
}
@keyframes crystalFly {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.5) translateY(-30px); opacity: 0; }
}

/* 敌人巡逻 */
.grid-enemy {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  transition: left 0.5s ease-in-out, top 0.5s ease-in-out;
  pointer-events: none;
}

.game-info-bar {
  padding: 8px 14px;
  background: #f8f9fa;
  border-top: 1px solid var(--color-border);
  display: flex;
  justify-content: space-around;
  font-size: 13px;
  font-weight: bold;
}

/* ---------- 创作工坊布局 ---------- */
.workshop-layout {
  display: flex;
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 56px - 120px);
  box-sizing: border-box;
}
.workshop-toolbox {
  width: 200px;
  flex-shrink: 0;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.workshop-canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  min-width: 0;
}
.workshop-preview {
  width: 300px;
  flex-shrink: 0;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 运行中高亮 */
.block.running {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
  box-shadow: 0 0 12px var(--color-accent);
}

/* 胜利粒子 */
.particle {
  position: absolute;
  font-size: 20px;
  pointer-events: none;
  z-index: 100;
  animation: particleFall 1.5s ease-out forwards;
}
@keyframes particleFall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(200px) rotate(360deg); opacity: 0; }
}

.drop-target {
  outline: 3px dashed #FF8C42 !important;
  outline-offset: 2px !important;
  border-radius: 8px;
  animation: dropPulse 0.8s infinite alternate;
}
@keyframes dropPulse {
  from { box-shadow: 0 0 0 0 rgba(255,140,66,0.35); }
  to { box-shadow: 0 0 12px 4px rgba(255,140,66,0.55); }
}

/* 画布内拖动重排：插入位置指示线 */
.insert-before {
  box-shadow: 0 -4px 0 0 #FF8C42 !important;
}
.insert-after {
  box-shadow: 0 4px 0 0 #FF8C42 !important;
}
.insert-before, .insert-after {
  border-radius: 8px;
  animation: dropPulse 0.8s infinite alternate;
}

/* 拖动中的积木半透明 */
.block.dragging, .block-event-wrapper.dragging, .block-loop-wrapper.dragging, .block-condition-wrapper.dragging {
  opacity: 0.9;
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
  transform: rotate(-2deg) scale(1.02);
}
