{{define "webmail_folder.html"}}
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{if .search_query}}Search: {{.search_query}}{{else}}{{.active_folder}}{{end}} - Webmail</title>
    <link href="/webmail/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/webmail/static/vendor/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        html, body { background-color: #1a1a1a; color: #e0e0e0; height: 100%; }
        .card { background-color: #2d2d2d; border: 1px solid #404040; }
        .folder-link.active { background-color: #0d6efd; color: #fff !important; }
        .msg-row { cursor: grab; }
        .msg-row.dragging { opacity: 0.4; }
        .folder-link.drop-hover { background-color: #0d6efd; color: #fff !important; outline: 2px dashed #6ea8fe; outline-offset: -2px; }
        .folder-unread-badge { font-size: .7rem; }
        .msg-row-older { display: none; }
        .msg-group-toggle { cursor: pointer; }
        /* A search result can span multiple folders, and the bulk-action endpoint is
           scoped to one folder path — rather than a bulk action silently no-oping on
           every row from a different folder, selection/bulk actions are just not
           offered while searching (per-row actions in the reading pane still work). */
        .search-mode .msg-check, .search-mode #selectAllCheck, .search-mode .bulk-btn { display: none; }

        /* Outlook-style three-pane shell: fixed-width sidebar + fixed-width list +
           flexible reading pane, instead of a responsive 12-column grid — this is
           deliberately a fixed desktop layout to match the reference, not a
           mobile-first one. */
        .mail-shell { display: flex; align-items: stretch; height: calc(100vh - 56px); overflow: hidden; }
        .mail-sidebar { width: 230px; flex: 0 0 auto; overflow-y: auto; border-right: 1px solid #404040; padding: .75rem; }
        .mail-list-pane { width: 380px; flex: 0 0 auto; overflow-y: auto; border-right: 1px solid #404040; display: flex; flex-direction: column; }
        .mail-reading-pane { flex: 1 1 auto; overflow-y: auto; padding: 1.5rem; min-width: 0; }
        .mail-toolbar { flex: 0 0 auto; padding: .5rem .75rem; border-bottom: 1px solid #404040; display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; }
        .mail-list-scroll { flex: 1 1 auto; overflow-y: auto; }
        .mail-list-header { padding: .35rem .75rem; font-size: .75rem; text-transform: uppercase; color: #8a8a8a; display: flex; justify-content: space-between; }

        .msg-item { display: flex; align-items: flex-start; gap: .6rem; padding: .55rem .75rem; border-bottom: 1px solid #333; cursor: pointer; }
        .msg-item:hover { background-color: #262626; }
        .msg-item.active { background-color: #0d3860; }
        .msg-item:has(.msg-check:checked) { background-color: #2a2f36; }
        .msg-item.unread .msg-subject { font-weight: 700; color: #fff; }
        .msg-item.unread .msg-from { font-weight: 700; color: #fff; }
        .msg-check { margin-top: .35rem; flex: 0 0 auto; }
        .msg-avatar { width: 34px; height: 34px; border-radius: 50%; background: #495057; color: #fff; display: flex; align-items: center; justify-content: center; font-size: .85rem; font-weight: 600; flex: 0 0 auto; }
        .msg-item.unread .msg-avatar { background: #0d6efd; }
        .msg-main { min-width: 0; flex: 1 1 auto; }
        .msg-row1 { display: flex; align-items: center; gap: .4rem; }
        .msg-from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
        .msg-star { flex: 0 0 auto; cursor: pointer; color: #6a6a6a; }
        .msg-star.bi-star-fill { color: #f4c150; }
        .msg-star:hover { color: #f4c150; }
        .msg-date { flex: 0 0 auto; font-size: .75rem; color: #8a8a8a; margin-left: auto; }
        .msg-row2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; color: #adb5bd; }
        .msg-subject { color: #e0e0e0; }

        #folderContextMenu, #messageContextMenu { z-index: 1085; }
        .dropdown-submenu { position: relative; }
        .dropdown-submenu > .dropdown-menu { top: 0; left: 100%; margin-left: 2px; display: none; }
        .dropdown-submenu:hover > .dropdown-menu { display: block; }
        .folder-row { display: flex; align-items: center; }
        .folder-drag-handle { cursor: grab; color: #6a6a6a; padding: 0 .25rem 0 0; flex: 0 0 auto; }
        .folder-drag-handle:hover { color: #adb5bd; }
        .folder-row.folder-row-dragging { opacity: 0.4; }
        .folder-expand-toggle { flex: 0 0 auto; width: 16px; text-align: center; cursor: pointer; color: #8a8a8a; font-size: .8rem; transition: transform .1s; }
        .folder-expand-toggle:hover { color: #fff; }
        .folder-expand-toggle.collapsed { transform: rotate(-90deg); }
        .folder-expand-spacer { flex: 0 0 auto; width: 16px; }
        .folder-children { margin-left: 16px; }
        .folder-children.collapsed { display: none; }

        .msg-body-html { background-color: #fff; color: #000; border-radius: 6px; padding: 1rem; overflow-wrap: break-word; word-break: break-word; overflow-x: auto; max-width: 100%; }
        .msg-body-html img { max-width: 100%; height: auto; }
        .msg-body-html table { max-width: 100%; }
        .msg-body-text { white-space: pre-wrap; word-break: break-word; overflow-wrap: break-word; }
        #readingPaneBody .pane-toolbar { border-bottom: 1px solid #404040; padding-bottom: .75rem; }
    </style>
</head>
<body>
    {{template "csrf_script" .}}
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark px-3" style="height: 56px;">
        <span class="navbar-brand mb-0 h1"><i class="bi bi-envelope-fill me-2"></i>Webmail <small class="text-muted">{{.mailbox.Email}}</small></span>
        <form method="get" action="/webmail/mail/search" class="mx-auto" style="width: 360px;">
            <div class="input-group input-group-sm">
                <span class="input-group-text bg-body-secondary border-0"><i class="bi bi-search"></i></span>
                <input type="search" name="q" id="mailSearchInput" class="form-control" placeholder="Search all mail" value="{{.search_query}}">
            </div>
        </form>
        <div class="navbar-nav flex-row gap-2 ms-auto">
            <button type="button" onclick="openCompose('/webmail/mail/compose')" class="btn btn-primary btn-sm"><i class="bi bi-pencil-square me-1"></i>Compose</button>
            <a href="/webmail/account" class="btn btn-outline-light btn-sm" title="Settings"><i class="bi bi-gear"></i></a>
            <form method="post" action="/webmail/logout" class="d-inline">
                <button type="submit" class="btn btn-outline-light btn-sm" title="Sign out"><i class="bi bi-box-arrow-right"></i></button>
            </form>
        </div>
    </nav>

    <div class="toast-container position-fixed top-0 end-0 p-3" style="z-index: 1090;">
        {{range .flashes}}
        <div class="toast align-items-center text-bg-{{if eq .Category "error"}}danger{{else}}{{.Category}}{{end}} border-0" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="d-flex">
                <div class="toast-body">
                    <i class="bi bi-{{if eq .Category "error"}}exclamation-triangle{{else if eq .Category "success"}}check-circle{{else}}info-circle{{end}} me-2"></i>
                    {{.Message}}
                </div>
                <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>
        {{end}}
    </div>

    <div class="mail-shell">
        <div class="mail-sidebar" id="folderSidebarCol">
            <div class="d-flex justify-content-between align-items-center mb-2">
                <span class="text-muted small text-uppercase">Folders</span>
                <div>
                    <button type="button" class="btn btn-sm btn-outline-secondary border-0 py-0 px-1" id="expandAllBtn" title="Expand all"><i class="bi bi-arrows-expand"></i></button>
                    <button type="button" class="btn btn-sm btn-outline-secondary border-0 py-0 px-1" id="collapseAllBtn" title="Collapse all"><i class="bi bi-arrows-collapse"></i></button>
                    <button type="button" class="btn btn-sm btn-outline-secondary border-0 py-0" id="sidebarCollapseBtn" title="Hide folder list"><i class="bi bi-chevron-bar-left"></i></button>
                </div>
            </div>
            <div class="list-group list-group-flush" id="folderList">
                {{range .folder_tree}}{{template "folderTreeNode" .}}{{end}}
            </div>
            <div class="text-muted small mt-2" style="font-size: .72rem;">Right-click a folder for more actions</div>
        </div>

        <div id="folderContextMenu" class="dropdown-menu" style="display: none; position: fixed;">
            <button type="button" class="dropdown-item" data-menu-action="new"><i class="bi bi-folder-plus me-2"></i>New folder</button>
            <button type="button" class="dropdown-item" data-menu-action="mark-all-read"><i class="bi bi-envelope-open me-2"></i>Mark all as read</button>
            <button type="button" class="dropdown-item" data-menu-action="rename"><i class="bi bi-pencil me-2"></i>Rename</button>
            <button type="button" class="dropdown-item text-danger" data-menu-action="empty-trash"><i class="bi bi-trash3 me-2"></i>Empty Trash</button>
            <button type="button" class="dropdown-item text-danger" data-menu-action="clean-spam"><i class="bi bi-shield-x me-2"></i>Clean up Junk</button>
            <button type="button" class="dropdown-item" data-menu-action="restore-folder"><i class="bi bi-arrow-counterclockwise me-2"></i>Restore</button>
            <button type="button" class="dropdown-item text-danger" data-menu-action="delete-folder-permanently"><i class="bi bi-trash3-fill me-2"></i>Delete permanently</button>
        </div>

        <form method="post" id="folderActionForm" class="d-none"></form>

        <div class="mail-list-pane{{if .search_query}} search-mode{{end}}" id="messageListCol">
            <div class="mail-toolbar">
                <button type="button" class="btn btn-sm btn-outline-secondary border-0 py-0 d-none" id="sidebarShowBtn" title="Show folder list"><i class="bi bi-chevron-bar-right"></i></button>
                <input type="checkbox" class="form-check-input" id="selectAllCheck" title="Select all">
                <div class="btn-group btn-group-sm">
                    <button type="button" class="btn btn-outline-secondary bulk-btn" data-action="delete" disabled title="Delete"><i class="bi bi-trash"></i></button>
                    <button type="button" class="btn btn-outline-secondary bulk-btn" data-action="read" disabled title="Mark as read"><i class="bi bi-envelope-open"></i></button>
                    <button type="button" class="btn btn-outline-secondary bulk-btn" data-action="unread" disabled title="Mark as unread"><i class="bi bi-envelope"></i></button>
                </div>
                {{if not .search_query}}
                <select class="form-select form-select-sm bulk-move-select" id="bulkMoveSelect" disabled style="width: auto;" title="Move selected to&hellip;">
                    <option value="">Move to&hellip;</option>
                    {{$folder := .active_folder}}
                    {{range .folders}}{{if ne . $folder}}<option value="{{.}}">{{.}}</option>{{end}}{{end}}
                </select>
                {{end}}
                <button type="button" class="btn btn-sm btn-outline-secondary border-0" onclick="location.reload()" title="Refresh"><i class="bi bi-arrow-clockwise"></i></button>
                <div class="ms-auto d-flex align-items-center gap-1">
                    {{if not .search_query}}
                    <a href="{{.sort_from_href}}" class="btn btn-sm btn-outline-secondary border-0 py-0" title="Sort by sender"><i class="bi bi-person{{if eq .sort_by "from"}}-fill{{end}}"></i>{{if eq .sort_by "from"}} <i class="bi bi-caret-{{if eq .sort_dir "asc"}}up{{else}}down{{end}}-fill"></i>{{end}}</a>
                    <a href="{{.sort_date_href}}" class="btn btn-sm btn-outline-secondary border-0 py-0" title="Sort by date"><i class="bi bi-calendar3{{if ne .sort_by "from"}}-fill{{end}}"></i>{{if ne .sort_by "from"}} <i class="bi bi-caret-{{if eq .sort_dir "asc"}}up{{else}}down{{end}}-fill"></i>{{end}}</a>
                    <a href="{{.unread_only_href}}" class="btn btn-sm border-0 py-0 {{if .unread_only}}btn-primary{{else}}btn-outline-secondary{{end}}" title="Unread only"><i class="bi bi-envelope-fill"></i></a>
                    <a href="{{.starred_only_href}}" class="btn btn-sm border-0 py-0 {{if .starred_only}}btn-warning{{else}}btn-outline-secondary{{end}}" title="Favourites only"><i class="bi bi-star-fill"></i></a>
                    {{end}}
                </div>
            </div>
            <div class="mail-list-header">
                <span>{{if .search_query}}Search: &ldquo;{{.search_query}}&rdquo;{{else}}{{.active_folder}}{{end}}</span>
                <span>{{.total}} message{{if ne .total 1}}s{{end}}</span>
            </div>
            <div class="mail-list-scroll" id="mailListScroll">
                {{if .messages}}
                    {{range .messages}}
                    {{$rowHref := printf "/webmail/mail/%s/%d" .Folder .ID}}
                    {{$paneHref := printf "/webmail/mail/%s/%d/pane" .Folder .ID}}
                    {{$isDraft := eq .Folder "Drafts"}}
                    {{if $isDraft}}{{$rowHref = printf "/webmail/mail/compose?draft=%d&folder=Drafts" .ID}}{{end}}
                    {{$displayName := senderName .CachedFrom}}
                    {{if eq .Folder "Sent"}}{{if .CachedTo}}{{$displayName = senderName .CachedTo}}{{else}}{{$displayName = "(no recipient)"}}{{end}}{{end}}
                    <div class="msg-item {{if .Unread}}unread{{end}}{{if .Collapsed}} msg-row-older{{end}} msg-row" draggable="true" data-uid="{{.ID}}" data-folder="{{.Folder}}" data-href="{{$rowHref}}" data-pane-href="{{$paneHref}}" data-is-draft="{{$isDraft}}">
                        <input type="checkbox" class="form-check-input msg-check" value="{{.ID}}" onclick="event.stopPropagation()">
                        <div class="msg-avatar">{{initial $displayName}}</div>
                        <div class="msg-main">
                            <div class="msg-row1">
                                <span class="msg-from" title="{{if eq .Folder "Sent"}}{{.CachedTo}}{{else}}{{.CachedFrom}}{{end}}">{{$displayName}}</span>
                                <i class="bi msg-star {{if .Starred}}bi-star-fill{{else}}bi-star{{end}}" data-uid="{{.ID}}" data-folder="{{.Folder}}" title="{{if .Starred}}Remove from favourites{{else}}Add to favourites{{end}}"></i>
                                <span class="msg-date">{{strftime "%Y-%m-%d %H:%M" .InternalDate}}</span>
                            </div>
                            <div class="msg-row2">
                                <span class="msg-subject">{{if .CachedSubject}}{{.CachedSubject}}{{else}}(no subject){{end}}</span>
                                {{if gt .GroupExtra 0}}<span class="badge bg-secondary msg-group-toggle" data-group-toggle="{{.ID}}">+{{.GroupExtra}} more</span>{{end}}
                                {{if .CachedPreview}} &ndash; {{.CachedPreview}}{{end}}
                            </div>
                        </div>
                    </div>
                    {{end}}
                {{else}}
                <div class="text-center py-5">
                    <i class="bi bi-inbox text-muted" style="font-size: 3rem;"></i>
                    <h6 class="text-muted mt-3">No messages</h6>
                </div>
                {{end}}
            </div>
            {{if or .has_prev .has_next}}
            <div class="d-flex justify-content-between p-2 border-top" style="border-color: #404040 !important;">
                {{if .has_prev}}<a href="{{.prev_href}}" class="btn btn-outline-secondary btn-sm">&laquo; Newer</a>{{else}}<span></span>{{end}}
                {{if .has_next}}<a href="{{.next_href}}" class="btn btn-outline-secondary btn-sm">Older &raquo;</a>{{end}}
            </div>
            {{end}}
        </div>

        <div class="mail-reading-pane" id="readingPaneBody">
            <div class="text-center text-muted py-5">
                <i class="bi bi-envelope-open" style="font-size: 3rem;"></i>
                <p class="mt-3">Select a message to read</p>
            </div>
        </div>
    </div>

    <form method="post" id="bulkActionForm" class="d-none">
        <input type="hidden" name="action" id="bulkActionField">
        <input type="hidden" name="target_folder" id="bulkTargetFolderField">
    </form>

    {{/* Always rendered (independent of search-mode, unlike the bulk toolbar's own
         Move-to dropdown) so the message context menu's Move-to submenu always has a
         folder list to build from, search results included. */}}
    <select id="allFoldersList" class="d-none">
        {{range .folders}}<option value="{{.}}">{{.}}</option>{{end}}
    </select>

    <div id="messageContextMenu" class="dropdown-menu" style="display: none; position: fixed;">
        <button type="button" class="dropdown-item" data-msg-action="reply"><i class="bi bi-reply me-2"></i>Reply</button>
        <button type="button" class="dropdown-item" data-msg-action="forward"><i class="bi bi-arrow-right me-2"></i>Forward</button>
        <div class="dropdown-divider"></div>
        <button type="button" class="dropdown-item" data-msg-action="toggle-read"><i class="bi bi-envelope-open me-2"></i><span data-toggle-read-label>Mark as read</span></button>
        <button type="button" class="dropdown-item" data-msg-action="junk"><i class="bi bi-shield-exclamation me-2"></i>Mark as Junk</button>
        <div class="dropdown-submenu">
            <button type="button" class="dropdown-item dropdown-toggle" data-msg-action="move-toggle">Move to&hellip;</button>
            <div class="dropdown-menu" id="messageMoveSubmenu"></div>
        </div>
        <button type="button" class="dropdown-item" data-msg-action="open-tab"><i class="bi bi-box-arrow-up-right me-2"></i>Open in new tab</button>
        <div class="dropdown-divider"></div>
        <button type="button" class="dropdown-item text-danger" data-msg-action="delete"><i class="bi bi-trash me-2"></i><span data-delete-label>Delete</span></button>
    </div>
    <form method="post" id="messageActionForm" class="d-none"></form>

    <div class="modal fade" id="messageModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-xl modal-dialog-scrollable">
            <div class="modal-content" style="background-color: #2d2d2d; border-color: #404040;">
                <div class="modal-header" style="border-color: #404040;">
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="messageModalBody"></div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="confirmationModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-question-circle me-2"></i>Confirm Action</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="confirmationModalBody">Are you sure you want to proceed?</div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-danger" id="confirmationModalConfirm">Confirm</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="inputModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="inputModalForm">
                    <div class="modal-header">
                        <h5 class="modal-title" id="inputModalTitle"><i class="bi bi-pencil me-2"></i>Folder name</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="text" class="form-control" id="inputModalField" maxlength="60" required>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary" id="inputModalConfirm">OK</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    {{template "compose_widget" .}}
    {{template "webmail_shortcuts" .}}

    <script src="/webmail/static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            document.querySelectorAll('.toast').forEach(function(el) { new bootstrap.Toast(el, {delay: 5000}).show(); });
        });
        function showConfirmation(message) {
            return new Promise((resolve) => {
                const modal = document.getElementById('confirmationModal');
                document.getElementById('confirmationModalBody').textContent = message;
                const confirmButton = document.getElementById('confirmationModalConfirm');
                const handleConfirm = () => { resolve(true); bootstrap.Modal.getInstance(modal).hide(); cleanup(); };
                const handleCancel = () => { resolve(false); cleanup(); };
                const cleanup = () => {
                    confirmButton.removeEventListener('click', handleConfirm);
                    modal.removeEventListener('hidden.bs.modal', handleCancel);
                };
                confirmButton.addEventListener('click', handleConfirm);
                modal.addEventListener('hidden.bs.modal', handleCancel, { once: true });
                new bootstrap.Modal(modal).show();
            });
        }
        // showInputPrompt replaces the browser's native prompt() for folder
        // name/rename input — a native prompt() blocks the whole tab (including any
        // in-flight automation/extension driving it) until dismissed, and looks/feels
        // inconsistent with the confirm dialog above. Resolves the entered text, or
        // null if cancelled — same null-on-cancel contract prompt() itself has.
        function showInputPrompt(title, defaultValue) {
            return new Promise((resolve) => {
                const modal = document.getElementById('inputModal');
                const form = document.getElementById('inputModalForm');
                const field = document.getElementById('inputModalField');
                document.getElementById('inputModalTitle').textContent = title;
                field.value = defaultValue || '';
                let resolved = false;
                const handleSubmit = (e) => {
                    e.preventDefault();
                    resolved = true;
                    resolve(field.value.trim());
                    bootstrap.Modal.getInstance(modal).hide();
                    cleanup();
                };
                const handleCancel = () => {
                    if (!resolved) resolve(null);
                    cleanup();
                };
                const cleanup = () => {
                    form.removeEventListener('submit', handleSubmit);
                    modal.removeEventListener('hidden.bs.modal', handleCancel);
                };
                form.addEventListener('submit', handleSubmit);
                modal.addEventListener('hidden.bs.modal', handleCancel, { once: true });
                new bootstrap.Modal(modal).show();
                modal.addEventListener('shown.bs.modal', function() { field.focus(); field.select(); }, { once: true });
            });
        }
        // Delegated (not per-element) so this also covers [data-confirm] buttons
        // injected later via fetch — the reading pane's own delete button, loaded
        // into #readingPaneBody by openInPane, wouldn't exist yet at DOMContentLoaded
        // time for a per-element listener to attach to.
        document.addEventListener('click', async function(e) {
            const button = e.target.closest('[data-confirm]');
            if (!button) return;
            e.preventDefault();
            if (await showConfirmation(button.getAttribute('data-confirm'))) {
                const form = button.closest('form');
                if (form) form.submit();
            }
        });

        // Drag a message row onto a folder in the sidebar to move it there — a
        // shortcut for the toolbar's "Move to..." control. Each row carries its OWN
        // folder (data-folder) rather than assuming the page's active folder, since a
        // search result can span multiple folders.
        (function() {
            let draggedUID = null;
            let draggedFolder = null;

            document.querySelectorAll('.msg-row').forEach(function(row) {
                row.addEventListener('dragstart', function() {
                    draggedUID = row.dataset.uid;
                    draggedFolder = row.dataset.folder;
                    row.classList.add('dragging');
                });
                row.addEventListener('dragend', function() {
                    row.classList.remove('dragging');
                });
            });

            document.querySelectorAll('.folder-link').forEach(function(link) {
                link.addEventListener('dragover', function(e) {
                    if (!draggedUID || link.dataset.folder === draggedFolder) return;
                    e.preventDefault();
                    link.classList.add('drop-hover');
                });
                link.addEventListener('dragleave', function() {
                    link.classList.remove('drop-hover');
                });
                link.addEventListener('drop', async function(e) {
                    e.preventDefault();
                    link.classList.remove('drop-hover');
                    const targetFolder = link.dataset.folder;
                    if (!draggedUID || !targetFolder || targetFolder === draggedFolder) return;
                    const body = new URLSearchParams();
                    body.set('target_folder', targetFolder);
                    body.set('csrf_token', window.__csrfToken || '');
                    await fetch(`/webmail/mail/${draggedFolder}/${draggedUID}/move`, {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        body: body.toString(),
                    });
                    window.location.reload();
                });
            });
        })();

        // Drag-and-drop folder reordering — root folders (INBOX/Junk/Sent/Drafts/
        // Trash) are static and never draggable; only a custom folder still under
        // INBOX (server-computed .Renameable — see buildFolderView) can be dragged,
        // and only among its OWN siblings (rows sharing the same immediate .folder-
        // children parent — dragging never re-parents a folder, only reorders it
        // where it already is). A dragged row's own .folder-children (if it has
        // descendants) moves right along with it, or its whole subtree would get
        // visually orphaned mid-drag. Reorders the DOM live as you drag (move the
        // dragged row to whichever side of the row under the cursor it's closer to —
        // a common no-library technique), persisting just that sibling group's final
        // order once on dragend. draggingRow being set is what keeps this from firing
        // during the unrelated "drag a message onto a folder" drag above (that one's
        // own dragover guard checks draggedUID instead, so the two never interfere).
        (function() {
            const list = document.getElementById('folderList');
            if (!list) return;
            let draggingRow = null;
            let draggingChildren = null;

            function persistOrder(parentEl) {
                const order = Array.from(parentEl.children)
                    .filter(function(el) { return el.classList.contains('folder-row'); })
                    .map(function(el) { return el.dataset.folder; });
                const body = new URLSearchParams();
                order.forEach(function(name) { body.append('order', name); });
                body.set('csrf_token', window.__csrfToken || '');
                fetch('/webmail/mail/folders/order', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    body: body.toString(),
                });
            }

            list.querySelectorAll('.folder-row[draggable="true"]').forEach(function(row) {
                row.addEventListener('dragstart', function(e) {
                    e.stopPropagation();
                    draggingRow = row;
                    draggingChildren = row.nextElementSibling && row.nextElementSibling.classList.contains('folder-children')
                        ? row.nextElementSibling : null;
                    row.classList.add('folder-row-dragging');
                });
                row.addEventListener('dragend', function() {
                    row.classList.remove('folder-row-dragging');
                    if (draggingRow) persistOrder(draggingRow.parentElement);
                    draggingRow = null;
                    draggingChildren = null;
                });
                row.addEventListener('dragover', function(e) {
                    if (!draggingRow || draggingRow === row) return;
                    if (draggingRow.parentElement !== row.parentElement) return; // reorder only, never re-parent
                    e.preventDefault();
                    e.stopPropagation();
                    const rect = row.getBoundingClientRect();
                    const before = (e.clientY - rect.top) < rect.height / 2;
                    row.parentNode.insertBefore(draggingRow, before ? row : row.nextSibling);
                    if (draggingChildren) row.parentNode.insertBefore(draggingChildren, draggingRow.nextSibling);
                });
            });
        })();

        // Folder tree expand/collapse — a display preference remembered per-browser
        // (localStorage, a Set of collapsed folder names), same pattern as the
        // sidebar-hide toggle below. Default is fully expanded (nothing collapsed).
        (function() {
            const KEY = 'webmail_collapsed_folders';
            function loadCollapsed() {
                try { return new Set(JSON.parse(localStorage.getItem(KEY)) || []); } catch (e) { return new Set(); }
            }
            function saveCollapsed(set) { localStorage.setItem(KEY, JSON.stringify(Array.from(set))); }

            function setCollapsed(name, collapsed) {
                const toggle = document.querySelector('[data-folder-toggle="' + CSS.escape(name) + '"]');
                const kids = document.querySelector('[data-children-of="' + CSS.escape(name) + '"]');
                if (toggle) { toggle.classList.toggle('collapsed', collapsed); toggle.title = collapsed ? 'Expand' : 'Collapse'; }
                if (kids) kids.classList.toggle('collapsed', collapsed);
            }

            const collapsed = loadCollapsed();
            collapsed.forEach(function(name) { setCollapsed(name, true); });

            document.querySelectorAll('.folder-expand-toggle').forEach(function(toggle) {
                toggle.addEventListener('click', function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    const name = toggle.dataset.folderToggle;
                    const nowCollapsed = !toggle.classList.contains('collapsed');
                    setCollapsed(name, nowCollapsed);
                    if (nowCollapsed) collapsed.add(name); else collapsed.delete(name);
                    saveCollapsed(collapsed);
                });
            });

            const expandAllBtn = document.getElementById('expandAllBtn');
            const collapseAllBtn = document.getElementById('collapseAllBtn');
            if (expandAllBtn) expandAllBtn.addEventListener('click', function() {
                document.querySelectorAll('[data-folder-toggle]').forEach(function(t) { setCollapsed(t.dataset.folderToggle, false); });
                collapsed.clear();
                saveCollapsed(collapsed);
            });
            if (collapseAllBtn) collapseAllBtn.addEventListener('click', function() {
                document.querySelectorAll('[data-folder-toggle]').forEach(function(t) {
                    setCollapsed(t.dataset.folderToggle, true);
                    collapsed.add(t.dataset.folderToggle);
                });
                saveCollapsed(collapsed);
            });
        })();

        // "+N more" toggle for a collapsed same-subject run — reveals every
        // immediately-following row marked as part of that group, self-terminating
        // at the first row that isn't (no need to track the count client-side).
        document.querySelectorAll('[data-group-toggle]').forEach(function(badge) {
            badge.addEventListener('click', function(e) {
                e.preventDefault();
                e.stopPropagation();
                let sib = badge.closest('.msg-item').nextElementSibling;
                while (sib && sib.classList.contains('msg-row-older')) {
                    sib.style.display = '';
                    sib = sib.nextElementSibling;
                }
                badge.style.display = 'none';
            });
        });

        // Folder sidebar collapse — a display preference remembered per-browser
        // (localStorage), not server state; default is pinned open (nothing stored
        // yet == not collapsed).
        (function() {
            const KEY = 'webmail_sidebar_collapsed';
            const sidebarCol = document.getElementById('folderSidebarCol');
            const showBtn = document.getElementById('sidebarShowBtn');
            function apply(collapsed) {
                sidebarCol.style.display = collapsed ? 'none' : '';
                showBtn.classList.toggle('d-none', !collapsed);
            }
            apply(localStorage.getItem(KEY) === '1');
            document.getElementById('sidebarCollapseBtn').addEventListener('click', function() {
                localStorage.setItem(KEY, '1');
                apply(true);
            });
            showBtn.addEventListener('click', function() {
                localStorage.setItem(KEY, '0');
                apply(false);
            });
        })();

        // Reading pane + selection share one block because Shift/Ctrl-click needs to
        // work when clicking anywhere on a row, not just its small checkbox — a plain
        // click opens the message in the reading pane; Shift-click range-selects from
        // the last row you interacted with (open or select) to this one; Ctrl/Cmd-click
        // toggles just this row. Any of those needs the full row list + index, not just
        // the checkbox's own click event, so it can't be two independent IIFEs the way
        // "load the pane" and "manage checkboxes" would otherwise naturally split.
        (function() {
            const paneBody = document.getElementById('readingPaneBody');
            const rows = Array.from(document.querySelectorAll('.msg-item'));
            const checks = rows.map(function(row) { return row.querySelector('.msg-check'); });
            const selectAll = document.getElementById('selectAllCheck');
            const bulkBtns = document.querySelectorAll('.bulk-btn');
            const moveSelect = document.getElementById('bulkMoveSelect');
            let lastIndex = null;

            function updateToolbar() {
                const any = checks.some(function(c) { return c.checked; });
                bulkBtns.forEach(function(b) { b.disabled = !any; });
                if (moveSelect) moveSelect.disabled = !any;
                selectAll.checked = checks.length > 0 && checks.every(function(c) { return c.checked; });
            }

            function openInPane(row) {
                if (row.dataset.isDraft === 'true') { window.location.href = row.dataset.href; return; }
                rows.forEach(function(r) { r.classList.remove('active'); });
                row.classList.add('active');
                row.classList.remove('unread');
                paneBody.innerHTML = '<div class="text-center text-muted py-5"><div class="spinner-border" role="status"></div></div>';
                fetch(row.dataset.paneHref)
                    .then(function(r) { if (!r.ok) throw new Error(r.status); return r.text(); })
                    .then(function(html) { paneBody.innerHTML = html; })
                    .catch(function() { paneBody.innerHTML = '<p class="text-danger">Failed to load the message.</p>'; });
            }

            rows.forEach(function(row, i) {
                row.addEventListener('click', function(e) {
                    if (e.target.closest('.msg-group-toggle') || e.target.classList.contains('msg-check')) return;

                    if (e.shiftKey && lastIndex !== null) {
                        e.preventDefault();
                        const [from, to] = [lastIndex, i].sort(function(a, b) { return a - b; });
                        for (let j = from; j <= to; j++) checks[j].checked = true;
                        updateToolbar();
                        return;
                    }
                    if (e.ctrlKey || e.metaKey) {
                        e.preventDefault();
                        checks[i].checked = !checks[i].checked;
                        lastIndex = i;
                        updateToolbar();
                        return;
                    }
                    lastIndex = i;
                    openInPane(row);
                });
            });

            checks.forEach(function(cb, i) {
                cb.addEventListener('click', function(e) {
                    if (e.shiftKey && lastIndex !== null) {
                        const [from, to] = [lastIndex, i].sort(function(a, b) { return a - b; });
                        for (let j = from; j <= to; j++) checks[j].checked = cb.checked;
                    }
                    lastIndex = i;
                    updateToolbar();
                });
            });
            selectAll.addEventListener('change', function() {
                checks.forEach(function(c) { c.checked = selectAll.checked; });
                updateToolbar();
            });
            document.addEventListener('keydown', function(e) {
                if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'a' && document.activeElement.tagName !== 'INPUT') {
                    e.preventDefault();
                    selectAll.checked = true;
                    checks.forEach(function(c) { c.checked = true; });
                    updateToolbar();
                }
            });

            function selectedUIDs() { return checks.filter(function(c) { return c.checked; }).map(function(c) { return c.value; }); }
            function submitBulk(action, targetFolder) {
                const uids = selectedUIDs();
                if (uids.length === 0) return;
                const form = document.getElementById('bulkActionForm');
                form.action = '/webmail/mail/{{.active_folder}}/bulk';
                document.getElementById('bulkActionField').value = action;
                document.getElementById('bulkTargetFolderField').value = targetFolder || '';
                form.querySelectorAll('input[name="uid"]').forEach(function(el) { el.remove(); });
                uids.forEach(function(uid) {
                    const input = document.createElement('input');
                    input.type = 'hidden'; input.name = 'uid'; input.value = uid;
                    form.appendChild(input);
                });
                const csrf = document.createElement('input');
                csrf.type = 'hidden'; csrf.name = 'csrf_token'; csrf.value = window.__csrfToken || '';
                form.appendChild(csrf);
                form.submit();
            }
            document.querySelectorAll('.bulk-btn').forEach(function(btn) {
                btn.addEventListener('click', async function() {
                    const action = btn.dataset.action;
                    if (action === 'delete' && !(await showConfirmation('Move the selected message(s) to Trash?'))) return;
                    submitBulk(action);
                });
            });
            if (moveSelect) {
                moveSelect.addEventListener('change', function() {
                    if (moveSelect.value) submitBulk('move', moveSelect.value);
                });
            }
        })();

        // Star toggle — click the star icon to flip \Flagged (see
        // db.ToggleMessageStarred) without a full page reload. stopPropagation keeps
        // this from also triggering the row's own click (which would open the reading
        // pane / range-select, see the combined IIFE above).
        document.querySelectorAll('.msg-star').forEach(function(star) {
            star.addEventListener('click', async function(e) {
                e.stopPropagation();
                const uid = star.dataset.uid, folder = star.dataset.folder;
                const body = new URLSearchParams();
                body.set('csrf_token', window.__csrfToken || '');
                const resp = await fetch(`/webmail/mail/${folder}/${uid}/star`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    body: body.toString(),
                });
                if (!resp.ok) return;
                const nowStarred = star.classList.toggle('bi-star-fill');
                star.classList.toggle('bi-star', !nowStarred);
                star.title = nowStarred ? 'Remove from favourites' : 'Add to favourites';
            });
        });

        // Folder sidebar context menu — right-click any folder for New folder / Mark
        // all as read / Rename / Empty Trash, replacing the old always-visible "+ New
        // folder" form. Every action posts through #folderActionForm (shared, its
        // action/fields set right before submit) rather than one form per action, same
        // reasoning as the message list's #bulkActionForm.
        (function() {
            const menu = document.getElementById('folderContextMenu');
            const form = document.getElementById('folderActionForm');
            let menuFolder = null;

            // fields is a plain {name: value} object — each key becomes a hidden
            // input, replacing whatever the previous action left behind.
            function postAction(url, fields) {
                form.action = url;
                form.querySelectorAll('input[data-dynamic]').forEach(function(el) { el.remove(); });
                const all = Object.assign({ csrf_token: window.__csrfToken || '' }, fields || {});
                Object.keys(all).forEach(function(name) {
                    const input = document.createElement('input');
                    input.type = 'hidden'; input.name = name; input.value = all[name];
                    input.dataset.dynamic = '1';
                    form.appendChild(input);
                });
                form.submit();
            }

            function hideMenu() { menu.style.display = 'none'; menuFolder = null; }

            document.querySelectorAll('.folder-row').forEach(function(row) {
                row.addEventListener('contextmenu', function(e) {
                    e.preventDefault();
                    menuFolder = row.dataset.folder;
                    const renameable = row.dataset.renameable === 'true';
                    const isTrash = row.dataset.trash === 'true';
                    const isSpam = row.dataset.spam === 'true';
                    const underTrash = row.dataset.underTrash === 'true';
                    const canAddSubfolder = row.dataset.canAddSubfolder === 'true';
                    menu.querySelector('[data-menu-action="new"]').style.display = canAddSubfolder ? '' : 'none';
                    menu.querySelector('[data-menu-action="rename"]').style.display = renameable ? '' : 'none';
                    menu.querySelector('[data-menu-action="empty-trash"]').style.display = isTrash ? '' : 'none';
                    menu.querySelector('[data-menu-action="clean-spam"]').style.display = isSpam ? '' : 'none';
                    menu.querySelector('[data-menu-action="restore-folder"]').style.display = underTrash ? '' : 'none';
                    menu.querySelector('[data-menu-action="delete-folder-permanently"]').style.display = underTrash ? '' : 'none';
                    menu.style.left = e.clientX + 'px';
                    menu.style.top = e.clientY + 'px';
                    menu.style.display = 'block';
                });
            });

            menu.querySelectorAll('[data-menu-action]').forEach(function(item) {
                item.addEventListener('click', async function() {
                    const folder = menuFolder;
                    const action = item.dataset.menuAction;
                    hideMenu();
                    if (!folder) return;
                    switch (action) {
                        case 'new': {
                            const name = await showInputPrompt('New folder name (inside "' + folder + '")');
                            if (name) postAction('/webmail/mail/folders/add', { name: name, parent: folder });
                            break;
                        }
                        case 'mark-all-read':
                            postAction(`/webmail/mail/${folder}/mark-all-read`);
                            break;
                        case 'rename': {
                            const name = await showInputPrompt('Rename "' + folder + '" to', folder);
                            if (name && name !== folder) {
                                postAction(`/webmail/mail/folders/${folder}/rename`, { new_name: name });
                            }
                            break;
                        }
                        case 'empty-trash':
                            if (await showConfirmation('Permanently delete every message in Trash (and any folder deleted into it)? This can\'t be undone.')) {
                                postAction(`/webmail/mail/${folder}/empty`);
                            }
                            break;
                        case 'clean-spam':
                            if (await showConfirmation('Permanently delete every message in Junk? This can\'t be undone — they will NOT be moved to Trash.')) {
                                postAction(`/webmail/mail/${folder}/clean-spam`);
                            }
                            break;
                        case 'restore-folder':
                            postAction(`/webmail/mail/folders/${folder}/restore`);
                            break;
                        case 'delete-folder-permanently':
                            if (await showConfirmation('Permanently delete folder "' + folder + '" (and anything nested inside it)? This can\'t be undone.')) {
                                postAction(`/webmail/mail/folders/${folder}/delete-permanently`);
                            }
                            break;
                    }
                });
            });

            document.addEventListener('click', function(e) {
                if (!menu.contains(e.target)) hideMenu();
            });
            document.addEventListener('keydown', function(e) {
                if (e.key === 'Escape') hideMenu();
            });
            window.addEventListener('scroll', hideMenu, true);
        })();

        // Message row right-click menu (Reply/Forward/Mark read-unread/Mark as Junk/
        // Move to.../Open in new tab/Delete) and double-click (open in a full-size
        // modal, reusing the same pane fragment the reading pane already loads —
        // useful for a long message without losing the list). Both are additive to
        // the existing plain/Shift/Ctrl-click handling above (different event types
        // entirely — contextmenu/dblclick never fire alongside a plain click).
        (function() {
            const menu = document.getElementById('messageContextMenu');
            const submenu = document.getElementById('messageMoveSubmenu');
            const form = document.getElementById('messageActionForm');
            let menuRow = null;

            function postMessageAction(url, fields) {
                form.action = url;
                form.querySelectorAll('input[data-dynamic]').forEach(function(el) { el.remove(); });
                const all = Object.assign({ csrf_token: window.__csrfToken || '' }, fields || {});
                Object.keys(all).forEach(function(name) {
                    const input = document.createElement('input');
                    input.type = 'hidden'; input.name = name; input.value = all[name];
                    input.dataset.dynamic = '1';
                    form.appendChild(input);
                });
                form.submit();
            }

            function hideMenu() { menu.style.display = 'none'; menuRow = null; }

            function buildMoveSubmenu(row) {
                submenu.innerHTML = '';
                const rowFolder = row.dataset.folder;
                Array.from(document.querySelectorAll('#allFoldersList option')).forEach(function(opt) {
                    if (opt.value === rowFolder) return;
                    const item = document.createElement('button');
                    item.type = 'button';
                    item.className = 'dropdown-item';
                    item.textContent = opt.value;
                    item.addEventListener('click', function() {
                        hideMenu();
                        postMessageAction(`/webmail/mail/${rowFolder}/${row.dataset.uid}/move`, { target_folder: opt.value });
                    });
                    submenu.appendChild(item);
                });
                const newItem = document.createElement('button');
                newItem.type = 'button';
                newItem.className = 'dropdown-item';
                newItem.innerHTML = '<i class="bi bi-folder-plus me-2"></i>New folder&hellip;';
                newItem.addEventListener('click', async function() {
                    hideMenu();
                    const name = await showInputPrompt('New folder name (inside "INBOX")');
                    if (!name) return;
                    const body = new URLSearchParams({ name: name, parent: 'INBOX', csrf_token: window.__csrfToken || '' });
                    const resp = await fetch('/webmail/mail/folders/add', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: body.toString() });
                    if (resp.ok) {
                        postMessageAction(`/webmail/mail/${rowFolder}/${row.dataset.uid}/move`, { target_folder: name });
                    }
                });
                submenu.appendChild(newItem);
            }

            document.querySelectorAll('.msg-row').forEach(function(row) {
                row.addEventListener('contextmenu', function(e) {
                    e.preventDefault();
                    if (row.dataset.isDraft === 'true') return; // drafts only open in compose
                    menuRow = row;
                    const isUnread = row.classList.contains('unread');
                    menu.querySelector('[data-toggle-read-label]').textContent = isUnread ? 'Mark as read' : 'Mark as unread';
                    const underTrash = row.dataset.folder && document.querySelector('.folder-row[data-folder="' + CSS.escape(row.dataset.folder) + '"][data-under-trash="true"]') !== null;
                    menu.querySelector('[data-delete-label]').textContent = underTrash ? 'Delete Permanently' : 'Delete';
                    buildMoveSubmenu(row);
                    menu.style.left = e.clientX + 'px';
                    menu.style.top = e.clientY + 'px';
                    menu.style.display = 'block';
                });
                row.addEventListener('dblclick', function(e) {
                    if (e.target.closest('.msg-check, .msg-star, .msg-group-toggle')) return;
                    if (row.dataset.isDraft === 'true') { window.location.href = row.dataset.href; return; }
                    const modalBody = document.getElementById('messageModalBody');
                    modalBody.innerHTML = '<div class="text-center text-muted py-5"><div class="spinner-border" role="status"></div></div>';
                    new bootstrap.Modal(document.getElementById('messageModal')).show();
                    fetch(row.dataset.paneHref)
                        .then(function(r) { if (!r.ok) throw new Error(r.status); return r.text(); })
                        .then(function(html) { modalBody.innerHTML = html; })
                        .catch(function() { modalBody.innerHTML = '<p class="text-danger p-3">Failed to load the message.</p>'; });
                });
            });

            menu.querySelectorAll('[data-msg-action]').forEach(function(item) {
                item.addEventListener('click', async function() {
                    const row = menuRow;
                    const action = item.dataset.msgAction;
                    if (action === 'move-toggle') return; // hover-only, handled by the submenu itself
                    hideMenu();
                    if (!row) return;
                    const folder = row.dataset.folder, uid = row.dataset.uid;
                    switch (action) {
                        case 'reply':
                            openCompose(`/webmail/mail/compose?reply=${uid}&folder=${folder}`);
                            break;
                        case 'forward':
                            openCompose(`/webmail/mail/compose?forward=${uid}&folder=${folder}`);
                            break;
                        case 'toggle-read': {
                            const isUnread = row.classList.contains('unread');
                            postMessageAction(`/webmail/mail/${folder}/bulk`, { action: isUnread ? 'read' : 'unread', uid: uid });
                            break;
                        }
                        case 'junk':
                            postMessageAction(`/webmail/mail/${folder}/${uid}/mark-junk`);
                            break;
                        case 'open-tab':
                            window.open(row.dataset.href, '_blank', 'noopener');
                            break;
                        case 'delete': {
                            const underTrash = menu.querySelector('[data-delete-label]').textContent === 'Delete Permanently';
                            const confirmMsg = underTrash ? 'Permanently delete this message? This cannot be undone.' : 'Move this message to Trash?';
                            if (await showConfirmation(confirmMsg)) postMessageAction(`/webmail/mail/${folder}/${uid}/delete`);
                            break;
                        }
                    }
                });
            });

            document.addEventListener('click', function(e) {
                if (!menu.contains(e.target)) hideMenu();
            });
            document.addEventListener('keydown', function(e) {
                if (e.key === 'Escape') hideMenu();
            });
            window.addEventListener('scroll', hideMenu, true);
        })();
    </script>
</body>
</html>
{{end}}

{{/* folderTreeNode renders one sidebar folder row plus (if any) its children,
     recursing into itself for each child — . is a *folderViewNode (webmail_mail.go's
     buildFolderView), which carries everything needed (icon, counts, active state,
     renameable/can-add-child/is-spam/is-trash) precomputed, so no shared state needs
     threading through the recursion beyond the node itself. */}}
{{define "folderTreeNode"}}
<div class="folder-row {{if .Children}}folder-has-children{{end}}" data-folder="{{.Name}}" data-renameable="{{.Renameable}}" data-can-add-subfolder="{{.CanAddKid}}" data-trash="{{.IsTrash}}" data-spam="{{.IsSpam}}" data-under-trash="{{.UnderTrash}}"{{if .Renameable}} draggable="true"{{end}}>
    {{if .Children}}
    <i class="bi bi-chevron-down folder-expand-toggle" data-folder-toggle="{{.Name}}" title="Collapse"></i>
    {{else}}
    <span class="folder-expand-spacer"></span>
    {{end}}
    {{if .Renameable}}<i class="bi bi-grip-vertical folder-drag-handle" title="Drag to reorder"></i>{{end}}
    <a href="/webmail/mail/{{.Name}}" data-folder="{{.Name}}" draggable="false" class="list-group-item list-group-item-action bg-transparent text-white folder-link flex-grow-1 d-flex justify-content-between align-items-center {{if .Active}}active{{end}}">
        <span>{{if isStandardFolder .Name}}<i class="bi {{.Icon}} me-1"></i>{{end}}{{.Name}}</span>
        {{if .Total}}
        <span class="badge {{if .Unread}}bg-primary{{else}}bg-secondary{{end}} rounded-pill folder-unread-badge" title="{{.Total}} total{{if .Unread}}, {{.Unread}} unread{{end}}">{{.Total}}{{if .Unread}} / <strong>{{.Unread}}</strong>{{end}}</span>
        {{end}}
    </a>
    {{if .Renameable}}
    <form method="post" action="/webmail/mail/folders/{{.Name}}/remove" class="d-inline">
        <button type="submit" class="btn btn-sm btn-outline-danger border-0" title="Delete folder" data-confirm="Delete folder &quot;{{.Name}}&quot;? Any mail in it moves to Trash, keeping this folder as a subfolder there so you can find it."><i class="bi bi-x-lg"></i></button>
    </form>
    {{end}}
</div>
{{if .Children}}
<div class="folder-children" data-children-of="{{.Name}}">
    {{range .Children}}{{template "folderTreeNode" .}}{{end}}
</div>
{{end}}
{{end}}
