/* ============ MRP mobile emulator UI ============ */
:root {
  --bg: #0a0f14;
  --bg2: #0d151c;
  --panel: #121c25;
  --panel2: #17232e;
  --line: #24384a;
  --text: #e6f1f7;
  --dim: #8aa0b2;
  --accent: #22d3b8;
  --accent-dark: #0b6a63;
  --danger: #e5534b;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; overflow: hidden; touch-action: manipulation; }
h1, h2 { margin: 0; font-weight: 600; }
button { font-family: inherit; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }

#app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  background: radial-gradient(1200px 600px at 50% -200px, #123041 0%, var(--bg) 60%);
}

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid rgba(36, 56, 74, .55);
  background: rgba(10, 15, 20, .65);
  backdrop-filter: blur(8px);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1 1 auto; overflow: hidden; }
.logo {
  font-weight: 800; letter-spacing: 1px; font-size: 15px;
  color: #05130f; background: linear-gradient(135deg, var(--accent), #57e6c9);
  padding: 3px 8px; border-radius: 7px;
}
#appTitle {
  font-size: 14px; color: #cfe1ea; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 40vw;
}
.topActions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.tbtn {
  appearance: none; border: 1px solid var(--line);
  background: linear-gradient(180deg, #1a2835, #121c25);
  color: #c7d8e2; border-radius: 9px;
  padding: 7px 9px; font-size: 12.5px; line-height: 1;
  white-space: nowrap; touch-action: manipulation; cursor: pointer;
}
.tbtn:active { background: #23414f; color: #fff; }
.tbtn.icon { width: 30px; padding: 7px 0; text-align: center; font-weight: 700; flex: 0 0 auto; }
@media (max-width: 380px) {
  .tbtn { padding: 6px 7px; font-size: 12px; }
  #topbar { padding: 7px 9px; }
}

/* ---------- 画面区 ---------- */
#stage {
  position: relative;
  flex: 1 1 auto; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 10px;
}
#screenBox {
  position: relative;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 1px #1d2b38, 0 10px 40px rgba(0, 0, 0, .55), 0 0 60px rgba(34, 211, 184, .06);
  touch-action: none;
}
#canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; image-rendering: crisp-edges; background: #000; cursor: default; }

/* ---------- 对话框（MRP 原生弹窗） ---------- */
#dialog {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  background: #f3f6f8; color: #111;
  visibility: hidden; z-index: 5;
}
#dialog.visible { visibility: visible; }
#dialogTitle {
  font-size: 13px; padding: 7px 10px; text-align: center;
  background: #dde6ec; border-bottom: 1px solid #b9c7d2;
}
#dialogText {
  flex: 1 1 auto; width: 100%; border: 0; outline: none; resize: none;
  padding: 8px 10px; font-size: 12px; line-height: 1.5;
  background: #fff; color: #111; overflow: auto;
  -webkit-user-select: text; user-select: text;
}
#dialogText[readonly] { background: #f7fafc; }
.dialogControl { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 7px 8px; background: #dde6ec; }
.dialogControl button { flex: 0 0 auto; padding: 9px 14px; font-size: 13px; border-radius: 6px; border: 1px solid #9fb0be; background: #fff; color: #111; }
.dialogControl button:active { background: #d4e0e8; }
#dialogInfo { font-size: 11px; color: #445562; white-space: nowrap; }

/* ---------- 控制区：软键在左右方向键上方，OK 居中 ---------- */
#controls {
  flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 9px; padding: 4px 16px 14px;
}
.cluster { flex: 0 0 auto; }
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 8px;
  width: min(68vw, 280px);
  aspect-ratio: 1 / 1;
}
.dpad .softL { grid-area: 1 / 1 / 2 / 2; }
.dpad .up    { grid-area: 1 / 2 / 2 / 3; }
.dpad .softR { grid-area: 1 / 3 / 2 / 4; }
.dpad .left  { grid-area: 2 / 1 / 3 / 2; }
.dpad .ok    { grid-area: 2 / 2 / 3 / 3; }
.dpad .right { grid-area: 2 / 3 / 3 / 4; }
.dpad .down  { grid-area: 3 / 2 / 4 / 3; }
.dpad .soft {
  font-size: 13px; font-weight: 500; color: #b9cbd6;
  background: linear-gradient(180deg, #1c2b39 0%, #141f29 100%);
  border-color: #223547;
  border-radius: 16px;
}
.dpad .ok {
  font-size: 21px; letter-spacing: 1px;
  background: linear-gradient(180deg, #14726a 0%, #0b544e 100%);
  border-color: #1a8a7e;
}
.miniRow { display: flex; gap: 10px; width: min(68vw, 280px); }
.miniRow .key {
  flex: 1 1 0; height: 38px; font-size: 12.5px; border-radius: 10px; color: var(--dim);
}
.key {
  appearance: none; -webkit-appearance: none;
  border: 1px solid #2a4055;
  background: linear-gradient(180deg, #1c2c3a 0%, #121d27 100%);
  color: #d7e6ef; border-radius: 14px;
  font-size: 19px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  touch-action: none; cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .06), 0 2px 0 #0a1116;
  transition: transform .04s ease, background .04s ease, border-color .04s ease;
  padding: 0;
}
.key.pressed, .key:active {
  background: linear-gradient(180deg, #12857a 0%, #0a5f57 100%);
  border-color: var(--accent);
  color: #eafffb;
  transform: scale(.955);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .12), 0 0 18px rgba(34, 211, 184, .25);
}
#controls .key { min-height: 0; }

/* ---------- 横屏 ---------- */
@media (orientation: landscape) and (max-height: 560px) {
  #app {
    display: grid;
    grid-template-columns: 1fr min(42vw, 320px);
    grid-template-rows: auto 1fr;
    grid-template-areas: "top top" "stage side";
  }
  #topbar { grid-area: top; padding: 5px 10px; }
  #appTitle { max-width: 30vw; }
  #stage { grid-area: stage; padding: 6px; }
  #controls {
    grid-area: side;
    flex-direction: column; align-items: center; justify-content: center;
    padding: 4px 10px; gap: 8px;
  }
  .dpad { width: min(46vh, 230px); }
  .dpad .soft { font-size: 11.5px; }
  .dpad .ok { font-size: 19px; }
  .miniRow { width: min(46vh, 230px); }
  .miniRow .key { height: 32px; font-size: 11.5px; }
}
@media (orientation: landscape) and (max-width: 640px) and (max-height: 560px) {
  .tbtn { padding: 6px 7px; font-size: 12px; }
}

/* ---------- 加载遮罩 ---------- */
#loading {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(800px 500px at 50% 30%, #10303c 0%, #070b0f 70%);
  transition: opacity .35s ease;
}
#loading.fade { opacity: 0; pointer-events: none; }
.loadBox { width: min(78vw, 340px); text-align: center; }
.loadLogo {
  display: inline-block; font-size: 30px; font-weight: 900; letter-spacing: 3px;
  color: #041512; background: linear-gradient(135deg, var(--accent), #63f0d3);
  padding: 8px 16px; border-radius: 14px; margin-bottom: 16px;
  box-shadow: 0 10px 40px rgba(34, 211, 184, .25);
}
#loadStatus { font-size: 14px; color: #cfe1ea; min-height: 20px; }
#loadProgress {
  width: 100%; height: 6px; margin: 14px 0 10px;
  appearance: none; -webkit-appearance: none; border: 0; border-radius: 6px;
  background: #16232e; overflow: hidden;
}
#loadProgress::-webkit-progress-bar { background: #16232e; border-radius: 6px; }
#loadProgress::-webkit-progress-value { background: linear-gradient(90deg, var(--accent), #61eed1); border-radius: 6px; }
#loadProgress::-moz-progress-bar { background: var(--accent); border-radius: 6px; }
#loadHint { font-size: 11px; color: #71889a; line-height: 1.6; }

/* ---------- 底部面板 ---------- */
#sheet { position: fixed; inset: 0; z-index: 40; }
.sheetBackdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); backdrop-filter: blur(2px); }
.sheetPanel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 72vh; max-height: 72dvh;
  display: flex; flex-direction: column;
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .5);
  padding-bottom: var(--safe-b);
  animation: sheetUp .22s ease;
}
@keyframes sheetUp { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheetHead {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 8px;
}
.sheetHead h2 { font-size: 16px; }
#sheetClose {
  appearance: none; border: 0; background: #1d2c39; color: #a9bfcd;
  border-radius: 8px; padding: 7px 12px; font-size: 13px;
}
.sheetBody { padding: 4px 16px 20px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheetBody p { margin: 10px 0; font-size: 13px; line-height: 1.65; color: #b9cbd6; }
.sheetBody p.dim { color: #7e94a5; font-size: 12px; }
.sheetBody a { color: var(--accent); text-decoration: none; }
.sheetBody .kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid rgba(36, 56, 74, .5); font-size: 13px; }
.sheetBody .kv span:last-child { color: #9db3c2; }
.fileRow {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 0; border-bottom: 1px solid rgba(36, 56, 74, .5);
}
.fileInfo { flex: 1 1 auto; min-width: 0; }
.fileName { font-size: 14px; color: #e2eef5; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fileMeta { font-size: 11px; color: #7e94a5; margin-top: 2px; }
.fileActions { display: flex; gap: 6px; flex: 0 0 auto; }
.fileActions button {
  appearance: none; border: 1px solid var(--line); background: #1a2834; color: #cfe0e9;
  border-radius: 8px; padding: 8px 10px; font-size: 12px;
}
.fileActions button.primary { background: var(--accent-dark); border-color: #12857a; color: #eafffb; }
.fileActions button.danger { color: #f2a49e; border-color: #5b2f2c; background: #2a1817; }
.fileActions button:active { filter: brightness(1.25); }
.padGrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 6px; }
.padGrid .key { height: 52px; font-size: 18px; border-radius: 12px; }
.padGrid .key.wide { grid-column: span 3; height: 44px; font-size: 14px; color: #b9cbd6; }
.emptyTip { padding: 22px 0; text-align: center; color: #71879a; font-size: 13px; line-height: 1.8; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; top: calc(58px + var(--safe-t));
  transform: translate(-50%, -8px);
  max-width: 80vw; padding: 10px 16px; border-radius: 10px;
  background: rgba(12, 20, 27, .92); color: #e8f3f8; font-size: 13px;
  border: 1px solid var(--line); opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease; z-index: 70; pointer-events: none;
  line-height: 1.5; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 应用退出遮罩 ---------- */
#exitOverlay, #confirmOverlay {
  position: fixed; inset: 0; z-index: 75;
  display: flex; align-items: center; justify-content: center;
  background: rgba(5, 10, 14, .78);
  backdrop-filter: blur(4px);
}
.exitBox {
  width: min(82vw, 320px); padding: 22px 18px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; text-align: center;
}
.exitTitle { font-size: 17px; font-weight: 700; margin-bottom: 8px; }
.exitMsg { font-size: 13px; color: var(--dim); line-height: 1.6; margin-bottom: 18px; }
.exitBtn {
  display: block; width: 100%; padding: 12px; margin-top: 10px;
  border-radius: 10px; border: 1px solid var(--line);
  background: #1a2834; color: #cfe0e9; font-size: 14px;
}
.exitBtn.primary { background: var(--accent-dark); border-color: #12857a; color: #eafffb; }
.exitBtn:active { filter: brightness(1.25); }

/* 小屏适配 */
@media (max-height: 700px) {
  #controls { padding-bottom: 8px; gap: 6px; }
  .dpad { width: min(60vw, 240px); gap: 6px; }
  .miniRow { width: min(60vw, 240px); gap: 8px; }
  .miniRow .key { height: 32px; }
  .dpad .soft { font-size: 12px; border-radius: 13px; }
  .dpad .ok { font-size: 19px; }
}

/* ---------- 游戏库 ---------- */
#library {
  position: fixed; inset: 0; z-index: 42;
  display: flex; flex-direction: column;
  background: radial-gradient(1200px 600px at 50% -300px, #102a35 0%, var(--bg) 55%);
  padding-top: var(--safe-t); padding-bottom: var(--safe-b);
  padding-left: var(--safe-l); padding-right: var(--safe-r);
}
.libHead { display: flex; align-items: center; gap: 8px; padding: 10px 12px 8px; flex: 0 0 auto; }
#libSearch {
  flex: 1 1 auto; min-width: 0;
  background: #121c25; border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px;
  color: var(--text); font-size: 14px; outline: none;
  -webkit-user-select: text; user-select: text;
}
#libSearch::placeholder { color: #5f7789; }
#libSearch:focus { border-color: #2f8f85; }
.libCount { font-size: 11px; color: var(--dim); flex: 0 0 auto; white-space: nowrap; }
.libChips { display: flex; gap: 7px; overflow-x: auto; padding: 2px 12px 8px; flex: 0 0 auto; scrollbar-width: none; }
.libChips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; appearance: none;
  border: 1px solid var(--line); background: #121c25;
  color: #a9bfcd; border-radius: 999px; padding: 7px 12px;
  font-size: 12.5px; white-space: nowrap;
}
.chip.active { background: var(--accent-dark); border-color: #12857a; color: #eafffb; }
.chip small { opacity: .65; margin-left: 3px; font-size: 10.5px; }
.libList { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 12px 16px; }
.libRow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid rgba(36, 56, 74, .5); }
.libIcon { width: 42px; height: 42px; border-radius: 10px; flex: 0 0 auto; object-fit: cover; background: #101a22; border: 1px solid #1d2c3a; }
.libInfo { flex: 1 1 auto; min-width: 0; }
.libName { font-size: 14px; color: #e2eef5; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.libMeta { font-size: 11px; color: #7e94a5; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.libBtn {
  flex: 0 0 auto; appearance: none;
  border: 1px solid var(--line); background: #1a2834; color: #cfe0e9;
  border-radius: 9px; padding: 9px 13px; font-size: 12.5px; min-width: 62px;
}
.libBtn.primary { background: var(--accent-dark); border-color: #12857a; color: #eafffb; }
.libBtn.busy { opacity: .75; }
.libBtn:disabled { opacity: .7; }
.libMore {
  flex: 0 0 auto; margin: 0 12px 12px; padding: 12px;
  text-align: center; font-size: 13px; color: #9db3c2;
  border: 1px solid var(--line); border-radius: 10px; background: #121c25;
}


/* ---------- 融合键盘（方向 + 数字） ---------- */
#controls {
  flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 8px; padding: 4px 14px 14px;
}
.auxRow { display: flex; gap: 8px; width: min(80vw, 320px); }
.auxRow .key {
  flex: 1 1 0; height: 34px; font-size: 12px; border-radius: 10px; color: var(--dim);
}
.keypad {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  width: min(80vw, 320px);
}
.keypad .key {
  height: 56px; border-radius: 14px; font-size: 19px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1; gap: 0;
}
.keypad .num .d { font-size: 19px; }
.keypad .num .a { font-size: 11px; color: #7fe3d3; margin-top: 3px; letter-spacing: 1px; }
.keypad .num.ok {
  background: linear-gradient(180deg, #14726a 0%, #0b544e 100%);
  border-color: #1a8a7e;
}
.keypad .num.ok .a { color: #d9fff7; font-weight: 700; }
@media (max-height: 700px) {
  #controls { padding-bottom: 8px; gap: 6px; }
  .keypad { width: min(76vw, 300px); gap: 6px; }
  .keypad .key { height: 46px; font-size: 17px; }
  .keypad .num .d { font-size: 17px; }
  .keypad .num .a { font-size: 10px; margin-top: 2px; }
  .auxRow { width: min(76vw, 300px); }
  .auxRow .key { height: 30px; font-size: 11px; }
}
@media (orientation: landscape) and (max-height: 560px) {
  #controls { gap: 6px; padding: 4px 8px; }
  .keypad { width: min(46vh, 240px); gap: 6px; }
  .keypad .key { height: min(9.5vh, 52px); font-size: 16px; border-radius: 12px; }
  .keypad .num .a { font-size: 10px; margin-top: 2px; }
  .auxRow { width: min(46vh, 240px); }
  .auxRow .key { height: 28px; font-size: 10.5px; }
}

/* ---------- 键盘：软键在两侧 + 加大按钮 ---------- */
#controls { padding: 6px 12px 14px; }
.padWrap {
  display: grid;
  grid-template-columns: 0.62fr 3fr 0.62fr;
  gap: 9px;
  width: min(94vw, 380px);
  align-items: stretch;
}
.softSide {
  width: 100%; height: 100%;
  border-radius: 16px;
  font-size: 13px; font-weight: 600;
  color: #cfe0e9; letter-spacing: 2px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px;
  background: linear-gradient(180deg, #1c2b39 0%, #141f29 100%);
  border: 1px solid #223547;
}
.softSide span { display: block; line-height: 1.15; }
.keypad { width: 100%; gap: 8px; }
.keypad .key { height: 62px; font-size: 20px; border-radius: 15px; }
.keypad .num .d { font-size: 20px; }
.keypad .num .a { font-size: 12px; margin-top: 3px; }
@media (max-height: 700px) {
  .padWrap { width: min(92vw, 340px); gap: 7px; }
  .keypad { gap: 6px; }
  .keypad .key { height: 52px; font-size: 18px; }
  .keypad .num .d { font-size: 18px; }
  .keypad .num .a { font-size: 10.5px; margin-top: 2px; }
  .softSide { font-size: 12px; letter-spacing: 1px; }
}
@media (orientation: landscape) and (max-height: 560px) {
  .padWrap { width: min(54vh, 300px); grid-template-columns: 0.6fr 3fr 0.6fr; gap: 7px; }
  .keypad { gap: 6px; }
  .keypad .key { height: min(11vh, 58px); font-size: 17px; border-radius: 13px; }
  .keypad .num .d { font-size: 17px; }
  .keypad .num .a { font-size: 10.5px; margin-top: 2px; }
  .softSide { font-size: 11px; letter-spacing: 1px; }
}

/* ---------- 软键放在键盘顶部 ---------- */
.softTop { display: flex; gap: 8px; width: min(94vw, 380px); }
.softTopBtn {
  flex: 1 1 0; height: 46px; font-size: 14px; font-weight: 600;
  letter-spacing: 2px; border-radius: 14px; color: #d7e6ef;
  background: linear-gradient(180deg, #1c2b39 0%, #141f29 100%);
  border-color: #223547;
}
.keypad { width: min(94vw, 380px); }
@media (max-height: 700px) {
  .softTop { width: min(92vw, 340px); gap: 6px; }
  .softTopBtn { height: 40px; font-size: 13px; }
  .keypad { width: min(92vw, 340px); }
}
@media (orientation: landscape) and (max-height: 560px) {
  .softTop { width: min(54vh, 300px); gap: 7px; }
  .softTopBtn { height: 36px; font-size: 12px; letter-spacing: 1px; }
  .keypad { width: min(54vh, 300px); }
}

/* ---------- 键盘提示 ---------- */
.padHint { font-size: 10.5px; color: #5f7789; letter-spacing: 0.5px; text-align: center; margin-top: 1px; }
.key.longpress { box-shadow: inset 0 0 0 2px var(--accent), 0 0 14px rgba(34, 211, 184, .35); }
