:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #05080d;
  color: #f3f8ff;
  --game-aspect: 1.479768786;
  --rsc-mobile-top-clearance: 28px;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  scroll-behavior: auto;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  overflow-x: hidden;
  background:
    radial-gradient(ellipse 72% 46% at 50% -12%, rgba(39, 83, 110, .32), transparent 72%),
    linear-gradient(180deg, #101a23 0, #080d14 42%, #05080d 100%);
}

.app-shell {
  /* Let the classic fixed-resolution surface consume the useful desktop
     viewport while retaining its exact aspect ratio.  The previous 1100px
     shell/1024px canvas caps left the game postcard-sized on 1440p and 4K
     displays. */
  width: min(100%, calc((100dvh - 88px) * var(--game-aspect) + 32px));
  max-width: 1568px;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.desktop-masthead {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 32px;
  padding: 0 3px 12px;
}

.brand {
  color: #c9efff;
  font-size: .91rem;
  font-weight: 760;
  letter-spacing: .075em;
  text-transform: uppercase;
}

.brand-divider { width: 1px; height: 15px; background: #527083; }
.section-label { color: #b5c4d0; font-size: .82rem; }
.release-label { margin-left: auto; color: #7e93a4; font-size: .75rem; font-variant-numeric: tabular-nums; }

.game-stage {
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  min-height: 220px;
  border: 1px solid rgba(122, 184, 214, .35);
  border-radius: 14px;
  background: #000;
  box-shadow: 0 24px 72px rgba(0, 0, 0, .42), 0 0 0 1px rgba(8, 14, 19, .6) inset;
  isolation: isolate;
}

/* The game is a direct-manipulation surface.  Do not let iOS turn a hold,
   drag, or image press into a browser selection/copy callout.  The hidden
   keyboard bridge below deliberately opts back in so typing and composition
   remain native. */
.app-shell,
.app-shell * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#game-canvas {
  display: block;
  width: min(100%, 1536px);
  height: auto;
  max-height: calc(100dvh - 88px);
  aspect-ratio: 512 / 346;
  image-rendering: auto;
  touch-action: none;
  background: #000;
}

/* Keep the launcher selection visible, then remove the browser focus ring as
   soon as the canvas becomes the active gameplay surface. */
#game-canvas:focus {
  outline: 2px solid rgba(255, 255, 255, .95);
  outline-offset: -2px;
}

.app-shell[data-gameplay="1"] #game-canvas:focus {
  outline: none;
}

.keyboard-proxy {
  position: fixed;
  left: env(safe-area-inset-left, 0);
  top: env(safe-area-inset-top, 0);
  /* Safari will not reliably summon its keyboard for a focused element that
     sits behind the page. Keep the invisible proxy in the active layer, while
     pointer-events keeps it from intercepting any game touch. */
  z-index: 1;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  resize: none;
  opacity: .01;
  border: 0;
  color: transparent;
  background: transparent;
  caret-color: transparent;
  clip-path: inset(50%);
  font-size: 16px;
  pointer-events: none;
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

.chat-history-controls {
  position: fixed;
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border: 1px solid rgba(255, 196, 0, .5);
  border-radius: 12px;
  background: rgba(0, 0, 0, .72);
  box-shadow: 0 6px 18px rgba(0, 0, 0, .45);
  -webkit-tap-highlight-color: transparent;
}

.chat-history-controls[hidden] { display: none !important; }

.chat-history-control {
  display: grid;
  place-items: center;
  width: clamp(48px, 12vw, 64px);
  height: clamp(44px, 10vw, 56px);
  padding: 0;
  border: 1px solid rgba(255, 216, 77, .5);
  border-radius: 9px;
  color: #ffe57b;
  background: rgba(13, 25, 31, .92);
  font: 800 clamp(26px, 6vw, 34px)/1 system-ui, -apple-system, sans-serif;
  touch-action: manipulation;
}

.chat-history-control:active { transform: scale(.94); background: rgba(76, 62, 13, .92); }
.chat-history-control:focus-visible { outline: 2px solid #ffe57b; outline-offset: 2px; }

.status-card {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 3;
  display: grid;
  width: min(288px, calc(100% - 28px));
  gap: 6px;
  padding: 14px 15px 13px;
  border: 1px solid rgba(173, 219, 240, .31);
  border-radius: 12px;
  background: linear-gradient(145deg, rgba(19, 34, 45, .94), rgba(4, 9, 14, .92));
  box-shadow: 0 12px 30px rgba(0, 0, 0, .46);
  backdrop-filter: blur(12px);
  transition: opacity 220ms ease, visibility 220ms ease, transform 220ms ease;
  pointer-events: none;
}

.status-kicker { color: #76d4f4; font-size: .62rem; font-weight: 750; letter-spacing: .13em; }
.status-title { color: #f0f8ff; font-size: .95rem; line-height: 1.15; }
.status-message { color: #c7d7e2; font-size: .77rem; line-height: 1.35; }
.status-detail { color: #839cab; font-size: .69rem; line-height: 1.35; }

.progress-track {
  display: block;
  height: 4px;
  margin: 3px 0 1px;
  overflow: hidden;
  border-radius: 99px;
  background: rgba(171, 211, 229, .16);
}

.progress-bar {
  display: block;
  width: 8%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #51c7ed, #a9edff);
  box-shadow: 0 0 12px rgba(111, 220, 249, .8);
  transition: width 260ms ease;
}

.game-controls {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 4;
  display: none;
  flex-direction: column;
  gap: 8px;
}

/* Touchscreen laptops can expose a coarse pointer alongside a real trackpad
   and hardware keyboard.  JavaScript classifies those as desktop; never let a
   broad responsive rule bring the mobile keyboard button back. */
body[data-mode="desktop"] #keyboard-button { display: none !important; }

.game-control,
.update-now {
  border: 1px solid rgba(164, 222, 243, .54);
  color: #d8f6ff;
  background: rgba(5, 14, 20, .72);
  box-shadow: 0 7px 18px rgba(0, 0, 0, .32);
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

.game-control {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  padding: 0;
  border-radius: 13px;
  touch-action: manipulation;
}

.game-control svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.game-control:active { transform: scale(.94); background: rgba(36, 81, 99, .86); }
.game-control:focus-visible, .update-now:focus-visible { outline: 2px solid #a5e9ff; outline-offset: 2px; }

.orientation-note {
  display: none;
  position: absolute;
  inset: auto 18px max(18px, env(safe-area-inset-bottom));
  z-index: 2;
  margin: 0;
  padding: 9px 11px;
  border-radius: 9px;
  color: #b9cddd;
  background: rgba(3, 8, 12, .76);
  font-size: .72rem;
  line-height: 1.3;
  text-align: center;
  pointer-events: none;
}

.app-shell[data-state="ready"] .status-card,
.app-shell[data-state="ready"] .orientation-note {
  visibility: hidden;
  opacity: 0;
  transform: translateY(-8px);
}

.app-shell[data-state="error"] .status-card { border-color: rgba(255, 161, 143, .62); }
.app-shell[data-state="error"] .progress-bar { background: #ff927d; box-shadow: none; }

.update-banner {
  /* Browser releases are applied on the next launch. Never obstruct the
     canvas with the retired manual-update banner. */
  display: none !important;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 13px auto 0;
  padding: 10px 12px;
  border: 1px solid rgba(108, 199, 234, .5);
  border-radius: 10px;
  background: rgba(15, 40, 52, .92);
  color: #dcf5ff;
  font-size: .82rem;
  box-shadow: 0 10px 32px rgba(0, 0, 0, .22);
}

.update-now { border-radius: 7px; padding: 7px 10px; background: #8be0fb; color: #071218; font-weight: 700; }
.desktop-hint { margin: 12px 0 0; color: #718897; font-size: .76rem; text-align: center; }

@media (max-width: 800px) {
  html:has(body[data-mode="mobile"]),
  body[data-mode="mobile"] { height: 100%; min-height: 100%; overflow: hidden; }

  body[data-mode="mobile"] {
    position: fixed;
    inset: 0;
    display: block;
    min-height: 0;
    background: #000;
  }

  body[data-mode="mobile"] .app-shell {
    position: fixed;
    inset: 0;
    width: 100%;
    min-height: 0;
    padding: 0;
  }

  body[data-mode="mobile"] .desktop-masthead,
  body[data-mode="mobile"] .desktop-hint { display: none; }

  body[data-mode="mobile"] .game-stage {
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right)
      env(safe-area-inset-bottom) env(safe-area-inset-left);
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  body[data-mode="mobile"] #game-canvas {
    width: min(calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc((100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--game-aspect)));
    max-width: calc(100vw - env(safe-area-inset-left) - env(safe-area-inset-right));
    max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }

  @supports (height: 100dvh) {
    body[data-mode="mobile"] #game-canvas {
      width: min(calc(100dvw - env(safe-area-inset-left) - env(safe-area-inset-right)),
        calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--game-aspect)));
      max-width: calc(100dvw - env(safe-area-inset-left) - env(safe-area-inset-right));
      max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    }
  }

  body[data-mode="mobile"] .game-controls { display: flex; }

  body[data-mode="mobile"] .status-card {
    top: max(9px, env(safe-area-inset-top));
    right: max(9px, env(safe-area-inset-right));
    width: min(286px, calc(100% - 18px));
    padding: 11px 12px;
  }

  body[data-mode="mobile"] .update-banner {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    left: max(8px, env(safe-area-inset-left));
    z-index: 10;
    margin: 0;
  }

  @media (orientation: portrait) {
    body[data-mode="mobile"] .app-shell[data-state="ready"] .orientation-note {
      display: block;
      visibility: visible;
      opacity: 1;
      transform: none;
      animation: orientation-note-dismiss 6s ease forwards;
    }
  }
}

/* JavaScript applies this on real phones/tablets too. It covers mobile
   browsers configured to request desktop pages, which otherwise report a fine
   pointer and leave the game in the cramped desktop frame. */
body[data-mode="mobile"] {
  /* A Home Screen game needs a stable viewport while the phone rotates.
     Safari's dynamic viewport can animate through an intermediate landscape
     height, leaving a fixed canvas scaled larger than its hit-test surface.
     svh is the settled, always-visible viewport on modern iOS. */
  --rsc-mobile-viewport-width: 100vw;
  --rsc-mobile-viewport-height: 100vh;
  position: fixed;
  inset: 0;
  width: var(--rsc-mobile-viewport-width);
  height: var(--rsc-mobile-viewport-height);
  max-width: var(--rsc-mobile-viewport-width);
  max-height: var(--rsc-mobile-viewport-height);
  display: block;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: #000;
}

/* Credential entry uses a fixed, clipped keyboard proxy. Android is allowed
   to overlay its IME, but it must never resize or scroll the login canvas. */
body[data-mode="mobile"][data-login-keyboard="1"] {
  overflow: hidden;
  overscroll-behavior: none;
}

body[data-mode="mobile"][data-login-keyboard="1"] .keyboard-proxy {
  scroll-margin: 0;
}

body[data-mode="mobile"] .app-shell {
  position: fixed;
  inset: 0;
  width: var(--rsc-mobile-viewport-width);
  height: var(--rsc-mobile-viewport-height);
  min-height: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

body[data-mode="mobile"] .desktop-masthead,
body[data-mode="mobile"] .desktop-hint { display: none; }

body[data-mode="mobile"] .game-stage {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
  border: 0;
  border-radius: 0;
  box-shadow: none;
  overscroll-behavior: none;
  touch-action: none;
}

/* iOS can keep the document taller than the visual viewport during a browser
   chrome transition unless every fixed ancestor explicitly clips it.  This is
   deliberately mobile-only: desktop page scrolling is unchanged. */
html:has(body[data-mode="mobile"]) {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #000;
}

@supports (-webkit-touch-callout: none) {
  html:has(body[data-mode="mobile"]),
  body[data-mode="mobile"],
  body[data-mode="mobile"] .app-shell {
    min-height: -webkit-fill-available;
  }

  /* iPhone Home Screen games need a genuine strip below the status/battery
     area so the system pull-down gesture and the game's top tabs never
     compete.  The canvas itself begins below this strip, preserving exact
     canvas-relative touch coordinates. */
  body[data-mode="mobile"] {
    --rsc-mobile-top-clearance: 52px;
  }
}

@supports (height: 100svh) {
  body[data-mode="mobile"] {
    --rsc-mobile-viewport-height: 100svh;
  }
}

/* In landscape, Android browsers can report the keyboard-reduced small
   viewport even when interactive-widget is overlaying the IME. Keep the
   fullscreen/game canvas at the large pre-keyboard height and let only the
   chat line/history strip respond to visualViewport. */
@media (orientation: landscape) {
  @supports (height: 100lvh) {
    body[data-mode="mobile"][data-keyboard="1"] {
      --rsc-mobile-viewport-height: 100lvh;
    }
  }
}

body[data-mode="mobile"] #game-canvas {
  width: min(calc(var(--rsc-mobile-viewport-width) - env(safe-area-inset-left) - env(safe-area-inset-right)),
    calc((var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--game-aspect)));
  max-width: calc(var(--rsc-mobile-viewport-width) - env(safe-area-inset-left) - env(safe-area-inset-right));
  max-height: calc(var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}

/* The square control is an explicit player choice: fill the complete safe
   viewport. bootstrap.js maps pointer X and Y independently to the fixed
   game surface, so menus and gameplay stay precise while it fills the view. */
body[data-mode="mobile"][data-fullscreen="1"] #game-canvas {
  width: calc(var(--rsc-mobile-viewport-width) - env(safe-area-inset-left) - env(safe-area-inset-right));
  height: calc(var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  max-width: none;
  max-height: none;
  aspect-ratio: auto;
}

/* Safari's landscape browser chrome can sit over the first few physical
   pixels of an edge-to-edge canvas even after the safe-area inset is applied.
   Leave a real top hit-test gutter above the game rather than translating
   it: the canvas rect and its offset-based pointer mapping stay identical.
   This keeps the top game tabs (settings, friends, map and inventory) fully
   touchable without sacrificing the bottom of the playable surface. */
@media (orientation: landscape) {
  body[data-mode="mobile"] .game-stage {
    padding-top: calc(env(safe-area-inset-top) + var(--rsc-mobile-top-clearance));
  }

  body[data-mode="mobile"] #game-canvas {
    width: min(calc(var(--rsc-mobile-viewport-width) - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc((var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - var(--rsc-mobile-top-clearance)) * var(--game-aspect)));
    max-height: calc(var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - var(--rsc-mobile-top-clearance));
  }

  /* Preserve the real top hit-test gutter while the square control fills the
     rest of the landscape viewport. */
  body[data-mode="mobile"][data-fullscreen="1"] #game-canvas {
    width: calc(var(--rsc-mobile-viewport-width) - env(safe-area-inset-left) - env(safe-area-inset-right));
    height: calc(var(--rsc-mobile-viewport-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - var(--rsc-mobile-top-clearance));
    max-width: none;
    max-height: none;
    aspect-ratio: auto;
  }

}

body[data-mode="mobile"] .game-controls {
  --rsc-mobile-control-size: clamp(52px, 13vmin, 72px);
  --rsc-mobile-control-gap: clamp(10px, 2.5vmin, 16px);
  right: calc(env(safe-area-inset-right, 0) + 12px);
  bottom: calc(env(safe-area-inset-bottom, 0) + 20px);
  display: grid;
  grid-template-columns: repeat(2, var(--rsc-mobile-control-size));
  gap: var(--rsc-mobile-control-gap);
  pointer-events: none;
}

/* Chat-history arrows sit above the visible mobile keyboard. Move the game
   controls to the opposite side while typing so they can never overlap the
   arrows or the keyboard's upper edge. */
body[data-mode="mobile"][data-keyboard="1"] .game-controls {
  right: auto;
  left: calc(env(safe-area-inset-left, 0) + 12px);
}

body[data-mode="desktop"] .chat-history-controls { display: none !important; }

body[data-mode="mobile"] .game-control {
  width: var(--rsc-mobile-control-size);
  height: var(--rsc-mobile-control-size);
  border-color: rgba(255, 216, 77, .78);
  border-radius: clamp(12px, 3vmin, 18px);
  color: #ffd84d;
  background: rgba(0, 0, 0, .72);
  font-weight: 700;
  pointer-events: auto;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .46), 0 0 0 1px rgba(255, 216, 77, .12);
}

body[data-mode="mobile"] .game-control svg {
  width: clamp(29px, 7.2vmin, 40px);
  height: clamp(29px, 7.2vmin, 40px);
}

body[data-mode="mobile"] .status-card {
  top: max(9px, env(safe-area-inset-top));
  right: max(9px, env(safe-area-inset-right));
  width: min(286px, calc(100% - 18px));
  padding: 11px 12px;
}

body[data-mode="mobile"] .update-banner {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  left: max(8px, env(safe-area-inset-left));
  z-index: 10;
  margin: 0;
}

@media (orientation: portrait) {
  body[data-mode="mobile"] .app-shell[data-state="ready"] .orientation-note {
    display: block;
    visibility: visible;
    opacity: 1;
    transform: none;
    animation: orientation-note-dismiss 6s ease forwards;
  }
}

@keyframes orientation-note-dismiss {
  0%, 70% { opacity: 1; visibility: visible; }
  100% { opacity: 0; visibility: hidden; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
