<script lang="ts">
  export let open = false;
  export let builtBuildings: string[] = [];

  $: built = new Set(builtBuildings);
</script>

{#if open}
  <!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
  <div class="help-backdrop" on:click={() => (open = false)}>
    <!-- svelte-ignore a11y-click-events-have-key-events a11y-no-noninteractive-element-interactions -->
    <div class="help-modal" on:click|stopPropagation role="dialog" aria-modal="true" aria-label="How to play">
      <button class="help-close" on:click={() => (open = false)} aria-label="Close">✕</button>

      <h2 class="help-title">How to Play</h2>
      <p class="help-intro">
        You are the Commander. Your units obey your every order — speak or type your commands and watch
        your army execute them in real time. Lead your forces to victory.
      </p>

      <section class="help-section">
        <h3 class="help-section-title">🎯 Objective</h3>
        <p>Destroy your opponent's <strong>Command Center</strong>. First player to eliminate the enemy HQ wins.</p>
      </section>

      <section class="help-section">
        <h3 class="help-section-title">💎 Resources</h3>
        <div class="help-table">
          <div class="help-row">
            <img class="res-icon-img" src="/sprites/icons/mineral.png" alt="Minerals" />
            <div>
              <strong>Minerals</strong>
              <span class="help-muted">— Basic currency. Send SCVs to mineral patches to collect.</span>
            </div>
          </div>
          <div class="help-row">
            <img class="res-icon-img" src="/sprites/icons/gas.png" alt="Gas" />
            <div>
              <strong>Vespene Gas</strong>
              <span class="help-muted">— Required for advanced units. Build a Refinery on a geyser, then assign SCVs.</span>
            </div>
          </div>
          <div class="help-row">
            <img class="res-icon-img" src="/sprites/icons/supply.png" alt="Supply" />
            <div>
              <strong>Supply</strong>
              <span class="help-muted">— Limits your army size. Build Supply Depots to raise the cap.</span>
            </div>
          </div>
        </div>
      </section>

      <section class="help-section">
        <h3 class="help-section-title">⚔️ Units</h3>
        <div class="help-units">
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/scv.png" alt="SCV" />
              <strong>SCV</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />50
              </span>
            </div>
            <p class="unit-desc">Worker. Gathers resources and constructs buildings.</p>
            <div class="unit-stats"><span>HP 60</span><span>Supply 1</span></div>
          </div>
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/marine.png" alt="Marine" />
              <strong>Marine</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />50
              </span>
            </div>
            <p class="unit-desc">Basic infantry. Attacks both ground and air targets.</p>
            <div class="unit-stats"><span>HP 40</span><span>DMG 6/6</span><span>Supply 1</span></div>
          </div>
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/medic.png" alt="Medic" />
              <strong>Medic</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />50
                <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />25
              </span>
            </div>
            <p class="unit-desc">Heals nearby infantry. Needs Barracks + Engineering Bay.</p>
            <div class="unit-stats"><span>HP 60</span><span>Heal 1.5/tick</span><span>Supply 1</span></div>
          </div>
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/goliath.png" alt="Goliath" />
              <strong>Goliath</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />100
                <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />50
              </span>
            </div>
            <p class="unit-desc">Heavy mech. Devastating vs both ground and air. Needs Factory.</p>
            <div class="unit-stats"><span>HP 125</span><span>DMG 12/20</span><span>Supply 2</span></div>
          </div>
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/tank.png" alt="Siege Tank" />
              <strong>Siege Tank</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />150
                <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />100
              </span>
            </div>
            <p class="unit-desc">Heavy artillery. Massive ground damage, no air attack. Needs Factory + Armory.</p>
            <div class="unit-stats"><span>HP 150</span><span>DMG 35</span><span>Supply 3</span></div>
          </div>
          <div class="unit-card">
            <div class="unit-header">
              <img class="unit-sprite" src="/sprites/units/wraith.png" alt="Wraith" />
              <strong>Wraith</strong>
              <span class="unit-cost">
                <img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />150
                <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />100
              </span>
            </div>
            <p class="unit-desc">Fast air unit. Attacks both ground and air targets. Needs Starport.</p>
            <div class="unit-stats"><span>HP 120</span><span>DMG 8/20</span><span>Supply 2</span></div>
          </div>
        </div>
      </section>

      <section class="help-section">
        <h3 class="help-section-title">🏗️ Tech Tree</h3>
        <div class="tech-tree">
          <div class="tech-row">
            <div class="tech-node tech-start" class:tech-built={built.has('command_center')}>
              {#if built.has('command_center')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/command_center.png" alt="" />
              Command Center<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />400</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/scv.png" alt="SCV" /> SCV</div>
          </div>
          <div class="tech-row tech-indent">
            <div class="tech-node" class:tech-built={built.has('supply_depot')}>
              {#if built.has('supply_depot')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/supply_depot.png" alt="" />
              Supply Depot<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />100</span>
            </div>
            <div class="tech-produces">
              <img class="tech-unit-sprite" src="/sprites/icons/supply.png" alt="supply" /> +8 supply
            </div>
          </div>
          <div class="tech-row tech-indent2">
            <div class="tech-node" class:tech-built={built.has('barracks')}>
              {#if built.has('barracks')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/barracks.png" alt="" />
              Barracks<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />150</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/marine.png" alt="Marine" /> Marine</div>
          </div>
          <div class="tech-row tech-indent3">
            <div class="tech-node" class:tech-built={built.has('engineering_bay')}>
              {#if built.has('engineering_bay')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/engineering_bay.png" alt="" />
              Eng. Bay<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />125</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/medic.png" alt="Medic" /> Medic</div>
          </div>
          <div class="tech-row tech-indent3">
            <div class="tech-node" class:tech-built={built.has('factory')}>
              {#if built.has('factory')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/factory.png" alt="" />
              Factory<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />200 <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />100</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/goliath.png" alt="Goliath" /> Goliath</div>
          </div>
          <div class="tech-row tech-indent4">
            <div class="tech-node" class:tech-built={built.has('armory')}>
              {#if built.has('armory')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/armory.png" alt="" />
              Armory<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />100 <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />50</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/tank.png" alt="Tank" /> Tank</div>
          </div>
          <div class="tech-row tech-indent4">
            <div class="tech-node" class:tech-built={built.has('starport')}>
              {#if built.has('starport')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/starport.png" alt="" />
              Starport<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />150 <img class="cost-icon" src="/sprites/icons/gas.png" alt="gas" />100</span>
            </div>
            <div class="tech-produces">→ <img class="tech-unit-sprite" src="/sprites/units/wraith.png" alt="Wraith" /> Wraith</div>
          </div>
          <div class="tech-row tech-indent">
            <div class="tech-node" class:tech-built={built.has('refinery')}>
              {#if built.has('refinery')}<span class="tech-check">✓</span>{/if}
              <img class="tech-sprite" src="/sprites/buildings/refinery.png" alt="" />
              Refinery<span class="tech-cost"><img class="cost-icon" src="/sprites/icons/mineral.png" alt="min" />100</span>
            </div>
            <div class="tech-produces">
              <img class="tech-unit-sprite" src="/sprites/icons/gas.png" alt="gas" /> enables gas
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
{/if}

<style>
  .help-backdrop {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    animation: fadeIn 0.2s ease;
  }

  @keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
  }

  .help-modal {
    position: relative;
    width: 100%;
    max-width: 640px;
    max-height: 88vh;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 32px 28px 28px;
    color: var(--text);
    animation: slideUp 0.25s ease;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
  }

  @keyframes slideUp {
    from { opacity: 0; transform: translateY(16px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  .help-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1rem;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: color 0.15s, background 0.15s;
  }
  .help-close:hover { color: var(--text); background: var(--surface2); }

  .help-title {
    font-size: 1.4rem;
    font-weight: 700;
    margin: 0 0 8px;
    color: var(--accent);
    letter-spacing: 0.02em;
  }

  .help-intro {
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.6;
    margin: 0 0 24px;
  }

  .help-section {
    margin-bottom: 24px;
  }

  .help-section-title {
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--warning);
    margin: 0 0 12px;
  }

  .help-section p {
    color: var(--text);
    font-size: 0.9rem;
    line-height: 1.6;
    margin: 0;
  }

  .help-muted { color: var(--text-muted); }

  .help-table { display: flex; flex-direction: column; gap: 10px; }
  .help-row {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    font-size: 0.88rem;
    line-height: 1.5;
  }

  .res-icon-img {
    width: 33px;
    height: 33px;
    object-fit: contain;
    flex-shrink: 0;
    margin-top: 1px;
    image-rendering: pixelated;
  }

  .help-units {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  @media (max-width: 480px) {
    .help-units { grid-template-columns: 1fr; }
  }

  .unit-card {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
  }

  .unit-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 4px;
  }

  .unit-sprite {
    width: 48px;
    height: 48px;
    object-fit: contain;
    flex-shrink: 0;
    image-rendering: pixelated;
  }

  .cost-icon {
    width: 21px;
    height: 21px;
    object-fit: contain;
    image-rendering: pixelated;
    vertical-align: middle;
    margin-right: 1px;
  }

  .unit-cost {
    margin-left: auto;
    font-size: 0.72rem;
    color: var(--text-muted);
    font-weight: 600;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .tech-sprite {
    width: 36px;
    height: 36px;
    object-fit: contain;
    flex-shrink: 0;
    image-rendering: pixelated;
  }

  .tech-unit-sprite {
    width: 27px;
    height: 27px;
    object-fit: contain;
    image-rendering: pixelated;
    vertical-align: middle;
  }

  .unit-desc {
    font-size: 0.78rem;
    color: var(--text-muted);
    line-height: 1.4;
    margin: 0 0 6px;
  }

  .unit-stats {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
  }

  .unit-stats span {
    font-size: 0.7rem;
    background: rgba(88, 166, 255, 0.08);
    border: 1px solid rgba(88, 166, 255, 0.2);
    color: var(--accent);
    border-radius: 4px;
    padding: 1px 6px;
    font-weight: 600;
  }

  .tech-tree {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .tech-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.82rem;
  }

  .tech-indent  { padding-left: 20px; }
  .tech-indent2 { padding-left: 40px; }
  .tech-indent3 { padding-left: 60px; }
  .tech-indent4 { padding-left: 80px; }

  .tech-node {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 10px;
    font-weight: 600;
    white-space: nowrap;
  }

  .tech-start { border-color: var(--accent); color: var(--accent); }

  .tech-built {
    border-color: rgba(80, 220, 130, 0.6) !important;
    background: rgba(80, 220, 130, 0.08) !important;
    color: #50dc82 !important;
  }

  .tech-check {
    font-size: 0.75rem;
    color: #50dc82;
    font-weight: 700;
    flex-shrink: 0;
  }

  .tech-cost {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 400;
  }

  .tech-produces {
    color: var(--text-muted);
    font-size: 0.78rem;
    white-space: nowrap;
  }
</style>
