*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%; height: 100%;
  background: #0a0a19;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  color: #fff;
  touch-action: none;
}

/* ── LOGIN GATE ──────────────────────────────────────────────── */
#login-overlay {
  position: fixed; inset: 0; z-index: 1000;
  display: none; align-items: center; justify-content: center;
  background: #0a0a19;
}
#login-form {
  display: flex; flex-direction: column; gap: 12px;
  width: 260px; padding: 24px;
}
#login-title {
  font-size: 20px; font-weight: 600; text-align: center; margin-bottom: 8px;
}
#login-token {
  background: #14142c; border: 1px solid #2a2a4a; border-radius: 8px;
  color: #fff; font-size: 16px; padding: 12px;
}
#login-form button[type="submit"] {
  background: #3b6bff; border: none; border-radius: 8px;
  color: #fff; font-size: 16px; padding: 12px; cursor: pointer;
}
#login-error {
  color: #ff6b6b; font-size: 13px; text-align: center;
}

/* ── SCREENS ─────────────────────────────────────────────────── */
.screen {
  position: fixed; inset: 0;
  display: none;
  flex-direction: column;
}
.screen.active { display: flex; }

/* ── SCREEN 1: WINDOW LIST ───────────────────────────────────── */
#screen-list {
  background: #0a0a19;
  overflow: hidden;
}

#list-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 12px;
  padding-top: max(16px, env(safe-area-inset-top));
  background: rgba(15,15,35,0.98);
  border-bottom: 1px solid rgba(255,255,255,0.07);
  flex-shrink: 0;
}

#list-title {
  font-size: 18px; font-weight: 700;
  letter-spacing: 0.04em;
  color: #f1f5f9;
}

#list-refresh-btn {
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  color: #aaa; font-size: 20px;
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#list-refresh-btn:active { background: rgba(255,255,255,0.15); color: #fff; }

#windows-grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-content: start;
}

.window-card {
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.15s, background 0.15s;
}
.window-card:active { background: rgba(233,69,96,0.15); border-color: #e94560; }
.window-card.active { border-color: #e94560; background: rgba(233,69,96,0.12); }

.window-card-thumb {
  width: 100%; aspect-ratio: 16/9;
  object-fit: contain;
  background: #111;
  display: block;
}

.window-card-title {
  font-size: 12px; color: #ccc;
  padding: 8px 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ── SCREEN 2: STREAM ────────────────────────────────────────── */
#screen-stream {
  background: #000;
}

#stream-container {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

#stream-img,
#stream-video {
  width: 100%; height: 100%;
  object-fit: contain;
  transform-origin: center center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  background: #000;
  pointer-events: none;
}

/* Fullscreen: fill entire screen */
#screen-stream:fullscreen,
#screen-stream:-webkit-full-screen {
  background: #000;
  width: 100vw; height: 100vh;
}
#screen-stream:fullscreen #stream-container,
#screen-stream:-webkit-full-screen #stream-container {
  position: absolute; inset: 0;
}
#screen-stream:fullscreen #stream-img,
#screen-stream:fullscreen #stream-video,
#screen-stream:-webkit-full-screen #stream-img,
#screen-stream:-webkit-full-screen #stream-video {
  width: 100%; height: 100%;
  object-fit: contain;
}

/* ── RIGHT TOOLBAR ──────────────────────────────────────────── */
#right-toolbar {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 52px;
  background: linear-gradient(180deg, rgba(16,24,46,0.72), rgba(10,14,30,0.82));
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border-left: 1px solid rgba(255,255,255,0.08);
  box-shadow: -8px 0 24px rgba(0,0,0,0.28);
  display: flex; flex-direction: column;
  align-items: center;
  padding: max(12px, env(safe-area-inset-top)) 0 max(24px, env(safe-area-inset-bottom));
  gap: 10px;
  z-index: 40;
  touch-action: none;   /* allow vertical swipe → instance switch */
}

.hud-pill {
  background: rgba(0,0,0,0.42);
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 999px;
  padding: 5px 4px;
  font-size: 9px; font-weight: 600;
  letter-spacing: 0.02em;
  color: #d4d7e0;
  text-align: center;
  width: 42px;
  pointer-events: none;
}
/* Network status dot (green good / yellow connecting / red down) */
.net-status {
  width: 42px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  pointer-events: none;
}
.net-dot {
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #6b7280;              /* neutral until first state */
  box-shadow: 0 0 0 0 rgba(0,0,0,0);
  transition: background 0.2s ease, box-shadow 0.2s ease;
}
.net-good .net-dot {
  background: #22c55e;
  box-shadow: 0 0 8px 1px rgba(34,197,94,0.6);
}
.net-warn .net-dot {
  background: #eab308;
  box-shadow: 0 0 8px 1px rgba(234,179,8,0.55);
  animation: net-pulse 1.1s ease-in-out infinite;
}
.net-bad .net-dot {
  background: #ef4444;
  box-shadow: 0 0 8px 1px rgba(239,68,68,0.6);
}
@keyframes net-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.35; }
}

.rt-spacer { flex: 1; }

.rt-btn {
  width: 42px; height: 42px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 13px;
  color: #eef1f8; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
  transition: background 0.12s ease, transform 0.08s ease, border-color 0.12s ease;
}
.rt-btn svg { width: 21px; height: 21px; display: block; }
.rt-btn:hover { background: rgba(255,255,255,0.10); }
#list-refresh-btn svg { width: 20px; height: 20px; }
.rt-btn:active {
  background: rgba(233,69,96,0.85);
  border-color: rgba(233,69,96,0.9);
  transform: scale(0.92);
}
/* While swiping the toolbar, don't flash button press states. */
#right-toolbar.rt-swiping .rt-btn,
#right-toolbar.rt-swiping .rt-btn:active {
  background: rgba(255,255,255,0.06);
  border-color: rgba(255,255,255,0.12);
  transform: none;
}
/* ── SETTINGS POPUP ─────────────────────────────────────────── */
#settings-overlay {
  position: fixed; inset: 0;
  z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(3px);
}
#settings-panel {
  width: min(340px, 86vw);
  padding: 20px;
  background: rgba(20,22,30,0.98);
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 18px;
  display: flex; flex-direction: column; gap: 12px;
}
.settings-title { font-size: 19px; font-weight: 700; color: #fff; }
.settings-section { font-size: 14px; font-weight: 600; color: #cfd4e2; margin-top: 4px; }
.settings-hint { font-size: 12px; color: #8b90a0; line-height: 1.4; }
#quality-opts { display: flex; flex-wrap: wrap; gap: 8px; }
.q-opt {
  flex: 1 1 auto; min-width: 60px;
  padding: 10px 12px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 10px;
  color: #eef1f8; font-size: 15px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.q-opt.sel { background: rgba(233,69,96,0.85); border-color: rgba(233,69,96,0.9); color: #fff; }
.q-opt:active { transform: scale(0.95); }
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 15px; color: #eef1f8;
  padding: 4px 0;
}
.settings-row input[type="checkbox"] { width: 20px; height: 20px; }
#settings-close {
  margin-top: 6px;
  padding: 12px;
  background: rgba(233,69,96,0.85);
  border: none; border-radius: 11px;
  color: #fff; font-size: 16px; font-weight: 600;
  cursor: pointer;
}

/* ── QUICK-SWITCH DRAWER ────────────────────────────────────── */
#switch-drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(300px, 78vw);
  z-index: 70;
  background: rgba(16,18,26,0.98);
  border-right: 1px solid rgba(255,255,255,0.12);
  display: flex; flex-direction: column;
  transform: translateX(0);
  transition: transform 0.22s cubic-bezier(0.4,0,0.2,1);
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
}
#switch-drawer.closed { transform: translateX(-105%); }
#switch-drawer-header {
  padding: 16px 18px 10px;
  font-size: 18px; font-weight: 700; color: #fff;
}
#switch-drawer-list {
  flex: 1; overflow-y: auto;
  padding: 4px 10px 14px;
  display: flex; flex-direction: column; gap: 6px;
}
.switch-row {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 13px 14px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 12px;
  color: #eef1f8; font-size: 16px; text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.switch-row.active {
  background: rgba(233,69,96,0.85);
  border-color: rgba(233,69,96,0.9);
  color: #fff;
}
.switch-row:active { transform: scale(0.98); }
.switch-row-ico { width: 20px; height: 20px; flex-shrink: 0; opacity: 0.85; }
.switch-row-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#switch-drawer-scrim {
  position: fixed; inset: 0;
  z-index: 65;
  background: rgba(0,0,0,0.45);
}

/* ── STATS OVERLAY ──────────────────────────────────────────── */
#stats-overlay {
  position: absolute;
  top: 10px; left: 10px;
  z-index: 35;
  padding: 7px 10px;
  background: rgba(0,0,0,0.62);
  color: #7fffa0;
  font: 600 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  border-radius: 9px;
  pointer-events: none;
  white-space: nowrap;
}

/* ── KEYBOARD INPUT ─────────────────────────────────────────── */
#keyboard-input {
  position: fixed; bottom: 0; left: 0; right: 0;
  opacity: 0; height: 1px;
  pointer-events: none;
}

/* ── UNAVAILABLE OVERLAY ────────────────────────────────────── */
#unavailable-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.85);
  display: none;
  flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px;
  z-index: 60;
}
#unavailable-overlay.show { display: flex; }
#unavailable-overlay p { color: #aaa; font-size: 15px; }

#reconnect-btn {
  background: #0f3460; color: #fff;
  border: none; border-radius: 8px;
  padding: 12px 32px; font-size: 15px;
  cursor: pointer;
}
#reconnect-btn:active { background: #e94560; }
