:root {
  --bg: var(--tg-theme-bg-color, #0e1621);
  --secondary: var(--tg-theme-secondary-bg-color, #17212b);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #8e9aab);
  --link: var(--tg-theme-link-color, #6ab3f3);
  --bubble-in: var(--tg-theme-secondary-bg-color, #24303d);
  --bubble-out: var(--tg-theme-button-color, #5288c1);
  --bubble-out-text: var(--tg-theme-button-text-color, #fff);
  --accent: var(--tg-theme-accent-text-color, #6ab3f3);
  --danger: #e85d5d;
  --topbar-h: 52px;
  --composer-h: 56px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.loader {
  position: fixed;
  inset: 0;
  background: rgba(14, 22, 33, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.loader.visible {
  opacity: 1;
  pointer-events: auto;
}

.spinner {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.18);
  border-top-color: var(--link);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.hidden {
  display: none !important;
}

.topbar {
  flex: 0 0 var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  background: var(--secondary);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-title {
  font-weight: 600;
  font-size: 17px;
  flex: 1;
  text-align: center;
  margin-right: 28px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--link);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}

.main {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.section-label {
  padding: 10px 14px 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 1;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-pill {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 0 3px rgba(255,255,255,0.06);
}

.section-chevron {
  margin-left: auto;
  color: var(--hint);
  font-size: 14px;
  transform: rotate(0deg);
  transition: transform 180ms ease;
}

.city-wrap {
  border-left: 3px solid rgba(255,255,255,0.06);
  margin-bottom: 6px;
  background: rgba(255,255,255,0.01);
}

.city-wrap.collapsed .section-chevron {
  transform: rotate(-90deg);
}

.city-body {
  overflow: hidden;
  max-height: 2400px;
  opacity: 1;
  transform: translateY(0);
  transition: max-height 220ms ease, opacity 220ms ease, transform 220ms ease;
}

.city-wrap.collapsed .city-body {
  max-height: 0;
  opacity: 0;
  transform: translateY(-4px);
}

.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  cursor: pointer;
  transition: background 160ms ease, transform 120ms ease;
}

.chat-row:active {
  background: rgba(255,255,255,0.04);
  transform: scale(0.995);
}

.chat-row.unread {
  background: rgba(106, 179, 243, 0.08);
}

.chat-row.unread:active {
  background: rgba(106, 179, 243, 0.12);
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(145deg, #3a6ea5, #1f3f63);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 15px;
  color: #fff;
  flex-shrink: 0;
}

.chat-meta {
  flex: 1;
  min-width: 0;
}

.chat-title {
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-preview {
  font-size: 14px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-side {
  text-align: right;
  flex-shrink: 0;
}

.chat-time {
  font-size: 12px;
  color: var(--hint);
}

.badge {
  display: inline-block;
  min-width: 20px;
  padding: 2px 6px;
  margin-top: 4px;
  border-radius: 10px;
  background: var(--link);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

.messages {
  padding: 12px 10px 80px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.load-more {
  text-align: center;
  padding: 8px;
}

.load-more button {
  background: transparent;
  border: none;
  color: var(--link);
  font-size: 14px;
  cursor: pointer;
}

.msg-row {
  display: flex;
  width: 100%;
}

.msg-row.in {
  justify-content: flex-start;
}

.msg-row.out {
  justify-content: flex-end;
}

.bubble {
  max-width: 82%;
  padding: 8px 11px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.35;
  word-wrap: break-word;
}

.bubble.in {
  background: var(--bubble-in);
  border-bottom-left-radius: 4px;
}

.bubble.out {
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}

.bubble .who {
  font-size: 12px;
  opacity: 0.85;
  margin-bottom: 4px;
}

.bubble .ts {
  font-size: 11px;
  opacity: 0.65;
  margin-top: 4px;
  text-align: right;
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: var(--secondary);
  border-top: 1px solid rgba(255,255,255,0.06);
  position: sticky;
  bottom: 0;
  flex-wrap: wrap;
}

.attach-preview {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.08);
  transform: translateY(0);
  opacity: 1;
  transition: opacity 160ms ease, transform 160ms ease;
}

.attach-preview.hidden {
  display: none !important;
}

.attach-preview__thumb {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: rgba(255,255,255,0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

.attach-preview__thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.attach-preview__meta {
  flex: 1;
  min-width: 0;
}

.attach-preview__name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attach-preview__hint {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.attach-preview__close {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.10);
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 160ms ease, transform 120ms ease;
}

.attach-preview__close:active {
  background: rgba(255,255,255,0.16);
  transform: scale(0.98);
}

#msg-input {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 10px 12px;
  border-radius: 20px;
  border: 1px solid rgba(255,255,255,0.1);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--link);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

.attach-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.08);
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  flex-shrink: 0;
}

.bubble a {
  color: var(--link);
  text-decoration: none;
}

.bubble a:hover {
  text-decoration: underline;
}

.media {
  margin-top: 6px;
}

.media img {
  max-width: 100%;
  border-radius: 12px;
  display: block;
  cursor: pointer;
}

.media video {
  width: 100%;
  border-radius: 12px;
  display: block;
}

.media-viewer {
  position: fixed;
  inset: 0;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.media-viewer.visible {
  opacity: 1;
  pointer-events: auto;
}

.media-viewer__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.72);
}

.media-viewer__content {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.media-viewer__stage {
  max-width: min(980px, 100%);
  max-height: min(92vh, 100%);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.media-viewer__stage img,
.media-viewer__stage video {
  max-width: 100%;
  max-height: 92vh;
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.45);
}

.media-viewer__close {
  position: absolute;
  top: 12px;
  right: 14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  transition: background 160ms ease;
}

.media-viewer__close:active {
  background: rgba(255,255,255,0.18);
}

/* City colors (harmonious, minimal) */
.city-wrap[data-city="Аренда квартир на Урале"] { border-left-color: rgba(124, 183, 255, 0.55); }
.city-wrap[data-city="Аренда квартир на Неве"]  { border-left-color: rgba(125, 224, 176, 0.55); }
.city-wrap[data-city="Аренда квартир Москвы"]   { border-left-color: rgba(255, 196, 120, 0.55); }
.city-wrap[data-city="Формула жизни"]           { border-left-color: rgba(211, 155, 255, 0.55); }

.city-wrap[data-city="Аренда квартир на Урале"] .section-pill { background: rgba(124, 183, 255, 0.9); }
.city-wrap[data-city="Аренда квартир на Неве"]  .section-pill { background: rgba(125, 224, 176, 0.9); }
.city-wrap[data-city="Аренда квартир Москвы"]   .section-pill { background: rgba(255, 196, 120, 0.9); }
.city-wrap[data-city="Формула жизни"]           .section-pill { background: rgba(211, 155, 255, 0.9); }

.file-pill {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255,255,255,0.08);
  color: inherit;
  text-decoration: none;
}

.file-pill .file-ico {
  font-size: 16px;
  opacity: 0.9;
}

.file-pill .file-name {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}

.error-banner {
  margin: 12px;
  padding: 12px;
  border-radius: 12px;
  background: rgba(232, 93, 93, 0.15);
  color: var(--danger);
  font-size: 14px;
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--hint);
  font-size: 15px;
}
