/* Fit the v2 shell to the actual iframe height, including its footer. */
html:has(body.portal-runtime #game:not([hidden])) {
  overflow: hidden;
}
body.portal-runtime:has(#game:not([hidden])) {
  height: var(--mobile-height, 100dvh);
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
body.portal-runtime main {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
}
body.portal-runtime #game {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
body.portal-runtime .game-toolbar,
body.portal-runtime .game-footer,
body.portal-runtime .provenance {
  flex: 0 0 auto;
}
body.portal-runtime .game-shell {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) 118px;
}
body.portal-runtime .stage,
body.portal-runtime.embed .stage,
body.portal-runtime.classic .stage {
  height: 100%;
  min-height: 0;
  aspect-ratio: auto;
}
body.portal-runtime .page-header {
  flex: 0 0 61px;
  width: 100%;
}
@media (max-width: 950px), (max-width: 1100px) and (max-height: 600px) {
  body.portal-runtime .game-shell {
    grid-template-rows: minmax(0, 1fr) auto;
  }
  body.portal-runtime .page-header {
    flex: 0 0 44px;
  }
}
@media (max-width: 950px) and (orientation: landscape),
  (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  body.portal-runtime .page-header {
    flex-basis: 34px;
  }
}

/* v2 layout adaptation retaining the canonical mobile controls and fullscreen. */
#mobile-chat-toggle,
.mobile-header-help {
  display: none;
}
@media (max-width: 950px), (max-width: 1100px) and (max-height: 600px) {
  html,
  body {
    height: 100%;
    min-width: 0;
    overflow: hidden;
    overscroll-behavior: none;
  }
  body {
    display: flex;
    flex-direction: column;
    height: var(--mobile-height, 100dvh);
  }
  input,
  select,
  textarea {
    font-size: 16px !important;
  }
  button {
    touch-action: manipulation;
  }
  .page-header {
    height: 44px;
    min-height: 44px;
    padding: 0 8px;
    flex-shrink: 0;
  }
  .page-header h1 {
    font-size: 14px;
    letter-spacing: 3px;
  }
  .page-header a {
    min-height: 44px;
    display: flex;
    align-items: center;
    font-size: 12px;
  }
  .page-header .build-label {
    display: none;
  }
  .mobile-header-help {
    display: block;
    min-height: 40px;
    padding: 0 8px;
    font-size: 12px;
  }
  main,
  body.embed main,
  body.embedded main {
    flex: 1;
    width: 100%;
    min-height: 0;
    max-width: none;
    margin: 0;
    padding: 0;
  }
  #game,
  body.classic #game {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    margin: 0;
    max-width: none;
  }
  #game[hidden] {
    display: none;
  }
  .game-toolbar {
    flex: 0 0 auto;
    min-height: 42px;
    gap: 4px;
    padding: 4px 6px;
  }
  .game-toolbar .location {
    min-width: 70px;
    flex: 1;
    font-size: 11px;
  }
  #map-name {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  #coordinates,
  #connection {
    display: none;
  }
  .game-toolbar button {
    min-height: 36px;
    min-width: 40px;
    padding: 3px 6px;
    font-size: 11px;
  }
  .compact-label {
    gap: 2px;
    font-size: 10px;
  }
  .compact-label select {
    font-size: 12px !important;
    max-width: 86px;
    min-height: 34px;
  }
  #help-button {
    display: none;
  }
  .game-shell {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .stage,
  body.classic .stage,
  body.embed .stage {
    min-height: 0;
    height: 100%;
    aspect-ratio: auto;
  }
  #world {
    height: 100%;
    width: 100%;
  }
  .vitals {
    left: 7px;
    top: 7px;
    width: 145px;
    padding: 5px 7px;
    font-size: 10px;
  }
  .world-overview {
    right: 7px;
    top: 7px;
    width: 92px;
  }
  .world-overview canvas {
    width: 92px;
    height: 69px;
  }
  .vitals-name button {
    min-height: 27px;
  }
  .vitals-foot {
    font-size: 9px;
  }
  .hud-shell {
    height: auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background-size: auto 100%;
    padding: 3px 4px;
    gap: 3px;
  }
  .hud-left {
    position: static;
    left: auto;
    height: auto;
    overflow: visible;
    display: flex;
    gap: 3px;
    width: 100%;
    padding: 0;
  }
  .hud-left button,
  #mobile-chat-toggle {
    display: block;
    flex: 1;
    min-height: 34px;
    min-width: 40px;
    padding: 3px;
    font-size: 11px;
    white-space: nowrap;
  }
  .native-menu {
    display: flex;
    width: 100%;
    height: 43px;
    gap: 3px;
  }
  .native-menu button {
    flex: 1;
    min-width: 0;
    min-height: 40px;
    font-size: 12px;
    padding: 3px;
  }
  .native-menu button span {
    font-size: 17px;
    display: inline;
    margin-right: 3px;
  }
  .chat {
    display: none;
    max-height: none;
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: 132px;
    width: auto;
    height: min(240px, 43dvh);
    padding: 4px;
    background: #10190ff2;
    border: 1px solid #9b9867;
    border-radius: 8px;
    z-index: 8;
  }
  .mobile-chat-open .chat {
    display: flex;
    flex-direction: column;
  }
  .chat ol {
    flex: 1;
    min-height: 0;
    max-height: none;
    height: auto;
    padding: 5px;
    margin: 0;
    font-size: 13px;
  }
  .chat form {
    min-height: 44px;
  }
  .chat input {
    min-width: 0;
  }
  .chat button {
    min-height: 44px;
    min-width: 50px;
  }
  .game-footer {
    flex: 0 0 auto;
    min-height: 31px;
    max-height: 62px;
    overflow-x: auto;
    flex-wrap: nowrap;
    gap: 4px;
    padding: 2px 5px;
  }
  .game-footer button {
    min-height: 28px;
    white-space: nowrap;
    font-size: 11px;
    padding: 3px 6px;
  }
  #progress,
  #online-count {
    font-size: 10px;
    white-space: nowrap;
  }
  #progress {
    display: none;
  }
  #account-button {
    display: block;
  }
  #logout,
  .provenance {
    display: none;
  }
  .joystick {
    width: 94px;
    height: 94px;
    left: max(12px, env(safe-area-inset-left));
    bottom: 72px;
    z-index: 5;
  }
  .joystick span {
    width: 34px;
    height: 34px;
    left: 28px;
    top: 28px;
  }
  .touch-actions {
    right: max(12px, env(safe-area-inset-right));
    bottom: 75px;
    gap: 7px;
    z-index: 5;
  }
  .touch-actions button {
    min-width: 54px;
    width: 58px;
    height: 58px;
    font-size: 12px;
  }
  .touch-actions #touch-interact {
    width: 66px;
    border-radius: 30px;
  }
  .quick-skills {
    bottom: 10px;
    max-width: 80%;
    gap: 4px;
  }
  .quick-skills button {
    width: 47px;
    height: 45px;
  }
  .drawer {
    left: 8px;
    right: 8px;
    top: 8px;
    bottom: 8px;
    width: auto;
    max-width: 430px;
    margin: auto;
  }
  .drawer button {
    min-height: 37px;
  }
  .drawer .inventory-grid button {
    min-height: 47px;
  }
  .drawer h2 {
    font-size: 15px;
  }
  .drawer #close-drawer {
    min-height: 38px;
    min-width: 38px;
  }
  #login {
    height: 100%;
    margin: 0;
    padding: 12px;
    overflow: auto;
    display: flex;
    flex-direction: column;
  }
  #login[hidden] {
    display: none;
  }
  .login-art {
    display: none;
  }
  .auth-panel {
    max-width: 410px;
    width: 100%;
    margin: auto;
  }
  .auth-panel input,
  .auth-panel button {
    min-height: 44px;
  }
  dialog {
    width: calc(100% - 20px);
    max-height: calc(var(--mobile-height, 100dvh) - 20px);
    overflow: auto;
  }
}
@media (max-width: 950px) and (orientation: landscape),
  (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  .page-header {
    height: 34px;
    min-height: 34px;
  }
  .page-header a,
  .mobile-header-help {
    min-height: 32px;
  }
  .game-toolbar {
    min-height: 34px;
    padding: 2px 6px;
  }
  .compact-label select,
  .game-toolbar button {
    min-height: 30px;
  }
  .hud-shell {
    flex-direction: row;
    align-items: stretch;
    gap: 4px;
  }
  .hud-left {
    width: 40%;
    display: flex;
    flex-wrap: nowrap;
  }
  .hud-left button {
    font-size: 10px;
    min-height: 37px;
    min-width: 35px;
  }
  .native-menu {
    width: 60%;
    height: 40px;
  }
  .native-menu button {
    font-size: 11px;
  }
  .native-menu button span {
    font-size: 14px;
  }
  .vitals {
    width: 130px;
  }
  .vitals label {
    height: 13px;
  }
  .world-overview {
    width: 75px;
  }
  .world-overview canvas {
    width: 75px;
    height: 55px;
  }
  .joystick {
    bottom: 12px;
    width: 86px;
    height: 86px;
  }
  .joystick span {
    left: 24px;
    top: 24px;
  }
  .touch-actions {
    bottom: 16px;
  }
  .touch-actions button {
    width: 50px;
    height: 50px;
    min-width: 50px;
  }
  .quick-skills {
    bottom: 8px;
  }
  .quick-skills button {
    width: 43px;
    height: 40px;
  }
  .chat {
    left: 120px;
    right: 200px;
    bottom: 8px;
    height: calc(100% - 18px);
  }
  .game-footer {
    min-height: 27px;
  }
  .game-footer button {
    min-height: 24px;
  }
}

@media (max-width: 950px), (max-width: 1100px) and (max-height: 600px) {
  body.embedded #help-button {
    display: block;
  }
}

/* Keep touch controls present in landscape widths above the old breakpoint. */
@media (max-width: 950px), (max-width: 1100px) and (max-height: 600px) {
  .joystick {
    display: block;
  }
  .touch-actions {
    display: flex;
  }
  .native-menu button {
    height: 40px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 3px;
  }
  .native-menu button span {
    height: auto;
    width: auto;
    border: 0;
    background: none;
    line-height: 1;
    padding: 0;
  }
  .hud-left #pickup {
    font-size: 0;
  }
  .hud-left #pickup::after {
    content: "줍기";
    font-size: 11px;
  }
}
@media (max-width: 950px) and (orientation: landscape),
  (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  .hud-left {
    flex: 2;
    width: auto;
    min-width: 0;
  }
  .native-menu {
    flex: 3;
    width: auto;
    min-width: 0;
  }
  .hud-left button {
    height: 37px;
    overflow: hidden;
  }
  .native-menu button {
    height: 37px;
  }
}

/* Landscape is the primary phone play layout: two 44px control rows, with the
   world filling the remaining area. Moving the existing controls retains the
   common fullscreen and durable-leave event handlers. */
@media (max-width: 950px) and (orientation: landscape),
  (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  body.portal-runtime .page-header,
  body.portal-runtime .game-toolbar {
    display: none;
    flex-basis: 0;
  }
  body.portal-runtime main {
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  .hud-shell {
    min-height: 48px;
    padding: 2px 4px;
  }
  .hud-left,
  .native-menu {
    flex: 1;
  }
  .hud-left button,
  .native-menu button,
  #mobile-chat-toggle {
    min-width: 44px;
    min-height: 44px;
    height: 44px;
    padding: 2px;
  }
  .native-menu {
    height: 44px;
  }
  .native-menu button span {
    display: none;
  }
  .hud-left #combat,
  .hud-left #rest,
  .hud-left #greet {
    font-size: 0;
  }
  .hud-left #combat::after,
  .hud-left #rest::after,
  .hud-left #greet::after {
    content: attr(data-mobile-label);
    font-size: 11px;
  }
  .game-footer {
    min-height: 48px;
    padding: 2px 4px max(2px, env(safe-area-inset-bottom, 0px));
    overflow: visible;
    justify-content: flex-end;
  }
  .game-footer button,
  .game-footer .mobile-save-return {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    height: 44px;
    margin: 0;
    padding: 3px 8px;
    white-space: nowrap;
    font-size: 11px;
  }
  .game-footer .mobile-save-return {
    border: 1px solid #887247;
    border-radius: 3px;
    background: #272b1d;
  }
  #online-count {
    display: none;
  }
  .touch-actions #touch-attack {
    width: 58px;
    height: 58px;
    min-width: 58px;
  }
  .touch-actions #touch-interact {
    width: 58px;
    height: 50px;
    min-width: 58px;
  }
  .touch-actions #touch-pickup {
    width: 50px;
    height: 50px;
    min-width: 50px;
  }
  .touch-actions {
    gap: 6px;
    bottom: 12px;
  }
  .quick-skills {
    gap: 2px;
    padding: 3px;
  }
  .quick-skills button {
    width: 44px;
    height: 44px;
    min-width: 44px;
  }
  .drawer #close-drawer {
    min-width: 44px;
    min-height: 44px;
  }
  .drawer {
    z-index: 9;
  }
}
@media (max-width: 600px) and (orientation: landscape) {
  .quick-skills {
    bottom: 82px;
  }
  .chat {
    left: 112px;
    right: 12px;
    bottom: 82px;
    height: calc(100% - 96px);
  }
}
@media (max-width: 950px), (max-width: 1100px) and (max-height: 600px) {
  .vitals-name > span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .vitals-name button,
  .hud-left button,
  .native-menu button,
  #mobile-chat-toggle,
  .game-footer button,
  .drawer button,
  .drawer #close-drawer,
  .game-toolbar button,
  .compact-label select {
    min-height: 44px;
  }
  .hud-left button,
  .native-menu button,
  #mobile-chat-toggle {
    height: 44px;
  }
  .native-menu {
    height: 46px;
  }
  .vitals-name button,
  .drawer #close-drawer {
    min-width: 44px;
  }
  .game-footer button,
  .game-toolbar button,
  .mobile-header-help,
  .dialog-close {
    min-width: 44px;
    min-height: 44px;
  }
  .setting-row {
    min-height: 44px;
    display: flex;
    align-items: center;
  }
}
@media (max-width: 950px) and (orientation: portrait) {
  .chat {
    top: clamp(8px, calc(100% - 400px), 152px);
    bottom: 180px;
    height: auto;
    min-height: 54px;
  }
  .chat:focus-within {
    top: 8px;
    bottom: 8px;
  }
}
@media (max-width: 950px) and (orientation: landscape),
  (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  .vitals-name {
    margin-bottom: 3px;
  }
  .vitals-foot {
    margin-top: 3px;
  }
  .native-menu {
    height: 44px;
  }
}
