/**
 * TREE VIEW CATEGORY MENU - Proven Pattern
 * Clean, hierarchical sidebar like VS Code Explorer
 */

/* =============================================================================
   MENU BUTTON & TRIGGER
============================================================================= */

.tree-menu-button {
    background: #2563eb;
    border: none;
    border-radius: 8px;
    color: white;
    padding: 12px 16px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
    margin-bottom: 12px;
}

.tree-menu-button:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
}

.tree-menu-button .icon {
    font-size: 16px;
}

.tree-menu-button .toggle-icon {
    font-size: 12px;
    margin-left: auto;
    transition: transform 0.2s ease;
}

/* =============================================================================
   SIDEBAR CONTAINER
============================================================================= */

.tree-menu-sidebar {
    position: fixed;
    top: 0;
    left: -100%; /* Hidden by default, use percentage */
    width: 100%; /* Match chat column mobile width */
    height: 100vh;
    background: #f8fafc;
    border-right: 1px solid #e2e8f0;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.1);
    z-index: 10000;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
}

.tree-menu-sidebar.open {
    left: 0;
}

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

.tree-menu-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    background: white;
    border-bottom: 1px solid #e2e8f0;
    position: sticky;
    top: 0;
    z-index: 1;
}

.tree-menu-title {
    font-size: 16px;
    font-weight: 600;
    color: #1e293b;
    margin: 0;
}

.tree-menu-close {
    background: none;
    border: none;
    font-size: 20px;
    color: #64748b;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.tree-menu-close:hover {
    background: #f1f5f9;
    color: #1e293b;
}

/* =============================================================================
   SEARCH BOX
============================================================================= */

.tree-menu-search {
    padding: 16px 20px;
    background: white;
    border-bottom: 1px solid #e2e8f0;
}

.tree-search-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 14px;
    background: white;
    transition: border-color 0.2s ease;
}

.tree-search-input:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.tree-search-input::placeholder {
    color: #94a3b8;
}

/* =============================================================================
   TREE VIEW
============================================================================= */

.tree-menu-content {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.tree-category-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.tree-category-item {
    margin: 0;
    padding: 0;
}

.tree-category-link {
    display: flex;
    align-items: center;
    padding: 8px 20px;
    color: #475569;
    text-decoration: none;
    font-size: 14px;
    line-height: 1.4;
    position: relative;
    cursor: pointer;
    transition: all 0.15s ease;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}

.tree-category-link:hover {
    background: #f1f5f9;
    color: #1e293b;
}

.tree-category-link.has-children {
    font-weight: 500;
}

.tree-category-link.expanded {
    background: #eff6ff;
    color: #1d4ed8;
}

/* =============================================================================
   TREE HIERARCHY INDENTATION
============================================================================= */

.tree-category-item.level-1 .tree-category-link {
    padding-left: 40px;
}

.tree-category-item.level-2 .tree-category-link {
    padding-left: 60px;
    font-size: 13px;
}

.tree-category-item.level-3 .tree-category-link {
    padding-left: 80px;
    font-size: 13px;
    color: #64748b;
}

.tree-category-item.level-4 .tree-category-link {
    padding-left: 100px;
    font-size: 12px;
    color: #64748b;
}

.tree-category-item.level-5 .tree-category-link {
    padding-left: 120px;
    font-size: 12px;
    color: #94a3b8;
}

.tree-category-item.level-6 .tree-category-link {
    padding-left: 140px;
    font-size: 12px;
    color: #94a3b8;
}

/* Fallback for any level deeper than 6 */
.tree-category-item[class*="level-"] .tree-category-link {
    min-height: 32px;
    display: flex;
    align-items: center;
}

/* =============================================================================
   EXPAND/COLLAPSE ICONS
============================================================================= */

.tree-expand-icon {
    margin-right: 8px;
    font-size: 12px;
    color: #94a3b8;
    transition: transform 0.2s ease;
    width: 12px;
    text-align: center;
}

.tree-category-link.expanded .tree-expand-icon {
    transform: rotate(90deg);
    color: #1d4ed8;
}

.tree-category-link:not(.has-children) .tree-expand-icon {
    opacity: 0;
}

/* =============================================================================
   NESTED CATEGORIES
============================================================================= */

.tree-nested-categories {
    display: none;
    background: #f8fafc;
    padding-left: 16px;
    border-left: 1px solid #e2e8f0;
    margin-left: 12px;
}

.tree-nested-categories.expanded {
    display: block;
}

/* =============================================================================
   PRODUCT COUNT BADGES
============================================================================= */

.tree-product-count {
    margin-left: auto;
    background: #e2e8f0;
    color: #64748b;
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: 500;
    min-width: 18px;
    text-align: center;
}

.tree-category-link:hover .tree-product-count {
    background: #cbd5e1;
    color: #475569;
}

.tree-category-link.expanded .tree-product-count {
    background: #dbeafe;
    color: #1d4ed8;
}

/* =============================================================================
   BACKDROP OVERLAY
============================================================================= */

.tree-menu-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    z-index: 9999;
    display: none;
    backdrop-filter: blur(2px);
}

.tree-menu-backdrop.active {
    display: block;
}

/* =============================================================================
   LOADING STATES
============================================================================= */

.tree-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    color: #64748b;
    font-size: 14px;
}

.tree-loading-spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid #e2e8f0;
    border-radius: 50%;
    border-top-color: #2563eb;
    animation: spin 1s ease-in-out infinite;
    margin-right: 8px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* =============================================================================
   SEARCH HIGHLIGHTING
============================================================================= */

.tree-search-highlight {
    background: #fef3c7;
    color: #92400e;
    padding: 1px 2px;
    border-radius: 2px;
}

.tree-category-item.hidden {
    display: none;
}

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

@media (max-width: 768px) {
    .tree-menu-sidebar {
        width: 100vw;
        left: -100vw;
    }
    
    .tree-menu-sidebar.open {
        left: 0;
    }
}

/* =============================================================================
   RESPONSIVE - MATCH CHAT COLUMN WIDTH
============================================================================= */

/* Mobile: Accordion mode - no sidebar */
@media (max-width: 767px) {
    .tree-menu-sidebar {
        display: none; /* Hide sidebar on mobile */
    }
    
    .tree-menu-backdrop {
        display: none; /* Hide backdrop on mobile */
    }
}

/* Desktop: Sidebar mode - match chat column width (33.33% of screen) */
@media (min-width: 768px) {
    .tree-menu-accordion {
        display: none; /* Hide accordion on desktop */
    }
    
    .tree-menu-sidebar {
        width: 33.33%; /* Match chat column desktop width */
        left: -33.33%; /* Adjust hidden position accordingly */
    }
}

/* =============================================================================
   SCROLLBAR STYLING
============================================================================= */

.tree-menu-content::-webkit-scrollbar {
    width: 6px;
}

.tree-menu-content::-webkit-scrollbar-track {
    background: transparent;
}

.tree-menu-content::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

.tree-menu-content::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* =============================================================================
   ANIMATIONS
============================================================================= */

.tree-category-item {
    animation: fadeInUp 0.2s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* =============================================================================
   ACCESSIBILITY
============================================================================= */

.tree-category-link:focus {
    outline: 2px solid #2563eb;
    outline-offset: -2px;
    background: #eff6ff;
}

.tree-menu-sidebar {
    /* Ensure keyboard navigation works */
    contain: layout style;
}

/* Focus trap for keyboard navigation */
.tree-menu-sidebar.open {
    contain: layout style;
}

/* =============================================================================
   MOBILE ACCORDION MODE
============================================================================= */

.tree-menu-accordion {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    margin-bottom: 1rem;
    overflow: hidden;
    max-height: 0;
    transition: max-height 0.3s ease-out;
}

.tree-menu-accordion.open {
    max-height: 80vh; /* Limit height on mobile */
}

.tree-menu-accordion .tree-menu-search {
    padding: 12px 16px;
    background: white;
    border-bottom: 1px solid #e2e8f0;
}

.tree-menu-accordion .tree-search-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
}

.tree-menu-accordion .tree-search-input:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.tree-menu-accordion .tree-menu-content {
    max-height: 60vh;
    overflow-y: auto;
    padding: 8px;
}

/* Mobile-specific category styling */
@media (max-width: 767px) {
    .tree-menu-button {
        width: 100%;
        justify-content: space-between;
        text-align: left;
    }
    
    .tree-category-item {
        border-bottom: 1px solid #f1f5f9;
    }
    
    .tree-category-item:last-child {
        border-bottom: none;
    }
    
    .tree-category-link {
        padding: 12px 8px;
        font-size: 14px;
    }
    
    .tree-nested-categories {
        padding-left: 16px;
        border-left: 2px solid #e2e8f0;
        margin-left: 8px;
    }
    
    .tree-expand-icon {
        margin-right: 8px;
        font-size: 12px;
        color: #6b7280;
        transition: transform 0.2s ease;
    }
    
    .tree-expand-icon:hover {
        color: #374151;
    }
}

/* Desktop expand icon styling */
@media (min-width: 768px) {
    .tree-expand-icon {
        margin-right: 8px;
        font-size: 10px;
        color: #6b7280;
        transition: all 0.2s ease;
        border-radius: 2px;
        padding: 2px;
    }
    
    .tree-expand-icon:hover {
        background: #f3f4f6;
        color: #374151;
    }
    
    .tree-category-link.has-children:hover .tree-expand-icon {
        background: #e5e7eb;
    }
}

/* Improved category link styling */
.tree-category-link {
    position: relative;
}
