<header class="page-heading split-heading">
  <div><p class="eyebrow">CENTRO DE MANDO</p><h1>{{ planet.name }}</h1><p>Coordenadas [{{ planet.galaxy }}:{{ planet.system }}:{{ planet.position }}] · {{ snap.fields_used }} de {{ snap.fields_max }} campos planetarios ocupados{% if moon %} · luna {{ moon.name }} con {{ moon.fields_max }} campos{% endif %}</p></div>
  <div class="world-orbit" aria-hidden="true"><span></span></div>
</header>
{% if pending_events %}<div class="notice notice-warning" role="status"><strong>{{ pending_events }} {{ 'decisión narrativa pendiente' if pending_events == 1 else 'decisiones narrativas pendientes' }}.</strong> Visita <a href="{{ url_for('game_view', section='operations') }}">Operaciones</a> para responder.</div>{% endif %}
{% if not current_ground_force or current_ground_force.soldiers < 100 %}
  <div class="notice notice-warning" role="status">
    <strong>Defensa terrestre insuficiente en {{ planet.name }}.</strong>
    Las defensas del Astillero solo protegen la órbita: no detienen tropas desembarcadas.
    <a href="{{ url_for('game_view', section='invasions', planet=planet.id) }}">Reclutar guarnición terrestre</a>.
  </div>
{% endif %}
{% set offline_report = config.get('last_offline_report') %}
{% if offline_report and offline_report.get('elapsed_seconds',0) >= 1 %}
  {% if offline_report.get('mode') == 'frozen' %}
    <div class="notice notice-success" role="status"><strong>Universo recuperado sin avance.</strong> Durante {{ offline_report.elapsed_seconds|duration }} con el servidor cerrado quedaron congelados tus recursos, los rivales y todos los temporizadores. <a href="{{ url_for('game_view', section='settings') }}#offline-report-title">Consultar el informe</a>.</div>
  {% elif offline_report.get('mode') == 'realtime' %}
    <div class="notice notice-warning" role="status"><strong>Ausencia reconstruida.</strong> Se han simulado {{ offline_report.elapsed_seconds|duration }}, {{ offline_report.get('ai_actions',0) }} decisiones de IA y {{ offline_report.get('attacks_against_player',0) }} ataques resueltos contra tu imperio. <a href="{{ url_for('game_view', section='settings') }}#offline-report-title">Consultar estadísticas completas</a>.</div>
  {% endif %}
{% endif %}

<section class="dashboard-section collapsible-panel" data-collapsible-panel data-panel-key="overview-metrics" data-default-open="true">
  <h2 class="collapsible-heading"><button type="button" aria-label="Estado general" aria-expanded="true" aria-controls="overview-metrics-content"><span><small>IMPERIO</small>Estado general</span><span data-panel-indicator aria-hidden="true">−</span></button></h2>
  <div id="overview-metrics-content" data-panel-content>
    <div class="metric-grid metric-grid-eight" aria-label="Estado del imperio">
      <article><span>Puntuación</span><strong>{{ empire.score|number }}</strong><small>puntos de desarrollo</small></article>
      <article><span>Planetas</span><strong>{{ planets|length }}</strong><small>{{ 'mundo bajo tu mando' if planets|length == 1 else 'mundos bajo tu mando' }}</small></article>
      <article><span>Flotas activas</span><strong>{{ fleets|length }} / {{ 1 + techs.get('computer_tech', 0) }}</strong><small>ranuras utilizadas</small></article>
      <article><span>Rendimiento</span><strong>{{ (snap.rates.energy_ratio * 100)|round|int }} %</strong><small>balance energético</small></article>
      <article><span>Población</span><strong>{{ snap.population|number }}</strong><small>habitantes activos</small></article>
      <article><span>Moral</span><strong>{{ snap.morale|round|int }} %</strong><small>política: {{ POLICIES[snap.policy].name }}</small></article>
      <article><span>Expediciones</span><strong>{{ active_expeditions|length }}</strong><small>{{ 'equipo en espacio profundo' if active_expeditions|length == 1 else 'equipos en espacio profundo' }}</small></article>
      <article><span>Artefactos</span><strong>{{ artifact_count }}</strong><small>{{ 'tecnología irrepetible' if artifact_count == 1 else 'tecnologías irrepetibles' }}</small></article>
    </div>
  </div>
</section>

<div class="dashboard-grid">
  <section class="panel collapsible-panel" data-collapsible-panel data-panel-key="overview-activity" data-default-open="true">
    <h2 class="collapsible-heading"><button type="button" aria-label="Colas y movimientos" aria-expanded="true" aria-controls="overview-activity-content"><span><small>ACTIVIDAD</small>Colas y movimientos</span><span data-panel-indicator aria-hidden="true">−</span></button></h2>
    <div id="overview-activity-content" data-panel-content>
      {% if queues or fleets or active_expeditions or active_projects %}
        <ul class="activity-list">
          {% for queue in queues %}
            {% set definition = BUILDINGS.get(queue.item) or TECHNOLOGIES.get(queue.item) or SHIPS.get(queue.item) or DEFENSES.get(queue.item) %}
            <li data-queue-id="{{ queue.id }}" data-queue-category="{{ queue.category }}"><div><strong>{{ definition.name }}</strong><span>{{ 'Nivel ' ~ queue.target_level if queue.target_level else queue.amount ~ ' unidades' }}</span></div><div class="queue-time"><span>Tiempo restante</span><time class="countdown" data-end="{{ queue.completes_at }}">{{ (queue.completes_at-now)|duration }}</time></div></li>
          {% endfor %}
          {% for fleet in fleets %}
            <li><div><strong>{{ MISSIONS[fleet.mission] }} [{{ fleet.target_galaxy }}:{{ fleet.target_system }}:{{ fleet.target_position }}]</strong><span>{{ 'En ruta' if fleet.status == 'outbound' else 'De regreso' }}</span></div><time class="countdown" data-end="{{ fleet.arrival_at if fleet.status == 'outbound' else fleet.return_at }}">{{ ((fleet.arrival_at if fleet.status == 'outbound' else fleet.return_at)-now)|duration }}</time></li>
          {% endfor %}
          {% for expedition in active_expeditions %}
            <li><div><strong>{{ expedition.ship_name }} · {{ EXPEDITION_PROFILES[expedition.profile].name }}</strong><span>Expedición profunda a [{{ expedition.target_galaxy }}:{{ expedition.target_system }}:{{ expedition.target_position }}]</span></div><time class="countdown" data-end="{{ expedition.completes_at }}">{{ (expedition.completes_at-now)|duration }}</time></li>
          {% endfor %}
          {% for project in active_projects %}
            <li data-project-id="{{ project.id }}"><div><strong>{{ MEGAPROJECTS[project.project_key].name }}</strong><span>Megaproyecto imperial en construcción</span></div><div class="queue-time"><span>Tiempo restante</span><time class="countdown" data-end="{{ project.completes_at }}">{{ (project.completes_at-now)|duration }}</time></div></li>
          {% endfor %}
        </ul>
      {% else %}<p class="empty-state">No hay colas ni flotas activas. Empieza ampliando tu economía.</p>{% endif %}
      <div class="button-row"><a class="button" href="{{ url_for('game_view', section='economy') }}">Construir</a><a class="button button-secondary" href="{{ url_for('game_view', section='fleet') }}">Gestionar flotas</a></div>
    </div>
  </section>

  <section class="panel collapsible-panel" data-collapsible-panel data-panel-key="overview-ranking" data-default-open="false">
    <h2 class="collapsible-heading"><button type="button" aria-label="Clasificación" aria-expanded="false" aria-controls="overview-ranking-content"><span><small>UNIVERSO</small>Clasificación</span><span data-panel-indicator aria-hidden="true">+</span></button></h2>
    <div id="overview-ranking-content" data-panel-content hidden>
      <div class="table-wrap"><table><caption class="sr-only">Clasificación de todos los imperios</caption><thead><tr><th scope="col">Pos.</th><th scope="col">Imperio</th><th scope="col">Estado</th><th scope="col">Perfil</th><th scope="col">Planetas</th><th scope="col">Puntos</th></tr></thead><tbody>
        {% for rival in standings %}<tr {% if rival.id == empire.id %}class="is-player"{% endif %}><td>{{ rival.position_rank }}</td><th scope="row">{{ rival.name }}{% if rival.id == empire.id %} <span class="sr-only">(tú)</span>{% endif %}</th><td>{{ 'Activo' if rival.planet_count else 'Eliminado' }}</td><td>{{ 'Humano' if rival.is_human else AI_STYLES[rival.ai_style] }}</td><td>{{ rival.planet_count }}</td><td>{{ rival.score|number }}</td></tr>{% endfor %}
      </tbody></table></div>
    </div>
  </section>

  <section class="panel panel-wide collapsible-panel" data-collapsible-panel data-panel-key="overview-reports" data-default-open="false">
    <h2 class="collapsible-heading"><button type="button" aria-label="Últimos informes" aria-expanded="false" aria-controls="overview-reports-content"><span><small>COMUNICACIONES</small>Últimos informes</span><span data-panel-indicator aria-hidden="true">+</span></button></h2>
    <div id="overview-reports-content" data-panel-content hidden>
      <p><a href="{{ url_for('game_view', section='messages') }}">Ver todos los informes</a></p>
      {% if recent_messages %}<ul class="report-list">{% for message in recent_messages %}<li><span class="report-category">{{ message.category }}</span><div><strong>{{ message.subject }}</strong><p>{{ message.body|truncate(150) }}</p></div></li>{% endfor %}</ul>{% else %}<p class="empty-state">Aún no se han recibido informes.</p>{% endif %}
    </div>
  </section>
</div>
