:root {
  --bg: #06080f;
  --surface: #0f1524;
  --surface2: #172036;
  --surface3: #1e2a45;
  --border: rgba(116, 142, 255, 0.22);
  --cyan: #00e5ff;
  --indigo: #6366f1;
  --purple: #a855f7;
  --green: #10b981;
  --amber: #f59e0b;
  --red: #ef4444;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --nav-h: 64px;
  --top-h: 54px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  touch-action: manipulation;
}

input, textarea, select {
  user-select: text;
  -webkit-user-select: text;
  font-family: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 50% -15%, #132042 0%, var(--bg) 60%);
}

/* Top bar */
.topbar {
  height: var(--top-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: rgba(12, 17, 31, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  z-index: 30;
}

.brand-mini {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--cyan), var(--indigo));
  color: #050811;
  font-weight: 900;
  font-size: 14px;
  display: grid;
  place-items: center;
  box-shadow: 0 0 16px rgba(0, 229, 255, 0.35);
}

.brand-title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.3px;
}

.brand-sub {
  font-size: 10.5px;
  color: var(--muted);
}

.conn-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
}
.conn-dot.ok {
  background: var(--green);
  box-shadow: 0 0 10px var(--green);
}
.conn-dot.wait {
  background: var(--amber);
}

/* Main viewport */
.main {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.tab-view {
  display: none;
  width: 100%;
  height: 100%;
  flex-direction: column;
  overflow: hidden;
}

.tab-view.active {
  display: flex;
}

/* Bottom Navigation */
.bottom-nav {
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: rgba(11, 16, 29, 0.96);
  border-top: 1px solid var(--border);
  z-index: 30;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s;
}

.nav-item svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.9;
}

.nav-item.active {
  color: var(--cyan);
}

.nav-item.active svg {
  filter: drop-shadow(0 0 8px rgba(0, 229, 255, 0.6));
}

/* Immersive mode hides chrome */
body.immersive .topbar,
body.immersive .bottom-nav {
  display: none;
}

/* ========== TAB 1: TOUCHPAD ========== */
.quick-bar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding: 10px 12px 6px;
  flex-shrink: 0;
}

.qbtn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 9px 4px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

.qbtn.active {
  background: rgba(0, 229, 255, 0.16);
  border-color: var(--cyan);
  color: var(--cyan);
}

.qdrawer {
  display: none;
  margin: 4px 12px 8px;
  padding: 12px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.qdrawer.open { display: block; }

.btn-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.btn-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.btn-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }

.act-btn {
  padding: 11px 8px;
  border-radius: 12px;
  background: var(--surface2);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.act-btn:active { background: var(--indigo); transform: scale(0.96); }
.act-btn.danger { background: rgba(239, 68, 68, 0.18); border-color: rgba(239, 68, 68, 0.4); color: #fca5a5; }
.act-btn.primary { background: linear-gradient(135deg, var(--indigo), #4f46e5); border: none; color: #fff; }
.act-btn.cyan { background: rgba(0, 229, 255, 0.16); border-color: rgba(0, 229, 255, 0.45); color: var(--cyan); }

.kb-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.kb-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 10px 12px;
  color: #fff;
  font-size: 14px;
  outline: none;
}

.pad-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 10px;
  padding: 8px 12px;
}

.trackpad {
  flex: 1;
  border-radius: 22px;
  background:
    radial-gradient(circle at 50% 45%, rgba(99, 102, 241, 0.13), transparent 70%),
    linear-gradient(180deg, #101729 0%, #0b101d 100%);
  border: 1px solid var(--border);
  position: relative;
  touch-action: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.trackpad-hint {
  pointer-events: none;
  text-align: center;
  color: rgba(148, 163, 184, 0.55);
  font-size: 12.5px;
  line-height: 1.5;
}

.trackpad-ripple {
  position: absolute;
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0, 229, 255, 0.42), transparent 70%);
  transform: translate(-50%, -50%) scale(0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}

.scroll-strip {
  width: 48px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  touch-action: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  color: var(--muted);
  font-size: 12px;
}

.mouse-buttons {
  display: grid;
  grid-template-columns: 1fr 74px 1fr;
  gap: 10px;
  padding: 4px 12px 12px;
  flex-shrink: 0;
}

.mbtn {
  height: 54px;
  border-radius: 16px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  touch-action: none;
}
.mbtn.pressed {
  background: linear-gradient(135deg, var(--cyan), var(--indigo));
  color: #050811;
}

/* ========== TAB 2: GAMEPAD ========== */
.gp-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(15, 21, 36, 0.9);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}

.gp-toolbar select, .chip-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 7px 11px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.chip-btn.active {
  background: rgba(0, 229, 255, 0.18);
  border-color: var(--cyan);
  color: var(--cyan);
}

.gp-canvas {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  touch-action: none;
  background:
    radial-gradient(circle at 20% 50%, rgba(0, 229, 255, 0.08), transparent 45%),
    radial-gradient(circle at 80% 50%, rgba(99, 102, 241, 0.1), transparent 45%),
    #070a13;
}

.gp-canvas.theme-nes {
  background: linear-gradient(180deg, #1b0c10 0%, #0d0709 100%);
}
.gp-canvas.theme-cyber {
  background: radial-gradient(circle at 50% 50%, rgba(250, 204, 21, 0.08), #08070f 70%);
}
.gp-canvas.theme-racing {
  background: radial-gradient(circle at 50% 50%, rgba(16, 185, 129, 0.09), #070b12 70%);
}

.gp-item {
  position: absolute;
  transform: translate(-50%, -50%);
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  transition: transform 0.05s, box-shadow 0.08s;
}

.gp-btn {
  border-radius: 50%;
  background: rgba(23, 32, 54, 0.88);
  border: 2px solid rgba(148, 163, 184, 0.4);
  color: #fff;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.18);
  font-size: 14px;
  text-align: center;
  line-height: 1.1;
}
.gp-btn.pill {
  border-radius: 14px;
}
.gp-btn.pressed {
  transform: translate(-50%, -50%) scale(0.92);
  box-shadow: 0 0 22px var(--btn-glow, var(--cyan));
  border-color: var(--btn-glow, var(--cyan));
  background: var(--btn-glow, var(--cyan));
  color: #050811;
}

.gp-stick-base {
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.04) 0%, rgba(15, 23, 42, 0.85) 75%);
  border: 2px solid rgba(0, 229, 255, 0.35);
  box-shadow: inset 0 0 22px rgba(0, 0, 0, 0.65);
}

.gp-stick-knob {
  width: 44%;
  height: 44%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #38bdf8, #4f46e5);
  border: 2px solid rgba(255, 255, 255, 0.55);
  box-shadow: 0 4px 16px rgba(0, 229, 255, 0.4);
  pointer-events: none;
  position: relative;
}

.gp-aimpad {
  border-radius: 22px;
  background: rgba(15, 23, 42, 0.45);
  border: 1.5px dashed rgba(0, 229, 255, 0.35);
  color: rgba(148, 163, 184, 0.65);
  font-size: 11px;
}

.gp-wheel {
  border-radius: 50%;
  border: 8px solid rgba(0, 229, 255, 0.48);
  background: radial-gradient(circle, rgba(16, 185, 129, 0.14), rgba(15, 23, 42, 0.8));
  color: var(--cyan);
  font-size: 12px;
}

.gp-dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3px;
}

.gp-dpad-cell {
  border-radius: 8px;
  background: rgba(23, 32, 54, 0.92);
  border: 1.5px solid rgba(148, 163, 184, 0.4);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 13px;
  touch-action: none;
}
.gp-dpad-cell.pressed {
  background: var(--cyan);
  color: #050811;
}

.gp-item.editing {
  outline: 2px dashed #fbbf24;
  outline-offset: 3px;
}

.exit-immersive {
  display: none;
  position: fixed;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(6, 8, 15, 0.78);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11.5px;
  font-weight: 700;
  backdrop-filter: blur(8px);
}
body.immersive .exit-immersive { display: block; }

/* ========== TAB 3: REMOTE DESKTOP ========== */
.rd-toolbar {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 10px;
  background: rgba(15, 21, 36, 0.94);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}

.rd-stage {
  flex: 1;
  min-height: 0;
  position: relative;
  background: #020308;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
}

#rdCanvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  touch-action: none;
}

.rd-placeholder {
  position: absolute;
  text-align: center;
  padding: 24px;
  max-width: 340px;
}
.rd-placeholder h3 { font-size: 18px; margin-bottom: 8px; color: var(--cyan); }
.rd-placeholder p { font-size: 13px; color: var(--muted); line-height: 1.5; margin-bottom: 16px; }

.rd-overlay-gp {
  display: none;
  position: absolute;
  inset: 0;
  opacity: 0.62;
  pointer-events: auto;
}
.rd-overlay-gp.active { display: block; }

.rd-hud {
  position: absolute;
  top: 8px;
  right: 10px;
  background: rgba(6, 8, 15, 0.72);
  border: 1px solid var(--border);
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11px;
  color: var(--cyan);
  pointer-events: none;
}

/* ========== TAB 4: UTILITIES ========== */
.util-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.util-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 10px 12px 2px;
  flex-shrink: 0;
}

.ucard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px;
}

.ucard h3 {
  font-size: 13.5px;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--cyan);
  margin-bottom: 10px;
}

.file-path {
  font-family: Consolas, monospace;
  font-size: 12px;
  color: var(--cyan);
  background: var(--surface2);
  padding: 8px 10px;
  border-radius: 10px;
  margin-bottom: 10px;
  word-break: break-all;
}

.file-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 52vh;
  overflow-y: auto;
}

.file-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface2);
  cursor: pointer;
}

.file-meta {
  min-width: 0;
  flex: 1;
}
.file-name {
  font-size: 13.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-size {
  font-size: 11px;
  color: var(--muted);
}

.file-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.mini-btn {
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface3);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

#camVideo {
  width: 100%;
  max-height: 240px;
  border-radius: 14px;
  background: #000;
  object-fit: cover;
  margin-bottom: 10px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.stat-box {
  background: var(--surface2);
  border-radius: 12px;
  padding: 12px;
}
.stat-box span { font-size: 11px; color: var(--muted); }
.stat-box strong { display: block; font-size: 20px; margin-top: 4px; color: #fff; }

/* Modal / Connection Sheet */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(3, 5, 11, 0.78);
  backdrop-filter: blur(8px);
  z-index: 100;
  align-items: flex-end;
  justify-content: center;
}
.modal-backdrop.open { display: flex; }

.modal-sheet {
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  background: #0f1629;
  border: 1px solid var(--border);
  border-radius: 24px 24px 0 0;
  padding: 20px;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.discovered-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 10px 0 16px;
}

.pc-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface2);
  border: 1px solid rgba(0, 229, 255, 0.25);
  border-radius: 14px;
  padding: 12px 14px;
  cursor: pointer;
}

.toast {
  position: fixed;
  bottom: 82px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid var(--cyan);
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
