:root {
  --bg: #0b0e14;
  --surface: #131924;
  --surface-2: #1b2433;
  --surface-3: #243044;
  --border: #2a364c;
  --text: #edf2f7;
  --muted: #93a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --good: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 10px;
  --topbar-h: 46px;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

/* Buttons & Form Inputs */
.btn, .tb-btn, .tb-select, .seg-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 7px;
  font: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 7px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  white-space: nowrap;
  user-select: none;
  transition: background 0.12s, border-color 0.12s;
}

.btn:hover, .tb-btn:hover { background: var(--surface-3); border-color: #3d4f6e; }
.btn.primary, .tb-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: rgba(239, 68, 68, 0.15); border-color: rgba(239, 68, 68, 0.45); color: #fca5a5; }
.btn.small { padding: 7px 11px; font-size: 12.5px; }
.btn.tiny { padding: 5px 9px; font-size: 12px; }
.btn.full, .tb-select.full { width: 100%; }

.tb-select {
  padding: 6px 8px;
  max-width: 190px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--muted);
}

.field input, textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
  font-size: 15px;
  outline: none;
}

.field input:focus, textarea:focus {
  border-color: var(--accent);
}

/* Auth Screen */
.auth-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #172033 0%, #0b0e14 65%);
  overflow-y: auto;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.brand-badge {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #6366f1);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
}

.auth-brand h1 { margin: 0; font-size: 20px; }
.auth-brand p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.auth-hint { font-size: 13px; color: var(--muted); line-height: 1.45; margin: 0 0 14px; }

.alert-banner {
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(245, 158, 11, 0.14);
  border: 1px solid rgba(245, 158, 11, 0.45);
  color: #fde68a;
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.4;
}
.alert-banner.error {
  background: rgba(239, 68, 68, 0.14);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

.agent-pill {
  margin-top: 14px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* Main Workspace */
.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg);
}

/* Permanent Top Control Bar */
.topbar {
  flex: 0 0 auto;
  min-height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 10px;
  z-index: 40;
  overflow-x: auto;
  scrollbar-width: none;
}
.topbar::-webkit-scrollbar { display: none; }

.tb-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot.online { background: var(--good); box-shadow: 0 0 8px var(--good); }
.status-dot.relay { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.status-dot.offline { background: var(--danger); }

.status-pill {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.seg-control {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 5px 9px;
  border-radius: 6px;
  font-size: 12px;
}
.seg-btn.active {
  background: var(--accent);
  color: #fff;
}

.tb-btn { position: relative; }
.dot-badge {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warn);
}

.label-short { display: none; }
.touch-only { display: none; }

@media (hover: none) and (pointer: coarse) {
  .touch-only { display: inline-flex; }
}

/* Overflow Menu Drawer */
.menu-drawer {
  flex: 0 0 auto;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px;
  z-index: 35;
  max-height: 60dvh;
  overflow-y: auto;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}

.menu-section {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-label {
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.menu-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.menu-row.end { justify-content: flex-end; margin-top: 12px; }

.stats-readout {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Desktop Stage */
.stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

.viewport {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  background: #000;
  overflow: hidden;
  outline: none;
  touch-action: none;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-layer {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: center center;
}

.viewport.scale-native {
  overflow: auto;
}

#remote-video, #remote-canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.viewport.scale-native #remote-video,
.viewport.scale-native #remote-canvas {
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
}

.touch-ripple {
  position: absolute;
  width: 36px;
  height: 36px;
  margin-left: -18px;
  margin-top: -18px;
  border-radius: 50%;
  border: 2px solid rgba(59, 130, 246, 0.9);
  background: rgba(59, 130, 246, 0.25);
  pointer-events: none;
  animation: ripple-out 0.35s ease-out forwards;
}

@keyframes ripple-out {
  from { transform: scale(0.5); opacity: 1; }
  to { transform: scale(1.4); opacity: 0; }
}

.drop-overlay, .offline-banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 14, 20, 0.72);
  backdrop-filter: blur(4px);
  z-index: 15;
  pointer-events: none;
}

.drop-box {
  border: 2px dashed var(--accent);
  background: var(--surface);
  padding: 20px 28px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
}

.offline-banner span {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
}

/* Side Drawers (Clipboard & File Manager) */
.side-drawer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 100%);
  background: var(--surface);
  border-left: 1px solid var(--border);
  z-index: 25;
  display: flex;
  flex-direction: column;
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.45);
}
.side-drawer.wide { width: min(480px, 100%); }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.drawer-header h2 { margin: 0; font-size: 15px; }
.icon-close {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
}
.icon-close:hover { color: var(--text); }

.drawer-body {
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1 1 auto;
}

/* File Manager */
.files-toolbar {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.files-quick, .files-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
.files-path {
  padding: 7px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  white-space: nowrap;
}
.files-dropzone {
  margin: 8px 12px 0;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}
.files-dropzone.dragover {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.1);
  color: var(--text);
}
.files-transfers {
  padding: 4px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.transfer-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 6px 8px;
  font-size: 12px;
}
.transfer-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}
.progress-track {
  height: 5px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.1s;
}

.files-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 6px 12px 16px;
}
.file-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 6px;
  border-bottom: 1px solid rgba(42, 54, 76, 0.45);
}
.file-main {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 auto;
  cursor: pointer;
}
.file-icon { flex-shrink: 0; font-size: 15px; }
.file-name {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-meta {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}
.file-btns {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

/* On-Screen Keyboard Dock */
.osk-dock {
  flex: 0 0 auto;
  background: #0e131c;
  border-top: 1px solid var(--border);
  padding: 6px;
  user-select: none;
  z-index: 30;
  max-height: 46dvh;
  overflow-y: auto;
}

.osk-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}

.osk-tabs {
  display: flex;
  gap: 4px;
}

.osk-text-bar {
  display: flex;
  gap: 6px;
  flex: 1 1 200px;
  max-width: 420px;
}
.osk-text-input {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
}

.osk-rows {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.osk-row {
  display: flex;
  gap: 4px;
  width: 100%;
}

.osk-key {
  flex: 1 1 0;
  min-width: 0;
  height: 36px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 6px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.05;
  padding: 2px;
  cursor: pointer;
  touch-action: manipulation;
}
.osk-key:active { background: var(--surface-3); transform: scale(0.97); }
.osk-key .sub { font-size: 9px; color: var(--muted); }
.osk-key.latched { background: var(--accent); border-color: #60a5fa; color: #fff; }
.osk-key.locked { background: #b45309; border-color: var(--warn); color: #fff; }
.osk-key.held { background: #1d4ed8; border-color: #93c5fd; color: #fff; }
.osk-key.w15 { flex-grow: 1.5; }
.osk-key.w20 { flex-grow: 2; }
.osk-key.w25 { flex-grow: 2.5; }
.osk-key.w60 { flex-grow: 6; }

.osk-combo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 6px;
}

/* Modal & Toast */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}
.modal-card h3 { margin: 0 0 14px; font-size: 17px; }

.toast-container {
  position: fixed;
  bottom: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 70;
  pointer-events: none;
}
.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

/* Responsive Breakpoints */
@media (max-width: 960px) {
  .desktop-only { display: none !important; }
}

@media (max-width: 640px) {
  .label-long { display: none; }
  .label-short { display: inline; }
  .topbar { padding: 4px 6px; gap: 4px; }
  .tb-btn { padding: 6px 8px; font-size: 12px; }
  .osk-key { height: 38px; font-size: 11px; }
}
