* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}
body {
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, sans-serif;
  background: linear-gradient(160deg, #FFE8F3 0%, #FFF6D9 50%, #E3F4FF 100%);
  color: #4a3a5a;
}
button { font: inherit; color: inherit; border: none; background: none; cursor: pointer; touch-action: manipulation; }
.hidden { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Trang chủ ---------- */
#home { display: flex; flex-direction: column; }
.home-head { display: flex; align-items: center; justify-content: center; position: relative; padding: 14px 16px 4px; }
.home-head h1 { font-size: clamp(34px, 6vw, 56px); font-weight: 900; letter-spacing: 1px; }
.home-head h1 span { display: inline-block; text-shadow: 0 3px 0 rgba(0,0,0,.12); animation: bob 2.4s ease-in-out infinite; }
.home-head h1 span:nth-child(7n+1) { color: #FF4B4B; }
.home-head h1 span:nth-child(7n+2) { color: #FF9A3C; animation-delay: .15s; }
.home-head h1 span:nth-child(7n+3) { color: #F2B705; animation-delay: .3s; }
.home-head h1 span:nth-child(7n+4) { color: #3FB55A; animation-delay: .45s; }
.home-head h1 span:nth-child(7n+5) { color: #4D96FF; animation-delay: .6s; }
.home-head h1 span:nth-child(7n+6) { color: #9B5DE5; animation-delay: .75s; }
.home-head h1 span:nth-child(7n+7) { color: #FF6FB5; animation-delay: .9s; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
.round-btn {
  position: absolute; right: 18px; top: 14px;
  width: 60px; height: 60px; border-radius: 50%;
  font-size: 30px; background: #fff; box-shadow: 0 4px 0 #e6d3e8;
}

.install-btn {
  position: absolute; left: 18px; top: 18px;
  height: 52px; padding: 0 16px; border-radius: 26px;
  font-size: 24px; font-weight: 800; background: #fff; box-shadow: 0 4px 0 #e6d3e8;
  display: flex; align-items: center; gap: 6px;
  animation: wiggle 3s ease-in-out infinite;
}
.install-btn span { font-size: 16px; }
@media (max-width: 600px) { .install-btn span { display: none; } }
@keyframes wiggle { 0%, 88%, 100% { transform: rotate(0); } 92% { transform: rotate(-6deg); } 96% { transform: rotate(6deg); } }
.install-box { max-width: min(560px, 92vw); text-align: left; }
.install-head { display: flex; gap: 14px; align-items: center; margin-bottom: 14px; }
.install-head img { width: 64px; height: 64px; border-radius: 16px; }
.install-head b { display: block; font-size: 24px; }
.install-head small { font-size: 16px; color: #8a7898; }
.install-steps { list-style: none; display: flex; flex-direction: column; gap: 12px; margin-bottom: 20px; }
.install-steps li { display: flex; align-items: center; gap: 12px; background: #FFF6FB; border-radius: 18px; padding: 10px 14px; font-size: 18px; }
.step-num { flex: 0 0 auto; width: 30px; height: 30px; border-radius: 50%; background: #FF6FB5; color: #fff; display: grid; place-items: center; font-weight: 900; }
.step-icon { flex: 0 0 auto; width: 34px; display: grid; place-items: center; font-size: 26px; font-weight: 900; }
.install-ok { display: block; margin: 0 auto; width: 140px; height: 70px; font-size: 30px; }

.tabs {
  display: flex; gap: 12px; padding: 8px 22px 16px;
  overflow-x: auto; touch-action: pan-x; scrollbar-width: none; flex: 0 0 auto;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; min-width: 96px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 14px 10px; border-radius: 24px;
  font-size: 16px; font-weight: 800; white-space: nowrap;
  background: #fff; box-shadow: 0 5px 0 #e6d3e8;
  transition: transform .12s;
}
.tab-icon { font-size: 40px; line-height: 1.1; }
.tab.active { background: #FF6FB5; color: #fff; box-shadow: 0 5px 0 #d94f95; transform: translateY(-2px) scale(1.04); }
.tab:active { transform: translateY(3px); }

.grid {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 20px; padding: 8px 22px 40px; align-content: start;
}
.card {
  position: relative; background: #fff; border-radius: 28px; padding: 12px 12px 8px;
  box-shadow: 0 6px 0 #ecdcef, 0 10px 24px rgba(120, 60, 140, .08);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: transform .12s;
}
.card:active { transform: scale(.95); }
.card img { width: 100%; aspect-ratio: 1; border-radius: 18px; pointer-events: none; }
.new-paper { justify-content: center; border: 5px dashed #FFB3D9; }
.new-paper .plus { width: 100%; aspect-ratio: 1; display: grid; place-items: center; font-size: 80px; }
.card-name { font-size: 17px; font-weight: 700; color: #7a6688; }
.badge { position: absolute; top: 6px; right: 8px; font-size: 30px; filter: drop-shadow(0 2px 2px rgba(0,0,0,.2)); }
.empty { grid-column: 1 / -1; text-align: center; font-size: 26px; padding: 60px 20px; color: #9a87a8; }

/* ---------- Màn tô màu ---------- */
#paint { display: flex; gap: 10px; padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
.tools {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 10px 0; overflow-y: auto;
}
.tool {
  width: 68px; height: 68px; flex: 0 0 auto; border-radius: 22px;
  font-size: 36px; background: #fff; box-shadow: 0 5px 0 #e6d3e8;
  transition: transform .1s;
}
.tool:active { transform: translateY(4px); box-shadow: 0 1px 0 #e6d3e8; }
.tool.active { background: #FFF3B0; box-shadow: 0 5px 0 #F2C230, inset 0 0 0 4px #F2C230; transform: scale(1.06); }
.tool[data-tool="fill"].active, .tool[data-tool="brush"].active, .tool[data-tool="water"].active { box-shadow: 0 5px 0 #F2C230, inset 0 0 0 6px var(--cur, #F2C230); }
.tool.disabled { opacity: .35; }
.tool-sep { width: 40px; height: 4px; border-radius: 2px; background: rgba(0,0,0,.08); flex: 0 0 auto; }

.stage { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; container-type: size; padding: 10px 0; }
.board {
  position: relative;
  width: min(100cqw, 100cqh); height: min(100cqw, 100cqh);
  background: #fff; border-radius: 26px; overflow: hidden;
  box-shadow: 0 8px 0 #ecdcef, 0 14px 40px rgba(120, 60, 140, .12);
  touch-action: none;
}
.layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.palette {
  width: 176px; flex: 0 0 auto; overflow-y: auto; touch-action: pan-y;
  display: flex; flex-direction: column; justify-content: center; gap: 12px; padding: 10px 0;
}
.swatches, .palette:has(.sticker) { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; justify-items: center; align-content: center; }
.swatch {
  width: 50px; height: 50px; border-radius: 50%;
  box-shadow: inset 0 -5px 0 rgba(0,0,0,.15), 0 3px 6px rgba(0,0,0,.12);
  transition: transform .12s;
}
.swatch.white { box-shadow: inset 0 0 0 2px #ddd, inset 0 -5px 0 rgba(0,0,0,.08), 0 3px 6px rgba(0,0,0,.12); }
.swatch.active { transform: scale(1.22); outline: 4px solid #fff; box-shadow: 0 0 0 7px #4a3a5a, 0 4px 10px rgba(0,0,0,.2); }
.sizes { display: flex; justify-content: space-around; background: #fff; border-radius: 20px; padding: 6px; box-shadow: 0 4px 0 #e6d3e8; }
.size { width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center; }
.size i { display: block; border-radius: 50%; background: #4a3a5a; }
.size.active { background: #FFF3B0; box-shadow: inset 0 0 0 3px #F2C230; }
.sticker { font-size: 40px; width: 52px; height: 52px; border-radius: 16px; display: grid; place-items: center; }
.sticker.active { background: #fff; box-shadow: 0 0 0 4px #FF6FB5; transform: scale(1.12); }

/* Dọc (iPad cầm đứng): tranh ở trên, công cụ + bảng màu ở dưới */
@media (orientation: portrait) {
  #paint { flex-direction: column; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .tools { flex-direction: row; justify-content: center; padding: 0; overflow-x: auto; order: 2; }
  .tool { width: 62px; height: 62px; font-size: 32px; }
  .tool-sep { width: 4px; height: 40px; }
  .stage { order: 1; }
  .palette { order: 3; width: auto; flex-direction: row; align-items: center; overflow-x: auto; touch-action: pan-x; padding: 4px 0 8px; }
  .swatches, .palette:has(.sticker) { grid-template-columns: repeat(10, 1fr); width: 100%; }
  .sizes { flex-direction: column; }
}

/* ---------- Hộp thoại, tải, hiệu ứng ---------- */
.modal { position: fixed; inset: 0; background: rgba(74, 58, 90, .45); display: grid; place-items: center; z-index: 20; }
.modal-box { background: #fff; border-radius: 36px; padding: 30px 40px; text-align: center; box-shadow: 0 10px 0 #e6d3e8; }
.modal-icon { font-size: 70px; }
.modal-text { font-size: 30px; font-weight: 800; margin: 6px 0 20px; }
.modal-btns { display: flex; gap: 24px; justify-content: center; }
.big-btn { width: 100px; height: 100px; border-radius: 30px; font-size: 50px; color: #fff; font-weight: 900; }
.big-btn.no { background: #9AA0A6; box-shadow: 0 6px 0 #6f757a; }
.big-btn.yes { background: #6BCB77; box-shadow: 0 6px 0 #3f9e4b; }

.viewer { position: fixed; inset: 0; z-index: 18; background: rgba(74, 58, 90, .55); display: grid; place-items: center; padding: 16px; }
.viewer-box { display: flex; flex-direction: column; align-items: center; gap: 16px; max-height: 100%; }
.viewer-box img {
  width: min(78vh, 86vw); aspect-ratio: 1; border-radius: 24px; background: #fff;
  border: 10px solid #fff; box-shadow: 0 0 0 6px #FFC4E1, 0 12px 40px rgba(0,0,0,.25);
}
.viewer-btns { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; }
.vbtn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 96px; padding: 8px 14px 10px; border-radius: 22px;
  background: #fff; box-shadow: 0 5px 0 #e6d3e8; font-size: 17px; font-weight: 800;
}
.vbtn span { font-size: 36px; }
.vbtn:active { transform: translateY(4px); box-shadow: 0 1px 0 #e6d3e8; }
.card.framed img { border: 6px solid #FFE08A; box-sizing: border-box; }
.flash { position: absolute; inset: 0; background: #fff; animation: flash .6s ease-out forwards; pointer-events: none; }
@keyframes flash { from { opacity: .9; } to { opacity: 0; } }

.loading { position: fixed; inset: 0; display: grid; place-items: center; z-index: 15; background: rgba(255,255,255,.4); }
.spinner { font-size: 80px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#fx { position: fixed; inset: 0; pointer-events: none; z-index: 30; }
.spark {
  position: absolute; font-size: 22px; font-weight: 900; transform: translate(-50%, -50%);
  text-shadow: 0 0 2px #fff, 0 0 4px #fff;
  animation: spark .6s ease-out forwards;
}
@keyframes spark {
  to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(.3); opacity: 0; }
}
.confetti { position: absolute; inset: 0; }
.cheer {
  position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%);
  font-size: clamp(48px, 9vw, 96px); font-weight: 900; color: #FF6FB5;
  -webkit-text-stroke: 3px #fff; paint-order: stroke fill;
  text-shadow: 0 6px 0 rgba(0,0,0,.12);
  animation: cheer 2.2s ease-out forwards; white-space: nowrap;
}
@keyframes cheer {
  0% { transform: translate(-50%, -50%) scale(.2); opacity: 0; }
  15% { transform: translate(-50%, -50%) scale(1.15); opacity: 1; }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { transform: translate(-50%, -70%) scale(1); opacity: 0; }
}

/* ---------- Hiệu ứng khen thưởng ---------- */
.balloon {
  position: absolute; bottom: -160px; width: calc(70px * var(--s)); height: calc(86px * var(--s));
  border-radius: 50% 50% 48% 48% / 55% 55% 45% 45%;
  background: radial-gradient(circle at 32% 28%, rgba(255,255,255,.75) 0 10%, transparent 11%), var(--c);
  box-shadow: inset -8px -10px 0 rgba(0,0,0,.12);
  pointer-events: auto; cursor: pointer;
  animation: rise var(--dur) ease-in forwards;
}
.balloon::after { /* nút buộc + dây */
  content: ''; position: absolute; left: 50%; top: 100%; width: 2px; height: 70px;
  background: rgba(80, 60, 90, .45); transform: translateX(-50%);
}
.balloon::before {
  content: ''; position: absolute; left: 50%; bottom: -6px; transform: translateX(-50%);
  border: 6px solid transparent; border-bottom-color: var(--c);
}
.balloon.popped { animation: pop .2s ease-out forwards; }
@keyframes rise {
  0% { transform: translate(0, 0); }
  30% { transform: translate(var(--sway), -40vh); }
  60% { transform: translate(calc(var(--sway) * -1), -80vh); }
  100% { transform: translate(var(--sway), calc(-100vh - 260px)); }
}
@keyframes pop { to { transform: scale(1.8); opacity: 0; } }

.rain {
  position: absolute; top: -80px;
  animation: fall var(--dur) linear forwards;
  filter: drop-shadow(0 3px 3px rgba(0,0,0,.15));
}
@keyframes fall { to { transform: translateY(calc(100vh + 120px)) rotate(var(--spin)); } }

.rainbow-arc {
  position: absolute; left: 50%; top: 52%; width: min(80vw, 720px);
  transform: translate(-50%, -50%) scale(0); opacity: .9;
  animation: arc 3s ease-out forwards;
}
.rainbow-arc svg { width: 100%; display: block; }
@keyframes arc {
  0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
  20% { transform: translate(-50%, -50%) scale(1.05); opacity: .9; }
  30% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: .9; }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.05); }
}

.mascot { position: absolute; bottom: 0; display: flex; flex-direction: column; align-items: center; animation: mascot 3.2s ease-in-out forwards; }
.mascot.left { left: 4vw; }
.mascot.right { right: 4vw; }
.mascot .buddy { font-size: 120px; line-height: 1; animation: hop .45s ease-in-out infinite alternate; }
.mascot .bubble {
  background: #fff; border-radius: 24px; padding: 10px 18px; margin-bottom: 8px;
  font-size: 26px; font-weight: 900; color: #FF6FB5; box-shadow: 0 6px 0 #ecdcef;
  position: relative; white-space: nowrap;
}
.mascot .bubble::after { content: ''; position: absolute; left: 50%; bottom: -14px; transform: translateX(-50%); border: 8px solid transparent; border-top-color: #fff; }
@keyframes mascot {
  0% { transform: translateY(110%); }
  15%, 80% { transform: translateY(-10px); }
  100% { transform: translateY(110%); }
}
@keyframes hop { to { transform: translateY(-14px) rotate(-6deg); } }

.float-word {
  position: absolute; transform: translate(-50%, -50%) rotate(var(--rot));
  font-size: 34px; font-weight: 900; white-space: nowrap;
  -webkit-text-stroke: 3px #fff; paint-order: stroke fill;
  text-shadow: 0 4px 0 rgba(0,0,0,.12);
  animation: floatup 1.3s ease-out forwards;
}
@keyframes floatup {
  0% { transform: translate(-50%, -30%) rotate(var(--rot)) scale(.3); opacity: 0; }
  20% { transform: translate(-50%, -80%) rotate(var(--rot)) scale(1.2); opacity: 1; }
  100% { transform: translate(-50%, -260%) rotate(var(--rot)) scale(1); opacity: 0; }
}

.trophy {
  position: fixed; inset: 0; z-index: 25; display: grid; place-items: center;
  background: rgba(74, 58, 90, .55); animation: fadein .3s ease-out;
}
@keyframes fadein { from { opacity: 0; } }
.trophy-box {
  position: relative; background: #fff; border-radius: 40px; padding: 26px 34px 24px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  box-shadow: 0 0 0 8px #FFD93D, 0 14px 50px rgba(0,0,0,.3);
  margin-top: 56px;
  animation: boing .6s cubic-bezier(.2, 1.6, .4, 1);
}
@keyframes boing { from { transform: scale(.3); } }
.trophy .rays {
  position: absolute; left: 50%; top: 60px; width: 300px; height: 300px; margin: -150px 0 0 -150px; z-index: -1;
  background: repeating-conic-gradient(rgba(255, 217, 61, .55) 0 12deg, transparent 12deg 30deg);
  border-radius: 50%; animation: spin 8s linear infinite;
}
.trophy .medal { font-size: 124px; line-height: 1; margin-top: -96px; animation: hop .5s ease-in-out infinite alternate; filter: drop-shadow(0 6px 4px rgba(0,0,0,.2)); }
.trophy-title { font-size: 30px; font-weight: 900; color: #FF6FB5; text-align: center; }
.trophy img { width: min(38vh, 66vw); aspect-ratio: 1; border-radius: 20px; border: 6px solid #FFE08A; }
.trophy-btns { display: flex; gap: 14px; }

.night {
  position: absolute; inset: 0;
  background: radial-gradient(ellipse at 50% 30%, rgba(40, 30, 90, .55), rgba(20, 10, 50, .7));
  animation: night var(--dur) ease-in-out forwards;
}
@keyframes night { 0% { opacity: 0; } 12%, 80% { opacity: 1; } 100% { opacity: 0; } }
