:root {
  --bg: #f4f4f2;
  --surface: #ffffff;
  --text: #17191c;
  --muted: #6b7076;
  --line: #e2e2de;
  --accent: #1f6feb;
  --ok: #1a7f37;
  --warn: #b26b00;
  --danger: #c62828;
  --stage: #1b1d20;
  --radius: 12px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111315;
    --surface: #1b1e21;
    --text: #e9eaeb;
    --muted: #9aa0a6;
    --line: #2b2f33;
    --accent: #4c8dff;
    --ok: #3fb950;
    --warn: #d29922;
    --danger: #f85149;
    --stage: #0b0c0d;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 8px 0 12px; }
h3 { font-size: 16px; margin: 0 0 6px; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.error { color: var(--danger); margin: 0; }

.view { min-height: 100%; }
.view.center { display: grid; place-items: center; padding: 16px; }
.page { max-width: 640px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
  display: grid;
  gap: 10px;
}
.card.narrow { width: min(360px, 100%); margin: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  min-width: 0;
}
#pair-code { flex: 1; font-size: 20px; letter-spacing: 3px; text-align: center; }
select { padding: 6px 8px; font-size: 13px; }

.btn {
  font: inherit;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 16px;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
}
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn:disabled { opacity: .5; cursor: default; }

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; flex: 1; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); }
.badge.ok { color: var(--ok); border-color: currentColor; }
.badge.bad { color: var(--danger); border-color: currentColor; }

.list { display: grid; gap: 8px; }
.device {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px;
}
.device .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.device.online .dot { background: var(--ok); }
.device .info { flex: 1; min-width: 0; }
.device .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .meta { font-size: 13px; color: var(--muted); }

/* ---------- control view ---------- */
.view.control { display: flex; flex-direction: column; height: 100dvh; min-height: 0; }
.control .title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.control .title span:first-child { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.state { font-size: 12px; color: var(--muted); }
.state.ok { color: var(--ok); }
.state.warn { color: var(--warn); }
.state.bad { color: var(--danger); }

.stage {
  flex: 1; min-height: 0;
  background: var(--stage);
  display: grid; place-items: center;
  padding: 8px;
  overflow: hidden;
}
.holder { position: relative; outline: none; display: grid; place-items: center; width: 100%; height: 100%; }
.holder:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
#screen, #screen-video {
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  border-radius: 10px;
  background: #000;
  cursor: pointer;
}
#screen-video { max-width: 100%; max-height: 100%; object-fit: contain; }
.overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: #d0d3d6; text-align: center; padding: 24px;
  background: rgba(0, 0, 0, .55);
  pointer-events: none;
  font-size: 15px;
}
.ripple {
  position: absolute; width: 28px; height: 28px; margin: -14px 0 0 -14px;
  border-radius: 50%; border: 2px solid rgba(255, 255, 255, .85);
  pointer-events: none; animation: rip .35s ease-out forwards;
}
@keyframes rip { from { transform: scale(.4); opacity: 1; } to { transform: scale(1.4); opacity: 0; } }

.navbar {
  display: flex; justify-content: center; gap: 4px;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 6px 8px max(6px, env(safe-area-inset-bottom));
}
.nav {
  font: inherit; font-size: 20px; min-width: 48px; height: 44px;
  border: 0; border-radius: 10px; background: transparent; color: var(--text); cursor: pointer;
}
.nav:active { background: var(--line); }

.text-panel, .apps-panel {
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 10px 12px; display: grid; gap: 8px;
}
.text-panel textarea { width: 100%; resize: vertical; }
.apps-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; max-height: 40vh; overflow: auto; }
.app { display: grid; justify-items: center; gap: 4px; font-size: 11px; text-align: center; cursor: pointer; padding: 4px; border-radius: 8px; }
.app:hover { background: var(--line); }
.app img { width: 40px; height: 40px; border-radius: 10px; }
.app span { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* pseudo-fullscreen for iPhone Safari (no Fullscreen API on non-video elements) */
.control.pseudo-fs .bar, .control.pseudo-fs .text-panel, .control.pseudo-fs .apps-panel { display: none; }
.control.pseudo-fs .stage { padding: 0; }
.fs-exit { position: fixed; top: max(8px, env(safe-area-inset-top)); right: 8px; z-index: 5; opacity: .7; }

@media (max-width: 600px) {
  #ctl-latency { display: none; }
  .bar { gap: 6px; }
  select { padding: 4px; font-size: 12px; }
}
.toast {
  position: fixed; left: 50%; bottom: calc(72px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; font-size: 14px;
  max-width: calc(100% - 32px); z-index: 10;
}
