@use '../../../styles/variables' as *;
@use '../../../styles/mixins' as *;

.awt-book-gallery {
    padding: 3rem 0;
    background-color: #ffffff;
    box-sizing: border-box;

    *,
    *::before,
    *::after {
        box-sizing: inherit;
    }

    &__container {
        @include awt-container;
    }

    /* Mobile Filter Button */
    &__mobile-toggle-wrap {
        display: none;
        width: 100%;
        margin-bottom: 1.5rem;

        @media (max-width: 900px) {
            display: block;
        }
    }

    &__mobile-toggle-btn {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        background-color: #ffffff;
        border: 1px solid #e2e8f0;
        color: $awt-color-primary;
        font-weight: 700;
        font-size: 0.875rem;
        padding: 0.75rem 1rem;
        border-radius: $awt-radius-lg;
        box-shadow: $awt-shadow-sm;
        cursor: pointer;
        transition: all 0.2s ease;

        svg {
            width: 16px;
            height: 16px;
        }

        &:hover {
            border-color: $awt-color-accent;
            color: $awt-color-accent;
        }
    }

    /* Backdrop */
    &__backdrop {
        position: fixed;
        inset: 0;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 40;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;

        &.is-awt-visible {
            opacity: 1;
            visibility: visible;
        }
    }

    /* Layout Grid */
    &__layout {
        display: grid;
        grid-template-columns: 280px 1fr;
        gap: 2.5rem;
        align-items: start;

        @media (max-width: 900px) {
            grid-template-columns: 1fr;
        }

        &--no-sidebar {
            grid-template-columns: 1fr;
        }
    }

    /* Sidebar */
    &__sidebar {
        display: flex;
        flex-direction: column;
        gap: 1.5rem;

        @media (max-width: 900px) {
            position: fixed;
            inset: 0 auto 0 0;
            width: 320px;
            max-width: 85vw;
            background-color: #ffffff;
            z-index: 50;
            padding: 1.5rem;
            overflow-y: auto;
            transform: translateX(-100%);
            transition: transform 0.3s ease;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);

            &.is-awt-open {
                transform: translateX(0);
            }
        }
    }

    /* Mobile Header inside Sidebar */
    &__mobile-header {
        display: none;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 1.5rem;

        @media (max-width: 900px) {
            display: flex;
        }
    }

    &__mobile-title {
        font-family: $awt-font-serif;
        font-size: 1.25rem;
        font-weight: 700;
        color: $awt-color-primary;
        margin: 0;
    }

    &__mobile-close {
        background: none;
        border: none;
        color: #94a3b8;
        cursor: pointer;
        padding: 0.5rem;
        display: flex;
        align-items: center;
        justify-content: center;

        svg {
            width: 18px;
            height: 18px;
        }

        &:hover {
            color: $awt-color-accent;
        }
    }

    /* Search Box */
    &__search-box,
    &__filters-box {
        background-color: #ffffff;
        padding: 1.5rem;
        border-radius: $awt-radius-xl;
        border: 1px solid #f1f5f9;
        box-shadow: $awt-shadow-sm;
    }

    &__search-title {
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: $awt-color-primary;
        margin: 0 0 1rem 0;
    }

    &__search-wrapper,
    &__search-wrap {
        position: relative;
        display: flex;
        align-items: center;
        width: 100%;
    }

    &__search-icon {
        position: absolute;
        left: 14px;
        top: 50%;
        transform: translateY(-50%);
        display: inline-flex;
        align-items: center;
        justify-content: center;
        pointer-events: none;
        color: #94a3b8;
        z-index: 2;

        svg {
            width: 16px !important;
            height: 16px !important;
            display: block !important;
            stroke: currentColor;
            fill: none;
        }
    }

    &__input,
    &__search-input {
        width: 100%;
        box-sizing: border-box;
        padding-top: 10px;
        padding-bottom: 10px;
        padding-left: 42px !important; // CRITICAL: leaves space for 16px icon + margins
        padding-right: 14px;
        font-size: 0.95rem;
        line-height: 1.5;
        border-radius: 8px;
        border: 1px solid #cbd5e1;
        background-color: #ffffff;
        color: #1e293b;
        transition: border-color 0.2s ease, box-shadow 0.2s ease;

        &::placeholder {
            color: #94a3b8;
            opacity: 1;
        }

        &:focus {
            outline: none;
            border-color: var(--awt-color-primary, #3b82f6);
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
        }
    }

    /* Filter Groups */
    &__filter-group {
        margin-bottom: 1.5rem;
    }

    &__filter-label,
    &__dropdown-label,
    &__group-title,
    &__search-title {
        display: block;
        font-family: var(--awt-font-sans, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif);
        font-size: 0.75rem;
        font-weight: 700;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        color: #0f172a;
        margin-bottom: 0.5rem;
    }

    &__locked-term {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        opacity: 0.75;
        cursor: not-allowed;
        margin-bottom: 0.5rem;
    }

    &__term-name {
        font-size: 0.875rem;
        font-weight: 700;
        color: $awt-color-secondary;
    }

    &__back-link {
        display: inline-flex;
        align-items: center;
        gap: 0.375rem;
        font-size: 0.75rem;
        font-weight: 700;
        color: $awt-color-accent;
        text-decoration: none;
        margin-top: 0.25rem;

        svg {
            width: 12px;
            height: 12px;
        }

        &:hover {
            text-decoration: underline;
        }
    }

    /* Dropdowns */
    &__dropdown {
        position: relative;
        margin-bottom: 1rem;
    }

    &__dropdown-btn,
    &__dropdown-trigger {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px 14px;
        font-size: 0.95rem;
        font-weight: 500;
        color: #1e293b !important; // Dark readable slate
        background-color: #ffffff !important; // Clean white default
        border: 1px solid #cbd5e1 !important;
        border-radius: 8px;
        cursor: pointer;
        transition: border-color 0.2s ease, background-color 0.2s ease, color 0.2s ease;
        text-align: left;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

        &:hover {
            border-color: #94a3b8 !important;
            background-color: #f8fafc !important;
            color: #0f172a !important;
        }

        // Active / Opened State (when dropdown is expanded)
        &.is-open,
        &.is-awt-open,
        &[aria-expanded="true"] {
            background-color: var(--awt-color-navy, #0f172a) !important;
            color: #ffffff !important;
            border-color: var(--awt-color-navy, #0f172a) !important;

            .awt-book-gallery__dropdown-text {
                color: #ffffff !important;
            }

            svg {
                stroke: #ffffff !important;
                color: #ffffff !important;
                transform: rotate(0deg);
            }
        }

        svg {
            width: 16px;
            height: 16px;
            stroke: currentColor;
            fill: none;
            transition: transform 0.2s ease;
            flex-shrink: 0;
        }
    }

    &__dropdown-text {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        color: inherit;
        font-weight: 500;
        font-size: 0.875rem;

        &.is-awt-selected {
            font-weight: 600;
        }
    }

    &__dropdown-icon {
        display: flex;
        align-items: center;
        transition: transform 0.2s ease;
        transform: rotate(180deg); // Default collapsed

        svg {
            width: 14px;
            height: 14px;
            color: inherit;
        }

        &.is-awt-expanded {
            transform: rotate(0deg);
        }
    }

    &__dropdown-menu {
        position: absolute;
        top: calc(100% + 4px);
        left: 0;
        width: 100%;
        background-color: #ffffff;
        border: 1px solid #e2e8f0;
        box-shadow: $awt-shadow-xl;
        border-radius: $awt-radius-lg;
        z-index: 50;
        padding: 0.5rem;
        max-height: 240px;
        overflow-y: auto;
        opacity: 0;
        visibility: hidden;
        transform: translateY(-4px);
        transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;

        &.is-awt-open {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
    }

    &__dropdown-search {
        width: 100%;
        font-size: 0.75rem;
        border: 1px solid #e2e8f0;
        border-radius: 4px;
        padding: 0.375rem 0.5rem;
        margin-bottom: 0.5rem;
        outline: none;

        &:focus {
            border-color: $awt-color-accent;
        }
    }

    &__dropdown-list {
        display: flex;
        flex-direction: column;
        gap: 2px;
    }

    &__dropdown-item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.375rem 0.5rem;
        border-radius: 4px;
        cursor: pointer;
        transition: background-color 0.15s ease;

        &:hover {
            background-color: #f8fafc;
        }
    }

    &__dropdown-checkbox,
    &__checkbox {
        width: 14px;
        height: 14px;
        border-radius: 3px;
        border: 1px solid #cbd5e1;
        accent-color: $awt-color-accent;
        cursor: pointer;
    }

    &__dropdown-item-label {
        font-size: 0.75rem;
        color: $awt-color-secondary;
        font-weight: 500;
        transition: color 0.15s ease;

        &:hover {
            color: $awt-color-accent;
        }
    }

    &__dropdown-count {
        color: #94a3b8;
        font-weight: 400;
        margin-left: 2px;
    }

    &__dropdown-empty {
        font-size: 0.75rem;
        color: #94a3b8;
        font-style: italic;
        padding: 0.5rem;
    }

    /* Filter Action Buttons */
    &__apply-btn {
        width: 100%;
        padding: 0.75rem;
        background-color: $awt-color-primary;
        color: #ffffff;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        transition: background-color 0.2s ease;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);

        &:hover {
            background-color: #1e293b;
        }
    }

    &__reset-btn {
        width: 100%;
        padding: 0.5rem;
        margin-top: 0.5rem;
        background: none;
        border: none;
        color: #94a3b8;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        cursor: pointer;
        transition: color 0.2s ease;

        &:hover {
            color: $awt-color-accent;
        }
    }

    /* Main Area */
    &__main {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-width: 0;
    }

    /* Top Bar */
    &__topbar {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem 1.25rem;
        background-color: #ffffff;
        border: 1px solid #f1f5f9;
        border-radius: $awt-radius-lg;
        box-shadow: $awt-shadow-sm;
        margin-bottom: 2rem;
        flex-wrap: wrap;
        gap: 1rem;
    }

    &__result-count {
        font-size: 0.875rem;
        color: $awt-color-secondary;
        margin: 0;
        font-weight: 500;

        strong {
            color: $awt-color-primary;
            font-weight: 700;
        }
    }

    &__sorting {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    &__sort-label {
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        color: #94a3b8;
    }

    &__sort-select {
        background-color: $awt-color-paper;
        border: 1px solid #e2e8f0;
        color: $awt-color-primary;
        font-size: 0.875rem;
        border-radius: 4px;
        padding: 0.375rem 0.75rem;
        outline: none;
        cursor: pointer;
        transition: border-color 0.2s ease;

        &:focus {
            border-color: $awt-color-accent;
        }
    }

    /* Book Grid */
    &__grid {
        display: grid;
        grid-template-columns: repeat(var(--awt-book-gallery-columns, 3), minmax(0, 1fr));
        gap: 2rem;
        transition: opacity 0.3s ease;

        &.is-awt-loading {
            opacity: 0.4;
            pointer-events: none;
        }

        @media (max-width: 1024px) {
            grid-template-columns: repeat(min(var(--awt-book-gallery-columns, 3), 2), minmax(0, 1fr));
        }

        @media (max-width: 640px) {
            grid-template-columns: 1fr;
        }
    }

    /* Card */
    &__card {
        background-color: #ffffff;
        border-radius: $awt-radius-xl;
        box-shadow: $awt-shadow-sm;
        border: 1px solid #f1f5f9;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        transition: transform 0.3s ease, box-shadow 0.3s ease;

        &:hover {
            transform: translateY(-4px);
            box-shadow: $awt-shadow-xl;

            .awt-book-gallery__cover-img {
                transform: scale(1.05);
            }
        }
    }

    &__cover-wrap {
        position: relative;
        padding: 1.5rem;
        background-color: #f1f5f9;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 16rem;
        overflow: hidden;
    }

    &__badge {
        position: absolute;
        top: 1rem;
        left: 1rem;
        background-color: $awt-color-accent;
        color: #ffffff;
        font-size: 10px;
        font-weight: 700;
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        z-index: 5;
    }

    &__cover-img {
        height: 12rem;
        width: auto;
        max-width: 100%;
        object-fit: contain;
        border-radius: 4px;
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        transition: transform 0.5s ease;
    }

    &__cover-placeholder {
        height: 12rem;
        width: 8rem;
        background-color: #e2e8f0;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #94a3b8;
        font-size: 0.75rem;
    }

    /* Card Content */
    &__content {
        padding: 1.25rem;
        flex: 1;
        display: flex;
        flex-direction: column;
    }

    &__meta-top {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 0.75rem;
        gap: 0.5rem;
    }

    &__category {
        font-size: 10px;
        font-weight: 700;
        color: $awt-color-accent;
        text-transform: uppercase;
        letter-spacing: 0.1em;
        margin-top: 0.125rem;
    }

    &__pricing {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
    }

    &__badge-save {
        font-size: 10px;
        font-weight: 700;
        color: #059669;
        background-color: #ecfdf5;
        padding: 0.125rem 0.375rem;
        border-radius: 4px;
        margin-bottom: 0.25rem;
    }

    &__price-wrap {
        display: flex;
        align-items: center;
        gap: 0.375rem;
        line-height: 1;
    }

    &__price-regular {
        font-size: 0.75rem;
        color: #9ca3af;
        text-decoration: line-through;
    }

    &__price-sale,
    &__price {
        font-size: 0.875rem;
        font-weight: 700;
        color: $awt-color-primary;
    }

    &__book-title {
        font-family: $awt-font-serif;
        font-size: 1.125rem;
        font-weight: 700;
        margin: 0 0 0.25rem 0;
        line-height: 1.35;
    }

    &__book-title-link {
        color: $awt-color-primary;
        text-decoration: none;
        transition: color 0.2s ease;

        &:hover {
            color: $awt-color-accent;
        }
    }

    /* Rating */
    &__rating {
        display: flex;
        align-items: center;
        gap: 0.375rem;
        margin-bottom: 0.5rem;
    }

    &__stars {
        display: flex;
        gap: 2px;
    }

    &__star {
        width: 12px;
        height: 12px;
        color: #d1d5db;

        &.is-awt-filled {
            color: #facc15;
        }
    }

    &__rating-count {
        font-size: 10px;
        color: #9ca3af;
        font-weight: 500;
    }

    /* Author */
    &__author {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        margin-bottom: 0.75rem;
    }

    &__author-icon {
        width: 12px;
        height: 12px;
        color: #9ca3af;
        flex-shrink: 0;
    }

    &__author-name {
        font-size: 0.75rem;
        font-weight: 700;
        color: $awt-color-secondary;
    }

    /* Excerpt */
    &__excerpt {
        font-size: 0.75rem;
        color: $awt-color-secondary;
        line-height: 1.6;
        margin: 0 0 1rem 0;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    /* Actions */
    &__actions {
        margin-top: auto;
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    &__store-links {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    &__store-btn {
        flex: 1;
        min-width: 100px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.5rem;
        background-color: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 4px;
        font-size: 0.75rem;
        font-weight: 700;
        color: $awt-color-primary;
        text-decoration: none;
        transition: all 0.2s ease;

        &:hover {
            background-color: #ffffff;
            border-color: #ff9900;
            color: #ff9900;
        }
    }

    &__btn-group {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        width: 100%;
    }

    &__details-btn {
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.625rem;
        border: 1px solid $awt-color-primary;
        color: $awt-color-primary;
        text-align: center;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        border-radius: 4px;
        text-decoration: none;
        transition: all 0.2s ease;
        line-height: 1.2;

        &:hover {
            background-color: $awt-color-primary;
            color: #ffffff;
        }
    }

    &__no-results {
        grid-column: 1 / -1;
        text-align: center;
        color: $awt-color-secondary;
        padding: 3rem 0;
    }

    /* Pagination */
    &__pagination {
        margin-top: 3.5rem;
        display: flex;
        justify-content: center;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    &__page-btn {
        width: 40px;
        height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 4px;
        background-color: #ffffff;
        border: 1px solid #e2e8f0;
        color: $awt-color-secondary;
        font-weight: 700;
        font-size: 0.875rem;
        cursor: pointer;
        transition: all 0.2s ease;

        svg {
            width: 14px;
            height: 14px;
        }

        &:hover {
            border-color: $awt-color-accent;
            color: $awt-color-accent;
        }

        &.is-awt-active {
            background-color: $awt-color-primary;
            border-color: $awt-color-primary;
            color: #ffffff;
            cursor: default;
        }
    }

    &__load-more-wrap {
        text-align: center;
        width: 100%;
    }

    &__load-more-btn {
        padding: 0.75rem 2rem;
        background-color: #ffffff;
        border: 1px solid #e2e8f0;
        color: $awt-color-primary;
        font-weight: 700;
        font-size: 0.75rem;
        text-transform: uppercase;
        border-radius: 4px;
        box-shadow: $awt-shadow-sm;
        cursor: pointer;
        transition: all 0.2s ease;

        &:hover {
            border-color: $awt-color-accent;
            color: $awt-color-accent;
        }
    }

    /* Scoped WooCommerce Loop Button */
    .rswpbs-book-loop-btn {
        display: inline-block;
        margin: 0;
        position: relative;

        a.button {
            display: inline-flex !important;
            align-items: center;
            justify-content: center;
            gap: 6px;

            background-color: #ea580c !important;
            color: #ffffff !important;
            border: 1px solid #ea580c !important;
            border-radius: 4px !important;

            padding: 0.5rem 1rem !important;
            font-size: 0.75rem !important;
            font-weight: 700 !important;
            text-transform: capitalize;
            text-decoration: none !important;
            line-height: 1.2 !important;
            min-height: 38px;
            transition: all 0.3s ease;

            &:hover {
                filter: brightness(0.9);
                transform: translateY(-1px);
            }

            &.loading {
                opacity: 0.7;
                cursor: wait;
            }

            &::before {
                content: '';
                display: inline-block;
                width: 14px;
                height: 14px;
                background-color: currentColor;
                -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/></svg>') no-repeat center;
                mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/></svg>') no-repeat center;
            }

            &.added::before {
                -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>') no-repeat center;
                mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>') no-repeat center;
            }
        }

        a.added_to_cart.wc-forward {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 10;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #111827;
            color: #ffffff;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 700;
            text-transform: uppercase;
            text-decoration: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
    }
}