/* ─────────────────────────────────────────────────────────────────────────────
   TG Mini App · личный AI-чат. Тёмная тема по умолчанию, без внешних шрифтов.
   ───────────────────────────────────────────────────────────────────────────── */

:root {
  color-scheme: dark;

  --bg: #0b0d12;
  --bg-elev: #11141b;
  --bg-elev-2: #171b24;
  --bg-hover: #1d2230;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.12);

  --text: #e8eaf0;
  --text-soft: #b9c0d0;
  --muted: #8b93a7;

  --accent: #5b8cff;
  --accent-2: #7bb0ff;
  --accent-soft: rgba(91, 140, 255, 0.14);
  --user-bubble: linear-gradient(135deg, #3f6fe0, #5b8cff);
  --user-text: #ffffff;
  --assistant-bubble: #171b24;
  --danger: #ff6b6b;

  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 14px 38px rgba(0, 0, 0, 0.42);
  --chunk-speed: 120ms; /* длительность «проявления» каждого чанка */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue",
    Arial, "Noto Sans", sans-serif;
}

/* Системная светлая тема — мягкий светлый вариант того же дизайна. */
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;

    --bg: #f4f5f9;
    --bg-elev: #ffffff;
    --bg-elev-2: #ffffff;
    --bg-hover: #eef0f7;
    --border: rgba(16, 20, 34, 0.09);
    --border-strong: rgba(16, 20, 34, 0.16);

    --text: #141824;
    --text-soft: #3b4356;
    --muted: #6d7688;

    --accent: #3f6fe0;
    --accent-2: #5b8cff;
    --accent-soft: rgba(63, 111, 224, 0.12);
    --assistant-bubble: #ffffff;
    --shadow: 0 14px 38px rgba(20, 24, 36, 0.12);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

/* ─────────────────────────────── каркас ─────────────────────────────── */

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ─────────────────────────── список чатов ──────────────────────────── */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 288px;
  flex: 0 0 288px;
  padding: calc(14px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
  padding-left: calc(12px + env(safe-area-inset-left));
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
}

.sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand__logo {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent-2);
  font-size: 12px;
  font-weight: 700;
}

.brand__name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-soft);
}

.new-chat {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border-strong);
  color: var(--text-soft);
  transition: background var(--chunk-speed), color var(--chunk-speed), transform var(--chunk-speed);
}

.new-chat:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.new-chat:active {
  transform: scale(0.985);
}

.new-chat__plus {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-2);
  font-size: 15px;
  line-height: 1;
}

.voice-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 2px 4px;
}
.voice-picker__label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  padding-left: 2px;
}
.voice-select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-soft);
  font-size: 13px;
  outline: none;
  cursor: pointer;
}
.voice-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.export-btn {
  width: 100%;
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-soft);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
}
.export-btn:hover { background: var(--bg-hover); color: var(--text); }

.mic-btn {
  display: grid; place-items: center;
  width: 36px; height: 36px; flex: 0 0 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-soft);
  font-size: 16px;
  cursor: pointer;
}
.mic-btn.is-recording { background: rgba(255,107,107,0.15); border-color: var(--danger); color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }

.chats {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.chats__skeleton {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 8px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  text-align: left;
  transition: background var(--chunk-speed);
  animation: fadeInChunk 160ms ease-out both;
}

.chat-item:hover {
  background: var(--bg-hover);
}

.chat-item.is-active {
  background: var(--accent-soft);
}

.chat-item__text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.chat-item__title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item__preview {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item__del {
  opacity: 0;
  padding: 4px 6px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
  transition: opacity var(--chunk-speed), color var(--chunk-speed), background var(--chunk-speed);
}

.chat-item:hover .chat-item__del,
.chat-item.is-active .chat-item__del {
  opacity: 1;
}

.chat-item__del:hover {
  color: var(--danger);
  background: rgba(255, 107, 107, 0.12);
}

.chats__empty {
  padding: 18px 10px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.sidebar__foot {
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.me {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
}

.me__text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.me__name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.me__status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #3ddc84;
  box-shadow: 0 0 8px rgba(61, 220, 132, 0.7);
}

.ava {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text-soft);
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
}

.ava--sm {
  width: 32px;
  height: 32px;
  flex-basis: 32px;
  border-radius: 10px;
  font-size: 12px;
}

.ava img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ─────────────────────────────── чат ───────────────────────────────── */

.chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  background: radial-gradient(120% 80% at 100% 0%, rgba(91, 140, 255, 0.07), transparent 60%), var(--bg);
}

.chat__head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  padding-right: calc(16px + env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  background: color-mix(in srgb, var(--bg-elev) 82%, transparent);
  backdrop-filter: blur(12px);
}

.chat__titles {
  flex: 1;
  min-width: 0;
}

.chat__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat__sub {
  margin: 1px 0 0;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  border-radius: 11px;
  color: var(--text-soft);
  font-size: 16px;
  transition: background var(--chunk-speed), color var(--chunk-speed), transform var(--chunk-speed);
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.icon-btn:active {
  transform: scale(0.94);
}

/* ───────────────────────────── сообщения ───────────────────────────── */

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  padding: 18px 16px 8px;
  padding-right: calc(16px + env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.messages::-webkit-scrollbar,
.chats::-webkit-scrollbar {
  width: 8px;
}

.messages::-webkit-scrollbar-thumb,
.chats::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
}

.empty {
  margin: auto;
  max-width: 420px;
  text-align: center;
  color: var(--muted);
  animation: fadeInChunk 240ms ease-out both;
}

.empty__logo {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 18px;
  background: var(--accent-soft);
  color: var(--accent-2);
  font-size: 20px;
  font-weight: 700;
}

.empty__title {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.empty__text {
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
}

.msg {
  display: flex;
  max-width: min(720px, 86%);
  animation: msgIn 320ms cubic-bezier(0.22, 0.61, 0.36, 1) both;
  will-change: transform, opacity;
}

.msg--user {
  align-self: flex-end;
}

.msg--assistant {
  align-self: flex-start;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.bubble {
  position: relative;
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  overflow-wrap: anywhere;
  transition: box-shadow 300ms, border-color 300ms;
}

.bubble .content {
  white-space: pre-wrap;
}

.msg--assistant .bubble .content {
  white-space: normal;
}

.msg--user .bubble {
  background: var(--user-bubble);
  border-color: transparent;
  color: var(--user-text);
  border-bottom-right-radius: 6px;
  box-shadow: 0 8px 22px rgba(63, 111, 224, 0.28);
}

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

/* Кнопки под ответом как в DeepSeek: Копировать / Перегенерировать */
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-soft);
  cursor: pointer;
  transition: background 150ms, color 150ms, border-color 150ms, transform 150ms;
}
.action-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
  border-color: var(--border-strong);
}
.action-btn:active {
  transform: scale(0.97);
}
.action-btn svg {
  flex: 0 0 14px;
  opacity: 0.9;
}
.action-btn.is-playing,
.action-btn.is-active {
  color: var(--accent-2);
  border-color: var(--accent);
  background: var(--accent-soft);
}
.tts-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: ttsSpin 0.7s linear infinite;
}
@keyframes ttsSpin { to { transform: rotate(360deg); } }
.is-karaoke .karaoke-word { transition: background 120ms, color 120ms, box-shadow 120ms; padding: 1px 2px; border-radius: 4px; }
.karaoke-word.is-active { background: var(--accent-soft); color: var(--accent-2); box-shadow: 0 0 0 1px var(--accent); }

/* Markdown внутри ответа ассистента — жирный без звёздочек */
.msg--assistant .content p {
  margin: 0 0 8px;
}
.msg--assistant .content p:last-child {
  margin-bottom: 0;
}
.msg--assistant .content strong {
  font-weight: 700;
}
.msg--assistant .content em {
  font-style: italic;
}
.msg--assistant .content code {
  padding: 1px 5px;
  border-radius: 6px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  white-space: pre-wrap;
  word-break: break-all;
}
.msg--assistant .content pre {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  overflow-x: auto;
}
.msg--assistant .content pre code {
  padding: 0;
  border: 0;
  background: transparent;
  white-space: pre;
}
.code-copy {
  position: absolute;
  top: 6px;
  right: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--muted);
  cursor: pointer;
  opacity: 0.85;
  transition: background 120ms, color 120ms, opacity 120ms;
}
.code-copy:hover { background: var(--bg-hover); color: var(--text); opacity: 1; }
.msg--assistant .content pre:hover .code-copy { opacity: 1; }
.code-run {
  position: absolute;
  top: 6px;
  right: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--muted);
  cursor: pointer;
  opacity: 0.85;
  font-size: 12px;
}
.code-run:hover { background: var(--accent-soft); color: var(--accent); opacity: 1; }
.run-output {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  font-family: ui-monospace, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  max-height: 200px;
  overflow: auto;
}
.msg--assistant .content ul,
.msg--assistant .content ol {
  margin: 8px 0;
  padding-left: 22px;
}
.msg--assistant .content li {
  margin: 3px 0;
}
.msg--assistant .content blockquote {
  margin: 8px 0;
  padding: 6px 12px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 6px;
  color: var(--text-soft);
}
.msg--assistant .content a {
  color: var(--accent-2);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.msg--assistant .content h1,
.msg--assistant .content h2,
.msg--assistant .content h3,
.msg--assistant .content h4,
.msg--assistant .content h5,
.msg--assistant .content h6 {
  margin: 10px 0 6px;
  font-weight: 700;
  line-height: 1.35;
}
.msg--assistant .content h1 { font-size: 1.25em; }
.msg--assistant .content h2 { font-size: 1.15em; }
.msg--assistant .content h3 { font-size: 1.05em; }
.msg--assistant .content hr {
  margin: 10px 0;
  border: 0;
  border-top: 1px solid var(--border-strong);
}

.msg__meta {
  margin-top: 5px;
  font-size: 11px;
  opacity: 0.65;
}

.msg--user .msg__meta {
  text-align: right;
}

/* Каждый приходящий чанк проявляется мягко — текст не «прыгает». */
.chunk {
  animation: fadeInChunk var(--chunk-speed) ease-out both;
}

/* Мигающая каретка в конце пузыря, пока идёт стрим. */
.caret {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.15em;
  background: currentColor;
  border-radius: 2px;
  animation: caretBlink 1s steps(1, end) infinite;
}

/* ───────────────────────────── композер ────────────────────────────── */

.composer {
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  padding-right: calc(16px + env(safe-area-inset-right));
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}

.composer__box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 900px;
  margin: 0 auto;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  background: var(--bg-elev-2);
  transition: border-color var(--chunk-speed), box-shadow var(--chunk-speed);
}

.composer__box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.composer__input {
  flex: 1;
  max-height: 160px;
  padding: 9px 0;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.45;
  overflow-y: auto;
  scrollbar-width: thin;
}

.composer__input::placeholder {
  color: var(--muted);
}

.send {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  transition: transform var(--chunk-speed), opacity var(--chunk-speed), background var(--chunk-speed);
}

.send svg {
  width: 19px;
  height: 19px;
}

.send:hover {
  background: var(--accent-2);
}

.send:active {
  transform: scale(0.92);
}

.send:disabled {
  opacity: 0.45;
  cursor: default;
}

.composer__hint {
  max-width: 900px;
  margin: 6px auto 0;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}

/* ─────────────────────── тосты и оверлей drawer ────────────────────── */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  max-width: min(90vw, 460px);
  padding: 11px 16px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: fadeInChunk 160ms ease-out both;
}

.toast--error {
  border-color: rgba(255, 107, 107, 0.4);
  color: var(--danger);
}

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(4, 6, 12, 0.55);
  backdrop-filter: blur(2px);
  animation: fadeInChunk 160ms ease-out both;
}

.only-mobile {
  display: none;
}

/* ─────────────────────────── мобильный режим ───────────────────────── */

@media (max-width: 820px) {
  .only-mobile {
    display: grid;
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    width: 84%;
    max-width: 320px;
    flex-basis: auto;
    transform: translateX(-102%);
    transition: transform 220ms cubic-bezier(0.22, 0.61, 0.36, 1);
    box-shadow: var(--shadow);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar .icon-btn {
    display: grid;
  }

  .msg {
    max-width: 92%;
  }

  .composer__hint {
    display: none;
  }
}

@media (max-width: 420px) {
  .messages {
    padding-left: calc(10px + env(safe-area-inset-left));
    padding-right: calc(10px + env(safe-area-inset-right));
  }
}

/* Индикатор «печатает…» до первого чанка. */
.typing {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 2px 0;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-soft);
  opacity: 0.35;
  animation: typingPulse 1.1s ease-in-out infinite;
}

.typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

/* Заглушка-скелет для загрузки истории. */
.skeleton {
  display: block;
  height: 14px;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--bg-elev-2), var(--bg-hover), var(--bg-elev-2));
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

.msg--error .bubble {
  background: rgba(255, 107, 107, 0.1);
  border-color: rgba(255, 107, 107, 0.4);
  color: var(--danger);
}

@keyframes fadeInChunk {
  from {
    opacity: 0;
    transform: translateY(2px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
    filter: blur(2px);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
    filter: blur(0);
  }
}

@keyframes streamIn {
  from { opacity: 0.88; }
  to   { opacity: 1; }
}
.msg--assistant .bubble .content.is-updating {
  animation: streamIn 140ms ease-out both;
}

@keyframes caretBlink {
  0%,
  50% {
    opacity: 1;
  }
  50.01%,
  100% {
    opacity: 0;
  }
}

@keyframes typingPulse {
  0%,
  100% {
    opacity: 0.25;
    transform: translateY(0);
  }
  50% {
    opacity: 0.9;
    transform: translateY(-2px);
  }
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chunk,
  .msg,
  .chat-item,
  .empty {
    animation: none;
  }
  .messages {
    scroll-behavior: auto;
  }
}
