/* ═══════════════ 基础 ═══════════════ */
* { margin: 0; padding: 0; box-sizing: border-box; }

/* hidden 属性必须赢过任何自定义 display */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  overflow: hidden;
  background: #0a0a0a;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

.screen { height: 100dvh; width: 100vw; }

/* ═══════════════ 首页 ═══════════════ */
#screen-home {
  overflow-y: auto;
  touch-action: pan-y;
  background: linear-gradient(160deg, #141414 0%, #0a0a0a 60%);
  color: #eee;
}

.home-inner {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 48px) 24px 48px;
}

.home-title {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: 2px;
  color: #e02b2b;
  margin-bottom: 32px;
}
.home-title span { color: #eee; font-weight: 200; font-size: 30px; }

.drop-zone {
  border: 2px dashed #444;
  border-radius: 16px;
  padding: 44px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.drop-zone.drag-over, .drop-zone:active { border-color: #e02b2b; background: rgba(224,43,43,.06); }
.drop-icon { font-size: 40px; margin-bottom: 12px; }
.drop-main { font-size: 17px; color: #ddd; }
.drop-sub { font-size: 13px; color: #777; margin-top: 8px; }

.recent-section { margin-top: 36px; }
.recent-title { font-size: 14px; font-weight: 600; color: #888; letter-spacing: 1px; margin-bottom: 12px; }
.recent-list { list-style: none; }
.recent-item {
  display: flex; align-items: center; gap: 14px;
  background: #1c1c1c; border-radius: 12px;
  padding: 14px 16px; margin-bottom: 10px; cursor: pointer;
}
.recent-item:active { background: #2a2a2a; }
.recent-cart {
  width: 40px; height: 34px; border-radius: 4px 4px 6px 6px; flex-shrink: 0;
  background: linear-gradient(180deg, #555 0%, #3a3a3a 100%);
  position: relative;
}
.recent-cart::after {
  content: ""; position: absolute; left: 6px; right: 6px; top: 6px; height: 12px;
  border-radius: 2px; background: #e02b2b;
}
.recent-name { flex: 1; min-width: 0; font-size: 15px; color: #eee; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recent-meta { font-size: 12px; color: #666; margin-top: 3px; }
.recent-del {
  border: none; background: none; color: #555; font-size: 18px; padding: 8px; cursor: pointer;
}

.storage-warning { margin-top: 24px; font-size: 13px; color: #c90; }

/* ═══════════════ 游戏页整体 ═══════════════ */
#screen-game {
  display: flex;
  flex-direction: column;
  background: #000;
}

.game-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: env(safe-area-inset-top, 0px);
  overflow: hidden;
}

#nes-canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  width: 100%;
  height: auto;
  aspect-ratio: 256 / 240;
  max-height: 100%;
  max-width: calc(100dvh * 256 / 240);
  background: #000;
}

/* ═══════════════ 控制器面板(Delta 风格拟物) ═══════════════ */
.controller {
  flex: 0 0 auto;
  background: linear-gradient(180deg, #f2f0ee 0%, #e2e0dd 100%);
  border-radius: 18px 18px 0 0;
  padding: 10px 14px calc(env(safe-area-inset-bottom, 0px) + 10px);
  box-shadow: 0 -2px 12px rgba(0,0,0,.5);
}

.ctrl-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 2px 6px 8px;
}
.ctrl-brand {
  font-size: 15px; font-weight: 800; letter-spacing: 4px; color: #2b2b2b;
}
.menu-btn {
  border: none; cursor: pointer;
  display: flex; align-items: center; gap: 8px;
  background: #262626; color: #f5f5f5;
  font-size: 11px; font-weight: 700; letter-spacing: 2px;
  padding: 8px 12px; border-radius: 20px;
}
.menu-btn:active { background: #444; }
.menu-dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff, #cfcfcf 60%, #aaa);
}

.ctrl-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background:
    radial-gradient(120% 100% at 50% 0%, rgba(255,255,255,.04), transparent 50%),
    #232323;
  border-radius: 14px;
  padding: 16px 12px;
}

/* —— 十字键 —— */
.dpad-wrap { flex: 0 0 auto; }
.dpad {
  --dpad-size: clamp(120px, 36vw, 168px);
  width: var(--dpad-size);
  height: var(--dpad-size);
  position: relative;
  touch-action: none;
}
.dpad-cross { position: absolute; inset: 0; }
/* 十字形本体:两个交叉矩形 */
.dpad-cross::before, .dpad-cross::after {
  content: ""; position: absolute;
  background: linear-gradient(180deg, #333 0%, #1a1a1a 100%);
  border-radius: 7px;
  box-shadow: 0 3px 6px rgba(0,0,0,.6), inset 0 1px 0 rgba(255,255,255,.08);
}
.dpad-cross::before { left: 33.5%; top: 2%; width: 33%; height: 96%; }
.dpad-cross::after  { top: 33.5%; left: 2%; height: 33%; width: 96%; }
.dpad-center {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  width: 26%; height: 26%; border-radius: 50%; z-index: 2;
  background: radial-gradient(circle at 40% 35%, #2e2e2e, #111 70%);
}
.dpad-arrow {
  position: absolute; z-index: 2;
  width: 0; height: 0; opacity: .85;
}
.dpad-arrow.up    { left: 50%; top: 9%;  transform: translateX(-50%); border-left: 7px solid transparent; border-right: 7px solid transparent; border-bottom: 11px solid #000; }
.dpad-arrow.down  { left: 50%; bottom: 9%; transform: translateX(-50%); border-left: 7px solid transparent; border-right: 7px solid transparent; border-top: 11px solid #000; }
.dpad-arrow.left  { top: 50%; left: 9%;  transform: translateY(-50%); border-top: 7px solid transparent; border-bottom: 7px solid transparent; border-right: 11px solid #000; }
.dpad-arrow.right { top: 50%; right: 9%; transform: translateY(-50%); border-top: 7px solid transparent; border-bottom: 7px solid transparent; border-left: 11px solid #000; }

/* 方向按下高亮:JS 给 .dpad 加 press-up/down/left/right */
.dpad.press-up    .dpad-arrow.up,
.dpad.press-down  .dpad-arrow.down,
.dpad.press-left  .dpad-arrow.left,
.dpad.press-right .dpad-arrow.right {
  opacity: 1;
  border-bottom-color: #e02b2b; border-top-color: #e02b2b;
}
.dpad.press-up .dpad-arrow.up { border-bottom-color: #e02b2b; }
.dpad.press-down .dpad-arrow.down { border-top-color: #e02b2b; }
.dpad.press-left .dpad-arrow.left { border-right-color: #e02b2b; }
.dpad.press-right .dpad-arrow.right { border-left-color: #e02b2b; }

/* —— 右侧按钮区 —— */
.btns-wrap { flex: 0 0 auto; display: flex; flex-direction: column; gap: 12px; align-items: center; }

.turbo-row { display: flex; gap: 12px; }
.turbo-btn {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 6px; margin: -6px; /* 扩大判定区 */
  touch-action: none;
}
.turbo-cap {
  --turbo-size: clamp(30px, 9vw, 40px);
  width: var(--turbo-size); height: var(--turbo-size);
  border-radius: 50%;
  background: radial-gradient(circle at 38% 30%, #ff7a6e, #d92020 62%, #a81414);
  box-shadow: 0 3px 5px rgba(0,0,0,.55), inset 0 1px 1px rgba(255,255,255,.35);
}
.turbo-btn label { font-size: 9px; font-weight: 700; color: #d0d0d0; letter-spacing: 1px; pointer-events: none; }
.turbo-btn.pressed .turbo-cap {
  background: radial-gradient(circle at 38% 30%, #ffb0a8, #ff3b30 62%, #c41c1c);
  transform: translateY(1px);
  box-shadow: 0 1px 2px rgba(0,0,0,.55), inset 0 1px 1px rgba(255,255,255,.35);
}

.ab-row { display: flex; gap: 16px; }
.ab-btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px; margin: -8px; /* 扩大判定区 */
  touch-action: none;
}
.ab-cap {
  --ab-size: clamp(60px, 18vw, 84px);
  width: var(--ab-size); height: var(--ab-size);
  border-radius: 22%;
  background: linear-gradient(180deg, #fafafa 0%, #dcdcda 100%);
  box-shadow: 0 4px 8px rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.9);
  display: flex; align-items: center; justify-content: center;
}
.ab-cap::before {
  content: "";
  width: 74%; height: 74%; border-radius: 50%;
  background: radial-gradient(circle at 38% 30%, #ff6b5e, #d92020 60%, #9e1010);
  box-shadow: 0 3px 6px rgba(0,0,0,.35), inset 0 2px 2px rgba(255,255,255,.4);
}
.ab-btn label {
  font-size: 15px; font-weight: 800; color: #c22; letter-spacing: 1px;
  font-style: italic; pointer-events: none;
}
.ab-btn.pressed .ab-cap::before {
  background: radial-gradient(circle at 38% 30%, #ffa79e, #ff3b30 60%, #b81818);
  transform: translateY(2px) scale(.97);
  box-shadow: 0 1px 2px rgba(0,0,0,.35), inset 0 2px 2px rgba(255,255,255,.4);
}

/* —— Select / Start —— */
.ss-row {
  display: flex; justify-content: center; gap: 26px;
  background: linear-gradient(180deg, #fafaf8 0%, #e6e4e1 100%);
  border-radius: 12px;
  margin: 12px auto 0;
  padding: 10px 26px;
  width: fit-content;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.12);
}
.ss-btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 6px 4px; margin: -6px -4px;
  touch-action: none;
}
.ss-cap {
  width: clamp(52px, 16vw, 72px); height: 16px;
  border-radius: 9px;
  background: linear-gradient(180deg, #3a3a3a 0%, #191919 100%);
  box-shadow: 0 2px 4px rgba(0,0,0,.4), inset 0 1px 0 rgba(255,255,255,.12);
}
.ss-btn label { font-size: 10px; font-weight: 800; letter-spacing: 2px; color: #c22; pointer-events: none; }
.ss-btn.pressed .ss-cap { transform: translateY(1px); background: #111; }

/* ═══════════════ MENU 面板 ═══════════════ */
.menu-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.62);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
}
.menu-panel {
  width: min(320px, 84vw);
  background: #1d1d1f;
  border-radius: 18px;
  padding: 20px 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.menu-game-name {
  color: #999; font-size: 13px; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding-bottom: 6px;
}
.menu-item {
  border: none; cursor: pointer;
  background: #2c2c2e; color: #f2f2f2;
  font-size: 16px; text-align: left;
  padding: 14px 16px; border-radius: 12px;
}
.menu-item:active { background: #3a3a3c; }
.menu-quit { color: #ff6b5e; }

/* ═══════════════ Toast ═══════════════ */
.toast {
  position: fixed; z-index: 200;
  top: calc(env(safe-area-inset-top, 0px) + 16px);
  left: 50%; transform: translateX(-50%);
  background: rgba(28,28,30,.95); color: #fff;
  font-size: 14px; padding: 10px 20px; border-radius: 20px;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
  white-space: nowrap;
  pointer-events: none;
}

/* ═══════════════ 桌面(鼠标)适配 ═══════════════ */
@media (hover: hover) and (pointer: fine) {
  .controller { display: none; }
  .game-area { padding: 16px; }
  #nes-canvas {
    width: auto; height: 100%;
    max-width: 100%;
  }
  .drop-zone:hover { border-color: #e02b2b; background: rgba(224,43,43,.06); }
  .recent-item:hover { background: #2a2a2a; }
  .menu-item:hover { background: #3a3a3c; }
}
