:root {
  --bg: #0a0d13;
  --bg-2: #0e131b;
  --surface: #141a24;
  --surface-2: #1b2331;
  --surface-3: #222c3d;
  --line: rgba(255, 255, 255, .08);
  --line-2: rgba(255, 255, 255, .13);
  --txt: #e7ecf3;
  --muted: #808b9e;
  --faint: #5b6577;
  --accent: #5b8cff;
  --accent-2: #7aa2ff;
  --accent-dim: rgba(91, 140, 255, .16);
  --danger: #f0555f;
  --ok: #37d399;
  --warn: #f2b34b;
  --shadow: 0 12px 40px rgba(0, 0, 0, .45);
  --r: 14px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg);
  color: var(--txt);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
body { display: flex; flex-direction: column; }
.hidden { display: none !important; }
.screen {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  animation: fade .22s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
button { font-family: inherit; cursor: pointer; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: linear-gradient(180deg, var(--bg-2), rgba(14, 19, 27, .82));
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  min-height: 56px; z-index: 10;
}
.icon-btn {
  display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--txt); width: 40px; height: 40px; border-radius: 11px;
  font-size: 20px; line-height: 1; transition: background .15s, transform .1s;
}
.icon-btn:active { background: var(--surface-3); transform: scale(.94); }
.term-title { flex: 1; font-weight: 650; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.env-select {
  flex: 1; appearance: none;
  background: var(--surface-2) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%23808b9e' d='M6 8.5 1.5 4h9z'/></svg>") no-repeat right 12px center;
  color: var(--txt); border: 1px solid var(--line); border-radius: 11px;
  padding: 10px 34px 10px 12px; font-size: 15px; font-weight: 550;
}

/* pílula de status */
.status {
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--surface-2); border: 1px solid var(--line);
  padding: 7px 11px 7px 10px; border-radius: 20px; font-size: 12.5px; color: var(--muted);
  font-weight: 550; white-space: nowrap;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: 0 0 auto; }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px rgba(55, 211, 153, .22); }
.dot.offline { background: var(--danger); box-shadow: 0 0 0 3px rgba(240, 85, 95, .18); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
.dot:not(.online):not(.offline) { animation: pulse 1.2s ease-in-out infinite; background: var(--warn); }

/* ---------- login ---------- */
#login { justify-content: center; background: radial-gradient(120% 60% at 50% -10%, rgba(91,140,255,.14), transparent 60%), var(--bg); }
.login-card {
  margin: auto; width: min(430px, 92vw); padding: 26px 22px 24px;
  background: linear-gradient(180deg, var(--surface), var(--bg-2));
  border: 1px solid var(--line-2); border-radius: 22px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.logo {
  width: 46px; height: 46px; border-radius: 13px; display: grid; place-items: center;
  font-size: 24px; background: linear-gradient(145deg, var(--accent), #8b5bff);
  box-shadow: 0 8px 20px rgba(91,140,255,.35);
}
.brand h1 { margin: 0; font-size: 21px; font-weight: 720; letter-spacing: -.01em; }
.brand .sub { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

label { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; color: var(--muted); font-weight: 600; }
input, select { font-family: inherit; }
input {
  width: 100%; min-width: 0; /* min-width:0 deixa o input encolher dentro do flex */
  background: var(--surface-2); border: 1px solid var(--line); color: var(--txt);
  border-radius: 11px; padding: 12px 13px; font-size: 16px; transition: border-color .15s, box-shadow .15s;
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
input::placeholder { color: var(--faint); }
.row { display: flex; gap: 10px; }
.row label { flex: 1; min-width: 0; }

.btn {
  border: 1px solid var(--line-2); background: var(--surface-2); color: var(--txt);
  border-radius: 12px; padding: 12px 14px; font-size: 15px; font-weight: 650;
  transition: transform .1s, background .15s, filter .15s;
}
.btn:active { transform: scale(.97); }
.btn.primary {
  background: linear-gradient(145deg, var(--accent), #6f7bff); border-color: transparent; color: #fff;
  box-shadow: 0 6px 18px rgba(91,140,255,.3);
}
.btn.primary:active { filter: brightness(.95); }
.btn.danger { background: var(--danger); border-color: transparent; color: #fff; }
.btn.ghost { background: transparent; }
.msg { color: var(--danger); font-size: 13px; min-height: 18px; margin: 0; }

/* ---------- árvore ---------- */
.tree-list { flex: 1; overflow-y: auto; padding: 12px 12px calc(20px + env(safe-area-inset-bottom)); }
.group {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r);
  margin-bottom: 12px; overflow: hidden;
}
.group > .node-row {
  display: flex; align-items: center; gap: 10px; padding: 14px 14px;
  font-weight: 680; font-size: 15.5px; position: relative;
}
.group > .node-row::before {
  content: ""; position: absolute; left: 0; top: 10px; bottom: 10px; width: 3px;
  border-radius: 3px; background: linear-gradient(var(--accent), #8b5bff);
}
.node-row:active { background: var(--surface-2); }
.gkids { padding: 0 10px 8px; }

.project { border-radius: 11px; margin: 4px 0; }
.project > .node-row {
  display: flex; align-items: center; gap: 9px; padding: 11px 10px; color: var(--txt);
  font-weight: 600; font-size: 14.5px; border-radius: 10px;
}
.pkids { padding-left: 6px; margin-left: 12px; border-left: 1.5px solid var(--line); }

.twisty { color: var(--faint); font-size: 12px; width: 14px; text-align: center; transition: transform .18s; flex: 0 0 auto; }
.twisty.open { transform: rotate(90deg); }
.ic { font-size: 17px; flex: 0 0 auto; opacity: .95; }
.row-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.add-btn {
  flex: 0 0 auto; width: 30px; height: 30px; border-radius: 8px; margin-left: 4px;
  background: var(--surface-2); border: 1px solid var(--line-2); color: var(--accent-2);
  font-size: 17px; line-height: 1; display: grid; place-items: center;
}
.add-btn:active { background: var(--surface-3); transform: scale(.92); }

.chat-row {
  display: flex; align-items: center; gap: 10px; padding: 12px 12px;
  border-radius: 11px; background: var(--surface-2); border: 1px solid var(--line);
  margin: 5px 0; transition: background .15s, transform .1s;
}
.chat-row:active { background: var(--surface-3); transform: scale(.99); }
.chat-row .ic { color: var(--accent-2); }
.chat-row .cname { flex: 1; font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chevron { color: var(--faint); font-size: 18px; }

.badge {
  font-size: 10.5px; font-weight: 650; letter-spacing: .02em; text-transform: uppercase;
  color: var(--accent-2); background: var(--accent-dim); border: 1px solid transparent;
  border-radius: 20px; padding: 3px 8px;
}
.empty { color: var(--muted); text-align: center; padding: 60px 24px; font-size: 14.5px; }
.empty .big { font-size: 40px; display: block; margin-bottom: 12px; opacity: .5; }

/* ---------- terminal ---------- */
#term-screen { background: #05070a; }
.term-empty { flex: 1; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; color: var(--muted); }
.term-empty .te-ic { font-size: 46px; opacity: .5; }
.term-empty p { margin: 0; font-size: 15px; }
#term-screen.empty .term-host, #term-screen.empty .keybar { display: none; }
#term-screen:not(.empty) .term-empty { display: none; }
.term-host { flex: 1; overflow: hidden; background: #05070a; padding: 6px 4px; min-height: 0; }
.term-host .xterm { height: 100%; padding: 4px; }
.keybar {
  display: flex; gap: 7px; padding: 9px 10px;
  padding-bottom: calc(9px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(14,19,27,.85), var(--bg-2));
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--line); overflow-x: auto; scrollbar-width: none;
}
.keybar::-webkit-scrollbar { display: none; }
.keybar button {
  flex: 0 0 auto; min-width: 46px; height: 42px; padding: 0 12px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--line-2); color: var(--txt);
  font-size: 15px; font-weight: 620; box-shadow: 0 2px 0 rgba(0,0,0,.35);
  transition: transform .08s, background .12s;
}
.keybar button:active { transform: translateY(1px); box-shadow: none; background: var(--surface-3); }
.keybar button.toggle.on { background: var(--accent); border-color: var(--accent); color: #fff; box-shadow: 0 0 0 3px var(--accent-dim); }

/* ---------- bottom sheet ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; z-index: 50; animation: fade .18s ease; }
.sheet {
  width: 100%; background: var(--surface); border-top: 1px solid var(--line-2);
  border-top-left-radius: 22px; border-top-right-radius: 22px;
  padding: 8px 14px calc(14px + env(safe-area-inset-bottom));
  box-shadow: 0 -10px 40px rgba(0,0,0,.5); animation: slideup .26s cubic-bezier(.2,.9,.3,1);
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.grabber { width: 40px; height: 4px; border-radius: 3px; background: var(--line-2); margin: 6px auto 10px; }
.sheet-title { font-size: 12.5px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 0 4px 6px; }
.sheet button {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 15px 8px; font-size: 16px; font-weight: 560;
  background: none; border: none; border-radius: 10px; color: var(--txt);
}
.sheet button:active { background: var(--surface-2); }
.sheet button.danger { color: var(--danger); }
.sheet button .ic { font-size: 19px; width: 22px; text-align: center; }

/* ---------- configurações ---------- */
.settings-body { padding: 18px 16px; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }
.set-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r);
  padding: 14px 15px; display: flex; flex-direction: column; gap: 4px;
}
.set-card .k { font-size: 12px; color: var(--muted); font-weight: 600; }
.set-card .v { font-size: 15px; font-family: ui-monospace, "SF Mono", Consolas, monospace; word-break: break-all; }

/* ---------- diálogo (novo chat) ---------- */
.dialog-back {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .58); z-index: 60;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  animation: fade .18s ease;
}
.dialog {
  width: min(410px, 100%); background: var(--surface); border: 1px solid var(--line-2);
  border-radius: 18px; padding: 20px; display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--shadow);
}
.dialog h3 { margin: 0; font-size: 18px; }
.dialog select {
  appearance: none; width: 100%; background: var(--surface-2); color: var(--txt);
  border: 1px solid var(--line); border-radius: 11px; padding: 12px; font-size: 16px;
}
.dialog .actions { display: flex; gap: 10px; margin-top: 2px; }
.dialog .actions .btn { flex: 1; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); z-index: 90; max-width: 88vw;
  background: var(--surface-3); color: var(--txt); border: 1px solid var(--line-2);
  padding: 12px 16px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- responsivo: desktop (duas colunas) ---------- */
@media (min-width: 900px) {
  body { flex-direction: row; }
  /* árvore vira barra lateral; terminal ocupa o resto — ambos visíveis juntos */
  #tree, #term-screen { position: static; height: 100vh; animation: none; }
  #tree {
    flex: 0 0 clamp(300px, 26vw, 400px); border-right: 1px solid var(--line);
    background: linear-gradient(180deg, var(--bg-2), var(--bg));
  }
  #term-screen { flex: 1; min-width: 0; }
  #btn-back { display: none; }          /* navegar entre telas não faz sentido no desktop */
  .tree-list { padding: 14px; }

  /* login e configurações viram modais centrados por cima do layout */
  #login, #settings {
    position: fixed; inset: 0; z-index: 100;
    background: rgba(4, 6, 10, .66); backdrop-filter: blur(6px);
    align-items: center; justify-content: center;
  }
  #settings { display: flex; flex-direction: column; }
  #settings .topbar {
    width: min(560px, 92vw); margin: 0 auto; border: 1px solid var(--line-2);
    border-bottom: none; border-radius: 18px 18px 0 0;
  }
  #settings .settings-body {
    width: min(560px, 92vw); margin: 0 auto; background: var(--surface);
    border: 1px solid var(--line-2); border-top: none; border-radius: 0 0 18px 18px;
    max-height: 70vh;
  }
  .keybar { padding-left: 14px; padding-right: 14px; }
}

/* telas bem largas: um respiro no card de login */
@media (min-width: 900px) { .login-card { width: 430px; } }
