/* Import Excel Toolbar styles */
@import '../shared/toolbar/excel-toolbar.css';

/* ============================================================================
   PROCUREMENT ANALYSIS MODULE STYLES
   Follows same patterns as gantt-container and grid sections
   ============================================================================ */

/* --- Toolbar Container (Excel Toolbar) --- */
.procurement-container .toolbar-container {
    display: block;
    width: 100%;
    background: #f8f9fa;
    border-bottom: 1px solid #dee2e6;
    z-index: 10000 !important; /* CRITICAL: Above all grid elements (headers max 100) to allow dropdowns to appear */
}

/* --- Force Toolbar to Single Row (No Wrapping) --- */
.procurement-header .toolbar-container .excel-toolbar {
    flex-wrap: nowrap !important; /* Prevent wrapping to second row */
    overflow-x: auto; /* Allow horizontal scroll if needed */
}

/* CRITICAL: Override SearchManager's display:none rule */
.procurement-header .toolbar-container .toolbar-group .search-box {
    display: flex !important; /* Flex layout - OVERRIDES display:none from SearchManager */
    align-items: center !important; /* Center vertically */
    position: relative !important; /* OVERRIDES position:fixed from SearchManager */
    width: auto !important; /* Auto width to match other groups */
    min-width: auto !important; /* Auto min-width */
    top: auto !important; /* Reset fixed positioning */
    right: auto !important; /* Reset fixed positioning */
    z-index: auto !important; /* Reset z-index */
    padding: 0 !important; /* No extra padding */
    margin: 0 !important; /* No extra margin */
    background: transparent !important; /* Transparent background */
    border: none !important; /* No border */
    box-shadow: none !important; /* No shadow */
}

/* --- FIX: Ensure Search Box Input and Icon Are Visible --- */
.procurement-header .toolbar-container .search-box input {
    display: block !important; /* Force display */
    visibility: visible !important; /* Force visibility */
    opacity: 1 !important; /* Force opacity */
    width: 180px !important; /* Force width */
    height: auto !important; /* Auto height like buttons */
    padding: 8px 12px 8px 32px !important; /* EXACT MATCH to toolbar-btn padding (8px vertical) */
    border: 1px solid #dee2e6 !important; /* Border - same as buttons */
    border-radius: 6px !important; /* EXACT MATCH to toolbar-btn radius */
    font-size: 14px !important; /* EXACT MATCH to toolbar-btn font */
    background: white !important; /* White background */
    color: #495057 !important; /* Text color - same as buttons */
    line-height: normal !important; /* Normal line-height like buttons */
    box-sizing: border-box !important; /* Border-box for consistent sizing */
}

.procurement-header .toolbar-container .search-box i {
    display: block !important; /* Force display */
    visibility: visible !important; /* Force visibility */
    opacity: 1 !important; /* Force opacity */
    position: absolute !important; /* Absolute positioning */
    left: 10px !important; /* Left position */
    top: 50% !important; /* Vertical center */
    transform: translateY(-50%) !important; /* Center vertically */
    color: #6c757d !important; /* Icon color */
    font-size: 13px !important; /* Match input font size */
    pointer-events: none !important; /* No pointer events */
}

/* --- Keep Toolbar Visible (contains Refresh, Export, Columns, Freeze buttons) --- */
/* Removed display: none - toolbar is now visible with freeze button */

/* --- Main Procurement Container --- */
.procurement-container {
    display: none; /* Hidden by default, shown when view is active */
    flex-direction: column;
    flex: 1; /* CRITICAL: Flex grow to fill parent gantt-container */
    min-height: 0; /* Allow flex shrinking */
    position: relative; /* CHANGED: Relative positioning to work inside gantt-container */
    background: #f8f9fa;
    overflow: hidden;
    /* Width/height set by JavaScript flexbox - responds to sidebar changes automatically */
}

/* --- Procurement Header --- */
.procurement-header {
    display: flex;
    flex-direction: column; /* Stack title row and toolbar vertically */
    align-items: stretch;
    padding: 0; /* No padding - title row has its own padding */
    background: white;
    border-bottom: 2px solid #dee2e6;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    position: relative;
}

/* Title row with presence avatars */
.procurement-title-row {
    border-bottom: 1px solid #e9ecef;
}

.procurement-title {
    font-size: 1.5rem;
    font-weight: 600;
    color: #2c3e50;
    margin: 0;
}

.procurement-toolbar {
    display: none !important; /* Hide this toolbar - using Excel Toolbar instead */
    gap: 12px;
    align-items: center;
}

/* --- Procurement Toolbar Buttons --- */
.procurement-toolbar button {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    background: white;
    color: #495057;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.procurement-toolbar button:hover {
    background: #f8f9fa;
    border-color: #007bff;
    color: #007bff;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0,123,255,0.2);
}

.procurement-toolbar button:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0,123,255,0.2);
}

.procurement-toolbar button svg {
    width: 16px;
    height: 16px;
}

/* Specific button colors */
.btn-procurement-refresh:hover {
    border-color: #28a745;
    color: #28a745;
}

.btn-procurement-export:hover {
    border-color: #17a2b8;
    color: #17a2b8;
}

.btn-procurement-columns:hover {
    border-color: #6c757d;
    color: #6c757d;
}

.btn-procurement-freeze:hover {
    border-color: #2196f3;
    color: #2196f3;
}

/* Active/highlighted state for freeze button (when columns are frozen) */
.btn-procurement-freeze.active {
    background: #e3f2fd !important;
    border-color: #2196f3 !important;
    color: #1976d2 !important;
    box-shadow: 0 2px 4px rgba(33,150,243,0.3) !important;
}

.btn-procurement-close:hover {
    border-color: #dc3545;
    color: #dc3545;
}

/* --- Procurement Grid Container --- */
.procurement-grid-container {
    flex: 1;
    overflow: auto;
    padding: 0; /* No padding */
    background: #f8f9fa;
}

/* --- Cell Content Wrapper (for column groups) --- */
/* CRITICAL: Required for text-align to work properly */
.procurement-grid-container .cell-content {
    display: block;
    width: 100%;
    min-height: 20px; /* Ensure cell has height even when empty */
}

/* Ensure vertical alignment works on cells */
.procurement-grid-container td {
    vertical-align: middle; /* Default vertical alignment */
}

/* When vertical alignment is set via inline style, override */
.procurement-grid-container td[style*="vertical-align"] .cell-content {
    display: flex;
    align-items: inherit; /* Inherit from parent td */
    height: 100%;
}

/* --- Procurement Grid Wrapper --- */
.procurement-grid-wrapper {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    overflow: hidden;
}

/* --- Procurement Table --- */
.procurement-table {
    width: 100%;
    border-collapse: separate; /* 🔑 CRITICAL: Must be 'separate' for sticky positioning to work */
    border-spacing: 0; /* Keep borders tight with no gaps */
    font-size: 14px;
    table-layout: auto; /* Allow columns to size based on content */
}

/* --- Procurement Table Head (Sticky Positioning) --- */
.procurement-table thead {
    position: sticky;
    top: 0;
    z-index: 100; /* Above all body rows */
    background: #f3f4f6; /* Ensure opaque background */
}

/* --- Procurement Header Row (Multi-Level Sticky Support) --- */
.procurement-header-row {
    background: linear-gradient(180deg, #f8f9fa 0%, #e9ecef 100%);
    border-bottom: 2px solid #dee2e6;
    position: sticky; /* 🔑 CRITICAL FIX: Stick header to top */
    /* Note: top value set dynamically via JavaScript for multi-level headers */
    z-index: 20; /* 🔑 Base z-index, adjusted per level */
    /* Fix border shifting during scroll */
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Simple table with single header row (no nesting) */
.procurement-header-row:not([data-level]):not(.column-header-row):not(.filter-row) {
    top: 0;
    z-index: 20;
}

/* Multi-level nested headers - z-index decreases by level */
.procurement-header-row[data-level="0"] {
    z-index: 24; /* Topmost nested level */
}

.procurement-header-row[data-level="1"] {
    z-index: 23; /* Second nested level */
}

.procurement-header-row[data-level="2"] {
    z-index: 22; /* Third nested level (if exists) */
}

/* Column header row (below nested groups) */
.procurement-header-row.column-header-row {
    z-index: 21;
}

/* Filter row (bottom of header stack) */
.procurement-header-row.filter-row {
    z-index: 20;
}

.procurement-header-cell {
    padding: 6px 6px;
    padding-right: 18px;
    font-weight: 600;
    color: #495057;
    text-align: center;
    border-right: 1px solid #dee2e6;
    position: relative;
    white-space: nowrap;
    user-select: none;
    /* Fix border shifting during scroll */
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.procurement-header-cell:last-child {
    border-right: none;
}

/* Sticky first column in header - HIGHEST z-index */
.procurement-header-cell.sticky-col {
    position: sticky;
    left: 0;
    z-index: 30; /* Above all header rows (20-24) for proper sticky intersection */
    background: linear-gradient(180deg, #f8f9fa 0%, #e9ecef 100%);
    box-shadow: 2px 0 4px rgba(0,0,0,0.05);
}

/* --- Procurement Data Rows --- */
#procurement-table tbody tr {
    border-bottom: 1px solid #e9ecef;
    transition: background-color 0.15s ease;
}

#procurement-table tbody tr:hover {
    background-color: #f1f3f5;
}

#procurement-table tbody tr.zebra-even {
    background-color: #fafbfc;
}

#procurement-table tbody tr.zebra-even:hover {
    background-color: #f1f3f5;
}

/* --- Procurement Cells --- */
.procurement-cell {
    padding: 10px 16px;
    border-right: 1px solid #e9ecef;
    position: relative;
    vertical-align: middle;
    user-select: none; /* Prevent text selection during range selection */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.procurement-cell:last-child {
    border-right: none;
}

/* --- Cell Text Wrapping (Allow multi-line content) --- */
/* Apply to all td elements in the excel-grid-table */
#procurement-table td,
.excel-grid-table td {
    white-space: normal;           /* Allow text to wrap */
    word-wrap: break-word;         /* Break long words */
    overflow-wrap: break-word;     /* Modern standard for word-wrap */
    max-width: 300px;              /* Limit cell width to force wrapping */
    vertical-align: middle;        /* Center text vertically in cell */
    text-align: center;            /* Default: center all cells horizontally */
    line-height: 1.15;             /* Compact line height (matches font-size feature) */
}

/* Exceptions: ID and Item columns stay left-aligned */
#procurement-table td[data-field="id"],
#procurement-table td[data-field="item"] {
    text-align: left !important;
}
#procurement-table td[data-field="id"] .cell-content,
#procurement-table td[data-field="item"] .cell-content {
    justify-content: flex-start !important;
    text-align: left !important;
}

/* --- Breakdown View Link (Display Breakdown column) --- */
/* Center content horizontally and vertically */
td[data-field="displayBreakdown"] {
    text-align: center;
    vertical-align: middle !important;
    /* Position relative needed for absolute centering of child */
    position: relative;
}

/* Use absolute positioning for true vertical centering in table cells */
td[data-field="displayBreakdown"] .cell-content {
    position: absolute !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    width: auto !important;
    display: inline-flex !important;
    align-items: center !important;
}

.breakdown-view-link {
    display: inline-flex;
    align-items: center;
    color: #007bff;
    text-decoration: none;
    font-weight: 500;
}

.breakdown-view-link:hover {
    text-decoration: underline;
}

/* Row height controlled by JavaScript (Grid Settings + RowResizingManager) */
/* Do NOT set min-height or height here - it overrides JS row height settings */
#procurement-table tbody tr,
.excel-grid-table tbody tr {
    /* min-height and height managed by _applyRowHeight() in procurement-manager.js */
}

/* Sticky first column for data cells - Below headers, above body */
.procurement-cell.sticky-col {
    position: sticky;
    left: 0;
    z-index: 15; /* Above body rows, below header rows (20-24), below header sticky (30) */
    background: white;
    font-weight: 500;
    box-shadow: 2px 0 4px rgba(0,0,0,0.05);
}

#procurement-table tbody tr:hover .procurement-cell.sticky-col {
    background-color: #f1f3f5;
}

#procurement-table tbody tr.zebra-even .procurement-cell.sticky-col {
    background-color: #fafbfc;
}

#procurement-table tbody tr.zebra-even:hover .procurement-cell.sticky-col {
    background-color: #f1f3f5;
}

/* --- Editable Cell Styling --- */
.procurement-cell.editable {
    cursor: text;
    position: relative;
}

.procurement-cell.editable:hover {
    background-color: #e7f3ff;
    outline: 1px solid #007bff;
    outline-offset: -1px;
}

.procurement-cell.editable:focus-within {
    background-color: #fff;
    outline: 2px solid #007bff;
    outline-offset: -2px;
    box-shadow: inset 0 0 0 1px #007bff;
}

/* --- Cell Selection --- */
.procurement-cell.selected {
    background-color: #e7f3ff !important;
    outline: 2px solid #007bff;
    outline-offset: -2px;
}

/* --- Cell Editor (Input) --- */
.procurement-cell .cell-editor {
    width: 100%;
    padding: 4px 8px;
    border: 2px solid #007bff;
    border-radius: 3px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    background: white;
}

.procurement-cell .cell-editor:focus {
    border-color: #0056b3;
    box-shadow: 0 0 0 3px rgba(0,123,255,0.1);
}

/* --- Empty Cell Placeholder --- */
.empty-cell {
    color: #adb5bd;
    font-style: italic;
}

/* --- Variance Color Coding --- */
.procurement-cell.variance-ahead {
    background-color: #d4edda !important;
    color: #155724;
    font-weight: 600;
}

.procurement-cell.variance-behind {
    background-color: #f8d7da !important;
    color: #721c24;
    font-weight: 600;
}

.procurement-cell.variance-ontrack {
    background-color: #e2e3e5 !important;
    color: #383d41;
    font-weight: 500;
}

/* Hover effects for variance cells */
#procurement-table tbody tr:hover .procurement-cell.variance-ahead {
    background-color: #c3e6cb !important;
}

#procurement-table tbody tr:hover .procurement-cell.variance-behind {
    background-color: #f5c6cb !important;
}

#procurement-table tbody tr:hover .procurement-cell.variance-ontrack {
    background-color: #d6d8db !important;
}

/* --- Loading State --- */
.procurement-loading {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 400px;
    color: #6c757d;
    font-size: 1.1rem;
}

.procurement-loading::before {
    content: "⏳";
    margin-right: 8px;
    animation: spin 2s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* --- Empty State --- */
.procurement-empty {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 400px;
    color: #6c757d;
    padding: 40px;
    text-align: center;
}

.procurement-empty svg {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.procurement-empty h3 {
    margin: 0 0 8px 0;
    font-size: 1.25rem;
    color: #495057;
}

.procurement-empty p {
    margin: 0;
    font-size: 0.95rem;
}

/* --- Responsive Adjustments --- */
@media (max-width: 1200px) {
    .procurement-header {
        padding: 12px 16px;
    }

    .procurement-title {
        font-size: 1.25rem;
    }

    .procurement-toolbar button {
        padding: 6px 12px;
        font-size: 13px;
    }
}

@media (max-width: 768px) {
    .procurement-toolbar {
        gap: 8px;
    }

    .procurement-toolbar button span {
        display: none; /* Hide button text on mobile, keep icons */
    }

    .procurement-cell,
    .procurement-header-cell {
        padding: 6px 6px;
        font-size: 13px;
    }
}

/* --- Print Styles --- */
@media print {
    .procurement-container {
        position: static;
        display: block !important;
        margin: 0;
        background: white;
    }

    .procurement-header {
        page-break-after: avoid;
    }

    .procurement-toolbar {
        display: none;
    }

    #procurement-table tbody tr {
        page-break-inside: avoid;
    }

    .procurement-cell.sticky-col,
    .procurement-header-cell.sticky-col {
        position: static;
        box-shadow: none;
    }
}

/* ============================================================================
   HORIZONTAL SCROLLBAR STYLES
   Excel-like horizontal scrolling for procurement grid
   ============================================================================ */

/* Procurement Grid - Scrollable wrapper (nested div inside grid container) */
.procurement-grid-container > div > div {
    overflow-x: auto !important;
    overflow-y: auto !important;
}

/* Horizontal scrollbar styling (WebKit browsers) */
.procurement-grid-container > div > div::-webkit-scrollbar {
    width: 30px; /* Vertical scrollbar - DOUBLED for better visibility */
    height: 30px; /* Horizontal scrollbar - DOUBLED for better visibility */
}

.procurement-grid-container > div > div::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 0px;
}

.procurement-grid-container > div > div::-webkit-scrollbar-thumb {
    background: #888888;
    border-radius: 4px;
    border: 1px solid #f1f1f1;
}

.procurement-grid-container > div > div::-webkit-scrollbar-thumb:hover {
    background: #555555;
}

.procurement-grid-container > div > div::-webkit-scrollbar-thumb:active {
    background: #333333;
}

.procurement-grid-container > div > div::-webkit-scrollbar-corner {
    background: #f1f1f1;
}

/* Firefox scrollbar styling */
.procurement-grid-container > div > div {
    scrollbar-width: auto; /* Changed from 'thin' to 'auto' for thicker scrollbars */
    scrollbar-color: #888888 #f1f1f1;
}

/* Procurement Table - Will be sized by JavaScript */
#procurement-table {
    /* Width set by JavaScript based on column widths */
    /* Remove width: 100% constraint to allow table to expand */
    /* CRITICAL: table-layout: auto prevents browser from redistributing column widths
       when columns are hidden/shown. With "fixed", browser stretches remaining columns
       BEFORE JavaScript recalculation runs, causing the stretching issue. */
    table-layout: auto;
}

/* ============================================================================
   PHASE 3: REAL-TIME DATA PERSISTENCE VISUAL FEEDBACK
   Visual feedback for cell persistence and remote updates
   ============================================================================ */

/* --- Persist Error Highlight --- */
/* Red flash to indicate failed save with rollback */
.persist-error-highlight {
    animation: persist-error-flash 2s ease-out;
}

@keyframes persist-error-flash {
    0% {
        background-color: #f8d7da !important;
        box-shadow: inset 0 0 8px rgba(220, 53, 69, 0.5);
    }
    25% {
        background-color: #f5c6cb !important;
    }
    50% {
        background-color: #f8d7da !important;
    }
    75% {
        background-color: #f5c6cb !important;
    }
    100% {
        background-color: inherit;
        box-shadow: none;
    }
}

/* --- Remote Update Highlight --- */
/* Blue/cyan pulse to indicate change from another user */
.remote-update-highlight {
    animation: remote-update-pulse 1.5s ease-out;
}

@keyframes remote-update-pulse {
    0% {
        background-color: #d1ecf1 !important;
        box-shadow: inset 0 0 8px rgba(23, 162, 184, 0.4);
    }
    50% {
        background-color: #bee5eb !important;
    }
    100% {
        background-color: inherit;
        box-shadow: none;
    }
}

/* --- Save Success Highlight (optional) --- */
/* Green flash to confirm successful save */
.persist-success-highlight {
    animation: persist-success-flash 0.8s ease-out;
}

@keyframes persist-success-flash {
    0% {
        background-color: #d4edda !important;
        box-shadow: inset 0 0 4px rgba(40, 167, 69, 0.3);
    }
    100% {
        background-color: inherit;
        box-shadow: none;
    }
}

/* --- Connection Status Indicator --- */
/* Used by WebSocket status in toolbar */
.connection-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

.connection-status.connected {
    background-color: #d4edda;
    color: #155724;
}

.connection-status.disconnected {
    background-color: #f8d7da;
    color: #721c24;
}

.connection-status.connecting {
    background-color: #fff3cd;
    color: #856404;
}

.connection-status::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.connection-status.connected::before {
    background-color: #28a745;
}

.connection-status.disconnected::before {
    background-color: #dc3545;
}

.connection-status.connecting::before {
    background-color: #ffc107;
    animation: pulse-connecting 1s ease-in-out infinite;
}

@keyframes pulse-connecting {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

/* ============================================================================
   WRIKE STATUS COLUMN STYLES
   Plan Reference: docs/WRIKE_STATUS_IN_PROCUREMENT_GRID.md - Phase 6
   ============================================================================ */

/* Wrike Status column - centered text, no colors */
td[data-field="wrikeStatus"] {
    text-align: center;
}

td[data-field="wrikeStatus"] .cell-content {
    text-align: center;
    justify-content: center;
}
