window.tmConfirm = {
    async ask(title, text, okLabel) {
        if (typeof window.Swal === 'undefined') {
            return window.confirm((title || '') + (text ? '\n\n' + text : ''));
        }
        const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
        const result = await window.Swal.fire({
            title: title || 'Are you sure?',
            text: text || '',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: okLabel || 'Yes, delete it',
            cancelButtonText: 'Cancel',
            reverseButtons: true,
            buttonsStyling: false,
            customClass: {
                popup: isDark ? 'tm-swal tm-swal-dark' : 'tm-swal',
                confirmButton: 'tm-btn tm-btn-danger',
                cancelButton: 'tm-btn tm-btn-ghost'
            }
        });
        return result.isConfirmed;
    },
    async prompt(title, text, placeholder) {
        if (typeof window.Swal === 'undefined') {
            return window.prompt((title || '') + (text ? '\n' + text : ''), '') || null;
        }
        const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
        const res = await window.Swal.fire({
            title: title || 'Notes',
            text: text || '',
            input: 'textarea',
            inputPlaceholder: placeholder || 'Optional…',
            inputAttributes: { 'aria-label': title || 'Notes' },
            showCancelButton: true,
            confirmButtonText: 'Save',
            cancelButtonText: 'Skip',
            buttonsStyling: false,
            customClass: {
                popup: isDark ? 'tm-swal tm-swal-dark' : 'tm-swal',
                confirmButton: 'tm-btn tm-btn-primary',
                cancelButton: 'tm-btn tm-btn-ghost'
            }
        });
        return res.isConfirmed ? (res.value ?? '') : null;
    },
    setOverdueRef(ref) { window.tmOverdueDotNet = ref; },
    openOverdue(id) {
        try { if (window.Swal) window.Swal.close(); } catch (e) { }
        try {
            if (window.tmOverdueDotNet) window.tmOverdueDotNet.invokeMethodAsync('OpenOverdueCard', id);
        } catch (e) { }
    },
    toast(message, type) {
        if (typeof window.Swal === 'undefined') return;
        window.Swal.fire({
            toast: true, position: 'top-end', showConfirmButton: false,
            timer: 2500, timerProgressBar: true,
            icon: type || 'success', title: message
        });
    }
};
