:root {
  --bg: #0f1115;
  --bg-2: #181b22;
  --bg-3: #232733;
  --fg: #e6e9ef;
  --fg-2: #9aa3b2;
  --accent: #7c93ff;
  --accent-2: #5b73e0;
  --user-bg: #1f2a4a;
  --assistant-bg: #232733;
  --error: #ff6b6b;
  --ok: #4ade80;
  --radius: 14px;
  --pad: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font: inherit; }
input, textarea, button { font: inherit; color: inherit; }

/* ---------- login ---------- */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 16px;
}
.login-card {
  background: var(--bg-2);
  padding: 28px 22px;
  border-radius: var(--radius);
  width: 100%;
  max-width: 340px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}
.login-card h1 {
  margin: 0 0 18px;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
}
.login-card label {
  display: block;
  font-size: 13px;
  color: var(--fg-2);
  margin: 10px 0 4px;
}
.login-card select,
.login-card input {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--bg-3);
  background: var(--bg);
  color: var(--fg);
  outline: none;
}
.login-card select:focus,
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:hover { background: var(--accent-2); }
.login-card .err {
  color: var(--error);
  font-size: 13px;
  margin-top: 10px;
  min-height: 18px;
  text-align: center;
}

/* ---------- chat layout ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  position: relative;
}
.app.drag-over::after {
  content: "Отпусти, чтобы загрузить";
  position: absolute;
  inset: 8px;
  background: rgba(124, 147, 255, 0.18);
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
  z-index: 100;
  backdrop-filter: blur(2px);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--bg-3);
}
.topbar .who {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
}
.topbar .icon-btn {
  background: transparent;
  border: none;
  color: var(--fg-2);
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 14px;
}
.topbar .icon-btn:hover { background: var(--bg-3); color: var(--fg); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.msg {
  max-width: min(720px, 88%);
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
}

/* Markdown rendered inside assistant messages */
.msg.assistant { white-space: normal; }
.msg.assistant > *:first-child { margin-top: 0; }
.msg.assistant > *:last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant ul,
.msg.assistant ol { margin: 4px 0 8px; padding-left: 22px; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant li > p { margin: 0; }
.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3,
.msg.assistant h4 {
  margin: 12px 0 6px;
  font-weight: 600;
  line-height: 1.25;
}
.msg.assistant h1 { font-size: 1.25em; }
.msg.assistant h2 { font-size: 1.15em; }
.msg.assistant h3 { font-size: 1.05em; }
.msg.assistant h4 { font-size: 1em; }
.msg.assistant a {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-word;
}
.msg.assistant a:hover { color: var(--accent-2); }
.msg.assistant strong { font-weight: 600; }
.msg.assistant em { font-style: italic; }
.msg.assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: rgba(255, 255, 255, 0.07);
  padding: 1px 5px;
  border-radius: 4px;
}
.msg.assistant pre {
  background: rgba(0, 0, 0, 0.35);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 6px 0 10px;
  font-size: 0.92em;
  line-height: 1.4;
}
.msg.assistant pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
}
.msg.assistant blockquote {
  margin: 6px 0 10px;
  padding: 4px 12px;
  border-left: 3px solid var(--bg-3);
  color: var(--fg-2);
}
.msg.assistant hr {
  border: none;
  border-top: 1px solid var(--bg-3);
  margin: 10px 0;
}
.msg.assistant table {
  border-collapse: collapse;
  margin: 6px 0 10px;
  font-size: 0.95em;
  display: block;
  overflow-x: auto;
}
.msg.assistant th,
.msg.assistant td {
  border: 1px solid var(--bg-3);
  padding: 4px 8px;
  text-align: left;
}
.msg.assistant th { background: rgba(255, 255, 255, 0.04); font-weight: 600; }

/* Inline images served from /api/file */
.msg.assistant img.inline-attachment {
  display: block;
  max-width: 100%;
  max-height: 60vh;
  border-radius: 10px;
  margin: 6px 0;
  background: var(--bg);
}

/* Download links to non-image files (PDF, MD, ZIP, etc.) */
.msg.assistant a.file-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  margin: 4px 4px 4px 0;
  background: var(--bg-3);
  border-radius: 8px;
  text-decoration: none;
  color: var(--fg);
  font-size: 14px;
}
.msg.assistant a.file-link::before { content: "⬇"; font-size: 16px; }
.msg.assistant a.file-link:hover { background: #2c3142; color: var(--accent); }
.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--assistant-bg);
  border-bottom-left-radius: 4px;
}
.msg.error {
  align-self: center;
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid var(--error);
  color: var(--error);
  font-size: 14px;
}
.msg.system {
  align-self: center;
  color: var(--fg-2);
  font-size: 13px;
  background: transparent;
  padding: 4px 8px;
}
.msg.tool-status {
  align-self: flex-start;
  font-style: italic;
  font-size: 12px;
  opacity: 0.75;
}

.typing {
  align-self: flex-start;
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--assistant-bg);
  border-radius: var(--radius);
}
.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--fg-2);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}

/* ---------- composer ---------- */
.composer {
  display: flex;
  gap: 8px;
  padding: 10px;
  background: var(--bg-2);
  border-top: 1px solid var(--bg-3);
  align-items: flex-end;
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 30vh;
  min-height: 44px;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--bg-3);
  background: var(--bg);
  outline: none;
  line-height: 1.4;
  overflow-y: hidden;
}
.composer textarea.at-max { overflow-y: auto; }
.composer textarea:focus { border-color: var(--accent); }

.btn {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  border: none;
  border-radius: 12px;
  background: var(--bg-3);
  color: var(--fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:hover { background: #2c3142; }
.btn.send { background: var(--accent); color: white; }
.btn.send:hover { background: var(--accent-2); }
.btn.recording {
  background: var(--error);
  color: white;
  animation: pulse 1.2s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,107,107,0.6); }
  50% { box-shadow: 0 0 0 8px rgba(255,107,107,0); }
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn svg { width: 20px; height: 20px; }

/* hidden file input */
.file-input { display: none; }

/* attachment chip shown above the composer when a file is pending send */
.pending-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 12px;
  margin: 0 10px 6px;
  background: var(--bg-3);
  border-radius: 999px;
  font-size: 13px;
  color: var(--fg);
  align-self: flex-start;
  max-width: calc(100% - 20px);
}
.pending-chip .pending-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}
.pending-chip .pending-remove {
  background: transparent;
  border: none;
  color: var(--fg-2);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.pending-chip .pending-remove:hover {
  background: rgba(255,255,255,0.08);
  color: var(--fg);
}

/* ---------- mobile ---------- */
@media (max-width: 480px) {
  .msg { max-width: 92%; }
  .topbar { padding: 8px 10px; }
  .composer { padding: 8px; gap: 6px; }
  .btn { width: 42px; height: 42px; }
}
