:root {
  --bg-dark: #0b0f19;
  --bg-card: rgba(18, 24, 38, 0.78);
  --bg-glass: rgba(255, 255, 255, 0.04);
  --border-glass: rgba(255, 255, 255, 0.09);
  --border-active: rgba(99, 102, 241, 0.45);

  --primary: #6366f1;
  --primary-glow: rgba(99, 102, 241, 0.35);
  --accent: #10b981;
  --accent-orange: #f59e0b;
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --text-dim: #6b7280;

  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 6px;

  --shadow-glow: 0 0 25px rgba(99, 102, 241, 0.22);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
  position: relative;
}

.app-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.glow-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(120px);
  opacity: 0.25;
}

.orb-1 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, #6366f1, #3b82f6);
  top: -100px;
  left: -100px;
}

.orb-2 {
  width: 450px;
  height: 450px;
  background: radial-gradient(circle, #10b981, #06b6d4);
  bottom: -100px;
  right: -100px;
}

.main-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 1120px;
  padding: 20px 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  background: var(--bg-card);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-lg);
  padding: 14px 22px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.logo-wrapper {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-cube {
  width: 38px;
  height: 38px;
  position: relative;
  transform: rotateX(60deg) rotateZ(45deg);
  transform-style: preserve-3d;
  animation: cube-float 3s ease-in-out infinite alternate;
}

@keyframes cube-float {
  0% { transform: translateY(0px) rotateX(60deg) rotateZ(45deg); }
  100% { transform: translateY(-6px) rotateX(60deg) rotateZ(45deg); }
}

.cube-face {
  position: absolute;
  width: 38px;
  height: 38px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.cube-face.top {
  background: linear-gradient(135deg, #4ade80, #22c55e);
  transform: translateZ(19px);
}

.cube-face.front {
  background: linear-gradient(135deg, #78350f, #92400e);
  transform: rotateX(-90deg) translateZ(19px);
}

.cube-face.side {
  background: linear-gradient(135deg, #b45309, #78350f);
  transform: rotateY(90deg) translateZ(19px);
}

.app-title {
  font-size: 25px;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.app-title span {
  color: var(--primary);
  background: linear-gradient(135deg, #818cf8, #6366f1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.app-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 400;
}

.header-status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid var(--border-glass);
  padding: 8px 13px;
  border-radius: 9999px;
  font-size: 13px;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
}

.status-dot {
  width: 8px;
  height: 8px;
  background-color: var(--accent);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--accent);
  animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.85); }
}

.toggle-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 13px;
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-glass);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.toggle-pill:hover {
  border-color: var(--border-active);
}

.toggle-pill input {
  accent-color: var(--primary);
  width: 15px;
  height: 15px;
  cursor: pointer;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.22), rgba(99, 102, 241, 0.08));
  border: 1px solid var(--border-active);
  color: var(--text-main);
  padding: 8px 15px;
  border-radius: var(--radius-md);
  font-family: 'Outfit', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.action-btn.secondary {
  background: rgba(255, 255, 255, 0.05);
  border-color: var(--border-glass);
}

.action-btn.secondary.muted {
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.45);
}

.action-btn:hover {
  background: var(--primary);
  box-shadow: var(--shadow-glow);
  transform: translateY(-1px);
}

.game-wrapper {
  width: 100%;
}

.canvas-container {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border-glass);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.6), var(--shadow-glow);
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-container:fullscreen,
.canvas-container:-webkit-full-screen,
.canvas-container:-moz-full-screen {
  width: 100vw;
  height: 100vh;
  max-width: none;
  border-radius: 0;
  border: none;
  background: #000;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
  outline: none;
}

/* Exact 16:9 letterboxing in fullscreen so pointer coordinates stay 1:1 with canvas pixels */
.canvas-container:fullscreen #game-canvas,
.canvas-container:-webkit-full-screen #game-canvas,
.canvas-container:-moz-full-screen #game-canvas {
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
}

.overlay-screen {
  position: absolute;
  inset: 0;
  background: rgba(11, 15, 25, 0.94);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 16px;
  z-index: 10;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.overlay-screen:not(.active) {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loader-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border-glass);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.pointerlock-prompt {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(18, 24, 38, 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border-active);
  padding: 11px 20px;
  border-radius: 9999px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.6), var(--shadow-glow);
  pointer-events: auto;
  cursor: pointer;
  transition: opacity 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
  z-index: 6;
}

.pointerlock-prompt:hover {
  border-color: #818cf8;
  transform: translateX(-50%) translateY(-2px);
}

.pointerlock-prompt.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(12px);
}

.prompt-content {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
}

.prompt-icon {
  font-size: 20px;
}

.prompt-text-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.prompt-text-group strong {
  font-size: 14px;
  color: #fff;
}

.prompt-text-group span {
  font-size: 12px;
  color: var(--text-muted);
}

.prompt-key {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 9px;
  border-radius: 5px;
  color: var(--text-main);
  white-space: nowrap;
}

.toast-banner {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid var(--border-active);
  color: var(--text-main);
  padding: 8px 16px;
  border-radius: 9999px;
  font-size: 13px;
  font-weight: 600;
  pointer-events: none;
  z-index: 8;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast-banner.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(-8px);
}

.app-footer {
  display: grid;
  grid-template-columns: 1.45fr 1fr;
  gap: 18px;
}

@media (max-width: 900px) {
  .app-footer {
    grid-template-columns: 1fr;
  }
}

.controls-card,
.gamepad-card {
  background: var(--bg-card);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-lg);
  padding: 18px 22px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.controls-card h3,
.gamepad-card h3 {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-main);
}

.gamepad-badge {
  margin-left: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 9999px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  border: 1px solid var(--border-glass);
}

.gamepad-badge.active {
  background: rgba(16, 185, 129, 0.16);
  border-color: rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

.controls-grid,
.gamepad-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 9px 12px;
}

.control-item {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text-muted);
}

kbd {
  display: inline-block;
  background: linear-gradient(180deg, #2a3449, #1a2233);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-bottom: 2px solid rgba(0, 0, 0, 0.5);
  color: #fff;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  white-space: nowrap;
}

.gamepad-status {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid var(--border-glass);
  padding: 9px 13px;
  border-radius: var(--radius-md);
  margin-bottom: 12px;
  font-size: 13px;
  font-family: 'JetBrains Mono', monospace;
}

.dot-indicator {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  background: var(--text-dim);
  border-radius: 50%;
}

.dot-indicator.connected {
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}

.gamepad-desc {
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.45;
}

/* --- Embed / Iframe Game-Only Mode --- */

body.embed-mode {
  min-height: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #000;
  display: block;
}

body.embed-mode .app-background,
body.embed-mode .app-header,
body.embed-mode .app-footer,
body.embed-mode .embed-modal:not(.warning-modal):not(.quit-modal) {
  display: none !important;
}

body.embed-mode .main-container {
  max-width: none;
  width: 100vw;
  height: 100vh;
  padding: 0;
  gap: 0;
}

body.embed-mode .game-wrapper,
body.embed-mode .canvas-container {
  width: 100vw;
  height: 100vh;
  aspect-ratio: auto;
  border-radius: 0;
  border: none;
  box-shadow: none;
  background: #000;
}

body.embed-mode #game-canvas {
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
}

.embed-controls {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 7;
  display: flex;
  gap: 6px;
  transition: opacity 0.2s ease;
}

.embed-controls.hidden,
body.pointer-locked .embed-controls {
  opacity: 0;
  pointer-events: none;
}

.embed-btn-mini {
  background: rgba(15, 23, 42, 0.78);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-glass);
  color: var(--text-main);
  padding: 6px 11px;
  border-radius: 9999px;
  font-family: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.embed-btn-mini:hover {
  background: var(--primary);
  border-color: var(--border-active);
}

.embed-btn-mini.muted {
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.45);
}

/* --- Embed Snippet Modal --- */

.embed-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(5, 8, 16, 0.78);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.embed-modal.hidden {
  display: none;
}

.embed-modal-card {
  width: 100%;
  max-width: 620px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-lg);
  padding: 22px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.75), var(--shadow-glow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.embed-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.embed-modal-header h2 {
  font-size: 18px;
  font-weight: 700;
}

.embed-close-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-glass);
  color: var(--text-main);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.embed-close-btn:hover {
  border-color: var(--border-active);
  background: rgba(99, 102, 241, 0.25);
}

.embed-modal-desc {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.45;
}

.embed-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
}

.embed-option-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-main);
  cursor: pointer;
  user-select: none;
}

.embed-option-item input {
  accent-color: var(--primary);
  width: 15px;
  height: 15px;
  cursor: pointer;
}

.embed-code-box {
  width: 100%;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-md);
  color: #e5e7eb;
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  line-height: 1.5;
  padding: 12px 14px;
  resize: vertical;
  outline: none;
}

.embed-code-box:focus {
  border-color: var(--border-active);
}

.embed-modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.embed-modal-actions a.action-btn {
  text-decoration: none;
}

/* --- First-Launch Browser Warning Modal --- */

.warning-modal-card {
  max-width: 680px;
  border-color: rgba(245, 158, 11, 0.45);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.78), 0 0 28px rgba(245, 158, 11, 0.18);
}

.warning-header-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.warning-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: rgba(245, 158, 11, 0.18);
  border: 1px solid rgba(245, 158, 11, 0.5);
  color: #fbbf24;
  font-size: 16px;
  flex-shrink: 0;
}

.warning-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.warning-item {
  background: rgba(0, 0, 0, 0.34);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-left: 3px solid rgba(245, 158, 11, 0.7);
  border-radius: var(--radius-md);
  padding: 9px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.warning-item strong {
  font-size: 13px;
  color: #fde68a;
}

.warning-item span {
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.45;
}

.warning-item code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11.5px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  padding: 1px 5px;
  border-radius: 4px;
}

.warning-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 4px;
}

/* --- Quit Confirmation Modal & Tab-Closed Fallback Overlay --- */

.quit-modal-card {
  max-width: 440px;
}

.action-btn.danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  border-color: rgba(248, 113, 113, 0.55);
  color: #fff;
}

.action-btn.danger:hover {
  background: linear-gradient(135deg, #f87171, #ef4444);
  border-color: rgba(252, 165, 165, 0.75);
}

.action-btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

.quit-closed-overlay {
  gap: 12px;
  text-align: center;
  padding: 24px;
}

.quit-closed-overlay p {
  max-width: 420px;
  color: var(--text-muted);
  line-height: 1.5;
}

.quit-closed-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(16, 185, 129, 0.16);
  border: 1px solid rgba(16, 185, 129, 0.5);
  color: #6ee7b7;
  font-size: 26px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

