<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ url }}</title>
<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=JetBrains+Mono:ital,wght@0,400;0,500;0,600;1,400&display=swap" rel="stylesheet">
<style>
  :root{
    color-scheme: dark;
    --bg: #070a0f;
    --card: rgba(255,255,255,.06);
    --card2: rgba(255,255,255,.04);
    --border: rgba(255,255,255,.10);
    --text: rgba(255,255,255,.92);
    --muted: rgba(255,255,255,.60);
    --focus: rgba(255,255,255,.22);
    --shadow: 0 18px 55px rgba(0,0,0,.55);
    --radius: 16px;
  }

  * { box-sizing: border-box; margin: 0; padding: 0; }

  body{
    min-height: 100vh;
    display: grid;
    place-items: center;
    font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    background:
      radial-gradient(900px 500px at 20% 15%, rgba(255,255,255,.05), transparent 55%),
      radial-gradient(800px 500px at 80% 85%, rgba(255,255,255,.04), transparent 55%),
      var(--bg);
    color: var(--text);
    padding: 24px;
  }

  .card{
    width: 100%;
    max-width: 640px;
    padding: 22px;
  }

  form{
    display: flex;
    gap: 10px;
    margin-top: 0;
  }

  input[type="text"]{
    width: 100%;
    padding: 12px 14px;
    font-size: 1rem;
    color: var(--text);
    background: rgba(0,0,0,.30);
    border: 1px solid var(--border);
    border-radius: 12px;
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease;
    font-family: inherit;
  }

  input[type="text"]::placeholder{
    color: rgba(255,255,255,.38);
  }

  input[type="text"]:focus{
    border-color: var(--focus);
    box-shadow: 0 0 0 4px rgba(255,255,255,.06);
  }

  button{
    flex: 0 0 auto;
    padding: 12px 16px;
    font-size: 0.98rem;
    font-weight: 600;
    color: var(--text);
    background: rgba(255,255,255,.10);
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: transform .08s ease, background .15s ease, border-color .15s ease;
    user-select: none;
    white-space: nowrap;
    font-family: inherit;
  }

  button:hover{
    background: rgba(255,255,255,.14);
    border-color: rgba(255,255,255,.14);
  }

  button:active{
    transform: scale(.98);
  }

  .below{
    margin-top: 14px;
    text-align: center;
  }

  .back-link{
    display: inline-block;
    font-size: .9rem;
    color: var(--muted);
    text-decoration: none;
    max-width: 100%;
    word-break: break-word;
    overflow-wrap: anywhere;
    white-space: normal;
  }

  .clicks{
    display: inline-block;
    font-size: .9rem;
  }
  
  .back-link:hover{
    color: var(--text);
  }

  /* mobile: stack */
  @media (max-width: 560px){
    form{ flex-direction: column; }
    button{ width: 100%; }
  }
</style>
</head>

<body>
  <main class="card">
    <form onsubmit="return false;">
      <input
        id="shortUrl"
        type="text"
        value="{{ url_root }}{{ short_code }}"
        readonly
        aria-label="Short URL"
      />
      <button type="button" id="copyBtn" onclick="copyUrl()">Copy</button>
    </form>
    <br>
    <a href="{{ url }}" class="back-link" target="_blank" rel="noreferrer">{{ url }}</a>
    <div class="below">
      <a href="/" class="back-link">&larr; Shorten another</a> &middot; <span class="clicks">{{ clicks }} clicks</span>
    </div>
  </main>

<script>
function copyUrl(){
  const input = document.getElementById('shortUrl');
  const btn = document.getElementById('copyBtn');

  input.focus();
  input.select();

  const text = input.value;
  if (navigator.clipboard && window.isSecureContext) {
    navigator.clipboard.writeText(text).then(() => {
      btn.textContent = 'Copied';
      setTimeout(() => btn.textContent = 'Copy', 1800);
    }).catch(() => fallbackCopy(text, btn));
  } else {
    fallbackCopy(text, btn);
  }
}

function fallbackCopy(text, btn){
  try{
    const ta = document.createElement('textarea');
    ta.value = text;
    ta.setAttribute('readonly', '');
    ta.style.position = 'absolute';
    ta.style.left = '-9999px';
    document.body.appendChild(ta);
    ta.select();
    document.execCommand('copy');
    document.body.removeChild(ta);

    btn.textContent = 'Copied';
    setTimeout(() => btn.textContent = 'Copy', 1800);
  } catch(e){
    btn.textContent = 'Copy failed';
    setTimeout(() => btn.textContent = 'Copy', 1800);
  }
}
</script>
</body>
</html>
