:root {
  --sky1: #d3f9d8;
  --sky2: #f4fce3;
  --ink: #23395b;
  --accent: #7048e8;
  --good: #2bb673;
  --card: rgba(255, 255, 255, 0.94);
  --cell: 56px; /* JS cập nhật theo kích thước bản đồ */
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Baloo 2', system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(var(--sky1), var(--sky2));
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; }
button:disabled { cursor: default; }
.hidden { display: none !important; }

.panel {
  background: var(--card); border-radius: 28px; padding: 28px 24px; width: min(560px, 100%);
  text-align: center; box-shadow: 0 12px 0 rgba(35, 57, 91, .12), 0 20px 40px rgba(35, 57, 91, .15);
}
h1 { font-size: clamp(1.8rem, 6vw, 2.5rem); line-height: 1.15; }
.sub { opacity: .75; margin-top: 4px; }
.row { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 18px; }

.btn {
  font-size: 1.2rem; font-weight: 800; padding: 10px 24px; border-radius: 999px;
  transition: transform .1s, box-shadow .1s;
}
.btn.go { background: var(--good); color: #fff; box-shadow: 0 5px 0 #1e8c57; }
.btn.ghost { background: #eef3ff; box-shadow: 0 5px 0 #c9d4f2; }
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0, 0, 0, .2); }
.btn:disabled { opacity: .5; }

/* ---------- Chọn màn ---------- */
#levels { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.level-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 20px; }
.level-grid button {
  aspect-ratio: 1; border-radius: 18px; background: #eef3ff; font-size: 1.8rem; font-weight: 800;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
  box-shadow: 0 5px 0 #c9d4f2; transition: transform .12s;
}
.level-grid button:hover:not(:disabled) { transform: translateY(-3px); }
.level-grid button small { font-size: .8rem; margin-top: 6px; letter-spacing: 1px; }
.level-grid button.done { background: #e6fcf0; box-shadow: 0 5px 0 #a9e5c4; }
.level-grid button:disabled { opacity: .45; }

/* ---------- Màn chơi ---------- */
#play { min-height: 100%; display: flex; flex-direction: column; gap: 10px; padding: 10px 16px 16px; max-width: 760px; margin: 0 auto; }
.hud { display: flex; align-items: center; gap: 10px; }
.hud h2 { flex: 1; text-align: center; font-size: 1.4rem; }
.icon {
  background: var(--card); width: 44px; height: 44px; border-radius: 14px; font-size: 1.3rem;
  box-shadow: 0 3px 0 rgba(35, 57, 91, .15); flex-shrink: 0;
}
.icon.off { opacity: .4; }

/* Khủng long đứng cạnh lời gợi ý thay vì nổi trên sân chơi */
.guide { display: flex; align-items: center; gap: 4px; min-height: 76px; }
.guide .mascot { width: 76px; height: 76px; }
.guide .bubble { margin: 0; white-space: normal; font-size: 1.05rem; border-radius: 18px 18px 18px 4px; flex: 1; }

.board-wrap { display: flex; justify-content: center; }
.board {
  position: relative; display: grid; grid-auto-rows: var(--cell); border-radius: 16px; overflow: hidden;
  box-shadow: 0 6px 0 #5aa364, 0 10px 24px rgba(35, 57, 91, .2);
}
.cell { width: var(--cell); height: var(--cell); display: grid; place-items: center; font-size: calc(var(--cell) * .62); background: #8fd694; }
.cell.alt { background: #82cc88; }
.cell.goal span { animation: shine 1s ease-in-out infinite alternate; }
@keyframes shine { to { transform: scale(1.15) rotate(8deg); filter: drop-shadow(0 0 6px #fff); } }

.robot {
  position: absolute; left: 0; top: 0; width: var(--cell); height: var(--cell);
  display: grid; place-items: center; font-size: calc(var(--cell) * .7);
  transition: transform .3s ease-in-out; z-index: 2;
}
.robot.bump span { display: inline-block; animation: bump .45s; }
.robot.win span { display: inline-block; animation: spin .7s; }
@keyframes bump { 25%, 75% { transform: translateX(-6px) rotate(-10deg); } 50% { transform: translateX(6px) rotate(10deg); } }
@keyframes spin { to { transform: rotate(360deg) scale(1.2); } }

/* ---------- Chương trình ---------- */
.program-area { background: var(--card); border-radius: 20px; padding: 10px 12px; }
.program-head { display: flex; justify-content: space-between; font-weight: 800; margin-bottom: 6px; gap: 8px; flex-wrap: wrap; }
.bug-count { background: #fff0f6; color: #c2255c; border-radius: 999px; padding: 0 10px; margin-left: 4px; }
#editInfo { opacity: .65; }
.program { display: flex; flex-wrap: wrap; gap: 10px; }
.program-help { font-size: .85rem; opacity: .65; margin-top: 6px; }
.blk {
  display: flex; flex-direction: column; align-items: center; position: relative; background: #f3f0ff;
  border-radius: 16px; padding: 4px; box-shadow: 0 4px 0 #d0bfff; border: 3px solid transparent;
}
.blk-num { position: absolute; top: -10px; left: -6px; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; font-size: .75rem; font-weight: 800; display: grid; place-items: center; }
.blk-dir { width: 56px; height: 52px; font-size: 1.9rem; background: #fff; border-radius: 12px; }
.blk-count { margin-top: 4px; font-weight: 800; color: var(--accent); background: #fff; border-radius: 8px; padding: 0 12px; }
.blk.changed::after { content: '🔧'; position: absolute; top: -12px; right: -8px; font-size: 1rem; }
.blk.active { border-color: #f0c419; background: #fff3bf; }
.blk.fail { border-color: #fa5252; background: #ffe3e3; animation: blk-shake .4s; }
@keyframes blk-shake { 25%, 75% { transform: translateX(-4px); } 50% { transform: translateX(4px); } }
.running .blk button { pointer-events: none; }

.controls { display: flex; gap: 12px; justify-content: center; }

/* ---------- Bảng thắng ---------- */
.modal { position: fixed; inset: 0; background: rgba(35, 57, 91, .45); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal .panel { animation: pop .3s; }
@keyframes pop { from { transform: scale(.7); opacity: 0; } }
.stars { font-size: 3.4rem; margin: 8px 0; letter-spacing: 6px; }
.stars span { display: inline-block; filter: grayscale(1); opacity: .3; }
.stars span.on { filter: none; opacity: 1; animation: starIn .5s backwards; }
.stars span:nth-child(2) { animation-delay: .2s; }
.stars span:nth-child(3) { animation-delay: .4s; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); } 70% { transform: scale(1.3); } }

@media (max-width: 520px) {
  .blk-dir { width: 48px; height: 44px; font-size: 1.6rem; }
}
