<!doctype html>
<html>
<head>
  <title>OnlyScavs – {{ gun.name }}</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    :root {
      --bg: #121212;
      --panel: #1a1a1a;
      --text: #eee;
      --muted: #bbb;
      --border: #333;
      --accent: #9ccfff;
      --amber: #ffd580;
      --key-border: #5a7a3a;
      --key-bg: #141e10;
    }
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      background: var(--bg);
      color: var(--text);
      margin: 0;
      padding-top: 52px;
      background-image: url('/assets/onlyscavs.png');
      background-attachment: fixed;
      background-size: cover;
      background-position: center 65%;
    }
    body::before {
      content: '';
      position: fixed;
      inset: 0;
      background: rgba(14,14,14,0.88);
      pointer-events: none;
      z-index: 0;
    }
    .page { max-width: 900px; margin: 0 auto; padding: 24px 16px; position: relative; z-index: 1; }
    a { color: var(--accent); }
    .site-nav {
      position: fixed; top: 0; left: 0; right: 0; z-index: 100;
      display: flex; align-items: center; justify-content: space-between;
      padding: 0 24px; height: 52px;
      background: rgba(14,14,14,0.92);
      backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
      border-bottom: 1px solid rgba(255,255,255,0.06);
    }
    .nav-brand { font-size: 0.85rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); text-decoration: none; flex-shrink: 0; }
    .nav-links { display: flex; gap: 2px; flex-wrap: wrap; }
    .nav-links a { color: #666; text-decoration: none; font-size: 0.8rem; padding: 5px 10px; border-radius: 5px; transition: color 0.15s, background 0.15s; }
    .nav-links a:hover { color: var(--text); background: rgba(255,255,255,0.06); }
    .nav-links a.active { color: var(--accent); background: rgba(156,207,255,0.08); }
    h1 { margin: 0 0 2px; font-size: 1.4rem; }
    .subtitle { color: var(--muted); font-size: 0.9rem; margin: 0 0 20px; }

    /* Gun summary card */
    .gun-card {
      display: flex;
      gap: 16px;
      align-items: flex-start;
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 24px;
    }
    .gun-card img { width: 96px; height: 96px; object-fit: contain; background: #222; border-radius: 6px; }
    .gun-stats { display: flex; gap: 24px; flex-wrap: wrap; margin-top: 8px; }
    .stat { display: flex; flex-direction: column; }
    .stat .val { font-size: 1.1rem; font-weight: bold; color: var(--amber); }
    .stat .lbl { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
    .gun-name { flex: 1; }
    .gun-name h2 { margin: 0 0 4px; font-size: 1.2rem; }
    .gun-name .sub { color: var(--muted); font-size: 0.85rem; margin-bottom: 10px; }

    /* Total bar */
    .total-bar {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 8px;
      padding: 12px 20px;
      margin-bottom: 24px;
      display: flex;
      align-items: baseline;
      gap: 10px;
    }
    .total-bar .big { font-size: 1.6rem; font-weight: bold; color: var(--amber); }
    .total-bar .lbl { color: var(--muted); font-size: 0.9rem; }
    .total-bar .breakdown { color: var(--muted); font-size: 0.82rem; margin-left: auto; }

    /* Section headers */
    .section-hdr {
      font-size: 0.78rem;
      font-weight: bold;
      text-transform: uppercase;
      letter-spacing: 0.08em;
      color: var(--muted);
      padding: 10px 0 6px;
      border-bottom: 1px solid var(--border);
      margin-bottom: 8px;
    }

    /* Slot cards */
    .slot-section { margin-bottom: 24px; }
    .slot-card {
      border: 1px solid var(--border);
      border-radius: 8px;
      margin-bottom: 10px;
      overflow: hidden;
    }
    .slot-card.key-slot {
      border-color: var(--key-border);
      background: var(--key-bg);
    }
    .slot-header {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 8px 12px;
      cursor: pointer;
      user-select: none;
      background: rgba(255,255,255,0.03);
    }
    .slot-header:hover { background: rgba(255,255,255,0.06); }
    .slot-header .slot-name { font-weight: bold; font-size: 0.9rem; flex: 1; }
    .slot-header .slot-count { color: var(--muted); font-size: 0.8rem; }
    .slot-header .slot-lightest { color: var(--amber); font-size: 0.85rem; white-space: nowrap; }
    .slot-header .chevron { color: var(--muted); font-size: 0.8rem; transition: transform 0.15s; }
    .slot-card.open .chevron { transform: rotate(90deg); }
    .required-badge {
      font-size: 0.7rem;
      background: #2a2a2a;
      border: 1px solid #555;
      border-radius: 4px;
      padding: 1px 5px;
      color: var(--muted);
    }
    .key-badge {
      font-size: 0.7rem;
      background: #1a3010;
      border: 1px solid var(--key-border);
      border-radius: 4px;
      padding: 1px 5px;
      color: #8fc87f;
    }

    /* Mod list inside slot */
    .mod-list { display: none; border-top: 1px solid var(--border); }
    .slot-card.open .mod-list { display: block; }
    .mod-row {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 6px 12px;
      border-bottom: 1px solid #1a1a1a;
      font-size: 0.85rem;
    }
    .mod-row:last-child { border-bottom: none; }
    .mod-row:hover { background: rgba(255,255,255,0.03); }
    .mod-row img { width: 36px; height: 36px; object-fit: contain; background: #222; border-radius: 4px; flex-shrink: 0; }
    .mod-name { flex: 1; }
    .mod-name small { display: block; color: var(--muted); font-size: 0.78rem; }
    .mod-weight { font-weight: bold; color: var(--amber); white-space: nowrap; min-width: 60px; text-align: right; }
    .mod-weight.lightest { color: #8fc87f; }
    .mod-wiki { font-size: 0.78rem; color: var(--muted); }
    .no-mods { padding: 10px 12px; color: var(--muted); font-size: 0.85rem; font-style: italic; }

    /* Toggle other slots */
    .toggle-other {
      background: none;
      border: 1px solid var(--border);
      border-radius: 6px;
      color: var(--muted);
      padding: 5px 14px;
      font-size: 0.85rem;
      cursor: pointer;
      margin-bottom: 12px;
    }
    .toggle-other:hover { border-color: var(--accent); color: var(--accent); }
    #other-slots { display: none; }
    #other-slots.visible { display: block; }
  </style>
</head>
<body>
<div class="page">
  <nav class="site-nav">
    <a class="nav-brand" href="/">OnlyScavs</a>
    <div class="nav-links">
      <a href="/keys">Keys</a>
      <a href="/collector">Collector</a>
      <a href="/quests">Quests</a>
      <a href="/loadout" class="active">Loadout</a>
      <a href="/meds">Injectors</a>
      <a href="/barters">Barters</a>
    </div>
  </nav>

  <div class="gun-card">
    {% if gun.grid_image_url %}
      <img src="{{ gun.grid_image_url }}" alt="{{ gun.name }}">
    {% endif %}
    <div class="gun-name">
      <h2>{{ gun.name }}</h2>
      <div class="sub">
        {{ gun.caliber or '?' }}
        {% if gun.wiki_url %}&nbsp;<a href="{{ gun.wiki_url }}" target="_blank">wiki ↗</a>{% endif %}
      </div>
      <div class="gun-stats">
        <div class="stat">
          <span class="val">{{ "%.3f"|format(gun.weight_kg) if gun.weight_kg is not none else '?' }}</span>
          <span class="lbl">Base weight (kg)</span>
        </div>
        {% if gun.ergonomics %}
        <div class="stat">
          <span class="val">{{ gun.ergonomics }}</span>
          <span class="lbl">Ergonomics</span>
        </div>
        {% endif %}
        {% if gun.recoil_vertical %}
        <div class="stat">
          <span class="val">{{ gun.recoil_vertical }}</span>
          <span class="lbl">Recoil (V)</span>
        </div>
        {% endif %}
        {% if gun.fire_rate %}
        <div class="stat">
          <span class="val">{{ gun.fire_rate }}</span>
          <span class="lbl">Fire rate</span>
        </div>
        {% endif %}
      </div>
    </div>
  </div>

  <div class="total-bar">
    <span class="lbl">Lightest possible build:</span>
    <span class="big">{{ "%.3f"|format(lightest_total) }} kg</span>
    <span class="breakdown">base {{ "%.3f"|format(gun.weight_kg or 0) }} kg + lightest mod per required slot</span>
  </div>

  {# ---- Macro to render a slot card ---- #}
  {% macro slot_card(slot, extra_class='', open=False) %}
    {% set lightest = slot.mods[0] if slot.mods else none %}
    <div class="slot-card {{ extra_class }} {% if open %}open{% endif %}" id="slot-{{ slot.slot_id }}">
      <div class="slot-header" onclick="toggleSlot('slot-{{ slot.slot_id }}')">
        <span class="slot-name">{{ slot.slot_name }}</span>
        {% if extra_class == 'key-slot' %}<span class="key-badge">key slot</span>{% endif %}
        <span class="slot-count">{{ slot.mods | length }} mods</span>
        <span class="slot-lightest">
          {% if lightest and lightest.weight_kg is not none %}
            lightest {{ "%.3f"|format(lightest.weight_kg) }} kg
          {% else %}—{% endif %}
        </span>
        <span class="chevron">▶</span>
      </div>
      <div class="mod-list">
        {% if slot.mods %}
          {% for mod in slot.mods %}
          <div class="mod-row">
            {% if mod.grid_image_url %}
              <img src="{{ mod.grid_image_url }}" loading="lazy" alt="">
            {% else %}
              <div style="width:36px;height:36px;background:#222;border-radius:4px;flex-shrink:0"></div>
            {% endif %}
            <div class="mod-name">
              {{ mod.mod_name }}
              {% if mod.mod_short and mod.mod_short != mod.mod_name %}
                <small>{{ mod.mod_short }}</small>
              {% endif %}
            </div>
            {% if mod.wiki_url %}
              <a class="mod-wiki" href="{{ mod.wiki_url }}" target="_blank">wiki</a>
            {% endif %}
            <span class="mod-weight {% if loop.first %}lightest{% endif %}">
              {% if mod.weight_kg is not none %}{{ "%.3f"|format(mod.weight_kg) }} kg{% else %}—{% endif %}
            </span>
          </div>
          {% endfor %}
        {% else %}
          <div class="no-mods">No compatible mods found in database.</div>
        {% endif %}
      </div>
    </div>
  {% endmacro %}

  {# ---- Required slots (key slots highlighted, open by default) ---- #}
  {% if key_slots or req_slots %}
  <div class="slot-section">
    <div class="section-hdr">Required slots <span style="font-weight:normal;color:#666">(counted in lightest build weight)</span></div>
    {% for slot in key_slots %}
      {{ slot_card(slot, 'key-slot', open=True) }}
    {% endfor %}
    {% for slot in req_slots %}
      {{ slot_card(slot, '', open=False) }}
    {% endfor %}
  </div>
  {% endif %}

  {# ---- Optional slots (collapsed behind toggle) ---- #}
  {% if optional_slots %}
  <button class="toggle-other" onclick="toggleOther(this)">Show {{ optional_slots | length }} optional slots ▼</button>
  <div id="other-slots">
    <div class="slot-section">
      <div class="section-hdr">Optional slots <span style="font-weight:normal;color:#666">(not counted in lightest build weight)</span></div>
      {% for slot in optional_slots %}
        {{ slot_card(slot) }}
      {% endfor %}
    </div>
  </div>
  {% endif %}

</div>
<script>
  function toggleSlot(id) {
    document.getElementById(id).classList.toggle('open');
  }
  function toggleOther(btn) {
    const div = document.getElementById('other-slots');
    div.classList.toggle('visible');
    btn.textContent = div.classList.contains('visible')
      ? 'Hide optional slots ▲'
      : 'Show {{ optional_slots | length }} optional slots ▼';
  }
</script>
</body>
</html>
