/* =========================================================
   Starlab Comment Editor Enhancements
   Shared styles for emoji/sticker/GIF/image/@mention tools
   ========================================================= */

.starlab-comment-editor {
    position: relative;
}

.starlab-comment-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    background: var(--cmt-toolbar-bg, #f8f9fb);
    border: 1px solid var(--cmt-border, #e2e5ea);
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    position: relative;
    z-index: 2;
}

.starlab-comment-toolbar .cmt-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    border: 1px solid transparent;
    background: transparent;
    border-radius: 6px;
    color: var(--cmt-tool-color, #555);
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.starlab-comment-toolbar .cmt-tool:hover {
    background: var(--cmt-tool-hover, #eef0f4);
    color: var(--cmt-tool-hover-color, #111);
}

.starlab-comment-toolbar .cmt-tool.active {
    background: var(--cmt-primary, #2b6cb0);
    color: #fff;
    border-color: var(--cmt-primary, #2b6cb0);
}

.starlab-comment-toolbar .cmt-tool.loading {
    opacity: 0.6;
    cursor: progress;
}

/* Locked toolbar button — rendered but gated by level / role. */
.starlab-comment-toolbar .cmt-tool.locked {
    position: relative;
    opacity: 0.55;
    cursor: not-allowed;
    filter: grayscale(0.4);
}
.starlab-comment-toolbar .cmt-tool.locked:hover {
    background: transparent;
    opacity: 0.75;
}
.starlab-comment-toolbar .cmt-tool .cmt-lock-badge {
    position: absolute;
    right: -3px;
    bottom: -3px;
    width: 15px;
    height: 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    line-height: 1;
    color: #fff;
    background: #6b7280;
    border: 1.5px solid var(--cmt-toolbar-bg, #f8f9fb);
    border-radius: 50%;
    pointer-events: none;
}
.starlab-comment-toolbar .cmt-tool .cmt-lock-badge i {
    font-size: 8px;
    line-height: 1;
}

.cmt-tool-label {
    font-weight: 700;
    letter-spacing: 0.5px;
    font-size: 11px;
}

/* The textarea that follows the toolbar */
.starlab-comment-editor > textarea,
.starlab-comment-editor textarea[name="comment"],
.starlab-comment-editor textarea[name="reply_comment"] {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

/* ----- Popovers ----- */

.cmt-tool-popover {
    position: absolute;
    top: 100%;
    left: 8px;
    margin-top: 4px;
    min-width: 280px;
    max-width: 360px;
    background: var(--cmt-popover-bg, #fff);
    border: 1px solid var(--cmt-border, #e2e5ea);
    border-radius: 10px;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.12);
    padding: 10px;
    z-index: 50;
    max-height: 340px;
    overflow-y: auto;
}

.cmt-popover-head {
    font-size: 12px;
    font-weight: 600;
    color: var(--cmt-muted, #6b7280);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-bottom: 8px;
}

.cmt-popover-loading,
.cmt-popover-empty {
    padding: 16px 8px;
    font-size: 13px;
    text-align: center;
    color: var(--cmt-muted, #6b7280);
}

/* Emoji grid */
.cmt-emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 4px;
}

.cmt-emoji-item {
    height: 30px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 6px;
    font-size: 18px;
    line-height: 1;
    transition: background 0.15s ease;
}

.cmt-emoji-item:hover {
    background: var(--cmt-tool-hover, #eef0f4);
}

/* Sticker / GIF grids */
.cmt-sticker-grid,
.cmt-gif-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.cmt-sticker-item,
.cmt-gif-item {
    aspect-ratio: 1 / 1;
    overflow: hidden;
    border: 1px solid var(--cmt-border, #e2e5ea);
    background: #fafafa;
    border-radius: 8px;
    cursor: pointer;
    padding: 0;
    transition: transform 0.15s ease, border-color 0.15s ease;
}

.cmt-sticker-item:hover,
.cmt-gif-item:hover {
    transform: translateY(-1px);
    border-color: var(--cmt-primary, #2b6cb0);
}

.cmt-sticker-item img,
.cmt-gif-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Locked sticker — keeps the thumbnail visible (so the user sees what they're
   working toward) but desaturates it and overlays a lock badge. */
.cmt-sticker-item.locked {
    cursor: not-allowed;
    position: relative;
}
.cmt-sticker-item.locked img {
    filter: grayscale(0.85) brightness(0.55);
    opacity: 0.85;
}
.cmt-sticker-item.locked:hover {
    transform: none;
    border-color: var(--cmt-border, #e2e5ea);
}
.cmt-sticker-lock {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
    pointer-events: none;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0) 35%, rgba(0, 0, 0, 0.55) 100%);
}
.cmt-sticker-lock-icon {
    font-size: 18px;
    line-height: 1;
}
.cmt-sticker-lock-level {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.4px;
    padding: 1px 6px;
    background: rgba(0, 0, 0, 0.65);
    border-radius: 6px;
}

/* Floating lock-reason tooltip (appended to <body> by JS). */
.cmt-lock-tooltip {
    position: absolute;
    z-index: 10002;
    max-width: 260px;
    padding: 8px 12px;
    font-size: 12px;
    line-height: 1.4;
    color: #fff;
    background: rgba(20, 22, 28, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.18s ease, transform 0.18s ease;
    pointer-events: none;
}
.cmt-lock-tooltip.visible {
    opacity: 1;
    transform: translateY(0);
}

/* GIF tabs + search */
.cmt-gif-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 8px;
    border-bottom: 1px solid var(--cmt-border, #e2e5ea);
    padding-bottom: 6px;
}

.cmt-gif-tab {
    flex: 1;
    height: 28px;
    border: none;
    background: transparent;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    color: var(--cmt-muted, #6b7280);
}

.cmt-gif-tab.active {
    background: var(--cmt-primary, #2b6cb0);
    color: #fff;
}

.cmt-gif-search {
    width: 100%;
    height: 32px;
    padding: 0 10px;
    border: 1px solid var(--cmt-border, #e2e5ea);
    border-radius: 6px;
    margin-bottom: 8px;
    font-size: 13px;
}

.cmt-gif-search:focus {
    outline: none;
    border-color: var(--cmt-primary, #2b6cb0);
}

/* ----- Mention autocomplete ----- */

.cmt-mention-popover {
    position: absolute;
    bottom: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 220px;
    overflow-y: auto;
    background: var(--cmt-popover-bg, #fff);
    border: 1px solid var(--cmt-border, #e2e5ea);
    border-radius: 10px;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.12);
    z-index: 55;
    padding: 4px;
}

.cmt-mention-item {
    display: flex;
    align-items: center;
    width: 100%;
    gap: 10px;
    padding: 6px 8px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    text-align: left;
    font-size: 13px;
}

.cmt-mention-item:hover,
.cmt-mention-item.active {
    background: var(--cmt-tool-hover, #eef0f4);
}

.cmt-mention-avatar img,
.cmt-mention-avatar .avatar-fallback {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: block;
}

.cmt-mention-avatar .avatar-fallback {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--cmt-primary, #2b6cb0);
    color: #fff;
    font-weight: 700;
    font-size: 12px;
}

.cmt-mention-name {
    font-weight: 500;
    color: var(--cmt-tool-hover-color, #111);
}

/* ----- Rendered comment content ----- */

.comment-mention {
    display: inline-block;
    color: var(--color-primary, #2b6cb0);
    background: color-mix(in srgb, var(--color-primary, #2b6cb0) 12%, transparent);
    padding: 1px 6px;
    border-radius: 4px;
    font-weight: 600;
    text-decoration: none;
}

/* Fallback for browsers without color-mix support */
@supports not (background: color-mix(in srgb, red, blue)) {
    .comment-mention {
        background: rgba(99, 102, 241, 0.12);
    }
}

.comment-mention:hover {
    background: color-mix(in srgb, var(--color-primary, #2b6cb0) 20%, transparent);
}

.comment-text .comment-media {
    display: block;
    max-width: 320px;
    max-height: 260px;
    width: auto;
    height: auto;
    margin: 6px 0;
    border-radius: 8px;
    border: 1px solid var(--cmt-border, #e2e5ea);
}

.comment-text .comment-media-sticker {
    max-width: 160px;
    max-height: 160px;
    border: none;
    background: transparent;
}

/* ----- Dark mode (inherits theme dark vars if present) ----- */

body.qi-dark-mode,
.qi-dark-mode {
    --cmt-toolbar-bg: #1f242b;
    --cmt-border: #2f3641;
    --cmt-tool-color: #c7cdd6;
    --cmt-tool-hover: #2a3039;
    --cmt-tool-hover-color: #fff;
    --cmt-primary: #5aa3ff;
    --cmt-popover-bg: #1b2027;
    --cmt-muted: #8a93a0;
}

body.qi-dark-mode .comment-mention {
    background: color-mix(in srgb, var(--color-primary, #5aa3ff) 22%, transparent);
}

/* ----- Editor media tray (chips for selected GIF/sticker/image) ----- */

.cmt-media-tray {
    display: none;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px;
    background: var(--cmt-toolbar-bg, #f8f9fb);
    border: 1px solid var(--cmt-border, #e2e5ea);
    border-top: none;
    border-bottom: none;
}

.cmt-media-tray.has-items {
    display: flex;
}

.cmt-media-chip {
    position: relative;
    width: 88px;
    height: 88px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--cmt-border, #e2e5ea);
    background: var(--cmt-popover-bg, #fff);
    flex: 0 0 auto;
}

.cmt-media-chip img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.cmt-media-chip .cmt-chip-badge {
    position: absolute;
    left: 4px;
    bottom: 4px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.3px;
    padding: 2px 6px;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.65);
    color: #fff;
    text-transform: uppercase;
}

.cmt-media-chip .cmt-chip-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.15s ease;
}

.cmt-media-chip .cmt-chip-remove:hover {
    background: #e74c3c;
}

/* When tray is visible, remove the textarea's top radius gap */
.starlab-comment-editor .cmt-media-tray.has-items + textarea,
.starlab-comment-editor .cmt-media-tray.has-items ~ textarea[name="comment"],
.starlab-comment-editor .cmt-media-tray.has-items ~ textarea[name="reply_comment"] {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

/* ----- Responsive ----- */

@media (max-width: 520px) {
    .cmt-emoji-grid { grid-template-columns: repeat(6, 1fr); }
    .cmt-sticker-grid,
    .cmt-gif-grid { grid-template-columns: repeat(3, 1fr); }
    .cmt-tool-popover {
        min-width: 240px;
        max-width: 90vw;
    }
}

/* =========================================================
   Comment Spoilers
   - Hidden by default with a heavy backdrop-blur over the text.
   - .revealed = clicked open (full content visible).
   ========================================================= */
.comment-spoiler {
    position: relative;
    display: inline;
    background: var(--cmt-spoiler-bg, rgba(120, 130, 145, 0.32));
    color: transparent;
    text-shadow: none;
    border-radius: 4px;
    padding: 0 2px;
    cursor: pointer;
    user-select: none;
    transition: background 160ms ease, color 160ms ease, filter 160ms ease;
    -webkit-user-select: none;
}

/* Inner blur that obscures the actual characters. */
.comment-spoiler .comment-spoiler-inner {
    filter: blur(6px);
    color: var(--cmt-spoiler-text, #c8cdd4);
    transition: filter 160ms ease, color 160ms ease;
    pointer-events: none;
}

.comment-spoiler:hover {
    background: var(--cmt-spoiler-bg-hover, rgba(120, 130, 145, 0.44));
}

.comment-spoiler:focus-visible {
    outline: 2px solid var(--color-primary, #3b82f6);
    outline-offset: 2px;
}

/* Revealed state — restore the text. */
.comment-spoiler.revealed {
    background: var(--cmt-spoiler-bg-revealed, rgba(120, 130, 145, 0.12));
    color: inherit;
    cursor: text;
    user-select: text;
    -webkit-user-select: text;
}
.comment-spoiler.revealed .comment-spoiler-inner {
    filter: none;
    color: inherit;
    pointer-events: auto;
}

/* Spoiler toolbar button — keep the same look as other tools. */
.starlab-comment-toolbar .cmt-tool-spoiler i {
    font-size: 14px;
}

/* =========================================================
   Show / Hide Replies Toggle
   ========================================================= */
.show-replies-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    margin: 4px 0 6px 52px; /* aligns under the comment text, after the avatar gutter */
    background: transparent;
    border: none;
    border-radius: 999px;
    color: var(--color-primary, #3b82f6);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 140ms ease, color 140ms ease;
    line-height: 1;
}

.show-replies-toggle:hover {
    background: var(--cmt-toggle-hover-bg, rgba(59, 130, 246, 0.10));
}

.show-replies-toggle .show-replies-icon {
    font-size: 11px;
    transition: transform 160ms ease;
}

.show-replies-toggle[aria-expanded="true"] .show-replies-icon {
    /* fa-chevron-up replaces fa-chevron-down via JS, but keep this rule
       as a graceful fallback if class swap is delayed by repaint. */
    transform: rotate(180deg);
}

.comment-children[hidden] {
    display: none !important;
}

/* On nested levels, drop the avatar offset so the toggle hugs the parent. */
.comment-reply > .show-replies-toggle,
.comment-children .show-replies-toggle {
    margin-left: 52px;
}

/* =========================================================
   Flat reply thread — @mention tag
   ========================================================= */

/* Flat children list: vertical guide line on the left so users can see at
   a glance which comment a reply belongs to (asurascans-style), while
   replies are still rendered flat (no nested toggles). */
.comment-children-flat {
    list-style: none;
    margin: 12px 0 0 20px !important;
    padding-left: 20px !important;
    border-left: 2px solid var(--color-border, rgba(255, 255, 255, 0.12)) !important;
}

/* Slightly more visible line in dark mode */
body.qi-dark-mode .comment-children-flat {
    border-left-color: rgba(255, 255, 255, 0.14) !important;
}

/* Replies inside the flat list shouldn't inherit reply indentation */
.comment-children-flat > .comment-item {
    margin-left: 0 !important;
}

/* Mobile: tighten the indent so threads still fit */
@media (max-width: 640px) {
    .comment-children-flat {
        margin-left: 12px !important;
        padding-left: 14px !important;
    }
}

/* @mention tag that appears before the reply text */
.comment-mention-tag {
    display: inline-block;
    color: var(--color-primary, #3b82f6);
    background: color-mix(in srgb, var(--color-primary, #3b82f6) 12%, transparent);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 0.85em;
    font-weight: 600;
    text-decoration: none;
    margin-right: 4px;
    transition: background 140ms ease;
}

.comment-mention-tag:hover {
    background: color-mix(in srgb, var(--color-primary, #3b82f6) 22%, transparent);
    text-decoration: none;
}

@supports not (background: color-mix(in srgb, red, blue)) {
    .comment-mention-tag {
        background: rgba(59, 130, 246, 0.12);
    }
}

body.qi-dark-mode .comment-mention-tag {
    background: color-mix(in srgb, var(--color-primary, #5aa3ff) 22%, transparent);
}

/* =========================================================
   Load More Comments button
   ========================================================= */
.load-more-comments-wrapper {
    text-align: center;
    margin: 24px 0 8px;
}

.load-more-comments-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 24px;
    background: var(--color-surface, #1e2530);
    color: var(--color-primary, #3b82f6);
    border: 1px solid var(--color-border, rgba(255,255,255,0.1));
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 160ms ease, border-color 160ms ease, color 160ms ease;
}

.load-more-comments-btn:hover:not(:disabled) {
    background: color-mix(in srgb, var(--color-primary, #3b82f6) 12%, var(--color-surface, #1e2530));
    border-color: var(--color-primary, #3b82f6);
}

.load-more-comments-btn:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

.load-more-comments-btn .load-more-remaining {
    font-size: 0.82em;
    font-weight: 400;
    opacity: 0.7;
}

/* =========================================================
   Notification deep-link highlight — asurascans style
   Solid background on entry, fades out over ~3s.
   ========================================================= */

@keyframes starlab-comment-highlight {
    0%   {
        background-color: rgba(88, 101, 242, 0.28);
        box-shadow: inset 0 0 0 1px rgba(88, 101, 242, 0.55);
    }
    40%  {
        background-color: rgba(88, 101, 242, 0.22);
        box-shadow: inset 0 0 0 1px rgba(88, 101, 242, 0.40);
    }
    100% {
        background-color: transparent;
        box-shadow: inset 0 0 0 1px transparent;
    }
}

.comment-item.comment-highlight-pulse {
    animation: starlab-comment-highlight 3s ease-out forwards;
    border-radius: 8px;
    /* Prevent layout shift */
    position: relative;
    z-index: 1;
}

/* Dark-mode variant — slightly lighter purple tint */
body.qi-dark-mode .comment-item.comment-highlight-pulse {
    animation-name: starlab-comment-highlight;
}
