/* ─── Telegram Desktop "Night" faithful theme ─────────────────────────────── */
:root {
  /* dark (default) */
  --rail-bg: #232e3c;
  --side-bg: #17212b;
  --side-hover: #202b36;
  --side-active: #2b5278;
  --chat-bg: #0e1621;
  --header-bg: #17212b;
  --border: #101921;
  --divider: #0f1a24;

  --text: #ffffff;
  --text2: #708499;
  --text3: #5b6b7b;
  --accent: #5288c1;
  --accent-hover: #6699cc;
  --link: #5eb5f7;

  --bubble-in: #182533;
  --bubble-out: #2b5278;
  --bubble-in-text: #ffffff;
  --bubble-out-text: #ffffff;
  --time-in: #6d7f8f;
  --time-out: #7da8d3;

  --online: #4dcd5e;
  --badge: #5288c1;
  --badge-muted: #4d5c6b;

  --radius-bubble: 12px;
  --radius-tail: 6px;
}

[data-theme="light"] {
  --rail-bg: #4b7bab;
  --side-bg: #ffffff;
  --side-hover: #f4f4f5;
  --side-active: #419fd9;
  --chat-bg: #cfdbe6;
  --header-bg: #ffffff;
  --border: #e4e8eb;
  --divider: #ebedf0;

  --text: #000000;
  --text2: #707579;
  --text3: #a8b0b8;
  --accent: #3390ec;
  --accent-hover: #4fa0f0;
  --link: #3390ec;

  --bubble-in: #ffffff;
  --bubble-out: #eeffde;
  --bubble-in-text: #000000;
  --bubble-out-text: #000000;
  --time-in: #a1aab3;
  --time-out: #6cc264;

  --online: #4dcd5e;
  --badge: #3390ec;
  --badge-muted: #b6bcc3;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", sans-serif;
  background: var(--chat-bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  height: 100vh;
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: rgba(120,140,160,0.25); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: rgba(120,140,160,0.45); }

/* ─── ACCOUNT RAIL ───────────────────────────────────────────────────────── */
.rail {
  width: 72px;
  min-width: 72px;
  background: var(--rail-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 0;
  gap: 10px;
}

.rail-accounts {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  flex: 1;
  overflow-y: auto;
  width: 100%;
  padding: 2px 0;
}
.rail-accounts::-webkit-scrollbar { width: 0; }

.rail-acc {
  position: relative;
  width: 48px; height: 48px;
  border-radius: 50%;
  cursor: pointer;
  transition: border-radius 0.18s;
  flex-shrink: 0;
}
.rail-acc-img {
  width: 48px; height: 48px;
  border-radius: 50%;
  object-fit: cover;
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #72d5a3, #5288c1);
  color: #fff; font-weight: 600; font-size: 18px;
  transition: border-radius 0.18s;
}
.rail-acc-img img { width: 100%; height: 100%; object-fit: cover; }

/* active account: pill highlight on the left + rounded square */
.rail-acc.active::before {
  content: '';
  position: absolute;
  left: -12px; top: 50%;
  transform: translateY(-50%);
  width: 4px; height: 36px;
  background: #fff;
  border-radius: 0 4px 4px 0;
}
.rail-acc.active .rail-acc-img { border-radius: 16px; }
.rail-acc:hover .rail-acc-img { border-radius: 16px; }

.rail-acc-dot {
  position: absolute;
  right: 0; bottom: 0;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--text3);
  border: 2.5px solid var(--rail-bg);
}
.rail-acc-dot.online { background: var(--online); }

.rail-add, .rail-icon {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: none;
  color: rgba(255,255,255,0.85);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
  font-size: 12px; font-weight: 700;
}
.rail-add:hover, .rail-icon:hover { background: rgba(255,255,255,0.18); }

.rail-bottom {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

/* ─── CHAT LIST COLUMN ───────────────────────────────────────────────────── */
.chatlist {
  width: 320px;
  min-width: 320px;
  background: var(--side-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chatlist-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--divider);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.active-account {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px;
}
.aa-avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  background: linear-gradient(135deg, #72d5a3, #5288c1);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 15px;
}
.aa-avatar img { width: 100%; height: 100%; object-fit: cover; }
.aa-info { min-width: 0; flex: 1; }
.aa-name {
  font-weight: 600; font-size: 15px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.aa-sub { font-size: 12px; color: var(--accent); }

.search-box {
  position: relative;
  display: flex;
  align-items: center;
}
.search-icon {
  position: absolute;
  left: 12px;
  color: var(--text2);
  pointer-events: none;
}
#search-input {
  width: 100%;
  background: var(--chat-bg);
  border: none;
  border-radius: 18px;
  color: var(--text);
  padding: 9px 12px 9px 38px;
  font-size: 14px;
  outline: none;
  transition: box-shadow 0.15s;
}
[data-theme="light"] #search-input { background: #f4f4f5; }
#search-input:focus { box-shadow: inset 0 0 0 1.5px var(--accent); }
#search-input::placeholder { color: var(--text2); }

.chat-tabs {
  display: flex;
  border-bottom: 1px solid var(--divider);
  padding: 0 6px;
  gap: 2px;
}
.chat-tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text2);
  font-size: 13px;
  font-weight: 600;
  padding: 10px 4px;
  cursor: pointer;
  position: relative;
  transition: color 0.15s;
  white-space: nowrap;
}
.chat-tab:hover { color: var(--text); }
.chat-tab.active { color: var(--accent); }
.chat-tab.active::after {
  content: '';
  position: absolute;
  left: 10%; right: 10%;
  bottom: -1px;
  height: 2.5px;
  background: var(--accent);
  border-radius: 3px 3px 0 0;
}

.dialogs-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.dialog-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background 0.1s;
}
.dialog-item:hover { background: var(--side-hover); }
.dialog-item.active { background: var(--side-active); }
.dialog-item.active .dialog-name,
.dialog-item.active .dialog-preview,
.dialog-item.active .dialog-date { color: #fff; }
.dialog-item.active .dialog-preview { opacity: 0.85; }

.dialog-avatar {
  width: 54px; height: 54px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 600; color: #fff;
  overflow: hidden;
}
.dialog-avatar img { width: 100%; height: 100%; object-fit: cover; }
/* deterministic gradient palette by index */
.grad-0 { background: linear-gradient(135deg, #ff885e, #ff516a); }
.grad-1 { background: linear-gradient(135deg, #ffcd6a, #ffa85c); }
.grad-2 { background: linear-gradient(135deg, #82b1ff, #665fff); }
.grad-3 { background: linear-gradient(135deg, #a0de7e, #54cb68); }
.grad-4 { background: linear-gradient(135deg, #53edd6, #28c9b7); }
.grad-5 { background: linear-gradient(135deg, #72d5fd, #2a9ef1); }
.grad-6 { background: linear-gradient(135deg, #e0a2f3, #d669ed); }

.dialog-body { flex: 1; min-width: 0; }
.dialog-row1 {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 8px;
}
.dialog-name {
  font-size: 15px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dialog-date { font-size: 12px; color: var(--text3); flex-shrink: 0; }
.dialog-row2 {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin-top: 3px;
}
.dialog-preview {
  font-size: 14px; color: var(--text2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1;
}
.dialog-unread {
  background: var(--badge);
  color: #fff;
  font-size: 12px; font-weight: 600;
  min-width: 20px; height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.dialog-item.active .dialog-unread { background: #fff; color: var(--side-active); }

.list-hint { color: var(--text3); text-align: center; padding: 24px 12px; font-size: 13px; }

/* ─── CHAT AREA ──────────────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--chat-bg);
  position: relative;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 20px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--divider);
  min-height: 56px;
  z-index: 3;
}
.ch-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 600; color: #fff;
}
.ch-avatar img { width: 100%; height: 100%; object-fit: cover; }
.ch-info { flex: 1; min-width: 0; }
.ch-title {
  font-size: 15px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ch-status { font-size: 13px; color: var(--text2); }
.ch-status.online { color: var(--accent); }
.ch-actions { display: flex; gap: 4px; }
.ch-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: none; border: none;
  color: var(--text2);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.ch-icon:hover { background: var(--side-hover); }

/* messages area with wallpaper */
.messages-wrap {
  flex: 1;
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}
.messages-wrap::before {
  content: '';
  position: absolute;
  inset: 0;
  background-color: var(--chat-bg);
  background-image: radial-gradient(circle, rgba(130,150,170,0.045) 1px, transparent 1.4px);
  background-size: 22px 22px;
  pointer-events: none;
  z-index: 0;
}
[data-theme="light"] .messages-wrap::before {
  background-image: radial-gradient(circle, rgba(90,120,150,0.10) 1px, transparent 1.4px);
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 18px 6%;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
  z-index: 1;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  pointer-events: none;
}
.empty-pill {
  background: rgba(16,26,36,0.6);
  color: #d5e0ea;
  padding: 8px 18px;
  border-radius: 16px;
  font-size: 14px;
  backdrop-filter: blur(4px);
}
[data-theme="light"] .empty-pill {
  background: rgba(255,255,255,0.55);
  color: #4a6072;
}

/* message bubbles */
.message {
  max-width: 72%;
  padding: 6px 10px 7px;
  border-radius: var(--radius-bubble);
  font-size: 14.5px;
  line-height: 1.35;
  position: relative;
  word-break: break-word;
  margin-top: 2px;
}
.message.in {
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  align-self: flex-start;
  border-bottom-left-radius: var(--radius-tail);
  box-shadow: 0 1px 1px rgba(0,0,0,0.12);
}
.message.out {
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  align-self: flex-end;
  border-bottom-right-radius: var(--radius-tail);
  box-shadow: 0 1px 1px rgba(0,0,0,0.12);
}
/* group spacing: first of a run gets more top margin */
.message.group-start { margin-top: 10px; }

.message-sender {
  font-size: 13px; font-weight: 600;
  margin-bottom: 2px;
  color: #5eb5f7;
}
.msg-sender-c0 { color:#ff8e86 } .msg-sender-c1 { color:#ffb54d }
.msg-sender-c2 { color:#7fc9ff } .msg-sender-c3 { color:#87d474 }
.msg-sender-c4 { color:#4fd6c0 } .msg-sender-c5 { color:#c99ef2 }

.message-text { white-space: pre-wrap; }
.message-text a { color: var(--link); }

.media-wrap {
  position: relative;
  margin: -2px -4px 4px;
  border-radius: 9px;
  overflow: hidden;
  max-width: 340px;
  line-height: 0;
}
.message-media {
  display: block;
  width: 100%;
  max-width: 340px;
  max-height: 380px;
  object-fit: cover;
  cursor: pointer;
}
.media-play {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 48px; height: 48px;
  border-radius: 50%;
  background: rgba(0,0,0,0.45);
  color: #fff;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(2px);
  padding-left: 3px;
}
/* stickers: no bubble */
.message.sticker-msg {
  background: none !important;
  box-shadow: none !important;
  padding: 0;
  max-width: 60%;
}
.message.sticker-msg .message-time {
  color: var(--text2);
  background: rgba(16,26,36,0.5);
  padding: 1px 6px; border-radius: 8px;
  float: none; display: inline-block; margin-top: 4px;
}
.message-sticker {
  display: block;
  width: 160px; height: auto;
  margin-bottom: 2px;
}
.media-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; opacity: 0.85; font-style: italic;
}
.file-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 4px 8px;
}
.file-icon {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; flex-shrink: 0;
}
.file-meta { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.file-size { font-size: 12px; color: var(--time-in); }
.message.out .file-size { color: var(--time-out); }

.message-time {
  font-size: 11px;
  float: right;
  margin: 6px 0 -2px 8px;
  position: relative;
  top: 3px;
}
.message.in .message-time { color: var(--time-in); }
.message.out .message-time { color: var(--time-out); }

.load-more-btn {
  align-self: center;
  background: rgba(16,26,36,0.5);
  border: none;
  color: #c5d2de;
  padding: 5px 16px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 12px;
  margin: 4px 0 8px;
  z-index: 1;
}
.load-more-btn:hover { background: rgba(16,26,36,0.75); }

.date-sep {
  align-self: center;
  background: rgba(16,26,36,0.5);
  color: #c5d2de;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  margin: 8px 0;
  z-index: 1;
}
[data-theme="light"] .date-sep { background: rgba(255,255,255,0.6); color: #4a6072; }

/* ─── COMPOSER ───────────────────────────────────────────────────────────── */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 20px 14px;
  background: var(--header-bg);
  border-top: 1px solid var(--divider);
  z-index: 3;
}
.composer-icon {
  background: none; border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.15s;
  flex-shrink: 0;
}
.composer-icon:hover { color: var(--accent); }

.file-preview {
  font-size: 13px;
  color: var(--accent);
  padding: 6px 10px;
  background: var(--side-hover);
  border-radius: 10px;
  display: flex; align-items: center; gap: 6px;
  align-self: center;
}
.file-preview.hidden { display: none; }
.file-preview-remove { cursor: pointer; color: #e0554f; font-weight: 700; }

textarea#msg-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 10px 4px;
  font-size: 15px;
  resize: none;
  outline: none;
  max-height: 140px;
  line-height: 1.4;
  font-family: inherit;
}
textarea#msg-input::placeholder { color: var(--text2); }

.send-btn {
  background: none;
  border: none;
  color: var(--accent);
  width: 42px; height: 42px;
  border-radius: 50%;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--side-hover); }
.send-btn:disabled { opacity: 0.4; cursor: default; }

/* ─── MODALS ─────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--side-bg);
  border-radius: 12px;
  padding: 20px 22px;
  width: 380px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.4);
}
.modal-title { font-weight: 600; font-size: 18px; color: var(--text); }

.modal input[type="text"] {
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--accent);
  color: var(--text);
  padding: 8px 2px;
  font-size: 15px;
  outline: none;
  width: 100%;
}
.modal input[type="text"]::placeholder { color: var(--text2); }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.btn-text {
  background: none; border: none;
  color: var(--text2);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px; font-weight: 600;
  text-transform: uppercase;
  transition: background 0.15s;
}
.btn-text:hover { background: var(--side-hover); }
.btn-accent { color: var(--accent); }

.upload-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--chat-bg);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  transition: background 0.15s;
}
[data-theme="light"] .upload-label { background: #f4f4f5; }
.upload-label:hover { background: var(--side-hover); }
.upload-label input { display: none; }
.upload-name { color: var(--accent); font-size: 13px; max-width: 170px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.error-msg {
  color: #e0554f;
  font-size: 13px;
  background: rgba(224,85,79,0.1);
  padding: 8px 12px;
  border-radius: 8px;
}
.error-msg.hidden { display: none; }

.hidden { display: none !important; }

@media (max-width: 720px) {
  .chatlist { width: 260px; min-width: 260px; }
  .message { max-width: 85%; }
  .messages-container { padding: 14px 12px; }
}
