body, html {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; background: #000;
  font-family: "Noto Sans", system-ui, sans-serif;
}
* {
  user-select: none;
  -webkit-user-select: none;
}
#webgl {
  width: 100vw; height: 100vh;
  display: block;
  position: fixed;
  top: 0; left: 0;
}
#ui { position: fixed; inset: 0; pointer-events: none; }
#crosshair {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid #000; opacity: 0.7;
}
#hint {
  position: absolute; left: 50%; bottom: 12%; transform: translateX(-50%);
  color: #000; font-size: 14px; opacity: 0.6; user-select: none;
}
#click-to-play {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  color: rgba(255, 255, 255, 0.9);
  font-size: 32px;
  font-weight: 600;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
  pointer-events: none;
  animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.8; }
  50% { opacity: 1; }
}
#throw-gauge {
  position: absolute; left: 50%; top: calc(50% + 32px); transform: translateX(-50%);
  width: 280px; height: 10px; background: rgba(0,0,0,0.1);
  border-radius: 12px; backdrop-filter: blur(8px);
  border: 1px solid rgba(0,0,0,0.15); pointer-events: none;
  display: none;
}
#throw-gauge.show { display: block; }
#throw-bar {
  height: 100%; width: 0%; background: #000; border-radius: 12px;
  transition: width 80ms ease;
}
#throw-gauge #throw-value {
  position: absolute; top: -22px; left: 0; width: 100%; text-align: center;
  font-size: 12px; color: #000; opacity: 0.7;
}
#debug-ui {
  position: fixed; top: 10px; left: 10px;
  background: rgba(0, 0, 0, 0.9); color: #fff;
  padding: 15px; border-radius: 8px;
  font-size: 14px; pointer-events: auto;
  z-index: 10000;
  max-width: 300px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
#debug-ui h3 { margin: 0 0 10px 0; font-size: 16px; }
#debug-ui label { display: block; margin: 5px 0; cursor: pointer; }
#debug-ui input[type="checkbox"] { margin-right: 8px; cursor: pointer; }
#debug-ui button {
  margin-top: 10px; padding: 5px 10px;
  background: #444; color: #fff; border: 1px solid #666;
  border-radius: 4px; cursor: pointer;
}
#debug-ui button:hover { background: #555; }
#debug-ui.hidden { display: none; }

#level-complete.hidden { display: none; }
#level-complete {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  pointer-events: auto; background: rgba(0,0,0,0.5);
}
#level-complete .panel {
  background: rgba(255,255,255,0.6);
  color: #000; padding: 24px 28px; border-radius: 18px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25), inset 0 1px 0 rgba(255,255,255,0.6);
  backdrop-filter: blur(20px) saturate(120%); -webkit-backdrop-filter: blur(20px) saturate(120%);
  border: 1px solid rgba(0,0,0,0.08); min-width: 260px; text-align: center;
}
#level-complete .panel button {
  margin-top: 12px; padding: 10px 14px; border-radius: 10px;
  background: rgba(0,0,0,0.85); color: #fff; border: 1px solid rgba(0,0,0,0.9);
  transition: transform 120ms ease, opacity 120ms ease;
}

#mobile-controls { display: none; }
@media (pointer: coarse) {
  #mobile-controls { display: block; position: fixed; inset: 0; pointer-events: none; }
  #left-joystick, #right-joystick {
    position: absolute; width: 160px; height: 160px; pointer-events: auto;
    bottom: 24px; background: rgba(0,0,0,0.06); border-radius: 12px;
  }
  #left-joystick { left: 24px; }
  #right-joystick { right: 24px; }
  #mobile-buttons {
    position: absolute; bottom: 24px; left: 50%; transform: translateX(-50%);
    display: flex; gap: 10px; pointer-events: auto;
  }
  #mobile-buttons button {
    width: 56px; height: 56px; border-radius: 50%;
    font-size: 24px; line-height: 1; display: grid; place-items: center;
    border: 1px solid #222;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }
  #mobile-buttons button:active {
    transform: scale(0.95);
    opacity: 0.8;
  }
  #btn-blue { background: #2a7fff; color: #fff; }
  #btn-orange { background: #ff7a00; color: #fff; }
  #btn-grab { background: #000; color: #fff; }
  #hint { display: none; }
  #click-to-play { display: none; }
}

#render-scale-control {
  position: fixed; right: 56px; top: 12px; pointer-events: auto;
  background: rgba(255,255,255,0.6); color: #000; border: 1px solid rgba(0,0,0,0.08);
  border-radius: 12px; padding: 8px 10px; backdrop-filter: blur(16px) saturate(120%);
  font-size: 12px; display: flex; align-items: center; gap: 8px;
}
#render-scale-control input[type="range"] { width: 140px; }
#render-scale-control label { opacity: 0.7; }
#render-scale-control span { min-width: 42px; text-align: right; opacity: 0.8; }

#menu-toggle {
  position: fixed; right: 12px; top: 12px; z-index: 1000;
  background: #000; color: #fff; border: 1px solid #222;
  border-radius: 8px; padding: 8px 10px; cursor: pointer;
  pointer-events: auto;
}
#back-to-menu {
  position: fixed; left: 12px; top: 12px; z-index: 1000;
  background: #000; color: #fff; border: 1px solid #222;
  border-radius: 8px; padding: 8px 10px; cursor: pointer;
  pointer-events: auto;
  font-size: 18px;
}
#back-to-menu:hover {
  background: #222;
}
/* Start Menu */
.menu-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  pointer-events: auto;
}

.menu-panel {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 40px;
  max-width: 500px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  text-align: center;
}

.menu-panel h1 {
  margin: 0 0 10px 0;
  font-size: 36px;
  color: #000;
  font-weight: 700;
}

.menu-panel .subtitle {
  margin: 0 0 30px 0;
  font-size: 16px;
  color: #666;
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.menu-btn {
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}

.menu-btn:hover {
  background: rgba(255, 255, 255, 1);
  border-color: rgba(0, 0, 0, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.menu-btn.primary {
  background: rgba(0, 100, 255, 0.9);
  border-color: rgba(0, 100, 255, 1);
  color: #fff;
}

.menu-btn.primary:hover {
  background: rgba(0, 100, 255, 1);
}

.menu-btn .btn-icon {
  font-size: 24px;
}

.menu-btn .btn-text {
  font-size: 20px;
  font-weight: 600;
}

.menu-btn .btn-desc {
  font-size: 14px;
  opacity: 0.8;
}

.menu-btn.primary .btn-desc {
  opacity: 0.9;
}

/* Edit Mode UI */
#edit-ui.hidden {
  display: none;
}

/* Top Toolbar (Back + Tools) */
.edit-topbar {
  position: fixed;
  top: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 900;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.topbar-divider {
  width: 1px;
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
}

.edit-toolbar {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.toolbar-section {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 4px;
}

.toolbar-section h3 {
  margin: 0 0 15px 0;
  font-size: 18px;
}

.toolbar-section h4 {
  margin: 0 0 10px 0;
  font-size: 14px;
  opacity: 0.8;
}

.toolbar-section p {
  margin: 5px 0;
  font-size: 12px;
  opacity: 0.7;
}

.toolbar-btn {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-bottom: 8px;
  font-size: 14px;
}

.toolbar-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
}

.toolbar-btn:active, .toolbar-btn.active {
  background: rgba(100, 150, 255, 0.4);
  border-color: rgba(100, 150, 255, 0.6);
}

.hidden {
  display: none !important;
}

/* Hide desktop-only elements on mobile */
@media (max-width: 768px), (pointer: coarse) {
  .desktop-only {
    display: none !important;
  }
}

.toolbar-checkbox {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 0px;
  padding: 4px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.toolbar-checkbox input[type="checkbox"] {
  cursor: pointer;
}

.toolbar-checkbox:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Icon buttons for transform modes */
.icon-buttons {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.icon-btn {
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
  transform: translateY(-2px);
}

.icon-btn:active, .icon-btn.active {
  background: rgba(100, 150, 255, 0.4);
  border-color: rgba(100, 150, 255, 0.6);
}

.icon-btn.danger {
  background: rgba(255, 50, 50, 0.2);
  border-color: rgba(255, 50, 50, 0.4);
}

.icon-btn.danger:hover {
  background: rgba(255, 50, 50, 0.4);
  border-color: rgba(255, 50, 50, 0.6);
}

.toolbar-btn.danger {
  background: rgba(255, 50, 50, 0.2);
  border-color: rgba(255, 50, 50, 0.4);
}

.toolbar-btn.danger:hover {
  background: rgba(255, 50, 50, 0.4);
  border-color: rgba(255, 50, 50, 0.6);
}

/* Object group styling */
.object-group {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Level Slot Selector (Top Right) */
#level-slot-selector {
  position: fixed;
  top: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  font-family: 'Courier New', monospace;
  font-size: 13px;
  z-index: 900;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.slot-label {
  color: rgba(255, 255, 255, 0.7);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.slot-buttons {
  display: flex;
  gap: 4px;
}

.slot-btn {
  width: 28px;
  height: 28px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.8);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  font-family: 'Courier New', monospace;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.slot-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.slot-btn.active {
  background: rgba(100, 200, 255, 0.4);
  border-color: rgba(100, 200, 255, 0.8);
  color: #fff;
  box-shadow: 0 0 8px rgba(100, 200, 255, 0.4);
}

.slot-reset-btn {
  width: 28px;
  height: 28px;
  border: 1px solid rgba(255, 100, 100, 0.3);
  background: rgba(255, 100, 100, 0.15);
  color: rgba(255, 150, 150, 0.9);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin-left: 4px;
}

.slot-reset-btn:hover {
  background: rgba(255, 100, 100, 0.3);
  border-color: rgba(255, 100, 100, 0.6);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(255, 0, 0, 0.2);
}

.slot-reset-btn i {
  font-size: 12px;
}

/* Editor Controls Panel (Bottom Right) */
#editor-controls-panel {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 900;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.controls-toggle {
  width: 40px;
  height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  color: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.controls-toggle:hover {
  background: rgba(20, 20, 20, 0.85);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.controls-toggle.active {
  background: rgba(100, 200, 255, 0.3);
  border-color: rgba(100, 200, 255, 0.6);
  color: rgba(100, 200, 255, 1);
}

.controls-content {
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  min-width: 200px;
  max-height: 300px;
  overflow-y: auto;
  transition: all 0.3s ease;
  opacity: 1;
  transform: translateY(0);
}

.controls-content.collapsed {
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  max-height: 0;
  padding: 0;
  border: none;
}

.controls-header {
  margin-bottom: 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

.controls-header h4 {
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  font-weight: 600;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-family: 'Courier New', monospace;
}

.controls-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.control-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 3px;
  transition: background 0.2s ease;
}

.control-item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.control-key {
  color: rgba(100, 200, 255, 0.9);
  font-family: 'Courier New', monospace;
  font-size: 10px;
  font-weight: 600;
  background: rgba(100, 200, 255, 0.1);
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid rgba(100, 200, 255, 0.3);
  white-space: nowrap;
  min-width: 65px;
  text-align: center;
}

.control-desc {
  color: rgba(255, 255, 255, 0.7);
  font-size: 10px;
  flex: 1;
  text-align: right;
}

/* Custom scrollbar for controls panel */
.controls-content::-webkit-scrollbar {
  width: 6px;
}

.controls-content::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
}

.controls-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.controls-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Place Objects Panel (Left Side, Always Visible) */
#place-objects-panel {
  position: fixed;
  left: 16px;
  top: 80px;
  z-index: 900;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  min-width: 200px;
}

.panel-header {
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

.panel-header h4 {
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  font-weight: 600;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-family: 'Courier New', monospace;
}

/* Place Objects Groups */
.place-group {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 8px;
  margin-bottom: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.place-text-btn {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 13px;
  text-align: left;
  font-family: 'Courier New', monospace;
}

.place-text-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
  transform: translateX(2px);
}

.place-text-btn.active {
  background: rgba(100, 200, 255, 0.3);
  border-color: rgba(100, 200, 255, 0.6);
  box-shadow: 0 0 8px rgba(100, 200, 255, 0.4);
}

/* Modern Toggle Switch */
.toggle-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 4px;
  margin-top: 4px;
}

.toggle-label {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.7);
  font-family: 'Courier New', monospace;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.2);
  transition: 0.3s;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.toggle-slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.toggle-switch input:checked + .toggle-slider {
  background-color: rgba(100, 200, 255, 0.6);
  border-color: rgba(100, 200, 255, 0.8);
}

.toggle-switch input:checked + .toggle-slider:before {
  transform: translateX(20px);
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(100, 200, 255, 0.5);
}

.toggle-switch input:focus + .toggle-slider {
  box-shadow: 0 0 4px rgba(100, 200, 255, 0.5);
}

.toggle-slider:hover {
  background-color: rgba(255, 255, 255, 0.25);
}

.toggle-switch input:checked + .toggle-slider:hover {
  background-color: rgba(100, 200, 255, 0.7);
}
