/**
 * Cited Source Citation Styles
 *
 * Styles for clickable cited source citations [Author]{source_XXX}
 * including tooltip and modal components.
 */

/* ==============================================
   INLINE CITATION STYLING
   ============================================== */

.cited-source-citation {
    color: var(--bb-accent, #16a34a);
    font-weight: 500;
    cursor: pointer;
    padding: 0.1em 0.2em;
    margin: 0 0.05em;
    border-radius: 3px;
    transition: background-color 0.15s ease, color 0.15s ease;
    text-decoration: none;
    white-space: nowrap;
    border-bottom: 1px dotted var(--bb-accent, #16a34a);
    /* CRITICAL: Ensure citations are selectable as part of surrounding text */
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
}

.cited-source-citation:hover {
    background-color: rgba(22, 163, 74, 0.1);
    text-decoration: none;
    border-bottom-style: solid;
}

.cited-source-citation:focus {
    outline: 2px solid var(--bb-accent, #16a34a);
    outline-offset: 2px;
}

.cited-source-citation:active {
    background-color: rgba(22, 163, 74, 0.2);
}

/* Mobile touch feedback */
.cited-source-citation.touch-active {
    background-color: rgba(22, 163, 74, 0.2);
    transform: scale(1.02);
}

/* ==============================================
   VIDEO CITATION INLINE STYLING
   ============================================== */

.video-citation {
    color: var(--bb-video-accent, #d97706);
    font-weight: 500;
    cursor: pointer;
    padding: 0.1em 0.2em;
    margin: 0 0.05em;
    border-radius: 3px;
    transition: background-color 0.15s ease, color 0.15s ease;
    text-decoration: none;
    border-bottom: 1px dotted var(--bb-video-accent, #d97706);
    -webkit-user-select: text;
    -moz-user-select: text;
    user-select: text;
}

.video-citation:hover {
    background-color: rgba(217, 119, 6, 0.1);
    border-bottom-style: solid;
}

.video-citation:focus {
    outline: 2px solid var(--bb-video-accent, #d97706);
    outline-offset: 2px;
}

.video-citation:active {
    background-color: rgba(217, 119, 6, 0.2);
}

/* ==============================================
   VIDEO SOURCES FOOTER
   ============================================== */

.node-video-citation-footer {
    margin-top: 1rem;
    padding: 0.75rem;
    border-radius: 8px;
    background: rgba(217, 119, 6, 0.04);
    border: 1px solid rgba(217, 119, 6, 0.12);
}

.video-footer-header .citation-footer-icon {
    font-size: 1rem;
}

.video-footer-header .citation-footer-title {
    color: var(--bb-video-accent, #d97706);
}

/* Video badge in citation footer */
.citation-badge.video {
    background-color: rgba(217, 119, 6, 0.15);
    color: var(--bb-video-accent, #d97706);
}

/* Video citation footer item hover */
.citation-footer-item.citation-video-item:hover {
    border-left-color: var(--bb-video-accent, #d97706);
}

/* Dark mode */
[data-theme="dark"] .video-citation {
    color: #fbbf24;
    border-bottom-color: #fbbf24;
}

[data-theme="dark"] .video-citation:hover {
    background-color: rgba(251, 191, 36, 0.15);
}

[data-theme="dark"] .citation-badge.video {
    background-color: rgba(251, 191, 36, 0.2);
    color: #fbbf24;
}

[data-theme="dark"] .node-video-citation-footer {
    background: rgba(251, 191, 36, 0.06);
    border-color: rgba(251, 191, 36, 0.15);
}

[data-theme="dark"] .video-footer-header .citation-footer-title {
    color: #fbbf24;
}

/* ==============================================
   TOOLTIP STYLING
   ============================================== */

.cited-source-tooltip {
    position: fixed;
    max-width: 380px;
    min-width: 240px;
    padding: 0;
    background: var(--bb-surface, #ffffff);
    border: 1px solid var(--bb-border, #e5e7eb);
    border-radius: 10px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 4px 10px rgba(0, 0, 0, 0.08);
    z-index: 1500;
    opacity: 0;
    visibility: hidden;
    transform: translateY(4px);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
    pointer-events: none;
    overflow: hidden;
}

.cited-source-tooltip.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    pointer-events: auto;
}

/* Tooltip header */
.cited-source-tooltip .tooltip-header {
    padding: 0.75rem 1rem;
    background: var(--bb-bg-secondary, #f9fafb);
    border-bottom: 1px solid var(--bb-border-light, #f3f4f6);
}

.cited-source-tooltip .source-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--bb-text, #1f2937);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Tooltip meta info */
.cited-source-tooltip .tooltip-meta {
    padding: 0.5rem 1rem;
    font-size: 0.75rem;
    color: var(--bb-text-secondary, #6b7280);
    border-bottom: 1px solid var(--bb-border-light, #f3f4f6);
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.5rem;
}

.cited-source-tooltip .source-org,
.cited-source-tooltip .source-date {
    white-space: nowrap;
}

/* Tooltip content area */
.cited-source-tooltip .tooltip-content {
    padding: 0.75rem 1rem;
    max-height: 150px;
    overflow-y: auto;
}

.cited-source-tooltip .tooltip-preview {
    font-size: 0.8125rem;
    line-height: 1.55;
    color: var(--bb-text, #1f2937);
}

.cited-source-tooltip .tooltip-error {
    font-size: 0.875rem;
    color: var(--bb-error, #dc2626);
    font-style: italic;
}

/* Loading state */
.cited-source-tooltip .tooltip-loading {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--bb-text-secondary, #6b7280);
    font-size: 0.875rem;
}

.cited-source-tooltip .tooltip-loading .spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--bb-border, #e5e7eb);
    border-top-color: var(--bb-accent, #16a34a);
    border-radius: 50%;
    animation: cited-source-spin 0.6s linear infinite;
}

@keyframes cited-source-spin {
    to { transform: rotate(360deg); }
}

/* Hint text at bottom */
.cited-source-tooltip .tooltip-hint {
    padding: 0.5rem 1rem;
    font-size: 0.6875rem;
    color: var(--bb-text-tertiary, #9ca3af);
    text-align: center;
    border-top: 1px solid var(--bb-border-light, #f3f4f6);
}

/* Arrow pointer */
.cited-source-tooltip::before {
    content: '';
    position: absolute;
    width: 12px;
    height: 12px;
    background: var(--bb-surface, #ffffff);
    border: 1px solid var(--bb-border, #e5e7eb);
    border-right: none;
    border-bottom: none;
}

.cited-source-tooltip.above::before {
    bottom: -7px;
    left: 50%;
    margin-left: -6px;
    transform: rotate(-135deg);
}

.cited-source-tooltip.below::before {
    top: -7px;
    left: 50%;
    margin-left: -6px;
    transform: rotate(45deg);
    background: var(--bb-bg-secondary, #f9fafb);
}

/* ==============================================
   MODAL STYLING
   ============================================== */

.cited-source-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.cited-source-modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}

.cited-source-modal-container {
    position: relative;
    background: var(--bb-surface, #ffffff);
    border-radius: 12px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    max-width: 900px;
    width: 100%;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Modal header */
.cited-source-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    padding: 1.25rem 1.5rem;
    background: var(--bb-bg-secondary, #f9fafb);
    border-bottom: 1px solid var(--bb-border, #e5e7eb);
}

.cited-source-modal-header .source-info {
    flex: 1;
    min-width: 0;
}

.cited-source-modal-header .source-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--bb-text, #1f2937);
    line-height: 1.4;
    margin: 0 0 0.5rem 0;
}

.cited-source-modal-header .source-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
    font-size: 0.8125rem;
    color: var(--bb-text-secondary, #6b7280);
}

.cited-source-modal-header .source-authors {
    font-weight: 500;
}

.cited-source-modal-close {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--bb-text-secondary, #6b7280);
    transition: background-color 0.15s ease, color 0.15s ease;
}

.cited-source-modal-close:hover {
    background: var(--bb-bg-hover, #e5e7eb);
    color: var(--bb-text, #1f2937);
}

.cited-source-modal-close:focus {
    outline: 2px solid var(--bb-accent, #16a34a);
    outline-offset: 2px;
}

/* Modal body */
.cited-source-modal-body {
    flex: 1;
    min-height: 400px;
    overflow: hidden;
    position: relative;
}

.cited-source-modal-body .source-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem;
    gap: 1rem;
    color: var(--bb-text-secondary, #6b7280);
}

.cited-source-modal-body .source-loading .spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--bb-border, #e5e7eb);
    border-top-color: var(--bb-accent, #16a34a);
    border-radius: 50%;
    animation: cited-source-spin 0.8s linear infinite;
}

/* Iframe container */
.source-iframe-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 400px;
}

.source-iframe {
    width: 100%;
    height: 100%;
    border: none;
}

/* Text content container */
.source-text-container {
    padding: 1.5rem;
    overflow-y: auto;
    max-height: 60vh;
}

.source-text-content {
    font-size: 0.9375rem;
    line-height: 1.7;
    color: var(--bb-text, #1f2937);
}

.source-text-content p {
    margin: 0 0 1rem 0;
}

.source-text-content p:last-child {
    margin-bottom: 0;
}

/* Error state */
.source-error {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3rem;
    color: var(--bb-text-secondary, #6b7280);
    font-style: italic;
}

/* Modal footer */
.cited-source-modal-footer {
    display: flex;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    background: var(--bb-bg-secondary, #f9fafb);
    border-top: 1px solid var(--bb-border, #e5e7eb);
}

.source-open-external,
.source-copy-citation {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
    text-decoration: none;
}

.source-open-external {
    background: var(--bb-accent, #16a34a);
    color: white;
    border: none;
}

.source-open-external:hover {
    background: var(--bb-accent-hover, #15803d);
}

.source-copy-citation {
    background: var(--bb-surface, #ffffff);
    color: var(--bb-text, #1f2937);
    border: 1px solid var(--bb-border, #e5e7eb);
}

.source-copy-citation:hover {
    background: var(--bb-bg-hover, #f3f4f6);
}

.source-copy-citation.copied {
    background: var(--bb-success-light, #dcfce7);
    border-color: var(--bb-success, #22c55e);
    color: var(--bb-success, #22c55e);
}

/* ==============================================
   DARK MODE
   ============================================== */

[data-theme="dark"] .cited-source-citation {
    color: var(--bb-accent, #4ade80);
    border-bottom-color: var(--bb-accent, #4ade80);
}

[data-theme="dark"] .cited-source-citation:hover {
    background-color: rgba(74, 222, 128, 0.15);
}

[data-theme="dark"] .cited-source-tooltip {
    background: var(--bb-surface, #2d2a26);
    border-color: var(--bb-border, #4a4238);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .cited-source-tooltip .tooltip-header {
    background: var(--bb-bg-secondary, #1f1d1a);
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .cited-source-tooltip .source-title,
[data-theme="dark"] .cited-source-tooltip .tooltip-preview {
    color: var(--bb-text, #f5f3ef);
}

[data-theme="dark"] .cited-source-tooltip .tooltip-meta {
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .cited-source-tooltip .tooltip-hint {
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .cited-source-tooltip::before {
    background: var(--bb-surface, #2d2a26);
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .cited-source-tooltip.below::before {
    background: var(--bb-bg-secondary, #1f1d1a);
}

[data-theme="dark"] .cited-source-modal-overlay {
    background: rgba(0, 0, 0, 0.75);
}

[data-theme="dark"] .cited-source-modal-container {
    background: var(--bb-surface, #2d2a26);
}

[data-theme="dark"] .cited-source-modal-header {
    background: var(--bb-bg-secondary, #1f1d1a);
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .cited-source-modal-header .source-title {
    color: var(--bb-text, #f5f3ef);
}

[data-theme="dark"] .cited-source-modal-close:hover {
    background: var(--bb-bg-hover, #3d3a35);
}

[data-theme="dark"] .source-text-content {
    color: var(--bb-text, #f5f3ef);
}

[data-theme="dark"] .cited-source-modal-footer {
    background: var(--bb-bg-secondary, #1f1d1a);
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .source-copy-citation {
    background: var(--bb-surface, #2d2a26);
    color: var(--bb-text, #f5f3ef);
    border-color: var(--bb-border, #4a4238);
}

[data-theme="dark"] .source-copy-citation:hover {
    background: var(--bb-bg-hover, #3d3a35);
}

/* ==============================================
   SEPIA MODE
   ============================================== */

[data-theme="sepia"] .cited-source-citation {
    color: #92400e;
    border-bottom-color: #92400e;
}

[data-theme="sepia"] .cited-source-citation:hover {
    background-color: rgba(146, 64, 14, 0.1);
}

[data-theme="sepia"] .cited-source-tooltip {
    background: #faf8f3;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-tooltip .tooltip-header {
    background: #f5f0e6;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-tooltip .source-title,
[data-theme="sepia"] .cited-source-tooltip .tooltip-preview {
    color: #5c4a2a;
}

[data-theme="sepia"] .cited-source-tooltip .tooltip-meta {
    color: #7a6545;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-tooltip .tooltip-hint {
    color: #9a8a6a;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-tooltip::before {
    background: #faf8f3;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-tooltip.below::before {
    background: #f5f0e6;
}

[data-theme="sepia"] .cited-source-modal-container {
    background: #faf8f3;
}

[data-theme="sepia"] .cited-source-modal-header {
    background: #f5f0e6;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .cited-source-modal-header .source-title {
    color: #5c4a2a;
}

[data-theme="sepia"] .source-text-content {
    color: #5c4a2a;
}

[data-theme="sepia"] .cited-source-modal-footer {
    background: #f5f0e6;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .source-open-external {
    background: #92400e;
}

[data-theme="sepia"] .source-open-external:hover {
    background: #78350f;
}

/* ==============================================
   MOBILE RESPONSIVENESS
   ============================================== */

@media (max-width: 640px) {
    .cited-source-tooltip {
        max-width: calc(100vw - 32px);
        left: 16px !important;
        right: 16px !important;
    }

    .cited-source-tooltip::before {
        display: none;
    }

    .cited-source-modal {
        padding: 0;
    }

    .cited-source-modal-container {
        max-width: 100%;
        max-height: 100vh;
        border-radius: 0;
    }

    .cited-source-modal-header {
        padding: 1rem;
    }

    .cited-source-modal-header .source-title {
        font-size: 1rem;
    }

    .cited-source-modal-footer {
        flex-direction: column;
        padding: 1rem;
    }

    .source-open-external,
    .source-copy-citation {
        width: 100%;
        justify-content: center;
    }

    .source-text-container {
        padding: 1rem;
        max-height: calc(100vh - 200px);
    }
}

/* ==============================================
   NODE CITATION FOOTER
   Compact, subtle footer showing sources referenced
   ============================================== */

.node-citation-footer {
    margin-top: 1.5rem;
    padding: 0.5rem 0.75rem;
    background: transparent;
    border-radius: 6px;
    border-left: 2px solid var(--bb-border-light, #e0e0dc);
    /* Ensure Further Reading section is always last */
    order: 9999;
}

/* Ensure node-content is flex so order property works */
.node-content {
    display: flex;
    flex-direction: column;
}

.citation-footer-header {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-bottom: 0.35rem;
    font-weight: 500;
    font-size: 0.7rem;
    color: var(--bb-text-muted, #888);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.citation-footer-icon {
    font-size: 0.8rem;
    opacity: 0.7;
}

.citation-footer-title {
    flex-grow: 1;
}

.citation-footer-count {
    font-weight: 400;
    font-size: 0.7rem;
    color: var(--bb-text-muted, #999);
}

.citation-footer-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.citation-footer-item {
    display: inline-flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0.3rem 0.5rem;
    background: var(--bb-surface-alt, #f7f7f5);
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s, box-shadow 0.15s;
    border: 1px solid var(--bb-border-light, #eee);
    max-width: 280px;
}

.citation-footer-item:hover {
    background: var(--bb-surface-hover, #eeeee8);
    border-color: var(--bb-accent-light, #86efac);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.citation-footer-item:focus {
    outline: 2px solid var(--bb-accent, #16a34a);
    outline-offset: 1px;
}

.citation-item-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.35rem;
}

.citation-source-title {
    font-weight: 500;
    color: var(--bb-text, #333);
    font-size: 0.75rem;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.citation-author {
    font-weight: 500;
    color: var(--bb-text-secondary, #666);
    font-size: 0.7rem;
}

.citation-badge {
    display: none; /* Hide badges for cleaner look */
}

/* Only show badge on hover */
.citation-footer-item:hover .citation-badge {
    display: inline-block;
    padding: 0.1em 0.35em;
    border-radius: 3px;
    font-size: 0.55em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    flex-shrink: 0;
}

.citation-badge.primary {
    background: var(--bb-accent, #16a34a);
    color: white;
}

.citation-badge.curated {
    background: var(--bb-info, #3b82f6);
    color: white;
}

.citation-badge.cited {
    background: var(--bb-text-muted, #999);
    color: white;
}

.citation-title {
    display: none; /* Hide separate title line - using source-title instead */
}

.citation-excerpt {
    display: none; /* Hide excerpt for compact view */
}

/* Dark mode */
[data-theme="dark"] .node-citation-footer {
    background: transparent;
    border-color: var(--bb-border, #444);
}

[data-theme="dark"] .citation-footer-item {
    background: var(--bb-surface-alt, #2a2825);
    border-color: var(--bb-border, #444);
}

[data-theme="dark"] .citation-footer-item:hover {
    background: var(--bb-surface-hover, #3a3835);
    border-color: var(--bb-accent, #4ade80);
}

[data-theme="dark"] .citation-source-title {
    color: var(--bb-text, #e5e5e0);
}

[data-theme="dark"] .citation-author {
    color: var(--bb-text-secondary, #aaa);
}

[data-theme="dark"] .citation-badge.cited {
    background: var(--bb-text-secondary, #777);
}

/* Sepia mode */
[data-theme="sepia"] .node-citation-footer {
    border-color: #d4c9b0;
}

[data-theme="sepia"] .citation-footer-item {
    background: #f5f0e6;
    border-color: #d4c9b0;
}

[data-theme="sepia"] .citation-footer-item:hover {
    background: #ebe5d8;
    border-color: #92400e;
}

[data-theme="sepia"] .citation-source-title {
    color: #5c4a2a;
}

[data-theme="sepia"] .citation-author {
    color: #7a6545;
}

/* Mobile responsive */
@media (max-width: 640px) {
    .node-citation-footer {
        margin-top: 0.75rem;
        padding: 0.4rem 0.5rem;
    }

    .citation-footer-header {
        font-size: 0.65rem;
    }

    .citation-footer-list {
        gap: 0.25rem;
    }

    .citation-footer-item {
        padding: 0.25rem 0.4rem;
        max-width: 200px;
    }

    .citation-source-title {
        font-size: 0.7rem;
    }

    .citation-author {
        font-size: 0.65rem;
    }
}
