<script lang="ts">
  let {
    blocked = false,
    label = 'Checking Access',
    title = 'Preparing your workspace.',
    detail = 'Applying your access rules before rendering this page.',
    children
  } = $props();
</script>

{#if blocked}
  <div class="auth-gate-screen">
    <section class="auth-gate-card" role="status" aria-live="polite">
      <div class="auth-gate-loader" aria-hidden="true">
        <span class="ring"></span>
        <span class="ring ring-2"></span>
        <span class="dot"></span>
      </div>
      <p class="auth-gate-label">{label}</p>
      <h2>{title}</h2>
      <p class="auth-gate-detail">{detail}</p>
    </section>
  </div>
{:else}
  {@render children()}
{/if}

<style>
  .auth-gate-screen {
    display: grid;
    place-items: center;
    width: 100%;
    min-height: 100%;
    height: 100%;
    padding: 2rem;
  }

  .auth-gate-card {
    display: grid;
    justify-items: center;
    gap: 0.5rem;
    width: min(26rem, 100%);
    padding: 2.1rem 2rem 2.25rem;
    border: 1px solid var(--line);
    border-radius: 1.25rem;
    background: var(--panel);
    text-align: center;
    box-shadow: 0 18px 50px -24px rgba(0, 0, 0, 0.4);
    animation: auth-gate-in 380ms cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  /* ── Animated loader ───────────────────────────────────────── */
  .auth-gate-loader {
    position: relative;
    width: 3.1rem;
    height: 3.1rem;
    margin-bottom: 0.65rem;
  }

  .auth-gate-loader .ring {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2.5px solid transparent;
    border-top-color: var(--color-brand, #2f9e6f);
    border-right-color: color-mix(in srgb, var(--color-brand, #2f9e6f) 45%, transparent);
    animation: auth-gate-spin 0.85s linear infinite;
  }

  .auth-gate-loader .ring-2 {
    inset: 0.42rem;
    border-top-color: color-mix(in srgb, var(--color-brand, #2f9e6f) 60%, transparent);
    border-right-color: transparent;
    animation-duration: 1.25s;
    animation-direction: reverse;
  }

  .auth-gate-loader .dot {
    position: absolute;
    inset: 0;
    margin: auto;
    width: 0.55rem;
    height: 0.55rem;
    border-radius: 50%;
    background: var(--color-brand, #2f9e6f);
    animation: auth-gate-pulse 1.1s ease-in-out infinite;
  }

  .auth-gate-label {
    margin: 0;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
  }

  .auth-gate-card h2 {
    margin: 0;
    font-size: 1.2rem;
  }

  .auth-gate-detail {
    margin: 0;
    max-width: 22rem;
    color: var(--muted);
    font-size: 0.9rem;
    line-height: 1.55;
  }

  @keyframes auth-gate-in {
    from {
      opacity: 0;
      transform: translateY(10px) scale(0.97);
    }
    to {
      opacity: 1;
      transform: translateY(0) scale(1);
    }
  }

  @keyframes auth-gate-spin {
    to {
      transform: rotate(360deg);
    }
  }

  @keyframes auth-gate-pulse {
    0%,
    100% {
      transform: scale(0.7);
      opacity: 0.55;
    }
    50% {
      transform: scale(1);
      opacity: 1;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .auth-gate-card {
      animation: none;
    }

    .auth-gate-loader .ring,
    .auth-gate-loader .ring-2 {
      animation-duration: 1.6s;
    }

    .auth-gate-loader .dot {
      animation: none;
    }
  }
</style>
