:root {
  color-scheme: dark;
  --bg: #0d1014;
  --bg-2: #13181f;
  --panel: #171c24;
  --panel-2: #1f2630;
  --line: #2a313d;
  --text: #e5e9f0;
  --muted: #8b95a5;
  --accent: #5aa9ff;
  --accent-ink: #06121f;
  --ok: #3ccf91;
  --warn: #f0b429;
  --danger: #ff6b6b;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, "Liberation Mono",
    "Noto Sans Mono CJK SC", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.5 var(--sans); -webkit-text-size-adjust: 100%; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.logo { color: var(--accent); margin-right: 2px; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 6px 14px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel-2); color: var(--text); font: inherit; font-size: 14px;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3a4352; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.link-btn {
  appearance: none; background: none; border: 0; padding: 0; color: var(--muted);
  font: inherit; font-size: 13px; text-decoration: underline; cursor: pointer;
}

/* ---------- top bar (list page) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(13, 16, 20, .92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 650; white-space: nowrap; }
.topbar .host { font-family: var(--mono); font-weight: 400; font-size: 13px; margin-left: 6px; }
.topbar .spacer { flex: 1; }
.topbar form { margin: 0; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 16px; }
.banner {
  margin: 0 0 12px; padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel);
}
.banner.error { border-color: #5c2b2b; background: #241414; color: #ffb4b4; }

/* ---------- session cards ---------- */
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 360px), 1fr)); }
.card {
  display: flex; flex-direction: column; gap: 10px; min-width: 0;
  padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel);
}
.card-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; min-width: 0; }
.card-name {
  font: 600 17px/1.3 var(--mono); color: var(--text); text-decoration: none;
  overflow-wrap: anywhere;
}
.card-sock { font: 12px var(--mono); color: var(--muted); }
.card-time { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--line); font-size: 12px; color: var(--muted); white-space: nowrap;
}
.badge.on { color: var(--ok); border-color: #1f4d3a; }
.badge.on::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--ok); }
.badge.mode { color: var(--warn); border-color: #5a4614; }
.card-sub { display: flex; gap: 8px; min-width: 0; font-size: 13px; }
.card-cmd { font-family: var(--mono); color: var(--text); white-space: nowrap; }
.card-path {
  font-family: var(--mono); color: var(--muted); min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: rtl; text-align: left;
}
.preview {
  margin: 0; padding: 8px 10px; min-height: 3em; border-radius: 8px; border: 1px solid var(--line);
  background: #07090c; color: #c9d1d9; font: 12px/1.4 var(--mono); white-space: pre;
  overflow: hidden; text-decoration: none; display: block;
  -webkit-mask-image: linear-gradient(to right, #000 88%, transparent);
  mask-image: linear-gradient(to right, #000 88%, transparent);
}
.preview:empty::before { content: "（空）"; color: var(--muted); }
.card-actions { display: flex; gap: 8px; }
.card-actions .btn { flex: 1; }
.empty { margin: 48px 0; text-align: center; color: var(--muted); }
.foot { display: flex; justify-content: center; gap: 16px; margin: 28px 0 8px; }
.foot form { margin: 0; }

/* ---------- dialogs ---------- */
dialog {
  width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 0;
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
.dlg { display: flex; flex-direction: column; gap: 12px; padding: 18px; }
.dlg h2 { margin: 0; font-size: 17px; }
.dlg label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.dlg input, .dlg select, .dlg textarea {
  width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #0b0e12; color: var(--text); font: 15px var(--mono);
}
.dlg textarea { min-height: 32dvh; resize: vertical; line-height: 1.45; }
.dlg .row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.dlg .err { margin: 0; color: var(--danger); font-size: 13px; }
.dlg .err:empty { display: none; }
.dlg .hint { margin: -4px 0 0; font-size: 12px; color: var(--muted); }
.text-view {
  margin: 0; max-height: 62dvh; overflow: auto; padding: 10px; border-radius: 8px;
  border: 1px solid var(--line); background: #07090c; font: 12px/1.4 var(--mono);
  white-space: pre; -webkit-user-select: text; user-select: text;
}

/* ---------- login ---------- */
body.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); padding: 28px 24px; border: 1px solid var(--line);
  border-radius: 14px; background: var(--panel);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p.sub { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; }
.login-card label { font-size: 13px; color: var(--muted); }
.login-card input[type=password] {
  padding: 11px 12px; border: 1px solid var(--line); border-radius: 8px; background: #0b0e12;
  color: var(--text); font-size: 16px;
}
.login-card .btn { min-height: 44px; font-size: 15px; margin-top: 6px; }
.login-card .error { margin: 0; color: var(--danger); font-size: 14px; }
.login-card .error:empty { display: none; }

/* ---------- terminal page ---------- */
body.term { overflow: hidden; overscroll-behavior: none; }
.app {
  position: fixed; left: 0; top: 0; width: 100%; height: var(--app-h, 100dvh);
  display: flex; flex-direction: column; background: #000;
}
.tbar {
  flex: none; display: flex; align-items: center; gap: 2px; min-height: 40px;
  padding: 3px 6px; padding-top: max(3px, env(safe-area-inset-top));
  background: var(--bg-2); border-bottom: 1px solid var(--line);
}
.tbar .title {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; padding: 0 4px;
  font-family: var(--mono); font-size: 14px; overflow: hidden; white-space: nowrap;
}
.tbar .title .name { overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  appearance: none; flex: none; display: inline-flex; align-items: center; justify-content: center;
  min-width: 34px; height: 34px; padding: 0 8px; border: 0; border-radius: 7px;
  background: transparent; color: var(--text); font: 14px var(--sans); text-decoration: none;
  cursor: pointer; touch-action: manipulation;
}
.icon-btn:hover { background: var(--panel-2); }
.icon-btn.on { color: var(--accent); }
#frame { flex: 1; display: block; width: 100%; min-height: 0; border: 0; background: #000; }
.notice {
  flex: none; padding: 8px 12px; font-size: 13px; background: #2a2210; color: #ffd98a;
  border-bottom: 1px solid #5a4614;
}
.notice a { color: inherit; font-weight: 600; }
.keys {
  flex: none; display: flex; flex-wrap: wrap; gap: 4px; padding: 5px;
  padding-bottom: calc(5px + env(safe-area-inset-bottom));
  background: var(--bg-2); border-top: 1px solid var(--line);
  -webkit-user-select: none; user-select: none; touch-action: none;
}
.keys button {
  appearance: none; flex: 1 0 38px; min-width: 38px; height: 38px; padding: 0 4px;
  border: 1px solid var(--line); border-radius: 7px; background: var(--panel-2);
  color: var(--text); font: 13px var(--mono); cursor: pointer;
  touch-action: none; /* a slight finger slide must not turn the tap into a pan (pointercancel) */
  -webkit-tap-highlight-color: transparent;
}
.keys button.down { background: #2b3442; }
.keys button.armed { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 20;
  max-width: calc(100vw - 32px); padding: 8px 14px; border-radius: 8px;
  background: #222a35; border: 1px solid var(--line); color: var(--text); font-size: 13px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .4);
}

@media (min-width: 900px) {
  .keys button { flex: 0 0 auto; min-width: 46px; }
}
