/* =========================
   Comment bubble
   Placed at its comment's line (top: y%); several close together share one,
   with a count. Opening it turns the bubble into the thread.
   ========================= */

.comment-bubble {
    position: absolute;
    left: 0;
    z-index: 2;
    width: var(--bubble);
    height: var(--bubble);
    margin-top: calc(var(--bubble) / -2);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 240ms var(--ease-out);
    animation: bubble-in 520ms var(--ease-spring) both;
}

@keyframes bubble-in {
    from { opacity: 0; transform: scale(0.6); }
}

.comment-bubble.calm {
    animation: none;
}

/* Bubbles arrive when their page scrolls into view (comments.js marks it revealed) */
.comment-margin:not(.revealed) .comment-bubble,
.comment-dots:not(.revealed) .comment-dot {
    opacity: 0 !important;
    animation: none !important;
}

.bubble-icon {
    position: absolute;
    left: 0;
    top: 0;
    width: var(--bubble);
    height: var(--bubble);
    background: url(../assets/icons/cmt_bubble_single.svg) center / contain no-repeat;
    transition:
        transform 420ms var(--ease-spring),
        opacity 240ms var(--ease-out),
        filter 240ms var(--ease-out);
}

.bubble-count {
    position: absolute;
    /* Centre of the speech-bubble body */
    top: calc(var(--bubble) * 0.45);
    left: calc(var(--bubble) * 0.51);
    transform: translate(-50%, -50%);
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
    transition: transform 420ms var(--ease-spring), opacity 200ms;
}

.bubble-underline {
    position: absolute;
    left: 0;
    top: calc(var(--bubble) * 0.83);
    width: var(--bubble);
    height: 15px;
    opacity: 0.85;
    transition: opacity 240ms var(--ease-out), transform 420ms var(--ease-spring);
}

/* Hover (or the pointer on its line): lift a touch */
.comment-bubble:not(.open):hover .bubble-icon,
.comment-bubble.is-preview .bubble-icon {
    transform: translateY(-2px) scale(1.1);
    filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.35));
}

.comment-bubble:not(.open):hover .bubble-count,
.comment-bubble.is-preview .bubble-count {
    transform: translate(-50%, -50%) translateY(-2px) scale(1.1);
}

.comment-bubble:not(.open):hover .bubble-underline,
.comment-bubble.is-preview .bubble-underline {
    opacity: 1;
    transform: scaleX(1.15);
}

/* Open: icon out, thread in */
.comment-bubble.open {
    z-index: 10;
    width: var(--thread-w, 300px);
    height: auto;
    cursor: default;
}

.comment-bubble.open .bubble-icon,
.comment-bubble.open .bubble-count,
.comment-bubble.open .bubble-underline {
    opacity: 0;
    transform: scale(0.6);
}

.comment-bubble.failed .bubble-count {
    color: var(--warn);
}

/* Other bubbles step back while one is open */
body.has-open .comment-bubble:not(.open) {
    opacity: 0.22;
}

/* =========================
   Thread
   ========================= */

.comment-thread {
    display: none;
    padding-bottom: 4px;
    background: #000;
}

.comment-bubble.open .comment-thread,
.comment-thread.in-sheet {
    display: block;
}

/* Lifts the first name so its underline lies on the line's highlight */
.comment-bubble.open .comment-thread {
    margin-top: -9px;
}

/* A long thread or form runs past its page's bottom and can cross the next
   page. From there down (--below, set by comments.js) its page side fades
   into the page, like the contents panel's edge. */
.comment-bubble.open .comment-thread {
    position: relative;
}

.comment-bubble.open .comment-thread::before,
.comment-composer:not(.in-sheet)::before {
    content: "";
    position: absolute;
    top: var(--below, 100%);
    bottom: 0;
    right: 100%;
    width: 48px;
    background: linear-gradient(to right, rgba(0, 0, 0, 0), #000);
    pointer-events: none;
}

.comment-item {
    padding: 8px 0;
}

.comment-item + .comment-item {
    border-top: 1px solid var(--rule);
}

/* Each item drifts in and comes into focus, one after another */
.comment-thread:not(.instant) .comment-item {
    animation: item-in 520ms var(--ease-out) both;
    animation-delay: calc(80ms + min(var(--i), 8) * 45ms);
}

@keyframes item-in {
    from { opacity: 0; transform: translateX(14px); filter: blur(4px); }
    to   { opacity: 1; transform: none; filter: none; }
}

.comment-meta {
    display: table;
}

.comment-author {
    position: relative;
    z-index: 1;
    font-size: 1rem;
    font-weight: 700;
}

.comment-cls {
    margin-left: 6px;
    font-size: 0.85em;
    font-weight: 400;
    color: var(--ink-3);
}

/* In the section's colour, drawn across the name after the item appears */
.author-underline {
    height: var(--line-glow);
    margin-top: -10px;
    transform-origin: left center;
}

.comment-thread:not(.instant) .author-underline {
    animation: underline-in 560ms var(--ease-out) both;
    animation-delay: calc(180ms + min(var(--i), 8) * 45ms);
}

@keyframes underline-in {
    from { transform: scaleX(0); }
}

.comment-text {
    margin-top: 2px;
    padding-left: calc(var(--bubble) + 20px);
    font-size: 1rem;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.comment-time {
    margin-top: 8px;
    padding-left: calc(var(--bubble) + 20px);
    font-size: 0.8rem;
    color: var(--ink-3);
}

.comment-time button {
    margin-left: 10px;
    padding: 0;
    color: #fff;
    text-decoration: underline;
    text-underline-offset: 3px;
}

.comment-item.pending .author-underline {
    animation: pending-pulse 1s ease-in-out infinite;
}

@keyframes pending-pulse {
    50% { opacity: 0.3; }
}

.comment-item.failed .comment-time {
    color: var(--warn);
}

.comment-item.fresh .comment-text {
    animation: fresh-flash 1600ms ease-out both;
}

@keyframes fresh-flash {
    from { background: rgba(var(--rgb), 0.35); }
    to   { background: transparent; }
}

/* =========================
   Comment form, in the margin at its line
   ========================= */

.comment-composer {
    position: absolute;
    left: 0;
    z-index: 12;
    width: var(--thread-w, 300px);
    /* Same lift as an open thread: the name's underline lies on the line */
    margin-top: calc(var(--bubble) / -2 - 9px);
    padding: 8px 0 6px;
    background: #000;
    animation: item-in 420ms var(--ease-out) both;
}

.comment-composer.in-sheet {
    position: static;
    width: auto;
    margin: 0;
    padding-top: 0;
    animation: none;
}

.composer-who {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.composer-who input {
    min-width: 0;
    padding: 0;
    border: 0;
    outline: none;
    background: none;
    color: #fff;
    font: inherit;
    font-size: 1rem;
}

#composer-name {
    font-weight: 700;
}

#composer-cls {
    width: 11ch;
    font-size: 0.85em;
    color: var(--ink-2);
}

.composer-who input::placeholder,
#composer-text::placeholder {
    color: var(--ink-3);
}

/* Follows the name's width, in the section's colour */
.composer-underline {
    width: 90px;
    height: var(--line-glow);
    margin-top: -10px;
    opacity: 0.45;
    transition: width 160ms var(--ease-out), opacity 160ms;
}

.comment-composer:has(#composer-name:focus) .composer-underline {
    opacity: 1;
}

#composer-text {
    display: block;
    width: calc(100% - var(--bubble) - 20px);
    min-height: 52px;
    margin: 6px 0 0 calc(var(--bubble) + 20px);
    padding: 2px 0 6px;
    border: 0;
    outline: none;
    resize: none;
    background: transparent;
    color: #fff;
    font: inherit;
    font-size: 1rem;
    line-height: 1.45;
}

.composer-msg {
    margin: 6px 0 0 calc(var(--bubble) + 20px);
    font-size: 0.8rem;
    color: var(--warn);
}

.composer-msg:empty {
    display: none;
}

.composer-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin: 10px 0 0 calc(var(--bubble) + 20px);
    font-size: 0.78rem;
    color: var(--ink-3);
}

.composer-actions {
    display: flex;
    gap: 4px;
    margin-left: auto;
}

.composer-actions button {
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.9rem;
}

.composer-send {
    position: relative;
    font-weight: 600;
}

/* Hover: the name's underline, drawn across the word */
.composer-send::after {
    content: "";
    position: absolute;
    left: 10px;
    right: 10px;
    bottom: -2px;
    height: var(--line-glow);
    background: linear-gradient(
        to bottom,
        rgba(var(--rgb), 0) 0%,
        rgba(var(--rgb), 0.5) 25%,
        rgba(var(--rgb), 1) 50%,
        rgba(var(--rgb), 0.5) 75%,
        rgba(var(--rgb), 0) 100%
    );
    transform: scaleX(0);
    transform-origin: left center;
    transition: transform 240ms var(--ease-out);
    pointer-events: none;
}

.composer-send:hover::after {
    transform: scaleX(1);
}

.margin-l .composer-send::after {
    transform-origin: right center;
}

.composer-cancel {
    color: var(--ink-2);
}

.composer-cancel:hover {
    color: #fff;
}

/* =========================
   Left page of a spread: everything mirrors, anchored to the page side
   of its outer margin, right-aligned and opening outward
   ========================= */

.margin-l .comment-bubble.open .comment-thread::before,
.margin-l .comment-composer::before {
    right: auto;
    left: 100%;
    background: linear-gradient(to left, rgba(0, 0, 0, 0), #000);
}

.margin-l .comment-bubble,
.margin-l .comment-composer {
    left: auto;
    right: 0;
    text-align: right;
}

.margin-l .comment-meta {
    margin-left: auto;
}

.margin-l .author-underline {
    transform-origin: right center;
}

.margin-l .comment-text,
.margin-l .comment-time {
    padding-left: 0;
    padding-right: calc(var(--bubble) + 20px);
}

.margin-l .composer-who {
    flex-direction: row-reverse;
}

.margin-l .composer-who input {
    text-align: right;
}

.margin-l .composer-underline {
    margin-left: auto;
}

.margin-l #composer-text,
.margin-l .composer-msg {
    margin-left: auto;
    margin-right: calc(var(--bubble) + 20px);
}

.margin-l .composer-msg {
    width: calc(100% - var(--bubble) - 20px);
}

.margin-l .composer-foot {
    flex-direction: row-reverse;
    margin: 10px calc(var(--bubble) + 20px) 0 0;
}

.margin-l .composer-actions {
    margin-left: 0;
    margin-right: auto;
}

.margin-l .comment-thread:not(.instant) .comment-item,
.margin-l .comment-composer {
    animation-name: item-in-left;
}

@keyframes item-in-left {
    from { opacity: 0; transform: translateX(-14px); filter: blur(4px); }
    to   { opacity: 1; transform: none; filter: none; }
}

/* Hidden while pages glide between layouts, then fade back */
body.regrouping .comment-bubble {
    opacity: 0 !important;
    transition: none;
}

body.cmts-off .comment-bubble,
body.cmts-off .comment-dots {
    display: none !important;
}

/* =========================
   Phones: dots on the page edge, threads in a bottom sheet
   ========================= */

.comment-dots {
    display: none;
}

.comment-dot {
    position: absolute;
    right: 8px;
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    margin-top: -16px;
    padding: 0;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.86);
    box-shadow: 0 0 0 2px rgba(var(--rgb), 0.95), 0 2px 10px rgba(0, 0, 0, 0.35);
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    pointer-events: auto;
    -webkit-tap-highlight-color: transparent;
    animation: bubble-in 520ms var(--ease-spring) both;
}

.dot-icon {
    width: 17px;
    height: 17px;
    background: url(../assets/icons/cmt_bubble_single.svg) center / contain no-repeat;
}

body.has-open .comment-dot {
    opacity: 0.35;
}

.comment-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 80;
    max-height: 64vh;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 6px 18px calc(18px + env(safe-area-inset-bottom, 0px));
    border-radius: 16px 16px 0 0;
    background: var(--sheet);
    box-shadow: 0 -14px 40px rgba(0, 0, 0, 0.6);
    transform: translateY(105%);
    transition: transform 360ms var(--ease-out);
}

.comment-sheet.on {
    transform: none;
}

.sheet-grip {
    width: 38px;
    height: 4px;
    margin: 4px auto 6px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.3);
}

.sheet-bar {
    height: 14px;
    margin: 0 -18px 8px;
}

.sheet-head {
    margin-bottom: 4px;
    font-size: 11.5px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--ink-3);
}

.sheet-scrim {
    position: fixed;
    inset: 0;
    z-index: 70;
    background: rgba(0, 0, 0, 0.3);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.sheet-scrim.on {
    opacity: 1;
    pointer-events: auto;
}

@media (max-width: 760px) {
    .comment-dots {
        display: block;
        position: absolute;
        inset: 0;
        z-index: 4;
        pointer-events: none;
    }

    .composer-keys {
        display: none;
    }
}
