<?php
require_once __DIR__ . '/includes/auth.php';
chat247_start_session();
$signedIn = !empty($_SESSION['bearer']);
?><!doctype html>
<html lang="en">
<head>
<base href="https://<?= htmlspecialchars($_SERVER['HTTP_HOST'] ?? '', ENT_QUOTES) ?>/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Pricing — 247ch.at</title>
<link rel="icon" href="/favicon.ico?v=<?= @filemtime(__DIR__ . '/favicon.ico') ?: time() ?>">
<style>
  :root { --brand:#3b82f6; --bg:#0b0f1a; --panel:#111827; --fg:#f3f4f6; --muted:#9ca3af; --border:#1f2937; --accent:#34d399; }
  * { box-sizing:border-box; }
  html,body { margin:0; padding:0; font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif; background:var(--bg); color:var(--fg); }
  a { color:var(--fg); }
  header { display:flex; align-items:center; justify-content:space-between; padding:18px 32px; border-bottom:1px solid var(--border); }
  header .logo { font-weight:700; font-size:20px; }
  header nav a { color:var(--fg); margin-left:18px; text-decoration:none; }
  header nav a.btn { background:var(--brand); padding:8px 16px; border-radius:6px; }
  header .header-search { display:flex; margin:0 12px; flex:0 0 auto; }
  header .header-search input { background:var(--panel); border:1px solid var(--border); color:var(--fg); padding:7px 12px; border-radius:6px; font-size:13px; width:220px; outline:none; }
  header .header-search input:focus { border-color:var(--brand); }
  @media (max-width:640px) { header .header-search { display:none; } }
  .hero { text-align:center; padding:64px 24px 24px; }
  .hero h1 { font-size:42px; margin:0 0 12px; }
  .hero p { font-size:18px; color:var(--muted); max-width:560px; margin:0 auto; }
  .tiers { display:grid; grid-template-columns:repeat(auto-fit,minmax(280px,1fr)); gap:24px; max-width:1140px; margin:48px auto 0; padding:0 24px; }
  .tier { background:var(--panel); border:1px solid var(--border); border-radius:14px; padding:32px; display:flex; flex-direction:column; position:relative; }
  .tier.pro { border-color:var(--brand); box-shadow:0 0 0 1px var(--brand); }
  .tier .badge { position:absolute; top:-14px; left:50%; transform:translateX(-50%); background:var(--brand); color:#fff; font-size:12px; padding:4px 12px; border-radius:999px; font-weight:600; letter-spacing:0.5px; }
  .tier h2 { margin:0 0 4px; font-size:22px; }
  .tier .price { font-size:38px; font-weight:700; margin:12px 0 4px; }
  .tier .price small { font-size:14px; color:var(--muted); font-weight:400; }
  .tier .recurring { color:var(--muted); font-size:12px; font-style:italic; margin:0 0 14px; }
  .tier .tag { color:var(--muted); margin:0 0 20px; font-size:14px; }
  .tier ul { list-style:none; padding:0; margin:0 0 24px; flex:1; }
  .tier li { padding:8px 0; font-size:14px; display:flex; align-items:flex-start; gap:8px; }
  .tier li::before { content:"\2713"; color:var(--accent); font-weight:700; flex-shrink:0; }
  .tier .cta { display:block; padding:12px; text-align:center; border-radius:8px; text-decoration:none; font-weight:600; }
  .tier .cta.primary { background:var(--brand); color:#fff; }
  .tier .cta.ghost { background:transparent; color:var(--fg); border:1px solid var(--border); }
  .faq { max-width:760px; margin:80px auto 0; padding:0 24px; }
  .faq h2 { font-size:28px; text-align:center; margin:0 0 32px; }
  .faq details { background:var(--panel); border:1px solid var(--border); border-radius:10px; padding:16px 20px; margin-bottom:12px; }
  .faq summary { cursor:pointer; font-weight:600; }
  .faq details[open] summary { margin-bottom:10px; }
  .faq p { margin:0; color:var(--muted); font-size:15px; line-height:1.55; }
</style>
</head>
<body>
<header>
  <div class="logo"><a href="/" style="text-decoration:none;color:var(--fg);">247ch.at</a></div>
  <form class="header-search" action="https://help.247ch.at/search.php" method="get" role="search">
    <input type="text" name="q" placeholder="Search docs…" aria-label="Search help docs">
  </form>
  <nav>
    <a href="/pricing" style="color:var(--brand);">Pricing</a>
    <?php if ($signedIn): ?>
      <a href="/dashboard" class="btn">Dashboard</a>
    <?php else: ?>
      <a href="/login">Sign in</a>
      <a href="/signup" class="btn">Create account</a>
    <?php endif; ?>
  </nav>
</header>

<section class="hero">
  <h1>Plans that fit your site</h1>
  <p>Start free. Upgrade to a recurring Pro plan — $19/month or $199/year (auto-renews) — when you want AI answering for you while you sleep.</p>
</section>

<section class="tiers">
  <div class="tier">
    <h2>Free</h2>
    <p class="tag">Get started</p>
    <div class="price">$0<small> / forever</small></div>
    <ul>
      <li>1 site</li>
      <li>Unlimited visitor messages</li>
      <li>Manual operator replies (no AI)</li>
      <li>"Powered by 247ch.at" badge in widget</li>
      <li>Community support</li>
    </ul>
    <a class="cta ghost" href="<?= $signedIn ? '/dashboard.php' : '/signup.php' ?>">
      <?= $signedIn ? 'Open dashboard' : 'Get started free' ?>
    </a>
  </div>

  <div class="tier pro">
    <div class="badge">MOST POPULAR</div>
    <h2>Pro Monthly</h2>
    <p class="tag">For sites that need 24/7 coverage</p>
    <div class="price"><strong>$19</strong><small> / month (auto-renews)</small></div>
    <p class="recurring">Recurring subscription. Cancel anytime from your dashboard.</p>
    <ul>
      <li>Unlimited sites</li>
      <li><strong>2500 AI replies / month included</strong> (ChatGPT, trained on your tone)</li>
      <li><strong>$5 per 1000 extra</strong> — purchase one-time top-ups, credits never expire</li>
      <li>Email notification when you're offline and a visitor sends a message</li>
      <li>No "Powered by" badge</li>
      <li>Priority email support</li>
      <li>Cash App + credit card billing</li>
    </ul>
    <?php if ($signedIn): ?>
      <a class="cta primary" href="/api/billing/checkout" data-checkout data-plan="monthly">Upgrade — $19/month (auto-renews)</a>
    <?php else: ?>
      <a class="cta primary" href="/signup?plan=monthly">Start Pro Monthly</a>
    <?php endif; ?>
  </div>

  <div class="tier">
    <h2>Pro Annual</h2>
    <p class="tag">Save $29 vs monthly</p>
    <div class="price"><strong>$199</strong><small> / year (auto-renews · save $29)</small></div>
    <p class="recurring">Recurring subscription. Cancel anytime from your dashboard.</p>
    <ul>
      <li>Everything in Pro Monthly</li>
      <li><strong>2500 AI replies / month included</strong> (same as monthly)</li>
      <li><strong>$5 per 1000 extra</strong> — one-time top-ups never expire</li>
      <li>Save $29/year vs $19 &times; 12 = $228</li>
      <li>Auto-renews yearly · cancel anytime</li>
    </ul>
    <?php if ($signedIn): ?>
      <a class="cta primary" href="/api/billing/checkout" data-checkout data-plan="annual">Upgrade — $199/year (auto-renews)</a>
    <?php else: ?>
      <a class="cta primary" href="/signup?plan=annual">Start Pro Annual</a>
    <?php endif; ?>
  </div>
</section>

<section class="faq">
  <h2>Questions, answered</h2>
  <details>
    <summary>What happens if I cancel?</summary>
    <p>You stay on Pro through the end of the billing cycle, then drop to Free. Your sites and chat history stay put — you just lose AI auto-reply and the offline-email pings. Both <strong>monthly</strong> and <strong>annual</strong> plans are recurring subscriptions; cancel anytime from your dashboard.</p>
  </details>
  <details>
    <summary>How does annual billing work?</summary>
    <p>Pro Annual is <strong>$199 charged once per year, auto-renews yearly</strong>. You save $29 vs paying $19/month for 12 months. Cancel anytime — if you cancel mid-term, you stay on Pro until the end of the year you paid for, then drop to Free.</p>
  </details>
  <details>
    <summary>Does the AI really handle every message?</summary>
    <p>On Pro, yes — every visitor message gets an instant AI reply trained on your tone and FAQ. You can still jump in and take over any conversation from the Windows app whenever you want.</p>
  </details>
  <details>
    <summary>Can I disable AI on a specific site?</summary>
    <p>Yep. The AI toggle is per-site in the dashboard. Turn it off on the sites you'd rather answer manually and leave it on everywhere else.</p>
  </details>
  <details>
    <summary>Is Cash App really supported?</summary>
    <p>It is. Stripe's Cash App Pay rail means you can subscribe straight from your Cash App balance — no credit card required.</p>
  </details>
  <details>
    <summary>Can I self-host?</summary>
    <p>The widget and the Windows operator app are free to run forever on a single site. The AI auto-reply + offline email features need our relay (that's what Pro pays for). If you want full source self-host, email <a href="mailto:support@247ch.at" style="color:var(--brand);">support@247ch.at</a> — we'll talk.</p>
  </details>
</section>

<script>
// Checkout: POST plan to /api/billing/checkout, follow returned Stripe URL.
// Requires a bearer; signed-in flow already gates this (the [data-checkout]
// anchors only render for $signedIn). Bearer is in the PHP session.
const BEARER = <?= json_encode($_SESSION['bearer'] ?? '') ?>;
document.querySelectorAll('a[data-checkout]').forEach(a => {
  a.addEventListener('click', async (e) => {
    e.preventDefault();
    const plan = a.dataset.plan || 'monthly';
    try {
      const r = await fetch('/api/billing/checkout', {
        method: 'POST',
        credentials: 'same-origin',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + BEARER,
        },
        body: JSON.stringify({ plan }),
      });
      const j = await r.json();
      if (j && j.url) { window.location.href = j.url; return; }
      alert('Could not start checkout' + (j && j.error ? ': ' + j.error : '. Please email support@247ch.at.'));
    } catch (err) {
      alert('Could not reach billing. Please try again or email support@247ch.at.');
    }
  });
});
</script>

<?php include __DIR__ . '/includes/footer.php'; ?>
</body>
</html>
