/* Four direction keys; independent contacts and corner hit areas allow diagonals. */
.game-stage { min-width: 0; }
.touch {
  --pad-unit: clamp(34px, 10vw, 48px);
  --action-size: clamp(44px, 12vw, 58px);
  display: grid; grid-template-columns: auto minmax(44px, 1fr) auto;
  align-items: center; gap: 8px;
  width: min(100%, 560px); margin: 12px auto 0; padding: 10px 8px;
  border: 1px solid #343a45; border-radius: 14px;
  background: linear-gradient(145deg, #1c2028, #14171d);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.touch, .touch * { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-user-drag: none; }
.touch button { touch-action: none; }
.touch .dpad { position: relative; display: block; width: calc(var(--pad-unit) * 3); height: calc(var(--pad-unit) * 3); }
.touch .dpad-key {
  --turn: 0deg;
  position: absolute; top: 0; left: var(--pad-unit);
  width: var(--pad-unit); height: calc(var(--pad-unit) * 4 / 3);
  min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent;
  clip-path: polygon(0 0, 100% 0, 100% 63%, 50% 100%, 0 63%);
  transform-origin: 50% calc(var(--pad-unit) * 1.5);
  transform: rotate(var(--turn)); transition: none;
}
.touch .dpad-key:active { transform: rotate(var(--turn)); }
.touch .dpad-key[data-key="7"] { --turn: 90deg; }
.touch .dpad-key[data-key="5"] { --turn: 180deg; }
.touch .dpad-key[data-key="6"] { --turn: 270deg; }
.dpad-key svg { display: block; width: 100%; height: 100%; pointer-events: none; }
.dpad-key .arm { fill: #242830; stroke: #8b929d; stroke-width: 1.5; }
.dpad-key .arrow { fill: none; stroke: #d5d9df; stroke-width: 1.7; stroke-linejoin: round; }
.dpad-key.pressed .arm { fill: #625130; stroke: #f3c65b; }
.dpad-key.pressed .arrow { fill: #f3c65b; stroke: #ffe4a8; }
.touch .dpad-key:focus-visible .arm { stroke: #ffe4a8; stroke-width: 3; }
.touch-system { display: flex; flex-direction: column; gap: 9px; align-items: center; }
.touch .touch-system button { width: 52px; max-width: 100%; min-height: 44px; padding: 8px 2px; border-radius: 8px; font-size: 9px; letter-spacing: .3px; color: #c5cbd4; }
.touch-actions { display: flex; gap: 7px; align-items: center; padding-block: 12px; }
.touch .touch-actions .action { width: var(--action-size); min-width: 0; height: var(--action-size); padding: 0; border-radius: 50%; border: 2px solid #ab8b47; background: #3f3523; color: #ffe3a0; font-size: 21px; box-shadow: 0 4px 0 #080b10; }
.touch-actions [data-key="1"] { margin-top: 24px; }
.touch-actions [data-key="0"] { margin-bottom: 24px; }
.touch-system button.pressed, .touch .touch-actions .action.pressed { background: #785e2b; color: #fff2d2; border-color: #ffdd8a; box-shadow: none; }
body.game-fullscreen { overflow: hidden; }
.game-stage:fullscreen, .game-stage.is-fullscreen {
  position: fixed; inset: auto; top: var(--game-view-top, 0px); left: var(--game-view-left, 0px); z-index: 1000; display: flex; flex-direction: column;
  width: var(--game-view-width, 100%); height: 100vh; height: var(--game-view-height, 100dvh); margin: 0;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  overflow: hidden; background: #080b10;
}
.is-fullscreen .screen-top { display: none; }
.is-fullscreen .screen { order: 1; flex: 1 1 0; min-height: 0; max-height: none; width: 100%; aspect-ratio: auto; border: 0; }
.is-fullscreen .screen canvas, .is-fullscreen .screen img { width: 100%; height: 100%; object-fit: contain; }
.is-fullscreen .touch { order: 2; flex: 0 0 auto; margin: 0 auto; border-radius: 12px; }
.is-fullscreen .screen-bottom { order: 3; flex: 0 0 auto; width: min(100%, 560px); align-self: center; padding: 3px 8px; border: 0; border-radius: 0; background: transparent; }
.is-fullscreen .screen-bottom button { min-height: 44px; padding: 6px 10px; }
.is-fullscreen .screen-bottom>span { display: none; }
@media (orientation: landscape) and (max-height: 600px) {
  .is-fullscreen .touch { --pad-unit: 36px; --action-size: 48px; padding-block: 3px; max-width: 620px; }
  .is-fullscreen .touch-system { flex-direction: row; justify-content: center; }
  .is-fullscreen .touch-actions { padding-block: 0; gap: 16px; }
  .is-fullscreen .touch-actions [data-key] { margin: 0; }
  .is-fullscreen .waiting { padding: 9px; bottom: 8%; }
  .is-fullscreen .waiting span { font-size: 14px; }
  .is-fullscreen .waiting small { font-size: 10px; margin-top: 3px; }
}

/* Give controls their own space. The canvas must never span their grid cells. */
.game-stage.is-fullscreen.mobile-room-ui {
  --rail: clamp(118px, 16vw, 164px);
  isolation: isolate; display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  background: #000;
}
.game-stage.is-fullscreen.mobile-room-ui > .screen {
  grid-area: 1 / 1; width: 100%; height: 100%; min-width: 0;
}
.is-fullscreen.mobile-room-ui .screen canvas,
.is-fullscreen.mobile-room-ui .screen img {
  inset: 0; width: 100%; height: 100%; object-fit: contain; object-position: center;
}
.is-fullscreen.mobile-room-ui .touch:not([hidden]) {
  grid-area: 2 / 1; min-width: 0; width: min(100%, 560px);
  margin: 0 auto; background: #10161e; border: 0;
}
.is-fullscreen.mobile-room-ui > .screen-bottom {
  position: absolute; top: max(6px, env(safe-area-inset-top));
  right: max(6px, env(safe-area-inset-right)); z-index: 30;
  display: flex; flex-direction: column; align-items: stretch;
  width: auto; max-width: 160px; gap: 4px; padding: 0;
  background: transparent;
}
.is-fullscreen.mobile-room-ui .screen-bottom > button {
  min-height: 44px; padding: 6px; font-size: 10px; white-space: normal;
}
@media (orientation: landscape) {
  .game-stage.is-fullscreen.mobile-room-ui {
    --rail: max(clamp(118px, 16vw, 164px), calc((100% - var(--game-view-height, 100dvh) * 1.066667) / 2));
    grid-template-columns: var(--rail) minmax(0, 1fr) var(--rail);
    grid-template-rows: minmax(0, 1fr);
  }
  .game-stage.is-fullscreen.mobile-room-ui > .screen { grid-area: 1 / 2; }
  .is-fullscreen.mobile-room-ui > .touch:not([hidden]) {
    --pad-unit: clamp(34px, 4.5vw, 48px);
    --action-size: clamp(44px, 5.8vw, 60px);
    grid-area: 1 / 1 / 2 / -1; width: 100%; max-width: none;
    grid-template-columns: var(--rail) minmax(0, 1fr) var(--rail);
    grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr);
    grid-template-areas: "pad . ." "pad . actions" "pad . system";
    align-self: stretch; justify-items: center; gap: 0; padding: 0;
    background: none; pointer-events: none;
  }
  .is-fullscreen.mobile-room-ui > .touch > * { pointer-events: auto; }
  .is-fullscreen.mobile-room-ui .touch .dpad { grid-area: pad; }
  .is-fullscreen.mobile-room-ui .touch-actions {
    grid-area: actions; align-self: center; padding: 0; gap: 6px;
  }
  .is-fullscreen.mobile-room-ui .touch-actions [data-key] { margin: 0; }
  .is-fullscreen.mobile-room-ui .touch-system {
    grid-area: system; align-self: start; flex-direction: row; gap: 4px; padding-top: 12px;
  }
  .game-stage.is-fullscreen.mobile-room-ui:has(> .touch[hidden]) > .screen {
    grid-column: 1 / -1;
  }
}
.game-tools-toggle { display: none; }
.is-fullscreen.mobile-room-ui > .screen-bottom > button { background: #16222ee8; }
.is-fullscreen.mobile-room-ui > .screen-bottom:not(.tools-open) > :not(.game-tools-toggle) { display: none; }
.game-stage.is-fullscreen.mobile-room-ui .screen-bottom > .game-tools-toggle { display: block; align-self: end; width: 44px; height: 44px; font-size: 22px; }
.is-fullscreen.mobile-room-ui > .screen-bottom.tools-open { background: #10161ef2; padding: 5px; border-radius: 10px; }
