<!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>
  <!-- Navigation -->
  <nav class="nav-bar">
    <div class="nav-inner">
      <a href="{{ url_for('public.landing') }}" class="nav-logo">{{ config.APP_NAME }}</a>
      <button class="nav-hamburger" aria-label="Toggle menu" onclick="document.querySelector('.nav-links').classList.toggle('open')">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round">
          <line x1="3" y1="6" x2="21" y2="6"/>
          <line x1="3" y1="12" x2="21" y2="12"/>
          <line x1="3" y1="18" x2="21" y2="18"/>
        </svg>
      </button>
      <div class="nav-links">
        <a href="{{ url_for('public.features') }}">Features</a>
        <a href="{{ url_for('billing.pricing') }}">Pricing</a>
        {% if user %}
        <a href="{{ url_for('dashboard.index') }}">Dashboard</a>
        {% if session.get('is_admin') %}
        <a href="{{ url_for('admin.index') }}"><span class="nav-badge">Admin</span></a>
        {% endif %}
        <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>
        {% else %}
        <a href="{{ url_for('auth.login') }}">Sign In</a>
        <a href="{{ url_for('auth.signup') }}" class="nav-auth-btn">Get Started</a>
        {% endif %}
      </div>
    </div>
  </nav>

  <!-- Flash messages -->
  {% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
  <div class="container-page mt-4">
    {% 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 %}">
      {{ message }}
    </div>
    {% endfor %}
  </div>
  {% endif %}
  {% endwith %}

  {% block content %}{% endblock %}

  <!-- Footer -->
  <footer class="bg-latte mt-20">
    <div class="container-page py-12">
      <div class="grid-3">
        <div>
          <strong class="heading-display text-lg">{{ config.APP_NAME }}</strong>
          <p class="text-sm text-stone mt-2 max-w-xs">Coffee commodity data for traders who refuse to overpay for market intelligence.</p>
        </div>
        <div>
          <strong class="text-espresso text-sm">Product</strong>
          <ul class="list-none p-0 mt-2 space-y-1.5 text-sm">
            <li><a href="{{ url_for('public.features') }}">Features</a></li>
            <li><a href="{{ url_for('billing.pricing') }}">Pricing</a></li>
            <li><a href="{{ url_for('public.about') }}">About</a></li>
          </ul>
        </div>
        <div>
          <strong class="text-espresso text-sm">Legal</strong>
          <ul class="list-none p-0 mt-2 space-y-1.5 text-sm">
            <li><a href="{{ url_for('public.terms') }}">Terms</a></li>
            <li><a href="{{ url_for('public.privacy') }}">Privacy</a></li>
          </ul>
        </div>
      </div>
      <div class="border-t border-parchment mt-8 pt-6 text-center text-xs text-stone">
        &copy; {{ now.year }} {{ config.APP_NAME }}. All rights reserved.
      </div>
    </div>
  </footer>

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

  <!-- Feedback Widget -->
  <div id="feedback-widget" style="position:fixed;bottom:20px;right:20px;z-index:100;">
    <button
      id="feedback-btn"
      onclick="document.getElementById('feedback-popover').classList.toggle('open')"
      style="
        background:#2C1810;color:#fff;border:none;border-radius:20px;
        padding:8px 16px;font-size:13px;font-weight:600;cursor:pointer;
        box-shadow:0 4px 12px rgba(0,0,0,0.2);display:flex;align-items:center;gap:6px;
        transition:background 0.15s;
      "
      onmouseover="this.style.background='#4A2C1A'"
      onmouseout="this.style.background='#2C1810'"
    >
      <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
        <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
      </svg>
      Feedback
    </button>

    <div
      id="feedback-popover"
      style="
        display:none;position:absolute;bottom:44px;right:0;
        width:300px;background:#fff;border:1px solid #E8DFD5;
        border-radius:10px;box-shadow:0 8px 30px rgba(0,0,0,0.12);
        padding:16px;
      "
    >
      <p style="font-size:14px;font-weight:600;color:#2C1810;margin:0 0 4px;">Share feedback</p>
      <p style="font-size:12px;color:#78716C;margin:0 0 12px;">What's on your mind?</p>
      <form
        hx-post="{{ url_for('public.feedback') }}"
        hx-target="#feedback-result"
        hx-swap="innerHTML"
        hx-on::after-request="this.reset()"
      >
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <input type="hidden" name="page_url" id="feedback-page-url" value="">
        <textarea
          name="message"
          placeholder="Tell us what you think..."
          required
          maxlength="2000"
          style="
            width:100%;box-sizing:border-box;resize:vertical;min-height:80px;
            border:1px solid #E8DFD5;border-radius:6px;padding:8px 10px;
            font-size:13px;color:#2C1810;outline:none;font-family:inherit;
            transition:border-color 0.15s;
          "
          onfocus="this.style.borderColor='#B45309'"
          onblur="this.style.borderColor='#E8DFD5'"
        ></textarea>
        <div style="display:flex;justify-content:space-between;align-items:center;margin-top:8px;">
          <div id="feedback-result" style="font-size:12px;"></div>
          <button
            type="submit"
            style="
              background:#B45309;color:#fff;border:none;border-radius:6px;
              padding:6px 14px;font-size:13px;font-weight:600;cursor:pointer;
              transition:background 0.15s;flex-shrink:0;
            "
            onmouseover="this.style.background='#92400E'"
            onmouseout="this.style.background='#B45309'"
          >Send</button>
        </div>
      </form>
    </div>
  </div>

  <style>
    #feedback-popover.open { display: block; }
  </style>

  <script>
    var urlInput = document.getElementById('feedback-page-url');
    if (urlInput) urlInput.value = window.location.href;

    document.addEventListener('click', function(e) {
      var widget = document.getElementById('feedback-widget');
      if (widget && !widget.contains(e.target)) {
        var popover = document.getElementById('feedback-popover');
        if (popover) popover.classList.remove('open');
      }
    });
  </script>

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