* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background: linear-gradient(145deg, #2b5a8c 0%, #1d3e60 100%);
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', system-ui, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
}

.game-container {
  max-width: 1080px;
  width: 100%;
  background: #fef9e8;
  border-radius: 40px 40px 28px 28px;
  box-shadow: 0 20px 30px rgba(0, 0, 0, 0.45), inset 0 2px 4px rgba(255, 255, 255, 0.6);
  overflow: hidden;
  border: 6px solid #ffc857;
  margin: 12px;
}

/* 顶部栏 */
.top-bar {
  background: #ffc857;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 4px solid #e0a800;
  gap: 10px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #2a2a2a;
}
.brand-icon {
  font-size: 1.8rem;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.2));
}
.brand-title {
  font-weight: bold;
  font-size: 1.15rem;
  line-height: 1.1;
}
.brand-sub {
  font-size: 0.82rem;
  color: #5a4a1e;
}
.top-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}
.icon-btn {
  border: 2px solid #fff3d1;
  background: #fff3d1;
  color: #2a2a2a;
  font-weight: bold;
  font-size: 0.95rem;
  padding: 7px 14px;
  border-radius: 30px;
  cursor: pointer;
  box-shadow: 0 3px 0 #dbb42c;
  transition: transform 0.05s;
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #dbb42c; }
#accountBtn {
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 主游戏区 */
.game-main {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 16px 18px 18px;
  gap: 16px;
  background: #fef9e8;
}

/* 舞台 */
.stage-area {
  flex: 1 1 300px;
  min-width: 280px;
  background: #cbe6b0;
  border-radius: 32px;
  box-shadow: inset 0 -8px 0 #8eb87a, 0 6px 0 #5e7c4a;
  padding: 14px;
  display: flex;
  flex-direction: column;
  border: 3px solid #6f9e56;
}
.stage-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-weight: bold;
  color: #2d4e1e;
  font-size: 0.9rem;
  flex-wrap: wrap;
  gap: 6px;
}
.stage-goal { font-size: 0.82rem; color: #38611e; }

.grid-map {
  display: grid;
  gap: 6px;
  background: #b3d9a0;
  padding: 10px;
  border-radius: 24px;
  box-shadow: inset 0 0 0 3px #8bb36e;
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
}
.cell {
  background: #e6f3da;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.2rem, 4vw, 2.2rem);
  box-shadow: inset 0 -4px 0 #b3cf9d;
  transition: transform 0.15s ease;
  /* 格子尺寸与内容无关：始终保持正方形，空/有内容一样大 */
  aspect-ratio: 1 / 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  line-height: 1;
}
.cell.robot {
  background: #ffe28a;
  box-shadow: inset 0 -4px 0 #dbaa2b, 0 0 14px #ffb800;
  animation: bob 0.8s ease-in-out infinite;
}
.cell.goal {
  background: #f9e076;
  box-shadow: inset 0 -4px 0 #c9a42b, 0 0 12px #ffd966;
  animation: pulse 1.6s infinite;
}
.cell.obstacle {
  background: #b8a28b;
  box-shadow: inset 0 -4px 0 #8b735a;
}
.cell.star { background: #ffeaa5; box-shadow: inset 0 -4px 0 #e6c44d; }
.cell.trail { background: #d8efc2; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }

.stage-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  font-weight: bold;
  color: #38611e;
  flex-wrap: wrap;
  gap: 8px;
}
.counter {
  background: #ffffffb3;
  padding: 5px 14px;
  border-radius: 30px;
  font-size: 1rem;
  box-shadow: 0 2px 0 #b3cf9d;
}
.counter.hint {
  flex: 1 1 100%;
  background: #fff8e1;
  color: #6b4f1e;
  font-size: 0.85rem;
  font-weight: normal;
  box-shadow: none;
  border: 2px dashed #e0c878;
}

/* 编程区 */
.code-area {
  flex: 1.4 1 320px;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  background: #f0e4d0;
  border-radius: 32px;
  padding: 14px 14px 16px;
  box-shadow: inset 0 -8px 0 #c9b394, 0 6px 0 #a58863;
  border: 3px solid #ba9a78;
  gap: 12px;
}

.blocks-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  background: #e2d2bb;
  padding: 10px;
  border-radius: 24px;
  align-items: center;
}
.palette-label {
  font-weight: bold;
  color: #6b4f2a;
  font-size: 0.85rem;
  margin-right: 4px;
}
.block {
  padding: 10px 14px;
  border-radius: 20px;
  font-weight: bold;
  font-size: 1.05rem;
  cursor: grab;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #2e1b0c;
  transition: transform 0.05s;
  border: 2px solid rgba(255,255,255,0.5);
  white-space: nowrap;
}
.block:active { cursor: grabbing; transform: scale(0.96); }
.block.forward { background: #6ec4e8; box-shadow: 0 4px 0 #3b8db3; color: #032a3b; }
.block.left    { background: #b0d96b; box-shadow: 0 4px 0 #7aa32e; color: #1e3a00; }
.block.right   { background: #f9c74f; box-shadow: 0 4px 0 #c49b1e; color: #3d2b00; }
.block.loop    { background: #c77dff; box-shadow: 0 4px 0 #8545b3; color: #1f0033; }
/* 条件积木：六边形，提示“它属于条件位置” */
.block.sensor {
  background: #ffd166;
  box-shadow: 0 4px 0 #c99b21;
  color: #4a3400;
  padding: 10px 20px;
  clip-path: polygon(9% 0, 91% 0, 100% 50%, 91% 100%, 9% 100%, 0 50%);
}
.block.if, .block.ifElse { background: #ef8b3c; box-shadow: 0 4px 0 #a85b12; color: #3d1c00; }

/* 程序区 */
.program-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 140px;
}
.program-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  font-weight: bold;
  color: #6b4f2a;
  font-size: 0.9rem;
}
.program-tip {
  font-size: 0.75rem;
  color: #9a7f58;
  font-weight: normal;
}
.program-sequence {
  background: #fcf5e9;
  border-radius: 22px;
  min-height: 120px;
  padding: 12px;
  box-shadow: inset 0 0 0 3px #d6c3a8, inset 0 6px 10px rgba(0,0,0,0.05);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-content: flex-start;
  flex: 1 1 auto;
  overflow-y: auto;
}
.program-sequence.drop-target { outline: 3px dashed #6f9e56; outline-offset: -4px; }
/* 拖拽时的插入位置提示（竖线光标） */
.drop-caret {
  position: fixed;
  width: 4px;
  border-radius: 2px;
  background: #ff3b3b;
  box-shadow: 0 0 8px rgba(255, 59, 59, 0.85);
  pointer-events: none;
  z-index: 30;
  display: none;
}
.program-empty-hint {
  color: #b9a37f;
  font-size: 0.9rem;
  margin: auto;
}

/* 已放置积木 */
.placed-block {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 22px;
  font-weight: bold;
  font-size: 1.05rem;
  color: #fff;
  cursor: grab;
  border: 2px solid rgba(255,255,255,0.55);
  transition: transform 0.08s, opacity 0.08s;
  position: relative;
}
.placed-block.dragging { opacity: 0.35; }
.placed-block.running { outline: 3px solid #ff3b3b; animation: runPulse 0.4s infinite; }
@keyframes runPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }
.placed-block.forward { background: #4a90b8; box-shadow: 0 4px 0 #2c5a75; }
.placed-block.left    { background: #6b9b3a; box-shadow: 0 4px 0 #3e5e1e; }
.placed-block.right   { background: #b8862c; box-shadow: 0 4px 0 #7a5a18; }
.pb-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.25);
  color: #fff;
  font-size: 0.75rem;
  cursor: pointer;
  margin-left: 2px;
}

/* 循环积木（容器） */
.loop-block {
  display: inline-flex;
  flex-direction: column;
  background: #9d4edd;
  border-radius: 20px;
  padding: 6px 8px 8px;
  box-shadow: 0 4px 0 #5a189a;
  border: 2px solid rgba(255,255,255,0.45);
  min-width: 140px;
  vertical-align: top;
  cursor: grab;
  position: relative;
}
.loop-block.running { outline: 3px solid #ff3b3b; animation: runPulse 0.4s infinite; }
.loop-block.dragging { opacity: 0.35; }
.loop-head {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #fff;
  font-weight: bold;
  font-size: 1rem;
  padding: 2px 4px 6px;
}
.loop-count {
  background: #fff;
  color: #5a189a;
  border-radius: 50%;
  width: 26px; height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 2px 0 #c9a0ff;
  user-select: none;
}
.loop-head .pb-remove { margin-left: auto; }
.loop-body {
  background: #f3e8ff;
  border-radius: 14px;
  min-height: 52px;
  padding: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-content: flex-start;
  box-shadow: inset 0 0 0 2px #c9a0ff;
}
.loop-body.drop-target { outline: 3px dashed #5a189a; outline-offset: -3px; }
.loop-body .placed-block { font-size: 0.92rem; padding: 6px 12px; }
.loop-body .loop-block { min-width: 110px; padding: 4px 6px 6px; }

/* 如果 / 如果否则（条件容器，C 形嵌套） */
.if-block {
  display: inline-flex;
  flex-direction: column;
  background: #ef8b3c;
  border-radius: 20px;
  padding: 6px 8px 8px;
  box-shadow: 0 4px 0 #a85b12;
  border: 2px solid rgba(255,255,255,0.45);
  border-left: 8px solid #c96a12;
  min-width: 150px;
  vertical-align: top;
  cursor: grab;
  position: relative;
}
.if-block.running { outline: 3px solid #ff3b3b; animation: runPulse 0.4s infinite; }
.if-block.dragging { opacity: 0.35; }
.if-head {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  color: #fff;
  font-weight: bold;
  font-size: 0.95rem;
  padding: 2px 4px 6px;
}
.if-head .pb-remove { margin-left: auto; }
.if-label { white-space: nowrap; }

/* 条件槽：六边形，只能放条件积木 */
.cond-slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  min-width: 88px;
  padding: 2px 12px;
  background: rgba(255, 255, 255, 0.82);
  border: 2px dashed #c96a12;
  border-radius: 14px;
  clip-path: polygon(9% 0, 91% 0, 100% 50%, 91% 100%, 9% 100%, 0 50%);
}
.cond-slot.drop-target { background: #ffe3bf; border-color: #ff3b3b; }
.cond-placeholder { color: #b06a1e; font-size: 0.8rem; font-weight: bold; }

/* 条件芯片 */
.sensor-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 12px;
  background: #ffd166;
  color: #4a3400;
  font-size: 0.8rem;
  font-weight: bold;
  cursor: grab;
  white-space: nowrap;
  position: relative;
}
.sensor-chip.dragging { opacity: 0.35; }
.sensor-chip .pb-remove { width: 16px; height: 16px; font-size: 0.66rem; }

/* 条件分支主体 */
.if-body, .else-body {
  background: #fff4e6;
  border-radius: 14px;
  min-height: 52px;
  padding: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-content: flex-start;
  box-shadow: inset 0 0 0 2px #f3b26d;
}
.else-head {
  color: #fff;
  font-weight: bold;
  font-size: 0.9rem;
  padding: 6px 4px 4px;
}
.if-body.drop-target, .else-body.drop-target { outline: 3px dashed #a85b12; outline-offset: -3px; }
.if-body .placed-block, .else-body .placed-block { font-size: 0.92rem; padding: 6px 12px; }
.if-body .loop-block, .else-body .loop-block,
.if-body .if-block, .else-body .if-block { min-width: 110px; padding: 4px 6px 6px; }

/* 控制按钮 */
.controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}
.btn {
  border: none;
  font-weight: bold;
  font-size: 1.1rem;
  padding: 11px 22px;
  border-radius: 40px;
  background: #ffe169;
  box-shadow: 0 5px 0 #c2a11b, 0 6px 10px rgba(0,0,0,0.18);
  cursor: pointer;
  color: #2f2f2f;
  transition: transform 0.04s;
  border: 2px solid #fff8d0;
}
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #c2a11b; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn.run   { background: #6fcf97; box-shadow: 0 5px 0 #2e7d5e; color: #023b1f; }
.btn.reset { background: #ff9e9e; box-shadow: 0 5px 0 #b15959; color: #3d0000; }
.btn.clear { background: #b0b0b0; box-shadow: 0 5px 0 #6e6e6e; color: #fff; }

/* 浮层 */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 25, 40, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
  backdrop-filter: blur(2px);
}
.overlay[hidden] { display: none; }
.overlay-card {
  background: #fef9e8;
  border-radius: 28px;
  border: 5px solid #ffc857;
  padding: 22px;
  max-width: 620px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 20px 40px rgba(0,0,0,0.5);
}
.overlay-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.overlay-head h2 { margin: 0; color: #5a4a1e; }

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
}
.level-card {
  background: #fff;
  border-radius: 18px;
  padding: 14px 10px;
  text-align: center;
  cursor: pointer;
  border: 3px solid #e0d2b4;
  box-shadow: 0 4px 0 #c9b394;
  transition: transform 0.08s;
}
.level-card:hover { transform: translateY(-2px); }
.level-card.locked { background: #e8e2d6; cursor: not-allowed; opacity: 0.7; }
.level-card.shake { animation: levelShake 0.35s ease; }
@keyframes levelShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}
.level-card.current { border-color: #ff9e2c; box-shadow: 0 4px 0 #c47a1e; }
.level-card .lc-id { font-size: 1.5rem; font-weight: bold; color: #3b6ea5; }
.level-card.locked .lc-id { color: #999; }
.level-card .lc-title { font-size: 0.9rem; color: #5a4a1e; font-weight: bold; margin: 4px 0; }
.level-card .lc-stars { font-size: 0.85rem; color: #c9a42b; letter-spacing: 2px; }

/* 胜利浮层 */
.win-card { text-align: center; }
.win-emoji { font-size: 3.5rem; animation: bob 0.8s ease-in-out infinite; }
.win-card h2 { margin: 6px 0; color: #5a4a1e; }
.star-rating { font-size: 2.6rem; letter-spacing: 6px; margin: 8px 0; }
.star-rating .star-on { color: #ffc857; text-shadow: 0 2px 4px rgba(0,0,0,0.2); }
.star-rating .star-off { color: #d8cfb8; }
.win-desc { color: #6b4f2a; font-size: 0.95rem; margin: 6px 0 16px; }
.win-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

/* 最优答案 */
.solution-wrap {
  margin: 4px 0 16px;
  padding: 10px 12px;
  background: #fff8e6;
  border: 2px dashed #e0b84a;
  border-radius: 18px;
  text-align: left;
}
.solution-title { font-weight: bold; color: #7a5a12; margin-bottom: 8px; }
.solution-desc { color: #7a5a12; font-size: 0.9rem; margin: 0 0 10px; text-align: left; }
.solution-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
  text-align: left;
  max-height: 48vh;
  overflow-y: auto;
}
.solution-blocks .sol { cursor: default; }
.solution-blocks .sol:active { transform: none; }
.solution-blocks .loop-count { cursor: default; }

/* 提示消息 */
.toast-message {
  position: fixed;
  top: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: #1f3a4b;
  color: #fff;
  padding: 10px 24px;
  border-radius: 40px;
  font-weight: bold;
  font-size: 1.1rem;
  box-shadow: 0 10px 20px rgba(0,0,0,0.35);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  z-index: 99;
  white-space: nowrap;
  max-width: 90vw;
}
.toast-message.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* 响应式 */
@media (max-width: 760px) {
  .game-main { flex-direction: column; }
  .grid-map { max-width: 360px; }
  .brand-title { font-size: 1rem; }
  .block { padding: 8px 10px; font-size: 0.95rem; }
}
