:root {
  --bg: #0d1117;
  --bg-dot: #1c2330;
  --panel: #161b22;
  --panel-2: #1f2630;
  --line: #2d3644;
  --text: #e6edf3;
  --muted: #8b96a5;
  --accent: #4f8cff;
  --accent-2: #2f6fe8;
  --good: #3fb950;
  --ok: #d29922;
  --bad: #f85149;
  --zoom: 1;
}
/* UI inside the zoomable world stays readable: counter-scaled, but capped so it never dwarfs a screen */
#stage { --ui: clamp(4px, calc(13px / var(--zoom)), 48px); }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; overscroll-behavior: none; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
#bar {
  position: fixed; inset: 0 0 auto 0; height: 52px; z-index: 20;
  display: flex; align-items: center; gap: 12px; padding: 0 12px; padding-top: env(safe-area-inset-top);
  background: rgba(13, 17, 23, .86); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 650; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.logo { color: var(--accent); font-size: 18px; }
.room { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; min-width: 0; }
.room #room-name { color: var(--text); }
.actions { margin-left: auto; display: flex; gap: 6px; }
.status { width: 9px; height: 9px; border-radius: 50%; background: var(--bad); flex: none; }
.status.online { background: var(--good); }
@media (max-width: 640px) { .brand-name { display: none; } #bar { gap: 8px; padding: 0 8px; } .actions { gap: 4px; } .btn { padding: 6px 9px; } }

.btn {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 6px 12px; cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { border-color: #3d4859; background: #263040; }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { color: #ffb3ae; }
.btn.icon { padding: 6px 10px; }
.btn.update { background: #1f6f3a; border-color: #2ea043; color: #fff; }
.update-box { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; background: var(--bg); }
.update-box .note { margin: 4px 0 8px; }
.btn.big { padding: 10px; width: 100%; font-weight: 600; }

/* ---------- space ---------- */
#stage {
  position: fixed; inset: 0; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none;
  background-color: var(--bg);
  background-image: radial-gradient(var(--bg-dot) 1px, transparent 1px);
  background-size: 24px 24px;
}
#stage.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.empty { position: absolute; left: 50%; bottom: 40px; transform: translateX(-50%); text-align: center; color: var(--muted); pointer-events: none; width: min(560px, 90vw); }
.empty h2 { color: var(--text); font-size: 17px; margin: 0 0 6px; }

/* ---------- device group ---------- */
.group {
  position: absolute; background: var(--panel); border: calc(1.5px / var(--zoom)) solid var(--line);
  border-radius: calc(12px / var(--zoom)); padding: calc(8px / var(--zoom)); box-shadow: 0 10px 40px rgba(0,0,0,.35);
  cursor: default; min-width: 200px;
}
.group.self { border-color: #2a4a7a; }
.group.dragging { box-shadow: 0 20px 60px rgba(0,0,0,.6); z-index: 5; }
.group.drop-over { border-color: var(--accent); box-shadow: 0 0 0 calc(4px / var(--zoom)) rgba(79,140,255,.35); }
.g-drop { display: none; position: absolute; inset: 0; border-radius: inherit; background: rgba(79,140,255,.15); align-items: center; justify-content: center; font-size: calc(22px / var(--zoom)); font-weight: 600; color: #cfe0ff; pointer-events: none; }
.group.drop-over .g-drop { display: flex; }

.g-head {
  display: flex; align-items: center; gap: .5em; font-size: var(--ui); padding: .2em .3em .6em; cursor: move; touch-action: none; white-space: nowrap; overflow: hidden;
}
.g-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.g-badges { display: flex; gap: .3em; }
.badge { font-size: .8em; padding: .05em .5em; border-radius: 99px; background: #23314a; color: #9fc0ff; }
.g-conn { margin-left: auto; width: .7em; height: .7em; border-radius: 50%; background: var(--muted); flex: none; }
.g-conn.connected, .g-conn.self { background: var(--good); }
.g-conn.connecting, .g-conn.new { background: var(--ok); }
.g-conn.failed, .g-conn.disconnected { background: var(--bad); }

.g-screens { position: relative; }
.tile {
  position: absolute; background: #000; border-radius: calc(4px / var(--zoom)); overflow: hidden; outline: calc(1px / var(--zoom)) solid #000;
}
.tile video { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; background: #000; }
.ctl-layer { position: absolute; inset: 0; display: none; cursor: none; }
.tile.controlling { outline: calc(3px / var(--zoom)) solid var(--accent); z-index: 2; }
.tile.controlling .ctl-layer { display: block; cursor: default; }
.t-wait { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; color: var(--muted); font-size: calc(16px / var(--zoom)); }
.tile.waiting .t-wait { display: flex; }

.t-bar {
  position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: center; gap: .5em; padding: .35em .5em;
  font-size: var(--ui); background: linear-gradient(rgba(0,0,0,.7), transparent); opacity: 0; transition: opacity .15s; white-space: nowrap;
}
.tile:hover .t-bar, .tile.controlling .t-bar, body:not(.is-controlling) .tile:focus-within .t-bar { opacity: 1; }
@media (hover: none) { .t-bar { opacity: 1; } }
.t-label { overflow: hidden; text-overflow: ellipsis; }
.t-fps { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--muted); }
.t-fps.good { color: var(--good); } .t-fps.ok { color: var(--ok); } .t-fps.bad { color: var(--bad); }
.t-btn { background: rgba(255,255,255,.12); border: 0; border-radius: .4em; padding: .15em .45em; cursor: pointer; font-size: 1em; line-height: 1.3; }
.t-btn:hover { background: rgba(255,255,255,.25); }
.tile.controlling .t-ctl { background: var(--accent); }

.tile.placeholder { background: var(--panel-2); display: flex; align-items: center; justify-content: center; text-align: center; border: calc(1.5px / var(--zoom)) dashed var(--line); outline: none; }
.ph-text { color: var(--muted); font-size: calc(14px / var(--zoom)); max-width: 80%; display: flex; flex-direction: column; gap: .8em; align-items: center; }
.ph-icon { font-size: 2.5em; }
.ph-text .btn { font-size: 1em; }

/* ---------- shelves ---------- */
.g-shelf { display: flex; flex-wrap: wrap; gap: .4em; padding-top: .6em; font-size: var(--ui); max-width: 100%; }
.shelf-empty { color: var(--muted); font-size: .9em; padding: .2em .3em; }
.chip {
  display: inline-flex; align-items: center; gap: .5em; max-width: 22em; padding: .3em .7em; border-radius: .6em;
  background: var(--panel-2); border: 1px solid var(--line); cursor: grab; touch-action: none;
}
.chip:hover { border-color: var(--accent); }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-size { color: var(--muted); font-size: .85em; white-space: nowrap; }
.chip.add { color: var(--accent); cursor: pointer; font: inherit; }
.chip.ghost { position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none; font-size: 13px; background: var(--accent-2); color: #fff; border-color: transparent; box-shadow: 0 8px 24px rgba(0,0,0,.5); }

/* ---------- control bar ---------- */
#ctl-bar {
  position: fixed; left: 50%; bottom: calc(12px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 25;
  display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; border-radius: 12px;
  background: rgba(22,27,34,.96); border: 1px solid var(--accent); box-shadow: 0 10px 30px rgba(0,0,0,.5);
  width: max-content; max-width: calc(100vw - 16px);
}
.ctl-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.ctl-title { overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
.ctl-row.keys { overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.ctl-row.keys::-webkit-scrollbar { display: none; }
.key {
  flex: none; min-width: 44px; height: 38px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font-size: 13px; cursor: pointer; touch-action: manipulation;
}
.key:active { background: #2a3546; }
.key.mod.on { background: var(--accent-2); border-color: var(--accent); color: #fff; }
#ctl-kbd.pulse { animation: kbdpulse 1s ease-in-out 3; border-color: var(--accent); }
@keyframes kbdpulse { 50% { background: var(--accent-2); color: #fff; } }
#ctl-bar b { overflow: hidden; text-overflow: ellipsis; }

/* offline devices from the room's roster */
.group.offline { opacity: .55; }
.group.offline .g-screens { filter: grayscale(1); }
.g-seen { color: var(--muted); font-size: .85em; }
.g-forget { margin-left: .3em; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1em; padding: 0 .2em; }
.g-forget:hover { color: var(--bad); }
.tile.offline-tile { background: var(--panel-2); display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: calc(14px / var(--zoom)); border: calc(1.5px / var(--zoom)) dashed var(--line); outline: none; }
.t-viewonly { font-size: .8em; color: var(--muted); }
#ctl-bar .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bad); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
#kbd { position: fixed; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; font-size: 16px; }

/* ---------- panels ---------- */
#settings {
  position: fixed; top: 60px; right: 12px; z-index: 30; width: min(340px, calc(100vw - 24px));
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; box-shadow: 0 20px 50px rgba(0,0,0,.5);
  max-height: calc(100vh - 80px); overflow: auto;
}
#settings h3 { margin: 0 0 10px; }
label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
label.row, .row { flex-direction: row; align-items: center; display: flex; }
.gap { gap: 8px; }
input:not([type=checkbox]), select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; color: var(--text); font-size: 15px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.note { color: var(--muted); font-size: 12.5px; margin: 8px 0; }
.muted { color: var(--muted); font-weight: 400; }
.err { color: var(--bad); min-height: 1.2em; margin-bottom: 8px; }

.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; background: rgba(5,8,12,.75); padding: 16px; }
.card { width: min(400px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; max-height: 100%; overflow: auto; }
.card h2 { margin: 0 0 4px; }

/* ---------- transfers & toasts ---------- */
#transfers { position: fixed; right: 12px; bottom: 12px; z-index: 40; display: flex; flex-direction: column; gap: 8px; width: min(300px, calc(100vw - 24px)); }
.xfer { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; box-shadow: 0 8px 24px rgba(0,0,0,.4); }
.x-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.x-bar { height: 4px; background: var(--bg); border-radius: 4px; margin: 6px 0 4px; overflow: hidden; }
.x-bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.xfer.done .x-bar i { background: var(--good); }
.xfer.failed .x-bar i { background: var(--bad); }
.x-meta { color: var(--muted); font-size: 12px; }
#toasts { position: fixed; left: 50%; top: 64px; transform: translateX(-50%); z-index: 45; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { background: var(--panel-2); border: 1px solid var(--line); padding: 8px 14px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.4); transition: opacity .3s; max-width: 90vw; }
.toast.out { opacity: 0; }
