:root {
  /* Минималистичный премиальный светлый стиль iOS (Apple & Telegram Light) */
  --bg-main: #f6f8fa;
  --bg-surface: #ffffff;
  --bg-card: #ffffff;
  --bg-card-hover: #f1f5f9;
  --bg-glass: rgba(255, 255, 255, 0.94);

  --border-subtle: rgba(0, 0, 0, 0.07);
  --border-focus: rgba(139, 92, 246, 0.45);

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  /* Акцентный цвет - пастельный лавандово-синий из аватара */
  --accent-blue: #6366f1;
  --accent-blue-soft: rgba(99, 102, 241, 0.1);
  --accent-gradient: linear-gradient(135deg, #8b5cf6 0%, #38bdf8 100%);

  /* Мягкие iOS цвета статусов */
  --status-del: #e11d48;
  --status-del-bg: rgba(225, 29, 72, 0.08);
  --status-del-border: rgba(225, 29, 72, 0.2);

  --status-edit: #d97706;
  --status-edit-bg: rgba(217, 119, 6, 0.08);
  --status-edit-border: rgba(217, 119, 6, 0.2);

  --status-media: #0284c7;
  --status-media-bg: rgba(2, 132, 199, 0.08);
  --status-media-border: rgba(2, 132, 199, 0.2);

  /* Бабблы сообщений (iOS Light Style) */
  --bubble-incoming: #ffffff;
  --bubble-incoming-border: rgba(0, 0, 0, 0.06);
  --bubble-outgoing: #e8f2fe;
  --bubble-outgoing-border: rgba(99, 102, 241, 0.18);

  --radius-xs: 6px;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --radius-pill: 9999px;

  --font-sans: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Telegram theme adaptive */
body.theme-tg {
  --bg-main: var(--tg-theme-bg-color, #f6f8fa);
  --bg-surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --text-primary: var(--tg-theme-text-color, #0f172a);
  --text-secondary: var(--tg-theme-hint-color, #64748b);
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--bg-main);
  color: var(--text-primary);
  line-height: 1.45;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-font-smoothing: antialiased;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 540px;
  margin: 0 auto;
  position: relative;
  background-color: var(--bg-main);
  overflow: hidden;
}

/* =====================================================================
   HEADER & STATS STRIP
   ===================================================================== */

.btn-back {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border-radius: var(--radius-xs);
  transition: color 0.15s, background-color 0.15s;
}

.btn-back:active {
  color: var(--text-primary);
  background-color: rgba(0, 0, 0, 0.05);
}

/* Эстетичный iOS виджет аналитики и статистики архива (Apple Card Dashboard) */
.stats-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  align-items: center;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 8px 4px;
  margin-bottom: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.stat-pill {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
  padding: 3px 2px;
  border-radius: var(--radius-sm);
  transition: background-color 0.15s ease, transform 0.1s ease;
  cursor: default;
}

.stat-pill:not(:last-child)::after {
  content: '';
  position: absolute;
  right: 0;
  top: 15%;
  height: 70%;
  width: 1px;
  background: rgba(0, 0, 0, 0.06);
  pointer-events: none;
}

.stat-val-wrap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.stat-val {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.15;
  letter-spacing: -0.2px;
  font-variant-numeric: tabular-nums;
}

.stat-lbl {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: -0.1px;
  white-space: nowrap;
}

.stat-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.dot-total {
  background: linear-gradient(135deg, #8b5cf6, #38bdf8);
}

.dot-del {
  background: var(--status-del);
  box-shadow: 0 0 5px rgba(225, 29, 72, 0.4);
}

.dot-edit {
  background: var(--status-edit);
  box-shadow: 0 0 5px rgba(217, 119, 6, 0.4);
}

.dot-media {
  background: var(--status-media);
  box-shadow: 0 0 5px rgba(2, 132, 199, 0.4);
}

@media (hover: hover) {
  .stat-pill:hover {
    background-color: rgba(0, 0, 0, 0.02);
  }
}

/* =====================================================================
   MAIN CONTENT & VIEWS
   ===================================================================== */

.main-content {
  flex: 1;
  min-height: 0;
  overflow: hidden !important;
  position: relative;
  display: flex !important;
  flex-direction: column !important;
  height: 100% !important;
  max-height: 100% !important;
  padding-bottom: 0 !important;
}

/* Скрываем общую полоску статистики внутри переписки */
body.in-chat #stats-strip,
.app-container:has(#view-chat-detail.active) #stats-strip {
  display: none !important;
}

.view-panel {
  display: none;
  padding: 0;
  height: 100%;
  max-height: 100%;
  min-height: 0;
  overflow: hidden;
  flex: 1;
}

/* Панель диалогов (Главный экран): строгий flex-контейнер, скроллится только список чатов */
#view-chats.active {
  display: flex !important;
  flex-direction: column !important;
  height: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
  padding: 10px 12px 0 12px !important;
  flex: 1 !important;
  min-height: 0 !important;
}

/* Панель диалога: строгий flex-контейнер на всю доступную высоту */
#view-chat-detail.active {
  display: flex !important;
  flex-direction: column !important;
  height: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
  padding: 0 !important;
  flex: 1 !important;
  min-height: 0 !important;
}

/* Поисковая строка на главном экране (зафиксирована вверху) */
.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  transition: border-color 0.2s, box-shadow 0.2s;
  flex-shrink: 0;
  touch-action: manipulation;
}

.search-wrap:focus-within {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
}

.search-icon {
  color: var(--text-muted);
  flex-shrink: 0;
}

.search-input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 13.5px;
  width: 100%;
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
}

.search-input::placeholder {
  color: var(--text-muted);
}

/* =====================================================================
   CHATS LIST (ТОЛЬКО СПИСОК ЧАТОВ СКРОЛЛИТСЯ)
   ===================================================================== */

.chats-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  overscroll-behavior-x: none;
  touch-action: pan-y;
  padding-bottom: calc(16px + var(--safe-bottom));
}

.chats-list::-webkit-scrollbar {
  width: 4px;
}

.chats-list::-webkit-scrollbar-track {
  background: transparent;
}

.chats-list::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.14);
  border-radius: 4px;
}

.chats-list::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.25);
}

.chat-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.chat-card:active {
  background-color: var(--bg-card-hover);
  transform: scale(0.995);
}

.chat-avatar-wrap {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
}

.chat-avatar-circle {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(135deg, #ede9fe 0%, #e0f2fe 100%);
  color: #6366f1;
  border: 1px solid rgba(139, 92, 246, 0.16);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 700;
  flex-shrink: 0;
  user-select: none;
}

.chat-avatar-img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 2;
}

.chat-avatar-img.loaded {
  opacity: 1;
}

/* Аватарка собеседника в шапке переписки */
.detail-avatar-wrap {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
  margin-right: 2px;
}

.detail-avatar-wrap .chat-avatar-circle {
  font-size: 14.5px;
}

.chat-info {
  flex: 1;
  min-width: 0;
}

.chat-top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.chat-name {
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-time {
  font-size: 11.5px;
  color: var(--text-muted);
  flex-shrink: 0;
  margin-left: 6px;
}

.chat-bottom-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.chat-snippet {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.chat-del-indicator {
  font-size: 10px;
  font-weight: 600;
  color: var(--status-del);
  background: var(--status-del-bg);
  border: 1px solid var(--status-del-border);
  padding: 1px 6px;
  border-radius: var(--radius-pill);
  flex-shrink: 0;
}

/* =====================================================================
   CHAT DETAIL & IN-CHAT SEARCH & STICKY FILTERS
   ===================================================================== */

.detail-header-sticky {
  flex-shrink: 0;
  position: relative;
  top: 0;
  margin: 0;
  padding: 10px 14px 8px 14px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 30;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  gap: 8px;
  touch-action: none;
}

.detail-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.detail-user-meta {
  min-width: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.detail-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: none;
  -webkit-user-select: none;
}

.detail-sub {
  font-size: 11.5px;
  color: var(--text-muted);
  user-select: none;
  -webkit-user-select: none;
}

.detail-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  background: #f1f5f9;
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  touch-action: manipulation;
  pointer-events: auto;
}

.icon-btn:active, .icon-btn.active {
  background: #e2e8f0;
  color: var(--text-primary);
  border-color: rgba(0, 0, 0, 0.12);
}

/* Внутричасовая строка поиска */
.chat-search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f8fafc;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  margin-bottom: 8px;
  animation: slideDown 0.18s ease-out;
}

.chat-search-bar .search-input {
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
  pointer-events: auto;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.clear-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  touch-action: manipulation;
  pointer-events: auto;
}

.clear-btn:active {
  color: var(--text-primary);
}

/* Закрепленные фильтры-таблетки переписки */
.filter-strip {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
  touch-action: pan-x;
  overscroll-behavior-x: contain;
  overscroll-behavior-y: none;
  -webkit-overflow-scrolling: touch;
}

.filter-strip::-webkit-scrollbar {
  display: none;
}

.filter-pill {
  background: #ffffff;
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  padding: 5px 12px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  transition: all 0.15s ease;
  touch-action: manipulation;
  flex-shrink: 0;
  pointer-events: auto;
}

.filter-pill:active {
  transform: scale(0.96);
}

.filter-pill.active {
  background: #0f172a;
  color: #ffffff;
  border-color: #0f172a;
  font-weight: 600;
}

.filter-pill.pill-del.active {
  background: var(--status-del-bg);
  color: var(--status-del);
  border-color: var(--status-del-border);
}

.filter-pill.pill-edit.active {
  background: var(--status-edit-bg);
  color: var(--status-edit);
  border-color: var(--status-edit-border);
}

.filter-pill.pill-media.active {
  background: var(--status-media-bg);
  color: var(--status-media);
  border-color: var(--status-media-border);
}

/* =====================================================================
   MESSAGES CONTAINER & BUBBLES
   ===================================================================== */

.messages-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  overscroll-behavior-x: none;
  touch-action: pan-y;
  padding: 12px 14px calc(16px + var(--safe-bottom)) 14px;
}

.messages-container::-webkit-scrollbar {
  width: 4px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.14);
  border-radius: 4px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.25);
}

/* Telegram-style разделитель дат между сообщениями (Сегодня, Вчера, 6 сентября) */
.chat-date-separator {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin: 10px 0 6px 0;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.chat-date-pill {
  background: rgba(15, 23, 42, 0.46);
  color: #ffffff;
  font-size: 11.5px;
  font-weight: 500;
  padding: 3px 12px;
  border-radius: var(--radius-pill);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  letter-spacing: -0.1px;
  display: inline-block;
}

.message-bubble {
  max-width: 84%;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  word-break: break-word;
  position: relative;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}

.bubble-incoming {
  align-self: flex-start;
  background: var(--bubble-incoming);
  border: 1px solid var(--bubble-incoming-border);
  border-bottom-left-radius: 4px;
}

.bubble-outgoing {
  align-self: flex-end;
  background: var(--bubble-outgoing);
  border: 1px solid var(--bubble-outgoing-border);
  border-bottom-right-radius: 4px;
}

.bubble-sender {
  font-size: 11px;
  font-weight: 600;
  color: #6366f1;
  margin-bottom: 2px;
}

.bubble-text {
  color: var(--text-primary);
  line-height: 1.4;
}

.bubble-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 3px;
  font-size: 10px;
  color: var(--text-muted);
}

/* Бейджи удалений и изменений внутри сообщений */
.deleted-alert-badge {
  display: inline-flex;
  align-items: center;
  font-size: 10px;
  font-weight: 600;
  color: var(--status-del);
  background: var(--status-del-bg);
  border: 1px solid var(--status-del-border);
  padding: 1.5px 6px;
  border-radius: var(--radius-xs);
  margin-bottom: 4px;
}

.edited-alert-badge {
  display: inline-flex;
  align-items: center;
  font-size: 10px;
  font-weight: 600;
  color: var(--status-edit);
  background: var(--status-edit-bg);
  border: 1px solid var(--status-edit-border);
  padding: 1.5px 6px;
  border-radius: var(--radius-xs);
  margin-bottom: 4px;
  cursor: pointer;
}

.edit-diff-box {
  background: #fffbeb;
  border: 1px dashed rgba(245, 158, 11, 0.4);
  border-radius: var(--radius-xs);
  padding: 6px 8px;
  margin-top: 4px;
  font-size: 12px;
}

.edit-diff-old {
  color: #e11d48;
  text-decoration: line-through;
  margin-bottom: 3px;
}

.edit-diff-new {
  color: #16a34a;
}

/* Медиа внутри бабблов */
.bubble-media-img {
  max-width: 100%;
  border-radius: var(--radius-sm);
  display: block;
  margin-bottom: 4px;
  cursor: pointer;
  max-height: 280px;
  object-fit: cover;
}

.bubble-video-note {
  width: 180px;
  height: 180px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  margin-bottom: 4px;
}

.bubble-voice-player {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}

.bubble-sticker-img {
  max-width: 150px;
  max-height: 150px;
  object-fit: contain;
  display: block;
  margin: 2px 0;
}

.bubble-sticker-emoji {
  font-size: 38px;
  line-height: 1.15;
  display: inline-block;
  padding: 2px 4px;
}

/* =====================================================================
   EMPTY STATES & SKELETON LOADERS
   ===================================================================== */

.empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.skeleton-loader {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.skeleton-chat {
  height: 56px;
  background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-md);
}

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

/* =====================================================================
   MODALS (LIGHTBOX & PAYWALL)
   ===================================================================== */

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-card {
  position: relative;
  max-width: 100%;
  max-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-card img, .modal-card video {
  max-width: 95vw;
  max-height: 85vh;
  border-radius: var(--radius-sm);
  object-fit: contain;
}

.modal-close-btn {
  position: absolute;
  top: -36px;
  right: 0;
  background: transparent;
  border: none;
  color: white;
  font-size: 28px;
  cursor: pointer;
  line-height: 1;
}

.paywall-sheet {
  background: #ffffff;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
  padding: 24px;
  max-width: 360px;
  text-align: center;
}

.paywall-icon-wrap {
  margin-bottom: 12px;
}

.paywall-icon {
  font-size: 38px;
}

.paywall-sheet h2 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.paywall-sheet p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.45;
  margin-bottom: 18px;
}

.btn-submit {
  width: 100%;
  background: linear-gradient(135deg, #8b5cf6 0%, #38bdf8 100%);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(139, 92, 246, 0.25);
  transition: opacity 0.15s, transform 0.1s;
}

.btn-submit:active {
  opacity: 0.9;
  transform: scale(0.99);
}

.hidden {
  display: none !important;
}
