:root {
  --bg: #0f1419;
  --panel: #161c24;
  --panel-2: #1c242e;
  --border: #2a3441;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #0285c5;
  --accent-2: #1a9ad9;
  --danger: #da3633;
  --ok: #3fb950;
  --warn: #d29922;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 14px; line-height: 1.4; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: #ff7b72; min-height: 1.2em; margin: 6px 0 0; font-size: 12px; }
.spacer { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }
input, select { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; font: inherit; margin-bottom: 8px; }
input:focus, select:focus { outline: none; border-color: var(--accent-2); }
label { display: block; font-size: 12px; color: var(--muted); }
label input { margin-top: 4px; }
label.check { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; color: var(--text); }
label.check input { width: auto; margin: 0; }

.btn { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 9px 14px; font: inherit; cursor: pointer; text-decoration: none; display: inline-block; }
.btn:hover { border-color: var(--accent-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); width: 100%; font-weight: 600; }
.btn.primary:hover { background: var(--accent-2); }
.btn.small { padding: 5px 10px; font-size: 12px; width: auto; }
.btn.active { border-color: var(--accent-2); color: #fff; background: #1f3350; }
.btn.danger { background: var(--danger); border-color: var(--danger); }
.icon { background: transparent; border: 1px solid transparent; color: var(--muted); border-radius: 6px; width: 28px; height: 28px; cursor: pointer; font-size: 15px; line-height: 1; }
.icon:hover { color: var(--text); border-color: var(--border); background: var(--panel-2); }
.icon.danger:hover { color: #fff; background: var(--danger); border-color: var(--danger); }

.brand { display: flex; align-items: center; gap: 10px; }
.brand strong { display: block; font-size: 15px; }
.brand span:last-child { display: block; font-size: 12px; color: var(--muted); }
.brand-mark { width: 36px; height: 36px; flex: none; display: block; }
.brand-full { height: 46px; width: auto; display: block; }

/* Login */
.login { height: 100%; display: grid; place-items: center; background: radial-gradient(1200px 600px at 20% -10%, #15284a 0%, var(--bg) 60%); }
.login-card { width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.login-card .brand { margin-bottom: 8px; }
.login-card label { margin: 0; }

/* App shell */
.app { display: grid; grid-template-columns: 320px minmax(0, 1fr); height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); padding: 16px; display: flex; flex-direction: column; overflow-y: auto; }
.tabs { display: flex; gap: 4px; margin: 16px 0 4px; background: var(--bg); padding: 4px; border-radius: 9px; }
.tab { flex: 1; background: transparent; border: 0; color: var(--muted); padding: 7px 0; border-radius: 6px; cursor: pointer; font: inherit; font-size: 13px; }
.tab.active { background: var(--panel-2); color: var(--text); }
.panel { flex: 1; }
.code-input { font-size: 24px; letter-spacing: .12em; text-align: center; font-variant-numeric: tabular-nums; }
.me { display: flex; align-items: center; justify-content: space-between; padding-top: 14px; border-top: 1px solid var(--border); margin-top: 14px; font-size: 13px; }

.device-list, .log-list { list-style: none; margin: 0; padding: 0; }
.device-list li { display: grid; grid-template-columns: 10px 1fr auto auto; gap: 10px; align-items: center; padding: 9px 8px; border-radius: 8px; border: 1px solid transparent; }
.device-list li:hover { background: var(--panel-2); border-color: var(--border); }
.device-list .name { font-weight: 600; }
.device-list .meta { color: var(--muted); font-size: 12px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.dot.online { background: var(--ok); }
.dot.busy { background: var(--warn); }
.dot.live { background: var(--ok); box-shadow: 0 0 0 3px rgba(63,185,80,.25); }
.log-list li { padding: 8px; border-bottom: 1px solid var(--border); font-size: 12px; cursor: pointer; }
.log-list li:hover { background: var(--panel-2); }
.log-list .title { color: var(--text); font-weight: 600; display: flex; justify-content: space-between; }
.log-list .events { margin: 6px 0 0 8px; padding: 0; list-style: none; color: var(--muted); }
.log-list .events li { padding: 2px 0; border: 0; cursor: default; }
.token-box { background: var(--bg); border: 1px dashed var(--warn); border-radius: 8px; padding: 10px; font-size: 12px; word-break: break-all; margin-bottom: 10px; }
.token-box code { display: block; font-size: 13px; margin: 6px 0; color: #ffd27a; user-select: all; }

/* Main */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--panel); }
.view-switch { display: flex; gap: 4px; }
.session-tabs { display: flex; gap: 4px; overflow-x: auto; }
.session-tabs button { background: transparent; border: 1px solid var(--border); color: var(--muted); padding: 5px 12px; border-radius: 6px; cursor: pointer; font: inherit; font-size: 12px; white-space: nowrap; }
.session-tabs button.active { color: var(--text); background: var(--panel-2); border-color: var(--accent-2); }

.sessions { flex: 1; min-height: 0; padding: 12px; gap: 12px; overflow: auto; position: relative; }
.sessions.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(480px, 1fr)); grid-auto-rows: minmax(320px, 1fr); }
.sessions.grid.one { grid-template-columns: 1fr; }
.sessions.tabs { display: block; }
.sessions.tabs .session { height: 100%; }
.sessions.tabs .session:not(.active) { display: none; }

.empty { grid-column: 1 / -1; display: block; color: var(--muted); overflow-y: auto; }
.empty h3 { margin: 10px 0 4px; color: var(--text); font-weight: 600; }

.session { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; outline: none; min-height: 0; }
.session:focus-within { border-color: var(--accent-2); box-shadow: 0 0 0 2px rgba(26,154,217,.25); }
.session-head { display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 12px; background: var(--panel-2); border-bottom: 1px solid var(--border); }
.session-head select { width: auto; margin: 0; padding: 3px 6px; font-size: 12px; }
.session-head .s-timer { font-variant-numeric: tabular-nums; }
.session.paused .session-head { background: #4a3a10; }
.viewport { flex: 1; min-height: 0; position: relative; background: #000; display: grid; place-items: center; overflow: hidden; }
.viewport canvas { max-width: 100%; max-height: 100%; object-fit: contain; image-rendering: auto; cursor: default; }
.remote-cursor { position: absolute; left: 0; top: 0; pointer-events: none; transform-origin: 0 0; image-rendering: pixelated; z-index: 1; }
.s-stats { position: absolute; right: 8px; bottom: 6px; background: rgba(15,20,25,.7); padding: 2px 6px; border-radius: 4px; pointer-events: none; font-variant-numeric: tabular-nums; }
.overlay { position: absolute; inset: 0; z-index: 2; display: grid; place-items: center; background: rgba(15,20,25,.82); }
.overlay-card { text-align: center; color: var(--muted); max-width: 320px; }
.overlay-text { margin: 12px 0 0; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent-2); border-radius: 50%; margin: 0 auto; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.session.ended .overlay { background: rgba(15,20,25,.92); }
.session.ended .spinner { display: none; }

/* Session toolbar and menus */
.toolbar { display: flex; align-items: center; gap: 2px; padding: 3px 6px; background: var(--panel); border-bottom: 1px solid var(--border); }
.menu-wrap { position: relative; }
.menu-btn, .tool-btn { background: transparent; border: 1px solid transparent; color: var(--text); padding: 4px 10px; border-radius: 6px; font: inherit; font-size: 12.5px; cursor: pointer; position: relative; }
.menu-btn:hover, .tool-btn:hover, .menu-wrap.open .menu-btn, .tool-btn.on { background: var(--panel-2); border-color: var(--border); }
.menu { display: none; position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; min-width: 220px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 4px; box-shadow: 0 12px 30px rgba(0,0,0,.45); }
.menu-wrap.open .menu { display: block; }
.menu button { display: block; width: 100%; text-align: left; background: transparent; border: 0; color: var(--text); padding: 6px 10px 6px 26px; border-radius: 5px; font: inherit; font-size: 13px; cursor: pointer; position: relative; }
.menu button:hover { background: #26303c; }
.menu button.check.on::before { content: "✓"; position: absolute; left: 9px; color: var(--accent-2); }
.menu button:disabled { color: var(--muted); cursor: default; background: transparent; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 2px; }
.menu-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding: 4px 10px 2px; }
.chat-badge { display: inline-block; min-width: 16px; height: 16px; line-height: 16px; border-radius: 8px; background: var(--danger); color: #fff; font-size: 10.5px; text-align: center; margin-left: 6px; padding: 0 4px; }
.session.viewonly .s-mode { color: var(--warn); }

/* Screen + chat side by side */
.stage { flex: 1; min-height: 0; display: flex; }
.stage .viewport { flex: 1; min-width: 0; }
.chat { width: 270px; flex: none; display: flex; flex-direction: column; border-left: 1px solid var(--border); background: var(--panel); }
.chat-head { display: flex; align-items: center; padding: 6px 8px 6px 12px; border-bottom: 1px solid var(--border); }
.c-list { list-style: none; margin: 0; padding: 8px; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.c-list li { max-width: 88%; padding: 6px 10px; border-radius: 10px; font-size: 13px; white-space: pre-wrap; word-break: break-word; }
.c-list li.me { align-self: flex-end; background: #1f3350; }
.c-list li.them { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--border); }
.c-list li.sys { align-self: center; background: transparent; color: var(--muted); font-size: 11.5px; }
.c-list li time { display: block; font-size: 10.5px; color: var(--muted); margin-top: 2px; }
.c-form { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--border); }
.c-form input { margin: 0; }

/* Actual size: 1:1 pixels with scrollbars */
.session.actual .viewport { display: block; overflow: auto; }
.session.actual .viewport canvas { max-width: none; max-height: none; }
.session.actual .overlay { position: sticky; }

/* Focus layout: one large session, the rest as live thumbnails */
/* --thumbs (set by the console) = number of thumbnails; the big pane spans exactly those rows. */
.sessions.focus { display: grid; grid-template-columns: 1fr 260px; grid-template-rows: repeat(var(--thumbs, 1), minmax(150px, 1fr)); }
.sessions.focus .session.active { grid-column: 1; grid-row: 1 / -1; }
.sessions.focus .session:not(.active) { grid-column: 2; cursor: pointer; }
.sessions.focus .session:not(.active) .toolbar,
.sessions.focus .session:not(.active) .files,
.sessions.focus .session:not(.active) .chat,
.sessions.focus .session:not(.active) .s-timer,
.sessions.focus .session:not(.active) .s-user { display: none; }
.sessions.focus .session:not(.active) .viewport canvas { pointer-events: none; }
.sessions.focus.one { grid-template-columns: 1fr; }

/* File browser drawer inside a session */
.files { display: flex; flex-direction: column; max-height: 45%; min-height: 160px; border-bottom: 1px solid var(--border); background: var(--panel); }
.files-bar { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.files-bar .f-path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: rtl; text-align: left; max-width: 60%; }
.f-upload-label { margin: 0; color: var(--text); font-size: 12px; }
.f-list { list-style: none; margin: 0; padding: 4px; overflow-y: auto; flex: 1; }
.f-list li { display: grid; grid-template-columns: 22px 1fr auto auto; gap: 8px; align-items: center; padding: 5px 8px; border-radius: 6px; font-size: 13px; }
.f-list li:hover { background: var(--panel-2); }
.f-list li.dir { cursor: pointer; }
.f-list .size { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.f-status { padding: 4px 10px 6px; min-height: 1.6em; }
.me > span:last-child { display: flex; gap: 6px; }

.session:fullscreen { border: 0; border-radius: 0; }
.session:fullscreen .viewport canvas { max-height: 100vh; }

/* Home screen (no sessions): quick connect, favourites and recent connections as cards */
.home { max-width: 1100px; margin: 0 auto; padding: 28px 12px; }
.home-hero { display: flex; align-items: center; gap: 18px; margin-bottom: 18px; }
.home-logo { width: 64px; height: 64px; flex: none; filter: drop-shadow(0 8px 24px rgba(2,133,197,.35)); }
.home-hero p { margin: 0; }
.home-connect { display: flex; gap: 8px; max-width: 460px; }
.home-connect input { margin: 0; font-size: 20px; }
.home-connect .btn.primary { width: auto; padding: 0 22px; }
.home h4 { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; font-weight: 600; margin: 22px 0 10px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; cursor: default; transition: border-color .15s, transform .15s; }
.card:hover { border-color: var(--accent-2); transform: translateY(-1px); }
.card .thumb { aspect-ratio: 16 / 9; background: linear-gradient(135deg, #17243a, #0f1720); display: grid; place-items: center; position: relative; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .thumb .initial { width: 46px; height: 46px; border-radius: 12px; background: #1f3350; color: #9cc7ff; display: grid; place-items: center; font-size: 20px; font-weight: 600; }
.card .thumb .dot { position: absolute; left: 8px; top: 8px; box-shadow: 0 0 0 2px rgba(0,0,0,.5); }
.card .body { padding: 8px 10px 10px; display: grid; grid-template-columns: 1fr auto; gap: 2px 6px; align-items: center; }
.card .name { color: var(--text); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; grid-column: 1; }
.card .actions { grid-column: 2; grid-row: 1 / span 2; display: flex; gap: 4px; align-items: center; }
.star { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 2px 4px; border-radius: 5px; }
.star:hover { color: #ffd27a; background: var(--panel-2); }
.star.on { color: #ffcc4d; }

/* Sidebar recent list */
h2.list-head { margin-top: 22px; }
.recent-list { list-style: none; margin: 0; padding: 0; }
.recent-list li { display: grid; grid-template-columns: 10px minmax(0, 1fr) auto auto; gap: 8px; align-items: center; padding: 7px 6px; border-radius: 8px; border: 1px solid transparent; }
.recent-list li:hover { background: var(--panel-2); border-color: var(--border); }
.recent-list .name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-list .meta { color: var(--muted); font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-list li.none { display: block; color: var(--muted); font-size: 12px; }
.device-list li { grid-template-columns: 10px minmax(0, 1fr) auto auto auto; }

/* Connection quality bars in the session header */
.s-net { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; color: var(--muted); }
.s-net i { width: 3px; background: var(--border); border-radius: 1px; }
.s-net i:nth-child(1) { height: 4px; } .s-net i:nth-child(2) { height: 7px; } .s-net i:nth-child(3) { height: 10px; } .s-net i:nth-child(4) { height: 13px; }
.s-net[data-level="4"] i, .s-net[data-level="3"] i:nth-child(-n+3), .s-net[data-level="2"] i:nth-child(-n+2), .s-net[data-level="1"] i:nth-child(1) { background: currentColor; }
.s-net[data-level="4"], .s-net[data-level="3"] { color: var(--ok); }
.s-net[data-level="2"] { color: var(--warn); }
.s-net[data-level="1"] { color: #ff7b72; }
.s-net-text { margin-left: 5px; align-self: center; font-variant-numeric: tabular-nums; }
.session.reconnecting .dot.live { background: var(--warn); box-shadow: 0 0 0 3px rgba(210,153,34,.25); }
.overlay-actions { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.cards-empty { grid-column: 1 / -1; margin: 0; padding: 14px; border: 1px dashed var(--border); border-radius: var(--radius); }
.embedded .sidebar > .brand { display: none; }
.embedded .sidebar .tabs { margin-top: 0; }
.embedded-only { display: none; }
.embedded .embedded-only { display: block; color: var(--text); }
