:root {
  --bg: #120d16;
  --surface: rgba(24, 18, 30, 0.9);
  --surface-solid: #1d1624;
  --border: rgba(255, 255, 255, 0.12);
  --text: #ece8f2;
  --muted: #a79fb5;
  --accent: #ffd65a;
  --accent-text: #2a2008;
  --danger: #e0584c;
  --ok: #6fcf6f;
  --btn: rgba(255, 255, 255, 0.08);
  --btn-hover: rgba(255, 255, 255, 0.16);
  --scrim: rgba(8, 5, 12, 0.78);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --surface: rgba(255, 252, 246, 0.94);
    --surface-solid: #fffcf6;
    --border: rgba(40, 30, 20, 0.16);
    --text: #2a2230;
    --muted: #6b6275;
    --accent: #b9770e;
    --accent-text: #fff8ea;
    --danger: #b3382d;
    --ok: #2f8f4e;
    --btn: rgba(40, 30, 20, 0.07);
    --btn-hover: rgba(40, 30, 20, 0.14);
    --scrim: rgba(245, 240, 232, 0.82);
    --shadow: 0 10px 40px rgba(40, 30, 20, 0.2);
  }
}
:root[data-theme="light"] {
  --surface: rgba(255, 252, 246, 0.94);
  --surface-solid: #fffcf6;
  --border: rgba(40, 30, 20, 0.16);
  --text: #2a2230;
  --muted: #6b6275;
  --accent: #b9770e;
  --accent-text: #fff8ea;
  --danger: #b3382d;
  --ok: #2f8f4e;
  --btn: rgba(40, 30, 20, 0.07);
  --btn-hover: rgba(40, 30, 20, 0.14);
  --scrim: rgba(245, 240, 232, 0.82);
  --shadow: 0 10px 40px rgba(40, 30, 20, 0.2);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 17px/1.5 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: none; }
canvas { position: fixed; inset: 0; display: block; }
[hidden] { display: none !important; }

/* 顶部状态栏 */
#hud { position: fixed; top: 0; left: 0; right: 0; display: flex; flex-wrap: wrap; gap: 6px 16px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  padding-top: max(10px, env(safe-area-inset-top)); }
.stat { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.stat .k { color: var(--muted); font-size: 15px; }
.stat b { font-variant-numeric: tabular-nums; font-weight: 600; }
.bar { width: 72px; height: 8px; border-radius: 4px; background: var(--btn); overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; background: var(--ok); transition: width .15s; }
.bar i.low { background: var(--danger); }

/* 事件提示 */
#toasts { position: fixed; left: 16px; top: 64px; margin: 0; padding: 0; list-style: none; display: flex;
  flex-direction: column; gap: 6px; pointer-events: none; max-width: min(320px, calc(100vw - 32px)); }
.toast { padding: 6px 10px; border-radius: 6px; background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--muted); font-size: 16px; animation: toast-in .2s ease-out; }
.toast.k-ghost, .toast.k-death { border-left-color: var(--danger); }
.toast.k-door { border-left-color: var(--accent); }
.toast.k-turret { border-left-color: var(--ok); }
.toast.out { opacity: 0; transition: opacity .4s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* 底部操作栏 */
#bar { position: fixed; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 8px;
  padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border); }
#bar button { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 76px; min-height: 52px;
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--btn); color: var(--text);
  font: inherit; cursor: pointer; touch-action: manipulation; }
#bar button:hover:not(:disabled) { background: var(--btn-hover); }
#bar button:focus-visible, .overlay button:focus-visible, .diff input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
#bar button:disabled { opacity: .42; cursor: not-allowed; }
#bar button.small { min-width: 56px; }
#bar .t { font-weight: 600; }
#bar .s { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* 触屏摇杆 */
#touch { position: fixed; left: 16px; bottom: 100px; }
#joy { position: relative; width: 112px; height: 112px; border-radius: 50%; background: var(--btn); border: 1px solid var(--border); touch-action: none; }
#joy-knob { position: absolute; left: 50%; top: 50%; width: 48px; height: 48px; margin: -24px 0 0 -24px; border-radius: 50%;
  background: var(--accent); opacity: .85; pointer-events: none; }

/* 遮罩与面板 */
.overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: var(--scrim); overflow-y: auto; }
.overlay[hidden] { display: none; }
.panel { width: min(520px, 100%); padding: 28px; border-radius: 14px; background: var(--surface-solid);
  border: 1px solid var(--border); box-shadow: var(--shadow); max-height: 100%; overflow-y: auto; }
.panel h1 { margin: 0 0 8px; font-size: 34px; letter-spacing: .08em; }
.panel h2 { margin: 0 0 8px; font-size: 28px; }
.lead { margin: 0 0 14px; color: var(--muted); font-size: 17px; }
.rules { margin: 0 0 12px; padding-left: 20px; }
.rules li { margin: 4px 0; }
.keys { margin: 0 0 16px; padding: 10px 12px; border-radius: 8px; background: var(--btn); color: var(--muted); font-size: 15px; }
.diff { display: flex; gap: 8px; margin-bottom: 16px; }
.diff label { flex: 1; cursor: pointer; }
.diff input { position: absolute; opacity: 0; pointer-events: none; }
.diff span, .diff small { display: block; text-align: center; }
.diff label { display: flex; flex-direction: column; align-items: center; padding: 10px 6px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--btn); }
.diff label:has(input:checked) { border-color: var(--accent); background: var(--btn-hover); }
.diff span { font-weight: 600; }
.diff small { color: var(--muted); font-size: 14px; }
.overlay button { min-height: 44px; padding: 8px 20px; border: 1px solid var(--border); border-radius: 8px; background: var(--btn);
  color: var(--text); font: inherit; font-weight: 600; cursor: pointer; touch-action: manipulation; }
.overlay button:hover { background: var(--btn-hover); }
.overlay button.primary { width: 100%; background: var(--accent); color: var(--accent-text); border-color: transparent; }
.overlay .row { display: flex; gap: 8px; }
.overlay .row .primary { flex: 2; }
.overlay .row button:not(.primary) { flex: 1; }
.credit { margin: 14px 0 0; color: var(--muted); font-size: 14px; text-align: center; }
.result { margin: 0 0 16px; }
.result div { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.result dt { color: var(--muted); }
.result dd { margin: 0; font-weight: 600; text-align: right; }

@media (max-width: 640px) {
  #hud { gap: 4px 12px; padding: 8px 12px; font-size: 15px; }
  #toasts { top: 100px; }
  #touch { bottom: 176px; }
  #bar button { min-width: 60px; padding: 6px 8px; }
  .panel { padding: 20px; }
  .panel h1 { font-size: 30px; }
}
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  .bar i { transition: none; }
}
