:root {
  --bg: #1a1a2e;
  --bg-card: #16213e;
  --bg-toolbar: #0f3460;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --text: #eee;
  --text-dim: #8899aa;
  --green: #4caf50;
  --gray: #666;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* --- Login page --- */
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 40px 30px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.login-card h1 {
  text-align: center;
  margin-bottom: 8px;
  font-size: 24px;
}

.login-card .subtitle {
  text-align: center;
  color: var(--text-dim);
  margin-bottom: 30px;
  font-size: 14px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--text-dim);
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #333;
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus {
  border-color: var(--accent);
}

.btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  touch-action: manipulation;
}

.btn:hover,
.btn:active {
  background: var(--accent-hover);
}

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

.login-error {
  background: #ff000020;
  color: #ff6b6b;
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 16px;
  font-size: 14px;
  text-align: center;
  display: none;
}

.login-error.visible {
  display: block;
}

/* --- Dashboard --- */
.dashboard {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 0 4px;
}

.dashboard-header h1 {
  font-size: 22px;
}

.btn-logout {
  background: transparent;
  border: 1px solid var(--text-dim);
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

.machines-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.machine-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px 20px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  border: 2px solid transparent;
  touch-action: manipulation;
  position: relative;
}

.machine-card__menu {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-dim);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  background: transparent;
  border: none;
}

.machine-card__menu:hover,
.machine-card__menu:active {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
}

.machine-card__menu-pop {
  position: absolute;
  top: 44px;
  right: 8px;
  background: var(--bg-card);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  min-width: 160px;
  z-index: 10;
  overflow: hidden;
}

.machine-card__menu-pop button {
  display: block;
  width: 100%;
  padding: 10px 14px;
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  font-size: 14px;
  cursor: pointer;
}

.machine-card__menu-pop button:hover {
  background: rgba(255, 255, 255, 0.06);
}

.machine-card__menu-pop button.danger {
  color: #ff6b6b;
}

.machine-card:active {
  transform: scale(0.97);
}

.machine-card.online {
  border-color: var(--green);
}

.machine-card.offline {
  opacity: 0.5;
}

.machine-card .name {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
}

.machine-card .status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-dim);
}

.machine-card .status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--gray);
}

.machine-card.online .status-dot {
  background: var(--green);
  box-shadow: 0 0 8px var(--green);
}

.machine-card .ip {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 8px;
  font-family: monospace;
}

.machine-card .heartbeat {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
}

.no-machines {
  text-align: center;
  color: var(--text-dim);
  padding: 60px 20px;
}

.no-machines p {
  margin-bottom: 8px;
}

/* --- Remote viewer --- */
/* Reserve a 48px strip at the bottom for the floating toolbar
   (☰ + ⛶ + monitor buttons). Both canvases inside (video + VNC-input)
   are 100%/100% of this container, so shrinking the container
   shrinks the remote image proportionally. Black letterbox bars fill
   the strip when aspect ratios differ. */
.viewer-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #000;
  overflow: hidden;
  touch-action: none;
}

/* Cursor policy: in desktop (mouse+kb) input mode the viewer container
   hides the native OS cursor by default — either the `LocalCursor` module
   (when the server-side cursor watcher is available) paints the remote
   cursor as a CSS `cursor: url(...)` on this same element at zero-latency,
   or (fallback) ffmpeg bakes the cursor into the video. Either way, we
   don't want the browser's default arrow on top of them. Not marked
   `!important` so `LocalCursor.enable()` can override via inline style. */
body.input-desktop .viewer-container {
  cursor: none;
}

.viewer-container canvas {
  image-rendering: auto;
  /* Inherit cursor from container so the local-cursor CSS rule set as
     inline style on .viewer-container also applies when the pointer is
     over the video/VNC canvas (which sits on top and would otherwise
     show its own `auto` arrow). */
  cursor: inherit;
}

.viewer-status {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 16px 32px;
  border-radius: 8px;
  font-size: 16px;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  z-index: 100;
}

.viewer-status.visible {
  opacity: 1;
}

/* --- Side menu (switch PCs) --- */
.side-menu {
  position: fixed;
  top: 0;
  left: -280px;
  width: 280px;
  height: 100vh;
  background: var(--bg-card);
  z-index: 200;
  transition: left 0.3s ease;
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.5);
  padding: 20px;
  overflow-y: auto;
}

.side-menu.open {
  left: 0;
}

.side-menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  z-index: 199;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.side-menu-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.side-menu h2 {
  font-size: 18px;
  margin-bottom: 16px;
}

.side-menu .machine-item {
  padding: 14px;
  border-radius: 8px;
  margin-bottom: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.2s;
}

.side-menu .machine-item:hover,
.side-menu .machine-item:active {
  background: var(--bg-toolbar);
}

.side-menu .machine-item.active {
  background: var(--accent);
}

.side-menu .machine-item .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gray);
  flex-shrink: 0;
}

.side-menu .machine-item.online .dot {
  background: var(--green);
}

.side-menu .btn-disconnect {
  margin-top: 20px;
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
}

/* --- Edge swipe indicator --- */
.edge-indicator {
  position: fixed;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  width: 6px;
  height: 60px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 0 4px 4px 0;
  z-index: 150;
  pointer-events: none;
}

/* --- Virtual keyboard --- */
.vkb {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 300;
}

.vkb-toolbar {
  display: flex;
  gap: 4px;
  padding: 6px 8px;
  background: var(--bg-toolbar);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.vkb-btn {
  padding: 8px 12px;
  border: 1px solid #444;
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  touch-action: manipulation;
  transition: background 0.15s;
}

.vkb-btn:active {
  background: var(--accent);
}

.vkb-btn.vkb-active {
  background: var(--accent);
  border-color: var(--accent);
}

.vkb-btn.vkb-mic.vkb-active {
  background: #e53935;
  border-color: #e53935;
  animation: mic-pulse 1.5s ease-in-out infinite;
}

@keyframes mic-pulse {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.6;
  }
}

/* --- Toast notification --- */
.vkb-toast {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-family: -apple-system, sans-serif;
  z-index: 10000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  max-width: 80vw;
  text-align: center;
  white-space: pre-line;
}

.vkb-toast.visible {
  opacity: 1;
}

.vkb-btn.vkb-sm {
  padding: 6px 8px;
  font-size: 12px;
}

.vkb-panel {
  background: var(--bg-toolbar);
  padding: 6px 8px;
  max-height: 60vh;
  overflow: auto;
  transition: max-height 0.2s, padding 0.2s;
}

.vkb-panel.vkb-hidden {
  max-height: 0;
  padding: 0 8px;
  overflow: hidden;
}

/* Free-position canvas: autosized by JS in normal mode */
.vkb-canvas {
  position: relative;
  width: 100%;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.vkb-canvas>.vkb-btn {
  margin: 0;
}

.vkb-row {
  display: flex;
  gap: 4px;
  margin-bottom: 4px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* --- Edit mode --- */
.vkb.vkb-edit {
  box-shadow: 0 0 0 2px var(--accent) inset;
  top: 0;
  /* grow to full viewport */
  display: flex;
  flex-direction: column;
  background: rgba(10, 14, 23, 0.97);
}

/* The toolbar remains at top of .vkb (first child). Panel fills remainder. */
.vkb.vkb-edit .vkb-toolbar {
  order: 2;
  /* move toolbar to bottom so it doesn't block the canvas */
  border-top: 1px solid var(--accent);
}

.vkb.vkb-edit .vkb-panel {
  order: 1;
  position: static;
  flex: 1 1 auto;
  max-height: none;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
}

.vkb.vkb-edit .vkb-canvas {
  flex: 1 1 auto;
  min-height: 40vh;
  background-image: radial-gradient(circle, rgba(128, 140, 170, 0.22) 1px, transparent 1px);
  background-size: 8px 8px;
  background-position: 0 0;
  border: 1px dashed #334;
  border-radius: 6px;
}

.vkb.vkb-edit .vkb-btn.vkb-draggable {
  cursor: grab;
  border-style: dashed;
  touch-action: none;
  /* prevent scroll while dragging */
}

.vkb-btn.vkb-dragging {
  opacity: 0.4;
}

.vkb-drop-hover {
  background: rgba(233, 69, 96, 0.15) !important;
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}

.vkb-toolbar-add {
  border: 1px dashed #4a7b4a !important;
  color: #7cc67c !important;
  background: rgba(76, 175, 80, 0.08) !important;
  font-weight: bold;
}

.vkb-toolbar-add:hover {
  background: rgba(76, 175, 80, 0.2) !important;
}

.vkb-trash {
  margin-top: 8px;
  padding: 14px;
  border: 2px dashed #c62828;
  border-radius: 8px;
  text-align: center;
  color: #ff8a80;
  font-size: 13px;
  background: rgba(198, 40, 40, 0.08);
}

.vkb-edit-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.vkb-canvas-add {
  border: 1px dashed #4a7b4a !important;
  color: #7cc67c !important;
  background: rgba(76, 175, 80, 0.08) !important;
}

.vkb-edit-hint {
  flex: 1 1 100%;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.4;
}

.vkb-btn.vkb-reset {
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 11px;
  padding: 4px 10px;
}

/* --- Mode toggle --- */
.mode-toggle {
  position: fixed;
  top: 10px;
  right: 10px;
  z-index: 250;
  background: rgba(15, 52, 96, 0.75);
  border-radius: 8px;
  padding: 4px;
  display: flex;
  gap: 2px;
  backdrop-filter: blur(6px);
}

.mode-toggle button {
  padding: 6px 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
}

.mode-toggle button.active {
  background: var(--accent);
  color: white;
}

/* --- Loading spinner --- */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--text-dim);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- Responsive --- */
/* --- Debug panel (tap version badge to toggle) --- */
.debug-panel {
  position: fixed;
  bottom: 24px;
  right: 8px;
  max-width: 90vw;
  max-height: 40vh;
  background: rgba(0, 0, 0, 0.85);
  color: #0f0;
  font-family: monospace;
  font-size: 11px;
  padding: 8px;
  border-radius: 8px;
  overflow-y: auto;
  z-index: 10000;
  display: none;
  white-space: pre-wrap;
  word-break: break-all;
}

.debug-panel.visible {
  display: block;
}

/* --- Version badge --- */
.version-badge {
  position: fixed;
  bottom: 6px;
  right: 8px;
  font-size: 10px;
  color: var(--text-dim);
  opacity: 0.5;
  z-index: 9999;
  pointer-events: all;
  cursor: pointer;
  font-family: monospace;
}

/* --- Fullscreen button --- */
.fullscreen-btn {
  position: fixed;
  bottom: 4px;
  right: 60px;
  z-index: 9999;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: var(--text-dim);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 11px;
  cursor: pointer;
  pointer-events: all;
  touch-action: manipulation;
  opacity: 0.6;
  font-family: monospace;
}

.fullscreen-btn:active {
  opacity: 1;
  background: rgba(255, 255, 255, 0.2);
}

@media (max-width: 500px) {
  .dashboard {
    padding: 12px;
  }

  .machines-grid {
    grid-template-columns: 1fr;
  }

  .login-card {
    padding: 30px 20px;
  }
}