:root {
  --bg: #0a0a0f;
  --panel: #15151f;
  --panel-alpha: rgba(21, 21, 31, 0.82);
  --border: #26263a;
  --text: #e8e8ee;
  --text-muted: #8d8da3;
  --accent: #6366f1;
  --accent-hover: #7b7ef2;
  --accent-soft: rgba(99, 102, 241, 0.15);
  --ok: #22c55e;
  --warn: #eab308;
  --err: #ef4444;
  --radius: 10px;
  --transition: 160ms cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(ellipse at top, #14141f 0%, var(--bg) 60%);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------- topbar */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px;
  background: var(--panel-alpha);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  z-index: 20;
}

#brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.logo {
  width: 20px;
  height: 20px;
  color: var(--accent);
  flex-shrink: 0;
}

#stats {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 12px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  transition: background var(--transition), border-color var(--transition), transform var(--transition);
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: #3a3a52;
}

.icon-btn:active { transform: scale(0.97); }

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.icon-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #c7c8ff;
}

/* ------------------------------------------------------------- dropdown */

.dropdown { position: relative; }

.dropdown-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 240px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  opacity: 0;
  transform: translateY(-6px) scale(0.98);
  pointer-events: none;
  transition: opacity var(--transition), transform var(--transition);
}

.dropdown-menu.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.dropdown-menu button {
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: background var(--transition);
}

.dropdown-menu button:hover { background: var(--accent-soft); }

.dropdown-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 2px;
}

/* ------------------------------------------------------------- viewport */

#viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

#screen {
  max-width: 100%;
  max-height: 100%;
  cursor: default;
  outline: none;
}

/* -------------------------------------------------------- connect overlay */

#overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 5, 10, 0.82);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 220ms ease;
}

#overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

#overlay-card {
  width: min(360px, 90vw);
  background: linear-gradient(180deg, #191926 0%, #131320 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  padding: 32px 28px;
  text-align: center;
}

.logo-lg {
  width: 40px;
  height: 40px;
  margin-bottom: 8px;
}

#overlay-card h1 {
  font-size: 19px;
  font-weight: 600;
  margin: 4px 0 6px;
  letter-spacing: -0.01em;
}

#overlay-card p {
  margin: 0 0 20px;
  color: var(--text-muted);
  font-size: 13.5px;
  line-height: 1.5;
}

#token-input {
  width: 100%;
  padding: 11px 14px;
  margin-bottom: 12px;
  background: #0d0d16;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#token-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#connect-btn {
  width: 100%;
  padding: 11px;
  background: linear-gradient(180deg, var(--accent-hover), var(--accent));
  color: white;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: filter var(--transition), transform var(--transition);
}

#connect-btn:hover { filter: brightness(1.08); }
#connect-btn:active { transform: scale(0.98); }
#connect-btn:disabled { background: #333340; cursor: not-allowed; filter: none; }

/* ------------------------------------------------------------- status pill */

.status {
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 11.5px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  transition: background var(--transition), color var(--transition);
}

.status-connected { background: rgba(34, 197, 94, 0.15); color: var(--ok); }
.status-connecting, .status-reconnecting { background: rgba(234, 179, 8, 0.15); color: var(--warn); }
.status-disconnected { background: rgba(239, 68, 68, 0.15); color: var(--err); }

/* --------------------------------------------------------- gamepad overlay */

#gamepad-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  /* Fully see-through -- only the buttons themselves should be visible, so
     the game/desktop underneath stays readable while the controls are up. */
  background: transparent;
  opacity: 1;
  transition: opacity 220ms ease;
  touch-action: none;
  --gp-btn-size: 1;   /* 0.5 - 1.5, "tamano de botones" */
  --gp-btn-alpha: 0.2; /* 0.05 - 0.6, "opacidad" */
  --gp-scale: 1;      /* 0.5 - 1.5, "escala general" */
}

#gamepad-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

/* Everything that visually IS the gamepad -- scales with "escala general"
   as one unit. Chrome (close/settings buttons, the settings panel) lives
   outside this layer so it stays a predictable, always-usable size no
   matter how the user scales their controls. */
#gp-layer {
  position: fixed;
  inset: 0;
  transform: scale(var(--gp-scale));
  transform-origin: center center;
}

/* Edit mode (settings panel open): draggable groups get a dashed outline +
   "move" cursor, and every button stops accepting pointer events so a drag
   never accidentally registers as a press -- the group underneath (which
   IS still interactive) catches the touch instead. */
#gp-layer.editing .gp-group {
  outline: 2px dashed rgba(255, 255, 255, 0.7);
  outline-offset: 6px;
  border-radius: 16px;
  cursor: move;
}

#gp-layer.editing .gp-btn {
  pointer-events: none;
}

.gp-btn {
  position: fixed;
  min-width: calc(60px * var(--gp-btn-size));
  min-height: calc(60px * var(--gp-btn-size));
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, var(--gp-btn-alpha));
  color: #fff;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  border-radius: 14px;
  font-size: calc(18px * var(--gp-btn-size));
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  transition: background 80ms ease, transform 80ms ease, border-color 80ms ease;
}

.gp-btn.pressed {
  background: rgba(255, 255, 255, calc(var(--gp-btn-alpha) + 0.2));
  border-color: #fff;
  transform: scale(0.92);
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.15);
}

/* close / settings chrome -- deliberately NOT .gp-btn: stays a fixed,
   predictable size regardless of the user's size/opacity/scale settings */
.gp-top-bar {
  position: fixed;
  top: max(12px, 2vh);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 31;
}

.gp-chrome-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

/* shoulders -- top corners */
.gp-shoulder {
  top: max(14px, 2vh);
  width: calc(84px * var(--gp-btn-size));
  height: calc(60px * var(--gp-btn-size));
  border-radius: 12px;
  font-size: calc(16px * var(--gp-btn-size));
}
.gp-l { left: max(14px, 3vw); }
.gp-r { right: max(14px, 3vw); }

/* d-pad and ABXY -- position is JS-driven (draggable, persisted) instead
   of fixed CSS offsets; .gp-group just marks them for the edit-mode
   outline/cursor above. */
.gp-dpad,
.gp-abxy {
  position: fixed;
  display: grid;
  grid-template-columns: repeat(3, calc(64px * var(--gp-btn-size)));
  grid-template-rows: repeat(3, calc(64px * var(--gp-btn-size)));
  gap: 2px;
}

.gp-dpad-btn,
.gp-abxy-btn {
  position: static;
  min-width: 0;
  min-height: 0;
  font-size: calc(20px * var(--gp-btn-size));
}

.gp-dpad-btn { border-radius: 8px; }
.gp-abxy-btn { border-radius: 50%; }

.gp-dpad-up { grid-column: 2; grid-row: 1; }
.gp-dpad-left { grid-column: 1; grid-row: 2; }
.gp-dpad-right { grid-column: 3; grid-row: 2; }
.gp-dpad-down { grid-column: 2; grid-row: 3; }

.gp-dpad-center {
  grid-column: 2;
  grid-row: 2;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.gp-btn-x { grid-column: 2; grid-row: 1; }
.gp-btn-y { grid-column: 1; grid-row: 2; }
.gp-btn-a { grid-column: 3; grid-row: 2; }
.gp-btn-b { grid-column: 2; grid-row: 3; }

/* start/select -- bottom center */
.gp-center-buttons {
  position: fixed;
  left: 50%;
  bottom: max(16px, 4vh);
  transform: translateX(-50%);
  display: flex;
  gap: 14px;
}

.gp-small {
  position: static;
  min-width: calc(90px * var(--gp-btn-size));
  min-height: calc(60px * var(--gp-btn-size));
  border-radius: 999px;
  font-size: calc(12px * var(--gp-btn-size));
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* -------------------------------------------------------- settings panel */

#gamepad-settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 32;
  width: min(340px, 88vw);
  max-height: 82vh;
  overflow-y: auto;
  background: linear-gradient(180deg, #191926 0%, #131320 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  padding: 20px 22px;
  opacity: 1;
  transition: opacity 180ms ease, transform 180ms ease;
}

#gamepad-settings-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -46%);
}

#gamepad-settings-panel h2 {
  margin: 0 0 16px;
  font-size: 16px;
  font-weight: 600;
}

.gp-setting { margin-bottom: 16px; }

.gp-setting label {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.gp-setting input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.gp-settings-hint {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 4px 0 18px;
  padding: 10px 12px;
  background: rgba(99, 102, 241, 0.1);
  border-radius: 8px;
}

.gp-settings-actions {
  display: flex;
  gap: 10px;
}

.gp-settings-actions button {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background var(--transition), filter var(--transition);
}

#gp-settings-reset:hover { background: rgba(255, 255, 255, 0.06); }

#gp-settings-done {
  background: linear-gradient(180deg, var(--accent-hover), var(--accent));
  border: none;
  color: white;
}
#gp-settings-done:hover { filter: brightness(1.08); }

/* ------------------------------------------------------------- responsive */

@media (max-width: 720px) {
  #topbar { flex-wrap: wrap; }
  #stats { display: none; }
  .icon-btn span.label { display: none; }
}
