<section class="nexus-shop-screen-shell nexus-shop-editor-shell nexus-shop-theme-{{#if preset}}{{preset.themeId}}{{else}}default{{/if}}">
    {{#if missing}}<p class="nexus-shop-config-missing">This Shop could not be found.</p>{{else}}
    <header class="nexus-shop-creator-header">
        <div class="nexus-shop-creator-title">
            <button type="button" class="nexus-shop-creator-icon nexus-shop-creator-icon-button" data-action="changeShopIcon" title="Change shop icon">
                {{#if draft.imageSrc}}<img src="{{draft.imageSrc}}" alt="{{draft.name}}">{{else}}<i class="{{#if preset}}{{preset.icon}}{{else}}fas fa-store{{/if}}"></i>{{/if}}
                <span><i class="fas fa-pen"></i></span>
            </button>
            <div>
                <small>{{#if isNew}}New Shop{{else}}Shop Editor{{/if}}</small>
                <h2>{{draft.name}}</h2>
                <p>{{#if preset}}{{preset.description}}{{else}}A custom merchant with a hand-built restock pool.{{/if}}</p>
            </div>
        </div>
        <div class="nexus-shop-editor-type-control">
            <span>Shop Type {{#if isAdvancedCustomized}}<em>Customized</em>{{/if}}</span>
            <div><select name="presetId" aria-label="Shop Type">{{#each presets}}<option value="{{id}}" {{#if selected}}selected{{/if}}>{{name}}</option>{{/each}}</select><button type="button" data-action="applyShopType" title="Apply this Shop Type"><i class="fas fa-wand-magic-sparkles"></i><b>Apply</b></button></div>
        </div>
    </header>

    <nav class="nexus-shop-editor-tabs" aria-label="Shop editor sections">
        <button type="button" class="{{#if isSettingsTab}}active{{/if}}" data-action="selectTab" data-tab="settings"><i class="fas fa-store"></i><span>Store Settings</span><small>Quick setup, current stock, and commerce</small></button>
        <button type="button" class="{{#if isPoolTab}}active{{/if}}" data-action="selectTab" data-tab="restock"><i class="fas fa-sliders"></i><span>Advanced Settings</span><small>Filters and exact potential stock</small></button>
    </nav>

    <main class="nexus-shop-editor-content">
        {{#if isPoolTab}}
        <section class="nexus-shop-editor-pool-tab">
            {{#if unavailable}}<p class="nexus-shop-editor-unavailable">No module has registered a merchandise catalog for this game system.</p>{{else}}
            <div class="nexus-stock-list-workspace nexus-shop-editor-pool-workspace">
                <aside class="nexus-stock-filter-rail">
                    <label class="nexus-stock-list-search"><i class="fas fa-magnifying-glass"></i><input type="search" name="catalogSearch" value="{{query}}" placeholder="Search available items"></label>
                    <fieldset class="nexus-stock-filter-types"><legend>Item Type</legend>{{#each typeOptions}}<label><input type="checkbox" name="catalogType" value="{{id}}" {{#if selected}}checked{{/if}}> {{label}}</label>{{/each}}</fieldset>
                    <fieldset class="nexus-stock-price-filter"><legend>Price</legend><div><input type="number" name="priceMinimumGp" min="0" step="0.01" value="{{priceMinimumGp}}" placeholder="Min"><span>–</span><input type="number" name="priceMaximumGp" min="0" step="0.01" value="{{priceMaximumGp}}" placeholder="Max"><b>gp</b></div></fieldset>
                    <div class="nexus-stock-filter-scroll">
                        {{#each filterGroups}}<details class="nexus-stock-filter-details {{#if active}}has-active-filters{{/if}}" data-filter-group-details data-group-id="{{id}}" {{#if open}}open{{/if}}><summary>{{label}}</summary><div class="nexus-stock-tristate-group">{{#each options}}<button type="button" class="state-{{state}}" data-action="cycleFilter" data-filter-group="{{../id}}" data-filter-value="{{id}}" title="Left click: include/off. Right click: exclude/off."><i class="fas {{#if include}}fa-check{{else}}{{#if exclude}}fa-xmark{{else}}fa-minus{{/if}}{{/if}}"></i><span>{{label}}</span></button>{{/each}}</div></details>{{/each}}
                        {{#each sourceGroups}}<details class="nexus-stock-filter-details nexus-stock-rules-sources {{#if active}}has-active-filters{{/if}}" data-filter-group-details data-group-id="{{id}}" {{#if open}}open{{/if}}><summary>{{label}}</summary><div class="nexus-stock-tristate-group">{{#each options}}<button type="button" class="state-{{state}}" data-action="cycleFilter" data-filter-group="{{../id}}" data-filter-value="{{id}}" title="Left click: include/off. Right click: exclude/off."><i class="fas {{#if include}}fa-check{{else}}{{#if exclude}}fa-xmark{{else}}fa-minus{{/if}}{{/if}}"></i><span>{{label}}</span></button>{{/each}}</div></details>{{/each}}
                        <p class="nexus-stock-filter-help">Available sources follow the game system's Compendium Browser configuration.</p>
                    </div>
                </aside>

                <section class="nexus-stock-list-column available">
                    <header><div><h2>Available Items</h2><small>{{availableCount}} matching · showing {{showingCount}}</small></div><nav><button type="button" data-action="addSelected" {{#unless selectedAvailableCount}}disabled{{/unless}}>Add Selected</button><button type="button" data-action="addAll" {{#unless availableCount}}disabled{{/unless}}>Add All Eligible</button></nav></header>
                    <div class="nexus-stock-list-items">{{#each availableItems}}<article class="{{#if alreadyAdded}}already-added{{/if}}"><input type="checkbox" name="availableSelection" value="{{key}}" {{#if selected}}checked{{/if}} {{#if alreadyAdded}}disabled{{/if}}><img src="{{imageSrc}}" alt=""><span><strong>{{name}}</strong><small>{{detailLabel}}</small><em>{{sourceLabel}}</em></span><b>{{priceLabel}}</b><button type="button" data-action="addItem" data-product-key="{{key}}" {{#if alreadyAdded}}disabled title="Already in this pool"{{else}}title="Add to Restock Pool"{{/if}}><i class="fas fa-chevron-right"></i></button></article>{{else}}<p class="nexus-stock-list-empty">{{#if hasTypeSelection}}No items match the current types, sources, and filters.{{else}}Select at least one Item Type.{{/if}}</p>{{/each}}{{#if hasMore}}<button type="button" class="nexus-stock-load-more" data-action="loadMore">Show 100 More</button>{{/if}}</div>
                </section>

                <section class="nexus-stock-list-column selected">
                    <header><div><h2>Restock Pool</h2><small>{{poolCount}} exact references{{#if hasMissing}} · {{missingCount}} missing{{/if}}</small></div><nav>{{#if hasMissing}}<button type="button" data-action="removeMissing">Remove Missing</button>{{/if}}<button type="button" data-action="clearPool">Clear Pool</button></nav></header>
                    <div class="nexus-stock-list-items nexus-shop-editor-pool-items">{{#each poolItems}}<article class="{{#if missing}}missing{{/if}}" data-pool-key="{{key}}"><img src="{{imageSrc}}" alt=""><span><strong>{{name}}</strong><small>{{detailLabel}}</small><em>{{sourceLabel}}</em></span><div class="nexus-shop-editor-entry-controls"><label title="Eligible for generation"><input type="checkbox" name="poolEnabled" data-pool-entry-setting {{#if enabled}}checked{{/if}}> On</label><label title="Relative draw weight">Wt <input type="number" name="poolWeight" min="1" value="{{weight}}" data-pool-entry-setting></label><label title="Zero uses Store Settings">Qty <input type="number" name="poolQuantityMin" min="0" value="{{quantity.min}}" data-pool-entry-setting>–<input type="number" name="poolQuantityMax" min="0" value="{{quantity.max}}" data-pool-entry-setting></label><label title="Blank uses source price">gp <input type="number" name="poolPriceGp" min="0" step="0.01" value="{{priceGp}}" placeholder="Auto" data-pool-entry-setting></label></div><button type="button" data-action="removePoolItem" data-product-key="{{key}}" title="Remove from Restock Pool"><i class="fas fa-xmark"></i></button></article>{{else}}<p class="nexus-stock-list-empty">Add individual, selected, or all eligible Items from the middle column.</p>{{/each}}</div>
                </section>
            </div>
            <footer class="nexus-shop-editor-pool-footer"><div>{{#if hasFilters}}<button type="button" class="nexus-stock-clear-filters" data-action="clearFilters"><i class="fas fa-filter-circle-xmark"></i> Clear Include / Exclude</button>{{/if}}<span><b>Left-click</b>: include/off · <b>Right-click</b>: exclude/off</span></div><strong>{{poolCount}} Items in Pool</strong><nav><input type="file" accept="application/json,.json" data-restock-import hidden><button type="button" data-action="importPool"><i class="fas fa-file-import"></i> Import</button><button type="button" data-action="exportPool"><i class="fas fa-file-export"></i> Export</button></nav></footer>
            {{/if}}
        </section>
        {{/if}}

        {{#if isSettingsTab}}
        <div class="nexus-shop-editor-store-workbench">
            <aside class="nexus-shop-editor-settings-rail">
                <section class="nexus-shop-editor-settings-card nexus-shop-editor-commerce-card">
                    <header><i class="fas fa-coins"></i><div><h2>Commerce</h2><small>Identity, prices, treasury, and trade directions.</small></div></header>
                    <label>Shop name<input type="text" name="shopName" value="{{draft.name}}" data-shop-setting></label>
                    <div class="nexus-shop-config-percentages"><label>Sells for<span><input type="number" name="customerPurchasePercent" min="0" max="1000" value="{{draft.customerPurchasePercent}}" data-shop-setting><em>%</em></span></label><label>Buys for<span><input type="number" name="customerSalePercent" min="0" max="1000" value="{{draft.customerSalePercent}}" data-shop-setting><em>%</em></span></label></div>
                    <div class="nexus-shop-config-treasury"><label>Treasury<span><input type="number" name="treasuryGp" min="0" step="0.01" value="{{draft.treasuryGp}}" data-shop-setting><em>gp</em></span></label><label class="nexus-shop-config-check"><input type="checkbox" name="unlimitedTreasury" {{#if draft.unlimitedTreasury}}checked{{/if}} data-shop-setting> Unlimited</label></div>
                    <div class="nexus-shop-config-toggle-list"><label><input type="checkbox" name="buyingEnabled" {{#if draft.buyingEnabled}}checked{{/if}} data-shop-setting> Sells to players</label><label><input type="checkbox" name="sellingEnabled" {{#if draft.sellingEnabled}}checked{{/if}} data-shop-setting> Buys from players</label><label><input type="checkbox" name="finiteStock" {{#if draft.finiteStock}}checked{{/if}} data-shop-setting> Finite stock</label></div>
                </section>
                <section class="nexus-shop-editor-settings-card">
                    <header><i class="fas fa-boxes-stacked"></i><div><h2>Restocking</h2><small>Size of each generated assortment.</small></div></header>
                    <div class="nexus-shop-config-ranges"><label>Products<span><input type="number" name="countMin" min="1" value="{{draft.countMin}}" data-shop-setting><em>to</em><input type="number" name="countMax" min="1" value="{{draft.countMax}}" data-shop-setting></span></label><label>Quantity each<span><input type="number" name="quantityMin" min="1" value="{{draft.quantityMin}}" data-shop-setting><em>to</em><input type="number" name="quantityMax" min="1" value="{{draft.quantityMax}}" data-shop-setting></span></label></div>
                    <div class="nexus-shop-editor-restock-actions"><button type="button" data-action="refreshGenerated" {{#unless canGenerate}}disabled{{/unless}}><i class="fas fa-dice"></i> Refresh Generated</button><button type="button" class="is-danger" data-action="replaceStock" {{#unless canGenerate}}disabled{{/unless}}><i class="fas fa-triangle-exclamation"></i> Replace Everything</button></div>
                    <p><strong>Refresh Generated</strong> preserves curated and pinned merchandise. <strong>Replace Everything</strong> clears all current merchandise first.</p>
                </section>
            </aside>
            <section class="nexus-shop-inventory-panel nexus-shop-market-inventory nexus-shop-creator-preview nexus-shop-editor-current-panel" data-stock-drop>
                <header>
                    <div><h3>Current Stock</h3><small><span data-visible-stock-count>{{stockCount}} shown</span> · {{stockCount}} total · Hover merchandise to edit</small></div>
                    <label class="nexus-shop-config-stock-search"><i class="fas fa-search"></i><input type="search" name="stockSearch" value="{{stockQuery}}" placeholder="Search stock"></label>
                </header>
                <nav class="nexus-shop-config-stock-filters" aria-label="Filter current stock">{{#each stockFilters}}<button type="button" class="{{#if selected}}is-selected{{/if}}" data-action="filterStock" data-filter="{{id}}">{{label}} <span>{{count}}</span></button>{{/each}}</nav>
                {{#if canDropItems}}<div class="nexus-shop-config-drop-hint nexus-shop-editor-drop-hint"><i class="fas fa-hand-pointer"></i><span>Drop Items here to add curated stock. Originals are not changed.</span></div>{{/if}}
                <div class="nexus-shop-grid nexus-shop-product-grid nexus-shop-compact-grid nexus-shop-editor-stock-grid">
                    {{#each stock}}
                    <article class="nexus-shop-item-card nexus-shop-creator-card {{#if pinned}}is-kept{{/if}}" data-stock-row data-row-id="{{id}}" data-origin="{{origin}}" data-pinned="{{pinned}}" data-search-text="{{searchText}}" title="{{detailLabel}}&#10;{{sourceLabel}}&#10;{{originLabel}}">
                        <img src="{{imageSrc}}" alt=""><span class="nexus-shop-card-price">{{priceLabel}}</span><span class="nexus-shop-card-quantity">×{{quantity}}</span><span class="nexus-shop-card-name">{{name}}</span>
                        {{#if pinned}}<i class="fas fa-thumbtack nexus-shop-card-state" title="Preserved when refreshing generated stock"></i>{{/if}}
                        <div class="nexus-shop-creator-card-tools nexus-shop-editor-card-tools"><button type="button" data-action="editStockPrice" data-row-id="{{id}}" title="Override price"><i class="fas fa-coins"></i></button><button type="button" data-action="changeStockQuantity" data-row-id="{{id}}" data-delta="-1" title="Decrease quantity"><i class="fas fa-minus"></i></button><button type="button" data-action="changeStockQuantity" data-row-id="{{id}}" data-delta="1" title="Increase quantity"><i class="fas fa-plus"></i></button><button type="button" data-action="togglePinnedStock" data-row-id="{{id}}" title="{{#if pinned}}Allow refresh{{else}}Preserve on refresh{{/if}}"><i class="fas fa-thumbtack"></i></button><button type="button" data-action="removeStockItem" data-row-id="{{id}}" title="Remove from current stock"><i class="fas fa-xmark"></i></button></div>
                    </article>
                    {{else}}<div class="nexus-shop-creator-blank"><i class="fas fa-store"></i><strong>No Current Stock</strong><p>Apply a Shop Type, refresh generated stock, or drop Items into this panel.</p></div>{{/each}}
                </div>
            </section>
        </div>
        {{/if}}
    </main>

    <footer class="nexus-shop-creator-footer nexus-shop-editor-footer"><span class="nexus-stock-list-unsaved" {{#unless dirty}}hidden{{/unless}}><i class="fas fa-circle"></i> Unsaved changes</span><button type="button" data-action="cancel"><i class="fas fa-xmark"></i> Cancel</button><button type="button" class="bright" data-action="saveShop"><i class="fas fa-store"></i> {{#if isNew}}Create Shop{{else}}Save Shop{{/if}}</button></footer>
    {{/if}}
</section>
