{% load i18n static %}
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}

<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:'en' }}" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}">
  <head>
    <meta charset="utf-8">
    <title>
      {% block title %}{% if title %}{{ title }} | {% endif %}{{ site_title|default:'' }}{% endblock %}
    </title>

    {% block dark-mode-vars %}{% endblock %}

    {% block extrastyle %}
      {# Standard Django admin CSS (мы больше не наследуемся от admin/base.html). #}
      <link rel="stylesheet" href="{% static 'admin/css/base.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/dark_mode.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/unusable_password_field.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/forms.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/widgets.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/changelists.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/autocomplete.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/login.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/nav_sidebar.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/constance.css' %}">
      <link rel="stylesheet" href="{% static 'admin/css/dashboard.css' %}">

      {# Lucus styles (single scaffold entrypoint). #}
      <link rel="stylesheet" href="{% static 'lucus/css/style.css' %}">
    {% endblock %}

    {% block responsive %}
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}">
      {% if LANGUAGE_BIDI %}<link rel="stylesheet" href="{% static 'admin/css/responsive_rtl.css' %}">{% endif %}
      {% if LANGUAGE_BIDI %}<link rel="stylesheet" href="{% static 'admin/css/rtl.css' %}">{% endif %}
    {% endblock %}

    {% block extrahead %}
      <script>
        (function() {
          document.addEventListener('DOMContentLoaded', function() {
            var dropdown = document.getElementById('lucus-user-dropdown');
            if (!dropdown) return;
            var trigger = dropdown.querySelector('.lucus-header__user-trigger');
            var menu = dropdown.querySelector('.lucus-header__user-menu');
            if (!trigger || !menu) return;
            function open() {
              dropdown.setAttribute('data-open', 'true');
              menu.removeAttribute('hidden');
              trigger.setAttribute('aria-expanded', 'true');
            }
            function close() {
              dropdown.removeAttribute('data-open');
              menu.setAttribute('hidden', '');
              trigger.setAttribute('aria-expanded', 'false');
            }
            function toggle(e) {
              e.preventDefault();
              if (dropdown.getAttribute('data-open') === 'true') close(); else open();
            }
            trigger.addEventListener('click', toggle);
            document.addEventListener('click', function(e) {
              if (dropdown.getAttribute('data-open') === 'true' && !dropdown.contains(e.target)) close();
            });
          });
        })();
        (function() {
          document.addEventListener('DOMContentLoaded', function() {
            var nav = document.querySelector('.lucus-header__breadcrumbs .breadcrumbs');
            if (!nav || !nav.innerHTML.trim()) return;
            var html = nav.innerHTML.replace(/\s*(?:&rsaquo;|&#8250;|›)\s*/g, '<span class="lucus-breadcrumb-sep" aria-hidden="true">/</span>');
            nav.innerHTML = html;
            var nodes = nav.childNodes;
            for (var i = nodes.length - 1; i >= 0; i--) {
              var n = nodes[i];
              if (n.nodeType === 3 && n.textContent && n.textContent.trim()) {
                var span = document.createElement('span');
                span.className = 'lucus-breadcrumb-current';
                span.appendChild(n.cloneNode(true));
                n.parentNode.replaceChild(span, n);
                break;
              }
            }
          });
        })();
      </script>
    {% endblock %}
  </head>

  <body class="{% block bodyclass %}{% endblock %}">
    <a href="#content" class="skip-to-content-link">{% translate 'Skip to main content' %}</a>

    {% if not is_popup %}
      {% block header %}
        <header id="header" class="lucus-header">
          <div class="lucus-header__inner">
            <div class="lucus-header__left">
              <nav class="lucus-header__breadcrumbs" aria-label="{% translate 'Breadcrumbs' %}">
                {% block breadcrumbs %}{% endblock %}
              </nav>
            </div>
            {% if has_permission %}
              <div class="lucus-header__right">
                <span class="lucus-header__avatar" aria-hidden="true">G</span>
                <div class="lucus-header__user" id="lucus-user-dropdown">
                  <button type="button" class="lucus-header__user-trigger" aria-expanded="false" aria-controls="lucus-user-menu" aria-haspopup="true">
                    <span class="lucus-header__user-name">{% firstof user.get_short_name user.get_username %}</span>
                    <span class="lucus-header__user-chevron" aria-hidden="true"></span>
                  </button>
                  <div class="lucus-header__user-menu" id="lucus-user-menu" role="menu" hidden>
                    {% url "set_language" as set_language_url %}
                    {% if set_language_url %}
                      <form action="{{ set_language_url }}" method="post" class="lucus-header__lang-form lucus-header__user-item">
                        {% csrf_token %}
                        <input name="next" type="hidden" value="{{ request.get_full_path }}">
                        {% get_available_languages as admin_languages %}
                        <label for="lucus-lang" class="visually-hidden">{% translate "Язык админки" %}</label>
                        <select id="lucus-lang" name="language" onchange="this.form.submit()" aria-label="{% translate "Язык админки" %}">
                          {% for code, name in admin_languages %}
                            <option value="{{ code }}" {% if LANGUAGE_CODE == code %}selected{% endif %}>{{ name }}</option>
                          {% endfor %}
                        </select>
                      </form>
                    {% endif %}
                    {% if site_url %}<a href="{{ site_url }}" class="lucus-header__user-item" role="menuitem">{% translate 'View site' %}</a>{% endif %}
                    {% if user.has_usable_password %}<a href="{% url 'admin:password_change' %}" class="lucus-header__user-item" role="menuitem">{% translate 'Change password' %}</a>{% endif %}
                    <form id="logout-form" method="post" action="{% url 'admin:logout' %}" class="lucus-header__logout-form lucus-header__user-item">
                      {% csrf_token %}
                      <button type="submit" class="lucus-header__user-item lucus-header__user-item-btn" role="menuitem">{% translate 'Log out' %}</button>
                    </form>
                  </div>
                </div>
              </div>
            {% endif %}
          </div>
        </header>
      {% endblock %}
    {% endif %}

    <div id="container">
      <div class="main">
        <div id="content-start">
          {% block nav-breadcrumbs %}{% endblock %}
        </div>

        {% if not is_popup and is_nav_sidebar_enabled %}
          <nav id="nav-sidebar" class="nav-sidebar" aria-label="{% translate 'Sidebar' %}">
            {% block nav-sidebar %}{% endblock %}
          </nav>
        {% endif %}

        <div id="content" class="content">
          {% block messages %}
            {% if messages %}
              <ul class="messagelist">
                {% for message in messages %}
                  <li class="{{ message.tags }}">{{ message|capfirst }}</li>
                {% endfor %}
              </ul>
            {% endif %}
          {% endblock %}

          {% block pretitle %}{% endblock %}
          {% block content_title %}{% if title %}{{ title }}{% endif %}{% endblock %}

          {% block object-tools %}{% endblock %}
          {% block content %}{{ content }}{% endblock %}

          {% block sidebar %}{% endblock %}
          {% block footer %}{% endblock %}
        </div>
      </div>
    </div>

    {% block extrabody %}
      {# Standard Django admin JS. #}
      <script src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
      <script src="{% static 'admin/js/vendor/xregexp/xregexp.js' %}"></script>
      <script src="{% static 'admin/js/vendor/select2/select2.full.js' %}"></script>

      <script src="{% static 'admin/js/jquery.init.js' %}"></script>
      <script src="{% static 'admin/js/core.js' %}"></script>
      <script src="{% static 'admin/js/theme.js' %}"></script>

      <script src="{% static 'admin/js/actions.js' %}"></script>
      <script src="{% static 'admin/js/calendar.js' %}"></script>
      <script src="{% static 'admin/js/SelectFilter2.js' %}"></script>
      <script src="{% static 'admin/js/SelectBox.js' %}"></script>
      <script src="{% static 'admin/js/urlify.js' %}"></script>

      <script src="{% static 'admin/js/autocomplete.js' %}"></script>
      <script src="{% static 'admin/js/filters.js' %}"></script>
      <script src="{% static 'admin/js/inlines.js' %}"></script>

      <script src="{% static 'admin/js/prepopulate_init.js' %}"></script>
      <script src="{% static 'admin/js/prepopulate.js' %}"></script>

      <script src="{% static 'admin/js/change_form.js' %}"></script>
      <script src="{% static 'admin/js/popup_response.js' %}"></script>
      <script src="{% static 'admin/js/unusable_password_field.js' %}"></script>
      <script src="{% static 'admin/js/admin/DateTimeShortcuts.js' %}"></script>
      <script src="{% static 'admin/js/admin/RelatedObjectLookups.js' %}"></script>
    {% endblock extrabody %}
  </body>
</html>
