:root {
  --mobile-safe-left: env(safe-area-inset-left, 0px);
  --mobile-safe-right: env(safe-area-inset-right, 0px);
  --mobile-safe-top: env(safe-area-inset-top, 0px);
  --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
}
#mobile-safe-area { position: fixed; visibility: hidden; pointer-events: none; padding: var(--mobile-safe-top) var(--mobile-safe-right) var(--mobile-safe-bottom) var(--mobile-safe-left); }
body.mobile { overflow: hidden; overscroll-behavior: none; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body.mobile #hud {
  inset: auto; left: var(--mobile-left); top: var(--mobile-top);
  width: var(--mobile-width); height: var(--mobile-height); overflow: clip;
}
body.mobile #mob { inset: 0; }
body.mobile #bar.v2 { zoom: 1; width: min(640px, 100%); height: 62px; bottom: 0; }
body.mobile #barfill { zoom: 1; height: 62px; }
/* quả cầu HP/MP: 62 px + viền 2 px × 2 = 66 px > thanh 62 px → lòi 2-4 px ra khỏi đáy màn hình; tính cả viền vào 62 px */
body.mobile #bar.v2 .orb { box-sizing: border-box; width: 62px; height: 62px; }
body.mobile #bar.v2 :is(.hot,.mbtn,#skillslot) {
  width: 40px !important; height: 40px !important; min-width: 40px; min-height: 40px;
  top: auto !important; bottom: 0 !important; box-sizing: border-box;
}
body.mobile #bar.v2 #hot-q { left: calc(50% - 174px) !important; }
body.mobile #bar.v2 #hot-w { left: calc(50% - 130px) !important; }
body.mobile #bar.v2 #hot-e { left: calc(50% - 86px) !important; }
body.mobile #bar.v2 #skillslot { left: calc(50% - 42px) !important; }
body.mobile #bar.v2 #mb-char { left: calc(50% + 2px) !important; }
body.mobile #bar.v2 #mb-inv { left: calc(50% + 46px) !important; }
body.mobile #bar.v2 #mb-skill { left: calc(50% + 90px) !important; }
body.mobile #bar.v2 #mb-menu { left: calc(50% + 134px) !important; }
body.mobile #bar.v2 #exp { left: 72px !important; width: calc(100% - 144px) !important; top: 2px !important; height: 14px !important; }
body.mobile #bar.v2 #exptxt { font-size: 12px !important; line-height: 14px !important; }
body.mobile #thpt { font-size: 12px; }
body.mobile #bar.v2 #lvbox { display: none; }
body.mobile #bar.v2 .mbtn::before { font-size: 12px !important; }
body.mobile #bar.v2 .hot::after { position: absolute; top: 0; left: 2px; color: #ffe7a0; font: bold 12px Arial,sans-serif; text-shadow: 0 1px 2px #000; }
body.mobile #hot-q::after { content: 'Q'; }
body.mobile #hot-w::after { content: 'W'; }
body.mobile #hot-e::after { content: 'E'; }
body.mobile #joy { left: 12px; bottom: 74px; }
body.mobile #mpad { right: 12px; bottom: 74px; }
body.mobile #mmenu { right: 6px; top: 6px; max-height: calc(100% - 12px); overflow-y: auto; overscroll-behavior: contain; }
body.mobile #mmlist { touch-action: pan-y; }
body.mobile #topbar {
  left: 12px !important; top: 6px !important; bottom: auto !important;
  transform: none !important; max-width: calc(100% - 248px); overflow-x: auto;
  overscroll-behavior: contain; touch-action: pan-x; gap: 4px !important;
}
body.mobile #topbar button { flex: 0 0 44px; }
body.mobile #charinfo { zoom: 1; left: 12px; top: 56px; max-width: 180px; font-size: 12px; overflow-wrap: anywhere; }
body.mobile #info { zoom: 1; top: 32px !important; right: 60px; max-width: 172px; max-height: min(116px, max(16px, calc(100% - 110px - 182px * var(--mz)))); box-sizing: border-box; overflow: auto; font-size: 12px; overflow-wrap: anywhere; pointer-events: auto; touch-action: pan-y; }
body.mobile #ping { right: 60px; top: 6px !important; transform: none; white-space: nowrap; }
body.mobile #ping > * { flex-shrink: 0; }
body.mobile #dayclock { right: 12px; top: 56px; width: 40px; height: 40px; line-height: 36px; transform: none; }
body.mobile #target { top: 56px !important; min-width: 180px; max-width: min(240px, 40%); }
body.mobile #pethud { left: 50% !important; top: 100px !important; max-width: 40%; }
body.mobile #partyhud { zoom: 1; left: 12px; top: 100px; max-height: max(44px, calc(100% - 136px * var(--mz) - 182px)); overflow-y: auto; touch-action: pan-y; }
body.mobile #partyhud :is(button,.ptk) { min-width: 40px; min-height: 40px; }
body.mobile #partyhud .ptm { min-height: 40px; box-sizing: border-box; }
body.mobile #buffbar { zoom: 1; top: 52px !important; max-width: 40%; overflow-x: auto; }
body.mobile #buffbar .bf { min-width: 40px !important; min-height: 40px; }
body.mobile #radar { display: none; }
body.mobile #bigmap { inset: var(--mobile-top) auto auto var(--mobile-left); width: var(--mobile-width); height: var(--mobile-height); }
body.mobile #bigmapui { right: calc(var(--mobile-safe-right) + 8px); top: calc(var(--mobile-safe-top) + 56px); max-height: calc(var(--mobile-height) - 64px); overflow-y: auto; touch-action: pan-y; }
body.mobile #bigmapui button { min-width: 40px; min-height: 40px; }
body.mobile #skillpop { z-index: 8; /* trên khung chat (z 4): chat mở không che danh sách skill */ bottom: 74px; max-width: calc(100% - 24px); max-height: calc(100% - 86px); overflow: auto; touch-action: pan-y; }
body.mobile #chat, body.mobile:has(#partyhud:not([hidden])) #chat {
  zoom: 1; left: calc(24px + 136px * var(--mz)); top: 100px; bottom: auto;
  width: min(320px, calc(100% - 372px * var(--mz) - 48px)); max-height: calc(100% - 174px);
  display: flex; flex-direction: column; overflow: hidden; font-size: 12px;
}
body.mobile #chat.hide { display: none; }
body.mobile #chatbox { min-height: 0; height: 110px; flex: 1 1 auto; touch-action: pan-y; }
body.mobile #chatbar, body.mobile #chattabs { flex: none; }
body.mobile #chatbar { flex-wrap: nowrap; align-items: center; }
body.mobile #chatbar button { min-width: 40px; min-height: 40px; }
body.mobile #chatbar > button { flex: 0 0 40px; padding: 0; box-sizing: border-box; }
body.mobile #chatbar #chathide { background: linear-gradient(#3a2a18,#1a120a); border: 1px solid #5a3e1a; color: #c9b48a; cursor: pointer; }
body.mobile #chatbar input { font-size: 16px; min-height: 40px; box-sizing: border-box; }
body.mobile #chattabs { flex-wrap: wrap; }
body.mobile #chattabs button { min-width: 40px; min-height: 40px; }
body.mobile #chatwho { width: 60px; max-width: 28%; min-width: 0; flex: 0 1 60px; }
body.mobile.mobile-chat-focus #chat {
  position: fixed; left: var(--mobile-visual-left) !important; top: var(--mobile-visual-top) !important;
  width: min(460px, var(--mobile-visual-width)) !important; max-height: var(--mobile-visual-height) !important;
  z-index: 100001;
}
body.mobile.mobile-chat-focus #chatbox { height: 90px !important; }
/* nút mở lại chat không được nằm dưới cần điều khiển (#joy, z 7 > chat z 4, cùng left/bottom nên che hoàn toàn): đặt sát bên phải cần */
body.mobile #chatshow { left: calc(12px + 132px * var(--mz, 1) + 8px); bottom: 74px; min-height: 40px; min-width: 40px; pointer-events: auto; /* #hud là pointer-events:none, nút này không tự bật lại → chạm xuyên xuống canvas */ }
body.mobile :is(.win,.npc-popup,[data-window])[data-mobile-fit] {
  box-sizing: border-box; min-width: 0 !important; height: auto !important;
  overflow: auto !important; overscroll-behavior: contain; touch-action: pan-x pan-y;
  -webkit-overflow-scrolling: touch; font-size: max(12px, calc(12px / var(--mobile-window-scale, 1)));
}
/* thanh tiêu đề sticky che nội dung vừa cuộn tới (scrollIntoView / focus): chừa chỗ cho nó */
body.mobile :is(.win,.npc-popup,[data-window])[data-mobile-fit] { scroll-padding-top: 52px; }
body.mobile :is(#hud [data-mobile-fit],[data-mobile-fit]) > :is(.wt,[data-window-handle]) {
  position: sticky !important; top: 0 !important; left: 0; z-index: 30;
  min-height: calc(44px / var(--mobile-window-scale, 1)) !important; height: auto !important;
  flex-shrink: 0; margin: 0 !important; padding: 8px calc(46px / var(--mobile-window-scale, 1)) 8px 8px !important;
  background: linear-gradient(#2c2010,#150e06) !important; box-sizing: border-box;
  font-size: max(12px, calc(12px / var(--mobile-window-scale, 1))) !important;
}
body.mobile [data-mobile-fit] :is(button,[role="button"],input:not([type="checkbox"]),.custom-select-trigger) {
  min-width: calc(40px / var(--mobile-window-scale, 1)) !important;
  min-height: calc(40px / var(--mobile-window-scale, 1)) !important; box-sizing: border-box;
  font-size: max(12px, calc(12px / var(--mobile-window-scale, 1))) !important;
}
body.mobile [data-mobile-fit] input { font-size: max(16px, calc(16px / var(--mobile-window-scale, 1))) !important; }
body.mobile [data-mobile-fit] label:has(input:is([type="checkbox"],[type="radio"])) {
  min-height: calc(40px / var(--mobile-window-scale, 1)); min-width: calc(40px / var(--mobile-window-scale, 1));
  display: flex; align-items: center; gap: 8px;
}
body.mobile :is(#hud [data-mobile-fit],[data-mobile-fit]) > :is(.wt,[data-window-handle]) :is(.x,.cms-close) {
  right: 0 !important; top: 0 !important; width: calc(40px / var(--mobile-window-scale, 1)) !important;
  height: calc(40px / var(--mobile-window-scale, 1)) !important; margin: 0 !important;
}
body.mobile :is(#hud [data-mobile-fit],[data-mobile-fit]) > .wt .x::before { line-height: calc(40px / var(--mobile-window-scale, 1)) !important; }
body.mobile [data-mobile-fit] > .wt .x::after { inset: 0 !important; }
/* thẻ người chơi / hộp có × là con trực tiếp (không nằm trong thanh tiêu đề): thanh tiêu đề sticky z-index 30 đè lên × → × phải cao hơn */
body.mobile [data-mobile-fit] > :is(.px,.x) { z-index: 31; }
body.mobile [data-mobile-fit] :is(#grid,#equip,#shopgrid,#chaosgrid,[data-trade-grid]) { zoom: calc(1.25 / var(--mobile-window-scale, 1)); flex-shrink: 0; }
/* túi đồ: lưới 8 cột phóng 1.25 (≈320 px) rộng hơn khung 300 px → bị cắt ngang, ô cuối ngoài màn hình; mở rộng khung vừa lưới */
body.mobile #w-inv[data-mobile-fit] { width: min(356px, calc(var(--mobile-visual-width) / var(--mobile-window-scale, 1))) !important; }
/* cửa hàng / Chaos Machine / trade / guild: lưới phóng 1.25 (hoặc ô 40 px) rộng hơn khung cố định → cột cuối bị cắt; khung co giãn theo nội dung (vẫn kẹp bởi max-width của mobile-layout) */
body.mobile :is(#w-shop,#w-chaos,#w-guild)[data-mobile-fit] { width: fit-content !important; min-width: 0 !important; }

body.mobile :is(#w-vault,#w-chaos) { right: calc(var(--mobile-hud-right, 0px) + 360px) !important; left: auto !important; }
body.mobile #w-trade { width: 382px !important; }
body.mobile #w-store { left: 4px; right: auto; transform: none; width: min(372px, calc(var(--mobile-visual-width) / var(--mobile-window-scale, 1))) !important; }
/* vùng trống của lưới / trang bị cho ngón tay cuộn cửa sổ (ô item .it / .slot vẫn touch-action:none để kéo) */
body.mobile [data-mobile-fit] :is(#grid,#equip) { touch-action: pan-y; }
body.mobile [data-mobile-fit] #vaultlist { zoom: calc(1.54 / var(--mobile-window-scale, 1)); }
body.mobile [data-mobile-fit] :is([data-trade-item],[data-item]) { min-width: 0 !important; min-height: 0 !important; }
body.mobile #w-guild .gugrid { grid-template-columns: repeat(8,40px); grid-template-rows: repeat(8,40px); scroll-margin-top: 52px; touch-action: none; /* vẽ huy hiệu bằng kéo ngón tay: trình duyệt không được cuộn / pan giữa chừng (pointercancel) */ }
body.mobile #w-guild .gupal { grid-template-columns: repeat(4,40px); }
body.mobile #w-guild .gupal i { min-width: 40px; min-height: 40px; }
body.mobile #w-menu[data-mobile-fit] > .wt { grid-column: 1 / -1; }
body.mobile :is(#tip,#mtip,.trade-offer-tooltip) {
  box-sizing: border-box; zoom: 1; min-width: 0; max-width: min(300px, var(--mobile-visual-width));
  max-height: var(--mobile-visual-height); overflow-y: auto; overflow-wrap: anywhere;
  white-space: normal; font-size: 12px; pointer-events: auto; touch-action: pan-y;
}
body.mobile #movelist { grid-template-columns: repeat(auto-fit,minmax(min(160px,100%),1fr)); }
body.mobile #rotate { padding: var(--mobile-safe-top) var(--mobile-safe-right) var(--mobile-safe-bottom) var(--mobile-safe-left); box-sizing: border-box; touch-action: none; }
body.mobile :is(.win,.npc-popup,[data-window],#chat,#mmenu) :is(input,textarea,button,a) { touch-action: manipulation; }
@media (orientation: portrait) {
  body.mobile:has(#rotate:not([hidden])) #hud > :not(#rotate) { visibility: hidden; }
  body.mobile:has(#rotate:not([hidden])) :is(.npc-popup,#mu-dialog,.cms-backdrop) { visibility: hidden; }
}

body.mobile .trade-offer-reopen { right: auto; left: 50%; top: calc(var(--mobile-top, 0px) + 52px); transform: translateX(-50%); min-height: 40px; box-sizing: border-box; }
body.mobile.mobile-window-open .trade-offer-reopen { display: none; }
body.mobile :is(#w-guide-box,[data-content-panel] > .cms-game-dialog)[data-mobile-fit] { overflow: hidden !important; }
body.mobile #w-guide .gtabs { flex-wrap: wrap; padding-right: 6px !important; }
body.mobile #w-guide .gtabs button { flex-shrink: 0; padding: 6px 8px; }
body.mobile [data-content-panel] > .cms-game-dialog > .cms-dialog-body { flex: 1; min-height: 0; }
