:root {
  --muc: #2E2352;
  --nhat: #6A5F8E;
  --giay: #FFFDF6;
  --troi: #BFE6FF;
  --co: #D9F4C7;
  --sang: #FFF3D6;
  --dam: #8CCB7E;
  --chinh: #5B3FD1;
  --vang: #FFD23F;
  --bong: 0 6px 0 rgba(46, 35, 82, .14);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, sans-serif;
  color: var(--muc);
  background: linear-gradient(180deg, var(--troi), var(--co));
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 4px solid var(--chinh); outline-offset: 2px; }

.man {
  height: 100dvh; width: 100%; overflow: auto;
  padding: calc(16px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(20px + env(safe-area-inset-left));
}

/* Trang chính */
#nha { display: flex; flex-direction: column; gap: 14px; }
#nha header { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
h1 { font-size: clamp(28px, 4.6vw, 46px); }
.nho { background: rgba(255, 255, 255, .7); border-radius: 99px; padding: 12px 18px; font-size: 16px; color: var(--nhat); position: relative; overflow: hidden; flex: none; }
.nho.giu { background: linear-gradient(90deg, var(--vang) 50%, rgba(255, 255, 255, .7) 50%) right / 200% 100%; animation: day 1.5s linear forwards; }
@keyframes day { to { background-position: left; } }
#luoi { flex: 1; display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 1fr; gap: 16px; min-height: 0; }
.the { background: var(--giay); border-radius: 28px; box-shadow: var(--bong); border: 5px solid var(--mau); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 10px; min-height: 190px; min-width: 0; }
.the:active { transform: translateY(4px); box-shadow: none; }
.the .hinh { height: min(18vh, 160px); width: 100%; display: flex; justify-content: center; }
.the .hinh svg { height: 100%; width: auto; aspect-ratio: 1; }
.the .hinh .doi { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; }
.the .hinh .doi svg { width: 55%; height: auto; max-height: 100%; margin: 0 -5%; }
.the b { font-size: clamp(20px, 2.4vw, 28px); }
.the small { font-size: clamp(14px, 1.6vw, 18px); color: var(--nhat); text-align: center; }
.sao-day i { font-style: normal; font-size: 22px; color: #DCD7EA; }
.sao-day i.co { color: #F2A900; }
.the.goi-y { animation: moi 1.8s ease-in-out infinite; }
@keyframes moi { 50% { transform: scale(1.035); } }

/* Màn chơi */
#choi { --ban: min(calc(100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc(100vw - 390px)); display: flex; align-items: center; justify-content: center; gap: 24px; overflow: hidden; }
.khung-ban { background: #7A5A3A; padding: 8px; border-radius: 22px; box-shadow: var(--bong); flex: none; }
#ban { width: calc(var(--ban) - 16px); height: calc(var(--ban) - 16px); display: grid; grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr); position: relative; border-radius: 14px; overflow: hidden; }
.o { background: var(--sang); position: relative; min-width: 0; min-height: 0; }
.o.dam { background: var(--dam); }
.o.dich { box-shadow: inset 0 6px 0 var(--vang); }
.o.suoi { background: repeating-radial-gradient(circle at 50% 120%, #4FB6F0 0 12%, #77CBF7 12% 24%); }
.o.chon { background: #FFE27A; }
.o.vua { box-shadow: inset 0 0 0 5px rgba(91, 63, 209, .45); }
.o.di::after { content: ""; position: absolute; inset: 32%; border-radius: 50%; background: var(--chinh); opacity: .62; box-shadow: 0 0 0 5px rgba(255, 255, 255, .75); }
.o.an::after { content: ""; position: absolute; inset: 4%; border-radius: 50%; border: 6px solid var(--chinh); z-index: 4; }
.o.nhac { animation: nhac .8s ease-in-out infinite alternate; }
@keyframes nhac { to { background: #FF9E3D; } }

.lop-quan { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.vat { position: absolute; top: 0; left: 0; width: calc(100% / var(--n)); height: calc(100% / var(--n)); transition: transform .3s ease; }
.vat .ruot { width: 100%; height: 100%; padding: 6%; }
.vat svg { width: 100%; height: 100%; display: block; }
.vat.quan { z-index: 2; }
.vat.cho .ruot { animation: cho 1.1s ease-in-out infinite; }
@keyframes cho { 50% { transform: translateY(-7%) scale(1.06); } }
.vat.bay .ruot { animation: bay .32s ease; }
@keyframes bay { 50% { transform: translateY(-45%) scale(1.25); } }
.vat.lac .ruot { animation: lac .4s ease; }
@keyframes lac { 20%, 60% { transform: rotate(-12deg); } 40%, 80% { transform: rotate(12deg); } }
.vat.sao .ruot { animation: xoay 2s ease-in-out infinite; }
@keyframes xoay { 50% { transform: rotate(9deg) scale(.9); } }
.vat.muc .ruot { transition: opacity .3s, transform .3s; }
.vat.muc.mat .ruot { animation: none; opacity: 0; transform: scale(1.8) rotate(40deg); }

aside { width: 330px; align-self: stretch; display: flex; flex-direction: column; justify-content: center; gap: 16px; min-width: 0; }
.dau { display: flex; align-items: center; gap: 14px; }
.dau h2 { font-size: 28px; }
.ve-nha { width: 60px; height: 60px; border-radius: 20px; background: var(--giay); box-shadow: var(--bong); display: grid; place-items: center; flex: none; }
.ve-nha svg { width: 32px; height: 32px; }
.noi { background: var(--giay); border-radius: 24px; box-shadow: var(--bong); padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
#hinh-ban { width: min(110px, 14vh); aspect-ratio: 1; }
#hinh-ban svg { width: 100%; height: 100%; }
#loi { font-size: 21px; line-height: 1.4; text-align: center; min-height: 4.2em; }
#cham { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
#cham:empty { display: none; }
.cham-bai { width: 50px; height: 50px; border-radius: 50%; background: rgba(255, 255, 255, .75); font-size: 20px; font-weight: 700; }
.cham-bai.xong { color: #D18F00; font-size: 26px; }
.cham-bai.dang { background: var(--giay); box-shadow: 0 0 0 4px var(--chinh); }
#nut { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.nut { background: var(--giay); border-radius: 20px; box-shadow: var(--bong); min-height: 62px; padding: 10px 14px; font-size: 19px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.nut:active { transform: translateY(4px); box-shadow: none; }
.nut.to { background: var(--chinh); color: #fff; font-size: 24px; min-height: 68px; padding: 12px 28px; }
.nut.phu-2 { background: #ECE8F8; color: var(--muc); }
.nut.nhay { animation: moi .8s ease-in-out infinite; background: var(--vang); }

/* Chọn trận và góc bố mẹ */
#chon-tran, #goc-bo-me { display: flex; }
.hop { margin: auto; width: min(760px, 100%); display: flex; flex-direction: column; gap: 14px; }
.hop h3 { font-size: 22px; }
.nhom { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.nhom button { background: var(--giay); border-radius: 24px; box-shadow: var(--bong); border: 5px solid transparent; padding: 14px 8px; font-size: 34px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.nhom button b { font-size: 22px; }
.nhom button small { font-size: 15px; color: var(--nhat); }
.nhom button.chon { border-color: var(--chinh); background: #FFF6CF; }
#bat-dau { align-self: center; margin-top: 8px; }
.hop.chu { background: var(--giay); border-radius: 28px; padding: 24px; box-shadow: var(--bong); font-size: 18px; line-height: 1.55; -webkit-user-select: text; user-select: text; }
.hop.chu label { display: flex; align-items: center; gap: 12px; font-size: 20px; min-height: 44px; }
.hop.chu input { width: 28px; height: 28px; accent-color: var(--chinh); }
.hop.chu ul { padding-left: 22px; }
.hop.chu .nut { background: #ECE8F8; }
.canh-bao { background: #FFF1C2; border-radius: 14px; padding: 12px 14px; }

/* Bảng chúc mừng */
#phu { position: fixed; inset: 0; background: rgba(46, 35, 82, .45); display: grid; place-items: center; padding: 20px; z-index: 20; }
.bang { background: var(--giay); border-radius: 32px; padding: 26px 30px 30px; width: min(480px, 100%); max-height: 100%; overflow: auto; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; animation: bat .35s ease; }
@keyframes bat { from { transform: scale(.6); opacity: 0; } }
.bang h2 { font-size: 36px; }
.bang p { font-size: 21px; }
#phu-hinh { display: flex; align-items: center; justify-content: center; gap: 8px; }
.mung { width: 120px; animation: tung 1s ease infinite alternate; }
.mung-quan { width: 100px; }
.mung svg, .mung-quan svg { width: 100%; display: block; }
@keyframes tung { from { transform: rotate(-12deg) scale(.92); } to { transform: rotate(12deg) scale(1.08); } }
#phu-nut { display: flex; flex-wrap: wrap; justify-content: center; gap: 14px; margin-top: 6px; }

/* iPad cầm dọc, màn hẹp */
@media (orientation: portrait) {
  #luoi { grid-template-columns: repeat(2, 1fr); }
  #luoi .the:last-child { grid-column: span 2; }
  #choi { --ban: min(calc(100vw - 40px), calc(100dvh - 430px)); flex-direction: column; gap: 14px; overflow: auto; }
  aside { width: min(720px, 100%); align-self: center; justify-content: flex-start; gap: 12px; }
  .noi { flex-direction: row; padding: 12px 16px; }
  #hinh-ban { width: 80px; flex: none; }
  #loi { text-align: left; min-height: 2.8em; flex: 1; }
  #nut { grid-template-columns: repeat(4, 1fr); }
  .nut { flex-direction: column; gap: 2px; font-size: 17px; }
  .nut.to { flex-direction: row; }
}
@media (max-width: 600px) {
  .man { padding-left: 10px; padding-right: 10px; }
  #choi { --ban: min(calc(100vw - 20px), calc(100dvh - 380px)); }
  #nut { gap: 8px; }
  .nut { font-size: 14px; padding: 8px 4px; }
  #loi { font-size: 17px; }
  .cham-bai { width: 40px; height: 40px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  #choi { --ban: calc(100dvh - 24px); }
  #hinh-ban { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .the.goi-y, .vat .ruot, .mung, .o.nhac, .nut.nhay { animation: none !important; }
  .o.nhac { background: #FF9E3D; }
}
