/* ============================================================
   MAX web client — design system
   Minimalist two-panel messenger. Identity: Golos Text UI,
   JetBrains Mono for data, a blue→violet "spark" gradient.
   ============================================================ */

:root {
  color-scheme: dark;

  /* color — dark is the default/priority theme */
  --bg:        #0f1115;
  --surface:   #171a21;
  --surface-2: #1e222b;
  --surface-3: #272c37;
  --border:    #262b35;
  --border-2:  #2f3542;
  --ink:       #e8eaf0;
  --ink-soft:  #c2c6d2;
  --muted:     #8a90a0;
  --faint:     #5f6676;
  --danger:    #ff6b6f;

  /* the spark — MAX blue→violet (shared across themes) */
  --accent:    #6457f7;
  --accent-ink:#a99bff;
  --grad:      linear-gradient(135deg, #5b6cf6 0%, #9b5bf6 100%);
  --grad-soft: linear-gradient(135deg, rgba(91,108,246,.20) 0%, rgba(155,91,246,.20) 100%);

  /* type */
  --font-ui:   "Golos Text", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

  /* shape */
  --r-sm: 8px;
  --r:    12px;
  --r-lg: 18px;
  --r-pill: 999px;

  /* depth */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .40);
  --shadow:    0 10px 34px rgba(0, 0, 0, .50);
  --shadow-lg: 0 28px 80px rgba(0, 0, 0, .62);

  --nav: 80px;     /* far-left vertical navigation rail */
  --rail: 322px;   /* chat-list panel */
  --tabbar: 58px;  /* mobile bottom tab-bar height (excl. safe-area) */
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;          /* panes scroll internally, never the page */
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-size-adjust: 100%;       /* don't let mobile browsers inflate text */
  -webkit-text-size-adjust: 100%;
}

/* App shell height: dynamic-viewport (dvh) so the layout follows the mobile
   URL bar / keyboard collapse, with a JS-set --vh fallback for browsers that
   lack dvh, and the static 100% as the last resort. */
#app, .screen {
  height: 100%;
  height: calc(var(--vh, 1vh) * 100);
  height: 100dvh;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

::selection { background: rgba(100, 87, 247, .18); }

/* ---------------- brand ---------------- */
.brand { display: flex; align-items: center; }
.brand-mark {
  font-weight: 700;
  letter-spacing: -.01em;
  font-size: 19px;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand-lg .brand-mark { font-size: 32px; }

/* ---------------- buttons & fields ---------------- */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--r);
  padding: 11px 18px;
  cursor: pointer;
  transition: transform .06s ease, background .15s ease, box-shadow .15s ease, opacity .15s;
}
.btn:active { transform: translateY(1px); }
.btn-block { width: 100%; }
.btn-primary { background: var(--grad); color: #fff; box-shadow: 0 6px 18px rgba(100, 87, 247, .28); }
.btn-primary:hover { box-shadow: 0 8px 22px rgba(100, 87, 247, .38); }
.btn-ghost { background: var(--surface-2); color: var(--ink-soft); }
.btn-ghost:hover { background: var(--surface-3); }
.btn-danger { background: transparent; color: var(--danger); border-color: transparent; }
.btn-danger:hover { background: rgba(229, 72, 77, .08); }
.btn:disabled { opacity: .5; cursor: default; }

.field {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: var(--r);
  padding: 12px 14px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field::placeholder { color: var(--faint); }
.field:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(100, 87, 247, .14);
}
.field.mono { font-family: var(--font-mono); letter-spacing: .02em; }
.field-code { text-align: center; font-size: 22px; letter-spacing: .5em; padding-left: .5em; }
.field-label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 2px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px; height: 38px;
  border: none;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.icon-sm { width: 32px; height: 32px; }
.icon-btn.active { color: var(--accent); background: rgba(100, 87, 247, .12); }
/* primary "create" action — violet gradient */
.add-btn { background: var(--grad); color: #fff; box-shadow: 0 4px 12px rgba(100, 87, 247, .35); }
.add-btn:hover { background: var(--grad); color: #fff; filter: brightness(1.08); }
.add-btn .ic { stroke-width: 2.4; }
.ic {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.error {
  color: var(--danger);
  background: rgba(229, 72, 77, .08);
  border-radius: var(--r-sm);
  padding: 10px 12px;
  font-size: 14px;
}
.error a { color: var(--danger); font-weight: 600; }
.status { min-height: 18px; font-size: 13px; margin-top: 6px; }

/* ============================================================
   Auth screens
   ============================================================ */
.auth {
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}
.auth-aura {
  position: absolute;
  width: 620px; height: 620px;
  border-radius: 50%;
  background: var(--grad);
  filter: blur(120px);
  opacity: .16;
  top: -180px; right: -120px;
  pointer-events: none;
}
.auth-card {
  position: relative;
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 32px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-card h1 { font-size: 23px; font-weight: 700; margin: 8px 0 -4px; letter-spacing: -.01em; }
.auth-card .muted { margin: 0; }
.auth-card .step { display: flex; flex-direction: column; gap: 12px; }
.auth-hint, .step-hint {
  margin: -6px 0 0;
  color: var(--faint);
  font-size: 12.5px;
  line-height: 1.35;
}
.step-hint { margin: -2px 0 0; }

/* ============================================================
   Workspace (two panels)
   ============================================================ */
.workspace {
  display: grid;
  grid-template-columns: var(--nav) var(--rail) 1fr;
  grid-template-rows: 100%;   /* pin the single row to the viewport height */
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}

/* ---------------- sidebar ---------------- */
/* Header + tabs + search stay fixed; only .chat-list scrolls (its own scrollbar
   appears when there are many chats). */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: relative;
}
/* brand left · actions right (settings + logout moved into the Settings modal) */
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  gap: 8px;
}
.head-actions { display: flex; align-items: center; gap: 6px; }
.sidebar-head .brand { padding-left: 4px; }

.sidebar-tools {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 0 12px 10px;
}
.search-field {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 11px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
.search-field:focus-within {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(100, 87, 247, .12);
}
.search-field .ic { width: 17px; height: 17px; flex-shrink: 0; }
.search-field input {
  min-width: 0;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
}
.search-field input::placeholder { color: var(--faint); }
.folders-trigger {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 0 11px;
  background: var(--surface-2);
  color: var(--ink-soft);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.folders-trigger:hover { background: var(--surface-3); color: var(--ink); }
.folders-trigger:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(100, 87, 247, .14); }
.folders-trigger .ic { width: 17px; height: 17px; }

/* folder chips — a swipeable row over the chat list; "Все" + one per folder */
.folder-chips {
  display: flex; gap: 6px;
  padding: 0 12px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.folder-chips::-webkit-scrollbar { display: none; }
.folder-chip {
  flex-shrink: 0;
  font: inherit; font-size: 12.5px; font-weight: 600;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  padding: 5px 13px; cursor: pointer;
  transition: background .12s ease, color .12s ease, border-color .12s ease;
}
.folder-chip:hover { background: var(--surface-3); color: var(--ink-soft); }
.folder-chip.active { color: #fff; background: var(--grad); border-color: transparent; }
.folder-chip:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(100, 87, 247, .2); }

/* ---------------- far-left navigation rail ---------------- */
.nav-rail {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 0;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--border);
  padding: 12px 6px;
}
.rail-top { display: flex; flex-direction: column; gap: 4px; }
.rail-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  width: 100%;
  padding: 9px 2px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: var(--r);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.rail-ic-wrap { display: grid; place-items: center; }
.rail-btn .ic { width: 23px; height: 23px; }
.rail-label {
  font-size: 11px; font-weight: 600; line-height: 1.1;
  text-align: center; letter-spacing: -.01em;
}
.rail-btn:hover { background: var(--surface); color: var(--ink-soft); }
.rail-btn.active { color: var(--accent-ink); }
.rail-btn.active::before {
  content: ""; position: absolute; left: -6px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 26px; border-radius: 0 3px 3px 0; background: var(--grad);
}
.rail-btn.active .ic { color: var(--accent-ink); }
#logout-btn:hover { color: var(--danger); }

/* ---------------- chat list ---------------- */
.chat-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 8px 10px; }

/* persistent low-days key reminder (shown when <= 3 days left / expired) */
.key-reminder {
  display: flex; align-items: center; gap: 10px; width: calc(100% - 16px);
  margin: 0 8px 8px; padding: 9px 11px; text-align: left; cursor: pointer;
  border: 1px solid var(--danger); border-radius: var(--r);
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  color: var(--ink); font: inherit;
}
.key-reminder:hover { background: color-mix(in srgb, var(--danger) 22%, transparent); }
.key-reminder.hidden { display: none; }
.key-reminder-ic { display: grid; place-items: center; width: 30px; height: 30px; flex-shrink: 0; border-radius: var(--r-sm); background: color-mix(in srgb, var(--danger) 22%, transparent); color: var(--danger); }
.key-reminder-ic .ic { width: 17px; height: 17px; }
.key-reminder-body { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.key-reminder-title { font-size: 13.5px; font-weight: 700; color: var(--danger); }
.key-reminder-sub { font-size: 12px; color: var(--muted); }

.list-note { text-align: center; color: var(--muted); padding: 28px 16px; font-size: 14px; }

/* Directional empty state for lists (no chats / no contacts / nothing found):
   a quiet glyph, a title, one line of guidance, and an optional action. */
.state-card {
  display: flex; flex-direction: column; align-items: center;
  gap: 6px; text-align: center;
  padding: 40px 22px; color: var(--muted);
}
.state-icon {
  width: 54px; height: 54px; margin-bottom: 4px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--grad-soft);
  color: var(--accent-ink);
}
.state-icon .ic { width: 24px; height: 24px; stroke-width: 1.7; }
.state-title { font-weight: 600; font-size: 14.5px; color: var(--ink-soft); }
.state-text { margin: 0; font-size: 13px; color: var(--muted); max-width: 230px; }
.state-action {
  margin-top: 8px;
  font: inherit; font-weight: 600; font-size: 13px;
  color: var(--accent-ink);
  background: transparent; border: none; cursor: pointer; padding: 4px;
}
.state-action:hover { text-decoration: underline; }
.state-action:focus-visible { outline: none; text-decoration: underline; }

/* Skeleton loaders — shimmering placeholders shaped like the incoming list rows
   (avatar disc + two text lines). Shown while a list/panel fetch is in flight. */
.sk-list { padding: 6px 4px; }
.sk-row { display: flex; align-items: center; gap: 12px; padding: 9px 10px; }
.sk-lines { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 7px; }
.sk {
  background: var(--surface-2);
  border-radius: var(--r-sm);
  position: relative;
  overflow: hidden;
}
.sk::after {
  content: "";
  position: absolute; inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .05), transparent);
  animation: sk-shimmer 1.3s infinite;
}
.sk-avatar { width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0; }
.sk-line { height: 11px; width: 70%; border-radius: var(--r-pill); }
.sk-line.short { width: 42%; }
@keyframes sk-shimmer { to { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) { .sk::after { animation: none; } }

.chat-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 10px;
  border-radius: var(--r);
  cursor: pointer;
  transition: background .12s ease;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--grad-soft); }
.chat-item.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--grad);
}
.chat-meta { flex: 1; min-width: 0; }
.chat-row { display: flex; align-items: baseline; justify-content: space-between; gap: 6px; }
.chat-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}
.chat-muted-ic {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  align-self: center;
  fill: none;
  stroke: var(--faint);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chat-time { font-family: var(--font-mono); font-size: 11px; color: var(--faint); flex-shrink: 0; }
.chat-last {
  color: var(--muted);
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}
/* tiny photo/video thumbnail before the preview label (Telegram-style) */
.preview-thumb {
  flex-shrink: 0;
  width: 18px; height: 18px;
  border-radius: 4px;
  object-fit: cover;
}
.chat-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.badge {
  flex-shrink: 0;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--r-pill);
  background: var(--grad);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  display: inline-grid;
  place-items: center;
}
.badge.muted { background: var(--faint); }

/* ---------------- avatar ---------------- */
.avatar {
  flex-shrink: 0;
  width: 46px; height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  overflow: hidden;
  user-select: none;
  background: var(--grad); /* fallback; JS sets a name-keyed gradient */
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-xs { width: 22px; height: 22px; font-size: 9px; }
.avatar-sm { width: 38px; height: 38px; font-size: 14px; }
.avatar-md { width: 44px; height: 44px; font-size: 15px; }
.avatar-lg { width: 84px; height: 84px; font-size: 30px; }

/* ============================================================
   Conversation pane
   ============================================================ */
.conversation { min-width: 0; min-height: 0; overflow: hidden; position: relative; display: flex; }
.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}
.empty-badge {
  width: 72px; height: 72px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--grad-soft);
  color: var(--accent-ink);
  margin-bottom: 8px;
}
.empty-badge .ic { width: 30px; height: 30px; stroke-width: 1.6; }
.empty-title { font-weight: 600; font-size: 16px; color: var(--ink-soft); }
.empty-sub { margin: 0; font-size: 13.5px; color: var(--muted); max-width: 260px; }

.conv-inner { position: relative; flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.conv-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
/* peer takes the slack so the subscribe + close buttons sit together at the
   right edge instead of the button floating in the middle of a wide header. */
/* one radius across the header — back-arrow, peer box and subscribe button all
   share var(--r) so nothing reads as more/less rounded than its neighbours. */
.conv-back { flex-shrink: 0; margin-left: -6px; border-radius: var(--r); }
#chat-subscribe-btn { border-radius: var(--r); }
/* peer takes the slack (full-width hover box); the wider .conv-head gap keeps
   that box clear of the back-arrow and subscribe button instead of butting
   against them. No negative horizontal margin, so the box never eats the gap. */
.conv-peer { flex: 1; display: flex; align-items: center; gap: 12px; min-width: 0; }
.conv-peer.clickable {
  cursor: pointer;
  border-radius: var(--r);
  padding: 6px 12px;
  margin: -6px 0;              /* vertical only — keeps the row height compact */
  transition: background .12s ease;
}
.conv-peer.clickable:hover { background: var(--surface-2); }
.conv-titles { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.conv-title {
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-sub {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* jump-to-latest — floats above the composer while scrolled up */
.jump-latest {
  position: absolute;
  right: 18px;
  bottom: 84px;
  z-index: 6;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--ink-soft);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s ease, color .15s ease, transform .12s ease;
  animation: jump-in .18s ease;
}
.jump-latest:hover { background: var(--surface-3); color: var(--ink); }
.jump-latest:active { transform: translateY(1px); }
.jump-count {
  position: absolute;
  top: -4px; right: -4px;
  min-width: 20px; height: 20px;
  padding: 0 6px;
  border-radius: var(--r-pill);
  background: var(--grad);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 11px; font-weight: 500;
  display: inline-grid; place-items: center;
  box-shadow: 0 2px 8px rgba(100, 87, 247, .4);
}
@keyframes jump-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------------- messages ---------------- */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 0;               /* run/day spacing is owned by bubble + day-sep margins */
}

/* momentum scrolling on iOS + keep the scroll from chaining to the page/body */
.messages, .chat-list, .channel-scroll, .thread-msgs, .sp-scroll, .sp-emoji, .pick-list, #cc-members {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ---- sticky day separator (Сегодня / Вчера / 6 июля) ---- */
.day-sep {
  position: sticky;
  top: 6px;
  z-index: 4;
  align-self: center;
  margin: 10px 0 6px;
  pointer-events: none;   /* never intercept scroll/hover */
}
.day-sep span {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: .01em;
  color: var(--ink-soft);
  background: rgba(39, 44, 55, .72);   /* --surface-3 @ .72 */
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  padding: 4px 12px;
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.bubble {
  position: relative;
  max-width: min(560px, 74%);
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  /* Runs share a full-round body; the tail corner is added only on the last
     bubble of a run via .grp-last (see below). */
  border-radius: var(--r-lg);
  padding: 8px 12px 7px;
  margin-top: 2px;         /* tight within a run… */
  box-shadow: var(--shadow-sm);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bubble.grp-first { margin-top: 14px; }   /* …looser between runs */
.bubble.grp-last:not(.mine) { border-bottom-left-radius: 5px; }
.bubble.grp-last.mine       { border-bottom-right-radius: 5px; }

/* File-group ("one box") styles live AFTER the .att-only rules (search
   "===== File group") — they must beat .att-only/.mine/.post at equal specificity,
   which source order decides. */
.bubble.mine {
  align-self: flex-end;
  background: var(--grad);
  border-color: transparent;
  color: #fff;
}
/* Channel post — Telegram-style: full-width, media-forward, footer with time +
   views. No left/right bubble, no read ticks, no per-post sender. */
.bubble.post {
  align-self: flex-start;
  max-width: min(460px, 68%);   /* contained post, not edge-to-edge */
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--ink);
  border-radius: var(--r-lg) !important;   /* never grow a run tail */
  padding: 10px 12px 8px;
}
.bubble.post .sender { display: none; }
.bubble.post .att-video { display: block; width: 100%; }
.bubble.post .att-photo,
.bubble.post .att-video video,
.bubble.post .att-video img {
  width: 100%;
  height: auto;
  max-height: 72vh;
  object-fit: contain;
  border-radius: var(--r);
}
.bubble.post .msg-text { display: block; }
.bubble.post .views { display: inline-flex; align-items: center; gap: 3px; color: var(--faint); font-size: 11px; }
.bubble.post .views .ic { width: 14px; height: 14px; opacity: .85; }
/* Sender line prints once per run — only on the first bubble. */
.bubble .sender {
  display: none;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent-ink);
  margin-bottom: 2px;
}
.bubble.grp-first .sender { display: block; }
/* Meta (edited · time · ticks) trails the last line as one non-breaking unit —
   floated so short messages don't reserve a whole row, grouped so the tick and
   timestamp never wrap apart. */
.bubble .msg-meta {
  float: right;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin: 5px 0 0 10px;
  white-space: nowrap;
  position: relative;
  top: 4px;              /* let the meta sink to the bubble's baseline, TG-style */
}
.bubble .time {
  font-family: var(--font-ui);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
  opacity: .9;
}
.bubble.mine .time { color: rgba(255, 255, 255, .72); }

/* reply quote inside a bubble */
.reply-quote {
  display: block;
  border-left: 3px solid var(--accent);
  padding: 4px 10px;
  margin-bottom: 6px;
  border-radius: 6px;
  background: rgba(100, 87, 247, .08);
  font-size: 13px;
  line-height: 1.35;
}
.bubble.mine .reply-quote { border-left-color: rgba(255,255,255,.8); background: rgba(255, 255, 255, .14); }
.reply-quote.clickable { cursor: pointer; }
.reply-quote .rq-name { display: block; font-weight: 600; color: var(--accent-ink); }
.bubble.mine .reply-quote .rq-name { color: #fff; }
.reply-quote .rq-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.bubble.mine .reply-quote .rq-text { color: rgba(255,255,255,.8); }

/* "Переслано от …" header above forwarded content */
.fwd-head { display: flex; align-items: center; gap: 5px; margin: 0 0 4px; font-size: 12.5px; color: var(--accent-ink); }
.fwd-head .ic { width: 14px; height: 14px; opacity: .85; flex-shrink: 0; }
.fwd-from b { font-weight: 600; }
.fwd-src { cursor: pointer; }
.fwd-src:hover { text-decoration: underline; }
.bubble.mine .fwd-head { color: rgba(255,255,255,.9); }
/* a forwarded file group shows the header once, on the top row */
.bubble.fgrp:not(.fgrp-first) .fwd-head { display: none; }

/* live-message entry — rises + fades, leaning in from the sender's side. Applied
   only to freshly-appended bubbles (appendMessage), never the initial render. */
.msg-enter { animation: msg-enter .22s cubic-bezier(.2, .8, .3, 1); }
@keyframes msg-enter {
  from { opacity: 0; transform: translateY(8px) scale(.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.msg-enter-mine  { transform-origin: bottom right; }
.msg-enter-peer  { transform-origin: bottom left; }

/* flash when jumping to a quoted original */
@keyframes flash { 0%,100% { box-shadow: var(--shadow-sm); } 30% { box-shadow: 0 0 0 3px rgba(100,87,247,.5); } }
.bubble.flash { animation: flash 1.1s ease; }

/* action menu — a vertical labeled context menu. Opened by right-click on a chat
   message, or the ⋯ button on a channel post. Positioned as a FIXED popover at the
   trigger point by JS (openMenuAt), flipping to stay on-screen; on phones CSS pins
   it as a bottom sheet. */
.msg-actions {
  position: fixed;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 4px;
  min-width: 160px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: scale(.96);
  transform-origin: top center;
  pointer-events: none;
  transition: opacity .12s ease, transform .12s ease;
  z-index: 40;
}


/* a labeled menu row */
.msg-act {
  display: flex; align-items: center; gap: 9px;
  border: none; background: transparent; cursor: pointer;
  width: 100%; padding: 7px 10px; border-radius: 8px;
  color: var(--ink-soft); font-size: 13px; text-align: left; white-space: nowrap;
  transition: background .12s ease, color .12s ease;
}
.msg-act .ic { width: 17px; height: 17px; flex-shrink: 0; }
.msg-act-label { flex: 1; }
.msg-act:hover { background: var(--surface-2); color: var(--accent-ink); }
.msg-act.danger { color: var(--ink-soft); }
.msg-act.danger:hover { background: rgba(229, 72, 77, .12); color: var(--danger); }

/* system + welcome */
.system-msg {
  align-self: center;
  background: var(--surface-3);
  color: var(--muted);
  font-size: 12.5px;
  padding: 4px 12px;
  border-radius: var(--r-pill);
  margin: 8px 0;
}
.welcome-card {
  align-self: center;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 22px 26px;
  margin: 14px 0;
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.welcome-title { font-weight: 700; font-size: 17px; }
.welcome-sub { color: var(--muted); font-size: 13.5px; max-width: 260px; }

/* ---------------- attachments ---------------- */
.att-photo, .att-sticker { display: block; border-radius: var(--r); margin-top: 4px; max-width: 100%; }
.att-photo { width: auto; max-height: 360px; object-fit: contain; cursor: zoom-in; }

/* album collage — 2+ photos/videos grouped like Telegram */
.att-album {
  display: grid; gap: 2px; width: 100%; max-width: 420px;
  margin-top: 4px; border-radius: var(--r); overflow: hidden;
}
.att-album .album-cell { position: relative; overflow: hidden; min-width: 0; min-height: 0; }
.att-album .att-photo, .att-album .att-video {
  width: 100%; height: 100%; margin: 0; max-height: none; border-radius: 0;
  object-fit: cover; cursor: zoom-in;
}
.att-album .att-video img { width: 100%; height: 100%; object-fit: cover; }
/* layouts (default: rows of 3 squares) */
.att-album .album-cell { aspect-ratio: 1 / 1; }
.att-album[data-n="2"] { grid-template-columns: 1fr 1fr; }
.att-album[data-n="3"] { grid-template-columns: 2fr 1fr; grid-template-rows: 1fr 1fr; aspect-ratio: 3 / 2; }
.att-album[data-n="3"] .album-cell { aspect-ratio: auto; }
.att-album[data-n="3"] .album-cell:first-child { grid-row: 1 / span 2; }
.att-album[data-n="4"] { grid-template-columns: 2fr 1fr; grid-template-rows: repeat(3, 1fr); aspect-ratio: 1 / 1; }
.att-album[data-n="4"] .album-cell { aspect-ratio: auto; }
.att-album[data-n="4"] .album-cell:first-child { grid-row: 1 / span 3; }
.att-album[data-n="5"], .att-album[data-n="6"], .att-album[data-n="7"],
.att-album[data-n="8"], .att-album[data-n="9"], .att-album[data-n="10"] { grid-template-columns: repeat(3, 1fr); }
/* channel posts: let the album span the full post width */
.bubble.post .att-album { max-width: 100%; }
.att-sticker { max-width: 160px; max-height: 160px; }
.att-video {
  position: relative; margin-top: 4px; border-radius: var(--r);
  overflow: hidden; cursor: pointer; max-width: 100%;
  display: inline-block; line-height: 0;
}
.att-video img { display: block; width: auto; max-width: 100%; max-height: 360px; }
/* Inline player after tapping the poster (plays in the bubble, not an overlay). */
.att-video.playing { cursor: default; }
.att-video-player { display: block; width: 100%; max-height: 70vh; background: #000; }
.att-play {
  position: absolute; inset: 0; margin: auto;
  width: 54px; height: 54px; border-radius: 50%;
  background: rgba(20,21,26,.55); color: #fff;
  display: grid; place-items: center; backdrop-filter: blur(2px);
}
.att-play .ic { width: 24px; height: 24px; margin-left: 2px; }  /* nudge the play triangle optically centered */
.att-play.busy .ic { animation: spin .8s linear infinite; margin-left: 0; }
.att-dl {
  position: absolute; top: 8px; right: 8px;
  width: 34px; height: 34px; border: none; border-radius: 50%;
  background: rgba(20,21,26,.55); color: #fff; cursor: pointer;
  display: grid; place-items: center;
}
.att-dl .ic { width: 17px; height: 17px; }

/* ----- file card ----- */
.att-file {
  display: flex; align-items: center; gap: 12px;
  margin-top: 6px; padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--r); cursor: pointer;
  min-width: 240px; max-width: 320px;
  transition: background .12s ease;
}
.att-file:hover { background: var(--surface-3); }
.bubble.mine .att-file { background: rgba(255,255,255,.14); }
.bubble.mine .att-file:hover { background: rgba(255,255,255,.2); }
.af-icon {
  flex-shrink: 0; width: 42px; height: 42px; border-radius: 11px;
  display: grid; place-items: center; background: var(--grad); color: #fff;
}
.af-icon .ic { width: 21px; height: 21px; }
.af-info { flex: 1; min-width: 0; }
.af-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.af-meta { font-size: 12px; color: var(--muted); font-family: var(--font-mono); margin-top: 1px; }
.bubble.mine .af-meta { color: rgba(255,255,255,.75); }
.af-dl {
  flex-shrink: 0; width: 34px; height: 34px; border: none; border-radius: 50%;
  background: transparent; color: var(--muted); cursor: pointer;
  display: grid; place-items: center; transition: background .12s ease, color .12s ease;
}
.af-dl .ic { width: 18px; height: 18px; }
.af-dl:hover { background: var(--surface); color: var(--accent-ink); }
.bubble.mine .af-dl { color: rgba(255,255,255,.85); }
.bubble.mine .af-dl:hover { background: rgba(255,255,255,.2); color: #fff; }

/* ----- contact card ----- */
.att-contact-card {
  display: flex; align-items: center; gap: 11px;
  margin-top: 6px; padding: 9px 11px;
  background: var(--surface-2);
  border-radius: var(--r); min-width: 240px; max-width: 320px;
  cursor: pointer; transition: background .12s ease;
}
.att-contact-card:hover { background: var(--surface-3); }
.bubble.mine .att-contact-card { background: rgba(255,255,255,.14); }
.bubble.mine .att-contact-card:hover { background: rgba(255,255,255,.2); }
.cc-info { flex: 1; min-width: 0; }
.cc-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cc-sub { font-size: 12px; color: var(--muted); }
.bubble.mine .cc-sub { color: rgba(255,255,255,.75); }

/* link-preview card (SHARE attach) — accent bar · host · title · desc · banner */
.att-share {
  display: block; margin-top: 6px; width: 100%; max-width: 340px;
  border-left: 3px solid var(--accent);
  background: var(--surface-2); border-radius: var(--r-sm);
  overflow: hidden; text-decoration: none; color: inherit;
  transition: background .12s ease;
}
.att-share:hover { background: var(--surface-3); }
.bubble.mine .att-share { background: rgba(255,255,255,.14); border-left-color: rgba(255,255,255,.65); }
.bubble.mine .att-share:hover { background: rgba(255,255,255,.2); }
.att-share-body { padding: 8px 11px; display: flex; flex-direction: column; gap: 2px; }
.att-share-host { font-size: 12px; font-weight: 700; color: var(--accent-ink); }
.bubble.mine .att-share-host { color: #fff; }
.att-share-title { font-size: 14px; font-weight: 600; color: var(--ink); }
.bubble.mine .att-share-title { color: #fff; }
.att-share-desc {
  font-size: 12.5px; color: var(--muted); line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.bubble.mine .att-share-desc { color: rgba(255,255,255,.82); }
.att-share-img { display: block; width: 100%; max-height: 260px; object-fit: cover; }
.cc-actions { display: flex; gap: 4px; flex-shrink: 0; }
.cc-btn {
  width: 34px; height: 34px; border: none; border-radius: 50%;
  background: transparent; color: var(--muted); cursor: pointer;
  display: grid; place-items: center; transition: background .12s ease, color .12s ease;
}
.cc-btn .ic { width: 18px; height: 18px; }
.cc-btn:hover { background: var(--surface); color: var(--accent-ink); }
.bubble.mine .cc-btn { color: rgba(255,255,255,.85); }
.bubble.mine .cc-btn:hover { background: rgba(255,255,255,.2); color: #fff; }
.cc-btn.contact-added { color: #2fb87a; cursor: default; }
.cc-btn.contact-added:hover { background: transparent; color: #2fb87a; }
.bubble.mine .cc-btn.contact-added { color: #bff3da; }

/* ----- poll (enlarged) ----- */
.att-poll { margin-top: 6px; min-width: min(340px, 100%); }
.poll-head { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.poll-ic {
  flex-shrink: 0; width: 36px; height: 36px; border-radius: 11px;
  display: grid; place-items: center; background: var(--grad); color: #fff;
}
.poll-ic .ic { width: 19px; height: 19px; }
.bubble.mine .poll-ic { background: rgba(255,255,255,.22); }
.poll-headtext { flex: 1; min-width: 0; }
.poll-title { font-weight: 700; font-size: 16px; line-height: 1.25; }
.poll-sub { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.bubble.mine .poll-sub { color: rgba(255,255,255,.7); }
.poll-refresh {
  flex-shrink: 0; border: none; background: transparent; color: var(--muted);
  cursor: pointer; padding: 0; display: grid; place-items: center; align-self: flex-start;
}
.poll-refresh .ic { width: 16px; height: 16px; }
.poll-refresh:hover { color: var(--accent-ink); }
.bubble.mine .poll-refresh { color: rgba(255,255,255,.85); }

.poll-opts { display: flex; flex-direction: column; gap: 15px; }
.poll-opt { cursor: pointer; }
.poll-opt-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 7px; }
.poll-opt-text { display: flex; align-items: center; gap: 6px; font-size: 15px; min-width: 0; }
.poll-opt.voted .poll-opt-text { color: var(--accent-ink); font-weight: 600; }
.bubble.mine .poll-opt.voted .poll-opt-text { color: #fff; }
.poll-check { display: inline-grid; place-items: center; color: var(--accent); flex-shrink: 0; }
.poll-check .ic { width: 17px; height: 17px; stroke-width: 2.5; }
.bubble.mine .poll-check { color: #fff; }
.poll-opt-right { display: flex; align-items: center; gap: 7px; flex-shrink: 0; }
.poll-faces { display: flex; }
.poll-faces .avatar { margin-left: -8px; box-shadow: 0 0 0 2px var(--surface); }
.poll-faces .avatar:first-child { margin-left: 0; }
.bubble.mine .poll-faces .avatar { box-shadow: 0 0 0 2px #6a5cf3; }
.poll-opt-count { font-family: var(--font-mono); font-size: 13px; color: var(--muted); min-width: 16px; text-align: right; }
.bubble.mine .poll-opt-count { color: rgba(255,255,255,.85); }
.poll-bar { height: 10px; background: var(--surface-2); border-radius: var(--r-pill); overflow: hidden; }
.bubble.mine .poll-bar { background: rgba(255,255,255,.2); }
.poll-fill { height: 100%; background: var(--grad); border-radius: var(--r-pill); transition: width .35s ease; }
.bubble.mine .poll-fill { background: #fff; }

.poll-voters-btn {
  width: 100%; margin-top: 16px;
  border: none; border-radius: var(--r);
  background: var(--surface-2); color: var(--accent-ink);
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 11px; cursor: pointer; transition: background .12s ease;
}
.poll-voters-btn:hover { background: var(--surface-3); }
.bubble.mine .poll-voters-btn { background: rgba(255,255,255,.16); color: #fff; }
.bubble.mine .poll-voters-btn:hover { background: rgba(255,255,255,.24); }
.poll-empty { margin-top: 14px; font-size: 12.5px; color: var(--faint); text-align: center; }
.bubble.mine .poll-empty { color: rgba(255,255,255,.6); }

/* voters overlay — grouped by option */
.pv-group { margin-bottom: 18px; }
.pv-group:last-child { margin-bottom: 0; }
.pv-group-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  margin-bottom: 8px; padding-bottom: 6px; border-bottom: 1px solid var(--border);
}
.pv-group-name { font-weight: 600; font-size: 14px; }
.pv-group-meta { font-size: 12px; color: var(--muted); font-family: var(--font-mono); flex-shrink: 0; }
.pv-row { display: flex; align-items: center; gap: 11px; padding: 6px 0; }
.pv-name { font-weight: 500; font-size: 14px; }

/* busy state for the download / refresh icon buttons */
.busy { opacity: .5; pointer-events: none; }
.poll-refresh.busy { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.att-service { color: var(--muted); font-size: 13px; }

/* ---------------- reply bar + composer ---------------- */
.reply-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px;
  background: var(--surface); border-top: 1px solid var(--border);
}
.reply-bar-arrow { color: var(--accent); display: inline-grid; place-items: center; }
.reply-bar-arrow .ic { width: 18px; height: 18px; }
.reply-bar-text { flex: 1; min-width: 0; font-size: 13px; line-height: 1.25; }
.reply-bar-text b { display: block; color: var(--accent-ink); font-size: 12.5px; }
.reply-bar-text span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }

.composer {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 16px 16px;
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer-input {
  flex: 1;
  font: inherit;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  padding: 11px 16px;
}
.composer-input::placeholder { color: var(--faint); }
.composer-input:focus { outline: none; border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px rgba(100,87,247,.12); }
.send-btn { background: var(--grad); color: #fff; }
.send-btn:hover { background: var(--grad); color: #fff; filter: brightness(1.05); }
/* the paper-plane glyph is visually weighted to the lower-left; nudge it so it
   reads centered inside the round button */
.send-btn .ic { transform: translate(-1px, 1px); }

.drop-hint {
  position: absolute; inset: 0; margin: 12px;
  border: 2px dashed var(--accent);
  border-radius: var(--r-lg);
  background: rgba(100,87,247,.06);
  display: grid; place-items: center;
  color: var(--accent-ink); font-weight: 600;
  pointer-events: none;
}

/* ============================================================
   Menus & overlays
   ============================================================ */
.menu {
  position: absolute;
  z-index: 30;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 210px;
}
#plus-menu { top: 56px; right: 14px; }
.menu-up { bottom: 76px; left: 16px; }
.menu-item {
  display: flex; align-items: center; gap: 11px;
  width: 100%;
  font: inherit; font-size: 14px; text-align: left;
  background: transparent; border: none; border-radius: var(--r-sm);
  padding: 9px 12px; cursor: pointer; color: var(--ink);
}
.menu-item:hover:not(:disabled) { background: var(--surface-2); }
.menu-item:hover:not(:disabled) .ic { color: var(--accent-ink); }
.menu-item:disabled { color: var(--faint); cursor: default; }
.menu-item .ic { width: 18px; height: 18px; color: var(--muted); flex-shrink: 0; transition: color .15s ease; }

.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20, 21, 26, .42);
  display: grid; place-items: center;
  padding: 20px;
  backdrop-filter: blur(2px);
  animation: overlay-in .16s ease;
}
@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }
.overlay-dark { background: rgba(8, 8, 12, .85); }
.modal {
  width: 100%; max-width: 440px;
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  padding: 20px;
  display: flex; flex-direction: column; gap: 12px;
  max-height: 90vh; overflow-y: auto;
  animation: modal-in .2s cubic-bezier(.2, .8, .3, 1);
}
/* The modal is a flex column with a max-height. Flex children shrink by default,
   so a tall stack would COMPRESS to fit (clipping cards with overflow:hidden)
   instead of overflowing — which killed the scrollbar. Pin them to natural size
   so the content overflows and .modal's overflow-y:auto actually scrolls. */
.modal > * { flex-shrink: 0; }
/* modal rises + settles with a subtle overshoot; the backdrop just fades */
@keyframes modal-in {
  from { opacity: 0; transform: translateY(12px) scale(.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { font-size: 18px; font-weight: 700; margin: 0; line-height: 1.2; }
/* Settings: pin the title + close while the body scrolls. The header bleeds over
   the modal's 20px padding (margin/top -20px) so it sits flush at the modal top
   and stays opaque as content scrolls beneath it. */
.settings-modal .modal-head {
  position: sticky;
  top: -20px;
  z-index: 3;
  background: var(--surface);
  margin: -20px -20px 0;
  padding: 20px 20px 14px;
  border-bottom: 1px solid var(--border);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
}

/* Channel/group profile: a collapsing header (Telegram-style). The modal is a
   fixed header + an independently scrolling body — cleaner than a sticky header
   inside a padded scroll container (which left a strip of content above the bar).
   At the top only the close button shows over the big centred avatar; scrolling
   the body fades in a compact name + members bar in the header. */
.avatar-xs { width: 32px; height: 32px; font-size: 12px; }
/* the scrolling body — centres its content (this modal isn't .modal-center);
   full-width children (.prows, .action-list, .btn-block) already set width:100% */
.channel-scroll {
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; align-items: center; text-align: center;
  gap: 12px;
  padding: 20px 20px 24px;
}
.channel-scroll > * { flex-shrink: 0; }
/* Settings reuse the inline-panel scroll but as a left-aligned, width-capped column
   (the profile block still centres its own avatar via .settings-profile). */
.settings-scroll { text-align: left; gap: 14px; }
.settings-scroll > * { width: 100%; max-width: 520px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; }

/* ===== Channel/group Info screen (Telegram/MAX-style) ===== */
.channel-followers { margin-top: -4px; }

/* Sound / More quick-action buttons */
.channel-hbtns { display: flex; gap: 10px; width: 100%; margin-top: 4px; }
.channel-hbtn {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 10px 6px; border-radius: var(--r);
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--accent-ink); font: inherit; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: background .12s ease, color .12s ease;
}
.channel-hbtn:hover { background: var(--surface-3); }
.channel-hbtn .ic { width: 20px; height: 20px; }
.channel-hbtn.active { color: var(--danger); }

/* Invite-link card with copy / share */
.link-card {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 10px 12px; border-radius: var(--r);
  background: var(--surface-2); border: 1px solid var(--border); text-align: left;
}
.link-card-ic {
  display: grid; place-items: center; width: 30px; height: 30px; flex-shrink: 0;
  border-radius: var(--r-sm); background: var(--grad-soft); color: var(--accent-ink);
}
.link-card-ic .ic { width: 17px; height: 17px; }
.link-card-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.link-card-label { font-size: 12px; color: var(--muted); }
.link-card-value {
  font-size: 14px; color: var(--accent-ink);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.link-card-btn { flex-shrink: 0; color: var(--muted); }
.link-card-btn:hover { color: var(--accent-ink); }

/* Description with collapse */
.channel-desc-wrap { width: 100%; text-align: left; }
.channel-desc { font-size: 14px; color: var(--ink-soft); white-space: pre-wrap; word-break: break-word; }
.channel-desc.clamp {
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.channel-desc-more {
  margin-top: 2px; padding: 0; background: none; border: none; cursor: pointer;
  font: inherit; font-size: 13px; font-weight: 600; color: var(--accent-ink);
}

.channel-more-actions { width: 100%; display: flex; flex-direction: column; gap: 0; }

/* Shared-content tabs */
.ch-tabs {
  display: flex; width: 100%; gap: 4px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); z-index: 1;
}
.ch-tab {
  flex: 1; padding: 10px 4px; background: none; border: none; cursor: pointer;
  font: inherit; font-size: 13px; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color .12s ease;
}
.ch-tab:hover { color: var(--ink); }
.ch-tab.active { color: var(--accent-ink); border-bottom-color: var(--accent); }
.ch-tab-content { width: 100%; text-align: left; }
.ch-tab-empty { padding: 28px 0; text-align: center; color: var(--faint); font-size: 13px; }
.ch-loading-more { padding: 14px 0; text-align: center; color: var(--faint); font-size: 12px; }
/* user profile: centered name that looks like text but is editable (contact rename) */
.pf-name-center {
  width: auto; max-width: 100%; text-align: center;
  border: 1px solid transparent; background: transparent; color: var(--ink);
  font: inherit; font-size: 19px; font-weight: 700; padding: 4px 10px; border-radius: var(--r-sm);
  transition: background .12s ease, border-color .12s ease;
}
.pf-name-center::placeholder { color: var(--faint); font-weight: 400; }
.pf-name-center:hover { background: var(--surface-2); }
.pf-name-center:focus { background: var(--surface-2); border-color: var(--accent, #3a8fff); outline: none; }
/* user profile: Add-to-contacts / Block CTA row + "chats in common" list */
.profile-cta { display: flex; gap: 10px; width: 100%; }
.profile-cta .btn { flex: 1; }
.ch-common-list { display: flex; flex-direction: column; }
.ch-common-row {
  display: flex; align-items: center; gap: 11px; width: 100%;
  padding: 8px 6px; border: none; background: transparent; cursor: pointer;
  text-align: left; border-radius: var(--r-sm);
}
.ch-common-row:hover { background: var(--surface-2); }
.ch-common-body { display: flex; flex-direction: column; min-width: 0; }
.ch-common-name { font-weight: 600; font-size: 14px; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-common-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-month {
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 12px 2px 6px;
}
.ch-media-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; }
.ch-media-cell {
  position: relative; aspect-ratio: 1 / 1; overflow: hidden; cursor: pointer;
  background: var(--surface-2); border: none; padding: 0; border-radius: 3px;
}
.ch-media-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ch-media-ph { display: grid; place-items: center; width: 100%; height: 100%; color: var(--faint); }
.ch-media-play {
  position: absolute; inset: 0; margin: auto; width: 30px; height: 30px;
  display: grid; place-items: center; color: #fff;
  background: rgba(20,21,26,.5); border-radius: 50%; pointer-events: none;
}
.ch-media-play .ic { width: 16px; height: 16px; }
.ch-list { display: flex; flex-direction: column; }
.ch-file-row, .ch-link-row {
  display: flex; align-items: center; gap: 12px; padding: 9px 2px;
  border-top: 1px solid var(--border); color: var(--ink); text-decoration: none;
}
.ch-file-row:first-child, .ch-link-row:first-child { border-top: none; }
.ch-link-row:hover { background: var(--surface-2); }
.ch-file-ic, .ch-link-ic {
  display: grid; place-items: center; width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--r-sm); background: var(--grad-soft); color: var(--accent-ink); overflow: hidden;
}
.ch-file-ic .ic, .ch-link-ic .ic { width: 20px; height: 20px; }
.ch-link-ic img { width: 100%; height: 100%; object-fit: cover; }
.ch-file-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.ch-file-name {
  font-size: 14px; color: var(--ink);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ch-file-meta {
  font-size: 12px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Info panel = inline view in the right pane (not a modal) */
.channel-info-head { display: grid; grid-template-columns: 40px 1fr 40px; gap: 0; }
.channel-info-title { text-align: center; font-weight: 700; font-size: 16px; align-self: center; }
.channel-info .channel-scroll { border-radius: 0; }

/* "Ещё" (•••) dropdown menu, anchored under the More button */
.channel-more-wrap { position: relative; flex: 1; display: flex; }
.channel-more-wrap > .channel-hbtn { flex: 1; }
.channel-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 50;
  min-width: 230px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px;
}
/* clean rows inside the dropdown: icon + label, no colored square / chevron */
.channel-menu .action-list,
.channel-menu .action-list.danger-list {
  background: transparent; border: none; border-radius: 0; margin: 0;
}
.channel-menu .action-item {
  padding: 9px 10px; border-top: none; border-radius: var(--r-sm); margin-top: 0;
}
.channel-menu .action-item:hover { background: var(--surface-2); }
.channel-menu .danger-list .action-item:hover { background: rgba(255, 107, 111, .1); }
.channel-menu .action-ic { width: 22px; height: 22px; background: transparent; color: inherit; }
.channel-menu .action-ic .ic { width: 18px; height: 18px; }
.channel-menu .danger-list .action-ic { background: transparent; }
.channel-menu .action-chevron { display: none; }

.media-preview { display: grid; place-items: center; background: var(--surface-2); border-radius: var(--r); padding: 12px; min-height: 120px; }
.media-preview img, .media-preview video { max-width: 100%; max-height: 50vh; border-radius: var(--r-sm); }

/* multi-attach send tray (album) */
.album-tray { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; width: 100%; }
.album-item { position: relative; border-radius: var(--r-sm); overflow: hidden; background: var(--surface-3); }
.album-item img, .album-item video { display: block; }
/* single: one big preview (keeps the old look) */
.media-preview:not(.album-multi) .album-item img,
.media-preview:not(.album-multi) .album-item video { max-height: 46vh; max-width: 100%; width: auto; object-fit: contain; }
/* many: uniform square thumbnails */
.media-preview.album-multi .album-item { width: 104px; height: 104px; }
.media-preview.album-multi .album-item img,
.media-preview.album-multi .album-item video { width: 100%; height: 100%; object-fit: cover; border-radius: 0; }
.album-rm {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px;
  border: none; border-radius: 50%; background: rgba(0, 0, 0, .55); color: #fff;
  display: grid; place-items: center; cursor: pointer;
}
.album-rm .ic { width: 14px; height: 14px; }
.album-add {
  width: 104px; height: 104px; flex-shrink: 0;
  border: 1px dashed var(--border-2); border-radius: var(--r-sm);
  background: var(--surface-3); color: var(--muted);
  display: grid; place-items: center; cursor: pointer;
  transition: color .12s ease, border-color .12s ease;
}
.album-add:hover { color: var(--ink); border-color: var(--accent); }
.album-add .ic { width: 26px; height: 26px; }
.album-file { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; padding: 10px; text-align: center; color: var(--ink-soft); }
.album-file .ic { width: 30px; height: 30px; }
.album-file-name { font-size: 11px; max-width: 88px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-pre { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.file-pre .af-icon { width: 56px; height: 56px; border-radius: 14px; }
.file-pre .af-icon .ic { width: 28px; height: 28px; }
.file-pre .af-name { margin-top: 8px; font-weight: 600; }
.file-pre .af-meta { font-size: 12px; color: var(--muted); font-family: var(--font-mono); }

.lightbox-video, .lightbox-photo {
  max-width: 90vw; max-height: 86vh; border-radius: var(--r);
  animation: lightbox-in .2s cubic-bezier(.2, .8, .3, 1);
}
.lightbox-photo { object-fit: contain; box-shadow: var(--shadow-lg); }
/* media zooms up from 92% as the dark backdrop fades in */
@keyframes lightbox-in { from { opacity: 0; transform: scale(.92); } to { opacity: 1; transform: scale(1); } }
.lightbox-close {
  position: fixed; top: 18px; right: 18px;
  width: 44px; height: 44px; border-radius: 50%;
  border: none; background: rgba(255,255,255,.12); color: #fff; cursor: pointer;
  display: grid; place-items: center;
}
.lightbox-close:hover { background: rgba(255,255,255,.22); }

/* ============================================================
   Iteration 2 — channels, settings, profiles, pickers
   ============================================================ */
.btn-sm { padding: 7px 14px; font-size: 13px; }
.btn-link {
  background: none; border: none; padding: 2px;
  font: inherit; font-weight: 600; font-size: 13px;
  color: var(--accent-ink); cursor: pointer; align-self: flex-start;
}
.btn-link:hover { text-decoration: underline; }
.btn.subscribed { background: var(--surface-2); color: var(--ink-soft); box-shadow: none; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.sep { border: none; height: 1px; background: var(--border); margin: 4px 0; }
.modal-subtitle { font-size: 13px; font-weight: 700; color: var(--muted); margin: 0; text-transform: uppercase; letter-spacing: .04em; }
.modal-center { align-items: center; text-align: center; position: relative; }
.modal-x { position: absolute; top: 12px; right: 12px; }

input[type="checkbox"] { accent-color: var(--accent); }
textarea.field { resize: vertical; font-family: var(--font-ui); }

/* Custom checkbox for the contact/chat pickers — a rounded box that fills with
   the spark gradient and shows a check when selected (replaces the OS square). */
.contact-row input[type="checkbox"],
.pick-row input[type="checkbox"] {
  appearance: none; -webkit-appearance: none;
  width: 20px; height: 20px; flex-shrink: 0; margin: 0;
  border: 1.5px solid var(--border-2); border-radius: 6px;
  background: var(--surface); cursor: pointer; position: relative;
  transition: background .15s ease, border-color .15s ease;
}
.contact-row input[type="checkbox"]:hover,
.pick-row input[type="checkbox"]:hover { border-color: var(--accent); }
.contact-row input[type="checkbox"]:checked,
.pick-row input[type="checkbox"]:checked { background: var(--grad); border-color: transparent; }
/* a centered check drawn as a background SVG — no fragile px-tuned triangle */
.contact-row input[type="checkbox"]:checked::after,
.pick-row input[type="checkbox"]:checked::after {
  content: ""; position: absolute; inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 13px;
}
.contact-row input[type="checkbox"]:focus-visible,
.pick-row input[type="checkbox"]:focus-visible {
  outline: none; box-shadow: 0 0 0 3px rgba(100, 87, 247, .2);
}

/* channel search bar */
.channel-search { padding: 0 14px 10px; }
.channel-search .field { padding: 9px 14px; }

/* contact pickers (share contact / members / create) */
.pick-list { max-height: 260px; overflow-y: auto; margin: 0 -4px; }
#forward-search { margin-bottom: 10px; }
.contact-row {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 8px; border-radius: var(--r); cursor: pointer;
}
.contact-row:hover { background: var(--surface-2); }
.contact-name { flex: 1; min-width: 0; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* relay (TG ⇄ MAX) modal */
.relay-routes { display: flex; flex-direction: column; gap: 6px; margin: 6px 0 12px; }
.relay-route-row {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border-radius: var(--r); background: var(--surface-2);
}
.relay-route-name { flex: 1; min-width: 0; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#relay-overlay .field-label { margin-top: 8px; }
/* relay dropdowns: full-width triggers, panels drop below the full field */
.relay-dd .dropdown { display: block; }
.relay-dd .dropdown-trigger { width: 100%; min-width: 0; padding: 10px 12px; font-size: 14px; }
.relay-dd .dropdown-panel { left: 0; right: 0; }
/* the bind button spans the width and sits flush under the direction field */
.relay-bind-foot { margin-top: 12px; }
.relay-bind-foot .btn { width: 100%; }
.relay-bind-cta { margin: 8px 0 4px; }
/* A CTA anchor styled as a button; .disabled greys it out while the link loads. */
a.btn.disabled { pointer-events: none; opacity: 0.55; }
/* personal-bot connect form (Approach A) */
.relay-guide { margin: 4px 0 10px; padding-left: 20px; line-height: 1.5; }
.relay-guide li { margin: 2px 0; }
/* red "bot is relay-only" warning above the token input */
.relay-token-warning {
  margin: 0 0 10px; padding: 8px 10px;
  border: 1px solid var(--danger); border-radius: var(--r-sm, 8px);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger); font-size: 13px; line-height: 1.45;
}
.relay-token-warning b { color: var(--danger); }
#relay-bot-token { width: 100%; }
/* status line showing the connected bot + a disconnect button */
.relay-bot-line {
  display: flex; align-items: center; gap: 10px;
  margin: 2px 0 12px; padding: 8px 10px;
  background: var(--surface-2); border-radius: 10px;
}
.relay-bot-line > span { flex: 1; min-width: 0; }
.relay-bot-line .btn { flex-shrink: 0; padding: 6px 12px; }
/* label row with an inline "Обновить" action on the right */
.field-label-row { display: flex; align-items: center; justify-content: space-between; }
.linklike {
  border: none; background: none; cursor: pointer; padding: 0;
  color: var(--accent-ink); font-size: 13px; font-weight: 600;
}
.linklike:hover { text-decoration: underline; }

/* ---- settings modal: a touch wider, sectioned into cards ---- */
.settings-modal { max-width: 480px; gap: 16px; }

/* settings — own profile: avatar with a camera overlay, fields grouped */
.settings-profile { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.avatar-edit { position: relative; }
.avatar-cam {
  position: absolute; right: -2px; bottom: -2px;
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--grad); color: #fff;
  border: 2px solid var(--surface); cursor: pointer;
  box-shadow: 0 3px 10px rgba(100, 87, 247, .4);
  transition: transform .12s ease, filter .15s ease;
}
.avatar-cam:hover { filter: brightness(1.08); }
.avatar-cam:active { transform: translateY(1px); }
.avatar-cam .ic { width: 15px; height: 15px; }
.settings-fields { width: 100%; display: flex; flex-direction: column; gap: 8px; }
.settings-profile .btn-block { margin-top: 2px; }

/* settings — a titled card per group (Приватность / Ключ / Заблокированные) */
.settings-section {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 6px 14px 10px;
}
.section-head {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 0 6px;
}
.section-ic {
  display: grid; place-items: center;
  width: 28px; height: 28px; border-radius: var(--r-sm);
  background: var(--grad-soft); color: var(--accent-ink);
}
.section-ic .ic { width: 16px; height: 16px; }
.section-title {
  font-size: 12px; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--muted);
}

/* a tappable row inside a section (e.g. "Ключ доступа") */
.settings-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  font: inherit; text-align: left;
  background: transparent; border: none; cursor: pointer;
  color: var(--ink-soft); padding: 8px 2px;
  border-radius: var(--r-sm);
  transition: color .12s ease;
}
.settings-row:hover { color: var(--ink); }
.settings-row-text { flex: 1; min-width: 0; font-family: var(--font-mono); font-size: 13px; }
.settings-row-chevron { width: 16px; height: 16px; color: var(--faint); flex-shrink: 0; }

/* settings — privacy rows */
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); }
.set-row:first-child { border-top: none; }
.set-label { font-size: 14px; }
/* secondary line under a setting label (e.g. what "Тихое чтение" does) */
.set-hint { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.notify-sound-actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

/* custom dropdown (settings) — our own popup instead of the OS select sheet */
.dropdown { position: relative; }
.dropdown-trigger {
  display: inline-flex; align-items: center; gap: 8px;
  min-width: 132px;
  font: inherit; font-size: 13px; color: var(--ink);
  background: var(--surface); border: 1px solid var(--border-2);
  border-radius: var(--r-sm); padding: 7px 10px 7px 12px; cursor: pointer;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.dropdown-trigger:hover { border-color: var(--accent); }
.dropdown-trigger.open,
.dropdown-trigger:focus-visible {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(100, 87, 247, .14);
}
.dropdown-value { flex: 1; text-align: left; white-space: nowrap; }
.dropdown-chevron {
  width: 15px; height: 15px; color: var(--muted);
  transition: transform .2s ease, color .15s ease;
}
.dropdown-trigger.open .dropdown-chevron { transform: rotate(180deg); color: var(--accent-ink); }
.dropdown-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 40;
  min-width: 100%;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow);
  padding: 5px; display: flex; flex-direction: column; gap: 2px;
  animation: dropdown-in .14s ease;
}
@keyframes dropdown-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
.dropdown-opt {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  font: inherit; font-size: 13px; text-align: left; white-space: nowrap;
  background: transparent; border: none; border-radius: var(--r-sm);
  padding: 8px 10px; cursor: pointer; color: var(--ink-soft);
  transition: background .12s ease, color .12s ease;
}
.dropdown-opt:hover { background: var(--surface-2); color: var(--ink); }
.dropdown-opt.selected { color: var(--accent-ink); font-weight: 600; }
.dropdown-opt .ic { width: 15px; height: 15px; color: var(--accent); flex-shrink: 0; }

.set-toggle {
  appearance: none; -webkit-appearance: none;
  width: 42px; height: 24px; flex-shrink: 0;
  border-radius: var(--r-pill); background: var(--surface-3);
  position: relative; cursor: pointer; transition: background .15s ease;
}
.set-toggle::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; transition: left .15s ease;
}
.set-toggle:checked { background: var(--accent); }
.set-toggle:checked::after { left: 21px; }

/* settings — blocked + devices */
.blocked-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.blocked-name { flex: 1; min-width: 0; font-weight: 500; }
.device-row { padding: 9px 0; border-top: 1px solid var(--border); }
.device-row:first-child { border-top: none; }
.device-row.current { border-left: 2px solid var(--accent); padding-left: 10px; }
.dev-title { font-weight: 600; font-size: 14px; }
.dev-sub { font-size: 12px; color: var(--muted); }
.dev-badge { font-size: 10px; font-weight: 600; color: var(--accent-ink); background: rgba(100,87,247,.14); padding: 1px 7px; border-radius: var(--r-pill); margin-left: 6px; }

/* profile / channel cards */
.profile-name { font-weight: 700; font-size: 19px; }
.prows { width: 100%; display: flex; flex-direction: column; }
.prow { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-top: 1px solid var(--border); font-size: 14px; text-align: left; }
.prow:first-child { border-top: none; }
.pk { color: var(--muted); flex-shrink: 0; }
.pv { color: var(--ink-soft); overflow-wrap: anywhere; text-align: right; }
.profile-edit { width: 100%; display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.channel-desc { font-size: 14px; color: var(--ink-soft); white-space: pre-wrap; }

/* channel/group action menu — a grouped list of rows with an icon + chevron,
   replacing the flat stack of ghost buttons. Danger actions live in their own
   group below. Left-aligned inside the centered profile modal. */
.action-list {
  width: 100%;
  display: flex; flex-direction: column;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
}
.action-list.danger-list { background: transparent; border: none; }
.action-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; text-align: left;
  font: inherit; font-size: 14px; font-weight: 500; color: var(--ink);
  background: transparent; border: none; cursor: pointer;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  transition: background .12s ease;
}
.action-item:first-child { border-top: none; }
.action-item:hover { background: var(--surface-3); }
.action-ic {
  display: grid; place-items: center;
  width: 30px; height: 30px; border-radius: var(--r-sm);
  background: var(--grad-soft); color: var(--accent-ink); flex-shrink: 0;
}
.action-ic .ic { width: 17px; height: 17px; }
.action-label { flex: 1; min-width: 0; }
.action-chevron { width: 16px; height: 16px; color: var(--faint); flex-shrink: 0; }
/* danger group: same rows, red tint, standalone cards */
.danger-list .action-item {
  border: 1px solid var(--border); border-radius: var(--r);
  margin-top: 8px; color: var(--danger);
}
.danger-list .action-item:hover { background: rgba(255, 107, 111, .08); }
.danger-list .action-ic { background: rgba(255, 107, 111, .12); color: var(--danger); }

/* participants management */
.participant-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; }
.participant-row.clickable { cursor: pointer; border-radius: var(--r-sm); margin: 0 -6px; padding: 7px 6px; }
.participant-row.clickable:hover { background: var(--surface-2); }
.participant-name { flex: 1; min-width: 0; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role { font-size: 11px; padding: 2px 8px; border-radius: var(--r-pill); flex-shrink: 0; }
.role.owner { background: rgba(155,91,246,.18); color: var(--accent-ink); }
.role.admin { background: var(--surface-3); color: var(--muted); }
.pm-act {
  width: 30px; height: 30px; border: none; border-radius: var(--r-sm);
  background: var(--surface-2); color: var(--muted); cursor: pointer;
  display: grid; place-items: center;
  transition: background .12s ease, color .12s ease;
}
.pm-act .ic { width: 16px; height: 16px; }
.pm-act:hover { background: var(--surface-3); color: var(--ink); }
.pm-act.danger:hover { color: var(--danger); }

/* create / edit icon picker */
.cc-icon-wrap { display: flex; flex-direction: column; align-items: center; gap: 6px; }

/* create/edit panel: the panel itself does NOT scroll — icon/name/description and
   the footer stay put; only the members list scrolls (flexes to fill the leftover
   height and gets its own scrollbar when there are many contacts). */
#cc-scroll { overflow: hidden; }
#cc-members-block { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
#cc-members { flex: 1 1 auto; min-height: 80px; overflow-y: auto; margin: 0 -4px; }

/* ---------------- confirm dialog ---------------- */
.modal-confirm { max-width: 380px; gap: 8px; }
.modal-confirm h2 { font-size: 18px; font-weight: 700; margin: 0; }
.modal-confirm p { margin: 0 0 6px; font-size: 14px; }
.btn-danger-solid { background: var(--danger); color: #fff; }
.btn-danger-solid:hover { filter: brightness(1.06); }

/* ---------------- toasts ---------------- */
.toast-host {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  z-index: 100; display: flex; flex-direction: column; gap: 8px;
  align-items: center; pointer-events: none;
}
.toast {
  pointer-events: auto;
  max-width: min(440px, 90vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  padding: 11px 16px;
  font-size: 14px;
  color: var(--ink);
  animation: toast-in .22s ease;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok    { border-left-color: #2fb87a; }
.toast.leaving { animation: toast-out .22s ease forwards; }
@keyframes toast-in  { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(10px); } }

/* ---------------- scrollbar ---------------- */
.chat-list::-webkit-scrollbar, .messages::-webkit-scrollbar, .modal::-webkit-scrollbar { width: 9px; }
.chat-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb, .modal::-webkit-scrollbar-thumb {
  background: var(--border-2); border-radius: var(--r-pill); border: 2px solid var(--surface);
}
.chat-list::-webkit-scrollbar-thumb:hover, .messages::-webkit-scrollbar-thumb:hover { background: var(--faint); }

/* tap-to-reveal the per-message toolbar on touch (JS toggles .show-actions) */
.bubble.show-actions .msg-actions {
  opacity: 1; transform: scale(1); pointer-events: auto;
}

/* ============================================================
   Responsive — phone layout
   The desktop three-column shell folds to one column: the nav rail
   becomes a fixed bottom tab-bar (with a slim access-key strip on
   top), the chat list fills the screen, and an open chat takes over
   full-screen — the tab-bar steps aside, mirroring native messengers.
   ============================================================ */
@media (max-width: 720px) {
  .workspace { grid-template-columns: 1fr; }

  /* ---- nav rail → fixed bottom tab-bar ---- */
  .nav-rail {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    flex-direction: column-reverse;   /* key strip on top, tab row below */
    justify-content: initial;
    overflow: visible;
    padding: 0 0 var(--safe-b);
    background: var(--surface);
    border-right: none;
    border-top: 1px solid var(--border);
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .35);
  }
  .rail-top { flex-direction: row; gap: 0; padding: 6px 6px 4px; }
  .rail-top .rail-btn { flex: 1; }
  /* a floating icon "pill" that lights up on the active tab, label under it —
     lighter than the old full-width gradient block */
  .rail-btn {
    border-radius: 0; padding: 5px 2px; gap: 4px;
    min-height: 50px; justify-content: center;
    color: var(--muted);
  }
  .rail-btn .rail-ic-wrap {
    display: grid; place-items: center;
    width: 46px; height: 30px; border-radius: var(--r-pill);
    transition: background .18s ease, color .18s ease;
  }
  .rail-btn .ic { width: 22px; height: 22px; }
  .rail-label { font-size: 10.5px; font-weight: 600; }
  .rail-btn.active { background: transparent; color: var(--accent-ink); }
  .rail-btn.active .rail-ic-wrap { background: var(--grad-soft); color: var(--accent-ink); }
  .rail-btn.active::before { display: none; }   /* drop the desktop left-edge bar */
  .rail-btn:active .rail-ic-wrap { transform: scale(.9); }
  .rail-sep { display: none; }                   /* a vertical divider can't sit in a row */

  /* ---- panels ---- */
  .sidebar { border-right: none; }
  /* keep the last chats clear of the fixed tab-bar */
  .chat-list { padding-bottom: calc(var(--tabbar) + 46px + var(--safe-b)); }

  .conversation { position: fixed; inset: 0; z-index: 30; background: var(--bg); }
  .workspace:not(.chat-open) .conversation { display: none; }
  .workspace.chat-open .sidebar { display: none; }
  .workspace.chat-open .nav-rail { display: none; }   /* chat is full-screen */
  .composer { padding-bottom: calc(14px + var(--safe-b)); }
  .menu-up { bottom: calc(78px + var(--safe-b)); }
  .jump-latest { bottom: calc(84px + var(--safe-b)); right: 14px; }

  .bubble { max-width: 86%; }
  /* channel posts are media-forward — let them use (nearly) the full width on phones */
  .bubble.post { max-width: 96%; }

  /* ---- 16px inputs so iOS doesn't auto-zoom on focus ---- */
  .field, textarea.field, .composer-input, .dropdown-trigger, .key-full, #renew-input,
  #message-input, #captcha-input, #addphone-input { font-size: 16px; }

  /* ---- comfortable tap targets ---- */
  .icon-btn { width: 42px; height: 42px; }
  .icon-btn.icon-sm { width: 38px; height: 38px; }

  /* action menu stays a compact popover near the tap point (openMenuAt), just with
     slightly larger tap targets on touch */
  .msg-act { padding: 9px 12px; font-size: 14px; }
  .msg-act .ic { width: 19px; height: 19px; }

  /* ---- modals → bottom sheets (media lightbox stays centered) ---- */
  .overlay:not(.overlay-dark) { align-items: flex-end; padding: 0; }
  .overlay:not(.overlay-dark) .modal {
    width: 100%; max-width: 100%;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    max-height: 88vh;
    padding: 18px 18px calc(18px + var(--safe-b));
    animation: sheet-up .24s ease;
    -webkit-overflow-scrolling: touch;   /* momentum scroll on iOS */
    overscroll-behavior: contain;        /* don't chain the scroll to the page */
  }
  /* a centered profile sheet must still scroll top-aligned, or the tall action
     stack gets clipped with no way to reach the lower rows */
  .overlay:not(.overlay-dark) .modal.modal-center { justify-content: flex-start; }
  .modal-center .modal-x { top: 8px; right: 8px; }
  /* match the sticky settings header bleed to the sheet's 18px padding */
  .settings-modal .modal-head { top: -18px; margin: -18px -18px 0; padding: 18px 18px 12px; }
  .channel-scroll { padding: 16px 16px calc(16px + var(--safe-b)); }

  /* ---- auth cards fill the small screen ---- */
  .auth { padding: 16px; }
  .auth-card { max-width: 100%; padding: 26px 22px; }

  /* ---- lift toasts clear of the bottom tab-bar / composer ---- */
  .toast-host { bottom: calc(var(--tabbar) + 28px + var(--safe-b)); }
  /* NB: the picker's mobile bottom-sheet rules live AFTER the base .sticker-panel
     definition (search "picker → full-width bottom sheet"), otherwise the base
     420px width wins by source order. */
}

@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* very small phones: tighten the tab labels and bubble width */
@media (max-width: 380px) {
  .rail-label { font-size: 9px; }
  .rail-btn .ic { width: 21px; height: 21px; }
  .bubble { max-width: 90%; }
}

/* ============================================================
   Touch devices — reveal controls that desktop shows on hover
   ============================================================ */
@media (hover: none) {
  .msg-actions { background: var(--surface-2); }
  /* let our long-press own the gesture instead of the OS text callout/selection
     (message text is still copyable via the action menu's Copy) */
  .bubble { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
  .bubble a { -webkit-user-select: auto; user-select: auto; }
}

/* ---------------- reduced motion ---------------- */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ============================================================
   New features: tabs fit, attachment-only bubbles, edit marker,
   key expiry, folders manager
   ============================================================ */

/* attachment-only messages drop the chat-bubble chrome (F7) */
.bubble.att-only {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  padding: 0;
}
.bubble.att-only.mine { background: transparent; color: var(--ink); }
.bubble.att-only .msg-meta {
  float: none; display: flex; justify-content: flex-end;
  margin: 2px 2px 0;
}
.bubble.att-only .time { color: var(--faint); }
/* keep file/contact cards readable when "mine" loses the purple backdrop */
.bubble.att-only.mine .att-file,
.bubble.att-only.mine .att-contact-card { background: var(--surface-2); }
.bubble.att-only.mine .att-file:hover,
.bubble.att-only.mine .att-contact-card:hover { background: var(--surface-3); }
.bubble.att-only.mine .af-name,
.bubble.att-only.mine .cc-name { color: var(--ink); }
.bubble.att-only.mine .af-meta,
.bubble.att-only.mine .cc-sub { color: var(--muted); }
.bubble.att-only.mine .af-icon,
.bubble.att-only.mine .af-dl,
.bubble.att-only.mine .cc-btn { color: var(--muted); }
.bubble.att-only.mine .af-dl:hover,
.bubble.att-only.mine .cc-btn:hover { background: var(--surface-3); color: var(--ink); }
.bubble.att-only.mine .cc-btn.contact-added { color: var(--ok, #3ecf8e); }

/* ===== File group: one box =====
   MAX can't carry >1 FILE in an op64, so a multi-file post lands as several
   messages. regroup() tags a consecutive run with .fgrp (+ -first/-mid/-last) and
   moves the caption to the last row. Here each bubble becomes a segment of ONE
   shared box (surface-2), the segments butt together with a hairline divider, and
   the caption + footer sit INSIDE the box on the last row — like a Telegram
   media-group of documents. Placed after .att-only/.mine so it wins source order. */
.bubble.fgrp {
  background: var(--surface-2);
  border: 1px solid transparent;
  box-shadow: none;
  padding: 4px 10px;
  max-width: min(360px, 82%);
  /* !important beats .bubble.post{border-radius:var(--r-lg) !important} — without
     it every segment stays fully rounded and reads as its own box */
  border-radius: 14px !important;
  color: var(--ink);
}
/* .mine needs surface-2 too — beat .bubble.att-only.mine{background:transparent}
   (0,3,0), else the caption/footer render on the page bg outside the box */
.bubble.fgrp.mine { align-self: flex-end; background: var(--surface-2); }
.bubble.post.fgrp { align-self: flex-start; }
/* segments butt into one box: no gap, only the box's OUTER corners round, the
   connecting corners go square (!important to beat .bubble.post's radius), a
   hairline divider between file rows */
.bubble.fgrp:not(.fgrp-first) {
  margin-top: 0;
  border-top: 1px solid rgba(255, 255, 255, .06);
  border-top-left-radius: 0 !important; border-top-right-radius: 0 !important;
}
.bubble.fgrp:not(.fgrp-last) {
  border-bottom-left-radius: 0 !important; border-bottom-right-radius: 0 !important;
}
/* the file row is transparent inside the box */
.bubble.fgrp .att-file {
  margin: 0; width: 100%; max-width: none; min-width: 0;
  background: transparent; border-radius: 8px; padding: 6px 4px;
}
.bubble.fgrp .att-file:hover { background: rgba(255, 255, 255, .05); }
.bubble.fgrp .af-name { color: var(--ink); }
.bubble.fgrp .af-meta { color: var(--muted); }
.bubble.fgrp .af-dl { color: var(--muted); }
.bubble.fgrp .af-dl:hover { background: rgba(255, 255, 255, .1); color: var(--ink); }
/* caption INSIDE the box, at the foot (regroup moved it onto the last row) */
.bubble.fgrp .msg-text { display: block; margin: 2px 4px 3px; color: var(--ink); }
/* one footer for the whole group, inside the box on the last row */
.bubble.fgrp .msg-meta { float: none; display: flex; justify-content: flex-end; margin: 2px 0 0; position: static; top: auto; }
.bubble.fgrp .time { color: var(--faint); }
.bubble.fgrp .views { color: var(--faint); }
.bubble.fgrp:not(.fgrp-last) .msg-meta { display: none; }
/* group hover-actions are consolidated onto the last row and use the default
   vertical-beside placement (shown only on that row's own hover). */

/* edited marker + multi-line text */
.msg-text { white-space: pre-wrap; }
/* Inline formatting mapped from MAX `elements` (relayed TG entities). */
.msg-text a { color: var(--accent, #4aa3ff); text-decoration: none; }
.msg-text a:hover { text-decoration: underline; }
.msg-text .fmt-heading { font-weight: 700; font-size: 1.05em; }
.msg-text .fmt-quote {
  /* Block-level: an inline(-block) quote containing newlines (pre-wrap) breaks
     the line box and scatters text around the border (the garbled-layout bug).
     A block renders MAX quotes like the native client: continuous left bar. */
  display: block;
  margin: 4px 0;
  border-left: 3px solid var(--accent, #4aa3ff);
  padding-left: 8px;
  opacity: .9;
}
.edited { font-size: 10.5px; color: var(--faint); margin-left: 6px; font-style: italic; }
.bubble.mine .edited { color: rgba(255, 255, 255, .7); }
.editmsg-text { resize: vertical; min-height: 64px; font: inherit; line-height: 1.4; }

/* access-key expiry — in Settings above "Заблокированные", click to open details (F6) */
/* key-access row lives inside a settings section (.settings-row handles layout);
   only the expiring-soon accent is specific here. */
.key-expiry.expiring .settings-row-text { color: var(--danger); }

/* access-key details modal */
.key-field-label { display: block; font-size: 12px; color: var(--muted); margin: 4px 0 6px; }
.key-value-row { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.key-full {
  flex: 1; min-width: 0; font-family: var(--font-mono); font-size: 13px; color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 9px 11px; word-break: break-all; user-select: all;
}
/* renewal-voucher input + submit; .key-value-row already lays them out in a row */
#renew-input::placeholder { color: var(--faint); letter-spacing: 0; }
#renew-submit { flex-shrink: 0; white-space: nowrap; }
#renew-msg { margin-top: -8px; }
#renew-msg:not(.muted) { color: var(--danger); }

/* folders manager (F2) */
.folders-hint { margin: -4px 0 10px; line-height: 1.4; }
.folders-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; max-height: 50vh; overflow: auto; }
.folder-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: var(--r); background: var(--surface-2); border: 1px solid var(--border);
}
.folder-info { flex: 1; min-width: 0; }
.folder-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.folder-meta { font-size: 12px; color: var(--muted); margin-top: 1px; }
.folder-acts { display: flex; gap: 4px; flex-shrink: 0; }
.icon-btn.danger { color: var(--danger); }
.folder-pick-label { font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.folder-chats {
  display: flex; flex-direction: column; gap: 2px; max-height: 46vh; overflow: auto;
  border: 1px solid var(--border); border-radius: var(--r); padding: 4px;
}
.pick-row { display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 8px; cursor: pointer; }
.pick-row:hover { background: var(--surface-2); }
.pick-row input { accent-color: var(--accent); width: 16px; height: 16px; flex-shrink: 0; }
.pick-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-block { width: 100%; }

/* ============================================================
   new.md batch: read receipts, contact menu, about
   ============================================================ */

/* read receipts: one tick = sent, two ticks (accent) = read by the peer */
.ticks { display: inline-flex; vertical-align: middle; margin-left: 3px; }
.ticks .ic { width: 15px; height: 15px; stroke-width: 2.2; }
/* a quick pop when ✓ becomes ✓✓ (peer read the message) */
.ticks.tick-pop { animation: tick-pop .32s cubic-bezier(.2, .9, .3, 1.4); }
@keyframes tick-pop { 0% { transform: scale(1); } 40% { transform: scale(1.35); } 100% { transform: scale(1); } }
.bubble.mine .ticks { color: rgba(255, 255, 255, .7); }
.bubble.mine .ticks.read { color: #7fd4ff; }
.bubble.att-only.mine .ticks { color: var(--muted); }
.bubble.att-only.mine .ticks.read { color: var(--accent-ink); }

/* floating context menu (contact "…" actions) */
.menu-float { position: fixed; z-index: 60; min-width: 180px; box-shadow: var(--shadow); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover { background: rgba(255, 107, 111, .12); }

/* "…" button on a contact row (appears on hover) */
.chat-item { position: relative; }
.contact-more {
  flex-shrink: 0; border: none; background: transparent; color: var(--muted);
  width: 34px; height: 34px; border-radius: 8px; cursor: pointer;
  display: grid; place-items: center; transition: background .12s, color .12s;
}
.contact-more:hover { background: var(--surface-3); color: var(--ink); }
.contact-more .ic { width: 18px; height: 18px; }

/* "About" textarea in the profile editor */
#me-about { resize: vertical; min-height: 48px; font: inherit; line-height: 1.4; }
.prow.about .pv { white-space: pre-wrap; }

/* divider between the chat folders (Все/Группы/Каналы) and Контакты in the rail */
.rail-sep { height: 1px; background: var(--border); margin: 6px 8px; }

/* section header in the chat list (e.g. "Мои каналы" / "Рекомендуемые") */
.list-section {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--faint); padding: 14px 14px 6px;
}
.list-section:first-child { padding-top: 8px; }

/* ============================================================================
   New MAX features: inline keyboard, reactions, comments, stickers/animoji
   ========================================================================== */

/* --- inline keyboard (link buttons under a post) --- */
.post-kbd { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.post-kbd-row { display: flex; gap: 6px; }
.post-kbd-btn {
  flex: 1; text-align: center; padding: 9px 12px; border-radius: var(--r-sm);
  background: var(--surface-2); color: var(--accent-ink); border: 1px solid var(--border-2);
  font-size: 13px; font-weight: 600; text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.post-kbd-btn:hover { background: var(--surface-3); }

/* --- reactions --- */
/* Reactions flow inline and fill the row ("все подряд"). Only when they actually
   wrap past one row does JS add .rx-grid to rebalance into even rows (6→3+3,
   5→3+2, …) via --rx-cols — see balanceReactions. */
.reactions-bar { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.reactions-bar.rx-grid {
  display: grid; justify-content: start;
  grid-template-columns: repeat(var(--rx-cols, 1), max-content);
}
.reactions-bar.rx-grid .reaction-chip { justify-content: center; }
.reaction-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border-radius: var(--r-pill);
  background: var(--surface-2); border: 1px solid var(--border-2);
  color: var(--ink-soft); font-size: 12px; cursor: pointer; line-height: 1.2;
}
.reaction-chip:hover { background: var(--surface-3); }
.reaction-chip.mine { background: var(--grad-soft); border-color: var(--accent); color: var(--ink); }
.reaction-picker {
  position: fixed; z-index: 1200; display: flex; gap: 2px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border-2);
  border-radius: var(--r-pill); box-shadow: var(--shadow);
}
.rp-emoji {
  border: none; background: transparent; cursor: pointer; font-size: 20px;
  width: 34px; height: 34px; border-radius: var(--r-pill); line-height: 1;
}
.rp-emoji:hover { background: var(--surface-2); transform: scale(1.15); }

/* --- channel-post comment badge + thread overlay --- */
.post-comments {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 8px;
  padding: 6px 12px; border-radius: var(--r-pill);
  background: var(--surface-2); border: 1px solid var(--border-2);
  color: var(--accent-ink); font-size: 12px; font-weight: 600; cursor: pointer;
}
.post-comments:hover { background: var(--surface-3); }
.post-comments .ic { width: 15px; height: 15px; }

/* Post footer: reactions span full width above; the last row holds the comment
   button (left) and views + time (right). Flex column + order keeps this stable
   regardless of the append order of reactions/comments (both added async). */
.bubble.post { display: flex; flex-direction: column; align-items: stretch; }
.bubble.post > .reactions-bar { order: 1; width: 100%; }
.bubble.post > .post-footer   { order: 2; }
.post-footer {
  display: flex; align-items: center; gap: 8px;
  margin-top: 6px; min-height: 22px;
}
.post-footer .post-comments { margin-top: 0; }
.post-footer .msg-meta {
  float: none; margin: 0 0 0 auto; top: 0;  /* pin to the bottom-right */
}

.thread-overlay {
  position: fixed; inset: 0; z-index: 1100; display: flex;
  justify-content: flex-end; background: rgba(0, 0, 0, .45);
}
.thread-overlay.hidden { display: none; }
.thread-panel {
  width: min(440px, 100%); height: 100%; display: flex; flex-direction: column;
  background: var(--bg); border-left: 1px solid var(--border); box-shadow: var(--shadow-lg);
}
.thread-head {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.thread-back {
  border: none; background: transparent; color: var(--ink); cursor: pointer;
  font-size: 20px; width: 32px; height: 32px; border-radius: var(--r-sm);
}
.thread-back:hover { background: var(--surface-2); }
.thread-title { font-weight: 700; font-size: 15px; }
.thread-post {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  color: var(--muted); font-size: 13px;
}
.thread-post-text { max-height: 4.5em; overflow: hidden; }
.thread-msgs { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.thread-empty { color: var(--faint); text-align: center; padding: 20px; }
.cmt {
  max-width: 88%; align-self: flex-start; padding: 7px 11px;
  background: var(--surface-2); border-radius: var(--r-lg); font-size: 14px;
}
.cmt.mine { align-self: flex-end; background: var(--grad-soft); }
.cmt-name { display: block; font-size: 12px; font-weight: 700; color: var(--accent-ink); margin-bottom: 2px; }
.cmt-text { display: block; white-space: pre-wrap; word-break: break-word; }
.cmt-time { display: block; text-align: right; font-size: 10px; color: var(--faint); margin-top: 2px; }
.thread-form { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.thread-form input {
  flex: 1; padding: 10px 14px; border-radius: var(--r-pill);
  background: var(--surface-2); border: 1px solid var(--border-2); color: var(--ink); font: inherit;
}
.thread-form button {
  border: none; background: var(--accent); color: #fff; cursor: pointer;
  width: 42px; border-radius: var(--r-pill); font-size: 16px;
}

/* --- lottie stickers / animoji --- */
.att-lottie { width: 160px; height: 160px; display: inline-block; }
.att-lottie svg, .att-lottie img { width: 100%; height: 100%; }
/* animoji: small inline glyph rendered inside message text */
.att-animoji { width: 1.5em; height: 1.5em; display: inline-block; vertical-align: middle; }
.att-animoji svg { width: 100%; height: 100%; }

/* --- sticker picker panel --- */
.sticker-panel {
  position: fixed; z-index: 1150; width: 420px; max-width: 94vw; max-height: 520px;
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border-2);
  border-radius: var(--r-lg); box-shadow: var(--shadow);
}
.sp-group-title { font-size: 13px; font-weight: 700; color: var(--ink); padding: 6px 2px 2px; }
.sticker-panel.hidden { display: none; }
/* top row of set icons (jump-to-section) */
.sp-tabs {
  display: flex; gap: 4px; padding: 8px; flex-shrink: 0;
  overflow-x: auto; border-bottom: 1px solid var(--border);
  user-select: none; scrollbar-width: none;
}
.sp-tabs::-webkit-scrollbar { display: none; }
.sp-tabs.hidden { display: none; }
.sp-tab {
  width: 36px; height: 36px; flex-shrink: 0; padding: 4px;
  border: none; background: transparent; border-radius: var(--r-sm); cursor: pointer;
}
.sp-tab:hover, .sp-tab.active { background: var(--surface-2); }
.sp-tab img { width: 100%; height: 100%; object-fit: contain; -webkit-user-drag: none; user-select: none; pointer-events: none; }
.sp-tab-uni { font-size: 22px; line-height: 1; display: flex; align-items: center; justify-content: center; }
.sp-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 8px; min-width: 0; }
.sp-section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 10px 2px 8px; }
.sp-section:first-child .sp-section-head { margin-top: 2px; }
.sp-section-titles { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.sp-section-title { font-size: 13px; font-weight: 700; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sp-section-count { font-size: 11px; color: var(--muted); }
.sp-add {
  flex-shrink: 0; padding: 6px 16px; border-radius: var(--r-pill);
  border: none; background: var(--accent, #3a8fff); color: #fff;
  font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
}
.sp-add:hover { opacity: .9; }
.sp-add.added { background: var(--surface-3); color: var(--muted); cursor: default; }
.sp-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; min-width: 0; }
.sp-item { width: 100%; aspect-ratio: 1; min-width: 0; object-fit: contain; overflow: hidden; cursor: pointer; border-radius: var(--r-sm); }
/* hover cell holds a static <img> by default; its Lottie <svg> (mounted on hover)
   must fill the same square and never overflow (lottie sets inline px sizes). */
.sp-item.sp-hover { display: block; }
.sp-item.sp-hover img, .sp-item.sp-hover svg { width: 100% !important; height: 100% !important; max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
/* scale only where a real pointer hovers — on touch the synthetic hover during a
   scroll would trigger a needless repaint on every cell the finger passes. */
@media (hover: hover) and (pointer: fine) {
  .sp-item:hover { background: var(--surface-2); transform: scale(1.05); }
}
.sp-loading, .sp-empty { text-align: center; color: var(--faint); padding: 24px; font-size: 13px; }
/* Emoji tab */
.sp-emoji { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.sp-emoji.hidden { display: none; }
/* minmax(0,1fr) so wide ZWJ glyphs (e.g. family emoji, multi-glyph on Windows) can't
   blow the tracks out to max-content and push columns off the overflow edge. */
.sp-emoji-grid { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 2px; }
.sp-animoji-grid { grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; }
.sp-emoji-item {
  display: flex; align-items: center; justify-content: center;
  width: 100%; min-width: 0; aspect-ratio: 1; overflow: hidden;
  border: none; background: transparent; cursor: pointer;
  font-size: 22px; line-height: 1; border-radius: var(--r-sm); padding: 0;
}
.sp-emoji-item:hover { background: var(--surface-2); }
/* bottom Emoji / Stickers switch */
.sp-foot { flex-shrink: 0; display: flex; gap: 6px; justify-content: center; padding: 6px; border-top: 1px solid var(--border); }
.sp-mode {
  padding: 6px 18px; border: none; border-radius: var(--r-pill); cursor: pointer;
  background: transparent; color: var(--muted); font: inherit; font-size: 13px; font-weight: 600;
}
.sp-mode.active { background: var(--surface-3); color: var(--ink); }

/* picker → full-width bottom sheet on phones. Placed AFTER the base .sticker-panel
   rule so it wins by source order (media queries don't add specificity); the
   !important on the offsets also defeats any leftover inline coords from the
   desktop positioner. Centered by spanning edge-to-edge, and height-capped so it
   always fits the screen. */
@media (max-width: 720px) {
  .sticker-panel {
    left: 0 !important; right: 0 !important; top: auto !important; bottom: 0 !important;
    width: auto; max-width: 100vw; max-height: 70vh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-left: none; border-right: none; border-bottom: none;
    padding-bottom: var(--safe-b);
    animation: sheet-up .24s ease;
  }
  .sp-scroll, .sp-emoji { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
  .sp-tabs { -webkit-overflow-scrolling: touch; }
  .sp-tab { width: 40px; height: 40px; }
  .sp-emoji-grid, .sp-animoji-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); }
  .sp-emoji-item { font-size: 26px; }
}

/* light theme overrides */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .thread-panel,
  :root:not([data-theme="dark"]) .sticker-panel { background: #fff; }
}
:root[data-theme="light"] .thread-panel,
:root[data-theme="light"] .sticker-panel { background: #fff; }
