/* Templates Grid */
.templates-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-xl);
}

.template-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    background: transparent;
    border: none;
    padding: 0;
    overflow: visible;
}

.template-card-img-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 4/5;
    border-radius: var(--radius-xl);
    overflow: hidden;
    background-color: var(--color-surface);
    cursor: pointer;
}

.template-grid-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top center;
    display: block;
    transition: transform 0.5s ease-in-out;
}


.display-preview-overlay {
    position: absolute;
    inset: 0;
    background: transparent;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    padding: var(--space-md);
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 5;
}

.display-preview-overlay .btn {
    transform: translateY(15px);
    transition: transform 0.3s ease, background 0.2s, color 0.2s;
    pointer-events: none;
}

.template-card-img-wrapper:hover .display-preview-overlay {
    opacity: 1;
}

.template-card-img-wrapper:hover .display-preview-overlay .btn {
    transform: translateY(0);
    pointer-events: auto;
}

.template-grid-label {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    color: var(--color-text);
    font-size: var(--text-lg);
    font-weight: 500;
}

.template-grid-name {
    font-weight: 600;
    line-height: 1.2;
}

.template-grid-tags {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: var(--text-sm);
    font-weight: 400;
    color: var(--color-text-muted, #9a9a9a);
}

.theme-light .template-grid-label {
    color: #000;
}

.theme-light .template-grid-tags {
    color: #5a5a5a;
}

/* Inline colour chip in the label (skin --bg-default + --primary-fill-default accent
   corner) — kept OFF the screenshot so the preview stays clean. */
.template-swatch {
    display: inline-block;
    width: 15px;
    height: 15px;
    border-radius: 4px;
    background: var(--sw-bg, #1a1a1a);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25), inset 0 0 0 1px rgba(255, 255, 255, 0.18);
    position: relative;
    overflow: hidden;
    flex: none;
    pointer-events: none;
}

.template-swatch::after {
    content: "";
    position: absolute;
    right: 0;
    bottom: 0;
    width: 62%;
    height: 62%;
    background: var(--sw-accent, #8a8a8a);
    clip-path: polygon(100% 0, 100% 100%, 0 100%);
}

/* Empty state + active-filter count */
.templates-empty {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--color-text-muted, #9a9a9a);
    font-size: var(--text-lg);
    padding: var(--space-3xl) 0;
    margin: 0;
}

.templates-count {
    color: var(--color-text-muted, #9a9a9a);
    font-weight: 400;
}

@media (max-width: 1024px) {
    .templates-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

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

/* Template Filters Bar */
.templates-filter-container {
    margin-bottom: var(--space-xl);
    position: relative;
    z-index: 20;
}

.templates-filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-md);
    padding-bottom: var(--space-md);
    border-bottom: 1px solid var(--color-border);
}

.templates-filter-left {
    display: flex;
    align-items: center;
    gap: var(--space-xl);
}

.templates-filter-right {
    display: flex;
    align-items: center;
    gap: var(--space-lg);
}

.filter-dropdown {
    position: relative;
}

.filter-dropdown-btn {
    background: transparent;
    border: none;
    font-family: inherit;
    font-size: var(--text-lg);
    font-weight: 500;
    color: var(--color-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: var(--space-xs) 0;
    transition: color 0.2s ease;
}

.filter-dropdown-btn:hover,
.filter-dropdown-btn.has-selection {
    color: var(--color-primary);
}

.filter-tab {
    text-decoration: none;
    color: var(--color-text-secondary);
    font-size: var(--text-lg);
    transition: color var(--transition-base);
}

.filter-tab:hover,
.filter-tab.active {
    color: var(--color-text);
}

.filter-dropdown {
    position: relative;
}

.filter-dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    transform: translateY(15px);
    min-width: 250px;
    background: var(--color-surface-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-1xl);
    padding: var(--space-sm);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition-base);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-top: var(--space-xs);
    list-style: none;
}

/* Invisible bridge to prevent menu from closing when moving mouse */
.filter-dropdown::after {
    content: "";
    position: absolute;
    bottom: -15px;
    left: 0;
    width: 100%;
    height: 15px;
    background: transparent;
}

.filter-dropdown.active .filter-dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.filter-dropdown-item {
    padding: var(--space-md) var(--space-lg);
    display: block;
    font-size: var(--text-base);
    color: var(--color-text-secondary);
    border-radius: var(--radius-xl);
    white-space: nowrap;
    text-align: left;
    transition: background var(--transition-base), color var(--transition-base);
    border: none;
    background: none;
    width: 100%;
    cursor: pointer;
    font-family: inherit;
}

.filter-dropdown-item:hover {
    background: var(--color-surface);
    color: var(--color-text);
}

/* Category icons inside Collection dropdown items */
.filter-dropdown-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.filter-item-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    object-fit: contain;
}

/* Category icon inside the active filter h2 heading */
.filter-label-icon {
    width: 1em;
    height: 1em;
    vertical-align: middle;
    margin-right: 0.2em;
    object-fit: contain;
    position: relative;
    top: -0.05em;
}

/* ---- Mobile: keep the four filters tidy and on-screen ---------------------- */
@media (max-width: 768px) {
    .templates-filter-bar {
        gap: var(--space-sm);
    }

    /* Lay the four filters out as a 2x2 grid of full-width chips instead of a
       single overflowing row (stops "Art style" wrapping to two lines). */
    .templates-filter-left {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: var(--space-sm) var(--space-md);
        width: 100%;
    }

    .filter-dropdown-btn {
        width: 100%;
        justify-content: space-between;
        white-space: nowrap;
        font-size: var(--text-base);
        padding: var(--space-sm) var(--space-md);
        border: 1px solid var(--color-border);
        border-radius: var(--radius-xl);
        background: var(--color-surface);
    }

    /* Clamp the menu to the viewport and let long option labels wrap so the
       panel is never cut off at the screen edge. */
    .filter-dropdown-menu {
        min-width: 0;
        width: min(72vw, 240px);
        max-width: min(72vw, 240px);
    }

    .filter-dropdown-item {
        white-space: normal;
    }

    /* Right-hand column opens leftward so it stays inside the viewport. */
    .templates-filter-left .filter-dropdown:nth-child(even) .filter-dropdown-menu {
        left: auto;
        right: 0;
    }
}