:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e53935;
  --warn: #f5a623;
  --subtle: rgba(127, 127, 127, 0.14);
  /* Отступы безопасной зоны: системные + Telegram (полноэкранный режим, Bot API 8.0). */
  --safe-top: calc(env(safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Тёмные фолбэки на случай открытия вне Telegram (тема Telegram всё равно приоритетнее). */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --secondary-bg: var(--tg-theme-secondary-bg-color, #232e3c);
    --text: var(--tg-theme-text-color, #f5f5f5);
    --hint: var(--tg-theme-hint-color, #8b9aa8);
    --link: var(--tg-theme-link-color, #6ab3f3);
    --button: var(--tg-theme-button-color, #5288c1);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button { font: inherit; color: inherit; }

#app {
  padding: calc(12px + var(--safe-top)) calc(16px + var(--safe-right)) calc(32px + var(--safe-bottom)) calc(16px + var(--safe-left));
  max-width: 720px;
  margin: 0 auto;
}

/* --- Шапка вложенных экранов: всегда видна, крупная кнопка «назад» --- */
.screen-header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: calc(-12px - var(--safe-top)) -16px 8px;
  padding: calc(4px + var(--safe-top)) 12px 4px 4px;
  min-height: calc(52px + var(--safe-top));
  background: var(--bg);
  border-bottom: 1px solid var(--subtle);
}
.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px 0 6px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--link);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}
.back-btn:active { background: var(--subtle); }
.back-btn:focus-visible { outline: 2px solid var(--link); outline-offset: -2px; }
.back-chevron { font-size: 30px; line-height: 1; margin-top: -3px; }
.screen-title {
  flex: 1;
  min-width: 0;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
}

/* --- Состояния загрузки / ошибки --- */
.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  color: var(--hint);
  margin-top: 60px;
  font-size: 15px;
}
.state.loading { flex-direction: row; justify-content: center; }
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--subtle);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.state-title { color: var(--text); font-weight: 600; }
.state-detail { font-size: 14px; max-width: 320px; word-break: break-word; }
.retry-btn {
  min-height: 44px;
  padding: 0 22px;
  border: none;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  cursor: pointer;
}

/* --- Кнопка «к последним сообщениям» --- */
.fab {
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: calc(20px + var(--safe-bottom));
  z-index: 40;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--secondary-bg);
  color: var(--link);
  font-size: 22px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}
.fab[hidden] { display: none; }

.searchbar {
  display: flex;
  align-items: center;
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 10px 14px;
  margin-bottom: 14px;
}
.searchbar input {
  border: none;
  background: transparent;
  outline: none;
  width: 100%;
  min-width: 0;
  /* 16px — иначе iOS зумит страницу при фокусе. */
  font-size: 16px;
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
.searchbar input::placeholder { color: var(--hint); }
.search-icon { font-size: 14px; opacity: 0.6; margin-right: 8px; flex-shrink: 0; }
.searchbar.in-chat { margin-bottom: 10px; }

/* Шапка списка чатов тоже липкая: поиск и навигация доступны при прокрутке. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: calc(-12px - var(--safe-top)) -16px 4px;
  padding: calc(8px + var(--safe-top)) 16px 0;
  background: var(--bg);
}
.topbar .searchbar { flex: 1; min-width: 0; margin-bottom: 10px; }
.gear {
  flex-shrink: 0;
  margin-bottom: 10px;
  font-size: 20px;
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  text-align: center;
  background: var(--secondary-bg);
  border-radius: 12px;
  cursor: pointer;
}
.gear:active { opacity: 0.7; }

.section-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--hint);
  margin: 8px 2px 6px;
}

mark { background: var(--warn); color: #1c1c1e; border-radius: 3px; padding: 0 1px; }

.contact-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 14px;
  background: var(--secondary-bg);
  margin-bottom: 8px;
  cursor: pointer;
}
.contact-card:active { opacity: 0.75; }
.contact-card:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--button);
  color: var(--button-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 18px;
  flex-shrink: 0;
}
.avatar.group { background: var(--warn); font-size: 22px; }
.contact-info { flex: 1; min-width: 0; }
.contact-row { display: flex; align-items: baseline; gap: 8px; }
.contact-name { font-weight: 600; font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-time { color: var(--hint); font-size: 12px; flex-shrink: 0; }
.contact-preview {
  color: var(--hint);
  font-size: 13px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.contact-sub { color: var(--hint); font-size: 13px; margin-top: 2px; }
.badges { display: flex; gap: 6px; margin-top: 4px; }
.badge {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--subtle);
}
.badge.edit { color: var(--warn); }
.badge.delete { color: var(--danger); }
.badge.paused { color: var(--hint); }
.badges:empty { display: none; }

.empty { color: var(--hint); text-align: center; margin: 60px 12px 0; font-size: 15px; line-height: 1.4; }
#messages .empty { margin-top: 32px; }

.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 0 16px;
}
.profile-header .avatar { width: 72px; height: 72px; font-size: 26px; margin-bottom: 10px; }
.profile-header .name { font-size: 19px; font-weight: 700; }
.profile-header .username { color: var(--hint); font-size: 14px; margin-top: 2px; }

.stats-row {
  display: flex;
  justify-content: space-around;
  background: var(--secondary-bg);
  border-radius: 14px;
  padding: 14px 0;
  margin-bottom: 16px;
}
.stat { text-align: center; }
.stat .n { font-size: 18px; font-weight: 700; }
.stat .label { font-size: 12px; color: var(--hint); margin-top: 2px; }

.tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.tab {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  text-align: center;
  padding: 8px 4px;
  border: none;
  border-radius: 10px;
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}
.tab.active { background: var(--button); color: var(--button-text); font-weight: 600; }

/* --- Лента чата (мессенджер-стиль) --- */
#messages { display: flex; flex-direction: column; }

.msg-row { display: flex; margin-bottom: 4px; }
.msg-row.in { justify-content: flex-start; }
.msg-row.out { justify-content: flex-end; }

.bubble {
  max-width: 78%;
  border-radius: 16px;
  padding: 8px 12px 6px;
  position: relative;
  word-break: break-word;
}
.msg-row.in .bubble {
  background: var(--secondary-bg);
  border-bottom-left-radius: 5px;
}
.msg-row.out .bubble {
  background: var(--button);
  color: var(--button-text);
  border-bottom-right-radius: 5px;
}
.bubble.deleted { outline: 1px solid var(--danger); }
.bubble .sender { font-size: 12px; font-weight: 600; margin-bottom: 2px; }
.bubble .body { font-size: 15px; line-height: 1.35; white-space: pre-wrap; }
.bubble a { color: inherit; }
.bubble .meta {
  font-size: 11px;
  margin-top: 3px;
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: flex-end;
  opacity: 0.75;
}
.msg-row.in .bubble .meta { color: var(--hint); }
.bubble .flag { font-size: 11px; font-weight: 600; }
.bubble .flag.deleted { color: var(--danger); }
.bubble .flag.edited { color: var(--warn); }
.msg-row.out .bubble .flag.deleted,
.msg-row.out .bubble .flag.edited { color: var(--button-text); }

.day-sep { text-align: center; margin: 12px 0; }
.day-sep span {
  display: inline-block;
  font-size: 12px;
  color: var(--hint);
  background: var(--secondary-bg);
  padding: 3px 12px;
  border-radius: 10px;
}

.edit-history { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--subtle); }
.edit-history .row { font-size: 13px; margin-bottom: 4px; }
.edit-history .old { color: var(--hint); text-decoration: line-through; }


.media-note { font-size: 12px; color: var(--hint); font-style: italic; margin-top: 4px; }

.media { margin-top: 8px; }
.media img, .media video {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: 10px;
  background: #000;
  display: block;
  cursor: pointer;
}
.media audio { width: 100%; }

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  cursor: zoom-out;
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}

/* --- Удаление --- */
/* Кнопка маленькая визуально, но с увеличенной зоной нажатия (отрицательные поля). */
.bubble .meta .del-msg {
  cursor: pointer;
  opacity: 0.5;
  margin: -8px auto -8px -8px;
  padding: 8px;
  border: none;
  background: transparent;
  font-size: 13px;
  line-height: 1;
}
.bubble .meta .del-msg:hover { opacity: 1; }

/* Отделена от ленты и не залита цветом — чтобы не нажать случайно после прокрутки вниз. */
.danger-btn {
  display: block;
  width: 100%;
  margin: 40px 0 8px;
  padding: 12px;
  min-height: 44px;
  border: 1px solid var(--danger);
  border-radius: 12px;
  background: transparent;
  color: var(--danger);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* --- ИИ-конспекты --- */
.konspekt-box { margin-bottom: 14px; }
.konspekt-btn {
  display: block;
  width: 100%;
  padding: 11px;
  min-height: 44px;
  border: none;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.konspekt-btn:disabled { opacity: 0.6; cursor: default; }
.konspekt-hint { font-size: 12px; color: var(--hint); margin: 6px 2px 0; }
.konspekt-card {
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 10px 12px;
  margin-top: 8px;
}
.konspekt-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--hint);
  margin-bottom: 6px;
}
.konspekt-del { cursor: pointer; opacity: 0.6; border: none; background: transparent; padding: 8px; margin: -8px -8px -8px 0; }
.konspekt-del:hover { opacity: 1; }
.konspekt-content { font-size: 14px; white-space: pre-wrap; word-break: break-word; }

/* --- Задачи и напоминания --- */
.task-add { display: flex; gap: 8px; margin: 10px 0 4px; }
.task-add input {
  flex: 1;
  min-width: 0;
  border: none;
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 11px 14px;
  min-height: 44px;
  font-size: 16px;
  color: var(--text);
  outline: none;
}
.task-add button:disabled { opacity: 0.6; }
.task-add button {
  flex-shrink: 0;
  border: none;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-size: 14px;
  font-weight: 600;
  padding: 0 16px;
  cursor: pointer;
}

.task-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 14px;
  background: var(--secondary-bg);
  margin-bottom: 8px;
}
.task-card.done .task-text { text-decoration: line-through; color: var(--hint); }
.task-main { flex: 1; min-width: 0; }
.task-text { font-size: 15px; word-break: break-word; }
.task-due { display: inline-block; font-size: 12px; color: var(--hint); margin-top: 4px; }
.task-due.overdue { color: var(--danger); font-weight: 600; }
.task-actions { display: flex; gap: 2px; flex-shrink: 0; margin: -6px -6px -6px 0; }
.task-act {
  cursor: pointer;
  opacity: 0.75;
  font-size: 17px;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: transparent;
}
.task-act:active { background: var(--subtle); }
.task-act:hover { opacity: 1; }

.task-card.editing { display: block; }
.task-edit { display: flex; flex-direction: column; gap: 8px; }
.task-edit input {
  border: none;
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  color: var(--text);
  outline: none;
  width: 100%;
}
.task-edit-btns { display: flex; gap: 8px; }
.task-edit-btns button {
  border: none;
  border-radius: 10px;
  padding: 9px 14px;
  min-height: 44px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.task-save { background: var(--button); color: var(--button-text); }
.task-cancel { background: var(--subtle); color: var(--text); }

/* --- Настройки --- */
.settings-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--hint); margin: 18px 2px 8px; }
.settings-hint { font-size: 13px; color: var(--hint); margin: 0 2px 10px; }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 12px;
  background: var(--secondary-bg);
  margin-bottom: 8px;
  min-height: 52px;
  cursor: pointer;
}
.toggle-name { flex: 1; min-width: 0; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.switch { position: relative; display: inline-block; width: 46px; height: 28px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch input:disabled + .slider { opacity: 0.6; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--hint);
  border-radius: 28px;
  transition: 0.2s;
  cursor: pointer;
}
.slider::before {
  content: "";
  position: absolute;
  height: 22px;
  width: 22px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
}
.switch input:checked + .slider { background: var(--button); }
.switch input:checked + .slider::before { transform: translateX(18px); }

/* Переключатель аккаунтов над списком чатов — только раскладка; визуал — в theme.css. */
.account-switcher {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.account-switcher::-webkit-scrollbar { display: none; }
.account-switcher[hidden] { display: none; }
.account-tab { flex: 0 0 auto; white-space: nowrap; }
.account-label { display: inline-block; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: middle; }
