/* Shared mobile viewport contract. Discord's embedded webview may report zero
   env() insets even when native chrome covers the top of the screen. Reserve
   a conservative floor, never add it on top of a larger native inset. */
:root {
  --safe-top: 0px;
  --safe-right: 0px;
  --safe-bottom: 0px;
  --safe-left: 0px;
  --safe-height: calc(100dvh - var(--safe-top) - var(--safe-bottom));
  --safe-width: calc(100vw - var(--safe-left) - var(--safe-right));
}
@media (max-width: 760px), (pointer: coarse) and (max-width: 1100px), (pointer: coarse) and (max-height: 520px) {
  :root {
    --safe-top: max(60px, env(safe-area-inset-top, 0px));
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: max(26px, env(safe-area-inset-bottom, 0px));
    --safe-left: env(safe-area-inset-left, 0px);
  }
  /* Reserve the safe area once; scroll the content inside it, not under
     native controls. The game renderer measures this smaller rectangle. */
  body:has(> #stage), body:has(> #admin-app) { overflow: hidden; }
  #stage, #admin-app {
    position: fixed;
    inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    overflow: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
  }
  #stage:not(.phone) .shell { min-height: var(--safe-height); --mobile-budget: var(--safe-height); padding-bottom: 12px; }
  #stage:not(.phone) .hub { min-height: var(--safe-height); }
  #stage:not(.phone) .live-table { height: var(--safe-height); min-height: 0; }
  #stage.phone .app { max-height: max(0px, min(844px, calc(var(--safe-height) - 24px))); }
  #stage.phone .live-table { height: max(0px, min(844px, calc(var(--safe-height) - 24px))); }
  #stage.phone { min-height: 0; }
  #stage:has(.playing-live):not(.phone) { overflow: hidden; }
  /* All dialogs are siblings of the stage (or fixed nested pickers), so
     they need their own safe rectangle, including their scroll containers. */
  .overlay, .overlay:has(.pf-full), .overlay:has(.shop-full) {
    inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    padding: 6px;
    min-height: 0;
  }
  .overlay:has(.shop-full) { top: max(var(--safe-top), var(--shop-top-offset, 0px)); }
  .overlay > .dialog, .overlay > .dialog.pf-full, .overlay > .dialog.shop-full {
    max-height: 100%; min-height: 0; max-width: 100%; overflow: auto;
  }
  .overlay > .dialog.pf-full { height: min(88dvh, 100%); }
  .overlay > .dialog.shop-full { height: 100%; }
  .shop-detail-shade, .pf-picker-shade {
    inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    padding: 8px;
  }
  .shop-detail, .pf-picker { max-height: 100%; min-height: 0; overflow: auto; }
  .access-stage, .activity-startup, .platform-message {
    inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    overflow: auto;
    place-items: safe center;
  }
  .platform-message section { max-height: 100%; overflow: auto; }
  .activity-startup { padding: 12px; }
  #toast, .selectstatus { bottom: calc(var(--safe-bottom) + 12px); max-width: calc(var(--safe-width) - 24px); }
  .progress-notice { bottom: calc(var(--safe-bottom) + 18px); max-width: calc(var(--safe-width) - 28px); }
  #dev-toggle { left: calc(var(--safe-left) + 4px); top: calc(var(--safe-top) + var(--safe-height) / 2); }
  #dev-panel { top: calc(var(--safe-top) + 6px); bottom: auto; left: calc(var(--safe-left) + 6px); transform: none; width: min(440px, calc(var(--safe-width) - 12px)); max-height: calc(var(--safe-height) - 12px); }
  .menu { max-width: calc(var(--safe-width) - 16px); max-height: calc(var(--safe-height) - 16px); overflow: auto; }
  /* The admin iframe remains full size; its own document applies the shared
     insets. Do not inset the frame as well (would double-pad it). */
  #admin-app .shell { min-height: var(--safe-height); }
  #admin-app .rail { height: var(--safe-height); }
  #admin-toast { bottom: calc(var(--safe-bottom) + 12px); left: calc(var(--safe-left) + 12px); max-width: calc(var(--safe-width) - 24px); }
  #admin-dialog {
    position: fixed;
    inset: calc(var(--safe-top) + 6px) calc(var(--safe-right) + 6px) calc(var(--safe-bottom) + 6px) calc(var(--safe-left) + 6px);
    margin: auto;
    box-sizing: border-box;
    max-height: calc(var(--safe-height) - 12px);
    max-width: calc(var(--safe-width) - 12px);
  }
  html:has(body > main > article) body {
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  }
}
@media (max-width: 700px) {
  #admin-app .rail {
    inset: var(--safe-top) var(--safe-right) var(--safe-bottom) auto;
    max-width: calc(var(--safe-width) - 12px);
    visibility: hidden;
  }
  #admin-app .rail.open { visibility: visible; }
}
@media (orientation: landscape) and (max-height: 520px) and (pointer: coarse), (orientation: landscape) and (max-width: 760px) {
  :root {
    --safe-top: max(18px, env(safe-area-inset-top, 0px));
    --safe-right: max(44px, env(safe-area-inset-right, 0px));
    --safe-bottom: max(20px, env(safe-area-inset-bottom, 0px));
    --safe-left: max(44px, env(safe-area-inset-left, 0px));
  }
}
