:root {
  --bg: #0e1117;
  --surface: #161b24;
  --surface-2: #1e2532;
  --border: rgba(255, 255, 255, 0.09);
  --text: #f3f6fc;
  --muted: #93a1b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --glass: rgba(20, 25, 36, 0.82);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { 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, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}

button, a {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

/* ---------- Màn danh sách / đăng nhập ---------- */

.lobby {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: rgba(14, 17, 23, 0.9);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.01em;
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 15px;
  color: #fff;
}

.brand-sub {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 4px;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 60%;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  flex: 0 0 auto;
}

.user-email {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.14s ease, border-color 0.14s ease, opacity 0.14s ease;
}

.btn:hover { background: #263042; }
.btn:active { opacity: 0.85; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: 13px;
  border-radius: 8px;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.16);
  border-color: rgba(239, 68, 68, 0.35);
  color: #fca5a5;
}

.container {
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 24px 18px 40px;
  flex: 1 0 auto;
}

/* ---------- Thẻ đăng nhập ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}

.auth-card {
  text-align: center;
  max-width: 440px;
  margin: 36px auto 0;
  padding: 32px 24px;
}

.auth-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 16px;
  background: rgba(59, 130, 246, 0.14);
  color: var(--accent);
  display: grid;
  place-items: center;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.auth-card p {
  margin: 0 0 22px;
  font-size: 14.5px;
  line-height: 1.55;
  color: var(--muted);
}

.auth-card .btn-primary {
  width: 100%;
  min-height: 48px;
  font-size: 15px;
}

.auth-note {
  margin-top: 16px !important;
  margin-bottom: 0 !important;
  font-size: 12.5px !important;
}

/* ---------- Danh sách thiết bị ---------- */

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

.section-head h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: -0.015em;
}

.signal-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18); }
.dot.warn { background: var(--warn); }
.dot.error { background: var(--danger); }

.notice {
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 13.5px;
  line-height: 1.5;
  margin-bottom: 16px;
  background: rgba(59, 130, 246, 0.12);
  border: 1px solid rgba(59, 130, 246, 0.25);
  color: #dbeafe;
}

.notice.is-error {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.3);
  color: #fecaca;
}

.device-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.device-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}

.device-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.device-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.device-row.is-online .device-icon {
  background: rgba(59, 130, 246, 0.14);
  color: var(--accent);
}

.device-meta {
  min-width: 0;
}

.device-name-line {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-width: 0;
}

.device-name {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.device-rename-btn {
  min-width: 34px;
  min-height: 34px;
  padding: 0 7px;
  flex: 0 0 auto;
}

.device-sub {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 3px;
  font-size: 12.5px;
  color: var(--muted);
}

.empty-state {
  text-align: center;
  padding: 36px 20px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

.empty-state strong {
  display: block;
  color: var(--text);
  font-size: 16px;
  margin-bottom: 6px;
}

.remote-tools {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.connect-card h2,
.recent-heading h2,
.dialog-card h2 {
  margin: 0;
  font-size: 16px;
  line-height: 1.35;
}

.web-field {
  display: grid;
  gap: 6px;
  min-width: 0;
  margin-top: 14px;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 600;
}

.web-field input,
.web-field textarea {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 400;
}

.web-field textarea {
  min-height: 92px;
  resize: vertical;
  line-height: 1.45;
}

.web-field input:focus,
.web-field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.password-input-wrap {
  display: flex;
  align-items: stretch;
  gap: 6px;
  min-width: 0;
}

.password-input-wrap input { flex: 1; }

.form-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 12px;
}

.form-note {
  min-height: 18px;
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.form-note.is-error { color: #fca5a5; }

.recent-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.recent-actions,
.recent-row-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.recent-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.recent-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 8px 0;
  border-top: 1px solid var(--border);
}

.recent-row-copy { min-width: 0; }
.recent-row-copy strong,
.recent-row-copy small { display: block; overflow-wrap: anywhere; }
.recent-row-copy strong { font-size: 13px; }
.recent-row-copy small { margin-top: 3px; color: var(--muted); font-size: 11.5px; }

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.68);
}

.dialog-card {
  width: min(420px, 100%);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
}

.dialog-actions { justify-content: flex-end; }

/* ---------- Màn điều khiển toàn màn hình ---------- */

.viewer {
  position: fixed;
  inset: 0;
  background: #000;
  display: flex;
  flex-direction: column;
  z-index: 50;
}

.stage {
  position: relative;
  flex: 1 1 0;
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  place-items: center;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#remoteCanvas {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  display: block;
  background: #05070b;
  transform-origin: center center;
  will-change: transform;
}

.virtual-cursor {
  position: absolute;
  top: 0;
  left: 0;
  width: 22px;
  height: 22px;
  pointer-events: none;
  z-index: 20;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.85));
}

.ripple {
  position: fixed;
  width: 38px;
  height: 38px;
  margin: -19px 0 0 -19px;
  border-radius: 50%;
  border: 2px solid rgba(59, 130, 246, 0.9);
  background: rgba(59, 130, 246, 0.22);
  pointer-events: none;
  z-index: 80;
  animation: ripple-out 0.35s ease-out forwards;
}

.ripple.is-right {
  border-color: rgba(245, 158, 11, 0.95);
  background: rgba(245, 158, 11, 0.24);
}

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

/* ---------- Thanh công cụ nổi (glass pill) ---------- */

.toolbar-wrap {
  position: fixed;
  top: calc(var(--safe-top) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100vw - 20px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 6px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 38px;
  height: 38px;
  padding: 0 10px;
  border-radius: 999px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.tb-btn:hover { background: rgba(255, 255, 255, 0.1); }
.tb-btn.is-active { background: rgba(59, 130, 246, 0.28); color: #93c5fd; }
.tb-btn.is-danger { color: #fca5a5; }
.tb-btn.is-danger:hover { background: rgba(239, 68, 68, 0.2); }

.tb-peer {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  font-size: 13px;
  font-weight: 600;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tb-handle {
  width: 56px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: var(--glass);
  backdrop-filter: blur(12px);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.tb-handle::after {
  content: "";
  width: 24px;
  height: 3px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.55);
}

.more-menu {
  background: rgba(22, 27, 36, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 14px;
  padding: 8px;
  min-width: 220px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.55);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.clipboard-panel {
  display: grid;
  gap: 8px;
  width: min(420px, calc(100vw - 20px));
  max-height: min(72vh, 620px);
  overflow: auto;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  background: rgba(22, 27, 36, 0.98);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.55);
  overscroll-behavior: contain;
}

.clipboard-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.clipboard-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.clipboard-actions .btn { min-height: 38px; }

.remote-clipboard {
  display: grid;
  gap: 8px;
  min-width: 0;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.remote-clipboard p {
  max-height: 110px;
  margin: 0;
  overflow: auto;
  color: var(--text);
  font-size: 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.remote-clipboard img {
  display: block;
  max-width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 6px;
  background: #000;
}

.more-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding: 6px 10px 2px;
}

.more-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 40px;
  padding: 0 12px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
  text-align: left;
}

.more-item:hover { background: rgba(255, 255, 255, 0.08); }
.more-item.is-selected { color: #93c5fd; font-weight: 600; }

/* ---------- Thanh phím phụ cho bàn phím ảo mobile ---------- */

.kbd-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--safe-bottom);
  z-index: 65;
  background: rgba(20, 25, 36, 0.96);
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(14px);
  padding: 6px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Phím phụ cuộn ngang; Tiếp/Enter/Ẩn luôn ghim bên phải để bấm được ngay trên màn hẹp. */
.kbd-keys {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.kbd-keys::-webkit-scrollbar { display: none; }

.kbd-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding-left: 6px;
  border-left: 1px solid rgba(255, 255, 255, 0.12);
}

.kbd-key {
  flex: 0 0 auto;
  min-width: 44px;
  height: 40px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.kbd-key:active { background: rgba(255, 255, 255, 0.16); }
.kbd-key.is-icon { display: inline-grid; place-items: center; padding: 0; }
.kbd-key.is-primary { background: var(--accent); border-color: transparent; color: #fff; }
.kbd-key.is-primary:active { background: var(--accent-hover); }

.kbd-key.is-on {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

/* Ô nhận chữ từ bàn phím ảo: ngoài khung nhìn, trong suốt, không chặn chạm. */
.hidden-kbd-input {
  position: fixed;
  top: -1000px;
  left: -1000px;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  margin: 0;
  resize: none;
  overflow: hidden;
  opacity: 0;
  color: transparent;
  background: transparent;
  caret-color: transparent;
  pointer-events: none;
  font-size: 16px; /* < 16px làm iOS tự phóng to trang khi focus */
}

/* ---------- Lớp phủ trạng thái trong phiên ---------- */

.status-overlay {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(8, 10, 15, 0.78);
  backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  padding: 20px;
}

.status-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 28px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.14);
  border-top-color: var(--accent);
  margin: 0 auto 14px;
  animation: spin 0.8s linear infinite;
}

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

.status-box p {
  margin: 0 0 18px;
  font-size: 14.5px;
  color: var(--muted);
  line-height: 1.5;
}

.status-banner {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 14px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 58;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-size: 13px;
  color: var(--text);
  pointer-events: none;
  white-space: nowrap;
}

@media (max-width: 560px) {
  .tb-label { display: none; }
  .tb-peer { max-width: 110px; }
  .user-email { display: none; }
  .remote-tools { grid-template-columns: minmax(0, 1fr); }
  .device-row { padding: 10px; gap: 8px; }
  .device-row > .btn { flex: 0 0 auto; }
  .recent-row { grid-template-columns: minmax(0, 1fr); }
  .recent-row-actions .btn { flex: 1 1 auto; }
  .dialog-card { padding: 16px; }
}
