/* ═══════════════════════════════════════════════════════════════
   Notes Hub — AI Features
   ═══════════════════════════════════════════════════════════════ */

:root {
    /*
     * BUGFIX: bridged to theme tokens so the Customizer accent reaches
     * AI features. Note that the ordering is INTENTIONAL and the opposite
     * of the theme's: AI leads with violet (--nh-primary-2) and uses
     * indigo (--nh-primary) as the secondary. Preserves the AI-branded
     * gradient direction while following user customization.
     */
    --nh-ai-accent:   var(--nh-primary-2, #8b5cf6);
    --nh-ai-accent-2: var(--nh-primary,   #6366f1);
    --nh-ai-surface: #ffffff;
    --nh-ai-border: #e2e8f0;
    --nh-ai-text: #0f172a;
    --nh-ai-muted: #64748b;
    --nh-ai-bg: rgba(15, 23, 42, .5);
}

/*
 * BUGFIX: removed `@media (prefers-color-scheme: dark)` block. The theme
 * drives dark mode exclusively via the `html.nh-dark` toggle — nothing
 * else in the theme respects the OS preference. Keeping this block meant:
 *   - a dark OS with light theme showed a dark AI panel on a light page;
 *   - the block's `:root` scope bled dark variables into every element
 *     on the page (not just `.nh-ai-*`), producing cascading surprises.
 *
 * BUGFIX: `body.nh-dark` → `html.nh-dark`. The theme toggles `nh-dark`
 * on <html>, so this selector previously matched nothing and dark mode
 * only worked for users on the admin-set dark default.
 */
html.nh-dark,
body.noteshub-dark-default {
    --nh-ai-surface: #111a2e;
    --nh-ai-border: #1e293b;
    --nh-ai-text: #f8fafc;
    --nh-ai-muted: #94a3b8;
}

/* ─── AI toolbar on note cards ─────────────────────────────── */

.nh-ai-toolbar {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin: 10px 0 6px;
}

.nh-ai-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    color: var(--nh-ai-accent);
    background: color-mix(in srgb, var(--nh-ai-accent) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--nh-ai-accent) 25%, transparent);
    border-radius: 999px;
    cursor: pointer;
    transition: transform 120ms ease, background 150ms ease, box-shadow 150ms ease;
    -webkit-tap-highlight-color: transparent;
}

.nh-ai-btn:hover {
    background: color-mix(in srgb, var(--nh-ai-accent) 18%, transparent);
    transform: translateY(-1px);
    box-shadow: 0 4px 10px -4px color-mix(in srgb, var(--nh-ai-accent) 50%, transparent);
}

.nh-ai-btn:active {
    transform: scale(.97);
}

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

.nh-ai-btn[data-busy="1"] {
    pointer-events: none;
}

.nh-ai-btn-icon {
    font-size: 14px;
    line-height: 1;
}

/* ─── Editor inline toolbar ────────────────────────────────── */

.nh-ai-toolbar-inline {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin: 8px 0 0;
    padding-top: 8px;
    border-top: 1px dashed var(--nh-ai-border);
    width: 100%;
    box-sizing: border-box;
}

.nh-ai-toolbar-inline .nh-ai-btn {
    font-size: 11px;
    padding: 5px 10px;
    background: linear-gradient(
        135deg,
        color-mix(in srgb, var(--nh-ai-accent-2) 12%, transparent),
        color-mix(in srgb, var(--nh-ai-accent) 12%, transparent)
    );
    border-color: color-mix(in srgb, var(--nh-ai-accent) 30%, transparent);
}

.nh-ai-toolbar-inline .nh-ai-btn:hover {
    background: linear-gradient(
        135deg,
        color-mix(in srgb, var(--nh-ai-accent-2) 22%, transparent),
        color-mix(in srgb, var(--nh-ai-accent) 22%, transparent)
    );
}

/* ─── Overlay backdrop ─────────────────────────────────────── */

/*
 * BUGFIX: moved up from 99999 → 100200. Was below the mobile drawer
 * (100000), auth modal (100050), kanban move sheet (100060), and every
 * goppo picker (100150). If any of those were open when the AI modal
 * was triggered (e.g. via a Cmd/Ctrl+K hotkey that bypasses normal
 * pointer flow), the AI panel appeared behind an opaque layer.
 *
 * Now sits above every navigation/modal tier except the notification
 * panel (9999999) and the editor modal (2147483647), which legitimately
 * need to remain on top.
 *
 * BUGFIX: also added visibility + pointer-events toggling via `.is-open`
 * so the overlay can't block clicks if the JS ever fails to remove it
 * from the DOM after the closing animation. Same pattern used by
 * .nh-notif-overlay and .nh-kanban-sheet-overlay.
 */
.nh-ai-overlay {
    position: fixed;
    inset: 0;
    background: var(--nh-ai-bg);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 100200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    animation: nh-ai-fade-in 200ms ease forwards;
}

.nh-ai-overlay.is-open {
    visibility: visible;
    pointer-events: auto;
}

@keyframes nh-ai-fade-in {
    to { opacity: 1; }
}

.nh-ai-overlay.is-closing {
    animation: nh-ai-fade-out 180ms ease forwards;
}

@keyframes nh-ai-fade-out {
    to { opacity: 0; }
}

/* ─── Result panel ─────────────────────────────────────────── */

.nh-ai-panel {
    width: 100%;
    max-width: 520px;
    background: var(--nh-ai-surface);
    border: 1px solid var(--nh-ai-border);
    border-radius: 18px;
    box-shadow: 0 24px 60px -12px rgba(15, 23, 42, .35);
    overflow: hidden;
    transform: translateY(12px);
    animation: nh-ai-slide-up 240ms cubic-bezier(.2, .9, .3, 1) forwards;
}

@keyframes nh-ai-slide-up {
    to { transform: translateY(0); }
}

.nh-ai-panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 20px;
    border-bottom: 1px solid var(--nh-ai-border);
}

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

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

.nh-ai-close {
    background: transparent;
    border: none;
    font-size: 22px;
    line-height: 1;
    color: var(--nh-ai-muted);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 8px;
    font-family: inherit;
}

.nh-ai-close:hover {
    background: color-mix(in srgb, var(--nh-ai-muted) 12%, transparent);
    color: var(--nh-ai-text);
}

/* ─── Body ─────────────────────────────────────────────────── */

.nh-ai-panel-body {
    padding: 20px;
    font-size: 15px;
    line-height: 1.65;
    color: var(--nh-ai-text);
    white-space: pre-wrap;
    /* BUGFIX: `overflow-wrap` is the modern property; `word-wrap` is a
     * legacy alias. Matches the pattern used elsewhere in the theme
     * (e.g. .nh-collab-draft-body). */
    overflow-wrap: break-word;
    max-height: 60vh;
    overflow-y: auto;
    /* BUGFIX: prevent the body's scroll from chaining to the page when
     * the user reaches the top/bottom of AI-generated text. */
    overscroll-behavior-y: contain;
}

.nh-ai-panel-body.is-loading {
    min-height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.nh-ai-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    color: var(--nh-ai-muted);
    font-size: 13px;
    font-weight: 600;
}

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

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

/* ─── Tag pills ────────────────────────────────────────────── */

.nh-ai-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.nh-ai-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 700;
    color: var(--nh-ai-accent);
    background: color-mix(in srgb, var(--nh-ai-accent) 12%, transparent);
    border-radius: 999px;
    cursor: pointer;
    transition: transform 120ms ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.nh-ai-tag:hover {
    transform: translateY(-1px);
}

/* BUGFIX: hardcoded #10b981 → theme token for consistency. */
.nh-ai-tag.is-copied {
    background: color-mix(in srgb, var(--nh-success, #10b981) 18%, transparent);
    color: var(--nh-success, #10b981);
}

/* ─── Footer actions ───────────────────────────────────────── */

.nh-ai-panel-foot {
    display: flex;
    gap: 10px;
    padding: 14px 20px;
    border-top: 1px solid var(--nh-ai-border);
    background: color-mix(in srgb, var(--nh-ai-muted) 4%, transparent);
}

.nh-ai-action {
    flex: 1;
    padding: 10px 16px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid var(--nh-ai-border);
    background: var(--nh-ai-surface);
    color: var(--nh-ai-text);
    transition: transform 120ms ease, background 150ms ease;
    -webkit-tap-highlight-color: transparent;
}

.nh-ai-action:hover {
    background: color-mix(in srgb, var(--nh-ai-muted) 6%, transparent);
    transform: translateY(-1px);
}

.nh-ai-action.is-primary {
    background: linear-gradient(135deg, var(--nh-ai-accent-2), var(--nh-ai-accent));
    border-color: transparent;
    color: #fff;
}

.nh-ai-action.is-primary:hover {
    box-shadow: 0 8px 20px -6px color-mix(in srgb, var(--nh-ai-accent) 60%, transparent);
}

/* ─── Error state ──────────────────────────────────────────── */

/* BUGFIX: hardcoded #ef4444 → theme token. */
.nh-ai-error {
    text-align: center;
    color: var(--nh-danger, #ef4444);
    font-size: 14px;
    font-weight: 600;
    padding: 12px 0;
}

.nh-ai-error-icon {
    font-size: 28px;
    margin-bottom: 8px;
    display: block;
}

/* ─── Mobile — bottom sheet ────────────────────────────────── */

/*
 * BUGFIX: hoisted `@keyframes nh-ai-sheet-up` out of the media query.
 * Some bundlers and older Safari versions mishandle keyframes defined
 * inside @media. Behavior is identical; portability is better.
 */
@keyframes nh-ai-sheet-up {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}

@media (max-width: 640px) {
    .nh-ai-overlay {
        align-items: flex-end;
        padding: 0;
    }

    .nh-ai-panel {
        max-width: 100%;
        border-radius: 20px 20px 0 0;
        animation: nh-ai-sheet-up 280ms cubic-bezier(.2, .9, .3, 1) forwards;
    }

    .nh-ai-panel-body {
        max-height: 55vh;
    }

    /* Compact inline toolbar on mobile */
    .nh-ai-toolbar-inline .nh-ai-btn {
        font-size: 10px;
        padding: 4px 8px;
        gap: 4px;
    }

    .nh-ai-toolbar-inline .nh-ai-btn-icon {
        font-size: 12px;
    }
}

/* ═══════════════════════════════════════════════════════════════
   Print — hide AI chrome
   BUGFIX: previously missing. The overlay, toolbar, and tag pills
   are interactive with no print value.
   ═══════════════════════════════════════════════════════════════ */

@media print {
    .nh-ai-toolbar,
    .nh-ai-toolbar-inline,
    .nh-ai-overlay {
        display: none !important;
    }
}