(() => {
  const TARGET_TIME = '2026-08-01T20:00:00+08:00'

  const targetTimestamp = new Date(TARGET_TIME).getTime()

  if (Number.isNaN(targetTimestamp)) {
    console.error('[MCLIVE countdown] Invalid TARGET_TIME:', TARGET_TIME)
    return
  }

  const style = document.createElement('style')
  style.textContent = `
    #mclive-countdown-overlay {
      --countdown-night: #041117;
      --countdown-ice: #d7f1e8;
      --countdown-mist: #8fafb2;
      --countdown-gold: #c6a15b;
      --countdown-line: rgba(166, 209, 203, 0.22);
      position: fixed;
      z-index: 2147483646;
      inset: 0;
      display: grid;
      place-items: center;
      min-height: 100svh;
      padding: 84px 24px 42px;
      overflow: hidden;
      color: var(--countdown-ice);
      background: rgba(4, 17, 23, 0.3);
      -webkit-backdrop-filter: blur(20px) saturate(0.72);
      backdrop-filter: blur(20px) saturate(0.72);
      font-family: 'Noto Sans SC', system-ui, sans-serif;
      touch-action: pan-y;
    }

    #mclive-countdown-overlay::before {
      content: '';
      position: absolute;
      inset: 0;
      opacity: 0.32;
      background-image:
        linear-gradient(var(--countdown-line) 1px, transparent 1px),
        linear-gradient(90deg, var(--countdown-line) 1px, transparent 1px);
      background-size: 54px 54px;
      mask-image: radial-gradient(circle at center, black 10%, transparent 72%);
      pointer-events: none;
    }

    #mclive-countdown-overlay::after {
      content: '';
      position: absolute;
      width: min(72vw, 760px);
      aspect-ratio: 1;
      border: 1px solid rgba(215, 241, 232, 0.13);
      border-radius: 50%;
      box-shadow:
        0 0 0 68px rgba(79, 143, 120, 0.035),
        0 0 0 136px rgba(79, 143, 120, 0.018);
      pointer-events: none;
    }

    .mclive-countdown-terminal {
      position: relative;
      z-index: 1;
      width: min(960px, 100%);
      padding: clamp(28px, 5vw, 64px);
      border: 1px solid var(--countdown-line);
      background: rgba(4, 17, 23, 0.78);
      box-shadow: 0 36px 120px rgba(0, 0, 0, 0.45);
      text-align: center;
    }

    .mclive-countdown-terminal::before,
    .mclive-countdown-terminal::after {
      content: '';
      position: absolute;
      width: 14px;
      height: 14px;
      border-color: var(--countdown-gold);
    }

    .mclive-countdown-terminal::before {
      top: -5px;
      left: -5px;
      border-top: 1px solid;
      border-left: 1px solid;
    }

    .mclive-countdown-terminal::after {
      right: -5px;
      bottom: -5px;
      border-right: 1px solid;
      border-bottom: 1px solid;
    }

    .mclive-countdown-kicker,
    .mclive-countdown-target,
    .mclive-countdown-unit,
    .mclive-countdown-status,
    .mclive-countdown-toggle {
      font-family: 'IBM Plex Mono', ui-monospace, monospace;
      text-transform: uppercase;
      letter-spacing: 0.14em;
    }

    .mclive-countdown-kicker {
      margin: 0 0 18px;
      color: var(--countdown-gold);
      font-size: 10px;
    }

    .mclive-countdown-title {
      margin: 0;
      font-family: 'ZCOOL QingKe HuangYou', 'Noto Sans SC', sans-serif;
      font-size: clamp(40px, 7vw, 82px);
      font-weight: 400;
      line-height: 1;
      letter-spacing: -0.02em;
    }

    .mclive-countdown-title span {
      color: var(--countdown-gold);
    }

    .mclive-countdown-target {
      margin: 18px 0 0;
      color: var(--countdown-mist);
      font-size: 9px;
    }

    .mclive-countdown-grid {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      margin-top: clamp(30px, 5vw, 54px);
      border-top: 1px solid var(--countdown-line);
      border-bottom: 1px solid var(--countdown-line);
    }

    .mclive-countdown-cell {
      min-width: 0;
      padding: clamp(18px, 4vw, 34px) 8px;
      border-right: 1px solid var(--countdown-line);
    }

    .mclive-countdown-cell:last-child {
      border-right: 0;
    }

    .mclive-countdown-value {
      display: block;
      color: var(--countdown-ice);
      font-family: 'IBM Plex Mono', ui-monospace, monospace;
      font-size: clamp(34px, 7vw, 72px);
      font-weight: 500;
      font-variant-numeric: tabular-nums;
      line-height: 1;
      text-shadow: 0 0 34px rgba(215, 241, 232, 0.13);
    }

    .mclive-countdown-unit {
      display: block;
      margin-top: 12px;
      color: var(--countdown-mist);
      font-size: 8px;
    }

    .mclive-countdown-status {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 10px;
      margin: 24px 0 0;
      color: var(--countdown-mist);
      font-size: 8px;
    }

    .mclive-countdown-status i {
      width: 7px;
      height: 7px;
      background: #e05a47;
      box-shadow: 0 0 12px #e05a47;
      animation: mclive-countdown-pulse 1.8s infinite;
    }

    .mclive-countdown-collapse {
      min-height: 42px;
      padding: 0 17px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 10px;
      border: 1px solid var(--countdown-line);
      color: var(--countdown-ice);
      background: rgba(4, 17, 23, 0.82);
      cursor: pointer;
      font-size: 8px;
      margin-top: 22px;
      border-color: rgba(166, 209, 203, 0.14);
      color: var(--countdown-mist);
      background: transparent;
    }

    .mclive-countdown-restore {
      display: none;
      border-bottom: 0;
    }

    .mclive-countdown-collapse:hover {
      border-color: var(--countdown-gold);
    }

    .mclive-countdown-collapse:focus-visible {
      outline: 2px solid var(--countdown-gold);
      outline-offset: 4px;
    }

    .mclive-countdown-collapse svg {
      width: 15px;
      height: 15px;
      stroke: currentColor;
      fill: none;
      stroke-width: 1.8;
    }

    #mclive-countdown-overlay.is-collapsed {
      display: none;
    }

    .mclive-countdown-restore.is-visible {
      display: inline-flex;
    }

    .mclive-countdown-restore .mclive-archive-mark {
      width: 9px;
      height: 9px;
      flex: 0 0 auto;
      border: 1px solid var(--mclive-control-gold, #c6a15b);
      transform: rotate(45deg);
    }

    .mclive-countdown-restore .mclive-control-value {
      color: var(--mclive-control-gold, #c6a15b);
    }

    @keyframes mclive-countdown-pulse {
      50% { opacity: 0.4; box-shadow: 0 0 3px #e05a47; }
    }

    @media (max-width: 620px) {
      #mclive-countdown-overlay { padding: 76px 16px 28px; }
      .mclive-countdown-terminal { padding: 32px 14px; }
      .mclive-countdown-cell { padding-left: 3px; padding-right: 3px; }
      .mclive-countdown-unit { letter-spacing: 0.08em; }
      .mclive-countdown-collapse { margin-top: 18px; }
    }

    @media (prefers-reduced-motion: reduce) {
      .mclive-countdown-status i { animation: none; }
    }
  `

  const overlay = document.createElement('div')
  overlay.id = 'mclive-countdown-overlay'
  overlay.setAttribute('role', 'dialog')
  overlay.setAttribute('aria-label', 'MCLIVE 世界实例开放倒计时')
  overlay.innerHTML = `
    <section class="mclive-countdown-terminal">
      <p class="mclive-countdown-kicker">WORLD INSTANCE 001 / ACCESS LOCKED</p>
      <h1 class="mclive-countdown-title">世界档案将在<br><span>倒计时结束后开启</span></h1>
      <p class="mclive-countdown-target"></p>
      <div class="mclive-countdown-grid" aria-live="off">
        <div class="mclive-countdown-cell"><strong class="mclive-countdown-value" data-countdown="days">00</strong><span class="mclive-countdown-unit">DAYS / 天</span></div>
        <div class="mclive-countdown-cell"><strong class="mclive-countdown-value" data-countdown="hours">00</strong><span class="mclive-countdown-unit">HOURS / 时</span></div>
        <div class="mclive-countdown-cell"><strong class="mclive-countdown-value" data-countdown="minutes">00</strong><span class="mclive-countdown-unit">MINUTES / 分</span></div>
        <div class="mclive-countdown-cell"><strong class="mclive-countdown-value" data-countdown="seconds">00</strong><span class="mclive-countdown-unit">SECONDS / 秒</span></div>
      </div>
      <p class="mclive-countdown-status"><i></i> SERVER PREPARATION IN PROGRESS</p>
      <button class="mclive-countdown-toggle mclive-countdown-collapse" type="button" aria-label="收起倒计时覆盖层">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 3H3v4M17 3h4v4M7 21H3v-4M17 21h4v-4"/></svg>
        <span>MINIMIZE ARCHIVE</span>
      </button>
    </section>
  `

  document.head.appendChild(style)
  document.body.appendChild(overlay)

  let controls = document.getElementById('mclive-floating-controls')
  if (!controls) {
    controls = document.createElement('div')
    controls.id = 'mclive-floating-controls'
    document.body.appendChild(controls)
  }

  const restoreButton = document.createElement('button')
  restoreButton.className = 'mclive-floating-control mclive-countdown-toggle mclive-countdown-restore'
  restoreButton.type = 'button'
  restoreButton.setAttribute('aria-label', '展开倒计时覆盖层')
  restoreButton.innerHTML = `
    <span class="mclive-archive-mark" aria-hidden="true"></span>
    <span class="mclive-control-name">ARCHIVE</span>
    <span class="mclive-control-value mclive-countdown-compact">--D --H</span>
  `
  controls.appendChild(restoreButton)

  const values = {
    days: overlay.querySelector('[data-countdown="days"]'),
    hours: overlay.querySelector('[data-countdown="hours"]'),
    minutes: overlay.querySelector('[data-countdown="minutes"]'),
    seconds: overlay.querySelector('[data-countdown="seconds"]'),
  }
  const targetLabel = overlay.querySelector('.mclive-countdown-target')
  const collapseButton = overlay.querySelector('.mclive-countdown-collapse')
  const compactCountdown = restoreButton.querySelector('.mclive-countdown-compact')

  collapseButton.addEventListener('click', () => {
    overlay.classList.add('is-collapsed')
    overlay.setAttribute('aria-hidden', 'true')
    restoreButton.classList.add('is-visible')
    restoreButton.focus()
  })

  restoreButton.addEventListener('click', () => {
    overlay.classList.remove('is-collapsed')
    overlay.removeAttribute('aria-hidden')
    restoreButton.classList.remove('is-visible')
    collapseButton.focus()
  })

  const targetDate = new Date(targetTimestamp)
  targetLabel.textContent = `TARGET / ${targetDate.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false,
  })}`

  function updateCountdown() {
    const remaining = Math.max(0, targetTimestamp - Date.now())
    const totalSeconds = Math.floor(remaining / 1000)
    const days = Math.floor(totalSeconds / 86400)
    const hours = Math.floor((totalSeconds % 86400) / 3600)
    const minutes = Math.floor((totalSeconds % 3600) / 60)
    const seconds = totalSeconds % 60

    values.days.textContent = String(days).padStart(2, '0')
    values.hours.textContent = String(hours).padStart(2, '0')
    values.minutes.textContent = String(minutes).padStart(2, '0')
    values.seconds.textContent = String(seconds).padStart(2, '0')
    compactCountdown.textContent = days > 0
      ? `${days}D ${String(hours).padStart(2, '0')}H`
      : `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
  }

  updateCountdown()
  window.setInterval(updateCountdown, 250)
})()
