/* =========================================================
   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;
    }
}
