/* Окс — TG-стиль: тёмная нейтральная тема (как в TG на Android). */
:root {
  --bg-chat: #1a1a1a;
  --bg-panel: #1c1c1c;
  --bg-input: #2c2c2c;
  --bubble-me: #232634;
  --bubble-bot: #3a3a3a;
  --accent: #3888f8;
  --text: #f5f5f5;
  --text-dim: #8a8a8a;
  --border: #0d0d0d;
  --online: #4dcc4d;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  /* фикс: не даём странице быть выше видимого экрана (адресная строка мобильного браузера) */
  position: fixed; inset: 0; width: 100%;
  background: var(--bg-chat);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  overflow: hidden;
  overscroll-behavior: none;
}
#root { display: flex; height: 100vh; height: 100dvh; }

/* ===== Sidebar — список чатов ===== */
#sidebar {
  width: 300px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.sb-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
}
.sb-title { font-weight: 700; font-size: 17px; color: var(--accent); }
#btn-new {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: 20px; cursor: pointer;
  line-height: 1;
}
#btn-new:hover { filter: brightness(1.1); }
#session-list { flex: 1; overflow-y: auto; padding: 4px 0; }

.chat-item {
  display: flex; gap: 10px; align-items: center;
  padding: 9px 12px; cursor: pointer; margin: 1px 6px; border-radius: 10px;
  user-select: none; -webkit-user-select: none;
}
.chat-item:hover { background: #262626; }
.chat-item.active { background: #323232; }
.chat-item.active .chat-name,
.chat-item.active .chat-time,
.chat-item.active .chat-preview { color: #fff; }
.chat-item.active .chat-preview { opacity: .8; }
.chat-avatar {
  width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, #2a9df0, #35d0d8);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 19px; color: #fff;
}
.chat-mid { flex: 1; min-width: 0; }
.chat-row1 { display: flex; justify-content: space-between; align-items: baseline; }
.chat-name {
  font-weight: 600; font-size: 15px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-time { font-size: 12px; color: var(--text-dim); flex-shrink: 0; margin-left: 6px; }
.chat-row2 {
  display: flex; justify-content: space-between; align-items: center; margin-top: 2px;
}
.chat-preview {
  font-size: 13.5px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.s-pin { font-size: 12px; flex-shrink: 0; margin-left: 6px; }
.s-run {
  width: 9px; height: 9px; border-radius: 50%; background: var(--online);
  animation: pulse 1.2s infinite; flex-shrink: 0; margin-left: 6px;
}
@keyframes pulse { 50% { opacity: .3; } }

.sb-foot { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.sb-foot button {
  flex: 1; padding: 8px; border-radius: 8px; border: 1px solid var(--border);
  background: transparent; color: var(--text-dim); cursor: pointer; font-size: 13px;
}
.sb-foot button:hover { color: var(--text); border-color: var(--accent); }
.sb-foot button.on { color: var(--accent); border-color: var(--accent); }

/* ===== Main ===== */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
#topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 7px 16px; border-bottom: 1px solid var(--border); background: var(--bg-panel);
}
.top-avatar {
  width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, #2a9df0, #35d0d8);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; color: #fff;
}
.top-mid { flex: 1; min-width: 0; }
#chat-title {
  font-weight: 600; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-status { font-size: 12.5px; color: var(--text-dim); }
.chat-status.typing { color: var(--accent); }
#btn-menu { display: none; background: none; border: none; color: var(--text); font-size: 20px; cursor: pointer; }
#btn-stop {
  padding: 6px 14px; border-radius: 8px; border: 1px solid #e05555;
  background: transparent; color: #e05555; cursor: pointer; font-size: 13px;
}

/* ===== Messages ===== */
#messages {
  flex: 1; overflow-y: auto; padding: 16px 8%;
  display: flex; flex-direction: column; gap: 3px;
}
#messages::before { content: ""; margin-top: auto; }
.day-divider {
  display: flex; justify-content: center; margin: 14px 0 8px;
}
.day-divider span {
  background: rgba(0,0,0,.55); color: #fff; font-size: 12.5px; font-weight: 500;
  padding: 4px 10px; border-radius: 12px;
}
.msg-row { display: flex; }
.msg-row.me { justify-content: flex-end; }
.msg-row.bot { justify-content: flex-start; }
.msg {
  max-width: 78%; padding: 7px 12px 6px; border-radius: 16px;
  line-height: 1.42; word-wrap: break-word; overflow-wrap: break-word;
  font-size: 15px; position: relative;
}
.msg.user { background: var(--bubble-me); }
.msg.agent { background: var(--bubble-bot); }
/* группировка: у подряд идущих пузырей стык менее скруглён */
.msg-row + .msg-row.me .msg.user { border-top-right-radius: 6px; }
.msg-row + .msg-row.bot .msg.agent { border-top-left-radius: 6px; }
.msg-row.me:has(+ .msg-row.me) .msg.user { border-bottom-right-radius: 6px; }
.msg-row.bot:has(+ .msg-row.bot) .msg.agent { border-bottom-left-radius: 6px; }
.msg-row + .msg-row { margin-top: 2px; }
.msg.agent p { margin: 0 0 6px; }
.msg.agent p:last-child { margin-bottom: 0; }
.msg.agent ul, .msg.agent ol { margin: 2px 0 6px 20px; }
.msg.agent code {
  background: rgba(0,0,0,.35); padding: 2px 6px; border-radius: 5px;
  font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 13px;
}
.msg.agent pre {
  background: #0a0a0a; border-radius: 10px;
  padding: 12px; overflow-x: auto; margin: 6px 0; position: relative;
}
.msg.agent pre code { background: none; padding: 0; }
.copy-btn {
  position: absolute; top: 6px; right: 6px; padding: 2px 8px; font-size: 11px;
  border-radius: 5px; border: 1px solid #3a3a3a; background: #1c1c1c;
  color: var(--text-dim); cursor: pointer; opacity: 0; transition: opacity .15s;
}
.msg.agent pre:hover .copy-btn { opacity: 1; }
.msg-meta {
  float: right; margin: 8px 0 -4px 10px; font-size: 11.5px; color: rgba(255,255,255,.45);
  user-select: none;
}
.msg.user .msg-meta { color: rgba(255,255,255,.55); }
.tool-block { margin: 6px 0 4px; font-size: 13px; clear: both; }
.tool-block summary {
  cursor: pointer; color: var(--accent); font-family: monospace; font-size: 12px;
  padding: 3px 8px; border-radius: 6px; background: rgba(0,0,0,.3); display: inline-block;
}
.tool-block summary:hover { filter: brightness(1.2); }
.tool-block pre {
  background: rgba(0,0,0,.4); padding: 8px; border-radius: 8px; margin-top: 6px;
  white-space: pre-wrap; font-size: 12px;
}
.msg img.attach { max-width: 100%; border-radius: 10px; margin-top: 6px; }

/* Typing indicator — точки в шапке + мягкий блок у низа */
#typing {
  display: flex; gap: 4px; padding: 0 16px 8px; align-self: flex-start;
  position: absolute; bottom: 70px; left: 12px;
  background: var(--bubble-bot); border-radius: 14px; padding: 8px 12px;
  z-index: 5;
}
#typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
  animation: blink 1.4s infinite;
}
#typing span:nth-child(2) { animation-delay: .2s; }
#typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 30% { opacity: .2; } }

/* ===== Composer ===== */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px 10px; background: var(--bg-panel);
}
.composer-box {
  flex: 1; display: flex; align-items: flex-end;
  background: var(--bg-input); border-radius: 22px; padding: 4px 6px 4px 12px;
}
#composer textarea {
  flex: 1; resize: none; border: none; background: transparent;
  color: var(--text); padding: 9px 4px;
  font-family: inherit; font-size: 15px; max-height: 140px; outline: none;
}
#composer textarea::placeholder { color: var(--text-dim); }
#btn-attach {
  width: 36px; height: 36px; border-radius: 50%; border: none; cursor: pointer;
  font-size: 17px; background: transparent; color: var(--text-dim); flex-shrink: 0;
}
#btn-attach:hover { color: var(--accent); }
#btn-send {
  width: 46px; height: 46px; border-radius: 50%; border: none; cursor: pointer;
  font-size: 19px; background: var(--accent); color: #fff; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: transform .15s;
}
#btn-send:hover { filter: brightness(1.1); }
#btn-send:disabled { opacity: .5; cursor: default; }
#attach-preview { display: flex; align-items: center; gap: 8px; padding: 0 16px 8px; }
#attach-preview img { max-height: 60px; border-radius: 8px; border: 1px solid var(--border); }
#btn-attach-clear {
  background: rgba(0,0,0,.5); color: #fff; border: none; border-radius: 50%;
  width: 24px; height: 24px; cursor: pointer;
}

/* Context menu */
#ctx-menu {
  position: fixed; z-index: 100; background: #262626;
  border: 1px solid #3a3a3a; border-radius: 12px; overflow: hidden;
  box-shadow: 0 8px 30px rgba(0,0,0,.6); min-width: 170px;
}
#ctx-menu button {
  display: block; width: 100%; padding: 10px 16px; background: none; border: none;
  color: var(--text); text-align: left; cursor: pointer; font-size: 14px;
}
#ctx-menu button:hover { background: #333; }
#ctx-menu button.danger { color: #e05555; }

/* Login */
#login-view {
  position: fixed; inset: 0; background: var(--bg-chat); z-index: 200;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
}
.login-avatar {
  width: 84px; height: 84px; border-radius: 50%;
  background: linear-gradient(135deg, #2a9df0, #35d0d8);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 36px; color: #fff;
}
#login-view h1 { color: var(--text); font-size: 24px; font-weight: 600; }
#login-view input {
  width: 280px; padding: 12px 16px; border-radius: 22px; border: 1px solid #3a3a3a;
  background: var(--bg-input); color: var(--text); font-size: 15px; outline: none; text-align: center;
}
#login-view input:focus { border-color: var(--accent); }
#login-view button {
  width: 280px; padding: 12px; border-radius: 22px; border: none;
  background: var(--accent); color: #fff; font-size: 15px; font-weight: 600; cursor: pointer;
}
#login-view button:hover { filter: brightness(1.1); }
#login-error { color: #e05555; font-size: 13px; min-height: 18px; }

/* Mobile */
@media (max-width: 768px) {
  #sidebar {
    position: fixed; left: -300px; top: 0; bottom: 0; z-index: 50;
    transition: left .25s ease;
  }
  #sidebar.open { left: 0; }
  #btn-menu { display: block; }
  .msg { max-width: 90%; }
  #messages { padding: 12px 10px; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 3px; }
::-webkit-scrollbar-track { background: transparent; }