/* ==========================================================================
   МАРГІНАЛІЇ - MODERN STYLE (FULL)
   ========================================================================== */

:root {
    --color-primary: #6366f1;
    --color-primary-dark: #4f46e5;
    --color-bg: #f8fafc;
    --color-card-bg: #ffffff;
    --color-text-main: #1e293b;
    --color-text-muted: #64748b;
    --border-color: #e2e8f0;
    
    --radius-lg: 24px;
    --radius-md: 16px;
    --radius-sm: 8px;
    
    --shadow-soft: 0 10px 30px -5px rgba(0, 0, 0, 0.04);
    --shadow-hover: 0 20px 40px -5px rgba(99, 102, 241, 0.15);
}

/* --- Hero Header --- */
.marginalia-hero {
    position: relative;
    background: #0f172a;
    background: radial-gradient(circle at top right, #1e1b4b 0%, #0f172a 100%);
    padding: clamp(80px, 10vw, 120px) 20px;
    text-align: center;
    color: #fff;
    border-radius: 0 0 50px 50px;
    margin-bottom: 60px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}

.hero-content {
    position: relative;
    z-index: 2;
    max-width: 800px;
    margin: 0 auto;
}

.hero-badge {
    display: inline-block;
    background: rgba(99, 102, 241, 0.2);
    color: #a5b4fc;
    padding: 8px 18px;
    border-radius: 50px;
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 24px;
    backdrop-filter: blur(4px);
    border: 1px solid rgba(99, 102, 241, 0.3);
}

.marginalia-hero h1 {
    font-size: clamp(2.5rem, 6vw, 4.5rem);
    margin-bottom: 20px;
    letter-spacing: -1.5px;
    font-weight: 800;
    line-height: 1.1;
    background: linear-gradient(to bottom right, #ffffff, #94a3b8);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.hero-text {
    font-size: clamp(1.1rem, 2vw, 1.35rem);
    color: #cbd5e1;
    font-style: italic;
    font-weight: 300;
    line-height: 1.6;
}

/* --- Masonry Grid Layout --- */
.main-container {
    max-width: 1400px;
}

.marginalia-grid {
    column-count: 4;
    column-gap: 24px;
    padding-bottom: 40px;
}

/* --- Card Styles --- */
.m-card {
    background: var(--color-card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-soft);
    margin-bottom: 24px;
    break-inside: avoid;
    position: relative;
    border: 1px solid var(--border-color);
    transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
    overflow: hidden;
    transform: translateZ(0); /* Safari flickering fix */
    display: flex;
    flex-direction: column;
}

.m-card:hover {
    transform: translateY(-8px);
    box-shadow: var(--shadow-hover);
    border-color: #c7d2fe;
}

/* Actions (Share Buttons) */
.m-card-actions {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 10;
    opacity: 0;
    transform: translateY(-10px);
    transition: all 0.3s ease;
}

.m-card:hover .m-card-actions {
    opacity: 1;
    transform: translateY(0);
}

.share-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: flex-end;
}

.action-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid var(--border-color);
    color: var(--color-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    transition: 0.2s;
}

.action-btn:hover {
    background: var(--color-primary);
    color: white;
    border-color: var(--color-primary);
}

/* Dropdown Menu styling */
.share-dropdown {
    display: none;
    position: absolute;
    right: 45px;
    top: 0;
    background: white;
    padding: 6px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    border: 1px solid var(--border-color);
    min-width: 120px;
    flex-direction: column;
    gap: 4px;
    z-index: 20;
}

.share-wrapper:hover .share-dropdown {
    display: flex;
    animation: fadeIn 0.2s ease-out;
}

.share-item {
    padding: 6px 12px;
    font-size: 12px;
    color: var(--color-text-main);
    text-decoration: none;
    border-radius: 6px;
    background: none;
    border: none;
    text-align: left;
    transition: background 0.2s;
    cursor: pointer;
    display: block;
    width: 100%;
}

.share-item:hover {
    background: #f1f5f9;
    color: var(--color-primary);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateX(10px); }
    to { opacity: 1; transform: translateX(0); }
}

/* Контейнер для поля введення */
.m-emoji-wrapper {
    position: relative;
    width: 100%;
    margin-bottom: 10px;
}

/* Кнопка-смайл всередині textarea */
.m-emoji-trigger {
    position: absolute;
    right: 10px;
    bottom: 10px;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.25rem;
    z-index: 5;
    padding: 5px;
    line-height: 1;
    transition: transform 0.2s ease;
}

.m-emoji-trigger:hover {
    transform: scale(1.2);
}

/* Випадаюче вікно зі смайлами */
.m-emoji-dropdown {
    position: absolute;
    bottom: 100%; /* Над полем */
    right: 0;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 8px;
    display: none; /* Керується через JS */
    flex-wrap: wrap;
    gap: 6px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    z-index: 100;
    width: 200px;
    margin-bottom: 8px;
    animation: fadeIn 0.2s ease-out;
}

/* Кнопки самих смайлів всередині панелі */
.m-emoji-dropdown button {
    background: #f1f5f9;
    border: none;
    border-radius: 6px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    cursor: pointer;
    transition: background 0.2s;
}

.m-emoji-dropdown button:hover {
    background: #e2e8f0;
}


/* Images */
.m-media {
    position: relative;
    overflow: hidden;
    border-bottom: 1px solid #f1f5f9;
}

.m-media img {
    width: 100%;
    height: auto;
    display: block;
    transition: transform 0.6s ease;
}

.m-card:hover .m-media img {
    transform: scale(1.05);
}

.m-media-placeholder {
    height: 160px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8fafc;
    border-bottom: 1px solid var(--border-color);
}

.m-icon {
    font-size: 3.5rem;
    opacity: 0.3;
}

/* Card Content */
.m-body {
    padding: 24px;
    display: flex;
    flex-direction: column;
    flex-grow: 1;
}

.m-meta-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 14px;
}

.m-badge {
    background: #eef2ff;
    color: var(--color-primary);
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    padding: 5px 10px;
    border-radius: 6px;
    letter-spacing: 0.5px;
}

.m-date {
    font-size: 0.75rem;
    color: #94a3b8;
    font-weight: 500;
}

.m-title {
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.35;
    margin-bottom: 12px;
    color: var(--color-text-main);
}

.m-description {
    font-size: 0.95rem;
    line-height: 1.6;
    color: var(--color-text-muted);
    margin-bottom: 20px;
    flex-grow: 1;
}

/* Footer & Comments */
.m-footer {
    border-top: 1px dashed var(--border-color);
    padding-top: 16px;
    margin-top: auto;
}

.btn-comments-toggle {
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: color 0.2s;
}

.btn-comments-toggle:hover {
    color: var(--color-primary);
}

.comments-container {
    margin-top: 16px;
    background: #f8fafc;
    padding: 16px;
    border-radius: 12px;
    border: 1px solid #f1f5f9;
}

.comment-smiles {
    display: flex;
    gap: 8px;
}

.comment-smiles span {
    cursor: pointer;
    font-size: 1.2rem;
    transition: transform 0.2s;
    user-select: none;
}
.comment-smiles span:hover {
    transform: scale(1.3);
}

/* --- Empty State --- */
.empty-state-wrapper {
    grid-column: 1 / -1;
    text-align: center;
    padding: 80px 20px;
}

.empty-card {
    background: white;
    padding: 50px;
    border-radius: 30px;
    border: 2px dashed #cbd5e1;
    display: inline-block;
    max-width: 450px;
}

.empty-icon {
    font-size: 4.5rem;
    display: block;
    margin-bottom: 20px;
}

/* --- Contribution Section (CTA) --- */
.contribution-section {
    margin-top: 80px;
    margin-bottom: 80px;
    padding: 0 20px;
}

.contribution-card {
    background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    border-radius: 32px;
    padding: 3px; 
    box-shadow: 0 20px 40px rgba(99, 102, 241, 0.25);
    color: white;
    max-width: 900px;
    margin: 0 auto;
}

.contribution-content {
    background: rgba(255,255,255,0.1); 
    backdrop-filter: blur(10px);
    border-radius: 30px;
    padding: 60px 40px;
    text-align: center;
}

.contribution-content h2 {
    font-weight: 800;
    margin-bottom: 16px;
    font-size: 2rem;
}

.contribution-content p {
    color: rgba(255,255,255,0.9);
    max-width: 600px;
    margin: 0 auto 30px;
    font-size: 1.15rem;
    line-height: 1.6;
}

.icon-box {
    font-size: 2.5rem;
    margin-bottom: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,0.2);
    width: 80px;
    height: 80px;
    border-radius: 50%;
    box-shadow: 0 8px 16px rgba(0,0,0,0.1);


/* Marginalia comments: emoji dropdown and unified comment bubble rules */
.m-form { position: relative; }

.m-input-group { position: relative; }
.m-input-group textarea { padding-right: 46px; }

.m-emoji-trigger, .emoji-toggle {
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 6px 8px;
    cursor: pointer;
    font-size: 1.05rem;
    margin-left: 8px;
    position: absolute;
    right: 8px;
    bottom: 8px;
    z-index: 1220;
}

.emoji-dropdown {
    position: absolute;
    right: 8px;
    bottom: 44px;
    display: none;
    background: #fff;
    border: 1px solid var(--border-color);
    box-shadow: 0 8px 24px rgba(16,24,40,0.08);
    padding: 8px;
    border-radius: 10px;
    z-index: 1200;
    white-space: nowrap;
}
.emoji-dropdown.active { display: block; }

.emoji-dropdown .emoji-bar { display:flex; gap:6px; align-items:center; }
.emoji-btn { background: transparent; border: none; font-size: 18px; cursor: pointer; padding: 4px; }

/* Shared renderer bubble styles (map to marginalia aesthetic) */
.comment-bubble, .m-bubble {
    background: #fff;
    border: 1px solid #eef2ff;
    padding: 10px 12px;
    border-radius: 12px;
    margin-bottom: 10px;
    box-shadow: 0 6px 18px rgba(15,23,42,0.03);
}
.comment-bubble.is-reply, .m-bubble.m-reply { border-left: 3px solid var(--color-primary); padding-left: 10px; }
.cb-header { display:flex; justify-content:space-between; align-items:center; gap:8px; margin-bottom:6px; }
.cb-name { font-weight:700; color:var(--color-text-main); }
.cb-date { font-size:0.8rem; color:var(--color-text-muted); }
.cb-text { color:var(--color-text-main); line-height:1.4; }
.cb-actions { margin-top:8px; }
.cb-reply { background:transparent; border:none; color:var(--color-primary); cursor:pointer; font-weight:600; }

.m-loading { color: var(--color-text-muted); font-style: italic; padding:10px; }
.m-no, .m-error { color: var(--color-text-muted); padding:10px; }

/* Emoji dropdown for marginalia comment forms */
.m-form { position: relative; }
.emoji-dropdown { display: none; position: absolute; bottom: 100%; left: 0; background: white; border: 1px solid #ccc; z-index: 100; display: none; grid-template-columns: repeat(5, 1fr); padding: 5px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); border-radius: 8px; gap:6px; }
.emoji-dropdown.active { display: grid; }
.emoji-toggle { background: transparent; border: none; cursor: pointer; font-size: 1.1rem; padding: 6px; margin-right:6px; }
.emoji-dropdown .emoji-btn { background: transparent; border: none; font-size: 1.1rem; cursor: pointer; padding: 6px; }
}

/* --------------------------------------------------------------------------
     Compact Marginalia comment styles (Маргіналія)
     ----------------------------------------------------------------------- */
.m-comments-list { display:flex; flex-direction:column; gap:8px; }
.m-bubble { background: var(--color-card-bg); border-radius: 10px; padding:8px 10px; box-shadow: 0 4px 14px rgba(16,24,40,0.04); border:1px solid rgba(14,16,39,0.04); }
.m-bubble.m-reply { margin-left: 18px; background: #f1f5ff; }
.m-h { display:flex; gap:8px; align-items:center; justify-content:space-between; margin-bottom:6px; }
.m-n { font-weight:700; color:var(--color-text-main); font-size:0.85rem; }
.m-d { color:var(--color-text-muted); font-size:0.72rem; }
.m-t { font-size:0.85rem; color:var(--color-text-main); line-height:1.3; }
.m-a { margin-top:6px; display:flex; gap:6px; }
.m-reply-btn { background:transparent; border:none; color:var(--color-primary); font-weight:600; cursor:pointer; padding:4px 6px; border-radius:6px; }
.m-form { display:flex; flex-direction:column; gap:6px; margin-top:8px; }
.m-form input, .m-form textarea { font-size:0.85rem; padding:6px 8px; border-radius:6px; border:1px solid var(--border-color); }
.m-emoji-bar { display:flex; gap:6px; }
.m-emoji-btn { background:transparent; border:1px solid #f1f5ff; padding:4px 6px; border-radius:6px; cursor:pointer; font-size:14px; }
.m-no { color:var(--color-text-muted); font-size:0.85rem; }

@media (max-width: 900px) {
    .m-bubble.m-reply { margin-left: 8px; }
    .m-title { font-size: 1rem; }
}


.btn-cta {
    display: inline-block;
    background: #ffffff;
    color: var(--color-primary);
    padding: 16px 36px;
    border-radius: 50px;
    font-weight: 700;
    text-decoration: none;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 10px 20px rgba(0,0,0,0.15);
    font-size: 1.1rem;
}

.btn-cta:hover {
    transform: translateY(-3px);
    box-shadow: 0 20px 30px rgba(0,0,0,0.2);
    color: var(--color-primary-dark);
}

/* --- Navigation --- */
.back-nav {
    text-align: center;
    margin-bottom: 60px;
}

.link-back {
    color: var(--color-text-muted);
    text-decoration: none;
    font-weight: 600;
    font-size: 1.1rem;
    padding: 10px 20px;
    border-radius: 50px;
    transition: all 0.2s;
    background: white;
    border: 1px solid var(--border-color);
}

.link-back:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
    background: #f8fafc;
}

/* ==========================================================================
   MOBILE ADAPTIVE
   ========================================================================== */

@media (max-width: 1200px) {
    .marginalia-grid { column-count: 3; }
}

@media (max-width: 900px) {
    .marginalia-grid { column-count: 2; }
    .hero-content h1 { font-size: 3rem; }
}

@media (max-width: 600px) {
    .marginalia-grid { column-count: 1; }
    
    .marginalia-hero {
        padding: 60px 20px;
        border-radius: 0 0 30px 30px;
    }

    .hero-content h1 { font-size: 2.2rem; }
    
    /* Make actions always visible on mobile since there is no hover */
    .m-card-actions {
        opacity: 1;
        transform: none;
        top: 12px;
        right: 12px;
    }
    
    .contribution-content { padding: 40px 20px; }
    .contribution-content h2 { font-size: 1.5rem; }
    
    .share-dropdown {
        right: 0;
        top: 45px;
    }
}
/* 1. Запобігаємо розриву картки між колонками */
.marginalia-item {
    break-inside: avoid-column;
    page-break-inside: avoid;
    display: inline-block; /* Важливо для стабільності в колонках */
    width: 100%;
    margin-bottom: 20px;
    position: relative; /* Щоб z-index працював коректно */
}

/* 2. Коли коментарі відкриті, піднімаємо картку над іншими */
.marginalia-item:has(.m-comments-list:not(.is-hidden)),
.marginalia-item:has(.m-form:not(.is-hidden)) {
    z-index: 100;
}

/* 3. Виправляємо контейнер коментарів у маргіналіях */
.m-comments-container {
    display: block !important; /* Щоб браузер точно бачив блок */
    overflow: visible !important; /* Дозволяємо випадаючим спискам виходити за межі */
}

/* 4. Стиль для кнопки перемикання (якщо вона "зникає") */
.toggle-comments, .m-toggle {
    position: relative;
    z-index: 10;
    cursor: pointer;
}

/* 5. Виправлення для випадаючого вікна емодзі (воно часто ховається під картку) */
.m-emoji-dropdown {
    position: absolute;
    z-index: 2000 !important;
    background: white;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
/* 3. ВАЖЛИВО: Картка маргіналії не повинна обрізати вікно */
.marginalia-item, .m-comments-container {
    overflow: visible !important; /* Дозволяє емодзі виходити за межі картки */
}

/* 4. Піднімаємо картку над іншими, коли фокус на формі */
.marginalia-item:focus-within {
    z-index: 100;
}