:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1a1d21;
  --text-muted: #6b7280;
  --accent: #3b6ef6;
  --accent-dark: #2c55c7;
  --user-bubble: #3b6ef6;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --border: #e5e7eb;
  --error: #dc2626;
  --radius: 16px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
}

.app {
  max-width: 780px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------- header */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header__brand { display: flex; align-items: center; gap: 12px; }

.header__logo {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff; font-weight: 700; font-size: 15px;
  border-radius: 12px;
}

.header__title { font-size: 17px; font-weight: 600; }
.header__subtitle { font-size: 12.5px; color: var(--text-muted); }

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #22c55e;
  transition: background 0.3s;
}
.status-dot.busy { background: #f59e0b; }
.status-dot.error { background: var(--error); }

.header__actions { display: flex; align-items: center; gap: 12px; }

.header__clear {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}
.header__clear:hover { color: var(--error); border-color: var(--error); }

/* ---------------------------------------------------------------- chat */

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.welcome { text-align: center; margin: auto; color: var(--text-muted); }
.welcome h2 { color: var(--text); margin-bottom: 8px; font-size: 22px; }

.msg { display: flex; gap: 10px; max-width: 88%; }
/* fit-content: пузырь строго по тексту (как в мессенджерах), а не на ширину ленты */
.msg--user { align-self: flex-end; flex-direction: row-reverse; width: fit-content; }

.msg__avatar {
  width: 32px; height: 32px; flex-shrink: 0;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 13px; font-weight: 600;
  background: var(--border); color: var(--text-muted);
}
.msg--assistant .msg__avatar {
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
}

.msg__bubble {
  width: fit-content;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 18px;
  line-height: 1.45;
  font-size: 15px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg--user .msg__bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 6px; /* «хвостик» со стороны отправителя */
}

.msg--assistant .msg__bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.msg__meta { margin-top: 8px; }
.badge {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: #ecfdf5; color: #047857; border: 1px solid #a7f3d0;
}

/* ---------------------------------------------------------------- typing */

.typing-bubble {
  display: flex; gap: 5px; align-items: center;
  padding: 14px 18px;
}
.typing-bubble span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.typing-bubble span:nth-child(2) { animation-delay: 0.15s; }
.typing-bubble span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ---------------------------------------------------------------- composer */

.composer {
  padding: 12px 20px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer__row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 8px 16px;
  transition: border-color 0.2s;
}
.composer__row:focus-within { border-color: var(--accent); }

.composer__input {
  flex: 1;
  border: none; outline: none; background: transparent;
  font: inherit; font-size: 15px;
  resize: none;
  max-height: 140px;
  line-height: 1.5;
  padding: 6px 0;
}

.composer__send {
  width: 40px; height: 40px; flex-shrink: 0;
  border: none; border-radius: 10px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}
.composer__send:hover:not(:disabled) { background: var(--accent-dark); }
.composer__send:disabled { opacity: 0.45; cursor: not-allowed; }

.composer__hint {
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

.error-banner {
  background: #fef2f2;
  color: var(--error);
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  margin-bottom: 10px;
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- mobile */

@media (max-width: 600px) {
  .msg { max-width: 94%; }
}

/* ------------------------------------------------------- auth / admin */

.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel, #fff);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-card--wide { max-width: 760px; }

.auth-logo {
  width: 44px; height: 44px;
  border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff; font-weight: 700;
}

.auth-card h1 { font-size: 20px; margin: 4px 0 0; }
.auth-header { display: flex; justify-content: space-between; align-items: center; }
.auth-hint { margin: 0; font-size: 13px; color: var(--text-muted); }

.auth-card label, .user-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 13px; color: var(--text-muted);
}

.auth-card input, .user-form input, .user-form select {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 11px;
  font: inherit;
  color: var(--text);
  background: #fff;
}

.auth-error {
  font-size: 13px;
  color: #b91c1c;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 8px 10px;
}

.auth-submit {
  border: 0; border-radius: 10px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff; font: inherit; font-weight: 600;
  cursor: pointer;
}
.auth-submit:disabled { opacity: .6; cursor: default; }

.link-button {
  border: 0; background: none; padding: 4px 6px;
  font: inherit; font-size: 13px;
  color: var(--accent-dark, #1d4ed8);
  cursor: pointer;
}
.link-button.danger { color: #b91c1c; }
.hidden { display: none !important; }

.users-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.users-table th, .users-table td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); }
.users-table th { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.row-actions { white-space: nowrap; text-align: right; }

.user-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.user-form h2 { grid-column: 1 / -1; font-size: 16px; margin: 12px 0 0; }
.form-actions { grid-column: 1 / -1; display: flex; gap: 10px; align-items: center; }
.user-form .auth-error { grid-column: 1 / -1; }

@media (max-width: 560px) { .user-form { grid-template-columns: 1fr; } }
