/* A running level owns the viewport. Site navigation returns on leaving play. */
body:has(.game-layout) .site-header,
body:has(.game-layout) .site-footer {
  display: none;
}
main:has(> .game-layout) {
  height: 100dvh;
  min-height: 0;
  padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right))
    max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
}
.game-layout {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  gap: 4px;
  padding: 0;
}
.game-topbar {
  flex: 0 0 44px;
  flex-wrap: nowrap;
  min-width: 0;
  gap: 8px;
}
.game-title,
.game-title > div {
  min-width: 0;
}
.game-title {
  gap: 8px;
}
.game-title h1 {
  font-size: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.game-topbar button {
  min-width: 44px;
  min-height: 44px;
  flex-shrink: 0;
}
.game-actions {
  flex-shrink: 0;
  width: auto;
  flex-wrap: nowrap;
  gap: 6px;
}
.game-actions .btn {
  padding: 6px 12px;
  font-size: 14px;
}
#game-info {
  font-size: 20px;
  line-height: 1;
}
.game-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.game-options button {
  min-height: 44px;
}
.game-play-area {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  gap: 4px;
}
.game-layout .game-stage {
  display: flex;
  flex-direction: column;
  align-self: stretch;
  min-width: 0;
  min-height: 0;
  padding: 4px;
}
.game-views {
  flex: 1;
  min-height: 0;
  gap: 4px;
  grid-template-rows: minmax(0, 1fr);
  align-items: center;
}
.game-views.split {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.game-views[data-split='vertical'].split {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
}
.game-layout .game-views.split .game-view + .game-view {
  border: 0;
  padding: 0;
}
.game-layout .game-views canvas {
  width: min(100%, calc(var(--room-height, 60dvh) * var(--room-aspect, 1.5)));
  height: auto;
  max-height: var(--room-height, 60dvh);
}
.game-layout .view-label {
  padding: 2px 4px;
}
.game-layout .game-underbar {
  flex: 0 0 auto;
  flex-wrap: nowrap;
  gap: 6px;
  padding: 4px 0 0;
  font-size: 9px;
  line-height: 12px;
  white-space: nowrap;
  overflow: hidden;
}
.game-underbar > span {
  overflow: hidden;
  text-overflow: ellipsis;
}
.game-underbar > span:empty {
  display: none;
}
.game-hud {
  display: flex;
  flex-wrap: nowrap;
  align-items: baseline;
  justify-content: center;
  gap: 12px;
  padding: 0;
  font-size: 12px;
}
.game-hud h2,
.game-hud .pair-dots {
  display: none;
}
.game-hud .stat-pairs {
  font-size: 14px;
  margin: 0;
}
.game-hud .stats-row,
.game-hud .stats-row > div {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 0;
  margin: 0;
  border: 0;
}
.game-hud .stats-row strong {
  font-size: 12px;
  margin: 0;
}
.game-layout .player-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 0;
  min-width: 0;
}
.game-layout .shared-controls:empty {
  display: none;
}
.game-layout .player-items {
  gap: 4px;
}
/* Keep the item strip and note at the same fixed height, even when empty. */
.game-layout .inventory-player {
  height: 80px;
  padding: 3px 5px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr) 18px;
  column-gap: 6px;
}
.game-layout .inventory-heading {
  grid-column: 2;
  grid-row: 1 / 3;
  flex-wrap: wrap;
  max-width: 110px;
  gap: 0 4px;
  justify-content: space-between;
  align-content: center;
}
.game-layout .inventory-action[data-inventory-action='previous'],
.game-layout .inventory-action[data-inventory-action='next'] {
  display: none;
}
.game-layout .inventory-heading strong {
  display: block;
}
.game-layout .inventory-lives {
  margin: 0;
}
.game-layout .inventory-strip {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  padding-block: 2px;
}
.game-layout .inventory-selection {
  grid-column: 1;
  grid-row: 2;
}
.game-layout .inventory-note {
  grid-area: 1 / 1 / 3 / 3;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr);
  align-items: start;
  gap: 6px;
}
.game-layout .inventory-note-heading {
  display: contents;
}
.game-layout .inventory-note-heading strong {
  display: none;
}
.game-layout .inventory-note-heading button {
  grid-column: 2;
  grid-row: 1;
}
.game-layout .inventory-note .document-text {
  grid-column: 1;
  grid-row: 1;
  max-height: 100%;
  margin: 0;
}
@media (max-width: 699px) {
  .game-layout .inventory-player:not(.inventory-current) {
    display: none;
  }
  .game-views.split:not([data-split='horizontal']) {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
}
@media (any-pointer: coarse), (max-width: 760px) {
  .game-layout #mouse-status {
    display: none;
  }
  .game-layout .player-actions {
    display: grid;
    grid-template-columns: 104px minmax(0, 1fr) 104px;
    align-items: center;
  }
  .game-layout .player-items {
    grid-column: 1 / -1;
    grid-row: 1;
  }
  .game-layout .touch-controls {
    display: contents;
  }
  .game-layout .touch-joystick {
    grid-column: 1;
    grid-row: 2;
    justify-self: center;
  }
  .game-layout .touch-brake {
    grid-column: 3;
    grid-row: 2;
    justify-self: center;
  }
  .game-layout [data-joystick-side='right'] .touch-joystick {
    grid-column: 3;
  }
  .game-layout [data-joystick-side='right'] .touch-brake {
    grid-column: 1;
  }
  .game-layout .shared-controls {
    grid-column: 2;
    grid-row: 2;
    grid-template-columns: minmax(0, 1fr);
    gap: 2px;
  }
  .game-layout .shared-controls button {
    font-size: 11px;
    padding: 3px;
  }
  .game-layout .shared-controls .small-note {
    display: none;
  }
}
/* Landscape reserves both side rails from the start, including all 12 items. */
@media (orientation: landscape) and (any-pointer: coarse),
  (orientation: landscape) and (max-width: 760px) {
  .game-layout {
    --joystick-size: 96px;
    --control-width: 104px;
    --inventory-width: clamp(128px, 18vw, 160px);
    --inventory-columns: 3;
    --inventory-rows: 4;
    --brake-height: 64px;
    position: relative;
  }
  .game-layout .game-topbar {
    position: absolute;
    inset: 0 auto auto 0;
    width: var(--control-width);
    height: calc(100% - var(--joystick-size) - 4px);
    display: grid;
    grid-template-columns: 20px minmax(0, 1fr);
    grid-template-rows: 44px minmax(0, 1fr);
    gap: 4px;
    /* Shared controls occupy the empty space beside the vertical title. */
    pointer-events: none;
  }
  .game-layout .game-topbar button {
    pointer-events: auto;
  }
  .game-layout .game-title,
  .game-layout .game-actions {
    display: contents;
  }
  .game-layout .game-title > div {
    grid-column: 1;
    grid-row: 2;
    min-height: 0;
    align-self: stretch;
  }
  .game-layout .game-title h1 {
    height: 100%;
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    font-size: 14px;
    line-height: 20px;
  }
  .game-layout #leave-game {
    grid-area: 1 / 1 / 2 / -1;
    justify-self: start;
  }
  .game-layout #pause-game {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    padding: 4px;
    font-size: 12px;
  }
  .game-layout #game-info {
    grid-column: 2;
    grid-row: 2;
    align-self: start;
    justify-self: end;
  }
  .game-play-area {
    grid-template-columns: var(--control-width) minmax(0, 1fr) var(--inventory-width);
    grid-template-rows: minmax(0, 1fr) auto;
    align-items: end;
  }
  .game-layout .game-stage {
    grid-column: 2;
    grid-row: 1;
  }
  .game-layout .player-actions {
    display: contents;
  }
  .game-layout .player-items {
    grid-column: 3;
    grid-row: 1 / -1;
    grid-template-rows: minmax(0, 1fr);
    align-self: stretch;
    min-height: 0;
    margin-bottom: calc(var(--brake-height) + 4px);
  }
  .game-layout .shared-controls {
    display: grid;
    grid-column: 1;
    grid-row: 1;
    align-self: start;
    margin-top: 96px;
    margin-left: 24px;
  }
  .game-layout:has([data-joystick-side='right']) .game-topbar {
    left: auto;
    right: 0;
  }
  .game-play-area:has([data-joystick-side='right']) {
    grid-template-columns: var(--inventory-width) minmax(0, 1fr) var(--control-width);
  }
  .game-play-area:has([data-joystick-side='right']) .player-items {
    grid-column: 1;
  }
  .game-play-area:has([data-joystick-side='right']) .shared-controls {
    grid-column: 3;
  }
  .game-layout .touch-joystick,
  .game-layout .touch-brake {
    grid-row: 1 / -1;
    align-self: end;
  }
  .game-layout .shared-controls button {
    font-size: 10px;
    line-height: 12px;
    padding: 2px;
  }
  .game-hud {
    grid-column: 2;
    grid-row: 2;
  }
  .game-layout .player-items:has(.inventory-player:nth-of-type(2)) {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 44px minmax(0, 1fr);
  }
  .game-layout .player-items:has(.inventory-player:nth-of-type(2)) .inventory-tabs {
    display: flex;
    gap: 4px;
  }
  .game-layout .inventory-tabs button {
    flex: 1;
    min-width: 0;
    height: 44px;
    overflow: hidden;
    font-size: 11px;
    color: var(--text);
    background: #26372c;
    border: 1px solid #52634e;
    border-radius: 6px;
  }
  .game-layout .inventory-tabs button[aria-pressed='true'] {
    border-color: var(--accent);
  }
  .game-layout .inventory-player:not(.inventory-current) {
    display: none;
  }
  .game-layout .inventory-player {
    height: 100%;
    min-height: 0;
    grid-template-rows: 16px minmax(0, 1fr) 18px 44px;
    column-gap: 0;
  }
  .game-layout .inventory-heading {
    display: contents;
  }
  .game-layout .inventory-heading strong,
  .game-layout .inventory-lives {
    grid-row: 1;
    font-size: 11px;
  }
  .game-layout .inventory-heading strong {
    grid-column: 1;
  }
  .game-layout .inventory-lives {
    grid-column: 2;
  }
  .game-layout .inventory-actions {
    grid-column: 1 / -1;
    grid-row: 4;
    justify-content: space-between;
  }
  .game-layout .inventory-action {
    min-width: 44px;
    min-height: 44px;
    padding-inline: 4px;
  }
  .game-layout .inventory-strip {
    grid-column: 1 / -1;
    grid-row: 2;
    display: grid;
    grid-template-columns: repeat(var(--inventory-columns), minmax(0, 1fr));
    grid-template-rows: repeat(var(--inventory-rows), minmax(0, 1fr));
    grid-auto-flow: row;
    gap: 4px;
    overflow: hidden;
  }
  .game-layout .inventory-slot {
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    max-width: 44px;
    max-height: 44px;
    justify-self: center;
    align-self: start;
  }
  .game-layout .inventory-slot canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  .game-layout .inventory-empty {
    display: none;
  }
  .game-layout .inventory-selection {
    grid-column: 1 / -1;
    grid-row: 3;
    font-size: 10px;
  }
  .game-layout .inventory-note {
    grid-area: 1 / 1 / -1 / -1;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) 44px;
  }
  .game-layout .inventory-note-heading button {
    grid-column: 1;
    grid-row: 2;
    justify-self: end;
  }
  @media (max-height: 340px) {
    .game-layout {
      --brake-height: 48px;
    }
    .game-layout:has(.inventory-player:nth-of-type(2)) {
      --inventory-width: clamp(144px, 20vw, 176px);
      --inventory-columns: 4;
      --inventory-rows: 3;
    }
    .game-layout .touch-brake {
      height: var(--brake-height);
    }
  }
  @media (max-width: 699px) {
    .game-layout {
      --control-width: 96px;
    }
  }
}
@media (max-width: 480px) and (max-height: 650px) and (orientation: portrait) {
  .game-layout .player-actions {
    grid-template-columns: 80px minmax(0, 1fr) 80px;
  }
  .game-layout .touch-controls {
    --joystick-size: 80px;
  }
}
