/* ═══════════════════════════════════════════════════════════════
   Notes Hub — Knowledge Graph
   ═══════════════════════════════════════════════════════════════ */

.nh-graph {
    --nh-graph-bg: #ffffff;
    --nh-graph-grid: rgba(148, 163, 184, .08);
    --nh-graph-border: #e2e8f0;
    --nh-graph-text: #0f172a;
    --nh-graph-muted: #64748b;
    --nh-graph-faint: #94a3b8;
    /*
     * BUGFIX: bridged to theme tokens so accent-color customization
     * (Customizer → noteshub_accent) actually reaches the graph.
     * Falls back to the original violet if the theme tokens aren't
     * defined for some reason.
     */
    --nh-graph-accent: var(--nh-primary, #6366f1);
    --nh-graph-accent-2: var(--nh-primary-2, #8b5cf6);

    background: var(--nh-graph-bg);
    border: 1px solid var(--nh-graph-border);
    border-radius: 16px;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
    margin: 16px 0;
}

/*
 * BUGFIX: Removed `@media (prefers-color-scheme: dark)` block. Theme dark
 * mode is driven exclusively by the `html.nh-dark` toggle — no other
 * stylesheet in the theme respects the OS preference. Keeping this block
 * meant a user with a dark OS on a light theme saw a dark graph card
 * floating on a light page, and a user with a manual dark toggle on a
 * light OS got a light card (because the next selector didn't match —
 * see next fix).
 */

/*
 * BUGFIX: `body.nh-dark` never matched — theme.css puts `nh-dark` on <html>.
 * Every dark-mode rule here was inert for users who toggled dark mode via
 * the theme button. Now uses `html.nh-dark`.
 */
html.nh-dark .nh-graph,
body.noteshub-dark-default .nh-graph {
    --nh-graph-bg: #0b1220;
    --nh-graph-grid: rgba(148, 163, 184, .12);
    --nh-graph-border: #1e293b;
    --nh-graph-text: #f8fafc;
    --nh-graph-muted: #94a3b8;
    --nh-graph-faint: #64748b;
}

/* ─── Head ────────────────────────────────────────────────── */

.nh-graph-head {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 18px;
    border-bottom: 1px solid var(--nh-graph-border);
    flex-wrap: wrap;
}

.nh-graph-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 15px;
    font-weight: 800;
    color: var(--nh-graph-text);
    letter-spacing: -.01em;
}

.nh-graph-title-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--nh-graph-accent), var(--nh-graph-accent-2));
    font-size: 15px;
}

.nh-graph-stats {
    flex: 1;
    display: flex;
    gap: 12px;
    font-size: 12px;
    color: var(--nh-graph-muted);
    font-weight: 600;
}

.nh-graph-stats strong {
    color: var(--nh-graph-text);
    font-weight: 800;
    font-size: 14px;
}

.nh-graph-actions {
    display: flex;
    gap: 6px;
}

.nh-graph-btn {
    padding: 6px 14px;
    border-radius: 999px;
    border: 1px solid var(--nh-graph-border);
    background: transparent;
    color: var(--nh-graph-muted);
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    transition: all 150ms ease;
}

.nh-graph-btn:hover {
    border-color: var(--nh-graph-accent);
    color: var(--nh-graph-accent);
}

.nh-graph-btn--accent {
    background: linear-gradient(135deg, var(--nh-graph-accent), var(--nh-graph-accent-2));
    border-color: transparent;
    color: #fff;
}

.nh-graph-btn--accent:hover {
    color: #fff;
    /*
     * BUGFIX: was hardcoded rgba(99,102,241,.5) — the accent-tinted glow
     * var is already published by the theme bridge.
     */
    box-shadow: 0 4px 12px -4px var(--nh-primary-glow, rgba(99, 102, 241, .5));
}

.nh-graph-btn:disabled {
    opacity: .5;
    cursor: wait;
}

/* ─── Canvas ──────────────────────────────────────────────── */

.nh-graph-canvas-wrap {
    position: relative;
    /*
     * BUGFIX: removed the dead radial-gradient (transparent → transparent
     * has no visual effect).
     */
    background:
        repeating-linear-gradient(0deg, var(--nh-graph-grid) 0, var(--nh-graph-grid) 1px, transparent 1px, transparent 40px),
        repeating-linear-gradient(90deg, var(--nh-graph-grid) 0, var(--nh-graph-grid) 1px, transparent 1px, transparent 40px);
    overflow: hidden;
    /*
     * BUGFIX: `touch-action: none` disabled ALL touch gestures on this
     * element, including page scroll — on mobile, users got stuck when
     * their finger landed on the graph. `pan-y` lets the browser handle
     * vertical page scroll while leaving horizontal panning and node
     * dragging to the graph's JS.
     *
     * If the graph library truly needs full gesture takeover, the JS
     * should toggle an `.is-panning` class that sets `touch-action: none`
     * only while a drag is in progress.
     */
    touch-action: pan-y;
}

.nh-graph canvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: grab;
}

.nh-graph canvas:active {
    cursor: grabbing;
}

.nh-graph canvas.is-node-hover {
    cursor: pointer;
}

/* ─── Loading / empty states ─────────────────────────────── */

.nh-graph-loading,
.nh-graph-empty {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    background: color-mix(in srgb, var(--nh-graph-bg) 88%, transparent);
    /* BUGFIX: added -webkit- prefix — required on Safari < 18. */
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    color: var(--nh-graph-muted);
    font-size: 13px;
    font-weight: 600;
    z-index: 5;
}

.nh-graph-empty[hidden],
.nh-graph-loading[hidden] { display: none; }

.nh-graph-empty-icon {
    font-size: 48px;
    opacity: .6;
}

.nh-graph-empty h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 800;
    color: var(--nh-graph-text);
}

.nh-graph-empty p {
    margin: 0;
    max-width: 380px;
    text-align: center;
    line-height: 1.6;
}

.nh-graph-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid color-mix(in srgb, var(--nh-graph-accent) 20%, transparent);
    border-top-color: var(--nh-graph-accent);
    border-radius: 50%;
    animation: nh-graph-spin .8s linear infinite;
}

@keyframes nh-graph-spin { to { transform: rotate(360deg); } }

/* ─── Tooltip ─────────────────────────────────────────────── */

.nh-graph-tooltip {
    position: absolute;
    background: var(--nh-graph-text);
    color: var(--nh-graph-bg);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 700;
    pointer-events: none;
    white-space: nowrap;
    z-index: 10;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    box-shadow: 0 8px 24px -8px rgba(15, 23, 42, .3);
    /* Default: position above the anchor. */
    transform: translate(-50%, -120%);
}

/*
 * BUGFIX: The default position-above got clipped by the wrap's
 * overflow: hidden for any node near the top edge of the canvas. The JS
 * should add `.is-below` when the anchor's y is < ~50px so the tooltip
 * flips to below the cursor.
 *
 * JS:
 *   var below = anchorY < 50;
 *   tooltip.classList.toggle( 'is-below', below );
 */
.nh-graph-tooltip.is-below {
    transform: translate(-50%, 20%);
}

.nh-graph-tooltip[hidden] { display: none; }

/* ─── Foot ────────────────────────────────────────────────── */

.nh-graph-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    padding: 12px 18px;
    border-top: 1px solid var(--nh-graph-border);
    font-size: 12px;
    color: var(--nh-graph-muted);
    flex-wrap: wrap;
}

.nh-graph-hint {
    font-weight: 600;
}

.nh-graph-legend {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
}

.nh-graph-legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--nh-graph-accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--nh-graph-accent) 20%, transparent);
}

/* ═══════════════════════════════════════════════════════════════
   Backlinks panel
   ═══════════════════════════════════════════════════════════════ */

.nh-backlinks {
    --nh-bl-border: #e2e8f0;
    --nh-bl-bg: #ffffff;
    --nh-bl-bg-soft: #f8fafc;
    --nh-bl-text: #0f172a;
    --nh-bl-muted: #64748b;
    --nh-bl-faint: #94a3b8;
    /* BUGFIX: same token-bridge as the graph. */
    --nh-bl-accent: var(--nh-primary, #6366f1);

    background: var(--nh-bl-bg);
    border: 1px solid var(--nh-bl-border);
    border-radius: 14px;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
    margin: 12px 0;
}

/* BUGFIX: `body.nh-dark` → `html.nh-dark`. */
html.nh-dark .nh-backlinks,
body.noteshub-dark-default .nh-backlinks {
    --nh-bl-border: #1e293b;
    --nh-bl-bg: #0f172a;
    --nh-bl-bg-soft: #111a2e;
    --nh-bl-text: #f8fafc;
    --nh-bl-muted: #94a3b8;
    --nh-bl-faint: #64748b;
}

.nh-backlinks-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--nh-bl-bg-soft);
    border-bottom: 1px solid var(--nh-bl-border);
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--nh-bl-muted);
}

.nh-backlinks-icon { font-size: 14px; }

.nh-backlinks-refresh {
    margin-left: auto;
    background: transparent;
    border: none;
    color: var(--nh-bl-muted);
    font-size: 14px;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 6px;
    font-family: inherit;
}

.nh-backlinks-refresh:hover {
    background: var(--nh-bl-border);
    color: var(--nh-bl-accent);
}

.nh-backlinks-body {
    padding: 6px;
    min-height: 40px;
    /*
     * Preemptive: if this ever becomes `overflow-y: auto`, the following
     * prevents bottom-of-list scroll from chaining to the parent app.
     */
    overscroll-behavior-y: contain;
}

.nh-backlinks-section {
    padding: 6px 8px 2px;
    font-size: 10.5px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--nh-bl-faint);
}

.nh-backlinks-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 10px;
    text-decoration: none;
    color: var(--nh-bl-text);
    font-size: 13.5px;
    font-weight: 600;
    transition: background 120ms ease;
    margin-bottom: 2px;
}

.nh-backlinks-item:hover {
    background: var(--nh-bl-bg-soft);
    color: var(--nh-bl-accent);
}

.nh-backlinks-item-icon {
    flex-shrink: 0;
    width: 26px;
    height: 26px;
    border-radius: 6px;
    background: var(--nh-bl-bg-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}

.nh-backlinks-item-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nh-backlinks-empty {
    padding: 14px 12px;
    text-align: center;
    color: var(--nh-bl-muted);
    font-size: 12.5px;
    line-height: 1.5;
}

.nh-backlinks-loading {
    padding: 20px;
    display: flex;
    justify-content: center;
}

.nh-backlinks-spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--nh-bl-border);
    border-top-color: var(--nh-bl-accent);
    border-radius: 50%;
    animation: nh-graph-spin .8s linear infinite;
}

@media (max-width: 640px) {
    .nh-graph-head { padding: 12px 14px; }
    .nh-graph-stats { display: none; }
    .nh-graph-foot { padding: 10px 14px; font-size: 11px; }
    .nh-graph-btn { padding: 5px 10px; font-size: 11px; }
}

/* ═══════════════════════════════════════════════════════════════
   Print — hide interactive chrome, keep the visualization
   BUGFIX: previously missing. Canvas content usually prints blank,
   and the buttons / tooltips / loading overlays have no print value.
   Keep the card border so the printed page shows where the graph
   belongs, and let background colors render.
   ═══════════════════════════════════════════════════════════════ */

@media print {
    .nh-graph-actions,
    .nh-graph-tooltip,
    .nh-graph-loading,
    .nh-graph-empty,
    .nh-backlinks-refresh,
    .nh-backlinks-loading {
        display: none !important;
    }
    .nh-graph,
    .nh-backlinks {
        border: 1px solid #ccc;
        box-shadow: none;
        page-break-inside: avoid;
    }
}