:root {
  --primary: #B388FF;
  --primary-dark: #7C4DFF;
  --on-primary: #1A1A1A;
  --bg: #121212;
  --surface: #1E1E1E;
  --surface-variant: #2A2A2A;
  --text: #FFFFFF;
  --text-dim: #B0B0B0;
  --bubble-in: #2A2A2A;
  --bubble-out: #B388FF;
  --bubble-out-text: #1A1A1A;
  --danger: #EF5350;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}
.screen.active { display: flex; }

/* LOGO / LOADING */
#screen-loading { align-items: center; justify-content: center; gap: 24px; }
.logo {
  font-size: 64px;
  font-weight: bold;
  color: var(--primary);
  text-align: center;
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--surface-variant);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* AUTH */
.auth-box {
  margin: auto;
  padding: 32px 24px;
  width: 100%;
  max-width: 400px;
  text-align: center;
}
.auth-box h1 { font-size: 28px; margin-bottom: 4px; }
.auth-box .subtitle { color: var(--text-dim); margin-bottom: 24px; }
.auth-box input {
  width: 100%;
  padding: 14px 16px;
  margin-bottom: 12px;
  background: var(--surface-variant);
  border: 2px solid transparent;
  border-radius: 8px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.auth-box input:focus { border-color: var(--primary); }
.btn {
  width: 100%;
  padding: 14px;
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: 24px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  margin-top: 8px;
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-outline {
  width: 100%;
  padding: 14px;
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
  border-radius: 24px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  margin-top: 8px;
}
.link {
  display: block;
  margin-top: 16px;
  color: var(--primary);
  text-decoration: none;
  cursor: pointer;
  font-weight: bold;
}
.error {
  margin-top: 12px;
  color: var(--danger);
  min-height: 20px;
  font-size: 14px;
}

/* TOPBAR */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--primary);
  color: var(--on-primary);
  min-height: 56px;
  box-shadow: 0 2px 4px rgba(0,0,0,.2);
  z-index: 10;
}
.topbar-title {
  font-size: 18px;
  font-weight: bold;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar-actions { display: flex; gap: 8px; }
.topbar button {
  background: transparent;
  border: none;
  color: var(--on-primary);       
  cursor: pointer;
  padding: 0;
  width: 40px;
  height: 40px;
  display: inline-flex;            
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.15s ease;
  font-size: 22px;                 
  line-height: 1;
}

.topbar button:hover { background: rgba(0, 0, 0, 0.08); }
.topbar button:active { background: rgba(0, 0, 0, 0.16); }

.topbar .icon {
  width: 22px;
  height: 22px;
  display: block;
  stroke: currentColor;
  pointer-events: none;
}
.topbar .back { font-size: 24px; padding: 4px 10px; }
.topbar .avatar.sm {
  width: 36px; height: 36px;
  border-radius: 50%;
  object-fit: cover;
}
.search-input {
  flex: 1;
  padding: 8px 12px;
  background: rgba(255,255,255,.15);
  border: none;
  border-radius: 8px;
  color: var(--on-primary);
  outline: none;
  font-size: 15px;
}
.search-input::placeholder { color: rgba(0,0,0,.5); }

/* LISTS */
.dialogs-list {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 24px;
}
.dialog-item {
  display: flex;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,.05);
}
.dialog-item:hover { background: var(--surface-variant); }
.avatar {
  width: 56px; height: 56px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-variant);
  flex-shrink: 0;
}
.avatar.lg { width: 120px; height: 120px; }
.dialog-info { flex: 1; min-width: 0; }
.dialog-name {
  font-weight: bold;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dialog-msg {
  color: var(--text-dim);
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
}
.empty {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  color: var(--text-dim);
  text-align: center;
  display: none;
}
.empty.visible { display: block; }

/* MESSAGES */
.messages-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-row {
  display: flex;
  gap: 8px;
  max-width: 100%;
  align-items: flex-end;
}
.msg-row.mine { justify-content: flex-end; }
.msg-row .avatar.xs {
  width: 32px; height: 32px;
  border-radius: 50%;
}
.msg-bubble {
  max-width: 70%;
  padding: 8px 12px 6px;
  border-radius: 16px;
  position: relative;
  word-wrap: break-word;
  font-size: 15px;
  line-height: 1.35;
}
.msg-row:not(.mine) .msg-bubble {
  background: var(--bubble-in);
  border-bottom-left-radius: 4px;
}
.msg-row.mine .msg-bubble {
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}
.msg-sender {
  font-size: 12px;
  font-weight: bold;
  color: var(--primary);
  margin-bottom: 2px;
}
.msg-time {
  font-size: 11px;
  opacity: .65;
  margin-left: 8px;
  float: right;
  margin-top: 4px;
}
.msg-image {
  max-width: 100%;
  max-height: 260px;
  border-radius: 12px;
  margin-bottom: 4px;
  display: block;
}
.date-sep {
  text-align: center;
  margin: 12px 0;
}
.date-sep span {
  background: var(--surface-variant);
  color: var(--text-dim);
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
}

/* INPUT BAR */
.input-bar {
  display: flex;
  gap: 8px;
  padding: 8px;
  background: var(--surface-variant);
  align-items: center;
}
.input-bar input[type="text"] {
  flex: 1;
  padding: 12px 16px;
  background: var(--surface);
  border: none;
  border-radius: 20px;
  color: var(--text);
  outline: none;
  font-size: 15px;
}
.input-bar button {
  width: 44px; height: 44px;
  border: none;
  background: transparent;
  color: var(--primary);
  font-size: 20px;
  cursor: pointer;
  border-radius: 50%;
}

/* PROFILE */
.profile-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 24px 32px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.profile-content .avatar.lg { align-self: center; margin-bottom: 8px; }
.profile-content label {
  font-size: 14px;
  color: var(--text-dim);
  margin-top: 8px;
}
.profile-content input,
.profile-content textarea {
  width: 100%;
  padding: 12px 16px;
  background: var(--surface-variant);
  border: 2px solid transparent;
  border-radius: 8px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  font-family: inherit;
}
.profile-content input:focus,
.profile-content textarea:focus { border-color: var(--primary); }
.colors-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;   /* ← центрируем кружки */
  gap: 8px;
  margin-top: 8px;
}
.color-cell {
  aspect-ratio: 1;
  border-radius: 50%;
  cursor: pointer;
  border: 3px solid transparent;
}
.color-cell.selected { border-color: var(--text); }

/* CHIPS (create group) */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--surface-variant);
  border-radius: 12px;
  font-size: 14px;
}
.chip button {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
}

/* TOAST */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(200%);
  background: var(--surface);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  transition: transform .25s;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,.4);
  max-width: 80vw;
}
.toast.visible { transform: translateX(-50%) translateY(0); }

/* MOBILE */
@media (max-width: 600px) {
  .auth-box { padding: 24px 16px; }
  .logo { font-size: 48px; }
  .msg-bubble { max-width: 80%; }
}

/* =============================================== */
/* ����: �������                                                        */
/* =============================================== */
body.light-theme {
  --bg: #FFFFFF;
  --surface: #F5F5F5;
  --surface-variant: #EEEEEE;
  --text: #121212;
  --text-dim: #666666;
  --bubble-in: #EEEEEE;
  --bubble-out: #B388FF;
  --bubble-out-text: #1A1A1A;
  --on-primary: #1A1A1A;
}

body.light-theme .msg-bubble {
  color: #121212;
}
body.light-theme .msg-row.mine .msg-bubble {
  color: #1A1A1A;
}

/* =============================================== */
/* ���������� ����                                                     */
/* =============================================== */
.popup-menu {
  position: fixed;
  min-width: 200px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,.4);
  padding: 6px;
  z-index: 2000;
  display: none;
  animation: popupFadeIn 0.15s ease-out;
}
.popup-menu.visible { display: block; }

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

.popup-menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
  color: var(--text);
  user-select: none;
}
.popup-menu-item:hover { background: var(--surface-variant); }
.popup-menu-item:active { background: var(--surface-variant); }
.popup-menu-item svg {
  width: 20px; height: 20px;
  flex-shrink: 0;
  stroke: currentColor;
  fill: none;
}

/* =============================================== */
/* ������ (������� ����������� � ��.)                                 */
/* =============================================== */
.badge-container {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin-left: 4px;
  vertical-align: middle;
}
.badge-icon {
  width: 16px;
  height: 16px;
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
}
.badge-icon.lg { width: 20px; height: 20px; }

/* --- ������� ����� (�������� ������ �������) --- */
.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8px 16px 4px;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
}
.profile-header .avatar.lg {
  margin-bottom: 12px;
}
.profile-header .display-name-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 22px;
  font-weight: bold;
  text-align: center;
  width: 100%;
}
.profile-header .username-line {
  color: var(--text-dim);
  font-size: 15px;
  margin-top: 4px;
  text-align: center;
  width: 100%;
}
.profile-header .bio-line {
  margin-top: 12px;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.4;
  word-break: break-word;
}

/* Цветной блок ПОД шапкой */
.profile-color-header {
  position: relative;
  width: 100%;
  height: 140px;
  background: linear-gradient(135deg, #B388FF 0%, #7C4DFF 100%);
  transition: background 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Аватарка внутри цветного блока */
.profile-color-header .avatar.lg {
  width: 120px;
  height: 120px;
  border: 4px solid var(--bg);
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
  margin-bottom: -60px;
  position: relative;
  z-index: 2;
}

/* Отступ для контента, чтобы аватарка не перекрывала кнопки */
.profile-content {
  padding-top: 40px;             /* место под вылезающую аватарку */
}

/* Сетка цветов */
.colors-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 8px;
  margin-top: 8px;
}

.color-cell {
  aspect-ratio: 1;
  border-radius: 50%;
  cursor: pointer;
  border: 3px solid transparent;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.color-cell:hover { transform: scale(1.08); }
.color-cell.selected {
  border-color: var(--text);
  transform: scale(1.1);
}

.profile-header .bio-line {
  margin-top: 12px;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  max-width: 400px;
  line-height: 1.4;
}

.profile-content textarea {
  width: 100% !important;
  min-height: 100px;
  box-sizing: border-box;
  align-self: stretch;
  resize: vertical;
  font-size: 16px;
  font-family: inherit;
  line-height: 1.5;
}

.registered-at-value {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 4px;
  margin-bottom: 8px;
}

/* ═══════════════════════════════════════════════ */
/* НАСТРОЙКИ                                        */
/* ═══════════════════════════════════════════════ */
.settings-profile-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  margin-bottom: 16px;
  background: var(--surface-variant);
  border-radius: 16px;
  cursor: pointer;
  transition: background 0.15s ease;
}
.settings-profile-card:hover { background: rgba(255,255,255,.08); }
.settings-profile-info { flex: 1; min-width: 0; }
.settings-profile-name {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 17px;
  font-weight: bold;
  color: var(--text);
}
.settings-profile-username {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
}
.settings-profile-registered-at {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.settings-section-header {
  font-size: 13px;
  font-weight: bold;
  color: var(--primary);
  margin: 16px 8px 6px;
  text-transform: none;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 12px;
  cursor: pointer;
  border-radius: 10px;
  transition: background 0.15s ease;
  user-select: none;
}
.settings-row:hover { background: var(--surface-variant); }

.settings-row-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  color: var(--text-dim);
}
.settings-row-label {
  flex: 1;
  font-size: 16px;
  color: var(--text);
}
.settings-row-label.danger { color: var(--danger); }
.settings-row-label-col { flex: 1; min-width: 0; }
.settings-row-sub {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
}

.chevron {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--text-dim);
}

/* Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.switch .slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--surface);
  border-radius: 24px;
  transition: background 0.2s;
}
.switch .slider::before {
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
}
.switch input:checked + .slider {
  background: var(--primary);
}
.switch input:checked + .slider::before {
  transform: translateX(20px);
}

/* ═══════════════════════════════════════════════ */
/* MESSAPP+                                         */
/* ═══════════════════════════════════════════════ */
.plus-header-title {
  font-size: 22px;
  font-weight: bold;
  color: var(--text);
  margin-bottom: 4px;
}
.plus-header-sub {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 20px;
}
.plus-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  margin-bottom: 12px;
  background: var(--surface-variant);
  border-radius: 16px;
  cursor: pointer;
  transition: background 0.15s ease;
}
.plus-card:hover { background: rgba(255,255,255,.08); }
.plus-card-icon {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
}
.plus-card-info { flex: 1; min-width: 0; }
.plus-card-title {
  font-size: 17px;
  font-weight: bold;
  color: var(--text);
}
.plus-card-sub {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
}

/* Светлая тема — переопределения */
body.light-theme .settings-profile-card { background: #EEEEEE; }
body.light-theme .settings-profile-card:hover { background: #E0E0E0; }
body.light-theme .settings-row:hover { background: #EEEEEE; }
body.light-theme .plus-card { background: #EEEEEE; }
body.light-theme .plus-card:hover { background: #E0E0E0; }
body.light-theme .switch .slider { background: #CFCFCF; }