:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1f2630;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --primary: #2f81f7;
  --primary-hover: #388bfd;
  --success: #238636;
  --success-hover: #2ea043;
  --warn: #d29922;
  --danger: #f85149;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  font-size: 14px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.tiny { font-size: 11px; }

/* Buttons & Inputs */
button, input, select {
  font-family: inherit; font-size: 13px; color: var(--text);
}
input[type="text"], input[type="password"], select {
  background: #0d1117; border: 1px solid var(--border);
  border-radius: 6px; padding: 8px 10px; outline: none;
}
input:focus, select:focus { border-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 6px;
  padding: 8px 14px; cursor: pointer; text-decoration: none;
  font-weight: 500; transition: 0.15s background, 0.15s opacity;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: var(--success-hover); }
.btn-ghost { background: var(--panel-2); border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: #2b3340; }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-xs { padding: 4px 8px; font-size: 12px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.badge-ok { background: rgba(35,134,54,0.22); color: #3fb950; border: 1px solid rgba(63,185,80,0.4); }
.badge-warn { background: rgba(210,153,34,0.22); color: #e3b341; border: 1px solid rgba(227,179,65,0.4); }
.badge-danger { background: rgba(248,81,73,0.2); color: #ff7b72; border: 1px solid rgba(255,123,114,0.4); }
.badge-info { background: rgba(47,129,247,0.2); color: #79c0ff; border: 1px solid rgba(121,192,255,0.4); }
.badge-muted { background: #21262d; color: var(--muted); border: 1px solid var(--border); }

/* Login */
.login-wrapper {
  min-height: 100%; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: radial-gradient(circle at top, #182234, #0d1117 65%);
}
.login-card {
  width: 100%; max-width: 480px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
}
.brand-row { display: flex; align-items: center; gap: 14px; margin-bottom: 20px; }
.brand-icon { font-size: 36px; }
.brand-row h1 { margin: 0; font-size: 20px; }
.brand-row p { margin: 4px 0 0; }

.login-form { display: flex; flex-direction: column; gap: 10px; }
.error-text { color: #ff7b72; font-size: 12px; }

.apk-box {
  margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
}
.apk-box h3 { margin: 0 0 4px; font-size: 14px; }
.apk-meta { margin: 6px 0 10px; }
.qr-wrap { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.qr-wrap svg, .qr-large svg { background: #fff; padding: 6px; border-radius: 8px; display: block; }

/* App Shell */
.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar {
  height: 50px; flex-shrink: 0; background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 0 16px; display: flex; align-items: center; justify-content: space-between;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.logo { font-weight: 700; font-size: 15px; }

.workspace {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 270px minmax(360px, 1fr) 430px;
  gap: 10px; padding: 10px; overflow: hidden;
}

.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.panel-header {
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-shrink: 0;
}
.panel-header h2 { margin: 0; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.count-pill {
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 1px 7px; border-radius: 999px; font-size: 11px; color: var(--muted);
}

/* Device list */
.device-filter-bar {
  padding: 8px 8px 6px; border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 6px; flex-shrink: 0; background: #10151d;
}
.filter-status-tabs { display: flex; gap: 4px; }
.filter-pill {
  flex: 1; background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
  border-radius: 6px; padding: 4px 6px; font-size: 11px; cursor: pointer; font-weight: 500;
}
.filter-pill.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.filter-search-wrap { position: relative; display: flex; align-items: center; }
.filter-search-wrap input {
  width: 100%; padding: 5px 24px 5px 8px; font-size: 12px;
  background: #0d1117; border: 1px solid var(--border); border-radius: 6px;
}
.filter-search-wrap .icon-btn { position: absolute; right: 4px; }
.dev-adb-id {
  font-family: "JetBrains Mono", Consolas, monospace; font-size: 11px;
  color: #79c0ff; margin-top: 4px; display: flex; justify-content: space-between; gap: 6px;
}
.dev-adb-id.empty { color: #8b949e; }
.device-list { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 8px; }
.empty-state { text-align: center; padding: 30px 12px; color: var(--muted); }

.device-card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; cursor: pointer; transition: 0.15s border-color, 0.15s background;
}
.device-card:hover { border-color: #58a6ff; }
.device-card.active { border-color: var(--primary); background: rgba(47,129,247,0.12); }
.device-card.offline { opacity: 0.65; }

.dev-row-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.dev-name-wrap { display: flex; align-items: center; gap: 7px; min-width: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.dot-online { background: #3fb950; box-shadow: 0 0 6px #3fb950; }
.dot-offline { background: #6e7681; }
.dev-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dev-sub { font-size: 11px; color: var(--muted); margin-top: 3px; display: flex; justify-content: space-between; }
.dev-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 7px; }
.dev-card-actions { display: flex; gap: 4px; }
.icon-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; padding: 2px 4px; border-radius: 4px; font-size: 12px;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }

/* Center Screen Stage */
.screen-title-group { display: flex; flex-direction: column; min-width: 0; }
.screen-actions { display: flex; gap: 6px; }

.telemetry-bar {
  display: flex; flex-wrap: wrap; gap: 12px; padding: 6px 12px;
  background: #0f141b; border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted); flex-shrink: 0;
}
.telemetry-bar b { color: #79c0ff; font-weight: 600; }

.stage-container {
  flex: 1; min-height: 0; background: #07090d;
  display: flex; align-items: center; justify-content: center;
  position: relative; overflow: hidden; padding: 10px;
}
.stage-placeholder { text-align: center; color: var(--muted); }
.placeholder-icon { font-size: 42px; margin-bottom: 8px; }

.screen-wrapper {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  max-width: 100%; max-height: 100%; user-select: none; -webkit-user-select: none;
  border-radius: 8px; overflow: hidden; box-shadow: 0 8px 32px rgba(0,0,0,0.65);
  border: 1px solid #262c36;
}
#screenImg {
  display: block; max-width: 100%; max-height: calc(100vh - 255px);
  width: auto; height: auto; object-fit: contain; pointer-events: none;
}
#overlayCanvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  cursor: crosshair; touch-action: none;
}

.control-strip {
  padding: 8px 12px; border-top: 1px solid var(--border);
  background: var(--panel); flex-shrink: 0;
}
.stream-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; font-size: 12px; }
.ctrl-item { display: inline-flex; align-items: center; gap: 6px; }
.ctrl-item select { padding: 4px 6px; font-size: 12px; }
.ctrl-item input[type="range"] { width: 85px; }

.nav-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.btn-group { display: flex; flex-wrap: wrap; gap: 5px; }
.text-send-form { display: flex; gap: 6px; flex: 1; min-width: 210px; max-width: 340px; }
.text-send-form input { flex: 1; padding: 5px 8px; font-size: 12px; }

/* Right ADB Panel */
.adb-toolbar {
  padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 6px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.adb-pair-box {
  padding: 8px 10px; border-bottom: 1px solid var(--border);
  background: #10151d; display: flex; flex-direction: column; gap: 6px; flex-shrink: 0;
}
.adb-inline-form { display: flex; gap: 6px; }
.adb-inline-form input {
  flex: 1; min-width: 0; padding: 5px 8px; font-size: 12px;
  background: #0d1117; border: 1px solid var(--border); border-radius: 6px; color: var(--text);
}
.adb-hint {
  margin: 8px 10px 0; padding: 8px 10px; border-radius: 6px; font-size: 12px;
  background: rgba(210,153,34,0.14); border: 1px solid rgba(227,179,65,0.35); color: #f0c966;
}
.mode-tabs {
  display: flex; gap: 4px; padding: 8px 10px 4px; flex-shrink: 0; flex-wrap: wrap;
}
.tab-btn {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
  padding: 5px 9px; border-radius: 6px; font-size: 12px; cursor: pointer;
}
.tab-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.quick-cmds {
  display: flex; flex-wrap: wrap; gap: 5px; padding: 6px 10px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.chip {
  background: #0d1117; border: 1px solid var(--border); color: #c9d1d9;
  padding: 3px 8px; border-radius: 999px; font-size: 11px; cursor: pointer;
}
.chip:hover { border-color: var(--primary); color: #79c0ff; }

.terminal-output {
  flex: 1; min-height: 0; margin: 0; padding: 10px 12px;
  background: #090c10; color: #d1e7dd;
  font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
  font-size: 12px; line-height: 1.48; overflow-y: auto;
  white-space: pre-wrap; word-break: break-all;
}

.adb-input-row {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  border-top: 1px solid var(--border); background: #0f141b; flex-shrink: 0;
}
.prompt-prefix {
  font-family: monospace; font-size: 12px; color: #3fb950; font-weight: 700; white-space: nowrap;
}
.adb-input-row input { flex: 1; font-family: monospace; font-size: 12px; padding: 7px 9px; }

/* Modal */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.68); }
.modal-card {
  position: relative; z-index: 1; width: 100%; max-width: 560px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.modal-sm { max-width: 380px; }
.modal-gallery { max-width: 960px; max-height: 86vh; display: flex; flex-direction: column; }
.modal-lightbox { max-width: 90vw; max-height: 92vh; width: auto; display: flex; flex-direction: column; }
.gallery-header-actions { display: flex; align-items: center; gap: 6px; }
.gallery-body { overflow-y: auto; flex: 1; min-height: 260px; max-height: 74vh; }
.gallery-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); gap: 12px;
}
.gallery-card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; display: flex; flex-direction: column;
}
.gallery-thumb-wrap {
  height: 200px; background: #07090d; display: flex; align-items: center; justify-content: center;
  cursor: pointer; overflow: hidden;
}
.gallery-thumb-wrap img { width: 100%; height: 100%; object-fit: contain; display: block; }
.gallery-card-meta { padding: 7px 8px; font-size: 11px; display: flex; flex-direction: column; gap: 3px; }
.gallery-card-title { font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gallery-card-actions { display: flex; gap: 4px; margin-top: 4px; }
.gallery-card-actions .btn { flex: 1; padding: 3px 6px; font-size: 11px; }
.lightbox-body { align-items: center; justify-content: center; background: #07090d; overflow: auto; }
#lightboxImg { max-width: 82vw; max-height: 78vh; object-fit: contain; border-radius: 6px; }
.modal-header {
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.apk-modal-body { flex-direction: row; align-items: flex-start; gap: 18px; }
.apk-modal-details { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.apk-modal-details p { margin: 0; }
.apk-modal-details code { background: #0d1117; padding: 2px 6px; border-radius: 4px; word-break: break-all; }
.setup-tips {
  background: #0d1117; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-size: 12px; color: var(--muted);
}
.setup-tips ol { margin: 6px 0 0; padding-left: 18px; }
.setup-tips li { margin-bottom: 4px; }

/* Toast */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #232b36; border: 1px solid #3c4654; color: #fff;
  padding: 9px 16px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5); z-index: 200;
}

@media (max-width: 1120px) {
  .workspace {
    grid-template-columns: 240px 1fr;
    grid-template-rows: 1fr 340px;
  }
  .panel-adb { grid-column: 1 / -1; }
}
@media (max-width: 760px) {
  .app-shell { height: auto; min-height: 100vh; overflow: auto; }
  .workspace { display: flex; flex-direction: column; overflow: visible; }
  .panel-devices { max-height: 240px; }
  .panel-screen { min-height: 520px; }
  .panel-adb { height: 420px; }
  .apk-modal-body { flex-direction: column; align-items: center; }
}
