:root {
  color-scheme: dark;
  --bg: #0a0a0c;
  --panel: #17181b;
  --panel-2: #202126;
  --text: #f2f0ea;
  --muted: #9fa2a8;
  --line: #34363d;
  --cyan: #4fd6d6;
  --amber: #ffb23e;
  --rose: #ff5f7e;
  --green: #82d96a;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Yu Gothic UI", Meiryo, sans-serif;
}

body {
  overflow: hidden;
  touch-action: manipulation;
}

html,
body,
.app-shell,
.app-shell *,
.app-shell *::before,
.app-shell *::after {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button,
input {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

button {
  min-width: 82px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #26282e;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button:hover {
  border-color: var(--cyan);
}

button:active {
  transform: translateY(1px);
}

button[data-label] {
  display: grid;
  place-items: center;
}

button[data-label]::before {
  content: attr(data-label);
  display: block;
  line-height: 1;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.app-shell {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: 64px 1fr;
  min-width: 900px;
  min-height: 600px;
}

.topbar {
  display: grid;
  grid-template-columns: 245px minmax(320px, 1fr) 190px;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: #111216;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border: 2px solid var(--cyan);
  border-left-color: var(--amber);
  border-bottom-color: var(--rose);
  transform: rotate(45deg);
}

h1,
h2,
h3 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 20px;
  font-weight: 700;
  white-space: nowrap;
}

.hud {
  display: grid;
  grid-template-columns: repeat(3, minmax(80px, 1fr));
  gap: 8px;
}

.hud div {
  min-width: 0;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  padding: 5px 8px;
}

.hud div::before {
  content: attr(data-label);
  display: block;
  font-size: 10px;
  color: var(--muted);
  line-height: 12px;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.hud strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
  line-height: 18px;
}

.top-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.main-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(640px, 1fr) 320px;
  gap: 14px;
  padding: 14px;
}

.game-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #050506;
  overflow: hidden;
  touch-action: none;
}

.game-mount {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: inherit;
}

.game-mount canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  object-fit: contain;
  background: #050506;
}

.edge-effect-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.state-overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  pointer-events: none;
  background: rgba(5, 5, 6, 0.45);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.state-overlay.hidden {
  display: none;
}

.state-overlay.state-overlay-clear {
  place-items: start end;
  padding: 14px;
  background: transparent;
}

.state-box {
  display: grid;
  gap: 16px;
  justify-items: center;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(18, 19, 23, 0.92);
  pointer-events: auto;
}

.state-overlay-clear .state-box {
  gap: 10px;
  padding: 12px 14px;
  background: rgba(18, 19, 23, 0.78);
}

#stateTitle {
  font-size: 22px;
  font-weight: 700;
  text-align: center;
}

.state-overlay-clear #stateTitle {
  font-size: 18px;
}

.asset-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--panel);
}

.panel-head {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: baseline;
  gap: 10px;
}

.panel-head h2 {
  font-size: 15px;
}

#stageName {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
  align-items: start;
}

.upload-label {
  display: grid;
  gap: 5px;
  min-width: 0;
  color: var(--muted);
  font-size: 11px;
}

.file-picker-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.file-pick-button {
  display: grid;
  place-items: center;
  min-width: 116px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #26282e;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

.file-pick-button::before {
  content: attr(data-label);
  display: block;
  line-height: 1;
}

.file-pick-button.is-disabled {
  opacity: 0.6;
  cursor: wait;
}

.file-name {
  min-width: 0;
  overflow: hidden;
  color: var(--text);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  min-width: 0;
  opacity: 0;
  pointer-events: none;
}

.upload-progress {
  position: relative;
  display: none;
  height: 5px;
  overflow: hidden;
  border-radius: 999px;
  background: #2c2e34;
}

.upload-progress span {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -45%;
  width: 45%;
  border-radius: inherit;
  background: linear-gradient(90deg, transparent, var(--cyan), transparent);
  animation: qix-upload-progress 1s ease-in-out infinite;
}

.app-shell.is-uploading .upload-progress {
  display: block;
}

.app-shell.is-uploading .game-panel,
.app-shell.is-uploading .top-actions,
.app-shell.is-uploading .asset-list-block,
.app-shell.is-uploading .touch-controls {
  pointer-events: none;
}

@keyframes qix-upload-progress {
  0% {
    left: -45%;
  }
  100% {
    left: 100%;
  }
}

.asset-list-block {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 6px;
}

.asset-list-block h3 {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.asset-list {
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
}

.asset-list li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 5px 6px 5px 8px;
  border-bottom: 1px solid #2c2e34;
}

.asset-list li.is-selected {
  background: rgba(79, 214, 214, 0.12);
  box-shadow: inset 3px 0 0 var(--cyan);
}

.asset-list li:last-child {
  border-bottom: 0;
}

.asset-name {
  min-width: 0;
  overflow: hidden;
  color: var(--text);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.select-asset {
  min-width: 0;
  height: 26px;
  justify-content: flex-start;
  padding: 0 4px;
  border-color: transparent;
  background: transparent;
  text-align: left;
}

.select-asset:hover,
.select-asset[aria-pressed="true"] {
  border-color: rgba(79, 214, 214, 0.55);
  background: rgba(79, 214, 214, 0.08);
}

.delete-asset {
  min-width: 0;
  width: 28px;
  height: 26px;
  padding: 0;
  color: var(--rose);
}

.empty-list {
  grid-template-columns: 1fr !important;
  color: var(--muted);
  font-size: 12px;
}

.touch-controls {
  position: absolute;
  inset: auto 12px 12px 12px;
  z-index: 2;
  display: none;
  grid-template-columns: 198px minmax(72px, auto);
  justify-content: space-between;
  align-items: end;
  gap: 12px;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.touch-dpad {
  display: grid;
  grid-template-columns: repeat(3, 66px);
  grid-template-rows: repeat(3, 66px);
  gap: 0;
  pointer-events: auto;
}

.touch-draw {
  display: grid;
  grid-template-rows: repeat(3, 66px);
  gap: 0;
  align-items: center;
  justify-items: end;
  pointer-events: auto;
}

.touch-btn {
  display: grid;
  place-items: center;
  min-width: 0;
  width: 44px;
  height: 44px;
  padding: 0;
  border-color: rgba(79, 214, 214, 0.7);
  background: rgba(17, 18, 22, 0.78);
  box-shadow: 0 0 0 1px rgba(5, 5, 6, 0.45);
  color: var(--text);
  font-weight: 800;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-dpad .touch-btn {
  width: 66px;
  height: 66px;
}

.touch-btn::before {
  display: block;
  line-height: 1;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.touch-btn.is-active {
  border-color: var(--amber);
  background: rgba(255, 178, 62, 0.32);
}

.touch-up {
  grid-column: 2;
  grid-row: 1;
}

.touch-up::before {
  content: "";
  width: 0;
  height: 0;
  border-right: 8px solid transparent;
  border-bottom: 13px solid var(--text);
  border-left: 8px solid transparent;
}

.touch-left {
  grid-column: 1;
  grid-row: 2;
}

.touch-left::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-right: 13px solid var(--text);
  border-bottom: 8px solid transparent;
}

.touch-right {
  grid-column: 3;
  grid-row: 2;
}

.touch-right::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-left: 13px solid var(--text);
}

.touch-down {
  grid-column: 2;
  grid-row: 3;
}

.touch-down::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 13px solid var(--text);
  border-right: 8px solid transparent;
  border-left: 8px solid transparent;
}

.touch-reset,
.touch-pause {
  width: 64px;
}

.touch-reset {
  grid-row: 1;
}

.touch-pause {
  grid-row: 2;
}

.touch-reset::before,
.touch-pause::before {
  font-size: 11px;
  letter-spacing: 0;
}

@media (max-width: 960px) {
  .app-shell {
    min-width: 0;
    min-height: 0;
  }

  .topbar {
    grid-template-columns: 1fr auto;
  }

  .brand {
    display: none;
  }

  .hud {
    grid-column: 1 / 2;
    grid-template-columns: repeat(3, minmax(58px, 1fr));
  }

  .top-actions {
    grid-column: 2 / 3;
  }

  .main-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) 174px;
  }

  .asset-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .panel-head {
    grid-column: 1 / 3;
  }

}

@media (pointer: coarse), (max-width: 760px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
  }

  button {
    height: 32px;
  }

  .app-shell {
    height: 100vh;
    height: 100dvh;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    grid-template-areas:
      "assets"
      "topbar"
      "game";
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .topbar {
    grid-area: topbar;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 8px;
  }

  .hud {
    grid-template-columns: repeat(3, minmax(48px, 1fr));
    gap: 5px;
  }

  .hud div {
    height: 34px;
    padding: 4px 6px;
  }

  .hud div::before {
    font-size: 9px;
  }

  .hud strong {
    font-size: 14px;
    line-height: 16px;
  }

  .top-actions {
    gap: 5px;
  }

  .top-actions button {
    min-width: 58px;
    padding: 0 8px;
    font-size: 12px;
  }

  .main-layout {
    display: contents;
  }

  .game-panel {
    --touch-control-space: 214px;
    grid-area: game;
    display: grid;
    grid-template-rows: auto var(--touch-control-space);
    align-self: start;
    align-content: start;
    margin: 0 8px 8px;
    border-radius: 6px;
  }

  .game-mount {
    grid-row: 1;
    align-self: start;
    height: auto;
    aspect-ratio: 4 / 3;
  }

  .edge-effect-overlay,
  .state-overlay {
    bottom: var(--touch-control-space);
  }

  .touch-controls {
    position: static;
    grid-row: 2;
    display: grid;
    align-self: stretch;
    align-items: center;
    padding: 8px 10px 10px;
  }

  .state-overlay.state-overlay-clear {
    inset: 0;
    place-items: end end;
    padding: 0 20px 18px 0;
  }

  .state-overlay.state-overlay-clear ~ .touch-controls {
    visibility: hidden;
    pointer-events: none;
  }

  .asset-panel {
    grid-area: assets;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    gap: 8px;
    padding: 8px;
    margin: 8px 8px 0;
  }

  .asset-panel .panel-head,
  .asset-panel .asset-list-block {
    display: none;
  }

}

@media (max-width: 480px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .top-actions {
    display: none;
  }

  .main-layout {
    grid-template-rows: auto auto;
  }

  .touch-controls {
    inset: auto;
    grid-template-columns: 180px minmax(72px, auto);
    padding: 8px;
  }

  .game-panel {
    --touch-control-space: 196px;
  }

  .touch-dpad {
    grid-template-columns: repeat(3, 60px);
    grid-template-rows: repeat(3, 60px);
  }

  .touch-draw {
    grid-template-rows: repeat(3, 60px);
  }

  .touch-dpad .touch-btn {
    width: 60px;
    height: 60px;
    font-size: 12px;
  }

  .touch-reset,
  .touch-pause {
    width: 72px;
  }
}
