<section class="{{cssClass}} les-oublies-sheet personnage-sheet" autocomplete="off">
  <header class="sheet-header hero-banner">
    <img class="profile-img" data-edit="img" data-action="editImage" src="{{actor.img}}" title="{{actor.name}}" />
    <div class="header-fields hero-copy">
      <button class="mode-button mode-button--icon" type="button" data-action="toggleSheet" title="{{#if isEditMode}}{{localize "LESOUBLIES.ui.playMode"}}{{else}}{{localize "LESOUBLIES.ui.editMode"}}{{/if}}" aria-label="{{#if isEditMode}}{{localize "LESOUBLIES.ui.playMode"}}{{else}}{{localize "LESOUBLIES.ui.editMode"}}{{/if}}">
        <i class="fa-solid {{#if isEditMode}}fa-eye{{else}}fa-pen-to-square{{/if}}"></i>
      </button>
      <h1 class="sheet-title"><input name="name" type="text" value="{{actor.name}}" placeholder="Nom" {{#if isPlayMode}}disabled{{/if}} /></h1>
      <div class="sheet-actions">
        <button type="button" data-action="openInitiative">{{localize "LESOUBLIES.rolls.initiative"}}</button>
      </div>
      <div class="creation-slots creation-slots--header" data-creation-drop-zone>
        {{#each creationSlots as |slot|}}
          <article class="creation-slot creation-slot--compact {{#if slot.item}}is-filled{{else}}is-empty{{/if}}" data-drop-creation-type="{{slot.type}}">
            <div class="creation-slot-header">
              <div>
                <p class="creation-slot-kicker">{{slot.label}}</p>
                {{#if slot.item}}
                  <strong class="creation-slot-name">{{slot.item.name}}</strong>
                {{else}}
                  <strong class="creation-slot-name">Glisser ici</strong>
                {{/if}}
              </div>
              {{#if slot.item}}
                <div class="item-controls item-controls--compact">
                  <button type="button" data-action="editItem" data-item-id="{{slot.item.id}}">Mod.</button>
                  {{#unless @root.isPlayMode}}
                    <button type="button" data-action="removeCreationItem" data-type="{{slot.type}}">X</button>
                  {{/unless}}
                </div>
              {{/if}}
            </div>
          </article>
        {{/each}}
      </div>
    </div>
  </header>

  <nav class="sheet-tabs" aria-label="Navigation de la fiche">
    {{#each tabs as |tab|}}
      <button type="button" class="sheet-tab-button {{tab.cssClass}}" data-action="switchTab" data-tab="{{tab.id}}">
        <i class="{{tab.icon}}"></i>
        <span>{{tab.label}}</span>
      </button>
    {{/each}}
  </nav>

  <section class="sheet-tab {{tabs.overview.cssClass}}" data-tab="overview">
    <div class="sheet-grid sheet-grid-2">
      <section class="sheet-card summary-card">
        <h2>{{localize "LESOUBLIES.ui.derivedOverview"}}</h2>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.taille"}}</label>
          <select name="system.size.value" {{#if isPlayMode}}disabled{{/if}}>
            {{#each choiceSets.personnageSizeOptions as |option|}}
              <option value="{{option.value}}" {{#if (eq option.value @root.system.size.value)}}selected{{/if}}>{{option.label}}</option>
            {{/each}}
          </select>
          <span>{{derived.sizeLabel}}</span>
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.ui.vie"}}</label>
          <input name="system.hp.value" type="number" value="{{system.hp.value}}" {{#if isPlayMode}}disabled{{/if}} />
          <span>/ {{derived.hpMax}}</span>
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.ui.songes"}}</label>
          <input name="system.songes.value" type="number" value="{{system.songes.value}}" {{#if isPlayMode}}disabled{{/if}} />
          <span>{{system.songes.points}} / {{system.songes.max}} pts</span>
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.detteSonges"}}</label>
          <input name="system.songes.debt" type="number" value="{{system.songes.debt}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.creditSonges"}}</label>
          <input name="system.songes.xpCredit" type="number" value="{{system.songes.xpCredit}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.ui.cauchemar"}}</label>
          <input name="system.cauchemar.value" type="number" value="{{system.cauchemar.value}}" {{#if isPlayMode}}disabled{{/if}} />
          <span>{{system.cauchemar.points}} / {{system.cauchemar.max}} pts</span>
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.detteCauchemar"}}</label>
          <input name="system.cauchemar.debt" type="number" value="{{system.cauchemar.debt}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.creditCauchemar"}}</label>
          <input name="system.cauchemar.xpCredit" type="number" value="{{system.cauchemar.xpCredit}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.xp"}}</label>
          <input name="system.experience.value" type="number" value="{{system.experience.value}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.ecorces"}}</label>
          <input name="system.money.ecorces" type="number" value="{{system.money.ecorces}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.ombreDuTourment"}}</label>
          <input name="system.flagsNarratifs.ombreDuTourment" type="checkbox" {{checked system.flagsNarratifs.ombreDuTourment}} {{#if isPlayMode}}disabled{{/if}} />
        </div>
      </section>

      <section class="sheet-card creation-card">
        <h2>{{localize "LESOUBLIES.labels.compagnie"}}</h2>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.compagnie"}}</label>
          <select name="system.references.compagnieId" {{#if isPlayMode}}disabled{{/if}}>
            <option value="">—</option>
            {{#each choiceSets.companyOptions as |option|}}
              <option value="{{option.value}}" {{#if (eq option.value @root.system.references.compagnieId)}}selected{{/if}}>{{option.label}}</option>
            {{/each}}
          </select>
        </div>
        <p class="help-text"><strong>{{localize "LESOUBLIES.labels.compagnie"}} :</strong> {{#if derived.compagnie}}{{derived.compagnie.name}}{{else}}—{{/if}}</p>
        {{#if activeCompanyPower}}
          <p class="help-text"><strong>{{localize "LESOUBLIES.labels.pouvoirCompagnieActif"}} :</strong> {{activeCompanyPower.name}} — {{activeCompanyPower.system.activationCondition}}</p>
        {{/if}}
        {{#if derived.compagnie}}
          <div class="embed-buttons">
            <button type="button" data-action="openLinkedActor" data-actor-id="{{derived.compagnie.id}}">Ouvrir la compagnie</button>
          </div>
        {{/if}}
      </section>
    </div>

  </section>

  <section class="sheet-tab {{tabs.skills.cssClass}}" data-tab="skills">
    <section class="sheet-card ledger-card skills-ledger-card">
      <div class="section-title-row">
        <h2>{{localize "LESOUBLIES.ui.competences"}}</h2>
        <button type="button" data-action="createItem" data-type="competence">+ {{localize "TYPES.Item.competence"}}</button>
      </div>
      <div class="skills-columns">
        {{#each skillColumns as |column|}}
          <div class="skills-column">
            {{#each column as |group|}}
              <div class="group-block skills-group">
                <div class="group-header">
                  <h3>{{group.label}}</h3>
                  <label class="profile-badge">
                    <span>{{localize "LESOUBLIES.labels.profil"}}</span>
                    <input name="system.profils.{{group.id}}" type="number" value="{{group.profileValue}}" {{#if @root.isPlayMode}}disabled{{/if}} />
                  </label>
                </div>
                <div class="item-list skills-item-list">
                  {{#each group.items as |entry|}}
                    <article class="item-card skill-card" data-item-id="{{entry.item.id}}" draggable="true">
                      <div class="skill-card-main">
                        <strong>{{entry.item.name}}</strong>
                        <span class="skill-summary">Base {{entry.item.system.base}} · {{group.label}} {{group.profileValue}} · {{localize "LESOUBLIES.labels.valeurFinale"}} {{entry.finalValue}}</span>
                        {{#if entry.domains.length}}
                          <span class="skill-summary">Domaines : {{join entry.domains ", "}}</span>
                        {{/if}}
                      </div>
                      <div class="item-controls skill-controls">
                        <button type="button" data-action="rollSkill" data-item-id="{{entry.item.id}}">{{localize "LESOUBLIES.ui.roll"}}</button>
                        <button type="button" data-action="editItem" data-item-id="{{entry.item.id}}">Edit</button>
                        <button type="button" data-action="deleteItem" data-item-id="{{entry.item.id}}">Delete</button>
                      </div>
                    </article>
                  {{/each}}
                </div>
              </div>
            {{/each}}
          </div>
        {{/each}}
      </div>
    </section>
  </section>

  <section class="sheet-tab {{tabs.actions.cssClass}}" data-tab="actions">
    <section class="sheet-card ledger-card">
      <div class="section-title-row">
        <h2>{{localize "LESOUBLIES.ui.combat"}}</h2>
        <div class="embed-buttons">
          <button type="button" data-action="openCombatPreset" data-preset="encourager">Encourager</button>
          <button type="button" data-action="openCombatPreset" data-preset="intimider">Intimider</button>
          <button type="button" data-action="openCombatPreset" data-preset="evaluer">Évaluer</button>
          <button type="button" data-action="openCombatPreset" data-preset="maitriser">Maîtriser</button>
          <button type="button" data-action="openCombatPreset" data-preset="seDeplacer">Se déplacer</button>
        </div>
      </div>
      <div class="item-list">
        {{#if equippedWeapons.length}}
          {{#each equippedWeapons as |item|}}
            <article class="item-card" data-item-id="{{item.id}}" draggable="true">
              <div><strong>{{item.name}}</strong><div>{{localize "TYPES.Item.arme"}} - {{item.displayDamage}}</div></div>
              <div class="item-controls"><button type="button" data-action="useWeapon" data-item-id="{{item.id}}">Attaque</button><button type="button" data-action="resolveWeaponDamage" data-item-id="{{item.id}}">Dégâts</button><button type="button" data-action="toggleEquipped" data-item-id="{{item.id}}">Retirer</button></div>
            </article>
          {{/each}}
        {{else}}
          <p class="help-text">Aucune arme équipée.</p>
        {{/if}}
      </div>
    </section>

    <section class="sheet-card ledger-card reserve-card">
      <div class="section-title-row">
        <h2>{{localize "LESOUBLIES.labels.threadReserves"}}</h2>
        <button type="button" data-action="openThreadHarvest">Récolte de fils</button>
      </div>
      <div class="reserve-grid">
        <div class="reserve-panel">
          <h3>{{localize "LESOUBLIES.labels.personalReserve"}}</h3>
          <div class="field-row">
            <label>{{localize "LESOUBLIES.labels.threadSonges"}}</label>
            <input name="system.reserves.songesThreads" type="number" value="{{system.reserves.songesThreads}}" {{#if isPlayMode}}disabled{{/if}} />
          </div>
          <div class="field-row">
            <label>{{localize "LESOUBLIES.labels.threadCauchemar"}}</label>
            <input name="system.reserves.cauchemarThreads" type="number" value="{{system.reserves.cauchemarThreads}}" {{#if isPlayMode}}disabled{{/if}} />
          </div>
          <div class="field-row">
            <label>{{localize "LESOUBLIES.labels.emptyGlobes"}}</label>
            <input name="system.reserves.emptyGlobes" type="number" value="{{system.reserves.emptyGlobes}}" {{#if isPlayMode}}disabled{{/if}} />
          </div>
        </div>

        <div class="reserve-panel reserve-panel--company">
          <h3>{{localize "LESOUBLIES.labels.companyReserve"}}</h3>
          {{#if derived.compagnie}}
            <div class="field-row">
              <label>{{localize "LESOUBLIES.labels.threadSonges"}}</label>
              <span>{{derived.companyReserves.songesThreads}}</span>
            </div>
            <div class="field-row">
              <label>{{localize "LESOUBLIES.labels.threadCauchemar"}}</label>
              <span>{{derived.companyReserves.cauchemarThreads}}</span>
            </div>
            <div class="field-row">
              <label>{{localize "LESOUBLIES.labels.emptyGlobes"}}</label>
              <span>{{derived.companyReserves.emptyGlobes}}</span>
            </div>

            <div class="transfer-list">
              <div class="transfer-row" data-transfer-row>
                <strong>{{localize "LESOUBLIES.labels.threadSonges"}}</strong>
                <input type="number" min="1" value="1" data-transfer-amount />
                <div class="item-controls">
                  <button type="button" data-action="transferThread" data-direction="toCompany" data-resource-key="songesThreads">{{localize "LESOUBLIES.labels.toCompany"}}</button>
                  <button type="button" data-action="transferThread" data-direction="toActor" data-resource-key="songesThreads">{{localize "LESOUBLIES.labels.toActor"}}</button>
                </div>
              </div>
              <div class="transfer-row" data-transfer-row>
                <strong>{{localize "LESOUBLIES.labels.threadCauchemar"}}</strong>
                <input type="number" min="1" value="1" data-transfer-amount />
                <div class="item-controls">
                  <button type="button" data-action="transferThread" data-direction="toCompany" data-resource-key="cauchemarThreads">{{localize "LESOUBLIES.labels.toCompany"}}</button>
                  <button type="button" data-action="transferThread" data-direction="toActor" data-resource-key="cauchemarThreads">{{localize "LESOUBLIES.labels.toActor"}}</button>
                </div>
              </div>
              <div class="transfer-row" data-transfer-row>
                <strong>{{localize "LESOUBLIES.labels.emptyGlobes"}}</strong>
                <input type="number" min="1" value="1" data-transfer-amount />
                <div class="item-controls">
                  <button type="button" data-action="transferThread" data-direction="toCompany" data-resource-key="emptyGlobes">{{localize "LESOUBLIES.labels.toCompany"}}</button>
                  <button type="button" data-action="transferThread" data-direction="toActor" data-resource-key="emptyGlobes">{{localize "LESOUBLIES.labels.toActor"}}</button>
                </div>
              </div>
            </div>
          {{else}}
            <p class="help-text">Liez une compagnie pour gérer une réserve commune de fils et de globes.</p>
          {{/if}}
        </div>
      </div>
      <p class="help-text">Les dépenses en fils rendent automatiquement autant de globes vides à la réserve utilisée.</p>
    </section>

    <section class="sheet-card ledger-card">
      <div class="section-title-row">
        <h2>{{localize "LESOUBLIES.ui.magie"}}</h2>
        <button type="button" data-action="createItem" data-type="sortilege">+ {{localize "TYPES.Item.sortilege"}}</button>
      </div>
      <div class="item-list">
        {{#each spells as |item|}}
          <article class="item-card" data-item-id="{{item.id}}" draggable="true">
            <div>
              <strong>{{item.name}}</strong>
              <div>{{item.system.tradition}} / {{item.system.polarity}} / coût {{item.system.cost}}</div>
            </div>
            <div class="item-controls">
              <button type="button" data-action="useSpell" data-item-id="{{item.id}}">Activer</button>
              <button type="button" data-action="editItem" data-item-id="{{item.id}}">Edit</button>
              <button type="button" data-action="deleteItem" data-item-id="{{item.id}}">Delete</button>
            </div>
          </article>
        {{/each}}
      </div>
    </section>

  </section>

  <section class="sheet-tab {{tabs.equipment.cssClass}}" data-tab="equipment">
    <section class="sheet-card ledger-card">
      <div class="section-title-row">
        <h2>{{localize "LESOUBLIES.ui.equipement"}}</h2>
        <div class="embed-buttons">
          <button type="button" data-action="createItem" data-type="arme">+ {{localize "TYPES.Item.arme"}}</button>
          <button type="button" data-action="createItem" data-type="armure">+ {{localize "TYPES.Item.armure"}}</button>
          <button type="button" data-action="createItem" data-type="equipement">+ {{localize "TYPES.Item.equipement"}}</button>
        </div>
      </div>
        <div class="item-list">
        {{#each weapons as |item|}}
          <article class="item-card" data-item-id="{{item.id}}" draggable="true">
            <div><strong>{{item.name}}</strong><div>{{localize "TYPES.Item.arme"}} - {{item.displayDamage}}{{#if item.system.equipped}} - Équipée{{/if}}</div></div>
            <div class="item-controls"><button type="button" data-action="toggleEquipped" data-item-id="{{item.id}}">{{#if item.system.equipped}}Retirer{{else}}Équiper{{/if}}</button><button type="button" data-action="editItem" data-item-id="{{item.id}}">Edit</button><button type="button" data-action="deleteItem" data-item-id="{{item.id}}">Delete</button></div>
          </article>
        {{/each}}
        {{#each armors as |item|}}
          <article class="item-card" data-item-id="{{item.id}}" draggable="true">
            <div><strong>{{item.name}}</strong><div>{{localize "TYPES.Item.armure"}} - Prot {{item.system.protection}}</div></div>
            <div class="item-controls"><button type="button" data-action="editItem" data-item-id="{{item.id}}">Edit</button><button type="button" data-action="deleteItem" data-item-id="{{item.id}}">Delete</button></div>
          </article>
        {{/each}}
        {{#each equipment as |item|}}
          <article class="item-card" data-item-id="{{item.id}}" draggable="true">
            <div><strong>{{item.name}}</strong><div>{{localize "TYPES.Item.equipement"}} - {{item.system.category}}</div></div>
            <div class="item-controls"><button type="button" data-action="editItem" data-item-id="{{item.id}}">Edit</button><button type="button" data-action="deleteItem" data-item-id="{{item.id}}">Delete</button></div>
          </article>
        {{/each}}
      </div>
    </section>
  </section>

  <section class="sheet-tab {{tabs.notes.cssClass}}" data-tab="notes">
    <section class="sheet-card creation-card identity-card identity-card--compact">
      <h2>{{localize "LESOUBLIES.labels.identite"}}</h2>
      <div class="identity-grid">
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.age"}}</label>
          <input name="system.biodata.age" type="number" value="{{system.biodata.age}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.sexe"}}</label>
          <input name="system.biodata.sexe" type="text" value="{{system.biodata.sexe}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.motsCles"}}</label>
          <input name="system.biodata.motscles" type="text" value="{{system.biodata.motscles}}" {{#if isPlayMode}}disabled{{/if}} />
        </div>
        <div class="field-row">
          <label>{{localize "LESOUBLIES.labels.capitaine"}}</label>
          <input name="system.flagsNarratifs.isCaptain" type="checkbox" {{checked system.flagsNarratifs.isCaptain}} {{#if isPlayMode}}disabled{{/if}} />
        </div>
      </div>
    </section>
    <section class="sheet-card notes-card">
      <h2>{{localize "LESOUBLIES.ui.notes"}}</h2>
      <label>{{localize "LESOUBLIES.labels.description"}}</label>
      {{formInput systemFields.biodata.fields.description enriched=enriched.biodata.description value=system.biodata.description name="system.biodata.description" toggled=true}}
      <label>{{localize "LESOUBLIES.labels.notes"}}</label>
      {{formInput systemFields.biodata.fields.notes enriched=enriched.biodata.notes value=system.biodata.notes name="system.biodata.notes" toggled=true}}
      <label>{{localize "LESOUBLIES.labels.gmnotes"}}</label>
      {{formInput systemFields.biodata.fields.gmnotes enriched=enriched.biodata.gmnotes value=system.biodata.gmnotes name="system.biodata.gmnotes" toggled=true}}
      <label>{{localize "LESOUBLIES.labels.liensNarratifs"}}</label>
      {{formInput systemFields.visions enriched=enriched.visions value=system.visions name="system.visions" toggled=true}}
    </section>
  </section>
</section>
