:root {
  --ink: #2E2547;
  --muted: #7D7396;
  --blue: #2E9BF0;
  --blue-dark: #1C6FB8;
  --pink: #FF7A9C;
  --yellow: #FFD23F;
  --green: #35C77B;
  --panel: rgba(255, 255, 255, .96);
  --shadow: 0 8px 0 rgba(0,0,0,.08), 0 14px 30px rgba(20, 60, 120, .25);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #7FD0FF; color: var(--ink); }
body { user-select: none; -webkit-user-select: none; touch-action: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
.hidden { display: none !important; }

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

.overlay { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: 16px; }

/* ---------- Loading ---------- */
#loading { background: linear-gradient(180deg, #5EC8FF, #BDEBFF); }
.load-box { text-align: center; color: #fff; }
.load-logo { font-size: 96px; animation: bob 2s ease-in-out infinite; }
.load-box h1 { font-size: 46px; margin: 6px 0 18px; text-shadow: 0 4px 0 #2C8BD8; }
.bar { width: min(360px, 80vw); height: 18px; border-radius: 99px; background: rgba(255,255,255,.5); overflow: hidden; margin: 0 auto; }
.bar i { display: block; height: 100%; width: 0; background: var(--yellow); border-radius: 99px; transition: width .2s; }
#loadText { color: #1D5F92; font-weight: 700; }

/* ---------- Creator ---------- */
#creator { background: radial-gradient(circle at 30% 20%, rgba(255,255,255,.5), transparent 60%), linear-gradient(180deg, #8ED8FF, #FFEFD0); }
.creator {
  display: grid; grid-template-columns: minmax(260px, 1fr) minmax(320px, 1.2fr); gap: 20px;
  width: min(1040px, 100%); max-height: 100%;
}
.preview { position: relative; background: radial-gradient(circle at 50% 40%, #FFFFFF, #CFEFFF); border-radius: 36px; box-shadow: var(--shadow); min-height: 300px; overflow: hidden; }
#previewCanvas { width: 100%; height: 100%; display: block; }
.preview-name { position: absolute; left: 0; right: 0; bottom: 18px; text-align: center; font-size: 28px; font-weight: 900; color: var(--blue-dark); }
.options { background: var(--panel); border-radius: 36px; box-shadow: var(--shadow); padding: 22px 24px; overflow-y: auto; max-height: calc(100vh - 32px); }
.options h2 { margin: 0 0 12px; font-size: 26px; }
.opt { margin-bottom: 12px; }
.opt > label { display: block; font-weight: 800; color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }
.row + .row { margin-top: 8px; }
.row button { padding: 9px 14px; border-radius: 14px; background: #F1F5FB; font-weight: 700; border: 3px solid transparent; }
.row button.on { border-color: var(--blue); background: #E3F2FF; }
.swatches button { width: 38px; height: 38px; padding: 0; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px #E1E6EF; }
.swatches button.on { box-shadow: 0 0 0 3px var(--blue); }
#optName { width: 100%; font: inherit; font-size: 20px; font-weight: 700; padding: 10px 14px; border-radius: 14px; border: 3px solid #E1E6EF; outline: none; user-select: text; -webkit-user-select: text; }
#optName:focus { border-color: var(--blue); }
.creator-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.btn { padding: 12px 22px; border-radius: 999px; font-weight: 800; font-size: 18px; transition: transform .12s; }
.btn:active { transform: scale(.95); }
.btn.primary { background: var(--blue); color: #fff; box-shadow: 0 5px 0 var(--blue-dark); }
.btn.ghost { background: #EEF3FA; }
@media (max-width: 760px) {
  .creator { grid-template-columns: 1fr; grid-template-rows: 240px auto; align-self: stretch; }
  .preview { min-height: 0; height: 240px; }
  .options { max-height: calc(100vh - 290px); }
}

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud > * { pointer-events: auto; }
.hud-top { position: absolute; top: max(12px, env(safe-area-inset-top)); left: 12px; right: 12px; display: flex; justify-content: space-between; align-items: flex-start; pointer-events: none; }
.hud-top > * { pointer-events: auto; }
.place {
  background: var(--panel); padding: 10px 18px; border-radius: 999px; box-shadow: var(--shadow);
  font-weight: 900; font-size: 20px; color: var(--blue-dark);
}
.place.flash { animation: pop .5s; }
.hud-right { display: flex; gap: 8px; align-items: center; }
.stars { background: #FFF3C2; padding: 9px 16px; border-radius: 999px; font-size: 20px; font-weight: 900; box-shadow: var(--shadow); }
.stars.flash { animation: pop .5s; }
.hud-btn { width: 48px; height: 48px; border-radius: 50%; background: var(--panel); box-shadow: var(--shadow); font-size: 22px; display: grid; place-items: center; text-decoration: none; }

.dpad {
  position: absolute; left: max(18px, env(safe-area-inset-left)); bottom: max(18px, env(safe-area-inset-bottom));
  width: 192px; height: 192px; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: 4px;
}
.dpad button {
  border-radius: 20px; background: rgba(255,255,255,.88); box-shadow: 0 5px 0 rgba(0,0,0,.12), 0 8px 18px rgba(0,0,0,.15);
  font-size: 26px; color: var(--blue-dark); touch-action: none;
}
.dpad button.on { background: var(--yellow); transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.12); }
.dpad [data-dir=up] { grid-area: 1 / 2; }
.dpad [data-dir=left] { grid-area: 2 / 1; }
.dpad [data-dir=right] { grid-area: 2 / 3; }
.dpad [data-dir=down] { grid-area: 3 / 2; }

.talk-btn {
  position: absolute; right: max(22px, env(safe-area-inset-right)); bottom: max(28px, env(safe-area-inset-bottom));
  width: 128px; height: 128px; border-radius: 50%; background: var(--pink); color: #fff; font-weight: 900; font-size: 20px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  box-shadow: 0 7px 0 #D34E73, 0 14px 28px rgba(0,0,0,.2); animation: pulse 1.4s ease-in-out infinite;
}
.talk-ico { font-size: 44px; line-height: 1; }
.toast {
  position: absolute; left: 50%; top: 84px; transform: translateX(-50%) translateY(-20px); opacity: 0;
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 800; font-size: 18px;
  transition: opacity .25s, transform .25s; pointer-events: none; white-space: nowrap; max-width: 92vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Map ---------- */
#map { background: rgba(20, 40, 70, .45); z-index: 25; }
.map-box { background: var(--panel); border-radius: 32px; padding: 18px; box-shadow: var(--shadow); width: min(920px, 100%); }
.map-head { display: flex; justify-content: space-between; align-items: center; }
.map-head h2 { margin: 0 0 10px; }
#mapCanvas { width: 100%; height: auto; border-radius: 20px; display: block; }
.map-note { color: var(--muted); margin: 10px 0 0; font-size: 14px; }
.x { width: 44px; height: 44px; border-radius: 50%; background: #EEF3FA; font-size: 20px; font-weight: 900; }

/* ---------- Dialog ---------- */
#dialog { position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; display: flex; justify-content: center; padding: 0 12px max(12px, env(safe-area-inset-bottom)); }
.dlg {
  width: min(860px, 100%); background: var(--panel); border-radius: 30px 30px 26px 26px; box-shadow: var(--shadow);
  padding: 14px 18px 16px; animation: up .3s cubic-bezier(.2,1.3,.4,1);
}
.dlg-head { display: flex; align-items: center; gap: 12px; }
.who { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.who-ico { font-size: 30px; }
.who b { font-size: 22px; }
.who small { color: var(--muted); font-weight: 700; }
.dlg-progress { display: flex; gap: 5px; }
.dlg-progress i { width: 14px; height: 14px; border-radius: 50%; background: #E3E8F0; }
.dlg-progress i.done { background: var(--yellow); }
.dlg-progress i.now { background: var(--blue); }
.dlg-body { display: flex; gap: 16px; align-items: center; margin: 10px 0 6px; min-height: 76px; }
.show { font-size: 64px; line-height: 1; }
.show:empty { display: none; }
.lines { flex: 1; }
.en { font-size: clamp(22px, 3.4vw, 30px); font-weight: 900; margin: 0; line-height: 1.25; }
.en .w { border-radius: 6px; transition: background .15s; }
.en .w.lit { background: #FFF0A8; }
.vi { color: var(--muted); font-size: 17px; margin: 4px 0 0; font-weight: 600; }
.heard { min-height: 30px; font-size: 18px; font-weight: 700; color: var(--muted); }
.heard.ok { color: var(--green); }
.heard.bad { color: #E2683C; }
.choices { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 8px; }
.choices:empty { display: none; }
.choices button { background: #EAF5FF; border: 3px solid #BFE1FF; padding: 10px 16px; border-radius: 16px; font-weight: 800; font-size: 18px; }
.choices button:active { transform: scale(.96); }
.dlg-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.mic {
  width: 96px; height: 96px; border-radius: 50%; background: var(--blue); color: #fff; font-weight: 900;
  display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: 0 6px 0 var(--blue-dark);
  transition: transform .12s, background .2s;
}
.mic-ico { font-size: 38px; line-height: 1; }
.mic.listening { background: #FF5A5F; box-shadow: 0 6px 0 #C23A3F, 0 0 0 0 rgba(255,90,95,.5); animation: ring 1.1s infinite; }
.mic.wait { background: #A9B6C8; box-shadow: 0 6px 0 #8593A6; }
.mic:active { transform: scale(.94); }

.fx-star { position: fixed; z-index: 40; font-size: 46px; pointer-events: none; animation: fly 1.2s ease-out forwards; }

@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-12px); } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes ring { 0% { box-shadow: 0 6px 0 #C23A3F, 0 0 0 0 rgba(255,90,95,.55); } 100% { box-shadow: 0 6px 0 #C23A3F, 0 0 0 26px rgba(255,90,95,0); } }
@keyframes fly {
  0% { transform: translate(0,0) scale(.5); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.3) rotate(var(--rot)); opacity: 0; }
}

@media (max-width: 640px), (max-height: 520px) {
  .dpad { width: 150px; height: 150px; }
  .dpad button { font-size: 20px; border-radius: 16px; }
  .talk-btn { width: 100px; height: 100px; font-size: 16px; }
  .talk-ico { font-size: 34px; }
  .place { font-size: 16px; padding: 8px 14px; }
  .mic { width: 80px; height: 80px; }
  .dlg-actions .btn { padding: 10px 14px; font-size: 15px; }
}

/* iPhone/iPad: nút micro nhấp nháy để bé biết cần bấm rồi nói */
.mic.tap { background: var(--green); box-shadow: 0 6px 0 #23955A; animation: pulse 1s ease-in-out infinite; }

/* ---------- Nhạc / tốc độ nói ---------- */
.hud-btn.off { opacity: .45; }
.hud-btn.on { background: var(--yellow); }

/* ---------- Hỏi rời khỏi khu vực ---------- */
#leave { background: rgba(20, 40, 70, .35); z-index: 30; }
.leave-box { background: var(--panel); border-radius: 32px; padding: 26px 28px; box-shadow: var(--shadow); text-align: center; width: min(520px, 100%); animation: up .3s cubic-bezier(.2,1.3,.4,1); }
.leave-ico { font-size: 60px; }
.leave-en { font-size: 26px; font-weight: 900; margin: 6px 0; }
.leave-vi { color: var(--muted); font-size: 18px; margin: 0 0 18px; }
.leave-actions { display: flex; gap: 14px; justify-content: center; }
.btn.big { font-size: 22px; padding: 14px 30px; }

/* ---------- Chuyển cảnh ---------- */
#fade { position: fixed; inset: 0; z-index: 40; pointer-events: none; opacity: 0; transition: opacity .45s;
  background: radial-gradient(circle at 50% 45%, #9BD8FF, #3E9BE8); display: flex; align-items: center; justify-content: center; }
#fade.on { opacity: 1; pointer-events: auto; }
#fadeTitle { color: #fff; font-size: clamp(30px, 6vw, 54px); font-weight: 900; text-shadow: 0 4px 0 #1C6FB8; }

/* ---------- Sao lưu tiến trình ---------- */
.backup { margin-top: 16px; background: #F4F8FD; border-radius: 18px; padding: 10px 14px; }
.backup summary { font-weight: 800; cursor: pointer; color: var(--blue-dark); }
.backup p { color: var(--muted); font-size: 14px; line-height: 1.45; margin: 8px 0; }
.backup textarea { width: 100%; font: 13px ui-monospace, monospace; border: 2px solid #E1E6EF; border-radius: 12px; padding: 8px; margin: 8px 0; user-select: text; -webkit-user-select: text; }
.backup .btn { font-size: 15px; padding: 9px 16px; }
.backup-msg { font-weight: 700; }
.backup-msg.ok { color: var(--green) !important; }
.backup-msg.bad { color: #E2683C !important; }

/* ---------- Tác giả ---------- */
.credit { font-size: 13px; color: var(--muted); text-align: center; margin: 12px 0 0; }
.credit a { color: var(--blue-dark); font-weight: 800; text-decoration: none; }
#loading .credit { color: #1D5F92; margin-top: 26px; }

/* Nút ủng hộ tác giả (bảng ủng hộ ở /donate.js) */
.btn.donate { background: #FFF0F6; color: #E64980; margin-right: auto; }

/* ---------- Thanh trên cùng trên điện thoại ---------- */
.place, .stars { white-space: nowrap; }
.hud-right { position: relative; }
.hud-extra { display: contents; }
#btnMore { display: none; font-weight: 900; }
@media (max-width: 600px) {
  .hud-top { left: 10px; right: 10px; gap: 8px; }
  .place { font-size: 14px; padding: 8px 12px; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; }
  .stars { font-size: 15px; padding: 7px 11px; }
  .hud-right { gap: 6px; }
  .hud-btn { width: 40px; height: 40px; font-size: 19px; }
  #btnMore { display: grid; }
  .hud-extra { display: none; position: absolute; top: 48px; right: 0; flex-direction: column; gap: 6px;
    background: rgba(255,255,255,.9); padding: 6px; border-radius: 26px; box-shadow: var(--shadow); }
  .hud-extra.open { display: flex; }
  .toast { top: 64px; font-size: 15px; white-space: normal; text-align: center; border-radius: 18px; }
}

/* Đang trò chuyện: ẩn bàn phím mũi tên để không che hộp thoại */
#hud.talking .dpad, #hud.talking .talk-btn { display: none; }
