:root {
  --gold: #d9a441;
  --panel: rgba(28, 20, 14, 0.72);
  --panel-solid: #241a12;
  --text: #f5ead6;
  --muted: #c7b79b;
  --line: rgba(217, 164, 65, 0.35);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #1a120c; color: var(--text);
  font-family: 'Be Vietnam Pro', system-ui, sans-serif; -webkit-tap-highlight-color: transparent; }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
button { font: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* Màn hình chờ */
#loading { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: radial-gradient(circle at 50% 40%, #4a2a14 0%, #1a100a 70%); transition: opacity .8s; }
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner { text-align: center; padding: 24px; max-width: 420px; }
.lotus { font-size: 54px; color: var(--gold); animation: breathe 3s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.08); opacity: .8; } }
.ten-chua { font-family: 'Lora', serif; font-weight: 600; font-size: 34px; margin: 8px 0 4px; color: var(--gold); }
.loading-text { color: var(--muted); margin: 0 0 18px; }
.bar { height: 4px; background: rgba(255,255,255,.1); border-radius: 4px; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--gold); transition: width .3s; }
.quote { font-family: 'Lora', serif; font-style: italic; color: var(--muted); margin-top: 22px; }

/* Thanh trên */
#topbar { position: fixed; top: 0; left: 0; right: 0; z-index: 10; display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; background: linear-gradient(rgba(20,12,6,.8), rgba(20,12,6,0)); flex-wrap: wrap; }
.brand { font-family: 'Lora', serif; font-weight: 600; color: var(--gold); font-size: 18px; margin-right: 6px; }
.brand .sep { margin: 0 6px; color: var(--muted); }
.brand #khu-vuc { color: var(--text); font-family: 'Be Vietnam Pro'; font-weight: 500; font-size: 14px; }
.pill { background: var(--panel); border: 1px solid var(--line); padding: 4px 10px; border-radius: 999px; font-size: 13px; }
#online-pill { color: #9fdc8a; }
#online-pill b { color: var(--text); }
.spacer { flex: 1; }
.icon-btn { background: var(--panel); border: 1px solid var(--line); color: var(--text); width: 34px; height: 34px; border-radius: 50%; }
#notice { position: fixed; top: 58px; left: 50%; transform: translateX(-50%); z-index: 9; background: var(--panel);
  border: 1px solid var(--line); padding: 6px 14px; border-radius: 999px; font-size: 13px; color: var(--muted);
  max-width: calc(100% - 32px); text-align: center; }

/* Thắp hương */
#btn-huong { position: fixed; left: 50%; bottom: 196px; transform: translateX(-50%); z-index: 12; background: var(--gold);
  color: #2a1a0a; border: 0; padding: 12px 22px; border-radius: 999px; font-weight: 600; font-size: 16px;
  box-shadow: 0 6px 24px rgba(217,164,65,.45); animation: glow 2s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 6px 34px rgba(255,190,80,.8); } }

/* Chat */
#chat-log { position: fixed; left: 14px; bottom: 80px; z-index: 11; width: min(360px, calc(100% - 28px)); pointer-events: none;
  max-height: 180px; overflow-y: auto; display: flex; flex-direction: column; gap: 3px;
  font-size: 14px; text-shadow: 0 1px 3px #000; mask-image: linear-gradient(transparent, #000 25%); }
#chat-log .line b { color: var(--gold); font-weight: 600; }
#chat-log .sys { color: #9fdc8a; font-style: italic; }
#chat-form { display: flex; align-items: center; gap: 4px; border-radius: 16px; padding: 5px 6px 5px 12px; width: 320px; flex: 0 0 auto; }
#chat-input { flex: 1; min-width: 0; background: rgba(255,255,255,.08); border: 0; outline: 0; color: var(--text); padding: 8px 14px; border-radius: 12px; font: inherit; font-size: 16px; }
#chat-form button { background: transparent; border: 0; color: var(--text); padding: 6px 8px; display: grid; place-items: center; }
#chat-form .ico { display: grid; place-items: center; color: var(--muted); }

/* Nút di chuyển (trên hàng công cụ) */
#dpad { position: fixed; right: 18px; bottom: 84px; z-index: 11; display: grid; gap: 6px;
  grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(3, 56px);
  grid-template-areas: ". up ." "left mid right" ". down ."; touch-action: none; user-select: none; -webkit-user-select: none; }
#dpad button { background: var(--panel); border: 1px solid var(--line); color: var(--text); border-radius: 14px;
  font-size: 20px; touch-action: none; -webkit-touch-callout: none; box-shadow: 0 3px 12px rgba(0,0,0,.35); }
#dpad button.on { background: var(--gold); color: #2a1a0a; }
#dpad .up { grid-area: up; } #dpad .down { grid-area: down; } #dpad .left { grid-area: left; } #dpad .right { grid-area: right; }
#dpad .mid { grid-area: mid; border-radius: 50%; font-size: 22px; }

/* Nhãn tên + bong bóng chat */
#labels { position: fixed; inset: 0; pointer-events: none; z-index: 5; overflow: hidden; }
.label { position: absolute; transform: translate(-50%, -100%); text-align: center; white-space: nowrap; will-change: transform; }
.label .nm { font-size: 12px; font-weight: 600; color: #fff; text-shadow: 0 1px 3px #000, 0 0 2px #000; }
.label.me .nm { color: var(--gold); }
.label .bubble { display: inline-block; max-width: 220px; white-space: normal; background: rgba(255,250,240,.95); color: #2a1a0a;
  font-size: 13px; padding: 5px 10px; border-radius: 12px; margin-bottom: 4px; }

/* Hướng dẫn */
#tutorial { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(0,0,0,.25); }
#tutorial .card { width: min(340px, calc(100% - 32px)); background: var(--panel-solid); border: 1px solid var(--line);
  border-radius: 16px; padding: 18px 20px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
.step-no { color: var(--muted); font-size: 13px; }
#tutorial h3 { font-family: 'Lora', serif; color: var(--gold); margin: 6px 0; }
#tutorial p { margin: 0 0 14px; line-height: 1.5; }
.dots { display: flex; gap: 6px; margin-bottom: 14px; }
.dots span { width: 6px; height: 6px; border-radius: 3px; background: rgba(255,255,255,.25); }
.dots span.on { width: 18px; background: var(--gold); }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.actions button { border: 0; border-radius: 999px; padding: 8px 16px; background: var(--gold); color: #2a1a0a; font-weight: 600; }
.actions .ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }

@media (max-width: 600px), (max-height: 500px) {
  .brand { font-size: 16px; }
  #room-pill { display: none; }
  #chat-log { max-height: 110px; font-size: 13px; padding-right: 160px; bottom: 236px; }
  #btn-huong { bottom: 250px; }
  #dpad { right: 10px; bottom: 236px; grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(3, 48px); gap: 5px; }
}

/* Thanh công cụ dưới */
.glass { background: rgba(48, 34, 22, 0.78); border: 1px solid rgba(255,255,255,.08); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 6px 24px rgba(0,0,0,.35); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
#dock { position: fixed; left: 14px; right: 14px; bottom: 12px; z-index: 12; display: flex; align-items: flex-end; gap: 10px; pointer-events: none; }
#dock > * { pointer-events: auto; }
#dock-mid { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px; pointer-events: none; }
#dock-mid > * { pointer-events: auto; }
#actions { display: flex; gap: 2px; border-radius: 16px; padding: 6px; }
.act { background: transparent; border: 0; color: var(--text); display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 6px 12px; border-radius: 10px; font-size: 13px; min-width: 64px; }
.act:hover, #tools button:hover { background: rgba(255,255,255,.08); }
.act:active { transform: scale(.95); }
#tools { display: flex; gap: 8px; max-width: 100%; min-width: 0; overflow-x: auto; scrollbar-width: none; padding: 1px; }
#tools::-webkit-scrollbar { display: none; }
#tools button { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; background: rgba(48, 34, 22, 0.82); color: var(--text);
  border: 1px solid rgba(255,255,255,.08); border-radius: 12px; padding: 8px 14px; font-size: 14px; white-space: nowrap; }
#tools button svg { color: var(--gold); width: 18px; height: 18px; }
#tools button.on { border-color: var(--gold); }

/* Bảng của từng mục */
#sheet { position: fixed; left: 50%; bottom: 176px; transform: translateX(-50%); z-index: 13; width: min(380px, calc(100% - 24px));
  max-height: 50vh; overflow-y: auto; border-radius: 16px; padding: 14px 16px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h3 { margin: 0; font-family: 'Lora', serif; color: var(--gold); font-size: 18px; }
#sheet-close { background: transparent; border: 0; color: var(--muted); font-size: 16px; }
#sheet-body .row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid rgba(255,255,255,.06); font-size: 14px; }
#sheet-body .row > span:first-child { color: var(--muted); }
#sheet-body .list button { width: 100%; text-align: left; background: rgba(255,255,255,.05); border: 0; color: var(--text);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 6px; font-size: 14px; }
#sheet-body .list button:hover { background: rgba(255,255,255,.1); }
#sheet-body .list small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
#sheet-body .btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.btn { border: 0; border-radius: 999px; padding: 8px 16px; background: var(--gold); color: #2a1a0a; font-weight: 600; }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
#sheet-body .person { padding: 7px 0; border-bottom: 1px solid rgba(255,255,255,.06); font-size: 14px; }
#sheet-body .person.me { color: var(--gold); }

@media (max-width: 600px), (max-height: 500px) {
  #dock { left: 8px; right: 8px; bottom: 8px; flex-direction: column; align-items: stretch; gap: 6px; }
  #dock-mid { order: -1; gap: 6px; }
  #chat-form { width: auto; }
  #tools { justify-content: flex-start; }
  .act { min-width: 56px; padding: 5px 8px; font-size: 12px; }
  #tools button { padding: 7px 11px; font-size: 13px; }
  #sheet { bottom: 196px; }
}
#btn-sound, #btn-troi { width: auto; border-radius: 999px; padding: 0 12px; font-size: 13px; white-space: nowrap; }
#chat-form.off { opacity: .6; }
#chat-form.off input { cursor: not-allowed; }

/* Chọn Tín Nam / Tín Nữ */
#chon-gt { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: rgba(0,0,0,.45); }
#chon-gt .card { width: min(380px, calc(100% - 32px)); background: var(--panel-solid); border: 1px solid var(--line);
  border-radius: 18px; padding: 22px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
#chon-gt h3 { font-family: 'Lora', serif; color: var(--gold); margin: 0 0 6px; font-size: 20px; }
#chon-gt p { margin: 0 0 14px; color: var(--muted); }
#chon-gt .hint { font-size: 13px; margin: 14px 0 0; }
.gt-btns { display: flex; gap: 12px; }
.gt-btns button { flex: 1; display: flex; flex-direction: column; gap: 4px; align-items: center; padding: 16px 10px; border-radius: 14px;
  border: 1px solid var(--line); color: var(--text); font-size: 17px; }
.gt-btns button[data-g=nam] { background: linear-gradient(160deg, rgba(79,109,138,.55), rgba(40,55,75,.55)); }
.gt-btns button[data-g=nu] { background: linear-gradient(160deg, rgba(200,97,58,.55), rgba(120,45,40,.55)); }
.gt-btns button:hover { border-color: var(--gold); }
.gt-btns small { font-size: 12px; color: var(--muted); }
#sheet-body .list button:disabled { opacity: .6; cursor: default; }

/* ---------- Điện thoại: 1 hàng nút gọn (vuốt ngang) + ô chat; lời chat ngắn; nút di chuyển nhỏ ---------- */
@media (max-width: 600px), (max-height: 500px) {
  #topbar { padding: 8px 10px; gap: 6px; }
  #notice { top: 50px; font-size: 12px; padding: 4px 10px; }
  #dock { gap: 6px; }
  #dock-mid { flex-wrap: nowrap; justify-content: flex-start; overflow-x: auto; scrollbar-width: none; gap: 4px;
    background: rgba(48, 34, 22, 0.78); border: 1px solid rgba(255,255,255,.08); border-radius: 14px; padding: 4px; pointer-events: auto; }
  #dock-mid::-webkit-scrollbar { display: none; }
  #actions { background: transparent; border: 0; box-shadow: none; padding: 0; gap: 0; backdrop-filter: none; -webkit-backdrop-filter: none; flex: 0 0 auto; }
  #tools { flex: 0 0 auto; overflow: visible; gap: 0; padding: 0; }
  .act, #tools button { flex: 0 0 auto; flex-direction: column; gap: 3px; min-width: 54px; padding: 5px 4px; font-size: 11px;
    background: transparent; border: 0; border-radius: 10px; box-shadow: none; }
  #tools button { border-left: 0; }
  #actions + #tools { border-left: 1px solid rgba(255,255,255,.12); margin-left: 2px; padding-left: 2px; }
  .act svg, #tools button svg { width: 20px; height: 20px; }
  #chat-form { padding: 3px 4px 3px 10px; border-radius: 14px; }
  #chat-input { padding: 7px 10px; }
  #chat-log { max-height: 66px; font-size: 12px; line-height: 1.35; width: calc(100% - 150px); padding-right: 0; }
  #dpad { right: 8px; grid-template-columns: repeat(3, 42px); grid-template-rows: repeat(3, 42px); gap: 4px; opacity: .85; }
  #dpad button { font-size: 16px; border-radius: 12px; }
  #btn-huong { padding: 9px 18px; font-size: 14px; }
}
#dpad .zo { grid-column: 1; grid-row: 1; border-radius: 50%; font-size: 20px; opacity: .9; }
#dpad .zi { grid-column: 3; grid-row: 1; border-radius: 50%; font-size: 20px; opacity: .9; }

/* Đồng hồ + lịch âm */
#clock { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
#clock b { color: var(--gold); font-size: 14px; }
#clock span { color: var(--muted); font-size: 12px; }
#clock #clock-am { color: var(--text); }
@media (max-width: 600px), (max-height: 500px) {
  #clock { gap: 6px; padding: 4px 8px; }
  #clock-duong { display: none; }
}

/* Cần điều khiển (thay cụm mũi tên): mờ khi không dùng, rõ khi chạm */
#dpad { display: flex; flex-direction: column; align-items: center; gap: 10px; grid-template-columns: none; grid-template-rows: none; }
#dpad .zoom-row { display: flex; gap: 10px; }
#dpad .zoom-row button { width: 40px; height: 40px; border-radius: 50%; font-size: 18px; padding: 0; opacity: .7; }
#joy { position: relative; width: 124px; height: 124px; border-radius: 50%; background: rgba(40, 30, 22, 0.38);
  border: 1px solid rgba(255,255,255,.14); touch-action: none; opacity: .45; transition: opacity .25s; box-shadow: 0 4px 18px rgba(0,0,0,.25); }
#joy.on { opacity: 1; }
#joy-knob { position: absolute; left: 50%; top: 50%; width: 54px; height: 54px; margin: -27px 0 0 -27px; border-radius: 50%;
  background: #efd49b; box-shadow: 0 3px 10px rgba(0,0,0,.35); pointer-events: none; }
@media (max-width: 600px), (max-height: 500px) {
  #joy { width: 112px; height: 112px; }
  #joy-knob { width: 48px; height: 48px; margin: -24px 0 0 -24px; }
  #dpad .zoom-row button { width: 36px; height: 36px; font-size: 16px; }
}
#dpad .zoom-row #pad-run.on { opacity: 1; background: #efd49b; }
