:root {
  --bg: #0A0A0B;
  --bg-soft: #111113;
  --bg-card: #161618;
  --text: #EDEDF0;
  --muted: #8A8A94;
  --line: rgba(255,255,255,.08);
  --accent-1: #5B8DEF;
  --accent-2: #A855F7;
  --grad: linear-gradient(100deg, var(--accent-1), var(--accent-2));
  --radius: 16px;
  --ease: cubic-bezier(.2,.7,.2,1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

/* КРИТИЧНО: display:flex у .screen перебивает атрибут hidden.
   Без этого экран логина не скрывается после входа. */
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px; line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
.screen { height: 100dvh; display: flex; flex-direction: column; max-width: 100vw; overflow: hidden; }

/* ---------- ВХОД ---------- */
.login-screen { align-items: center; justify-content: center; padding: 20px; }
.login-box { width: 100%; max-width: 360px; text-align: center; }
.login-logo {
  width: 56px; height: 56px; border-radius: 18px; margin: 0 auto 20px;
  background: var(--grad);
  box-shadow: 0 0 40px rgba(140,110,245,.45);
}
.login-box h1 { font-size: 26px; font-weight: 600; letter-spacing: -.02em; }
.login-box h1 span { color: var(--muted); font-weight: 300; }
.login-sub { color: var(--muted); font-size: 14px; margin: 6px 0 28px; }
#login-form { display: grid; gap: 10px; }
#login-form input {
  background: var(--bg-card); border: 1px solid var(--line);
  border-radius: 12px; padding: 13px 16px; color: var(--text);
  font-size: 16px; outline: none; width: 100%;
}
#login-form input:focus { border-color: rgba(139,120,246,.6); }
.btn-primary {
  background: var(--grad); border: none; border-radius: 12px;
  padding: 13px; color: #0A0A0B; font-weight: 600; font-size: 16px;
  cursor: pointer; width: 100%;
}
.login-error { color: #EF4444; font-size: 13px; }

/* viewport: без user-scalable=no (доступность), но зум по двойному тапу отключаем тапами */
body { touch-action: manipulation; }

/* ---------- ШАПКА ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  background: rgba(10,10,11,.92); backdrop-filter: blur(12px);
  position: sticky; top: 0; z-index: 20;
  flex: none;
}
.topbar-title { flex: 1; text-align: center; font-weight: 600; font-size: 16px; }
.icon-btn {
  background: none; border: none; color: var(--text);
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center; cursor: pointer; position: relative;
}
.icon-btn:active { background: rgba(255,255,255,.08); }
.badge {
  position: absolute; top: 6px; right: 6px; min-width: 16px; height: 16px;
  border-radius: 8px; background: var(--grad); color: #0A0A0B;
  font-size: 10px; font-weight: 700; display: grid; place-items: center;
  padding: 0 4px;
}

/* ---------- БОКОВАЯ ---------- */
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40;
  background: var(--bg-soft); border-right: 1px solid var(--line);
  transform: translateX(-100%); transition: transform .25s var(--ease);
  display: flex; flex-direction: column;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
}
.sidebar.open { transform: none; }
.sidebar-overlay {
  position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.5);
}
.sidebar-head {
  padding: 18px 16px 10px; font-size: 11px; letter-spacing: .16em;
  text-transform: uppercase; color: var(--muted);
}
.agent-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; cursor: pointer; border: none;
  background: none; width: 100%; text-align: left; color: var(--text);
}
.agent-item.active { background: rgba(255,255,255,.06); }
.agent-item .agent-avatar {
  width: 38px; height: 38px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-size: 15px; font-weight: 600;
  color: #0A0A0B; background: var(--grad);
}
.agent-item .agent-name { font-size: 15px; }
.agent-item .agent-role { font-size: 12px; color: var(--muted); }
.sidebar-exit {
  margin-top: auto; padding: 16px; background: none; border: none;
  border-top: 1px solid var(--line); color: var(--muted);
  font-size: 14px; cursor: pointer; text-align: left;
}

/* ---------- ЧАТ ---------- */
.chat { flex: 1; overflow-y: auto; scrollbar-width: none; min-height: 0; }
.chat::-webkit-scrollbar { display: none; }
.messages { padding: 16px 12px; display: flex; flex-direction: column; gap: 10px; }
.msg {
  max-width: 82%; padding: 10px 14px; border-radius: 16px;
  font-size: 15.5px; line-height: 1.5; white-space: pre-wrap;
  word-break: break-word; animation: rise .25s var(--ease) both;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg.user {
  align-self: flex-end; background: var(--grad); color: #0A0A0B;
  border-bottom-right-radius: 5px; font-weight: 500;
}
.msg.bot {
  align-self: flex-start; background: var(--bg-card);
  border: 1px solid var(--line); border-bottom-left-radius: 5px;
}
.msg.err { background: rgba(239,68,68,.1); border-color: rgba(239,68,68,.3); }
.typing { display: flex; gap: 5px; padding: 4px 16px 12px; }
.typing[hidden] { display: none; }
.dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: .15s; }
.dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%,60%,100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ---------- ВВОД ---------- */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px; padding-bottom: calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--line); background: var(--bg);
  flex: none;
}
#input {
  flex: 1; resize: none; min-height: 44px; max-height: 120px;
  background: var(--bg-card); border: 1px solid var(--line);
  border-radius: 14px; padding: 11px 14px; color: var(--text);
  font: inherit; font-size: 16px; outline: none;
}
#input:focus { border-color: rgba(139,120,246,.55); }
.send-btn {
  flex: none; width: 44px; height: 44px; border-radius: 14px;
  border: none; background: var(--grad); color: #0A0A0B;
  display: grid; place-items: center; cursor: pointer;
}
.send-btn:disabled { opacity: .4; }

/* ---------- ВХОДЯЩИЕ ---------- */
.inbox {
  position: fixed; inset: 0 0 0 auto; width: min(400px, 100%);
  background: var(--bg-soft); z-index: 45;
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  animation: slideIn .25s var(--ease);
}
@keyframes slideIn { from { transform: translateX(40px); opacity: 0; } }
.inbox-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--line); font-weight: 600;
}
.inbox-list { flex: 1; overflow-y: auto; padding: 12px; display: grid; gap: 8px; }
.inbox-item {
  background: var(--bg-card); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; cursor: pointer;
}
.inbox-item.unread { border-color: rgba(168,85,247,.5); }
.inbox-item .from { font-size: 12px; color: var(--accent-1); margin-bottom: 3px; }
.inbox-item .text {
  font-size: 14px; color: var(--text); display: -webkit-box;
  -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.inbox-item .when { font-size: 11px; color: var(--muted); margin-top: 5px; }
.inbox-empty { color: var(--muted); text-align: center; padding: 40px 20px; font-size: 14px; }

@media (min-width: 700px) {
  .sidebar { transform: none; }
  .sidebar-overlay { display: none !important; }
  .menu-btn { display: none; }
  .messages { max-width: 800px; margin: 0 auto; width: 100%; }
  .inbox { width: min(400px, calc(100% - 260px)); }
}

/* очень узкие экраны */
@media (max-width: 360px) {
  .msg { max-width: 92%; font-size: 15px; }
  .sidebar { width: 84vw; }
  #input { font-size: 16px; }
}
