/* Telegram Web K — light theme clone */
:root {
  --tg-sidebar-w: 420px;
  --tg-header-h: 56px;
  --tg-peer-bg: #99ba92;
  --tg-peer-pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200' viewBox='0 0 200 200'%3E%3Cg fill='%23ffffff' fill-opacity='0.08'%3E%3Ccircle cx='30' cy='40' r='8'/%3E%3Crect x='80' y='20' width='14' height='14' rx='3' transform='rotate(15 87 27)'/%3E%3Cpath d='M150 50l10 6-10 6-6-10z'/%3E%3Ccircle cx='40' cy='120' r='6'/%3E%3Crect x='120' y='100' width='12' height='12' rx='2'/%3E%3Cpath d='M170 140c0 8-6 14-14 14s-14-6-14-14 6-14 14-14 14 6 14 14z'/%3E%3Ccircle cx='90' cy='170' r='7'/%3E%3Crect x='20' y='160' width='10' height='10' rx='2' transform='rotate(-20 25 165)'/%3E%3C/g%3E%3C/svg%3E");
  --tg-active: rgba(51, 144, 236, 0.08);
  --tg-hover: #f4f4f5;
  --tg-text: #000;
  --tg-text-secondary: #707579;
  --tg-border: #dadce0;
  --tg-input-bg: #f4f4f5;
}

.tg-app {
  display: flex;
  height: 100%;
  background: #fff;
  position: relative;
  overflow: hidden;
}

/* ── Sidebar ── */
.tg-sidebar {
  width: 100%;
  max-width: var(--tg-sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--tg-border);
  background: #fff;
  z-index: 2;
}

.tg-sidebar-top {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: calc(6px + var(--safe-top)) 8px 8px;
  background: #fff;
}

.sidebar-account {
  flex: 0 0 auto;
  padding: 0 14px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--blue);
  background: #fff;
  border-bottom: 1px solid var(--tg-border);
}

.tg-search-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--tg-input-bg);
  border-radius: 22px;
  padding: 0 12px;
  min-height: 42px;
}

.tg-search-wrap svg {
  flex-shrink: 0;
  color: var(--tg-text-secondary);
  opacity: 0.65;
}

.tg-search {
  flex: 1;
  border: 0;
  background: transparent;
  font-size: 15px;
  outline: none;
  color: var(--tg-text);
  min-width: 0;
}

.tg-search::placeholder { color: var(--tg-text-secondary); }

.tg-icon-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--tg-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.tg-icon-btn:hover { background: var(--tg-hover); }
.tg-icon-btn:active { background: #ebebeb; }
.tg-icon-btn svg { display: block; }

.tg-sidebar .chat-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tg-sidebar .chat-item {
  display: flex;
  gap: 12px;
  padding: 8px 10px 8px 12px;
  min-height: 72px;
  align-items: center;
  cursor: pointer;
  border-bottom: none;
  transition: background 0.1s;
}

.tg-sidebar .chat-item:hover { background: var(--tg-hover); }
.tg-sidebar .chat-item.active { background: var(--tg-active); }

.tg-sidebar .avatar {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  font-size: 20px;
  font-weight: 500;
  letter-spacing: -0.5px;
}

.tg-sidebar .chat-title {
  font-size: 16px;
  font-weight: 500;
  color: var(--tg-text);
}

.tg-sidebar .chat-preview {
  font-size: 15px;
  color: var(--tg-text-secondary);
  margin-top: 3px;
}

.tg-sidebar .chat-time {
  font-size: 12px;
  color: var(--tg-text-secondary);
}

.tg-sidebar .badge {
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

/* ── Main / empty ── */
.tg-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--tg-peer-bg);
  background-image: var(--tg-peer-pattern);
  position: relative;
}

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px 24px;
  text-align: center;
  background: #fff;
  background-image: none;
}

.chat-empty .empty-logo {
  width: 160px;
  height: 160px;
  opacity: 0.35;
  object-fit: contain;
  background: transparent !important;
}

.chat-empty h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 400;
  color: var(--tg-text);
  letter-spacing: -0.3px;
}

.chat-empty p {
  margin: 0;
  font-size: 14px;
  color: var(--tg-text-secondary);
  max-width: 280px;
  line-height: 1.45;
}

.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-pane.hidden { display: none !important; }

/* ── Chat header (white, like TG Web) ── */
.tg-chat-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: var(--tg-header-h);
  padding: calc(4px + var(--safe-top)) 8px 4px 4px;
  background: #fff;
  border-bottom: 1px solid var(--tg-border);
  color: var(--tg-text);
  z-index: 3;
}

.tg-chat-header .tg-icon-btn { color: var(--tg-text-secondary); }

.tg-chat-header-main {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}

.tg-chat-header-main:hover { background: var(--tg-hover); }

.tg-chat-header-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 500;
  flex-shrink: 0;
}

.tg-chat-header-text { min-width: 0; }

.tg-chat-header-name {
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--tg-text);
}

.tg-chat-header-status {
  font-size: 13px;
  color: var(--tg-text-secondary);
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tg-chat-header-status.online { color: var(--blue); }

.tg-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

/* ── Messages ── */
.tg-main .messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 12px;
  background: transparent;
  -webkit-overflow-scrolling: touch;
}

.typing-bar {
  flex: 0 0 auto;
  padding: 2px 16px 6px;
  font-size: 13px;
  color: var(--blue);
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(4px);
}

.typing-bar.hidden { display: none !important; }

.date-sep {
  display: flex;
  justify-content: center;
  margin: 10px 0 6px;
  pointer-events: none;
}

.date-sep span {
  background: rgba(255,255,255,.88);
  color: var(--tg-text-secondary);
  font-size: 13px;
  font-weight: 500;
  padding: 4px 11px;
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
}

/* Bubbles — TG shape: входящие слева (белые), исходящие справа (зелёные) */
.bubble-row {
  display: flex;
  width: 100%;
  margin-bottom: 3px;
  padding: 0 10px;
  box-sizing: border-box;
}
.bubble-row.in { justify-content: flex-start; }
.bubble-row.out { justify-content: flex-end; }
.bubble-row.in + .bubble-row.in .bubble { border-top-left-radius: 12px; }
.bubble-row.in .bubble:not(:first-child) { border-top-left-radius: 12px; }

.bubble {
  max-width: min(82vw, 320px);
  padding: 7px 11px 6px;
  border-radius: 16px;
  box-shadow: 0 1px 1px rgba(0,0,0,.08);
  word-wrap: break-word;
  position: relative;
}

.bubble.has-media { padding: 3px; max-width: min(82vw, 280px); }
.bubble.in {
  background: #ffffff !important;
  border-bottom-left-radius: 4px;
  margin-right: 48px;
  border: 1px solid rgba(0,0,0,.06);
}
.bubble.out {
  background: #d9f7a7 !important;
  border-bottom-right-radius: 4px;
  margin-left: 48px;
  border: 1px solid rgba(76,140,46,.25);
}
.bubble-row.out + .bubble-row.out .bubble { border-top-right-radius: 12px; }

.bubble .sender {
  font-size: 13px;
  font-weight: 500;
  color: var(--blue);
  margin: 2px 4px 1px;
}

.bubble .text {
  font-size: 15px;
  line-height: 1.3125;
  white-space: pre-wrap;
  padding: 0 2px;
  color: var(--tg-text);
}

.bubble.deleted .text { font-style: italic; color: var(--tg-text-secondary); }

.bubble .time-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
  margin-top: 2px;
  padding: 0 2px;
  float: right;
  margin-left: 8px;
  position: relative;
  top: 4px;
}

.bubble .time-row .time {
  font-size: 11px;
  color: var(--tg-text-secondary);
  margin: 0;
}

.bubble .time.media-time {
  position: absolute;
  right: 6px;
  bottom: 5px;
  margin: 0;
  padding: 2px 6px 2px 8px;
  border-radius: 12px;
  background: rgba(0,0,0,.45);
  color: #fff;
  font-size: 11px;
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 3px;
}

.read-check { font-size: 15px; line-height: 1; color: var(--tg-text-secondary); opacity: 0.7; }
.read-check.read { color: #4fae4e; opacity: 1; }
.msg-status { display: inline-flex; align-items: center; margin-left: 3px; line-height: 1; vertical-align: middle; }
.msg-status .ck { font-size: 14px; font-style: normal; color: #8d969c; }
.msg-status .ck2 { margin-left: -7px; }
.msg-status.read .ck { color: #4fae4e; }
.msg-status.sending { color: var(--blue); font-size: 13px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.tg-chat-header-status.online { color: var(--blue); }
.typing-bar { color: var(--blue); font-weight: 500; }

/* ── Composer (TG Web style) ── */
.tg-composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: #fff;
  border-top: 1px solid var(--tg-border);
  position: relative;
}

.tg-composer-field {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  background: var(--tg-input-bg);
  border-radius: 22px;
  padding: 4px 4px 4px 2px;
  min-height: 44px;
}

.tg-composer-field textarea {
  flex: 1;
  resize: none;
  border: 0;
  background: transparent;
  padding: 10px 4px;
  font-size: 15px;
  font-family: inherit;
  max-height: 120px;
  min-height: 36px;
  outline: none;
  line-height: 1.3125;
}

.tg-composer-field .tg-icon-btn {
  width: 36px;
  height: 36px;
  margin-bottom: 2px;
  color: var(--tg-text-secondary);
}

.tg-composer-action {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition: background 0.15s, transform 0.1s;
}

.tg-composer-action:hover { background: var(--blue-dark); }
.tg-composer-action:active { transform: scale(0.95); }
.tg-composer-action.mic-mode { background: transparent; color: var(--tg-text-secondary); }
.tg-composer-action.mic-mode:hover { background: var(--tg-hover); }
.tg-composer-action.recording { background: #e53935 !important; color: #fff !important; animation: mbc-pulse 1s ease-in-out infinite; }
@keyframes mbc-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.75; } }
.bubble-audio audio { width: min(240px, 100%); height: 36px; display: block; }
.tg-composer-action:disabled { opacity: 0.4; cursor: default; }

/* Attach popup */
.attach-menu {
  position: absolute;
  bottom: calc(100% + 4px);
  left: 12px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 16px rgba(0,0,0,.16);
  padding: 8px;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  min-width: 240px;
}

.attach-menu.hidden { display: none !important; }

.attach-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border: 0;
  background: transparent;
  padding: 12px 8px;
  font-size: 12px;
  color: var(--tg-text-secondary);
  cursor: pointer;
  border-radius: 10px;
}

.attach-item:hover { background: var(--tg-hover); }

.attach-item .attach-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.attach-item[data-kind="photo"] .attach-icon { background: #e3f2fd; }
.attach-item[data-kind="video"] .attach-icon { background: #fce4ec; }
.attach-item[data-kind="file"] .attach-icon { background: #f3e5f5; }

/* Context menu */
.msg-ctx {
  position: fixed;
  z-index: 50;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 24px rgba(0,0,0,.18);
  overflow: hidden;
  min-width: 180px;
  padding: 6px 0;
}

.msg-ctx button {
  display: block;
  width: 100%;
  border: 0;
  background: #fff;
  padding: 11px 20px;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
  color: var(--tg-text);
}

.msg-ctx button.danger { color: var(--danger); }
.msg-ctx button:hover { background: var(--tg-hover); }

.chat-item.hidden-by-search { display: none !important; }

/* ── Drawer (hamburger menu) ── */
.drawer-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 20;
  opacity: 1;
  transition: opacity 0.2s;
}

.drawer-overlay.hidden {
  display: none !important;
  opacity: 0;
  pointer-events: none;
}

.tg-drawer {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: #fff;
  z-index: 25;
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 2px 0 12px rgba(0,0,0,.12);
  padding-top: var(--safe-top);
}

.tg-drawer.closed {
  transform: translateX(-105%);
  pointer-events: none;
}

.drawer-profile {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 20px 18px 24px;
  background: var(--blue);
  color: #fff;
  cursor: pointer;
}

.drawer-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255,255,255,.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 500;
  flex-shrink: 0;
}

.drawer-profile-name { font-size: 17px; font-weight: 500; }
.drawer-profile-login { font-size: 14px; opacity: 0.85; margin-top: 2px; }

.drawer-nav { flex: 1; overflow-y: auto; padding: 8px 0; }

.drawer-item {
  display: flex;
  align-items: center;
  gap: 18px;
  width: 100%;
  border: 0;
  background: transparent;
  padding: 14px 22px;
  font-size: 15px;
  color: var(--tg-text);
  cursor: pointer;
  text-align: left;
}

.drawer-item:hover { background: var(--tg-hover); }
.drawer-item svg { color: var(--tg-text-secondary); flex-shrink: 0; }
.drawer-item.danger { color: var(--danger); }

/* Settings panel */
#screen-settings.settings-panel {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: #fff;
  display: none;
  flex-direction: column;
}

#screen-settings.settings-panel.active { display: flex; }

#screen-settings .topbar {
  background: #fff;
  color: var(--tg-text);
  border-bottom: 1px solid var(--tg-border);
}

#screen-settings .topbar .icon-btn { color: var(--tg-text-secondary); }
#screen-settings .topbar h1 { font-size: 17px; font-weight: 500; }

/* Responsive */
@media (min-width: 769px) {
  .tg-sidebar { width: var(--tg-sidebar-w); max-width: 33vw; }
  #back-btn { display: none !important; }
  .chat-empty { display: flex !important; }
  .tg-app.chat-open .chat-empty { display: none !important; }
}

@media (max-width: 768px) {
  .tg-sidebar { max-width: 100%; width: 100%; }
  .tg-app.chat-open .tg-sidebar { display: none; }
  .chat-empty { display: none !important; }
  .tg-app:not(.chat-open) .tg-main { display: none; }
  .tg-drawer { width: min(300px, 90vw); }
}

/* In-app message alert (foreground) */
.mbc-toast {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 12px;
  right: 12px;
  z-index: 9999;
  pointer-events: auto;
  animation: mbc-toast-in 0.25s ease;
}
.mbc-toast.hidden { display: none !important; }
.mbc-toast-inner {
  background: #fff;
  border-left: 4px solid var(--blue);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,.18);
  cursor: pointer;
}
.mbc-toast-inner strong {
  display: block;
  font-size: 14px;
  color: var(--blue);
  margin-bottom: 2px;
}
.mbc-toast-inner span {
  display: block;
  font-size: 15px;
  color: #111;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes mbc-toast-in {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}
