:root {
  --bg: #0e1016;
  --surface: #151823;
  --surface-2: #1b1f2c;
  --surface-3: #232838;
  --border: #272c3b;
  --text: #e6e8ee;
  --muted: #8b93a7;
  --dim: #5b6274;
  --accent: #7c8cff;
  --accent-strong: #5c64e6;
  --accent-text: #0b0d16;
  --danger: #f87171;
  --ok: #4ade80;
  --warn: #fbbf24;
  --radius: 14px;
  --sidebar-w: 300px;
  --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);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  font-size: 15px;
  line-height: 1.4;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.hidden { display: none !important; }
.mono { font-family: ui-monospace, "JetBrains Mono", "Fira Code", Menlo, Consolas, "DejaVu Sans Mono", "Noto Sans Mono", monospace; }

#app {
  position: fixed;
  left: 0; top: 0;
  width: 100%;
  height: 100%;
  height: var(--app-h, 100%);
  display: flex;
  overflow: hidden;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px;
  background: var(--surface-3); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font-weight: 500; white-space: nowrap;
  transition: background .12s, opacity .12s;
}
.btn:hover { background: #2a3044; }
.btn:active { opacity: .8; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover { background: #8e9cff; }
.btn.danger { background: transparent; color: var(--danger); border-color: rgba(248,113,113,.35); }
.btn.danger:hover { background: rgba(248,113,113,.1); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-3); color: var(--text); }
.btn.block { width: 100%; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 10px;
  background: transparent; border: 0; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.active { color: var(--accent); background: rgba(124,140,255,.12); }

/* ---------- sidebar ---------- */
#sidebar {
  position: fixed; left: 0; top: 0; bottom: 0; z-index: 30;
  width: min(var(--sidebar-w), 86vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(-102%);
  transition: transform .22s cubic-bezier(.2,.8,.2,1);
  padding-top: var(--sat);
  padding-left: var(--sal);
}
#sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.5); }
#backdrop {
  position: fixed; inset: 0; z-index: 25;
  background: rgba(0,0,0,.5);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#backdrop.show { opacity: 1; pointer-events: auto; }
.sb-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 12px 8px;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 16px; flex: 1; }
.brand img { width: 26px; height: 26px; border-radius: 7px; }
.sb-new { margin: 4px 12px 8px; }
.sb-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; overscroll-behavior: contain; }
.sb-section { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); padding: 10px 8px 4px; }
.sess {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 12px;
  cursor: pointer; user-select: none; -webkit-user-select: none;
  border: 1px solid transparent;
}
.sess:hover { background: var(--surface-2); }
.sess.active { background: var(--surface-3); border-color: var(--border); }
.sess .avatar { flex: none; }
.sess .body { flex: 1; min-width: 0; }
.sess .title { font-weight: 550; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sess .preview { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.sess .meta { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 11px; color: var(--dim); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); }
.dot.on { background: var(--ok); box-shadow: 0 0 6px rgba(74,222,128,.6); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface-3); color: #fff;
}
.avatar svg { width: 20px; height: 20px; }
.sb-foot { padding: 8px 12px calc(10px + var(--sab)); border-top: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
.sb-foot .who { flex: 1; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty-list { color: var(--dim); font-size: 13px; padding: 24px 12px; text-align: center; }

/* ---------- main ---------- */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#topbar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(6px + var(--sat)) calc(6px + var(--sar)) 6px calc(6px + var(--sal));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 52px;
}
#topbar .ttl { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; padding: 0 4px; }
#topbar .ttl .name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#topbar .ttl .sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
#topbar .ttl .sub .dot { width: 7px; height: 7px; }
.sub .dot.warn { background: var(--warn); }
.sub .dot.bad { background: var(--danger); }

#term-wrap {
  flex: 1; min-height: 0; position: relative;
  background: var(--bg);
}
#terminal { position: absolute; inset: 0; padding: 6px 4px 0 8px; touch-action: none; }
#terminal .xterm { height: 100%; }
#terminal .xterm .xterm-viewport { overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: #2c3243 transparent; }
#terminal .xterm .xterm-viewport::-webkit-scrollbar { width: 6px; }
#terminal .xterm .xterm-viewport::-webkit-scrollbar-thumb { background: #2c3243; border-radius: 3px; }
#term-wrap.dimmed #terminal { opacity: .5; }

#empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px; text-align: center; color: var(--muted);
}
#empty img { width: 72px; height: 72px; border-radius: 18px; margin-bottom: 6px; }
#empty h2 { margin: 0; color: var(--text); font-size: 20px; }
#empty p { margin: 0; max-width: 360px; font-size: 14px; }

#scroll-down {
  position: absolute; right: 14px; bottom: 12px; z-index: 5;
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--surface-3); border: 1px solid var(--border); color: var(--text);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
#scroll-down svg { width: 20px; height: 20px; }

#conn-banner {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); z-index: 6;
  background: var(--surface-3); border: 1px solid var(--border); color: var(--muted);
  padding: 6px 12px; border-radius: 999px; font-size: 12px;
  display: flex; align-items: center; gap: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
#conn-banner.bad { color: var(--danger); }

/* ---------- key bar ---------- */
#keybar {
  display: flex; gap: 6px; padding: 6px 8px 2px;
  overflow-x: auto; overscroll-behavior-x: contain;
  scrollbar-width: none;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding-left: calc(8px + var(--sal)); padding-right: calc(8px + var(--sar));
}
#keybar::-webkit-scrollbar { display: none; }
.key {
  flex: none;
  min-width: 42px; height: 34px; padding: 0 10px;
  border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted);
  font-size: 13px; font-weight: 500;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.key:active, .key.pressed { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.key.sticky { color: var(--accent); border-color: var(--accent); }

/* ---------- composer ---------- */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 6px calc(8px + var(--sar)) calc(8px + var(--sab)) calc(8px + var(--sal));
  background: var(--bg);
}
.box {
  flex: 1; display: flex; align-items: flex-end; gap: 4px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 22px; padding: 4px 4px 4px 8px;
  transition: border-color .12s;
}
.box:focus-within { border-color: #3d4560; }
#input {
  flex: 1; resize: none; border: 0; outline: 0; background: transparent;
  min-height: 36px; max-height: 160px; padding: 8px 6px;
  font-size: 16px; line-height: 20px; color: var(--text);
}
#input::placeholder { color: var(--dim); }
.box .icon-btn { width: 36px; height: 36px; border-radius: 50%; flex: none; }
#send {
  width: 36px; height: 36px; border-radius: 50%; flex: none;
  background: var(--accent); color: var(--accent-text); border: 0;
  display: inline-flex; align-items: center; justify-content: center;
}
#send:hover { background: #8e9cff; }
#send svg { width: 20px; height: 20px; }
#send.enter { background: var(--surface-3); color: var(--text); }
#send:disabled { opacity: .4; }

/* ---------- menus ---------- */
.menu {
  position: absolute; z-index: 40; min-width: 200px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5); padding: 6px;
}
.menu button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; border: 0; background: transparent; color: var(--text);
  border-radius: 8px; text-align: left; font-size: 14px;
}
.menu button:hover { background: var(--surface-3); }
.menu button.danger { color: var(--danger); }
.menu button svg { width: 18px; height: 18px; color: var(--muted); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }

/* ---------- modals ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center;
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
}
.sheet {
  width: 100%; max-height: min(92%, calc(100% - 20px));
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  display: flex; flex-direction: column;
  animation: up .2s cubic-bezier(.2,.8,.2,1);
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 8px; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 17px; }
.sheet-body { flex: 1; overflow-y: auto; padding: 4px 16px 12px; overscroll-behavior: contain; }
.sheet-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 16px calc(14px + var(--sab)); border-top: 1px solid var(--border); }
.sheet-foot .btn { min-width: 96px; }

.field { margin: 10px 0 14px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; font-weight: 500; }
.field .row { display: flex; gap: 8px; }
.field .row input { flex: 1; min-width: 0; }
input[type=text], input[type=password], input[type=number] {
  width: 100%; min-height: 42px; padding: 8px 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  outline: 0; font-size: 16px;
}
input[type=text]:focus, input[type=password]:focus { border-color: var(--accent); }
.help { font-size: 12px; color: var(--dim); margin-top: 6px; }
.err { color: var(--danger); font-size: 13px; margin: 6px 0; min-height: 1em; }

.tool-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin: 8px 0 4px; }
.tool-card {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  text-align: left; color: var(--text);
}
.tool-card .avatar { width: 32px; height: 32px; }
.tool-card .avatar svg { width: 18px; height: 18px; }
.tool-card .n { font-weight: 550; font-size: 14px; }
.tool-card .d { font-size: 11px; color: var(--muted); }
.tool-card.sel { border-color: var(--accent); background: rgba(124,140,255,.1); }
.tool-card:disabled { opacity: .45; cursor: not-allowed; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  padding: 5px 10px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip:hover { color: var(--text); border-color: #3d4560; }

.check { display: flex; align-items: center; gap: 10px; padding: 8px 0; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; flex: none; }
.check .lbl { flex: 1; }
.check .sub { display: block; font-size: 12px; color: var(--dim); }

.dir-path { font-size: 13px; color: var(--muted); word-break: break-all; padding: 6px 0; }
.dir-list { display: flex; flex-direction: column; gap: 2px; }
.dir-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 8px;
  border: 0; border-radius: 10px; background: transparent; color: var(--text); text-align: left; font-size: 14px;
}
.dir-item:hover { background: var(--surface-2); }
.dir-item svg { width: 18px; height: 18px; color: var(--muted); flex: none; }
.dir-item.up { color: var(--muted); }

.section { margin: 14px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); }
.setting-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.setting-row:last-child { border-bottom: 0; }
.setting-row .lbl { flex: 1; }
.setting-row .lbl .sub { display: block; font-size: 12px; color: var(--dim); }
.stepper { display: inline-flex; align-items: center; gap: 4px; }
.stepper .val { min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--surface-3); border-radius: 999px; transition: .15s; border: 1px solid var(--border); }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: .15s; }
.switch input:checked + span { background: var(--accent); border-color: transparent; }
.switch input:checked + span::after { transform: translateX(18px); }
.login-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.login-row .lbl { flex: 1; min-width: 0; }
.login-row .ua { color: var(--dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qr { display: block; width: 200px; height: 200px; margin: 10px auto; border-radius: 10px; background: #fff; padding: 8px; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 13px; word-break: break-all; text-align: center; color: var(--muted); }
pre.screen { white-space: pre-wrap; word-break: break-word; font-size: 12px; line-height: 1.35; margin: 0; user-select: text; -webkit-user-select: text; }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--sab)); transform: translateX(-50%) translateY(10px);
  z-index: 60; background: #2a3044; color: var(--text); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw;
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: rgba(248,113,113,.4); color: #fecaca; }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 26px 22px; }
.login-card .brand { justify-content: center; margin-bottom: 18px; font-size: 20px; }
.login-card .brand img { width: 40px; height: 40px; border-radius: 10px; }
.login-card .btn { margin-top: 8px; }

/* ---------- desktop ---------- */
@media (min-width: 900px) {
  #sidebar { position: relative; transform: none; box-shadow: none; flex: none; width: var(--sidebar-w); }
  #backdrop { display: none; }
  #menu-btn { display: none; }
  .modal { align-items: center; }
  .sheet { width: min(560px, 92vw); max-height: 88vh; border-radius: 18px; }
  .sheet-foot { padding-bottom: 14px; }
  #input { font-size: 15px; }
  #keybar { padding-bottom: 4px; }
}
@media (hover: none) {
  .btn:hover, .icon-btn:hover, .menu button:hover, .sess:hover, .dir-item:hover, .chip:hover { background: inherit; }
  .sess.active { background: var(--surface-3); }
}
