/* ========================================================================== */
/* TABLA DINÁMICA PAGE - Pivot Table Builder                                */
/* ========================================================================== */

/* Eliminar padding del main-content para esta página */
.main-content:has(.tabla-dinamica-page) {
    padding: 0 !important;
}

.tabla-dinamica-page {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 95px);
    width: 100%;
    padding: 0;
    overflow: hidden;
    background: var(--bg-primary);
}

/* Ajuste del camara-panel para que ocupe todo el espacio */
.tabla-dinamica-page .camara-panel {
    height: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0.75rem;
    box-sizing: border-box;
}

/* Ajuste del camara-header */
.tabla-dinamica-page .camara-header {
    margin: 0 0 0.5rem 0;
    padding: 0.5rem;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
}

.tabla-dinamica-page .camara-header h4 {
    margin: 0;
    flex-shrink: 0;
}

/* Botones en el header */
.header-action-buttons {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex: 1;
    justify-content: center;
}

.header-action-buttons button {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.header-action-buttons button svg {
    flex-shrink: 0;
}

/* Selector de Año en el header */
.tabla-dinamica-page .filter-group label {
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
}

.tabla-dinamica-page .filter-group .custom-dropdown-container {
    min-width: 85px;
}

.tabla-dinamica-page .filter-group .modern-select {
    padding: 0.4rem 0.75rem;
    padding-right: 2rem;
    min-width: 70px;
    width: 100%;
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.3s ease;
}

.tabla-dinamica-page .filter-group .modern-select:hover {
    border-color: var(--primary-color);
}

.tabla-dinamica-page .filter-group .modern-select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(240, 116, 0, 0.15);
}

.tabla-dinamica-page .filter-group .custom-dropdown-list {
    min-width: 100%;
    z-index: 1000;
}

.tabla-dinamica-page .filter-group .dropdown-arrow {
    right: 0.5rem;
    width: 16px;
    height: 16px;
}

/* Override de camara-body para layout de 2 columnas */
.tabla-dinamica-page .camara-body {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 1rem;
    overflow: hidden;
    padding: 0.75rem;
    flex: 1;
    min-height: 0;
}

/* ========================================================================== */
/* COLUMNA IZQUIERDA - Campos arriba + Zonas abajo                           */
/* ========================================================================== */

.left-column {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    overflow: hidden;
    height: 100%;
}

/* ========================================================================== */
/* CAMPOS DISPONIBLES                                                        */
/* ========================================================================== */

.fields-panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    height: calc(50% - 0.5rem); /* 50% del height menos la mitad del gap */
    min-height: 0;
    overflow: hidden;
}

.panel-title {
    margin: 0;
    color: var(--text-primary);
    font-size: 0.95rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid;
    border-image: linear-gradient(90deg, #F07400, #00b4ff) 1;
}

.panel-title svg {
    stroke: var(--text-primary);
    flex-shrink: 0;
}

.fields-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto;
    padding-right: 0.25rem;
}

/* Campo individual arrastrable */
.field-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 0.75rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    cursor: grab;
    transition: all 0.3s ease;
    position: relative;
}

.field-item:hover {
    border-color: var(--primary-color);
    transform: translateX(4px);
    box-shadow: 0 2px 8px rgba(240, 116, 0, 0.2);
}

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

.field-item.dragging {
    opacity: 0.5;
    transform: scale(0.95);
}

.field-icon {
    width: 18px;
    height: 18px;
    stroke: var(--text-secondary);
    flex-shrink: 0;
}

.field-name {
    flex: 1;
    color: var(--text-primary);
    font-size: 0.9rem;
    font-weight: 500;
}

.field-restrictions {
    display: flex;
    gap: 0.25rem;
}

.restriction-badge {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
    color: white;
}

.badge-filter {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

.badge-row {
    background: linear-gradient(135deg, #F07400, #ff8c1a);
}

.badge-column {
    background: linear-gradient(135deg, #00b4ff, #1ac4ff);
}

/* ========================================================================== */
/* ZONAS DE DROP (debajo de campos)                                          */
/* ========================================================================== */

.drop-zones-container {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    overflow-y: auto;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem;
    height: calc(50% - 0.5rem); /* 50% del height menos la mitad del gap */
    min-height: 0;
}

.drop-zone {
    background: var(--bg-primary);
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    padding: 1rem;
    transition: all 0.3s ease;
    min-height: 70px;
    flex-shrink: 0;
}

.drop-zone-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    color: var(--text-primary);
    font-weight: 600;
    font-size: 0.9rem;
    pointer-events: none; /* No interferir con drag & drop */
}

.drop-zone-header svg {
    stroke: var(--text-primary);
    flex-shrink: 0;
}

.drop-zone-body {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    min-height: 40px;
    pointer-events: none; /* No interferir con drag & drop */
}

.drop-zone-body > * {
    pointer-events: auto; /* Restaurar eventos para elementos internos */
}

/* Estados de la zona de drop */
.drop-zone-active {
    border-color: var(--primary-color);
    background: rgba(240, 116, 0, 0.05);
    box-shadow: 0 0 0 4px rgba(240, 116, 0, 0.1);
    transform: scale(1.02);
}

.drop-zone-invalid {
    border-color: #ff453a;
    background: rgba(255, 69, 58, 0.05);
    box-shadow: 0 0 0 4px rgba(255, 69, 58, 0.1);
}

.drop-placeholder {
    color: var(--text-secondary);
    font-style: italic;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 0.5rem;
}

/* Campos que ya fueron soltados */
.dropped-field {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: var(--primary-gradient);
    color: white;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 500;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
    animation: dropIn 0.3s ease;
}

@keyframes dropIn {
    0% {
        opacity: 0;
        transform: scale(0.8);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.remove-field-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.2rem;
    line-height: 1;
    transition: all 0.2s ease;
    padding: 0;
    flex-shrink: 0;
}

.remove-field-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

/* Estilos para filtros con checkboxes */
.filter-field-container {
    width: 100%;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
}

.filter-field-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.filter-field-name {
    color: var(--text-primary);
    font-weight: 600;
    font-size: 0.9rem;
}

.filter-values {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 200px;
    overflow-y: auto;
    padding-right: 0.25rem;
}

.filter-checkbox {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    padding: 0.35rem 0.5rem;
    border-radius: 6px;
    transition: background-color 0.2s ease;
}

.filter-checkbox:hover {
    background: rgba(240, 116, 0, 0.1);
}

.filter-checkbox input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
    accent-color: var(--primary-color);
    flex-shrink: 0;
}

.filter-checkbox span {
    color: var(--text-primary);
    font-size: 0.85rem;
    user-select: none;
}

/* ========================================================================== */
/* COLUMNA DERECHA - Tabla Dinámica                                          */
/* ========================================================================== */

.pivot-table-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    overflow: hidden;
    height: 100%;
    min-height: 0;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
    0% {
        opacity: 0;
        transform: translateY(10px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

.pivot-table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid;
    border-image: linear-gradient(90deg, #F07400, #00b4ff) 1;
}

.pivot-table-header h5 {
    margin: 0;
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.pivot-table-header h5 svg {
    stroke: var(--text-primary);
}

.table-stats {
    display: flex;
    gap: 0.75rem;
}

.stat-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.4rem 0.75rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-primary);
}

.stat-badge svg {
    stroke: var(--primary-color);
}

.pivot-table-wrapper {
    flex: 1;
    overflow-x: auto;
    overflow-y: auto;
    border-radius: 8px;
    background: var(--bg-primary);
    min-height: 0;
    max-height: 100%;
}

/* Tabla Pivot */
.pivot-table {
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.pivot-table thead {
    position: sticky;
    top: 0;
    z-index: 10;
    background: linear-gradient(135deg, #F07400, #00b4ff);
}

.pivot-table th {
    padding: 0.75rem;
    text-align: left;
    font-weight: 600;
    color: white;
    background: transparent;
    border-bottom: 2px solid rgba(255, 255, 255, 0.2);
    white-space: nowrap;
    font-size: 0.8rem;
}

.pivot-table .header-row {
    background: rgba(240, 116, 0, 0.3);
}

.pivot-table .header-column {
    background: rgba(0, 180, 255, 0.3);
}

.pivot-table .header-total {
    background: rgba(255, 255, 255, 0.2);
    text-align: center;
    font-weight: 700;
}

.pivot-table tbody tr {
    transition: background-color 0.2s ease;
}

.pivot-table tbody tr:hover:not(.total-row) {
    background: rgba(240, 116, 0, 0.05);
}

.pivot-table td {
    padding: 0.65rem 0.75rem;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.pivot-table tbody tr:last-child td {
    border-bottom: none;
}

/* Celdas específicas */
.cell-row-header {
    font-weight: 600;
    color: var(--text-primary);
    background: rgba(240, 116, 0, 0.03);
}

.cell-data {
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.cell-total {
    text-align: center;
    font-weight: 700;
    background: rgba(0, 180, 255, 0.08);
    color: var(--text-primary);
}

.total-row {
    background: rgba(0, 180, 255, 0.05);
}

.total-row td {
    font-weight: 700;
    border-top: 2px solid var(--border-color);
}

.cell-total-header {
    text-align: right;
    padding-right: 1rem;
    background: rgba(240, 116, 0, 0.05);
}

.cell-grand-total {
    text-align: center;
    background: var(--primary-gradient) !important;
    color: white !important;
    font-weight: 700;
    font-size: 0.9rem;
}

/* ========================================================================== */
/* ESTADO VACÍO                                                              */
/* ========================================================================== */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4rem 2rem;
    text-align: center;
    gap: 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    height: 100%;
}

.empty-state svg {
    stroke: var(--text-secondary);
    opacity: 0.5;
}

.empty-state h3 {
    color: var(--text-primary);
    margin: 0;
    font-size: 1.2rem;
}

.empty-state p {
    color: var(--text-secondary);
    margin: 0.5rem 0 0 0;
    font-size: 0.95rem;
}

/* ========================================================================== */
/* RESPONSIVE                                                                 */
/* ========================================================================== */

@media (max-width: 1024px) {
    /* Permitir scroll en el contenedor principal */
    .tabla-dinamica-page {
        height: auto;
        min-height: calc(100vh - 95px);
        overflow-y: auto;
    }

    .tabla-dinamica-page .camara-panel {
        height: auto;
        min-height: 0;
    }

    .tabla-dinamica-page .camara-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .header-action-buttons {
        width: 100%;
        justify-content: flex-start;
        flex-wrap: wrap;
    }

    .header-action-buttons button {
        flex: 1;
        min-width: 150px;
    }

    .tabla-dinamica-page .camara-body {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;
        overflow: visible;
        flex: none;
    }

    .left-column {
        grid-column: 1;
        grid-row: 1;
        flex-direction: row;
        gap: 1rem;
        height: auto;
        overflow: visible;
    }

    .fields-panel {
        flex: 1;
        height: auto;
        min-height: 300px;
        max-height: none;
    }

    .drop-zones-container {
        flex: 1;
        height: auto;
        min-height: 300px;
        max-height: none;
    }

    .pivot-table-container,
    .empty-state {
        grid-column: 1;
        grid-row: 2;
        height: auto;
        min-height: 500px;
    }
}

@media (max-width: 768px) {
    .tabla-dinamica-page {
        gap: 0.75rem;
        height: auto;
        min-height: calc(100vh - 95px);
        overflow-y: auto;
    }

    .tabla-dinamica-page .camara-panel {
        display: flex;
        flex-direction: column;
        height: auto;
        overflow: visible;
    }

    .tabla-dinamica-page .camara-header {
        flex-shrink: 0;
    }

    .tabla-dinamica-page .camara-body {
        flex: none;
        overflow: visible;
        grid-template-rows: auto auto;
    }

    .left-column {
        flex-direction: column;
        height: auto;
        overflow: visible;
    }

    .fields-panel {
        height: auto;
        min-height: 250px;
        max-height: none;
    }

    .drop-zones-container {
        height: auto;
        min-height: 250px;
        max-height: none;
    }

    .pivot-table-container,
    .empty-state {
        height: auto;
        min-height: 400px;
    }

    .action-buttons {
        flex-direction: column;
    }

    .action-buttons button {
        width: 100%;
        justify-content: center;
    }

    .pivot-table-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }

    .table-stats {
        width: 100%;
        justify-content: space-between;
    }
}

@media (max-width: 480px) {
    .tabla-dinamica-page {
        height: auto;
        min-height: calc(100vh - 95px);
        overflow-y: auto;
    }

    .tabla-dinamica-page .camara-panel {
        padding: 0.5rem;
        height: auto;
        overflow: visible;
    }

    .tabla-dinamica-page .camara-body {
        padding: 0.5rem;
        gap: 0.75rem;
        flex: none;
        overflow: visible;
    }

    .left-column {
        height: auto;
        overflow: visible;
    }

    .config-panel {
        gap: 0.75rem;
    }

    .fields-panel {
        height: auto;
        min-height: 200px;
        max-height: none;
    }

    .drop-zones-container {
        height: auto;
        min-height: 200px;
        max-height: none;
    }

    .pivot-table-container,
    .empty-state {
        height: auto;
        min-height: 350px;
    }

    .field-item {
        padding: 0.5rem;
    }

    .drop-zone {
        padding: 0.75rem;
    }

    .action-buttons {
        padding: 0.75rem;
    }

    .pivot-table th,
    .pivot-table td {
        padding: 0.5rem;
        font-size: 0.75rem;
    }
}

/* ========================================================================== */
/* MODO CLARO - LIGHT MODE                                                  */
/* ========================================================================== */

:root.light-mode .field-item,
.light-mode .field-item {
    background: #ffffff;
}

:root.light-mode .drop-zone,
.light-mode .drop-zone {
    background: #ffffff;
}

:root.light-mode .pivot-table tbody tr:hover:not(.total-row),
.light-mode .pivot-table tbody tr:hover:not(.total-row) {
    background: rgba(240, 116, 0, 0.08);
}

:root.light-mode .cell-row-header,
.light-mode .cell-row-header {
    background: rgba(240, 116, 0, 0.05);
}

:root.light-mode .cell-total,
.light-mode .cell-total {
    background: rgba(0, 180, 255, 0.12);
}

:root.light-mode .total-row,
.light-mode .total-row {
    background: rgba(0, 180, 255, 0.08);
}

:root.light-mode .cell-total-header,
.light-mode .cell-total-header {
    background: rgba(240, 116, 0, 0.08);
}

:root.light-mode .stat-badge,
.light-mode .stat-badge {
    background: #ffffff;
}

/* ========================================================================== */
/* LOADING SPINNER                                                           */
/* ========================================================================== */

.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4rem 2rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    height: 100%;
    animation: fadeIn 0.3s ease;
}

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.spinner {
    width: 60px;
    height: 60px;
    border: 4px solid var(--border-color);
    border-top: 4px solid #F07400;
    border-right: 4px solid #00b4ff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

.loading-text {
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    text-align: center;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}
