:root {
  --sky1: #c5f6fa;
  --sky2: #f1fcfd;
  --ink: #23395b;
  --accent: #4c6ef5;
  --good: #2bb673;
  --bad: #fa5252;
  --card: rgba(255, 255, 255, 0.95);
  --car-w: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-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: 100vh; display: grid; place-items: center; padding: 16px; }
.level-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 20px; }
.level-grid button {
  aspect-ratio: 1.2; 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 { display: flex; flex-direction: column; gap: 10px; padding: 10px 16px 20px; 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; }
.guide { display: flex; align-items: center; gap: 4px; min-height: 70px; }
.guide .mascot { width: 70px; height: 70px; }
.guide .bubble { margin: 0; white-space: normal; font-size: 1.05rem; flex: 1; }

/* ---------- Con đường ---------- */
.road-wrap { background: var(--card); border-radius: 20px; padding: 8px 10px 10px; }
.scene-head { display: flex; justify-content: space-between; font-weight: 800; margin-bottom: 6px; }
.scene-dots { display: flex; gap: 4px; }
.scene-dots i { width: 14px; height: 14px; border-radius: 50%; background: #dee2e6; }
.scene-dots i.ok { background: var(--good); }
.scene-dots i.now { background: #ffd43b; }
.scene-dots i.fail { background: var(--bad); }

.road {
  position: relative; height: 160px; border-radius: 14px; overflow: hidden;
  background: linear-gradient(#b2f2bb 0 22%, #adb5bd 22% 24%, #495057 24% 90%, #adb5bd 90% 92%, #b2f2bb 92%);
}
.lane {
  position: absolute; left: 0; right: 0; top: 56%; height: 4px;
  background: repeating-linear-gradient(90deg, #fff 0 24px, transparent 24px 44px); opacity: .7;
}
.zebra {
  position: absolute; left: 51%; width: 9%; top: 24%; height: 66%;
  background: repeating-linear-gradient(#f8f9fa 0 9px, transparent 9px 18px); opacity: .9;
}
.stopline { position: absolute; left: 50%; width: 5px; top: 58%; height: 32%; background: #fff; }
.light {
  position: absolute; left: calc(50% - 46px); top: 2px; display: flex; gap: 4px; padding: 4px 6px;
  background: #212529; border-radius: 12px; z-index: 2;
}
.light i { width: 18px; height: 18px; border-radius: 50%; background: #495057; transition: background .2s, box-shadow .2s; }
.light[data-c="red"] .r { background: #ff4d4d; box-shadow: 0 0 10px #ff4d4d; }
.light[data-c="yellow"] .y { background: #ffd43b; box-shadow: 0 0 10px #ffd43b; }
.light[data-c="green"] .g { background: #51cf66; box-shadow: 0 0 10px #51cf66; }

.obstacle {
  position: absolute; left: 53%; top: 34%; font-size: 2.6rem; line-height: 1; z-index: 2;
  transition: transform .6s, opacity .6s;
}
.obstacle.leave { transform: translateY(-90px); opacity: 0; }
.obstacle.scared { animation: jump .5s 2; }
@keyframes jump { 50% { transform: translateY(-16px); } }

/* left = mép phải của xe; JS đặt vị trí theo % chiều dài đường */
.car {
  position: absolute; top: 60%; width: var(--car-w); font-size: 2.8rem; line-height: 1; z-index: 3;
  left: calc(10% - var(--car-w)); transition-property: left; transition-timing-function: ease-in-out;
}
.car span { display: inline-block; transform: scaleX(-1); } /* emoji xe hướng sang trái, lật lại */
.car.crash span { animation: crash .5s; }
@keyframes crash { 25%, 75% { transform: scaleX(-1) rotate(-12deg); } 50% { transform: scaleX(-1) rotate(12deg); } }

.scene-msg {
  position: absolute; left: 50%; top: 6px; transform: translateX(-50%) scale(.6); opacity: 0;
  background: #fff; border-radius: 999px; padding: 2px 14px; font-weight: 800; white-space: nowrap; z-index: 4;
  transition: .2s; margin-left: 90px;
}
.scene-msg.show { opacity: 1; transform: translateX(-50%) scale(1); }

/* ---------- Luật ---------- */
.rules-area { background: var(--card); border-radius: 20px; padding: 10px 12px; }
.rules-head { font-weight: 800; margin-bottom: 8px; }
.rules-head small { font-weight: 600; opacity: .7; }
.rules { display: flex; flex-direction: column; gap: 8px; }
.rule {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; background: #f1f3ff; border-radius: 16px;
  padding: 8px 10px; border: 3px solid transparent; transition: border-color .2s, background .2s;
}
.rule.active { border-color: #ffd43b; background: #fff9db; }
.rule.fail { border-color: var(--bad); background: #fff5f5; }
.rule-num { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 800; display: grid; place-items: center; flex-shrink: 0; }
.rule-cond { font-size: 1.1rem; font-weight: 700; min-width: 210px; }
.rule-cond b { color: var(--accent); }
.rule-acts { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.rule-acts button {
  background: #fff; border-radius: 12px; padding: 4px 10px; font-weight: 700; border: 3px solid #dbe4ff;
}
.rule-acts button.on { border-color: var(--good); background: #e6fcf0; }
.rule-move { display: flex; gap: 4px; }
.rule-move button { width: 34px; height: 34px; border-radius: 10px; background: #fff; font-size: 1rem; box-shadow: 0 3px 0 #dbe4ff; }
.rule-move button:disabled { opacity: .3; }
.running .rule-acts button, .running .rule-move button { pointer-events: none; }
#runBtn { align-self: center; font-size: 1.35rem; padding: 12px 36px; }

/* ---------- 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: 560px) {
  :root { --car-w: 48px; }
  .car { font-size: 2.1rem; }
  .obstacle { font-size: 2rem; }
  .rule-cond { min-width: 0; width: 100%; }
  .scene-msg { margin-left: 0; top: 40px; }
}
