/* ========================================
   Collapsible Sidebar Layout
   Pure Bootstrap 5 Implementation
   ======================================== */

/* Layout Wrapper */
.layout-wrapper {
    display: flex;
    min-height: 100vh;
    /* Removed overflow-x: hidden to allow Bootstrap grid to work properly */
    /* and enable horizontal scrollbars on tables when needed */
}

/* Sidebar Styles */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 250px;
    background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
    color: #ecf0f1;
    transition: width 0.3s ease;
    z-index: 1030;
    overflow-x: hidden;
    overflow-y: auto;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

/* Collapsed State */
.sidebar.collapsed {
    width: 60px;
}

/* Hide scrollbar but keep functionality */
.sidebar::-webkit-scrollbar {
    width: 6px;
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Sidebar Inner Container */
.sidebar-inner {
    padding: 1rem 0;
}

/* Brand */
.sidebar-brand {
    padding: 0 1rem 1rem;
    text-align: center;
}

.sidebar-brand a {
    color: #ecf0f1;
    font-size: 1.5rem;
    font-weight: bold;
}

.sidebar.collapsed .brand-text {
    display: none;
}

/* Company/Project Context */
.sidebar-context {
    padding: 0 1rem;
    margin-bottom: 1rem;
}

.context-item {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    padding: 0.5rem;
    margin-bottom: 0.5rem;
    position: relative;
    transition: all 0.3s ease;
}

.context-item:hover {
    background: rgba(255, 255, 255, 0.15);
}

.context-label {
    font-size: 0.7rem;
    opacity: 0.7;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.context-label i {
    font-size: 0.8rem;
}

.context-value {
    font-size: 0.9rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-right: 24px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.context-value i {
    font-size: 1rem;
    opacity: 0.9;
}

.context-change-btn {
    position: absolute;
    top: 50%;
    right: 0.5rem;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #ecf0f1;
    padding: 0.25rem;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.context-change-btn:hover {
    opacity: 1;
}

/* Context Dropdown */
.context-dropdown {
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 4px;
    animation: slideDown 0.2s ease;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.context-dropdown .form-select {
    background-color: rgba(255, 255, 255, 0.9);
    color: #2c3e50;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

.context-dropdown .form-select:focus {
    background-color: #fff;
    border-color: #3498db;
    box-shadow: 0 0 0 0.2rem rgba(52, 152, 219, 0.25);
}

.sidebar.collapsed .sidebar-context {
    padding: 0 0.5rem;
}

.sidebar.collapsed .context-label,
.sidebar.collapsed .context-value {
    /* Hide text but keep container visible for icons */
    font-size: 0 !important;
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
}

.sidebar.collapsed .context-change-btn,
.sidebar.collapsed .context-dropdown {
    display: none !important;
}

.sidebar.collapsed .context-item {
    background: transparent;
    padding: 0.25rem;
    text-align: center;
}

.sidebar.collapsed .context-item i {
    display: inline-block;
    font-size: 1.1rem;
    margin: 0 !important;
}

/* Context Note - for read-only sidebars */
.context-note {
    margin-top: 0.75rem;
    padding: 0.5rem;
    background: rgba(52, 152, 219, 0.2);
    border-left: 3px solid #3498db;
    border-radius: 4px;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.9);
    line-height: 1.4;
}

.context-note a {
    color: #fff;
    font-weight: 500;
}

.context-note a:hover {
    color: #5dade2;
}

.sidebar.collapsed .context-note {
    display: none;
}

/* Read-only context items - no padding for change button */
.context-item.readonly .context-value {
    padding-right: 0;
}

/* Divider */
.sidebar-divider {
    border-color: rgba(255, 255, 255, 0.2);
    margin: 0.5rem 1rem;
}

.sidebar.collapsed .sidebar-divider {
    margin: 0.5rem;
}

/* Navigation */
.sidebar-nav {
    padding: 0 0.5rem;
}

/* Sidebar Items */
.sidebar-item {
    display: flex;
    align-items: center;
    padding: 0.65rem 1rem 0.65rem 1.5rem;
    color: rgba(236, 240, 241, 0.85);
    text-decoration: none;
    border-radius: 6px;
    margin-bottom: 0.25rem;
    margin-left: 0.25rem;
    transition: all 0.2s ease;
    cursor: pointer;
    font-size: 0.9rem;
}

.sidebar-item:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    margin-left: 0.5rem;
    padding-left: 1.25rem;
}

.sidebar-item.active {
    background: #3498db;
    color: #fff;
    margin-left: 0.5rem;
    font-weight: 500;
}

.sidebar-item i {
    font-size: 1.1rem;
    min-width: 22px;
    margin-right: 0.75rem;
    text-align: center;
    opacity: 0.8;
}

.sidebar.collapsed .sidebar-item {
    justify-content: center;
    padding: 0.75rem 0.5rem;
}

.sidebar.collapsed .sidebar-item i {
    margin-right: 0;
}

.sidebar.collapsed .item-text {
    display: none;
}

/* Section Titles */
.sidebar-section {
    margin-bottom: 1.25rem;
    transition: all 0.3s ease;
}

.sidebar.collapsed .sidebar-section {
    overflow: hidden;
    padding: 0 !important;
    margin: 0.25rem 0 !important;
}

.sidebar.collapsed .sidebar-section h6 {
    padding: 0.5rem 0;
    margin: 0.75rem 0;
    text-align: center;
    font-size: 0;  /* Hide text */
    font-weight: normal;
}

.sidebar.collapsed .sidebar-section h6 i {
    display: inline-block !important;
    font-size: 1.2rem !important;
    margin: 0 !important;
}

.section-title {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    font-size: 0.95rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #fff;
    opacity: 1;
    margin-top: 0.75rem;
    margin-bottom: 0.5rem;
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 6px;
    border-left: 3px solid #3498db;
}

.section-title:hover {
    background: rgba(255, 255, 255, 0.12);
    border-left-color: #5dade2;
}

.section-title i:first-child {
    font-size: 1.1rem;
    min-width: 24px;
    margin-right: 0.75rem;
    text-align: center;
}

/* Chevron Toggle Icon */
.chevron-toggle {
    font-size: 0.85rem;
    transition: transform 0.3s ease;
    opacity: 0.7;
    margin-left: auto !important;
    margin-right: 0 !important;
    min-width: auto !important;
}

.section-title:not(.collapsed) .chevron-toggle {
    transform: rotate(180deg);
}

.sidebar.collapsed .section-title {
    justify-content: center;
    padding: 0.5rem;
}

.sidebar.collapsed .section-title i:first-child {
    margin-right: 0;
    opacity: 0.5;
}

.sidebar.collapsed .section-title .item-text,
.sidebar.collapsed .chevron-toggle {
    display: none;
}

/* Hide all content in collapsed sidebar sections except for section titles and layer cards */
.sidebar.collapsed .sidebar-section > *:not(.section-title):not(.layer-card):not(h6) {
    display: none;
}

.sidebar.collapsed .layer-card {
    background: transparent;
    border: none;
    padding: 0.5rem 0;
    margin: 0;
}

.sidebar.collapsed .layer-header {
    justify-content: center;
    margin: 0;
}

.sidebar.collapsed .layer-name {
    display: none;
}

.sidebar.collapsed .toggle-switch {
    display: none;
}

.sidebar.collapsed .layer-controls,
.sidebar.collapsed .layer-card button,
.sidebar.collapsed .sidebar-section > .btn,
.sidebar.collapsed .sidebar-section > .form-select {
    display: none !important;
}

/* Main Content Area */
.main-content {
    flex: 1;
    margin-left: 250px;
    transition: margin-left 0.3s ease, max-width 0.3s ease;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    max-width: calc(100vw - 250px); /* Constrain to viewport minus sidebar */
    overflow-x: hidden; /* Prevent horizontal page scroll */
}

.sidebar.collapsed ~ .main-content,
body.sidebar-collapsed .main-content {
    margin-left: 60px;
    max-width: calc(100vw - 60px); /* Adjust for collapsed sidebar */
}

/* Content Wrapper */
.content-wrapper {
    flex: 1;
    padding: 1.5rem;
    background: #f8f9fa;
}

/* Navbar within Main Content */
.main-content .navbar {
    z-index: 1040; /* Above sidebar (1030) so toggle button is always clickable */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* When sidebar is collapsed, navbar is already shifted by margin-left: 60px on main-content */
/* So the toggle button should be visible. But ensure it's clickable */
body.sidebar-collapsed .main-content .navbar .container-fluid {
    padding-left: 1rem; /* Ensure button isn't at the very edge */
}

/* Sidebar Toggle Button */
.sidebar-toggle {
    color: #2c3e50;
    padding: 0.5rem;
    font-size: 1.5rem;
    line-height: 1;
    transition: color 0.2s ease;
    position: relative;
    z-index: 1050; /* Ensure it's above navbar and sidebar */
    pointer-events: auto; /* Ensure clicks work */
    cursor: pointer;
}

.sidebar-toggle:hover {
    color: #3498db;
}

/* Sidebar Toggle Icon Animation */
.sidebar-toggle-icon {
    transition: all 0.3s ease;
    display: inline-block;
    pointer-events: none; /* Let clicks pass through to button */
}

/* Responsive Styles */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-250px);
    }

    .sidebar.show {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0 !important;
        max-width: 100vw !important;
    }

    /* Mobile overlay */
    .sidebar.show::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: -1;
    }
}

/* Tooltip for collapsed sidebar items */
.sidebar.collapsed .sidebar-item,
.sidebar.collapsed .section-title {
    position: relative;
}

.sidebar.collapsed .sidebar-item:hover::after,
.sidebar.collapsed .section-title:hover::after {
    content: attr(data-title);
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    background: #2c3e50;
    color: #fff;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    white-space: nowrap;
    margin-left: 0.5rem;
    z-index: 1040;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    font-size: 0.875rem;
}

/* Navbar Title Area */
.navbar-title h5 {
    color: #2c3e50;
    font-weight: 600;
}

/* Navbar Actions */
.navbar-actions .btn {
    border-radius: 6px;
}

/* Print Styles */
@media print {
    .sidebar,
    .navbar {
        display: none !important;
    }

    .main-content {
        margin-left: 0 !important;
    }

    .content-wrapper {
        padding: 0 !important;
    }
}