:root {
    --primary-color: #2563eb;
    --sidebar-width: 240px;
    --sidebar-collapsed-width: 68px;
    --sidebar-tree-width: 320px;
    --sidebar-transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* No transitions during initial load */
html.no-transitions,
html.no-transitions *,
html.no-transitions *::before,
html.no-transitions *::after {
    transition: none !important;
}

/* Pre-apply sidebar state before JS runs */
html.pre-tree-mode .sidebar {
    width: var(--sidebar-tree-width) !important;
}
html.pre-tree-mode .main-content {
    margin-left: var(--sidebar-tree-width) !important;
}
html.pre-pinned .sidebar {
    width: var(--sidebar-width) !important;
}
html.pre-pinned .main-content {
    margin-left: var(--sidebar-width) !important;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: #f5f5f7;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-collapsed-width);
    height: 100vh;
    background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
    padding: 1rem 0;
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-width: none;
    z-index: 1000;
    transition: width var(--sidebar-transition);
}
.sidebar::-webkit-scrollbar { display: none; }

.sidebar:hover,
.sidebar.pinned {
    width: var(--sidebar-width);
}

.sidebar.tree-mode {
    width: var(--sidebar-tree-width);
}

.sidebar.tree-mode:hover {
    width: var(--sidebar-tree-width);
}

/* Sidebar resize handle */
.sidebar-resize-handle {
    position: absolute;
    top: 0;
    right: 0;
    width: 5px;
    height: 100%;
    cursor: ew-resize;
    background: transparent;
    transition: background 0.2s;
    z-index: 1001;
}

.sidebar-resize-handle:hover,
.sidebar-resize-handle.dragging {
    background: var(--primary-color);
}

.sidebar.resizing {
    transition: none !important;
}

body.sidebar-resizing {
    cursor: ew-resize !important;
    user-select: none !important;
}

.sidebar-brand {
    color: #fff;
    font-weight: 700;
    font-size: 1.25rem;
    padding: 0.5rem 1.25rem 1.5rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 1rem;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.sidebar-brand .brand-icon {
    font-size: 1.5rem;
    min-width: 24px;
}

.sidebar-brand .brand-text {
    opacity: 0;
    transition: opacity var(--sidebar-transition);
}

.sidebar:hover .brand-text,
.sidebar.pinned .brand-text,
.sidebar.tree-mode .brand-text {
    opacity: 1;
}

.sidebar-brand .brand-name {
    font-size: 1.5rem;
    letter-spacing: 2px;
}

.sidebar-brand .brand-full {
    font-size: 0.65rem;
    opacity: 0.5;
    letter-spacing: 0.5px;
    display: block;
    margin-top: 2px;
}

.sidebar-brand .brand-version {
    font-size: 0.7rem;
    opacity: 0.6;
    margin-top: 4px;
}

.sidebar-pin {
    position: absolute;
    top: 1rem;
    right: 0.75rem;
    background: transparent;
    border: none;
    color: rgba(255,255,255,0.4);
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 4px;
    opacity: 0;
    transition: all 0.2s;
}

.sidebar:hover .sidebar-pin,
.sidebar.tree-mode .sidebar-pin {
    opacity: 1;
}

.sidebar-pin:hover {
    color: rgba(255,255,255,0.8);
    background: rgba(255,255,255,0.1);
}

.sidebar.pinned .sidebar-pin {
    opacity: 1;
    color: var(--primary-color);
}

.sidebar-nav {
    padding: 0;
    margin: 0;
    list-style: none;
}

.sidebar-nav li a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.25rem;
    color: rgba(255,255,255,0.7);
    text-decoration: none;
    transition: all 0.2s;
    white-space: nowrap;
}

.sidebar-nav li a i {
    font-size: 1.1rem;
    min-width: 20px;
    text-align: center;
}

.sidebar-nav li a .nav-text {
    opacity: 0;
    transition: opacity var(--sidebar-transition);
}

.sidebar:hover .nav-text,
.sidebar.pinned .nav-text,
.sidebar.tree-mode .nav-text {
    opacity: 1;
}

.sidebar-nav li a:hover,
.sidebar-nav li a.active {
    background: rgba(255,255,255,0.1);
    color: #fff;
}

.sidebar-nav li a.active {
    border-left: 3px solid var(--primary-color);
}

.sidebar-section {
    font-size: 0.75rem;
    text-transform: uppercase;
    color: rgba(255,255,255,0.4);
    padding: 1.5rem 1.25rem 0.5rem;
    letter-spacing: 0.05em;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-section .section-text {
    opacity: 0;
    transition: opacity var(--sidebar-transition);
}

.sidebar:hover .section-text,
.sidebar.pinned .section-text,
.sidebar.tree-mode .section-text {
    opacity: 1;
}

.sidebar:not(:hover):not(.pinned):not(.tree-mode) .sidebar-section {
    padding: 1rem 0;
    text-align: center;
}

.sidebar:not(:hover):not(.pinned):not(.tree-mode) .sidebar-section::after {
    content: '';
    display: block;
    width: 24px;
    height: 1px;
    background: rgba(255,255,255,0.2);
    margin: 0 auto;
}

/* Tree Mode */
.sidebar-tree-toggle {
    display: none;
    padding: 0.5rem 1.25rem;
    margin-bottom: 0.5rem;
}

.sidebar-tree-toggle button {
    width: 100%;
    background: rgba(37, 99, 235, 0.2);
    border: 1px solid rgba(37, 99, 235, 0.3);
    color: #fff;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.2s;
}

.sidebar-tree-toggle button:hover {
    background: rgba(37, 99, 235, 0.3);
}

.sidebar-tree-toggle button.active {
    background: var(--primary-color);
}

body.estimate-page .sidebar-tree-toggle {
    display: block;
}

body.estimate-page .sidebar:hover .sidebar-tree-toggle,
body.estimate-page .sidebar.pinned .sidebar-tree-toggle,
body.estimate-page .sidebar.tree-mode .sidebar-tree-toggle {
    display: block;
}

body.estimate-page .sidebar:not(:hover):not(.pinned):not(.tree-mode) .sidebar-tree-toggle {
    display: none;
}

.equipment-tree {
    display: none;
    padding: 0 0.5rem;
    overflow-y: auto;
    max-height: calc(100vh - 200px);
}

.sidebar.tree-mode .equipment-tree {
    display: block;
}

.sidebar.tree-mode .sidebar-nav-main {
    display: none;
}

.tree-node {
    user-select: none;
}

.tree-node-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.5rem;
    color: rgba(255,255,255,0.8);
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.15s;
}

.tree-node-header:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
}

.tree-node-header .tree-icon {
    font-size: 0.75rem;
    width: 16px;
    text-align: center;
    color: rgba(255,255,255,0.5);
    transition: transform 0.2s;
}

.tree-node.expanded > .tree-node-header .tree-icon {
    transform: rotate(90deg);
}

.tree-node-header .tree-type-icon {
    font-size: 1rem;
    min-width: 18px;
}

.tree-node-header .tree-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tree-node-header .tree-count {
    font-size: 0.7rem;
    background: rgba(255,255,255,0.15);
    padding: 0.1rem 0.4rem;
    border-radius: 10px;
    color: rgba(255,255,255,0.6);
}

.tree-children {
    display: none;
    padding-left: 1rem;
}

.tree-node.expanded > .tree-children {
    display: block;
}

.tree-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.5rem 0.35rem 1.5rem;
    color: rgba(255,255,255,0.7);
    font-size: 0.8rem;
    border-radius: 4px;
    cursor: grab;
    transition: all 0.15s;
}

.tree-item:hover {
    background: rgba(37, 99, 235, 0.3);
    color: #fff;
}

.tree-item:active {
    cursor: grabbing;
}

.tree-item.dragging {
    opacity: 0.5;
}

.tree-item .item-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tree-item .item-price {
    font-size: 0.7rem;
    color: rgba(255,255,255,0.5);
}

.tree-item .item-drag-icon {
    opacity: 0.4;
    font-size: 0.75rem;
}

.tree-search-results .tree-item {
    padding-left: 0.5rem;
}

.tree-search-results .search-result-type {
    font-size: 0.65rem;
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    margin-left: 0.25rem;
}

.tree-search-results .search-result-type.equipment {
    background: rgba(34, 197, 94, 0.3);
    color: #86efac;
}

.tree-search-results .search-result-type.subrent {
    background: rgba(168, 85, 247, 0.3);
    color: #d8b4fe;
}

.tree-items-list {
    max-height: 300px;
    overflow-y: auto;
    margin-top: 0.25rem;
}

.tree-search {
    padding: 0.5rem;
    margin-bottom: 0.5rem;
}

.tree-search input {
    width: 100%;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    color: #fff;
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    font-size: 0.85rem;
}

.tree-search input::placeholder {
    color: rgba(255,255,255,0.4);
}

.tree-search input:focus {
    outline: none;
    border-color: var(--primary-color);
    background: rgba(255,255,255,0.15);
}

/* Edit mode styles */
.tree-edit-toggle {
    display: none;
    padding: 0.25rem 0.5rem 0.5rem;
}

.sidebar.tree-mode .tree-edit-toggle {
    display: block;
}

.tree-edit-toggle button {
    width: 100%;
    background: rgba(168, 85, 247, 0.2);
    border: 1px solid rgba(168, 85, 247, 0.3);
    color: #fff;
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.2s;
}

.tree-edit-toggle button:hover {
    background: rgba(168, 85, 247, 0.3);
}

.tree-edit-toggle button.active {
    background: #a855f7;
}

.edit-mode .tree-item {
    cursor: pointer;
}

.edit-mode .tree-item .item-checkbox {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.4);
    border-radius: 3px;
    margin-right: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
}

.edit-mode .tree-item.selected .item-checkbox {
    background: var(--primary-color);
    border-color: var(--primary-color);
}

.edit-mode .tree-item.selected .item-checkbox::after {
    content: "\2713";
    color: #fff;
    font-size: 10px;
}

.edit-mode .tree-item.selected {
    background: rgba(37, 99, 235, 0.25);
}

.edit-mode .tree-item:not(.selected):hover .item-checkbox {
    border-color: rgba(255,255,255,0.7);
}

.edit-mode .tree-item .item-drag-icon {
    display: none;
}

.edit-mode .tree-node[data-type="category"].drop-target > .tree-node-header {
    background: rgba(37, 99, 235, 0.4);
    outline: 2px dashed var(--primary-color);
    outline-offset: -2px;
}

.tree-selection-info {
    display: none;
    padding: 0.5rem;
    background: rgba(37, 99, 235, 0.2);
    border-radius: 6px;
    margin: 0.5rem;
    font-size: 0.8rem;
    color: rgba(255,255,255,0.9);
}

.tree-selection-info.visible {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.tree-selection-info .selection-hint {
    font-size: 0.75rem;
    opacity: 0.7;
    margin-left: 0.5rem;
}

.tree-selection-info button {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.3);
    color: #fff;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.75rem;
}

.tree-selection-info button:hover {
    background: rgba(255,255,255,0.1);
}

.tree-node.empty-category > .tree-node-header {
    opacity: 0.6;
}

.tree-node.empty-category > .tree-node-header .tree-name {
    font-style: italic;
}

.tree-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    color: rgba(255,255,255,0.5);
}

.tree-back-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    margin: 0.5rem;
    background: rgba(255,255,255,0.1);
    border: none;
    color: rgba(255,255,255,0.8);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s;
}

.tree-back-btn:hover {
    background: rgba(255,255,255,0.2);
    color: #fff;
}

/* Drop zones */
.drop-zone-active {
    outline: 2px dashed var(--primary-color) !important;
    outline-offset: -2px;
    background: rgba(37, 99, 235, 0.05) !important;
}

.drop-zone-hover {
    outline: 2px solid var(--primary-color) !important;
    background: rgba(37, 99, 235, 0.1) !important;
}

.drag-ghost {
    position: fixed;
    padding: 0.5rem 1rem;
    background: var(--primary-color);
    color: #fff;
    border-radius: 6px;
    font-size: 0.85rem;
    pointer-events: none;
    z-index: 10000;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    max-width: 250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Main content */
.main-content {
    margin-left: var(--sidebar-collapsed-width);
    padding: 2rem;
    min-height: 100vh;
    transition: margin-left var(--sidebar-transition);
}

body.sidebar-pinned .main-content {
    margin-left: var(--sidebar-width);
}

body.sidebar-tree-mode .main-content {
    margin-left: var(--sidebar-tree-width);
}

.card {
    border: none;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.card-header {
    background: #fff;
    border-bottom: 1px solid #e5e7eb;
    padding: 1rem 1.25rem;
    border-radius: 12px 12px 0 0 !important;
}

.btn-primary {
    background: var(--primary-color);
    border: none;
}

.btn-primary:hover {
    background: #1d4ed8;
}

.table th {
    font-weight: 600;
    font-size: 0.875rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.alert {
    border: none;
    border-radius: 8px;
}

.form-control, .form-select {
    border-radius: 8px;
    border: 1px solid #d1d5db;
}

.form-control:focus, .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s, width var(--sidebar-transition);
        width: var(--sidebar-width);
    }

    .sidebar.show {
        transform: translateX(0);
    }

    .sidebar.tree-mode {
        width: 100%;
    }

    .main-content {
        margin-left: 0 !important;
    }

    .mobile-toggle {
        display: block !important;
    }

    .sidebar-pin {
        display: none;
    }
}

.mobile-toggle {
    display: none;
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 1001;
    background: var(--primary-color);
    color: #fff;
    border: none;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
}

@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 1s linear infinite; }

/* ============================================
   Responsive: Tablets & Small Screens
   ============================================ */
@media (max-width: 767.98px) {
    /* Tables: horizontal scroll on mobile */
    .table-responsive-custom {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Cards: full-width, less padding */
    .card {
        border-radius: 8px;
    }
    .card-body {
        padding: 0.75rem;
    }

    /* Forms: larger touch targets */
    .form-control, .form-select, .btn {
        min-height: 42px;
        font-size: 0.95rem;
    }

    /* Modals: full-screen on mobile */
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100vh;
    }
    .modal-content {
        border-radius: 0;
        min-height: 100vh;
    }

    /* Badge + small elements */
    .badge {
        font-size: 0.75rem;
        padding: 0.35em 0.5em;
    }

    /* Project header: stack vertically */
    .project-header {
        flex-direction: column;
        gap: 0.5rem;
    }

    /* Gantt: horizontal scroll */
    .gantt-card {
        overflow-x: auto;
    }

    /* Selection toolbar: compact on mobile */
    .selection-toolbar {
        flex-wrap: wrap;
        gap: 0.25rem;
    }
    .selection-toolbar select,
    .selection-toolbar input {
        font-size: 0.8rem;
        min-height: 36px;
    }

    /* Date cards: smaller */
    .date-card {
        padding: 0.5rem;
    }

    /* Finance section */
    .finance-row {
        flex-wrap: wrap;
    }

    /* Staff cards */
    .staff-row {
        flex-wrap: wrap;
    }

    /* Toast notifications */
    .toast-container {
        max-width: 90vw;
    }

    /* Bottom safe area for phones */
    .main-content {
        padding-bottom: env(safe-area-inset-bottom, 20px);
    }
}

/* Extra small screens (phones) */
@media (max-width: 575.98px) {
    .main-content {
        padding: 0.5rem;
    }

    h1, .h1 { font-size: 1.4rem; }
    h2, .h2 { font-size: 1.2rem; }
    h3, .h3 { font-size: 1.1rem; }

    /* Estimate item rows: more compact */
    .item-row td {
        padding: 0.35rem 0.25rem;
        font-size: 0.85rem;
    }

    /* Hide less-important columns on phone */
    .d-phone-none {
        display: none !important;
    }
}

/* Touch device optimizations */
@media (hover: none) {
    /* Larger click targets */
    .btn-sm {
        min-height: 36px;
        min-width: 36px;
    }

    /* Remove hover-only effects */
    .editable:hover {
        background: inherit;
    }

    /* Better scrolling */
    .table-responsive-custom {
        -webkit-overflow-scrolling: touch;
    }
}
