:root {
  --bg: #0a0a14;
  --bg-alt: #0d0d1f;
  --surface: #141428;
  --surface2: #1e1e3a;
  --surface3: #2a2a4a;
  --text: #e8e8f0;
  --text-secondary: #8888aa;
  --accent: #ff3366;
  --accent-glow: rgba(255,51,102,0.4);
  --green: #2ecc71;
  --blue: #3498db;
  --orange: #e67e22;
  --yellow: #f1c40f;
  --purple: #9b59b6;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,0.5);
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.4);
  --transition: 0.15s ease;
}

[data-theme="light"] {
  --bg: #f0f0f5;
  --bg-alt: #e8e8f0;
  --surface: #ffffff;
  --surface2: #e8e8f0;
  --surface3: #d8d8e8;
  --text: #1a1a2e;
  --text-secondary: #666688;
  --accent: #e91e63;
  --accent-glow: rgba(233,30,99,0.25);
  --green: #27ae60;
  --blue: #2980b9;
  --orange: #d35400;
  --yellow: #f39c12;
  --purple: #8e44ad;
}

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

html, body, #root, #app {
  height: 100%; width: 100%;
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#root, #app {
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ── Status Bar ── */
#status-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: rgba(20,20,40,0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  font-size: 13px; flex-shrink: 0; z-index: 10;
}
[data-theme="light"] #status-bar { background: rgba(255,255,255,0.85); }

.dot { width: 9px; height: 9px; border-radius: 50%; }
.dot.offline { background: #555; }
.dot.connecting { background: var(--yellow); animation: pulse 1.2s infinite; }
.dot.online { background: var(--blue); box-shadow: 0 0 6px var(--blue); }
.dot.paired { background: var(--green); box-shadow: 0 0 8px rgba(46,204,113,0.6); }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.25; } }

#room-code {
  margin-left: auto;
  font-family: 'SF Mono','Fira Code',monospace;
  font-size: 13px; letter-spacing: 3px;
  color: var(--accent); font-weight: 600;
}

/* ── Screens ── */
.screen { display: none; flex: 1; flex-direction: column; align-items: center; justify-content: center; padding: 16px; }
.screen.active { display: flex; }

/* ── Card ── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  width: 100%; max-width: 380px;
  text-align: center;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,0.05);
}
.card h1 { font-size: 32px; margin-bottom: 4px; color: var(--accent); letter-spacing: -1px; }
.card p { color: var(--text-secondary); margin-bottom: 20px; font-size: 14px; }

#code-input-group { display: flex; gap: 8px; margin-bottom: 16px; }
#code-input {
  flex: 1; font-size: 22px;
  font-family: 'SF Mono','Fira Code',monospace;
  letter-spacing: 5px; text-align: center;
  padding: 14px 12px;
  border: 2px solid var(--surface3);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  outline: none; text-transform: uppercase;
  transition: border-color var(--transition);
}
#code-input:focus { border-color: var(--accent); box-shadow: 0 0 12px var(--accent-glow); }

#scan-area { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 100%; }
#btn-scan { padding: 14px 20px; font-size: 15px; width: 100%; }
#qr-viewfinder { width: 100%; max-width: 260px; overflow: hidden; border-radius: var(--radius-sm); border: 2px solid var(--surface3); }
#qr-viewfinder video { width: 100%; display: block; }

/* ── Buttons ── */
button, .btn {
  font-family: inherit; font-size: 15px;
  border: none; border-radius: var(--radius-sm);
  padding: 12px 20px;
  cursor: pointer; touch-action: manipulation;
  min-height: 48px; min-width: 48px;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
  font-weight: 600;
}
button:active, .btn:active { transform: scale(0.94); }

.btn-primary {
  background: var(--accent); color: white;
  box-shadow: 0 4px 16px var(--accent-glow);
}
.btn-primary:active { box-shadow: 0 2px 8px var(--accent-glow); }

.btn-secondary {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--surface3);
}
.btn-secondary:active { background: var(--surface3); }

.hidden { display: none !important; }

/* ── Control Screen ── */
#control-screen { justify-content: space-between; padding: 8px 12px; gap: 10px; }

#control-footer {
  display: flex; gap: 10px; justify-content: center;
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  flex-shrink: 0;
}
#control-footer button, #control-footer select {
  flex: 1; max-width: 60px;
  font-size: 18px; padding: 10px 6px;
  text-align: center;
}
#control-footer button:last-child { max-width: 48px; }

#layout-select {
  font-size: 15px; padding: 8px;
  border: 1px solid var(--surface3); border-radius: var(--radius-sm);
  background: var(--surface2); color: var(--text);
  min-height: 48px; text-align: center;
  max-width: 120px !important;
  appearance: none; -webkit-appearance: none;
}

/* ── Layouts Common ── */
.layout-content {
  width: 100%; flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px;
}
.layout-hint {
  font-size: 12px; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: 2px;
  opacity: 0.5; text-align: center;
}

/* ── D-Pad ── */
.dpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 72px);
  grid-template-rows: repeat(3, 72px);
  gap: 5px;
  touch-action: none;
}
.dpad-btn {
  width: 72px; height: 72px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--surface2), var(--surface3));
  border: 1px solid rgba(255,255,255,0.06);
  font-size: 22px; color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: manipulation;
  transition: all var(--transition);
  box-shadow: 0 3px 10px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.04);
}
.dpad-btn:active {
  transform: scale(0.9);
  background: var(--surface3);
  box-shadow: 0 1px 4px rgba(0,0,0,0.4), inset 0 1px 2px rgba(0,0,0,0.3);
}
.dpad-btn.ok-btn {
  background: var(--accent);
  color: white;
  font-weight: 700; font-size: 15px;
  box-shadow: 0 4px 16px var(--accent-glow);
  border: none;
}
.dpad-btn.ok-btn:active {
  box-shadow: 0 1px 6px var(--accent-glow);
}

.dpad-btn[data-btn="up"] { grid-column: 2; grid-row: 1; }
.dpad-btn[data-btn="left"] { grid-column: 1; grid-row: 2; }
.dpad-btn[data-btn="ok"] { grid-column: 2; grid-row: 2; }
.dpad-btn[data-btn="right"] { grid-column: 3; grid-row: 2; }
.dpad-btn[data-btn="down"] { grid-column: 2; grid-row: 3; }

/* ── Action Buttons ── */
.action-row-fancy { display: flex; gap: 16px; justify-content: center; }

.action-ball {
  width: 60px; height: 60px; border-radius: 50%;
  font-weight: 800; font-size: 21px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: manipulation;
  transition: all var(--transition);
  box-shadow: 0 5px 20px rgba(0,0,0,0.4), inset 0 -3px 0 rgba(0,0,0,0.25), inset 0 2px 0 rgba(255,255,255,0.15);
  color: white;
  text-shadow: 0 1px 2px rgba(0,0,0,0.4);
  border: none;
}
.action-ball:active { transform: scale(0.88); box-shadow: 0 2px 8px rgba(0,0,0,0.5), inset 0 1px 0 rgba(0,0,0,0.3); }

.ball-a { background: radial-gradient(circle at 40% 35%, #55dd77, #27ae60); }
.ball-b { background: radial-gradient(circle at 40% 35%, #ff6666, #c0392b); }
.ball-x { background: radial-gradient(circle at 40% 35%, #66aaff, #2980b9); }
.ball-y { background: radial-gradient(circle at 40% 35%, #ffcc44, #e67e22); }

/* SNES diamond layout */
.action-diamond {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 52px 52px 52px;
  gap: 8px;
}
.action-diamond .ball-y { grid-column: 2; grid-row: 1; }
.action-diamond .ball-x { grid-column: 1; grid-row: 2; }
.action-diamond .ball-b { grid-column: 3; grid-row: 2; }
.action-diamond .ball-a { grid-column: 2; grid-row: 3; }

.action-diamond .action-ball { width: 52px; height: 52px; font-size: 18px; }

.control-row {
  display: flex; gap: 32px;
  align-items: center; justify-content: center;
  width: 100%;
}

#trackpad-hint {
  font-size: 12px; color: var(--text-secondary);
  text-align: center; padding: 2px 0;
  opacity: 0.5; letter-spacing: 1px;
}

/* ── Xbox Layout ── */
.xbox-layout {
  display: flex; align-items: center; justify-content: center;
  gap: 24px; width: 100%; padding: 0 8px;
}
.stick-area {
  width: 130px; height: 130px; border-radius: 50%;
  background: radial-gradient(circle at center, #252545, #161630);
  position: relative; touch-action: none;
  border: 3px solid #2a2a4a;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5), inset 0 2px 8px rgba(255,255,255,0.03);
}
.stick-knob {
  width: 46px; height: 46px; border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #555, #222);
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  transition: transform 0.04s linear;
  border: 2px solid #333;
  box-shadow: 0 3px 10px rgba(0,0,0,0.5);
}
.xbox-center { display: flex; flex-direction: column; align-items: center; gap: 16px; }

.dpad-mini {
  display: grid;
  grid-template-columns: 34px 34px 34px;
  grid-template-rows: 34px 34px 34px;
  gap: 3px;
}
.dpad-mini .dpad-btn {
  width: 34px; height: 34px; font-size: 12px;
  border-radius: 8px; padding: 0;
}
.dpad-mini .dpad-btn[data-btn="up"] { grid-column:2; grid-row:1; }
.dpad-mini .dpad-btn[data-btn="left"] { grid-column:1; grid-row:2; }
.dpad-mini .dpad-btn[data-btn="down"] { grid-column:2; grid-row:3; }
.dpad-mini .dpad-btn[data-btn="right"] { grid-column:3; grid-row:2; }

.xbox-center .action-ball { width: 40px; height: 40px; font-size: 14px; }
.xbox-menu-btns { display: flex; gap: 24px; }

.xbox-triggers {
  display: flex; gap: 10px; justify-content: center;
  flex-wrap: wrap; width: 100%; padding: 0 8px;
}
.trigger-btn {
  padding: 16px 28px;
  background: linear-gradient(180deg, var(--surface3), var(--surface2));
  border-radius: 10px; font-weight: 700; font-size: 16px;
  color: var(--text);
  cursor: pointer; touch-action: manipulation;
  min-width: 64px; text-align: center;
  border: 1px solid rgba(255,255,255,0.06);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  transition: all var(--transition);
  flex: 1;
}
.trigger-btn:active {
  background: linear-gradient(180deg, var(--surface2), var(--surface));
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  border-color: var(--accent);
}
.trigger-btn.ball-a { background: linear-gradient(180deg, #44cc66, #27ae60); color: white; }
.trigger-btn.ball-b { background: linear-gradient(180deg, #ff5555, #c0392b); color: white; }
.trigger-btn.ball-x { background: linear-gradient(180deg, #5599ff, #2980b9); color: white; }
.trigger-btn.ball-y { background: linear-gradient(180deg, #ffbb33, #e67e22); color: white; }

/* ── KBM Layout ── */
#trackpad-zone { width: 100%; max-width: 360px; height: 100px; margin: 8px auto; }
#trackpad-surface {
  width: 100%; height: 100%;
  background: linear-gradient(135deg, var(--surface2), var(--surface));
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary); font-size: 14px;
  touch-action: none; user-select: none;
  border: 1px solid var(--surface3);
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.3);
  letter-spacing: 1px; text-transform: uppercase;
}
#keyboard-zone { width: 100%; max-width: 380px; margin: 0 auto; }
.key-row { display: flex; gap: 4px; justify-content: center; margin-bottom: 4px; }
.key-btn {
  flex: 1; min-width: 30px; height: 40px;
  background: linear-gradient(180deg, var(--surface2), var(--surface));
  border-radius: 8px; border: 1px solid var(--surface3);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: var(--text);
  cursor: pointer; touch-action: manipulation;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  transition: all var(--transition);
}
.key-btn:active {
  background: var(--accent);
  color: white;
  transform: scale(0.92);
  border-color: var(--accent);
  box-shadow: 0 0 12px var(--accent-glow);
}
.key-btn.wide { min-width: 46px; flex: 1.6; }
.key-btn.space { flex: 5; }

/* ── Settings Panel ── */
#settings-panel .card { text-align: left; max-height: 85vh; overflow-y: auto; }
#settings-panel h2 { margin-bottom: 16px; color: var(--accent); font-size: 20px; }
#settings-panel label {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; font-size: 14px; color: var(--text-secondary);
  border-bottom: 1px solid rgba(255,255,255,0.04);
}
#settings-panel label:last-of-type { border-bottom: none; }
#settings-panel input[type="range"] { flex: 1; accent-color: var(--accent); }
#settings-panel input[type="checkbox"] { accent-color: var(--accent); width: 18px; height: 18px; }
#settings-panel select {
  margin-left: auto;
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--surface3); border-radius: 6px;
  padding: 6px 10px; font-size: 13px;
  appearance: none; -webkit-appearance: none;
}
#btn-settings-close { width: 100%; margin-top: 16px; }

/* ── Sensor status ── */
#sensor-status { font-size: 12px; color: var(--yellow); margin-top: 8px; }

/* ── Footer ── */
#control-footer { display: flex; gap: 8px; justify-content: center; padding: 6px 0 calc(6px + env(safe-area-inset-bottom)); flex-shrink: 0; }
#control-footer button, #control-footer select {
  flex: 1; font-size: 16px; padding: 10px 8px; text-align: center;
  min-width: 40px;
}
#btn-recenter, #btn-back, #btn-settings { max-width: 52px; font-size: 18px; }
#layout-select { max-width: 120px !important; font-size: 15px; }

/* ── Landscape ── */
@media (orientation: landscape) {
  .control-row { gap: 20px; }
  .dpad-grid { grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(3, 56px); gap: 4px; }
  .dpad-btn { width: 56px; height: 56px; font-size: 17px; border-radius: 12px; }
  .action-ball { width: 48px; height: 48px; font-size: 17px; }
  .action-diamond { grid-template-columns: 40px 40px 40px; grid-template-rows: 40px 40px 40px; gap: 6px; }
  .action-diamond .action-ball { width: 40px; height: 40px; font-size: 14px; }

  .xbox-layout { gap: 16px; }
  .stick-area { width: 100px; height: 100px; }
  .stick-knob { width: 36px; height: 36px; }

  #trackpad-zone { max-width: 440px; height: 60px; }
  #keyboard-zone { max-width: 520px; }
  .key-btn { height: 30px; font-size: 12px; }
}

/* ── Layout JUEGO (kid-friendly: apuntar + disparar) ─────────────────────── */
#layout-game {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  height: 100%;
  padding: 16px;
  text-align: center;
}
.game-hint {
  font-size: 17px;
  font-weight: 700;
  opacity: 0.85;
  line-height: 1.3;
}
.game-shoot {
  flex: 0 0 auto;
  width: min(74vw, 320px);
  height: min(74vw, 320px);
  border-radius: 50%;
  background: radial-gradient(circle at 50% 38%, #ff6b8f 0%, var(--accent) 60%, #c81f4a 100%);
  box-shadow: 0 0 0 8px var(--accent-glow), 0 14px 40px rgba(0,0,0,0.45), inset 0 -10px 26px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  cursor: pointer;
  transition: transform 0.06s ease, filter 0.06s ease;
  animation: game-pulse 2.2s ease-in-out infinite;
}
.game-shoot:active {
  transform: scale(0.93);
  filter: brightness(1.25);
  animation: none;
}
.game-shoot-emoji { font-size: 72px; line-height: 1; filter: drop-shadow(0 3px 6px rgba(0,0,0,0.4)); }
.game-shoot-label { font-size: 30px; font-weight: 900; letter-spacing: 1px; text-shadow: 0 2px 6px rgba(0,0,0,0.4); }
.game-shoot-sub { font-size: 14px; font-weight: 700; opacity: 0.85; }
@keyframes game-pulse {
  0%, 100% { box-shadow: 0 0 0 8px var(--accent-glow), 0 14px 40px rgba(0,0,0,0.45), inset 0 -10px 26px rgba(0,0,0,0.35); }
  50%      { box-shadow: 0 0 0 16px transparent, 0 14px 40px rgba(0,0,0,0.45), inset 0 -10px 26px rgba(0,0,0,0.35); }
}
.game-center {
  flex: 0 0 auto;
  padding: 16px 28px;
  border: none;
  border-radius: 999px;
  font-size: 19px;
  font-weight: 800;
  color: #fff;
  background: rgba(255,255,255,0.14);
  backdrop-filter: blur(6px);
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease;
}
.game-center:active { transform: scale(0.95); background: rgba(255,255,255,0.28); }

/* ── Layout RACING (gyro volante + pedales) ─────────────────────────────── */
#layout-racing { display:flex; flex-direction:column; height:100%; padding:14px; gap:14px; }
.racing-shifts { display:flex; gap:12px; justify-content:center; }
.shift-btn {
  flex:1; max-width:180px; padding:14px; border-radius:14px; text-align:center;
  font-weight:800; font-size:16px; color:#fff; background:rgba(255,255,255,0.12);
  -webkit-tap-highlight-color:transparent; user-select:none; touch-action:none;
}
.shift-btn:active { background:rgba(255,255,255,0.28); transform:scale(0.96); }
.racing-pedals { flex:1; display:flex; gap:14px; }
.pedal {
  flex:1; display:flex; align-items:center; justify-content:center; text-align:center;
  border-radius:22px; font-size:24px; font-weight:900; color:#fff; line-height:1.3;
  -webkit-tap-highlight-color:transparent; user-select:none; touch-action:none;
  box-shadow: inset 0 -10px 24px rgba(0,0,0,0.35), 0 8px 24px rgba(0,0,0,0.4);
  transition: transform 0.06s ease, filter 0.06s ease;
}
.pedal-gas { background:linear-gradient(160deg,#39d353,#1a8f38); }
.pedal-brake { background:linear-gradient(160deg,#ff5a5a,#b02020); }
.pedal:active { transform:scale(0.95); filter:brightness(1.2); }

/* ── Layout ARCADE (dpad grande + 4 botones grandes) ────────────────────── */
#layout-arcade { display:flex; align-items:center; justify-content:center; height:100%; padding:14px; }
.arcade-row { display:flex; align-items:center; justify-content:space-between; gap:20px; width:100%; max-width:520px; }
.dpad-grid.big .dpad-btn { width:64px; height:64px; font-size:24px; }
.arcade-buttons { display:grid; grid-template-columns:1fr 1fr; gap:16px; }
.arcade-btn {
  width:76px; height:76px; border-radius:50%; display:flex; align-items:center; justify-content:center;
  font-size:26px; font-weight:900; color:#fff; -webkit-tap-highlight-color:transparent;
  user-select:none; touch-action:none; box-shadow: inset 0 -8px 20px rgba(0,0,0,0.35), 0 6px 18px rgba(0,0,0,0.4);
  transition: transform 0.06s ease, filter 0.06s ease;
}
.arcade-btn:active { transform:scale(0.9); filter:brightness(1.25); }
