<!-- Pattern Template Editor Modal -->
<div id="pattern-template-modal" class="modal" role="dialog" aria-modal="true" aria-labelledby="pattern-template-modal-title">
    <div class="modal-content modal-content-wide">
        <div class="modal-header">
            <h2 id="pattern-template-modal-title"><svg class="icon" viewBox="0 0 24 24"><path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z"/><path d="M14 2v5a1 1 0 0 0 1 1h5"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></svg> <span data-i18n="pattern.add">Add Pattern Template</span></h2>
            <button class="modal-close-btn" onclick="closePatternTemplateModal()" title="Close" data-i18n-aria-label="aria.close">&#x2715;</button>
        </div>
        <div class="modal-body">
            <form id="pattern-template-form">
                <input type="hidden" id="pattern-template-id">

                <div id="pattern-name-group" class="form-group">
                    <div class="label-row">
                        <label for="pattern-template-name" data-i18n="pattern.name">Template Name:</label>
                        <button type="button" class="hint-toggle" onclick="toggleHint(this)" title="?">?</button>
                    </div>
                    <small class="input-hint" style="display:none" data-i18n="pattern.name.hint">A descriptive name for this rectangle layout</small>
                    <input type="text" id="pattern-template-name" data-i18n-placeholder="pattern.name.placeholder" placeholder="My Pattern Template" required>
                    <div id="pattern-tags-container"></div>
                </div>

                <div id="pattern-desc-group" class="form-group">
                    <div class="label-row">
                        <label for="pattern-template-description" data-i18n="pattern.description_label">Description (optional):</label>
                        <button type="button" class="hint-toggle" onclick="toggleHint(this)" title="?">?</button>
                    </div>
                    <small class="input-hint" style="display:none" data-i18n="pattern.description.hint">Optional notes about where or how this pattern is used</small>
                    <input type="text" id="pattern-template-description" data-i18n-placeholder="pattern.description_placeholder" placeholder="Describe this pattern...">
                </div>

                <!-- Visual Editor -->
                <div class="form-group">
                    <div class="label-row">
                        <label data-i18n="pattern.visual_editor">Visual Editor</label>
                        <button type="button" class="hint-toggle" onclick="toggleHint(this)" title="?">?</button>
                    </div>
                    <small class="input-hint" style="display:none" data-i18n="pattern.visual_editor.hint">Click + buttons to add rectangles. Drag edges to resize, drag inside to move.</small>
                    <div class="pattern-bg-row">
                        <select id="pattern-bg-source"></select>
                        <button type="button" class="btn btn-icon btn-secondary pattern-capture-btn" onclick="capturePatternBackground()" title="Capture Background" data-i18n-title="pattern.capture_bg"><svg class="icon" viewBox="0 0 24 24"><path d="M13.997 4a2 2 0 0 1 1.76 1.05l.486.9A2 2 0 0 0 18.003 7H20a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h1.997a2 2 0 0 0 1.759-1.048l.489-.904A2 2 0 0 1 10.004 4z"/><circle cx="12" cy="13" r="3"/></svg></button>
                    </div>
                    <div class="pattern-canvas-container">
                        <canvas id="pattern-canvas"></canvas>
                    </div>
                </div>

                <!-- Precise coordinate list -->
                <div class="form-group">
                    <div id="pattern-rect-labels" class="pattern-rect-labels">
                        <span data-i18n="pattern.rect.name">Name</span>
                        <span data-i18n="pattern.rect.x">X</span>
                        <span data-i18n="pattern.rect.y">Y</span>
                        <span data-i18n="pattern.rect.width">W</span>
                        <span data-i18n="pattern.rect.height">H</span>
                        <span></span>
                    </div>
                    <div id="pattern-rect-list" class="pattern-rect-list"></div>
                </div>

                <div id="pattern-template-error" class="error-message" style="display: none;"></div>
            </form>
        </div>
        <div class="modal-footer">
            <button class="btn btn-icon btn-secondary" onclick="closePatternTemplateModal()" title="Cancel" data-i18n-aria-label="aria.cancel">&#x2715;</button>
            <button class="btn btn-icon btn-primary" onclick="savePatternTemplate()" title="Save" data-i18n-aria-label="aria.save">&#x2713;</button>
        </div>
    </div>
</div>
