<div class="tab factions {{tab.cssClass}}" data-group="sheet" data-tab="factions">
  <table class="factions-table">
    <thead>
      <tr>
        <th>{{localize "CELESTOPOL.Faction.label"}}</th>
        <th>{{localize "CELESTOPOL.Faction.score"}}</th>
      </tr>
    </thead>
    <tbody>
      {{#each factions as |faction factionId|}}
      <tr class="faction-row" data-faction="{{factionId}}">
        <td class="faction-name">{{localize faction.label}}</td>
        <td class="faction-value">
          <div class="faction-checkboxes-container">
            <div class="faction-checkboxes">
              {{#each (array 1 2 3 4 5 6 7 8 9) as |level|}}
              {{#if @root.isEditMode}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" name="system.factions.{{factionId}}.level{{level}}"
                       {{#if (lookup (lookup @root.system.factions factionId) (concat 'level' level))}}checked{{/if}}
                       class="faction-checkbox"
                       data-faction="{{factionId}}"
                       data-level="{{level}}">
              </label>
              {{else}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" 
                       {{#if (lookup (lookup @root.system.factions factionId) (concat 'level' level))}}checked{{/if}}
                       disabled class="faction-checkbox">
              </label>
              {{/if}}
              {{/each}}
            </div>
            <span class="faction-count">
              {{#if ../isEditMode}}
                <input type="number" name="system.factions.{{factionId}}.value"
                       value="{{lookup (lookup ../system.factions factionId) 'value'}}" min="0" max="9" class="faction-value-input">
              {{else}}
                {{lookup (lookup ../system.factions factionId) 'value'}}
              {{/if}}
            </span>
          </div>
        </td>
      </tr>
      {{/each}}

      {{!-- Factions personnalisées --}}
      <tr class="faction-row custom" data-faction="perso1">
        <td>
          {{#if isEditMode}}
            <input type="text" name="system.factions.perso1.label"
                   value="{{system.factions.perso1.label}}"
                   placeholder="{{localize 'CELESTOPOL.Faction.custom'}}">
          {{else}}
            <span>{{#if system.factions.perso1.label}}{{system.factions.perso1.label}}{{else}}—{{/if}}</span>
          {{/if}}
        </td>
        <td>
          <div class="faction-checkboxes-container">
            <div class="faction-checkboxes">
              {{#each (array 1 2 3 4 5 6 7 8 9) as |level|}}
              {{#if ../isEditMode}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" name="system.factions.perso1.level{{level}}"
                       {{#if (lookup ../system.factions.perso1 (concat 'level' level))}}checked{{/if}}
                       class="faction-checkbox"
                       data-faction="perso1"
                       data-level="{{level}}">
              </label>
              {{else}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" 
                       {{#if (lookup ../system.factions.perso1 (concat 'level' level))}}checked{{/if}}
                       disabled class="faction-checkbox">
              </label>
              {{/if}}
              {{/each}}
            </div>
            <span class="faction-count">
              {{#if ../isEditMode}}
                <input type="number" name="system.factions.perso1.value"
                       value="{{system.factions.perso1.value}}" min="0" max="9" class="faction-value-input">
              {{else}}
                {{system.factions.perso1.value}}
              {{/if}}
            </span>
          </div>
        </td>
      </tr>
      <tr class="faction-row custom" data-faction="perso2">
        <td>
          {{#if isEditMode}}
            <input type="text" name="system.factions.perso2.label"
                   value="{{system.factions.perso2.label}}"
                   placeholder="{{localize 'CELESTOPOL.Faction.custom'}}">
          {{else}}
            <span>{{#if system.factions.perso2.label}}{{system.factions.perso2.label}}{{else}}—{{/if}}</span>
          {{/if}}
        </td>
        <td>
          <div class="faction-checkboxes-container">
            <div class="faction-checkboxes">
              {{#each (array 1 2 3 4 5 6 7 8 9) as |level|}}
              {{#if ../isEditMode}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" name="system.factions.perso2.level{{level}}"
                       {{#if (lookup ../system.factions.perso2 (concat 'level' level))}}checked{{/if}}
                       class="faction-checkbox"
                       data-faction="perso2"
                       data-level="{{level}}">
              </label>
              {{else}}
              <label class="faction-checkbox-wrapper">
                <input type="checkbox" 
                       {{#if (lookup ../system.factions.perso2 (concat 'level' level))}}checked{{/if}}
                       disabled class="faction-checkbox">
              </label>
              {{/if}}
              {{/each}}
            </div>
            <span class="faction-count">
              {{#if ../isEditMode}}
                <input type="number" name="system.factions.perso2.value"
                       value="{{system.factions.perso2.value}}" min="0" max="9" class="faction-value-input">
              {{else}}
                {{system.factions.perso2.value}}
              {{/if}}
            </span>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>
