<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{% block title %}{{ config.APP_NAME }}{% endblock %}</title>
  <link rel="icon" href="{{ url_for('static', filename='favicon.svg') }}" type="image/svg+xml">

  <!-- Fonts -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&family=Fraunces:ital,opsz,wght@0,9..144,100..900;1,9..144,100..900&display=swap" rel="stylesheet">

  <!-- Tailwind CSS -->
  <link rel="stylesheet" href="{{ url_for('static', filename='css/output.css') }}">

  {% block head %}{% endblock %}
</head>
<body class="bg-cream">

  <!-- ── App Shell ── -->
  <div class="app-shell">

    <!-- Slim App Header -->
    <header class="app-header">
      <a href="{{ url_for('public.landing') }}" class="nav-logo">{{ config.APP_NAME }}</a>
      <div class="app-header-right">
        {% if session.get('is_admin') %}
        <a href="{{ url_for('admin.index') }}" class="nav-badge hidden sm:inline">Admin</a>
        {% endif %}
        <span class="app-header-email">{{ user.email }}</span>
        <form method="post" action="{{ url_for('auth.logout') }}" class="nav-form">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
          <button type="submit" class="btn-outline btn-sm">Sign Out</button>
        </form>
      </div>
    </header>

    <!-- Body: sidebar + content -->
    <div class="app-body">

      <!-- ── Sidebar (desktop) ── -->
      <nav class="app-sidebar" aria-label="Main navigation">
        <a href="{{ url_for('dashboard.index') }}"
           class="sidebar-item{% if request.path == '/dashboard/' %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
            <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/>
          </svg>
          Pulse
        </a>
        <a href="{{ url_for('dashboard.supply') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/supply') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
            <line x1="18" y1="20" x2="18" y2="10"/>
            <line x1="12" y1="20" x2="12" y2="4"/>
            <line x1="6" y1="20" x2="6" y2="14"/>
          </svg>
          Supply
        </a>
        <a href="{{ url_for('dashboard.positioning') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/positioning') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
            <polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/>
            <polyline points="17 6 23 6 23 12"/>
          </svg>
          Positioning
        </a>
        <a href="{{ url_for('dashboard.warehouse') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/warehouse') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
            <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
            <polyline points="3.27 6.96 12 12.01 20.73 6.96"/>
            <line x1="12" y1="22.08" x2="12" y2="12"/>
          </svg>
          Warehouse
        </a>
        <a href="{{ url_for('dashboard.weather') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/weather') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
            <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/>
            <circle cx="12" cy="12" r="4"/>
          </svg>
          Weather
        </a>
        <a href="{{ url_for('dashboard.countries') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/countries') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" viewBox="0 0 24 24" aria-hidden="true">
            <circle cx="12" cy="12" r="9"/>
            <path d="M3 12h18"/>
            <path d="M12 3c-3.5 4.5-3.5 13.5 0 18"/>
            <path d="M12 3c3.5 4.5 3.5 13.5 0 18"/>
          </svg>
          Origins
        </a>
        <a href="{{ url_for('dashboard.settings') }}"
           class="sidebar-item{% if request.path.startswith('/dashboard/settings') %} active{% endif %}">
          <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" viewBox="0 0 24 24" aria-hidden="true">
            <circle cx="12" cy="12" r="3"/>
            <path d="M12 2v2M12 20v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M2 12h2M20 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/>
          </svg>
          Settings
        </a>
      </nav>

      <!-- ── Main Content ── -->
      <main class="app-content">
        {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
          {% for category, message in messages %}
          <div class="{% if category == 'error' %}flash-error{% elif category == 'success' %}flash-success{% elif category == 'warning' %}flash-warning{% else %}flash{% endif %} mb-4">
            {{ message }}
          </div>
          {% endfor %}
        {% endif %}
        {% endwith %}

        {% block content %}{% endblock %}
      </main>
    </div>
  </div>

  <!-- ── Mobile Bottom Nav ── -->
  <nav class="mobile-bottom-nav" aria-label="Mobile navigation">
    <a href="{{ url_for('dashboard.index') }}"
       class="mobile-nav-item{% if request.path == '/dashboard/' %} active{% endif %}">
      <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
        <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/>
      </svg>
      <span>Pulse</span>
    </a>
    <a href="{{ url_for('dashboard.supply') }}"
       class="mobile-nav-item{% if request.path.startswith('/dashboard/supply') %} active{% endif %}">
      <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
        <line x1="18" y1="20" x2="18" y2="10"/>
        <line x1="12" y1="20" x2="12" y2="4"/>
        <line x1="6" y1="20" x2="6" y2="14"/>
      </svg>
      <span>Supply</span>
    </a>
    <a href="{{ url_for('dashboard.positioning') }}"
       class="mobile-nav-item{% if request.path.startswith('/dashboard/positioning') %} active{% endif %}">
      <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
        <polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/>
        <polyline points="17 6 23 6 23 12"/>
      </svg>
      <span>Position</span>
    </a>
    <a href="{{ url_for('dashboard.warehouse') }}"
       class="mobile-nav-item{% if request.path.startswith('/dashboard/warehouse') %} active{% endif %}">
      <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
        <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
        <polyline points="3.27 6.96 12 12.01 20.73 6.96"/>
        <line x1="12" y1="22.08" x2="12" y2="12"/>
      </svg>
      <span>Warehouse</span>
    </a>
    <a href="{{ url_for('dashboard.weather') }}"
       class="mobile-nav-item{% if request.path.startswith('/dashboard/weather') %} active{% endif %}">
      <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
        <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/>
        <circle cx="12" cy="12" r="4"/>
      </svg>
      <span>Weather</span>
    </a>
  </nav>

  <!-- HTMX -->
  <script src="https://unpkg.com/htmx.org@2.0.4"></script>

  <!-- Destroy Chart.js instances before HTMX swaps canvas-containing elements.
       Without this, the old chart's ResizeObserver stays alive on the parent container
       and fights the new chart instance, producing wrong dimensions after the swap. -->
  <script>
  document.addEventListener('htmx:beforeSwap', function(e) {
    if (typeof Chart === 'undefined') return;
    e.detail.target.querySelectorAll('canvas').forEach(function(canvas) {
      var chart = Chart.getChart(canvas);
      if (chart) chart.destroy();
    });
  });
  </script>

  {% include "_feedback_widget.html" %}

  {% block scripts %}{% endblock %}
</body>
</html>
