/**
 * ConversationSidebar Component
 * Displays conversation history with status indicators
 * 
 * Dependencies: chat-tokens.css
 */

/* ============================================
   BASE SIDEBAR
   ============================================ */

.sidebar-history {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 270px;
  transform: translateX(-110%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.23s ease, opacity 0.22s ease;
  z-index: var(--chat-z-sidebar);
  background: var(--chat-surface-primary);
  border-right: 1px solid var(--chat-border-primary);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.sidebar-history.open {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}

/* ============================================
   HEADER
   ============================================ */

.sidebar-header {
  display: flex;
  align-items: center;
  gap: var(--chat-space-sm);
  padding: 0.9rem var(--chat-space-lg);
  border-bottom: 1px solid var(--chat-border-primary);
  flex-shrink: 0;
}

.sidebar-header h3 {
  margin: 0;
  font-size: var(--chat-font-size-lg);
  font-weight: var(--chat-font-weight-semibold);
}

/* ============================================
   ACTIONS
   ============================================ */

.sidebar-actions {
  padding: 12px;
  flex-shrink: 0;
}

.new-conv-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  box-sizing: border-box;
  padding: 0.7rem var(--chat-space-lg);
  border: none;
  border-radius: var(--chat-radius-lg);
  background: var(--chat-brand-primary);
  color: var(--chat-bg-primary);
  font-size: 0.875rem;
  font-weight: var(--chat-font-weight-semibold);
  box-shadow: 0 2px 8px rgba(108, 67, 224, 0.15);
  cursor: pointer;
  text-align: left;
  transition: all 0.2s ease;
}

.new-conv-btn .material-symbols-outlined {
  font-size: 18px;
}

.new-conv-btn:hover {
  background: var(--chat-brand-primary);
  box-shadow: 0 4px 12px rgba(108, 67, 224, 0.2);
  transform: translateY(-1px);
}

/* ============================================
   CONVERSATION LIST
   ============================================ */

.sidebar-list {
  padding: 12px;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1;
  min-height: 0;
}

/* ============================================
   HISTORY ITEM
   ============================================ */

.history-item {
  all: unset;
  display: block;
  width: 100%;
  padding: 0.85rem var(--chat-space-lg);
  margin-bottom: var(--chat-space-sm);
  border-radius: var(--chat-radius-lg);
  cursor: pointer;
  transition: var(--chat-transition-all);
  font-size: var(--chat-font-size-base);
  color: var(--chat-text-primary);
  box-sizing: border-box;
  text-align: left;
  background: var(--chat-surface-secondary);
  border: 2px solid transparent;
}

.history-item:hover {
  background: var(--chat-bg-quaternary);
  transform: translateX(2px);
}

.history-item.active {
  background: var(--chat-brand-primary);
  color: var(--chat-bg-primary);
  border-color: var(--chat-brand-primary);
}

/* ============================================
   HISTORY ITEM LAYOUT
   ============================================ */

.history-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--chat-space-sm);
  gap: var(--chat-space-sm);
}

.history-item-status {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: var(--chat-font-size-sm);
  font-weight: var(--chat-font-weight-semibold);
}

.history-item-status .status-icon {
  font-size: 16px;
  font-weight: var(--chat-font-weight-semibold);
}

.history-item-date {
  font-size: 0.7rem;
  opacity: 0.7;
  white-space: nowrap;
}

.history-item-id {
  font-size: 0.65rem;
  font-weight: var(--chat-font-weight-bold);
  padding: 0.15rem 0.4rem;
  border-radius: var(--chat-radius-md);
  background-color: var(--chat-bg-tertiary);
  color: var(--chat-text-secondary);
  white-space: nowrap;
  font-family: monospace;
}

.history-item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--chat-space-sm);
  font-size: var(--chat-font-size-sm);
  opacity: 0.85;
}

.message-count {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.message-count .material-symbols-outlined {
  font-size: 14px;
}

/* ============================================
   STATUS VARIANTS
   ============================================ */

.history-item.open {
  border-left: 3px solid var(--chat-status-success);
}

.history-item.open .status-icon {
  color: var(--chat-status-success);
}

.history-item.open .status-label {
  color: var(--chat-status-success-dark);
}

.history-item.closed {
  border-left: 3px solid var(--chat-text-tertiary);
  opacity: 0.75;
}

.history-item.closed .status-icon {
  color: var(--chat-text-secondary);
}

.history-item.closed .status-label {
  color: var(--chat-text-secondary);
}

/* Active state overrides */
.history-item.active .status-icon,
.history-item.active .status-label,
.history-item.active .history-item-date,
.history-item.active .message-count {
  color: inherit;
  opacity: 1;
}

.history-item.active .history-item-id {
  background-color: rgba(255, 255, 255, 0.2);
  color: grey;
}

.history-item.active {
  opacity: 1;
  border-left-color: var(--chat-bg-primary);
}

/* ============================================
   CHAT LAYOUT INTEGRATION
   ============================================ */

.sidebar-container .sidebar-history {
  position: static;
  transform: none;
  opacity: 1;
  pointer-events: auto;
  width: 100%;
  height: 100%;
  box-shadow: none;
  border: none;
  background: transparent;
  padding: 0;
  margin: 0;
  transition: none;
}

.sidebar-container {
  background: var(--chat-bg-tertiary);
  border-right: 1px solid var(--chat-border-primary);
  box-shadow: var(--chat-shadow-md);
}

.sidebar-container .sidebar-header {
  padding: var(--chat-space-2xl) var(--chat-space-xl) var(--chat-space-lg);
  border-bottom: 1px solid var(--chat-border-primary);
}

.sidebar-container .sidebar-header h3 {
  font-size: var(--chat-font-size-xl);
  font-weight: var(--chat-font-weight-semibold);
  color: #1f2937;
  margin: 0;
  display: flex;
  align-items: center;
  gap: var(--chat-space-sm);
}

.sidebar-container .sidebar-actions {
  padding: var(--chat-space-lg) var(--chat-space-xl);
  border-bottom: 1px solid var(--chat-border-primary);
}

.sidebar-container .sidebar-list {
  padding: var(--chat-space-lg) var(--chat-space-xl);
  max-height: calc(100vh - 350px);
}

.sidebar-container .new-conv-btn {
  background: linear-gradient(
    135deg,
    var(--chat-brand-primary) 0%,
    var(--chat-brand-primary-light) 100%
  );
  color: var(--chat-bg-primary);
  border: none;
  border-radius: var(--chat-radius-lg);
  padding: 0.85rem var(--chat-space-xl);
  font-weight: var(--chat-font-weight-semibold);
  font-size: var(--chat-font-size-md);
  cursor: pointer;
  transition: var(--chat-transition-transform);
  box-shadow: var(--chat-shadow-button);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--chat-space-sm);
}

.sidebar-container .new-conv-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--chat-shadow-button-hover);
  background: linear-gradient(
    135deg,
    var(--chat-brand-primary-hover) 0%,
    #6d4dd0 100%
  );
}

.sidebar-container .new-conv-btn:active {
  transform: translateY(0);
}

.sidebar-container .history-item {
  background: var(--chat-surface-primary);
  border: 2px solid var(--chat-border-primary);
  border-radius: var(--chat-radius-lg);
  padding: 0.9rem var(--chat-space-lg);
  margin-bottom: 0.65rem;
  cursor: pointer;
  transition: var(--chat-transition-all);
  font-size: var(--chat-font-size-base);
  color: #4b5563;
  text-align: left;
  width: 100%;
  box-shadow: var(--chat-shadow-xs);
}

.sidebar-container .history-item:hover {
  background: var(--chat-bg-tertiary);
  border-color: var(--chat-border-secondary);
  transform: translateX(3px);
  box-shadow: var(--chat-shadow-sm);
}

.sidebar-container .history-item.active {
  background: #ede7fd;
  border-color: var(--chat-brand-primary);
  color: var(--chat-brand-primary);
  font-weight: var(--chat-font-weight-medium);
  box-shadow: 0 4px 12px var(--chat-brand-primary-alpha-15);
}

.sidebar-container .history-item.open {
  border-left-width: 4px;
  border-left-color: var(--chat-status-success);
}

.sidebar-container .history-item.closed {
  border-left-width: 4px;
  border-left-color: var(--chat-text-tertiary);
}

.sidebar-container .history-item.active.open {
  border-left-color: var(--chat-status-success);
}

.sidebar-container .history-item.active.closed {
  border-left-color: var(--chat-text-tertiary);
}

.sidebar-history #sidebarToggleClose {
  display: none;
}

/* Scrollbar styling */
.sidebar-container .sidebar-list::-webkit-scrollbar {
  width: 6px;
}

.sidebar-container .sidebar-list::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-container .sidebar-list::-webkit-scrollbar-thumb {
  background: var(--chat-border-primary);
  border-radius: 3px;
}

.sidebar-container .sidebar-list::-webkit-scrollbar-thumb:hover {
  background: var(--chat-subtle);
}

/* ============================================
   DARK MODE
   ============================================ */

body.dark-mode .sidebar-history {
  background: var(--chat-dark-surface-primary);
  border-right-color: var(--chat-dark-border-primary);
}

body.dark-mode .sidebar-header {
  border-bottom-color: var(--chat-dark-border-primary);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .history-item {
  background: var(--chat-dark-surface-light);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .history-item:hover {
  background: var(--chat-dark-border-primary);
}

body.dark-mode .history-item.active {
  background: var(--chat-brand-primary);
  color: var(--chat-bg-primary);
}

body.dark-mode .history-item.open .status-icon {
  color: var(--chat-status-success-light);
}

body.dark-mode .history-item.open .status-label {
  color: var(--chat-status-success);
}

body.dark-mode .history-item.closed .status-icon {
  color: var(--chat-text-secondary);
}

body.dark-mode .history-item.closed .status-label {
  color: var(--chat-text-tertiary);
}

body.dark-mode .sidebar-container {
  background: var(--chat-dark-bg-secondary);
  border-right-color: var(--chat-dark-border-primary);
  box-shadow: var(--chat-dark-shadow-overlay);
}

body.dark-mode .sidebar-container .sidebar-header {
  border-bottom-color: var(--chat-dark-border-primary);
}

body.dark-mode .sidebar-container .sidebar-header h3 {
  color: var(--chat-dark-text-primary);
}

body.dark-mode .sidebar-container .sidebar-actions {
  border-bottom-color: var(--chat-dark-border-primary);
}

body.dark-mode .sidebar-container .history-item {
  background: var(--chat-dark-surface-tertiary);
  border-color: var(--chat-dark-border-primary);
  color: var(--chat-dark-text-secondary);
  box-shadow: var(--chat-dark-shadow-xs);
}

body.dark-mode .sidebar-container .history-item:hover {
  background: var(--chat-dark-border-primary);
  border-color: var(--chat-dark-border-secondary);
  box-shadow: var(--chat-dark-shadow-sm);
}

body.dark-mode .sidebar-container .history-item.active {
  background: var(--chat-brand-primary-alpha-20);
  border-color: var(--chat-brand-primary);
  color: #a78bfa;
  box-shadow: var(--chat-dark-shadow-md);
}

body.dark-mode .sidebar-container .history-item.active .history-item-id {
  background-color: rgba(255, 255, 255, 0.1);
  color: #a78bfa;
}

body.dark-mode .sidebar-container .history-item.open {
  border-left-color: var(--chat-status-success-light);
}

body.dark-mode .sidebar-container .history-item.closed {
  border-left-color: var(--chat-text-secondary);
}

body.dark-mode .sidebar-container .history-item.open .status-icon {
  color: var(--chat-status-success-light);
}

body.dark-mode .sidebar-container .history-item.open .status-label {
  color: var(--chat-status-success);
}

body.dark-mode .sidebar-container .history-item.closed .status-icon {
  color: var(--chat-text-secondary);
}

body.dark-mode .sidebar-container .history-item.closed .status-label {
  color: var(--chat-text-tertiary);
}

body.dark-mode .sidebar-container .sidebar-list::-webkit-scrollbar-thumb {
  background: var(--chat-dark-border-primary);
}

body.dark-mode .sidebar-container .sidebar-list::-webkit-scrollbar-thumb:hover {
  background: var(--chat-dark-border-secondary);
}

/* ============================================
   RESPONSIVE
   ============================================ */

@media (max-width: 768px) {
  .sidebar-history #sidebarToggleClose {
    display: block;
  }
}

/* ============================================
   LEGACY STYLES (from main.css)
   TODO: Refactor to use component tokens
   ============================================ */

/* Sidebar layout and transitions */
.app-layout .sidebar-history {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 270px;
  background: #fff;
  border-right: 1.5px solid var(--clr-border);
  box-shadow: 3px 0 16px rgba(13, 28, 63, 0.04);
  z-index: 100;
  padding: 1.6rem 0 1.6rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  min-height: 100vh;
  transform: translateX(-110%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.23s, opacity 0.22s;
}

.app-layout.sidebar-open .sidebar-history {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}

.sidebar-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--clr-primary);
  margin-bottom: 1rem;
}

.app-layout #historyList {
  flex: 1;
  overflow-y: auto;
  padding-right: 0.5rem;
}

.history-item {
  background: var(--clr-surface-alt);
  border-radius: var(--radius-md);
  padding: 0.8rem 1rem;
  margin-bottom: 0.5rem;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  border: 1px solid var(--clr-border);
  transition: background 0.2s, border 0.2s;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.history-item:hover {
  background: #e0e7ef;
  border-color: var(--clr-accent);
}

.history-item-title {
  font-weight: 500;
  font-size: 1.03rem;
  color: var(--clr-primary-light);
  margin-bottom: 0.18rem;
}

.history-item-date {
  color: #64748b;
  font-size: 0.7rem;
}

.history-item-preview {
  color: #52525b;
  font-size: 0.97rem;
  font-style: italic;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* Sidebar toggle button */
.sidebar-toggle {
  background: none;
  border: none;
  height: 44px;
  width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  cursor: pointer;
}

.sidebar-toggle:hover {
  background: var(--clr-surface-alt);
}

.sidebar-toggle svg {
  display: block;
  stroke: #fff;
  transition: stroke 0.15s;
}

.sidebar-toggle:hover svg {
  stroke: var(--clr-accent);
}

/* Sidebar new conversation button */
.sidebar-new-btn {
  background-color: var(--clr-accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-weight: 500;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  margin-bottom: 1rem;
  transition: background 0.18s, box-shadow 0.2s;
  width: calc(100% - 1.2rem);
  margin-left: 0.1rem;
  text-align: left;
}

.sidebar-new-btn:hover {
  background-color: var(--clr-accent-dark);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.1);
}

/* Mobile: hide sidebar */
@media (max-width: 900px) {
  .app-layout .sidebar-history {
    display: none !important;
  }

  .app-layout.sidebar-open .main-content,
  .app-layout.sidebar-open footer {
    margin-left: 0 !important;
  }

  .sidebar-toggle {
    padding-left: 6px;
    margin-right: 0;
  }
}

/* Sidebar open state - no layout shift */
.app-layout.sidebar-open .main-content,
.app-layout.sidebar-open footer {
  margin-left: 0 !important;
  width: 100% !important;
}

/* Dark mode */
body.dark-mode .app-layout .sidebar-history {
  background-color: var(--clr-panel);
  color: var(--clr-text);
  border-right: 1px solid var(--clr-border);
}
