:root {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 15px;
  color: #e0e0e0;
  background-color: #121212;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* no scrolling anywhere */
}

body {
  display: flex;
  flex-direction: column;
  background: #0d0d0d;
}

/* Banner shown only when the in-browser WASM engine fails to load. */
.hosted-note {
  flex: 0 0 auto;
  margin: 8px;
  padding: 10px 14px;
  border: 1px solid #8a6d3b;
  border-radius: 6px;
  background: #2d2417;
  color: #e8c87a;
  font-size: 13px;
  line-height: 1.5;
}
.hosted-note code {
  background: #1a1a1a;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
}
.hosted-note a { color: #7ec8ff; }

/* ---- Top menu bar ---- */
.menubar {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
  height: 36px;
  padding: 0 8px;
  background: #1c1c1f;
  border-bottom: 1px solid #333;
  user-select: none;
  -webkit-user-select: none;
}

.brand {
  font-weight: 700;
  margin-right: 10px;
  color: #24c8db;
  letter-spacing: 0.03em;
}

/* Small label marking a menu as pre-release (e.g. Online beta). */
.beta-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  background: #4a3b10;
  color: #ffcf4d;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  vertical-align: 2px;
}

.menu { position: relative; }

.menu > summary {
  list-style: none;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 5px;
  color: #ddd;
  white-space: nowrap;
}
.menu > summary::-webkit-details-marker { display: none; }
.menu > summary:hover,
.menu[open] > summary { background: #2c2c31; }

.menu-items {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  min-width: 220px;
  background: #242429;
  border: 1px solid #3a3a40;
  border-radius: 6px;
  padding: 4px;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
}

.menu-items button {
  text-align: left;
  background: none;
  border: none;
  color: #e0e0e0;
  padding: 7px 10px;
  border-radius: 4px;
  font-size: 0.95rem;
  cursor: pointer;
}
.menu-items button:hover { background: #24c8db; color: #06262a; }
.menu-items button.active { background: #1a6b77; color: #fff; }
.menu-items button.active:hover { background: #24c8db; color: #06262a; }

.menu-note {
  padding: 6px 10px;
  font-size: 0.8rem;
  color: #999;
  white-space: nowrap;
}

.online-slot-label {
  padding: 5px 10px 2px;
  color: #aaa;
  font-size: 0.78rem;
}
#online-slot {
  margin: 0 6px 4px;
  padding: 6px 8px;
  border: 1px solid #444;
  border-radius: 5px;
  background: #17171b;
  color: #e0e0e0;
  font-size: 0.88rem;
}

.status {
  margin-left: auto;
  font-size: 0.8rem;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 40%;
}

.solo-badge {
  margin-left: 10px;
  font-size: 0.8rem;
  color: #4fc3f7;
  font-weight: 600;
  white-space: nowrap;
}

/* ---- Screen grid (video-call layout) ---- */
.screen-container {
  flex: 1 1 auto;
  display: grid;
  gap: 6px;
  padding: 6px;
  min-height: 0;
  background: #000;
  position: relative;
}

.screen-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  background: #0a0a0a;
  overflow: hidden;
}

.screen-cell canvas {
  width: 100%;
  height: 100%;
  object-fit: contain; /* letterbox to the GBA 3:2 ratio, like a video tile */
  image-rendering: pixelated;
  background: #000;
}

/* ---- Player color coding (like name borders on a video call) ---- */
.screen-cell.player-1 { outline: 3px solid #ff4444; outline-offset: -3px; }
.screen-cell.player-2 { outline: 3px solid #4488ff; outline-offset: -3px; }
.screen-cell.player-3 { outline: 3px solid #2eaa44; outline-offset: -3px; }
.screen-cell.player-4 { outline: 3px solid #e08a1e; outline-offset: -3px; }
.screen-container.no-outlines .screen-cell { outline: none; }

/* Bottom-right player tag, matching the border color. */
.screen-tag {
  position: absolute;
  bottom: 6px;
  right: 6px;
  padding: 2px 8px;
  font-size: 0.72rem;
  font-weight: 700;
  color: #fff;
  border-radius: 4px;
  letter-spacing: 0.04em;
  pointer-events: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
}
.screen-tag.player-1 { background: #ff4444; }
.screen-tag.player-2 { background: #4488ff; }
.screen-tag.player-3 { background: #2eaa44; }
.screen-tag.player-4 { background: #e08a1e; }

/* ---- View modes (video-call arrangements) ---- */
/* The "focused" tile is the one speaker/focus/overlay enlarge or show. */
.screen-container[data-view="speaker"] .focused-tile {
  grid-column: 1 / -1;
  grid-row: 1;
}
.screen-container[data-view="focus"] .screen-cell { display: none; }
.screen-container[data-view="focus"] .screen-cell.focused-tile { display: flex; }
.screen-container[data-view="overlay"] .focused-tile {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 58%;
  height: 58%;
  z-index: 50;
  box-shadow: 0 10px 36px rgba(0, 0, 0, 0.75);
}

/* ---- Debug overlay (bottom ticker, pointer-transparent) ---- */
.overlay {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 200;
  pointer-events: none;
  display: flex;
  flex-direction: column-reverse;
  gap: 2px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
}

.overlay .line {
  align-self: flex-start;
  max-width: 100%;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.78);
  color: #9fe8ff;
  white-space: pre-wrap;
  word-break: break-word;
  text-shadow: 0 1px 2px #000;
}
.overlay .line.warn { color: #ffb066; }
.overlay .line.err  { color: #ff6f6f; }
.overlay .line.ok   { color: #7dff8a; }

/* ---- Control re-mapping overlay ---- */
.remap-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
}
.remap-overlay[hidden] { display: none; } /* keep the hidden attribute effective */

.remap-panel {
  width: min(520px, 92vw);
  max-height: 86vh;
  overflow-y: auto;
  background: #1e1e22;
  border: 1px solid #3a3a40;
  border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}

.remap-panel h2 {
  margin: 0 0 10px;
  font-size: 1.1rem;
  color: #24c8db;
}

.remap-grid-head {
  display: grid;
  grid-template-columns: 90px 1fr 1fr;
  gap: 8px;
  padding: 4px 8px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #999;
}

.remap-row {
  display: grid;
  grid-template-columns: 90px 1fr 1fr;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
}

.remap-action {
  font-weight: 700;
  font-size: 0.95rem;
  color: #e0e0e0;
}

.remap-cell {
  background: #26262c;
  border: 1px solid #3a3a40;
  border-radius: 6px;
  color: #ddd;
  padding: 7px 10px;
  font-size: 0.9rem;
  text-align: center;
  cursor: pointer;
}
.remap-cell:hover { border-color: #24c8db; }
.remap-cell.listening {
  border-color: #24c8db;
  background: #0f3f45;
  color: #fff;
  box-shadow: 0 0 0 2px rgba(36, 200, 219, 0.35);
}

/* Gamepad focus cursor: a distinct amber ring so it's obvious without a mouse. */
.remap-cell.focused,
.remap-actions button.focused {
  outline: none;
  border-color: #ffd24a;
  box-shadow: 0 0 0 2px rgba(255, 210, 74, 0.65);
}

.remap-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}

.remap-actions button {
  background: #2a2a30;
  border: 1px solid #444;
  border-radius: 6px;
  color: #e0e0e0;
  padding: 8px 16px;
  font-size: 0.9rem;
  cursor: pointer;
}
.remap-actions button:hover { background: #3a3a42; }
.remap-actions #remap-done { background: #1a6b77; border-color: #1a6b77; }
.remap-actions #remap-done:hover { background: #24c8db; color: #06262a; }

.remap-hint {
  margin-top: 12px;
  font-size: 0.82rem;
  color: #9fe8ff;
  min-height: 1.2em;
}

/* ---- Game library (controller-navigable launcher) ---- */
.library-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
}
.library-overlay[hidden] { display: none; }

.library-panel {
  width: min(860px, 94vw);
  height: min(640px, 88vh);
  display: flex;
  flex-direction: column;
  background: #1b1b1f;
  border: 1px solid #3a3a40;
  border-radius: 10px;
  padding: 16px 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}

.library-header {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 10px;
}
.library-header h2 {
  margin: 0;
  font-size: 1.15rem;
  color: #24c8db;
}
.library-hint {
  font-size: 0.8rem;
  color: #9fe8ff;
}

.library-grid {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  align-content: start;
  padding: 4px;
}

.library-section-title {
  grid-column: 1 / -1;
  margin-top: 6px;
  padding-bottom: 4px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #999;
  border-bottom: 1px solid #333;
}

.library-empty {
  grid-column: 1 / -1;
  padding: 24px 12px;
  text-align: center;
  color: #888;
  font-size: 0.9rem;
}

.library-tile {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  background: #242429;
  border: 2px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  text-align: center;
}
.library-tile:hover { border-color: #24c8db; }
.library-tile.focused {
  border-color: #ffd24a;
  box-shadow: 0 0 0 2px rgba(255, 210, 74, 0.55);
}

.library-art {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 5px;
  overflow: hidden;
  background: #121216;
  display: flex;
  align-items: center;
  justify-content: center;
}
.library-art img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-rendering: auto;
}

.library-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  color: rgba(255, 255, 255, 0.92);
  font-size: 0.78rem;
  font-weight: 600;
  text-align: center;
  word-break: break-word;
  line-height: 1.15;
  overflow: hidden;
}

.library-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  padding: 1px 7px;
  font-size: 0.66rem;
  font-weight: 700;
  color: #fff;
  background: rgba(255, 210, 74, 0.9);
  color: #2a1e00;
  border-radius: 4px;
}

.library-name {
  font-size: 0.78rem;
  color: #e0e0e0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 12px;
}
.library-actions button {
  background: #2a2a30;
  border: 1px solid #444;
  border-radius: 6px;
  color: #e0e0e0;
  padding: 8px 16px;
  font-size: 0.9rem;
  cursor: pointer;
}
.library-actions button:hover { background: #3a3a42; }
.library-actions button.focused {
  border-color: #ffd24a;
  box-shadow: 0 0 0 2px rgba(255, 210, 74, 0.55);
}
.library-actions #library-close { background: #1a6b77; border-color: #1a6b77; }
.library-actions #library-close:hover { background: #24c8db; color: #06262a; }

/* Hide scrollbars app-wide (library/pause panels scroll with wheel or D-pad). */
* { scrollbar-width: none; }
*::-webkit-scrollbar { display: none; }

/* ---- Pause menu (controller-navigable) ---- */
.pause-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
}
.pause-overlay[hidden] { display: none; }
.pause-panel {
  width: min(340px, 90vw);
  background: #1e1e22;
  border: 1px solid #3a3a40;
  border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}
.pause-panel h2 { margin: 0 0 12px; font-size: 1.1rem; color: #24c8db; }
.pause-items { display: flex; flex-direction: column; gap: 6px; }
.pause-items button {
  text-align: left;
  background: #26262c;
  border: 1px solid #3a3a40;
  border-radius: 6px;
  color: #e0e0e0;
  padding: 10px 14px;
  font-size: 0.95rem;
  cursor: pointer;
}
.pause-items button:hover { border-color: #24c8db; }
.pause-items button.focused {
  border-color: #ffd24a;
  box-shadow: 0 0 0 2px rgba(255, 210, 74, 0.55);
}

/* Player-count choices in the library flow (full-width rows). */
.library-choice {
  grid-column: 1 / -1;
  background: #26262c;
  border: 1px solid #3a3a40;
  border-radius: 8px;
  color: #e0e0e0;
  padding: 14px 18px;
  font-size: 1rem;
  cursor: pointer;
  text-align: center;
}
.library-choice:hover { border-color: #24c8db; }
.library-choice.focused {
  border-color: #ffd24a;
  box-shadow: 0 0 0 2px rgba(255, 210, 74, 0.55);
}

/* ---- Touchscreen controls overlay ----
   Fixed layer above the game; pointer-events only on the buttons themselves
   so the game view stays clickable/scrollable between them. Sized for thumbs
   (GBA-like ergonomics): D-pad bottom-left, A/B bottom-right, shoulders top. */
.touch-controls {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 250; /* above screens and menus, below modals (350+) */
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  pointer-events: none; /* the layer itself is transparent to touches */
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
.touch-controls[hidden] { display: none; }

.touch-btn {
  pointer-events: auto;
  border: none;
  border-radius: 50%;
  background: rgba(36, 40, 46, 0.55);
  color: #cfd6dd;
  font: 700 0.9rem/1 system-ui, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.touch-btn.pressed {
  background: rgba(36, 200, 219, 0.55);
  color: #06262a;
  transform: scale(0.94);
}

.touch-top {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
}
.touch-shoulder {
  width: 74px;
  height: 38px;
  border-radius: 12px;
  font-size: 0.85rem;
}
.touch-top-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
}
.touch-pause {
  width: 34px;
  height: 30px;
  border-radius: 8px;
  font-size: 0.9rem;
}
.touch-note {
  color: #9fd8e2;
  font: 600 0.72rem/1.2 system-ui, sans-serif;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
}
.touch-player-picker {
  display: flex;
  gap: 4px;
}
.touch-player-picker button {
  pointer-events: auto;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 999px;
  background: rgba(30, 30, 34, 0.55);
  color: #cfd6dd;
  font: 700 0.68rem/1 system-ui, sans-serif;
  padding: 4px 9px;
  touch-action: manipulation;
}
.touch-player-picker button.active {
  background: #24c8db;
  border-color: #24c8db;
  color: #06262a;
}

.touch-main {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
}

/* D-pad: 3x3 grid, cross-shaped. The empty corners pass touches through. */
.touch-dpad {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 3px;
}
.touch-dpad .touch-btn { border-radius: 12px; font-size: 0.8rem; }
.dp-up    { grid-area: 1 / 2; }
.dp-left  { grid-area: 2 / 1; }
.dp-center {
  grid-area: 2 / 2;
  border-radius: 10px;
  background: rgba(36, 40, 46, 0.35);
}
.dp-right { grid-area: 2 / 3; }
.dp-down  { grid-area: 3 / 2; }

/* A/B cluster: B lower-left of A, GBA-style diagonal. */
.touch-actions {
  display: grid;
  grid-template-columns: repeat(2, 64px);
  grid-template-rows: repeat(2, 64px);
  gap: 6px;
}
.face-b { grid-area: 2 / 1; font-size: 1.15rem; }
.face-a { grid-area: 1 / 2; font-size: 1.15rem; }

.touch-center-row {
  display: flex;
  justify-content: center;
  gap: 14px;
}
.touch-meta {
  width: 76px;
  height: 30px;
  border-radius: 999px;
  font-size: 0.62rem;
  letter-spacing: 0.06em;
}

/* Guest seat tile: same video-call look, but the one tile is the guest's own
   seat — drop the focus ring (there is nothing to focus past it). */
.screen-cell.guest-seat-tile { outline: none; }

/* Hidden must always win: the touch player picker is display:flex by default,
   which used to defeat the hidden attribute and left guests a stray P1 pill. */
.touch-player-picker[hidden] { display: none; }

/* Short screens (landscape phones): slim everything so the game stays visible. */
@media (max-height: 460px) {
  .touch-dpad {
    grid-template-columns: repeat(3, 46px);
    grid-template-rows: repeat(3, 46px);
  }
  .touch-actions {
    grid-template-columns: repeat(2, 52px);
    grid-template-rows: repeat(2, 52px);
  }
  .touch-shoulder { width: 62px; height: 32px; }
  .touch-meta { width: 66px; height: 26px; }
}

/* ---- Online guest session (magic-link joiners) ----
   A guest on a phone gets a console-free controller view: the menu bar is all
   host powers (ROMs, invites, saves, remap), so body.guest-session (set by
   main.js when the Online guest status lands) removes it and everything that
   floats over the game. The touch pad itself is NOT hidden — that's the guest's
   controls — and the crash-report pill stays reachable for bug reports. */
body.guest-session .menubar,
body.guest-session .overlay,
body.guest-session #solo-badge {
  display: none !important;
}
body.guest-session .screen-container {
  padding: 0;
  gap: 0;
}

/* ---- Full screen / hide menu (F11) ----
   body.ui-hidden drops the chrome so the game fills the window — the same
   treatment the guest view gets, but user-toggled instead of role-based. The
   menubar is display:none rather than collapsed so its shortcut keys can't
   fire while hidden; the hint pill fades out after a few seconds. */
body.ui-hidden .menubar,
body.ui-hidden .overlay,
body.ui-hidden #solo-badge {
  display: none !important;
}
body.ui-hidden .screen-container {
  padding: 0;
  gap: 0;
}

.ui-hidden-hint {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  z-index: 300;
  padding: 7px 16px;
  border-radius: 999px;
  background: rgba(28, 28, 31, 0.88);
  border: 1px solid #3a3a40;
  color: #ddd;
  font-size: 0.86rem;
  pointer-events: none;
  opacity: 1;
  transition: opacity 1.1s ease 2.2s;
}
.ui-hidden-hint[hidden] { display: none; }
.ui-hidden-hint.fade { opacity: 0; }

/* ---- Online invite QR/share dialog ---- */
.online-qr-overlay {
  position: fixed;
  inset: 0;
  z-index: 350;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.78);
}
.online-qr-overlay[hidden] { display: none; }
.online-qr-panel {
  width: min(440px, 96vw);
  max-height: 94vh;
  overflow-y: auto;
  padding: 20px;
  border: 1px solid #3a3a40;
  border-radius: 12px;
  background: #1e1e22;
  box-shadow: 0 12px 44px rgba(0, 0, 0, 0.72);
  text-align: center;
}
.online-qr-panel h2 {
  margin: 0 0 8px;
  color: #24c8db;
  font-size: 1.2rem;
}
.online-qr-help {
  margin: 0 auto 14px;
  max-width: 360px;
  color: #aaa;
  font-size: 0.84rem;
  line-height: 1.4;
}
#online-qr-canvas {
  display: block;
  width: min(320px, 78vw);
  height: auto;
  margin: 0 auto 14px;
  border-radius: 4px;
  background: #fff;
  image-rendering: pixelated;
}
.online-url-label {
  display: block;
  margin: 4px 0;
  color: #aaa;
  font-size: 0.76rem;
  text-align: left;
}
.online-invite-url {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #444;
  border-radius: 6px;
  background: #111;
  color: #ddd;
  font: 0.76rem ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.online-qr-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
}
.online-qr-actions button {
  padding: 8px 12px;
  border: 1px solid #444;
  border-radius: 6px;
  background: #2a2a30;
  color: #e0e0e0;
  cursor: pointer;
}
.online-qr-actions button:hover { border-color: #24c8db; background: #1a6b77; }
.online-qr-actions #online-qr-close { background: #1a6b77; border-color: #1a6b77; }
