/* =====================  SCROLL-DRIVEN STORIES SECTION  ===================== */

.stories-hero {
    position: relative;
    background: var(--bg);
    color: var(--text);
    min-height: 75vh;
    padding: calc(var(--header-height, 70px) + 1.5rem) 0 0;
    width: 100%;
    box-sizing: border-box;
    overflow: visible;
    /* Allow wave to extend above */
}

.stories-hero .section-title {
    margin-bottom: 0;
}

.stories-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0;
    position: relative;
    z-index: 2;
}

/* Tablet Layout */
@media (min-width: 768px) {
    .stories-hero {
        padding: 1rem 2rem;
    }

    .stories-wrapper {
        padding: 0 2rem;
    }
}

@media (min-width: 1024px) {
    .stories-wrapper {
        display: grid;
        grid-template-columns: 1.5fr 1fr;
        grid-template-rows: 1fr auto;
        gap: 2rem;
        padding: 0;
    }

    .stories-visual {
        grid-column: 1;
        grid-row: 1;
    }

    .stories-content {
        grid-column: 2;
        grid-row: 1;
        min-width: 0;
        min-inline-size: 280px;
        padding-left: 1.5rem;
        padding-right: 1.5rem;
        padding-top: 0.5rem;
        box-sizing: border-box;
    }

    .stories-pagination {
        grid-column: 1 / -1;
        grid-row: 2;
    }

    .stories-mobile-titles {
        grid-column: 1 / -1;
        grid-row: 2;
    }

    html {
        scroll-padding-block-start: var(--header-height);
    }

    body {
        timeline-scope: --story-lockup-1, --story-lockup-2, --story-lockup-3, --story-lockup-4;
    }
}

/* Узкий десктоп: больше места для текста, чтобы не обрезался */
@media (min-width: 1024px) and (max-width: 1200px) {
    .stories-wrapper {
        grid-template-columns: 1.2fr 1fr;
    }

    .stories-content {
        min-inline-size: 300px;
    }
}

/* Large Desktop */
@media (min-width: 1400px) {
    .stories-wrapper {
        gap: 3rem;
    }
}

/* ===== Visual Cards ===== */
.stories-visual {
    position: relative;
    display: grid;
    place-items: center;
    width: 100%;
}

.stories-bg-canvas {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
    opacity: 0.6;
}

@media (max-width: 1023px) {
    .stories-visual {
        position: sticky;
        /* Тянем фон до самого верха, а куб опускаем padding-ом под шапку */
        top: 0;
        height: calc(min(420px, 95vw) + var(--header-height, 70px));
        min-height: calc(min(420px, 95vw) + var(--header-height, 70px));
        max-height: calc(min(420px, 95vw) + var(--header-height, 70px));
        z-index: 50;
        /* Gradient mask: solid at top to hide text behind cube, transparent at very bottom */
        background: linear-gradient(to bottom,
                var(--bg) 0%,
                var(--bg) 90%,
                transparent 100%);
        margin: 0;
        /* Сверху отступ под шапку, чтобы куб не цеплял topbar */
        padding: var(--header-height, 70px) 1rem 0;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
    }

    .story-card {
        width: min(400px, 95%);
        height: min(400px, 95%);
        flex-shrink: 0;
        margin: 4px;
    }

    .story-card-2,
    .story-card-3,
    .story-card-4 {
        display: none;
    }
}

/* Tablet adjustments */
@media (min-width: 768px) and (max-width: 1023px) {
    .stories-visual {
        min-height: 70vh;
    }

    .story-card {
        max-width: 500px;
    }
}

/* Desktop: Fixed, stacked cards with animations */
@media (min-width: 1024px) {
    .stories-visual {
        position: sticky;
        top: calc(var(--header-height, 80px) + 2rem);
        height: 75vh;
        align-self: start;
    }

    .story-card {
        grid-area: 1/1;
        width: min(580px, 72%);
        height: min(580px, 72%);
        aspect-ratio: 1;
        border-radius: var(--radius-lg, 1rem);
        transform-origin: center center;
        margin: 8px;
    }

    /* Rotation and layering */
    .story-card-1 {
        transform: none;
        z-index: 3;
    }

    .story-card-2,
    .story-card-3,
    .story-card-4 {
        display: none;
    }
}

/* Large Desktop - карточки чуть крупнее, но не гигантские */
@media (min-width: 1400px) {
    .story-card {
        width: min(680px, 78%) !important;
        height: min(680px, 78%) !important;
        margin: 8px;
    }
}

/* ===== Content Lockups ===== */
.stories-content {
    display: block;
    width: 100%;
}

/* Mobile content */
@media (max-width: 1023px) {
    .stories-content {
        padding: 0 0.75rem;
        position: relative;
        z-index: 5;
        box-sizing: border-box;
    }

    .story-lockup {
        min-height: 50vh;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        padding: 0.75rem 0.5rem;
        margin-bottom: 0;
        text-align: center;
        box-sizing: border-box;
    }

    .story-lockup-inner {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow-wrap: break-word;
        word-wrap: break-word;
    }

    .story-lockup:last-child {
        padding-bottom: 8rem;
    }

    .story-lockup h3 {
        font-size: clamp(1.5rem, 5vw, 2.2rem);
        font-weight: 700;
        line-height: 1.2;
        margin-bottom: 1rem;
        color: var(--text-heading, var(--text));
        max-width: 100%;
    }

    .story-subhead {
        color: var(--accent);
        font-size: clamp(1rem, 3.5vw, 1.125rem);
        font-weight: 600;
        margin-bottom: 1rem;
        line-height: 1.4;
        max-width: 100%;
    }

    .story-lockup p {
        font-size: clamp(0.95rem, 3vw, 1.05rem);
        line-height: 1.6;
        color: var(--text-secondary);
        max-width: min(400px, 100%);
        margin: 0 auto;
    }
}

/* Desktop content */
@media (min-width: 1024px) {
    .stories-content {
        display: block;
        padding: 1rem 1.5rem 0;
        box-sizing: border-box;
    }

    .story-lockup {
        min-height: 75vh;
        display: flex;
        align-items: flex-start;
        justify-content: center;
        flex-direction: column;
        padding: 1rem 0;
    }

    .story-lockup-inner {
        max-width: 100%;
        min-width: 0;
        overflow-wrap: break-word;
        word-wrap: break-word;
        padding-right: 0.75rem;
    }

    .story-lockup h3 {
        font-size: clamp(1.75rem, 3vw, 3rem);
        font-weight: 700;
        line-height: 1.1;
        margin-bottom: 1.5rem;
        max-width: 100%;
        text-wrap: balance;
        color: var(--text-heading, var(--text));
    }

    .story-subhead {
        color: var(--accent);
        font-size: clamp(1rem, 1.5vw, 1.5rem);
        font-weight: 600;
        margin-bottom: 1rem;
        max-width: 100%;
        line-height: 1.3;
        text-wrap: pretty;
    }

    .story-lockup p {
        font-size: clamp(0.875rem, 1.1vw, 1.125rem);
        line-height: 1.7;
        color: var(--muted, var(--text));
        max-width: 100%;
    }

    /* View timeline names for scroll-driven animations */
    .story-lockup-1 {
        view-timeline-name: --story-lockup-1;
    }

    .story-lockup-2 {
        view-timeline-name: --story-lockup-2;
    }

    .story-lockup-3 {
        view-timeline-name: --story-lockup-3;
    }

    .story-lockup-4 {
        view-timeline-name: --story-lockup-4;
    }
}

/* ===== Mobile Titles ===== */
.stories-mobile-titles {
    display: none;
}

@media (max-width: 1023px) {
    .stories-mobile-titles {
        display: none !important;
    }

    .mobile-title {
        grid-area: 1/1;
        width: 100%;
        text-align: center;
        padding: 1rem;
        font-weight: 600;
        font-size: clamp(0.875rem, 3vw, 1.125rem);
        color: var(--text-heading, var(--text));
        background: var(--bg);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    [data-theme="dark"] .mobile-title {
        background: var(--panel, var(--bg));
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }

    body {
        timeline-scope: --story-card-1, --story-card-2, --story-card-3, --story-card-4;
    }

    .story-card-1 {
        view-timeline-name: --story-card-1;
    }

    .story-card-2 {
        view-timeline-name: --story-card-2;
    }

    .story-card-3 {
        view-timeline-name: --story-card-3;
    }

    .story-card-4 {
        view-timeline-name: --story-card-4;
    }
}

/* ===== Pagination ===== */
.stories-pagination {
    display: none;
}

/* Mobile: пагинация по центру внизу, липкая при прокрутке */
@media (max-width: 1023px) {
    .stories-pagination {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1rem 0;
        width: 100%;
        position: sticky;
        bottom: 1.5rem;
        z-index: 100;
        pointer-events: none;
    }

    .stories-pagination .pagination-dots {
        pointer-events: auto;
    }

    .pagination-dots {
        display: flex;
        gap: 0.75rem;
        pointer-events: auto;
        background: rgba(var(--bg-rgb, 255, 255, 255), 0.8);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        padding: 0.75rem 1.5rem;
        border-radius: 2rem;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    [data-theme="dark"] .pagination-dots {
        background: rgba(0, 0, 0, 0.6);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .pagination-dots a {
        display: block;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        border: 2px solid var(--accent);
        background: transparent;
        transition: all 300ms ease;
        cursor: pointer;
    }

    .pagination-dots a:hover,
    .pagination-dots a:active {
        background: var(--accent);
        transform: scale(1.3);
    }

    .pagination-dots a.active {
        background: var(--accent);
        transform: scale(1.3);
        border-color: var(--accent);
    }
}

@media (min-width: 1024px) {
    .stories-pagination {
        /* По центру секции внизу, липкая при прокрутке */
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1rem 0;
        width: 100%;
        position: sticky;
        bottom: 2rem;
        z-index: 100;
        pointer-events: none;
    }

    .stories-pagination .pagination-dots {
        pointer-events: auto;
    }

    .pagination-dots {
        display: flex;
        gap: 0.75rem;
        pointer-events: auto;
        background: rgba(var(--bg-rgb, 255, 255, 255), 0.8);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        padding: 0.75rem 1.5rem;
        border-radius: 2rem;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    [data-theme="dark"] .pagination-dots {
        background: rgba(0, 0, 0, 0.6);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .pagination-dots a {
        display: block;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        border: 2px solid var(--accent);
        background: transparent;
        transition: all 300ms ease;
        cursor: pointer;
    }

    .pagination-dots a:hover {
        background: var(--accent);
        transform: scale(1.3);
    }
}

/* Large screens - bigger pagination */
@media (min-width: 1400px) {
    .pagination-dots {
        gap: 1rem;
        padding: 1rem 2rem;
    }

    .pagination-dots a {
        width: 12px;
        height: 12px;
    }
}

/* ===== Scroll-Driven Animations ===== */
@supports (animation-timeline: scroll()) {
    /* Desktop: Card slide-out animations */
    @media (min-width: 1024px) {
        .story-card-2 {
            animation: slide-out-rotate linear both;
            animation-timeline: --story-lockup-4;
        }

        .story-card-3 {
            animation: slide-out-rotate linear both;
            animation-timeline: --story-lockup-3;
        }

        .story-card-4 {
            animation: slide-out-rotate linear both;
            animation-timeline: --story-lockup-2;
        }

        /* Pagination highlight */
        .pagination-dots a:nth-child(1) {
            animation: pagination-highlight linear both;
            animation-timeline: --story-lockup-1;
        }

        .pagination-dots a:nth-child(2) {
            animation: pagination-highlight linear both;
            animation-timeline: --story-lockup-2;
        }

        .pagination-dots a:nth-child(3) {
            animation: pagination-highlight linear both;
            animation-timeline: --story-lockup-3;
        }

        .pagination-dots a:nth-child(4) {
            animation: pagination-highlight linear both;
            animation-timeline: --story-lockup-4;
        }
    }

    /* Mobile: Title reveal animations */
    @media (max-width: 1023px) {
        .mobile-title {
            animation: mobile-title-reveal linear both;
            animation-range: cover 45%;
        }

        .mobile-title-1 {
            animation-timeline: --story-card-1;
        }

        .mobile-title-2 {
            visibility: hidden;
            animation-timeline: --story-card-2;
        }

        .mobile-title-3 {
            visibility: hidden;
            animation-timeline: --story-card-3;
        }

        .mobile-title-4 {
            visibility: hidden;
            animation-timeline: --story-card-4;
        }
    }
}

/* Fallback for browsers without scroll-driven animations */
@supports not (animation-timeline: scroll()) {
    .stories-pagination {
        display: none;
    }

    @media (min-width: 1024px) {

        .story-card-2,
        .story-card-3,
        .story-card-4 {
            opacity: 0.3;
        }
    }
}

/* ===== Keyframes ===== */
@keyframes slide-out-rotate {

    50%,
    100% {
        transform: translate(-50%, 50%) rotate(-50deg);
    }

    40%,
    100% {
        opacity: 0;
    }
}

@keyframes mobile-title-reveal {
    to {
        visibility: visible;
    }
}

@keyframes pagination-highlight {
    50% {
        background-color: var(--accent);
        transform: scale(1.4);
    }
}