:root {
  --bg: #101214;
  --surface: #181b1f;
  --surface-2: #20242a;
  --surface-3: #292e35;
  --line: #343a42;
  --line-soft: #282d33;
  --text: #f2f4f7;
  --muted: #9ca5b0;
  --muted-2: #747d88;
  --primary: #0f9d8a;
  --primary-hover: #12b39d;
  --primary-soft: rgba(15, 157, 138, 0.14);
  --blue: #4f8cff;
  --danger: #e05252;
  --danger-hover: #f06060;
  --ok: #36b875;
  --warning: #e4a340;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  font-size: 14px;
}
button, input { font: inherit; letter-spacing: 0; }
button { -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }

.top {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px max(18px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line-soft);
  background: rgba(16, 18, 20, 0.96);
  backdrop-filter: blur(12px);
}
.brand-block { min-width: 0; }
.brand { font-size: 18px; font-weight: 720; line-height: 1.2; }
.connection-line {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 18px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}
.connection-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warning);
  box-shadow: 0 0 0 3px rgba(228, 163, 64, 0.12);
}
body.server-ready .connection-dot {
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(54, 184, 117, 0.12);
}
.top-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.account-badge {
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 24px 20px 44px;
}
.workspace-grid {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(320px, 0.86fr) minmax(380px, 1.14fr);
  gap: 18px;
  align-items: start;
}
.tool-panel {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
}
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 38px;
  margin-bottom: 18px;
}
.section-head h2 { margin: 0; font-size: 17px; line-height: 1.25; }
.section-meta { margin-top: 4px; color: var(--muted); font-size: 12px; }
.method-tag {
  flex: 0 0 auto;
  padding: 4px 8px;
  border: 1px solid rgba(79, 140, 255, 0.3);
  border-radius: 6px;
  color: #9bbcff;
  background: rgba(79, 140, 255, 0.1);
  font-size: 11px;
}

.notice {
  margin-bottom: 14px;
  padding: 11px 13px;
  border: 1px solid rgba(54, 184, 117, 0.35);
  border-radius: 6px;
  background: rgba(54, 184, 117, 0.1);
  color: #9be0bb;
  font-size: 13px;
}
.notice.error {
  border-color: rgba(224, 82, 82, 0.42);
  background: rgba(224, 82, 82, 0.1);
  color: #ffaaaa;
}

.form-stack { display: grid; gap: 14px; min-width: 0; }
.field { display: grid; gap: 7px; width: 100%; max-width: 100%; min-width: 0; color: var(--muted); font-size: 12px; }
input {
  width: 100%;
  min-width: 0;
  height: 42px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  background: #121417;
  color: var(--text);
}
input::placeholder { color: #626b75; }
input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.input-action-wrap { position: relative; display: block; }
.input-action-wrap input { padding-right: 44px; }
.input-icon { position: absolute; top: 4px; right: 4px; }

.captcha-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(88px, 116px) 36px;
  gap: 8px;
  align-items: center;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}
.captcha {
  display: block;
  min-width: 0;
  width: 116px;
  height: 42px;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
}
.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}
.check-row input { width: 16px; height: 16px; accent-color: var(--primary); }

.btn, .icon-btn, .control-btn {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: var(--surface-2);
  cursor: pointer;
}
.btn { min-height: 40px; padding: 8px 13px; }
.btn:hover, .icon-btn:hover, .control-btn:hover { background: var(--surface-3); }
.btn:disabled, .icon-btn:disabled, .control-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:focus-visible, .icon-btn:focus-visible, .control-btn:focus-visible,
.device-item:focus-visible, input:focus-visible {
  outline: 2px solid var(--primary-hover);
  outline-offset: 2px;
}
.btn.primary { border-color: var(--primary); background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.secondary { border-color: #506078; background: #354154; }
.btn.secondary:hover { background: #42516a; }
.btn.quiet { min-height: 34px; background: transparent; color: var(--muted); }
.btn.command {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 650;
}
.btn.compact { min-height: 36px; padding: 6px 12px; }
.btn-icon { font-size: 15px; }
.icon-btn {
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  line-height: 1;
}
.captcha-refresh { width: 36px; height: 42px; font-size: 18px; }

.account-line {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line-soft);
}
.account-mark {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #b8efd0;
  background: rgba(54, 184, 117, 0.15);
}
.account-name { font-weight: 650; }
.account-caption { margin-top: 2px; color: var(--muted); font-size: 11px; }
.device-area { display: grid; gap: 12px; }
.device-list { display: grid; gap: 8px; }
.device-empty {
  padding: 28px 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}
.device-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 66px;
  padding: 11px 12px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #14171a;
}
.device-item.online-device { cursor: pointer; }
.device-item.online-device:hover { border-color: rgba(15, 157, 138, 0.55); background: #171c1f; }
.device-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 650; }
.device-meta { margin-top: 5px; color: var(--muted); font-size: 11px; }
.device-actions { display: flex; align-items: center; gap: 9px; }
.device-state { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; }
.device-state::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted-2); }
.device-state.online { color: #80d9aa; }
.device-state.online::before { background: var(--ok); }
.device-connect { min-height: 34px; padding: 5px 10px; border-color: rgba(15, 157, 138, 0.45); color: #a5e7dc; }

.control-page { width: 100%; height: calc(100vh - 64px); }
.screen-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 420px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  touch-action: none;
  user-select: none;
}
.screen-wrap:fullscreen { width: 100vw; height: 100vh; height: 100dvh; }
.remote-surface {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  cursor: crosshair;
}
.screen-empty {
  position: absolute;
  display: flex;
  align-items: center;
  gap: 9px;
  color: #a9b0b8;
  font-size: 13px;
  pointer-events: none;
}
.loading-ring {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: var(--primary-hover);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.float-bar {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 7;
  width: min(680px, calc(100% - 20px));
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 7px;
  border: 1px solid rgba(255,255,255,0.16);
  border-radius: 8px;
  background: rgba(24, 27, 31, 0.92);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
  touch-action: none;
  will-change: left, top;
}
.float-title {
  min-width: 0;
  min-height: 34px;
  display: grid;
  grid-template-columns: 22px 8px minmax(0, 1fr) 32px;
  align-items: center;
  gap: 8px;
  padding: 0 2px;
  cursor: grab;
  touch-action: none;
}
.float-bar.dragging .float-title { cursor: grabbing; }
.drag-grip { color: #88919b; font-size: 16px; line-height: 1; }
.session-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); }
.float-bar[data-status="online"] .session-dot { background: var(--ok); }
.float-bar[data-status="error"] .session-dot { background: var(--danger); }
.float-context { min-width: 0; display: flex; align-items: center; gap: 10px; }
.float-context #targetName { flex: 0 0 auto; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 650; }
.float-sub { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 11px; }
.bar-toggle { width: 32px; height: 30px; }
.float-keys {
  display: grid;
  grid-template-columns: repeat(7, minmax(58px, 1fr));
  gap: 5px;
}
.control-btn {
  min-width: 0;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 5px 7px;
  font-size: 12px;
}
.control-btn > span:first-child { font-size: 15px; }
.control-btn.danger { border-color: rgba(224,82,82,0.55); background: rgba(224,82,82,0.15); color: #ffb0b0; }
.control-btn.danger:hover { background: rgba(224,82,82,0.28); }
.control-btn.active { border-color: rgba(15,157,138,0.65); background: var(--primary-soft); color: #a5e7dc; }
.remote-text { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; }
.remote-text input { height: 36px; }
.float-bar.collapsed {
  width: 46px;
  height: 46px;
  gap: 0;
  padding: 0;
  border-radius: 50%;
  background: rgba(24, 27, 31, 0.94);
}
.float-bar.collapsed .float-title {
  width: 100%;
  height: 100%;
  min-height: 0;
  grid-template-columns: 1fr;
  place-items: center;
  gap: 0;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
}
.float-bar.collapsed.dragging .float-title { cursor: grabbing; }
.float-bar.collapsed .drag-grip,
.float-bar.collapsed .float-context,
.float-bar.collapsed .bar-toggle { display: none; }
.float-bar.collapsed .session-dot {
  width: 13px;
  height: 13px;
  box-shadow: 0 0 0 7px rgba(228, 163, 64, 0.12);
}
.float-bar.collapsed[data-status="online"] { border-color: rgba(54, 184, 117, 0.55); }
.float-bar.collapsed[data-status="online"] .session-dot { box-shadow: 0 0 0 7px rgba(54, 184, 117, 0.14); }
.float-bar.collapsed[data-status="error"] { border-color: rgba(224, 82, 82, 0.6); }
.float-bar.collapsed[data-status="error"] .session-dot { box-shadow: 0 0 0 7px rgba(224, 82, 82, 0.14); }
.float-bar.collapsed .float-keys,
.float-bar.collapsed .remote-text { display: none !important; }

body.control-mode .top { display: none; }
body.control-mode .control-page { height: 100vh; height: 100dvh; }

@media (max-width: 820px) {
  .workspace { padding: 16px 12px 34px; }
  .workspace-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .tool-panel { padding: 16px; }
  .account-panel { order: -1; }
}

@media (max-width: 560px) {
  .top { min-height: 58px; padding: 8px 12px; }
  .brand { font-size: 16px; }
  .account-badge { max-width: 120px; }
  .workspace { width: 100vw; max-width: 100vw; padding: 12px 10px 28px; }
  .workspace-grid, .tool-panel { width: calc(100vw - 20px); max-width: calc(100vw - 20px); }
  .tool-panel { padding: 14px; }
  .captcha-row { grid-template-columns: minmax(0, 1fr) 96px 34px; gap: 6px; }
  .captcha { width: 96px; }
  .device-item { padding: 10px; }
  .device-actions { gap: 6px; }
  .float-bar { bottom: max(8px, env(safe-area-inset-bottom)); width: calc(100% - 12px); }
  .float-title { grid-template-columns: 20px 8px minmax(0, 1fr) 32px; gap: 6px; }
  .float-context { display: grid; gap: 1px; }
  .float-context #targetName { max-width: 100%; font-size: 12px; }
  .float-keys { grid-template-columns: repeat(4, 1fr); }
  .control-btn { min-height: 38px; padding: 5px 4px; }
}

@media (max-width: 370px) {
  .captcha-row { grid-template-columns: minmax(0, 1fr) 88px 32px; gap: 5px; }
  .captcha { width: 88px; }
  .control-btn { font-size: 11px; }
}
