<form class="myriad-system item-sheet theme-{{sheetTheme}}" autocomplete="off">
  <header class="sheet-header">
    {{#if hasEditableImage}}
      <img class="profile-img" src="{{item.img}}" data-edit="img" title="{{item.name}}" />
    {{else}}
      <i class="profile-img item-type-icon {{itemTypeIcon}}" title="{{itemTypeLabel}}"></i>
    {{/if}}
    <div class="header-details">
      <h1><input name="name" type="text" value="{{item.name}}" /></h1>
      <p class="item-type">{{itemTypeLabel}}</p>
    </div>
  </header>

  <section class="item-fields">

    {{#if (eq item.type "skill")}}
    {{#if skillUsesGrades}}
    <div class="field-row">
      <label>{{localize "MYRIADES.Sheet.grade"}}</label>
      <div class="grade-radio-group">
        {{#each gradeOptions as |opt|}}
        <label class="grade-radio-option{{#if opt.selected}} is-selected{{/if}}">
          <input type="radio" name="system.grade" value="{{opt.value}}" {{#if opt.selected}}checked{{/if}} />
          {{opt.roman}}
        </label>
        {{/each}}
      </div>
    </div>
    {{/if}}
    {{else}}
    <div class="field-row">
      <label>{{localize "MYRIADES.Sheet.grade"}}</label>
      <div class="grade-radio-group">
        {{#each gradeOptions as |opt|}}
        <label class="grade-radio-option{{#if opt.selected}} is-selected{{/if}}">
          <input type="radio" name="system.grade" value="{{opt.value}}" {{#if opt.selected}}checked{{/if}} />
          {{opt.roman}}
        </label>
        {{/each}}
      </div>
    </div>
    {{/if}}

    {{#if (eq item.type "skill")}}
      <fieldset class="item-fieldset">
        <legend>{{localize "MYRIADES.ItemType.skill"}}</legend>

        <div class="field-row checkbox-row">
          <label>{{localize "MYRIADES.Skill.usesGrades"}}</label>
          <input type="checkbox" name="system.usesGrades" {{checked skillUsesGrades}} />
        </div>

        {{#if skillUsesGrades}}
        <div class="field-row">
          <label>{{localize "MYRIADES.Skill.maxGrade"}}</label>
          <input type="number" name="system.maxGrade" value="{{skillMaxGrade}}" min="1" max="5" />
        </div>
        {{/if}}

        <div class="field-row field-description">
          <label>{{localize "MYRIADES.Sheet.description"}}</label>
          <textarea name="system.description">{{system.description}}</textarea>
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Skill.origin"}}</label>
          <select name="system.skillType">
            {{#each skillTypeOptions as |opt|}}
            <option value="{{opt.key}}" {{#if opt.selected}}selected{{/if}}>{{opt.label}}</option>
            {{/each}}
          </select>
        </div>

        <div class="field-row checkbox-row">
          <label>{{localize "MYRIADES.Skill.isRollable"}}</label>
          <input type="checkbox" name="system.isRollable" {{checked skillIsRollable}} />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Sheet.characteristic"}}</label>
          <select name="system.linkedCharacteristic">
            {{#each characteristicOptions as |opt|}}
            <option value="{{opt.key}}" {{#if opt.selected}}selected{{/if}}>{{opt.label}}</option>
            {{/each}}
          </select>
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Skill.pointCost"}}</label>
          <input type="number" name="system.skillPointCost" value="{{system.skillPointCost}}" min="1" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Skill.xpCost"}}</label>
          <input type="text" value="{{skillXpCost}}" disabled />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Skill.rollModifier"}}</label>
          <input type="number" name="system.rollModifier" value="{{system.rollModifier}}" step="1" />
        </div>
      </fieldset>
    {{/if}}

    {{#if (eq item.type "spell")}}
      <fieldset class="item-fieldset">
        <legend>{{localize "MYRIADES.ItemType.spell"}}</legend>

        <div class="field-row">
          <label>{{localize "MYRIADES.Spell.magicCost"}}</label>
          <input type="number" name="system.magicCost" value="{{system.magicCost}}" min="1" max="5" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Spell.domain"}}</label>
          <input type="text" name="system.domain" value="{{system.domain}}" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Spell.art"}}</label>
          <input type="text" name="system.art" value="{{system.art}}" />
        </div>
      </fieldset>
    {{/if}}

    {{#if (eq item.type "path")}}
      <fieldset class="item-fieldset">
        <legend>{{localize "MYRIADES.ItemType.path"}}</legend>

        <div class="field-row">
          <label>{{localize "MYRIADES.Path.xpCost"}}</label>
          <input type="number" name="system.xpCost" value="{{system.xpCost}}" min="0" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Path.totalXpCost"}}</label>
          <input type="text" value="{{pathXpCost}}" disabled />
        </div>

        <div class="field-row field-description">
          <label>{{localize "MYRIADES.Sheet.description"}}</label>
          <textarea name="system.description">{{system.description}}</textarea>
        </div>

        <div class="field-row field-path-bundle-head">
          <label>{{localize "MYRIADES.Path.bundledSkills"}}</label>
          <div class="path-bundle-actions">
            <span class="path-bundle-count">{{pathBundledSkillsCount}}</span>
            <button type="button" class="add-btn path-bundle-add" title="{{localize 'MYRIADES.Path.addBundledSkill'}}">+</button>
          </div>
        </div>

        <div class="path-bundled-list">
          {{#each pathBundledSkills as |skill|}}
          <article class="path-bundled-item">
            <div class="path-bundled-head">
              <strong>{{localize "MYRIADES.ItemType.skill"}} #{{skill.displayIndex}}</strong>
              <button
                type="button"
                class="item-delete path-bundle-delete"
                data-index="{{skill.idx}}"
                title="{{localize 'MYRIADES.Path.removeBundledSkill'}}">✕</button>
            </div>

            <input type="hidden" name="system.bundledSkills.{{skill.idx}}.key" value="{{skill.key}}" />

            <div class="field-row">
              <label>{{localize "MYRIADES.Sheet.name"}}</label>
              <input type="text" name="system.bundledSkills.{{skill.idx}}.name" value="{{skill.name}}" />
            </div>

            <div class="field-row checkbox-row">
              <label>{{localize "MYRIADES.Skill.usesGrades"}}</label>
              <input type="checkbox" name="system.bundledSkills.{{skill.idx}}.usesGrades" {{checked skill.usesGrades}} />
            </div>

            {{#if skill.usesGrades}}
            <div class="field-row">
              <label>{{localize "MYRIADES.Sheet.grade"}}</label>
              <div class="grade-radio-group">
                {{#each skill.gradeOptions as |opt|}}
                <label class="grade-radio-option{{#if opt.selected}} is-selected{{/if}}">
                  <input type="radio" name="system.bundledSkills.{{skill.idx}}.grade" value="{{opt.value}}" {{#if opt.selected}}checked{{/if}} />
                  {{opt.roman}}
                </label>
                {{/each}}
              </div>
            </div>
            <div class="field-row">
              <label>{{localize "MYRIADES.Skill.maxGrade"}}</label>
              <input type="number" name="system.bundledSkills.{{skill.idx}}.maxGrade" value="{{skill.maxGrade}}" min="1" max="5" />
            </div>
            {{else}}
            <div class="field-row">
              <label>{{localize "MYRIADES.Sheet.grade"}}</label>
              <input type="text" value="{{skill.gradeLabel}}" disabled />
            </div>
            {{/if}}

            <div class="field-row">
              <label>{{localize "MYRIADES.Skill.origin"}}</label>
              <select name="system.bundledSkills.{{skill.idx}}.skillType">
                {{#each skill.skillTypeOptions as |opt|}}
                <option value="{{opt.key}}" {{#if opt.selected}}selected{{/if}}>{{opt.label}}</option>
                {{/each}}
              </select>
            </div>

            <div class="field-row">
              <label>{{localize "MYRIADES.Sheet.characteristic"}}</label>
              <select name="system.bundledSkills.{{skill.idx}}.linkedCharacteristic">
                {{#each skill.characteristicOptions as |opt|}}
                <option value="{{opt.key}}" {{#if opt.selected}}selected{{/if}}>{{opt.label}}</option>
                {{/each}}
              </select>
            </div>

            <div class="field-row checkbox-row">
              <label>{{localize "MYRIADES.Skill.isRollable"}}</label>
              <input type="checkbox" name="system.bundledSkills.{{skill.idx}}.isRollable" {{checked skill.isRollable}} />
            </div>

            <div class="field-row">
              <label>{{localize "MYRIADES.Skill.pointCost"}}</label>
              <input type="number" name="system.bundledSkills.{{skill.idx}}.skillPointCost" value="{{skill.skillPointCost}}" min="1" />
            </div>

            <div class="field-row">
              <label>{{localize "MYRIADES.Skill.xpCost"}}</label>
              <input type="text" value="{{skill.xpCost}}" disabled />
            </div>

            <div class="field-row">
              <label>{{localize "MYRIADES.Skill.rollModifier"}}</label>
              <input type="number" name="system.bundledSkills.{{skill.idx}}.rollModifier" value="{{skill.rollModifier}}" step="1" />
            </div>

            <div class="field-row field-description">
              <label>{{localize "MYRIADES.Sheet.description"}}</label>
              <textarea name="system.bundledSkills.{{skill.idx}}.description">{{skill.description}}</textarea>
            </div>
          </article>
          {{else}}
          <p class="path-bundled-empty">{{localize "MYRIADES.Path.noBundledSkills"}}</p>
          {{/each}}
        </div>
      </fieldset>
    {{/if}}

    {{#if (eq item.type "equipment")}}
      <fieldset class="item-fieldset">
        <legend>{{localize "MYRIADES.ItemType.equipment"}}</legend>

        <div class="field-row">
          <label>{{localize "MYRIADES.Equipment.quantity"}}</label>
          <input type="number" name="system.quantity" value="{{system.quantity}}" min="1" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Equipment.slots"}}</label>
          <input type="number" name="system.slots" value="{{system.slots}}" min="1" max="36" />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Equipment.totalSlots"}}</label>
          <input type="text" value="{{equipmentTotalSlots}}" disabled />
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Equipment.gridSize"}}</label>
          <div class="inline-pair">
            <input type="number" name="system.width" value="{{system.width}}" min="1" max="6" />
            <span>×</span>
            <input type="number" name="system.height" value="{{system.height}}" min="1" max="6" />
          </div>
        </div>

        <div class="field-row">
          <label>{{localize "MYRIADES.Equipment.charges"}}</label>
          <div class="inline-pair">
            <input type="number" name="system.charges.value" value="{{system.charges.value}}" min="0" />
            <span>/</span>
            <input type="number" name="system.charges.max" value="{{system.charges.max}}" min="0" />
          </div>
        </div>

        <div class="field-row checkbox-row">
          <label>{{localize "MYRIADES.Equipment.equipped"}}</label>
          <input type="checkbox" name="system.equipped" {{checked system.equipped}} />
        </div>

        <div class="properties-grid">
          <label><input type="checkbox" name="system.usable" {{checked system.usable}} /> {{localize "MYRIADES.Equipment.usable"}}</label>
          <label><input type="checkbox" name="system.equippable" {{checked system.equippable}} /> {{localize "MYRIADES.Equipment.equippable"}}</label>
          <label><input type="checkbox" name="system.magical" {{checked system.magical}} /> {{localize "MYRIADES.Equipment.magical"}}</label>
          <label><input type="checkbox" name="system.consumable" {{checked system.consumable}} /> {{localize "MYRIADES.Equipment.consumable"}}</label>
          <label><input type="checkbox" name="system.throwable" {{checked system.throwable}} /> {{localize "MYRIADES.Equipment.throwable"}}</label>
        </div>
      </fieldset>
    {{/if}}

    {{#if (eq item.type "spell")}}
      <div class="field-row field-description">
        <label>{{localize "MYRIADES.Sheet.description"}}</label>
        <textarea name="system.description">{{system.description}}</textarea>
      </div>
    {{/if}}

    {{#if (eq item.type "equipment")}}
      <div class="field-row field-description">
        <label>{{localize "MYRIADES.Sheet.description"}}</label>
        <textarea name="system.description">{{system.description}}</textarea>
      </div>
    {{/if}}

    {{#if (eq item.type "effect")}}
      <fieldset class="item-fieldset">
        <legend>{{localize "MYRIADES.ItemType.effect"}}</legend>

        <div class="field-row checkbox-row">
          <label>{{localize "MYRIADES.Effect.enabled"}}</label>
          <input type="checkbox" name="system.enabled" {{checked system.enabled}} />
        </div>

        <div class="field-row field-description">
          <label>{{localize "MYRIADES.Sheet.description"}}</label>
          <textarea name="system.description">{{system.description}}</textarea>
        </div>

        <div class="modifiers-list">
          <h4>{{localize "MYRIADES.Effect.modifiers"}}</h4>
          {{#each effectModifiers as |mod idx|}}
          <div class="modifier-row" data-modifier-idx="{{idx}}">
            <select name="system.modifiers.{{idx}}.target" class="modifier-target">
              <option value="puissance" {{#if (eq mod.target "puissance")}}selected{{/if}}>{{localize "MYRIADES.Characteristic.puissance"}}</option>
              <option value="coordination" {{#if (eq mod.target "coordination")}}selected{{/if}}>{{localize "MYRIADES.Characteristic.coordination"}}</option>
              <option value="ingeniosite" {{#if (eq mod.target "ingeniosite")}}selected{{/if}}>{{localize "MYRIADES.Characteristic.ingeniosite"}}</option>
              <option value="perspicacite" {{#if (eq mod.target "perspicacite")}}selected{{/if}}>{{localize "MYRIADES.Characteristic.perspicacite"}}</option>
              <option value="healthMax" {{#if (eq mod.target "healthMax")}}selected{{/if}}>{{localize "MYRIADES.Secondary.healthMax"}}</option>
              <option value="magicMax" {{#if (eq mod.target "magicMax")}}selected{{/if}}>{{localize "MYRIADES.Secondary.magicMax"}}</option>
              <option value="inventoryCapacity" {{#if (eq mod.target "inventoryCapacity")}}selected{{/if}}>{{localize "MYRIADES.Secondary.inventoryCapacity"}}</option>
            </select>

            <select name="system.modifiers.{{idx}}.mode" class="modifier-mode">
              <option value="add" {{#if (eq mod.mode "add")}}selected{{/if}}>{{localize "MYRIADES.Effect.mode.add"}}</option>
              <option value="multiply" {{#if (eq mod.mode "multiply")}}selected{{/if}}>{{localize "MYRIADES.Effect.mode.multiply"}}</option>
              <option value="override" {{#if (eq mod.mode "override")}}selected{{/if}}>{{localize "MYRIADES.Effect.mode.override"}}</option>
            </select>

            <input type="number" name="system.modifiers.{{idx}}.value" value="{{mod.value}}" step="0.1" class="modifier-value" placeholder="Value" />

            <label class="checkbox-label">
              <input type="checkbox" name="system.modifiers.{{idx}}.perGrade" {{checked mod.perGrade}} class="modifier-per-grade" />
              ×Grade
            </label>

            {{#if mod.perGrade}}
            <input type="number" name="system.modifiers.{{idx}}.grade" value="{{mod.grade}}" min="1" max="5" class="modifier-grade" />
            {{/if}}

            <button type="button" class="remove-modifier" data-idx="{{idx}}" title="{{localize 'MYRIADES.Action.remove'}}">✕</button>
          </div>
          {{/each}}
          <button type="button" class="add-modifier" title="{{localize 'MYRIADES.Action.add'}}">+ {{localize "MYRIADES.Effect.addModifier"}}</button>
        </div>
      </fieldset>
    {{/if}}

  </section>
</form>
