/**
 * 📸💬 Cover Comments Modal Styles
 * Bottom sheet modal for viewing and interacting with cover post comments
 * Instagram-style behavior with smooth transitions
 */

/* Modal backdrop */
.cover-comments-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.cover-comments-backdrop.active {
    opacity: 1;
}

/* Modal container - Desktop fallback */
.cover-comments-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    width: calc(100% - 32px);
    max-width: 600px;
    max-height: 80vh;
    background: var(--bg-primary);
    border-radius: 16px;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.3s ease, transform 0.3s ease;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.cover-comments-modal.active {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
}

/* Modal content wrapper */
.cover-comments-modal__content {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Modal header */
.cover-comments-modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.cover-comments-modal__title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

/* 🔧 Hide close button on mobile bottom sheet */
.cover-comments-modal__close {
    display: none;
}

/* Modal body (scrollable area) */
.cover-comments-modal__body {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    -webkit-overflow-scrolling: touch;
}

/* Loading state */
.cover-comments-modal__loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
}

.cover-comments-modal__loading-more {
    text-align: center;
    padding: 12px;
    color: var(--text-secondary);
    font-size: 14px;
}

/* Empty state */
.cover-comments-modal__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
}

.cover-comments-modal__empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
    opacity: 0.5;
}

.cover-comments-modal__empty-text {
    color: var(--text-secondary);
    font-size: 14px;
    text-align: center;
}

/* Comments list */
.cover-comments-list {
    display: flex;
    flex-direction: column;
    gap: 0;
}

/* Comment thread */
.comment-thread {
    margin-bottom: 16px;
}

.comment-thread:last-child {
    margin-bottom: 0;
}

/* Comment replies container */
.comment-replies {
    margin-left: 40px;
    margin-top: 8px;
    border-left: 2px solid var(--border-color);
    padding-left: 12px;
}

/* Individual comment */
.comment {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 12px;
    padding: 12px 0;
    align-items: start;
}

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

.comment__avatar {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 50%;
    object-fit: cover;
    cursor: pointer;
    transition: opacity 0.2s ease;
    align-self: start;
}

.comment__avatar:hover {
    opacity: 0.8;
}

.comment__avatar--placeholder {
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.comment--reply .comment__avatar {
    width: 32px;
    height: 32px;
    min-width: 32px;
}

.comment__content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.comment__header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    flex-wrap: wrap;
}

.comment__name {
    font-weight: 600;
    font-size: 14px;
    color: var(--text-primary);
    cursor: pointer;
    transition: opacity 0.2s ease;
}

.comment__name:hover {
    opacity: 0.8;
}

.comment__time {
    font-size: 12px;
    color: var(--text-secondary);
}

/* 🔧 FIX: Comment body with text and like button on same line */
.comment__body {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 0; /* 🔧 FIX: Remove gap, action buttons appear immediately below text */
}

.comment__text {
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
    word-wrap: break-word;
    white-space: pre-wrap;
    flex: 1;
    min-width: 0;
}

.comment__mention {
    color: var(--primary-color);
    font-weight: 500;
}

/* 🔧 FIX: Action buttons positioned below text */
.comment__actions {
    display: inline-flex;
    align-items: baseline;
    gap: 12px;
    flex-wrap: wrap;
    margin-top: 4px; /* Small gap from text */
    margin-left: 0;
    padding-left: 0;
    padding-top: 0;
}

.comment__action-btn {
    background: none;
    border: none;
    padding: 4px 8px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 12px;
    transition: background 0.2s ease, color 0.2s ease, opacity 0.2s ease;
    display: inline-flex;
    align-items: baseline;
    gap: 4px;
    min-height: 32px; /* Smaller for tighter alignment */
}

.comment__action-btn:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.comment__action-btn:active {
    transform: scale(0.95);
}

/* 🔧 FIX: Like button positioned on same line as text, to the right */
.comment__like-btn {
    color: var(--text-secondary);
    opacity: 0.7;
    flex-shrink: 0;
    align-self: flex-start;
    /* Remove margin-left: auto since it's in a flex container with the text */
    padding: 0 4px;
    min-height: auto;
    min-width: auto;
}

.comment__like-btn.liked {
    color: var(--primary-color, #D2452C);
    opacity: 1;
}

.comment__like-count {
    font-size: 13px;
}

/* 🔧 FIX: Reply button positioned under text */
.comment__reply-btn {
    align-self: flex-start;
    margin-top: 0;
    padding: 4px 8px;
    min-height: 32px;
}

/* 🔧 FIX: Delete button positioned under text */
.comment__delete-btn {
    padding: 4px 8px;
    font-size: 13px;
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary);
    min-width: auto;
    min-height: 32px;
    margin-top: 0;
}

.comment__delete-btn:hover {
    background: rgba(244, 67, 54, 0.1);
    color: #f44336;
}


/* Replies toggle button (Instagram-style) */
.comment-replies-toggle {
    margin-left: 52px;
    margin-top: 8px;
}

.comment-replies-toggle__btn {
    background: none;
    border: none;
    padding: 4px 0;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
    font-weight: 500;
    min-height: 44px; /* 🔧 Touch target ≥44px */
}

.comment-replies-toggle__btn:hover,
.comment-replies-toggle__btn:active {
    color: var(--text-primary);
}

/* Load more button */
.cover-comments-modal__load-more {
    width: 100%;
    padding: 12px;
    margin-top: 16px;
    background: var(--bg-secondary);
    border: none;
    border-radius: 12px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s ease;
    min-height: 44px;
}

.cover-comments-modal__load-more:hover {
    background: var(--bg-tertiary);
}

.cover-comments-modal__load-more:active {
    transform: scale(0.98);
}

/* Reply form */
.cover-comments-modal__reply-form {
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
    flex-shrink: 0;
}

.reply-form {
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
}

.reply-form__cancel {
    position: absolute;
    top: -24px;
    left: 0;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 8px;
    transition: background 0.2s ease;
}

.reply-form__cancel:hover {
    background: var(--bg-secondary);
}

.reply-form__input {
    flex: 1;
    min-height: 44px;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    resize: none;
    transition: border-color 0.2s ease;
}

.reply-form__input:focus {
    outline: none;
    border-color: var(--accent-color, #007aff);
}

.reply-form__input::placeholder {
    color: var(--text-secondary);
}

.reply-form__submit {
    padding: 10px 20px;
    background: var(--primary-color, #D2452C);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s ease, transform 0.1s ease;
    min-height: 44px;
    min-width: 100px;
    flex-shrink: 0;
}

.reply-form__submit:hover {
    opacity: 0.9;
}

.reply-form__submit:active {
    transform: scale(0.97);
}

.reply-form__submit:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Responsive adjustments - Bottom Sheet on Mobile */
@media (max-width: 480px) {
    /* Instagram-style bottom sheet with three-position state machine */
    .cover-comments-modal {
        width: 100%;
        max-width: none;
        border-radius: 16px 16px 0 0;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        transform: translate3d(0, 100dvh, 0); /* Start closed with GPU acceleration */
        max-height: 96dvh;
        height: 96dvh; /* Always full height, use transform to position */
        transition: transform 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);
    }
    
    /* State-based positioning */
    .cover-comments-modal.sheet-state-closed {
        transform: translate3d(0, 100dvh, 0); /* Completely off-screen */
    }
    
    .cover-comments-modal.sheet-state-initial {
        /* Use JS-calculated custom property for precise positioning to show 50% of viewport */
        /* Fallback: 50vh positions modal to show exactly half the screen (strict half-screen in INITIAL) */
        transform: translate3d(0, var(--sheet-initial-height, 50vh), 0);
    }
    
    .cover-comments-modal.sheet-state-full {
        transform: translate3d(0, 0dvh, 0); /* Fully expanded */
    }
    
    /* Legacy support for .active class (will be overridden by state classes) */
    .cover-comments-modal.active:not(.sheet-state-closed):not(.sheet-state-initial):not(.sheet-state-full) {
        transform: translate3d(0, 0, 0);
    }
    
    /* State-based visible content height with flex layout */
    .cover-comments-modal.sheet-state-initial .cover-comments-modal__content {
        position: relative;
        display: flex;
        flex-direction: column;
        /* Use visible height variable (viewport - bottom nav) */
        height: calc(var(--sheet-visible-height, 96dvh) - var(--sheet-initial-height, 50vh));
        max-height: 96dvh;
    }
    
    .cover-comments-modal.sheet-state-full .cover-comments-modal__content {
        position: relative;
        display: flex;
        flex-direction: column;
        /* Use visible height variable (viewport - bottom nav) */
        height: var(--sheet-visible-height, 96dvh);
        max-height: 96dvh;
    }
    
    /* Sheet content as a column filling the height */
    .cover-comments-modal__content {
        position: relative;
        display: flex;
        flex-direction: column;
        max-height: 96dvh; /* 96dvh leaves space for iOS safe areas and avoids overlap with bottom navigation bar */
    }
    
    /* Body scrolls within visible area */
    /* On mobile: padding-bottom set by JS = fixed reply overlay height + safe area */
    /* On desktop: padding-bottom set by JS = absolute reply form height + safe area + extra */
    .cover-comments-modal__body {
        flex: 1 1 auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 8px 12px;
        /* JS will set precise padding-bottom */
        padding-bottom: 8px;
        overscroll-behavior: contain;
        -webkit-overscroll-behavior: contain;
    }
    
    /* Reply form absolutely pinned at bottom (DESKTOP ONLY - mobile uses fixed overlay) */
    .cover-comments-modal__reply-form {
        position: absolute;
        left: 0;
        right: 0;
        bottom: calc(env(safe-area-inset-bottom, 0px) + var(--reply-extra-bottom, 10px));
        min-height: var(--reply-form-height, 64px);
        z-index: 10;
        background: var(--bg-primary);
        border-top: 1px solid var(--border-color);
        padding: 8px 12px;
    }
    
    .reply-form {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px 12px;
        min-height: 64px;
    }
    
    .reply-form__input {
        flex: 1 1 auto;
        min-height: 40px;
    }
    
    .reply-form__submit {
        min-height: 40px;
    }
    
    /* Align "Ответить" under comment text */
    .comment__body {
        margin-bottom: 0;
        line-height: 1.4;
    }
    
    .comment__actions {
        margin-top: 4px;
        padding-top: 0;
        display: inline-flex;
        gap: 16px;
        align-items: baseline;
    }
    
    .comment__reply-btn,
    .comment__delete-btn {
        margin-top: 0;
        padding: 0;
    }
    
    /* 🔧 Shift page content up when sheet is open 
     * Note: Requires #page-content element in DOM */
    body.sheet-open #page-content {
        padding-bottom: var(--sheet-height, 65dvh);
        transition: padding-bottom 0.2s ease;
    }
    
    /* 🔧 Add drag handle at top */
    .cover-comments-modal__header::before {
        content: '';
        display: block;
        width: 40px;
        height: 4px;
        background: var(--text-secondary);
        opacity: 0.3;
        border-radius: 2px;
        margin: 0 auto 12px;
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
    }
    
    /* Center title */
    .cover-comments-modal__header {
        padding-top: 28px;
        justify-content: center;
        position: relative;
    }
    
    .cover-comments-modal__title {
        text-align: center;
        flex: 1;
    }
    
    /* 🔧 Add aria-label hint for screen readers */
    .cover-comments-modal__header::after {
        content: attr(aria-label);
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border-width: 0;
    }
    
    .comment-replies {
        margin-left: 28px;
        padding-left: 8px;
    }
}

/* Dark theme support (if needed) */
@media (prefers-color-scheme: dark) {
    .cover-comments-backdrop {
        background: rgba(0, 0, 0, 0.7);
    }
}
