/* ===================================================
   Prompt Templates Panel — Right Sidebar
   =================================================== */

/* --- Toggle Button in Chat Header --- */
.templates-toggle-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    background: none;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    padding: 5px 10px;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    transition: all var(--transition);
    white-space: nowrap;
}
.templates-toggle-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: color-mix(in srgb, var(--primary) 8%, transparent);
}
.templates-toggle-btn.active {
    border-color: var(--primary);
    color: var(--primary);
    background: color-mix(in srgb, var(--primary) 10%, transparent);
}
.templates-toggle-btn svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* --- Panel Container --- */
#page-chat {
    position: relative;
}

.templates-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 360px;
    max-width: 90vw;
    background: var(--header-bg);
    border-left: 1px solid var(--header-border);
    display: flex;
    flex-direction: column;
    z-index: 50;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.08);
}
.templates-panel.open {
    transform: translateX(0);
}

/* When panel is open, shrink chat area */
#page-chat.templates-open .chat-messages,
#page-chat.templates-open .chat-input-area {
    margin-right: 360px;
    transition: margin-right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* --- Panel Header --- */
.templates-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
}
.templates-panel-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
}
.templates-panel-title svg {
    width: 18px;
    height: 18px;
    color: var(--primary);
}
.templates-panel-close {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 4px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
}
.templates-panel-close:hover {
    background: var(--hover-bg);
    color: var(--text-primary);
}
.templates-panel-close svg {
    width: 18px;
    height: 18px;
}

/* --- Panel Actions Bar --- */
.templates-actions-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 18px;
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
}
.templates-search {
    flex: 1;
    padding: 7px 12px;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    background: var(--input-bg);
    color: var(--text-primary);
    font-size: 0.82rem;
    outline: none;
    transition: border-color var(--transition);
}
.templates-search::placeholder {
    color: var(--text-muted);
}
.templates-search:focus {
    border-color: var(--primary);
}
.templates-add-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 7px 12px;
    border: none;
    border-radius: var(--radius-md);
    background: var(--primary);
    color: #fff;
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--transition);
}
.templates-add-btn:hover {
    background: var(--primary-dark);
}
.templates-add-btn svg {
    width: 14px;
    height: 14px;
}

/* --- Templates List --- */
.templates-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px;
}
.templates-list::-webkit-scrollbar { width: 5px; }
.templates-list::-webkit-scrollbar-track { background: transparent; }
.templates-list::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }

.templates-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    color: var(--text-muted);
    text-align: center;
    gap: 12px;
}
.templates-empty svg {
    width: 40px;
    height: 40px;
    opacity: 0.4;
}
.templates-empty h4 {
    font-size: 0.92rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin: 0;
}
.templates-empty p {
    font-size: 0.8rem;
    margin: 0;
}

/* --- Template Card --- */
.template-card {
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-lg);
    padding: 12px 14px;
    margin-bottom: 8px;
    transition: all var(--transition);
    cursor: default;
}
.template-card:hover {
    border-color: var(--primary);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
.template-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;
}
.template-card-name {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text-primary);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.template-card-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity var(--transition);
}
.template-card:hover .template-card-actions {
    opacity: 1;
}
.template-card-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 4px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
}
.template-card-btn:hover {
    background: var(--hover-bg);
}
.template-card-btn.edit-btn:hover { color: var(--primary); }
.template-card-btn.delete-btn:hover { color: var(--danger, #ef4444); }
.template-card-btn svg {
    width: 14px;
    height: 14px;
}
.template-card-preview {
    font-size: 0.78rem;
    color: var(--text-secondary);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    margin-bottom: 8px;
    white-space: pre-wrap;
    word-break: break-word;
}
.template-card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.template-card-footer-left {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}
.template-type-badge {
    font-size: 0.65rem;
    padding: 1px 7px;
    border-radius: 999px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.template-type-badge.personal {
    background: color-mix(in srgb, var(--primary) 14%, transparent);
    color: var(--primary);
}
.template-type-badge.global {
    background: color-mix(in srgb, #f59e0b 16%, transparent);
    color: #d97706;
}
.template-card-global {
    border-left: 3px solid #f59e0b;
}
.template-card-locked {
    opacity: 0.75;
    border-left: 3px solid var(--text-muted, #888);
}
.template-lock-icon {
    width: 13px;
    height: 13px;
    flex-shrink: 0;
    margin-right: 4px;
    vertical-align: -2px;
    display: inline;
    color: var(--text-muted, #888);
}
.template-owner-label {
    font-size: 0.62rem;
    color: var(--text-muted, #888);
    font-style: italic;
    white-space: nowrap;
}
.template-card-tag {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--primary) 12%, transparent);
    color: var(--primary);
    font-weight: 500;
}
.template-insert-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border: 1px solid var(--primary);
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--primary);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
}
.template-insert-btn:hover {
    background: var(--primary);
    color: #fff;
}
.template-insert-btn svg {
    width: 12px;
    height: 12px;
}

/* --- Template Editor Modal (overlay inside panel) --- */
.template-editor-overlay {
    position: absolute;
    inset: 0;
    background: var(--header-bg);
    display: flex;
    flex-direction: column;
    z-index: 5;
}
.template-editor-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
}
.template-editor-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
}
.template-editor-back {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 4px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    transition: all var(--transition);
}
.template-editor-back:hover {
    color: var(--text-primary);
    background: var(--hover-bg);
}
.template-editor-back svg {
    width: 18px;
    height: 18px;
}
.template-editor-body {
    flex: 1;
    overflow-y: auto;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.template-field label {
    display: block;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.template-field input,
.template-field textarea,
.template-field select {
    width: 100%;
    padding: 8px 12px;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    background: var(--input-bg);
    color: var(--text-primary);
    font-size: 0.85rem;
    font-family: inherit;
    outline: none;
    transition: border-color var(--transition);
    box-sizing: border-box;
}
.template-field input:focus,
.template-field textarea:focus,
.template-field select:focus {
    border-color: var(--primary);
}
.template-field textarea {
    min-height: 140px;
    resize: vertical;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.82rem;
    line-height: 1.5;
}
.template-editor-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    padding: 14px 18px;
    border-top: 1px solid var(--header-border);
    flex-shrink: 0;
}
.template-save-btn {
    padding: 8px 20px;
    border: none;
    border-radius: var(--radius-md);
    background: var(--primary);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition);
}
.template-save-btn:hover {
    background: var(--primary-dark);
}
.template-cancel-btn {
    padding: 8px 16px;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all var(--transition);
}
.template-cancel-btn:hover {
    border-color: var(--text-secondary);
    color: var(--text-primary);
}

/* --- Delete Confirmation --- */
.template-delete-confirm {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: color-mix(in srgb, var(--danger, #ef4444) 10%, var(--input-bg));
    border: 1px solid color-mix(in srgb, var(--danger, #ef4444) 30%, var(--input-border));
    border-radius: var(--radius-md);
    margin-top: 4px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    animation: fadeIn 0.15s ease;
}
.template-delete-confirm button {
    padding: 3px 10px;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    cursor: pointer;
    border: none;
    font-weight: 500;
}
.template-delete-yes {
    background: var(--danger, #ef4444);
    color: #fff;
}
.template-delete-no {
    background: var(--hover-bg);
    color: var(--text-primary);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .templates-panel {
        width: 100%;
        max-width: 100%;
    }
    #page-chat.templates-open .chat-messages,
    #page-chat.templates-open .chat-input-area {
        margin-right: 0;
    }
    .templates-toggle-btn span {
        display: none;
    }
}
