:root {
  color-scheme: dark;
  --bg: #0f172a;
  --panel: #1e293b;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --error: #f87171;
  --user: #1d4ed8;
  --bot: #334155;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: pan-x pan-y pinch-zoom;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 100%;
  margin: 0 auto;
  position: relative;
}

body.logged-in #login-screen {
  display: none !important;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid #334155;
  flex-shrink: 0;
}

.header-title {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-width: 0;
  flex: 0 1 auto;
  max-width: calc(100% - 120px);
}

header h1 {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--muted);
  white-space: nowrap;
}

.vps-status {
  position: relative;
  max-width: 100%;
}

.vps-status-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 6px 1px 4px;
  border-radius: 999px;
  border: 1px solid #334155;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.68rem;
  line-height: 1.2;
  cursor: pointer;
  max-width: 100%;
}

.vps-status-btn:hover,
.vps-status-btn:focus-visible,
.vps-status-btn[aria-expanded="true"] {
  color: var(--text);
  border-color: var(--accent);
  background: #0b1220;
}

.vps-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #64748b;
  flex-shrink: 0;
}

.vps-status-dot.ok {
  background: #22c55e;
  box-shadow: 0 0 5px rgba(34, 197, 94, 0.45);
}

.vps-status-dot.warn {
  background: #f59e0b;
}

.vps-status-dot.bad {
  background: #ef4444;
}

.vps-status-label {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vps-status-panel {
  position: fixed;
  left: 10px;
  top: calc(54px + env(safe-area-inset-top));
  z-index: 120;
  min-width: 220px;
  max-width: calc(100vw - 20px);
  border: 1px solid #475569;
  border-radius: 12px;
  background: #0b1220;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  padding: 10px 12px;
}

.vps-status-panel[hidden] {
  display: none !important;
}

.vps-status-details {
  display: grid;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--text);
}

.vps-status-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.vps-status-row span:last-child {
  color: var(--muted);
  text-align: right;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
  margin-left: auto;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 8px;
  background: #1e293b;
  color: var(--muted);
  border: 1px solid #475569;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  color: var(--text);
  border-color: var(--accent);
}

.icon-btn.active {
  color: var(--accent);
  border-color: var(--accent);
}

.letter-btn {
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1;
}

.attach-btn svg,
.logout-btn svg,
.mic-btn svg,
.camera-btn svg,
.hamburger-btn svg,
.workspace-btn svg {
  display: block;
}

.nav-menu {
  position: relative;
}

.nav-menu-panel {
  position: fixed;
  right: 10px;
  top: calc(46px + env(safe-area-inset-top));
  z-index: 110;
  min-width: 168px;
  border: 1px solid #475569;
  border-radius: 12px;
  background: #0b1220;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  padding: 6px;
  overflow: hidden;
}

.nav-menu-panel[hidden] {
  display: none !important;
}

.nav-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 14px;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  border: none;
  cursor: pointer;
}

.nav-menu-item:hover,
.nav-menu-item:focus-visible {
  background: #1e293b;
}

.nav-menu-item.active {
  color: var(--accent);
  font-weight: 600;
}

.icon-btn.listening {
  color: #fecaca;
  border-color: #f87171;
  animation: mic-pulse 1.2s ease-in-out infinite;
}

@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248, 113, 113, 0.35); }
  50% { box-shadow: 0 0 0 4px rgba(248, 113, 113, 0.12); }
}

#chat-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid #334155;
}

.chat-composer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.composer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  flex-shrink: 0;
}

.composer-actions-left {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  flex-shrink: 0;
}

#attach-btn,
#mic-btn,
#stop-btn,
#send-btn {
  flex: 0 0 40px;
}

.composer-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 10px;
}

.composer-btn svg {
  display: block;
}

#send-btn.composer-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #0f172a;
  border: none;
}

#send-btn.composer-btn:hover,
#send-btn.composer-btn:focus-visible {
  filter: brightness(1.08);
}

#send-btn.composer-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
}

.attachment-list[hidden] {
  display: none !important;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 6px 10px;
  border-radius: 999px;
  background: #1e293b;
  border: 1px solid #475569;
  color: var(--text);
  font-size: 0.82rem;
}

.attachment-chip .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-chip button {
  width: 22px;
  height: 22px;
  padding: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
}

.attachment-chip button:hover {
  background: #334155;
  color: #fecaca;
}

#chat-form .attachment-list {
  width: 100%;
}

.stop-btn {
  color: #fecaca;
  border-color: #f87171;
  font-size: 0.72rem;
  font-weight: 700;
}

.stop-btn:hover,
.stop-btn:focus-visible {
  color: #fff;
  background: #7f1d1d;
  border-color: #ef4444;
}

.picker-section-label {
  padding: 8px 14px 4px;
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.model-picker,
.history-picker,
.workspace-picker,
.mode-picker,
.export-picker,
.voice-picker,
.host-picker {
  position: relative;
  display: contents;
}

.picker-option.unreachable {
  opacity: 0.55;
}

.login-install-hint {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.login-install-hint[hidden] {
  display: none !important;
}

.picker-panel {
  position: fixed;
  left: 50%;
  right: auto;
  top: calc(52px + env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 100;
  width: min(340px, calc(100vw - 24px));
  max-width: calc(100vw - 24px);
  max-height: min(50vh, 420px);
  border: 1px solid #475569;
  border-radius: 12px;
  background: #0b1220;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

.picker-panel[hidden] {
  display: none !important;
}

.picker-list {
  max-height: min(50vh, 420px);
  overflow-y: auto;
  padding: 6px;
}

.picker-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  border: none;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.picker-option:hover,
.picker-option:focus-visible {
  background: #1e293b;
}

.picker-option.selected {
  background: #334155;
  color: var(--accent);
}

.picker-option.history-item {
  white-space: normal;
  line-height: 1.35;
}

.picker-option .history-title {
  display: block;
  font-weight: 600;
}

.picker-option .history-preview {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.picker-option.new-chat {
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 4px;
  border: 1px dashed #475569;
}

#login-screen {
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.app-menu {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: #0b1220;
  border-bottom: 1px solid #334155;
  overflow-x: auto;
  flex-shrink: 0;
  position: relative;
  z-index: 30;
}

.app-menu[hidden] {
  display: none !important;
}

.menu-btn {
  flex: 0 0 auto;
  padding: 8px 14px;
  font-size: 0.9rem;
  border-radius: 10px;
  background: #1e293b;
  color: var(--text);
  border: 1px solid #475569;
}

.menu-btn:active {
  background: #334155;
}

.menu-btn-compact {
  padding: 8px 10px;
  min-width: 42px;
  font-weight: 600;
}

.terminal-tabs {
  display: flex;
  gap: 6px;
  flex: 1;
  overflow-x: auto;
  min-width: 0;
}

.terminal-tab {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
  padding: 6px 8px 6px 10px;
  font-size: 0.85rem;
  border-radius: 10px;
  background: #1e293b;
  color: var(--muted);
  border: 1px solid #475569;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.terminal-tab.active {
  background: #334155;
  color: var(--text);
  border-color: var(--accent);
}

.terminal-tab-close {
  width: 22px;
  height: 22px;
  padding: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
}

.terminal-tab-close:hover {
  background: #7f1d1d;
  color: #fecaca;
}

#main-area {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  position: relative;
}

#chat-screen {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

body.logged-in #chat-screen {
  display: flex;
}

body.terminals-open #chat-screen {
  display: none !important;
}

#chat-screen[hidden] {
  display: none !important;
}

#terminal-stack {
  display: none;
  flex: 1;
  min-height: 0;
  position: relative;
  background: #0b1220;
}

#terminal-stack:not([hidden]) {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.terminal-window {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: #0b1220;
  border-top: 1px solid #334155;
  visibility: hidden;
  pointer-events: none;
  z-index: 1;
}

.terminal-window.active {
  visibility: visible;
  pointer-events: auto;
  z-index: 2;
}

.terminal-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid #334155;
  font-size: 0.9rem;
}

.terminal-close {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
}

.terminal-close:hover {
  background: #7f1d1d;
  color: #fecaca;
}

.terminal-body {
  flex: 1;
  min-height: 0;
  padding: 4px;
}

.terminal-body .xterm {
  height: 100%;
}

.xterm .xterm-helper-textarea {
  text-transform: none !important;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  position: relative;
  max-width: 92%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user,
.msg.bot:not(.typing) {
  padding-bottom: 36px;
}

.msg-body {
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-text + .msg-text,
.msg-text + .code-block,
.code-block + .msg-text,
.code-block + .code-block {
  margin-top: 10px;
}

.code-block {
  border: 1px solid #334155;
  border-radius: 10px;
  overflow: hidden;
  background: #020617;
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: #1e293b;
  border-bottom: 1px solid #334155;
  font-size: 0.78rem;
  color: var(--muted);
}

.code-lang {
  text-transform: lowercase;
}

.code-block pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.4;
  white-space: pre;
  word-break: normal;
}

.code-block code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.diff-line.diff-add {
  color: #86efac;
}

.diff-line.diff-del {
  color: #fca5a5;
}

.code-block code.hljs {
  background: transparent;
  padding: 0;
}

.msg-expand-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px dashed #475569;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 0.84rem;
  cursor: pointer;
}

.msg-body.collapsed {
  max-height: 280px;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(180deg, #000 72%, transparent);
  mask-image: linear-gradient(180deg, #000 72%, transparent);
}

.file-changes {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(148, 163, 184, 0.25);
}

.file-changes-title {
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.file-change-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.file-change-item {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid #475569;
  background: #0b1220;
  color: var(--text);
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-change-item:hover,
.file-change-item:focus-visible,
.file-change-item.active {
  border-color: var(--accent);
  color: var(--accent);
}

.file-preview {
  margin-top: 8px;
  border: 1px solid #334155;
  border-radius: 10px;
  overflow: hidden;
  background: #020617;
}

.file-preview[hidden] {
  display: none !important;
}

.file-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: #1e293b;
  border-bottom: 1px solid #334155;
  font-size: 0.78rem;
  color: var(--muted);
}

.file-preview-path {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-preview-body {
  margin: 0;
  padding: 10px 12px;
  max-height: 220px;
  overflow: auto;
  font-size: 0.8rem;
  line-height: 1.35;
  white-space: pre;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.msg-actions {
  position: absolute;
  right: 8px;
  bottom: 6px;
  display: flex;
  gap: 4px;
}

.msg-action-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid #475569;
  background: rgba(15, 23, 42, 0.85);
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
}

.msg-action-btn:hover,
.msg-action-btn:focus-visible {
  color: var(--text);
  border-color: var(--accent);
  background: #1e293b;
}

.msg-action-btn.copied {
  color: #86efac;
  border-color: #22c55e;
}

.code-copy-btn {
  width: auto;
  min-width: 28px;
  padding: 0 8px;
  font-size: 0.82rem;
}

.copy-toast {
  position: fixed;
  left: 50%;
  bottom: calc(72px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 200;
  padding: 8px 14px;
  border-radius: 999px;
  background: #14532d;
  color: #bbf7d0;
  font-size: 0.88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.copy-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.update-btn {
  position: fixed;
  left: 50%;
  bottom: calc(120px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 210;
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid var(--accent);
  background: #0b1220;
  color: var(--accent);
  font: inherit;
  font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.update-btn[hidden] {
  display: none !important;
}

.msg.user { align-self: flex-end; background: var(--user); }
.msg.bot { align-self: flex-start; background: var(--bot); }
.msg.bot.typing {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  min-width: 120px;
}

.typing-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid #64748b;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

.typing-label {
  font-size: 0.95rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

body.agent-busy header h1::after {
  content: " · думает";
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 500;
}

.msg.status { align-self: center; background: transparent; color: var(--muted); font-size: 0.9rem; padding: 4px; }
.msg.error { align-self: flex-start; background: #7f1d1d; color: #fecaca; }

#message-input {
  width: 100%;
  flex: none;
  resize: none;
  overflow-y: auto;
  min-height: 52px;
  max-height: 52vh;
  border: 1px solid #475569;
  border-radius: 12px;
  padding: 12px 14px;
  background: #0b1220;
  color: var(--text);
  font: inherit;
  line-height: 1.4;
  field-sizing: content;
  touch-action: pan-x pan-y;
}

@supports (field-sizing: content) {
  #message-input {
    min-height: 52px;
    max-height: 52vh;
  }
}

#send-btn.sending {
  background: #334155;
  color: var(--muted);
}

#send-btn.sending .send-icon {
  display: none;
}

#send-btn .send-loading {
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1;
}

#send-btn .send-loading[hidden] {
  display: none !important;
}

#send-btn.sending .send-loading {
  display: block !important;
}

textarea {
  flex: 1;
  resize: none;
  overflow-y: auto;
  min-height: 44px;
  max-height: 45vh;
  border: 1px solid #475569;
  border-radius: 12px;
  padding: 10px 12px;
  background: #0b1220;
  color: var(--text);
  font: inherit;
  line-height: 1.4;
}

button {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  background: var(--accent);
  color: #0f172a;
  font-weight: 600;
  cursor: pointer;
}

button:disabled { opacity: 0.5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--muted); padding: 6px 10px; }
.error { color: var(--error); }

input[type=password] {
  border: 1px solid #475569;
  border-radius: 12px;
  padding: 12px;
  background: #0b1220;
  color: var(--text);
  font: inherit;
  touch-action: pan-x pan-y;
}

.remember {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.95rem;
}
