/* ===========================================
   AUDIT TAB - Audit Log & Undo System
   Phase 6: Professional Grey Design
   Phase 26.16: Renamed from History to Audit
   =========================================== */

/* History Tab Container */
#history-tab {
    display: none;
    height: 100%;
    flex-direction: column;
    background: var(--color-bg-secondary);
}

#history-tab.active {
    display: flex;
}

/* History Header — removed, no longer used */
.history-header {
    display: none;
}

/* Refresh Button */
.history-refresh-btn {
    background: transparent;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: 4px;
    cursor: pointer;
    color: var(--color-text-secondary);
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.history-refresh-btn:hover {
    background: var(--color-bg-tertiary);
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.history-refresh-btn svg {
    width: 13px;
    height: 13px;
}

.history-refresh-btn.loading svg {
    animation: spin 1s linear infinite;
}

/* Filters Section — single row */
.history-filters {
    display: flex;
    gap: 4px;
    padding: 6px 8px;
    background: white;
    align-items: center;
    flex-shrink: 0;
    flex-wrap: nowrap;
}

/* Custom Dropdown */
.custom-dropdown {
    position: relative;
}

.custom-dropdown-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 2px 7px;
    height: 22px;
    border: none;
    background: rgba(94, 65, 222, 0.15);
    border-radius: 20px;
    cursor: pointer;
    transition: background 0.2s;
    flex-shrink: 0;
}

.custom-dropdown-btn:hover {
    background: rgba(94, 65, 222, 0.3);
}

.custom-dropdown-label {
    font-size: 10px;
    font-weight: 500;
    color: #5D41DE;
    font-family: 'Inter', sans-serif;
    letter-spacing: 0;
    white-space: nowrap;
}

.custom-dropdown-btn svg {
    width: 7px;
    height: 7px;
    color: #5D41DE;
    flex-shrink: 0;
    transition: transform 0.2s;
}

.custom-dropdown.open .custom-dropdown-btn svg {
    transform: rotate(180deg);
}

.custom-dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    min-width: 140px;
    background: #ffffff;
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    z-index: 50;
    padding: 4px 0;
    animation: dropdownFadeIn 0.15s ease;
}

.custom-dropdown.open .custom-dropdown-menu {
    display: block;
}

@keyframes dropdownFadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

.custom-dropdown-item {
    padding: 8px 14px;
    font-size: 13px;
    color: #374151;
    cursor: pointer;
    transition: color 0.15s;
    font-family: 'Inter', sans-serif;
}

.custom-dropdown-item:hover {
    color: #5D41DE;
    background: rgba(94, 65, 222, 0.05);
}

.custom-dropdown-item.selected {
    color: #5D41DE;
    font-weight: 500;
}

/* Animated search input */
.history-search-form {
    flex: 1;
    min-width: 0;
    position: relative;
    height: 22px;
    display: flex;
    align-items: center;
    padding-inline: 0.6em;
    border-radius: 1px;
    background: #fff;
    border-bottom: 1px solid #d1d5db;
    transition: border-radius 0.5s ease;
}

.history-search-form::before {
    content: "";
    position: absolute;
    background: #6D55D6;
    transform: scaleX(0);
    transform-origin: center;
    width: 100%;
    height: 2px;
    left: 0;
    bottom: 0;
    border-radius: 1px;
    transition: transform 0.3s ease;
}

.history-search-form:focus-within {
    border-bottom-color: #6D55D6;
}

.history-search-form:focus-within::before {
    transform: scaleX(1);
}

.history-search-form svg {
    width: 12px;
    height: 12px;
    color: #8b8ba7;
    flex-shrink: 0;
}

.history-search-input {
    font-size: 11px;
    background: transparent;
    width: 100%;
    height: 100%;
    padding-inline: 0.4em;
    border: none;
    outline: none;
    color: var(--color-text-primary);
}

.history-search-input::placeholder {
    color: #8b8ba7;
}

.history-search-reset {
    border: none;
    background: none;
    color: #8b8ba7;
    font-size: 16px;
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s;
}

.history-search-input:not(:placeholder-shown) ~ .history-search-reset {
    opacity: 1;
    visibility: visible;
}

/* History List */
.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
}

.history-list::-webkit-scrollbar {
    width: 6px;
}

.history-list::-webkit-scrollbar-track {
    background: transparent;
}

.history-list::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 3px;
}

.history-list::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

/* Empty State */
.history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    color: var(--color-text-muted);
    text-align: center;
}

.history-empty svg {
    width: 48px;
    height: 48px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.history-empty p {
    font-size: 14px;
    margin: 0;
}

/* History Item Card */
.history-item {
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    margin-bottom: 10px;
    transition: all var(--transition-fast);
    cursor: pointer;
}

.history-item:hover {
    border-color: #d1d5db;
    box-shadow: var(--shadow-sm);
}

.history-item:last-child {
    margin-bottom: 0;
}

/* Item Header */
.history-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 8px;
}

.history-item-type {
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-type-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: var(--radius-full);
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Type Badge Colors - Professional greys with subtle hints */
.history-type-badge.data_cleaning {
    background: #e5e7eb;
    color: #374151;
}

.history-type-badge.formula_generation {
    background: #ede9fe;
    color: #5D46BF;
}

.history-type-badge.model_building {
    background: #ede9fe;
    color: #5b21b6;
}

.history-type-badge.error_fixing {
    background: #fee2e2;
    color: #991b1b;
}

.history-type-badge.structure_change {
    background: #fef3c7;
    color: #92400e;
}

.history-type-badge.cell_change {
    background: #d1fae5;
    color: #065f46;
}

.history-type-badge.bulk_operation {
    background: #f3e8ff;
    color: #7c3aed;
}

.history-type-badge.chat {
    background: #f3f4f6;
    color: #4b5563;
}

/* Status Badge */
.history-status-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: var(--radius-full);
    font-size: 10px;
    font-weight: 500;
}

.history-status-badge.success {
    background: #dcfce7;
    color: #166534;
}

.history-status-badge.failed {
    background: #fee2e2;
    color: #991b1b;
}

.history-status-badge.pending {
    background: #fef3c7;
    color: #92400e;
}

.history-status-badge.executing {
    background: #ede9fe;
    color: #5D46BF;
}

.history-status-badge.undone {
    background: #f3f4f6;
    color: #6b7280;
}

.history-status-badge .status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

/* Item Description */
.history-item-description {
    font-size: 13px;
    color: var(--color-text-primary);
    margin-bottom: 10px;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Item Meta Row */
.history-item-meta {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    font-size: 11px;
    color: var(--color-text-secondary);
    margin-bottom: 10px;
}

.history-meta-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.history-meta-item svg {
    width: 12px;
    height: 12px;
    opacity: 0.7;
}

/* Item Footer - Undo Section */
.history-item-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 10px;
    border-top: 1px solid var(--color-border);
}

.history-time {
    font-size: 11px;
    color: var(--color-text-muted);
}

/* Phase 26.16: Approved By Display */
.history-approved-by {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: var(--color-text-muted);
    margin-left: 12px;
}

.history-approved-by svg {
    color: var(--color-primary);
    opacity: 0.7;
}

/* Undo Button */
.history-undo-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: 11px;
    font-weight: 500;
    color: var(--color-text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.history-undo-btn:hover:not(:disabled) {
    background: var(--color-bg-tertiary);
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.history-undo-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.history-undo-btn svg {
    width: 14px;
    height: 14px;
}

/* Undo Countdown Timer */
.undo-countdown {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--color-text-muted);
}

.undo-countdown.urgent {
    color: #dc2626;
    font-weight: 500;
}

.undo-countdown-bar {
    width: 60px;
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
    overflow: hidden;
}

.undo-countdown-progress {
    height: 100%;
    background: var(--color-primary);
    transition: width 1s linear;
}

.undo-countdown.urgent .undo-countdown-progress {
    background: #dc2626;
}

/* Pagination */
.history-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    padding: 16px;
    background: white;
    flex-shrink: 0;
}

.history-page-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--color-text-secondary);
    font-size: 12px;
    transition: all var(--transition-fast);
}

.history-page-btn:hover:not(:disabled) {
    background: var(--color-bg-tertiary);
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.history-page-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.history-page-btn.active {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: white;
}

.history-page-info {
    font-size: 12px;
    color: var(--color-text-secondary);
    padding: 0 12px;
}

/* Stats Summary Bar */
.history-stats {
    display: flex;
    gap: 16px;
    padding: 8px 12px;
    background: white;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.history-stat {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
}

.history-stat-value {
    font-weight: 600;
    color: var(--color-text-primary);
}

.history-stat-label {
    color: var(--color-text-secondary);
}

/* Loading Skeleton */
.history-skeleton {
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    margin-bottom: 10px;
}

.skeleton-line {
    height: 12px;
    background: linear-gradient(90deg, #f3f4f6 25%, #e5e7eb 50%, #f3f4f6 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s infinite;
    border-radius: 4px;
    margin-bottom: 8px;
}

.skeleton-line:last-child {
    margin-bottom: 0;
}

.skeleton-line.short {
    width: 40%;
}

.skeleton-line.medium {
    width: 70%;
}

@keyframes skeleton-shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}

/* Detail Modal */
.history-detail-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-modal);
    animation: modalBgIn 0.15s ease;
}

@keyframes modalBgIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.history-detail-modal.hidden {
    display: none;
}

.history-detail-content {
    background: white;
    border-radius: 16px;
    width: 92%;
    max-width: 480px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    animation: modalScaleIn 0.15s ease;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}

@keyframes modalScaleIn {
    from { opacity: 0; transform: scale(0.92); }
    to { opacity: 1; transform: scale(1); }
}

.history-detail-header {
    background: white;
    color: #1f2937;
    padding: 16px 20px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f3f4f6;
}

.history-detail-header h4 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: #5D41DE;
}

.history-detail-close {
    background: transparent;
    border: none;
    color: #9ca3af;
    cursor: pointer;
    padding: 4px;
    transition: color 0.15s;
}

.history-detail-close:hover {
    color: #374151;
}

.history-detail-close svg {
    width: 18px;
    height: 18px;
}

.history-detail-body {
    padding: 16px 20px;
    overflow-y: auto;
    flex: 1;
}

/* Inline meta row (type + status + initiator) */
.detail-meta-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.detail-section {
    margin-bottom: 14px;
}

.detail-section:last-child {
    margin-bottom: 0;
}

.detail-label {
    font-size: 12px;
    font-weight: 600;
    color: #5D41DE;
    margin-bottom: 4px;
}

.detail-value {
    font-size: 13px;
    color: var(--color-text-primary);
    line-height: 1.6;
}

.detail-value.code {
    font-family: 'Courier New', monospace;
    background: var(--color-bg-tertiary);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    overflow-x: auto;
}

.history-detail-footer {
    padding: 10px 20px;
    background: white;
    display: flex;
    justify-content: center;
}

/* Phase 31.2: Close Button Styling - Light theme */
.history-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 24px;
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-primary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.history-close-btn:hover {
    background: var(--color-bg-tertiary);
    border-color: #d1d5db;
}

.history-close-btn:active {
    background: #e5e7eb;
}

/* Toast Notifications for History */
.history-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #1f2937;
    color: white;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    font-size: 13px;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transition: all 0.3s ease;
    z-index: 10001;
}

.history-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.history-toast.success {
    background: #166534;
}

.history-toast.error {
    background: #991b1b;
}

/* Responsive Adjustments */
@media (max-width: 400px) {
    .history-item-meta {
        gap: 8px;
    }

    .history-stats {
        gap: 8px;
    }
}

/* ===========================================
   DARK MODE — History/Audit Overrides
   =========================================== */

[data-theme="dark"] #history-tab {
    background: #1a1a2e;
}

[data-theme="dark"] .history-filters {
    background: #1f1f35;
}

[data-theme="dark"] .custom-dropdown-btn {
    background: rgba(139, 117, 232, 0.2);
}

[data-theme="dark"] .custom-dropdown-btn:hover {
    background: rgba(139, 117, 232, 0.35);
}

[data-theme="dark"] .custom-dropdown-label {
    color: #8B75E8;
}

[data-theme="dark"] .custom-dropdown-btn svg {
    color: #8B75E8;
}

[data-theme="dark"] .custom-dropdown-menu {
    background: #24243d;
    border-color: #353550;
}

[data-theme="dark"] .custom-dropdown-item {
    color: #a8a3c0;
}

[data-theme="dark"] .custom-dropdown-item:hover {
    color: #8B75E8;
    background: rgba(139, 117, 232, 0.1);
}

[data-theme="dark"] .custom-dropdown-item.selected {
    color: #8B75E8;
}

[data-theme="dark"] .history-search-form {
    background: #1f1f35;
    border-bottom-color: #353550;
}

[data-theme="dark"] .history-search-input {
    color: #f0f0f5;
}

[data-theme="dark"] .history-search-input::placeholder {
    color: #706b8a;
}

[data-theme="dark"] .history-search-form svg {
    color: #706b8a;
}

[data-theme="dark"] .history-refresh-btn {
    border-color: #353550;
    color: #a8a3c0;
}

[data-theme="dark"] .history-refresh-btn:hover {
    background: #2a2a45;
    border-color: #8B75E8;
    color: #8B75E8;
}

[data-theme="dark"] .history-stats {
    color: #a8a3c0;
}

[data-theme="dark"] .history-stats .stat-number {
    color: #f0f0f5;
}

[data-theme="dark"] .history-item {
    background: #24243d;
    border-color: #353550;
}

[data-theme="dark"] .history-item:hover {
    background: #2a2a45;
}

[data-theme="dark"] .history-item-title {
    color: #f0f0f5;
}

[data-theme="dark"] .history-item-preview {
    color: #a8a3c0;
}

[data-theme="dark"] .history-item-meta {
    color: #706b8a;
}

[data-theme="dark"] .history-type-badge {
    background: #2a2a45;
    color: #a8a3c0;
}

[data-theme="dark"] .history-type-badge.chat { background: #f3e8ff; color: #7c3aed; }
[data-theme="dark"] .history-type-badge.chat { background: #2d2850; color: #8B75E8; }

[data-theme="dark"] .history-status-badge.success { color: #22c55e; }
[data-theme="dark"] .history-status-badge.failed { background: #2e1a1a; color: #ef4444; }

[data-theme="dark"] .history-empty-state {
    color: #706b8a;
}

[data-theme="dark"] .history-detail-panel {
    background: #1f1f35;
}

[data-theme="dark"] .history-detail-header {
    background: #24243d;
    border-bottom-color: #353550;
}

[data-theme="dark"] .history-detail-title {
    color: #f0f0f5;
}

[data-theme="dark"] .history-detail-content {
    background: #1f1f35;
    color: #a8a3c0;
}

[data-theme="dark"] .history-detail-section {
    border-bottom-color: #353550;
}

[data-theme="dark"] .history-skeleton {
    background: #1a1432;
    border-color: #2a2545;
}

[data-theme="dark"] .skeleton-line {
    background: linear-gradient(90deg, #2a2a45 25%, #353550 50%, #2a2a45 75%);
    background-size: 200% 100%;
}

[data-theme="dark"] .history-skeleton-line {
    background: linear-gradient(90deg, #2a2a45 25%, #353550 50%, #2a2a45 75%);
}

[data-theme="dark"] .history-toast {
    background: #24243d;
    color: #f0f0f5;
}

/* Stats bar */
[data-theme="dark"] .history-stats {
    background: #1f1f35;
}

/* Pagination */
[data-theme="dark"] .history-pagination {
    background: #1f1f35;
}

[data-theme="dark"] .history-page-btn {
    background: #24243d;
    border-color: #353550;
    color: #a8a3c0;
}

[data-theme="dark"] .history-page-btn:hover {
    background: #2a2a45;
}

[data-theme="dark"] .history-page-btn.active {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: #ffffff;
}

/* History item internal separators */
[data-theme="dark"] .history-item-footer {
    border-top-color: #353550;
}

[data-theme="dark"] .history-item-separator {
    border-color: #353550;
}

/* Type badge dark backgrounds */
[data-theme="dark"] .history-type-badge.data_cleaning {
    background: #2a2a45;
    color: #a8a3c0;
}

[data-theme="dark"] .history-type-badge.formula_generation,
[data-theme="dark"] .history-type-badge.model_building {
    background: #2d2850;
    color: #8B75E8;
}

[data-theme="dark"] .history-type-badge.error_fixing {
    background: #2e1a1a;
    color: #f87171;
}

[data-theme="dark"] .history-type-badge.structure_change {
    background: #2e2a1a;
    color: #fbbf24;
}

[data-theme="dark"] .history-type-badge.cell_change {
    background: #1a2e2a;
    color: #34d399;
}

[data-theme="dark"] .history-type-badge.bulk_operation {
    background: #2d2850;
    color: #a78bfa;
}

[data-theme="dark"] .history-type-badge.chat {
    background: #2a2a45;
    color: #a8a3c0;
}

/* Status badge dark backgrounds */
[data-theme="dark"] .history-status-badge.success {
    background: #1a2e1a;
    color: #22c55e;
}

[data-theme="dark"] .history-status-badge.failed {
    background: #2e1a1a;
    color: #f87171;
}

[data-theme="dark"] .history-status-badge.pending {
    background: #2e2a1a;
    color: #fbbf24;
}

[data-theme="dark"] .history-status-badge.executing {
    background: #2d2850;
    color: #8B75E8;
}

[data-theme="dark"] .history-status-badge.undone {
    background: #2a2a45;
    color: #706b8a;
}

/* Undo button */
[data-theme="dark"] .history-undo-btn {
    background: #2a2a45;
    border-color: #353550;
}

[data-theme="dark"] .history-undo-btn:hover {
    background: #353550;
}

/* History list container */
[data-theme="dark"] .history-list {
    background: #1a1a2e;
}

/* History item hover in dark */
[data-theme="dark"] .history-item:hover {
    border-color: #454565;
}

/* Detail footer + close button */
[data-theme="dark"] .history-detail-footer {
    background: #1f1f35;
    border-top: 1px solid #353550;
}

[data-theme="dark"] .history-close-btn {
    background: #24243d;
    border-color: #353550;
    color: #a8a3c0;
}

[data-theme="dark"] .history-close-btn:hover {
    background: #2a2a45;
    border-color: #454565;
}

[data-theme="dark"] .history-close-btn:active {
    background: #353550;
}

/* Detail title */
[data-theme="dark"] .history-detail-title-text {
    color: #8B75E8;
}

/* Detail close X */
[data-theme="dark"] .history-detail-close {
    color: #706b8a;
}

[data-theme="dark"] .history-detail-close:hover {
    color: #f0f0f5;
}

/* Detail section labels */
[data-theme="dark"] .history-detail-label {
    color: #8B75E8;
}

[data-theme="dark"] .history-detail-value {
    color: #f0f0f5;
}

/* Detail panel scrollable area */
[data-theme="dark"] .history-detail-body {
    background: #1f1f35;
}

/* Detail code blocks */
[data-theme="dark"] .history-detail-code {
    background: #1a1a2e;
    border-color: #353550;
}
