<section class="nexus-shop-screen-shell nexus-shop-editor-shell fantasy-marketplace-editor-shell nexus-shop-theme-fantasy">
    {{#if missing}}<p class="nexus-shop-config-missing">This settlement Marketplace 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="changeIcon" title="Change marketplace icon">
                {{#if imageSrc}}<img src="{{imageSrc}}" alt="{{name}}">{{else}}<i class="fas fa-store"></i>{{/if}}
                <span><i class="fas fa-pen"></i></span>
            </button>
            <div>
                <small>Settlement Marketplace</small>
                <h2>{{name}}</h2>
                <p>The trading face of {{locationName}}'s local exports and trade network.</p>
            </div>
        </div>
        <div class="fantasy-marketplace-header-tools"><button type="button" data-action="manageTradeGoods"><i class="fas fa-boxes-stacked"></i> Trade Goods &amp; Prices</button><div class="fantasy-marketplace-specialized-label"><span>Specialized Shop</span><strong><i class="fas fa-scale-balanced"></i> Marketplace</strong></div></div>
    </header>

    <nav class="nexus-shop-editor-tabs" aria-label="Marketplace editor sections">
        <button type="button" class="{{#if isEconomyTab}}active{{/if}}" data-action="selectTab" data-tab="economy"><i class="fas fa-chart-column"></i><span>Local Exports</span><small>Define goods produced at this settlement</small></button>
        <button type="button" class="{{#if isStockTab}}active{{/if}}" data-action="selectTab" data-tab="stock"><i class="fas fa-boxes-stacked"></i><span>Market &amp; Stock</span><small>Marketplace rules and current assortment</small></button>
    </nav>

    <main class="nexus-shop-editor-content fantasy-marketplace-editor-content">
        {{#if isEconomyTab}}
        <div class="fantasy-marketplace-economy-layout">
            <section class="fantasy-marketplace-goods-browser">
                <header>
                    <div><h2>Locally Produced Goods</h2><p>These goods are cheaper here and become available throughout the connected trade network.</p></div>
                    <label><i class="fas fa-magnifying-glass"></i><input type="search" name="marketGoodsSearch" value="{{query}}" placeholder="Search trade goods"></label>
                </header>
                <div class="fantasy-marketplace-state-summary">
                    <span class="is-produced"><i class="fas fa-arrow-up"></i> Produces <b>{{producedCount}} / {{goodsRoleLimit}}</b></span>
                    <em>Goods missing from a buyer's trade network can earn a higher sale price.</em>
                </div>
                <div class="fantasy-marketplace-goods-grid">
                    {{#each goods}}
                    <article class="fantasy-marketplace-good {{#if produced}}is-produced{{/if}}" data-market-good data-search-text="{{searchText}}">
                        <img src="{{imageSrc}}" alt="">
                        <span><strong>{{name}}</strong><small>{{familyLabel}} · {{valueLabel}}</small></span>
                        <div class="fantasy-marketplace-good-state">
                            <button type="button" class="{{#if neutral}}active{{/if}}" data-action="setGoodState" data-good-id="{{id}}" data-state="neutral" title="No special role"><i class="fas fa-minus"></i></button>
                            <button type="button" class="produced {{#if produced}}active{{/if}}" data-action="setGoodState" data-good-id="{{id}}" data-state="produced" title="Produced locally" {{#if producedDisabled}}disabled{{/if}}><i class="fas fa-arrow-up"></i> Produces</button>
                        </div>
                    </article>
                    {{/each}}
                </div>
            </section>
        </div>
        {{else}}
        <div class="fantasy-marketplace-stock-layout">
        <aside class="fantasy-marketplace-settings-column">
            <section class="nexus-shop-editor-settings-card fantasy-marketplace-settings-card fantasy-marketplace-unified-settings">
                <header><i class="fas fa-store"></i><div><h2>Marketplace Settings</h2><small>Identity, pricing, funds, and stock generation.</small></div></header>
                <div class="fantasy-marketplace-settings-section">
                    <h3>Marketplace</h3>
                    <label>Marketplace name<input type="text" name="marketName" value="{{name}}" data-market-setting></label>
                    {{#if naming.isDerived}}<div class="fantasy-marketplace-name-mode is-derived"><i class="fas fa-link"></i><span>Follows the settlement name. Editing this field makes it custom.</span></div>{{/if}}
                    {{#if naming.canRestore}}<div class="fantasy-marketplace-name-mode is-custom"><i class="fas fa-pen"></i><span>Custom Marketplace name.</span><button type="button" data-action="restoreGeneratedName"><i class="fas fa-rotate-left"></i> Restore Generated Name</button></div>{{/if}}
                    <div class="fantasy-marketplace-treasury-row">
                        <label>Market treasury<input type="number" name="treasuryGp" min="0" step="0.01" value="{{treasuryGp}}" data-market-setting></label>
                        <label class="fantasy-marketplace-check"><input type="checkbox" name="unlimitedTreasury" {{#if unlimitedTreasury}}checked{{/if}} data-market-setting> Unlimited</label>
                    </div>
                </div>
                <div class="fantasy-marketplace-settings-section">
                    <h3>Trade <small>Prices follow the world trade rates</small></h3>
                    <label class="fantasy-marketplace-check"><input type="checkbox" name="buysTradeGoods" {{#if rules.buysTradeGoods}}checked{{/if}} data-market-setting> Buys trade goods from players</label>
                    <p>Local exports and supplied goods use <strong>{{rules.regionalPercent}}%</strong>. Towns pay <strong>100%</strong> for missing goods; cities pay <strong>{{rules.cityImportPercent}}%</strong>. Change these rates from Trade Goods &amp; Prices.</p>
                </div>
                <div class="fantasy-marketplace-settings-section">
                    <h3>Stock Generation <small>Local exports, regional variety, and quantities</small></h3>
                    <div class="fantasy-marketplace-generation-grid">
                        <label>Network goods stocked <span class="fantasy-marketplace-field-note">Goods drawn from this trade network, in addition to every local export.</span><input type="number" name="regionalVariety" min="0" max="10" value="{{rules.regionalVariety}}" data-market-setting></label>
                        <div class="fantasy-marketplace-quantity-row"><label>Quantity each<input type="number" name="quantityMin" min="1" max="99" value="{{rules.quantityMin}}" data-market-setting></label><span>to</span><label>&nbsp;<input type="number" name="quantityMax" min="1" max="99" value="{{rules.quantityMax}}" data-market-setting></label></div>
                    </div>
                    <p>Every local export is stocked automatically. Additional stock is drawn only from goods available through this trade network.</p>
                </div>
            </section>
        </aside>
        <section class="fantasy-marketplace-stock-surface">
            <header>
                <div><h2>Current Market Stock</h2><p><strong>{{stockCount}}</strong> trade goods currently available. Generated stock combines local exports with goods from the network.</p></div>
                <button type="button" data-action="rebuildStock"><i class="fas fa-arrows-rotate"></i> Rebuild Market Stock</button>
            </header>
            {{#if stockNeedsRebuild}}<div class="fantasy-marketplace-sync-note fantasy-marketplace-stock-sync"><i class="fas fa-circle-info"></i> This preview reflects the previous economy. Rebuild it now, or save to rebuild it automatically.</div>{{/if}}
            {{#if invalidStockCount}}<div class="fantasy-marketplace-warning"><i class="fas fa-triangle-exclamation"></i> {{invalidStockCount}} incompatible non-trade item(s) were found. They will be removed when this Marketplace is saved.</div>{{/if}}
            <div class="nexus-shop-grid nexus-shop-product-grid nexus-shop-compact-grid fantasy-marketplace-stock-grid">
                {{#each stock}}
                <article class="nexus-shop-item-card nexus-shop-creator-card {{#if produced}}is-kept{{/if}}" title="{{name}} · {{valueLabel}}">
                    <img src="{{imageSrc}}" alt=""><span class="nexus-shop-card-price">{{valueLabel}}</span><span class="nexus-shop-card-quantity">×{{quantity}}</span><span class="nexus-shop-card-name">{{name}}</span>
                    {{#if produced}}<i class="fas fa-arrow-up nexus-shop-card-state" title="Produced locally"></i>{{else}}{{#if supplied}}<i class="fas fa-route nexus-shop-card-state" title="Available through this trade network"></i>{{/if}}{{/if}}
                </article>
                {{else}}<div class="nexus-shop-creator-blank"><i class="fas fa-box-open"></i><strong>No Market Stock</strong><p>Define the Local Economy, then rebuild the Marketplace stock.</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="save"><i class="fas fa-store"></i> Save Marketplace</button></footer>
    {{/if}}
</section>
