:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --border: #e2e6ee;
  --text: #172033;
  --muted: #64708a;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1320;
    --surface: #161d2e;
    --border: #273149;
    --text: #e6eaf2;
    --muted: #97a3bc;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 10px; }
a { color: var(--primary); }
hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0; }
.row { display: flex; gap: 8px; align-items: center; }
.row input { flex: 1; min-width: 0; }

label { display: block; font-weight: 500; margin: 10px 0; }
input, textarea, select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
textarea { resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { border-color: var(--border); }
.btn.ghost:hover { background: color-mix(in srgb, var(--primary) 8%, var(--surface)); }
.btn.danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.card-head h2 { margin: 0; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }

.pill {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--border);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.pill.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.pill.warn { background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn); }

/* Auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .brand { margin-bottom: 18px; }
.auth-card .btn { width: 100%; justify-content: center; margin-top: 6px; }

/* Dashboard */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-right { display: flex; align-items: center; gap: 12px; }
.dash {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  max-width: 1200px;
  margin: 24px auto;
  padding: 0 24px;
}
.side { display: grid; gap: 20px; align-content: start; }
@media (max-width: 860px) {
  .dash { grid-template-columns: 1fr; padding: 0 16px; }
  .topbar { padding: 12px 16px; }
  #me { display: none; }
}

.device-list { list-style: none; margin: 0; padding: 0; }
.device {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 12px 4px;
  border-top: 1px solid var(--border);
}
.device:first-child { border-top: 0; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--border); }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.device-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-meta { color: var(--muted); font-size: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.device-actions { display: flex; gap: 6px; }
.empty { text-align: center; padding: 24px 8px; }

.history { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.history li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-top: 1px solid var(--border); }
.history li:first-child { border-top: 0; }
.history .when { color: var(--muted); white-space: nowrap; }

#support-code { font-size: 18px; letter-spacing: 2px; text-align: center; margin: 0; }

.dialog { max-width: min(520px, calc(100vw - 32px)); color: var(--text); }
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
#deploy-code { font: 600 18px/1.4 ui-monospace, Consolas, monospace; letter-spacing: 1px; text-align: center; }

/* Session */
.session { height: 100%; display: flex; flex-direction: column; background: #05070c; }
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar select { width: auto; margin: 0; padding: 4px 8px; font-size: 13px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; margin: 0; font-weight: 500; }
.toggle input { width: auto; margin: 0; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 6px; font-size: 11px; }

.stage-wrap { flex: 1; display: flex; min-height: 0; }
.stage { position: relative; flex: 1; min-width: 0; display: grid; place-items: center; background: #05070c; }
.stage video { width: 100%; height: 100%; object-fit: contain; outline: none; cursor: default; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: #dbe2f0;
  background: rgb(5 7 12 / 85%);
  text-align: center;
  padding: 16px;
}
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #33405c;
  border-top-color: #6b9cff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.stats {
  position: absolute;
  top: 10px;
  left: 10px;
  margin: 0;
  padding: 8px 10px;
  background: rgb(0 0 0 / 70%);
  color: #b8f7c4;
  font: 12px/1.5 ui-monospace, Consolas, monospace;
  border-radius: 6px;
  pointer-events: none;
}

.notice {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, calc(100% - 20px));
  padding: 8px 14px;
  border-radius: 8px;
  background: rgb(217 119 6 / 92%);
  color: #fff;
  font-size: 13px;
  text-align: center;
  pointer-events: none;
}

.panel {
  width: 300px;
  flex: none;
  padding: 14px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow: auto;
}
.panel .row { margin-top: 8px; }
.chat { display: flex; flex-direction: column; }
.chat-log { list-style: none; margin: 0; padding: 0; flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
.chat-log li { padding: 6px 10px; border-radius: 10px; max-width: 85%; word-wrap: break-word; background: var(--bg); align-self: flex-start; }
.chat-log li.me { background: var(--primary); color: #fff; align-self: flex-end; }
.chat-log li.sys { background: transparent; color: var(--muted); font-size: 12px; align-self: center; text-align: center; }
@media (max-width: 860px) {
  .panel { position: absolute; right: 0; top: 0; bottom: 0; z-index: 2; width: min(300px, 90vw); }
  .stage-wrap { position: relative; }
}

.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 10; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: #172033; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 4px 14px rgb(0 0 0 / 25%); }
.toast.error { background: var(--danger); }
