/* ==========================================================================
   SecureChat Pro - Mobile Bottom Nav vs Desktop Left Rail Architecture
   Plus Chat Pinning, Muting, Starred Favorites & Unified Deep-Dark Palette
   ========================================================================== */

:root,
[data-theme="obsidian"] {
  /* Unified Deep-Dark Theme Palette (WeChat Dark + Telegram + WhatsApp) */
  --palette-deep-bg: #0b0e14;
  --palette-rail-bg: #0e131d;
  --palette-sidebar-bg: #121722;
  --palette-chat-bg: #090c12;
  --palette-card-bg: #161c2a;
  --palette-card-hover: #1c2436;
  --palette-border: rgba(255, 255, 255, 0.08);
  --palette-border-glow: rgba(59, 130, 246, 0.25);
  --palette-text-primary: #f1f5f9;
  --palette-text-secondary: #94a3b8;
  --palette-text-muted: #64748b;
  --palette-accent-blue: #3b82f6;
  --palette-accent-emerald: #10b981;
  --palette-accent-amber: #f59e0b;
  --palette-accent-rose: #ef4444;
  --tg-bar-bg: rgba(18, 23, 34, 0.88);
  --tg-bar-border: rgba(255, 255, 255, 0.12);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="light"] {
  /* Telegram iOS Light Theme Palette */
  --palette-deep-bg: #f2f2f7;
  --palette-rail-bg: #e5e5ea;
  --palette-sidebar-bg: #ffffff;
  --palette-chat-bg: #f2f2f7;
  --palette-card-bg: #ffffff;
  --palette-card-hover: #f8fafc;
  --palette-border: rgba(60, 60, 67, 0.18);
  --palette-border-glow: rgba(0, 122, 255, 0.22);
  --palette-text-primary: #000000;
  --palette-text-secondary: #707579;
  --palette-text-muted: #8e8e93;
  --palette-accent-blue: #007aff;
  --palette-accent-emerald: #34c759;
  --palette-accent-amber: #ff9500;
  --palette-accent-rose: #ff3b30;
  --tg-bar-bg: rgba(249, 249, 249, 0.90);
  --tg-bar-border: rgba(60, 60, 67, 0.22);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="obsidian"]) {
    --palette-deep-bg: #f2f2f7;
    --palette-rail-bg: #e5e5ea;
    --palette-sidebar-bg: #ffffff;
    --palette-chat-bg: #f2f2f7;
    --palette-card-bg: #ffffff;
    --palette-card-hover: #f8fafc;
    --palette-border: rgba(60, 60, 67, 0.18);
    --palette-border-glow: rgba(0, 122, 255, 0.22);
    --palette-text-primary: #000000;
    --palette-text-secondary: #707579;
    --palette-text-muted: #8e8e93;
    --palette-accent-blue: #007aff;
    --palette-accent-emerald: #34c759;
    --palette-accent-amber: #ff9500;
    --palette-accent-rose: #ff3b30;
    --tg-bar-bg: rgba(249, 249, 249, 0.90);
    --tg-bar-border: rgba(60, 60, 67, 0.22);
  }
}

/* ==========================================================================
   1. DESKTOP LEFT VERTICAL ICON RAIL (>= 768px)
   ========================================================================== */
.desktop-left-rail {
  width: 68px;
  min-width: 68px;
  height: 100%;
  background: var(--palette-rail-bg);
  border-right: 1px solid var(--palette-border);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0 14px;
  z-index: 25;
  flex-shrink: 0;
  user-select: none;
}

.rail-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  width: 100%;
}

.rail-avatar-box {
  position: relative;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.rail-avatar-box:hover {
  transform: scale(1.05);
}

.rail-user-avatar {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--palette-accent-blue);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  border: 2px solid rgba(255, 255, 255, 0.12);
}

.rail-status-indicator {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 12px;
  height: 12px;
  border: 2px solid var(--palette-rail-bg);
}

.rail-nav {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  align-items: center;
}

.rail-item {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: transparent;
  border: none;
  color: var(--palette-text-secondary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 0;
}

.rail-item:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--palette-text-primary);
}

.rail-item.active {
  background: rgba(59, 130, 246, 0.16);
  color: var(--palette-accent-blue);
}

.rail-item.active svg {
  stroke: var(--palette-accent-blue);
}

.rail-icon-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rail-label {
  font-size: 10px;
  font-weight: 500;
  margin-top: 2px;
  line-height: 1;
}

.rail-badge {
  position: absolute;
  top: -6px;
  right: -10px;
  background: var(--palette-accent-rose);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 9999px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 5px rgba(239, 68, 68, 0.5);
  border: 2px solid var(--palette-rail-bg);
}

.rail-badge.badge-warning {
  background: var(--palette-accent-amber);
  color: #000;
}

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

.rail-tool-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: transparent;
  border: none;
  color: var(--palette-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.rail-tool-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--palette-text-primary);
}

/* ==========================================================================
   2. MOBILE NATIVE BOTTOM NAVIGATION BAR (< 768px, TELEGRAM iOS SPEC)
   ========================================================================== */
.mobile-bottom-nav {
  display: none; /* Desktop default: hidden */
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(49px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--tg-bar-bg, rgba(18, 23, 34, 0.88));
  border-top: 0.5px solid var(--tg-bar-border, rgba(255, 255, 255, 0.12));
  z-index: 28;
  display: flex;
  justify-content: space-around;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  backdrop-filter: blur(25px) saturate(190%);
  -webkit-backdrop-filter: blur(25px) saturate(190%);
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.22s ease;
}

.mobile-nav-btn {
  flex: 1;
  height: 49px;
  background: transparent;
  border: none;
  color: var(--palette-text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  position: relative;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Telegram signature tap spring micro-interaction */
.mobile-nav-btn .mobile-nav-icon-box {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 24px;
  transition: transform 0.12s cubic-bezier(0.2, 0, 0, 1);
}

.mobile-nav-btn:active .mobile-nav-icon-box {
  transform: scale(0.85);
}

.mobile-nav-btn.tg-tab-bounce .mobile-nav-icon-box {
  animation: tgTabBounce 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes tgTabBounce {
  0% { transform: scale(0.85); }
  42% { transform: scale(1.18); }
  72% { transform: scale(0.96); }
  100% { transform: scale(1); }
}

.mobile-nav-btn.active {
  color: var(--palette-accent-blue);
}

.mobile-nav-btn svg {
  transition: stroke 0.2s ease, fill 0.2s ease, transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.mobile-nav-btn.active svg {
  stroke: var(--palette-accent-blue);
  filter: drop-shadow(0 0 5px rgba(42, 171, 238, 0.35));
}

.mobile-nav-btn.active[data-tab="chats"] svg path {
  fill: var(--palette-accent-blue);
  fill-opacity: 0.2;
}

.mobile-nav-btn.active[data-tab="favorites"] svg polygon {
  fill: #f59e0b;
  stroke: #f59e0b;
}

.mobile-nav-btn.active[data-tab="contacts"] svg circle {
  fill: var(--palette-accent-blue);
  fill-opacity: 0.28;
}

.mobile-nav-btn.active[data-tab="settings"] svg circle {
  fill: var(--palette-accent-blue);
  fill-opacity: 0.32;
}

.mobile-nav-label {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: -0.15px;
  line-height: 1.1;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  color: var(--palette-text-muted);
  transition: color 0.15s ease, font-weight 0.15s ease;
}

.mobile-nav-btn.active .mobile-nav-label {
  color: var(--palette-accent-blue);
  font-weight: 600;
}

.mobile-nav-badge {
  position: absolute;
  top: -5px;
  right: -10px;
  background: var(--palette-accent-rose, #ff3b30);
  color: #ffffff;
  font-size: 10.5px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  line-height: 18px;
  padding: 0 5px;
  border-radius: 9999px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(255, 59, 48, 0.4);
  border: 1.5px solid var(--palette-sidebar-bg);
  animation: tgBadgePop 0.28s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes tgBadgePop {
  0% { transform: scale(0.4); opacity: 0; }
  70% { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

.mobile-nav-badge.badge-warning {
  background: var(--palette-accent-amber);
  color: #000;
}

/* ==========================================================================
   3. RESPONSIVE TRANSITIONS (MOBILE VS DESKTOP)
   ========================================================================== */
@media screen and (max-width: 767px) {
  /* Hide Desktop Rail */
  .desktop-left-rail {
    display: none !important;
  }

  /* Show Mobile Bottom Nav */
  .mobile-bottom-nav {
    display: flex !important;
  }

  /* Smoothly slide out bottom nav when entering a chat */
  .main-wrapper.in-chat .mobile-bottom-nav {
    transform: translateY(100%);
    pointer-events: none;
    opacity: 0;
  }

  /* Give sidebar content space above bottom nav */
  .sidebar-content {
    padding-bottom: calc(64px + var(--safe-bottom)) !important;
  }

  /* Make sure sidebar user bar has mobile touch padding */
  .user-profile-bar {
    padding: 12px 16px;
  }
}

@media screen and (min-width: 768px) {
  /* Always display Desktop Rail */
  .desktop-left-rail {
    display: flex !important;
  }

  /* Never display Mobile Bottom Nav on Desktop */
  .mobile-bottom-nav {
    display: none !important;
  }

  /* Hide redundant profile bar actions that moved to rail */
  .profile-actions {
    display: none !important;
  }
}

/* ==========================================================================
   4. CHAT PINNING & MUTING ENHANCEMENTS
   ========================================================================== */
.chat-conv-item {
  position: relative;
  transition: background 0.2s ease, border 0.2s ease;
}

/* Pinned Conversation Item */
.chat-conv-item.is-pinned {
  background: rgba(59, 130, 246, 0.08) !important;
  border-left: 3px solid var(--palette-accent-blue) !important;
}

.chat-conv-item.is-pinned:hover {
  background: rgba(59, 130, 246, 0.12) !important;
}

.item-pin-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--palette-accent-amber);
  margin-left: 4px;
  font-size: 12px;
  line-height: 1;
}

.item-mute-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--palette-text-muted);
  margin-left: 4px;
  font-size: 12px;
  line-height: 1;
}

/* Mark as Unread indicator dot */
.item-unread-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--palette-accent-rose);
  display: inline-block;
  margin-left: auto;
  flex-shrink: 0;
  box-shadow: 0 0 6px rgba(239, 68, 68, 0.6);
}

/* Conversation More Options Trigger */
.btn-conv-more {
  opacity: 0;
  background: transparent;
  border: none;
  color: var(--palette-text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  transition: all 0.15s ease;
  margin-left: 6px;
}

.chat-conv-item:hover .btn-conv-more,
.chat-conv-item:focus-within .btn-conv-more {
  opacity: 1;
}

.btn-conv-more:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--palette-text-primary);
}

/* Floating Conversation Context Menu */
.conv-context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.05);
  padding: 6px;
  min-width: 170px;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  animation: contextMenuPop 0.12s ease-out;
}

@keyframes contextMenuPop {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.conv-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  background: transparent;
  border: none;
  color: var(--palette-text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s ease, color 0.15s ease;
}

.conv-menu-item:hover {
  background: rgba(59, 130, 246, 0.15);
  color: #fff;
}

.conv-menu-item.danger:hover {
  background: rgba(239, 68, 68, 0.18);
  color: #fca5a5;
}

.conv-menu-divider {
  height: 1px;
  background: var(--palette-border);
  margin: 4px 6px;
}

/* ==========================================================================
   5. STARRED / FAVORITE MESSAGES TAB & CARDS
   ========================================================================== */
#tab-favorites {
  display: none;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
}

#tab-favorites.active {
  display: flex;
}

.favorites-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--palette-border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: rgba(18, 23, 34, 0.5);
}

.favorites-search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 10px;
  padding: 8px 12px;
  color: var(--palette-text-primary);
}

.favorites-search-box input {
  background: transparent;
  border: none;
  color: var(--palette-text-primary);
  font-size: 13px;
  width: 100%;
  outline: none;
}

.favorites-filter-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.fav-chip {
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 9999px;
  color: var(--palette-text-secondary);
  font-size: 12px;
  font-weight: 500;
  padding: 4px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.fav-chip:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.fav-chip.active {
  background: var(--palette-accent-blue);
  border-color: var(--palette-accent-blue);
  color: #fff;
}

.favorites-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  overflow-y: auto;
  flex: 1;
}

.favorite-card {
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.15s ease;
  cursor: default;
}

.favorite-card:hover {
  border-color: var(--palette-border-glow);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  transform: translateY(-1px);
}

.fav-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.fav-card-user {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.fav-user-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.fav-user-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

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

.fav-origin-tag {
  font-size: 11px;
  color: var(--palette-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fav-card-time {
  font-size: 11px;
  color: var(--palette-text-muted);
  flex-shrink: 0;
}

.fav-card-content {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  padding: 10px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--palette-text-primary);
  word-break: break-word;
  max-height: 150px;
  overflow-y: auto;
}

.fav-card-content img.fav-thumb {
  max-width: 100%;
  max-height: 180px;
  border-radius: 6px;
  cursor: pointer;
  object-fit: cover;
}

.fav-card-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.fav-action-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--palette-border);
  border-radius: 6px;
  color: var(--palette-text-secondary);
  font-size: 12px;
  padding: 4px 10px;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.fav-action-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.fav-action-btn.btn-jump-chat {
  color: var(--palette-accent-blue);
  border-color: rgba(59, 130, 246, 0.3);
}

.fav-action-btn.btn-jump-chat:hover {
  background: rgba(59, 130, 246, 0.18);
}

.fav-action-btn.btn-unstar:hover {
  color: var(--palette-accent-rose);
  border-color: rgba(239, 68, 68, 0.3);
}

.favorites-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 20px;
  text-align: center;
  color: var(--palette-text-muted);
  gap: 12px;
}

.favorites-empty svg {
  stroke: var(--palette-text-muted);
  opacity: 0.6;
}

/* Message Hover Action Star button */
.msg-act-btn.btn-act-fav.is-favorited {
  color: var(--palette-accent-amber);
  background: rgba(245, 158, 11, 0.15);
}

/* ==========================================================================
   6. CONTACTS SUB-TABS & SETTINGS PANE
   ========================================================================== */
.contacts-segmented-control {
  display: flex;
  margin: 10px 14px 4px;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
}

.contacts-sub-tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--palette-text-secondary);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.contacts-sub-tab.active {
  background: var(--palette-card-bg);
  color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

/* Settings Pane Styling */
#tab-settings {
  display: none;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
  padding: 16px 14px;
  gap: 16px;
}

#tab-settings.active {
  display: flex;
}

.settings-profile-card {
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 14px;
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.settings-avatar-big {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--palette-accent-blue);
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.settings-user-meta {
  flex: 1;
  min-width: 0;
}

.settings-nickname {
  font-size: 16px;
  font-weight: 700;
  color: var(--palette-text-primary);
}

.settings-handle {
  font-size: 12px;
  color: var(--palette-text-muted);
  margin-top: 2px;
}

.settings-status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  margin-top: 6px;
  color: var(--palette-accent-emerald);
  background: rgba(16, 185, 129, 0.12);
  padding: 2px 8px;
  border-radius: 9999px;
}

.settings-group {
  background: var(--palette-card-bg);
  border: 1px solid var(--palette-border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.settings-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--palette-border);
  background: transparent;
  border-left: none;
  border-right: none;
  border-top: none;
  color: var(--palette-text-primary);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s ease;
  width: 100%;
  text-align: left;
}

.settings-item-row:last-child {
  border-bottom: none;
}

.settings-item-row:hover {
  background: rgba(255, 255, 255, 0.04);
}

.settings-item-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.settings-item-left svg {
  color: var(--palette-text-secondary);
}

.settings-item-right {
  color: var(--palette-text-muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
