/* ========================================
   Sidebar UI - AMC WebUI Style
   ======================================== */
#sidebar {
    --sb-bg: #f9f9f9;
    --sb-bg-hover: #ECECF1;
    --sb-text-primary: #000000;
    --sb-text-secondary: #000000;
    --sb-border: #D9D9E3;
    --sb-icon: #000000;
    --brand-google-blue: #0024bb;
    --brand-google-cyan: #00a5a5;

    width: var(--sidebar-width);
    background: var(--sb-bg);
    border-right: 1px solid var(--sb-border);
    display: flex;
    flex-direction: column;
    position: relative;
    padding: 16px 0 0 0;
    flex-shrink: 0;
    transition: width var(--transition-smooth), padding var(--transition-smooth), background var(--transition-smooth), border-color var(--transition-smooth);
    z-index: 100;
    will-change: width;
    overflow: hidden;
}

[data-theme="dark"] #sidebar {
    --sb-bg: #000000;
    --sb-bg-hover: #18181b;
    --sb-text-primary: #f4f4f5;
    --sb-text-secondary: #a1a1aa;
    --sb-border: #18181b;
    --sb-icon: #a1a1aa;
}

#sidebar.collapsed {
    width: 60px;
    padding: 16px 0;
}

/* Expanded Pane container */
.sidebar-expanded-pane {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 272px; /* Fixed width of expanded content to prevent squishing during collapse transition */
    flex-shrink: 0;
    opacity: 1;
    visibility: visible;
    transform: translateX(0);
    transition: opacity var(--transition-normal), transform var(--transition-normal), visibility 0s linear 0s;
}

#sidebar.collapsed .sidebar-expanded-pane {
    opacity: 0;
    visibility: hidden;
    transform: translateX(-8px);
    pointer-events: none;
    transition-delay: 0s, 0s, var(--transition-normal);
}

/* Collapsed (Mini) Pane container */
.sidebar-collapsed-pane {
    position: absolute;
    inset: 16px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 12px;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateX(8px);
    transition: opacity var(--transition-normal), transform var(--transition-normal), visibility 0s linear var(--transition-normal);
}

#sidebar.collapsed .sidebar-collapsed-pane {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateX(0);
    transition-delay: 0s, 0s, 0s;
}

.sidebar-collapsed-pane .mini-btn {
    width: 40px !important;
    height: 40px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
    color: var(--sb-icon);
    background: transparent;
    border: none;
    cursor: pointer;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.sidebar-collapsed-pane .mini-btn:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
}

.sidebar-expand-icon {
    transform: scaleX(-1);
}

.sidebar-collapsed-pane .mini-divider {
    width: 32px;
    height: 1px;
    background-color: var(--sb-border);
    margin: 4px 0;
}

.sidebar-collapsed-pane .mini-footer {
    margin-top: auto;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* --- Brand Header --- */
.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding: 0 16px;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-brand-name {
    display: inline-flex;
    align-items: baseline;
    padding: 4px 6px;
    margin-left: -6px;
    border-radius: 8px;
    font-size: 19.8px;
    font-weight: 600;
    color: var(--sb-text-primary);
    cursor: pointer;
    letter-spacing: 0;
    transition: background-color var(--transition-fast);
}

.sidebar-brand-name:hover {
    background-color: var(--sb-bg-hover);
}

.sidebar-brand-name:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.brand-text-blue {
    color: var(--brand-google-blue);
}

.brand-text-cyan {
    color: var(--brand-google-cyan);
}

.sidebar-header-buttons {
    display: flex;
    align-items: center;
    gap: 2px;
}

.sidebar-header-buttons .icon-btn {
    width: 41px !important;
    height: 41px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    color: var(--sb-icon);
    background: transparent;
    border: none;
    cursor: pointer;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.sidebar-header-buttons .icon-svg {
    width: 21.6px;
    height: 21.6px;
}

.sidebar-header-buttons .icon-btn:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
}

.mobile-only { display: none !important; }
.desktop-only { display: flex !important; }

.sidebar-toggle {
    z-index: 10;
    background-color: transparent;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    width: auto !important;
    display: none !important;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

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

/* --- Mobile Overlay --- */
.mobile-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 90;
    opacity: 0;
    transition: opacity var(--transition-smooth);
}

.mobile-overlay.active {
    display: block;
    opacity: 1;
}

/* --- New Chat Button --- */
.new-chat-wrapper {
    margin: 0 12px 8px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.new-chat-btn,
.history-search-btn,
.new-group-btn {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--sb-text-primary);
    padding: 0 12px;
    height: 32px;
    border-radius: 9999px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
    font-weight: 500;
    transition: background-color var(--transition-fast), color var(--transition-fast);
    box-shadow: none;
    position: relative;
    overflow: hidden;
}

.new-chat-btn:hover,
.history-search-btn:hover,
.new-group-btn:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
    box-shadow: none;
    transform: none;
}

.new-chat-btn:active,
.history-search-btn:active,
.new-group-btn:active {
    transform: none;
    box-shadow: none;
}

.new-chat-btn .icon-svg,
.history-search-btn .icon-svg,
.new-group-btn .icon-svg {
    width: 18px;
    height: 18px;
    color: var(--sb-icon);
    flex-shrink: 0;
}

.new-chat-btn .btn-text,
.history-search-btn .btn-text,
.new-group-btn .btn-text {
    flex-grow: 1;
    text-align: left;
    min-w-0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Keyboard Shortcut Hints */
.sidebar-kbd {
    min-width: 28px;
    font-family: inherit;
    font-size: 11px;
    font-weight: 500;
    color: var(--sb-text-secondary);
    background: transparent;
    border: none;
    border-radius: 0;
    padding: 0;
    box-shadow: none;
    opacity: 0;
    text-align: right;
    pointer-events: none;
    transition: opacity var(--transition-fast);
}

.new-chat-btn:hover .sidebar-kbd,
.history-search-btn:hover .sidebar-kbd,
.new-group-btn:hover .sidebar-kbd {
    background: transparent;
    color: var(--sb-text-secondary);
    border-color: transparent;
    box-shadow: none;
    opacity: 1;
}

/* --- History Search Box --- */
.history-search-box {
    position: relative;
    width: 100%;
    height: 36px;
    padding: 0 6px 0 12px;
    border: 1px solid var(--sb-border);
    border-radius: 8px;
    background-color: var(--sb-bg);
    color: var(--sb-text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
    box-sizing: border-box;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast), background-color var(--transition-fast);
}

.history-search-box[hidden] {
    display: none;
}

.history-search-box:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 1px var(--primary-light);
}

.history-search-box .search-icon-svg {
    width: 18px;
    height: 18px;
    color: var(--sb-icon);
    flex-shrink: 0;
    transition: color var(--transition-fast);
}

.history-search-input {
    min-width: 0;
    flex: 1 1 auto;
    height: 100%;
    padding: 0;
    border: none;
    background-color: transparent;
    color: var(--sb-text-primary);
    font-size: 14px;
    outline: none;
    box-sizing: border-box;
}

.history-search-input::placeholder {
    color: var(--sb-text-secondary);
    opacity: 0.75;
}

.history-search-box:focus-within .search-icon-svg {
    color: var(--primary);
}

.history-search-close-btn {
    width: 24px;
    height: 24px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--sb-icon);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: pointer;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.history-search-close-btn:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
}

.history-no-results {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    text-align: center;
    color: var(--sb-text-secondary);
    font-size: 13px;
    padding: 30px 0;
    cursor: default;
}

.history-no-results .history-no-results-icon {
    width: 32px;
    height: 32px;
    color: var(--sb-icon);
}

/* --- History Rename --- */
.history-title-edit {
    background: var(--bg-input);
    border: 1px solid var(--primary);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 13px;
    padding: 4px 8px;
    outline: none;
    width: 100%;
    flex-grow: 1;
    font-family: inherit;
    box-shadow: 0 0 0 2px var(--primary-light);
}

/* --- History List & Groups --- */
.history-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 0 14px;
    margin-bottom: 12px;
    cursor: pointer;
}

.history-group {
    margin-bottom: 14px;
    cursor: default;
}

.history-group-header {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    margin-bottom: 6px;
    padding-left: 8px;
}

.history-item {
    position: relative;
    padding: 8px 10px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13.5px;
    color: var(--sb-text-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background-color var(--transition-fast), color var(--transition-fast);
    margin-bottom: 2px;
    overflow: hidden;
}

.history-item.dragging {
    opacity: 0.48;
}

.history-item:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
}

.history-item.active {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
    font-weight: 500;
}

.history-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-grow: 1;
    transition: padding-right var(--transition-fast);
}

.history-item:hover .history-title {
    padding-right: 96px; /* Avoid overlay with hover buttons */
}

/* --- History Actions --- */
.history-item-actions {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%) translateX(8px);
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-normal), transform var(--transition-normal), background var(--transition-fast);
    background: linear-gradient(90deg, transparent, var(--sb-bg-hover) 20%);
    padding-left: 10px;
    height: 100%;
}

.history-item.active:hover .history-item-actions {
    background: linear-gradient(90deg, transparent, var(--sb-bg-hover) 20%);
}

.history-item:hover .history-item-actions {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(-50%) translateX(0);
}

.history-action-btn {
    background: none;
    border: none;
    color: var(--sb-text-secondary);
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.history-action-btn .icon-svg {
    width: 14px;
    height: 14px;
}

.history-action-btn:hover {
    background-color: var(--sb-bg) !important;
}

.history-rename-btn:hover {
    color: var(--primary);
}

.history-share-btn:hover {
    color: var(--success);
}

.history-export-btn:hover {
    color: var(--info);
}

.history-delete-btn:hover {
    color: var(--error);
}

/* --- Chat History Groups --- */
.chat-group {
    margin-bottom: 6px;
    border-radius: 8px;
    transition: background-color var(--transition-fast), box-shadow var(--transition-fast);
}

.chat-group-header {
    min-height: 32px;
    padding: 0 6px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--sb-text-secondary);
    cursor: pointer;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.chat-group-header:hover {
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-primary);
}

.chat-group-toggle,
.chat-group-action-btn {
    width: 24px;
    height: 24px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--sb-icon);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: pointer;
    transition: background-color var(--transition-fast), color var(--transition-fast), transform var(--transition-fast);
}

.chat-group-toggle .icon-svg,
.chat-group-action-btn .icon-svg {
    width: 14px;
    height: 14px;
}

.chat-group.collapsed .chat-group-toggle {
    transform: rotate(-90deg);
}

.chat-group-title {
    min-width: 0;
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13.5px;
    font-weight: 600;
}

.chat-group-count {
    min-width: 18px;
    height: 18px;
    padding: 0 6px;
    border-radius: 999px;
    background-color: var(--sb-bg-hover);
    color: var(--sb-text-secondary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 600;
    line-height: 1;
}

.chat-group-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-fast);
}

.chat-group-header:hover .chat-group-actions,
.chat-group-header:focus-within .chat-group-actions {
    opacity: 1;
    pointer-events: auto;
}

.chat-group-action-btn:hover,
.chat-group-toggle:hover {
    background-color: var(--sb-bg);
    color: var(--sb-text-primary);
}

.chat-group-delete-btn:hover {
    color: var(--error);
}

.chat-group-session-list {
    padding-left: 12px;
}

.chat-group-session-list .history-group {
    margin-bottom: 8px;
}

.chat-group-session-list .history-group-header {
    margin: 4px 0 4px;
    padding-left: 4px;
    font-size: 10px;
}

.chat-group-empty {
    margin: 2px 0 6px 28px;
    padding: 6px 8px;
    border: 1px dashed var(--sb-border);
    border-radius: 8px;
    color: var(--sb-text-secondary);
    font-size: 12px;
    line-height: 1.3;
}

.chat-group-drop-target.drag-over {
    background-color: var(--sb-bg-hover);
    box-shadow: inset 0 0 0 1px var(--primary);
}

.ungrouped-drop-target {
    min-height: 30px;
    margin: 2px 0 6px 0;
    border-radius: 8px;
    transition: background-color var(--transition-fast), box-shadow var(--transition-fast);
}

.ungrouped-drop-target .chat-group-empty {
    margin: 0 0 6px 0;
}

.chat-group-title-edit {
    height: 26px;
    min-width: 0;
    flex: 1 1 auto;
    font-weight: 600;
}

/* --- Sidebar Footer --- */
.sidebar-footer {
    padding: 12px;
    margin-top: auto;
}

.sidebar-footer-settings-btn {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: transparent;
    border: none;
    border-radius: 12px;
    color: var(--sb-text-secondary);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

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

.sidebar-footer-settings-btn .icon-svg {
    width: 18px;
    height: 18px;
    color: var(--sb-icon);
    transition: color var(--transition-fast);
}

.sidebar-footer-settings-btn:hover .icon-svg {
    color: var(--sb-text-primary);
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    text-align: left;
    font-size: 14px;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.icon-btn:hover {
    background-color: var(--primary-light);
    color: var(--primary);
}

/* --- Collapsed Recent Chats Popover --- */
.recent-chats-popover {
    position: fixed;
    background-color: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    width: 280px;
    z-index: 10000;
    overflow: hidden;
    animation: popoverFadeIn 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes popoverFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateX(-8px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateX(0);
    }
}

.popover-header {
    padding: 12px 16px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-light);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.popover-list {
    max-height: 320px;
    overflow-y: auto;
    padding: 6px 0;
}

.popover-item {
    display: block;
    padding: 10px 16px;
    font-size: 13.5px;
    color: var(--text-secondary);
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background-color var(--transition-fast), color var(--transition-fast);
}

.popover-item:hover {
    background-color: var(--bg-secondary);
    color: var(--text-primary);
}

.popover-empty {
    padding: 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}
