.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-dark);
  display: flex; flex-direction: column;
  height: 100%; flex-shrink: 0; z-index: 50;
  overflow: hidden;
  transition: margin-left 0.4s cubic-bezier(0.32,0.72,0,1);
}
[data-theme="light"] .sidebar {
  background: var(--sidebar-light);
}
.sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-width)); border-right: none; }

@media (max-width: 1024px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: var(--sidebar-width);
    margin-left: 0;
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 100;
    padding-top: var(--safe-area-inset-top);
    padding-left: var(--safe-area-inset-left);
  }
  .sidebar.collapsed {
    margin-left: 0;
  }
  .sidebar.mobile-open {
    opacity: 1;
    pointer-events: auto;
    visibility: visible;
  }
}

.sidebar-top { padding: 16px 14px 8px; }

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 2px; margin-bottom: 14px;
}

.logo {
  display: flex; align-items: center; gap: 10px;
  text-decoration: none; color: var(--text-primary);
}
.logo-icon {
  color: var(--accent); display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: var(--radius-sm);
  background: var(--accent-soft);
}
.logo-text { font-size: 22px; font-weight: 800; letter-spacing: -0.3px; white-space: nowrap; }

.collapse-btn { opacity: 0; transition: opacity var(--transition-fast); }
.collapse-icon-desktop { display: block; }
.collapse-icon-mobile { display: none; }
.sidebar:hover .collapse-btn { opacity: 1; }
@media (max-width: 1024px) {
  .collapse-btn { 
    opacity: 1; 
    width: 40px; 
    height: 40px; 
    border-radius: var(--radius-md); 
  }
  .collapse-icon-desktop { display: none; }
  .collapse-icon-mobile { display: block; }
  .collapse-btn svg {
    width: 20px;
    height: 20px;
  }
}

.new-chat-btn {
  width: 100%; display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--surface-2);
  border: 1px solid var(--border-1); border-radius: var(--radius-md);
  color: var(--text-primary); font-size: 16px; font-weight: 500;
  font-family: inherit; cursor: pointer;
  transition: all var(--transition-fast); margin-bottom: 8px;
}
.new-chat-btn:hover { background: var(--surface-3); border-color: var(--border-2); }
.new-chat-btn:active { transform: scale(0.98); }
.new-chat-btn svg { color: var(--text-secondary); }

/* Nav */
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-secondary); font-size: 16px; font-weight: 500;
  background: none; border: none; cursor: pointer;
  white-space: nowrap; font-family: inherit; text-align: left;
}
.nav-item:hover { background: var(--surface-2); color: var(--text-primary); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item.active svg { color: var(--accent); }
.nav-item svg { color: var(--text-tertiary); flex-shrink: 0; }

/* Chat History */
.chat-history { flex: 1; overflow-y: auto; padding: 8px 10px; margin: 0 4px; }
.history-section { margin-bottom: 16px; animation: fadeInUp 0.3s ease both; }
.history-label {
  font-size: 13px; font-weight: 600; color: var(--text-muted);
  padding: 4px 8px 6px; text-transform: uppercase; letter-spacing: 0.6px;
}
.history-list { display: flex; flex-direction: column; gap: 1px; }
.history-item {
  display: flex; flex-direction: column; gap: 4px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--text-secondary); font-size: 14px; cursor: pointer;
  position: relative;
}
.history-item-content {
  display: flex; align-items: center; gap: 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-item-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; font-weight: bold; color: #4b4b4b;
}
[data-theme="dark"] .history-item-title { color: #ededed; }
.history-item-snippet {
  font-size: 14px; color: var(--text-tertiary);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  line-clamp: 2;
  overflow: hidden; line-height: 1.4;
  padding-left: 22px;
}
.history-item:hover { background: var(--surface-2); color: var(--text-primary); }
.history-item:hover .history-item-snippet { color: var(--text-secondary); }
.history-item.active { background: var(--accent-soft); color: var(--text-primary); }
.history-item.active .history-item-snippet { color: var(--text-secondary); }
.history-item.active::before {
  content: ''; position: absolute; left: 0; top: 10px; bottom: 10px;
  width: 3px; background: var(--accent); border-radius: 0 3px 3px 0;
}
.history-item svg { flex-shrink: 0; color: var(--text-muted); opacity: 0.6; }
.history-image-icon { color: var(--accent); opacity: 1; margin-left: auto; }

/* Sidebar Footer */
.sidebar-footer { padding: 12px; border-top: 1px solid var(--border-1); position: relative; padding-bottom: calc(12px + var(--safe-area-inset-bottom)); }
.upgrade-card {
  padding: 14px;
  background: linear-gradient(135deg, rgba(99,102,241,0.08), rgba(139,92,246,0.06));
  border: 1px solid var(--border-1); border-radius: var(--radius-md);
  margin-bottom: 10px; transition: all var(--transition-fast);
}
.upgrade-card:hover { border-color: rgba(99,102,241,0.2); }
.upgrade-card-title {
  display: flex; align-items: center; gap: 6px;
  font-size: 14px; font-weight: 600; color: var(--text-primary); margin-bottom: 3px;
}
.pro-badge {
  font-size: 11px; font-weight: 700; letter-spacing: 0.5px;
  padding: 2px 6px; background: var(--accent); color: white; border-radius: 4px;
}
.upgrade-card-desc { font-size: 13px; color: var(--text-tertiary); line-height: 1.4; }
.upgrade-card-btn {
  width: 100%; padding: 8px; background: var(--accent); border: none;
  border-radius: var(--radius-sm); color: white; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: all var(--transition-fast); font-family: inherit; margin-top: 10px;
}
.upgrade-card-btn:hover { background: var(--accent-hover); transform: translateY(-1px); box-shadow: 0 4px 12px var(--accent-glow); }

/* User Card */
.user-card {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: var(--radius-md);
  cursor: pointer; transition: all var(--transition-fast); border: 1px solid transparent;
  user-select: none;
}
.user-card:hover { background: var(--surface-2); border-color: var(--border-1); }
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--surface-3); display: flex; align-items: center; justify-content: center;
  color: var(--text-tertiary); overflow: hidden; flex-shrink: 0;
}
.user-avatar img { width: 100%; height: 100%; object-fit: cover; }
.user-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.user-name { font-size: 16px; font-weight: 500; color: var(--text-primary); white-space: nowrap; }
.user-badge {
  font-size: 14px; color: var(--text-muted); background: var(--surface-2);
  padding: 1px 6px; border-radius: 4px; width: fit-content;
}
.user-menu-btn { width: 28px; height: 28px; }

/* Expand & Overlay */
.expand-sidebar-btn {
  display: none; position: fixed; left: 12px; top: 10px; z-index: 40;
  width: 36px; height: 36px; border-radius: var(--radius-md);
  background: var(--surface-2); border: 1px solid var(--border-1);
  color: var(--text-secondary); cursor: pointer;
  align-items: center; justify-content: center; transition: all var(--transition-fast);
}
.expand-sidebar-btn:hover { background: var(--surface-3); color: var(--text-primary); }
.sidebar.collapsed ~ .expand-sidebar-btn { display: flex; }

.sidebar-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5); z-index: 45; opacity: 0;
  pointer-events: none;
}
.sidebar-overlay.active { opacity: 1; pointer-events: auto; }

@keyframes userDropdownOpen {
  from { opacity: 0; transform: translateY(6px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.user-dropdown {
  display: none; flex-direction: column;
  position: absolute; bottom: calc(66px + var(--safe-area-inset-bottom)); left: 12px; right: 12px;
  background: var(--surface-2); border: 1px solid var(--border-1);
  border-radius: var(--radius-md); padding: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15); z-index: 60;
}
.user-dropdown.open {
  display: flex;
  animation: userDropdownOpen 0.18s ease;
}
.user-menu-btn.flipped svg {
  transform: rotate(180deg);
}
.user-dropdown-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 12px 10px; border-radius: var(--radius-sm);
  font-size: 15px; color: var(--text-primary);
  background: transparent; border: none; cursor: pointer;
  transition: background var(--transition-fast);
  user-select: none;
}
.user-dropdown-item:hover { background: var(--surface-3); }
.user-dropdown-item--danger { color: var(--danger); }
.user-dropdown-divider {
  height: 1px; background: var(--border-1); margin: 4px 0;
}
