/* Custom styles for Project Financial Performance App - Bootstrap Litera theme */

/* Override Bootstrap table styles for custom functionality */
#dataTable {
    min-width: 800px;
}

#dataTable thead {
    position: sticky;
    top: 0;
    z-index: 10;
}

/* Scrolling headers should NOT be sticky - they inherit vertical stickiness from thead */
/* They should just be regular positioned elements with lower z-index */
#tableHeader th:not(:nth-child(1)):not(:nth-child(2)):not(:nth-child(3)):not(:nth-child(4)):not(:nth-child(5)) {
    position: relative !important; /* Regular positioning, not sticky */
    z-index: 1 !important; /* Lower z-index than frozen columns */
    background-color: #34495e !important;
    color: white !important;
}

/* Blocker element to prevent scrolling headers from overlapping (created by JS) */
#frozen-column-blocker {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    background: transparent;
    z-index: 27;
    pointer-events: none;
}

/* Table header row */
#tableHeader {
    background-color: #34495e !important;
}

#tableHeader th {
    background-color: #34495e !important;
    color: white !important;
    border-color: rgba(255,255,255,0.2) !important;
}

#dataTable thead th {
    position: relative;
    white-space: nowrap;
    border-right: 1px solid rgba(255,255,255,0.2) !important;
    color: white !important;
}

/* Ensure all header cells in thead have proper styling */
#dataTable thead th:not(.table-light):not(.table-dark) {
    background-color: #34495e !important;
    color: white !important;
}

/* Override Bootstrap table-hover for header */
#dataTable thead tr:hover th {
    background-color: #34495e !important;
    color: white !important;
}

/* Make Project Type column narrow */
#dataTable th:first-child:not([style*="display: none"]),
#dataTable td:first-child:not([style*="display: none"]) {
    min-width: 120px;
    width: 120px;
    font-size: 0.875rem;
}

/* Ensure hidden columns take no space */
#dataTable th[style*="display: none"],
#dataTable td[style*="display: none"] {
    width: 0 !important;
    min-width: 0 !important;
    padding: 0 !important;
    border: none !important;
}

/* Make Project Name column wider */
#dataTable th:nth-child(2):not([style*="display: none"]),
#dataTable td:nth-child(2):not([style*="display: none"]) {
    min-width: 200px;
    width: 200px;
}

/* Set fixed width for Project Manager column (3rd column) */
#dataTable th:nth-child(3):not([style*="display: none"]),
#dataTable td:nth-child(3):not([style*="display: none"]) {
    min-width: 150px;
    width: 150px;
}

/* Set fixed width for Sales Order # column (4th column) */
#dataTable th:nth-child(4):not([style*="display: none"]),
#dataTable td:nth-child(4):not([style*="display: none"]) {
    min-width: 120px;
    width: 120px;
}

/* Set fixed width for Sales Order Amount column (5th column) */
#dataTable th:nth-child(5):not([style*="display: none"]),
#dataTable td:nth-child(5):not([style*="display: none"]) {
    min-width: 150px;
    width: 150px;
}

/* Set consistent width for week columns (6th column onwards) */
#dataTable thead th:nth-child(n+6),
#dataTable tbody td:nth-child(n+6) {
    min-width: 100px;
    width: 100px;
    white-space: nowrap;
}

/* Freeze first 5 columns (sticky positioning) */
#dataTable thead th:nth-child(1),
#dataTable thead th:nth-child(2),
#dataTable thead th:nth-child(3),
#dataTable thead th:nth-child(4),
#dataTable thead th:nth-child(5),
#dataTable tbody td:nth-child(1),
#dataTable tbody td:nth-child(2),
#dataTable tbody td:nth-child(3),
#dataTable tbody td:nth-child(4),
#dataTable tbody td:nth-child(5) {
    position: sticky !important;
    background-color: inherit;
}


/* Set left offsets and z-index for frozen columns - headers need higher z-index */
#dataTable thead th:nth-child(1),
#dataTable tbody td:nth-child(1) {
    left: 0;
}

#dataTable thead th:nth-child(1) {
    z-index: 25 !important;
    background-color: #34495e !important;
    isolation: isolate;
}

#tableHeader th:nth-child(1) {
    z-index: 25 !important;
    isolation: isolate;
    position: sticky !important;
}

#dataTable tbody td:nth-child(1) {
    z-index: 5; /* Lower z-index for data cells */
}

#dataTable thead th:nth-child(2),
#dataTable tbody td:nth-child(2) {
    left: 120px; /* Will be dynamically adjusted */
}

#dataTable thead th:nth-child(2) {
    z-index: 25 !important;
    background-color: #34495e !important;
    isolation: isolate;
}

#tableHeader th:nth-child(2) {
    z-index: 25 !important;
    isolation: isolate;
    position: sticky !important;
}

#dataTable tbody td:nth-child(2) {
    z-index: 5; /* Lower z-index for data cells */
}

#dataTable thead th:nth-child(3),
#dataTable tbody td:nth-child(3) {
    left: 320px; /* Will be dynamically adjusted */
}

#dataTable thead th:nth-child(3) {
    z-index: 25 !important;
    background-color: #34495e !important;
    isolation: isolate;
}

#tableHeader th:nth-child(3) {
    z-index: 25 !important;
    isolation: isolate;
    position: sticky !important;
}

#dataTable tbody td:nth-child(3) {
    z-index: 5; /* Lower z-index for data cells */
}

#dataTable thead th:nth-child(4),
#dataTable tbody td:nth-child(4) {
    left: 470px; /* Will be dynamically adjusted */
}

#dataTable thead th:nth-child(4) {
    z-index: 25 !important;
    background-color: #34495e !important;
    isolation: isolate;
}

#tableHeader th:nth-child(4) {
    z-index: 25 !important;
    isolation: isolate;
    position: sticky !important;
}

#dataTable tbody td:nth-child(4) {
    z-index: 5; /* Lower z-index for data cells */
}

#dataTable thead th:nth-child(5),
#dataTable tbody td:nth-child(5) {
    left: 590px; /* Will be dynamically adjusted */
}

#dataTable thead th:nth-child(5) {
    z-index: 25 !important;
    background-color: #34495e !important;
    isolation: isolate;
}

#tableHeader th:nth-child(5) {
    z-index: 25 !important;
    isolation: isolate;
    position: sticky !important;
}

#dataTable tbody td:nth-child(5) {
    z-index: 5; /* Lower z-index for data cells */
}

/* Ensure header cells have proper background */
#dataTable thead th:nth-child(1),
#dataTable thead th:nth-child(2),
#dataTable thead th:nth-child(3),
#dataTable thead th:nth-child(4),
#dataTable thead th:nth-child(5) {
    background-color: #34495e !important;
}


/* Ensure data cells have white background when frozen (except totals row) */
#dataTable tbody tr:not(.totals-row) td:nth-child(1),
#dataTable tbody tr:not(.totals-row) td:nth-child(2),
#dataTable tbody tr:not(.totals-row) td:nth-child(3),
#dataTable tbody tr:not(.totals-row) td:nth-child(4),
#dataTable tbody tr:not(.totals-row) td:nth-child(5) {
    background-color: white !important;
}

/* Ensure frozen cells maintain background on hover (except totals row) */
#dataTable tbody tr:not(.totals-row):hover td:nth-child(1),
#dataTable tbody tr:not(.totals-row):hover td:nth-child(2),
#dataTable tbody tr:not(.totals-row):hover td:nth-child(3),
#dataTable tbody tr:not(.totals-row):hover td:nth-child(4),
#dataTable tbody tr:not(.totals-row):hover td:nth-child(5) {
    background-color: #f8f9fa !important;
}

/* Add shadow to frozen columns for visual separation */
#dataTable thead th:nth-child(5),
#dataTable tbody td:nth-child(5) {
    box-shadow: 2px 0 5px rgba(0,0,0,0.1);
}

/* Ensure table container allows horizontal scrolling */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    /* Force scrollbar to be visible when content overflows */
    scrollbar-width: thin;
    scrollbar-color: #cbd5e0 #f7fafc;
}

/* Style scrollbar for webkit browsers (Chrome, Safari, Edge) */
.table-responsive::-webkit-scrollbar {
    height: 12px;
}

.table-responsive::-webkit-scrollbar-track {
    background: #f7fafc;
    border-radius: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 6px;
    border: 2px solid #f7fafc;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
    background: #a0aec0;
}

/* Add a mask/clip to prevent scrolling headers from overlapping frozen columns */
.table-responsive::before {
    content: '';
    position: sticky;
    left: 0;
    top: 0;
    width: 0;
    height: 100%;
    z-index: 30;
    pointer-events: none;
    box-shadow: none;
}

/* Calculate the width of frozen columns and clip scrolling content */
#dataTable {
    position: relative;
}

/* Ensure frozen columns are always rendered on top */
#dataTable thead th:nth-child(1),
#dataTable thead th:nth-child(2),
#dataTable thead th:nth-child(3),
#dataTable thead th:nth-child(4),
#dataTable thead th:nth-child(5) {
    will-change: transform;
    transform: translateZ(0); /* Force GPU acceleration */
}


/* Column hide icon */
.column-hide-icon {
    position: absolute;
    top: 2px;
    right: 2px;
    cursor: pointer;
    font-size: 14px;
    opacity: 0.7;
    padding: 2px;
    z-index: 20;
}

.column-hide-icon:hover {
    opacity: 1;
    transform: scale(1.2);
}

/* Sortable column indicators */
th.sortable {
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 30px;
}

/* Ensure scrolling sortable headers maintain proper positioning and z-index */
#tableHeader th.sortable:not(:nth-child(1)):not(:nth-child(2)):not(:nth-child(3)):not(:nth-child(4)):not(:nth-child(5)) {
    position: relative !important; /* Regular positioning */
    z-index: 1 !important; /* Lower than frozen columns */
}

th.sortable:hover {
    background-color: rgba(255,255,255,0.1) !important;
    color: white !important;
}

th.sortable::after {
    content: ' ⇅';
    position: absolute;
    right: 10px;
    opacity: 0.5;
    z-index: 1; /* Ensure sort indicator stays within its cell */
}

th.sortable.sort-asc::after {
    content: ' ↑';
    opacity: 1;
}

th.sortable.sort-desc::after {
    content: ' ↓';
    opacity: 1;
}

/* Month separator (visual border when new month starts in week columns) */
#tableHeader th.month-start,
#dataTable tbody td.month-start {
    border-left: 4px solid #e74c3c !important;
    padding-left: 8px;
}

/* Totals row styling */
tr.totals-row {
    background-color: #2c3e50 !important;
    color: white !important;
    font-weight: bold;
    position: sticky;
    top: 40px; /* Below the header row */
    z-index: 9;
}

tr.totals-row td {
    background-color: #2c3e50 !important;
    color: white !important;
    border-top: 2px solid #e74c3c !important;
    border-bottom: 2px solid #e74c3c !important;
}

/* Frozen columns in totals row */
tr.totals-row td:nth-child(1),
tr.totals-row td:nth-child(2),
tr.totals-row td:nth-child(3),
tr.totals-row td:nth-child(4),
tr.totals-row td:nth-child(5) {
    background-color: #2c3e50 !important;
    z-index: 24;
}

/* Historical vs Projection styling - only apply if no heatmap color */
td.historical:not(.heatmap-green):not(.heatmap-red) {
    background-color: #d5f4e6 !important;
    font-weight: 500;
}

td.projection:not(.heatmap-green):not(.heatmap-red) {
    background-color: #ffeaa7 !important;
    font-weight: 500;
    font-style: italic;
}

/* Heatmap colors take precedence over historical/projection */
td.performance-cell.heatmap-green,
td.performance-cell.heatmap-red {
    /* Background color is set via inline style in JavaScript */
    /* The inline style + class selector ensures it overrides base colors */
}

/* Performance cell with overlay layout */
td.performance-cell {
    position: relative;
    padding: 8px !important;
}

.performance-value {
    display: block;
    font-size: 1em;
    font-weight: 400;
    text-align: left;
    line-height: 1.2;
    color: #000;
}

.target-value {
    display: block;
    font-size: 0.75em;
    font-weight: 400;
    text-align: right;
    line-height: 1;
    color: #6c757d;
    margin-top: 4px;
    position: absolute;
    bottom: 4px;
    right: 8px;
}

/* Hidden columns dropdown styling */
#hiddenColumnsList {
    min-width: 200px;
}

#hiddenColumnsList .dropdown-item:hover {
    background-color: #f8f9fa;
    cursor: pointer;
}

/* Query Builder Styling */
#query-builder .rule-container {
    border: 1px solid #dee2e6;
    border-radius: 4px;
    padding: 10px;
    margin-bottom: 10px;
}

#query-builder .rule-header {
    margin-bottom: 10px;
}

#query-builder .btn-group .btn {
    font-size: 0.875rem;
}

#query-builder .rule-filter-container,
#query-builder .rule-operator-container,
#query-builder .rule-value-container {
    margin-right: 10px;
}

.query-builder .rules-group-container {
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
    padding: 15px;
    margin: 10px 0;
}

.query-builder .rules-group-header {
    margin-bottom: 10px;
}

/* Ensure table is responsive */
.table-responsive {
    overflow-x: auto;
}

/* Navigation Bar Styles */
.navbar-brand {
    font-weight: 600;
    font-size: 1.25rem;
}

.navbar {
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.nav-link {
    font-weight: 500;
    padding: 0.5rem 1rem !important;
}

.nav-link.active {
    font-weight: 600;
}

/* Adjust body to account for navbar */
body {
    padding-top: 0;
}

/* Compact Toolbar Styles */
.compact-toolbar {
    transition: all 0.3s ease;
}

/* Search box - expand on focus */
#searchInput {
    transition: all 0.3s ease;
}

#searchInput:focus {
    max-width: 400px !important;
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

/* Advanced filter button - highlight when active */
.btn-outline-primary[data-bs-toggle="collapse"][aria-expanded="true"] {
    background-color: #0d6efd;
    color: white;
}

/* Refresh button icon style */
.btn-primary:hover {
    /* Rotation animation removed */
}

/* Monthly Target Styles */

/* Input cells for monthly targets */
.target-input {
    width: 100%;
    border: 1px solid #dee2e6;
    padding: 4px 8px;
    text-align: right;
    font-size: 0.875rem;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.target-input:focus {
    outline: 2px solid #0d6efd;
    outline-offset: -2px;
    border-color: #0d6efd;
    background-color: #fff;
}

.target-input.saving {
    background-color: #fff3cd;
    border-color: #ffc107;
}

.target-input.saved {
    background-color: #d1e7dd;
    border-color: #198754;
    animation: fadeToNormal 2s ease-in-out;
}

@keyframes fadeToNormal {
    0% {
        background-color: #d1e7dd;
        border-color: #198754;
    }
    100% {
        background-color: #fff;
        border-color: #dee2e6;
    }
}

/* Version selector */
.version-selector {
    min-width: 200px;
    max-width: 300px;
}

/* Remove spinner from number inputs */
.target-input::-webkit-inner-spin-button,
.target-input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.target-input[type=number] {
    -moz-appearance: textfield;
}

/* AG Grid Custom Styling */

/* Make AG Grid blend with Bootstrap theme */
.ag-theme-alpine {
    --ag-font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --ag-font-size: 14px;
    --ag-header-height: 40px;
    --ag-row-height: 35px;
    --ag-header-background-color: #f8f9fa;
    --ag-header-foreground-color: #212529;
    --ag-odd-row-background-color: #ffffff;
    --ag-row-hover-color: #f5f5f5;
    --ag-selected-row-background-color: #e7f1ff;
    --ag-range-selection-background-color: rgba(13, 110, 253, 0.2);
    --ag-range-selection-border-color: #0d6efd;
}

/* Editable cells styling */
.ag-theme-alpine .editable-cell {
    background-color: #fff;
    cursor: cell;
    position: relative;
}

.ag-theme-alpine .editable-cell:hover {
    background-color: #f0f8ff;
    outline: 1px solid #0d6efd;
}

.ag-theme-alpine .editable-cell.ag-cell-focus {
    outline: 2px solid #0d6efd !important;
    outline-offset: -2px;
}

/* Pinned (frozen) columns styling */
.ag-theme-alpine .ag-pinned-left-header {
    border-right: 2px solid #dee2e6 !important;
}

.ag-theme-alpine .ag-pinned-left-cols-container {
    border-right: 2px solid #dee2e6 !important;
    box-shadow: 2px 0 5px rgba(0,0,0,0.1);
}

/* Cell editing highlight */
.ag-theme-alpine .ag-cell-inline-editing {
    background-color: #fff3cd !important;
    border: 2px solid #ffc107 !important;
    padding: 0 !important;
}

/* Fill handle (drag to fill) */
.ag-theme-alpine .ag-fill-handle {
    background-color: #0d6efd !important;
    width: 8px !important;
    height: 8px !important;
    border: 2px solid #fff !important;
    border-radius: 2px !important;
}

.ag-theme-alpine .ag-fill-handle:hover {
    background-color: #0a58ca !important;
    cursor: crosshair !important;
}

/* Range selection styling */
.ag-theme-alpine .ag-cell-range-selected:not(.ag-cell-focus) {
    background-color: rgba(13, 110, 253, 0.1);
}

.ag-theme-alpine .ag-cell-range-single-cell {
    border: 2px solid #0d6efd !important;
}

.ag-theme-alpine .ag-cell-range-selected {
    border-color: #0d6efd !important;
}

/* Make the range selection border more visible */
.ag-theme-alpine .ag-cell-range-top {
    border-top: 2px solid #0d6efd !important;
}

.ag-theme-alpine .ag-cell-range-bottom {
    border-bottom: 2px solid #0d6efd !important;
}

.ag-theme-alpine .ag-cell-range-left {
    border-left: 2px solid #0d6efd !important;
}

.ag-theme-alpine .ag-cell-range-right {
    border-right: 2px solid #0d6efd !important;
}

/* Cell flash on change */
.ag-theme-alpine .ag-cell-data-changed {
    background-color: #d1e7dd !important;
}

/* Full page layout - scoped to pages with full-page-layout class */
html.full-page-layout, html.full-page-layout body {
    height: 100%;
    margin: 0;
    overflow: hidden;
}

html.full-page-layout body {
    display: flex;
    flex-direction: column;
}

/* Navbar should maintain normal layout */
html.full-page-layout .navbar {
    flex-shrink: 0;
}

/* Ensure navbar doesn't collapse on full-page-layout */
html.full-page-layout .navbar-collapse {
    display: flex !important;
    flex-basis: auto;
    justify-content: flex-start !important;
}

html.full-page-layout .navbar-nav {
    flex-direction: row !important;
    align-items: center;
}

html.full-page-layout .navbar .container-fluid {
    display: flex !important;
    align-items: center;
    justify-content: flex-start !important;
}

/* Make container take remaining space (body content, not navbar) */
html.full-page-layout body > .container-fluid {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Grid container should grow to fill available space */
html.full-page-layout #myGrid {
    height: 100% !important;
    width: 100%;
}

html.full-page-layout .card:has(#myGrid) {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin-bottom: 0 !important;
}

html.full-page-layout .card:has(#myGrid) .card-body {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* REMOVE ALL CUSTOM SPACING - Let AG Grid handle it naturally */

/* Totals row styling (pinned top row) */
.ag-theme-alpine .ag-row-pinned-top {
    background-color: #2c3e50 !important;
    color: white !important;
    font-weight: bold !important;
    border-bottom: 1px solid #dee2e6 !important;
}

.ag-theme-alpine .ag-row-pinned-top .ag-cell {
    background-color: #2c3e50 !important;
    color: white !important;
    font-weight: bold !important;
}

/* Ensure pinned columns in totals row also have dark background */
.ag-theme-alpine .ag-pinned-left-cols-container .ag-row-pinned-top .ag-cell {
    background-color: #2c3e50 !important;
    color: white !important;
}

/* First pinned row (TOTAL) should be darker and have a thicker border */
.ag-theme-alpine .ag-row-pinned-top:first-child {
    background-color: #1a252f !important;
    border-bottom: 2px solid #e74c3c !important;
}

.ag-theme-alpine .ag-row-pinned-top:first-child .ag-cell {
    background-color: #1a252f !important;
}

.ag-theme-alpine .ag-pinned-left-cols-container .ag-row-pinned-top:first-child .ag-cell {
    background-color: #1a252f !important;
}

/* Last subtotal row should have thicker bottom border */
.ag-theme-alpine .ag-row-pinned-top:last-child {
    border-bottom: 2px solid #e74c3c !important;
}

/* Make header text bold */
.ag-theme-alpine .ag-header-cell-text {
    font-weight: 600;
}

/* Numeric column alignment */
.ag-theme-alpine .ag-cell.ag-right-aligned-cell {
    text-align: right;
}

/* Loading overlay */
.ag-theme-alpine .ag-overlay-loading-center {
    background-color: rgba(255, 255, 255, 0.9);
    padding: 20px;
    border-radius: 8px;
}

/* Context menu styling */
.ag-theme-alpine .ag-menu {
    border: 1px solid #dee2e6;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

/* Make column separators visible */
.ag-theme-alpine .ag-header-cell::after,
.ag-theme-alpine .ag-header-group-cell::after {
    border-right: 1px solid #dee2e6;
}

/* Status bar styling */
.ag-theme-alpine .ag-status-bar {
    background-color: #f8f9fa;
    border-top: 2px solid #dee2e6;
    font-size: 13px;
    padding: 8px 12px;
}

.ag-theme-alpine .ag-status-name-value-value {
    font-weight: 600;
    color: #0d6efd;
}

/* Side bar styling */
.ag-theme-alpine .ag-side-bar {
    border-left: 1px solid #dee2e6;
    background-color: #f8f9fa;
}

.ag-theme-alpine .ag-tool-panel-wrapper {
    background-color: #fff;
}

/* Row group panel styling */
.ag-theme-alpine .ag-row-group-panel {
    background-color: #e7f1ff;
    border-bottom: 2px solid #0d6efd;
    padding: 8px;
    min-height: 40px;
}

.ag-theme-alpine .ag-column-drop-empty-message {
    color: #6c757d;
    font-style: italic;
}

/* Floating filter styling */
.ag-theme-alpine .ag-floating-filter-body input {
    border: 1px solid #dee2e6;
    border-radius: 4px;
    padding: 4px 8px;
}

.ag-theme-alpine .ag-floating-filter-body input:focus {
    border-color: #0d6efd;
    outline: none;
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

/* Column menu styling */
.ag-theme-alpine .ag-menu {
    background-color: #fff;
}

.ag-theme-alpine .ag-menu-option-active {
    background-color: #e7f1ff;
}

/* Selected rows styling */
.ag-theme-alpine .ag-row-selected {
    background-color: #e7f1ff !important;
}

.ag-theme-alpine .ag-row-selected:hover {
    background-color: #d0e7ff !important;
}
