<PhoenixKitWeb.Components.AuthPageWrapper.auth_page_wrapper
  flash={@flash}
  phoenix_kit_current_scope={assigns[:phoenix_kit_current_scope]}
  page_title={@page_title}
  current_path={assigns[:url_path] || "/users/magic-link"}
  dev_mailbox_message={gettext("magic link emails")}
>
  <h1 class="text-2xl font-bold text-center mb-2">{gettext("Magic Link Login")}</h1>
  <p class="text-center text-base-content/60 text-sm mb-6">
    {gettext("Enter your email to receive a secure login link")}
  </p>

  <.form
    for={@form}
    id="magic_link_form"
    phx-submit="send_magic_link"
    phx-change="validate"
  >
    <%!-- `min-w-0` overrides <fieldset>'s default `min-width: min-content`
         so a long-content child doesn't push the form past the card. --%>
    <fieldset class="fieldset min-w-0">
      <legend class="fieldset-legend sr-only">{gettext("Magic Link Authentication")}</legend>

      <div :if={@error} class="alert alert-error text-sm mb-4">
        <PhoenixKitWeb.Components.Core.Icons.icon_error_circle class="stroke-current shrink-0 h-6 w-6" />
        <span>{@error}</span>
      </div>

      <label class="label" for="magic_link_email">
        <span class="label-text flex items-center">
          <PhoenixKitWeb.Components.Core.Icons.icon_email class="w-4 h-4 mr-2" /> {gettext(
            "Email"
          )}
        </span>
      </label>
      <.input
        field={@form[:email]}
        type="email"
        placeholder="you@example.com"
        autocomplete="email"
        class="transition-colors focus:input-primary"
        required
      />

      <%!-- `@loading` already swaps the button content; redundant
           `phx-disable-with` was removed because the double swap
           broke the DOM merge. `min-w-0 max-w-full` belt-and-
           suspenders for the fieldset overflow fix. --%>
      <button
        type="submit"
        class="btn btn-primary w-full max-w-full min-w-0 mt-4 transition-all hover:scale-[1.02] active:scale-[0.98]"
        disabled={@loading || @sent}
      >
        <%= if @loading do %>
          <span class="loading loading-spinner loading-sm mr-2"></span>
          {gettext("Sending magic link...")}
        <% else %>
          <PhoenixKitWeb.Components.Core.Icons.icon_email class="w-5 h-5 mr-2" /> {gettext(
            "Send Magic Link"
          )}
          <span aria-hidden="true">→</span>
        <% end %>
      </button>
    </fieldset>
  </.form>

  <div :if={@sent} class="alert alert-success mt-4">
    <PhoenixKitWeb.Components.Core.Icons.icon_check_circle_filled class="stroke-current shrink-0 h-6 w-6" />
    <div>
      <h3 class="font-bold">{gettext("Magic link sent!")}</h3>
      <div class="text-xs">
        {gettext("Check your email for a secure login link. The link will expire in 15 minutes.")}
      </div>
    </div>
  </div>

  <div class="text-center mt-4 text-sm">
    <span>{gettext("Prefer a password?")}</span>
    <.link
      navigate={Routes.path("/users/log-in")}
      class="font-semibold text-primary hover:underline"
    >
      {gettext("Sign in with password")}
    </.link>
  </div>

  <div class="text-center mt-2 text-sm">
    <span>{gettext("Don't have an account?")}</span>
    <.link
      navigate={Routes.path("/users/register")}
      class="font-semibold text-primary hover:underline"
    >
      {gettext("Create an account")}
    </.link>
  </div>
</PhoenixKitWeb.Components.AuthPageWrapper.auth_page_wrapper>
