<link rel="stylesheet" href="mail/mail.css" />

<div class="mail-layout">
    <!-- Sidebar -->
    <div class="panel mail-sidebar">
        <div class="compose-section">
            <button class="compose-btn" onclick="openCompose()">
                <svg
                    width="18"
                    height="18"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path d="M12 5v14M5 12h14" />
                </svg>
                <span data-i18n="email-compose">Compose</span>
            </button>
        </div>

        <!-- Folder List -->
        <div class="folders-section">
            <div
                class="nav-item active"
                data-folder="inbox"
                hx-get="/api/ui/email/list?folder=inbox"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polyline points="22 12 16 12 14 15 10 15 8 12 2 12" />
                    <path
                        d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"
                    />
                </svg>
                <span data-i18n="email-inbox">Inbox</span>
                <span class="folder-badge unread" id="inbox-count">0</span>
            </div>
            <div
                class="nav-item"
                data-folder="starred"
                hx-get="/api/ui/email/list?folder=starred"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polygon
                        points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"
                    />
                </svg>
                <span data-i18n="email-starred">Starred</span>
            </div>
            <div
                class="nav-item"
                data-folder="sent"
                hx-get="/api/ui/email/list?folder=sent"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="22" y1="2" x2="11" y2="13" />
                    <polygon points="22 2 15 22 11 13 2 9 22 2" />
                </svg>
                <span data-i18n="email-sent">Sent</span>
            </div>
            <div
                class="nav-item"
                data-folder="scheduled"
                hx-get="/api/ui/email/list?folder=scheduled"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <circle cx="12" cy="12" r="10" />
                    <polyline points="12 6 12 12 16 14" />
                </svg>
                <span data-i18n="email-scheduled">Scheduled</span>
                <span class="folder-badge" id="scheduled-count">0</span>
            </div>
            <div
                class="nav-item"
                data-folder="drafts"
                hx-get="/api/ui/email/list?folder=drafts"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path
                        d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"
                    />
                    <polyline points="14 2 14 8 20 8" />
                    <line x1="16" y1="13" x2="8" y2="13" />
                    <line x1="16" y1="17" x2="8" y2="17" />
                </svg>
                <span data-i18n="email-drafts">Drafts</span>
                <span class="folder-badge" id="drafts-count">0</span>
            </div>
            <div
                class="nav-item"
                data-folder="tracking"
                hx-get="/api/email/tracking/list"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
                    <circle cx="12" cy="12" r="3" />
                </svg>
                <span data-i18n="email-tracking">Tracking</span>
            </div>
            <div
                class="nav-item"
                data-folder="spam"
                hx-get="/api/ui/email/list?folder=spam"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path
                        d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"
                    />
                    <line x1="12" y1="9" x2="12" y2="13" />
                    <line x1="12" y1="17" x2="12.01" y2="17" />
                </svg>
                <span data-i18n="email-spam">Spam</span>
            </div>
            <div
                class="nav-item"
                data-folder="trash"
                hx-get="/api/ui/email/list?folder=trash"
                hx-target="closest .window-body #mail-list"
                hx-swap="innerHTML"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polyline points="3 6 5 6 21 6" />
                    <path
                        d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
                    />
                </svg>
                <span data-i18n="email-trash">Trash</span>
            </div>
        </div>

        <!-- Accounts Section -->
        <div class="labels-section">
            <div class="section-header">
                <span>Accounts</span>
                <button
                    class="icon-btn"
                    onclick="document.getElementById('add-account-modal').showModal()"
                    title="Add account"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="12" y1="5" x2="12" y2="19" />
                        <line x1="5" y1="12" x2="19" y2="12" />
                    </svg>
                </button>
            </div>
            <div
                id="accounts-list"
                hx-get="/api/ui/email/accounts"
                hx-trigger="load"
                hx-swap="innerHTML"
            >
                <!-- Accounts loaded here -->
            </div>
        </div>

        <!-- Labels Section -->
        <div class="labels-section">
            <div class="section-header">
                <span>Labels</span>
                <button
                    class="icon-btn"
                    onclick="openLabelManager()"
                    title="Manage labels"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="12" y1="5" x2="12" y2="19" />
                        <line x1="5" y1="12" x2="19" y2="12" />
                    </svg>
                </button>
            </div>
            <div
                id="labels-list"
                hx-get="/api/ui/email/labels"
                hx-trigger="load"
                hx-swap="innerHTML"
            >
                <div class="label-item" style="--label-color: #ef4444">
                    <span class="label-dot"></span>
                    <span>Important</span>
                </div>
                <div class="label-item" style="--label-color: #f59e0b">
                    <span class="label-dot"></span>
                    <span>Work</span>
                </div>
                <div class="label-item" style="--label-color: #22c55e">
                    <span class="label-dot"></span>
                    <span>Personal</span>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="quick-actions">
            <button
                class="quick-action-btn"
                onclick="openTemplates()"
                title="Email Templates"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
                    <line x1="3" y1="9" x2="21" y2="9" />
                    <line x1="9" y1="21" x2="9" y2="9" />
                </svg>
                <span>Templates</span>
            </button>
            <button
                class="quick-action-btn"
                onclick="openSignatures()"
                title="Signatures"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path
                        d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"
                    />
                </svg>
                <span>Signatures</span>
            </button>
            <button
                class="quick-action-btn"
                onclick="openRules()"
                title="Email Rules"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polygon
                        points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"
                    />
                </svg>
                <span>Rules</span>
            </button>
            <button
                class="quick-action-btn"
                onclick="openAutoResponder()"
                title="Out of Office"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <circle cx="12" cy="12" r="10" />
                    <path d="M8 14s1.5 2 4 2 4-2 4-2" />
                    <line x1="9" y1="9" x2="9.01" y2="9" />
                    <line x1="15" y1="9" x2="15.01" y2="9" />
                </svg>
                <span>Auto-reply</span>
            </button>
        </div>
    </div>

    <!-- Mail List -->
    <div class="panel mail-list">
        <div class="list-header">
            <div class="list-header-left">
                <input
                    type="checkbox"
                    class="select-all"
                    onchange="toggleSelectAll(this)"
                    title="Select all"
                />
                <h3 id="folder-title">Inbox</h3>
            </div>
            <div class="list-header-right">
                <div class="search-box">
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <circle cx="11" cy="11" r="8" />
                        <line x1="21" y1="21" x2="16.65" y2="16.65" />
                    </svg>
                    <input
                        type="text"
                        placeholder="Search emails..."
                        id="email-search"
                        hx-get="/api/ui/email/search"
                        hx-trigger="keyup changed delay:300ms"
                        hx-target="closest .window-body #mail-list"
                        hx-include="this"
                        name="q"
                    />
                </div>
                <button
                    class="icon-btn"
                    onclick="refreshMailList()"
                    title="Refresh"
                >
                    <svg
                        width="16"
                        height="16"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <polyline points="23 4 23 10 17 10" />
                        <polyline points="1 20 1 14 7 14" />
                        <path
                            d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"
                        />
                    </svg>
                </button>
            </div>
        </div>

        <!-- Bulk Actions Bar (hidden by default) -->
        <div class="bulk-actions" id="bulk-actions" style="display: none">
            <span class="selected-count">0 selected</span>
            <button
                class="bulk-btn"
                onclick="archiveSelected()"
                title="Archive"
            >
                <svg
                    width="14"
                    height="14"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polyline points="21 8 21 21 3 21 3 8" />
                    <rect x="1" y="3" width="22" height="5" />
                    <line x1="10" y1="12" x2="14" y2="12" />
                </svg>
            </button>
            <button
                class="bulk-btn"
                onclick="markAsRead()"
                title="Mark as read"
            >
                <svg
                    width="14"
                    height="14"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
                    <polyline points="22 4 12 14.01 9 11.01" />
                </svg>
            </button>
            <button
                class="bulk-btn"
                onclick="addLabelToSelected()"
                title="Add label"
            >
                <svg
                    width="14"
                    height="14"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <path
                        d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"
                    />
                    <line x1="7" y1="7" x2="7.01" y2="7" />
                </svg>
            </button>
            <button
                class="bulk-btn danger"
                onclick="deleteSelected()"
                title="Delete"
            >
                <svg
                    width="14"
                    height="14"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <polyline points="3 6 5 6 21 6" />
                    <path
                        d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
                    />
                </svg>
            </button>
        </div>

        <div
            id="mail-list"
            hx-get="/api/ui/email/list?folder=inbox"
            hx-trigger="load"
            hx-swap="innerHTML"
        >
            <div class="loading-state">
                <div class="spinner"></div>
                <p>Loading emails...</p>
            </div>
        </div>
    </div>

    <!-- Mail Content -->
    <div class="panel mail-content">
        <div id="mail-content">
            <div class="empty-state">
                <svg
                    width="64"
                    height="64"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="1.5"
                >
                    <path
                        d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"
                    />
                    <polyline points="22,6 12,13 2,6" />
                </svg>
                <h3>Select an email to read</h3>
                <p>Choose an email from the list to view its contents</p>
            </div>
        </div>
    </div>
</div>

<!-- Compose Modal -->
<dialog id="compose-modal" class="modal">
    <div class="modal-content modal-large">
        <div class="modal-header">
            <h2>New Message</h2>
            <div class="compose-actions-top">
                <button
                    class="icon-btn"
                    onclick="minimizeCompose()"
                    title="Minimize"
                >
                    <svg
                        width="16"
                        height="16"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="5" y1="12" x2="19" y2="12" />
                    </svg>
                </button>
                <button class="icon-btn" onclick="closeCompose()" title="Close">
                    <svg
                        width="16"
                        height="16"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="18" y1="6" x2="6" y2="18" />
                        <line x1="6" y1="6" x2="18" y2="18" />
                    </svg>
                </button>
            </div>
        </div>
        <form
            id="compose-form"
            hx-post="/api/email/send"
            hx-target="#mail-content"
            hx-swap="innerHTML"
        >
            <div class="compose-fields">
                <div class="field-row">
                    <label>To:</label>
                    <input
                        type="text"
                        name="to"
                        id="compose-to"
                        placeholder="Recipients"
                        required
                        autocomplete="email"
                    />
                    <button
                        type="button"
                        class="field-toggle"
                        onclick="toggleCcBcc()"
                    >
                        Cc/Bcc
                    </button>
                </div>
                <div class="field-row cc-bcc" style="display: none">
                    <label>Cc:</label>
                    <input
                        type="text"
                        name="cc"
                        id="compose-cc"
                        placeholder="Carbon copy"
                    />
                </div>
                <div class="field-row cc-bcc" style="display: none">
                    <label>Bcc:</label>
                    <input
                        type="text"
                        name="bcc"
                        id="compose-bcc"
                        placeholder="Blind carbon copy"
                    />
                </div>
                <div class="field-row">
                    <label>Subject:</label>
                    <input
                        type="text"
                        name="subject"
                        id="compose-subject"
                        placeholder="Subject"
                    />
                </div>
            </div>
            <div class="compose-toolbar">
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Bold"
                    onclick="formatText('bold')"
                >
                    <b>B</b>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Italic"
                    onclick="formatText('italic')"
                >
                    <i>I</i>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Underline"
                    onclick="formatText('underline')"
                >
                    <u>U</u>
                </button>
                <span class="toolbar-divider"></span>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Bullet list"
                    onclick="formatText('insertUnorderedList')"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="8" y1="6" x2="21" y2="6" />
                        <line x1="8" y1="12" x2="21" y2="12" />
                        <line x1="8" y1="18" x2="21" y2="18" />
                        <line x1="3" y1="6" x2="3.01" y2="6" />
                        <line x1="3" y1="12" x2="3.01" y2="12" />
                        <line x1="3" y1="18" x2="3.01" y2="18" />
                    </svg>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Numbered list"
                    onclick="formatText('insertOrderedList')"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <line x1="10" y1="6" x2="21" y2="6" />
                        <line x1="10" y1="12" x2="21" y2="12" />
                        <line x1="10" y1="18" x2="21" y2="18" />
                        <path d="M4 6h1v4" />
                        <path d="M4 10h2" />
                        <path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1" />
                    </svg>
                </button>
                <span class="toolbar-divider"></span>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Attach file"
                    onclick="attachFile()"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <path
                            d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"
                        />
                    </svg>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Insert link"
                    onclick="insertLink()"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <path
                            d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"
                        />
                        <path
                            d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"
                        />
                    </svg>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Insert image"
                    onclick="insertImage()"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <rect
                            x="3"
                            y="3"
                            width="18"
                            height="18"
                            rx="2"
                            ry="2"
                        />
                        <circle cx="8.5" cy="8.5" r="1.5" />
                        <polyline points="21 15 16 10 5 21" />
                    </svg>
                </button>
                <span class="toolbar-divider"></span>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Insert template"
                    onclick="showTemplateSelector()"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <rect
                            x="3"
                            y="3"
                            width="18"
                            height="18"
                            rx="2"
                            ry="2"
                        />
                        <line x1="3" y1="9" x2="21" y2="9" />
                        <line x1="9" y1="21" x2="9" y2="9" />
                    </svg>
                </button>
                <button
                    type="button"
                    class="toolbar-btn"
                    title="Insert signature"
                    onclick="insertSignature()"
                >
                    <svg
                        width="14"
                        height="14"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                    >
                        <path
                            d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"
                        />
                    </svg>
                </button>
            </div>
            <div
                class="compose-body"
                contenteditable="true"
                id="compose-body"
                placeholder="Write your message..."
            ></div>
            <input type="hidden" name="body" id="compose-body-hidden" />
            <div class="compose-attachments" id="compose-attachments"></div>
            <div class="compose-footer">
                <div class="compose-options">
                    <label class="option-checkbox">
                        <input type="checkbox" name="track_opens" checked />
                        <span>Track opens</span>
                    </label>
                    <label class="option-checkbox">
                        <input type="checkbox" name="request_receipt" />
                        <span>Request read receipt</span>
                    </label>
                    <select name="priority" class="priority-select">
                        <option value="normal">Normal priority</option>
                        <option value="high">High priority</option>
                        <option value="low">Low priority</option>
                    </select>
                </div>
                <div class="compose-actions">
                    <button
                        type="button"
                        class="btn-secondary"
                        hx-post="/api/email/draft"
                        hx-include="#compose-form"
                        hx-swap="none"
                        onclick="prepareSubmit()"
                    >
                        Save Draft
                    </button>
                    <div class="send-group">
                        <button
                            type="submit"
                            class="btn-primary"
                            onclick="prepareSubmit()"
                        >
                            <svg
                                width="14"
                                height="14"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                stroke-width="2"
                            >
                                <line x1="22" y1="2" x2="11" y2="13" />
                                <polygon points="22 2 15 22 11 13 2 9 22 2" />
                            </svg>
                            Send
                        </button>
                        <button
                            type="button"
                            class="btn-primary dropdown-toggle"
                            onclick="toggleScheduleMenu()"
                        >
                            <svg
                                width="12"
                                height="12"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                stroke-width="2"
                            >
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </button>
                        <div class="schedule-menu" id="schedule-menu">
                            <div
                                class="schedule-option"
                                onclick="scheduleSend('tomorrow-morning')"
                            >
                                <svg
                                    width="14"
                                    height="14"
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    stroke-width="2"
                                >
                                    <circle cx="12" cy="12" r="5" />
                                    <line x1="12" y1="1" x2="12" y2="3" />
                                    <line x1="12" y1="21" x2="12" y2="23" />
                                    <line
                                        x1="4.22"
                                        y1="4.22"
                                        x2="5.64"
                                        y2="5.64"
                                    />
                                    <line
                                        x1="18.36"
                                        y1="18.36"
                                        x2="19.78"
                                        y2="19.78"
                                    />
                                    <line x1="1" y1="12" x2="3" y2="12" />
                                    <line x1="21" y1="12" x2="23" y2="12" />
                                    <line
                                        x1="4.22"
                                        y1="19.78"
                                        x2="5.64"
                                        y2="18.36"
                                    />
                                    <line
                                        x1="18.36"
                                        y1="5.64"
                                        x2="19.78"
                                        y2="4.22"
                                    />
                                </svg>
                                <span>Tomorrow morning (8:00 AM)</span>
                            </div>
                            <div
                                class="schedule-option"
                                onclick="scheduleSend('tomorrow-afternoon')"
                            >
                                <svg
                                    width="14"
                                    height="14"
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    stroke-width="2"
                                >
                                    <circle cx="12" cy="12" r="10" />
                                    <polyline points="12 6 12 12 16 14" />
                                </svg>
                                <span>Tomorrow afternoon (1:00 PM)</span>
                            </div>
                            <div
                                class="schedule-option"
                                onclick="scheduleSend('monday')"
                            >
                                <svg
                                    width="14"
                                    height="14"
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    stroke-width="2"
                                >
                                    <rect
                                        x="3"
                                        y="4"
                                        width="18"
                                        height="18"
                                        rx="2"
                                        ry="2"
                                    />
                                    <line x1="16" y1="2" x2="16" y2="6" />
                                    <line x1="8" y1="2" x2="8" y2="6" />
                                    <line x1="3" y1="10" x2="21" y2="10" />
                                </svg>
                                <span>Monday morning (8:00 AM)</span>
                            </div>
                            <div class="schedule-divider"></div>
                            <div
                                class="schedule-option"
                                onclick="openCustomSchedule()"
                            >
                                <svg
                                    width="14"
                                    height="14"
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    stroke-width="2"
                                >
                                    <circle cx="12" cy="12" r="3" />
                                    <path
                                        d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"
                                    />
                                </svg>
                                <span>Pick date & time...</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</dialog>

<!-- Templates Modal -->
<dialog id="templates-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Email Templates</h2>
            <button class="icon-btn" onclick="closeTemplates()">
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <div
            class="templates-list"
            id="templates-list"
            hx-get="/api/ui/email/templates"
            hx-trigger="load"
            hx-swap="innerHTML"
        >
            <div class="loading-state"><div class="spinner"></div></div>
        </div>
        <div class="modal-footer">
            <button class="btn-secondary" onclick="closeTemplates()">
                Cancel
            </button>
            <button class="btn-primary" onclick="createNewTemplate()">
                + New Template
            </button>
        </div>
    </div>
</dialog>

<!-- Signatures Modal -->
<dialog id="signatures-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Email Signatures</h2>
            <button class="icon-btn" onclick="closeSignatures()">
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <div
            class="signatures-list"
            id="signatures-list"
            hx-get="/api/ui/email/signatures"
            hx-trigger="load"
            hx-swap="innerHTML"
        >
            <div class="loading-state"><div class="spinner"></div></div>
        </div>
        <div class="modal-footer">
            <button class="btn-secondary" onclick="closeSignatures()">
                Cancel
            </button>
            <button class="btn-primary" onclick="createNewSignature()">
                + New Signature
            </button>
        </div>
    </div>
</dialog>

<!-- Rules Modal -->
<dialog id="rules-modal" class="modal">
    <div class="modal-content modal-large">
        <div class="modal-header">
            <h2>Email Rules</h2>
            <button class="icon-btn" onclick="closeRules()">
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <div
            class="rules-list"
            id="rules-list"
            hx-get="/api/ui/email/rules"
            hx-trigger="load"
            hx-swap="innerHTML"
        >
            <div class="loading-state"><div class="spinner"></div></div>
        </div>
        <div class="modal-footer">
            <button class="btn-secondary" onclick="closeRules()">Cancel</button>
            <button class="btn-primary" onclick="createNewRule()">
                + New Rule
            </button>
        </div>
    </div>
</dialog>

<!-- Auto-Responder Modal -->
<dialog id="autoresponder-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Automatic Replies</h2>
            <button class="icon-btn" onclick="closeAutoResponder()">
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <form
            id="autoresponder-form"
            hx-post="/api/ui/email/auto-responder"
            hx-swap="none"
        >
            <div class="form-group">
                <label class="toggle-label">
                    <input
                        type="checkbox"
                        name="enabled"
                        id="autoresponder-enabled"
                    />
                    <span class="toggle-switch"></span>
                    <span>Send automatic replies</span>
                </label>
            </div>
            <div class="form-group">
                <label>Start date</label>
                <input
                    type="datetime-local"
                    name="start_date"
                    id="autoresponder-start"
                />
            </div>
            <div class="form-group">
                <label>End date</label>
                <input
                    type="datetime-local"
                    name="end_date"
                    id="autoresponder-end"
                />
            </div>
            <div class="form-group">
                <label>Subject</label>
                <input
                    type="text"
                    name="subject"
                    value="Out of Office"
                    placeholder="Reply subject"
                />
            </div>
            <div class="form-group">
                <label>Message</label>
                <textarea
                    name="body"
                    rows="6"
                    placeholder="Your automatic reply message..."
                ></textarea>
            </div>
            <div class="form-group">
                <label class="checkbox-label">
                    <input type="checkbox" name="internal_only" />
                    <span>Send only to people in my organization</span>
                </label>
            </div>
        </form>
        <div class="modal-footer">
            <button class="btn-secondary" onclick="closeAutoResponder()">
                Cancel
            </button>
            <button class="btn-primary" onclick="saveAutoResponder()">
                Save
            </button>
        </div>
    </div>
</dialog>

<!-- Schedule Picker Modal -->
<!-- Add Account Modal -->
<dialog id="add-account-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Add Email Account</h2>
            <button
                class="icon-btn"
                onclick="document.getElementById('add-account-modal').close()"
            >
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <form
            hx-post="/api/email/accounts/add"
            hx-target="#accounts-list"
            hx-swap="innerHTML"
            hx-on::after-request="if(event.detail.successful) document.getElementById('add-account-modal').close()"
        >
            <div class="form-group">
                <label>Account Name</label>
                <input
                    type="text"
                    name="name"
                    placeholder="Work, Personal, etc."
                    required
                />
            </div>
            <div class="form-group">
                <label>Email Address</label>
                <input
                    type="email"
                    name="email"
                    placeholder="you@example.com"
                    required
                />
            </div>
            <div class="form-group">
                <label>IMAP Server</label>
                <input
                    type="text"
                    name="imap_server"
                    placeholder="imap.example.com"
                    required
                />
            </div>
            <div class="form-group">
                <label>IMAP Port</label>
                <input type="number" name="imap_port" value="993" required />
            </div>
            <div class="form-group">
                <label>SMTP Server</label>
                <input
                    type="text"
                    name="smtp_server"
                    placeholder="smtp.example.com"
                    required
                />
            </div>
            <div class="form-group">
                <label>SMTP Port</label>
                <input type="number" name="smtp_port" value="587" required />
            </div>
            <div class="form-group">
                <label>Username</label>
                <input
                    type="text"
                    name="username"
                    placeholder="username or email"
                    required
                    autocomplete="username"
                />
            </div>
            <div class="form-group">
                <label>Password</label>
                <input
                    type="password"
                    name="password"
                    placeholder="••••••••"
                    required
                    autocomplete="current-password"
                />
            </div>
            <div class="form-group">
                <label class="checkbox-label">
                    <input type="checkbox" name="use_ssl" checked />
                    <span>Use SSL/TLS</span>
                </label>
            </div>
            <div class="modal-footer">
                <button
                    type="button"
                    class="btn-secondary"
                    onclick="document.getElementById('add-account-modal').close()"
                >
                    Cancel
                </button>
                <button type="submit" class="btn-primary">Add Account</button>
            </div>
        </form>
    </div>
</dialog>

<dialog id="schedule-modal" class="modal">
    <div class="modal-content modal-small">
        <div class="modal-header">
            <h2>Schedule Send</h2>
            <button class="icon-btn" onclick="closeScheduleModal()">
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                >
                    <line x1="18" y1="6" x2="6" y2="18" />
                    <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
            </button>
        </div>
        <div class="schedule-form">
            <div class="form-group">
                <label>Date</label>
                <input type="date" id="schedule-date" min="" />
            </div>
            <div class="form-group">
                <label>Time</label>
                <input type="time" id="schedule-time" value="09:00" />
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn-secondary" onclick="closeScheduleModal()">
                Cancel
            </button>
            <button class="btn-primary" onclick="confirmSchedule()">
                Schedule
            </button>
        </div>
    </div>
</dialog>

<link rel="stylesheet" href="/suite/mail/mail.css" />
<script src="/suite/mail/mail.js"></script>
