#cd-template-catalog.tp-modal {
    --bg: #f5f6f8;
    --surface: #fff;
    --text: #20242b;
    --muted: #737b87;
    --line: #e7e9ed;
    --accent: #FBC048;
    --primary: #FBC048;
    --thumb-bg: #eef0f3;
    --tag-bg: #f5f6f8;
    position: fixed;
    inset: 0;
    z-index: 100001 !important;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    color: var(--text);
    background: var(--bg);
}

/* The admin shell uses a fixed navbar at z-index 99999. Keep the picker and
   its backdrop above that shell while the modal is open. */
.modal-backdrop.show {
    z-index: 100000;
}

.tp-modal .tp-dialog {
    width: 100%;
    max-width: none;
    height: 100%;
    min-height: 0;
    margin: 0;
}

.tp-modal .tp-shell {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
    color: var(--text);
    background: var(--bg);
    border: 0;
    border-radius: 0;
}

.tp-modal .tp-header {
    position: sticky;
    z-index: 2;
    top: 0;
    flex: 0 0 auto;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 5px 18px rgba(20, 27, 38, .045);
}

.tp-header-inner {
    width: min(100%, 1800px);
    margin: 0 auto;
    padding: 22px clamp(18px, 4vw, 64px) 0;
}

.tp-heading-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 18px;
}

.tp-title {
    margin: 0;
    color: var(--text);
    font-size: clamp(1.25rem, 2vw, 1.7rem);
    font-weight: 750;
    letter-spacing: -.035em;
    line-height: 1.2;
}

.tp-subtitle {
    margin: 6px 0 0;
    color: var(--muted);
    font-size: .92rem;
}

.tp-close {
    display: grid;
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    place-items: center;
    padding: 0 0 3px;
    color: var(--muted);
    font-size: 28px;
    line-height: 1;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 50%;
    transition: color .18s ease, background .18s ease, border-color .18s ease;
}

.tp-close:hover,
.tp-close:focus-visible {
    color: var(--text);
    background: var(--tag-bg);
    border-color: #cbd0d8;
}

.tp-search-wrap {
    position: relative;
    width: min(100%, 720px);
    margin: 0 auto 17px;
}

.tp-search-icon {
    position: absolute;
    top: 50%;
    left: 16px;
    width: 19px;
    height: 19px;
    fill: none;
    stroke: var(--muted);
    stroke-linecap: round;
    stroke-width: 1.8;
    transform: translateY(-50%);
    pointer-events: none;
}

.tp-search-input {
    width: 100%;
    height: 50px;
    padding: 0 18px 0 48px;
    color: var(--text);
    font: inherit;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 13px;
    outline: 0;
    box-shadow: 0 2px 8px rgba(25, 33, 44, .035);
    transition: border-color .18s ease, box-shadow .18s ease;
}

.tp-search-input::placeholder { color: #9299a3; }
.tp-search-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 14%, transparent);
}

.tp-category-strip {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 0 0 14px;
    scrollbar-width: thin;
    scrollbar-color: var(--line) transparent;
}

.tp-category-chip {
    flex: 0 0 auto;
    min-height: 34px;
    padding: 7px 14px;
    color: var(--muted);
    font: inherit;
    font-size: .82rem;
    font-weight: 650;
    white-space: nowrap;
    background: transparent;
    border: 1px solid var(--line);
    border-radius: 999px;
    transition: color .15s ease, background .15s ease, border-color .15s ease;
}

.tp-category-chip:hover { color: var(--text); background: var(--tag-bg); }
.tp-category-chip.is-active {
    color: #24200f;
    background: var(--accent);
    border-color: var(--accent);
}

.tp-scroll-area {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 20px clamp(14px, 4vw, 64px) 32px;
    background: var(--bg);
}

.tp-results-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: min(100%, 1800px);
    min-height: 28px;
    margin: 0 auto 14px;
}

.tp-result-count,
.tp-status {
    margin: 0;
    color: var(--muted);
    font-size: .84rem;
}

.tp-result-count { font-weight: 650; color: var(--text); }
.tp-status { text-align: right; }

.tp-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 18px;
    width: min(100%, 1800px);
    margin: 0 auto;
}

.tp-card {
    display: flex;
    min-width: 0;
    flex-direction: column;
    overflow: hidden;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 2px 8px rgba(23, 31, 43, .035);
    transition: transform .2s ease, box-shadow .2s ease, border-color .2s ease;
}

.tp-card:hover,
.tp-card:focus-within {
    z-index: 1;
    border-color: #d7dce4;
    box-shadow: 0 12px 30px rgba(23, 31, 43, .11);
    transform: translateY(-3px);
}

.tp-thumb {
    position: relative;
    display: grid;
    flex: 0 0 auto;
    overflow: hidden;
    aspect-ratio: 4 / 5;
    place-items: center;
    background: var(--thumb-bg);
}

.tp-thumb img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.tp-thumb-fallback {
    color: var(--muted);
    font-size: .8rem;
}

.tp-category-badge {
    position: absolute;
    top: 12px;
    left: 12px;
    max-width: calc(100% - 24px);
    overflow: hidden;
    padding: 6px 10px;
    color: #fff;
    font-size: .7rem;
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
    background: rgba(25, 29, 36, .76);
    border: 1px solid rgba(255, 255, 255, .16);
    border-radius: 999px;
    backdrop-filter: blur(8px);
}

.tp-use-template {
    position: absolute;
    right: 12px;
    bottom: 12px;
    left: 12px;
    min-height: 42px;
    color: #fff;
    font: inherit;
    font-size: .84rem;
    font-weight: 700;
    background: var(--primary);
    border: 0;
    border-radius: 11px;
    box-shadow: 0 4px 14px rgba(15, 31, 58, .2);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity .16s ease, transform .16s ease, background .16s ease;
}

.tp-card:hover .tp-use-template,
.tp-card:focus-within .tp-use-template { opacity: 1; transform: translateY(0); }
.tp-use-template:hover { color: #fff; background: #FBC048; }

.tp-card-body {
    display: flex;
    flex: 1 1 auto;
    min-width: 0;
    flex-direction: column;
    padding: 14px 15px 15px;
}

.tp-card-title {
    display: -webkit-box;
    min-height: 2.8em;
    overflow: hidden;
    margin: 0;
    color: var(--text);
    font-size: .98rem;
    font-weight: 720;
    line-height: 1.4;
    overflow-wrap: anywhere;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

.tp-card-summary {
    display: -webkit-box;
    min-height: 2.7em;
    overflow: hidden;
    margin: 7px 0 12px;
    color: var(--muted);
    font-size: .82rem;
    line-height: 1.4;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

.tp-card-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: auto;
}

.tp-tag,
.tp-tag-more {
    max-width: 100%;
    overflow: hidden;
    padding: 5px 9px;
    color: var(--muted);
    font: inherit;
    font-size: .7rem;
    line-height: 1.1;
    text-overflow: ellipsis;
    white-space: nowrap;
    background: var(--tag-bg);
    border: 1px solid var(--line);
    border-radius: 999px;
}

.tp-tag:hover,
.tp-tag:focus-visible { color: var(--text); border-color: var(--accent); }
.tp-tag-more { color: var(--muted); }

.tp-empty {
    width: min(100%, 420px);
    margin: clamp(48px, 12vh, 120px) auto;
    padding: 28px;
    color: var(--text);
    text-align: center;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 18px;
}

.tp-empty-icon { display: block; margin-bottom: 12px; color: var(--muted); font-size: 3rem; line-height: 1; }
.tp-empty h3 { margin: 0; font-size: 1.05rem; font-weight: 700; }
.tp-empty p { margin: 6px 0 0; color: var(--muted); font-size: .88rem; }

.tp-more-wrap { display: flex; justify-content: center; padding: 24px 0 8px; }
.tp-more {
    min-height: 42px;
    padding: 9px 18px;
    color: var(--text);
    font: inherit;
    font-size: .84rem;
    font-weight: 650;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 999px;
}
.tp-more:hover { background: var(--tag-bg); border-color: #c9ced7; }
.tp-more:disabled { cursor: wait; opacity: .6; }

.tp-skeleton {
    overflow: hidden;
    background: linear-gradient(100deg, var(--thumb-bg) 25%, color-mix(in srgb, var(--thumb-bg) 50%, var(--surface)) 40%, var(--thumb-bg) 55%);
    background-size: 220% 100%;
    animation: tp-shimmer 1.25s ease-in-out infinite;
}
.tp-skeleton-thumb { aspect-ratio: 4 / 5; }
.tp-skeleton-line { height: 13px; margin: 0 0 10px; border-radius: 6px; }
.tp-skeleton-line--short { width: 62%; }
@keyframes tp-shimmer { to { background-position-x: -220%; } }

.tp-modal :focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 55%, transparent); outline-offset: 2px; }
.tp-modal .tp-search-input:focus-visible { outline: 0; }

@media (max-width: 600px) {
    .tp-header-inner { padding: 16px 14px 0; }
    .tp-heading-row { gap: 12px; margin-bottom: 14px; }
    .tp-subtitle { max-width: 32ch; font-size: .82rem; }
    .tp-close { flex-basis: 36px; width: 36px; height: 36px; }
    .tp-search-wrap { margin-bottom: 13px; }
    .tp-search-input { height: 46px; }
    .tp-scroll-area { padding: 14px 14px 24px; }
    .tp-results-toolbar { margin-bottom: 10px; }
    .tp-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
    .tp-card { border-radius: 13px; }
    .tp-category-badge { top: 7px; left: 7px; max-width: calc(100% - 14px); padding: 5px 7px; font-size: .62rem; }
    .tp-card-body { padding: 10px; }
    .tp-card-title { min-height: 2.65em; font-size: .86rem; }
    .tp-card-summary { min-height: 2.55em; margin: 5px 0 9px; font-size: .74rem; }
    .tp-tag { padding: 4px 7px; font-size: .64rem; }
    .tp-tag-more { padding: 4px 7px; font-size: .64rem; }
    .tp-use-template { right: 7px; bottom: 7px; left: 7px; min-height: 36px; font-size: .72rem; }
}

@media (hover: none) {
    .tp-use-template { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
    .tp-modal *, .tp-modal *::before, .tp-modal *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

@media (prefers-color-scheme: dark) {
    .tp-modal {
        --bg: #14171c;
        --surface: #1d2229;
        --text: #eef1f5;
        --muted: #a1aab6;
        --line: #343b45;
        --accent: #f2b705;
        --primary: #3979d1;
        --thumb-bg: #292f38;
        --tag-bg: #252b33;
    }
    .tp-category-chip.is-active { color: #201b0c; }
    .tp-close:hover, .tp-close:focus-visible { border-color: #596372; }
    .tp-card:hover, .tp-card:focus-within { border-color: #4a5564; }
    .tp-search-input::placeholder { color: #8d98a5; }
}
