/* ============================================
   SIDEBAR - Claude.ai Style (Minimalist)
   ============================================ */

/* Основной контейнер sidebar */
.chat-sidebar {
    width: 260px;
    background: var(--color-bg-sidebar);
    border-right: 1px solid var(--color-border-main);
    /* Base layout */
    overflow-x: hidden;
    overflow-y: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: width 0.3s ease;
}

.chat-sidebar.collapsed {
    width: 60px;
}

/* ============================================
   SIDEBAR TITLE
   ============================================ */

.sidebar-title {
    padding: 6px 4px 6px 16px;
    margin: 1px 8px 1px 8px;
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-heading);
    background: var(--color-bg-sidebar);
    display: flex;
    align-items: center;
    min-height: 32px;
    border-radius: 6px;
    position: relative;
    height: 44px; /* Fixed height to match main area */
    box-sizing: border-box;
}

.sidebar-title::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--color-border-thin);
}

/* Hamburger button (Mobile only) */
.sidebar-hamburger-btn {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--color-text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: none; /* Скрыт по умолчанию (desktop) */
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.sidebar-hamburger-btn:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.sidebar-hamburger-btn svg,
.sidebar-hamburger-btn i {
    width: 16px;
    height: 16px;
}

/* Collapse button (Desktop only) */
.sidebar-collapse-btn {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--color-text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px; /* Было 24px */
    height: 32px; /* Было 24px */
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px; /* Было 4px */
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.sidebar-collapse-btn:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.sidebar-collapse-btn svg {
    width: 16px;
    height: 16px;
}

/* ============================================
   NEW CHAT BUTTON - Claude.ai Style
   ============================================ */

.new-chat-container {
    padding: 16px 8px 12px 8px;
    background: var(--color-bg-sidebar);
    position: relative;
}

.new-chat-btn {
    width: 100%;
    margin: 0;
    padding: 0 16px;
    background: var(--color-primary);
    border: none;
    border-radius: 6px;
    color: var(--color-bg-main);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    position: relative;
    height: 32px;
    box-sizing: border-box;
}


.new-chat-btn:hover {
    background: var(--color-primary-hover);
}


/* ============================================
   SEARCH SECTION
   ============================================ */

.search-container {
    padding: 0 8px 12px 8px;
    background: var(--color-bg-sidebar);
    position: relative;
}

.search-input-wrapper {
    position: relative;
    display: inline-block;
    width: 100%;
}

.search-container::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 8px;
    right: 8px;
    height: 1px;
    background: var(--color-border-thin);
}

#searchConversations {
    width: 100%;
    padding: 8px 32px 8px 16px;
    border: 1px solid var(--color-border-main);
    border-radius: 6px;
    font-size: 14px;
    background: var(--color-bg-main);
    color: var(--color-text-primary);
    transition: all 0.15s ease;
    outline: none;
    box-sizing: border-box;
}

#searchConversations:focus {
    border-color: var(--color-text-muted);
    box-shadow: 0 0 0 3px rgba(156, 163, 175, 0.1);
}

#searchConversations::placeholder {
    color: var(--color-text-muted);
}

#clearSearch {
    position: absolute;
    top: 50%;
    right: 8px;
    transform: translateY(-50%);
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 16px;
    color: var(--color-text-muted);
    padding: 0;
    width: 24px;
    height: 24px;
    line-height: 1;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

#clearSearch.is-visible {
    display: flex;
}

#clearSearch:hover {
    color: var(--color-text-secondary);
}

/* ============================================
   CONVERSATIONS LIST
   ============================================ */

/* Header "Recents" */
.conversations-header {
    padding: 12px 16px 8px 16px;
    font-size: 12px;
    font-weight: 600;
    color: var(--color-text-secondary);
    letter-spacing: 0.05em;
    background: var(--color-bg-sidebar);
    position: relative;
}

.conversations-header::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 8px;
    right: 8px;
    height: 1px;
    background: var(--color-border-thin);
}

/* List container */
.conversations-list {
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    overflow-x: hidden;
    padding: 4px 0;
    background: var(--color-bg-sidebar); /* Unified background */
}

/* Scrollbar styling - Show only when scrolled down */
.conversations-list::-webkit-scrollbar {
    width: 6px;
    background: transparent;
}

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

.conversations-list::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 3px;
    transition: background 0.2s ease;
}

/* Show scrollbar only when scrolled down (not at top) */
.conversations-list.scrolled::-webkit-scrollbar-thumb {
    background: var(--color-bg-selected);
}

.conversations-list::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* ============================================
   CONVERSATION ITEM - Minimalist
   ============================================ */

.conversation-item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 4px 6px 16px;
    margin: 1px 8px 1px 8px;
    cursor: pointer;
    transition: background-color 0.15s ease;
    border-radius: 6px;
    min-height: 32px;
}

.conversation-item:hover {
    background-color: var(--color-bg-hover);
}

.conversation-item.active {
    background-color: var(--color-bg-selected);
}

.conversation-item.active:hover {
    background-color: var(--color-bg-selected); /* Сохраняем более темный фон при hover на активном */
}

/* Content area */
.conversation-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
    margin-right: 8px;
}

/* Title */
.conversation-preview {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}


/* ============================================
   THREE DOTS MENU BUTTON
   ============================================ */

.conversation-menu-btn {
    opacity: 0;
    background: none;
    border: none;
    color: var(--color-text-secondary);
    cursor: pointer;
    padding: 0;
    margin: 0;
    transition: all 0.15s ease;
    flex-shrink: 0;
    border-radius: 4px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* Create three dots using CSS instead of text */
.conversation-menu-btn::before {
    content: '';
    width: 3px;
    height: 3px;
    border-radius: 50%;
    background-color: currentColor;
    box-shadow: 
        -6px 0 0 currentColor,
        6px 0 0 currentColor;
}

/* Show menu button on hover OR for active conversation */
.conversation-item:hover .conversation-menu-btn,
.conversation-item.active .conversation-menu-btn {
    opacity: 1;
}

.conversation-menu-btn:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

/* Hide the text content since we're using CSS-generated dots */
.conversation-menu-btn {
    font-size: 0;
    line-height: 0;
}


/* ============================================
   DROPDOWN MENU (Three dots menu)
   ============================================ */

.conversation-menu {
    position: fixed;
    background: var(--color-bg-main);
    border: 1px solid var(--color-border-main);
    border-radius: 8px;
    padding: 4px 0;
    min-width: 160px;
    z-index: 10002; /* Выше sidebar (10001) и overlay (10000) на mobile */
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.conversation-menu-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    color: var(--color-text-primary);
    text-align: left;
    transition: background-color 0.15s ease;
}

.conversation-menu-item:hover {
    background-color: var(--color-bg-hover);
}

.conversation-menu-item.delete-item {
    color: var(--color-error);
}

.conversation-menu-item.delete-item:hover {
    background-color: rgba(239, 68, 68, 0.1);
}

.menu-icon {
    font-size: 14px;
    width: 16px;
    text-align: center;
    flex-shrink: 0;
}

.menu-text {
    flex: 1;
    font-weight: 500;
}

/* ============================================
   LOAD MORE BUTTON
   ============================================ */

.load-more-btn {
    margin: 1px 8px 1px 8px;
    padding: 6px 16px;
    background: none;
    border: none;
    border-radius: 6px;
    color: var(--color-text-secondary);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    text-align: center;
    width: calc(100% - 16px);
}

    .load-more-btn:hover {
    background: var(--color-bg-hover);
    color: var(--color-text-primary);
}

/* ============================================
   COLLAPSED STATE - Hide text elements
   ============================================ */

/* Скрыть текст "AI Aggregator", оставить только кнопку по центру */
.chat-sidebar.collapsed .sidebar-title {
    justify-content: center;
    padding: 6px 4px;
    font-size: 0; /* Скрываем текст */
}

/* Показать кнопку collapse всегда */
.chat-sidebar.collapsed .sidebar-collapse-btn {
    font-size: 14px; /* Восстанавливаем размер для кнопки */
    transform: rotate(180deg);
}

/* Убрать margin-left в collapsed состоянии */
.chat-sidebar.collapsed .sidebar-collapse-btn {
    margin-left: 0;
}

/* Hide "Chat history" header */
.chat-sidebar.collapsed .conversations-header {
    display: none;
}

/* Hide all conversation items */
.chat-sidebar.collapsed .conversations-list {
    display: none;
}

/* Hide search */
.chat-sidebar.collapsed .search-container {
    display: none;
}

/* Center new chat container */
.chat-sidebar.collapsed .new-chat-container {
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Transform New Chat button to icon only */
.chat-sidebar.collapsed .new-chat-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    font-size: 0; /* Hide text */
    display: flex;
    justify-content: center;
    align-items: center;
}

.chat-sidebar.collapsed .new-chat-btn::before {
    content: '+';
    font-size: 20px;
    font-weight: 400;
    display: block;
}

/* Rotate collapse icon when collapsed */
.chat-sidebar.collapsed .sidebar-collapse-btn svg {
    transform: rotate(0deg); /* Иконка уже повернута на кнопке */
}

/* ============================================
   SIDEBAR OVERLAY (Mobile)
   ============================================ */

/* Overlay для затемнения фона когда sidebar открыт на mobile */
.sidebar-overlay {
    /* По умолчанию скрыт */
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px); /* Размытие фона для красоты */
    z-index: 10000; /* Выше всего контента, но ниже sidebar */
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none; /* По умолчанию не блокирует клики */
}

/* Видимое состояние overlay - только на mobile через media query */
.sidebar-overlay.active {
    /* display и opacity управляются через media query для mobile */
    opacity: 1;
    pointer-events: auto; /* Разрешаем клики для закрытия */
}

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

@media (max-width: 768px) {
    /* Sidebar становится overlay на mobile */
    .chat-sidebar {
        width: 260px; /* Фиксированная ширина sidebar */
        position: fixed;
        top: 0;
        left: -260px; /* Скрыт за левым краем по умолчанию */
        height: 100vh;
        height: 100dvh; /* Use dynamic viewport height for iOS */
        max-height: 100vh;
        max-height: 100dvh;
        z-index: 10001; /* Выше overlay (10000) и всего контента, но ниже модалов (99999+) */
        transition: left 0.3s ease; /* Плавная анимация */
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15); /* Тень справа */
        background: var(--color-bg-sidebar); /* Убеждаемся что фон есть */
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    
    /* Верхние элементы не сжимаются */
    .sidebar-title,
    .new-chat-container,
    .layout-toggle-container,
    .search-container {
        flex-shrink: 0;
    }
    
    /* Conversations list должен быть между верхними элементами и Settings */
    .conversations-list {
        position: static !important;                  /* возвращаем в flex-поток */
        flex: 1 1 auto !important;                   /* занимает всё между верхом и Settings */
        overflow-y: auto !important;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 4px 0;                              /* можно оставить небольшой отступ */
        background: var(--color-bg-sidebar);
    }
    
    /* Settings всегда внизу - используем flex для позиционирования */
    .sidebar-settings-container {
        margin-top: auto !important;
        flex-shrink: 0 !important;
        position: relative; /* Относительное позиционирование для flex */
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)) !important;
    }
    
    /* Открытое состояние sidebar */
    .chat-sidebar.mobile-open {
        left: 0; /* Выезжает на экран */
    }
    
    /* Блокируем scroll body когда sidebar открыт */
    body.sidebar-mobile-open {
        overflow: hidden;
    }
    
    /* Убеждаемся что overlay показывается только на mobile */
    .sidebar-overlay.active {
        display: block !important; /* Принудительно показываем на mobile */
    }
    
    /* Показываем hamburger кнопку на mobile */
    .sidebar-hamburger-btn {
        display: flex !important;
    }
    
    /* Скрываем collapse кнопку на mobile */
    .sidebar-collapse-btn {
        display: none !important;
    }
    
    /* Three dots menu button всегда видна на mobile */
    .conversation-menu-btn {
        opacity: 1 !important; /* Всегда видна на mobile */
    }
}

/* На desktop overlay всегда скрыт */
@media (min-width: 769px) {
    .sidebar-overlay {
        display: none !important;
    }
    
    .sidebar-overlay.active {
        display: none !important;
    }
}

/* ============================================
   GENERATING INDICATOR - для чатов где идет генерация
   ============================================ */

.conversation-item.generating {
    background-color: rgba(59, 130, 246, 0.1);
}

.conversation-item.generating:hover {
    background-color: rgba(59, 130, 246, 0.15);
}

/* Spinner иконка - СПРАВА от текста, СЛЕВА от three dots */
.generating-spinner {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    color: var(--color-primary);
    animation: spin-loader 1s linear infinite;
    flex-shrink: 0;
    margin-left: auto; /* Прижать к правой стороне */
    margin-right: 4px; /* Отступ от кнопки three dots */
}

.generating-spinner svg {
    width: 14px;
    height: 14px;
}

/* Completed indicator - синяя галочка */
.completed-indicator {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    color: var(--color-info); /* Синий цвет */
    flex-shrink: 0;
    margin-left: auto;
    margin-right: 4px;
}

.completed-indicator svg {
    width: 14px;
    height: 14px;
}

.conversation-item.completed {
    background-color: rgba(59, 130, 246, 0.08);
}

.conversation-item.completed:hover {
    background-color: rgba(59, 130, 246, 0.12);
}

@keyframes spin-loader {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* ============================================
   SIDEBAR SETTINGS (Bottom of Sidebar)
   ============================================ */

/* Settings block — ensure it stays at bottom */
.sidebar-settings-container,
.sidebar-settings,
.sidebar-footer,
.settings-block {
    margin-top: auto !important;
    flex-shrink: 0 !important;
    padding-bottom: env(safe-area-inset-bottom, 12px) !important;
}

.sidebar-settings-container {
    padding: 12px 8px;
    background: var(--color-bg-sidebar);
    border-top: 1px solid var(--color-border-main);
}

/* Settings dropdown in sidebar */
.sidebar-settings-dropdown {
    position: relative;
}

.sidebar-settings-dropdown .user-dropdown-trigger {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--color-bg-sidebar);
    border: 1px solid var(--color-border-thin);
    border-radius: 8px;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 14px;
    color: var(--color-text-primary);
    transition: all 0.2s ease;
    justify-content: space-between;
}

.sidebar-settings-dropdown .user-dropdown-trigger:hover {
    background: var(--color-bg-hover);
    border-color: var(--color-primary);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
}

.sidebar-settings-dropdown .user-dropdown-trigger i {
    color: var(--color-text-secondary);
    font-size: 12px;
    transition: transform 0.2s ease;
}

.sidebar-settings-dropdown.open .user-dropdown-trigger i {
    transform: rotate(180deg);
}

/* Dropdown menu positioned to open UP from sidebar */
.sidebar-settings-container .sidebar-settings-dropdown .user-dropdown-menu {
    position: absolute !important;
    bottom: 100% !important; /* Position above the button */
    left: 0 !important;
    top: auto !important;
    right: auto !important;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid var(--color-border-thin);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
    min-width: 200px;
    width: calc(100% - 0px); /* Match button width */
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: all 0.2s ease;
    backdrop-filter: blur(10px);
    margin-top: 0;
    margin-bottom: 8px;
}

/* Arrow pointing down (towards button) */
.sidebar-settings-container .sidebar-settings-dropdown .user-dropdown-menu::before {
    content: '';
    position: absolute;
    bottom: -6px;
    left: 20px;
    top: auto;
    right: auto;
    width: 12px;
    height: 12px;
    background: rgba(255, 255, 255, 0.95);
    border-right: 1px solid rgba(224, 224, 224, 0.8);
    border-bottom: 1px solid rgba(224, 224, 224, 0.8);
    border-top: none;
    border-left: none;
    transform: rotate(45deg);
    z-index: -1;
}

.sidebar-settings-container .sidebar-settings-dropdown.open .user-dropdown-menu {
    opacity: 1 !important;
    visibility: visible !important;
    transform: translateY(0) !important;
}

/* Collapsed sidebar state - hide settings */
.chat-sidebar.collapsed .sidebar-settings-container {
    display: none;
}

/* ---------------------------------------------------------
   LANDSCAPE FIX FOR iOS SAFARI
   iOS Safari reduces visual viewport in landscape by 150–200px.
   This causes bottom elements (Settings, Load More) to disappear.
   Fix: switch sidebar to 100svh and allow it to scroll.
----------------------------------------------------------- */
@media (orientation: landscape) and (max-width: 1024px) {
    .chat-sidebar {
        height: 100svh !important;          /* REAL visual height in iOS */
        max-height: 100svh !important;
        overflow-y: auto !important;        /* sidebar becomes scrollable */
        overflow-x: hidden !important;
    }

    .conversations-list {
        flex: 1 1 auto !important;
        overflow-y: auto !important;         /* ensure middle scroll works */
    }

    .sidebar-settings-container,
    .sidebar-settings,
    .sidebar-footer,
    .settings-block {
        margin-top: auto !important;         /* stays at true bottom */
        flex-shrink: 0 !important;
    }
}
