/**
 * Auth Modal - Client Login/Register Modal
 * v1.3.0
 *
 * Combined login and registration modal with tab switching,
 * OAuth support, AJAX form submission, and dark mode.
 *
 * Changelog:
 * - v1.3.0: Use --z-auth-modal CSS variable instead of hardcoded z-index: 9999
 * - v1.2.0: Added password requirements checklist styles (.pwd-req)
 * - v1.1.0: Compact redesign with orange gradient header bar,
 *   tabs + close button integrated in header, reduced spacing/fonts,
 *   removed subtitle paragraphs, fixed close button overlap.
 * - v1.0.0: Initial implementation with login/register tabs,
 *   OAuth buttons, form validation, loading states, GDPR checkbox,
 *   dark mode support, and responsive design.
 */

/* ===== Overlay ===== */
.auth-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-auth-modal, 9000);
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    opacity: 0;
    transition: opacity var(--transition-base);
}

.auth-modal-overlay.is-open {
    display: flex;
    opacity: 1;
}

/* ===== Modal Card ===== */
.auth-modal {
    position: relative;
    width: 100%;
    max-width: 460px;
    max-height: 90vh;
    overflow-y: auto;
    background: var(--bg-primary);
    border-radius: var(--rounded-xl);
    box-shadow: var(--shadow-2xl);
    padding: 0;
    transform: scale(0.95);
    transition: transform var(--transition-base), max-width var(--transition-base);
    animation: authModalScaleIn 0.25s ease-out forwards;
}

.auth-modal.auth-modal--register {
    max-width: 520px;
}

@keyframes authModalScaleIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Scrollbar styling for modal content */
.auth-modal::-webkit-scrollbar {
    width: 6px;
}

.auth-modal::-webkit-scrollbar-track {
    background: transparent;
}

.auth-modal::-webkit-scrollbar-thumb {
    background: var(--color-gray-300);
    border-radius: var(--rounded-full);
}

.auth-modal::-webkit-scrollbar-thumb:hover {
    background: var(--color-gray-400);
}

/* ===== Header Bar (orange gradient with tabs + close) ===== */
.auth-modal-header-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    background: linear-gradient(90deg, var(--color-primary) 0%, rgba(255, 112, 82, 0.1) 100%);
    border-radius: var(--rounded-xl) var(--rounded-xl) 0 0;
}

/* ===== Close Button ===== */
.auth-modal-close {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: var(--rounded-full);
    cursor: pointer;
    color: var(--color-white);
    transition: all var(--transition-fast);
    flex-shrink: 0;
}

.auth-modal-close:hover {
    background: rgba(255, 255, 255, 0.35);
    color: var(--color-white);
}

.auth-modal-close:active {
    transform: scale(0.92);
}

.auth-modal-close svg {
    width: 14px;
    height: 14px;
}

/* ===== Tabs ===== */
.auth-modal-tabs {
    display: flex;
    gap: 2px;
}

.auth-modal-tab {
    padding: var(--space-1) var(--space-3);
    text-align: center;
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: var(--font-medium);
    color: rgba(255, 255, 255, 0.7);
    background: transparent;
    border: none;
    border-radius: var(--rounded-lg);
    cursor: pointer;
    transition: all var(--transition-fast);
    user-select: none;
    white-space: nowrap;
}

.auth-modal-tab:hover:not(.active) {
    color: var(--color-white);
    background: rgba(255, 255, 255, 0.1);
}

.auth-modal-tab.active {
    background: rgba(255, 255, 255, 0.25);
    color: var(--color-white);
    font-weight: var(--font-semibold);
}

/* ===== View Containers ===== */
.auth-modal-view {
    display: none;
    padding: var(--space-4);
}

.auth-modal-view.active {
    display: block;
    animation: authViewFadeIn 0.2s ease-out;
}

@keyframes authViewFadeIn {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===== Header ===== */
.auth-modal-header {
    text-align: center;
    margin-bottom: var(--space-3);
}

.auth-modal-title {
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-weight: var(--font-bold);
    color: var(--text-primary);
    margin: 0;
    line-height: var(--leading-tight);
}

.auth-modal-subtitle {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin: var(--space-1) 0 0 0;
    line-height: var(--leading-relaxed);
}

/* ===== OAuth Section ===== */
.auth-modal-oauth {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-bottom: var(--space-3);
}

.auth-modal-btn-oauth {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-gray-300);
    border-radius: var(--rounded-lg);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: var(--font-medium);
    text-decoration: none;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.auth-modal-btn-oauth:hover {
    background: var(--color-gray-50);
    border-color: var(--color-gray-400);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

.auth-modal-btn-oauth:active {
    transform: translateY(0);
}

.auth-modal-btn-oauth svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.auth-modal-btn-oauth--google:hover {
    border-color: #4285F4;
    background: rgba(66, 133, 244, 0.04);
}

.auth-modal-btn-oauth--facebook:hover {
    border-color: #1877F2;
    background: rgba(24, 119, 242, 0.04);
}

/* ===== Divider ===== */
.auth-modal-divider {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    margin: var(--space-3) 0;
    color: var(--text-muted);
    font-size: var(--text-xs);
}

.auth-modal-divider::before,
.auth-modal-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--color-gray-300);
}

/* ===== Form ===== */
.auth-modal-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.auth-modal-form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
}

/* Reuse existing form element patterns */
.auth-modal-form .form-group {
    margin-bottom: 0;
}

.auth-modal-form .form-label {
    display: block;
    font-family: var(--font-body);
    font-size: 12px;
    font-weight: var(--font-medium);
    color: var(--text-primary);
    margin-bottom: var(--space-1);
    letter-spacing: 0.01em;
}

.auth-modal-form .form-input {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-gray-300);
    border-radius: var(--rounded-lg);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    background: var(--color-gray-50);
    color: var(--text-primary);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast), background-color var(--transition-fast);
}

.auth-modal-form .form-input::placeholder {
    color: var(--text-muted);
}

.auth-modal-form .form-input:hover {
    border-color: var(--color-gray-400);
    background: var(--bg-primary);
}

.auth-modal-form .form-input:focus {
    outline: none;
    border-color: var(--color-primary);
    background: var(--bg-primary);
    box-shadow: 0 0 0 3px rgba(var(--color-primary-rgb), 0.15);
}

/* ===== Submit Button ===== */
.auth-modal-btn-submit {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-2) var(--space-4);
    background: var(--color-primary);
    color: var(--color-white);
    border: 2px solid var(--color-primary);
    border-radius: var(--rounded-lg);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: var(--font-semibold);
    cursor: pointer;
    transition: all var(--transition-base);
    margin-top: var(--space-1);
}

.auth-modal-btn-submit:hover:not(:disabled) {
    background: var(--color-primary-dark);
    border-color: var(--color-primary-dark);
    transform: translateY(-1px);
    box-shadow: 0 6px 20px -4px rgba(var(--color-primary-rgb), 0.35);
}

.auth-modal-btn-submit:active:not(:disabled) {
    transform: translateY(0);
}

.auth-modal-btn-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ===== Footer ===== */
.auth-modal-footer {
    text-align: center;
    margin-top: var(--space-4);
    padding-top: var(--space-3);
    border-top: 1px solid var(--color-gray-200);
    color: var(--text-secondary);
    font-size: var(--text-sm);
}

.auth-modal-footer a {
    color: var(--color-primary);
    font-weight: var(--font-semibold);
    text-decoration: none;
    transition: color var(--transition-fast);
}

.auth-modal-footer a:hover {
    color: var(--color-primary-dark);
    text-decoration: underline;
    text-underline-offset: 3px;
}

/* ===== Extra / Resend Verification ===== */
.auth-modal-extra {
    margin-top: var(--space-3);
    font-size: var(--text-xs);
}

.auth-modal-extra summary {
    cursor: pointer;
    color: var(--text-secondary);
    transition: color var(--transition-fast);
    user-select: none;
}

.auth-modal-extra summary:hover {
    color: var(--color-primary);
}

.auth-modal-extra .auth-resend-form {
    display: flex;
    gap: var(--space-2);
    margin-top: var(--space-2);
}

.auth-modal-extra .auth-resend-form .form-input {
    flex: 1;
}

/* ===== Checkbox (GDPR Consent) ===== */
.auth-modal-checkbox {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    cursor: pointer;
    font-size: var(--text-xs);
    color: var(--text-secondary);
    line-height: var(--leading-relaxed);
}

.auth-modal-checkbox input[type="checkbox"] {
    width: 16px;
    height: 16px;
    margin-top: 2px;
    accent-color: var(--color-primary);
    cursor: pointer;
    flex-shrink: 0;
}

.auth-modal-checkbox span {
    user-select: none;
}

.auth-modal-checkbox a {
    color: var(--color-primary);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.auth-modal-checkbox a:hover {
    color: var(--color-primary-dark);
}

/* ===== Password Hint ===== */
.auth-modal-password-hint {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: var(--space-1);
}

.auth-modal-password-hint svg {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
}

/* ===== Password Requirements Checklist ===== */
.auth-modal-password-requirements {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px var(--space-3);
    margin-top: calc(-1 * var(--space-1));
}

.pwd-req {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    color: var(--text-muted);
    transition: color 0.2s ease;
}

.pwd-req-icon {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
    position: relative;
}

/* Default state: small circle bullet */
.pwd-req-icon::before {
    content: '';
    display: block;
    width: 6px;
    height: 6px;
    margin: 3px;
    border-radius: var(--rounded-full);
    background: var(--color-gray-300);
    transition: all 0.2s ease;
}

/* Met state: green check */
.pwd-req.met {
    color: var(--color-success);
}

.pwd-req.met .pwd-req-icon::before {
    content: '';
    width: 12px;
    height: 12px;
    margin: 0;
    border-radius: 0;
    background: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%234caf50' stroke-width='3'%3E%3Cpolyline points='20 6 9 17 4 12'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-size: contain;
}

[data-theme="dark"] .pwd-req-icon::before {
    background: rgba(255, 255, 255, 0.2);
}

[data-theme="dark"] .pwd-req.met .pwd-req-icon::before {
    background: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2366bb6a' stroke-width='3'%3E%3Cpolyline points='20 6 9 17 4 12'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-size: contain;
}

/* ===== Error Message ===== */
.auth-modal-error {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: rgba(244, 67, 54, 0.08);
    border: 1px solid rgba(244, 67, 54, 0.2);
    border-radius: var(--rounded-lg);
    color: var(--color-error);
    font-size: var(--text-xs);
    line-height: var(--leading-relaxed);
    animation: authErrorAppear 0.25s ease-out;
}

.auth-modal-error:empty {
    display: none;
}

.auth-modal-error svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

@keyframes authErrorAppear {
    from {
        opacity: 0;
        transform: translateY(-4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===== Success Message ===== */
.auth-modal-success {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: rgba(76, 175, 80, 0.08);
    border: 1px solid rgba(76, 175, 80, 0.2);
    border-radius: var(--rounded-lg);
    color: var(--color-success);
    font-size: var(--text-xs);
    line-height: var(--leading-relaxed);
    animation: authErrorAppear 0.25s ease-out;
}

.auth-modal-success:empty {
    display: none;
}

.auth-modal-success svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

/* ===== Loading Overlay ===== */
.auth-modal-loading {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    border-radius: var(--rounded-xl);
    z-index: 2;
}

.auth-modal-loading.active {
    display: flex;
}

.auth-modal-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--color-gray-200);
    border-top-color: var(--color-primary);
    border-radius: var(--rounded-full);
    animation: authModalSpin 0.7s linear infinite;
}

@keyframes authModalSpin {
    to {
        transform: rotate(360deg);
    }
}

/* ===== Field-level Validation States ===== */
.auth-modal-form .form-input.is-error {
    border-color: var(--color-error);
    background: rgba(244, 67, 54, 0.04);
}

.auth-modal-form .form-input.is-error:focus {
    box-shadow: 0 0 0 3px rgba(244, 67, 54, 0.12);
}

.auth-modal-form .form-input.is-valid {
    border-color: var(--color-success);
    background: rgba(76, 175, 80, 0.04);
}

.auth-modal-form .form-input.is-valid:focus {
    box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.12);
}

.auth-modal-form .field-error {
    display: block;
    margin-top: var(--space-1);
    font-size: var(--text-xs);
    color: var(--color-error);
    animation: authErrorAppear 0.2s ease-out;
}

.auth-modal-form .field-error:empty {
    display: none;
}

/* ===== Forgot Password Link ===== */
.auth-modal-form .form-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.auth-modal-forgot-link {
    font-size: var(--text-xs);
    color: var(--color-primary);
    text-decoration: none;
    font-weight: var(--font-medium);
    transition: color var(--transition-fast);
}

.auth-modal-forgot-link:hover {
    color: var(--color-primary-dark);
    text-decoration: underline;
    text-underline-offset: 3px;
}

/* ===== Dark Mode ===== */
[data-theme="dark"] .auth-modal-overlay {
    background: rgba(0, 0, 0, 0.7);
}

[data-theme="dark"] .auth-modal {
    background: var(--bg-secondary);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5),
                0 0 0 1px rgba(255, 255, 255, 0.05);
}

[data-theme="dark"] .auth-modal::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
}

[data-theme="dark"] .auth-modal::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.25);
}

[data-theme="dark"] .auth-modal-header-bar {
    background: linear-gradient(90deg, var(--color-primary) 0%, rgba(255, 112, 82, 0.2) 100%);
}

[data-theme="dark"] .auth-modal-close {
    background: rgba(255, 255, 255, 0.15);
}

[data-theme="dark"] .auth-modal-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

[data-theme="dark"] .auth-modal-tab {
    color: rgba(255, 255, 255, 0.6);
}

[data-theme="dark"] .auth-modal-tab:hover:not(.active) {
    color: var(--color-white);
    background: rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] .auth-modal-tab.active {
    background: rgba(255, 255, 255, 0.2);
    color: var(--color-white);
}

[data-theme="dark"] .auth-modal-btn-oauth {
    border-color: rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.04);
}

[data-theme="dark"] .auth-modal-btn-oauth:hover {
    border-color: rgba(255, 255, 255, 0.25);
    background: rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] .auth-modal-btn-oauth--google:hover {
    border-color: rgba(66, 133, 244, 0.5);
    background: rgba(66, 133, 244, 0.08);
}

[data-theme="dark"] .auth-modal-btn-oauth--facebook:hover {
    border-color: rgba(24, 119, 242, 0.5);
    background: rgba(24, 119, 242, 0.08);
}

[data-theme="dark"] .auth-modal-divider::before,
[data-theme="dark"] .auth-modal-divider::after {
    background: rgba(255, 255, 255, 0.12);
}

[data-theme="dark"] .auth-modal-form .form-input {
    border-color: rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-primary);
}

[data-theme="dark"] .auth-modal-form .form-input:hover {
    border-color: rgba(255, 255, 255, 0.25);
    background: rgba(255, 255, 255, 0.06);
}

[data-theme="dark"] .auth-modal-form .form-input:focus {
    border-color: var(--color-primary);
    background: rgba(255, 255, 255, 0.06);
    box-shadow: 0 0 0 3px rgba(var(--color-primary-rgb), 0.2);
}

[data-theme="dark"] .auth-modal-footer {
    border-top-color: rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] .auth-modal-error {
    background: rgba(244, 67, 54, 0.12);
    border-color: rgba(244, 67, 54, 0.25);
}

[data-theme="dark"] .auth-modal-success {
    background: rgba(76, 175, 80, 0.12);
    border-color: rgba(76, 175, 80, 0.25);
}

[data-theme="dark"] .auth-modal-loading {
    background: rgba(22, 33, 62, 0.85);
}

[data-theme="dark"] .auth-modal-spinner {
    border-color: rgba(255, 255, 255, 0.1);
    border-top-color: var(--color-primary);
}

[data-theme="dark"] .auth-modal-checkbox {
    color: var(--text-secondary);
}

/* ===== Responsive: Mobile ===== */
@media (max-width: 480px) {
    .auth-modal-overlay {
        padding: var(--space-2);
        align-items: flex-end;
    }

    .auth-modal {
        max-width: 100%;
        max-height: 95vh;
        border-radius: var(--rounded-xl) var(--rounded-xl) 0 0;
        animation: authModalSlideUp 0.3s ease-out forwards;
    }

    .auth-modal.auth-modal--register {
        max-width: 100%;
    }

    @keyframes authModalSlideUp {
        from {
            opacity: 0;
            transform: translateY(100%);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .auth-modal-header-bar {
        border-radius: var(--rounded-xl) var(--rounded-xl) 0 0;
        padding: var(--space-2);
    }

    .auth-modal-view {
        padding: var(--space-3);
    }

    .auth-modal-form-row {
        grid-template-columns: 1fr;
    }

    .auth-modal-header {
        margin-bottom: var(--space-2);
    }

    .auth-modal-title {
        font-size: var(--text-base);
    }

    .auth-modal-btn-submit {
        padding: var(--space-3) var(--space-4);
    }
}

/* ===== Responsive: Small Desktop ===== */
@media (max-width: 640px) and (min-width: 481px) {
    .auth-modal {
        max-width: 420px;
    }

    .auth-modal.auth-modal--register {
        max-width: 480px;
    }

    .auth-modal-view {
        padding: var(--space-3) var(--space-4);
    }
}

/* ===== Accessibility: Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
    .auth-modal-overlay {
        transition: none;
    }

    .auth-modal {
        animation: none;
        transform: scale(1);
    }

    .auth-modal-view.active {
        animation: none;
    }

    .auth-modal-error,
    .auth-modal-success,
    .auth-modal-form .field-error {
        animation: none;
    }

    .auth-modal-close:active {
        transform: none;
    }

    .auth-modal-btn-submit:hover:not(:disabled),
    .auth-modal-btn-oauth:hover {
        transform: none;
    }
}

/* ===== Focus Visible (Keyboard Navigation) ===== */
.auth-modal-tab:focus-visible {
    outline: 2px solid var(--color-white);
    outline-offset: 2px;
}

.auth-modal-close:focus-visible {
    outline: 2px solid var(--color-white);
    outline-offset: 2px;
}

.auth-modal-btn-submit:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(var(--color-primary-rgb), 0.2);
}

.auth-modal-btn-oauth:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* ===== Body Scroll Lock ===== */
body.auth-modal-open {
    overflow: hidden;
}
