html {
  background: #312F28;
  /* Safe-area insets: the single source of truth all chrome reads via
     var(--sai-*). Default to env() (correct in landscape and standalone);
     portrait iOS in a Safari tab reports env()=0, so ios-portrait.js
     overrides --sai-t/--sai-b with measured values once the toolbar collapses.
     Also read from JS via getComputedStyle (boot.js sizes the viewport). */
  --sai-t: env(safe-area-inset-top);
  --sai-r: env(safe-area-inset-right);
  --sai-b: env(safe-area-inset-bottom);
  --sai-l: env(safe-area-inset-left);
}

/* controller in portrait: the game occupies the top half of the screen */
html.pad-portrait body {
  height: 50dvh;
  min-height: 50dvh;
}

body {
  margin: 0;
  background:#312F28;
  min-height: 100vh;
  min-height: 100dvh;
  /* Touch is game input: no scrolling, zooming, text selection, or callouts. */
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}


/* The GAME canvas only — full-bleed since adaptive resolution (2026-07-19):
   the canvas fills the true viewport (including under notches/toolbars; UI
   insets itself instead). It stretches to #screen rather than sitting at an
   integer px size centered with letterbox bars — that centering left mobile
   browsers (Safari's dynamic toolbars vs 100dvh) showing the body behind the
   canvas as black bands. The internal resolution stays adaptive/integer; the
   sub-device-pixel stretch to the edge is imperceptible. Dialogs draw their
   own small canvases that must not inherit these rules. */
#screen canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  -webkit-filter:brightness(0.85) sepia(20%) ;
  image-rendering: pixelated !important;
}

/* fixed to the true screen edges so no layout/toolbar mismatch can inset it */
#screen {
  position: fixed;
  inset: 0;
}

/* Portrait iOS (ios-portrait.js): render at the true device height and let the
   page scroll so the toolbar can collapse; the canvas then fills the whole
   screen (under the status bar) instead of the reduced viewport. Higher
   specificity than the fixed default above, so it wins only when active. */
html.ios-portrait body {
  height: var(--screen-h); /* taller than the viewport -> scrollable to collapse */
}
html.ios-portrait #screen {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: var(--screen-h);
}
/* pre-collapse: the swipe must reach the page, so let it scroll and take the
   canvas out of hit-testing (the game's touch layer also stands down on
   html.ios-swipe -- see touch.js) */
html.ios-swipe body {
  touch-action: pan-y;
}
html.ios-swipe #screen canvas {
  pointer-events: none;
}
/* the page is scroll-locked, and iOS anchors position:fixed to the document
   (so it rides up with the scroll) — sticky keeps the chrome on screen. The
   bar already pads its content down by var(--sai-t). */
html.ios-portrait #topbar {
  position: sticky;
  top: 0;
}
/* once locked, sticky/fixed chrome rides down by the locked scroll (--scroll-lock);
   translate it back up so it pins to the true top of the screen */
html.ios-portrait-locked #topbar,
html.ios-portrait-locked .dropdown {
  transform: translateY(calc(-1 * var(--scroll-lock, 0px)));
}

/* Immersive mode (hide-tabs.js): while the mobile browser chrome is expanded,
   the page becomes scrollable via the spacer so a swipe collapses the UI. */
#swipe-spacer,
#swipe-hint {
  display: none;
}

html.tabs-open body {
  touch-action: pan-y; /* the collapse gesture needs a real scroll */
}

/* While the browser chrome is expanded, the swipe must reach the page:
   Phaser's canvas listeners preventDefault touches, so take the canvas out
   of hit-testing entirely until hide-tabs re-engages the scroll lock. */
html.tabs-open #screen canvas {
  pointer-events: none;
}

html.tabs-open #screen {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: grid;
  place-items: center;
}

html.tabs-open #swipe-spacer {
  display: block;
  height: 2000vh;
}

html.tabs-open #swipe-hint,
html.ios-swipe #swipe-hint {
  display: block;
  position: fixed;
  z-index: 2;
  left: 50%;
  transform: translateX(-50%);
  top: calc(var(--sai-t) + 10px);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: rgba(255, 255, 255, 0.9);
  font: 14px -apple-system, system-ui, sans-serif;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  pointer-events: none;
}

/* Top chrome: one borderless bar — wordmark left, edit tools and icon
   buttons right. Icons and text only, no button surfaces. */
#topbar {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: calc(var(--sai-t) + 10px) max(14px, var(--sai-r)) 10px max(14px, var(--sai-l));
  color: #fff;
  font: 13px -apple-system, system-ui, sans-serif;
  pointer-events: none; /* the bar is a layout strip; only its children are interactive */
}
#topbar > * {
  pointer-events: auto;
}
#topbar .spacer {
  flex: 1;
  pointer-events: none;
}

#brand {
  display: flex;
  align-items: center;
  text-decoration: none;
}
#logo {
  display: block;
  width: 28px;
  height: 28px;
}
/* the wordmark shows the current world name (defaults to "subpixel") */
#world-name {
  color: #fff;
  font-family: "Jersey 10", monospace;
  font-weight: 400;
  font-size: 24px;
  line-height: 1;
  letter-spacing: 0.02em;
}

#topbar button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  background: none;
  border: none;
  color: #fff;
  font: inherit;
  cursor: pointer;
  opacity: 0.85;
}
#topbar button:hover {
  opacity: 1;
}
#topbar button:disabled,
#topbar button:disabled:hover {
  opacity: 0.3;
  cursor: default;
}
#places-trigger .material-symbols-outlined,
#account-trigger .material-symbols-outlined { font-size: 22px; }
#auth-close .material-symbols-outlined { font-size: 20px; }
#swipe-hint .material-symbols-outlined { font-size: 18px; vertical-align: -4px; }
/* the ellipsis doubles as the edit-mode indicator once editing */
#account-trigger.active {
  color: #b9a1ff;
  opacity: 1;
}

/* the single dropdown menu needs a surface to read over the game */
.dropdown {
  position: fixed;
  z-index: 40;
  top: calc(var(--sai-t) + 42px);
  left: max(14px, var(--sai-l));
  display: grid;
  min-width: 180px;
  padding: 6px;
  border-radius: 10px;
  background: rgba(10, 10, 14, 0.92);
  color: #fff;
  font: 13px -apple-system, system-ui, sans-serif;
}
.dropdown[hidden] {
  display: none;
}
/* the account menu hangs from the ellipsis on the right edge */
.dropdown-right {
  left: auto;
  right: max(14px, var(--sai-r));
}
/* recents live in a wrapper; unbox it so its links are dropdown items too */
#places-recents {
  display: contents;
}
#places-recents a {
  padding: 9px 12px;
  border-radius: 6px;
  color: #fff;
  text-decoration: none;
}
#places-recents a:hover {
  background: rgba(255, 255, 255, 0.08);
}
.dropdown > * {
  padding: 9px 12px;
  border: none;
  border-radius: 6px;
  background: none;
  color: #fff;
  font: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.dropdown > *:hover {
  background: rgba(255, 255, 255, 0.08);
}
/* non-interactive info rows (username, draft status) */
.dropdown-note {
  color: rgba(255, 255, 255, 0.5);
  cursor: default;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}
.dropdown-note:hover {
  background: none !important;
}
#edit-publish {
  color: #b9a1ff;
}

/* the say composer + its coarse-pointer send button */
#say-bar {
  position: fixed;
  left: 50%;
  bottom: calc(var(--sai-b) + 48px);
  transform: translateX(-50%);
  z-index: 30;
  display: none;
  gap: 6px;
  width: min(70vw, 420px);
}
#say-bar.open {
  display: flex;
}
#say-composer {
  flex: 1;
  min-width: 0;
  padding: 8px 12px;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
  font: 14px monospace;
}
#say-send {
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
  cursor: pointer;
}
@media (pointer: coarse) {
  #say-send { display: flex; }
}

#auth-dialog,
#create-dialog {
  position: fixed;
  z-index: 40;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.35);
}

#auth-dialog[hidden],
#create-dialog[hidden] {
  display: none;
}

#auth-panel,
#create-panel {
  position: relative;
  display: grid;
  gap: 12px;
  min-width: 260px;
  padding: 22px;
  background: rgba(0, 0, 0, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 8px;
  color: #fff;
  font: 14px -apple-system, system-ui, sans-serif;
}

#auth-close,
#create-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font-size: 18px;
  line-height: 1;
}

#create-name,
#create-template {
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
  font: 14px -apple-system, system-ui, sans-serif;
}

#create-template option {
  background: #17181d;
}

#create-status {
  font-size: 12px;
  color: #ff9c9c;
}

#create-go {
  height: 36px;
  border-radius: 8px;
  border: none;
  background: #6024F6;
  color: #fff;
  font-weight: 600;
}
#create-go:disabled {
  opacity: 0.5;
}

#edit-toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--sai-b) + 70px);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  font: 14px -apple-system, system-ui, sans-serif;
  pointer-events: none;
}

/* ---- themed dialogs: settings + the standard app dialog ---- */
#settings-dialog,
#ui-dialog {
  --dlg-bg: #ffffff;
  --dlg-fg: #1c1c22;
  --dlg-muted: #55555f;
  --dlg-border: #e4e4ea;
  --dlg-input: #f4f4f8;
  --dlg-accent: #6024f6;
  --dlg-accent-fg: #ffffff;
  --dlg-ok: #1e7d32;
  --dlg-error: #c93030;
  position: fixed;
  z-index: 40;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.4);
}
@media (prefers-color-scheme: dark) {
  #settings-dialog,
  #ui-dialog {
    --dlg-bg: #1e1e28;
    --dlg-fg: #e8e8ee;
    --dlg-muted: #9a9aa6;
    --dlg-border: #33333f;
    --dlg-input: #16161d;
    --dlg-accent: #7c4dff;
    --dlg-ok: #7ee08a;
    --dlg-error: #ff7a6e;
  }
}
#settings-dialog[hidden] {
  display: none;
}
/* Pico renders inside these containers with OUR tokens */
#settings-dialog.pico,
#ui-dialog.pico {
  --pico-background-color: var(--dlg-bg);
  --pico-color: var(--dlg-fg);
  --pico-muted-color: var(--dlg-muted);
  --pico-h1-color: var(--dlg-fg);
  --pico-h2-color: var(--dlg-fg);
  --pico-h3-color: var(--dlg-fg);
  --pico-border-color: var(--dlg-border);
  --pico-form-element-background-color: var(--dlg-input);
  --pico-form-element-border-color: var(--dlg-border);
  --pico-form-element-color: var(--dlg-fg);
  --pico-form-element-focus-color: var(--dlg-accent);
  --pico-primary: var(--dlg-accent);
  --pico-primary-background: var(--dlg-accent);
  --pico-primary-border: var(--dlg-accent);
  --pico-primary-hover-background: var(--dlg-accent);
  --pico-primary-hover-border: var(--dlg-accent);
  --pico-primary-inverse: var(--dlg-accent-fg, #fff);
  --pico-font-size: 100%;
  --pico-font-family: -apple-system, system-ui, sans-serif;
  --pico-spacing: 0.6rem;
  --pico-form-element-spacing-vertical: 0.4rem;
  --pico-form-element-spacing-horizontal: 0.65rem;
  font-size: 14px;
}
#settings-panel,
.ui-dialog-panel {
  background: var(--dlg-bg);
  color: var(--dlg-fg);
  border: 1px solid var(--dlg-border);
  border-radius: 12px;
  font: 14px/1.5 -apple-system, system-ui, sans-serif;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}
#settings-panel {
  width: min(28rem, calc(100vw - 2rem));
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px 0 10px;
  border-bottom: 1px solid var(--dlg-border);
}
#settings-tabs {
  display: flex;
  gap: 2px;
}
#settings-tabs button {
  appearance: none;
  border: none;
  background: none;
  color: var(--dlg-muted);
  font: inherit;
  font-weight: 600;
  padding: 10px 12px;
  margin-bottom: -1px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}
#settings-tabs button.active {
  color: var(--dlg-fg);
  border-bottom-color: var(--dlg-accent);
}
#settings-close {
  border: none;
  background: none;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--dlg-muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
#settings-close:hover {
  background: var(--dlg-input);
  color: var(--dlg-fg);
}
.settings-tab[hidden] {
  display: none;
}
.settings-tab {
  overflow-y: auto;
  padding: 16px 18px 18px;
  display: grid;
  gap: 18px;
  align-content: start;
}
.settings-tab section {
  display: grid;
  gap: 6px;
}
.settings-tab h3 {
  margin: 0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--dlg-muted);
}
.settings-tab p {
  margin: 0;
}
#settings-panel .settings-note {
  color: var(--dlg-muted);
  font-size: 12px;
}
#settings-panel .settings-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
#settings-panel .settings-split {
  justify-content: space-between;
}
/* Pico styles inputs/buttons; keep only layout-level tweaks */
#settings-panel input[type="text"],
#settings-panel input[type="number"],
.ui-dialog-panel input[type="text"] {
  flex: 1;
  min-width: 6rem;
  margin-bottom: 0;
}
#settings-panel input[type="number"] {
  flex: 0 0 5rem;
  min-width: 0;
}
#settings-panel .settings-row button,
.ui-dialog-actions button {
  width: auto;
  margin-bottom: 0;
}
#settings-panel .status {
  min-height: 1.1em;
  font-size: 12px;
  color: var(--dlg-muted);
}
#settings-panel .status.error { color: var(--dlg-error); }
#settings-panel .status.ok { color: var(--dlg-ok); }
#settings-panel .handle-display { font-weight: 700; font-size: 15px; margin: 2px 0; }

/* the standard app dialog */
.ui-dialog-panel {
  width: min(22rem, calc(100vw - 2rem));
  padding: 18px;
  display: grid;
  gap: 12px;
}
.ui-dialog-panel h2 {
  margin: 0;
  font-size: 15px;
}
.ui-dialog-panel p {
  margin: 0;
  color: var(--dlg-muted);
  font-size: 13px;
}
.ui-dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.ui-dialog-panel button.danger {
  background: var(--dlg-error);
  border-color: var(--dlg-error);
  color: #fff;
}

#avatar-grid { display: flex; flex-wrap: wrap; gap: 6px; }
#avatar-grid button {
  /* fixed slot: thumbnails stream in without any reflow */
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 76px;
  padding: 5px;
  border-radius: 8px;
  border: 1px solid var(--dlg-border);
  background: var(--dlg-input);
  color: var(--dlg-fg);
}
#avatar-grid canvas {
  width: 40px;
  height: 64px;
  object-fit: contain; /* 16x32 frames land at a crisp integer 2x */
}
#avatar-grid canvas, .sprite-preview { image-rendering: pixelated; }
.sprite-preview {
  width: 64px;
  height: 64px;
  border-radius: 6px;
  background: repeating-conic-gradient(rgba(128, 128, 128, 0.25) 0% 25%, transparent 0% 50%) 0 0 / 16px 16px;
}
