/* =========================================================
   天使之翼 II — 原版卡带运行壳
   布局：游戏画面 → 按键说明 → 功能栏
   ========================================================= */

:root {
  --bg: #0a0a0c;
  --bg2: #121216;
  --line: #26262e;
  --ink: #ece7dd;
  --dim: #8b8b98;
  --red: #c8372d;
  --pad: #1a1a20;

  --hint-h: 34px;   /* 按键说明条高度（fit() 里会读这个） */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  font: 400 14px/1.6 "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
}

/* ---------------- 舞台 ---------------- */
/* 注意：这里刻意**不用** grid/flex 居中。
 * 居中是布局级计算，(容器高 - 画面高) / 2 很容易落到 .5px 上，
 * 乘 dpr 之后就是半个设备像素的偏移 —— 浏览器为了保证合成正确
 * 只能对整张画布做一次重采样，画面于是整体发糊。
 * 所以 #stage 只当定位基准，画面位置由 fit() 用**整数设备像素**算好，
 * 直接写 left/top。 */
#stage {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: block;
  padding: 0;
  overflow: hidden;
  background: radial-gradient(120% 80% at 50% 0%, #16161c 0%, #0a0a0c 70%);
}

.crt {
  position: absolute;
  left: 0;
  top: 0;
  line-height: 0;
  border-radius: 3px;
  padding: 0;
  background: #000;
  box-shadow:
    0 0 0 1px #23232b,
    0 0 40px rgba(200, 55, 45, .07),
    0 10px 40px rgba(0, 0, 0, .8);
}

#screen {
  display: block;
  background: #000;
  /* 先写兼容值再写标准值：Chrome 认 pixelated（最近邻），
     Firefox/Safari 认 crisp-edges。顺序反了会被后一条覆盖。 */
  image-rendering: crisp-edges;
  image-rendering: pixelated;
  outline: none;
  border-radius: 2px;
}

/* ---------------- 按键说明条 ---------------- */
#hint {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 13px;
  min-height: var(--hint-h);
  padding: 5px 84px 5px 16px;   /* 右侧留出 fps 的位置 */
  border-top: 1px solid var(--line);
  background: #0b0b0f;
  font-size: 11px;
  color: #9a9aa8;
  line-height: 1.4;
}

.hgroup {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: 0 0 auto;
}
.hgroup.dim { color: #6b6b79; }
.hgroup .k { display: inline-flex; gap: 2px; }

.hint kbd, #hint kbd {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 3px;
  border: 1px solid #33333d;
  border-bottom-width: 2px;
  background: #1b1b21;
  font: 600 9.5px/1 ui-monospace, Menlo, monospace;
  color: #ddd9d0;
}

#hint .fps {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  font: 500 10px/1 ui-monospace, Menlo, monospace;
  color: #55555f;
  letter-spacing: .3px;
}

/* ---------------- 报错 / 提示 ---------------- */
#fatal {
  display: none;
  position: absolute;
  left: 50%;
  top: 16px;
  transform: translateX(-50%);
  max-width: min(560px, 90%);
  padding: 10px 14px;
  border: 1px solid #6d2a24;
  border-radius: 6px;
  background: #1d0f0e;
  color: #f0b3aa;
  font-size: 12px;
  z-index: 40;
  white-space: pre-wrap;
}

#toast {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translate(-50%, 8px);
  padding: 8px 14px;
  border-radius: 20px;
  background: rgba(20, 20, 26, .94);
  border: 1px solid #30303a;
  color: #d8d8e2;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 40;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- 功能栏 ---------------- */
#bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px calc(7px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: #0c0c10;
}

#bar button {
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--pad);
  color: #c9c9d4;
  font: 500 12px/1 "Noto Sans SC", "Microsoft YaHei", sans-serif;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
#bar button:hover { background: #24242c; border-color: #3a3a45; color: #fff; }
#bar button:active { transform: translateY(1px); }

.sep { flex: 1; }

/* 右下角的渲染标记：位图尺寸 · 整数倍率 */
.ver {
  font: 500 10px/1 ui-monospace, Menlo, monospace;
  color: #4c4c58;
  letter-spacing: .2px;
  padding-left: 4px;
  white-space: nowrap;
}

/* ---------------- 触摸手柄 ---------------- */
.touchpad { display: none; }

/* 窄窗口：先砍掉辅助键（存档/读档这些在功能栏有按钮），保住主操作键 */
@media (max-width: 780px) {
  .hgroup.dim { display: none; }
  #hint { padding-right: 66px; }
}

/* fps + 倍率那一串要占更宽，窗口窄了就藏掉，免得压住按键说明 */
@media (max-width: 700px) {
  #hint .fps { display: none; }
}

@media (max-width: 560px) {
  #hint {
    font-size: 10px;
    gap: 3px 9px;
    padding: 5px 56px 5px 10px;
  }
  #hint kbd { min-width: 17px; height: 17px; font-size: 9px; }
  .ver { display: none; }
}

@media (pointer: coarse) {
  /* 手机上没有键盘，PC 键位说明整条去掉 —— 省下的高度给画面和手柄 */
  #hint { display: none; }

  /* #stage 不再撑满：高度由 fit() 按画面实际高度收紧，
   * 功能栏与手柄紧接其下，多余空间留到页面底部，手柄就不会顶到屏幕最下沿 */
  #stage { flex: 0 0 auto; }

  /* 画面四周没有富余空间了，大范围外发光会被 overflow 裁成硬边，去掉 */
  .crt { box-shadow: 0 0 0 1px #23232b; }

  /* 功能栏挪到画面**上方**。
   * 原位置夹在画面与虚拟摇杆之间，手指按方向键时极易误触，
   * 一碰「重置」整局就重开了。移到顶部后，画面以下的整个操作区
   * 不再有任何会破坏进度的按钮。 */
  #bar {
    order: -1;                    /* #app 是 flex column，直接提到最前 */
    padding: calc(5px + env(safe-area-inset-top, 0px)) 8px 5px;
    border-top: 0;
    border-bottom: 1px solid var(--line);
  }
  #bar button { padding: 7px 9px; font-size: 11px; }

  .touchpad {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: #0c0c10;
    height: 160px;
  }

  /* ---------------- 方向摇杆 ----------------
   * 一整块圆盘当控件，而不是四个独立按钮。
   * 手指按住后可以一直滑：左→下滑到下方，方向会连续切换，
   * 松开才停 —— 这才是十字键的手感。四个独立按钮做不到，
   * 因为 pointerleave 一离开当前按钮就断了。 */
  .stick {
    position: relative;
    flex: 0 0 auto;
    width: 140px;
    height: 140px;
    border-radius: 50%;
    border: 1px solid #3c3c48;
    background:
      radial-gradient(circle at 50% 38%, #34343f 0%, #24242c 52%, #15151a 100%);
    box-shadow:
      inset 0 4px 10px rgba(0, 0, 0, .8),
      inset 0 -2px 5px rgba(255, 255, 255, .05),
      0 4px 14px rgba(0, 0, 0, .55);
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }
  /* 盘面上的十字凹槽，纯装饰 */
  .stick::before {
    content: "";
    position: absolute;
    inset: 13%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(0, 0, 0, .38) 0%, rgba(0, 0, 0, 0) 72%);
    pointer-events: none;
  }
  .stick.active { border-color: #55556a; }

  .ar {
    position: absolute;
    width: 17px;
    height: 14px;
    background: #a3a3b6;
    transition: background .06s linear, filter .06s linear;
    pointer-events: none;
  }
  .ar.up    { left: 50%; top: 12%;    transform: translateX(-50%);
              clip-path: polygon(50% 0, 100% 100%, 0 100%); }
  .ar.down  { left: 50%; bottom: 12%; transform: translateX(-50%);
              clip-path: polygon(0 0, 100% 0, 50% 100%); }
  .ar.left  { top: 50%; left: 12%;    transform: translateY(-50%);
              clip-path: polygon(100% 0, 0 50%, 100% 100%); }
  .ar.right { top: 50%; right: 12%;   transform: translateY(-50%);
              clip-path: polygon(0 0, 100% 50%, 0 100%); }
  .ar.on {
    background: #ffffff;
    filter: drop-shadow(0 0 5px rgba(255, 255, 255, .85));
  }

  .hub {
    position: absolute;
    left: 50%; top: 50%;
    width: 26px; height: 26px;
    margin: -13px 0 0 -13px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 32%, #45454f, #1c1c23);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, .85), 0 1px 0 rgba(255, 255, 255, .08);
    pointer-events: none;
    /* 跟手：--hx/--hy 由 JS 按手指方向写入，最多偏移盘半径 30% */
    transform: translate(var(--hx, 0px), var(--hy, 0px));
    transition: transform .1s ease-out;
  }
  .stick.active .hub { transition: none; }

  /* SELECT / START 收窄：原来占了中间一整条，
   * 拇指从摇杆横移到 A/B 时会扫到它们 */
  .pad-mid {
    display: flex;
    flex-direction: column;
    gap: 9px;
    flex: 0 0 auto;
    align-items: center;
  }
  .sys {
    width: 68px;
    padding: 7px 0;
    border-radius: 14px;
    border: 1px solid #33333d;
    background: #17171c;
    color: #a9a9b8;
    font: 600 9px/1 ui-monospace, Menlo, monospace;
    letter-spacing: .4px;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }

  .pad-right {
    display: flex;
    gap: 10px;
    align-items: center;
    flex: 0 0 auto;
  }
  /* A 红 B 蓝：深灰圆钮在深色背景上会被当成装饰，
   * 玩家不会觉得那能按 */
  .ab {
    width: 58px; height: 58px;
    border-radius: 50%;
    color: #fff;
    font: 700 16px/1 ui-monospace, Menlo, monospace;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    transition: filter .08s linear;
  }
  .ab.a {
    border: 1px solid #6d241c;
    background: radial-gradient(circle at 35% 28%, #e2513f, #9b2418);
    color: #fff2ec;
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, .28), 0 3px 0 #55130d;
  }
  .ab.b {
    border: 1px solid #1d4a7d;
    background: radial-gradient(circle at 35% 28%, #4f9ae0, #1d5a99);
    color: #eaf4ff;
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, .28), 0 3px 0 #12395f;
  }

  /* 按下态用 .pressed（不能用 down，会被方向键定位规则抢走） */
  .ab.pressed, .sys.pressed {
    filter: brightness(1.4);
    transform: translateY(2px) scale(.97);
  }
}

@media (max-height: 560px) {
  #hint { font-size: 10.5px; gap: 2px 10px; padding: 4px 12px; }
  .touchpad { height: 124px; }
  .stick { width: 106px; height: 106px; }
  .ar { width: 13px; height: 10px; }
  .hub { width: 20px; height: 20px; margin: -10px 0 0 -10px; }
  .sys { width: 58px; padding: 6px 0; font-size: 8.5px; }
  .ab { width: 48px; height: 48px; font-size: 14px; }
}
