/* ============================================================
   电游吗 · NES 模拟器 —— 全局样式
   布局：顶部游戏名+菜单栏 / 左 2/3 游戏画面 / 右 1/3 操作说明
   自适应：>1100px 双栏 | ≤1100px 收窄侧栏 | ≤860px 上下堆叠
           ≤600px 压缩顶栏 | ≤480px 高度横屏压缩
   ============================================================ */

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, sans-serif;
  background: #000;
  color: #eee;
  overflow: hidden;
  user-select: none;
}
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ============ 顶部：游戏名称 + 菜单栏 ============ */
#topbar {
  flex: none;
  min-height: 52px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 6px 14px;
  background: #101410;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}
#brand {
  font-size: 17px; font-weight: 700;
  color: #cfffc8;
  letter-spacing: 1px;
  white-space: nowrap;
}
#brand small { font-size: 11px; font-weight: 400; color: #7fa87f; margin-left: 6px; letter-spacing: 0; }
#gameName {
  flex: 1 1 140px; min-width: 0;
  font-size: 15px; font-weight: 600;
  color: #ffd479;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding-left: 14px;
  border-left: 1px solid rgba(255, 255, 255, 0.15);
}
#gameName .ph { color: #666; font-weight: 400; }
#menuBar { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; white-space: nowrap; }
#menuBar button {
  padding: 7px 13px;
  font-size: 13px;
  cursor: pointer;
  background: rgba(60, 60, 60, 0.6);
  color: #eee;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
}
#menuBar button:hover { background: rgba(90, 90, 90, 0.7); }
#menuBar .scaleCtl { display: flex; align-items: center; gap: 6px; margin-left: 4px; font-size: 13px; color: #ccc; }
#menuBar input[type=range] { width: 100px; vertical-align: middle; }
#scaleVal { color: #ffd479; min-width: 2.5em; text-align: right; }

/* ============ 主体：左 2/3 游戏画面 + 右 1/3 操作说明 ============ */
#main { flex: 1; display: flex; min-height: 0; }
#stage {
  position: relative;
  flex: 2 1 0;
  min-width: 0;
  overflow: hidden;
  background: #000;
}
#side {
  flex: 1 1 0;
  min-width: 230px;
  max-width: 420px;
  overflow-y: auto;
  background: #0d100d;
  border-left: 1px solid rgba(255, 255, 255, 0.12);
  padding: 16px 18px;
  font-size: 13px;
  line-height: 1.7;
  color: #bbb;
}
#side h2 { margin: 0 0 14px; font-size: 16px; color: #cfffc8; letter-spacing: 2px; }
.side-sec { margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px dashed rgba(255, 255, 255, 0.08); }
.side-sec:last-child { margin-bottom: 0; border-bottom: none; }
.side-sec h3 { margin: 0 0 8px; font-size: 13px; color: #8f8; font-weight: 600; }
.side-sec p { margin: 4px 0; }
.keyrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 3px 0; }
.keyrow .k { color: #999; }
kbd {
  display: inline-block; min-width: 22px; text-align: center;
  padding: 1px 6px; margin: 0 1px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-bottom-width: 2px;
  border-radius: 4px;
  font-family: inherit; font-size: 12px; color: #eee;
}
#sideGameName { color: #ffd479; font-weight: 600; word-break: break-all; }

/* ============ 自适应：中等屏幕收窄侧栏 ============ */
@media (max-width: 1100px) {
  #side { min-width: 200px; max-width: 340px; padding: 14px; }
  #menuBar input[type=range] { width: 80px; }
}

/* ============ 自适应：窄屏改为上下堆叠 ============ */
@media (max-width: 860px) {
  #main { flex-direction: column; }
  #stage { flex: none; height: 55%; }
  #side {
    flex: 1; min-width: 0; max-width: none;
    border-left: none;
    border-top: 1px solid rgba(255, 255, 255, 0.12);
  }
}

/* ============ 自适应：小屏手机压缩顶栏与侧栏 ============ */
@media (max-width: 600px) {
  #topbar { padding: 6px 10px; gap: 6px 8px; }
  #brand { font-size: 15px; }
  #brand small { display: none; }
  #gameName { font-size: 13px; padding-left: 8px; }
  #menuBar { gap: 6px; }
  #menuBar button { padding: 6px 9px; font-size: 12px; }
  #menuBar .scaleCtl { font-size: 12px; margin-left: 0; }
  #menuBar input[type=range] { width: 64px; }
  #side { padding: 12px; font-size: 12px; }
  #side h2 { font-size: 15px; }
  #startHint .title { font-size: 19px; }
  #startHint .desc { font-size: 13px; }
}

/* ============ 自适应：超矮屏（横屏手机）进一步压缩 ============ */
@media (max-height: 480px) {
  #topbar { min-height: 44px; padding: 4px 10px; }
  #brand { font-size: 14px; }
  #gameName { font-size: 12px; }
  #menuBar button { padding: 5px 8px; font-size: 12px; }
  #startHint .icon { display: none; }
  #startHint .title { font-size: 16px; }
  #startHint .desc { font-size: 12px; }
}

/* ============ 游戏画面 ============ */
#gameWrap {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}
#game canvas {
  display: block;
  image-rendering: pixelated;
  background: #000;
}

#dropOverlay {
  position: absolute;
  inset: 0;
  background: rgba(30, 60, 30, 0.75);
  border: 4px dashed #8f8;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-size: 26px;
  color: #fff;
  pointer-events: none;
  z-index: 50;
}
body.dragover #dropOverlay { display: flex; }
#dropOverlay .big { font-size: 42px; font-weight: 700; color: #cfffc8; }
#dropOverlay .sub { font-size: 15px; color: #a8d8a8; }

#startHint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  pointer-events: none;
  text-align: center;
  z-index: 5;
}
#startHint.hidden { display: none; }
#startHint .icon { font-size: 64px; color: #2a5a2a; }
#startHint .title { font-size: 24px; color: #aaa; font-weight: 600; }
#startHint .desc { font-size: 14px; color: #666; line-height: 1.8; }
#startHint .btn {
  pointer-events: auto;
  padding: 10px 24px;
  font-size: 15px;
  cursor: pointer;
  background: #2a5a2a;
  color: #eee;
  border: 1px solid #4a8a4a;
  border-radius: 6px;
}
#startHint .btn:hover { background: #3a6a3a; }

#audioHint {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  background: rgba(200, 120, 0, 0.85);
  border: 1px solid #fa0;
  border-radius: 8px;
  color: #fff;
  font-size: 13px;
  z-index: 60;
  cursor: pointer;
  display: none;
}
#audioHint.show { display: block; }

.pad {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  margin-top: 10px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
}
/* 侧栏内的虚拟手柄：缩小按键以适配 1/3 宽度 */
#side .pad button {
  width: 30px; height: 30px;
  font-size: 12px;
}
#side .dpad2 {
  grid-template-columns: repeat(3, 30px);
  grid-template-rows: repeat(2, 30px);
}
#side .actions2 {
  grid-template-columns: 30px 30px 6px 30px 30px;
  grid-template-rows: repeat(2, 30px);
}
.pad .label { text-align: center; font-size: 12px; color: #aaa; }
.pad .body { display: flex; align-items: center; gap: 14px; }

.pad button {
  width: 38px; height: 38px;
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.12);
  color: #eee;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 4px;
  touch-action: none;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.25s, color 0.25s;
}
.pad button:hover { background: rgba(255, 255, 255, 0.2); }
.pad button.active { background: rgba(120, 220, 120, 0.75); color: #000; }

.dpad2 {
  display: grid;
  grid-template-columns: repeat(3, 38px);
  grid-template-rows: repeat(2, 38px);
  gap: 4px;
}
.dpad2 .blank { visibility: hidden; }

.actions2 {
  display: grid;
  grid-template-columns: 38px 38px 6px 38px 38px;
  grid-template-rows: repeat(2, 38px);
  gap: 4px;
  align-items: center;
}
.actions2 .blank { visibility: hidden; }

#startHint .hintBtns { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

/* ============ 游戏库 ============ */
#gameLib {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(8, 10, 8, 0.94);
  display: none;
  flex-direction: column;
  padding: 18px 22px;
  gap: 12px;
}
#gameLib.open { display: flex; }
.libHead { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.libTitle { font-size: 20px; font-weight: 700; color: #cfffc8; white-space: nowrap; }
.libTitle em { font-style: normal; font-size: 13px; color: #7fa87f; margin-left: 6px; }
#libSearch {
  flex: 1; min-width: 180px;
  padding: 8px 12px; font-size: 14px;
  background: rgba(255, 255, 255, 0.08);
  color: #eee;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  outline: none;
}
#libSearch:focus { border-color: #8f8; background: rgba(255, 255, 255, 0.12); }
.libHead button {
  padding: 8px 14px; font-size: 13px; cursor: pointer;
  background: rgba(60, 60, 60, 0.6); color: #eee;
  border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 8px;
}
.libHead button:hover { background: rgba(90, 90, 90, 0.7); }
.libHead button.primary { background: #2a5a2a; border-color: #4a8a4a; }
.libHead button.primary:hover { background: #3a6a3a; }
.libHead button.primary.ok { background: #1c5c2a; border-color: #6c6; }
.libHead .toolLink {
  padding: 8px 14px; font-size: 13px; cursor: pointer;
  background: rgba(60, 60, 60, 0.6); color: #eee;
  border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 8px;
  text-decoration: none; white-space: nowrap;
}
.libHead .toolLink:hover { background: rgba(90, 90, 90, 0.7); }
#libHint {
  display: none; padding: 10px 14px; font-size: 13px; line-height: 1.8;
  background: rgba(200, 120, 0, 0.18);
  border: 1px solid rgba(250, 170, 0, 0.5);
  border-radius: 8px; color: #ffd9a0;
}
#libHint.show { display: block; }
#libHint code { background: rgba(0, 0, 0, 0.35); padding: 1px 5px; border-radius: 4px; }

#libGrid {
  flex: 1; overflow-y: auto; align-content: start;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 10px;
  padding-right: 4px;
}
.romCard {
  padding: 12px 14px; cursor: pointer;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  transition: background 0.15s, border-color 0.15s;
}
.romCard:hover { background: rgba(120, 220, 120, 0.18); border-color: #8f8; }
.romCard .rn { font-size: 14px; font-weight: 600; color: #eee; word-break: break-all; }
.romCard .rm { margin-top: 6px; font-size: 11px; color: #8ab08a; display: flex; gap: 8px; }
.romCard .rm .cat { color: #9fd0ff; }
.romEmpty { grid-column: 1 / -1; text-align: center; color: #777; padding: 40px 0; font-size: 14px; }
