/* ================================================================
   AUDIT TRAIL — Enterprise-grade UI
   ================================================================ */

/* ── Page shell ───────────────────────────────────────────────── */
.audit-page {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 60px);   /* fill viewport under nav */
    background: #f1f5f9;
    overflow: hidden;             /* children scroll, not the page */
}

/* ── Header ───────────────────────────────────────────────────── */
.audit-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 1.75rem;
    background: #fff;
    border-bottom: 1px solid #e2e8f0;
    flex-shrink: 0;
}

.audit-header-left {
    display: flex;
    align-items: center;
    gap: 0.875rem;
}

.audit-header-icon {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    color: #1d4ed8;
    flex-shrink: 0;
}

.audit-title {
    font-size: var(--ds-h2);
    font-weight: var(--ds-fw-semibold);
    line-height: var(--ds-lh-h2);
    color: var(--gray-900);
    margin: 0;
    letter-spacing: -0.015em;
}

.audit-subtitle {
    font-size: var(--ds-label);
    color: var(--gray-500);
    margin: 2px 0 0;
}

.audit-refresh-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 6px 14px;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    font-size: var(--ds-label);
    font-weight: var(--ds-fw-semibold);
    color: var(--gray-700);
    cursor: pointer;
    transition: all 0.15s;
}

.audit-refresh-btn:hover:not(:disabled) {
    background: #f8fafc;
    border-color: #94a3b8;
    color: #0f172a;
}

.audit-refresh-btn:disabled { opacity: 0.5; cursor: not-allowed; }

@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 0.8s linear infinite; display: inline-block; }

/* ── Stats row ────────────────────────────────────────────────── */
.audit-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0;
    border-bottom: 1px solid #e2e8f0;
    flex-shrink: 0;
}

.audit-stat {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.45rem 0.875rem;
    background: #fff;
    border-right: 1px solid #e2e8f0;
    position: relative;
    overflow: hidden;
}

.audit-stat::before {
    content: '';
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 3px;
}

.audit-stat--blue::before  { background: #2563eb; }
.audit-stat--teal::before  { background: #0d9488; }
.audit-stat--violet::before{ background: #7c3aed; }
.audit-stat--red::before   { background: #dc2626; }

.audit-stat:last-child { border-right: none; }

.audit-stat-icon-wrap {
    width: 26px;
    height: 26px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
    flex-shrink: 0;
}

.audit-stat--blue  .audit-stat-icon-wrap { background: #eff6ff; color: #2563eb; }
.audit-stat--teal  .audit-stat-icon-wrap { background: #f0fdfa; color: #0d9488; }
.audit-stat--violet.audit-stat-icon-wrap, .audit-stat--violet .audit-stat-icon-wrap { background: #f5f3ff; color: #7c3aed; }
.audit-stat--red   .audit-stat-icon-wrap { background: #fef2f2; color: #dc2626; }

.audit-stat-value {
    font-size: var(--ds-h3);
    font-weight: var(--ds-fw-bold);
    color: var(--gray-900);
    line-height: 1;
    font-variant-numeric: tabular-nums;
}

.audit-stat--red .audit-stat-value { color: #dc2626; }

.audit-stat-label {
    font-size: var(--ds-small);
    font-weight: var(--ds-fw-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--gray-500);
    margin-top: 2px;
}

/* ── Filter bar ───────────────────────────────────────────────── */
.audit-filters {
    display: flex;
    align-items: center;
    gap: 0;
    padding: 0 1.75rem;
    background: #fff;
    border-bottom: 1px solid #e2e8f0;
    flex-shrink: 0;
    height: 44px;
}

.audit-filter-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0 1rem;
}

.audit-filter-divider {
    width: 1px;
    height: 24px;
    background: #e2e8f0;
}

.audit-filter-label {
    font-size: var(--ds-small);
    font-weight: var(--ds-fw-bold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--gray-400);
    white-space: nowrap;
}

.audit-filter-input {
    height: 32px;
    padding: 0 10px;
    font-size: var(--ds-label);
    font-family: var(--ds-font);
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    background: var(--gray-50);
    color: var(--gray-800);
    outline: none;
    transition: all 0.15s;
    cursor: pointer;
}

.audit-filter-input:focus {
    border-color: #2563eb;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(37,99,235,0.08);
}

.audit-filter-actions {
    display: flex;
    gap: 0.5rem;
    margin-left: auto;
    padding-left: 1rem;
}

.audit-btn-apply {
    height: 30px;
    padding: 0 0.875rem;
    background: #2563eb;
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    transition: background 0.15s;
}

.audit-btn-apply:hover { background: #1d4ed8; }

.audit-btn-reset {
    height: 30px;
    padding: 0 0.75rem;
    background: transparent;
    color: #64748b;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    transition: all 0.15s;
}

.audit-btn-reset:hover { background: #f1f5f9; color: #1e293b; border-color: #cbd5e1; }

/* ── Table card ───────────────────────────────────────────────── */
.audit-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 1rem 1.75rem;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    min-height: 0;    /* allow flex child to shrink */
}

/* Count bar */
.audit-count-bar {
    display: flex;
    align-items: center;
    padding: 0.35rem 1.25rem;
    border-bottom: 1px solid #f1f5f9;
    background: #fafbfc;
    flex-shrink: 0;
}

.audit-count-label {
    font-size: var(--ds-label);
    color: var(--gray-500);
}

.audit-count-label strong { color: var(--gray-800); }

/* Loading / Empty */
.audit-state-loading,
.audit-state-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4rem 2rem;
    color: #64748b;
    font-size: 0.875rem;
    gap: 0.5rem;
    flex: 1;
}

.audit-state-empty i { font-size: 2.5rem; color: #cbd5e1; }

/* ── Scroll area ──────────────────────────────────────────────── */
.audit-scroll {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
}

/* ── Table ────────────────────────────────────────────────────── */
.audit-tbl {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    font-family: var(--ds-font);
    font-size: var(--tbl-body-size);
}

/* Sticky header */
.audit-tbl thead tr {
    position: sticky;
    top: 0;
    z-index: 2;
}

.audit-tbl thead th {
    padding: 0 var(--tbl-cell-px);
    height: var(--tbl-head-h);
    background: var(--gray-50);
    border-bottom: 2px solid var(--gray-200);
    font-size: var(--tbl-head-size);
    font-weight: var(--ds-fw-bold);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--gray-500);
    white-space: nowrap;
    text-align: left;
    user-select: none;
    vertical-align: middle;
}

.audit-tbl thead th.col-navigate { text-align: center; }
.audit-tbl thead th.col-detail   { text-align: center; }

/* Rows */
.audit-tbl tbody tr {
    border-bottom: 1px solid var(--gray-100);
    transition: background 0.1s;
}

.audit-tbl tbody tr:last-child { border-bottom: none; }
.audit-tbl tbody tr:hover { background: var(--gray-50); }

.audit-tbl td {
    padding: 0 var(--tbl-cell-px);
    height: var(--tbl-row-h);
    vertical-align: middle;
    color: var(--gray-700);
    font-size: var(--tbl-body-size);
    overflow: hidden;
}

/* Column-specific alignment */
.audit-tbl td.col-navigate { text-align: center; }
.audit-tbl td.col-detail   { text-align: center; }

/* ── Timestamp cell ───────────────────────────────────────────── */
.audit-tbl td.col-timestamp {
    white-space: nowrap;
    font-size: var(--ds-body);
    color: var(--gray-800);
}

.ts-date {
    display: block;
    font-size: var(--ds-body);
    font-weight: var(--ds-fw-semibold);
    color: var(--gray-800);
    white-space: nowrap;
}

.ts-time {
    display: block;
    font-size: var(--ds-small);
    color: var(--gray-400);
    font-family: 'Consolas', monospace;
    margin-top: 1px;
}

/* ── Action badge ─────────────────────────────────────────────── */
.audit-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.12rem 0.45rem;
    border-radius: 4px;
    font-size: 0.69rem;
    font-weight: 700;
    white-space: nowrap;
    border: 1px solid transparent;
    letter-spacing: 0.02em;
    line-height: 1.6;
}

.audit-badge--create   { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }
.audit-badge--update   { background: #fff7ed; color: #c2410c; border-color: #fed7aa; }
.audit-badge--delete   { background: #fef2f2; color: #b91c1c; border-color: #fecaca; }
.audit-badge--test     { background: #ecfeff; color: #0e7490; border-color: #a5f3fc; }
.audit-badge--enqueue  { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }
.audit-badge--complete { background: #f0fdf4; color: #15803d; border-color: #bbf7d0; }
.audit-badge--fail     { background: #fef2f2; color: #b91c1c; border-color: #fecaca; }
.audit-badge--cancel   { background: #fffbeb; color: #b45309; border-color: #fde68a; }
.audit-badge--retry    { background: #f5f3ff; color: #6d28d9; border-color: #ddd6fe; }
.audit-badge--login    { background: #f0fdf4; color: #15803d; border-color: #bbf7d0; }
.audit-badge--logout   { background: #f8fafc; color: #475569; border-color: #e2e8f0; }

/* ── Entity cell ──────────────────────────────────────────────── */
.entity-type {
    display: block;
    font-size: var(--ds-body);
    font-weight: var(--ds-fw-semibold);
    color: var(--gray-800);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.entity-id {
    display: block;
    font-family: 'Consolas', monospace;
    font-size: var(--ds-small);
    color: var(--gray-400);
    margin-top: 1px;
}

/* ── Actor cell ───────────────────────────────────────────────── */
.actor-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    overflow: hidden;
}

.actor-avatar {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563eb, #7c3aed);
    color: #fff;
    font-size: 0.62rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.actor-name {
    font-size: var(--ds-body);
    font-weight: var(--ds-fw-medium);
    color: var(--gray-800);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.actor-system-icon {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #f1f5f9;
    color: #94a3b8;
    font-size: 0.62rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.actor-system {
    font-size: 0.78rem;
    color: #94a3b8;
    font-style: italic;
}

/* ── Description cell ─────────────────────────────────────────── */
.desc-text {
    display: block;
    font-size: var(--ds-body);
    color: var(--gray-600);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ── Navigate button ──────────────────────────────────────────── */
.audit-nav-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    background: #fff;
    color: #3b82f6;
    font-size: 0.72rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.audit-nav-btn:hover {
    background: #eff6ff;
    border-color: #93c5fd;
    color: #1d4ed8;
}

/* ── Eye button ───────────────────────────────────────────────── */
.audit-eye-btn {
    width: 24px;
    height: 24px;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    background: #f8fafc;
    color: #64748b;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    transition: all 0.15s;
    padding: 0;
}

.audit-eye-btn:hover {
    background: #2563eb;
    border-color: #2563eb;
    color: #fff;
}

/* ── Pagination ───────────────────────────────────────────────── */
.audit-pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px;
    border-top: 1px solid var(--gray-100);
    background: var(--gray-50);
    flex-shrink: 0;
}

.audit-page-info {
    font-size: var(--ds-label);
    color: var(--gray-500);
}

.audit-page-btns { display: flex; gap: 4px; }

.audit-page-btn {
    min-width: 26px;
    height: 26px;
    padding: 0 6px;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    font-size: var(--ds-label);
    font-family: var(--ds-font);
    color: var(--gray-600);
    cursor: pointer;
    transition: all 0.15s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.audit-page-btn:hover:not(:disabled) { background: #f1f5f9; border-color: #2563eb; color: #2563eb; }
.audit-page-btn.is-active            { background: #2563eb; border-color: #2563eb; color: #fff; font-weight: 700; }
.audit-page-btn:disabled             { opacity: 0.35; cursor: not-allowed; }

/* ── Detail Modal ─────────────────────────────────────────────── */
.audit-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,0.55);
    z-index: 1050;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    backdrop-filter: blur(3px);
}

.audit-modal {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 24px 64px rgba(0,0,0,0.22);
    width: 100%;
    max-width: 860px;
    max-height: 85vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.audit-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid #e2e8f0;
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 1;
    border-radius: 12px 12px 0 0;
}

.audit-modal-title {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.audit-modal-entity {
    font-size: var(--ds-h3);
    font-weight: var(--ds-fw-semibold);
    color: var(--gray-700);
}

.audit-modal-close {
    width: 32px;
    height: 32px;
    border: none;
    background: #f1f5f9;
    border-radius: 6px;
    color: #64748b;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    transition: all 0.15s;
    padding: 0;
}

.audit-modal-close:hover { background: #fee2e2; color: #dc2626; }

.audit-modal-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.8rem;
    color: #64748b;
    background: #fafbfc;
}

.audit-modal-desc-text {
    width: 100%;
    color: #334155;
    font-weight: 500;
    font-size: 0.85rem;
}

/* ── Diff panels ──────────────────────────────────────────────── */
.audit-diff-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1px;
    background: #e2e8f0;
    border-radius: 0 0 12px 12px;
    overflow: hidden;
}

.audit-diff-grid > :only-child { grid-column: 1 / -1; }

.audit-diff-panel {
    background: #fff;
    display: flex;
    flex-direction: column;
}

.diff-old { border-top: 3px solid #fca5a5; }
.diff-new { border-top: 3px solid #86efac; }

.diff-label {
    padding: 0.45rem 1rem;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    background: #f8fafc;
    border-bottom: 1px solid #e2e8f0;
}

.diff-old .diff-label { color: #dc2626; }
.diff-new .diff-label { color: #16a34a; }

.diff-json {
    margin: 0;
    padding: 1rem;
    font-size: 0.78rem;
    font-family: 'Consolas', 'Cascadia Code', 'JetBrains Mono', monospace;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
    color: #1e293b;
    background: transparent;
    border: none;
    flex: 1;
}

/* ── Responsive ─────────────────────────────────────────────── */
@media (max-width: 768px) {
    .audit-stats {
        grid-template-columns: repeat(2, 1fr);
    }
    .audit-scroll {
        overflow-x: auto;
    }
}

@media (max-width: 640px) {
    .audit-filters {
        flex-direction: column;
        height: auto;
        padding: 0.5rem 1rem;
        gap: 0.5rem;
        align-items: flex-start;
    }
    .audit-filter-actions {
        margin-left: 0;
        padding-left: 0;
    }
}

@media (max-width: 480px) {
    .audit-stats {
        grid-template-columns: 1fr;
    }
}

.audit-filter-select-scroll {
    max-height: 200px;
    overflow-y: auto;
    font-size: 0.76rem;
}

.audit-filter-select-scroll optgroup {
    font-size: 0.72rem;
    font-weight: 700;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.audit-filter-select-scroll option {
    font-size: 0.78rem;
    padding: 3px 6px;
    color: #1e293b;
}

/* ── Custom Action Filter Dropdown (afd-*) ── */
.afd-wrap {
    position: relative;
    display: inline-block;
}

.afd-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    width: 148px;
    height: 32px;
    padding: 0 10px;
    font-size: var(--ds-label);
    font-family: var(--ds-font);
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    color: var(--gray-800);
    cursor: pointer;
    text-align: left;
    transition: all 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.afd-trigger:focus, .afd-trigger:hover {
    border-color: #2563eb;
    background: #fff;
    outline: none;
}

.afd-caret {
    font-size: 0.65rem;
    color: #64748b;
    flex-shrink: 0;
}

.afd-menu {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    z-index: 9999;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.12);
    min-width: 160px;
    max-height: 220px;
    overflow-y: auto;
    padding: 4px 0;
}

.afd-group {
    padding: 6px 12px 3px;
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #94a3b8;
    pointer-events: none;
    border-top: 1px solid #f1f5f9;
    margin-top: 2px;
}

.afd-group:first-child { border-top: none; margin-top: 0; }

.afd-item {
    padding: 5px 12px 5px 20px;
    font-size: 0.78rem;
    color: #1e293b;
    cursor: pointer;
    transition: background 0.1s;
    white-space: nowrap;
}

.afd-item:hover { background: #eff6ff; color: #2563eb; }
.afd-item.afd-selected { background: #dbeafe; color: #1d4ed8; font-weight: 600; }
