/* ===== v10：运行模式选择 + 全屏键位编辑器 + 自定义键位布局修复 =====
   1) 自定义键位：画面不再被按键面板挤成一条，按键改为全屏浮层（贴视口，不缩放不平移）
   2) 键位编辑器：全屏半透明，直接拖动真实按键
   3) 模式选择：进入游戏前的全屏选择页
*/

/* ---------- 1. 自定义键位模式：画面铺满 + 按键全屏浮层 ---------- */
body.ck-mode .safe-pad > [data-testid="custom-keys-overlay"] {
  position: fixed !important;
  left: 0 !important;
  top: 0 !important;
  right: auto !important;
  bottom: auto !important;
  width: 100vw !important;
  height: 100vh !important;
  padding: 0 !important;
  margin: 0 !important;
  transform: none !important;
  transform-origin: 0 0 !important;
  z-index: 26 !important;
  overflow: visible !important;
}

/* 每个按键的包裹层保持自身尺寸，中心对准 left/top */
body.ck-mode .safe-pad > [data-testid="custom-keys-overlay"] > div:not(#npe-stick) {
  width: max-content !important;
  height: max-content !important;
  touch-action: none;
}

/* 底部「自定义按键布局已开启」提示条让位给画面 */
body.ck-mode .safe-pad > div.shrink-0 { display: none !important; }

/* 战斗画面吃掉全部高度（原来被提示条挤成三百来像素） */
body.ck-mode .safe-pad > .flex-1 { flex: 1 1 auto !important; min-height: 0 !important; }

/* 横屏自定义键位：去掉画面容器的内边距，画面才能真正铺满、按键才不会压在黑边上 */
@media (orientation: landscape) {
  body.ck-mode .safe-pad > .flex-1 { padding: 0 !important; }
  body.ck-mode .battle-stage {
    border-radius: 0 !important;
    border-width: 0 !important;
  }
}

/* 画面下方的舞台底板在自定义键位模式下透明，避免出现一块生硬的黑边 */
body.ck-mode .battle-stage {
  background: transparent !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

/* ---------- 2. 键位编辑器 ---------- */
#npe-dim {
  position: fixed;
  inset: 0;
  z-index: 9940;
  pointer-events: none;
  background:
    linear-gradient(rgba(255, 255, 255, .05) 1px, transparent 1px) 0 0 / 100% 10vh,
    linear-gradient(90deg, rgba(255, 255, 255, .05) 1px, transparent 1px) 0 0 / 10vw 100%,
    radial-gradient(circle at 50% 42%, rgba(8, 4, 12, .04), rgba(6, 3, 10, .42) 82%);
}

/* 独立「操作方式」设置页：顶部一块面板，下方留出画面给按键直接拖 */
#npe-panel {
  position: fixed;
  left: 50%;
  top: 6px;
  transform: translateX(-50%);
  z-index: 9960;
  width: min(520px, calc(100vw - 14px));
  max-height: calc(100vh - 96px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px 10px 9px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, .2);
  background: rgba(16, 11, 22, .9);
  box-shadow: 0 14px 40px rgba(0, 0, 0, .6);
  backdrop-filter: blur(12px);
  color: #fff;
  font: 700 12px/1.2 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
}

#npe-panel .npe-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 7px;
  margin-bottom: 7px;
  border-bottom: 1px solid rgba(255, 255, 255, .12);
}

#npe-panel .npe-h-t {
  font-size: 13px;
  letter-spacing: .08em;
  color: #ffd9ea;
}

#npe-panel .npe-h-r { display: flex; align-items: center; gap: 6px; }

#npe-panel .npe-h-r button,
#npe-panel .npe-acts button,
#npe-panel .npe-mini,
#npe-panel .npe-more {
  padding: 5px 9px;
  border: 1px solid rgba(255, 255, 255, .22);
  background: rgba(255, 255, 255, .08);
  color: #fff;
  border-radius: 9px;
  font: 700 11.5px/1 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  cursor: pointer;
}

#npe-panel .npe-x {
  width: 26px;
  height: 26px;
  padding: 0 !important;
  border-radius: 50% !important;
  font-size: 13px !important;
  line-height: 1 !important;
}

#npe-panel button:active { transform: scale(.95); }

#npe-panel .npe-h-r button.on { border-color: #ffd166; color: #ffd166; }

#npe-panel .npe-acts {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  margin-bottom: 7px;
}

#npe-panel .npe-acts button { padding: 8px 0; font-size: 12px; }

#npe-panel .npe-acts button[data-npe="save"] {
  border-color: rgba(255, 209, 102, .75);
  background: linear-gradient(90deg, #f5b942, #d98b1f);
  color: #2a1a02;
  box-shadow: 0 4px 14px rgba(245, 185, 66, .3);
}

#npe-panel .npe-body { display: block; }

#npe-panel .npe-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 5px 0;
  font-size: 11.5px;
  color: rgba(255, 255, 255, .74);
}

#npe-panel .npe-row > span { width: 58px; flex: 0 0 auto; }
#npe-panel .npe-row > b {
  min-width: 38px;
  text-align: right;
  color: #fff;
  font-variant-numeric: tabular-nums;
}

#npe-panel input[type="range"] {
  flex: 1 1 auto;
  min-width: 0;
  height: 16px;
  accent-color: #f43f5e;
}

#npe-panel .npe-row2 { justify-content: space-between; gap: 10px; }
#npe-panel .npe-row2 .npe-chk { flex: 0 0 auto; }

#npe-panel .npe-chk {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  color: rgba(255, 255, 255, .74);
  cursor: pointer;
}
#npe-panel .npe-chk input { accent-color: #f43f5e; width: 14px; height: 14px; }
#npe-panel .npe-chk i { font-style: normal; }
#npe-panel .npe-chk2 { display: flex; margin: 6px 0; }

#npe-panel .npe-pick {
  flex: 1 1 auto;
  text-align: center;
  font-size: 11px;
  color: #ffd166;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#npe-panel .npe-adv {
  margin-top: 6px;
  padding-top: 7px;
  border-top: 1px dashed rgba(255, 255, 255, .16);
}

#npe-panel .npe-mini { width: 100%; margin-top: 4px; }

/* ---------- 虚拟摇杆：大圆包小圆 ---------- */
#npe-stick {
  position: absolute;
  left: 16vw;
  top: 76vh;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  touch-action: none;
  pointer-events: auto;
  z-index: 27;
  will-change: transform;
}

#npe-stick .npe-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, .62);
  background:
    radial-gradient(circle at 50% 46%, rgba(255, 255, 255, .13), rgba(255, 255, 255, .04) 58%, rgba(120, 130, 150, .16) 100%);
  box-shadow:
    inset 0 0 22px rgba(0, 0, 0, .5),
    inset 0 2px 10px rgba(255, 255, 255, .12),
    0 8px 26px rgba(0, 0, 0, .45),
    0 0 0 1px rgba(0, 0, 0, .28);
  backdrop-filter: blur(2px);
}

/* 四个方向的小三角刻度，让大圆一眼看出是摇杆而不是按钮 */
#npe-stick .npe-ring::before,
#npe-stick .npe-ring::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  border: 5px solid transparent;
  opacity: .5;
}
#npe-stick .npe-ring::before { transform: translate(-50%, -50%) translateY(-42%); border-top: 0; border-bottom-color: rgba(255, 255, 255, .8); }
#npe-stick .npe-ring::after { transform: translate(-50%, -50%) translateY(42%); border-bottom: 0; border-top-color: rgba(255, 255, 255, .8); }

#npe-stick .npe-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46%;
  height: 46%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background:
    radial-gradient(circle at 38% 34%, rgba(255, 255, 255, .95), rgba(226, 232, 240, .82) 46%, rgba(148, 163, 184, .9) 100%);
  border: 1px solid rgba(255, 255, 255, .75);
  box-shadow: 0 3px 12px rgba(0, 0, 0, .5), inset 0 -2px 6px rgba(0, 0, 0, .18);
  pointer-events: none;
}

body.npe-editing #npe-stick { cursor: move; }
body.npe-editing #npe-stick .npe-ring { border-style: dashed; border-color: rgba(255, 255, 255, .8); }
body.npe-editing #npe-stick.npe-sel .npe-ring {
  border-color: #ffd166;
  box-shadow: 0 0 0 2.5px rgba(255, 209, 102, .9), 0 0 24px rgba(255, 209, 102, .5);
}

/* ---------- 云端布局弹窗 ---------- */
#npe-cloud {
  position: fixed;
  inset: 0;
  z-index: 9985;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(5, 3, 8, .62);
}

#npe-cloud .npe-card {
  width: min(430px, 100%);
  padding: 14px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, .2);
  background: rgba(20, 14, 26, .97);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .7);
  color: #fff;
  font: 600 12px/1.45 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
}

#npe-cloud .npe-card-h { font-size: 14px; font-weight: 900; letter-spacing: .05em; color: #ffd9ea; margin-bottom: 6px; }
#npe-cloud .npe-card-p { color: rgba(255, 255, 255, .62); margin: 0 0 9px; }

#npe-cloud textarea {
  width: 100%;
  height: 76px;
  resize: none;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(0, 0, 0, .45);
  color: #a7f3d0;
  font: 600 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

#npe-cloud .npe-card-f { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 7px; margin-top: 10px; }
#npe-cloud .npe-card-f button {
  padding: 9px 0;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .22);
  background: rgba(255, 255, 255, .08);
  color: #fff;
  font: 800 12px/1 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  cursor: pointer;
}
#npe-cloud .npe-card-f button[data-c="paste"] {
  border-color: rgba(255, 209, 102, .7);
  background: linear-gradient(90deg, #f5b942, #d98b1f);
  color: #2a1a02;
}

/* ---------- 按键名称标签 ---------- */
body.npe-labels .safe-pad > [data-testid="custom-keys-overlay"] > div[data-label]::after {
  content: attr(data-label);
  position: absolute;
  left: 50%;
  bottom: 100%;
  transform: translate(-50%, -3px);
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(6, 3, 10, .82);
  border: 1px solid rgba(255, 255, 255, .22);
  color: #fff;
  font: 700 9.5px/1.4 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  white-space: nowrap;
  pointer-events: none;
}

body.npe-labels #npe-stick::after {
  content: "摇杆";
  position: absolute;
  left: 50%;
  bottom: 100%;
  transform: translate(-50%, -3px);
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(6, 3, 10, .82);
  border: 1px solid rgba(255, 255, 255, .22);
  color: #fff;
  font: 700 9.5px/1.4 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  white-space: nowrap;
  pointer-events: none;
}

#npe-panel #npe-tip {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed rgba(255, 255, 255, .12);
  color: rgba(255, 255, 255, .55);
  font: 600 10.5px/1.35 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  text-align: center;
  pointer-events: none;
  transition: opacity .4s;
}

/* 编辑中：按键浮到最上层，本体不吃点击，包裹层吃拖动 */
body.npe-editing .safe-pad > [data-testid="custom-keys-overlay"] { z-index: 9950 !important; }

body.npe-editing .safe-pad > [data-testid="custom-keys-overlay"] > div {
  cursor: move;
  border-radius: 16px;
  box-shadow: 0 0 0 1.5px rgba(255, 255, 255, .55), 0 0 16px rgba(0, 0, 0, .35);
  background: rgba(255, 255, 255, .06);
}

body.npe-editing .safe-pad > [data-testid="custom-keys-overlay"] > div.npe-sel {
  box-shadow: 0 0 0 2.5px #ffd166, 0 0 22px rgba(255, 209, 102, .55);
  background: rgba(255, 209, 102, .12);
}

body.npe-editing .safe-pad > [data-testid="custom-keys-overlay"] .touch-btn {
  pointer-events: none !important;
}

/* ---------- 3. 运行模式选择页 ---------- */
#npe-mode {
  position: fixed;
  inset: 0;
  z-index: 99999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 28px 18px calc(28px + env(safe-area-inset-bottom, 0px));
  background:
    radial-gradient(120% 80% at 50% 0%, rgba(244, 63, 94, .22), transparent 60%),
    radial-gradient(100% 70% at 50% 100%, rgba(124, 58, 237, .22), transparent 62%),
    #0b0710;
  color: #fff;
  font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#npe-mode .npe-mode-head { text-align: center; }

#npe-mode .npe-mode-logo {
  font-size: 13px;
  letter-spacing: .5em;
  color: rgba(255, 255, 255, .45);
  margin-bottom: 8px;
}

#npe-mode .npe-mode-title {
  font-size: clamp(20px, 6vw, 30px);
  font-weight: 900;
  letter-spacing: .06em;
  background: linear-gradient(90deg, #ff8fb1, #ffd166 45%, #a78bfa);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

#npe-mode .npe-mode-sub {
  margin-top: 8px;
  font-size: 12px;
  color: rgba(255, 255, 255, .55);
}

#npe-mode .npe-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  width: 100%;
  max-width: 860px;
}

#npe-mode .npe-card {
  position: relative;
  flex: 1 1 200px;
  min-width: 168px;
  max-width: 260px;
  padding: 18px 14px 16px;
  border-radius: 18px;
  border: 1.5px solid rgba(255, 255, 255, .14);
  background: linear-gradient(160deg, rgba(255, 255, 255, .07), rgba(255, 255, 255, .02));
  cursor: pointer;
  text-align: left;
  transition: transform .12s ease, border-color .12s ease, box-shadow .12s ease;
}

#npe-mode .npe-card:active { transform: scale(.98); }

#npe-mode .npe-card.on {
  border-color: rgba(244, 63, 94, .85);
  background: linear-gradient(160deg, rgba(244, 63, 94, .22), rgba(124, 58, 237, .12));
  box-shadow: 0 0 0 1px rgba(244, 63, 94, .4), 0 12px 30px rgba(244, 63, 94, .18);
}

#npe-mode .npe-card .material-icons {
  font-size: 30px;
  line-height: 1;
  color: #ffd9ea;
}

#npe-mode .npe-card h3 {
  margin: 10px 0 6px;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: .04em;
}

#npe-mode .npe-card p {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.55;
  color: rgba(255, 255, 255, .6);
}

#npe-mode .npe-badge {
  position: absolute;
  right: 10px;
  top: 10px;
  padding: 2px 7px;
  border-radius: 999px;
  background: linear-gradient(90deg, #f43f5e, #a855f7);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .04em;
}

#npe-mode .npe-foot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 420px;
}

#npe-mode .npe-go {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(90deg, #e11d48, #7c3aed);
  color: #fff;
  font: 900 16px/1 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  letter-spacing: .08em;
  box-shadow: 0 10px 26px rgba(225, 29, 72, .35);
  cursor: pointer;
}

#npe-mode .npe-go:active { transform: scale(.98); }

#npe-mode .npe-ask {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  color: rgba(255, 255, 255, .55);
}

#npe-mode .npe-ask input { accent-color: #f43f5e; width: 15px; height: 15px; }

#npe-mode .npe-hint {
  font-size: 11px;
  color: rgba(255, 255, 255, .35);
  text-align: center;
  line-height: 1.6;
}

/* 竖屏小屏：卡片改单列，压缩留白 */
@media (max-width: 520px) {
  #npe-mode { gap: 12px; justify-content: flex-start; padding-top: 40px; }
  #npe-mode .npe-cards { flex-direction: column; align-items: stretch; }
  #npe-mode .npe-card { max-width: none; flex: 0 0 auto; padding: 14px; display: flex; gap: 12px; align-items: center; }
  #npe-mode .npe-card h3 { margin: 0 0 4px; }
  #npe-mode .npe-card .npe-ico { flex: 0 0 auto; }
  #npe-mode .npe-card .npe-txt { flex: 1 1 auto; }
}

/* 矮屏（320×480 这类老机器 / 横屏手机）：把留白和卡片压扁，
   保证「继续」按钮落在首屏里 —— 否则要滚动才点得到，玩家会以为卡住了 */
@media (max-height: 620px) {
  #npe-mode {
    gap: 8px;
    justify-content: flex-start;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  }
  #npe-mode .npe-mode-logo { display: none; }
  #npe-mode .npe-mode-sub { display: none; }
  #npe-mode .npe-mode-title { font-size: 18px; margin: 0; }
  #npe-mode .npe-cards { flex-direction: row; flex-wrap: nowrap; align-items: stretch; gap: 7px; }
  #npe-mode .npe-card {
    flex: 1 1 0; min-width: 0; max-width: none;
    padding: 8px 7px; display: block; border-radius: 12px;
  }
  #npe-mode .npe-card .npe-ico { display: none; }
  #npe-mode .npe-card h3 { font-size: 12.5px; margin: 0 0 3px; }
  #npe-mode .npe-card p { font-size: 10px; line-height: 1.35; }
  #npe-mode .npe-badge { font-size: 9px; padding: 1px 5px; }
  #npe-mode .npe-foot { gap: 6px; }
  #npe-mode .npe-go { padding: 10px; font-size: 14px; }
  #npe-mode .npe-hint { display: none; }
}

/* 编辑器的暂停菜单入口按钮 */#npe-pause-entry {
  width: 100%;
  border-radius: 12px;
  border: 1px solid rgba(255, 209, 102, .45);
  background: rgba(255, 209, 102, .12);
  color: #ffe6a8;
  padding: 11px;
  font: 900 14px/1 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  cursor: pointer;
}

#npe-pause-entry:active { transform: scale(.97); }

/* 横屏 / 矮屏：面板压扁，别把操作区全挡住 */
@media (max-height: 560px) {
  #npe-panel {
    top: 4px;
    width: min(470px, calc(100vw - 10px));
    padding: 6px 8px 7px;
    max-height: calc(100vh - 12px);
  }
  #npe-panel .npe-head { padding-bottom: 4px; margin-bottom: 5px; }
  #npe-panel .npe-h-t { font-size: 12px; }
  #npe-panel .npe-acts { margin-bottom: 5px; }
  #npe-panel .npe-acts button { padding: 6px 0; font-size: 11.5px; }
  #npe-panel .npe-row { margin: 3px 0; font-size: 11px; }
  #npe-panel .npe-row > span { width: 52px; }
  #npe-panel .npe-adv { margin-top: 4px; padding-top: 5px; }
  #npe-panel .npe-chk2 { margin: 4px 0; }
  #npe-panel .npe-mini { padding: 6px 0; }
  #npe-panel #npe-tip { margin-top: 4px; padding-top: 4px; font-size: 10px; }
}

/* 窄屏：工具栏去标题、滑块收窄，尽量少压画面 */
@media (max-width: 520px) {
  #npe-bar { top: 4px; gap: 4px 6px; padding: 5px 7px; }
  #npe-bar .npe-title { display: none; }
  #npe-bar input[type="range"] { width: 62px; }
  #npe-bar label { font-size: 10.5px; }
}

/* ---------- 4. 手机/平板横放（未开强制横屏）：画面在左、按键在右 ----------
   之前横屏时还在按竖屏排版，画面被压成屏幕中间一小块、按键面板超出底部被裁。
   用 grid 明确分两栏，画面区 overflow:hidden，保证不会压到按键区。 */
body.bl-land:not(.ck-mode) .safe-pad {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 44%) !important;
  grid-template-rows: 100% !important;
  align-items: center !important;
  gap: 0 !important;
}

body.bl-land:not(.ck-mode) .safe-pad > div.flex-1 {
  grid-column: 1 !important;
  grid-row: 1 !important;
  min-width: 0 !important;
  width: 100% !important;
  height: 100% !important;
  overflow: hidden !important;
  align-items: center !important;
  padding: 6px !important;
}

/* 画面按 16:9 收进左栏，绝不越界压住按键 */
body.bl-land:not(.ck-mode) .battle-stage {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  max-height: 100% !important;
  aspect-ratio: 16 / 9 !important;
}

body.bl-land:not(.ck-mode) .safe-pad > div.shrink-0 {
  grid-column: 2 !important;
  grid-row: 1 !important;
  width: 100% !important;
  max-width: none !important;
  height: 100% !important;
  overflow-y: auto;
  padding: 6px !important;
}

body.bl-land:not(.ck-mode) .safe-pad > div.shrink-0 > div {
  max-width: none !important;
  width: 100% !important;
}

/* 电脑模式 + 触摸设备：暂停菜单里的回退入口 */
#npe-pause-touch {
  margin-top: 10px; width: 100%; padding: 11px 14px; border-radius: 12px;
  border: 1px solid rgba(255,255,255,.22); background: rgba(255,255,255,.08);
  color: #fff; font: 800 13px/1.2 system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
  letter-spacing: .02em; cursor: pointer; -webkit-tap-highlight-color: transparent;
}
#npe-pause-touch:active { transform: scale(.97); }
