        <header class="page-heading"><p class="eyebrow">CIENCIA IMPERIAL</p><h1>Investigación</h1><p>Las tecnologías se comparten entre todos tus planetas. Laboratorio actual: nivel {{ snap.levels.get('research_lab', 0) }}.</p></header>
        {% set research_queue = queues|selectattr('category','equalto','research')|list %}
        {% if research_queue %}{% set q = research_queue[0] %}<section class="active-queue" data-queue-id="{{ q.id }}"><div><span>Investigación en curso</span><strong>{{ TECHNOLOGIES[q.item].name }} nivel {{ q.target_level }}</strong></div><div class="queue-time"><span>Tiempo restante</span><time class="countdown" data-end="{{ q.completes_at }}">{{ (q.completes_at-now)|duration }}</time></div><form method="post" action="{{ url_for('cancel_action') }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="queue_id" value="{{ q.id }}"><input type="hidden" name="return_section" value="research"><button class="button button-danger" type="submit">Cancelar</button></form></section>{% endif %}
        {% if research_funding_preview %}
        <section id="research-funding-confirmation" class="panel notice notice-warning" aria-labelledby="research-funding-confirmation-title" tabindex="-1">
          <p class="eyebrow">REVISIÓN DE LA RED CIENTÍFICA</p>
          <h2 id="research-funding-confirmation-title">Confirmar {{ research_funding_preview.name }} nivel {{ research_funding_preview.target_level }}</h2>
          <p>Coste total: {{ research_funding_preview.cost.metal|number }} metal, {{ research_funding_preview.cost.crystal|number }} cristal y {{ research_funding_preview.cost.deuterium|number }} deuterio.</p>
          <h3>Recursos que se descontarán</h3>
          <ul>
            {% for source in research_funding_preview.sources %}
            <li>{{ source.planet_name }}: {{ source.metal|number }} metal, {{ source.crystal|number }} cristal y {{ source.deuterium|number }} deuterio.</li>
            {% endfor %}
          </ul>
          <p>Si cancelas después, el 75 % volverá proporcionalmente a estos mismos planetas.</p>
          <form method="post" action="{{ url_for('research_action') }}" class="stacked-form">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="stage" value="confirm">
            <label class="check-row"><input type="checkbox" name="confirm" value="1" required> Confirmo la tecnología, el nivel, el coste total y el reparto entre planetas</label>
            <button class="button button-primary" type="submit">Iniciar investigación financiada</button>
          </form>
        </section>
        {% endif %}
        {% if not research_queue %}
        <section class="panel panel-wide" aria-labelledby="research-network-title">
          <p class="eyebrow">FINANCIACIÓN MULTIPLANETARIA</p>
          <h2 id="research-network-title">Red científica imperial</h2>
          <p>El planeta activo siempre puede aportar. Los mundos remotos necesitan laboratorio y Comunicación cuántica o Logística autónoma suficiente. La vista previa no gasta ni reserva recursos.</p>
          <form method="post" action="{{ url_for('research_action') }}" class="stacked-form">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="stage" value="preview">
            <label>Tecnología y próximo nivel
              <select name="item">
                {% for entry in entries %}<option value="{{ entry.key }}">{{ entry.definition.name }} nivel {{ entry.level + 1 }} — {{ entry.cost.metal|number }} metal, {{ entry.cost.crystal|number }} cristal, {{ entry.cost.deuterium|number }} deuterio</option>{% endfor %}
              </select>
            </label>
            <fieldset>
              <legend>Forma de repartir el coste</legend>
              <label class="check-row"><input type="radio" name="funding_mode" value="auto" checked> Reparto automático: usa el planeta activo primero y completa con nodos remotos</label>
              <label class="check-row"><input type="radio" name="funding_mode" value="manual"> Reparto manual: las cantidades deben sumar exactamente el coste</label>
            </fieldset>
            <div class="card-grid">
              {% for source in research_funding_planets %}
              <fieldset>
                <legend>{{ source.name }}{% if source.local %}, planeta activo{% else %}, nodo remoto{% endif %}</legend>
                <p>Laboratorio {{ source.research_lab }}. Disponible: {{ source.resources.metal|number }} metal, {{ source.resources.crystal|number }} cristal y {{ source.resources.deuterium|number }} deuterio.</p>
                <label>Metal aportado por {{ source.name }}<input type="number" name="fund_{{ source.id }}_metal" min="0" max="{{ source.resources.metal|int }}" value="0"></label>
                <label>Cristal aportado por {{ source.name }}<input type="number" name="fund_{{ source.id }}_crystal" min="0" max="{{ source.resources.crystal|int }}" value="0"></label>
                <label>Deuterio aportado por {{ source.name }}<input type="number" name="fund_{{ source.id }}_deuterium" min="0" max="{{ source.resources.deuterium|int }}" value="0"></label>
              </fieldset>
              {% endfor %}
            </div>
            <button class="button button-primary" type="submit">Calcular y revisar financiación</button>
          </form>
        </section>
        {% endif %}
        <section aria-labelledby="research-catalog-title"><h2 id="research-catalog-title" class="section-title">Catálogo de tecnologías</h2><div class="card-grid">
          {% for entry in entries %}<article class="tech-card">
            <div class="card-number science" aria-hidden="true">{{ '%02d'|format(loop.index) }}</div><div class="card-copy"><h3>{{ entry.definition.name }} <span>Nivel {{ entry.level }}</span></h3><p>{{ entry.definition.description }}</p><p><strong>Comparación con el nivel actual:</strong> {{ entry.next_benefit }}</p>
              {% if not entry.lab_ok %}<p class="requirement">Requiere Laboratorio nivel {{ entry.definition.lab }}.</p>{% endif %}{% if entry.missing %}<p class="requirement">Requiere: {{ entry.missing|join(', ') }}</p>{% endif %}
              <dl class="cost-list"><div><dt>Metal</dt><dd>{{ entry.cost.metal|number }}</dd></div><div><dt>Cristal</dt><dd>{{ entry.cost.crystal|number }}</dd></div><div><dt>Deuterio</dt><dd>{{ entry.cost.deuterium|number }}</dd></div><div><dt>Tiempo</dt><dd>{{ entry.duration|duration }}</dd></div></dl>
              <a href="{{ url_for('game_view', section='encyclopedia') }}#wiki-technology-{{ entry.key }}">Consultar ficha completa</a>
            </div><form method="post" action="{{ url_for('research_action') }}" data-cost-metal="{{ entry.cost.metal }}" data-cost-crystal="{{ entry.cost.crystal }}" data-cost-deuterium="{{ entry.cost.deuterium }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="item" value="{{ entry.key }}"><button class="button button-primary" type="submit" {% if research_queue or not entry.met or not entry.lab_ok %}disabled{% endif %}>Investigar nivel {{ entry.level + 1 }}</button></form>
          </article>{% endfor %}
        </div></section>
