:root {
  color-scheme: dark;
  --ink: #1c2932;
  --backdrop: #243744;
  --muted: #adc0cc;
  --text: #f1f4f5;
  --key: #e8edf0;
  --key-edge: #8b9eaa;
  --gold: #f9c43a;
  --focus: #aad8e6;
  --app-height: 100vh;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

@supports (height: 100dvh) {
  :root { --app-height: 100dvh; }
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-width: 0;
  margin: 0;
  background: var(--backdrop);
  color: var(--text);
  overscroll-behavior: none;
}

body { touch-action: manipulation; }

button, a { -webkit-tap-highlight-color: transparent; }

button { font: inherit; }

button:focus-visible, a:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 4px;
}

[hidden] { display: none !important; }

.app {
  height: var(--app-height);
  min-height: 260px;
  width: min(100%, 1600px);
  margin-inline: auto;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  min-height: 30px;
  flex: 0 0 auto;
}

h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 650;
  line-height: 1.35;
  white-space: nowrap;
}

h1 span {
  margin-left: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
}

.state-label {
  flex-shrink: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.game-layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 12px;
}

.stage-holder {
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.game-screen {
  position: relative;
  width: min(100%, 960px);
  aspect-ratio: 4 / 3;
  flex: 0 0 auto;
  overflow: hidden;
  background: var(--ink);
  box-shadow: 0 0 0 2px #59717e;
}

#canvas {
  display: none;
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
  object-fit: contain;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
  touch-action: none;
  outline-offset: -3px;
}

#canvas:focus { outline: none; }
#canvas:focus-visible { outline: 3px solid var(--focus); }

.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--ink);
  text-align: center;
}

.overlay-content { max-width: 28em; }

.overlay-title {
  margin: 0 0 10px;
  color: var(--gold);
  font-size: clamp(18px, 4.5vw, 27px);
  font-weight: 650;
  line-height: 1.25;
}

.load-status {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.start-button {
  min-width: 144px;
  min-height: 48px;
  margin-top: 18px;
  padding: 10px 20px;
  border: 0;
  border-radius: 4px;
  background: var(--gold);
  color: var(--ink);
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
}

.start-button:disabled { cursor: progress; opacity: .7; }

.load-detail {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

#spinner { display: none; }

.controls-panel {
  width: min(100%, 720px);
  margin-inline: auto;
}

.game-tip {
  min-height: 20px;
  margin: 0 0 10px;
  color: var(--muted);
  text-align: center;
  font-size: 12px;
  line-height: 1.6;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
  padding-bottom: 3px;
}

.control-button {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 3px;
  min-width: 44px;
  min-height: 58px;
  padding: 6px 4px;
  border: 0;
  border-radius: 4px;
  box-shadow: 0 3px 0 var(--key-edge);
  background: var(--key);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.control-button kbd {
  color: #4a606f;
  font-family: inherit;
  font-size: 10px;
  font-weight: 400;
  line-height: 1.2;
}

.grab-button {
  background: var(--gold);
  box-shadow: 0 3px 0 #b88a15;
}

.grab-button kbd { color: #70571c; }

.control-button.is-pressed {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--key-edge);
}

.grab-button.is-pressed { box-shadow: 0 1px 0 #b88a15; }

.control-button:disabled {
  opacity: .4;
  cursor: default;
}

.attribution {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 3px 12px;
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.5;
}

.attribution a { color: inherit; text-underline-offset: 2px; }

noscript {
  position: fixed;
  inset: auto 12px 20px;
  padding: 12px;
  background: var(--gold);
  color: var(--ink);
}

@media (orientation: landscape) and (min-width: 480px) {
  .game-layout {
    grid-template-columns: minmax(0, 1fr) clamp(172px, 23vw, 232px);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
    gap: 16px;
  }

  .controls-panel {
    align-self: center;
    margin: 0;
  }

  .control-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .game-tip { min-height: 38px; }
}

@media (max-height: 440px) and (orientation: landscape) {
  .app { gap: 7px; padding-top: max(5px, env(safe-area-inset-top)); padding-bottom: max(5px, env(safe-area-inset-bottom)); }
  .topbar { min-height: 22px; }
  h1 { font-size: 16px; }
  h1 span { font-size: 10px; }
  .game-layout { gap: 12px; }
  .game-tip { min-height: 30px; margin-bottom: 6px; font-size: 11px; line-height: 1.4; }
  .control-grid { gap: 7px; }
  .control-button { min-height: 46px; font-size: 12px; gap: 1px; }
  .attribution { font-size: 9px; line-height: 1.3; }
  .overlay-title { margin-bottom: 5px; font-size: 20px; }
  .start-button { margin-top: 10px; min-height: 44px; }
  .load-detail { margin-top: 6px; }
}

@media (max-width: 340px) {
  .app { padding-inline: 8px; gap: 9px; }
  h1 { font-size: 17px; }
  h1 span { margin-left: 5px; font-size: 10px; }
  .control-grid { gap: 7px; }
  .control-button { font-size: 12px; }
  .load-status { font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; }
}
