.template-editor-page {
    --template-border: color-mix(in srgb, var(--md-outline, #777680) 45%, transparent);
    --template-surface: var(--md-surface-container-low, #f7f2fa);
    --template-surface-high: var(--md-surface-container-high, #ece6f0);
    width: 100%;
    padding: clamp(0.75rem, 2vw, 1.75rem) 0 clamp(2rem, 5vw, 4rem);
    color: var(--md-on-surface, #1d1b20);
}

.template-editor-hero,
.template-picker,
.template-workspace,
.template-editor-empty {
    width: 100%;
    border: 1px solid var(--template-border);
    border-radius: clamp(1.25rem, 2vw, 2rem);
    background: var(--md-surface, #fff);
    box-shadow: 0 12px 32px rgba(31, 28, 43, 0.08);
}

.template-editor-hero {
    position: relative;
    overflow: hidden;
    padding: clamp(1.5rem, 4vw, 3.5rem);
    background:
        radial-gradient(circle at 92% 18%, rgba(255, 183, 197, 0.55) 0 8rem, transparent 8.1rem),
        linear-gradient(135deg, var(--md-secondary-container, #e8def8), var(--md-primary-container, #eaddff));
}

.template-editor-hero::after {
    position: absolute;
    right: 7%;
    bottom: -4rem;
    width: 10rem;
    height: 10rem;
    border: 2rem solid rgba(73, 69, 79, 0.08);
    border-radius: 50%;
    content: "";
}

.template-editor-hero h1 {
    position: relative;
    z-index: 1;
    max-width: 900px;
    margin: 0;
    font-size: clamp(2rem, 5vw, 4.4rem);
    font-weight: 800;
    letter-spacing: -0.045em;
}

.template-editor-hero > p:not(.gallery-eyebrow) {
    position: relative;
    z-index: 1;
    max-width: 900px;
    margin: 1rem 0 0;
    font-size: clamp(1rem, 1.4vw, 1.2rem);
}

.template-editor-hero__limits {
    position: relative;
    z-index: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem;
    margin-top: 1.5rem;
}

.template-editor-hero__limits span,
.template-workspace__counts span {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 38px;
    padding: 0.45rem 0.85rem;
    border: 1px solid rgba(73, 69, 79, 0.16);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.66);
    font-size: 0.88rem;
    font-weight: 700;
    backdrop-filter: blur(8px);
}

.template-picker,
.template-workspace {
    margin-top: clamp(1rem, 2vw, 1.5rem);
    padding: clamp(1rem, 2.5vw, 2rem);
}

.template-picker__heading,
.template-workspace__heading,
.template-canvas-panel__toolbar,
.template-canvas-panel__footer,
.template-layers__heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.template-picker__heading h2,
.template-workspace__heading h2 {
    margin: 0;
    font-size: clamp(1.45rem, 2.5vw, 2.25rem);
    font-weight: 800;
    letter-spacing: -0.025em;
}

.template-picker__heading p:last-child {
    margin: 0.35rem 0 0;
    color: var(--md-on-surface-variant, #49454f);
}

.template-picker__controls,
.template-workspace__counts {
    display: flex;
    gap: 0.5rem;
}

.template-picker__arrow {
    display: inline-grid;
    width: 42px;
    height: 42px;
    place-items: center;
    border: 1px solid var(--template-border);
    border-radius: 50%;
    background: var(--md-surface, #fff);
    color: var(--md-primary, #6750a4);
}

.template-picker__arrow:hover {
    border-color: transparent;
    background: var(--md-primary-container, #eaddff);
}

.template-picker__rail {
    display: grid;
    grid-auto-columns: minmax(160px, 210px);
    grid-auto-flow: column;
    gap: 0.9rem;
    margin-top: 1.25rem;
    padding: 0.25rem 0.1rem 0.75rem;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scroll-snap-type: inline mandatory;
    scrollbar-width: thin;
}

.template-picker__item {
    position: relative;
    display: grid;
    gap: 0.45rem;
    padding: 0.6rem;
    border: 2px solid transparent;
    border-radius: 1.25rem;
    background: var(--template-surface);
    color: inherit;
    text-align: left;
    scroll-snap-align: start;
    transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

.template-picker__item:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 22px rgba(31, 28, 43, 0.12);
}

.template-picker__item.is-selected {
    border-color: var(--md-primary, #6750a4);
    background: var(--md-primary-container, #eaddff);
}

.template-picker__preview {
    position: relative;
    display: block;
    overflow: hidden;
    aspect-ratio: 4 / 3;
    border-radius: 0.85rem;
    background: var(--template-surface-high);
}

.template-picker__preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.template-picker__selected-mark {
    position: absolute;
    top: 0.55rem;
    right: 0.55rem;
    display: none;
    width: 30px;
    height: 30px;
    place-items: center;
    border-radius: 50%;
    background: var(--md-primary, #6750a4);
    color: var(--md-on-primary, #fff);
}

.template-picker__item.is-selected .template-picker__selected-mark {
    display: grid;
}

.template-picker__name {
    overflow: hidden;
    font-weight: 800;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.template-picker__apply {
    color: var(--md-primary, #6750a4);
    font-size: 0.78rem;
    font-weight: 700;
}

.template-editor-message {
    margin: 1rem 0;
    padding: 0.8rem 1rem;
    border-radius: 0.85rem;
    background: var(--md-error-container, #f9dedc);
    color: var(--md-on-error-container, #410e0b);
}

.template-workspace__layout {
    display: grid;
    grid-template-columns: minmax(210px, 260px) minmax(0, 1fr) minmax(190px, 230px);
    gap: 1rem;
    margin-top: 1.25rem;
    align-items: start;
}

.template-tools,
.template-layers {
    display: grid;
    gap: 0.85rem;
}

.template-tool-card,
.template-layers,
.template-canvas-panel {
    border: 1px solid var(--template-border);
    border-radius: 1.15rem;
    background: var(--template-surface);
}

.template-tool-card,
.template-layers {
    padding: 1rem;
}

.template-tool-card h3,
.template-layers h3 {
    margin: 0 0 0.8rem;
    font-size: 1rem;
    font-weight: 800;
}

.template-tool-card label {
    display: grid;
    gap: 0.35rem;
    margin-bottom: 0.75rem;
    color: var(--md-on-surface-variant, #49454f);
    font-size: 0.82rem;
    font-weight: 700;
}

.template-tool-card textarea,
.template-tool-card input[type="number"],
.template-tool-card input[type="color"],
.template-tool-card select {
    width: 100%;
    min-height: 42px;
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--template-border);
    border-radius: 0.7rem;
    background: var(--md-surface, #fff);
    color: inherit;
}

.template-tool-card textarea {
    resize: vertical;
}

.template-tool-card input[type="range"] {
    width: 100%;
    accent-color: var(--md-primary, #6750a4);
}

.template-tool-card input[type="range"]:disabled {
    opacity: 0.45;
}

.template-crop-mode {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    width: 100%;
    min-height: 42px;
    margin-bottom: 0.9rem;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--md-primary, #6750a4);
    border-radius: 999px;
    background: transparent;
    color: var(--md-primary, #6750a4);
    font-weight: 750;
}

.template-crop-mode.is-active {
    border-color: transparent;
    background: var(--md-primary-container, #eaddff);
}

.template-crop-mode:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.template-action,
.template-download-button,
.template-clear-button,
.template-layer-actions button,
.template-inline-actions button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 42px;
    border-radius: 999px;
    font-weight: 750;
}

.template-action {
    width: 100%;
    margin-top: 0.45rem;
    border: 1px solid transparent;
}

.template-action--primary,
.template-download-button {
    background: var(--md-primary, #6750a4);
    color: var(--md-on-primary, #fff);
}

.template-action--secondary {
    border-color: var(--md-primary, #6750a4);
    background: transparent;
    color: var(--md-primary, #6750a4);
}

.template-tool-card small {
    display: block;
    margin-top: 0.65rem;
    color: var(--md-on-surface-variant, #49454f);
    font-size: 0.73rem;
}

.template-format-grid {
    display: grid;
    grid-template-columns: 1fr 0.75fr;
    gap: 0.6rem;
}

.template-format-grid__wide {
    grid-column: 1 / -1;
}

.template-inline-actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.35rem;
}

.template-inline-actions button {
    min-height: 38px;
    border: 1px solid var(--template-border);
    background: var(--md-surface, #fff);
    color: var(--md-primary, #6750a4);
}

.template-inline-actions button.is-active {
    border-color: transparent;
    background: var(--md-primary-container, #eaddff);
}

.template-layer-actions {
    display: grid;
    gap: 0.45rem;
}

.template-layer-actions button {
    justify-content: flex-start;
    padding: 0.5rem 0.8rem;
    border: 1px solid var(--template-border);
    background: var(--md-surface, #fff);
    color: inherit;
}

.template-layer-actions__delete {
    color: var(--md-error, #b3261e) !important;
}

.template-canvas-panel {
    min-width: 0;
    overflow: hidden;
    background: var(--md-surface, #fff);
}

.template-canvas-panel__toolbar,
.template-canvas-panel__footer {
    padding: 0.85rem 1rem;
}

.template-canvas-panel__toolbar small {
    display: block;
    color: var(--md-on-surface-variant, #49454f);
}

.template-clear-button {
    min-height: 36px;
    padding: 0.35rem 0.75rem;
    border: 1px solid var(--template-border);
    background: transparent;
    color: var(--md-primary, #6750a4);
    font-size: 0.82rem;
}

.template-canvas-shell {
    position: relative;
    display: grid;
    min-height: 0;
    padding: 0;
    place-items: center;
    overflow: hidden;
    border-block: 1px solid var(--template-border);
    background:
        linear-gradient(45deg, #e8e3eb 25%, transparent 25%),
        linear-gradient(-45deg, #e8e3eb 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #e8e3eb 75%),
        linear-gradient(-45deg, transparent 75%, #e8e3eb 75%);
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
    background-size: 20px 20px;
}

.template-canvas-shell .canvas-container {
    box-shadow: 0 12px 34px rgba(31, 28, 43, 0.2);
}

.template-canvas-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.65rem;
    background: rgba(247, 242, 250, 0.82);
    font-weight: 800;
    backdrop-filter: blur(4px);
}

.template-canvas-loading[hidden] {
    display: none;
}

.template-canvas-loading span {
    width: 1.25rem;
    height: 1.25rem;
    border: 3px solid var(--md-primary-container, #eaddff);
    border-top-color: var(--md-primary, #6750a4);
    border-radius: 50%;
    animation: template-spin 0.75s linear infinite;
}

@keyframes template-spin {
    to { transform: rotate(360deg); }
}

.template-canvas-panel__footer p {
    margin: 0;
    color: var(--md-on-surface-variant, #49454f);
    font-size: 0.8rem;
}

.template-download-button {
    min-height: 46px;
    padding: 0.65rem 1.1rem;
    border: 0;
}

.template-layers__heading {
    align-items: baseline;
}

.template-layers__heading h3 {
    margin: 0;
}

.template-layers__heading small {
    color: var(--md-on-surface-variant, #49454f);
}

.template-layer-list {
    display: grid;
    gap: 0.45rem;
    margin-top: 0.85rem;
}

.template-layer-item {
    display: grid;
    grid-template-columns: 32px minmax(0, 1fr) 30px;
    align-items: center;
    gap: 0.45rem;
    width: 100%;
    min-height: 46px;
    padding: 0.35rem;
    border: 1px solid var(--template-border);
    border-radius: 0.75rem;
    background: var(--md-surface, #fff);
    overflow: hidden;
}

.template-layer-item.is-selected {
    border-color: var(--md-primary, #6750a4);
    background: var(--md-primary-container, #eaddff);
}

.template-layer-item__select,
.template-layer-item__delete {
    display: grid;
    border: 0;
    background: transparent;
    color: inherit;
    place-items: center;
}

.template-layer-item__select {
    grid-column: 1 / 3;
    grid-template-columns: 32px minmax(0, 1fr);
    gap: 0.45rem;
    text-align: left;
    min-width: 0;
    overflow: hidden;
}

.template-layer-item__select span:last-child {
    display: block;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.template-layer-item__delete {
    color: var(--md-error, #b3261e);
}

.template-layer-empty {
    margin-top: 0.85rem;
    padding: 1rem 0.5rem;
    color: var(--md-on-surface-variant, #49454f);
    font-size: 0.82rem;
    text-align: center;
}

.template-editor-empty {
    display: grid;
    margin-top: 1.25rem;
    padding: 4rem 1.5rem;
    place-items: center;
    text-align: center;
}

.template-editor-empty > i {
    margin-bottom: 1rem;
    color: var(--md-primary, #6750a4);
    font-size: 3rem;
}

.template-editor-empty h2,
.template-editor-empty p {
    margin: 0.35rem 0;
}

@media (max-width: 1180px) {
    .template-workspace__layout {
        grid-template-columns: minmax(210px, 250px) minmax(0, 1fr);
    }

    .template-layers {
        grid-column: 1 / -1;
    }
}

@media (max-width: 760px) {
    .template-picker__heading,
    .template-workspace__heading,
    .template-canvas-panel__toolbar,
    .template-canvas-panel__footer {
        align-items: flex-start;
        flex-direction: column;
    }

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

    .template-canvas-panel {
        order: -1;
    }

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

    .template-canvas-shell {
        min-height: 0;
        padding: 0;
    }

    .template-download-button {
        width: 100%;
    }
}

@media (prefers-reduced-motion: reduce) {
    .template-picker__item,
    .template-canvas-loading span {
        animation: none;
        transition: none;
    }
}
