@if (showDialog()) {
  <div class="login-overlay" (click)="close()">
    <div class="login-dialog" (click)="$event.stopPropagation()">
      <button class="close-btn" (click)="close()" [attr.aria-label]="'cart.close' | translate">
        <app-icon name="x" [size]="18" />
      </button>

      <div class="login-icon">
        <app-icon name="lock" [size]="40" />
      </div>

      <h2>{{ (mode === 'admin' ? 'auth.adminLoginRequired' : 'auth.loginRequired') | translate }}</h2>
      <p class="login-desc">{{ (mode === 'admin' ? 'auth.adminLoginDescription' : 'auth.loginDescription') | translate }}</p>

      @if (status() === 'checking') {
        <div class="login-status checking">
          <div class="spinner"></div>
          <span>{{ 'auth.checking' | translate }}</span>
        </div>
      } @else {
        <button class="telegram-btn" (click)="openTelegramLogin()">
          <svg class="tg-icon" width="22" height="22" viewBox="0 0 24 24" fill="currentColor">
            <path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z"/>
          </svg>
          {{ (mode === 'admin' ? 'auth.adminLoginWithTelegram' : 'auth.loginWithTelegram') | translate }}
        </button>

        <!-- @if (loginUrl()) {
          <a class="bot-link" [href]="loginUrl()" target="_blank" rel="noopener noreferrer">
            {{ loginUrl() }}
          </a>
        } -->

        <div class="qr-section">
          <p class="qr-hint">{{ 'auth.orScanQr' | translate }}</p>

          @switch (qrStatus()) {
            @case ('loading') {
              <div class="qr-container qr-loading">
                <div class="spinner"></div>
              </div>
            }
            @case ('ready') {
              <div class="qr-container">
                <img [src]="'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=' + encodedQrUrl()"
                     [attr.alt]="'common.qrCode' | translate"
                     width="180"
                     height="180"
                     loading="eager" />
              </div>
            }
            @case ('expired') {
              <div class="qr-container qr-expired" (click)="refreshQr()">
                <app-icon name="refresh" [size]="28" />
                <span>{{ 'auth.qrExpired' | translate }}</span>
              </div>
            }
            @case ('error') {
              <div class="qr-container qr-error" (click)="refreshQr()">
                <app-icon name="refresh" [size]="28" />
                <span>{{ 'auth.qrError' | translate }}</span>
              </div>
            }
          }
        </div>

        @if (mode === 'admin') {
          @if (!credentialsVisible()) {
            <button class="credentials-toggle" type="button" (click)="toggleCredentials()">
              {{ 'auth.loginWithCredentials' | translate }}
            </button>
          } @else {
            <form class="credentials-form" (submit)="submitCredentials($event)">
              <label>
                {{ 'auth.credentialsLoginLabel' | translate }}
                <input type="text" autocomplete="username" [value]="credentialsLogin()"
                       (input)="credentialsLogin.set($any($event.target).value)" />
              </label>
              <label>
                {{ 'auth.credentialsPasswordLabel' | translate }}
                <input type="password" autocomplete="current-password" [value]="credentialsPassword()"
                       (input)="credentialsPassword.set($any($event.target).value)" />
              </label>
              @if (credentialsError()) {
                <p class="credentials-error" role="alert">{{ 'auth.credentialsGenericError' | translate }}</p>
              }
              <div class="credentials-actions">
                <button type="submit" class="credentials-submit" [disabled]="credentialsBusy()">
                  {{ 'auth.credentialsSubmit' | translate }}
                </button>
                <button type="button" class="credentials-back" (click)="toggleCredentials()">
                  {{ 'auth.credentialsBack' | translate }}
                </button>
              </div>
            </form>
          }
        }

        <p class="login-note">{{ (mode === 'admin' ? 'auth.adminLoginNote' : 'auth.loginNote') | translate }}</p>
      }
    </div>
  </div>
}
