/* Каркас веб-приложения поверх chat.css: боковая панель, библиотека, модалка, вход. */

:root {
  color-scheme: dark;
  --sidebar-w: 260px;
  --files-w: 248px;
  --library-w: 340px;
}

body {
  font-family: "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
}

.app {
  display: flex;
  flex: 1;
  min-height: 0;
  height: 100vh;
  height: 100dvh;
}

/* ── Файлы слева, чаты справа ─────────────────────────────────────── */

.files-sidebar,
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: #111;
}

.files-sidebar {
  width: var(--files-w);
  border-right: 1px solid var(--border);
}

.sidebar {
  border-left: 1px solid var(--border);
  transition: width 0.18s ease, opacity 0.18s ease, border-color 0.18s ease;
}

.sidebar.is-collapsed {
  width: 0;
  opacity: 0;
  border-left-color: transparent;
  overflow: hidden;
  pointer-events: none;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px 8px 14px;
}

.sidebar-logo {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  object-fit: cover;
}

.sidebar-brand {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.files-search {
  padding: 0 10px 8px;
}

.files-search input {
  box-sizing: border-box;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #181818;
  color: var(--fg);
  font-size: 12px;
}

.files-search input:focus {
  outline: none;
  border-color: #3a3a3a;
}

.files-tree {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 12px;
}

.files-section {
  margin-top: 4px;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 2px;
  border-radius: 5px;
}

.tree-row:hover,
.tree-row:focus-within {
  background: var(--bg-hover);
}

.tree-actions {
  display: none;
  flex-shrink: 0;
  align-items: center;
}

.tree-row:hover .tree-actions,
.tree-row:focus-within .tree-actions {
  display: inline-flex;
}

.tree-action {
  width: 20px;
  height: 20px;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tree-action:hover {
  color: var(--fg);
  background: #2a2a2a;
}

.files-section-head,
.file-row,
.project-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
  padding: 4px 6px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--fg-soft);
  font-size: 12.5px;
  text-align: left;
  cursor: pointer;
}

.files-section-head {
  color: var(--fg-soft);
  font-size: 12.5px;
  font-weight: 600;
}

.files-section-head:hover,
.file-row:hover,
.project-row:hover {
  color: var(--fg);
}

.file-row.active,
.project-row.active {
  background: #242424;
  color: var(--white);
}

.files-count {
  margin-left: auto;
  color: var(--fg-muted);
  font-weight: 500;
  letter-spacing: 0;
}

.files-children {
  padding-left: 8px;
}

.files-children[hidden] {
  display: none;
}

.file-name,
.project-name {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.file-ico,
.project-ico,
.section-chevron {
  flex-shrink: 0;
  width: 14px;
  color: var(--fg-muted);
}

.files-empty {
  padding: 4px 8px 8px 22px;
  color: var(--fg-muted);
  font-size: 12px;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px 10px;
}

.session-group {
  padding: 12px 8px 4px;
  font-size: 11px;
  color: var(--fg-muted);
}

.session-row {
  display: flex;
  align-items: center;
  border-radius: 7px;
}

.session-row:hover,
.session-row:focus-within {
  background: var(--bg-hover);
}

.session-row.active {
  background: #242424;
  color: var(--white);
}

.session-row .tree-actions {
  display: none;
  padding-right: 4px;
}

.session-row:hover .tree-actions,
.session-row:focus-within .tree-actions {
  display: inline-flex;
}

.session-item {
  display: block;
  flex: 1;
  min-width: 0;
  padding: 7px 8px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.session-row:hover .session-item,
.session-row.active .session-item {
  color: var(--fg);
}

.session-name {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.session-empty {
  padding: 16px 10px;
  color: var(--fg-muted);
  font-size: 12px;
}

.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 8px 10px;
  border-top: 1px solid var(--border);
}

.sidebar-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--fg-soft);
  font-size: 12.5px;
  cursor: pointer;
}

.sidebar-link:hover {
  background: var(--bg-hover);
  color: var(--fg);
}

.sidebar-user {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 8px;
  color: var(--fg-muted);
  font-size: 12px;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.5);
}

/* ── Основная колонка ─────────────────────────────────────────────── */

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

.main .topbar {
  justify-content: flex-start;
  gap: 8px;
  min-height: 44px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-toggle {
  display: none;
}

#chatsToggle.active svg rect {
  fill: color-mix(in srgb, currentColor 16%, transparent);
}

.topbar-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  font-weight: 500;
  color: var(--fg);
}

.main .chat-column {
  margin-left: auto;
  margin-right: auto;
}

.main .composer-dock {
  margin: 0 auto 14px;
}

.conn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--fg-muted);
  font: inherit;
  font-size: 11px;
  white-space: nowrap;
  cursor: pointer;
}

.conn-pill:hover {
  background: var(--bg-hover);
  color: var(--fg);
}

.diag {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  background: rgba(0, 0, 0, 0.45);
}

.diag[hidden] {
  display: none;
}

.diag-card {
  display: flex;
  flex-direction: column;
  width: min(560px, 100%);
  max-height: min(640px, 86vh);
  background: #1e1e1e;
  border: 1px solid #333;
  border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.diag-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 16px 12px;
}

.diag-title {
  font-size: 13px;
  font-weight: 600;
}

.diag-sub {
  margin-top: 4px;
  color: var(--fg-muted);
  font-size: 12px;
}

.diag-run {
  flex-shrink: 0;
  padding: 4px 10px;
  border: 1px solid #3a3a3a;
  border-radius: 6px;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.diag-run:hover {
  background: #2a2a2a;
}

.diag-run:disabled {
  opacity: 0.6;
  cursor: default;
}

.diag-list {
  list-style: none;
  margin: 0;
  padding: 4px 8px 8px;
  overflow-y: auto;
}

.diag-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 4px;
  font-size: 13px;
}

.diag-mark {
  width: 16px;
  text-align: center;
  color: #3ecf8e;
  font-size: 13px;
}

.diag-item.is-fail .diag-mark {
  color: #f07178;
}

.diag-item.is-pending .diag-mark {
  color: var(--fg-muted);
}

.diag-name {
  flex: 1;
}

.diag-detail {
  color: var(--fg-muted);
  font-size: 12px;
}

.diag-foot {
  display: flex;
  justify-content: flex-end;
  padding: 8px 12px;
  border-top: 1px solid #2a2a2a;
}

.diag-logs {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  background: transparent;
  color: var(--fg-muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.diag-logs:hover {
  color: var(--fg);
}

.diag-log {
  margin: 0;
  max-height: 160px;
  overflow: auto;
  padding: 8px 14px 12px;
  border-top: 1px solid #2a2a2a;
  background: #181818;
  color: #bdbdbd;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--fg-muted);
}

.conn-pill[data-phase="ok"] .conn-dot { background: var(--accent); }
.conn-pill[data-phase="warn"] .conn-dot { background: var(--warn); }
.conn-pill[data-phase="error"] .conn-dot { background: var(--danger); }
.conn-pill[data-phase="error"] { color: #f1a39a; }

.stream-preview {
  margin-top: 8px;
  max-height: 260px;
  overflow-y: auto;
  white-space: pre-wrap;
  color: var(--fg-soft);
  font-size: 13px;
  line-height: 1.55;
}

.message.system.is-error .message-content {
  color: #f1a39a;
}

/* ── Библиотека ───────────────────────────────────────────────────── */

.library-panel {
  width: var(--library-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: #111;
  border-left: 1px solid var(--border);
}

.library-panel[hidden] {
  display: none;
}

.library-panel .library-list {
  flex: 1;
  overflow-y: auto;
}

.library-filter {
  padding: 0 12px 8px;
  color: var(--fg-muted);
  font-size: 12px;
}

.library-filter button {
  border: none;
  background: none;
  color: var(--accent-blue);
  cursor: pointer;
}

/* ── Модалка записи библиотеки ────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 56px;
  background: rgba(0, 0, 0, 0.42);
}

.modal[hidden] {
  display: none;
}

.modal-card {
  display: flex;
  flex-direction: column;
  width: min(920px, 100%);
  height: min(680px, 84vh);
  background: #1e1e1e;
  border: 1px solid #333;
  border-radius: 6px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 35px;
  flex-shrink: 0;
  padding: 0 8px 0 12px;
  background: #181818;
  border-bottom: 1px solid #2a2a2a;
}

.modal-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  font-weight: 400;
}

.modal-head .history-close {
  width: 22px;
  height: 22px;
  font-size: 14px;
}

.modal-meta {
  flex-shrink: 0;
  padding: 4px 16px;
  color: var(--fg-muted);
  font-size: 11px;
  border-bottom: 1px solid #2a2a2a;
  background: #1e1e1e;
}

.modal-meta:empty {
  display: none;
}

.modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 22px 24px;
  background: #1e1e1e;
}

.entry-title-input,
.entry-editor {
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
  background: #1e1e1e;
  color: var(--fg);
  font: inherit;
}

.entry-title-input {
  flex-shrink: 0;
  height: 32px;
  padding: 0 16px;
  border-bottom: 1px solid #2a2a2a;
  font-size: 13px;
}

.entry-title-input:focus,
.entry-editor:focus {
  outline: none;
}

.entry-editor {
  flex: 1;
  min-height: 0;
  padding: 12px 16px 20px;
  resize: none;
  line-height: 1.55;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}

.md-toolbar {
  display: flex;
  align-items: center;
  gap: 1px;
  flex-shrink: 0;
  height: 30px;
  margin: 0;
  padding: 0 6px;
  border: none;
  border-bottom: 1px solid #2a2a2a;
  border-radius: 0;
  background: #1e1e1e;
}

.md-toolbar[hidden],
.entry-title-input[hidden],
.entry-editor[hidden],
.modal-body[hidden] {
  display: none;
}

.md-toolbar-gap {
  flex: 1;
}

.md-toolbar .tree-action {
  display: inline-flex;
}

.entry-meta {
  margin: 0 0 14px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #181818;
  color: var(--fg-muted);
  font-size: 12px;
  line-height: 1.6;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 70;
  transform: translateX(-50%);
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #222;
  color: var(--fg);
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

/* ── Мобильная раскладка ──────────────────────────────────────────── */

@media (max-width: 860px) {
  .main .topbar {
    position: relative;
    z-index: 60;
  }

  .sidebar-backdrop {
    top: 44px;
  }

  .files-sidebar,
  .sidebar {
    position: fixed;
    top: 44px;
    bottom: auto;
    height: calc(100vh - 44px);
    height: calc(100dvh - 44px);
    z-index: 50;
    transition: transform 0.18s ease-out;
  }

  .files-sidebar {
    left: 0;
    transform: translateX(-100%);
  }

  .sidebar {
    right: 0;
    transform: translateX(100%);
  }

  .tree-actions,
  .session-row .tree-actions {
    display: inline-flex;
  }

  .message-input,
  .files-search input,
  .login-card input {
    font-size: 16px;
  }

  .main .composer-dock {
    margin-bottom: max(10px, env(safe-area-inset-bottom));
  }

  .files-sidebar.open,
  .sidebar.open {
    transform: none;
  }

  .sidebar-toggle {
    display: inline-flex;
  }

  .library-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 45;
    width: min(var(--library-w), 100%);
  }

  .conn-pill .conn-text {
    display: none;
  }
}

/* ── Вход ─────────────────────────────────────────────────────────── */

.login-page {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #141414;
  color: #e8e8e8;
}

.login-card {
  width: min(380px, 92vw);
  padding: 28px 24px 22px;
  background: #1c1c1c;
  border: 1px solid #2e2e2e;
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}

.login-logo {
  display: block;
  width: 48px;
  height: 48px;
  margin: 0 auto 14px;
  border-radius: 12px;
  object-fit: cover;
}

.login-card h1 {
  margin: 0 0 4px;
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.login-card p {
  margin: 0 0 20px;
  text-align: center;
  color: #8b8b8b;
  font-size: 13px;
}

.login-card .login-hint {
  margin: 14px 0 0;
}

.login-card .login-hint a {
  color: #8b8b8b;
}

.login-card label {
  display: block;
  margin: 0 0 6px;
  color: #8b8b8b;
  font-size: 12px;
}

.login-card input {
  box-sizing: border-box;
  width: 100%;
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid #2e2e2e;
  border-radius: 10px;
  background: #141414;
  color: #f4f4f4;
  font-size: 14px;
}

.login-card input:focus {
  outline: none;
  border-color: #3da65f;
}

.login-card button {
  width: 100%;
  margin-top: 4px;
  padding: 11px 14px;
  border: none;
  border-radius: 10px;
  background: #3da65f;
  color: #141414;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.login-card button:disabled {
  opacity: 0.6;
  cursor: default;
}

.login-error {
  margin: -4px 0 12px;
  color: #f1a39a;
  font-size: 12.5px;
}
