* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: 'Baloo 2', system-ui, sans-serif;
  background: #fff8e1; color: #4e342e;
  user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; border: none; color: inherit; }
#app { height: 100vh; height: 100dvh; }
.screen { display: none; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- Thanh trên ---------- */
.top { display: flex; align-items: center; gap: 10px; padding: 8px 14px; flex: none; }
h1 {
  margin: 0; flex: 1; white-space: nowrap;
  font-size: clamp(24px, 5vw, 42px); font-weight: 800;
  color: #e53935; text-shadow: 3px 3px 0 #ffca28;
}
.badge3d {
  display: inline-block; font-size: .55em; background: #1e88e5; color: #fff;
  border-radius: 10px; padding: 0 10px; text-shadow: none; vertical-align: middle;
  transform: rotate(-6deg);
}
.round-btn {
  width: 52px; height: 52px; border-radius: 50%; flex: none;
  background: #fff; font-size: 24px; box-shadow: 0 4px 0 #e0c27a;
  display: flex; align-items: center; justify-content: center;
}
.round-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #e0c27a; }

.seg { display: flex; background: #fff; border-radius: 30px; padding: 4px; box-shadow: 0 4px 0 #e0c27a; flex: none; }
.seg button { background: none; border-radius: 26px; padding: 6px 12px; font-size: 17px; font-weight: 800; }
.seg button.on { background: #ffca28; }

/* ---------- Tab danh mục ---------- */
#tabs { display: flex; gap: 8px; padding: 2px 14px 8px; overflow-x: auto; flex: none; }
#tabs button {
  flex: none; background: #fff; border-radius: 18px; padding: 6px 16px;
  font-size: 19px; font-weight: 800; box-shadow: 0 4px 0 #e0c27a;
}
#tabs button.on { background: #ff7043; color: #fff; box-shadow: 0 4px 0 #bf360c; }
#catHead { padding: 0 16px; font-size: 18px; font-weight: 700; color: #6d4c41; flex: none; }

/* ---------- Thẻ mô hình ---------- */
#cards {
  flex: 1; overflow: auto; min-height: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 14px; padding: 8px 14px 16px; align-content: start;
}
.card {
  background: #fff; border-radius: 22px; padding: 8px 8px 10px;
  box-shadow: 0 6px 0 rgba(0,0,0,.08);
  display: flex; flex-direction: column; align-items: center; text-align: center;
  position: relative; transition: transform .15s;
}
.card:active { transform: scale(.96); }
.card .thumb { width: 100%; aspect-ratio: 4 / 3; border-radius: 16px; background: linear-gradient(#e3f2fd, #f1f8e9); object-fit: contain; }
.card .nm { font-size: 19px; font-weight: 800; line-height: 1.15; margin-top: 4px; }
.card .en { font-size: 13px; font-style: italic; color: #8d6e63; }
.card .meta { display: flex; gap: 8px; font-size: 13px; font-weight: 700; margin-top: 2px; }
.card .stars { position: absolute; top: 10px; right: 12px; font-size: 18px; letter-spacing: -2px; }
.card .diet { position: absolute; top: 10px; left: 12px; font-size: 22px; }
.classic-link { flex: none; text-align: center; padding: 6px; font-weight: 700; color: #8d6e63; text-decoration: none; }

/* ---------- Màn chơi ---------- */
.title-wrap { flex: 1; min-width: 0; }
#title { font-size: clamp(18px, 3.6vw, 26px); font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { position: relative; height: 18px; background: #ffe082; border-radius: 10px; overflow: hidden; max-width: 380px; }
#bar { height: 100%; width: 0; background: linear-gradient(90deg, #66bb6a, #43a047); transition: width .3s; }
#count { position: absolute; inset: 0; text-align: center; font-size: 13px; font-weight: 800; line-height: 18px; }
.pic-btn { width: 72px; height: 54px; border-radius: 14px; background: #fff; padding: 2px; box-shadow: 0 4px 0 #e0c27a; flex: none; }
.pic-btn img { width: 100%; height: 100%; object-fit: contain; }

#hint {
  flex: none; text-align: center; font-weight: 800;
  font-size: clamp(17px, 3vw, 26px); min-height: 36px; padding: 0 12px; color: #6d4c41;
}
#stage { flex: 1; min-height: 0; position: relative; margin: 0 10px 8px; border-radius: 22px; overflow: hidden; background: #bfe3ff; }
#c { width: 100%; height: 100%; display: block; touch-action: none; }
#rotHint {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
  background: rgba(255,255,255,.85); border-radius: 16px; padding: 2px 12px;
  font-size: 14px; font-weight: 700; pointer-events: none; transition: opacity .5s; white-space: nowrap;
}

#tray {
  flex: none; min-height: 140px;
  display: flex; justify-content: center; align-items: center;
  gap: clamp(8px, 3vw, 30px); padding: 10px;
  background: #ffecb3; border-top: 4px solid #ffca28;
}
.piece {
  background: #fff; border-radius: 20px; padding: 4px;
  width: clamp(80px, 21vw, 170px); height: 118px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 0 #e0c27a; touch-action: none;
}
.piece img { max-width: 100%; max-height: 100%; pointer-events: none; }
.piece:active { transform: translateY(3px); box-shadow: 0 3px 0 #e0c27a; }
.piece.lifted { opacity: .3; }
.shake { animation: shake .45s; }
@keyframes shake { 20%, 60% { transform: translateX(-12px); } 40%, 80% { transform: translateX(12px); } }
.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; width: 150px;
  transform: translate(-50%, -50%) scale(1.1); filter: drop-shadow(0 10px 6px rgba(0,0,0,.3));
}

/* ---------- Khu lập trình ---------- */
#code {
  flex: none; background: #e3f2fd; border-top: 4px solid #64b5f6;
  padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 8px;
}
#program {
  display: flex; gap: 6px; align-items: center;
  min-height: 78px; background: #fff; border-radius: 18px; padding: 6px 10px; overflow-x: auto;
}
.blk {
  width: 58px; height: 58px; border-radius: 14px; flex: none;
  font-size: 30px; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 0 rgba(0,0,0,.2); transition: transform .15s;
}
.blk.start { background: #66bb6a; }
.blk.now { transform: scale(1.18); outline: 5px solid #ffca28; }
.arrow { font-size: 16px; color: #90a4ae; flex: none; }
.code-row { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
#palette { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.pal { background: none; display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: 12px; font-weight: 800; width: 64px; }
.pal:active .blk { transform: translateY(3px); }
.run-btns { display: flex; gap: 8px; align-items: center; flex: none; }
#runBtn {
  width: 80px; height: 80px; border-radius: 50%;
  background: #43a047; color: #fff; font-size: 40px; padding-left: 8px; box-shadow: 0 6px 0 #2e7d32;
}
#runBtn:active { transform: translateY(4px); box-shadow: 0 2px 0 #2e7d32; }
#runBtn.running { background: #fbc02d; box-shadow: 0 6px 0 #f57f17; }

/* ---------- Hộp thoại ---------- */
.modal {
  position: fixed; inset: 0; z-index: 80; background: rgba(62,39,35,.45);
  display: flex; align-items: center; justify-content: center; padding: 14px;
}
.sheet {
  position: relative; background: #fff; border-radius: 28px; padding: 16px 20px 20px;
  width: min(520px, 100%); max-height: 100%; overflow: auto; text-align: center;
  animation: popIn .3s ease-out;
}
@keyframes popIn { from { transform: scale(.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.sheet .close { position: absolute; top: 10px; right: 10px; width: 44px; height: 44px; font-size: 18px; }
#dImg, #bigPic { width: 100%; aspect-ratio: 4 / 3; object-fit: contain; border-radius: 20px; background: linear-gradient(#e3f2fd, #f1f8e9); }
#dName { margin: 8px 0 0; font-size: 30px; line-height: 1.1; color: #e53935; }
#dEn { font-style: italic; color: #8d6e63; font-size: 17px; }
#dChips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 8px 0; }
.chip { background: #fff3e0; border-radius: 14px; padding: 2px 12px; font-weight: 700; font-size: 15px; }
#dFact { font-size: 19px; font-weight: 600; margin: 6px 0 14px; }
.go-btn {
  background: #43a047; color: #fff; font-size: 26px; font-weight: 800;
  padding: 10px 34px; border-radius: 30px; box-shadow: 0 6px 0 #2e7d32;
}
.go-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #2e7d32; }
.pic-sheet p { font-size: 18px; font-weight: 700; margin: 8px 0 0; }

/* ---------- Ăn mừng ---------- */
#celebrate {
  position: fixed; inset: 0; z-index: 100; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(255,248,225,.45);
}
.big-star { font-size: 170px; line-height: 1; animation: starIn 1s ease-out; }
@keyframes starIn { from { transform: scale(0) rotate(-180deg); } to { transform: scale(1) rotate(0); } }
.cele-text { font-size: 60px; font-weight: 800; color: #e53935; text-shadow: 3px 3px 0 #fff; }
.confetti { position: fixed; top: -30px; width: 14px; height: 20px; z-index: 101; border-radius: 3px; pointer-events: none; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(115vh) rotate(800deg); } }

#loading {
  position: fixed; inset: 0; z-index: 200; background: #fff8e1;
  display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 800;
}

/* ---------- Màn hình nhỏ ---------- */
@media (max-width: 640px) {
  .top { flex-wrap: wrap; }
  #home h1 { flex-basis: calc(100% - 70px); }
  #levels { order: 3; width: 100%; justify-content: center; }
  .seg button { flex: 1; }
  #cards { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .card .nm { font-size: 16px; }
  .code-row { flex-direction: column; align-items: stretch; }
  .run-btns { justify-content: flex-end; }
  .pal { width: 56px; font-size: 11px; }
  .blk { width: 50px; height: 50px; font-size: 26px; }
  #play .top { flex-wrap: nowrap; }
}
@media (max-height: 560px) {
  .top { padding: 4px 10px; }
  .round-btn { width: 44px; height: 44px; font-size: 20px; }
  #tray { min-height: 104px; padding: 6px; }
  .piece { height: 92px; }
  .blk { width: 46px; height: 46px; font-size: 24px; }
  #program { min-height: 60px; }
  #runBtn { width: 62px; height: 62px; font-size: 30px; }
  #code { padding: 4px 8px 6px; gap: 4px; }
  #hint { min-height: 28px; font-size: 18px; }
}

/* ---------- Nút ngôn ngữ ---------- */
.lang-btn {
  height: 52px; padding: 0 14px; border-radius: 26px; flex: none;
  background: #fff; font-size: 19px; font-weight: 800; box-shadow: 0 4px 0 #e0c27a;
}
.lang-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #e0c27a; }
@media (max-width: 640px) { #home h1 { flex-basis: calc(100% - 180px); } .lang-btn { height: 46px; font-size: 16px; padding: 0 10px; } }
@media (max-height: 560px) { .lang-btn { height: 44px; } }

/* ---------- Tự sáng tạo ---------- */
#freePanel {
  flex: none; background: #f3e5f5; border-top: 4px solid #ba68c8;
  padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 8px;
}
#shapes { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.shape {
  flex: none; width: 84px; height: 66px; border-radius: 14px; background: #fff; padding: 2px;
  box-shadow: 0 4px 0 #ce93d8; display: flex; align-items: center; justify-content: center;
}
.shape img { max-width: 100%; max-height: 100%; pointer-events: none; }
.shape.on { outline: 4px solid #8e24aa; }
.free-row { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
#colors { display: flex; gap: 6px; flex-wrap: wrap; }
.color {
  width: 38px; height: 38px; border-radius: 50%; border: 3px solid #fff;
  box-shadow: 0 3px 0 rgba(0,0,0,.2);
}
.color.on { outline: 4px solid #4e342e; transform: scale(1.1); }
#tools { display: flex; gap: 6px; }
.tool {
  background: #fff; border-radius: 14px; padding: 2px 8px; min-width: 56px; height: 56px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 22px; line-height: 1; box-shadow: 0 4px 0 #ce93d8;
}
.tool small { font-size: 11px; font-weight: 800; margin-top: 3px; }
.tool.on { background: #8e24aa; color: #fff; box-shadow: 0 4px 0 #4a148c; }
.tool:active { transform: translateY(3px); }

/* thẻ tác phẩm */
.card.new .plus {
  width: 100%; aspect-ratio: 4 / 3; border-radius: 16px; display: flex; align-items: center; justify-content: center;
  font-size: 64px; background: repeating-linear-gradient(45deg, #f3e5f5, #f3e5f5 12px, #fff 12px, #fff 24px);
}
.card .del {
  position: absolute; top: 6px; right: 6px; width: 34px; height: 34px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 0 rgba(0,0,0,.2); font-size: 15px;
}

/* ---------- Album ---------- */
.album-sheet { width: min(900px, 100%); }
#albumTitle { margin: 4px 0 0; font-size: 28px; color: #e53935; }
#albumCount { font-weight: 800; font-size: 18px; margin-bottom: 8px; }
.album-cat { text-align: left; font-weight: 800; font-size: 18px; margin: 10px 4px 4px; }
.album-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.sticker {
  position: relative; background: #fff; border-radius: 50%; aspect-ratio: 1;
  border: 6px solid #e0e0e0; overflow: hidden; display: flex; align-items: center; justify-content: center;
}
.sticker img { width: 120%; height: 120%; object-fit: contain; }
.sticker.l1 { border-color: #cd7f32; } .sticker.l2 { border-color: #b0bec5; } .sticker.l3 { border-color: #ffc107; box-shadow: 0 0 12px #ffe082; }
.sticker.locked img { filter: brightness(0); opacity: .18; }
.sticker.locked::after { content: '?'; position: absolute; font-size: 34px; font-weight: 800; color: #bdbdbd; }
.sticker-name { font-size: 12px; font-weight: 700; text-align: center; margin-top: 2px; line-height: 1.1; }
.cele-sticker {
  width: 200px; height: 200px; border-radius: 50%; background: #fff; border: 8px solid #ffc107;
  object-fit: contain; animation: starIn .9s ease-out; box-shadow: 0 0 30px #ffe082;
}
#runBtn.running { padding-left: 0; }
@media (max-width: 640px) {
  .shape { width: 70px; height: 56px; }
  .color { width: 32px; height: 32px; }
  .tool { min-width: 48px; height: 50px; font-size: 19px; }
}

.music-toggle.off{opacity:.45;filter:grayscale(1)}
.music-toggle.off::after{content:'';position:absolute;left:22%;right:22%;top:50%;height:3px;background:#e53935;transform:rotate(-40deg);border-radius:2px}
.music-toggle{position:relative}
