/* ═══════════════════════════════════════════════════════════════════
   NIR INTEGRATION — Theme-Aware Styling
   Matches Invoicing.razor / TicketsMain.razor pattern (--bg-primary,
   --accent-primary, etc.) so it inherits every Majesty theme without
   needing a per-theme override.
   ═══════════════════════════════════════════════════════════════════ */

.nir-page {
    width: 100%;
    height: calc(100vh - 60px);
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
}

/* Force ml-main to stop scrolling — the grid manages its own scroll. */
.ml-main:has(.nir-page) { overflow: hidden !important; }

/* ── Header bar ─────────────────────────────────────────────────── */
.nir-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    flex-shrink: 0;
}

.nir-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-primary);
}

.nir-header-left > i,
.nir-header-left svg {
    width: 22px;
    height: 22px;
    color: var(--accent-primary);
}

.nir-header-left > span:first-of-type {
    font-size: 18px;
    font-weight: 600;
}

.nir-header-sub {
    font-size: 13px;
    color: var(--text-muted);
    border-left: 1px solid var(--border-primary);
    padding-left: 12px;
    margin-left: 4px;
}

.nir-header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* ── Lookback input ─────────────────────────────────────────────── */
.nir-lookback {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    height: 36px;
    background: var(--bg-primary);
    border: 1px solid var(--border-primary);
    border-radius: 6px;
}

.nir-lookback label {
    font-size: 13px;
    color: var(--text-muted);
    margin: 0;
}

.nir-lookback-input {
    width: 54px;
    height: 28px;
    padding: 0 6px;
    font-size: 13px;
    font-weight: 600;
    background: transparent;
    border: 1px solid var(--border-primary);
    border-radius: 4px;
    color: var(--text-primary);
    text-align: center;
}

.nir-lookback-input:focus {
    outline: none;
    border-color: var(--accent-primary);
}

/* ── Buttons ────────────────────────────────────────────────────── */
.nir-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 36px;
    padding: 0 16px;
    font-size: 13px;
    font-weight: 600;
    border-radius: 6px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.nir-btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.nir-btn i,
.nir-btn svg {
    width: 16px;
    height: 16px;
}

.nir-btn-primary {
    background: var(--accent-primary);
    color: var(--on-accent, #fff);
}
.nir-btn-primary:hover:not(:disabled) {
    background: var(--accent-primary-hover, var(--accent-primary));
    filter: brightness(1.08);
}

.nir-btn-success {
    background: #16a34a;
    color: #fff;
    border-color: #15803d;
}
.nir-btn-success:hover:not(:disabled) {
    background: #15803d;
}

/* ── Spinner glyph on the Scan button ───────────────────────────── */
.nir-spin {
    animation: nir-spin 0.9s linear infinite;
}
@keyframes nir-spin {
    to { transform: rotate(360deg); }
}

/* ── Status banner ──────────────────────────────────────────────── */
.nir-status {
    padding: 14px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    flex-shrink: 0;
}

.nir-status-ok {
    border-left: 4px solid #16a34a;
}

.nir-status-warn {
    border-left: 4px solid #f59e0b;
}

.nir-status-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.nir-status-counts {
    display: flex;
    align-items: center;
    gap: 32px;
    flex-wrap: wrap;
}

.nir-stat {
    display: flex;
    flex-direction: column;
}

.nir-stat-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    font-weight: 500;
}

.nir-stat-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
    margin-top: 2px;
}

.nir-status-error {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    padding: 10px 12px;
    background: rgba(245, 158, 11, 0.08);
    border: 1px solid rgba(245, 158, 11, 0.35);
    border-radius: 6px;
    font-size: 13px;
    color: var(--text-primary);
}

.nir-status-error i,
.nir-status-error svg {
    width: 16px;
    height: 16px;
    color: #f59e0b;
    flex-shrink: 0;
}

/* ── Empty-state hint ───────────────────────────────────────────── */
.nir-empty-hint {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    color: var(--text-muted);
    font-size: 13px;
    flex-shrink: 0;
}

.nir-empty-hint i,
.nir-empty-hint svg {
    width: 16px;
    height: 16px;
    color: var(--accent-primary);
}

.nir-empty-hint strong {
    color: var(--text-primary);
}

/* ── Grid wrap ──────────────────────────────────────────────────── */
.nir-grid-wrap {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-primary);
}

.nir-grid-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: var(--overlay-light, rgba(0, 0, 0, 0.05));
    backdrop-filter: blur(2px);
    z-index: 10;
}

.nir-spinner-ring {
    width: 44px;
    height: 44px;
    border: 3px solid var(--border-primary);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: nir-spin 1s linear infinite;
}

.nir-grid-overlay p {
    margin: 0;
    color: var(--text-muted);
    font-size: 14px;
    font-weight: 500;
}

/* ── Empty-state (no rows after a scan) ─────────────────────────── */
.nir-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 24px;
    text-align: center;
}

.nir-empty i,
.nir-empty svg {
    width: 48px;
    height: 48px;
    color: #16a34a;
    margin-bottom: 16px;
}

.nir-empty h3 {
    margin: 0 0 8px;
    color: var(--text-primary);
    font-size: 18px;
    font-weight: 600;
}

.nir-empty p {
    margin: 0;
    max-width: 520px;
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.6;
}

.nir-empty code {
    background: var(--bg-secondary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
    color: var(--text-primary);
}

/* ── Results table ──────────────────────────────────────────────── */
.nir-table-wrap {
    height: 100%;
    overflow: auto;
}

.nir-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 13px;
}

.nir-table thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-weight: 600;
    text-align: left;
    padding: 10px 12px;
    border-bottom: 2px solid var(--border-primary);
    white-space: nowrap;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.4px;
}

.nir-table tbody td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-primary);
    color: var(--text-primary);
    vertical-align: top;
}

.nir-table tbody tr:hover td {
    background: var(--bg-secondary);
}

.nir-th-results {
    min-width: 360px;
}

.nir-mono {
    font-family: 'JetBrains Mono', 'Consolas', monospace;
    font-size: 12px;
}

.nir-muted {
    color: var(--text-muted);
}

.nir-results {
    font-family: 'JetBrains Mono', 'Consolas', monospace;
    font-size: 12px;
    line-height: 1.5;
    white-space: normal;
    color: var(--text-primary);
}

/* ── Machine chip ───────────────────────────────────────────────── */
.nir-chip {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

.nir-chip-lab {
    background: rgba(37, 99, 235, 0.12);
    color: #2563eb;
    border: 1px solid rgba(37, 99, 235, 0.35);
}

.nir-chip-grading {
    background: rgba(124, 58, 237, 0.12);
    color: #7c3aed;
    border: 1px solid rgba(124, 58, 237, 0.35);
}

/* ── Flag pills ─────────────────────────────────────────────────── */
.nir-flag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    margin-right: 4px;
}

.nir-flag-rejected {
    background: rgba(220, 38, 38, 0.12);
    color: #dc2626;
    border: 1px solid rgba(220, 38, 38, 0.35);
}

.nir-flag-ignored {
    background: rgba(245, 158, 11, 0.12);
    color: #b45309;
    border: 1px solid rgba(245, 158, 11, 0.35);
}

.nir-flag-ok {
    background: rgba(22, 163, 74, 0.10);
    color: #16a34a;
    border: 1px solid rgba(22, 163, 74, 0.35);
}

/* ── Dark-theme contrast adjustments ────────────────────────────── */
:root[data-theme="dark"] .nir-chip-lab,
:root[data-theme="wa-dark"] .nir-chip-lab,
:root[data-theme="meta-dark"] .nir-chip-lab {
    color: #60a5fa;
    background: rgba(96, 165, 250, 0.15);
    border-color: rgba(96, 165, 250, 0.4);
}

:root[data-theme="dark"] .nir-chip-grading,
:root[data-theme="wa-dark"] .nir-chip-grading,
:root[data-theme="meta-dark"] .nir-chip-grading {
    color: #a78bfa;
    background: rgba(167, 139, 250, 0.15);
    border-color: rgba(167, 139, 250, 0.4);
}

:root[data-theme="dark"] .nir-flag-rejected,
:root[data-theme="wa-dark"] .nir-flag-rejected,
:root[data-theme="meta-dark"] .nir-flag-rejected {
    color: #f87171;
    background: rgba(248, 113, 113, 0.15);
    border-color: rgba(248, 113, 113, 0.4);
}

:root[data-theme="dark"] .nir-flag-ignored,
:root[data-theme="wa-dark"] .nir-flag-ignored,
:root[data-theme="meta-dark"] .nir-flag-ignored {
    color: #fbbf24;
    background: rgba(251, 191, 36, 0.15);
    border-color: rgba(251, 191, 36, 0.4);
}

:root[data-theme="dark"] .nir-flag-ok,
:root[data-theme="wa-dark"] .nir-flag-ok,
:root[data-theme="meta-dark"] .nir-flag-ok {
    color: #4ade80;
    background: rgba(74, 222, 128, 0.15);
    border-color: rgba(74, 222, 128, 0.4);
}

/* ── Auto-sync control banner ───────────────────────────────────── */
.nir-autosync {
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    flex-shrink: 0;
    border-left: 4px solid var(--border-primary);
    transition: border-left-color 0.2s;
}

.nir-autosync-on {
    border-left-color: #16a34a;
    background: linear-gradient(
        to right,
        rgba(22, 163, 74, 0.06) 0%,
        var(--bg-secondary) 200px
    );
}

.nir-autosync-off {
    border-left-color: var(--text-muted);
}

.nir-autosync-row {
    display: flex;
    align-items: center;
    gap: 24px;
    flex-wrap: wrap;
}

.nir-autosync-state {
    margin-left: 4px;
    font-weight: 700;
    letter-spacing: 0.5px;
}
.nir-autosync-on .nir-autosync-state {
    color: #16a34a;
}
.nir-autosync-off .nir-autosync-state {
    color: var(--text-muted);
}

/* Larger pill switch for the auto-sync toggle so it stands out
   from the smaller "Show imported too" toggle in the header. */
.nir-toggle-lg .nir-toggle-track {
    width: 40px;
    height: 22px;
}
.nir-toggle-lg .nir-toggle-thumb {
    width: 18px;
    height: 18px;
}
.nir-toggle-lg input[type="checkbox"]:checked + .nir-toggle-track .nir-toggle-thumb {
    left: 20px;
}
.nir-toggle-lg .nir-toggle-label {
    font-size: 14px;
}

.nir-autosync-interval {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-muted);
}
.nir-autosync-select {
    height: 32px;
    padding: 0 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-primary);
    border-radius: 4px;
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
.nir-autosync-select:focus {
    outline: none;
    border-color: var(--accent-primary);
}

.nir-autosync-status {
    font-size: 13px;
    color: var(--text-primary);
}
.nir-autosync-running {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--accent-primary);
    font-weight: 600;
}
.nir-autosync-running i,
.nir-autosync-running svg {
    width: 14px;
    height: 14px;
}
.nir-autosync-next strong {
    color: var(--accent-primary);
    font-family: 'JetBrains Mono', 'Consolas', monospace;
    font-size: 14px;
}
.nir-autosync-idle {
    color: var(--text-muted);
    font-style: italic;
}

.nir-autosync-stats {
    margin-left: auto;
    font-size: 12px;
    color: var(--text-muted);
}
.nir-autosync-stats strong {
    color: var(--text-primary);
}
.nir-autosync-last {
    margin-left: 4px;
}

.nir-autosync-error {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    padding: 8px 12px;
    background: rgba(245, 158, 11, 0.08);
    border: 1px solid rgba(245, 158, 11, 0.35);
    border-radius: 6px;
    font-size: 12px;
    color: var(--text-primary);
}
.nir-autosync-error i,
.nir-autosync-error svg {
    width: 14px;
    height: 14px;
    color: #f59e0b;
    flex-shrink: 0;
}

/* ── Import-result banner ───────────────────────────────────────── */
.nir-import-result {
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    flex-shrink: 0;
}

.nir-import-result-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.nir-import-result-icon {
    width: 20px;
    height: 20px;
    color: #16a34a;
    flex-shrink: 0;
}

.nir-import-result.nir-status-warn .nir-import-result-icon {
    color: #f59e0b;
}

.nir-import-result-text {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    flex-wrap: wrap;
    font-size: 13px;
    color: var(--text-primary);
}

.nir-import-result-text strong {
    color: var(--text-primary);
}

.nir-import-result-time {
    margin-left: auto;
    color: var(--text-muted);
    font-family: 'JetBrains Mono', 'Consolas', monospace;
    font-size: 12px;
}

.nir-import-dismiss {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-muted);
    width: 28px;
    height: 28px;
    border-radius: 4px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.nir-import-dismiss:hover {
    background: var(--bg-primary);
    color: var(--text-primary);
}
.nir-import-dismiss i,
.nir-import-dismiss svg {
    width: 16px;
    height: 16px;
}

.nir-import-errors {
    margin-top: 8px;
    padding: 8px 12px;
    background: rgba(245, 158, 11, 0.05);
    border: 1px solid rgba(245, 158, 11, 0.2);
    border-radius: 6px;
}
.nir-import-errors summary {
    cursor: pointer;
    font-size: 12px;
    color: var(--text-muted);
    user-select: none;
}
.nir-import-errors[open] summary {
    margin-bottom: 6px;
    color: var(--text-primary);
    font-weight: 500;
}
.nir-import-errors ul {
    margin: 0;
    padding-left: 18px;
    font-size: 12px;
    line-height: 1.6;
    color: var(--text-primary);
    max-height: 240px;
    overflow: auto;
}
.nir-import-errors li {
    font-family: 'JetBrains Mono', 'Consolas', monospace;
}

/* ── Show-imported toggle (header-bar pill switch) ──────────────── */
.nir-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 36px;
    padding: 0 12px;
    background: var(--bg-primary);
    border: 1px solid var(--border-primary);
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
    transition: border-color 0.15s;
}
.nir-toggle:hover {
    border-color: var(--accent-primary);
}
.nir-toggle input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}
.nir-toggle-track {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 18px;
    background: var(--border-primary);
    border-radius: 999px;
    transition: background 0.18s;
    flex-shrink: 0;
}
.nir-toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    transition: left 0.18s;
}
.nir-toggle input[type="checkbox"]:checked + .nir-toggle-track {
    background: var(--accent-primary);
}
.nir-toggle input[type="checkbox"]:checked + .nir-toggle-track .nir-toggle-thumb {
    left: 16px;
}
.nir-toggle-label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
}

/* ── Status stat colours (pending vs imported breakdown) ────────── */
.nir-stat-value-pending {
    color: var(--accent-primary);
}
.nir-stat-value-imported {
    color: var(--text-muted);
}

/* ── Pending / Imported status flags in the grid ────────────────── */
.nir-flag-pending {
    background: rgba(37, 99, 235, 0.12);
    color: #2563eb;
    border: 1px solid rgba(37, 99, 235, 0.35);
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.nir-flag-pending i,
.nir-flag-pending svg {
    width: 12px;
    height: 12px;
}

.nir-flag-imported {
    background: rgba(107, 114, 128, 0.12);
    color: #6b7280;
    border: 1px solid rgba(107, 114, 128, 0.35);
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.nir-flag-imported i,
.nir-flag-imported svg {
    width: 12px;
    height: 12px;
}

/* Dim already-imported rows so the eye lands on pending first.
   The dim isn't so strong that the row becomes unreadable — the
   operator can still see everything, it just falls into the
   background visually. */
.nir-row-imported td {
    opacity: 0.55;
    background: var(--bg-secondary);
}
.nir-row-imported:hover td {
    opacity: 0.9;
}

/* Pending rows when the toggle is ON get a subtle left-edge accent
   so they're visually distinct from the dimmed imported rows. */
.nir-row-pending td:first-child {
    box-shadow: inset 3px 0 0 var(--accent-primary);
}

/* ── Dark-theme tweaks for new status flags ─────────────────────── */
:root[data-theme="dark"] .nir-flag-pending,
:root[data-theme="wa-dark"] .nir-flag-pending,
:root[data-theme="meta-dark"] .nir-flag-pending {
    color: #60a5fa;
    background: rgba(96, 165, 250, 0.15);
    border-color: rgba(96, 165, 250, 0.4);
}

:root[data-theme="dark"] .nir-flag-imported,
:root[data-theme="wa-dark"] .nir-flag-imported,
:root[data-theme="meta-dark"] .nir-flag-imported {
    color: #9ca3af;
    background: rgba(156, 163, 175, 0.15);
    border-color: rgba(156, 163, 175, 0.4);
}

/* ── Mobile responsive ──────────────────────────────────────────── */
@media (max-width: 900px) {
    .nir-header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }
    .nir-header-right {
        justify-content: flex-end;
        flex-wrap: wrap;
    }
    .nir-header-sub {
        display: none;
    }
    .nir-status-counts {
        gap: 18px;
    }
    .nir-th-results {
        min-width: 240px;
    }
}
