/* ===================================
   PreviredGrid - Excel-like Grid Styles
   =================================== */

/* Variables */
:root {
    --grid-cell-height: 32px;
    --grid-header-height: 48px;
    --grid-row-number-width: 50px;
    --grid-column-width: 120px; /* ANCHO FIJO PARA TODAS LAS COLUMNAS */
    --grid-border-color: #d0d0d0;
    --grid-header-bg: #f8f9fa;
    --grid-cell-bg: #ffffff;
    --grid-cell-hover-bg: #f0f0f0;
    --grid-cell-selected-bg: #e7f3ff;
    --grid-cell-selected-border: #0b5fff;
    --grid-cell-editing-bg: #ffffff;
    --grid-cell-error-bg: #ffe6e6;
    --grid-cell-error-border: #c62828;
    --grid-row-number-bg: #f0f0f0;
    --grid-scrollbar-size: 12px;
    --grid-total-width: calc(var(--grid-row-number-width) + (var(--grid-column-width) * 105)); /* 50px + 12600px = 12650px */
}

/* Grid Container - CONTENEDOR PRINCIPAL CON SCROLL */
.previred-grid {
    display: flex;
    flex-direction: column;
    height: 100%;
    border: 2px solid var(--grid-border-color);
    background: var(--grid-cell-bg);
    outline: none;
    overflow: auto;
    position: relative;
}

/* Header Row - Grid Layout con anchos fijos */
.grid-header-row {
    display: grid;
    grid-template-columns: var(--grid-row-number-width) repeat(105, var(--grid-column-width));
    width: var(--grid-total-width); /* ANCHO FIJO TOTAL */
    min-width: var(--grid-total-width); /* FORZAR ANCHO MÍNIMO */
    flex-shrink: 0;
    height: var(--grid-header-height);
    background: var(--grid-header-bg);
    border-bottom: 2px solid var(--grid-border-color);
    position: sticky;
    top: 0;
    z-index: 10;
}

.grid-row-number-header {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.813rem;
    color: #666;
    background: var(--grid-row-number-bg);
    border-right: 2px solid var(--grid-border-color);
    position: sticky;
    left: 0;
    z-index: 11;
}

.grid-column-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.25rem 0.5rem;
    border-right: 1px solid var(--grid-border-color);
    font-size: 0.75rem;
    background: var(--grid-header-bg);
    user-select: none;
    overflow: hidden;
}

.grid-column-header:hover {
    background: #e9ecef;
}

.column-number {
    font-weight: 700;
    color: #0b5fff;
    font-size: 0.813rem;
    margin-bottom: 0.125rem;
    white-space: nowrap;
}

.column-name {
    font-weight: 400;
    color: #666;
    font-size: 0.688rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    text-align: center;
}

/* Grid Body */
.grid-body {
    display: block;
    width: var(--grid-total-width); /* ANCHO FIJO TOTAL */
    min-width: var(--grid-total-width); /* FORZAR ANCHO MÍNIMO */
}

/* Data Row - Grid Layout sincronizado con header */
.grid-data-row {
    display: grid;
    grid-template-columns: var(--grid-row-number-width) repeat(105, var(--grid-column-width));
    width: var(--grid-total-width); /* ANCHO FIJO TOTAL */
    min-width: var(--grid-total-width); /* FORZAR ANCHO MÍNIMO */
    min-height: var(--grid-cell-height);
    border-bottom: 1px solid var(--grid-border-color);
    background: var(--grid-cell-bg);
}

.grid-data-row:hover {
    background: var(--grid-cell-hover-bg);
}

.grid-data-row.selected {
    background: #f0f8ff;
}

/* Fila marcada para eliminación (soft delete) - tachada */
.grid-data-row.deleted {
    background: #ffe6e6;
    opacity: 0.7;
}

.grid-data-row.deleted:hover {
    background: #ffd0d0;
}

.grid-data-row.deleted .grid-row-number {
    background: #ffb3b3;
    color: #c62828;
}

.grid-data-row.deleted.selected {
    background: #ffe0e0;
}

/* Fila nueva (agregada) */
.grid-data-row.new-row {
    background: #e6f7ff;
}

.grid-data-row.new-row:hover {
    background: #d0ecff;
}

.grid-data-row.new-row .grid-row-number {
    background: #b3e0ff;
    color: #0b5fff;
}

.grid-data-row.new-row.selected {
    background: #d0f0ff;
}

/* Row Number */
.grid-row-number {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    font-weight: 600;
    font-size: 0.813rem;
    color: #666;
    background: var(--grid-row-number-bg);
    border-right: 2px solid var(--grid-border-color);
    position: sticky;
    left: 0;
    z-index: 1;
}

.grid-data-row:hover .grid-row-number {
    background: #e0e0e0;
    font-weight: 700;
}

.grid-data-row.selected .grid-row-number {
    background: #c0d8f0;
    font-weight: 700;
    color: #0b5fff;
}

/* Cell - Perfectamente sincronizado con header via CSS Grid */
.grid-cell {
    display: flex;
    align-items: center;
    padding: 0.25rem 0.5rem;
    border-right: 1px solid var(--grid-border-color);
    font-size: 0.813rem;
    background: var(--grid-cell-bg);
    cursor: cell;
    position: relative;
    overflow: hidden;
}

.grid-cell:hover {
    background: var(--grid-cell-hover-bg);
}

.grid-cell.selected {
    background: var(--grid-cell-selected-bg);
    border: 2px solid var(--grid-cell-selected-border);
    z-index: 2;
    box-shadow: 0 0 0 1px var(--grid-cell-selected-border) inset;
}

/* Celda en fila eliminada - tachada */
.grid-cell.cell-deleted {
    color: #999;
    cursor: not-allowed;
}

.grid-cell.cell-deleted .cell-content {
    text-decoration: line-through;
    text-decoration-thickness: 2px;
    text-decoration-color: #c62828;
}

.grid-cell.error {
    background: var(--grid-cell-error-bg);
    border-left: 3px solid var(--grid-cell-error-border);
}

.grid-cell.error::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 8px 8px 0;
    border-color: transparent var(--grid-cell-error-border) transparent transparent;
}

/* Cell Content */
.cell-content {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    display: block;
}

/* Cell Content - Numeric (alineación derecha para valores numéricos) */
.cell-content.numeric {
    text-align: right;
}

/* Cell Content - Date (alineación centrada para fechas) */
.cell-content.date {
    text-align: center;
}

/* Cell Input */
.cell-input {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    background: var(--grid-cell-editing-bg);
    font-family: inherit;
    font-size: inherit;
    padding: 0;
    margin: 0;
    box-shadow: 0 0 0 2px #0b5fff inset;
}

.cell-input:focus {
    box-shadow: 0 0 0 2px #0b5fff inset, 0 0 5px rgba(11, 95, 255, 0.5);
}

/* Cell Input - Numeric */
.cell-input-number {
    text-align: right;
}

/* Cell Input - Date */
.cell-input-date {
    text-align: center;
}

/* Cell Select - Dropdown */
.cell-select {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    background: var(--grid-cell-editing-bg);
    font-family: inherit;
    font-size: inherit;
    padding: 0.125rem 0.25rem;
    margin: 0;
    box-shadow: 0 0 0 2px #0b5fff inset;
    cursor: pointer;
    border-radius: 0;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.25rem center;
    background-size: 1em;
    padding-right: 1.5rem;
}

.cell-select:focus {
    box-shadow: 0 0 0 2px #0b5fff inset, 0 0 5px rgba(11, 95, 255, 0.5);
}

.cell-select option {
    background: white;
    color: #333;
    padding: 0.25rem 0.5rem;
}

.cell-select option:first-child {
    color: #999;
    font-style: italic;
}

/* Hover en dropdown */
.cell-select:hover {
    background-color: #f8f9fa;
}

/* Scrollbars - MÁS VISIBLES */
.previred-grid::-webkit-scrollbar {
    width: var(--grid-scrollbar-size);
    height: var(--grid-scrollbar-size);
}

.previred-grid::-webkit-scrollbar-track {
    background: #f1f1f1;
    border: 1px solid #d0d0d0;
}

.previred-grid::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
    border: 2px solid #f1f1f1;
}

.previred-grid::-webkit-scrollbar-thumb:hover {
    background: #555;
}

.previred-grid::-webkit-scrollbar-corner {
    background: #f1f1f1;
}

/* Firefox Scrollbars */
.previred-grid {
    scrollbar-width: auto;
    scrollbar-color: #888 #f1f1f1;
}

/* Focus Styles */
.previred-grid:focus {
    outline: 2px solid #0b5fff;
    outline-offset: -2px;
}

/* Empty State */
.grid-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #999;
    font-size: 1rem;
    padding: 2rem;
}

/* Performance Optimizations */
.grid-data-row {
    will-change: transform;
    contain: layout style paint;
}

.grid-cell {
    contain: layout style paint;
}

/* Selection Effects */
@keyframes cell-select {
    0% {
        box-shadow: 0 0 0 0 rgba(11, 95, 255, 0.4);
    }
    100% {
        box-shadow: 0 0 0 4px rgba(11, 95, 255, 0);
    }
}

.grid-cell.selected {
    animation: cell-select 0.3s ease-out;
}

/* Error Cell Pulse */
@keyframes error-pulse {
    0%, 100% {
        background: var(--grid-cell-error-bg);
    }
    50% {
        background: #ffcccc;
    }
}

.grid-cell.error:hover {
    animation: error-pulse 1s ease-in-out infinite;
}

/* Responsive */
@media (max-width: 768px) {
    :root {
        --grid-cell-height: 28px;
        --grid-header-height: 40px;
        --grid-row-number-width: 40px;
        --grid-column-width: 100px;
        --grid-total-width: calc(var(--grid-row-number-width) + (var(--grid-column-width) * 105));
    }
    
    .grid-cell,
    .cell-input {
        font-size: 0.75rem;
    }
    
    .column-number {
        font-size: 0.75rem;
    }
    
    .column-name {
        font-size: 0.625rem;
    }
}

/* Print Styles */
@media print {
    .previred-grid {
        border: 1px solid #000;
    }
    
    .grid-cell.selected {
        border: 1px solid #000;
        background: #fff;
    }
    
    .previred-grid {
        overflow: visible;
    }
}

/* Accessibility */
.grid-cell:focus-visible {
    outline: 2px solid #0b5fff;
    outline-offset: -2px;
    z-index: 3;
}

.grid-data-row:focus-within {
    box-shadow: 0 0 0 2px rgba(11, 95, 255, 0.2);
}

/* Loading State */
.grid-loading {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.grid-loading-spinner {
    color: #0b5fff;
}
