:root {
  --bg: #f6f6f7;
  --surface: #ffffff;
  --surface-2: #efeff1;
  --border: #dcdce0;
  --ink: #111113;
  --muted: #58585f;
  --accent: #111113;
  --accent-fg: #ffffff;
  --ok: #157a3c;
  --ok-bg: #e7f6ec;
  --danger: #b42318;
  --danger-bg: #fdecec;
  --warn: #9a5b00;
  --warn-bg: #fff4df;
  --radius: 14px;
  --app-h: 100dvh;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d0f;
    --surface: #16161a;
    --surface-2: #202026;
    --border: #2c2c34;
    --ink: #f3f3f5;
    --muted: #9b9ba6;
    --accent: #f3f3f5;
    --accent-fg: #111113;
    --ok: #4ade80;
    --ok-bg: #112a1c;
    --danger: #f87171;
    --danger-bg: #2f1416;
    --warn: #fbbf24;
    --warn-bg: #2b2110;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: var(--app-h);
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* Экран авторизации */
.auth-screen {
  height: var(--app-h);
  display: grid;
  place-items: center;
  padding: 16px;
}
.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px;
}
.auth-badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.auth-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 800;
}
.auth-actions {
  margin-top: 18px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* Оболочка */
.app {
  display: flex;
  flex-direction: column;
  height: var(--app-h);
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.session-head {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.session-title {
  font-weight: 700;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}
.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.conn-dot.ok { background: var(--ok); }
.conn-dot.err { background: var(--danger); }

.tabs {
  display: flex;
  background: var(--surface-2);
  padding: 3px;
  border-radius: 999px;
  gap: 2px;
}
.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.tab.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.pill-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
}
.pill-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.pill-toggle.on {
  background: var(--danger-bg);
  border-color: var(--danger);
  color: var(--danger);
}
.pill-toggle.on .pill-dot { background: var(--danger); }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.btn-danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}
.btn-ok {
  background: var(--ok);
  color: #fff;
  border-color: var(--ok);
}
.btn-sm {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 8px;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Основная область */
.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  position: relative;
}

.sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sidebar-head-actions { display: flex; gap: 6px; align-items: center; }
.sessions-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.session-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.session-item:hover { background: var(--surface-2); }
.session-item.active {
  background: var(--surface-2);
  border-color: var(--border);
  font-weight: 600;
}
.session-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.session-item-meta {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  gap: 6px;
  align-items: center;
}
.session-del {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 13px;
}
.session-del:hover { color: var(--danger); background: var(--danger-bg); }

.sidebar-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}
.foot-link { color: var(--muted); text-decoration: none; }
.foot-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
}

.main {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.tab-panel {
  display: none;
  flex: 1 1 auto;
  min-height: 0;
  flex-direction: column;
}
.tab-panel.active { display: flex; }

/* Карточки подтверждения */
.approvals-bar {
  flex: 0 0 auto;
  padding: 10px 14px;
  background: var(--warn-bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.approval-card {
  background: var(--surface);
  border: 1px solid var(--danger);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.approval-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.approval-host {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--danger-bg);
  color: var(--danger);
}
.approval-cmd {
  margin: 0;
  padding: 8px 10px;
  background: var(--surface-2);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.approval-btns { display: flex; gap: 8px; flex-wrap: wrap; }

/* Лента сообщений и шагов */
.feed {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: min(820px, 100%);
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.msg.assistant {
  align-self: flex-start;
  width: min(820px, 100%);
}
.msg-meta {
  font-size: 11px;
  opacity: 0.65;
  margin-bottom: 4px;
  display: flex;
  gap: 8px;
}
.msg-body {
  white-space: pre-wrap;
  font-size: 14px;
}
.msg-body code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
}
.msg.user .msg-body code {
  background: rgba(255,255,255,0.18);
}
.msg-body pre {
  margin: 8px 0;
  padding: 10px;
  background: var(--surface-2);
  border-radius: 10px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.4;
}

.step-card {
  max-width: min(820px, 100%);
  width: min(820px, 100%);
  align-self: flex-start;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface-2);
  font-size: 13px;
}
.step-card summary {
  padding: 8px 12px;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: 12px;
  word-break: break-word;
}
.step-card summary::-webkit-details-marker { display: none; }
.step-badge {
  flex: 0 0 auto;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-weight: 700;
}
.step-badge.ssh {
  background: var(--warn-bg);
  color: var(--warn);
  border-color: transparent;
}
.step-body {
  padding: 0 12px 10px;
  border-top: 1px solid var(--border);
}
.step-body pre {
  margin: 8px 0 0;
  padding: 8px;
  background: var(--surface);
  border-radius: 8px;
  overflow-x: auto;
  max-height: 300px;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

.streaming-box {
  border-style: dashed;
}

/* Поле ввода */
.composer-wrap {
  flex: 0 0 auto;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.quick-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 6px;
  scrollbar-width: none;
}
.quick-chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
}
.chip:hover { color: var(--ink); }

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.composer textarea {
  flex: 1 1 auto;
  min-width: 0;
  resize: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  padding: 9px 12px;
  border-radius: 12px;
  max-height: 150px;
  line-height: 1.4;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Терминал */
.term-toolbar {
  flex: 0 0 auto;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 12px;
}
.term-actions { display: flex; gap: 6px; }
.term-frame {
  flex: 1 1 auto;
  width: 100%;
  border: 0;
  background: #000;
}

/* Хосты и аудит */
.hosts-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px;
}
.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 10px;
}
.section-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
}
.hosts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  gap: 10px;
  margin-bottom: 20px;
}
.host-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.host-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.host-tier {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}
.host-tier.critical {
  background: var(--danger-bg);
  color: var(--danger);
}
.host-ip {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.host-desc { font-size: 13px; color: var(--muted); }

.audit-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.audit-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-family: var(--mono);
  font-size: 12px;
  word-break: break-word;
}
.audit-ts { color: var(--muted); margin-right: 8px; }

/* Мобильный адаптив */
.mobile-only { display: none; }
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 20;
}

@media (max-width: 820px) {
  .mobile-only { display: grid; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    transform: translateX(-105%);
    transition: transform 0.18s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .session-title { max-width: 110px; }
  .pill-label { display: none; }
  .tab { padding: 5px 9px; font-size: 12px; }
}
