body.template-library-body {
    overflow: hidden;
}

body.template-modal-open {
    overflow: hidden;
}

.template-library-shell {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: 210px minmax(0, 1fr);
    gap: var(--spacing-md);
    padding: var(--spacing-md);
    background: var(--bg-primary);
    overflow: hidden;
}

.template-filter-panel,
.template-gallery-panel,
.template-modal-card {
    background: var(--glass-bg);
    border: 1px solid var(--border-color);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
}

.template-filter-panel {
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    overflow-y: auto;
}

.template-filter-header {
    padding-bottom: var(--spacing-md);
    border-bottom: 1px solid var(--border-color);
    margin-bottom: var(--spacing-md);
}

.template-filter-header h1 {
    font-size: 1.35rem;
    line-height: 1.2;
    margin-bottom: var(--spacing-xs);
}

.template-filter-header p {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.template-filter-group {
    margin-bottom: var(--spacing-lg);
}

.template-filter-title {
    font-size: 0.75rem;
    color: var(--accent-purple-light);
    font-weight: 700;
    margin-bottom: var(--spacing-sm);
}

.template-category-btn,
.template-filter-chip {
    width: 100%;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-sm);
    min-height: 34px;
    padding: 8px 10px;
    margin-bottom: 6px;
    font-size: 0.82rem;
    text-align: left;
    transition: all var(--transition-fast);
}

.template-category-btn:hover,
.template-filter-chip:hover,
.template-category-btn.active,
.template-filter-chip.active {
    color: var(--text-primary);
    background: rgba(14, 165, 233, 0.14);
    border-color: rgba(20, 184, 166, 0.24);
}

.template-category-count {
    color: var(--text-muted);
    font-size: 0.72rem;
    flex-shrink: 0;
}

.template-gallery-panel {
    min-width: 0;
    min-height: 0;
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    overflow: hidden;
}

.template-toolbar {
    display: grid;
    grid-template-columns: minmax(260px, 1fr) 156px auto;
    gap: var(--spacing-sm);
    align-items: center;
}

.template-search-wrap {
    position: relative;
}

.template-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
}

.template-search,
.template-select {
    width: 100%;
    height: 40px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.86rem;
    outline: none;
}

.template-search {
    padding: 0 12px 0 34px;
}

.template-select {
    padding: 0 10px;
}

.template-search:focus,
.template-select:focus {
    border-color: var(--border-glow);
    box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.12);
}

.template-count {
    color: var(--text-muted);
    font-size: 0.8rem;
    white-space: nowrap;
}

.template-quick-tags {
    display: flex;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 2px;
}

.template-quick-tag {
    height: 32px;
    padding: 0 14px;
    border-radius: 999px;
    border: 1px solid rgba(14, 165, 233, 0.2);
    background: rgba(14, 165, 233, 0.08);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.8rem;
    white-space: nowrap;
    transition: all var(--transition-fast);
}

.template-quick-tag:hover,
.template-quick-tag.active {
    background: var(--accent-gradient);
    color: #fff;
    border-color: transparent;
}

.template-grid {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    gap: 18px;
    align-content: start;
    padding: 2px 4px 4px 2px;
}

.template-card {
    min-width: 0;
    min-height: 276px;
    border: 1px solid rgba(14, 165, 233, 0.18);
    border-radius: var(--radius-sm);
    background: rgba(10, 18, 24, 0.9);
    overflow: hidden;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    position: relative;
    transition: transform var(--transition-fast), border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.template-card:hover,
.template-card:focus-visible {
    transform: translateY(-3px);
    border-color: var(--border-light);
    box-shadow: 0 14px 30px rgba(0, 0, 0, 0.34);
    outline: none;
}

.template-card-media {
    position: relative;
    width: 100%;
    aspect-ratio: 4 / 3;
    background: var(--bg-tertiary);
    overflow: hidden;
}

.template-card-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.template-card-badge {
    position: absolute;
    left: 10px;
    top: 10px;
    max-width: calc(100% - 20px);
    height: 24px;
    padding: 0 9px;
    border-radius: 999px;
    border: 1px solid rgba(20, 184, 166, 0.25);
    background: rgba(7, 18, 22, 0.82);
    color: #a6fbef;
    font-size: 0.68rem;
    display: flex;
    align-items: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    backdrop-filter: blur(8px);
}

.template-card-body {
    min-height: 82px;
    padding: 12px 13px;
    background: rgba(7, 18, 22, 0.98);
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 9px;
}

.template-card-title {
    color: var(--text-primary);
    font-size: 0.92rem;
    font-weight: 700;
    line-height: 1.25;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.template-card-meta {
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-size: 0.72rem;
    min-width: 0;
}

.template-card-meta span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.template-empty {
    grid-column: 1 / -1;
    min-height: 260px;
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
}

.template-pagination {
    flex-shrink: 0;
    min-height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding-top: 4px;
}

.template-page-numbers {
    display: flex;
    align-items: center;
    gap: 6px;
}

.template-page-btn {
    min-width: 36px;
    height: 34px;
    padding: 0 11px;
    border: 1px solid rgba(14, 165, 233, 0.2);
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 600;
    transition: all var(--transition-fast);
}

.template-page-btn:hover:not(:disabled),
.template-page-btn.active {
    color: #fff;
    border-color: transparent;
    background: var(--accent-gradient);
}

.template-page-btn:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.template-page-arrow {
    min-width: 72px;
}

.template-page-ellipsis,
.template-page-summary {
    color: var(--text-muted);
    font-size: 0.78rem;
}

.template-page-ellipsis {
    padding: 0 2px;
}

.template-primary-action,
.template-secondary-action {
    border: none;
    min-height: 38px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.86rem;
    font-weight: 700;
    transition: all var(--transition-fast);
}

.template-primary-action {
    background: var(--accent-gradient);
    color: #fff;
}

.template-primary-action:hover {
    background: var(--accent-gradient-hover);
}

.template-secondary-action {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

.template-secondary-action:hover {
    color: var(--text-primary);
    border-color: var(--border-light);
}

.template-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 28px;
}

.template-modal[hidden] {
    display: none !important;
}

.template-modal-backdrop {
    position: absolute;
    inset: 0;
    border: none;
    background: rgba(3, 7, 10, 0.78);
    cursor: pointer;
}

.template-modal-card {
    position: relative;
    width: min(980px, calc(100vw - 40px));
    max-height: min(760px, calc(100vh - 48px));
    border-radius: var(--radius-lg);
    display: grid;
    grid-template-columns: minmax(320px, 1.05fr) minmax(300px, 0.95fr);
    overflow: hidden;
    box-shadow: 0 28px 80px rgba(0, 0, 0, 0.58);
}

.template-modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 2;
    width: 34px;
    height: 34px;
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-radius: 999px;
    background: rgba(7, 18, 22, 0.82);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.25rem;
    line-height: 1;
}

.template-modal-media {
    min-height: 420px;
    background: var(--bg-tertiary);
}

.template-modal-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.template-modal-copy {
    min-width: 0;
    padding: 28px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    overflow-y: auto;
}

.template-modal-kicker {
    color: var(--accent-purple-light);
    font-size: 0.78rem;
    line-height: 1.35;
    padding-right: 40px;
}

.template-modal-copy h2 {
    font-size: 1.35rem;
    line-height: 1.22;
}

.template-modal-copy p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.6;
}

.template-modal-tags {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.template-modal-tag {
    font-size: 0.72rem;
    color: #a6fbef;
    border: 1px solid rgba(20, 184, 166, 0.22);
    background: rgba(20, 184, 166, 0.08);
    border-radius: 999px;
    padding: 3px 8px;
}

.template-modal-actions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 9px;
    margin-top: auto;
}

@media (min-width: 1500px) {
    .template-grid {
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    }
}

@media (max-width: 1180px) {
    .template-library-shell {
        grid-template-columns: 180px minmax(0, 1fr);
    }

    .template-grid {
        grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    }
}

@media (max-width: 900px) {
    .template-modal-card {
        grid-template-columns: 1fr;
        width: min(680px, calc(100vw - 28px));
    }

    .template-modal-media {
        min-height: 300px;
        max-height: 42vh;
    }
}

@media (max-width: 768px) {
    body.template-library-body {
        overflow: auto;
    }

    .template-library-shell {
        display: flex;
        flex-direction: column;
        height: auto;
        overflow: visible;
        padding: var(--spacing-sm);
    }

    .template-filter-panel {
        border-radius: var(--radius-lg);
        overflow: visible;
    }

    .template-category-list,
    .template-filter-group:nth-of-type(2) {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .template-category-btn,
    .template-filter-chip {
        margin-bottom: 0;
        min-height: 32px;
        justify-content: center;
    }

    .template-gallery-panel {
        border-radius: var(--radius-lg);
        overflow: visible;
        min-height: 0;
    }

    .template-toolbar {
        grid-template-columns: 1fr;
    }

    .template-grid {
        overflow: visible;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 12px;
    }

    .template-card {
        min-height: 220px;
    }

    .template-card-body {
        min-height: 76px;
        padding: 10px;
    }

    .template-card-title {
        font-size: 0.82rem;
    }

    .template-card-meta {
        font-size: 0.66rem;
    }

    .template-pagination {
        flex-wrap: wrap;
    }

    .template-page-summary {
        width: 100%;
        text-align: center;
    }

    .template-modal {
        padding: 12px;
        align-items: flex-start;
        overflow-y: auto;
    }

    .template-modal-card {
        width: 100%;
        max-height: none;
    }

    .template-modal-media {
        min-height: 240px;
        max-height: none;
    }

    .template-modal-copy {
        padding: 20px;
    }
}
