:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #0b0b0f);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --group-bg: var(--tg-theme-secondary-bg-color, #f1f1f4);

  /* Производные токены */
  --card: var(--bg);
  --radius-lg: 20px;
  --radius-md: 16px;
  --radius-sm: 12px;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.04), 0 6px 20px rgba(0, 0, 0, 0.06);
  --shadow-bubble: 0 1px 1.5px rgba(0, 0, 0, 0.06);
  --border: rgba(128, 128, 128, 0.14);
  --pressable: cubic-bezier(0.2, 0.8, 0.2, 1);
}
body.dark {
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-bubble: 0 1px 2px rgba(0, 0, 0, 0.3);
  --border: rgba(255, 255, 255, 0.1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--group-bg);
  color: var(--text);
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}
/* Страница зафиксирована — ничего не скроллится и не дёргается при клавиатуре */
body {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.view { display: flex; flex-direction: column; height: var(--app-height, 100%); }
.hidden { display: none !important; }

/* ===== Топбар ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px 12px;
  position: sticky;
  top: 0;
  background: var(--group-bg);
  z-index: 10;
}
.topbar h1 { font-size: 26px; margin: 0; font-weight: 800; letter-spacing: -0.5px; }
#chat-view .topbar { padding: 12px 14px; background: var(--bg); border-bottom: 1px solid var(--border); }
#chat-view .topbar h1 { font-size: 17px; font-weight: 700; flex: 1; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  background: none;
  border: none;
  color: var(--link);
  font-size: 22px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  transition: transform 0.15s var(--pressable), opacity 0.15s;
}
.icon-btn:active { transform: scale(0.88); opacity: 0.7; }

/* Круглая акцентная кнопка «+» */
#new-chat-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 24px; font-weight: 400;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 45%, transparent);
}
#back-btn { font-size: 28px; color: var(--link); }

/* ===== Список чатов ===== */
.chat-list {
  list-style: none;
  margin: 8px 14px;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}
.chat-list:empty { display: none; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 12px 14px;
  cursor: pointer;
  position: relative;
  transition: background 0.15s;
}
.chat-item:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 61px; right: 0; bottom: 0;
  height: 1px;
  background: var(--border);
}
.chat-item:active { background: var(--group-bg); }

.avatar {
  width: 44px; height: 44px;
  flex-shrink: 0;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 19px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}
.chat-item .title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-item .del {
  background: none; border: none; cursor: pointer;
  width: 30px; height: 30px;
  border-radius: 50%;
  color: var(--hint);
  opacity: 0.5;
  transition: opacity 0.15s, transform 0.15s var(--pressable);
}
.chat-item .del::before { content: '🗑'; font-size: 15px; }
.chat-item .del:active { transform: scale(0.85); opacity: 1; }
.chat-item .chevron { color: var(--hint); font-size: 20px; opacity: 0.5; font-weight: 600; }

/* Пустой стейт */
.empty {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center;
  padding: 0 30px;
  animation: rise 0.4s var(--pressable) both;
}
.empty-icon { font-size: 56px; margin-bottom: 14px; opacity: 0.85; }
.empty-title { font-size: 19px; font-weight: 700; margin: 0 0 6px; }
.empty-sub { color: var(--hint); margin: 0; font-size: 15px; }

/* ===== Переписка ===== */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--group-bg);
}
.msg {
  max-width: 84%;
  padding: 9px 13px;
  border-radius: 18px;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.45;
  font-size: 15.5px;
  box-shadow: var(--shadow-bubble);
}
.msg.anim { animation: pop 0.26s var(--pressable) both; }
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 6px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--card);
  color: var(--text);
  border-bottom-left-radius: 6px;
}
.msg img { max-width: 100%; border-radius: 12px; margin-bottom: 4px; display: block; }

/* Индикатор «печатает» — перекатывающиеся точки */
.msg.typing { padding: 14px 16px; }
.typing .dot {
  display: inline-block;
  width: 7px; height: 7px;
  margin: 0 2px;
  border-radius: 50%;
  background: var(--hint);
  animation: roll 1.3s infinite both;
}
.typing .dot:nth-child(2) { animation-delay: 0.18s; }
.typing .dot:nth-child(3) { animation-delay: 0.36s; }
@keyframes roll {
  0%, 70%, 100% { opacity: 0.35; transform: translateY(0); }
  35% { opacity: 1; transform: translateY(-5px); }
}

/* Markdown ассистента */
.msg.assistant { white-space: pre-wrap; }
.msg.assistant.md { white-space: normal; }
.msg.assistant :first-child { margin-top: 0; }
.msg.assistant :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 6px 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { margin: 10px 0 6px; line-height: 1.25; font-weight: 700; }
.msg.assistant h1 { font-size: 1.22em; }
.msg.assistant h2 { font-size: 1.13em; }
.msg.assistant h3 { font-size: 1.04em; }
.msg.assistant ul, .msg.assistant ol { margin: 6px 0; padding-left: 22px; }
.msg.assistant li { margin: 3px 0; }
.msg.assistant code {
  background: rgba(128, 128, 128, 0.16);
  padding: 1px 5px;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
}
.msg.assistant pre {
  background: rgba(128, 128, 128, 0.13);
  padding: 11px 13px;
  border-radius: 12px;
  overflow-x: auto;
  margin: 8px 0;
  border: 1px solid var(--border);
}
.msg.assistant pre code { background: none; padding: 0; font-size: 0.86em; line-height: 1.5; }
.msg.assistant a { color: var(--link); text-decoration: none; }
.msg.assistant blockquote {
  margin: 6px 0; padding-left: 11px;
  border-left: 3px solid var(--accent);
  color: var(--hint);
}

/* ===== Превью вложения ===== */
.attach-preview {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  animation: rise 0.25s var(--pressable) both;
}
.attach-preview img { height: 52px; width: 52px; object-fit: cover; border-radius: 10px; box-shadow: var(--shadow-bubble); }
.attach-remove {
  margin-left: auto;
  background: var(--group-bg); border: none; color: var(--text);
  border-radius: 50%; width: 28px; height: 28px; cursor: pointer;
  font-size: 13px;
  transition: transform 0.15s var(--pressable);
}
.attach-remove:active { transform: scale(0.85); }

/* ===== Композер ===== */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--border);
}
#attach-btn { font-size: 20px; padding: 8px; align-self: center; }
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 10px 16px;
  font-size: 16px;
  font-family: inherit;
  background: var(--group-bg);
  color: var(--text);
  max-height: 120px;
  transition: border-color 0.2s;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.icon-btn.send {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
  box-shadow: 0 3px 12px color-mix(in srgb, var(--accent) 40%, transparent);
}
.icon-btn.send:disabled { opacity: 0.45; box-shadow: none; }

/* Скрыть скроллбары */
.messages::-webkit-scrollbar, .chat-list::-webkit-scrollbar { width: 0; }

/* ===== Анимации ===== */
@keyframes pop {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Голосовой режим ===== */
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.voice-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: color-mix(in srgb, var(--group-bg) 92%, transparent);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  display: flex; align-items: center; justify-content: center;
  animation: rise 0.25s var(--pressable);
}
.voice-overlay.hidden { display: none; }
.voice-card {
  display: flex; flex-direction: column; align-items: center;
  gap: 28px; padding: 40px;
}
.voice-orb {
  width: 140px; height: 140px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--accent), color-mix(in srgb, var(--accent) 55%, #000));
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 45%, transparent);
  transition: transform 0.12s ease;
}
.voice-orb.listening { animation: voicePulse 1.6s ease-in-out infinite; }
.voice-orb.speaking  { animation: voicePulse 0.7s ease-in-out infinite; }
@keyframes voicePulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 50%, transparent); }
  70%  { box-shadow: 0 0 0 32px color-mix(in srgb, var(--accent) 0%, transparent); }
  100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 0%, transparent); }
}
.voice-status { color: var(--hint); font-size: 16px; margin: 0; min-height: 20px; }
.voice-hangup {
  border: none; border-radius: 999px; padding: 14px 36px;
  background: #ff3b30; color: #fff; font-size: 16px; font-weight: 600;
  box-shadow: 0 4px 16px rgba(255, 59, 48, 0.35);
}
.voice-hangup:active { transform: scale(0.96); }
