:root {
  --bg: #12151a;
  --panel: #1b2129;
  --panel-2: #232b36;
  --line: #2f3a48;
  --text: #e8eef5;
  --muted: #8b98a8;
  --accent: #3d9b7a;
  --accent-hover: #4cb58e;
  --danger: #c45c5c;
  --frame-w: 720px;
  --frame-h: 1280px;
  --radius: 10px;
  --font: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --side-w: 280px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, #1e2a33 0%, transparent 55%),
    radial-gradient(900px 500px at 90% 0%, #1a2430 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.app {
  min-height: 100%;
}

.nav-toggle {
  display: none;
  position: fixed;
  top: calc(10px + var(--safe-top));
  left: 10px;
  z-index: 60;
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(27, 33, 41, 0.94);
  color: var(--text);
  cursor: pointer;
  padding: 0;
  align-items: center;
  justify-content: center;
}
.nav-toggle-bars,
.nav-toggle-bars::before,
.nav-toggle-bars::after {
  display: block;
  width: 18px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  position: relative;
}
.nav-toggle-bars::before,
.nav-toggle-bars::after {
  content: "";
  position: absolute;
  left: 0;
}
.nav-toggle-bars::before { top: -6px; }
.nav-toggle-bars::after { top: 6px; }

.nav-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.45);
}

.side-nav {
  display: none;
  flex-direction: column;
  width: var(--side-w);
  max-width: 86vw;
  background: var(--panel);
  border-right: 1px solid var(--line);
  z-index: 50;
}
.side-nav-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(16px + var(--safe-top)) 14px 14px;
  border-bottom: 1px solid var(--line);
}
.side-nav-head strong {
  display: block;
  font-size: 1rem;
}
.side-sub {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}
.nav-close {
  margin-left: auto;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  width: 36px;
  height: 36px;
}
.game-nav {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.game-nav-btn {
  appearance: none;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 0.95rem;
  cursor: pointer;
}
.game-nav-btn:hover {
  background: var(--panel-2);
}
.game-nav-btn.is-active {
  border-color: #3d6a57;
  background: rgba(61, 155, 122, 0.16);
  color: #d7f3e8;
}
.side-nav-foot {
  padding: 12px 14px calc(14px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.main-wrap { min-width: 0; }

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(18, 21, 26, 0.88);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 8px;
  background: linear-gradient(145deg, #2a4a3f, #1a2e28);
  border: 1px solid #3d6a57;
  font-weight: 700;
  letter-spacing: 0.04em;
  font-size: 13px;
  flex: 0 0 auto;
}
.brand h1, #active-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 650;
  letter-spacing: 0.02em;
}
.sub { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; }

.top-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
}
.global-scale, .scale-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.85rem;
}
input[type="range"] {
  width: 120px;
  accent-color: var(--accent);
}
.help-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
}
.help-link:hover { color: var(--accent-hover); text-decoration: underline; }

.status {
  margin: 0;
  padding: 8px 24px 0;
  font-size: 0.85rem;
  color: var(--muted);
  min-height: 1.2em;
}
.status.is-error { color: #f0a8a8; }

.drop-hint-line {
  margin: 0 0 8px;
  font-size: 0.78rem;
  color: var(--muted);
}

.empty-drop {
  width: calc(var(--frame-w) * var(--slot-scale, 0.55));
  height: calc(var(--frame-h) * var(--slot-scale, 0.55));
  min-width: 220px;
  min-height: 280px;
  max-width: 90vw;
  border: 2px dashed var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: rgba(0, 0, 0, 0.2);
}
.slot-empty.is-dragover .empty-drop {
  border-color: var(--accent);
  background: rgba(61, 155, 122, 0.12);
}
.empty-drop p { margin: 0; line-height: 1.5; }
.empty-drop .muted { color: var(--muted); font-size: 0.85rem; }
.btn-file-lg { padding: 10px 18px; font-size: 0.95rem; }

.btn-add, .btn-file, .btn-open {
  appearance: none;
  border: 1px solid #4a5a4f;
  background: var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}
.btn-add:hover, .btn-file:hover, .btn-open:hover {
  border-color: var(--accent);
  background: #24332c;
}

.board {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: flex-start;
  gap: 20px;
  padding: 20px 24px 40px;
  overflow-x: auto;
  overflow-y: visible;
  min-height: calc(100vh - 88px);
}

.slot {
  flex: 0 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28);
  position: relative;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.slot.is-dragover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(61, 155, 122, 0.35), 0 8px 28px rgba(0, 0, 0, 0.28);
}
.slot.is-loading { opacity: 0.85; }

.slot-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  padding: 4px 2px 10px;
  border-bottom: 1px solid var(--line);
}
.slot-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}
.slot-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.btn-reset {
  appearance: none;
  border: 1px solid #4a5a4f;
  background: var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.btn-reset:hover {
  border-color: var(--accent);
  background: #24332c;
}
.btn-reset:active { transform: translateY(1px); }

.frame-shell {
  resize: both;
  overflow: auto;
  width: calc(var(--frame-w) * var(--slot-scale, 0.55));
  height: calc(var(--frame-h) * var(--slot-scale, 0.55));
  min-width: 180px;
  min-height: 320px;
  max-width: 90vw;
  max-height: 85vh;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #0a0c0f;
  position: relative;
}

.slot.has-game.is-dragover .frame-shell::before {
  content: "松开以替换为该 zip";
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  background: rgba(61, 155, 122, 0.22);
  color: var(--text);
  font-size: 0.95rem;
  pointer-events: none;
}

.frame-scaler {
  width: var(--frame-w);
  height: var(--frame-h);
  transform-origin: top left;
  transform: scale(var(--slot-scale, 0.55));
}

.game-frame {
  width: var(--frame-w);
  height: var(--frame-h);
  border: 0;
  display: block;
  background: #000;
}

.touch-unlock {
  position: absolute;
  inset: 0;
  z-index: 5;
  border: 0;
  background: rgba(10, 12, 15, 0.82);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
}
.touch-unlock[hidden] { display: none !important; }

.empty {
  color: var(--muted);
  padding: 40px 8px;
}
.error {
  color: #f0a8a8;
  padding: 24px;
  background: #2a1c1c;
  border: 1px solid #5a3030;
  border-radius: 8px;
}

/* —— Mobile / WeChat: drawer sidebar + single fit container —— */
@media (max-width: 900px) {
  .desktop-only { display: none !important; }

  html, body, .app {
    height: 100%;
    overflow: hidden;
  }

  .nav-toggle { display: inline-flex; }

  .nav-backdrop {
    display: block;
  }
  .nav-backdrop[hidden] { display: none !important; }

  .side-nav {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: 8px 0 28px rgba(0, 0, 0, 0.35);
  }
  body.nav-open .side-nav { transform: translateX(0); }

  .main-wrap {
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .top {
    padding: calc(12px + var(--safe-top)) 16px 12px 64px;
    position: relative;
    flex: 0 0 auto;
  }
  #active-title { font-size: 1.05rem; }
  .sub { font-size: 0.78rem; }

  .status {
    padding: 6px 16px 0;
    flex: 0 0 auto;
  }

  .board {
    flex: 1 1 auto;
    flex-direction: column;
    overflow: hidden;
    padding: 8px 0 0;
    gap: 0;
    min-height: 0;
    align-items: stretch;
  }

  .slot {
    display: none;
    width: 100%;
    height: 100%;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    background: transparent;
    padding: 0;
    margin: 0;
    flex-direction: column;
  }
  .slot.is-active {
    display: flex;
  }
  .slot.slot-empty {
    display: none !important;
  }

  .slot-bar {
    flex: 0 0 auto;
    margin: 0;
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    background: rgba(18, 21, 26, 0.92);
  }
  .slot-title { font-size: 0.92rem; }

  .frame-shell {
    flex: 1 1 auto;
    width: 100% !important;
    height: auto !important;
    min-width: 0;
    min-height: 0;
    max-width: none;
    max-height: none;
    resize: none;
    overflow: hidden;
    border: 0;
    border-radius: 0;
  }

  /* Critical for WeChat/X5: no CSS transform on iframe (breaks touch coords) */
  .frame-scaler {
    width: 100%;
    height: 100%;
    transform: none !important;
  }

  .game-frame {
    width: 100%;
    height: 100%;
    min-height: 100%;
  }
}

/* WeChat / compact play mode class set by JS even if width heuristic differs */
body.is-mobile-play .desktop-only { display: none !important; }
body.is-mobile-play .nav-toggle { display: inline-flex; }
body.is-mobile-play .side-nav {
  display: flex;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  transform: translateX(-105%);
  transition: transform 0.22s ease;
  box-shadow: 8px 0 28px rgba(0, 0, 0, 0.35);
}
body.is-mobile-play.nav-open .side-nav { transform: translateX(0); }
body.is-mobile-play .nav-backdrop { display: block; }
body.is-mobile-play .nav-backdrop[hidden] { display: none !important; }
body.is-mobile-play,
body.is-mobile-play html,
body.is-mobile-play .app {
  height: 100%;
  overflow: hidden;
}
body.is-mobile-play .main-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
body.is-mobile-play .top {
  padding: calc(12px + var(--safe-top)) 16px 12px 64px;
  flex: 0 0 auto;
}
body.is-mobile-play .board {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  padding: 0;
  gap: 0;
}
body.is-mobile-play .slot {
  display: none;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
  padding: 0;
  flex-direction: column;
}
body.is-mobile-play .slot.is-active { display: flex; }
body.is-mobile-play .slot.slot-empty { display: none !important; }
body.is-mobile-play .slot-bar {
  flex: 0 0 auto;
  margin: 0;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}
body.is-mobile-play .frame-shell {
  flex: 1 1 auto;
  width: 100% !important;
  height: auto !important;
  min-width: 0;
  min-height: 0;
  max-width: none;
  max-height: none;
  resize: none;
  overflow: hidden;
  border: 0;
  border-radius: 0;
}
body.is-mobile-play .frame-scaler {
  width: 100%;
  height: 100%;
  transform: none !important;
}
body.is-mobile-play .game-frame {
  width: 100%;
  height: 100%;
}

body.is-wechat .btn-open { display: inline-flex !important; }
