* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: #101014;
  color: #f4f4f7;
  font-family:
    Inter,
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    sans-serif;
}

body {
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
}

[hidden] {
  display: none !important;
}

.app-shell {
  display: grid;
  width: 100vw;
  height: 100vh;
  place-items: center;
  padding: 18px;
}

.full-stage .app-shell {
  padding: 0;
}

.stage-wrap {
  position: relative;
  display: grid;
  place-items: center;
  width: min(100%, 960px);
  min-width: 0;
}

.full-stage .stage-wrap {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.player-stage {
  overflow: hidden;
  border: 0;
  background: #171720;
}

canvas {
  display: block;
  width: 100%;
  max-width: 960px;
  aspect-ratio: 16 / 9;
  height: auto;
  background: #171720;
  border: 1px solid #333642;
  touch-action: none;
}

.full-stage canvas {
  width: var(--stage-canvas-width, 100vw);
  max-width: none;
  height: var(--stage-canvas-height, 100vh);
  aspect-ratio: auto;
  border: 0;
}

.mobile-game-controls {
  display: none;
}

@media (max-width: 820px) {
  .mobile-game-controls {
    position: fixed;
    z-index: 20;
    inset: 0;
    display: block;
    overflow: hidden;
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
  }

  .mobile-direction-pad {
    position: absolute;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: max(12px, env(safe-area-inset-left));
    width: clamp(165px, 41.8vw, 231px);
    height: clamp(165px, 41.8vw, 231px);
    overflow: hidden;
    border: 2px solid rgba(255, 255, 255, 0.44);
    border-radius: 50%;
    background:
      linear-gradient(
        45deg,
        transparent calc(50% - 1px),
        rgba(255, 255, 255, 0.38) calc(50% - 1px),
        rgba(255, 255, 255, 0.38) calc(50% + 1px),
        transparent calc(50% + 1px)
      ),
      linear-gradient(
        -45deg,
        transparent calc(50% - 1px),
        rgba(255, 255, 255, 0.38) calc(50% - 1px),
        rgba(255, 255, 255, 0.38) calc(50% + 1px),
        transparent calc(50% + 1px)
      ),
      rgba(12, 14, 22, 0.42);
    box-shadow:
      inset 0 0 28px rgba(255, 255, 255, 0.04),
      0 4px 18px rgba(0, 0, 0, 0.22);
    pointer-events: auto;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-direction-pad button {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    color: rgba(255, 255, 255, 0.9);
    font-size: clamp(24px, 6vw, 36px);
    line-height: 1;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-direction-pad button.is-pressed {
    background: rgba(112, 121, 164, 0.82);
  }

  .direction-up {
    padding-bottom: 52% !important;
    clip-path: polygon(0 0, 100% 0, 50% 50%);
  }

  .direction-right {
    padding-left: 52% !important;
    clip-path: polygon(100% 0, 100% 100%, 50% 50%);
  }

  .direction-down {
    padding-top: 52% !important;
    clip-path: polygon(100% 100%, 0 100%, 50% 50%);
  }

  .direction-left {
    padding-right: 52% !important;
    clip-path: polygon(0 100%, 0 0, 50% 50%);
  }

  .mobile-action-pad {
    --q-size: clamp(250px, 64vw, 360px);
    --skill-size: clamp(69.6px, 18vw, 100.8px);
    --skill-half: clamp(34.8px, 9vw, 50.4px);
    position: absolute;
    right: 0;
    bottom: 0;
    width: var(--q-size);
    height: var(--q-size);
  }

  .mobile-action-pad button {
    position: absolute;
    display: grid;
    padding: 0;
    place-items: center;
    border: 1px solid rgba(255, 255, 255, 0.38);
    border-radius: 50%;
    background: rgba(12, 14, 22, 0.58);
    box-shadow:
      inset 0 0 0 2px rgba(255, 255, 255, 0.06),
      0 3px 12px rgba(0, 0, 0, 0.25);
    color: rgba(255, 255, 255, 0.9);
    font-size: clamp(13px, 3.6vw, 18px);
    font-weight: 800;
    line-height: 1;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-action-pad button.is-pressed {
    border-color: rgba(255, 255, 255, 0.85);
    background: rgba(112, 121, 164, 0.82);
  }

  .mobile-action-pad .mobile-primary-action {
    right: calc(-50% - 5px);
    bottom: calc(-50% + 23px);
    width: var(--q-size);
    height: var(--q-size);
    border-width: 2px;
    font-size: clamp(28px, 8vw, 48px);
    pointer-events: auto;
  }

  .mobile-primary-action span {
    position: absolute;
    top: 22%;
    left: 22%;
  }

  .mobile-skill-button {
    width: var(--skill-size);
    height: var(--skill-size);
    pointer-events: auto;
  }

  .mobile-skill-w {
    right: calc(46.3% - var(--skill-half));
    bottom: calc(55.2% - var(--skill-half));
  }

  .mobile-skill-space {
    right: calc(67.7% - var(--skill-half));
    bottom: 10px;
    font-size: clamp(10px, 2.7vw, 14px);
  }
}

@media (max-width: 420px), (max-height: 500px) and (max-width: 820px) {
  .mobile-direction-pad {
    bottom: max(8px, env(safe-area-inset-bottom));
    left: max(8px, env(safe-area-inset-left));
  }
}

.control-hint {
  position: absolute;
  top: 12px;
  left: 50%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  width: max-content;
  max-width: min(760px, calc(100% - 24px));
  transform: translateX(-50%);
  pointer-events: auto;
}

.control-hint button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 4px 8px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 5px;
  background: rgba(15, 16, 22, 0.72);
  color: #f5f7fb;
  font-size: 13px;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  gap: 5px;
}

.control-hint kbd {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  min-height: 18px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.14);
  color: #ffffff;
  font:
    700 11px/1 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
}

.control-hint span {
  line-height: 1;
}

.control-hint button.is-pressed,
.control-hint button:active {
  background: rgba(124, 195, 162, 0.34);
  border-color: rgba(124, 195, 162, 0.72);
}

.screen-zoom-control {
  display: inline-grid;
  grid-template-columns: auto minmax(72px, 112px) 42px;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  max-width: 280px;
  padding: 4px 8px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 5px;
  background: rgba(15, 16, 22, 0.72);
  color: #f5f7fb;
  font-size: 13px;
  line-height: 1;
  pointer-events: auto;
}

.screen-zoom-control input {
  width: 100%;
  min-width: 0;
  accent-color: #7cc3a2;
}

.screen-zoom-control output {
  color: #cdd6e6;
  font:
    700 11px/1 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  text-align: right;
}

.battle-controls {
  position: absolute;
  top: 52px;
  left: 50%;
  display: flex;
  gap: 8px;
  transform: translateX(-50%);
}

.battle-controls button {
  min-height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  background: rgba(24, 25, 32, 0.9);
  color: #fff;
  padding: 0 12px;
  cursor: pointer;
  white-space: nowrap;
}

.battle-controls button:disabled {
  color: #8f96a8;
  cursor: default;
  opacity: 0.62;
}

.player-battle-controls {
  left: 50%;
  right: auto;
  transform: translateX(-50%);
}

.game-control-guide {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: 32;
  width: min(300px, calc(100vw - 32px));
  padding: 20px 22px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  background: rgba(10, 11, 14, 0.94);
  color: rgba(255, 255, 255, 0.82);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.18);
  font-size: 14px;
  line-height: 1.3;
  pointer-events: none;
  transform: translate(-50%, -50%);
  backdrop-filter: blur(8px);
}

.control-guide-button {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 24;
  min-height: 30px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  background: rgba(10, 11, 14, 0.68);
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  backdrop-filter: blur(5px);
}

.game-control-guide h2 {
  margin: 0 0 11px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.18);
  color: rgba(255, 255, 255, 0.94);
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0;
}

.game-control-guide dl {
  display: grid;
  gap: 8px;
  margin: 0;
}

.game-control-guide div {
  display: grid;
  grid-template-columns: minmax(94px, auto) minmax(0, 1fr);
  align-items: baseline;
  gap: 12px;
}

.game-control-guide dt,
.game-control-guide dd {
  margin: 0;
}

.game-control-guide dt {
  color: rgba(255, 255, 255, 0.92);
  font:
    800 13px/1.25 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  white-space: nowrap;
}

.game-control-guide dd {
  min-width: 0;
  color: rgba(255, 255, 255, 0.72);
  font-weight: 700;
  white-space: nowrap;
}

.runtime-debug-root {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 28;
  display: grid;
  justify-items: start;
  gap: 6px;
  max-width: min(360px, calc(100% - 24px));
  color: #e8f2ff;
  pointer-events: none;
}

.runtime-debug-toggle {
  pointer-events: auto;
  min-height: 28px;
  padding: 4px 8px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 5px;
  background: rgba(18, 20, 28, 0.82);
  color: #d7deec;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.runtime-debug-toggle.is-on {
  border-color: rgba(117, 205, 164, 0.88);
  background: rgba(28, 63, 48, 0.9);
  color: #baf3d2;
}

.runtime-debug-hud {
  width: 100%;
  max-height: min(72vh, 520px);
  overflow: hidden;
  padding: 10px;
  border: 1px solid rgba(132, 156, 199, 0.38);
  border-radius: 6px;
  background: rgba(9, 11, 17, 0.86);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.34);
  font:
    600 11px/1.35 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  pointer-events: none;
}

.runtime-debug-summary {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 3px 10px;
  margin: 0;
}

.runtime-debug-summary dt {
  color: #8ec5ff;
}

.runtime-debug-summary dd {
  min-width: 0;
  margin: 0;
  color: #f4f7ff;
  overflow-wrap: anywhere;
}

.runtime-debug-summary .runtime-debug-notice {
  grid-column: 1 / -1;
  margin-top: 4px;
  color: #ffd166;
}

.runtime-debug-status {
  font-weight: 800;
}

.runtime-debug-status.is-on {
  color: #75d69a;
}

.runtime-debug-status.is-off {
  color: #ff7070;
}

.runtime-debug-event-section {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.runtime-debug-event-section h3 {
  margin: 0;
  color: #f4f7ff;
  font-size: 11px;
  line-height: 1.3;
}

.runtime-debug-event-section p {
  margin: 2px 0 0;
  color: #7f8ca7;
  font-size: 10px;
  line-height: 1.3;
}

.runtime-debug-events {
  display: grid;
  gap: 3px;
  margin: 6px 0 0;
  padding: 0 0 0 16px;
  color: #b8c3d8;
}

.runtime-load-error {
  position: absolute;
  inset: 24px;
  display: grid;
  place-items: center;
  z-index: 20;
  border: 1px solid rgba(255, 112, 112, 0.7);
  border-radius: 8px;
  background: rgba(11, 15, 24, 0.88);
  color: #ffd1d1;
  padding: 18px;
  text-align: center;
  font-size: 18px;
  font-weight: 800;
}

@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .app-shell {
    min-height: 100vh;
    padding: 10px;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: 100vw;
    margin: 0;
    padding: 0;
  }

  .control-hint {
    top: 8px;
    left: 50%;
    gap: 4px;
    max-width: calc(100% - 16px);
    transform: translateX(-50%);
  }

  .control-hint button {
    min-height: 24px;
    padding: 3px 6px;
    font-size: 11px;
  }

  .screen-zoom-control {
    grid-template-columns: auto minmax(64px, 90px) 32px;
    min-height: 24px;
    padding: 3px 6px;
    font-size: 11px;
  }

  .battle-controls {
    top: 40px;
  }

  .battle-controls button {
    min-height: 30px;
    padding: 0 8px;
    font-size: 12px;
  }

  .game-control-guide {
    top: 50%;
    left: 50%;
    width: min(280px, calc(100vw - 24px));
    padding: 16px 18px;
    font-size: 12px;
    transform: translate(-50%, -50%);
  }

  .game-control-guide h2 {
    margin-bottom: 8px;
    padding-bottom: 6px;
    font-size: 14px;
  }

  .game-control-guide dl {
    gap: 6px;
  }

  .game-control-guide div {
    grid-template-columns: minmax(82px, auto) minmax(0, 1fr);
    gap: 9px;
  }

  .game-control-guide dt {
    font-size: 11px;
  }
}
