:root {
  --bg: #0b1016;
  --panel: #111922;
  --panel2: #16212c;
  --line: #22303d;
  --text: #d6e0ea;
  --muted: #7d8e9e;
  --accent: #3ddc97;
  --accent-ink: #062016;
  --danger: #ff6b6b;
  --warn: #f5c451;
  --radius: 8px;
  --mono: ui-monospace, "JetBrains Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body, #root { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; background: var(--bg); }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; overscroll-behavior: none; touch-action: manipulation;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; }
a { color: var(--accent); }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------- controls ---------- */
.btn {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #33485a; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.btn.icon { padding: 4px 8px; }
.btn.on { border-color: var(--warn); color: var(--warn); }
input[type=text], input[type=password], input[type=number], input:not([type]), select, textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { font-family: var(--mono); font-size: 12px; resize: vertical; }
label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; }
label.check input { width: auto; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.err { color: var(--danger); margin-top: 10px; min-height: 1.2em; }

/* ---------- login ---------- */
.auth-wrap { height: 100%; display: grid; place-items: center; padding: 16px; overflow: auto; }
.auth-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; display: flex; gap: 10px; align-items: center; }
.auth-card h1 img { width: 28px; height: 28px; }
.auth-card .btn.primary { width: 100%; margin-top: 18px; padding: 9px; }
.auth-card .qr { display: block; margin: 14px auto; background: #fff; padding: 8px; border-radius: 8px; width: 200px; height: 200px; }
.code-input { font-family: var(--mono); font-size: 22px; letter-spacing: 6px; text-align: center; }

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: var(--sbw, 270px) minmax(0, 1fr); grid-template-rows: calc(46px + var(--sat)) 1fr; height: 100%; padding-bottom: var(--sab); }
@supports (height: 100dvh) { .app { height: var(--vh, 100dvh); } }
.topbar {
  grid-column: 1 / -1; display: flex; align-items: center; gap: 6px;
  padding: var(--sat) max(10px, var(--sar)) 0 max(10px, var(--sal));
  background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0;
}
.brand { display: flex; gap: 8px; align-items: center; font-weight: 700; margin-right: 8px; }
.brand img { width: 22px; height: 22px; }
.topbar .spacer { flex: 1; }
.topbar .nav { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.topbar .nav .btn { background: transparent; border-color: transparent; }
.topbar .nav .btn:hover { background: var(--panel2); }
#menuToggle { display: none; }

.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar { position: relative; }
.sidebar .tools { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
.sidebar .tools input { flex: 1; }
.tree { overflow: auto; flex: 1; padding: 6px 0 20px; }
.grp > .grp-head {
  display: flex; align-items: center; gap: 6px; padding: 5px 10px; cursor: pointer; color: var(--muted);
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em; user-select: none;
}
.grp > .grp-head:hover { color: var(--text); }
.grp > .grp-head .caret { width: 10px; transition: transform .15s; }
.grp.collapsed > .grp-head .caret { transform: rotate(-90deg); }
.grp.collapsed > .grp-body { display: none; }
.grp-head .acts, .host .acts { margin-left: auto; display: none; gap: 2px; }
.grp-head:hover .acts, .host:hover .acts { display: flex; }
.grp-body { padding-left: 10px; }
.host {
  display: flex; align-items: center; gap: 8px; padding: 6px 9px; cursor: pointer; border-radius: 7px; margin: 1px 6px;
  border-left: 2px solid transparent; transition: background .12s ease, border-color .12s ease;
}
.host:hover { background: var(--panel2); }
.host.active { background: color-mix(in srgb, var(--accent) 14%, var(--panel)); border-left-color: var(--accent); }
.host .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.host .sub { font-size: 11px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #556673; flex: none; }
.dot.up { background: var(--accent); box-shadow: 0 0 6px color-mix(in srgb, var(--accent) 65%, transparent); }
.dot.down { background: var(--danger); }
.proto { font-size: 10px; font-weight: 600; font-family: var(--mono); padding: 1px 6px; border-radius: 5px; letter-spacing: .03em; flex: none; border: 1px solid transparent; background: var(--panel2); color: var(--muted); }
.proto.ssh { color: #2fbf84; background: color-mix(in srgb, #2fbf84 15%, transparent); border-color: color-mix(in srgb, #2fbf84 28%, transparent); }
.proto.rdp { color: #7fb8ff; background: color-mix(in srgb, #7fb8ff 15%, transparent); border-color: color-mix(in srgb, #7fb8ff 28%, transparent); }
.proto.vnc { color: #d39cff; background: color-mix(in srgb, #d39cff 15%, transparent); border-color: color-mix(in srgb, #d39cff 28%, transparent); }
.mini { background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 2px 5px; border-radius: 4px; font-size: 13px; }
.mini:hover { color: var(--text); background: var(--line); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tabs { display: flex; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; scrollbar-width: thin; }
.tab {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px 8px 12px; border-right: 1px solid var(--line);
  cursor: pointer; white-space: nowrap; color: var(--muted); max-width: 220px;
}
.tab .t { overflow: hidden; text-overflow: ellipsis; }
.tab.active { color: var(--text); background: var(--bg); box-shadow: inset 0 2px 0 var(--accent); }
.tab .x { opacity: .6; }
.tab .x:hover { opacity: 1; }
.tab .st { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); flex: none; }
.tab .st.ok { background: var(--accent); }
.tab .st.bad { background: var(--danger); }
.panes { flex: 1; position: relative; min-height: 0; }
.pane { position: absolute; inset: 0; display: none; flex-direction: column; min-height: 0; }
.pane.active { display: flex; }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 20px; }
.empty h2 { color: var(--text); font-weight: 600; }

/* terminal */
.term-bar { display: flex; gap: 6px; align-items: center; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.term-bar select { width: auto; padding: 3px 6px; font-size: 12px; }
.term-wrap { flex: 1; min-height: 0; padding: 4px 0 0 6px; background: #0b1016; }
.term-wrap .xterm { height: 100%; }
.keybar { display: none; gap: 4px; padding: 6px; overflow-x: auto; background: var(--panel); border-top: 1px solid var(--line); flex: none; }
.keybar .btn { padding: 6px 10px; font-family: var(--mono); font-size: 13px; }
@media (pointer: coarse) { .keybar { display: flex; } }

/* guac */
.guac-stage { flex: 1; min-height: 0; display: flex; position: relative; }
.guac-wrap.drop { outline: 2px dashed var(--accent); outline-offset: -6px; }
.guac-files { width: 320px; flex: none; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.gf-head, .gf-tools { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.gf-list { flex: 1; overflow: auto; min-height: 0; }
.gf-row { display: flex; gap: 8px; align-items: center; padding: 6px 10px; cursor: pointer; font-size: 13px; }
.gf-row:hover { background: var(--panel2); }
.gf-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gf-jobs { border-top: 1px solid var(--line); max-height: 30%; overflow: auto; }
.gf-job { padding: 6px 10px; }
.gf-job.bad .small { color: var(--danger); }
.gf-track { height: 4px; background: var(--line); border-radius: 2px; margin-top: 4px; overflow: hidden; }
.gf-bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.gf-job.bad .gf-bar { background: var(--danger); }
.gf-hint { padding: 8px 10px; border-top: 1px solid var(--line); }
@media (max-width: 760px) { .guac-files { position: absolute; right: 0; top: 0; bottom: 0; width: min(320px, 90vw); z-index: 5; } }
.guac-wrap { flex: 1; min-height: 0; overflow: hidden; background: #000; display: grid; place-items: center; outline: none; position: relative; }
.guac-wrap .msg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); pointer-events: none; text-align: center; padding: 20px; }

.guac-frame { flex: 1; min-height: 0; width: 100%; border: 0; background: #000; }

/* generic page */
.page { overflow: auto; padding: 18px; flex: 1; }
.page h2 { margin: 0 0 14px; font-size: 18px; }
.page .toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.page .toolbar input { max-width: 320px; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th, table.grid td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.grid th { color: var(--muted); font-weight: 500; font-size: 12px; position: sticky; top: 0; background: var(--bg); }
table.grid tr.click { cursor: pointer; }
table.grid tr.click:hover td { background: var(--panel); }
table.grid td.wrap { word-break: break-all; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; max-width: 760px; }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.keybox { font-family: var(--mono); font-size: 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 10px; word-break: break-all; }

/* sftp */
.sftp-path { display: flex; gap: 6px; align-items: center; flex: 1; min-width: 200px; }
.sftp-path input { font-family: var(--mono); font-size: 12px; }
.sftp-drop { outline: 2px dashed transparent; outline-offset: -8px; }
.sftp-drop.over { outline-color: var(--accent); }
.fname { display: flex; gap: 6px; align-items: center; }
.ficon { width: 16px; text-align: center; color: var(--muted); }

/* player */
.player-wrap { display: grid; grid-template-columns: 1fr 320px; gap: 14px; flex: 1; min-height: 0; padding: 14px; }
.player-wrap .ap { min-width: 0; overflow: auto; }
.cmdlist { overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.cmdlist .c { padding: 6px 10px; border-bottom: 1px solid var(--line); cursor: pointer; font-family: var(--mono); font-size: 12px; word-break: break-all; }
.cmdlist .c:hover { background: var(--panel2); }
.cmdlist .c .tm { color: var(--muted); margin-right: 8px; }

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: 100%; max-width: 560px; max-height: calc(100dvh - 32px - var(--sat)); display: flex; flex-direction: column; }
.modal .mh { padding: 14px 18px; border-bottom: 1px solid var(--line); font-weight: 600; display: flex; }
.modal .mh .x { margin-left: auto; }
.modal .mb { padding: 4px 18px 18px; overflow: auto; }
.modal .mf { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; flex-wrap: wrap; }
.modal .mf .spacer { flex: 1; }

.toast { position: fixed; bottom: calc(18px + var(--sab)); right: 18px; background: var(--panel2); border: 1px solid var(--line); border-left: 3px solid var(--accent); padding: 10px 14px; border-radius: 8px; z-index: 60; max-width: min(420px, calc(100vw - 36px)); box-shadow: 0 8px 30px rgba(0,0,0,.4); }
.toast.bad { border-left-color: var(--danger); }

/* mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  #menuToggle { display: inline-block; }
  .sidebar { position: fixed; top: calc(46px + var(--sat)); left: 0; bottom: var(--sab); width: min(300px, 86vw); z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .app.menu-open .sidebar { transform: none; }
  .brand span { display: none; }
  .player-wrap { grid-template-columns: 1fr; grid-template-rows: auto 220px; }
  .page { padding: 12px; }
  .hide-m { display: none; }
  .grp-head .acts, .host .acts { display: flex; }
}

/* live host stats panel */
.statbox { display: flex; flex-direction: column; gap: 10px; min-width: 320px; }
.statkv { display: flex; justify-content: space-between; gap: 16px; font-size: 13px; }
.statrow { display: flex; align-items: center; gap: 10px; }
.statlabel { flex: 0 0 150px; display: flex; flex-direction: column; font-size: 12px; gap: 2px; }
.statbar { flex: 1; height: 10px; border-radius: 999px; background: var(--panel2); border: 1px solid var(--line); overflow: hidden; }
.statfill { height: 100%; background: var(--accent); transition: width .3s ease; }
.statfill.warn { background: #d99a2b; }
.statfill.hot { background: var(--danger); }
.bad-msg { color: var(--danger); font-size: 13px; }

/* split screen: two panes side by side */
.panes.split { display: flex; flex-direction: row; }
.panes.split > .pane { position: static; inset: auto; display: none; }
.panes.split > .pane.active,
.panes.split > .pane.split-second { position: relative; display: flex; flex: 1 1 0; min-width: 0; }
.panes.split > .pane.split-second { border-left: 2px solid var(--line); }
@media (max-width: 700px) { .panes.split { flex-direction: column; } .panes.split > .pane.split-second { border-left: 0; border-top: 2px solid var(--line); } }
.tab.split { box-shadow: inset 0 -2px 0 var(--accent); }
.tabsplit { flex: none; margin-left: 4px; padding: 2px 8px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--muted); cursor: pointer; font-size: 12px; letter-spacing: -2px; }
.tabsplit:hover { background: var(--panel2); }
.tabsplit.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* file upload progress bar */
.upbar { height: 6px; border-radius: 999px; background: var(--panel2); border: 1px solid var(--line); overflow: hidden; margin: 6px 0; }
.upfill { height: 100%; width: 0; background: var(--accent); transition: width .15s ease; }

.grid tr.fdrag { cursor: grab; }
.grid tr.fdrag:active { cursor: grabbing; }

.gf-row .gf-check { flex: none; margin-right: 2px; }

/* quick-connect palette */
.qc-modal { max-width: 560px; width: 100%; padding: 0; overflow: hidden; }
.qc-head { padding: 12px; border-bottom: 1px solid var(--line); }
.qc-head input { width: 100%; }
.qc-list { max-height: 60vh; overflow: auto; }
.qc-row { display: flex; align-items: center; gap: 10px; padding: 8px 12px; cursor: pointer; }
.qc-row.active { background: var(--panel2); }
.qc-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qc-sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 45%; }
.qc-empty { padding: 14px; }

/* fan-out */
.fanout-out { margin-top: 12px; }
.fo-item { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 6px; background: var(--panel); }
.fo-item summary { padding: 8px 10px; cursor: pointer; display: flex; align-items: center; gap: 6px; list-style: none; }
.fo-item summary::-webkit-details-marker { display: none; }
.fo-pre { margin: 0; padding: 10px; border-top: 1px solid var(--line); background: var(--panel2); overflow: auto; max-height: 360px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-word; }
/* dashboard */
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; margin-top: 12px; }
.dash-card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 12px; }
.dash-h { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.dash-h .mono { margin-left: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 55%; }
.dash-body { display: flex; flex-direction: column; gap: 6px; }

/* iOS / touch polish */
:root.kb-open { --sab: 0px; } /* on-screen keyboard covers the home indicator */
.auth-wrap { padding: max(16px, var(--sat)) max(16px, var(--sar)) max(16px, var(--sab)) max(16px, var(--sal)); }
@media (pointer: coarse) {
  .tab, .host, .grp-head, .btn, .keybar, .tabs, .topbar { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
  .btn { min-height: 36px; }
  .btn.sm { min-height: 30px; }
  .mini { padding: 6px 9px; }
  .tab .x { padding: 4px 6px; }
}
/* iOS Safari zooms the page when focusing inputs below 16px */
@supports (-webkit-touch-callout: none) {
  input, select, textarea, .term-bar select { font-size: 16px !important; }
  .code-input { font-size: 22px !important; }
  .term-wrap .xterm-helper-textarea { font-size: 16px !important; }
}
:root.kb-open .app { height: var(--vh); }
/* phone: keep the terminal toolbar to one scrollable row so the keyboard leaves room for the terminal */
@media (max-width: 760px) {
  .term-bar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .term-bar::-webkit-scrollbar { display: none; }
  .term-bar > * { flex: none; }
  .term-bar > span.mono { flex: 0 1 auto; min-width: 0; max-width: 38vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .term-wrap { padding-left: max(6px, var(--sal)); }
}

/* resizable sidebar (desktop): drag the thin handle on its right edge */
.sb-resizer { position: absolute; top: 0; right: -4px; bottom: 0; width: 9px; cursor: col-resize; z-index: 8; touch-action: none; }
.sb-resizer::after { content: ""; position: absolute; top: 0; bottom: 0; left: 4px; width: 1px; background: transparent; transition: background .12s ease; }
.sb-resizer:hover::after, .sb-resizer:focus-visible::after, .sb-resizer.drag::after { background: var(--accent); width: 2px; left: 3px; }
.sb-resizer:focus-visible { outline: none; }
body.sb-resizing { cursor: col-resize; user-select: none; -webkit-user-select: none; }
body.sb-resizing iframe { pointer-events: none; }
@media (max-width: 760px) { .sb-resizer { display: none; } }

/* favorites */
.mini.fav.on { color: var(--warn); }
.favmark { color: var(--warn); font-size: 11px; flex: none; margin-left: -2px; }
.fav-grp > .grp-head { color: var(--warn); }

/* instance label (e.g. TEST) shown next to the brand */
.instance-badge { background: var(--warn); color: #201800; font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 5px; letter-spacing: .04em; margin-left: 2px; align-self: center; }
