.templates-page {
    display: grid;
    gap: 1.25rem;
    padding: 0 1.25rem 2rem;
}

.templates-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
    padding: 0.5rem 0;
}

.templates-heading h1,
.templates-heading p {
    margin: 0;
}

.templates-heading h1 {
    margin: 0;
}

.template-partial-upload {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.65rem;
    margin-top: 0.7rem;
}

.template-partial-upload small {
    color: var(--muted);
}

.templates-heading p {
    max-width: 58rem;
    margin-top: 0.4rem;
    color: var(--muted);
    line-height: 1.5;
}

.coverage-summary {
    display: grid;
    min-width: 9rem;
    padding: 0.9rem 1.1rem;
    border-radius: 12px;
    background: var(--accent-soft);
    color: #1741a6;
    text-align: center;
}

.coverage-summary strong {
    font-size: 1.45rem;
}

.coverage-summary span {
    font-size: 0.78rem;
    font-weight: 700;
}

.coverage-summary small {
    margin-top: 0.2rem;
    color: var(--muted);
    font-size: 0.7rem;
}

.template-message {
    padding: 0.9rem 1rem;
    border: 1px solid;
    border-radius: 10px;
    font-weight: 700;
}

.template-message.success {
    border-color: #86efac;
    background: var(--success-soft);
    color: var(--success);
}

.template-message.error {
    border-color: #fecaca;
    background: var(--danger-soft);
    color: var(--danger);
}

.template-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    color: var(--muted);
    font-size: 0.86rem;
}

.template-legend span {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.template-note {
    padding: 0.85rem 1rem;
    border: 1px solid #bfdbfe;
    border-radius: 10px;
    background: #eff6ff;
    color: #1e3a8a;
    font-size: 0.88rem;
    line-height: 1.5;
}

.status-dot {
    display: inline-block;
    flex: 0 0 auto;
    width: 0.7rem;
    height: 0.7rem;
    border-radius: 50%;
}

.status-dot.exists {
    background: var(--success);
}

.status-dot.missing {
    background: #f59e0b;
}

.status-dot.unassigned {
    background: #64748b;
}

.status-dot.planned {
    background: #7c3aed;
}

.template-tree-panel {
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--panel);
    box-shadow: var(--shadow);
}

.template-tree {
    position: relative;
    display: grid;
    gap: 0.8rem;
    margin: 0;
    padding: 0 0 0 1.6rem;
    list-style: none;
}

.template-tree:not(.template-tree-root)::before {
    position: absolute;
    top: 0;
    bottom: 1.1rem;
    left: 0.55rem;
    width: 1px;
    background: #cbd5e1;
    content: "";
}

.template-tree-root {
    gap: 0.2rem;
    padding: 0;
}

.template-group,
.template-slot {
    position: relative;
}

.template-tree:not(.template-tree-root) > li::before {
    position: absolute;
    top: 1.15rem;
    left: -1.05rem;
    width: 1rem;
    height: 1px;
    background: #cbd5e1;
    content: "";
}

.template-tree-root > .template-group {
    padding: 0;
}

.template-tree-root > .template-group + .template-group {
    border-top: 1px solid #edf0f4;
}

.template-group-heading {
    display: grid;
    grid-template-columns: 0.8rem 1.15rem minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.65rem;
    min-height: 2.3rem;
    padding: 0.45rem 0.55rem;
    border-radius: 8px;
    cursor: pointer;
    list-style: none;
}

.template-group-heading::-webkit-details-marker {
    display: none;
}

.template-group-heading:hover {
    background: #f1f5f9;
}

.template-tree-root > .template-group > .template-group-details > .template-group-heading {
    padding: 0.6rem 0.7rem;
}

.template-tree-root > .template-group > .template-group-details > .template-group-heading .template-group-copy > strong,
.template-tree-root > .template-group > .template-group-heading .template-group-copy > strong {
    font-size: 1.05rem;
}

.template-group-copy {
    display: grid;
    min-width: 0;
    gap: 0.2rem;
}

.template-group-copy > strong {
    font-size: 0.98rem;
}

.template-group-copy > small {
    max-width: 58rem;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 400;
    line-height: 1.45;
}

.tree-chevron {
    width: 0.6rem;
    height: 0.6rem;
    border-right: 2px solid #64748b;
    border-bottom: 2px solid #64748b;
    transform: rotate(-45deg);
    transition: transform 0.15s ease;
}

.tree-chevron-spacer {
    display: block;
    width: 0.8rem;
}

.tree-folder-icon {
    position: relative;
    display: block;
    width: 1.05rem;
    height: 0.75rem;
    border: 1px solid #b7791f;
    border-radius: 2px;
    background: #fbbf24;
}

.tree-folder-icon::before {
    position: absolute;
    top: -0.28rem;
    left: -1px;
    width: 0.52rem;
    height: 0.28rem;
    border: 1px solid #b7791f;
    border-bottom: 0;
    border-radius: 2px 2px 0 0;
    background: #fcd34d;
    content: "";
}

.tree-file-icon {
    position: relative;
    display: block;
    flex: 0 0 auto;
    width: 0.85rem;
    height: 1.05rem;
    border: 1px solid #64748b;
    border-radius: 2px;
    background: #fff;
}

.tree-file-icon::after {
    position: absolute;
    top: -1px;
    right: -1px;
    width: 0.3rem;
    height: 0.3rem;
    border-bottom: 1px solid #64748b;
    border-left: 1px solid #64748b;
    background: #e2e8f0;
    content: "";
}

.tree-file-icon.exists {
    border-color: var(--success);
    background: var(--success-soft);
}

.tree-file-icon.missing {
    border-color: #d97706;
    background: #fffbeb;
}

.tree-file-icon.unassigned {
    border-color: #64748b;
    background: #f1f5f9;
}

.template-group-details[open] > .template-group-heading .tree-chevron {
    transform: rotate(45deg) translate(-2px, -2px);
}

.template-group-content {
    margin-top: 0.7rem;
}

.template-group-leaf {
    cursor: default;
}

.template-group-leaf:hover {
    background: transparent;
}

.template-group.is-planned > .template-group-details > .template-group-heading,
.template-group.is-planned > .template-group-leaf {
    color: #4c1d95;
}

.planned-badge {
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    background: #f3e8ff;
    color: #6b21a8;
    font-size: 0.7rem;
    font-weight: 800;
    white-space: nowrap;
}

.template-slot {
    padding: 0.85rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel-muted);
}

.template-slot.is-exists {
    border-color: var(--success);
}

.template-slot.is-missing {
    border-color: #fbbf24;
}

.template-slot.is-unassigned {
    border-color: #cbd5e1;
}

.template-slot-main,
.template-file-list > li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.template-slot-title,
.template-file-name {
    display: flex;
    align-items: center;
    min-width: 0;
    gap: 0.65rem;
}

.template-slot-title > div,
.template-file-name > div {
    display: grid;
    min-width: 0;
    gap: 0.2rem;
}

.template-slot code,
.template-file-list code {
    overflow-wrap: anywhere;
    color: #334155;
}

.template-slot small,
.template-file-list small {
    color: var(--muted);
    line-height: 1.4;
}

.template-status {
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    font-size: 0.74rem;
    font-weight: 800;
    text-transform: uppercase;
}

.template-status.exists {
    background: var(--success-soft);
    color: var(--success);
}

.template-status.missing {
    background: #fffbeb;
    color: #92400e;
}

.template-status.unassigned {
    background: #f1f5f9;
    color: #475569;
}

.template-file-list {
    display: grid;
    gap: 0.55rem;
    margin: 0.8rem 0 0;
    padding: 0;
    list-style: none;
}

.template-file-list > li {
    padding: 0.65rem;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    background: #fff;
}

.template-file-name > span {
    padding: 0.2rem 0.35rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    font-size: 0.65rem;
    font-weight: 800;
}

.template-file-actions,
.template-upload {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.45rem;
}

.template-file-actions form {
    margin: 0;
}

.template-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: auto;
    min-height: 2.2rem;
    padding: 0.4rem 0.7rem;
    border: 1px solid var(--accent);
    border-radius: 7px;
    background: var(--accent);
    color: #fff;
    font: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    text-decoration: none;
    cursor: pointer;
}

.template-button.secondary {
    border-color: var(--border);
    background: #fff;
    color: var(--text);
}

.template-button.danger {
    border-color: #fecaca;
    background: var(--danger-soft);
    color: var(--danger);
}

.file-button input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.template-upload {
    margin-top: 0.8rem;
    padding-top: 0.8rem;
    border-top: 1px solid #e2e8f0;
}

.template-upload input[type="file"] {
    max-width: 22rem;
}

.variable-upload label {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 700;
}

.variable-upload input[type="number"] {
    width: 5rem;
    min-height: 2.2rem;
}

@media (max-width: 780px) {
    .templates-heading,
    .template-slot-main,
    .template-file-list > li {
        align-items: stretch;
        flex-direction: column;
    }

    .coverage-summary {
        align-self: stretch;
    }

    .template-tree {
        padding-left: 1rem;
    }

    .template-tree:not(.template-tree-root) > li::before {
        left: -0.45rem;
        width: 0.45rem;
    }

    .template-file-actions,
    .template-upload {
        align-items: stretch;
        flex-direction: column;
    }

    .template-button,
    .template-upload input[type="file"] {
        width: 100%;
        max-width: none;
    }
}
