/* ===========================
   Tree Sidebar Navigation
   =========================== */

.tree-layout {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.tree-sidebar {
    width: 210px;
    min-width: 210px;
    flex-shrink: 0;
    position: sticky;
    top: calc(var(--sticky-top, 90px) + 8px);
    max-height: calc(100vh - var(--sticky-top, 90px) - 24px);
    overflow-y: auto;
    padding: 4px 0;
}

.tree-content {
    flex: 1;
    min-width: 0;
}

/* ── Group ── */

.tree-group {
    margin-bottom: 2px;
}

.tree-group:first-child > .tree-group-header {
    margin-top: 0;
}

.tree-group-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    cursor: pointer;
    user-select: none;
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--text-muted);
    border-radius: 6px;
    transition: color 0.15s, background 0.15s;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 6px;
}

.tree-group-header:hover {
    color: var(--text-color);
    background: var(--bg-secondary);
}

.tree-chevron {
    font-size: 0.5rem;
    width: 10px;
    display: inline-block;
    flex-shrink: 0;
    transition: transform 0.2s ease;
    color: var(--text-secondary);
}

.tree-chevron.open {
    transform: rotate(90deg);
}

.tree-node-icon {
    flex-shrink: 0;
    line-height: 1;
}

.tree-node-icon .icon {
    width: 14px;
    height: 14px;
}

.tree-node-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tree-group-count {
    background: var(--border-color);
    color: var(--text-secondary);
    font-size: 0.6rem;
    font-weight: 600;
    padding: 0 5px;
    border-radius: 8px;
    flex-shrink: 0;
    min-width: 16px;
    text-align: center;
}

/* ── Children (leaves) ── */

.tree-children {
    overflow: hidden;
    margin-left: 14px;
    border-left: 1px solid var(--border-color);
    padding-left: 0;
    max-height: 500px;
    opacity: 1;
    transition: max-height 0.25s ease, opacity 0.2s ease;
}

.tree-children.collapsed {
    max-height: 0;
    opacity: 0;
}

.tree-leaf {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px 5px 12px;
    cursor: pointer;
    font-size: 0.8rem;
    color: var(--text-secondary);
    border-radius: 0 6px 6px 0;
    margin: 1px 0;
    transition: color 0.15s, background 0.15s;
}

.tree-leaf:hover {
    color: var(--text-color);
    background: var(--bg-secondary);
}

.tree-leaf.active {
    color: var(--primary-text-color);
    background: color-mix(in srgb, var(--primary-color) 12%, transparent);
    font-weight: 600;
}

.tree-leaf.active .tree-count {
    background: var(--primary-color);
    color: var(--primary-contrast);
}

/* ── Standalone leaf (top-level, no group) ── */

.tree-standalone {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: 6px;
    margin: 1px 0;
    transition: color 0.15s, background 0.15s;
}

.tree-standalone:hover {
    color: var(--text-color);
    background: var(--bg-secondary);
}

.tree-standalone.active {
    color: var(--primary-text-color);
    background: color-mix(in srgb, var(--primary-color) 12%, transparent);
    font-weight: 600;
}

.tree-standalone.active .tree-count {
    background: var(--primary-color);
    color: var(--primary-contrast);
}

/* ── Count badge ── */

.tree-count {
    background: var(--border-color);
    color: var(--text-secondary);
    font-size: 0.6rem;
    font-weight: 600;
    padding: 0 5px;
    border-radius: 8px;
    flex-shrink: 0;
    min-width: 16px;
    text-align: center;
}

/* ── Extra (expand/collapse, tutorial buttons) ── */

.tree-extra {
    padding: 8px 10px;
    margin-top: 4px;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 4px;
    align-items: center;
}

/* ── Sidebar eats into card space — allow 2-col with smaller minmax ── */

.tree-content .displays-grid,
.tree-content .devices-grid {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}

.tree-content .templates-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

/* ── Responsive: stack on narrow screens ── */

@media (max-width: 900px) {
    .tree-layout {
        flex-direction: column;
        align-items: stretch;
        gap: 0;
    }

    .tree-sidebar {
        width: 100%;
        min-width: unset;
        position: static;
        max-height: none;
        overflow-y: visible;
        padding: 0 0 8px 0;
        margin-bottom: 8px;
        border-bottom: 1px solid var(--border-color);
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 2px;
    }

    .tree-group {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 2px;
        margin-bottom: 0;
    }

    .tree-group-header {
        padding: 4px 8px;
        text-transform: none;
        letter-spacing: normal;
    }

    .tree-children {
        display: flex;
        flex-wrap: wrap;
        gap: 2px;
        margin-left: 0;
        border-left: none;
        padding-left: 0;
        max-height: none;
        opacity: 1;
        transition: none;
    }

    .tree-children.collapsed {
        display: none;
    }

    .tree-leaf {
        padding: 4px 10px;
        margin: 0;
    }

    .tree-standalone {
        padding: 4px 10px;
    }

    .tree-extra {
        margin-top: 0;
        border-top: none;
        padding: 4px;
        margin-left: auto;
    }

}
